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