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