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