GCOVProfiling.cpp 48 KB

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