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