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