GCOVProfiling.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. //===- GCOVProfiling.cpp - Insert edge counters for gcov profiling --------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This pass implements GCOV-style profiling. When this pass is run it emits
  10. // "gcno" files next to the existing source, and instruments the code that runs
  11. // to records the edges between blocks that run and emit a complementary "gcda"
  12. // file on exit.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "CFGMST.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/Hashing.h"
  18. #include "llvm/ADT/MapVector.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/Sequence.h"
  21. #include "llvm/ADT/Statistic.h"
  22. #include "llvm/ADT/StringExtras.h"
  23. #include "llvm/ADT/StringMap.h"
  24. #include "llvm/Analysis/BlockFrequencyInfo.h"
  25. #include "llvm/Analysis/BranchProbabilityInfo.h"
  26. #include "llvm/Analysis/EHPersonalities.h"
  27. #include "llvm/Analysis/TargetLibraryInfo.h"
  28. #include "llvm/IR/CFG.h"
  29. #include "llvm/IR/DebugInfo.h"
  30. #include "llvm/IR/DebugLoc.h"
  31. #include "llvm/IR/IRBuilder.h"
  32. #include "llvm/IR/InstIterator.h"
  33. #include "llvm/IR/Instructions.h"
  34. #include "llvm/IR/IntrinsicInst.h"
  35. #include "llvm/IR/Module.h"
  36. #include "llvm/InitializePasses.h"
  37. #include "llvm/Pass.h"
  38. #include "llvm/Support/CRC.h"
  39. #include "llvm/Support/CommandLine.h"
  40. #include "llvm/Support/Debug.h"
  41. #include "llvm/Support/FileSystem.h"
  42. #include "llvm/Support/Path.h"
  43. #include "llvm/Support/Regex.h"
  44. #include "llvm/Support/raw_ostream.h"
  45. #include "llvm/Transforms/Instrumentation.h"
  46. #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
  47. #include "llvm/Transforms/Utils/ModuleUtils.h"
  48. #include <algorithm>
  49. #include <memory>
  50. #include <string>
  51. #include <utility>
  52. using namespace llvm;
  53. namespace endian = llvm::support::endian;
  54. #define DEBUG_TYPE "insert-gcov-profiling"
  55. enum : uint32_t {
  56. GCOV_ARC_ON_TREE = 1 << 0,
  57. GCOV_TAG_FUNCTION = 0x01000000,
  58. GCOV_TAG_BLOCKS = 0x01410000,
  59. GCOV_TAG_ARCS = 0x01430000,
  60. GCOV_TAG_LINES = 0x01450000,
  61. };
  62. static cl::opt<std::string> DefaultGCOVVersion("default-gcov-version",
  63. cl::init("408*"), cl::Hidden,
  64. cl::ValueRequired);
  65. static cl::opt<bool> AtomicCounter("gcov-atomic-counter", cl::Hidden,
  66. cl::desc("Make counter updates atomic"));
  67. // Returns the number of words which will be used to represent this string.
  68. static unsigned wordsOfString(StringRef s) {
  69. // Length + NUL-terminated string + 0~3 padding NULs.
  70. return (s.size() / 4) + 2;
  71. }
  72. GCOVOptions GCOVOptions::getDefault() {
  73. GCOVOptions Options;
  74. Options.EmitNotes = true;
  75. Options.EmitData = true;
  76. Options.NoRedZone = false;
  77. Options.Atomic = AtomicCounter;
  78. if (DefaultGCOVVersion.size() != 4) {
  79. llvm::report_fatal_error(Twine("Invalid -default-gcov-version: ") +
  80. DefaultGCOVVersion);
  81. }
  82. memcpy(Options.Version, DefaultGCOVVersion.c_str(), 4);
  83. return Options;
  84. }
  85. namespace {
  86. class GCOVFunction;
  87. class GCOVProfiler {
  88. public:
  89. GCOVProfiler() : GCOVProfiler(GCOVOptions::getDefault()) {}
  90. GCOVProfiler(const GCOVOptions &Opts) : Options(Opts) {}
  91. bool
  92. runOnModule(Module &M, function_ref<BlockFrequencyInfo *(Function &F)> GetBFI,
  93. function_ref<BranchProbabilityInfo *(Function &F)> GetBPI,
  94. std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
  95. void write(uint32_t i) {
  96. char Bytes[4];
  97. endian::write32(Bytes, i, Endian);
  98. os->write(Bytes, 4);
  99. }
  100. void writeString(StringRef s) {
  101. write(wordsOfString(s) - 1);
  102. os->write(s.data(), s.size());
  103. os->write_zeros(4 - s.size() % 4);
  104. }
  105. void writeBytes(const char *Bytes, int Size) { os->write(Bytes, Size); }
  106. private:
  107. // Create the .gcno files for the Module based on DebugInfo.
  108. bool
  109. emitProfileNotes(NamedMDNode *CUNode, bool HasExecOrFork,
  110. function_ref<BlockFrequencyInfo *(Function &F)> GetBFI,
  111. function_ref<BranchProbabilityInfo *(Function &F)> GetBPI,
  112. function_ref<const TargetLibraryInfo &(Function &F)> GetTLI);
  113. Function *createInternalFunction(FunctionType *FTy, StringRef Name);
  114. void emitGlobalConstructor(
  115. SmallVectorImpl<std::pair<GlobalVariable *, MDNode *>> &CountersBySP);
  116. bool isFunctionInstrumented(const Function &F);
  117. std::vector<Regex> createRegexesFromString(StringRef RegexesStr);
  118. static bool doesFilenameMatchARegex(StringRef Filename,
  119. std::vector<Regex> &Regexes);
  120. // Get pointers to the functions in the runtime library.
  121. FunctionCallee getStartFileFunc(const TargetLibraryInfo *TLI);
  122. FunctionCallee getEmitFunctionFunc(const TargetLibraryInfo *TLI);
  123. FunctionCallee getEmitArcsFunc(const TargetLibraryInfo *TLI);
  124. FunctionCallee getSummaryInfoFunc();
  125. FunctionCallee getEndFileFunc();
  126. // Add the function to write out all our counters to the global destructor
  127. // list.
  128. Function *
  129. insertCounterWriteout(ArrayRef<std::pair<GlobalVariable *, MDNode *>>);
  130. Function *insertReset(ArrayRef<std::pair<GlobalVariable *, MDNode *>>);
  131. bool AddFlushBeforeForkAndExec();
  132. enum class GCovFileType { GCNO, GCDA };
  133. std::string mangleName(const DICompileUnit *CU, GCovFileType FileType);
  134. GCOVOptions Options;
  135. support::endianness Endian;
  136. raw_ostream *os;
  137. // Checksum, produced by hash of EdgeDestinations
  138. SmallVector<uint32_t, 4> FileChecksums;
  139. Module *M = nullptr;
  140. std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
  141. LLVMContext *Ctx = nullptr;
  142. SmallVector<std::unique_ptr<GCOVFunction>, 16> Funcs;
  143. std::vector<Regex> FilterRe;
  144. std::vector<Regex> ExcludeRe;
  145. DenseSet<const BasicBlock *> ExecBlocks;
  146. StringMap<bool> InstrumentedFiles;
  147. };
  148. class GCOVProfilerLegacyPass : public ModulePass {
  149. public:
  150. static char ID;
  151. GCOVProfilerLegacyPass()
  152. : GCOVProfilerLegacyPass(GCOVOptions::getDefault()) {}
  153. GCOVProfilerLegacyPass(const GCOVOptions &Opts)
  154. : ModulePass(ID), Profiler(Opts) {
  155. initializeGCOVProfilerLegacyPassPass(*PassRegistry::getPassRegistry());
  156. }
  157. StringRef getPassName() const override { return "GCOV Profiler"; }
  158. bool runOnModule(Module &M) override {
  159. auto GetBFI = [this](Function &F) {
  160. return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI();
  161. };
  162. auto GetBPI = [this](Function &F) {
  163. return &this->getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI();
  164. };
  165. auto GetTLI = [this](Function &F) -> const TargetLibraryInfo & {
  166. return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  167. };
  168. return Profiler.runOnModule(M, GetBFI, GetBPI, GetTLI);
  169. }
  170. void getAnalysisUsage(AnalysisUsage &AU) const override {
  171. AU.addRequired<BlockFrequencyInfoWrapperPass>();
  172. AU.addRequired<TargetLibraryInfoWrapperPass>();
  173. }
  174. private:
  175. GCOVProfiler Profiler;
  176. };
  177. struct BBInfo {
  178. BBInfo *Group;
  179. uint32_t Index;
  180. uint32_t Rank = 0;
  181. BBInfo(unsigned Index) : Group(this), Index(Index) {}
  182. std::string infoString() const {
  183. return (Twine("Index=") + Twine(Index)).str();
  184. }
  185. };
  186. struct Edge {
  187. // This class implements the CFG edges. Note the CFG can be a multi-graph.
  188. // So there might be multiple edges with same SrcBB and DestBB.
  189. const BasicBlock *SrcBB;
  190. const BasicBlock *DestBB;
  191. uint64_t Weight;
  192. BasicBlock *Place = nullptr;
  193. uint32_t SrcNumber, DstNumber;
  194. bool InMST = false;
  195. bool Removed = false;
  196. bool IsCritical = false;
  197. Edge(const BasicBlock *Src, const BasicBlock *Dest, uint64_t W = 1)
  198. : SrcBB(Src), DestBB(Dest), Weight(W) {}
  199. // Return the information string of an edge.
  200. std::string infoString() const {
  201. return (Twine(Removed ? "-" : " ") + (InMST ? " " : "*") +
  202. (IsCritical ? "c" : " ") + " W=" + Twine(Weight))
  203. .str();
  204. }
  205. };
  206. }
  207. char GCOVProfilerLegacyPass::ID = 0;
  208. INITIALIZE_PASS_BEGIN(
  209. GCOVProfilerLegacyPass, "insert-gcov-profiling",
  210. "Insert instrumentation for GCOV profiling", false, false)
  211. INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
  212. INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
  213. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  214. INITIALIZE_PASS_END(
  215. GCOVProfilerLegacyPass, "insert-gcov-profiling",
  216. "Insert instrumentation for GCOV profiling", false, false)
  217. ModulePass *llvm::createGCOVProfilerPass(const GCOVOptions &Options) {
  218. return new GCOVProfilerLegacyPass(Options);
  219. }
  220. static StringRef getFunctionName(const DISubprogram *SP) {
  221. if (!SP->getLinkageName().empty())
  222. return SP->getLinkageName();
  223. return SP->getName();
  224. }
  225. /// Extract a filename for a DISubprogram.
  226. ///
  227. /// Prefer relative paths in the coverage notes. Clang also may split
  228. /// up absolute paths into a directory and filename component. When
  229. /// the relative path doesn't exist, reconstruct the absolute path.
  230. static SmallString<128> getFilename(const DISubprogram *SP) {
  231. SmallString<128> Path;
  232. StringRef RelPath = SP->getFilename();
  233. if (sys::fs::exists(RelPath))
  234. Path = RelPath;
  235. else
  236. sys::path::append(Path, SP->getDirectory(), SP->getFilename());
  237. return Path;
  238. }
  239. namespace {
  240. class GCOVRecord {
  241. protected:
  242. GCOVProfiler *P;
  243. GCOVRecord(GCOVProfiler *P) : P(P) {}
  244. void write(uint32_t i) { P->write(i); }
  245. void writeString(StringRef s) { P->writeString(s); }
  246. void writeBytes(const char *Bytes, int Size) { P->writeBytes(Bytes, Size); }
  247. };
  248. class GCOVFunction;
  249. class GCOVBlock;
  250. // Constructed only by requesting it from a GCOVBlock, this object stores a
  251. // list of line numbers and a single filename, representing lines that belong
  252. // to the block.
  253. class GCOVLines : public GCOVRecord {
  254. public:
  255. void addLine(uint32_t Line) {
  256. assert(Line != 0 && "Line zero is not a valid real line number.");
  257. Lines.push_back(Line);
  258. }
  259. uint32_t length() const {
  260. return 1 + wordsOfString(Filename) + Lines.size();
  261. }
  262. void writeOut() {
  263. write(0);
  264. writeString(Filename);
  265. for (int i = 0, e = Lines.size(); i != e; ++i)
  266. write(Lines[i]);
  267. }
  268. GCOVLines(GCOVProfiler *P, StringRef F)
  269. : GCOVRecord(P), Filename(std::string(F)) {}
  270. private:
  271. std::string Filename;
  272. SmallVector<uint32_t, 32> Lines;
  273. };
  274. // Represent a basic block in GCOV. Each block has a unique number in the
  275. // function, number of lines belonging to each block, and a set of edges to
  276. // other blocks.
  277. class GCOVBlock : public GCOVRecord {
  278. public:
  279. GCOVLines &getFile(StringRef Filename) {
  280. return LinesByFile.try_emplace(Filename, P, Filename).first->second;
  281. }
  282. void addEdge(GCOVBlock &Successor, uint32_t Flags) {
  283. OutEdges.emplace_back(&Successor, Flags);
  284. }
  285. void writeOut() {
  286. uint32_t Len = 3;
  287. SmallVector<StringMapEntry<GCOVLines> *, 32> SortedLinesByFile;
  288. for (auto &I : LinesByFile) {
  289. Len += I.second.length();
  290. SortedLinesByFile.push_back(&I);
  291. }
  292. write(GCOV_TAG_LINES);
  293. write(Len);
  294. write(Number);
  295. llvm::sort(SortedLinesByFile, [](StringMapEntry<GCOVLines> *LHS,
  296. StringMapEntry<GCOVLines> *RHS) {
  297. return LHS->getKey() < RHS->getKey();
  298. });
  299. for (auto &I : SortedLinesByFile)
  300. I->getValue().writeOut();
  301. write(0);
  302. write(0);
  303. }
  304. GCOVBlock(const GCOVBlock &RHS) : GCOVRecord(RHS), Number(RHS.Number) {
  305. // Only allow copy before edges and lines have been added. After that,
  306. // there are inter-block pointers (eg: edges) that won't take kindly to
  307. // blocks being copied or moved around.
  308. assert(LinesByFile.empty());
  309. assert(OutEdges.empty());
  310. }
  311. uint32_t Number;
  312. SmallVector<std::pair<GCOVBlock *, uint32_t>, 4> OutEdges;
  313. private:
  314. friend class GCOVFunction;
  315. GCOVBlock(GCOVProfiler *P, uint32_t Number)
  316. : GCOVRecord(P), Number(Number) {}
  317. StringMap<GCOVLines> LinesByFile;
  318. };
  319. // A function has a unique identifier, a checksum (we leave as zero) and a
  320. // set of blocks and a map of edges between blocks. This is the only GCOV
  321. // object users can construct, the blocks and lines will be rooted here.
  322. class GCOVFunction : public GCOVRecord {
  323. public:
  324. GCOVFunction(GCOVProfiler *P, Function *F, const DISubprogram *SP,
  325. unsigned EndLine, uint32_t Ident, int Version)
  326. : GCOVRecord(P), SP(SP), EndLine(EndLine), Ident(Ident),
  327. Version(Version), EntryBlock(P, 0), ReturnBlock(P, 1) {
  328. LLVM_DEBUG(dbgs() << "Function: " << getFunctionName(SP) << "\n");
  329. bool ExitBlockBeforeBody = Version >= 48;
  330. uint32_t i = ExitBlockBeforeBody ? 2 : 1;
  331. for (BasicBlock &BB : *F)
  332. Blocks.insert(std::make_pair(&BB, GCOVBlock(P, i++)));
  333. if (!ExitBlockBeforeBody)
  334. ReturnBlock.Number = i;
  335. std::string FunctionNameAndLine;
  336. raw_string_ostream FNLOS(FunctionNameAndLine);
  337. FNLOS << getFunctionName(SP) << SP->getLine();
  338. FNLOS.flush();
  339. FuncChecksum = hash_value(FunctionNameAndLine);
  340. }
  341. GCOVBlock &getBlock(const BasicBlock *BB) {
  342. return Blocks.find(const_cast<BasicBlock *>(BB))->second;
  343. }
  344. GCOVBlock &getEntryBlock() { return EntryBlock; }
  345. GCOVBlock &getReturnBlock() {
  346. return ReturnBlock;
  347. }
  348. uint32_t getFuncChecksum() const {
  349. return FuncChecksum;
  350. }
  351. void writeOut(uint32_t CfgChecksum) {
  352. write(GCOV_TAG_FUNCTION);
  353. SmallString<128> Filename = getFilename(SP);
  354. uint32_t BlockLen =
  355. 2 + (Version >= 47) + wordsOfString(getFunctionName(SP));
  356. if (Version < 80)
  357. BlockLen += wordsOfString(Filename) + 1;
  358. else
  359. BlockLen += 1 + wordsOfString(Filename) + 3 + (Version >= 90);
  360. write(BlockLen);
  361. write(Ident);
  362. write(FuncChecksum);
  363. if (Version >= 47)
  364. write(CfgChecksum);
  365. writeString(getFunctionName(SP));
  366. if (Version < 80) {
  367. writeString(Filename);
  368. write(SP->getLine());
  369. } else {
  370. write(SP->isArtificial()); // artificial
  371. writeString(Filename);
  372. write(SP->getLine()); // start_line
  373. write(0); // start_column
  374. // EndLine is the last line with !dbg. It is not the } line as in GCC,
  375. // but good enough.
  376. write(EndLine);
  377. if (Version >= 90)
  378. write(0); // end_column
  379. }
  380. // Emit count of blocks.
  381. write(GCOV_TAG_BLOCKS);
  382. if (Version < 80) {
  383. write(Blocks.size() + 2);
  384. for (int i = Blocks.size() + 2; i; --i)
  385. write(0);
  386. } else {
  387. write(1);
  388. write(Blocks.size() + 2);
  389. }
  390. LLVM_DEBUG(dbgs() << (Blocks.size() + 1) << " blocks\n");
  391. // Emit edges between blocks.
  392. const uint32_t Outgoing = EntryBlock.OutEdges.size();
  393. if (Outgoing) {
  394. write(GCOV_TAG_ARCS);
  395. write(Outgoing * 2 + 1);
  396. write(EntryBlock.Number);
  397. for (const auto &E : EntryBlock.OutEdges) {
  398. write(E.first->Number);
  399. write(E.second);
  400. }
  401. }
  402. for (auto &It : Blocks) {
  403. const GCOVBlock &Block = It.second;
  404. if (Block.OutEdges.empty()) continue;
  405. write(GCOV_TAG_ARCS);
  406. write(Block.OutEdges.size() * 2 + 1);
  407. write(Block.Number);
  408. for (const auto &E : Block.OutEdges) {
  409. write(E.first->Number);
  410. write(E.second);
  411. }
  412. }
  413. // Emit lines for each block.
  414. for (auto &It : Blocks)
  415. It.second.writeOut();
  416. }
  417. public:
  418. const DISubprogram *SP;
  419. unsigned EndLine;
  420. uint32_t Ident;
  421. uint32_t FuncChecksum;
  422. int Version;
  423. MapVector<BasicBlock *, GCOVBlock> Blocks;
  424. GCOVBlock EntryBlock;
  425. GCOVBlock ReturnBlock;
  426. };
  427. }
  428. // RegexesStr is a string containing differents regex separated by a semi-colon.
  429. // For example "foo\..*$;bar\..*$".
  430. std::vector<Regex> GCOVProfiler::createRegexesFromString(StringRef RegexesStr) {
  431. std::vector<Regex> Regexes;
  432. while (!RegexesStr.empty()) {
  433. std::pair<StringRef, StringRef> HeadTail = RegexesStr.split(';');
  434. if (!HeadTail.first.empty()) {
  435. Regex Re(HeadTail.first);
  436. std::string Err;
  437. if (!Re.isValid(Err)) {
  438. Ctx->emitError(Twine("Regex ") + HeadTail.first +
  439. " is not valid: " + Err);
  440. }
  441. Regexes.emplace_back(std::move(Re));
  442. }
  443. RegexesStr = HeadTail.second;
  444. }
  445. return Regexes;
  446. }
  447. bool GCOVProfiler::doesFilenameMatchARegex(StringRef Filename,
  448. std::vector<Regex> &Regexes) {
  449. for (Regex &Re : Regexes)
  450. if (Re.match(Filename))
  451. return true;
  452. return false;
  453. }
  454. bool GCOVProfiler::isFunctionInstrumented(const Function &F) {
  455. if (FilterRe.empty() && ExcludeRe.empty()) {
  456. return true;
  457. }
  458. SmallString<128> Filename = getFilename(F.getSubprogram());
  459. auto It = InstrumentedFiles.find(Filename);
  460. if (It != InstrumentedFiles.end()) {
  461. return It->second;
  462. }
  463. SmallString<256> RealPath;
  464. StringRef RealFilename;
  465. // Path can be
  466. // /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/*.h so for
  467. // such a case we must get the real_path.
  468. if (sys::fs::real_path(Filename, RealPath)) {
  469. // real_path can fail with path like "foo.c".
  470. RealFilename = Filename;
  471. } else {
  472. RealFilename = RealPath;
  473. }
  474. bool ShouldInstrument;
  475. if (FilterRe.empty()) {
  476. ShouldInstrument = !doesFilenameMatchARegex(RealFilename, ExcludeRe);
  477. } else if (ExcludeRe.empty()) {
  478. ShouldInstrument = doesFilenameMatchARegex(RealFilename, FilterRe);
  479. } else {
  480. ShouldInstrument = doesFilenameMatchARegex(RealFilename, FilterRe) &&
  481. !doesFilenameMatchARegex(RealFilename, ExcludeRe);
  482. }
  483. InstrumentedFiles[Filename] = ShouldInstrument;
  484. return ShouldInstrument;
  485. }
  486. std::string GCOVProfiler::mangleName(const DICompileUnit *CU,
  487. GCovFileType OutputType) {
  488. bool Notes = OutputType == GCovFileType::GCNO;
  489. if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
  490. for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
  491. MDNode *N = GCov->getOperand(i);
  492. bool ThreeElement = N->getNumOperands() == 3;
  493. if (!ThreeElement && N->getNumOperands() != 2)
  494. continue;
  495. if (dyn_cast<MDNode>(N->getOperand(ThreeElement ? 2 : 1)) != CU)
  496. continue;
  497. if (ThreeElement) {
  498. // These nodes have no mangling to apply, it's stored mangled in the
  499. // bitcode.
  500. MDString *NotesFile = dyn_cast<MDString>(N->getOperand(0));
  501. MDString *DataFile = dyn_cast<MDString>(N->getOperand(1));
  502. if (!NotesFile || !DataFile)
  503. continue;
  504. return std::string(Notes ? NotesFile->getString()
  505. : DataFile->getString());
  506. }
  507. MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0));
  508. if (!GCovFile)
  509. continue;
  510. SmallString<128> Filename = GCovFile->getString();
  511. sys::path::replace_extension(Filename, Notes ? "gcno" : "gcda");
  512. return std::string(Filename.str());
  513. }
  514. }
  515. SmallString<128> Filename = CU->getFilename();
  516. sys::path::replace_extension(Filename, Notes ? "gcno" : "gcda");
  517. StringRef FName = sys::path::filename(Filename);
  518. SmallString<128> CurPath;
  519. if (sys::fs::current_path(CurPath))
  520. return std::string(FName);
  521. sys::path::append(CurPath, FName);
  522. return std::string(CurPath.str());
  523. }
  524. bool GCOVProfiler::runOnModule(
  525. Module &M, function_ref<BlockFrequencyInfo *(Function &F)> GetBFI,
  526. function_ref<BranchProbabilityInfo *(Function &F)> GetBPI,
  527. std::function<const TargetLibraryInfo &(Function &F)> GetTLI) {
  528. this->M = &M;
  529. this->GetTLI = std::move(GetTLI);
  530. Ctx = &M.getContext();
  531. NamedMDNode *CUNode = M.getNamedMetadata("llvm.dbg.cu");
  532. if (!CUNode || (!Options.EmitNotes && !Options.EmitData))
  533. return false;
  534. bool HasExecOrFork = AddFlushBeforeForkAndExec();
  535. FilterRe = createRegexesFromString(Options.Filter);
  536. ExcludeRe = createRegexesFromString(Options.Exclude);
  537. emitProfileNotes(CUNode, HasExecOrFork, GetBFI, GetBPI, this->GetTLI);
  538. return true;
  539. }
  540. PreservedAnalyses GCOVProfilerPass::run(Module &M,
  541. ModuleAnalysisManager &AM) {
  542. GCOVProfiler Profiler(GCOVOpts);
  543. FunctionAnalysisManager &FAM =
  544. AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  545. auto GetBFI = [&FAM](Function &F) {
  546. return &FAM.getResult<BlockFrequencyAnalysis>(F);
  547. };
  548. auto GetBPI = [&FAM](Function &F) {
  549. return &FAM.getResult<BranchProbabilityAnalysis>(F);
  550. };
  551. auto GetTLI = [&FAM](Function &F) -> const TargetLibraryInfo & {
  552. return FAM.getResult<TargetLibraryAnalysis>(F);
  553. };
  554. if (!Profiler.runOnModule(M, GetBFI, GetBPI, GetTLI))
  555. return PreservedAnalyses::all();
  556. return PreservedAnalyses::none();
  557. }
  558. static bool functionHasLines(const Function &F, unsigned &EndLine) {
  559. // Check whether this function actually has any source lines. Not only
  560. // do these waste space, they also can crash gcov.
  561. EndLine = 0;
  562. for (auto &BB : F) {
  563. for (auto &I : BB) {
  564. // Debug intrinsic locations correspond to the location of the
  565. // declaration, not necessarily any statements or expressions.
  566. if (isa<DbgInfoIntrinsic>(&I)) continue;
  567. const DebugLoc &Loc = I.getDebugLoc();
  568. if (!Loc)
  569. continue;
  570. // Artificial lines such as calls to the global constructors.
  571. if (Loc.getLine() == 0) continue;
  572. EndLine = std::max(EndLine, Loc.getLine());
  573. return true;
  574. }
  575. }
  576. return false;
  577. }
  578. static bool isUsingScopeBasedEH(Function &F) {
  579. if (!F.hasPersonalityFn()) return false;
  580. EHPersonality Personality = classifyEHPersonality(F.getPersonalityFn());
  581. return isScopedEHPersonality(Personality);
  582. }
  583. bool GCOVProfiler::AddFlushBeforeForkAndExec() {
  584. SmallVector<CallInst *, 2> Forks;
  585. SmallVector<CallInst *, 2> Execs;
  586. for (auto &F : M->functions()) {
  587. auto *TLI = &GetTLI(F);
  588. for (auto &I : instructions(F)) {
  589. if (CallInst *CI = dyn_cast<CallInst>(&I)) {
  590. if (Function *Callee = CI->getCalledFunction()) {
  591. LibFunc LF;
  592. if (TLI->getLibFunc(*Callee, LF)) {
  593. if (LF == LibFunc_fork) {
  594. #if !defined(_WIN32)
  595. Forks.push_back(CI);
  596. #endif
  597. } else if (LF == LibFunc_execl || LF == LibFunc_execle ||
  598. LF == LibFunc_execlp || LF == LibFunc_execv ||
  599. LF == LibFunc_execvp || LF == LibFunc_execve ||
  600. LF == LibFunc_execvpe || LF == LibFunc_execvP) {
  601. Execs.push_back(CI);
  602. }
  603. }
  604. }
  605. }
  606. }
  607. }
  608. for (auto F : Forks) {
  609. IRBuilder<> Builder(F);
  610. BasicBlock *Parent = F->getParent();
  611. auto NextInst = ++F->getIterator();
  612. // We've a fork so just reset the counters in the child process
  613. FunctionType *FTy = FunctionType::get(Builder.getInt32Ty(), {}, false);
  614. FunctionCallee GCOVFork = M->getOrInsertFunction("__gcov_fork", FTy);
  615. F->setCalledFunction(GCOVFork);
  616. // We split just after the fork to have a counter for the lines after
  617. // Anyway there's a bug:
  618. // void foo() { fork(); }
  619. // void bar() { foo(); blah(); }
  620. // then "blah();" will be called 2 times but showed as 1
  621. // because "blah()" belongs to the same block as "foo();"
  622. Parent->splitBasicBlock(NextInst);
  623. // back() is a br instruction with a debug location
  624. // equals to the one from NextAfterFork
  625. // So to avoid to have two debug locs on two blocks just change it
  626. DebugLoc Loc = F->getDebugLoc();
  627. Parent->back().setDebugLoc(Loc);
  628. }
  629. for (auto E : Execs) {
  630. IRBuilder<> Builder(E);
  631. BasicBlock *Parent = E->getParent();
  632. auto NextInst = ++E->getIterator();
  633. // Since the process is replaced by a new one we need to write out gcdas
  634. // No need to reset the counters since they'll be lost after the exec**
  635. FunctionType *FTy = FunctionType::get(Builder.getVoidTy(), {}, false);
  636. FunctionCallee WriteoutF =
  637. M->getOrInsertFunction("llvm_writeout_files", FTy);
  638. Builder.CreateCall(WriteoutF);
  639. DebugLoc Loc = E->getDebugLoc();
  640. Builder.SetInsertPoint(&*NextInst);
  641. // If the exec** fails we must reset the counters since they've been
  642. // dumped
  643. FunctionCallee ResetF = M->getOrInsertFunction("llvm_reset_counters", FTy);
  644. Builder.CreateCall(ResetF)->setDebugLoc(Loc);
  645. ExecBlocks.insert(Parent);
  646. Parent->splitBasicBlock(NextInst);
  647. Parent->back().setDebugLoc(Loc);
  648. }
  649. return !Forks.empty() || !Execs.empty();
  650. }
  651. static BasicBlock *getInstrBB(CFGMST<Edge, BBInfo> &MST, Edge &E,
  652. const DenseSet<const BasicBlock *> &ExecBlocks) {
  653. if (E.InMST || E.Removed)
  654. return nullptr;
  655. BasicBlock *SrcBB = const_cast<BasicBlock *>(E.SrcBB);
  656. BasicBlock *DestBB = const_cast<BasicBlock *>(E.DestBB);
  657. // For a fake edge, instrument the real BB.
  658. if (SrcBB == nullptr)
  659. return DestBB;
  660. if (DestBB == nullptr)
  661. return SrcBB;
  662. auto CanInstrument = [](BasicBlock *BB) -> BasicBlock * {
  663. // There are basic blocks (such as catchswitch) cannot be instrumented.
  664. // If the returned first insertion point is the end of BB, skip this BB.
  665. if (BB->getFirstInsertionPt() == BB->end())
  666. return nullptr;
  667. return BB;
  668. };
  669. // Instrument the SrcBB if it has a single successor,
  670. // otherwise, the DestBB if this is not a critical edge.
  671. Instruction *TI = SrcBB->getTerminator();
  672. if (TI->getNumSuccessors() <= 1 && !ExecBlocks.count(SrcBB))
  673. return CanInstrument(SrcBB);
  674. if (!E.IsCritical)
  675. return CanInstrument(DestBB);
  676. // Some IndirectBr critical edges cannot be split by the previous
  677. // SplitIndirectBrCriticalEdges call. Bail out.
  678. const unsigned SuccNum = GetSuccessorNumber(SrcBB, DestBB);
  679. BasicBlock *InstrBB =
  680. isa<IndirectBrInst>(TI) ? nullptr : SplitCriticalEdge(TI, SuccNum);
  681. if (!InstrBB)
  682. return nullptr;
  683. MST.addEdge(SrcBB, InstrBB, 0);
  684. MST.addEdge(InstrBB, DestBB, 0).InMST = true;
  685. E.Removed = true;
  686. return CanInstrument(InstrBB);
  687. }
  688. #ifndef NDEBUG
  689. static void dumpEdges(CFGMST<Edge, BBInfo> &MST, GCOVFunction &GF) {
  690. size_t ID = 0;
  691. for (auto &E : make_pointee_range(MST.AllEdges)) {
  692. GCOVBlock &Src = E.SrcBB ? GF.getBlock(E.SrcBB) : GF.getEntryBlock();
  693. GCOVBlock &Dst = E.DestBB ? GF.getBlock(E.DestBB) : GF.getReturnBlock();
  694. dbgs() << " Edge " << ID++ << ": " << Src.Number << "->" << Dst.Number
  695. << E.infoString() << "\n";
  696. }
  697. }
  698. #endif
  699. bool GCOVProfiler::emitProfileNotes(
  700. NamedMDNode *CUNode, bool HasExecOrFork,
  701. function_ref<BlockFrequencyInfo *(Function &F)> GetBFI,
  702. function_ref<BranchProbabilityInfo *(Function &F)> GetBPI,
  703. function_ref<const TargetLibraryInfo &(Function &F)> GetTLI) {
  704. int Version;
  705. {
  706. uint8_t c3 = Options.Version[0];
  707. uint8_t c2 = Options.Version[1];
  708. uint8_t c1 = Options.Version[2];
  709. Version = c3 >= 'A' ? (c3 - 'A') * 100 + (c2 - '0') * 10 + c1 - '0'
  710. : (c3 - '0') * 10 + c1 - '0';
  711. }
  712. bool EmitGCDA = Options.EmitData;
  713. for (unsigned i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
  714. // Each compile unit gets its own .gcno file. This means that whether we run
  715. // this pass over the original .o's as they're produced, or run it after
  716. // LTO, we'll generate the same .gcno files.
  717. auto *CU = cast<DICompileUnit>(CUNode->getOperand(i));
  718. // Skip module skeleton (and module) CUs.
  719. if (CU->getDWOId())
  720. continue;
  721. std::vector<uint8_t> EdgeDestinations;
  722. SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP;
  723. Endian = M->getDataLayout().isLittleEndian() ? support::endianness::little
  724. : support::endianness::big;
  725. unsigned FunctionIdent = 0;
  726. for (auto &F : M->functions()) {
  727. DISubprogram *SP = F.getSubprogram();
  728. unsigned EndLine;
  729. if (!SP) continue;
  730. if (!functionHasLines(F, EndLine) || !isFunctionInstrumented(F))
  731. continue;
  732. // TODO: Functions using scope-based EH are currently not supported.
  733. if (isUsingScopeBasedEH(F)) continue;
  734. if (F.hasFnAttribute(llvm::Attribute::NoProfile))
  735. continue;
  736. // Add the function line number to the lines of the entry block
  737. // to have a counter for the function definition.
  738. uint32_t Line = SP->getLine();
  739. auto Filename = getFilename(SP);
  740. BranchProbabilityInfo *BPI = GetBPI(F);
  741. BlockFrequencyInfo *BFI = GetBFI(F);
  742. // Split indirectbr critical edges here before computing the MST rather
  743. // than later in getInstrBB() to avoid invalidating it.
  744. SplitIndirectBrCriticalEdges(F, BPI, BFI);
  745. CFGMST<Edge, BBInfo> MST(F, /*InstrumentFuncEntry_=*/false, BPI, BFI);
  746. // getInstrBB can split basic blocks and push elements to AllEdges.
  747. for (size_t I : llvm::seq<size_t>(0, MST.AllEdges.size())) {
  748. auto &E = *MST.AllEdges[I];
  749. // For now, disable spanning tree optimization when fork or exec* is
  750. // used.
  751. if (HasExecOrFork)
  752. E.InMST = false;
  753. E.Place = getInstrBB(MST, E, ExecBlocks);
  754. }
  755. // Basic blocks in F are finalized at this point.
  756. BasicBlock &EntryBlock = F.getEntryBlock();
  757. Funcs.push_back(std::make_unique<GCOVFunction>(this, &F, SP, EndLine,
  758. FunctionIdent++, Version));
  759. GCOVFunction &Func = *Funcs.back();
  760. // Some non-tree edges are IndirectBr which cannot be split. Ignore them
  761. // as well.
  762. llvm::erase_if(MST.AllEdges, [](std::unique_ptr<Edge> &E) {
  763. return E->Removed || (!E->InMST && !E->Place);
  764. });
  765. const size_t Measured =
  766. std::stable_partition(
  767. MST.AllEdges.begin(), MST.AllEdges.end(),
  768. [](std::unique_ptr<Edge> &E) { return E->Place; }) -
  769. MST.AllEdges.begin();
  770. for (size_t I : llvm::seq<size_t>(0, Measured)) {
  771. Edge &E = *MST.AllEdges[I];
  772. GCOVBlock &Src =
  773. E.SrcBB ? Func.getBlock(E.SrcBB) : Func.getEntryBlock();
  774. GCOVBlock &Dst =
  775. E.DestBB ? Func.getBlock(E.DestBB) : Func.getReturnBlock();
  776. E.SrcNumber = Src.Number;
  777. E.DstNumber = Dst.Number;
  778. }
  779. std::stable_sort(
  780. MST.AllEdges.begin(), MST.AllEdges.begin() + Measured,
  781. [](const std::unique_ptr<Edge> &L, const std::unique_ptr<Edge> &R) {
  782. return L->SrcNumber != R->SrcNumber ? L->SrcNumber < R->SrcNumber
  783. : L->DstNumber < R->DstNumber;
  784. });
  785. for (const Edge &E : make_pointee_range(MST.AllEdges)) {
  786. GCOVBlock &Src =
  787. E.SrcBB ? Func.getBlock(E.SrcBB) : Func.getEntryBlock();
  788. GCOVBlock &Dst =
  789. E.DestBB ? Func.getBlock(E.DestBB) : Func.getReturnBlock();
  790. Src.addEdge(Dst, E.Place ? 0 : uint32_t(GCOV_ARC_ON_TREE));
  791. }
  792. // Artificial functions such as global initializers
  793. if (!SP->isArtificial())
  794. Func.getBlock(&EntryBlock).getFile(Filename).addLine(Line);
  795. LLVM_DEBUG(dumpEdges(MST, Func));
  796. for (auto &GB : Func.Blocks) {
  797. const BasicBlock &BB = *GB.first;
  798. auto &Block = GB.second;
  799. for (auto Succ : Block.OutEdges) {
  800. uint32_t Idx = Succ.first->Number;
  801. do EdgeDestinations.push_back(Idx & 255);
  802. while ((Idx >>= 8) > 0);
  803. }
  804. for (auto &I : BB) {
  805. // Debug intrinsic locations correspond to the location of the
  806. // declaration, not necessarily any statements or expressions.
  807. if (isa<DbgInfoIntrinsic>(&I)) continue;
  808. const DebugLoc &Loc = I.getDebugLoc();
  809. if (!Loc)
  810. continue;
  811. // Artificial lines such as calls to the global constructors.
  812. if (Loc.getLine() == 0 || Loc.isImplicitCode())
  813. continue;
  814. if (Line == Loc.getLine()) continue;
  815. Line = Loc.getLine();
  816. if (SP != getDISubprogram(Loc.getScope()))
  817. continue;
  818. GCOVLines &Lines = Block.getFile(Filename);
  819. Lines.addLine(Loc.getLine());
  820. }
  821. Line = 0;
  822. }
  823. if (EmitGCDA) {
  824. DISubprogram *SP = F.getSubprogram();
  825. ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(*Ctx), Measured);
  826. GlobalVariable *Counters = new GlobalVariable(
  827. *M, CounterTy, false, GlobalValue::InternalLinkage,
  828. Constant::getNullValue(CounterTy), "__llvm_gcov_ctr");
  829. CountersBySP.emplace_back(Counters, SP);
  830. for (size_t I : llvm::seq<size_t>(0, Measured)) {
  831. const Edge &E = *MST.AllEdges[I];
  832. IRBuilder<> Builder(E.Place, E.Place->getFirstInsertionPt());
  833. Value *V = Builder.CreateConstInBoundsGEP2_64(
  834. Counters->getValueType(), Counters, 0, I);
  835. if (Options.Atomic) {
  836. Builder.CreateAtomicRMW(AtomicRMWInst::Add, V, Builder.getInt64(1),
  837. MaybeAlign(), AtomicOrdering::Monotonic);
  838. } else {
  839. Value *Count =
  840. Builder.CreateLoad(Builder.getInt64Ty(), V, "gcov_ctr");
  841. Count = Builder.CreateAdd(Count, Builder.getInt64(1));
  842. Builder.CreateStore(Count, V);
  843. }
  844. }
  845. }
  846. }
  847. char Tmp[4];
  848. JamCRC JC;
  849. JC.update(EdgeDestinations);
  850. uint32_t Stamp = JC.getCRC();
  851. FileChecksums.push_back(Stamp);
  852. if (Options.EmitNotes) {
  853. std::error_code EC;
  854. raw_fd_ostream out(mangleName(CU, GCovFileType::GCNO), EC,
  855. sys::fs::OF_None);
  856. if (EC) {
  857. Ctx->emitError(
  858. Twine("failed to open coverage notes file for writing: ") +
  859. EC.message());
  860. continue;
  861. }
  862. os = &out;
  863. if (Endian == support::endianness::big) {
  864. out.write("gcno", 4);
  865. out.write(Options.Version, 4);
  866. } else {
  867. out.write("oncg", 4);
  868. std::reverse_copy(Options.Version, Options.Version + 4, Tmp);
  869. out.write(Tmp, 4);
  870. }
  871. write(Stamp);
  872. if (Version >= 90)
  873. writeString(""); // unuseful current_working_directory
  874. if (Version >= 80)
  875. write(0); // unuseful has_unexecuted_blocks
  876. for (auto &Func : Funcs)
  877. Func->writeOut(Stamp);
  878. write(0);
  879. write(0);
  880. out.close();
  881. }
  882. if (EmitGCDA) {
  883. emitGlobalConstructor(CountersBySP);
  884. EmitGCDA = false;
  885. }
  886. }
  887. return true;
  888. }
  889. Function *GCOVProfiler::createInternalFunction(FunctionType *FTy,
  890. StringRef Name) {
  891. Function *F = Function::createWithDefaultAttr(
  892. FTy, GlobalValue::InternalLinkage, 0, Name, M);
  893. F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
  894. F->addFnAttr(Attribute::NoUnwind);
  895. if (Options.NoRedZone)
  896. F->addFnAttr(Attribute::NoRedZone);
  897. return F;
  898. }
  899. void GCOVProfiler::emitGlobalConstructor(
  900. SmallVectorImpl<std::pair<GlobalVariable *, MDNode *>> &CountersBySP) {
  901. Function *WriteoutF = insertCounterWriteout(CountersBySP);
  902. Function *ResetF = insertReset(CountersBySP);
  903. // Create a small bit of code that registers the "__llvm_gcov_writeout" to
  904. // be executed at exit and the "__llvm_gcov_reset" function to be executed
  905. // when "__gcov_flush" is called.
  906. FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
  907. Function *F = createInternalFunction(FTy, "__llvm_gcov_init");
  908. F->addFnAttr(Attribute::NoInline);
  909. BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
  910. IRBuilder<> Builder(BB);
  911. FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
  912. auto *PFTy = PointerType::get(FTy, 0);
  913. FTy = FunctionType::get(Builder.getVoidTy(), {PFTy, PFTy}, false);
  914. // Initialize the environment and register the local writeout, flush and
  915. // reset functions.
  916. FunctionCallee GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy);
  917. Builder.CreateCall(GCOVInit, {WriteoutF, ResetF});
  918. Builder.CreateRetVoid();
  919. appendToGlobalCtors(*M, F, 0);
  920. }
  921. FunctionCallee GCOVProfiler::getStartFileFunc(const TargetLibraryInfo *TLI) {
  922. Type *Args[] = {
  923. Type::getInt8PtrTy(*Ctx), // const char *orig_filename
  924. Type::getInt32Ty(*Ctx), // uint32_t version
  925. Type::getInt32Ty(*Ctx), // uint32_t checksum
  926. };
  927. FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
  928. AttributeList AL;
  929. if (auto AK = TLI->getExtAttrForI32Param(false))
  930. AL = AL.addParamAttribute(*Ctx, 2, AK);
  931. FunctionCallee Res = M->getOrInsertFunction("llvm_gcda_start_file", FTy, AL);
  932. return Res;
  933. }
  934. FunctionCallee GCOVProfiler::getEmitFunctionFunc(const TargetLibraryInfo *TLI) {
  935. Type *Args[] = {
  936. Type::getInt32Ty(*Ctx), // uint32_t ident
  937. Type::getInt32Ty(*Ctx), // uint32_t func_checksum
  938. Type::getInt32Ty(*Ctx), // uint32_t cfg_checksum
  939. };
  940. FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
  941. AttributeList AL;
  942. if (auto AK = TLI->getExtAttrForI32Param(false)) {
  943. AL = AL.addParamAttribute(*Ctx, 0, AK);
  944. AL = AL.addParamAttribute(*Ctx, 1, AK);
  945. AL = AL.addParamAttribute(*Ctx, 2, AK);
  946. }
  947. return M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
  948. }
  949. FunctionCallee GCOVProfiler::getEmitArcsFunc(const TargetLibraryInfo *TLI) {
  950. Type *Args[] = {
  951. Type::getInt32Ty(*Ctx), // uint32_t num_counters
  952. Type::getInt64PtrTy(*Ctx), // uint64_t *counters
  953. };
  954. FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
  955. AttributeList AL;
  956. if (auto AK = TLI->getExtAttrForI32Param(false))
  957. AL = AL.addParamAttribute(*Ctx, 0, AK);
  958. return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy, AL);
  959. }
  960. FunctionCallee GCOVProfiler::getSummaryInfoFunc() {
  961. FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
  962. return M->getOrInsertFunction("llvm_gcda_summary_info", FTy);
  963. }
  964. FunctionCallee GCOVProfiler::getEndFileFunc() {
  965. FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
  966. return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
  967. }
  968. Function *GCOVProfiler::insertCounterWriteout(
  969. ArrayRef<std::pair<GlobalVariable *, MDNode *> > CountersBySP) {
  970. FunctionType *WriteoutFTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
  971. Function *WriteoutF = M->getFunction("__llvm_gcov_writeout");
  972. if (!WriteoutF)
  973. WriteoutF = createInternalFunction(WriteoutFTy, "__llvm_gcov_writeout");
  974. WriteoutF->addFnAttr(Attribute::NoInline);
  975. BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF);
  976. IRBuilder<> Builder(BB);
  977. auto *TLI = &GetTLI(*WriteoutF);
  978. FunctionCallee StartFile = getStartFileFunc(TLI);
  979. FunctionCallee EmitFunction = getEmitFunctionFunc(TLI);
  980. FunctionCallee EmitArcs = getEmitArcsFunc(TLI);
  981. FunctionCallee SummaryInfo = getSummaryInfoFunc();
  982. FunctionCallee EndFile = getEndFileFunc();
  983. NamedMDNode *CUNodes = M->getNamedMetadata("llvm.dbg.cu");
  984. if (!CUNodes) {
  985. Builder.CreateRetVoid();
  986. return WriteoutF;
  987. }
  988. // Collect the relevant data into a large constant data structure that we can
  989. // walk to write out everything.
  990. StructType *StartFileCallArgsTy = StructType::create(
  991. {Builder.getInt8PtrTy(), Builder.getInt32Ty(), Builder.getInt32Ty()},
  992. "start_file_args_ty");
  993. StructType *EmitFunctionCallArgsTy = StructType::create(
  994. {Builder.getInt32Ty(), Builder.getInt32Ty(), Builder.getInt32Ty()},
  995. "emit_function_args_ty");
  996. StructType *EmitArcsCallArgsTy = StructType::create(
  997. {Builder.getInt32Ty(), Builder.getInt64Ty()->getPointerTo()},
  998. "emit_arcs_args_ty");
  999. StructType *FileInfoTy =
  1000. StructType::create({StartFileCallArgsTy, Builder.getInt32Ty(),
  1001. EmitFunctionCallArgsTy->getPointerTo(),
  1002. EmitArcsCallArgsTy->getPointerTo()},
  1003. "file_info");
  1004. Constant *Zero32 = Builder.getInt32(0);
  1005. // Build an explicit array of two zeros for use in ConstantExpr GEP building.
  1006. Constant *TwoZero32s[] = {Zero32, Zero32};
  1007. SmallVector<Constant *, 8> FileInfos;
  1008. for (int i : llvm::seq<int>(0, CUNodes->getNumOperands())) {
  1009. auto *CU = cast<DICompileUnit>(CUNodes->getOperand(i));
  1010. // Skip module skeleton (and module) CUs.
  1011. if (CU->getDWOId())
  1012. continue;
  1013. std::string FilenameGcda = mangleName(CU, GCovFileType::GCDA);
  1014. uint32_t CfgChecksum = FileChecksums.empty() ? 0 : FileChecksums[i];
  1015. auto *StartFileCallArgs = ConstantStruct::get(
  1016. StartFileCallArgsTy,
  1017. {Builder.CreateGlobalStringPtr(FilenameGcda),
  1018. Builder.getInt32(endian::read32be(Options.Version)),
  1019. Builder.getInt32(CfgChecksum)});
  1020. SmallVector<Constant *, 8> EmitFunctionCallArgsArray;
  1021. SmallVector<Constant *, 8> EmitArcsCallArgsArray;
  1022. for (int j : llvm::seq<int>(0, CountersBySP.size())) {
  1023. uint32_t FuncChecksum = Funcs.empty() ? 0 : Funcs[j]->getFuncChecksum();
  1024. EmitFunctionCallArgsArray.push_back(ConstantStruct::get(
  1025. EmitFunctionCallArgsTy,
  1026. {Builder.getInt32(j),
  1027. Builder.getInt32(FuncChecksum),
  1028. Builder.getInt32(CfgChecksum)}));
  1029. GlobalVariable *GV = CountersBySP[j].first;
  1030. unsigned Arcs = cast<ArrayType>(GV->getValueType())->getNumElements();
  1031. EmitArcsCallArgsArray.push_back(ConstantStruct::get(
  1032. EmitArcsCallArgsTy,
  1033. {Builder.getInt32(Arcs), ConstantExpr::getInBoundsGetElementPtr(
  1034. GV->getValueType(), GV, TwoZero32s)}));
  1035. }
  1036. // Create global arrays for the two emit calls.
  1037. int CountersSize = CountersBySP.size();
  1038. assert(CountersSize == (int)EmitFunctionCallArgsArray.size() &&
  1039. "Mismatched array size!");
  1040. assert(CountersSize == (int)EmitArcsCallArgsArray.size() &&
  1041. "Mismatched array size!");
  1042. auto *EmitFunctionCallArgsArrayTy =
  1043. ArrayType::get(EmitFunctionCallArgsTy, CountersSize);
  1044. auto *EmitFunctionCallArgsArrayGV = new GlobalVariable(
  1045. *M, EmitFunctionCallArgsArrayTy, /*isConstant*/ true,
  1046. GlobalValue::InternalLinkage,
  1047. ConstantArray::get(EmitFunctionCallArgsArrayTy,
  1048. EmitFunctionCallArgsArray),
  1049. Twine("__llvm_internal_gcov_emit_function_args.") + Twine(i));
  1050. auto *EmitArcsCallArgsArrayTy =
  1051. ArrayType::get(EmitArcsCallArgsTy, CountersSize);
  1052. EmitFunctionCallArgsArrayGV->setUnnamedAddr(
  1053. GlobalValue::UnnamedAddr::Global);
  1054. auto *EmitArcsCallArgsArrayGV = new GlobalVariable(
  1055. *M, EmitArcsCallArgsArrayTy, /*isConstant*/ true,
  1056. GlobalValue::InternalLinkage,
  1057. ConstantArray::get(EmitArcsCallArgsArrayTy, EmitArcsCallArgsArray),
  1058. Twine("__llvm_internal_gcov_emit_arcs_args.") + Twine(i));
  1059. EmitArcsCallArgsArrayGV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
  1060. FileInfos.push_back(ConstantStruct::get(
  1061. FileInfoTy,
  1062. {StartFileCallArgs, Builder.getInt32(CountersSize),
  1063. ConstantExpr::getInBoundsGetElementPtr(EmitFunctionCallArgsArrayTy,
  1064. EmitFunctionCallArgsArrayGV,
  1065. TwoZero32s),
  1066. ConstantExpr::getInBoundsGetElementPtr(
  1067. EmitArcsCallArgsArrayTy, EmitArcsCallArgsArrayGV, TwoZero32s)}));
  1068. }
  1069. // If we didn't find anything to actually emit, bail on out.
  1070. if (FileInfos.empty()) {
  1071. Builder.CreateRetVoid();
  1072. return WriteoutF;
  1073. }
  1074. // To simplify code, we cap the number of file infos we write out to fit
  1075. // easily in a 32-bit signed integer. This gives consistent behavior between
  1076. // 32-bit and 64-bit systems without requiring (potentially very slow) 64-bit
  1077. // operations on 32-bit systems. It also seems unreasonable to try to handle
  1078. // more than 2 billion files.
  1079. if ((int64_t)FileInfos.size() > (int64_t)INT_MAX)
  1080. FileInfos.resize(INT_MAX);
  1081. // Create a global for the entire data structure so we can walk it more
  1082. // easily.
  1083. auto *FileInfoArrayTy = ArrayType::get(FileInfoTy, FileInfos.size());
  1084. auto *FileInfoArrayGV = new GlobalVariable(
  1085. *M, FileInfoArrayTy, /*isConstant*/ true, GlobalValue::InternalLinkage,
  1086. ConstantArray::get(FileInfoArrayTy, FileInfos),
  1087. "__llvm_internal_gcov_emit_file_info");
  1088. FileInfoArrayGV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
  1089. // Create the CFG for walking this data structure.
  1090. auto *FileLoopHeader =
  1091. BasicBlock::Create(*Ctx, "file.loop.header", WriteoutF);
  1092. auto *CounterLoopHeader =
  1093. BasicBlock::Create(*Ctx, "counter.loop.header", WriteoutF);
  1094. auto *FileLoopLatch = BasicBlock::Create(*Ctx, "file.loop.latch", WriteoutF);
  1095. auto *ExitBB = BasicBlock::Create(*Ctx, "exit", WriteoutF);
  1096. // We always have at least one file, so just branch to the header.
  1097. Builder.CreateBr(FileLoopHeader);
  1098. // The index into the files structure is our loop induction variable.
  1099. Builder.SetInsertPoint(FileLoopHeader);
  1100. PHINode *IV = Builder.CreatePHI(Builder.getInt32Ty(), /*NumReservedValues*/ 2,
  1101. "file_idx");
  1102. IV->addIncoming(Builder.getInt32(0), BB);
  1103. auto *FileInfoPtr = Builder.CreateInBoundsGEP(
  1104. FileInfoArrayTy, FileInfoArrayGV, {Builder.getInt32(0), IV});
  1105. auto *StartFileCallArgsPtr =
  1106. Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 0, "start_file_args");
  1107. auto *StartFileCall = Builder.CreateCall(
  1108. StartFile,
  1109. {Builder.CreateLoad(StartFileCallArgsTy->getElementType(0),
  1110. Builder.CreateStructGEP(StartFileCallArgsTy,
  1111. StartFileCallArgsPtr, 0),
  1112. "filename"),
  1113. Builder.CreateLoad(StartFileCallArgsTy->getElementType(1),
  1114. Builder.CreateStructGEP(StartFileCallArgsTy,
  1115. StartFileCallArgsPtr, 1),
  1116. "version"),
  1117. Builder.CreateLoad(StartFileCallArgsTy->getElementType(2),
  1118. Builder.CreateStructGEP(StartFileCallArgsTy,
  1119. StartFileCallArgsPtr, 2),
  1120. "stamp")});
  1121. if (auto AK = TLI->getExtAttrForI32Param(false))
  1122. StartFileCall->addParamAttr(2, AK);
  1123. auto *NumCounters = Builder.CreateLoad(
  1124. FileInfoTy->getElementType(1),
  1125. Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 1), "num_ctrs");
  1126. auto *EmitFunctionCallArgsArray =
  1127. Builder.CreateLoad(FileInfoTy->getElementType(2),
  1128. Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 2),
  1129. "emit_function_args");
  1130. auto *EmitArcsCallArgsArray = Builder.CreateLoad(
  1131. FileInfoTy->getElementType(3),
  1132. Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 3), "emit_arcs_args");
  1133. auto *EnterCounterLoopCond =
  1134. Builder.CreateICmpSLT(Builder.getInt32(0), NumCounters);
  1135. Builder.CreateCondBr(EnterCounterLoopCond, CounterLoopHeader, FileLoopLatch);
  1136. Builder.SetInsertPoint(CounterLoopHeader);
  1137. auto *JV = Builder.CreatePHI(Builder.getInt32Ty(), /*NumReservedValues*/ 2,
  1138. "ctr_idx");
  1139. JV->addIncoming(Builder.getInt32(0), FileLoopHeader);
  1140. auto *EmitFunctionCallArgsPtr = Builder.CreateInBoundsGEP(
  1141. EmitFunctionCallArgsTy, EmitFunctionCallArgsArray, JV);
  1142. auto *EmitFunctionCall = Builder.CreateCall(
  1143. EmitFunction,
  1144. {Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(0),
  1145. Builder.CreateStructGEP(EmitFunctionCallArgsTy,
  1146. EmitFunctionCallArgsPtr, 0),
  1147. "ident"),
  1148. Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(1),
  1149. Builder.CreateStructGEP(EmitFunctionCallArgsTy,
  1150. EmitFunctionCallArgsPtr, 1),
  1151. "func_checkssum"),
  1152. Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(2),
  1153. Builder.CreateStructGEP(EmitFunctionCallArgsTy,
  1154. EmitFunctionCallArgsPtr, 2),
  1155. "cfg_checksum")});
  1156. if (auto AK = TLI->getExtAttrForI32Param(false)) {
  1157. EmitFunctionCall->addParamAttr(0, AK);
  1158. EmitFunctionCall->addParamAttr(1, AK);
  1159. EmitFunctionCall->addParamAttr(2, AK);
  1160. }
  1161. auto *EmitArcsCallArgsPtr =
  1162. Builder.CreateInBoundsGEP(EmitArcsCallArgsTy, EmitArcsCallArgsArray, JV);
  1163. auto *EmitArcsCall = Builder.CreateCall(
  1164. EmitArcs,
  1165. {Builder.CreateLoad(
  1166. EmitArcsCallArgsTy->getElementType(0),
  1167. Builder.CreateStructGEP(EmitArcsCallArgsTy, EmitArcsCallArgsPtr, 0),
  1168. "num_counters"),
  1169. Builder.CreateLoad(
  1170. EmitArcsCallArgsTy->getElementType(1),
  1171. Builder.CreateStructGEP(EmitArcsCallArgsTy, EmitArcsCallArgsPtr, 1),
  1172. "counters")});
  1173. if (auto AK = TLI->getExtAttrForI32Param(false))
  1174. EmitArcsCall->addParamAttr(0, AK);
  1175. auto *NextJV = Builder.CreateAdd(JV, Builder.getInt32(1));
  1176. auto *CounterLoopCond = Builder.CreateICmpSLT(NextJV, NumCounters);
  1177. Builder.CreateCondBr(CounterLoopCond, CounterLoopHeader, FileLoopLatch);
  1178. JV->addIncoming(NextJV, CounterLoopHeader);
  1179. Builder.SetInsertPoint(FileLoopLatch);
  1180. Builder.CreateCall(SummaryInfo, {});
  1181. Builder.CreateCall(EndFile, {});
  1182. auto *NextIV = Builder.CreateAdd(IV, Builder.getInt32(1), "next_file_idx");
  1183. auto *FileLoopCond =
  1184. Builder.CreateICmpSLT(NextIV, Builder.getInt32(FileInfos.size()));
  1185. Builder.CreateCondBr(FileLoopCond, FileLoopHeader, ExitBB);
  1186. IV->addIncoming(NextIV, FileLoopLatch);
  1187. Builder.SetInsertPoint(ExitBB);
  1188. Builder.CreateRetVoid();
  1189. return WriteoutF;
  1190. }
  1191. Function *GCOVProfiler::insertReset(
  1192. ArrayRef<std::pair<GlobalVariable *, MDNode *>> CountersBySP) {
  1193. FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
  1194. Function *ResetF = M->getFunction("__llvm_gcov_reset");
  1195. if (!ResetF)
  1196. ResetF = createInternalFunction(FTy, "__llvm_gcov_reset");
  1197. ResetF->addFnAttr(Attribute::NoInline);
  1198. BasicBlock *Entry = BasicBlock::Create(*Ctx, "entry", ResetF);
  1199. IRBuilder<> Builder(Entry);
  1200. LLVMContext &C = Entry->getContext();
  1201. // Zero out the counters.
  1202. for (const auto &I : CountersBySP) {
  1203. GlobalVariable *GV = I.first;
  1204. auto *GVTy = cast<ArrayType>(GV->getValueType());
  1205. Builder.CreateMemSet(GV, Constant::getNullValue(Type::getInt8Ty(C)),
  1206. GVTy->getNumElements() *
  1207. GVTy->getElementType()->getScalarSizeInBits() / 8,
  1208. GV->getAlign());
  1209. }
  1210. Type *RetTy = ResetF->getReturnType();
  1211. if (RetTy->isVoidTy())
  1212. Builder.CreateRetVoid();
  1213. else if (RetTy->isIntegerTy())
  1214. // Used if __llvm_gcov_reset was implicitly declared.
  1215. Builder.CreateRet(ConstantInt::get(RetTy, 0));
  1216. else
  1217. report_fatal_error("invalid return type for __llvm_gcov_reset");
  1218. return ResetF;
  1219. }