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