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