Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 1 | //===- GCOVProfiling.cpp - Insert edge counters for gcov profiling --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass implements GCOV-style profiling. When this pass is run it emits |
| 11 | // "gcno" files next to the existing source, and instruments the code that runs |
| 12 | // to records the edges between blocks that run and emit a complementary "gcda" |
| 13 | // file on exit. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #define DEBUG_TYPE "insert-gcov-profiling" |
| 18 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Instrumentation.h" |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 21 | #include "llvm/ADT/Hashing.h" |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
| 25 | #include "llvm/ADT/StringMap.h" |
| 26 | #include "llvm/ADT/UniqueVector.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 27 | #include "llvm/IR/DebugInfo.h" |
| 28 | #include "llvm/IR/DebugLoc.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/IRBuilder.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 30 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Instructions.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 32 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Module.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 34 | #include "llvm/Pass.h" |
Nick Lewycky | a204ef3 | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
Bill Wendling | 39c41c3 | 2013-03-26 22:47:50 +0000 | [diff] [blame] | 37 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | a11c3e2 | 2013-06-11 22:21:28 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Path.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
| 40 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Nick Lewycky | c4e6b54 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 41 | #include <algorithm> |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 42 | #include <string> |
| 43 | #include <utility> |
| 44 | using namespace llvm; |
| 45 | |
Nick Lewycky | a204ef3 | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 46 | static cl::opt<std::string> |
| 47 | DefaultGCOVVersion("default-gcov-version", cl::init("402*"), cl::Hidden, |
| 48 | cl::ValueRequired); |
| 49 | |
| 50 | GCOVOptions GCOVOptions::getDefault() { |
| 51 | GCOVOptions Options; |
| 52 | Options.EmitNotes = true; |
| 53 | Options.EmitData = true; |
| 54 | Options.UseCfgChecksum = false; |
| 55 | Options.NoRedZone = false; |
| 56 | Options.FunctionNamesInData = true; |
| 57 | |
| 58 | if (DefaultGCOVVersion.size() != 4) { |
| 59 | llvm::report_fatal_error(std::string("Invalid -default-gcov-version: ") + |
| 60 | DefaultGCOVVersion); |
| 61 | } |
| 62 | memcpy(Options.Version, DefaultGCOVVersion.c_str(), 4); |
| 63 | return Options; |
| 64 | } |
| 65 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 66 | namespace { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 67 | class GCOVFunction; |
| 68 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 69 | class GCOVProfiler : public ModulePass { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 70 | public: |
| 71 | static char ID; |
Nick Lewycky | a204ef3 | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 72 | GCOVProfiler() : ModulePass(ID), Options(GCOVOptions::getDefault()) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 73 | init(); |
Nick Lewycky | a61e52c | 2011-04-21 01:56:25 +0000 | [diff] [blame] | 74 | } |
Nick Lewycky | a204ef3 | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 75 | GCOVProfiler(const GCOVOptions &Options) : ModulePass(ID), Options(Options){ |
| 76 | assert((Options.EmitNotes || Options.EmitData) && |
| 77 | "GCOVProfiler asked to do nothing?"); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 78 | init(); |
| 79 | } |
| 80 | ~GCOVProfiler() { |
| 81 | DeleteContainerPointers(Funcs); |
| 82 | } |
| 83 | const char *getPassName() const override { |
| 84 | return "GCOV Profiler"; |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | void init() { |
Nick Lewycky | a204ef3 | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 89 | ReversedVersion[0] = Options.Version[3]; |
| 90 | ReversedVersion[1] = Options.Version[2]; |
| 91 | ReversedVersion[2] = Options.Version[1]; |
| 92 | ReversedVersion[3] = Options.Version[0]; |
| 93 | ReversedVersion[4] = '\0'; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 94 | initializeGCOVProfilerPass(*PassRegistry::getPassRegistry()); |
| 95 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 96 | bool runOnModule(Module &M) override; |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 97 | |
Nick Lewycky | 64a0a33 | 2013-03-13 22:55:42 +0000 | [diff] [blame] | 98 | // Create the .gcno files for the Module based on DebugInfo. |
| 99 | void emitProfileNotes(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 100 | |
Nick Lewycky | 0c4de8a | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 101 | // Modify the program to track transitions along edges and call into the |
| 102 | // profiling runtime to emit .gcda files when run. |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 103 | bool emitProfileArcs(); |
Nick Lewycky | 0c4de8a | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 104 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 105 | // Get pointers to the functions in the runtime library. |
| 106 | Constant *getStartFileFunc(); |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 107 | Constant *getIncrementIndirectCounterFunc(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 108 | Constant *getEmitFunctionFunc(); |
| 109 | Constant *getEmitArcsFunc(); |
Yuchen Wu | f42264e | 2013-11-12 04:59:08 +0000 | [diff] [blame] | 110 | Constant *getSummaryInfoFunc(); |
Bill Wendling | 1876471 | 2013-03-19 21:03:22 +0000 | [diff] [blame] | 111 | Constant *getDeleteWriteoutFunctionListFunc(); |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 112 | Constant *getDeleteFlushFunctionListFunc(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 113 | Constant *getEndFileFunc(); |
| 114 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 115 | // Create or retrieve an i32 state value that is used to represent the |
| 116 | // pred block number for certain non-trivial edges. |
| 117 | GlobalVariable *getEdgeStateValue(); |
| 118 | |
| 119 | // Produce a table of pointers to counters, by predecessor and successor |
| 120 | // block number. |
| 121 | GlobalVariable *buildEdgeLookupTable(Function *F, |
| 122 | GlobalVariable *Counter, |
Nick Lewycky | 64a0a33 | 2013-03-13 22:55:42 +0000 | [diff] [blame] | 123 | const UniqueVector<BasicBlock *>&Preds, |
| 124 | const UniqueVector<BasicBlock*>&Succs); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 125 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 126 | // Add the function to write out all our counters to the global destructor |
| 127 | // list. |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 128 | Function *insertCounterWriteout(ArrayRef<std::pair<GlobalVariable*, |
| 129 | MDNode*> >); |
| 130 | Function *insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> >); |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 131 | void insertIndirectCounterIncrement(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 132 | |
Bill Wendling | f2a2806 | 2013-03-28 22:40:08 +0000 | [diff] [blame] | 133 | std::string mangleName(DICompileUnit CU, const char *NewStem); |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 134 | |
Nick Lewycky | a204ef3 | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 135 | GCOVOptions Options; |
| 136 | |
| 137 | // Reversed, NUL-terminated copy of Options.Version. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 138 | char ReversedVersion[5]; |
| 139 | // Checksum, produced by hash of EdgeDestinations |
| 140 | SmallVector<uint32_t, 4> FileChecksums; |
Nick Lewycky | a61e52c | 2011-04-21 01:56:25 +0000 | [diff] [blame] | 141 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 142 | Module *M; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 143 | LLVMContext *Ctx; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 144 | SmallVector<GCOVFunction *, 16> Funcs; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 145 | }; |
| 146 | } |
| 147 | |
| 148 | char GCOVProfiler::ID = 0; |
| 149 | INITIALIZE_PASS(GCOVProfiler, "insert-gcov-profiling", |
| 150 | "Insert instrumentation for GCOV profiling", false, false) |
| 151 | |
Nick Lewycky | a204ef3 | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 152 | ModulePass *llvm::createGCOVProfilerPass(const GCOVOptions &Options) { |
| 153 | return new GCOVProfiler(Options); |
Nick Lewycky | a61e52c | 2011-04-21 01:56:25 +0000 | [diff] [blame] | 154 | } |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 155 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 156 | static StringRef getFunctionName(DISubprogram SP) { |
Nick Lewycky | 5d22d02 | 2013-03-19 01:37:55 +0000 | [diff] [blame] | 157 | if (!SP.getLinkageName().empty()) |
| 158 | return SP.getLinkageName(); |
| 159 | return SP.getName(); |
| 160 | } |
| 161 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 162 | namespace { |
| 163 | class GCOVRecord { |
| 164 | protected: |
Craig Topper | d6d6a97 | 2013-07-17 03:43:10 +0000 | [diff] [blame] | 165 | static const char *const LinesTag; |
| 166 | static const char *const FunctionTag; |
| 167 | static const char *const BlockTag; |
| 168 | static const char *const EdgeTag; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 169 | |
| 170 | GCOVRecord() {} |
| 171 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 172 | void writeBytes(const char *Bytes, int Size) { |
| 173 | os->write(Bytes, Size); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 176 | void write(uint32_t i) { |
| 177 | writeBytes(reinterpret_cast<char*>(&i), 4); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Returns the length measured in 4-byte blocks that will be used to |
| 181 | // represent this string in a GCOV file |
Craig Topper | 619850c | 2013-07-17 03:54:53 +0000 | [diff] [blame] | 182 | static unsigned lengthOfGCOVString(StringRef s) { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 183 | // A GCOV string is a length, followed by a NUL, then between 0 and 3 NULs |
Nick Lewycky | 17df2c3 | 2011-04-21 02:48:39 +0000 | [diff] [blame] | 184 | // padding out to the next 4-byte word. The length is measured in 4-byte |
| 185 | // words including padding, not bytes of actual string. |
Nick Lewycky | d363ff3 | 2011-05-05 23:52:18 +0000 | [diff] [blame] | 186 | return (s.size() / 4) + 1; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 189 | void writeGCOVString(StringRef s) { |
| 190 | uint32_t Len = lengthOfGCOVString(s); |
| 191 | write(Len); |
| 192 | writeBytes(s.data(), s.size()); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 193 | |
| 194 | // Write 1 to 4 bytes of NUL padding. |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 195 | assert((unsigned)(4 - (s.size() % 4)) > 0); |
| 196 | assert((unsigned)(4 - (s.size() % 4)) <= 4); |
| 197 | writeBytes("\0\0\0\0", 4 - (s.size() % 4)); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | raw_ostream *os; |
| 201 | }; |
Craig Topper | d6d6a97 | 2013-07-17 03:43:10 +0000 | [diff] [blame] | 202 | const char *const GCOVRecord::LinesTag = "\0\0\x45\x01"; |
| 203 | const char *const GCOVRecord::FunctionTag = "\0\0\0\1"; |
| 204 | const char *const GCOVRecord::BlockTag = "\0\0\x41\x01"; |
| 205 | const char *const GCOVRecord::EdgeTag = "\0\0\x43\x01"; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 206 | |
| 207 | class GCOVFunction; |
| 208 | class GCOVBlock; |
| 209 | |
| 210 | // Constructed only by requesting it from a GCOVBlock, this object stores a |
Devang Patel | 16c19a1 | 2011-09-20 18:35:00 +0000 | [diff] [blame] | 211 | // list of line numbers and a single filename, representing lines that belong |
| 212 | // to the block. |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 213 | class GCOVLines : public GCOVRecord { |
| 214 | public: |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 215 | void addLine(uint32_t Line) { |
| 216 | Lines.push_back(Line); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Craig Topper | 619850c | 2013-07-17 03:54:53 +0000 | [diff] [blame] | 219 | uint32_t length() const { |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 220 | // Here 2 = 1 for string length + 1 for '0' id#. |
Devang Patel | 16c19a1 | 2011-09-20 18:35:00 +0000 | [diff] [blame] | 221 | return lengthOfGCOVString(Filename) + 2 + Lines.size(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Devang Patel | 16c19a1 | 2011-09-20 18:35:00 +0000 | [diff] [blame] | 224 | void writeOut() { |
| 225 | write(0); |
| 226 | writeGCOVString(Filename); |
| 227 | for (int i = 0, e = Lines.size(); i != e; ++i) |
| 228 | write(Lines[i]); |
| 229 | } |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 230 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 231 | GCOVLines(StringRef F, raw_ostream *os) |
Devang Patel | 16c19a1 | 2011-09-20 18:35:00 +0000 | [diff] [blame] | 232 | : Filename(F) { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 233 | this->os = os; |
| 234 | } |
| 235 | |
Devang Patel | 680018f | 2011-09-20 18:48:56 +0000 | [diff] [blame] | 236 | private: |
Devang Patel | 16c19a1 | 2011-09-20 18:35:00 +0000 | [diff] [blame] | 237 | StringRef Filename; |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 238 | SmallVector<uint32_t, 32> Lines; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
Nick Lewycky | c4e6b54 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 241 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 242 | // Represent a basic block in GCOV. Each block has a unique number in the |
| 243 | // function, number of lines belonging to each block, and a set of edges to |
| 244 | // other blocks. |
| 245 | class GCOVBlock : public GCOVRecord { |
| 246 | public: |
Devang Patel | 68155d3 | 2011-09-20 17:55:19 +0000 | [diff] [blame] | 247 | GCOVLines &getFile(StringRef Filename) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 248 | GCOVLines *&Lines = LinesByFile[Filename]; |
| 249 | if (!Lines) { |
Devang Patel | 16c19a1 | 2011-09-20 18:35:00 +0000 | [diff] [blame] | 250 | Lines = new GCOVLines(Filename, os); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 251 | } |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 252 | return *Lines; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 255 | void addEdge(GCOVBlock &Successor) { |
| 256 | OutEdges.push_back(&Successor); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 259 | void writeOut() { |
| 260 | uint32_t Len = 3; |
Nick Lewycky | c4e6b54 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 261 | SmallVector<StringMapEntry<GCOVLines *> *, 32> SortedLinesByFile; |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 262 | for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(), |
| 263 | E = LinesByFile.end(); I != E; ++I) { |
Devang Patel | 16c19a1 | 2011-09-20 18:35:00 +0000 | [diff] [blame] | 264 | Len += I->second->length(); |
Nick Lewycky | c4e6b54 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 265 | SortedLinesByFile.push_back(&*I); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 268 | writeBytes(LinesTag, 4); |
| 269 | write(Len); |
| 270 | write(Number); |
Nick Lewycky | c4e6b54 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 271 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 272 | std::sort(SortedLinesByFile.begin(), SortedLinesByFile.end(), |
| 273 | [](StringMapEntry<GCOVLines *> *LHS, |
| 274 | StringMapEntry<GCOVLines *> *RHS) { |
| 275 | return LHS->getKey() < RHS->getKey(); |
| 276 | }); |
Craig Topper | 6227d5c | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 277 | for (SmallVectorImpl<StringMapEntry<GCOVLines *> *>::iterator |
Nick Lewycky | c4e6b54 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 278 | I = SortedLinesByFile.begin(), E = SortedLinesByFile.end(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 279 | I != E; ++I) |
Nick Lewycky | c4e6b54 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 280 | (*I)->getValue()->writeOut(); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 281 | write(0); |
| 282 | write(0); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | ~GCOVBlock() { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 286 | DeleteContainerSeconds(LinesByFile); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | private: |
| 290 | friend class GCOVFunction; |
| 291 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 292 | GCOVBlock(uint32_t Number, raw_ostream *os) |
| 293 | : Number(Number) { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 294 | this->os = os; |
| 295 | } |
| 296 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 297 | uint32_t Number; |
| 298 | StringMap<GCOVLines *> LinesByFile; |
| 299 | SmallVector<GCOVBlock *, 4> OutEdges; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | // A function has a unique identifier, a checksum (we leave as zero) and a |
| 303 | // set of blocks and a map of edges between blocks. This is the only GCOV |
| 304 | // object users can construct, the blocks and lines will be rooted here. |
| 305 | class GCOVFunction : public GCOVRecord { |
| 306 | public: |
Nick Lewycky | d9686a9 | 2013-03-07 08:28:49 +0000 | [diff] [blame] | 307 | GCOVFunction(DISubprogram SP, raw_ostream *os, uint32_t Ident, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 308 | bool UseCfgChecksum) : |
| 309 | SP(SP), Ident(Ident), UseCfgChecksum(UseCfgChecksum), CfgChecksum(0) { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 310 | this->os = os; |
| 311 | |
| 312 | Function *F = SP.getFunction(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 313 | DEBUG(dbgs() << "Function: " << getFunctionName(SP) << "\n"); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 314 | uint32_t i = 0; |
| 315 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 316 | Blocks[BB] = new GCOVBlock(i++, os); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 317 | } |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 318 | ReturnBlock = new GCOVBlock(i++, os); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 319 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 320 | std::string FunctionNameAndLine; |
| 321 | raw_string_ostream FNLOS(FunctionNameAndLine); |
| 322 | FNLOS << getFunctionName(SP) << SP.getLineNumber(); |
| 323 | FNLOS.flush(); |
| 324 | FuncChecksum = hash_value(FunctionNameAndLine); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | ~GCOVFunction() { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 328 | DeleteContainerSeconds(Blocks); |
| 329 | delete ReturnBlock; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 332 | GCOVBlock &getBlock(BasicBlock *BB) { |
| 333 | return *Blocks[BB]; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 336 | GCOVBlock &getReturnBlock() { |
| 337 | return *ReturnBlock; |
Nick Lewycky | a4c4c0e | 2011-04-21 03:18:00 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 340 | std::string getEdgeDestinations() { |
| 341 | std::string EdgeDestinations; |
| 342 | raw_string_ostream EDOS(EdgeDestinations); |
| 343 | Function *F = Blocks.begin()->first->getParent(); |
| 344 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 345 | GCOVBlock &Block = *Blocks[I]; |
| 346 | for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) |
| 347 | EDOS << Block.OutEdges[i]->Number; |
| 348 | } |
| 349 | return EdgeDestinations; |
| 350 | } |
| 351 | |
| 352 | uint32_t getFuncChecksum() { |
| 353 | return FuncChecksum; |
| 354 | } |
| 355 | |
| 356 | void setCfgChecksum(uint32_t Checksum) { |
| 357 | CfgChecksum = Checksum; |
| 358 | } |
| 359 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 360 | void writeOut() { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 361 | writeBytes(FunctionTag, 4); |
| 362 | uint32_t BlockLen = 1 + 1 + 1 + lengthOfGCOVString(getFunctionName(SP)) + |
| 363 | 1 + lengthOfGCOVString(SP.getFilename()) + 1; |
| 364 | if (UseCfgChecksum) |
| 365 | ++BlockLen; |
| 366 | write(BlockLen); |
| 367 | write(Ident); |
| 368 | write(FuncChecksum); |
| 369 | if (UseCfgChecksum) |
| 370 | write(CfgChecksum); |
| 371 | writeGCOVString(getFunctionName(SP)); |
| 372 | writeGCOVString(SP.getFilename()); |
| 373 | write(SP.getLineNumber()); |
| 374 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 375 | // Emit count of blocks. |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 376 | writeBytes(BlockTag, 4); |
| 377 | write(Blocks.size() + 1); |
| 378 | for (int i = 0, e = Blocks.size() + 1; i != e; ++i) { |
| 379 | write(0); // No flags on our blocks. |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 380 | } |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 381 | DEBUG(dbgs() << Blocks.size() << " blocks.\n"); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 382 | |
| 383 | // Emit edges between blocks. |
Nick Lewycky | c4e6b54 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 384 | if (Blocks.empty()) return; |
| 385 | Function *F = Blocks.begin()->first->getParent(); |
| 386 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 387 | GCOVBlock &Block = *Blocks[I]; |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 388 | if (Block.OutEdges.empty()) continue; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 389 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 390 | writeBytes(EdgeTag, 4); |
| 391 | write(Block.OutEdges.size() * 2 + 1); |
| 392 | write(Block.Number); |
| 393 | for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) { |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 394 | DEBUG(dbgs() << Block.Number << " -> " << Block.OutEdges[i]->Number |
| 395 | << "\n"); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 396 | write(Block.OutEdges[i]->Number); |
| 397 | write(0); // no flags |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | // Emit lines for each block. |
Nick Lewycky | c4e6b54 | 2013-06-18 06:38:21 +0000 | [diff] [blame] | 402 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 403 | Blocks[I]->writeOut(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | private: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 408 | DISubprogram SP; |
| 409 | uint32_t Ident; |
| 410 | uint32_t FuncChecksum; |
| 411 | bool UseCfgChecksum; |
| 412 | uint32_t CfgChecksum; |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 413 | DenseMap<BasicBlock *, GCOVBlock *> Blocks; |
| 414 | GCOVBlock *ReturnBlock; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 415 | }; |
| 416 | } |
| 417 | |
Bill Wendling | f2a2806 | 2013-03-28 22:40:08 +0000 | [diff] [blame] | 418 | std::string GCOVProfiler::mangleName(DICompileUnit CU, const char *NewStem) { |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 419 | if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) { |
| 420 | for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) { |
| 421 | MDNode *N = GCov->getOperand(i); |
| 422 | if (N->getNumOperands() != 2) continue; |
Nick Lewycky | fcf74ed | 2011-05-05 00:03:30 +0000 | [diff] [blame] | 423 | MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0)); |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 424 | MDNode *CompileUnit = dyn_cast<MDNode>(N->getOperand(1)); |
Nick Lewycky | fcf74ed | 2011-05-05 00:03:30 +0000 | [diff] [blame] | 425 | if (!GCovFile || !CompileUnit) continue; |
| 426 | if (CompileUnit == CU) { |
Bill Wendling | 6e5190c | 2012-08-30 00:34:21 +0000 | [diff] [blame] | 427 | SmallString<128> Filename = GCovFile->getString(); |
| 428 | sys::path::replace_extension(Filename, NewStem); |
| 429 | return Filename.str(); |
Nick Lewycky | fcf74ed | 2011-05-05 00:03:30 +0000 | [diff] [blame] | 430 | } |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
Nick Lewycky | fcf74ed | 2011-05-05 00:03:30 +0000 | [diff] [blame] | 433 | |
Bill Wendling | 6e5190c | 2012-08-30 00:34:21 +0000 | [diff] [blame] | 434 | SmallString<128> Filename = CU.getFilename(); |
Nick Lewycky | fcf74ed | 2011-05-05 00:03:30 +0000 | [diff] [blame] | 435 | sys::path::replace_extension(Filename, NewStem); |
Bill Wendling | 39c41c3 | 2013-03-26 22:47:50 +0000 | [diff] [blame] | 436 | StringRef FName = sys::path::filename(Filename); |
Bill Wendling | 39c41c3 | 2013-03-26 22:47:50 +0000 | [diff] [blame] | 437 | SmallString<128> CurPath; |
| 438 | if (sys::fs::current_path(CurPath)) return FName; |
| 439 | sys::path::append(CurPath, FName.str()); |
| 440 | return CurPath.str(); |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Nick Lewycky | 0c4de8a | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 443 | bool GCOVProfiler::runOnModule(Module &M) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 444 | this->M = &M; |
Nick Lewycky | 0c4de8a | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 445 | Ctx = &M.getContext(); |
| 446 | |
Nick Lewycky | a204ef3 | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 447 | if (Options.EmitNotes) emitProfileNotes(); |
| 448 | if (Options.EmitData) return emitProfileArcs(); |
Nick Lewycky | a61e52c | 2011-04-21 01:56:25 +0000 | [diff] [blame] | 449 | return false; |
Nick Lewycky | 0c4de8a | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Nick Lewycky | 64a0a33 | 2013-03-13 22:55:42 +0000 | [diff] [blame] | 452 | void GCOVProfiler::emitProfileNotes() { |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 453 | NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu"); |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 454 | if (!CU_Nodes) return; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 455 | |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 456 | for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { |
| 457 | // Each compile unit gets its own .gcno file. This means that whether we run |
| 458 | // this pass over the original .o's as they're produced, or run it after |
| 459 | // LTO, we'll generate the same .gcno files. |
| 460 | |
| 461 | DICompileUnit CU(CU_Nodes->getOperand(i)); |
| 462 | std::string ErrorInfo; |
Bill Wendling | f2a2806 | 2013-03-28 22:40:08 +0000 | [diff] [blame] | 463 | raw_fd_ostream out(mangleName(CU, "gcno").c_str(), ErrorInfo, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 464 | sys::fs::F_None); |
| 465 | std::string EdgeDestinations; |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 466 | |
| 467 | DIArray SPs = CU.getSubprograms(); |
| 468 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) { |
| 469 | DISubprogram SP(SPs.getElement(i)); |
Manman Ren | cbafae6 | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 470 | assert((!SP || SP.isSubprogram()) && |
| 471 | "A MDNode in subprograms of a CU should be null or a DISubprogram."); |
| 472 | if (!SP) |
| 473 | continue; |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 474 | |
| 475 | Function *F = SP.getFunction(); |
| 476 | if (!F) continue; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 477 | |
| 478 | // gcov expects every function to start with an entry block that has a |
| 479 | // single successor, so split the entry block to make sure of that. |
| 480 | BasicBlock &EntryBlock = F->getEntryBlock(); |
| 481 | BasicBlock::iterator It = EntryBlock.begin(); |
| 482 | while (isa<AllocaInst>(*It) || isa<DbgInfoIntrinsic>(*It)) |
| 483 | ++It; |
| 484 | EntryBlock.splitBasicBlock(It); |
| 485 | |
| 486 | GCOVFunction *Func = |
| 487 | new GCOVFunction(SP, &out, i, Options.UseCfgChecksum); |
| 488 | Funcs.push_back(Func); |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 489 | |
| 490 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 491 | GCOVBlock &Block = Func->getBlock(BB); |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 492 | TerminatorInst *TI = BB->getTerminator(); |
| 493 | if (int successors = TI->getNumSuccessors()) { |
| 494 | for (int i = 0; i != successors; ++i) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 495 | Block.addEdge(Func->getBlock(TI->getSuccessor(i))); |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 496 | } |
| 497 | } else if (isa<ReturnInst>(TI)) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 498 | Block.addEdge(Func->getReturnBlock()); |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | uint32_t Line = 0; |
| 502 | for (BasicBlock::iterator I = BB->begin(), IE = BB->end(); |
| 503 | I != IE; ++I) { |
| 504 | const DebugLoc &Loc = I->getDebugLoc(); |
| 505 | if (Loc.isUnknown()) continue; |
| 506 | if (Line == Loc.getLine()) continue; |
| 507 | Line = Loc.getLine(); |
| 508 | if (SP != getDISubprogram(Loc.getScope(*Ctx))) continue; |
| 509 | |
| 510 | GCOVLines &Lines = Block.getFile(SP.getFilename()); |
| 511 | Lines.addLine(Loc.getLine()); |
| 512 | } |
| 513 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 514 | EdgeDestinations += Func->getEdgeDestinations(); |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 515 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 516 | |
| 517 | FileChecksums.push_back(hash_value(EdgeDestinations)); |
| 518 | out.write("oncg", 4); |
| 519 | out.write(ReversedVersion, 4); |
| 520 | out.write(reinterpret_cast<char*>(&FileChecksums.back()), 4); |
| 521 | |
| 522 | for (SmallVectorImpl<GCOVFunction *>::iterator I = Funcs.begin(), |
| 523 | E = Funcs.end(); I != E; ++I) { |
| 524 | GCOVFunction *Func = *I; |
| 525 | Func->setCfgChecksum(FileChecksums.back()); |
| 526 | Func->writeOut(); |
| 527 | } |
| 528 | |
Nick Lewycky | bba40db | 2011-11-27 23:22:20 +0000 | [diff] [blame] | 529 | out.write("\0\0\0\0\0\0\0\0", 8); // EOF |
| 530 | out.close(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 534 | bool GCOVProfiler::emitProfileArcs() { |
| 535 | NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu"); |
| 536 | if (!CU_Nodes) return false; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 537 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 538 | bool Result = false; |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 539 | bool InsertIndCounterIncrCode = false; |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 540 | for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { |
| 541 | DICompileUnit CU(CU_Nodes->getOperand(i)); |
| 542 | DIArray SPs = CU.getSubprograms(); |
| 543 | SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP; |
| 544 | for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) { |
| 545 | DISubprogram SP(SPs.getElement(i)); |
Manman Ren | cbafae6 | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 546 | assert((!SP || SP.isSubprogram()) && |
| 547 | "A MDNode in subprograms of a CU should be null or a DISubprogram."); |
| 548 | if (!SP) |
| 549 | continue; |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 550 | Function *F = SP.getFunction(); |
| 551 | if (!F) continue; |
| 552 | if (!Result) Result = true; |
| 553 | unsigned Edges = 0; |
| 554 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 555 | TerminatorInst *TI = BB->getTerminator(); |
| 556 | if (isa<ReturnInst>(TI)) |
| 557 | ++Edges; |
| 558 | else |
| 559 | Edges += TI->getNumSuccessors(); |
| 560 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 561 | |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 562 | ArrayType *CounterTy = |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 563 | ArrayType::get(Type::getInt64Ty(*Ctx), Edges); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 564 | GlobalVariable *Counters = |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 565 | new GlobalVariable(*M, CounterTy, false, |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 566 | GlobalValue::InternalLinkage, |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 567 | Constant::getNullValue(CounterTy), |
Hans Wennborg | ce718ff | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 568 | "__llvm_gcov_ctr"); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 569 | CountersBySP.push_back(std::make_pair(Counters, (MDNode*)SP)); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 570 | |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 571 | UniqueVector<BasicBlock *> ComplexEdgePreds; |
| 572 | UniqueVector<BasicBlock *> ComplexEdgeSuccs; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 573 | |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 574 | unsigned Edge = 0; |
| 575 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 576 | TerminatorInst *TI = BB->getTerminator(); |
| 577 | int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors(); |
| 578 | if (Successors) { |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 579 | if (Successors == 1) { |
Bill Wendling | f675b3c | 2013-08-20 23:52:00 +0000 | [diff] [blame] | 580 | IRBuilder<> Builder(BB->getFirstInsertionPt()); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 581 | Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0, |
| 582 | Edge); |
| 583 | Value *Count = Builder.CreateLoad(Counter); |
Nick Lewycky | bd2d124 | 2013-02-27 05:46:30 +0000 | [diff] [blame] | 584 | Count = Builder.CreateAdd(Count, Builder.getInt64(1)); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 585 | Builder.CreateStore(Count, Counter); |
| 586 | } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
Bill Wendling | f675b3c | 2013-08-20 23:52:00 +0000 | [diff] [blame] | 587 | IRBuilder<> Builder(BI); |
Nick Lewycky | bd2d124 | 2013-02-27 05:46:30 +0000 | [diff] [blame] | 588 | Value *Sel = Builder.CreateSelect(BI->getCondition(), |
| 589 | Builder.getInt64(Edge), |
| 590 | Builder.getInt64(Edge + 1)); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 591 | SmallVector<Value *, 2> Idx; |
Nick Lewycky | bd2d124 | 2013-02-27 05:46:30 +0000 | [diff] [blame] | 592 | Idx.push_back(Builder.getInt64(0)); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 593 | Idx.push_back(Sel); |
| 594 | Value *Counter = Builder.CreateInBoundsGEP(Counters, Idx); |
| 595 | Value *Count = Builder.CreateLoad(Counter); |
Nick Lewycky | bd2d124 | 2013-02-27 05:46:30 +0000 | [diff] [blame] | 596 | Count = Builder.CreateAdd(Count, Builder.getInt64(1)); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 597 | Builder.CreateStore(Count, Counter); |
| 598 | } else { |
| 599 | ComplexEdgePreds.insert(BB); |
| 600 | for (int i = 0; i != Successors; ++i) |
| 601 | ComplexEdgeSuccs.insert(TI->getSuccessor(i)); |
| 602 | } |
Bill Wendling | f675b3c | 2013-08-20 23:52:00 +0000 | [diff] [blame] | 603 | |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 604 | Edge += Successors; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 605 | } |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 606 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 607 | |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 608 | if (!ComplexEdgePreds.empty()) { |
| 609 | GlobalVariable *EdgeTable = |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 610 | buildEdgeLookupTable(F, Counters, |
| 611 | ComplexEdgePreds, ComplexEdgeSuccs); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 612 | GlobalVariable *EdgeState = getEdgeStateValue(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 613 | |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 614 | for (int i = 0, e = ComplexEdgePreds.size(); i != e; ++i) { |
Bill Wendling | f675b3c | 2013-08-20 23:52:00 +0000 | [diff] [blame] | 615 | IRBuilder<> Builder(ComplexEdgePreds[i + 1]->getFirstInsertionPt()); |
Nick Lewycky | bd2d124 | 2013-02-27 05:46:30 +0000 | [diff] [blame] | 616 | Builder.CreateStore(Builder.getInt32(i), EdgeState); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 617 | } |
Bill Wendling | f675b3c | 2013-08-20 23:52:00 +0000 | [diff] [blame] | 618 | |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 619 | for (int i = 0, e = ComplexEdgeSuccs.size(); i != e; ++i) { |
Bill Wendling | f675b3c | 2013-08-20 23:52:00 +0000 | [diff] [blame] | 620 | // Call runtime to perform increment. |
| 621 | IRBuilder<> Builder(ComplexEdgeSuccs[i+1]->getFirstInsertionPt()); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 622 | Value *CounterPtrArray = |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 623 | Builder.CreateConstInBoundsGEP2_64(EdgeTable, 0, |
| 624 | i * ComplexEdgePreds.size()); |
Bill Wendling | c7a8840 | 2012-05-25 23:55:00 +0000 | [diff] [blame] | 625 | |
| 626 | // Build code to increment the counter. |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 627 | InsertIndCounterIncrCode = true; |
| 628 | Builder.CreateCall2(getIncrementIndirectCounterFunc(), |
| 629 | EdgeState, CounterPtrArray); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 630 | } |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 631 | } |
| 632 | } |
Bill Wendling | 4a8fefa | 2012-06-01 23:14:32 +0000 | [diff] [blame] | 633 | |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 634 | Function *WriteoutF = insertCounterWriteout(CountersBySP); |
| 635 | Function *FlushF = insertFlush(CountersBySP); |
| 636 | |
| 637 | // Create a small bit of code that registers the "__llvm_gcov_writeout" to |
Bill Wendling | 1876471 | 2013-03-19 21:03:22 +0000 | [diff] [blame] | 638 | // be executed at exit and the "__llvm_gcov_flush" function to be executed |
| 639 | // when "__gcov_flush" is called. |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 640 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
| 641 | Function *F = Function::Create(FTy, GlobalValue::InternalLinkage, |
| 642 | "__llvm_gcov_init", M); |
| 643 | F->setUnnamedAddr(true); |
| 644 | F->setLinkage(GlobalValue::InternalLinkage); |
| 645 | F->addFnAttr(Attribute::NoInline); |
| 646 | if (Options.NoRedZone) |
| 647 | F->addFnAttr(Attribute::NoRedZone); |
| 648 | |
| 649 | BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F); |
| 650 | IRBuilder<> Builder(BB); |
| 651 | |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 652 | FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
Bill Wendling | 8640c6a | 2013-03-20 21:13:59 +0000 | [diff] [blame] | 653 | Type *Params[] = { |
| 654 | PointerType::get(FTy, 0), |
| 655 | PointerType::get(FTy, 0) |
| 656 | }; |
| 657 | FTy = FunctionType::get(Builder.getVoidTy(), Params, false); |
Bill Wendling | 1876471 | 2013-03-19 21:03:22 +0000 | [diff] [blame] | 658 | |
Yuchen Wu | d7da590 | 2013-10-23 20:35:00 +0000 | [diff] [blame] | 659 | // Initialize the environment and register the local writeout and flush |
Bill Wendling | 8640c6a | 2013-03-20 21:13:59 +0000 | [diff] [blame] | 660 | // functions. |
| 661 | Constant *GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy); |
| 662 | Builder.CreateCall2(GCOVInit, WriteoutF, FlushF); |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 663 | Builder.CreateRetVoid(); |
| 664 | |
| 665 | appendToGlobalCtors(*M, F, 0); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 666 | } |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 667 | |
| 668 | if (InsertIndCounterIncrCode) |
| 669 | insertIndirectCounterIncrement(); |
| 670 | |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 671 | return Result; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 674 | // All edges with successors that aren't branches are "complex", because it |
| 675 | // requires complex logic to pick which counter to update. |
| 676 | GlobalVariable *GCOVProfiler::buildEdgeLookupTable( |
| 677 | Function *F, |
| 678 | GlobalVariable *Counters, |
| 679 | const UniqueVector<BasicBlock *> &Preds, |
| 680 | const UniqueVector<BasicBlock *> &Succs) { |
| 681 | // TODO: support invoke, threads. We rely on the fact that nothing can modify |
| 682 | // the whole-Module pred edge# between the time we set it and the time we next |
| 683 | // read it. Threads and invoke make this untrue. |
| 684 | |
| 685 | // emit [(succs * preds) x i64*], logically [succ x [pred x i64*]]. |
Benjamin Kramer | 9e6ee16 | 2012-11-17 13:49:37 +0000 | [diff] [blame] | 686 | size_t TableSize = Succs.size() * Preds.size(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 687 | Type *Int64PtrTy = Type::getInt64PtrTy(*Ctx); |
Benjamin Kramer | 9e6ee16 | 2012-11-17 13:49:37 +0000 | [diff] [blame] | 688 | ArrayType *EdgeTableTy = ArrayType::get(Int64PtrTy, TableSize); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 689 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 690 | std::unique_ptr<Constant * []> EdgeTable(new Constant *[TableSize]); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 691 | Constant *NullValue = Constant::getNullValue(Int64PtrTy); |
Benjamin Kramer | 9e6ee16 | 2012-11-17 13:49:37 +0000 | [diff] [blame] | 692 | for (size_t i = 0; i != TableSize; ++i) |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 693 | EdgeTable[i] = NullValue; |
| 694 | |
| 695 | unsigned Edge = 0; |
| 696 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 697 | TerminatorInst *TI = BB->getTerminator(); |
| 698 | int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors(); |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 699 | if (Successors > 1 && !isa<BranchInst>(TI) && !isa<ReturnInst>(TI)) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 700 | for (int i = 0; i != Successors; ++i) { |
| 701 | BasicBlock *Succ = TI->getSuccessor(i); |
Nick Lewycky | bd2d124 | 2013-02-27 05:46:30 +0000 | [diff] [blame] | 702 | IRBuilder<> Builder(Succ); |
| 703 | Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0, |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 704 | Edge + i); |
| 705 | EdgeTable[((Succs.idFor(Succ)-1) * Preds.size()) + |
| 706 | (Preds.idFor(BB)-1)] = cast<Constant>(Counter); |
| 707 | } |
| 708 | } |
| 709 | Edge += Successors; |
| 710 | } |
| 711 | |
Benjamin Kramer | 9e6ee16 | 2012-11-17 13:49:37 +0000 | [diff] [blame] | 712 | ArrayRef<Constant*> V(&EdgeTable[0], TableSize); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 713 | GlobalVariable *EdgeTableGV = |
| 714 | new GlobalVariable( |
| 715 | *M, EdgeTableTy, true, GlobalValue::InternalLinkage, |
Jay Foad | 2670108 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 716 | ConstantArray::get(EdgeTableTy, V), |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 717 | "__llvm_gcda_edge_table"); |
| 718 | EdgeTableGV->setUnnamedAddr(true); |
| 719 | return EdgeTableGV; |
| 720 | } |
| 721 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 722 | Constant *GCOVProfiler::getStartFileFunc() { |
Nick Lewycky | d9686a9 | 2013-03-07 08:28:49 +0000 | [diff] [blame] | 723 | Type *Args[] = { |
| 724 | Type::getInt8PtrTy(*Ctx), // const char *orig_filename |
| 725 | Type::getInt8PtrTy(*Ctx), // const char version[4] |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 726 | Type::getInt32Ty(*Ctx), // uint32_t checksum |
Nick Lewycky | d9686a9 | 2013-03-07 08:28:49 +0000 | [diff] [blame] | 727 | }; |
| 728 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 729 | return M->getOrInsertFunction("llvm_gcda_start_file", FTy); |
| 730 | } |
| 731 | |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 732 | Constant *GCOVProfiler::getIncrementIndirectCounterFunc() { |
| 733 | Type *Int32Ty = Type::getInt32Ty(*Ctx); |
Bill Wendling | c7a8840 | 2012-05-25 23:55:00 +0000 | [diff] [blame] | 734 | Type *Int64Ty = Type::getInt64Ty(*Ctx); |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 735 | Type *Args[] = { |
Micah Villmow | b8bce92 | 2012-10-24 17:25:11 +0000 | [diff] [blame] | 736 | Int32Ty->getPointerTo(), // uint32_t *predecessor |
| 737 | Int64Ty->getPointerTo()->getPointerTo() // uint64_t **counters |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 738 | }; |
| 739 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false); |
| 740 | return M->getOrInsertFunction("__llvm_gcov_indirect_counter_increment", FTy); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | Constant *GCOVProfiler::getEmitFunctionFunc() { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 744 | Type *Args[] = { |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 745 | Type::getInt32Ty(*Ctx), // uint32_t ident |
| 746 | Type::getInt8PtrTy(*Ctx), // const char *function_name |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 747 | Type::getInt32Ty(*Ctx), // uint32_t func_checksum |
Nick Lewycky | 17d2f77 | 2013-03-09 01:33:06 +0000 | [diff] [blame] | 748 | Type::getInt8Ty(*Ctx), // uint8_t use_extra_checksum |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 749 | Type::getInt32Ty(*Ctx), // uint32_t cfg_checksum |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 750 | }; |
Bill Wendling | c7a8840 | 2012-05-25 23:55:00 +0000 | [diff] [blame] | 751 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 752 | return M->getOrInsertFunction("llvm_gcda_emit_function", FTy); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | Constant *GCOVProfiler::getEmitArcsFunc() { |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 756 | Type *Args[] = { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 757 | Type::getInt32Ty(*Ctx), // uint32_t num_counters |
| 758 | Type::getInt64PtrTy(*Ctx), // uint64_t *counters |
| 759 | }; |
Nick Lewycky | d9686a9 | 2013-03-07 08:28:49 +0000 | [diff] [blame] | 760 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 761 | return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Yuchen Wu | f42264e | 2013-11-12 04:59:08 +0000 | [diff] [blame] | 764 | Constant *GCOVProfiler::getSummaryInfoFunc() { |
| 765 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
| 766 | return M->getOrInsertFunction("llvm_gcda_summary_info", FTy); |
| 767 | } |
| 768 | |
Bill Wendling | 1876471 | 2013-03-19 21:03:22 +0000 | [diff] [blame] | 769 | Constant *GCOVProfiler::getDeleteWriteoutFunctionListFunc() { |
| 770 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
| 771 | return M->getOrInsertFunction("llvm_delete_writeout_function_list", FTy); |
| 772 | } |
| 773 | |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 774 | Constant *GCOVProfiler::getDeleteFlushFunctionListFunc() { |
| 775 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
| 776 | return M->getOrInsertFunction("llvm_delete_flush_function_list", FTy); |
| 777 | } |
| 778 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 779 | Constant *GCOVProfiler::getEndFileFunc() { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 780 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 781 | return M->getOrInsertFunction("llvm_gcda_end_file", FTy); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 784 | GlobalVariable *GCOVProfiler::getEdgeStateValue() { |
| 785 | GlobalVariable *GV = M->getGlobalVariable("__llvm_gcov_global_state_pred"); |
| 786 | if (!GV) { |
| 787 | GV = new GlobalVariable(*M, Type::getInt32Ty(*Ctx), false, |
| 788 | GlobalValue::InternalLinkage, |
| 789 | ConstantInt::get(Type::getInt32Ty(*Ctx), |
| 790 | 0xffffffff), |
| 791 | "__llvm_gcov_global_state_pred"); |
| 792 | GV->setUnnamedAddr(true); |
| 793 | } |
| 794 | return GV; |
| 795 | } |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 796 | |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 797 | Function *GCOVProfiler::insertCounterWriteout( |
Bill Wendling | 21b742f | 2012-08-29 18:45:41 +0000 | [diff] [blame] | 798 | ArrayRef<std::pair<GlobalVariable *, MDNode *> > CountersBySP) { |
Bill Wendling | 253353c | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 799 | FunctionType *WriteoutFTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
| 800 | Function *WriteoutF = M->getFunction("__llvm_gcov_writeout"); |
| 801 | if (!WriteoutF) |
| 802 | WriteoutF = Function::Create(WriteoutFTy, GlobalValue::InternalLinkage, |
| 803 | "__llvm_gcov_writeout", M); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 804 | WriteoutF->setUnnamedAddr(true); |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 805 | WriteoutF->addFnAttr(Attribute::NoInline); |
Nick Lewycky | a204ef3 | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 806 | if (Options.NoRedZone) |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 807 | WriteoutF->addFnAttr(Attribute::NoRedZone); |
Bill Wendling | 253353c | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 808 | |
| 809 | BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 810 | IRBuilder<> Builder(BB); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 811 | |
| 812 | Constant *StartFile = getStartFileFunc(); |
| 813 | Constant *EmitFunction = getEmitFunctionFunc(); |
| 814 | Constant *EmitArcs = getEmitArcsFunc(); |
Yuchen Wu | f42264e | 2013-11-12 04:59:08 +0000 | [diff] [blame] | 815 | Constant *SummaryInfo = getSummaryInfoFunc(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 816 | Constant *EndFile = getEndFileFunc(); |
| 817 | |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 818 | NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu"); |
| 819 | if (CU_Nodes) { |
| 820 | for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { |
Bill Wendling | 032dbee | 2012-09-13 14:32:30 +0000 | [diff] [blame] | 821 | DICompileUnit CU(CU_Nodes->getOperand(i)); |
Bill Wendling | f2a2806 | 2013-03-28 22:40:08 +0000 | [diff] [blame] | 822 | std::string FilenameGcda = mangleName(CU, "gcda"); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 823 | uint32_t CfgChecksum = FileChecksums.empty() ? 0 : FileChecksums[i]; |
| 824 | Builder.CreateCall3(StartFile, |
Nick Lewycky | d9686a9 | 2013-03-07 08:28:49 +0000 | [diff] [blame] | 825 | Builder.CreateGlobalStringPtr(FilenameGcda), |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 826 | Builder.CreateGlobalStringPtr(ReversedVersion), |
| 827 | Builder.getInt32(CfgChecksum)); |
Nick Lewycky | 8fa6dc4 | 2013-03-09 02:06:37 +0000 | [diff] [blame] | 828 | for (unsigned j = 0, e = CountersBySP.size(); j != e; ++j) { |
| 829 | DISubprogram SP(CountersBySP[j].second); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 830 | uint32_t FuncChecksum = Funcs.empty() ? 0 : Funcs[j]->getFuncChecksum(); |
| 831 | Builder.CreateCall5( |
Nick Lewycky | 5d22d02 | 2013-03-19 01:37:55 +0000 | [diff] [blame] | 832 | EmitFunction, Builder.getInt32(j), |
| 833 | Options.FunctionNamesInData ? |
| 834 | Builder.CreateGlobalStringPtr(getFunctionName(SP)) : |
| 835 | Constant::getNullValue(Builder.getInt8PtrTy()), |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 836 | Builder.getInt32(FuncChecksum), |
| 837 | Builder.getInt8(Options.UseCfgChecksum), |
| 838 | Builder.getInt32(CfgChecksum)); |
Nick Lewycky | 17d2f77 | 2013-03-09 01:33:06 +0000 | [diff] [blame] | 839 | |
Nick Lewycky | 8fa6dc4 | 2013-03-09 02:06:37 +0000 | [diff] [blame] | 840 | GlobalVariable *GV = CountersBySP[j].first; |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 841 | unsigned Arcs = |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 842 | cast<ArrayType>(GV->getType()->getElementType())->getNumElements(); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 843 | Builder.CreateCall2(EmitArcs, |
Nick Lewycky | bd2d124 | 2013-02-27 05:46:30 +0000 | [diff] [blame] | 844 | Builder.getInt32(Arcs), |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 845 | Builder.CreateConstGEP2_64(GV, 0, 0)); |
| 846 | } |
Yuchen Wu | f42264e | 2013-11-12 04:59:08 +0000 | [diff] [blame] | 847 | Builder.CreateCall(SummaryInfo); |
Devang Patel | f6d3a4c | 2011-08-17 22:49:38 +0000 | [diff] [blame] | 848 | Builder.CreateCall(EndFile); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 849 | } |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 850 | } |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 851 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 852 | Builder.CreateRetVoid(); |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 853 | return WriteoutF; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 854 | } |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 855 | |
| 856 | void GCOVProfiler::insertIndirectCounterIncrement() { |
| 857 | Function *Fn = |
| 858 | cast<Function>(GCOVProfiler::getIncrementIndirectCounterFunc()); |
| 859 | Fn->setUnnamedAddr(true); |
| 860 | Fn->setLinkage(GlobalValue::InternalLinkage); |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 861 | Fn->addFnAttr(Attribute::NoInline); |
Nick Lewycky | a204ef3 | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 862 | if (Options.NoRedZone) |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 863 | Fn->addFnAttr(Attribute::NoRedZone); |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 864 | |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 865 | // Create basic blocks for function. |
| 866 | BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", Fn); |
| 867 | IRBuilder<> Builder(BB); |
| 868 | |
| 869 | BasicBlock *PredNotNegOne = BasicBlock::Create(*Ctx, "", Fn); |
| 870 | BasicBlock *CounterEnd = BasicBlock::Create(*Ctx, "", Fn); |
| 871 | BasicBlock *Exit = BasicBlock::Create(*Ctx, "exit", Fn); |
| 872 | |
| 873 | // uint32_t pred = *predecessor; |
| 874 | // if (pred == 0xffffffff) return; |
| 875 | Argument *Arg = Fn->arg_begin(); |
| 876 | Arg->setName("predecessor"); |
| 877 | Value *Pred = Builder.CreateLoad(Arg, "pred"); |
Nick Lewycky | bd2d124 | 2013-02-27 05:46:30 +0000 | [diff] [blame] | 878 | Value *Cond = Builder.CreateICmpEQ(Pred, Builder.getInt32(0xffffffff)); |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 879 | BranchInst::Create(Exit, PredNotNegOne, Cond, BB); |
| 880 | |
| 881 | Builder.SetInsertPoint(PredNotNegOne); |
| 882 | |
| 883 | // uint64_t *counter = counters[pred]; |
| 884 | // if (!counter) return; |
Nick Lewycky | bd2d124 | 2013-02-27 05:46:30 +0000 | [diff] [blame] | 885 | Value *ZExtPred = Builder.CreateZExt(Pred, Builder.getInt64Ty()); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 886 | Arg = std::next(Fn->arg_begin()); |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 887 | Arg->setName("counters"); |
| 888 | Value *GEP = Builder.CreateGEP(Arg, ZExtPred); |
| 889 | Value *Counter = Builder.CreateLoad(GEP, "counter"); |
Nick Lewycky | 58591b1 | 2013-02-27 06:21:30 +0000 | [diff] [blame] | 890 | Cond = Builder.CreateICmpEQ(Counter, |
| 891 | Constant::getNullValue( |
| 892 | Builder.getInt64Ty()->getPointerTo())); |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 893 | Builder.CreateCondBr(Cond, Exit, CounterEnd); |
| 894 | |
| 895 | // ++*counter; |
| 896 | Builder.SetInsertPoint(CounterEnd); |
| 897 | Value *Add = Builder.CreateAdd(Builder.CreateLoad(Counter), |
Nick Lewycky | bd2d124 | 2013-02-27 05:46:30 +0000 | [diff] [blame] | 898 | Builder.getInt64(1)); |
Bill Wendling | 77b1913 | 2012-05-28 06:10:56 +0000 | [diff] [blame] | 899 | Builder.CreateStore(Add, Counter); |
| 900 | Builder.CreateBr(Exit); |
| 901 | |
| 902 | // Fill in the exit block. |
| 903 | Builder.SetInsertPoint(Exit); |
| 904 | Builder.CreateRetVoid(); |
| 905 | } |
Bill Wendling | 253353c | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 906 | |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 907 | Function *GCOVProfiler:: |
Bill Wendling | 253353c | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 908 | insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> > CountersBySP) { |
| 909 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 910 | Function *FlushF = M->getFunction("__llvm_gcov_flush"); |
Bill Wendling | 253353c | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 911 | if (!FlushF) |
| 912 | FlushF = Function::Create(FTy, GlobalValue::InternalLinkage, |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 913 | "__llvm_gcov_flush", M); |
Bill Wendling | 253353c | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 914 | else |
| 915 | FlushF->setLinkage(GlobalValue::InternalLinkage); |
| 916 | FlushF->setUnnamedAddr(true); |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 917 | FlushF->addFnAttr(Attribute::NoInline); |
Nick Lewycky | a204ef3 | 2013-03-14 05:13:26 +0000 | [diff] [blame] | 918 | if (Options.NoRedZone) |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 919 | FlushF->addFnAttr(Attribute::NoRedZone); |
Bill Wendling | 253353c | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 920 | |
Bill Wendling | 253353c | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 921 | BasicBlock *Entry = BasicBlock::Create(*Ctx, "entry", FlushF); |
| 922 | |
| 923 | // Write out the current counters. |
| 924 | Constant *WriteoutF = M->getFunction("__llvm_gcov_writeout"); |
| 925 | assert(WriteoutF && "Need to create the writeout function first!"); |
| 926 | |
| 927 | IRBuilder<> Builder(Entry); |
| 928 | Builder.CreateCall(WriteoutF); |
| 929 | |
Bill Wendling | 032dbee | 2012-09-13 14:32:30 +0000 | [diff] [blame] | 930 | // Zero out the counters. |
| 931 | for (ArrayRef<std::pair<GlobalVariable *, MDNode *> >::iterator |
| 932 | I = CountersBySP.begin(), E = CountersBySP.end(); |
| 933 | I != E; ++I) { |
| 934 | GlobalVariable *GV = I->first; |
| 935 | Constant *Null = Constant::getNullValue(GV->getType()->getElementType()); |
Bill Wendling | ec3fc2e | 2012-09-14 22:35:49 +0000 | [diff] [blame] | 936 | Builder.CreateStore(Null, GV); |
Bill Wendling | 032dbee | 2012-09-13 14:32:30 +0000 | [diff] [blame] | 937 | } |
Bill Wendling | 253353c | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 938 | |
| 939 | Type *RetTy = FlushF->getReturnType(); |
| 940 | if (RetTy == Type::getVoidTy(*Ctx)) |
| 941 | Builder.CreateRetVoid(); |
| 942 | else if (RetTy->isIntegerTy()) |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 943 | // Used if __llvm_gcov_flush was implicitly declared. |
Bill Wendling | 253353c | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 944 | Builder.CreateRet(ConstantInt::get(RetTy, 0)); |
| 945 | else |
Bill Wendling | d195eb6 | 2013-03-18 23:04:39 +0000 | [diff] [blame] | 946 | report_fatal_error("invalid return type for __llvm_gcov_flush"); |
| 947 | |
| 948 | return FlushF; |
Bill Wendling | 253353c | 2012-09-13 00:09:55 +0000 | [diff] [blame] | 949 | } |