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