Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 1 | //===- GCOVProfiling.cpp - Insert edge counters for gcov profiling --------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 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 | |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Hashing.h" |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Sequence.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Statistic.h" |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
| 22 | #include "llvm/ADT/StringMap.h" |
Marco Castelluccio | 0dcf64a | 2017-10-13 13:49:15 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/EHPersonalities.h" |
Ulrich Weigand | b961fdc | 2018-07-10 16:05:47 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Vedant Kumar | 727d895 | 2018-09-11 18:38:34 +0000 | [diff] [blame] | 25 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 9205140 | 2014-03-05 10:30:38 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DebugLoc.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 29 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Instructions.h" |
Bob Wilson | 055a0b4 | 2014-01-31 05:24:01 +0000 | [diff] [blame] | 31 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/Pass.h" |
Nick Lewycky | fdfed3e | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Bill Wendling | 5aa8239 | 2013-03-26 22:47:50 +0000 | [diff] [blame] | 36 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | 3bc8e71 | 2013-06-11 22:21:28 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Path.h" |
Calixte Denizet | c6fabea | 2018-11-12 09:01:43 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Regex.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
Xinliang David Li | 64dbb29 | 2016-06-05 05:12:23 +0000 | [diff] [blame] | 40 | #include "llvm/Transforms/Instrumentation.h" |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/Instrumentation/GCOVProfiler.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Nick Lewycky | 0fdd019 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 43 | #include <algorithm> |
David Blaikie | 229de50 | 2014-04-21 20:41:55 +0000 | [diff] [blame] | 44 | #include <memory> |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 45 | #include <string> |
| 46 | #include <utility> |
| 47 | using namespace llvm; |
| 48 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 49 | #define DEBUG_TYPE "insert-gcov-profiling" |
| 50 | |
Nick Lewycky | fdfed3e | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 51 | static cl::opt<std::string> |
| 52 | DefaultGCOVVersion("default-gcov-version", cl::init("402*"), cl::Hidden, |
| 53 | cl::ValueRequired); |
Justin Bogner | 3faa76b | 2015-03-16 23:52:03 +0000 | [diff] [blame] | 54 | static cl::opt<bool> DefaultExitBlockBeforeBody("gcov-exit-block-before-body", |
| 55 | cl::init(false), cl::Hidden); |
Nick Lewycky | fdfed3e | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 56 | |
| 57 | GCOVOptions GCOVOptions::getDefault() { |
| 58 | GCOVOptions Options; |
| 59 | Options.EmitNotes = true; |
| 60 | Options.EmitData = true; |
| 61 | Options.UseCfgChecksum = false; |
| 62 | Options.NoRedZone = false; |
| 63 | Options.FunctionNamesInData = true; |
Justin Bogner | 3faa76b | 2015-03-16 23:52:03 +0000 | [diff] [blame] | 64 | Options.ExitBlockBeforeBody = DefaultExitBlockBeforeBody; |
Nick Lewycky | fdfed3e | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 65 | |
| 66 | if (DefaultGCOVVersion.size() != 4) { |
| 67 | llvm::report_fatal_error(std::string("Invalid -default-gcov-version: ") + |
| 68 | DefaultGCOVVersion); |
| 69 | } |
| 70 | memcpy(Options.Version, DefaultGCOVVersion.c_str(), 4); |
| 71 | return Options; |
| 72 | } |
| 73 | |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 74 | namespace { |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 75 | class GCOVFunction; |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 76 | |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 77 | class GCOVProfiler { |
| 78 | public: |
| 79 | GCOVProfiler() : GCOVProfiler(GCOVOptions::getDefault()) {} |
| 80 | GCOVProfiler(const GCOVOptions &Opts) : Options(Opts) { |
| 81 | assert((Options.EmitNotes || Options.EmitData) && |
| 82 | "GCOVProfiler asked to do nothing?"); |
| 83 | ReversedVersion[0] = Options.Version[3]; |
| 84 | ReversedVersion[1] = Options.Version[2]; |
| 85 | ReversedVersion[2] = Options.Version[1]; |
| 86 | ReversedVersion[3] = Options.Version[0]; |
| 87 | ReversedVersion[4] = '\0'; |
| 88 | } |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 89 | bool |
| 90 | runOnModule(Module &M, |
| 91 | std::function<const TargetLibraryInfo &(Function &F)> GetTLI); |
Benjamin Kramer | 298a3a0 | 2015-03-06 16:21:15 +0000 | [diff] [blame] | 92 | |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 93 | private: |
| 94 | // Create the .gcno files for the Module based on DebugInfo. |
| 95 | void emitProfileNotes(); |
Nick Lewycky | 6d9f061 | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 96 | |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 97 | // Modify the program to track transitions along edges and call into the |
| 98 | // profiling runtime to emit .gcda files when run. |
| 99 | bool emitProfileArcs(); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 100 | |
Calixte Denizet | c6fabea | 2018-11-12 09:01:43 +0000 | [diff] [blame] | 101 | bool isFunctionInstrumented(const Function &F); |
| 102 | std::vector<Regex> createRegexesFromString(StringRef RegexesStr); |
| 103 | static bool doesFilenameMatchARegex(StringRef Filename, |
| 104 | std::vector<Regex> &Regexes); |
| 105 | |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 106 | // Get pointers to the functions in the runtime library. |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 107 | FunctionCallee getStartFileFunc(const TargetLibraryInfo *TLI); |
| 108 | FunctionCallee getEmitFunctionFunc(const TargetLibraryInfo *TLI); |
| 109 | FunctionCallee getEmitArcsFunc(const TargetLibraryInfo *TLI); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 110 | FunctionCallee getSummaryInfoFunc(); |
| 111 | FunctionCallee getEndFileFunc(); |
Nick Lewycky | c5ea852 | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 112 | |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 113 | // Add the function to write out all our counters to the global destructor |
| 114 | // list. |
| 115 | Function * |
| 116 | insertCounterWriteout(ArrayRef<std::pair<GlobalVariable *, MDNode *>>); |
| 117 | Function *insertFlush(ArrayRef<std::pair<GlobalVariable *, MDNode *>>); |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 118 | |
Calixte Denizet | c3bed1e | 2018-11-07 13:49:17 +0000 | [diff] [blame] | 119 | void AddFlushBeforeForkAndExec(); |
| 120 | |
Nick Lewycky | 97e49ac | 2016-08-31 23:04:32 +0000 | [diff] [blame] | 121 | enum class GCovFileType { GCNO, GCDA }; |
| 122 | std::string mangleName(const DICompileUnit *CU, GCovFileType FileType); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 123 | |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 124 | GCOVOptions Options; |
Nick Lewycky | 6d9f061 | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 125 | |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 126 | // Reversed, NUL-terminated copy of Options.Version. |
| 127 | char ReversedVersion[5]; |
| 128 | // Checksum, produced by hash of EdgeDestinations |
| 129 | SmallVector<uint32_t, 4> FileChecksums; |
Nick Lewycky | fdfed3e | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 130 | |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 131 | Module *M; |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 132 | std::function<const TargetLibraryInfo &(Function &F)> GetTLI; |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 133 | LLVMContext *Ctx; |
| 134 | SmallVector<std::unique_ptr<GCOVFunction>, 16> Funcs; |
Calixte Denizet | c6fabea | 2018-11-12 09:01:43 +0000 | [diff] [blame] | 135 | std::vector<Regex> FilterRe; |
| 136 | std::vector<Regex> ExcludeRe; |
| 137 | StringMap<bool> InstrumentedFiles; |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 138 | }; |
Nick Lewycky | 8e0a38f | 2011-04-21 01:56:25 +0000 | [diff] [blame] | 139 | |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 140 | class GCOVProfilerLegacyPass : public ModulePass { |
| 141 | public: |
| 142 | static char ID; |
| 143 | GCOVProfilerLegacyPass() |
| 144 | : GCOVProfilerLegacyPass(GCOVOptions::getDefault()) {} |
| 145 | GCOVProfilerLegacyPass(const GCOVOptions &Opts) |
| 146 | : ModulePass(ID), Profiler(Opts) { |
| 147 | initializeGCOVProfilerLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 148 | } |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 149 | StringRef getPassName() const override { return "GCOV Profiler"; } |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 150 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 151 | bool runOnModule(Module &M) override { |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 152 | return Profiler.runOnModule(M, [this](Function &F) -> TargetLibraryInfo & { |
| 153 | return getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); |
| 154 | }); |
Ulrich Weigand | b961fdc | 2018-07-10 16:05:47 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 158 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| 159 | } |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 160 | |
| 161 | private: |
| 162 | GCOVProfiler Profiler; |
| 163 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 164 | } |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 165 | |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 166 | char GCOVProfilerLegacyPass::ID = 0; |
Ulrich Weigand | b961fdc | 2018-07-10 16:05:47 +0000 | [diff] [blame] | 167 | INITIALIZE_PASS_BEGIN( |
| 168 | GCOVProfilerLegacyPass, "insert-gcov-profiling", |
| 169 | "Insert instrumentation for GCOV profiling", false, false) |
| 170 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 171 | INITIALIZE_PASS_END( |
| 172 | GCOVProfilerLegacyPass, "insert-gcov-profiling", |
| 173 | "Insert instrumentation for GCOV profiling", false, false) |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 174 | |
Nick Lewycky | fdfed3e | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 175 | ModulePass *llvm::createGCOVProfilerPass(const GCOVOptions &Options) { |
Xinliang David Li | fb3137c | 2016-06-05 03:40:03 +0000 | [diff] [blame] | 176 | return new GCOVProfilerLegacyPass(Options); |
Nick Lewycky | 8e0a38f | 2011-04-21 01:56:25 +0000 | [diff] [blame] | 177 | } |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 178 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 179 | static StringRef getFunctionName(const DISubprogram *SP) { |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 180 | if (!SP->getLinkageName().empty()) |
| 181 | return SP->getLinkageName(); |
| 182 | return SP->getName(); |
Nick Lewycky | d671863 | 2013-03-19 01:37:55 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Adrian Prantl | fbeeac0 | 2018-12-06 18:44:48 +0000 | [diff] [blame] | 185 | /// Extract a filename for a DISubprogram. |
| 186 | /// |
| 187 | /// Prefer relative paths in the coverage notes. Clang also may split |
| 188 | /// up absolute paths into a directory and filename component. When |
| 189 | /// the relative path doesn't exist, reconstruct the absolute path. |
Benjamin Kramer | b17d213 | 2019-01-12 18:36:22 +0000 | [diff] [blame] | 190 | static SmallString<128> getFilename(const DISubprogram *SP) { |
Adrian Prantl | fbeeac0 | 2018-12-06 18:44:48 +0000 | [diff] [blame] | 191 | SmallString<128> Path; |
| 192 | StringRef RelPath = SP->getFilename(); |
| 193 | if (sys::fs::exists(RelPath)) |
| 194 | Path = RelPath; |
| 195 | else |
| 196 | sys::path::append(Path, SP->getDirectory(), SP->getFilename()); |
| 197 | return Path; |
| 198 | } |
| 199 | |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 200 | namespace { |
| 201 | class GCOVRecord { |
| 202 | protected: |
Craig Topper | 1c4d667 | 2013-07-17 03:43:10 +0000 | [diff] [blame] | 203 | static const char *const LinesTag; |
| 204 | static const char *const FunctionTag; |
| 205 | static const char *const BlockTag; |
| 206 | static const char *const EdgeTag; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 207 | |
Benjamin Kramer | 79de6e6 | 2015-04-11 18:57:14 +0000 | [diff] [blame] | 208 | GCOVRecord() = default; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 209 | |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 210 | void writeBytes(const char *Bytes, int Size) { |
| 211 | os->write(Bytes, Size); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 214 | void write(uint32_t i) { |
| 215 | writeBytes(reinterpret_cast<char*>(&i), 4); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Returns the length measured in 4-byte blocks that will be used to |
| 219 | // represent this string in a GCOV file |
Craig Topper | 24048c9 | 2013-07-17 03:54:53 +0000 | [diff] [blame] | 220 | static unsigned lengthOfGCOVString(StringRef s) { |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 221 | // A GCOV string is a length, followed by a NUL, then between 0 and 3 NULs |
Nick Lewycky | ed749d8 | 2011-04-21 02:48:39 +0000 | [diff] [blame] | 222 | // padding out to the next 4-byte word. The length is measured in 4-byte |
| 223 | // words including padding, not bytes of actual string. |
Nick Lewycky | a702884 | 2011-05-05 23:52:18 +0000 | [diff] [blame] | 224 | return (s.size() / 4) + 1; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 227 | void writeGCOVString(StringRef s) { |
| 228 | uint32_t Len = lengthOfGCOVString(s); |
| 229 | write(Len); |
| 230 | writeBytes(s.data(), s.size()); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 231 | |
| 232 | // Write 1 to 4 bytes of NUL padding. |
Nick Lewycky | 6aa7949 | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 233 | assert((unsigned)(4 - (s.size() % 4)) > 0); |
| 234 | assert((unsigned)(4 - (s.size() % 4)) <= 4); |
| 235 | writeBytes("\0\0\0\0", 4 - (s.size() % 4)); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | raw_ostream *os; |
| 239 | }; |
Craig Topper | 1c4d667 | 2013-07-17 03:43:10 +0000 | [diff] [blame] | 240 | const char *const GCOVRecord::LinesTag = "\0\0\x45\x01"; |
| 241 | const char *const GCOVRecord::FunctionTag = "\0\0\0\1"; |
| 242 | const char *const GCOVRecord::BlockTag = "\0\0\x41\x01"; |
| 243 | const char *const GCOVRecord::EdgeTag = "\0\0\x43\x01"; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 244 | |
| 245 | class GCOVFunction; |
| 246 | class GCOVBlock; |
| 247 | |
| 248 | // Constructed only by requesting it from a GCOVBlock, this object stores a |
Devang Patel | add1f17 | 2011-09-20 18:35:00 +0000 | [diff] [blame] | 249 | // list of line numbers and a single filename, representing lines that belong |
| 250 | // to the block. |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 251 | class GCOVLines : public GCOVRecord { |
| 252 | public: |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 253 | void addLine(uint32_t Line) { |
Nick Lewycky | 5f53ddd | 2014-06-03 04:25:36 +0000 | [diff] [blame] | 254 | assert(Line != 0 && "Line zero is not a valid real line number."); |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 255 | Lines.push_back(Line); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Craig Topper | 24048c9 | 2013-07-17 03:54:53 +0000 | [diff] [blame] | 258 | uint32_t length() const { |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 259 | // Here 2 = 1 for string length + 1 for '0' id#. |
Devang Patel | add1f17 | 2011-09-20 18:35:00 +0000 | [diff] [blame] | 260 | return lengthOfGCOVString(Filename) + 2 + Lines.size(); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Devang Patel | add1f17 | 2011-09-20 18:35:00 +0000 | [diff] [blame] | 263 | void writeOut() { |
| 264 | write(0); |
| 265 | writeGCOVString(Filename); |
| 266 | for (int i = 0, e = Lines.size(); i != e; ++i) |
| 267 | write(Lines[i]); |
| 268 | } |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 269 | |
Duncan P. N. Exon Smith | cec1c24 | 2014-03-11 02:44:45 +0000 | [diff] [blame] | 270 | GCOVLines(StringRef F, raw_ostream *os) |
Devang Patel | add1f17 | 2011-09-20 18:35:00 +0000 | [diff] [blame] | 271 | : Filename(F) { |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 272 | this->os = os; |
| 273 | } |
| 274 | |
Devang Patel | 7d06f5c | 2011-09-20 18:48:56 +0000 | [diff] [blame] | 275 | private: |
Adrian Prantl | fbeeac0 | 2018-12-06 18:44:48 +0000 | [diff] [blame] | 276 | std::string Filename; |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 277 | SmallVector<uint32_t, 32> Lines; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
Nick Lewycky | 0fdd019 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 280 | |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 281 | // Represent a basic block in GCOV. Each block has a unique number in the |
| 282 | // function, number of lines belonging to each block, and a set of edges to |
| 283 | // other blocks. |
| 284 | class GCOVBlock : public GCOVRecord { |
| 285 | public: |
Devang Patel | 9cb1fc0 | 2011-09-20 17:55:19 +0000 | [diff] [blame] | 286 | GCOVLines &getFile(StringRef Filename) { |
Benjamin Kramer | eab3d36 | 2016-07-21 13:37:48 +0000 | [diff] [blame] | 287 | return LinesByFile.try_emplace(Filename, Filename, os).first->second; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 290 | void addEdge(GCOVBlock &Successor) { |
| 291 | OutEdges.push_back(&Successor); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 294 | void writeOut() { |
| 295 | uint32_t Len = 3; |
Benjamin Kramer | 2a185a2 | 2016-07-21 12:06:31 +0000 | [diff] [blame] | 296 | SmallVector<StringMapEntry<GCOVLines> *, 32> SortedLinesByFile; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 297 | for (auto &I : LinesByFile) { |
Benjamin Kramer | 2a185a2 | 2016-07-21 12:06:31 +0000 | [diff] [blame] | 298 | Len += I.second.length(); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 299 | SortedLinesByFile.push_back(&I); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 302 | writeBytes(LinesTag, 4); |
| 303 | write(Len); |
| 304 | write(Number); |
Nick Lewycky | 0fdd019 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 305 | |
Fangrui Song | 3507c6e | 2018-09-30 22:31:29 +0000 | [diff] [blame] | 306 | llvm::sort(SortedLinesByFile, [](StringMapEntry<GCOVLines> *LHS, |
| 307 | StringMapEntry<GCOVLines> *RHS) { |
| 308 | return LHS->getKey() < RHS->getKey(); |
| 309 | }); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 310 | for (auto &I : SortedLinesByFile) |
Benjamin Kramer | 2a185a2 | 2016-07-21 12:06:31 +0000 | [diff] [blame] | 311 | I->getValue().writeOut(); |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 312 | write(0); |
| 313 | write(0); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 314 | } |
| 315 | |
David Blaikie | ea37c11 | 2014-12-22 23:12:42 +0000 | [diff] [blame] | 316 | GCOVBlock(const GCOVBlock &RHS) : GCOVRecord(RHS), Number(RHS.Number) { |
| 317 | // Only allow copy before edges and lines have been added. After that, |
| 318 | // there are inter-block pointers (eg: edges) that won't take kindly to |
| 319 | // blocks being copied or moved around. |
| 320 | assert(LinesByFile.empty()); |
| 321 | assert(OutEdges.empty()); |
| 322 | } |
| 323 | |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 324 | private: |
| 325 | friend class GCOVFunction; |
| 326 | |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 327 | GCOVBlock(uint32_t Number, raw_ostream *os) |
| 328 | : Number(Number) { |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 329 | this->os = os; |
| 330 | } |
| 331 | |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 332 | uint32_t Number; |
Benjamin Kramer | 2a185a2 | 2016-07-21 12:06:31 +0000 | [diff] [blame] | 333 | StringMap<GCOVLines> LinesByFile; |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 334 | SmallVector<GCOVBlock *, 4> OutEdges; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | // A function has a unique identifier, a checksum (we leave as zero) and a |
| 338 | // set of blocks and a map of edges between blocks. This is the only GCOV |
| 339 | // object users can construct, the blocks and lines will be rooted here. |
| 340 | class GCOVFunction : public GCOVRecord { |
| 341 | public: |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 342 | GCOVFunction(const DISubprogram *SP, Function *F, raw_ostream *os, |
| 343 | uint32_t Ident, bool UseCfgChecksum, bool ExitBlockBeforeBody) |
David Blaikie | ea37c11 | 2014-12-22 23:12:42 +0000 | [diff] [blame] | 344 | : SP(SP), Ident(Ident), UseCfgChecksum(UseCfgChecksum), CfgChecksum(0), |
| 345 | ReturnBlock(1, os) { |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 346 | this->os = os; |
| 347 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 348 | LLVM_DEBUG(dbgs() << "Function: " << getFunctionName(SP) << "\n"); |
Nick Lewycky | 2e8a621 | 2014-12-03 02:45:01 +0000 | [diff] [blame] | 349 | |
David Blaikie | ea37c11 | 2014-12-22 23:12:42 +0000 | [diff] [blame] | 350 | uint32_t i = 0; |
| 351 | for (auto &BB : *F) { |
Justin Bogner | 3faa76b | 2015-03-16 23:52:03 +0000 | [diff] [blame] | 352 | // Skip index 1 if it's assigned to the ReturnBlock. |
| 353 | if (i == 1 && ExitBlockBeforeBody) |
| 354 | ++i; |
| 355 | Blocks.insert(std::make_pair(&BB, GCOVBlock(i++, os))); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 356 | } |
Justin Bogner | 3faa76b | 2015-03-16 23:52:03 +0000 | [diff] [blame] | 357 | if (!ExitBlockBeforeBody) |
| 358 | ReturnBlock.Number = i; |
Daniel Jasper | 87a24d5 | 2013-12-04 08:57:17 +0000 | [diff] [blame] | 359 | |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 360 | std::string FunctionNameAndLine; |
| 361 | raw_string_ostream FNLOS(FunctionNameAndLine); |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 362 | FNLOS << getFunctionName(SP) << SP->getLine(); |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 363 | FNLOS.flush(); |
| 364 | FuncChecksum = hash_value(FunctionNameAndLine); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 367 | GCOVBlock &getBlock(BasicBlock *BB) { |
David Blaikie | ea37c11 | 2014-12-22 23:12:42 +0000 | [diff] [blame] | 368 | return Blocks.find(BB)->second; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 371 | GCOVBlock &getReturnBlock() { |
David Blaikie | ea37c11 | 2014-12-22 23:12:42 +0000 | [diff] [blame] | 372 | return ReturnBlock; |
Nick Lewycky | 8411b55 | 2011-04-21 03:18:00 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 375 | std::string getEdgeDestinations() { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 376 | std::string EdgeDestinations; |
| 377 | raw_string_ostream EDOS(EdgeDestinations); |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 378 | Function *F = Blocks.begin()->first->getParent(); |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 379 | for (BasicBlock &I : *F) { |
| 380 | GCOVBlock &Block = getBlock(&I); |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 381 | for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 382 | EDOS << Block.OutEdges[i]->Number; |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 383 | } |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 384 | return EdgeDestinations; |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Daniel Jasper | 87a24d5 | 2013-12-04 08:57:17 +0000 | [diff] [blame] | 387 | uint32_t getFuncChecksum() { |
| 388 | return FuncChecksum; |
| 389 | } |
| 390 | |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 391 | void setCfgChecksum(uint32_t Checksum) { |
| 392 | CfgChecksum = Checksum; |
| 393 | } |
| 394 | |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 395 | void writeOut() { |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 396 | writeBytes(FunctionTag, 4); |
Adrian Prantl | fbeeac0 | 2018-12-06 18:44:48 +0000 | [diff] [blame] | 397 | SmallString<128> Filename = getFilename(SP); |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 398 | uint32_t BlockLen = 1 + 1 + 1 + lengthOfGCOVString(getFunctionName(SP)) + |
Adrian Prantl | fbeeac0 | 2018-12-06 18:44:48 +0000 | [diff] [blame] | 399 | 1 + lengthOfGCOVString(Filename) + 1; |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 400 | if (UseCfgChecksum) |
| 401 | ++BlockLen; |
| 402 | write(BlockLen); |
| 403 | write(Ident); |
Daniel Jasper | 87a24d5 | 2013-12-04 08:57:17 +0000 | [diff] [blame] | 404 | write(FuncChecksum); |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 405 | if (UseCfgChecksum) |
| 406 | write(CfgChecksum); |
| 407 | writeGCOVString(getFunctionName(SP)); |
Adrian Prantl | fbeeac0 | 2018-12-06 18:44:48 +0000 | [diff] [blame] | 408 | writeGCOVString(Filename); |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 409 | write(SP->getLine()); |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 410 | |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 411 | // Emit count of blocks. |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 412 | writeBytes(BlockTag, 4); |
| 413 | write(Blocks.size() + 1); |
| 414 | for (int i = 0, e = Blocks.size() + 1; i != e; ++i) { |
| 415 | write(0); // No flags on our blocks. |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 416 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 417 | LLVM_DEBUG(dbgs() << Blocks.size() << " blocks.\n"); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 418 | |
| 419 | // Emit edges between blocks. |
Nick Lewycky | 0fdd019 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 420 | if (Blocks.empty()) return; |
| 421 | Function *F = Blocks.begin()->first->getParent(); |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 422 | for (BasicBlock &I : *F) { |
| 423 | GCOVBlock &Block = getBlock(&I); |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 424 | if (Block.OutEdges.empty()) continue; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 425 | |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 426 | writeBytes(EdgeTag, 4); |
| 427 | write(Block.OutEdges.size() * 2 + 1); |
| 428 | write(Block.Number); |
| 429 | for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 430 | LLVM_DEBUG(dbgs() << Block.Number << " -> " |
| 431 | << Block.OutEdges[i]->Number << "\n"); |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 432 | write(Block.OutEdges[i]->Number); |
| 433 | write(0); // no flags |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | // Emit lines for each block. |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 438 | for (BasicBlock &I : *F) |
| 439 | getBlock(&I).writeOut(); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | private: |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 443 | const DISubprogram *SP; |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 444 | uint32_t Ident; |
Daniel Jasper | 87a24d5 | 2013-12-04 08:57:17 +0000 | [diff] [blame] | 445 | uint32_t FuncChecksum; |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 446 | bool UseCfgChecksum; |
| 447 | uint32_t CfgChecksum; |
David Blaikie | ea37c11 | 2014-12-22 23:12:42 +0000 | [diff] [blame] | 448 | DenseMap<BasicBlock *, GCOVBlock> Blocks; |
| 449 | GCOVBlock ReturnBlock; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 450 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 451 | } |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 452 | |
Calixte Denizet | c6fabea | 2018-11-12 09:01:43 +0000 | [diff] [blame] | 453 | // RegexesStr is a string containing differents regex separated by a semi-colon. |
| 454 | // For example "foo\..*$;bar\..*$". |
| 455 | std::vector<Regex> GCOVProfiler::createRegexesFromString(StringRef RegexesStr) { |
| 456 | std::vector<Regex> Regexes; |
| 457 | while (!RegexesStr.empty()) { |
| 458 | std::pair<StringRef, StringRef> HeadTail = RegexesStr.split(';'); |
| 459 | if (!HeadTail.first.empty()) { |
| 460 | Regex Re(HeadTail.first); |
| 461 | std::string Err; |
| 462 | if (!Re.isValid(Err)) { |
| 463 | Ctx->emitError(Twine("Regex ") + HeadTail.first + |
| 464 | " is not valid: " + Err); |
| 465 | } |
| 466 | Regexes.emplace_back(std::move(Re)); |
| 467 | } |
| 468 | RegexesStr = HeadTail.second; |
| 469 | } |
| 470 | return Regexes; |
| 471 | } |
| 472 | |
| 473 | bool GCOVProfiler::doesFilenameMatchARegex(StringRef Filename, |
| 474 | std::vector<Regex> &Regexes) { |
| 475 | for (Regex &Re : Regexes) { |
| 476 | if (Re.match(Filename)) { |
| 477 | return true; |
| 478 | } |
| 479 | } |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | bool GCOVProfiler::isFunctionInstrumented(const Function &F) { |
| 484 | if (FilterRe.empty() && ExcludeRe.empty()) { |
| 485 | return true; |
| 486 | } |
Adrian Prantl | fbeeac0 | 2018-12-06 18:44:48 +0000 | [diff] [blame] | 487 | SmallString<128> Filename = getFilename(F.getSubprogram()); |
Calixte Denizet | c6fabea | 2018-11-12 09:01:43 +0000 | [diff] [blame] | 488 | auto It = InstrumentedFiles.find(Filename); |
| 489 | if (It != InstrumentedFiles.end()) { |
| 490 | return It->second; |
| 491 | } |
| 492 | |
| 493 | SmallString<256> RealPath; |
| 494 | StringRef RealFilename; |
| 495 | |
| 496 | // Path can be |
| 497 | // /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/*.h so for |
| 498 | // such a case we must get the real_path. |
| 499 | if (sys::fs::real_path(Filename, RealPath)) { |
| 500 | // real_path can fail with path like "foo.c". |
| 501 | RealFilename = Filename; |
| 502 | } else { |
| 503 | RealFilename = RealPath; |
| 504 | } |
| 505 | |
| 506 | bool ShouldInstrument; |
| 507 | if (FilterRe.empty()) { |
| 508 | ShouldInstrument = !doesFilenameMatchARegex(RealFilename, ExcludeRe); |
| 509 | } else if (ExcludeRe.empty()) { |
| 510 | ShouldInstrument = doesFilenameMatchARegex(RealFilename, FilterRe); |
| 511 | } else { |
| 512 | ShouldInstrument = doesFilenameMatchARegex(RealFilename, FilterRe) && |
| 513 | !doesFilenameMatchARegex(RealFilename, ExcludeRe); |
| 514 | } |
| 515 | InstrumentedFiles[Filename] = ShouldInstrument; |
| 516 | return ShouldInstrument; |
| 517 | } |
| 518 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 519 | std::string GCOVProfiler::mangleName(const DICompileUnit *CU, |
Nick Lewycky | 97e49ac | 2016-08-31 23:04:32 +0000 | [diff] [blame] | 520 | GCovFileType OutputType) { |
| 521 | bool Notes = OutputType == GCovFileType::GCNO; |
| 522 | |
Nick Lewycky | 6d9f061 | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 523 | if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) { |
| 524 | for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 525 | MDNode *N = GCov->getOperand(i); |
Nick Lewycky | 97e49ac | 2016-08-31 23:04:32 +0000 | [diff] [blame] | 526 | bool ThreeElement = N->getNumOperands() == 3; |
| 527 | if (!ThreeElement && N->getNumOperands() != 2) |
| 528 | continue; |
Nick Lewycky | 8dd4dad | 2016-08-31 23:24:43 +0000 | [diff] [blame] | 529 | if (dyn_cast<MDNode>(N->getOperand(ThreeElement ? 2 : 1)) != CU) |
Nick Lewycky | 97e49ac | 2016-08-31 23:04:32 +0000 | [diff] [blame] | 530 | continue; |
| 531 | |
| 532 | if (ThreeElement) { |
| 533 | // These nodes have no mangling to apply, it's stored mangled in the |
| 534 | // bitcode. |
| 535 | MDString *NotesFile = dyn_cast<MDString>(N->getOperand(0)); |
| 536 | MDString *DataFile = dyn_cast<MDString>(N->getOperand(1)); |
| 537 | if (!NotesFile || !DataFile) |
| 538 | continue; |
| 539 | return Notes ? NotesFile->getString() : DataFile->getString(); |
Nick Lewycky | a3d5d16 | 2011-05-05 00:03:30 +0000 | [diff] [blame] | 540 | } |
Nick Lewycky | 97e49ac | 2016-08-31 23:04:32 +0000 | [diff] [blame] | 541 | |
| 542 | MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0)); |
| 543 | if (!GCovFile) |
| 544 | continue; |
| 545 | |
| 546 | SmallString<128> Filename = GCovFile->getString(); |
| 547 | sys::path::replace_extension(Filename, Notes ? "gcno" : "gcda"); |
| 548 | return Filename.str(); |
Nick Lewycky | 6d9f061 | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
Nick Lewycky | a3d5d16 | 2011-05-05 00:03:30 +0000 | [diff] [blame] | 551 | |
Ilya Biryukov | 449a7f0 | 2018-12-04 16:30:31 +0000 | [diff] [blame] | 552 | SmallString<128> Filename = CU->getFilename(); |
Nick Lewycky | 97e49ac | 2016-08-31 23:04:32 +0000 | [diff] [blame] | 553 | sys::path::replace_extension(Filename, Notes ? "gcno" : "gcda"); |
Bill Wendling | 5aa8239 | 2013-03-26 22:47:50 +0000 | [diff] [blame] | 554 | StringRef FName = sys::path::filename(Filename); |
Bill Wendling | 5aa8239 | 2013-03-26 22:47:50 +0000 | [diff] [blame] | 555 | SmallString<128> CurPath; |
| 556 | if (sys::fs::current_path(CurPath)) return FName; |
Yaron Keren | 75e0c4b | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 557 | sys::path::append(CurPath, FName); |
Bill Wendling | 5aa8239 | 2013-03-26 22:47:50 +0000 | [diff] [blame] | 558 | return CurPath.str(); |
Nick Lewycky | 6d9f061 | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 561 | bool GCOVProfiler::runOnModule( |
| 562 | Module &M, std::function<const TargetLibraryInfo &(Function &F)> GetTLI) { |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 563 | this->M = &M; |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 564 | this->GetTLI = std::move(GetTLI); |
Nick Lewycky | c5ea852 | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 565 | Ctx = &M.getContext(); |
| 566 | |
Calixte Denizet | c3bed1e | 2018-11-07 13:49:17 +0000 | [diff] [blame] | 567 | AddFlushBeforeForkAndExec(); |
| 568 | |
Calixte Denizet | c6fabea | 2018-11-12 09:01:43 +0000 | [diff] [blame] | 569 | FilterRe = createRegexesFromString(Options.Filter); |
| 570 | ExcludeRe = createRegexesFromString(Options.Exclude); |
| 571 | |
Nick Lewycky | fdfed3e | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 572 | if (Options.EmitNotes) emitProfileNotes(); |
| 573 | if (Options.EmitData) return emitProfileArcs(); |
Nick Lewycky | 8e0a38f | 2011-04-21 01:56:25 +0000 | [diff] [blame] | 574 | return false; |
Nick Lewycky | c5ea852 | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Xinliang David Li | 64dbb29 | 2016-06-05 05:12:23 +0000 | [diff] [blame] | 577 | PreservedAnalyses GCOVProfilerPass::run(Module &M, |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 578 | ModuleAnalysisManager &AM) { |
Xinliang David Li | 64dbb29 | 2016-06-05 05:12:23 +0000 | [diff] [blame] | 579 | |
| 580 | GCOVProfiler Profiler(GCOVOpts); |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 581 | FunctionAnalysisManager &FAM = |
| 582 | AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
Xinliang David Li | 64dbb29 | 2016-06-05 05:12:23 +0000 | [diff] [blame] | 583 | |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 584 | if (!Profiler.runOnModule(M, [&](Function &F) -> TargetLibraryInfo & { |
| 585 | return FAM.getResult<TargetLibraryAnalysis>(F); |
| 586 | })) |
Xinliang David Li | 64dbb29 | 2016-06-05 05:12:23 +0000 | [diff] [blame] | 587 | return PreservedAnalyses::all(); |
| 588 | |
| 589 | return PreservedAnalyses::none(); |
| 590 | } |
| 591 | |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 592 | static bool functionHasLines(Function &F) { |
Nick Lewycky | 05e0f1c | 2014-04-18 23:32:28 +0000 | [diff] [blame] | 593 | // Check whether this function actually has any source lines. Not only |
| 594 | // do these waste space, they also can crash gcov. |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 595 | for (auto &BB : F) { |
| 596 | for (auto &I : BB) { |
Nick Lewycky | ce98b43 | 2014-06-04 21:47:19 +0000 | [diff] [blame] | 597 | // Debug intrinsic locations correspond to the location of the |
| 598 | // declaration, not necessarily any statements or expressions. |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 599 | if (isa<DbgInfoIntrinsic>(&I)) continue; |
Nick Lewycky | ff114da | 2014-06-05 04:31:43 +0000 | [diff] [blame] | 600 | |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 601 | const DebugLoc &Loc = I.getDebugLoc(); |
Duncan P. N. Exon Smith | ec819c0 | 2015-03-30 19:49:49 +0000 | [diff] [blame] | 602 | if (!Loc) |
| 603 | continue; |
Nick Lewycky | ff114da | 2014-06-05 04:31:43 +0000 | [diff] [blame] | 604 | |
| 605 | // Artificial lines such as calls to the global constructors. |
Justin Bogner | 3faa76b | 2015-03-16 23:52:03 +0000 | [diff] [blame] | 606 | if (Loc.getLine() == 0) continue; |
Nick Lewycky | ff114da | 2014-06-05 04:31:43 +0000 | [diff] [blame] | 607 | |
Nick Lewycky | 5f53ddd | 2014-06-03 04:25:36 +0000 | [diff] [blame] | 608 | return true; |
Nick Lewycky | 05e0f1c | 2014-04-18 23:32:28 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | return false; |
| 612 | } |
| 613 | |
Heejin Ahn | b4be38f | 2018-05-17 20:52:03 +0000 | [diff] [blame] | 614 | static bool isUsingScopeBasedEH(Function &F) { |
Marco Castelluccio | 0dcf64a | 2017-10-13 13:49:15 +0000 | [diff] [blame] | 615 | if (!F.hasPersonalityFn()) return false; |
| 616 | |
| 617 | EHPersonality Personality = classifyEHPersonality(F.getPersonalityFn()); |
Heejin Ahn | b4be38f | 2018-05-17 20:52:03 +0000 | [diff] [blame] | 618 | return isScopedEHPersonality(Personality); |
Marco Castelluccio | 0dcf64a | 2017-10-13 13:49:15 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Sylvestre Ledru | e7d4cd6 | 2017-09-26 11:56:43 +0000 | [diff] [blame] | 621 | static bool shouldKeepInEntry(BasicBlock::iterator It) { |
| 622 | if (isa<AllocaInst>(*It)) return true; |
| 623 | if (isa<DbgInfoIntrinsic>(*It)) return true; |
| 624 | if (auto *II = dyn_cast<IntrinsicInst>(It)) { |
| 625 | if (II->getIntrinsicID() == llvm::Intrinsic::localescape) return true; |
| 626 | } |
| 627 | |
| 628 | return false; |
| 629 | } |
| 630 | |
Calixte Denizet | c3bed1e | 2018-11-07 13:49:17 +0000 | [diff] [blame] | 631 | void GCOVProfiler::AddFlushBeforeForkAndExec() { |
| 632 | SmallVector<Instruction *, 2> ForkAndExecs; |
| 633 | for (auto &F : M->functions()) { |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 634 | auto *TLI = &GetTLI(F); |
Calixte Denizet | c3bed1e | 2018-11-07 13:49:17 +0000 | [diff] [blame] | 635 | for (auto &I : instructions(F)) { |
| 636 | if (CallInst *CI = dyn_cast<CallInst>(&I)) { |
| 637 | if (Function *Callee = CI->getCalledFunction()) { |
| 638 | LibFunc LF; |
| 639 | if (TLI->getLibFunc(*Callee, LF) && |
| 640 | (LF == LibFunc_fork || LF == LibFunc_execl || |
| 641 | LF == LibFunc_execle || LF == LibFunc_execlp || |
| 642 | LF == LibFunc_execv || LF == LibFunc_execvp || |
| 643 | LF == LibFunc_execve || LF == LibFunc_execvpe || |
| 644 | LF == LibFunc_execvP)) { |
| 645 | ForkAndExecs.push_back(&I); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | // We need to split the block after the fork/exec call |
| 653 | // because else the counters for the lines after will be |
| 654 | // the same as before the call. |
| 655 | for (auto I : ForkAndExecs) { |
| 656 | IRBuilder<> Builder(I); |
| 657 | FunctionType *FTy = FunctionType::get(Builder.getVoidTy(), {}, false); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 658 | FunctionCallee GCOVFlush = M->getOrInsertFunction("__gcov_flush", FTy); |
Calixte Denizet | c3bed1e | 2018-11-07 13:49:17 +0000 | [diff] [blame] | 659 | Builder.CreateCall(GCOVFlush); |
| 660 | I->getParent()->splitBasicBlock(I); |
| 661 | } |
| 662 | } |
| 663 | |
Nick Lewycky | ad14550 | 2013-03-13 22:55:42 +0000 | [diff] [blame] | 664 | void GCOVProfiler::emitProfileNotes() { |
Devang Patel | 2b21d86 | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 665 | NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu"); |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 666 | if (!CU_Nodes) return; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 667 | |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 668 | for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { |
| 669 | // Each compile unit gets its own .gcno file. This means that whether we run |
| 670 | // this pass over the original .o's as they're produced, or run it after |
| 671 | // LTO, we'll generate the same .gcno files. |
| 672 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 673 | auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i)); |
Vedant Kumar | 61035fa | 2016-01-21 17:04:42 +0000 | [diff] [blame] | 674 | |
| 675 | // Skip module skeleton (and module) CUs. |
| 676 | if (CU->getDWOId()) |
| 677 | continue; |
| 678 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 679 | std::error_code EC; |
Fangrui Song | d9b948b | 2019-08-05 05:43:48 +0000 | [diff] [blame] | 680 | raw_fd_ostream out(mangleName(CU, GCovFileType::GCNO), EC, |
| 681 | sys::fs::OF_None); |
Reid Kleckner | 1aa4ea8 | 2017-09-18 21:31:48 +0000 | [diff] [blame] | 682 | if (EC) { |
| 683 | Ctx->emitError(Twine("failed to open coverage notes file for writing: ") + |
| 684 | EC.message()); |
| 685 | continue; |
| 686 | } |
| 687 | |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 688 | std::string EdgeDestinations; |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 689 | |
Justin Bogner | 58e4134 | 2014-11-06 06:55:02 +0000 | [diff] [blame] | 690 | unsigned FunctionIdent = 0; |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 691 | for (auto &F : M->functions()) { |
| 692 | DISubprogram *SP = F.getSubprogram(); |
| 693 | if (!SP) continue; |
Calixte Denizet | c6fabea | 2018-11-12 09:01:43 +0000 | [diff] [blame] | 694 | if (!functionHasLines(F) || !isFunctionInstrumented(F)) |
| 695 | continue; |
Heejin Ahn | b4be38f | 2018-05-17 20:52:03 +0000 | [diff] [blame] | 696 | // TODO: Functions using scope-based EH are currently not supported. |
| 697 | if (isUsingScopeBasedEH(F)) continue; |
Bob Wilson | 055a0b4 | 2014-01-31 05:24:01 +0000 | [diff] [blame] | 698 | |
| 699 | // gcov expects every function to start with an entry block that has a |
| 700 | // single successor, so split the entry block to make sure of that. |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 701 | BasicBlock &EntryBlock = F.getEntryBlock(); |
Bob Wilson | 055a0b4 | 2014-01-31 05:24:01 +0000 | [diff] [blame] | 702 | BasicBlock::iterator It = EntryBlock.begin(); |
Sylvestre Ledru | e7d4cd6 | 2017-09-26 11:56:43 +0000 | [diff] [blame] | 703 | while (shouldKeepInEntry(It)) |
Bob Wilson | 055a0b4 | 2014-01-31 05:24:01 +0000 | [diff] [blame] | 704 | ++It; |
| 705 | EntryBlock.splitBasicBlock(It); |
Yuchen Wu | c87ca32 | 2013-11-22 23:07:45 +0000 | [diff] [blame] | 706 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 707 | Funcs.push_back(std::make_unique<GCOVFunction>(SP, &F, &out, FunctionIdent++, |
Justin Bogner | 3faa76b | 2015-03-16 23:52:03 +0000 | [diff] [blame] | 708 | Options.UseCfgChecksum, |
| 709 | Options.ExitBlockBeforeBody)); |
David Blaikie | 229de50 | 2014-04-21 20:41:55 +0000 | [diff] [blame] | 710 | GCOVFunction &Func = *Funcs.back(); |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 711 | |
Calixte Denizet | d2f290b | 2018-10-11 08:53:43 +0000 | [diff] [blame] | 712 | // Add the function line number to the lines of the entry block |
| 713 | // to have a counter for the function definition. |
Calixte Denizet | 38d5054 | 2018-10-30 18:41:31 +0000 | [diff] [blame] | 714 | uint32_t Line = SP->getLine(); |
Adrian Prantl | fbeeac0 | 2018-12-06 18:44:48 +0000 | [diff] [blame] | 715 | auto Filename = getFilename(SP); |
| 716 | Func.getBlock(&EntryBlock).getFile(Filename).addLine(Line); |
Calixte Denizet | d2f290b | 2018-10-11 08:53:43 +0000 | [diff] [blame] | 717 | |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 718 | for (auto &BB : F) { |
| 719 | GCOVBlock &Block = Func.getBlock(&BB); |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 720 | Instruction *TI = BB.getTerminator(); |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 721 | if (int successors = TI->getNumSuccessors()) { |
| 722 | for (int i = 0; i != successors; ++i) { |
David Blaikie | 229de50 | 2014-04-21 20:41:55 +0000 | [diff] [blame] | 723 | Block.addEdge(Func.getBlock(TI->getSuccessor(i))); |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 724 | } |
| 725 | } else if (isa<ReturnInst>(TI)) { |
David Blaikie | 229de50 | 2014-04-21 20:41:55 +0000 | [diff] [blame] | 726 | Block.addEdge(Func.getReturnBlock()); |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 729 | for (auto &I : BB) { |
Nick Lewycky | ce98b43 | 2014-06-04 21:47:19 +0000 | [diff] [blame] | 730 | // Debug intrinsic locations correspond to the location of the |
| 731 | // declaration, not necessarily any statements or expressions. |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 732 | if (isa<DbgInfoIntrinsic>(&I)) continue; |
Nick Lewycky | ff114da | 2014-06-05 04:31:43 +0000 | [diff] [blame] | 733 | |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 734 | const DebugLoc &Loc = I.getDebugLoc(); |
Duncan P. N. Exon Smith | ec819c0 | 2015-03-30 19:49:49 +0000 | [diff] [blame] | 735 | if (!Loc) |
| 736 | continue; |
Nick Lewycky | ff114da | 2014-06-05 04:31:43 +0000 | [diff] [blame] | 737 | |
| 738 | // Artificial lines such as calls to the global constructors. |
Calixte Denizet | eb7f602 | 2018-09-20 08:53:06 +0000 | [diff] [blame] | 739 | if (Loc.getLine() == 0 || Loc.isImplicitCode()) |
| 740 | continue; |
Nick Lewycky | ff114da | 2014-06-05 04:31:43 +0000 | [diff] [blame] | 741 | |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 742 | if (Line == Loc.getLine()) continue; |
| 743 | Line = Loc.getLine(); |
Duncan P. N. Exon Smith | ec819c0 | 2015-03-30 19:49:49 +0000 | [diff] [blame] | 744 | if (SP != getDISubprogram(Loc.getScope())) |
| 745 | continue; |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 746 | |
Adrian Prantl | fbeeac0 | 2018-12-06 18:44:48 +0000 | [diff] [blame] | 747 | GCOVLines &Lines = Block.getFile(Filename); |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 748 | Lines.addLine(Loc.getLine()); |
| 749 | } |
Calixte Denizet | 38d5054 | 2018-10-30 18:41:31 +0000 | [diff] [blame] | 750 | Line = 0; |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 751 | } |
David Blaikie | 229de50 | 2014-04-21 20:41:55 +0000 | [diff] [blame] | 752 | EdgeDestinations += Func.getEdgeDestinations(); |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 753 | } |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 754 | |
Yuchen Wu | 664dc76 | 2013-11-21 04:01:05 +0000 | [diff] [blame] | 755 | FileChecksums.push_back(hash_value(EdgeDestinations)); |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 756 | out.write("oncg", 4); |
| 757 | out.write(ReversedVersion, 4); |
Yuchen Wu | 664dc76 | 2013-11-21 04:01:05 +0000 | [diff] [blame] | 758 | out.write(reinterpret_cast<char*>(&FileChecksums.back()), 4); |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 759 | |
David Blaikie | 229de50 | 2014-04-21 20:41:55 +0000 | [diff] [blame] | 760 | for (auto &Func : Funcs) { |
Yuchen Wu | 664dc76 | 2013-11-21 04:01:05 +0000 | [diff] [blame] | 761 | Func->setCfgChecksum(FileChecksums.back()); |
| 762 | Func->writeOut(); |
| 763 | } |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 764 | |
Nick Lewycky | 6404d97 | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 765 | out.write("\0\0\0\0\0\0\0\0", 8); // EOF |
| 766 | out.close(); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Devang Patel | 2b21d86 | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 770 | bool GCOVProfiler::emitProfileArcs() { |
| 771 | NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu"); |
| 772 | if (!CU_Nodes) return false; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 773 | |
Duncan P. N. Exon Smith | cec1c24 | 2014-03-11 02:44:45 +0000 | [diff] [blame] | 774 | bool Result = false; |
Devang Patel | 2b21d86 | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 775 | for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { |
Devang Patel | 2b21d86 | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 776 | SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP; |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 777 | for (auto &F : M->functions()) { |
| 778 | DISubprogram *SP = F.getSubprogram(); |
| 779 | if (!SP) continue; |
Calixte Denizet | c6fabea | 2018-11-12 09:01:43 +0000 | [diff] [blame] | 780 | if (!functionHasLines(F) || !isFunctionInstrumented(F)) |
| 781 | continue; |
Heejin Ahn | b4be38f | 2018-05-17 20:52:03 +0000 | [diff] [blame] | 782 | // TODO: Functions using scope-based EH are currently not supported. |
| 783 | if (isUsingScopeBasedEH(F)) continue; |
Devang Patel | 2b21d86 | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 784 | if (!Result) Result = true; |
Marco Castelluccio | 0dcf64a | 2017-10-13 13:49:15 +0000 | [diff] [blame] | 785 | |
Vedant Kumar | 727d895 | 2018-09-11 18:38:34 +0000 | [diff] [blame] | 786 | DenseMap<std::pair<BasicBlock *, BasicBlock *>, unsigned> EdgeToCounter; |
Devang Patel | 2b21d86 | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 787 | unsigned Edges = 0; |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 788 | for (auto &BB : F) { |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 789 | Instruction *TI = BB.getTerminator(); |
Vedant Kumar | 727d895 | 2018-09-11 18:38:34 +0000 | [diff] [blame] | 790 | if (isa<ReturnInst>(TI)) { |
| 791 | EdgeToCounter[{&BB, nullptr}] = Edges++; |
| 792 | } else { |
| 793 | for (BasicBlock *Succ : successors(TI)) { |
| 794 | EdgeToCounter[{&BB, Succ}] = Edges++; |
| 795 | } |
| 796 | } |
Devang Patel | 2b21d86 | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 797 | } |
Duncan P. N. Exon Smith | cec1c24 | 2014-03-11 02:44:45 +0000 | [diff] [blame] | 798 | |
Devang Patel | 2b21d86 | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 799 | ArrayType *CounterTy = |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 800 | ArrayType::get(Type::getInt64Ty(*Ctx), Edges); |
Devang Patel | 2b21d86 | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 801 | GlobalVariable *Counters = |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 802 | new GlobalVariable(*M, CounterTy, false, |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 803 | GlobalValue::InternalLinkage, |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 804 | Constant::getNullValue(CounterTy), |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 805 | "__llvm_gcov_ctr"); |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 806 | CountersBySP.push_back(std::make_pair(Counters, SP)); |
Duncan P. N. Exon Smith | cec1c24 | 2014-03-11 02:44:45 +0000 | [diff] [blame] | 807 | |
Vedant Kumar | 727d895 | 2018-09-11 18:38:34 +0000 | [diff] [blame] | 808 | // If a BB has several predecessors, use a PHINode to select |
| 809 | // the correct counter. |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 810 | for (auto &BB : F) { |
Vedant Kumar | 727d895 | 2018-09-11 18:38:34 +0000 | [diff] [blame] | 811 | const unsigned EdgeCount = |
| 812 | std::distance(pred_begin(&BB), pred_end(&BB)); |
| 813 | if (EdgeCount) { |
| 814 | // The phi node must be at the begin of the BB. |
| 815 | IRBuilder<> BuilderForPhi(&*BB.begin()); |
| 816 | Type *Int64PtrTy = Type::getInt64PtrTy(*Ctx); |
| 817 | PHINode *Phi = BuilderForPhi.CreatePHI(Int64PtrTy, EdgeCount); |
| 818 | for (BasicBlock *Pred : predecessors(&BB)) { |
| 819 | auto It = EdgeToCounter.find({Pred, &BB}); |
| 820 | assert(It != EdgeToCounter.end()); |
| 821 | const unsigned Edge = It->second; |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 822 | Value *EdgeCounter = BuilderForPhi.CreateConstInBoundsGEP2_64( |
| 823 | Counters->getValueType(), Counters, 0, Edge); |
Vedant Kumar | 727d895 | 2018-09-11 18:38:34 +0000 | [diff] [blame] | 824 | Phi->addIncoming(EdgeCounter, Pred); |
Devang Patel | 2b21d86 | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 825 | } |
Bill Wendling | 707f601 | 2013-08-20 23:52:00 +0000 | [diff] [blame] | 826 | |
Vedant Kumar | 727d895 | 2018-09-11 18:38:34 +0000 | [diff] [blame] | 827 | // Skip phis, landingpads. |
| 828 | IRBuilder<> Builder(&*BB.getFirstInsertionPt()); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 829 | Value *Count = Builder.CreateLoad(Builder.getInt64Ty(), Phi); |
Vedant Kumar | 727d895 | 2018-09-11 18:38:34 +0000 | [diff] [blame] | 830 | Count = Builder.CreateAdd(Count, Builder.getInt64(1)); |
| 831 | Builder.CreateStore(Count, Phi); |
Duncan P. N. Exon Smith | cec1c24 | 2014-03-11 02:44:45 +0000 | [diff] [blame] | 832 | |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 833 | Instruction *TI = BB.getTerminator(); |
Vedant Kumar | 727d895 | 2018-09-11 18:38:34 +0000 | [diff] [blame] | 834 | if (isa<ReturnInst>(TI)) { |
| 835 | auto It = EdgeToCounter.find({&BB, nullptr}); |
| 836 | assert(It != EdgeToCounter.end()); |
| 837 | const unsigned Edge = It->second; |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 838 | Value *Counter = Builder.CreateConstInBoundsGEP2_64( |
| 839 | Counters->getValueType(), Counters, 0, Edge); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 840 | Value *Count = Builder.CreateLoad(Builder.getInt64Ty(), Counter); |
Vedant Kumar | 727d895 | 2018-09-11 18:38:34 +0000 | [diff] [blame] | 841 | Count = Builder.CreateAdd(Count, Builder.getInt64(1)); |
| 842 | Builder.CreateStore(Count, Counter); |
| 843 | } |
Devang Patel | 2b21d86 | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 844 | } |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 845 | } |
| 846 | } |
Bill Wendling | e85f349 | 2012-06-01 23:14:32 +0000 | [diff] [blame] | 847 | |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 848 | Function *WriteoutF = insertCounterWriteout(CountersBySP); |
| 849 | Function *FlushF = insertFlush(CountersBySP); |
| 850 | |
| 851 | // Create a small bit of code that registers the "__llvm_gcov_writeout" to |
Bill Wendling | 04d57c7 | 2013-03-19 21:03:22 +0000 | [diff] [blame] | 852 | // be executed at exit and the "__llvm_gcov_flush" function to be executed |
| 853 | // when "__gcov_flush" is called. |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 854 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
| 855 | Function *F = Function::Create(FTy, GlobalValue::InternalLinkage, |
| 856 | "__llvm_gcov_init", M); |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 857 | F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 858 | F->setLinkage(GlobalValue::InternalLinkage); |
| 859 | F->addFnAttr(Attribute::NoInline); |
| 860 | if (Options.NoRedZone) |
| 861 | F->addFnAttr(Attribute::NoRedZone); |
| 862 | |
| 863 | BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F); |
| 864 | IRBuilder<> Builder(BB); |
| 865 | |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 866 | FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
Bill Wendling | c77e944 | 2013-03-20 21:13:59 +0000 | [diff] [blame] | 867 | Type *Params[] = { |
| 868 | PointerType::get(FTy, 0), |
| 869 | PointerType::get(FTy, 0) |
| 870 | }; |
| 871 | FTy = FunctionType::get(Builder.getVoidTy(), Params, false); |
Bill Wendling | 04d57c7 | 2013-03-19 21:03:22 +0000 | [diff] [blame] | 872 | |
Yuchen Wu | 3197b25 | 2013-10-23 20:35:00 +0000 | [diff] [blame] | 873 | // Initialize the environment and register the local writeout and flush |
Bill Wendling | c77e944 | 2013-03-20 21:13:59 +0000 | [diff] [blame] | 874 | // functions. |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 875 | FunctionCallee GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 876 | Builder.CreateCall(GCOVInit, {WriteoutF, FlushF}); |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 877 | Builder.CreateRetVoid(); |
| 878 | |
| 879 | appendToGlobalCtors(*M, F, 0); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 880 | } |
Bill Wendling | 1560517 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 881 | |
Devang Patel | 2b21d86 | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 882 | return Result; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 885 | FunctionCallee GCOVProfiler::getStartFileFunc(const TargetLibraryInfo *TLI) { |
Nick Lewycky | 492afe8 | 2013-03-07 08:28:49 +0000 | [diff] [blame] | 886 | Type *Args[] = { |
| 887 | Type::getInt8PtrTy(*Ctx), // const char *orig_filename |
| 888 | Type::getInt8PtrTy(*Ctx), // const char version[4] |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 889 | Type::getInt32Ty(*Ctx), // uint32_t checksum |
Nick Lewycky | 492afe8 | 2013-03-07 08:28:49 +0000 | [diff] [blame] | 890 | }; |
| 891 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 892 | AttributeList AL; |
| 893 | if (auto AK = TLI->getExtAttrForI32Param(false)) |
| 894 | AL = AL.addParamAttribute(*Ctx, 2, AK); |
| 895 | FunctionCallee Res = M->getOrInsertFunction("llvm_gcda_start_file", FTy, AL); |
Ulrich Weigand | b961fdc | 2018-07-10 16:05:47 +0000 | [diff] [blame] | 896 | return Res; |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 899 | FunctionCallee GCOVProfiler::getEmitFunctionFunc(const TargetLibraryInfo *TLI) { |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 900 | Type *Args[] = { |
Nick Lewycky | 4f9c367 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 901 | Type::getInt32Ty(*Ctx), // uint32_t ident |
| 902 | Type::getInt8PtrTy(*Ctx), // const char *function_name |
Daniel Jasper | 87a24d5 | 2013-12-04 08:57:17 +0000 | [diff] [blame] | 903 | Type::getInt32Ty(*Ctx), // uint32_t func_checksum |
Nick Lewycky | 88f1d0d | 2013-03-09 01:33:06 +0000 | [diff] [blame] | 904 | Type::getInt8Ty(*Ctx), // uint8_t use_extra_checksum |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 905 | Type::getInt32Ty(*Ctx), // uint32_t cfg_checksum |
Nick Lewycky | 4f9c367 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 906 | }; |
Bill Wendling | 8ed0749 | 2012-05-25 23:55:00 +0000 | [diff] [blame] | 907 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 908 | AttributeList AL; |
| 909 | if (auto AK = TLI->getExtAttrForI32Param(false)) { |
| 910 | AL = AL.addParamAttribute(*Ctx, 0, AK); |
| 911 | AL = AL.addParamAttribute(*Ctx, 2, AK); |
| 912 | AL = AL.addParamAttribute(*Ctx, 3, AK); |
| 913 | AL = AL.addParamAttribute(*Ctx, 4, AK); |
| 914 | } |
| 915 | return M->getOrInsertFunction("llvm_gcda_emit_function", FTy); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 918 | FunctionCallee GCOVProfiler::getEmitArcsFunc(const TargetLibraryInfo *TLI) { |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 919 | Type *Args[] = { |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 920 | Type::getInt32Ty(*Ctx), // uint32_t num_counters |
| 921 | Type::getInt64PtrTy(*Ctx), // uint64_t *counters |
| 922 | }; |
Nick Lewycky | 492afe8 | 2013-03-07 08:28:49 +0000 | [diff] [blame] | 923 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 924 | AttributeList AL; |
| 925 | if (auto AK = TLI->getExtAttrForI32Param(false)) |
| 926 | AL = AL.addParamAttribute(*Ctx, 0, AK); |
| 927 | return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy, AL); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 928 | } |
| 929 | |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 930 | FunctionCallee GCOVProfiler::getSummaryInfoFunc() { |
Yuchen Wu | 062f24c | 2013-11-12 04:59:08 +0000 | [diff] [blame] | 931 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
| 932 | return M->getOrInsertFunction("llvm_gcda_summary_info", FTy); |
| 933 | } |
| 934 | |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 935 | FunctionCallee GCOVProfiler::getEndFileFunc() { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 936 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 937 | return M->getOrInsertFunction("llvm_gcda_end_file", FTy); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 940 | Function *GCOVProfiler::insertCounterWriteout( |
Bill Wendling | e8aee6b | 2012-08-29 18:45:41 +0000 | [diff] [blame] | 941 | ArrayRef<std::pair<GlobalVariable *, MDNode *> > CountersBySP) { |
Bill Wendling | 2e6e866 | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 942 | FunctionType *WriteoutFTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
| 943 | Function *WriteoutF = M->getFunction("__llvm_gcov_writeout"); |
| 944 | if (!WriteoutF) |
| 945 | WriteoutF = Function::Create(WriteoutFTy, GlobalValue::InternalLinkage, |
| 946 | "__llvm_gcov_writeout", M); |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 947 | WriteoutF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 948 | WriteoutF->addFnAttr(Attribute::NoInline); |
Nick Lewycky | fdfed3e | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 949 | if (Options.NoRedZone) |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 950 | WriteoutF->addFnAttr(Attribute::NoRedZone); |
Bill Wendling | 2e6e866 | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 951 | |
| 952 | BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF); |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 953 | IRBuilder<> Builder(BB); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 954 | |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 955 | auto *TLI = &GetTLI(*WriteoutF); |
| 956 | |
| 957 | FunctionCallee StartFile = getStartFileFunc(TLI); |
| 958 | FunctionCallee EmitFunction = getEmitFunctionFunc(TLI); |
| 959 | FunctionCallee EmitArcs = getEmitArcsFunc(TLI); |
James Y Knight | 1368022 | 2019-02-01 02:28:03 +0000 | [diff] [blame] | 960 | FunctionCallee SummaryInfo = getSummaryInfoFunc(); |
| 961 | FunctionCallee EndFile = getEndFileFunc(); |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 962 | |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 963 | NamedMDNode *CUNodes = M->getNamedMetadata("llvm.dbg.cu"); |
| 964 | if (!CUNodes) { |
| 965 | Builder.CreateRetVoid(); |
| 966 | return WriteoutF; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 967 | } |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 968 | |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 969 | // Collect the relevant data into a large constant data structure that we can |
| 970 | // walk to write out everything. |
| 971 | StructType *StartFileCallArgsTy = StructType::create( |
| 972 | {Builder.getInt8PtrTy(), Builder.getInt8PtrTy(), Builder.getInt32Ty()}); |
| 973 | StructType *EmitFunctionCallArgsTy = StructType::create( |
| 974 | {Builder.getInt32Ty(), Builder.getInt8PtrTy(), Builder.getInt32Ty(), |
| 975 | Builder.getInt8Ty(), Builder.getInt32Ty()}); |
| 976 | StructType *EmitArcsCallArgsTy = StructType::create( |
| 977 | {Builder.getInt32Ty(), Builder.getInt64Ty()->getPointerTo()}); |
| 978 | StructType *FileInfoTy = |
| 979 | StructType::create({StartFileCallArgsTy, Builder.getInt32Ty(), |
| 980 | EmitFunctionCallArgsTy->getPointerTo(), |
| 981 | EmitArcsCallArgsTy->getPointerTo()}); |
| 982 | |
| 983 | Constant *Zero32 = Builder.getInt32(0); |
Chandler Carruth | e74c354 | 2018-05-03 00:11:03 +0000 | [diff] [blame] | 984 | // Build an explicit array of two zeros for use in ConstantExpr GEP building. |
| 985 | Constant *TwoZero32s[] = {Zero32, Zero32}; |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 986 | |
| 987 | SmallVector<Constant *, 8> FileInfos; |
| 988 | for (int i : llvm::seq<int>(0, CUNodes->getNumOperands())) { |
| 989 | auto *CU = cast<DICompileUnit>(CUNodes->getOperand(i)); |
| 990 | |
| 991 | // Skip module skeleton (and module) CUs. |
| 992 | if (CU->getDWOId()) |
| 993 | continue; |
| 994 | |
| 995 | std::string FilenameGcda = mangleName(CU, GCovFileType::GCDA); |
| 996 | uint32_t CfgChecksum = FileChecksums.empty() ? 0 : FileChecksums[i]; |
| 997 | auto *StartFileCallArgs = ConstantStruct::get( |
| 998 | StartFileCallArgsTy, {Builder.CreateGlobalStringPtr(FilenameGcda), |
| 999 | Builder.CreateGlobalStringPtr(ReversedVersion), |
| 1000 | Builder.getInt32(CfgChecksum)}); |
| 1001 | |
| 1002 | SmallVector<Constant *, 8> EmitFunctionCallArgsArray; |
| 1003 | SmallVector<Constant *, 8> EmitArcsCallArgsArray; |
| 1004 | for (int j : llvm::seq<int>(0, CountersBySP.size())) { |
| 1005 | auto *SP = cast_or_null<DISubprogram>(CountersBySP[j].second); |
| 1006 | uint32_t FuncChecksum = Funcs.empty() ? 0 : Funcs[j]->getFuncChecksum(); |
| 1007 | EmitFunctionCallArgsArray.push_back(ConstantStruct::get( |
| 1008 | EmitFunctionCallArgsTy, |
| 1009 | {Builder.getInt32(j), |
| 1010 | Options.FunctionNamesInData |
| 1011 | ? Builder.CreateGlobalStringPtr(getFunctionName(SP)) |
| 1012 | : Constant::getNullValue(Builder.getInt8PtrTy()), |
| 1013 | Builder.getInt32(FuncChecksum), |
| 1014 | Builder.getInt8(Options.UseCfgChecksum), |
| 1015 | Builder.getInt32(CfgChecksum)})); |
| 1016 | |
| 1017 | GlobalVariable *GV = CountersBySP[j].first; |
| 1018 | unsigned Arcs = cast<ArrayType>(GV->getValueType())->getNumElements(); |
| 1019 | EmitArcsCallArgsArray.push_back(ConstantStruct::get( |
| 1020 | EmitArcsCallArgsTy, |
Chandler Carruth | e74c354 | 2018-05-03 00:11:03 +0000 | [diff] [blame] | 1021 | {Builder.getInt32(Arcs), ConstantExpr::getInBoundsGetElementPtr( |
| 1022 | GV->getValueType(), GV, TwoZero32s)})); |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 1023 | } |
| 1024 | // Create global arrays for the two emit calls. |
| 1025 | int CountersSize = CountersBySP.size(); |
| 1026 | assert(CountersSize == (int)EmitFunctionCallArgsArray.size() && |
| 1027 | "Mismatched array size!"); |
| 1028 | assert(CountersSize == (int)EmitArcsCallArgsArray.size() && |
| 1029 | "Mismatched array size!"); |
| 1030 | auto *EmitFunctionCallArgsArrayTy = |
| 1031 | ArrayType::get(EmitFunctionCallArgsTy, CountersSize); |
| 1032 | auto *EmitFunctionCallArgsArrayGV = new GlobalVariable( |
| 1033 | *M, EmitFunctionCallArgsArrayTy, /*isConstant*/ true, |
| 1034 | GlobalValue::InternalLinkage, |
| 1035 | ConstantArray::get(EmitFunctionCallArgsArrayTy, |
| 1036 | EmitFunctionCallArgsArray), |
| 1037 | Twine("__llvm_internal_gcov_emit_function_args.") + Twine(i)); |
| 1038 | auto *EmitArcsCallArgsArrayTy = |
| 1039 | ArrayType::get(EmitArcsCallArgsTy, CountersSize); |
| 1040 | EmitFunctionCallArgsArrayGV->setUnnamedAddr( |
| 1041 | GlobalValue::UnnamedAddr::Global); |
| 1042 | auto *EmitArcsCallArgsArrayGV = new GlobalVariable( |
| 1043 | *M, EmitArcsCallArgsArrayTy, /*isConstant*/ true, |
| 1044 | GlobalValue::InternalLinkage, |
| 1045 | ConstantArray::get(EmitArcsCallArgsArrayTy, EmitArcsCallArgsArray), |
| 1046 | Twine("__llvm_internal_gcov_emit_arcs_args.") + Twine(i)); |
| 1047 | EmitArcsCallArgsArrayGV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
| 1048 | |
| 1049 | FileInfos.push_back(ConstantStruct::get( |
| 1050 | FileInfoTy, |
| 1051 | {StartFileCallArgs, Builder.getInt32(CountersSize), |
Chandler Carruth | e74c354 | 2018-05-03 00:11:03 +0000 | [diff] [blame] | 1052 | ConstantExpr::getInBoundsGetElementPtr(EmitFunctionCallArgsArrayTy, |
| 1053 | EmitFunctionCallArgsArrayGV, |
| 1054 | TwoZero32s), |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 1055 | ConstantExpr::getInBoundsGetElementPtr( |
Chandler Carruth | e74c354 | 2018-05-03 00:11:03 +0000 | [diff] [blame] | 1056 | EmitArcsCallArgsArrayTy, EmitArcsCallArgsArrayGV, TwoZero32s)})); |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | // If we didn't find anything to actually emit, bail on out. |
| 1060 | if (FileInfos.empty()) { |
| 1061 | Builder.CreateRetVoid(); |
| 1062 | return WriteoutF; |
| 1063 | } |
| 1064 | |
| 1065 | // To simplify code, we cap the number of file infos we write out to fit |
| 1066 | // easily in a 32-bit signed integer. This gives consistent behavior between |
| 1067 | // 32-bit and 64-bit systems without requiring (potentially very slow) 64-bit |
| 1068 | // operations on 32-bit systems. It also seems unreasonable to try to handle |
| 1069 | // more than 2 billion files. |
| 1070 | if ((int64_t)FileInfos.size() > (int64_t)INT_MAX) |
| 1071 | FileInfos.resize(INT_MAX); |
| 1072 | |
| 1073 | // Create a global for the entire data structure so we can walk it more |
| 1074 | // easily. |
| 1075 | auto *FileInfoArrayTy = ArrayType::get(FileInfoTy, FileInfos.size()); |
| 1076 | auto *FileInfoArrayGV = new GlobalVariable( |
| 1077 | *M, FileInfoArrayTy, /*isConstant*/ true, GlobalValue::InternalLinkage, |
| 1078 | ConstantArray::get(FileInfoArrayTy, FileInfos), |
| 1079 | "__llvm_internal_gcov_emit_file_info"); |
| 1080 | FileInfoArrayGV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
| 1081 | |
| 1082 | // Create the CFG for walking this data structure. |
| 1083 | auto *FileLoopHeader = |
| 1084 | BasicBlock::Create(*Ctx, "file.loop.header", WriteoutF); |
| 1085 | auto *CounterLoopHeader = |
| 1086 | BasicBlock::Create(*Ctx, "counter.loop.header", WriteoutF); |
| 1087 | auto *FileLoopLatch = BasicBlock::Create(*Ctx, "file.loop.latch", WriteoutF); |
| 1088 | auto *ExitBB = BasicBlock::Create(*Ctx, "exit", WriteoutF); |
| 1089 | |
| 1090 | // We always have at least one file, so just branch to the header. |
| 1091 | Builder.CreateBr(FileLoopHeader); |
| 1092 | |
| 1093 | // The index into the files structure is our loop induction variable. |
| 1094 | Builder.SetInsertPoint(FileLoopHeader); |
| 1095 | PHINode *IV = |
| 1096 | Builder.CreatePHI(Builder.getInt32Ty(), /*NumReservedValues*/ 2); |
| 1097 | IV->addIncoming(Builder.getInt32(0), BB); |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1098 | auto *FileInfoPtr = Builder.CreateInBoundsGEP( |
| 1099 | FileInfoArrayTy, FileInfoArrayGV, {Builder.getInt32(0), IV}); |
| 1100 | auto *StartFileCallArgsPtr = |
| 1101 | Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 0); |
Ulrich Weigand | b961fdc | 2018-07-10 16:05:47 +0000 | [diff] [blame] | 1102 | auto *StartFileCall = Builder.CreateCall( |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 1103 | StartFile, |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1104 | {Builder.CreateLoad(StartFileCallArgsTy->getElementType(0), |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1105 | Builder.CreateStructGEP(StartFileCallArgsTy, |
| 1106 | StartFileCallArgsPtr, 0)), |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1107 | Builder.CreateLoad(StartFileCallArgsTy->getElementType(1), |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1108 | Builder.CreateStructGEP(StartFileCallArgsTy, |
| 1109 | StartFileCallArgsPtr, 1)), |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1110 | Builder.CreateLoad(StartFileCallArgsTy->getElementType(2), |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1111 | Builder.CreateStructGEP(StartFileCallArgsTy, |
| 1112 | StartFileCallArgsPtr, 2))}); |
Ulrich Weigand | b961fdc | 2018-07-10 16:05:47 +0000 | [diff] [blame] | 1113 | if (auto AK = TLI->getExtAttrForI32Param(false)) |
| 1114 | StartFileCall->addParamAttr(2, AK); |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1115 | auto *NumCounters = |
| 1116 | Builder.CreateLoad(FileInfoTy->getElementType(1), |
| 1117 | Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 1)); |
| 1118 | auto *EmitFunctionCallArgsArray = |
| 1119 | Builder.CreateLoad(FileInfoTy->getElementType(2), |
| 1120 | Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 2)); |
| 1121 | auto *EmitArcsCallArgsArray = |
| 1122 | Builder.CreateLoad(FileInfoTy->getElementType(3), |
| 1123 | Builder.CreateStructGEP(FileInfoTy, FileInfoPtr, 3)); |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 1124 | auto *EnterCounterLoopCond = |
| 1125 | Builder.CreateICmpSLT(Builder.getInt32(0), NumCounters); |
| 1126 | Builder.CreateCondBr(EnterCounterLoopCond, CounterLoopHeader, FileLoopLatch); |
| 1127 | |
| 1128 | Builder.SetInsertPoint(CounterLoopHeader); |
| 1129 | auto *JV = Builder.CreatePHI(Builder.getInt32Ty(), /*NumReservedValues*/ 2); |
| 1130 | JV->addIncoming(Builder.getInt32(0), FileLoopHeader); |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1131 | auto *EmitFunctionCallArgsPtr = Builder.CreateInBoundsGEP( |
| 1132 | EmitFunctionCallArgsTy, EmitFunctionCallArgsArray, JV); |
Ulrich Weigand | b961fdc | 2018-07-10 16:05:47 +0000 | [diff] [blame] | 1133 | auto *EmitFunctionCall = Builder.CreateCall( |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 1134 | EmitFunction, |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1135 | {Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(0), |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1136 | Builder.CreateStructGEP(EmitFunctionCallArgsTy, |
| 1137 | EmitFunctionCallArgsPtr, 0)), |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1138 | Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(1), |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1139 | Builder.CreateStructGEP(EmitFunctionCallArgsTy, |
| 1140 | EmitFunctionCallArgsPtr, 1)), |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1141 | Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(2), |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1142 | Builder.CreateStructGEP(EmitFunctionCallArgsTy, |
| 1143 | EmitFunctionCallArgsPtr, 2)), |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1144 | Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(3), |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1145 | Builder.CreateStructGEP(EmitFunctionCallArgsTy, |
| 1146 | EmitFunctionCallArgsPtr, 3)), |
| 1147 | Builder.CreateLoad(EmitFunctionCallArgsTy->getElementType(4), |
| 1148 | Builder.CreateStructGEP(EmitFunctionCallArgsTy, |
| 1149 | EmitFunctionCallArgsPtr, |
| 1150 | 4))}); |
Ulrich Weigand | b961fdc | 2018-07-10 16:05:47 +0000 | [diff] [blame] | 1151 | if (auto AK = TLI->getExtAttrForI32Param(false)) { |
| 1152 | EmitFunctionCall->addParamAttr(0, AK); |
| 1153 | EmitFunctionCall->addParamAttr(2, AK); |
| 1154 | EmitFunctionCall->addParamAttr(3, AK); |
| 1155 | EmitFunctionCall->addParamAttr(4, AK); |
| 1156 | } |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 1157 | auto *EmitArcsCallArgsPtr = |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1158 | Builder.CreateInBoundsGEP(EmitArcsCallArgsTy, EmitArcsCallArgsArray, JV); |
Ulrich Weigand | b961fdc | 2018-07-10 16:05:47 +0000 | [diff] [blame] | 1159 | auto *EmitArcsCall = Builder.CreateCall( |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 1160 | EmitArcs, |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1161 | {Builder.CreateLoad( |
| 1162 | EmitArcsCallArgsTy->getElementType(0), |
| 1163 | Builder.CreateStructGEP(EmitArcsCallArgsTy, EmitArcsCallArgsPtr, 0)), |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1164 | Builder.CreateLoad(EmitArcsCallArgsTy->getElementType(1), |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1165 | Builder.CreateStructGEP(EmitArcsCallArgsTy, |
| 1166 | EmitArcsCallArgsPtr, 1))}); |
Ulrich Weigand | b961fdc | 2018-07-10 16:05:47 +0000 | [diff] [blame] | 1167 | if (auto AK = TLI->getExtAttrForI32Param(false)) |
| 1168 | EmitArcsCall->addParamAttr(0, AK); |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 1169 | auto *NextJV = Builder.CreateAdd(JV, Builder.getInt32(1)); |
| 1170 | auto *CounterLoopCond = Builder.CreateICmpSLT(NextJV, NumCounters); |
| 1171 | Builder.CreateCondBr(CounterLoopCond, CounterLoopHeader, FileLoopLatch); |
| 1172 | JV->addIncoming(NextJV, CounterLoopHeader); |
| 1173 | |
| 1174 | Builder.SetInsertPoint(FileLoopLatch); |
| 1175 | Builder.CreateCall(SummaryInfo, {}); |
| 1176 | Builder.CreateCall(EndFile, {}); |
| 1177 | auto *NextIV = Builder.CreateAdd(IV, Builder.getInt32(1)); |
| 1178 | auto *FileLoopCond = |
| 1179 | Builder.CreateICmpSLT(NextIV, Builder.getInt32(FileInfos.size())); |
| 1180 | Builder.CreateCondBr(FileLoopCond, FileLoopHeader, ExitBB); |
| 1181 | IV->addIncoming(NextIV, FileLoopLatch); |
| 1182 | |
| 1183 | Builder.SetInsertPoint(ExitBB); |
Nick Lewycky | c58d293 | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 1184 | Builder.CreateRetVoid(); |
Chandler Carruth | 71c3a3f | 2018-05-02 22:24:39 +0000 | [diff] [blame] | 1185 | |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 1186 | return WriteoutF; |
Nick Lewycky | 966edd0 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 1187 | } |
Bill Wendling | 1560517 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 1188 | |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 1189 | Function *GCOVProfiler:: |
Bill Wendling | 2e6e866 | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 1190 | insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> > CountersBySP) { |
| 1191 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 1192 | Function *FlushF = M->getFunction("__llvm_gcov_flush"); |
Bill Wendling | 2e6e866 | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 1193 | if (!FlushF) |
| 1194 | FlushF = Function::Create(FTy, GlobalValue::InternalLinkage, |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 1195 | "__llvm_gcov_flush", M); |
Bill Wendling | 2e6e866 | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 1196 | else |
| 1197 | FlushF->setLinkage(GlobalValue::InternalLinkage); |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 1198 | FlushF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1199 | FlushF->addFnAttr(Attribute::NoInline); |
Nick Lewycky | fdfed3e | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 1200 | if (Options.NoRedZone) |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1201 | FlushF->addFnAttr(Attribute::NoRedZone); |
Bill Wendling | 2e6e866 | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 1202 | |
Bill Wendling | 2e6e866 | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 1203 | BasicBlock *Entry = BasicBlock::Create(*Ctx, "entry", FlushF); |
| 1204 | |
| 1205 | // Write out the current counters. |
James Y Knight | 7976eb5 | 2019-02-01 20:43:25 +0000 | [diff] [blame] | 1206 | Function *WriteoutF = M->getFunction("__llvm_gcov_writeout"); |
Bill Wendling | 2e6e866 | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 1207 | assert(WriteoutF && "Need to create the writeout function first!"); |
| 1208 | |
| 1209 | IRBuilder<> Builder(Entry); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1210 | Builder.CreateCall(WriteoutF, {}); |
Bill Wendling | 2e6e866 | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 1211 | |
Bill Wendling | fb1f668 | 2012-09-13 14:32:30 +0000 | [diff] [blame] | 1212 | // Zero out the counters. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 1213 | for (const auto &I : CountersBySP) { |
| 1214 | GlobalVariable *GV = I.first; |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 1215 | Constant *Null = Constant::getNullValue(GV->getValueType()); |
Bill Wendling | 8d26bc3 | 2012-09-14 22:35:49 +0000 | [diff] [blame] | 1216 | Builder.CreateStore(Null, GV); |
Bill Wendling | fb1f668 | 2012-09-13 14:32:30 +0000 | [diff] [blame] | 1217 | } |
Bill Wendling | 2e6e866 | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 1218 | |
| 1219 | Type *RetTy = FlushF->getReturnType(); |
| 1220 | if (RetTy == Type::getVoidTy(*Ctx)) |
| 1221 | Builder.CreateRetVoid(); |
| 1222 | else if (RetTy->isIntegerTy()) |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 1223 | // Used if __llvm_gcov_flush was implicitly declared. |
Bill Wendling | 2e6e866 | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 1224 | Builder.CreateRet(ConstantInt::get(RetTy, 0)); |
| 1225 | else |
Bill Wendling | c3cab81 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 1226 | report_fatal_error("invalid return type for __llvm_gcov_flush"); |
| 1227 | |
| 1228 | return FlushF; |
Bill Wendling | 2e6e866 | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 1229 | } |