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