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