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 | |
| 19 | #include "ProfilingUtils.h" |
| 20 | #include "llvm/Transforms/Instrumentation.h" |
| 21 | #include "llvm/Analysis/DebugInfo.h" |
| 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Pass.h" |
| 24 | #include "llvm/Instructions.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/DebugLoc.h" |
| 28 | #include "llvm/Support/InstIterator.h" |
| 29 | #include "llvm/Support/IRBuilder.h" |
| 30 | #include "llvm/Support/PathV2.h" |
| 31 | #include "llvm/ADT/DenseMap.h" |
| 32 | #include "llvm/ADT/Statistic.h" |
| 33 | #include "llvm/ADT/STLExtras.h" |
| 34 | #include "llvm/ADT/StringExtras.h" |
| 35 | #include "llvm/ADT/StringMap.h" |
| 36 | #include "llvm/ADT/UniqueVector.h" |
| 37 | #include <string> |
| 38 | #include <utility> |
| 39 | using namespace llvm; |
| 40 | |
| 41 | namespace { |
| 42 | class GCOVProfiler : public ModulePass { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 43 | public: |
| 44 | static char ID; |
Nick Lewycky | a61e52c | 2011-04-21 01:56:25 +0000 | [diff] [blame] | 45 | GCOVProfiler() |
| 46 | : ModulePass(ID), EmitNotes(true), EmitData(true) { |
| 47 | initializeGCOVProfilerPass(*PassRegistry::getPassRegistry()); |
| 48 | } |
| 49 | GCOVProfiler(bool EmitNotes, bool EmitData) |
| 50 | : ModulePass(ID), EmitNotes(EmitNotes), EmitData(EmitData) { |
| 51 | assert((EmitNotes || EmitData) && "GCOVProfiler asked to do nothing?"); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 52 | initializeGCOVProfilerPass(*PassRegistry::getPassRegistry()); |
| 53 | } |
| 54 | virtual const char *getPassName() const { |
| 55 | return "GCOV Profiler"; |
| 56 | } |
| 57 | |
| 58 | private: |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 59 | bool runOnModule(Module &M); |
| 60 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 61 | // Create the GCNO files for the Module based on DebugInfo. |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 62 | void emitGCNO(DebugInfoFinder &DIF); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 63 | |
Nick Lewycky | 0c4de8a | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 64 | // Modify the program to track transitions along edges and call into the |
| 65 | // profiling runtime to emit .gcda files when run. |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 66 | bool emitProfileArcs(DebugInfoFinder &DIF); |
Nick Lewycky | 0c4de8a | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 67 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 68 | // Get pointers to the functions in the runtime library. |
| 69 | Constant *getStartFileFunc(); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 70 | Constant *getIncrementIndirectCounterFunc(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 71 | Constant *getEmitFunctionFunc(); |
| 72 | Constant *getEmitArcsFunc(); |
| 73 | Constant *getEndFileFunc(); |
| 74 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 75 | // Create or retrieve an i32 state value that is used to represent the |
| 76 | // pred block number for certain non-trivial edges. |
| 77 | GlobalVariable *getEdgeStateValue(); |
| 78 | |
| 79 | // Produce a table of pointers to counters, by predecessor and successor |
| 80 | // block number. |
| 81 | GlobalVariable *buildEdgeLookupTable(Function *F, |
| 82 | GlobalVariable *Counter, |
| 83 | const UniqueVector<BasicBlock *> &Preds, |
| 84 | const UniqueVector<BasicBlock *> &Succs); |
| 85 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 86 | // Add the function to write out all our counters to the global destructor |
| 87 | // list. |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 88 | void insertCounterWriteout(DebugInfoFinder &, |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 89 | SmallVector<std::pair<GlobalVariable *, |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 90 | MDNode *>, 8> &); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 91 | |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 92 | std::string mangleName(DICompileUnit CU, std::string NewStem); |
| 93 | |
Nick Lewycky | a61e52c | 2011-04-21 01:56:25 +0000 | [diff] [blame] | 94 | bool EmitNotes; |
| 95 | bool EmitData; |
| 96 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 97 | Module *M; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 98 | LLVMContext *Ctx; |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | char GCOVProfiler::ID = 0; |
| 103 | INITIALIZE_PASS(GCOVProfiler, "insert-gcov-profiling", |
| 104 | "Insert instrumentation for GCOV profiling", false, false) |
| 105 | |
Nick Lewycky | a61e52c | 2011-04-21 01:56:25 +0000 | [diff] [blame] | 106 | ModulePass *llvm::createGCOVProfilerPass(bool EmitNotes, bool EmitData) { |
| 107 | return new GCOVProfiler(EmitNotes, EmitData); |
| 108 | } |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 109 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 110 | static DISubprogram findSubprogram(DIScope Scope) { |
| 111 | while (!Scope.isSubprogram()) { |
| 112 | assert(Scope.isLexicalBlock() && |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 113 | "Debug location not lexical block or subprogram"); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 114 | Scope = DILexicalBlock(Scope).getContext(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 115 | } |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 116 | return DISubprogram(Scope); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | namespace { |
| 120 | class GCOVRecord { |
| 121 | protected: |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 122 | static const char *LinesTag; |
| 123 | static const char *FunctionTag; |
| 124 | static const char *BlockTag; |
| 125 | static const char *EdgeTag; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 126 | |
| 127 | GCOVRecord() {} |
| 128 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 129 | void writeBytes(const char *Bytes, int Size) { |
| 130 | os->write(Bytes, Size); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 133 | void write(uint32_t i) { |
| 134 | writeBytes(reinterpret_cast<char*>(&i), 4); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Returns the length measured in 4-byte blocks that will be used to |
| 138 | // represent this string in a GCOV file |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 139 | unsigned lengthOfGCOVString(StringRef s) { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 140 | // 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] | 141 | // padding out to the next 4-byte word. The length is measured in 4-byte |
| 142 | // words including padding, not bytes of actual string. |
Nick Lewycky | d363ff3 | 2011-05-05 23:52:18 +0000 | [diff] [blame^] | 143 | return (s.size() / 4) + 1; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 146 | void writeGCOVString(StringRef s) { |
| 147 | uint32_t Len = lengthOfGCOVString(s); |
| 148 | write(Len); |
| 149 | writeBytes(s.data(), s.size()); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 150 | |
| 151 | // Write 1 to 4 bytes of NUL padding. |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 152 | assert((unsigned)(4 - (s.size() % 4)) > 0); |
| 153 | assert((unsigned)(4 - (s.size() % 4)) <= 4); |
| 154 | writeBytes("\0\0\0\0", 4 - (s.size() % 4)); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | raw_ostream *os; |
| 158 | }; |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 159 | const char *GCOVRecord::LinesTag = "\0\0\x45\x01"; |
| 160 | const char *GCOVRecord::FunctionTag = "\0\0\0\1"; |
| 161 | const char *GCOVRecord::BlockTag = "\0\0\x41\x01"; |
| 162 | const char *GCOVRecord::EdgeTag = "\0\0\x43\x01"; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 163 | |
| 164 | class GCOVFunction; |
| 165 | class GCOVBlock; |
| 166 | |
| 167 | // Constructed only by requesting it from a GCOVBlock, this object stores a |
| 168 | // list of line numbers and a single filename, representing lines that belong |
| 169 | // to the block. |
| 170 | class GCOVLines : public GCOVRecord { |
| 171 | public: |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 172 | void addLine(uint32_t Line) { |
| 173 | Lines.push_back(Line); |
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 | uint32_t length() { |
| 177 | return lengthOfGCOVString(Filename) + 2 + Lines.size(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | private: |
| 181 | friend class GCOVBlock; |
| 182 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 183 | GCOVLines(std::string Filename, raw_ostream *os) |
| 184 | : Filename(Filename) { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 185 | this->os = os; |
| 186 | } |
| 187 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 188 | std::string Filename; |
| 189 | SmallVector<uint32_t, 32> Lines; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | // Represent a basic block in GCOV. Each block has a unique number in the |
| 193 | // function, number of lines belonging to each block, and a set of edges to |
| 194 | // other blocks. |
| 195 | class GCOVBlock : public GCOVRecord { |
| 196 | public: |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 197 | GCOVLines &getFile(std::string Filename) { |
| 198 | GCOVLines *&Lines = LinesByFile[Filename]; |
| 199 | if (!Lines) { |
| 200 | Lines = new GCOVLines(Filename, os); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 201 | } |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 202 | return *Lines; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 205 | void addEdge(GCOVBlock &Successor) { |
| 206 | OutEdges.push_back(&Successor); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 209 | void writeOut() { |
| 210 | uint32_t Len = 3; |
| 211 | for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(), |
| 212 | E = LinesByFile.end(); I != E; ++I) { |
| 213 | Len += I->second->length(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 216 | writeBytes(LinesTag, 4); |
| 217 | write(Len); |
| 218 | write(Number); |
| 219 | for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(), |
| 220 | E = LinesByFile.end(); I != E; ++I) { |
| 221 | write(0); |
| 222 | writeGCOVString(I->second->Filename); |
| 223 | for (int i = 0, e = I->second->Lines.size(); i != e; ++i) { |
| 224 | write(I->second->Lines[i]); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 227 | write(0); |
| 228 | write(0); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | ~GCOVBlock() { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 232 | DeleteContainerSeconds(LinesByFile); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | private: |
| 236 | friend class GCOVFunction; |
| 237 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 238 | GCOVBlock(uint32_t Number, raw_ostream *os) |
| 239 | : Number(Number) { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 240 | this->os = os; |
| 241 | } |
| 242 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 243 | uint32_t Number; |
| 244 | StringMap<GCOVLines *> LinesByFile; |
| 245 | SmallVector<GCOVBlock *, 4> OutEdges; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | // A function has a unique identifier, a checksum (we leave as zero) and a |
| 249 | // set of blocks and a map of edges between blocks. This is the only GCOV |
| 250 | // object users can construct, the blocks and lines will be rooted here. |
| 251 | class GCOVFunction : public GCOVRecord { |
| 252 | public: |
| 253 | GCOVFunction(DISubprogram SP, raw_ostream *os) { |
| 254 | this->os = os; |
| 255 | |
| 256 | Function *F = SP.getFunction(); |
| 257 | uint32_t i = 0; |
| 258 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 259 | Blocks[BB] = new GCOVBlock(i++, os); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 260 | } |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 261 | ReturnBlock = new GCOVBlock(i++, os); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 262 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 263 | writeBytes(FunctionTag, 4); |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 264 | uint32_t BlockLen = 1 + 1 + 1 + 1 + lengthOfGCOVString(SP.getName()) + |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 265 | 1 + lengthOfGCOVString(SP.getFilename()) + 1; |
| 266 | write(BlockLen); |
| 267 | uint32_t Ident = reinterpret_cast<intptr_t>((MDNode*)SP); |
| 268 | write(Ident); |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 269 | write(0); // checksum #1 |
| 270 | write(0); // checksum #2 |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 271 | writeGCOVString(SP.getName()); |
| 272 | writeGCOVString(SP.getFilename()); |
| 273 | write(SP.getLineNumber()); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | ~GCOVFunction() { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 277 | DeleteContainerSeconds(Blocks); |
| 278 | delete ReturnBlock; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 281 | GCOVBlock &getBlock(BasicBlock *BB) { |
| 282 | return *Blocks[BB]; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 285 | GCOVBlock &getReturnBlock() { |
| 286 | return *ReturnBlock; |
Nick Lewycky | a4c4c0e | 2011-04-21 03:18:00 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 289 | void writeOut() { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 290 | // Emit count of blocks. |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 291 | writeBytes(BlockTag, 4); |
| 292 | write(Blocks.size() + 1); |
| 293 | for (int i = 0, e = Blocks.size() + 1; i != e; ++i) { |
| 294 | write(0); // No flags on our blocks. |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // Emit edges between blocks. |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 298 | for (DenseMap<BasicBlock *, GCOVBlock *>::iterator I = Blocks.begin(), |
| 299 | E = Blocks.end(); I != E; ++I) { |
| 300 | GCOVBlock &Block = *I->second; |
| 301 | if (Block.OutEdges.empty()) continue; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 302 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 303 | writeBytes(EdgeTag, 4); |
| 304 | write(Block.OutEdges.size() * 2 + 1); |
| 305 | write(Block.Number); |
| 306 | for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) { |
| 307 | write(Block.OutEdges[i]->Number); |
| 308 | write(0); // no flags |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | // Emit lines for each block. |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 313 | for (DenseMap<BasicBlock *, GCOVBlock *>::iterator I = Blocks.begin(), |
| 314 | E = Blocks.end(); I != E; ++I) { |
| 315 | I->second->writeOut(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | private: |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 320 | DenseMap<BasicBlock *, GCOVBlock *> Blocks; |
| 321 | GCOVBlock *ReturnBlock; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 322 | }; |
| 323 | } |
| 324 | |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 325 | std::string GCOVProfiler::mangleName(DICompileUnit CU, std::string NewStem) { |
| 326 | if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) { |
| 327 | for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) { |
| 328 | MDNode *N = GCov->getOperand(i); |
| 329 | if (N->getNumOperands() != 2) continue; |
Nick Lewycky | fcf74ed | 2011-05-05 00:03:30 +0000 | [diff] [blame] | 330 | MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0)); |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 331 | MDNode *CompileUnit = dyn_cast<MDNode>(N->getOperand(1)); |
Nick Lewycky | fcf74ed | 2011-05-05 00:03:30 +0000 | [diff] [blame] | 332 | if (!GCovFile || !CompileUnit) continue; |
| 333 | if (CompileUnit == CU) { |
| 334 | SmallString<128> Filename = GCovFile->getString(); |
| 335 | sys::path::replace_extension(Filename, NewStem); |
| 336 | return Filename.str(); |
| 337 | } |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
Nick Lewycky | fcf74ed | 2011-05-05 00:03:30 +0000 | [diff] [blame] | 340 | |
| 341 | SmallString<128> Filename = CU.getFilename(); |
| 342 | sys::path::replace_extension(Filename, NewStem); |
| 343 | return sys::path::filename(Filename.str()); |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Nick Lewycky | 0c4de8a | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 346 | bool GCOVProfiler::runOnModule(Module &M) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 347 | this->M = &M; |
Nick Lewycky | 0c4de8a | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 348 | Ctx = &M.getContext(); |
| 349 | |
| 350 | DebugInfoFinder DIF; |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 351 | DIF.processModule(M); |
Nick Lewycky | 0c4de8a | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 352 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 353 | if (EmitNotes) emitGCNO(DIF); |
| 354 | if (EmitData) return emitProfileArcs(DIF); |
Nick Lewycky | a61e52c | 2011-04-21 01:56:25 +0000 | [diff] [blame] | 355 | return false; |
Nick Lewycky | 0c4de8a | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 358 | void GCOVProfiler::emitGCNO(DebugInfoFinder &DIF) { |
| 359 | DenseMap<const MDNode *, raw_fd_ostream *> GcnoFiles; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 360 | for (DebugInfoFinder::iterator I = DIF.compile_unit_begin(), |
| 361 | E = DIF.compile_unit_end(); I != E; ++I) { |
| 362 | // Each compile unit gets its own .gcno file. This means that whether we run |
| 363 | // this pass over the original .o's as they're produced, or run it after |
| 364 | // LTO, we'll generate the same .gcno files. |
| 365 | |
| 366 | DICompileUnit CU(*I); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 367 | raw_fd_ostream *&out = GcnoFiles[CU]; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 368 | std::string ErrorInfo; |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 369 | out = new raw_fd_ostream(mangleName(CU, "gcno").c_str(), ErrorInfo, |
| 370 | raw_fd_ostream::F_Binary); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 371 | out->write("oncg*404MVLL", 12); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | for (DebugInfoFinder::iterator SPI = DIF.subprogram_begin(), |
| 375 | SPE = DIF.subprogram_end(); SPI != SPE; ++SPI) { |
| 376 | DISubprogram SP(*SPI); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 377 | raw_fd_ostream *&os = GcnoFiles[SP.getCompileUnit()]; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 378 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 379 | Function *F = SP.getFunction(); |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 380 | if (!F) continue; |
| 381 | GCOVFunction Func(SP, os); |
| 382 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 383 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 384 | GCOVBlock &Block = Func.getBlock(BB); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 385 | TerminatorInst *TI = BB->getTerminator(); |
| 386 | if (int successors = TI->getNumSuccessors()) { |
| 387 | for (int i = 0; i != successors; ++i) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 388 | Block.addEdge(Func.getBlock(TI->getSuccessor(i))); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 389 | } |
Nick Lewycky | a4c4c0e | 2011-04-21 03:18:00 +0000 | [diff] [blame] | 390 | } else if (isa<ReturnInst>(TI)) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 391 | Block.addEdge(Func.getReturnBlock()); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 394 | uint32_t Line = 0; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 395 | for (BasicBlock::iterator I = BB->begin(), IE = BB->end(); I != IE; ++I) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 396 | const DebugLoc &Loc = I->getDebugLoc(); |
| 397 | if (Loc.isUnknown()) continue; |
| 398 | if (Line == Loc.getLine()) continue; |
| 399 | Line = Loc.getLine(); |
| 400 | if (SP != findSubprogram(DIScope(Loc.getScope(*Ctx)))) continue; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 401 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 402 | GCOVLines &Lines = Block.getFile(SP.getFilename()); |
| 403 | Lines.addLine(Loc.getLine()); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 406 | Func.writeOut(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | for (DenseMap<const MDNode *, raw_fd_ostream *>::iterator |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 410 | I = GcnoFiles.begin(), E = GcnoFiles.end(); I != E; ++I) { |
| 411 | raw_fd_ostream *&out = I->second; |
| 412 | out->write("\0\0\0\0\0\0\0\0", 8); // EOF |
| 413 | out->close(); |
| 414 | delete out; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 418 | bool GCOVProfiler::emitProfileArcs(DebugInfoFinder &DIF) { |
Nick Lewycky | 0c4de8a | 2011-04-16 02:05:18 +0000 | [diff] [blame] | 419 | if (DIF.subprogram_begin() == DIF.subprogram_end()) |
| 420 | return false; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 421 | |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 422 | SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 423 | for (DebugInfoFinder::iterator SPI = DIF.subprogram_begin(), |
| 424 | SPE = DIF.subprogram_end(); SPI != SPE; ++SPI) { |
| 425 | DISubprogram SP(*SPI); |
| 426 | Function *F = SP.getFunction(); |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 427 | if (!F) continue; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 428 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 429 | unsigned Edges = 0; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 430 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 431 | TerminatorInst *TI = BB->getTerminator(); |
Nick Lewycky | a4c4c0e | 2011-04-21 03:18:00 +0000 | [diff] [blame] | 432 | if (isa<ReturnInst>(TI)) |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 433 | ++Edges; |
Nick Lewycky | a4c4c0e | 2011-04-21 03:18:00 +0000 | [diff] [blame] | 434 | else |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 435 | Edges += TI->getNumSuccessors(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 438 | const ArrayType *CounterTy = |
| 439 | ArrayType::get(Type::getInt64Ty(*Ctx), Edges); |
| 440 | GlobalVariable *Counters = |
| 441 | new GlobalVariable(*M, CounterTy, false, |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 442 | GlobalValue::InternalLinkage, |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 443 | Constant::getNullValue(CounterTy), |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 444 | "__llvm_gcov_ctr", 0, false, 0); |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 445 | CountersBySP.push_back(std::make_pair(Counters, (MDNode*)SP)); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 446 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 447 | UniqueVector<BasicBlock *> ComplexEdgePreds; |
| 448 | UniqueVector<BasicBlock *> ComplexEdgeSuccs; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 449 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 450 | unsigned Edge = 0; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 451 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 452 | TerminatorInst *TI = BB->getTerminator(); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 453 | int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors(); |
| 454 | if (Successors) { |
| 455 | IRBuilder<> Builder(TI); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 456 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 457 | if (Successors == 1) { |
| 458 | Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0, |
| 459 | Edge); |
| 460 | Value *Count = Builder.CreateLoad(Counter); |
| 461 | Count = Builder.CreateAdd(Count, |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 462 | ConstantInt::get(Type::getInt64Ty(*Ctx),1)); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 463 | Builder.CreateStore(Count, Counter); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 464 | } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 465 | Value *Sel = Builder.CreateSelect( |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 466 | BI->getCondition(), |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 467 | ConstantInt::get(Type::getInt64Ty(*Ctx), Edge), |
| 468 | ConstantInt::get(Type::getInt64Ty(*Ctx), Edge + 1)); |
| 469 | SmallVector<Value *, 2> Idx; |
| 470 | Idx.push_back(Constant::getNullValue(Type::getInt64Ty(*Ctx))); |
| 471 | Idx.push_back(Sel); |
| 472 | Value *Counter = Builder.CreateInBoundsGEP(Counters, |
| 473 | Idx.begin(), Idx.end()); |
| 474 | Value *Count = Builder.CreateLoad(Counter); |
| 475 | Count = Builder.CreateAdd(Count, |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 476 | ConstantInt::get(Type::getInt64Ty(*Ctx),1)); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 477 | Builder.CreateStore(Count, Counter); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 478 | } else { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 479 | ComplexEdgePreds.insert(BB); |
| 480 | for (int i = 0; i != Successors; ++i) |
| 481 | ComplexEdgeSuccs.insert(TI->getSuccessor(i)); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 482 | } |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 483 | Edge += Successors; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 487 | if (!ComplexEdgePreds.empty()) { |
| 488 | GlobalVariable *EdgeTable = |
| 489 | buildEdgeLookupTable(F, Counters, |
| 490 | ComplexEdgePreds, ComplexEdgeSuccs); |
| 491 | GlobalVariable *EdgeState = getEdgeStateValue(); |
| 492 | |
| 493 | const Type *Int32Ty = Type::getInt32Ty(*Ctx); |
| 494 | for (int i = 0, e = ComplexEdgePreds.size(); i != e; ++i) { |
| 495 | IRBuilder<> Builder(ComplexEdgePreds[i+1]->getTerminator()); |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 496 | Builder.CreateStore(ConstantInt::get(Int32Ty, i), EdgeState); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 497 | } |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 498 | for (int i = 0, e = ComplexEdgeSuccs.size(); i != e; ++i) { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 499 | // call runtime to perform increment |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 500 | IRBuilder<> Builder(ComplexEdgeSuccs[i+1]->getFirstNonPHI()); |
| 501 | Value *CounterPtrArray = |
| 502 | Builder.CreateConstInBoundsGEP2_64(EdgeTable, 0, |
| 503 | i * ComplexEdgePreds.size()); |
| 504 | Builder.CreateCall2(getIncrementIndirectCounterFunc(), |
| 505 | EdgeState, CounterPtrArray); |
| 506 | // clear the predecessor number |
| 507 | Builder.CreateStore(ConstantInt::get(Int32Ty, 0xffffffff), EdgeState); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 512 | insertCounterWriteout(DIF, CountersBySP); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 513 | |
| 514 | return true; |
| 515 | } |
| 516 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 517 | // All edges with successors that aren't branches are "complex", because it |
| 518 | // requires complex logic to pick which counter to update. |
| 519 | GlobalVariable *GCOVProfiler::buildEdgeLookupTable( |
| 520 | Function *F, |
| 521 | GlobalVariable *Counters, |
| 522 | const UniqueVector<BasicBlock *> &Preds, |
| 523 | const UniqueVector<BasicBlock *> &Succs) { |
| 524 | // TODO: support invoke, threads. We rely on the fact that nothing can modify |
| 525 | // the whole-Module pred edge# between the time we set it and the time we next |
| 526 | // read it. Threads and invoke make this untrue. |
| 527 | |
| 528 | // emit [(succs * preds) x i64*], logically [succ x [pred x i64*]]. |
| 529 | const Type *Int64PtrTy = Type::getInt64PtrTy(*Ctx); |
| 530 | const ArrayType *EdgeTableTy = ArrayType::get( |
| 531 | Int64PtrTy, Succs.size() * Preds.size()); |
| 532 | |
| 533 | Constant **EdgeTable = new Constant*[Succs.size() * Preds.size()]; |
| 534 | Constant *NullValue = Constant::getNullValue(Int64PtrTy); |
| 535 | for (int i = 0, ie = Succs.size() * Preds.size(); i != ie; ++i) |
| 536 | EdgeTable[i] = NullValue; |
| 537 | |
| 538 | unsigned Edge = 0; |
| 539 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 540 | TerminatorInst *TI = BB->getTerminator(); |
| 541 | int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors(); |
Nick Lewycky | 7a2ba2f | 2011-04-28 21:35:49 +0000 | [diff] [blame] | 542 | if (Successors > 1 && !isa<BranchInst>(TI) && !isa<ReturnInst>(TI)) { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 543 | for (int i = 0; i != Successors; ++i) { |
| 544 | BasicBlock *Succ = TI->getSuccessor(i); |
| 545 | IRBuilder<> builder(Succ); |
| 546 | Value *Counter = builder.CreateConstInBoundsGEP2_64(Counters, 0, |
| 547 | Edge + i); |
| 548 | EdgeTable[((Succs.idFor(Succ)-1) * Preds.size()) + |
| 549 | (Preds.idFor(BB)-1)] = cast<Constant>(Counter); |
| 550 | } |
| 551 | } |
| 552 | Edge += Successors; |
| 553 | } |
| 554 | |
| 555 | GlobalVariable *EdgeTableGV = |
| 556 | new GlobalVariable( |
| 557 | *M, EdgeTableTy, true, GlobalValue::InternalLinkage, |
| 558 | ConstantArray::get(EdgeTableTy, |
| 559 | &EdgeTable[0], Succs.size() * Preds.size()), |
| 560 | "__llvm_gcda_edge_table"); |
| 561 | EdgeTableGV->setUnnamedAddr(true); |
| 562 | return EdgeTableGV; |
| 563 | } |
| 564 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 565 | Constant *GCOVProfiler::getStartFileFunc() { |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 566 | const Type *Args[] = { Type::getInt8PtrTy(*Ctx) }; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 567 | const FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), |
| 568 | Args, false); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 569 | return M->getOrInsertFunction("llvm_gcda_start_file", FTy); |
| 570 | } |
| 571 | |
| 572 | Constant *GCOVProfiler::getIncrementIndirectCounterFunc() { |
| 573 | const Type *Args[] = { |
| 574 | Type::getInt32PtrTy(*Ctx), // uint32_t *predecessor |
| 575 | Type::getInt64PtrTy(*Ctx)->getPointerTo(), // uint64_t **state_table_row |
| 576 | }; |
| 577 | const FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), |
| 578 | Args, false); |
| 579 | return M->getOrInsertFunction("llvm_gcda_increment_indirect_counter", FTy); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | Constant *GCOVProfiler::getEmitFunctionFunc() { |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 583 | const Type *Args[2] = { |
| 584 | Type::getInt32Ty(*Ctx), // uint32_t ident |
| 585 | Type::getInt8PtrTy(*Ctx), // const char *function_name |
| 586 | }; |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 587 | const FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), |
| 588 | Args, false); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 589 | return M->getOrInsertFunction("llvm_gcda_emit_function", FTy); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | Constant *GCOVProfiler::getEmitArcsFunc() { |
| 593 | const Type *Args[] = { |
| 594 | Type::getInt32Ty(*Ctx), // uint32_t num_counters |
| 595 | Type::getInt64PtrTy(*Ctx), // uint64_t *counters |
| 596 | }; |
| 597 | const FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), |
| 598 | Args, false); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 599 | return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | Constant *GCOVProfiler::getEndFileFunc() { |
| 603 | const FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 604 | return M->getOrInsertFunction("llvm_gcda_end_file", FTy); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 607 | GlobalVariable *GCOVProfiler::getEdgeStateValue() { |
| 608 | GlobalVariable *GV = M->getGlobalVariable("__llvm_gcov_global_state_pred"); |
| 609 | if (!GV) { |
| 610 | GV = new GlobalVariable(*M, Type::getInt32Ty(*Ctx), false, |
| 611 | GlobalValue::InternalLinkage, |
| 612 | ConstantInt::get(Type::getInt32Ty(*Ctx), |
| 613 | 0xffffffff), |
| 614 | "__llvm_gcov_global_state_pred"); |
| 615 | GV->setUnnamedAddr(true); |
| 616 | } |
| 617 | return GV; |
| 618 | } |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 619 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 620 | void GCOVProfiler::insertCounterWriteout( |
| 621 | DebugInfoFinder &DIF, |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 622 | SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> &CountersBySP) { |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 623 | const FunctionType *WriteoutFTy = |
| 624 | FunctionType::get(Type::getVoidTy(*Ctx), false); |
| 625 | Function *WriteoutF = Function::Create(WriteoutFTy, |
| 626 | GlobalValue::InternalLinkage, |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 627 | "__llvm_gcov_writeout", M); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 628 | WriteoutF->setUnnamedAddr(true); |
| 629 | BasicBlock *BB = BasicBlock::Create(*Ctx, "", WriteoutF); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 630 | IRBuilder<> Builder(BB); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 631 | |
| 632 | Constant *StartFile = getStartFileFunc(); |
| 633 | Constant *EmitFunction = getEmitFunctionFunc(); |
| 634 | Constant *EmitArcs = getEmitArcsFunc(); |
| 635 | Constant *EndFile = getEndFileFunc(); |
| 636 | |
| 637 | for (DebugInfoFinder::iterator CUI = DIF.compile_unit_begin(), |
| 638 | CUE = DIF.compile_unit_end(); CUI != CUE; ++CUI) { |
| 639 | DICompileUnit compile_unit(*CUI); |
Nick Lewycky | 269687f | 2011-05-04 04:03:04 +0000 | [diff] [blame] | 640 | std::string FilenameGcda = mangleName(compile_unit, "gcda"); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 641 | Builder.CreateCall(StartFile, |
| 642 | Builder.CreateGlobalStringPtr(FilenameGcda)); |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 643 | for (SmallVector<std::pair<GlobalVariable *, MDNode *>, 8>::iterator |
| 644 | I = CountersBySP.begin(), E = CountersBySP.end(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 645 | I != E; ++I) { |
Nick Lewycky | 5409a18 | 2011-05-05 02:46:38 +0000 | [diff] [blame] | 646 | DISubprogram SP(I->second); |
| 647 | intptr_t ident = reinterpret_cast<intptr_t>(I->second); |
| 648 | Builder.CreateCall2(EmitFunction, |
| 649 | ConstantInt::get(Type::getInt32Ty(*Ctx), ident), |
| 650 | Builder.CreateGlobalStringPtr(SP.getName())); |
| 651 | |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 652 | GlobalVariable *GV = I->first; |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 653 | unsigned Arcs = |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 654 | cast<ArrayType>(GV->getType()->getElementType())->getNumElements(); |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 655 | Builder.CreateCall2(EmitArcs, |
| 656 | ConstantInt::get(Type::getInt32Ty(*Ctx), Arcs), |
| 657 | Builder.CreateConstGEP2_64(GV, 0, 0)); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 658 | } |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 659 | Builder.CreateCall(EndFile); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 660 | } |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 661 | Builder.CreateRetVoid(); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 662 | |
Nick Lewycky | 1790c9c | 2011-04-26 03:54:16 +0000 | [diff] [blame] | 663 | InsertProfilingShutdownCall(WriteoutF, M); |
Nick Lewycky | b192870 | 2011-04-16 01:20:23 +0000 | [diff] [blame] | 664 | } |