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