2011-04-16 01:20:23 +00:00
|
|
|
//===- GCOVProfiling.cpp - Insert edge counters for gcov profiling --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass implements GCOV-style profiling. When this pass is run it emits
|
|
|
|
// "gcno" files next to the existing source, and instruments the code that runs
|
|
|
|
// to records the edges between blocks that run and emit a complementary "gcda"
|
|
|
|
// file on exit.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "insert-gcov-profiling"
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2013-11-20 04:15:05 +00:00
|
|
|
#include "llvm/ADT/Hashing.h"
|
2011-04-16 01:20:23 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-06-29 12:38:19 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2011-04-16 01:20:23 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/UniqueVector.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/DebugInfo.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Pass.h"
|
2013-03-14 05:13:26 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-06-29 12:38:19 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/DebugLoc.h"
|
2013-03-26 22:47:50 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-06-29 12:38:19 +00:00
|
|
|
#include "llvm/Support/InstIterator.h"
|
2013-06-11 22:21:28 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
2012-06-29 12:38:19 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
2013-06-18 06:38:21 +00:00
|
|
|
#include <algorithm>
|
2011-04-16 01:20:23 +00:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-03-14 05:13:26 +00:00
|
|
|
static cl::opt<std::string>
|
|
|
|
DefaultGCOVVersion("default-gcov-version", cl::init("402*"), cl::Hidden,
|
|
|
|
cl::ValueRequired);
|
|
|
|
|
|
|
|
GCOVOptions GCOVOptions::getDefault() {
|
|
|
|
GCOVOptions Options;
|
|
|
|
Options.EmitNotes = true;
|
|
|
|
Options.EmitData = true;
|
|
|
|
Options.UseCfgChecksum = false;
|
|
|
|
Options.NoRedZone = false;
|
|
|
|
Options.FunctionNamesInData = true;
|
|
|
|
|
|
|
|
if (DefaultGCOVVersion.size() != 4) {
|
|
|
|
llvm::report_fatal_error(std::string("Invalid -default-gcov-version: ") +
|
|
|
|
DefaultGCOVVersion);
|
|
|
|
}
|
|
|
|
memcpy(Options.Version, DefaultGCOVVersion.c_str(), 4);
|
|
|
|
return Options;
|
|
|
|
}
|
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
namespace {
|
2013-11-20 04:15:05 +00:00
|
|
|
class GCOVFunction;
|
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
class GCOVProfiler : public ModulePass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
2013-03-14 05:13:26 +00:00
|
|
|
GCOVProfiler() : ModulePass(ID), Options(GCOVOptions::getDefault()) {
|
2013-11-20 04:15:05 +00:00
|
|
|
init();
|
2011-04-21 01:56:25 +00:00
|
|
|
}
|
2013-03-14 05:13:26 +00:00
|
|
|
GCOVProfiler(const GCOVOptions &Options) : ModulePass(ID), Options(Options){
|
|
|
|
assert((Options.EmitNotes || Options.EmitData) &&
|
|
|
|
"GCOVProfiler asked to do nothing?");
|
2013-11-20 04:15:05 +00:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
~GCOVProfiler() {
|
|
|
|
DeleteContainerPointers(Funcs);
|
|
|
|
}
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "GCOV Profiler";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void init() {
|
2013-03-14 05:13:26 +00:00
|
|
|
ReversedVersion[0] = Options.Version[3];
|
|
|
|
ReversedVersion[1] = Options.Version[2];
|
|
|
|
ReversedVersion[2] = Options.Version[1];
|
|
|
|
ReversedVersion[3] = Options.Version[0];
|
|
|
|
ReversedVersion[4] = '\0';
|
2011-04-16 01:20:23 +00:00
|
|
|
initializeGCOVProfilerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2011-05-04 04:03:04 +00:00
|
|
|
bool runOnModule(Module &M);
|
|
|
|
|
2013-03-13 22:55:42 +00:00
|
|
|
// Create the .gcno files for the Module based on DebugInfo.
|
|
|
|
void emitProfileNotes();
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2011-04-16 02:05:18 +00:00
|
|
|
// Modify the program to track transitions along edges and call into the
|
|
|
|
// profiling runtime to emit .gcda files when run.
|
2011-08-17 22:49:38 +00:00
|
|
|
bool emitProfileArcs();
|
2011-04-16 02:05:18 +00:00
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
// Get pointers to the functions in the runtime library.
|
|
|
|
Constant *getStartFileFunc();
|
2012-05-28 06:10:56 +00:00
|
|
|
Constant *getIncrementIndirectCounterFunc();
|
2011-04-16 01:20:23 +00:00
|
|
|
Constant *getEmitFunctionFunc();
|
|
|
|
Constant *getEmitArcsFunc();
|
2013-11-12 04:59:08 +00:00
|
|
|
Constant *getSummaryInfoFunc();
|
2013-03-19 21:03:22 +00:00
|
|
|
Constant *getDeleteWriteoutFunctionListFunc();
|
2013-03-18 23:04:39 +00:00
|
|
|
Constant *getDeleteFlushFunctionListFunc();
|
2011-04-16 01:20:23 +00:00
|
|
|
Constant *getEndFileFunc();
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
// Create or retrieve an i32 state value that is used to represent the
|
|
|
|
// pred block number for certain non-trivial edges.
|
|
|
|
GlobalVariable *getEdgeStateValue();
|
|
|
|
|
|
|
|
// Produce a table of pointers to counters, by predecessor and successor
|
|
|
|
// block number.
|
|
|
|
GlobalVariable *buildEdgeLookupTable(Function *F,
|
|
|
|
GlobalVariable *Counter,
|
2013-03-13 22:55:42 +00:00
|
|
|
const UniqueVector<BasicBlock *>&Preds,
|
|
|
|
const UniqueVector<BasicBlock*>&Succs);
|
2011-04-26 03:54:16 +00:00
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
// Add the function to write out all our counters to the global destructor
|
|
|
|
// list.
|
2013-03-18 23:04:39 +00:00
|
|
|
Function *insertCounterWriteout(ArrayRef<std::pair<GlobalVariable*,
|
|
|
|
MDNode*> >);
|
|
|
|
Function *insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> >);
|
2012-05-28 06:10:56 +00:00
|
|
|
void insertIndirectCounterIncrement();
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2013-03-28 22:40:08 +00:00
|
|
|
std::string mangleName(DICompileUnit CU, const char *NewStem);
|
2011-05-04 04:03:04 +00:00
|
|
|
|
2013-03-14 05:13:26 +00:00
|
|
|
GCOVOptions Options;
|
|
|
|
|
|
|
|
// Reversed, NUL-terminated copy of Options.Version.
|
|
|
|
char ReversedVersion[5];
|
2013-11-20 04:15:05 +00:00
|
|
|
// Checksum, produced by hash of EdgeDestinations
|
2013-11-21 04:01:05 +00:00
|
|
|
SmallVector<uint32_t, 4> FileChecksums;
|
2011-04-21 01:56:25 +00:00
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
Module *M;
|
2011-04-16 01:20:23 +00:00
|
|
|
LLVMContext *Ctx;
|
2013-11-20 04:15:05 +00:00
|
|
|
SmallVector<GCOVFunction *, 16> Funcs;
|
2011-04-16 01:20:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char GCOVProfiler::ID = 0;
|
|
|
|
INITIALIZE_PASS(GCOVProfiler, "insert-gcov-profiling",
|
|
|
|
"Insert instrumentation for GCOV profiling", false, false)
|
|
|
|
|
2013-03-14 05:13:26 +00:00
|
|
|
ModulePass *llvm::createGCOVProfilerPass(const GCOVOptions &Options) {
|
|
|
|
return new GCOVProfiler(Options);
|
2011-04-21 01:56:25 +00:00
|
|
|
}
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2013-11-20 04:15:05 +00:00
|
|
|
static StringRef getFunctionName(DISubprogram SP) {
|
2013-03-19 01:37:55 +00:00
|
|
|
if (!SP.getLinkageName().empty())
|
|
|
|
return SP.getLinkageName();
|
|
|
|
return SP.getName();
|
|
|
|
}
|
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
namespace {
|
|
|
|
class GCOVRecord {
|
|
|
|
protected:
|
2013-07-17 03:43:10 +00:00
|
|
|
static const char *const LinesTag;
|
|
|
|
static const char *const FunctionTag;
|
|
|
|
static const char *const BlockTag;
|
|
|
|
static const char *const EdgeTag;
|
2011-04-16 01:20:23 +00:00
|
|
|
|
|
|
|
GCOVRecord() {}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
void writeBytes(const char *Bytes, int Size) {
|
|
|
|
os->write(Bytes, Size);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
void write(uint32_t i) {
|
|
|
|
writeBytes(reinterpret_cast<char*>(&i), 4);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the length measured in 4-byte blocks that will be used to
|
|
|
|
// represent this string in a GCOV file
|
2013-07-17 03:54:53 +00:00
|
|
|
static unsigned lengthOfGCOVString(StringRef s) {
|
2011-04-16 01:20:23 +00:00
|
|
|
// A GCOV string is a length, followed by a NUL, then between 0 and 3 NULs
|
2011-04-21 02:48:39 +00:00
|
|
|
// padding out to the next 4-byte word. The length is measured in 4-byte
|
|
|
|
// words including padding, not bytes of actual string.
|
2011-05-05 23:52:18 +00:00
|
|
|
return (s.size() / 4) + 1;
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
void writeGCOVString(StringRef s) {
|
|
|
|
uint32_t Len = lengthOfGCOVString(s);
|
|
|
|
write(Len);
|
|
|
|
writeBytes(s.data(), s.size());
|
2011-04-16 01:20:23 +00:00
|
|
|
|
|
|
|
// Write 1 to 4 bytes of NUL padding.
|
2011-04-28 21:35:49 +00:00
|
|
|
assert((unsigned)(4 - (s.size() % 4)) > 0);
|
|
|
|
assert((unsigned)(4 - (s.size() % 4)) <= 4);
|
|
|
|
writeBytes("\0\0\0\0", 4 - (s.size() % 4));
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
raw_ostream *os;
|
|
|
|
};
|
2013-07-17 03:43:10 +00:00
|
|
|
const char *const GCOVRecord::LinesTag = "\0\0\x45\x01";
|
|
|
|
const char *const GCOVRecord::FunctionTag = "\0\0\0\1";
|
|
|
|
const char *const GCOVRecord::BlockTag = "\0\0\x41\x01";
|
|
|
|
const char *const GCOVRecord::EdgeTag = "\0\0\x43\x01";
|
2011-04-16 01:20:23 +00:00
|
|
|
|
|
|
|
class GCOVFunction;
|
|
|
|
class GCOVBlock;
|
|
|
|
|
|
|
|
// Constructed only by requesting it from a GCOVBlock, this object stores a
|
2011-09-20 18:35:00 +00:00
|
|
|
// list of line numbers and a single filename, representing lines that belong
|
|
|
|
// to the block.
|
2011-04-16 01:20:23 +00:00
|
|
|
class GCOVLines : public GCOVRecord {
|
|
|
|
public:
|
2011-04-26 03:54:16 +00:00
|
|
|
void addLine(uint32_t Line) {
|
|
|
|
Lines.push_back(Line);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 03:54:53 +00:00
|
|
|
uint32_t length() const {
|
2011-11-27 23:22:20 +00:00
|
|
|
// Here 2 = 1 for string length + 1 for '0' id#.
|
2011-09-20 18:35:00 +00:00
|
|
|
return lengthOfGCOVString(Filename) + 2 + Lines.size();
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-09-20 18:35:00 +00:00
|
|
|
void writeOut() {
|
|
|
|
write(0);
|
|
|
|
writeGCOVString(Filename);
|
|
|
|
for (int i = 0, e = Lines.size(); i != e; ++i)
|
|
|
|
write(Lines[i]);
|
|
|
|
}
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2011-09-20 18:35:00 +00:00
|
|
|
GCOVLines(StringRef F, raw_ostream *os)
|
|
|
|
: Filename(F) {
|
2011-04-16 01:20:23 +00:00
|
|
|
this->os = os;
|
|
|
|
}
|
|
|
|
|
2011-09-20 18:48:56 +00:00
|
|
|
private:
|
2011-09-20 18:35:00 +00:00
|
|
|
StringRef Filename;
|
2011-04-26 03:54:16 +00:00
|
|
|
SmallVector<uint32_t, 32> Lines;
|
2011-04-16 01:20:23 +00:00
|
|
|
};
|
|
|
|
|
2013-06-18 06:38:21 +00:00
|
|
|
|
|
|
|
// Sorting function for deterministic behaviour in GCOVBlock::writeOut.
|
|
|
|
struct StringKeySort {
|
|
|
|
bool operator()(StringMapEntry<GCOVLines *> *LHS,
|
|
|
|
StringMapEntry<GCOVLines *> *RHS) const {
|
|
|
|
return LHS->getKey() < RHS->getKey();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
// Represent a basic block in GCOV. Each block has a unique number in the
|
|
|
|
// function, number of lines belonging to each block, and a set of edges to
|
|
|
|
// other blocks.
|
|
|
|
class GCOVBlock : public GCOVRecord {
|
|
|
|
public:
|
2011-09-20 17:55:19 +00:00
|
|
|
GCOVLines &getFile(StringRef Filename) {
|
2011-04-26 03:54:16 +00:00
|
|
|
GCOVLines *&Lines = LinesByFile[Filename];
|
|
|
|
if (!Lines) {
|
2011-09-20 18:35:00 +00:00
|
|
|
Lines = new GCOVLines(Filename, os);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
2011-04-26 03:54:16 +00:00
|
|
|
return *Lines;
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
void addEdge(GCOVBlock &Successor) {
|
|
|
|
OutEdges.push_back(&Successor);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
void writeOut() {
|
|
|
|
uint32_t Len = 3;
|
2013-06-18 06:38:21 +00:00
|
|
|
SmallVector<StringMapEntry<GCOVLines *> *, 32> SortedLinesByFile;
|
2011-04-26 03:54:16 +00:00
|
|
|
for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(),
|
|
|
|
E = LinesByFile.end(); I != E; ++I) {
|
2011-09-20 18:35:00 +00:00
|
|
|
Len += I->second->length();
|
2013-06-18 06:38:21 +00:00
|
|
|
SortedLinesByFile.push_back(&*I);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
writeBytes(LinesTag, 4);
|
|
|
|
write(Len);
|
|
|
|
write(Number);
|
2013-06-18 06:38:21 +00:00
|
|
|
|
|
|
|
StringKeySort Sorter;
|
|
|
|
std::sort(SortedLinesByFile.begin(), SortedLinesByFile.end(), Sorter);
|
2013-07-04 01:31:24 +00:00
|
|
|
for (SmallVectorImpl<StringMapEntry<GCOVLines *> *>::iterator
|
2013-06-18 06:38:21 +00:00
|
|
|
I = SortedLinesByFile.begin(), E = SortedLinesByFile.end();
|
|
|
|
I != E; ++I)
|
|
|
|
(*I)->getValue()->writeOut();
|
2011-04-26 03:54:16 +00:00
|
|
|
write(0);
|
|
|
|
write(0);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~GCOVBlock() {
|
2011-04-26 03:54:16 +00:00
|
|
|
DeleteContainerSeconds(LinesByFile);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class GCOVFunction;
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
GCOVBlock(uint32_t Number, raw_ostream *os)
|
|
|
|
: Number(Number) {
|
2011-04-16 01:20:23 +00:00
|
|
|
this->os = os;
|
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
uint32_t Number;
|
|
|
|
StringMap<GCOVLines *> LinesByFile;
|
|
|
|
SmallVector<GCOVBlock *, 4> OutEdges;
|
2011-04-16 01:20:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// A function has a unique identifier, a checksum (we leave as zero) and a
|
|
|
|
// set of blocks and a map of edges between blocks. This is the only GCOV
|
|
|
|
// object users can construct, the blocks and lines will be rooted here.
|
|
|
|
class GCOVFunction : public GCOVRecord {
|
|
|
|
public:
|
2013-03-07 08:28:49 +00:00
|
|
|
GCOVFunction(DISubprogram SP, raw_ostream *os, uint32_t Ident,
|
2013-11-20 04:15:05 +00:00
|
|
|
bool UseCfgChecksum) :
|
|
|
|
SP(SP), Ident(Ident), UseCfgChecksum(UseCfgChecksum), CfgChecksum(0) {
|
2011-04-16 01:20:23 +00:00
|
|
|
this->os = os;
|
|
|
|
|
|
|
|
Function *F = SP.getFunction();
|
2011-11-27 23:22:20 +00:00
|
|
|
DEBUG(dbgs() << "Function: " << F->getName() << "\n");
|
2011-04-16 01:20:23 +00:00
|
|
|
uint32_t i = 0;
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
2011-04-26 03:54:16 +00:00
|
|
|
Blocks[BB] = new GCOVBlock(i++, os);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
2011-04-26 03:54:16 +00:00
|
|
|
ReturnBlock = new GCOVBlock(i++, os);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~GCOVFunction() {
|
2011-04-26 03:54:16 +00:00
|
|
|
DeleteContainerSeconds(Blocks);
|
|
|
|
delete ReturnBlock;
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
GCOVBlock &getBlock(BasicBlock *BB) {
|
|
|
|
return *Blocks[BB];
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
GCOVBlock &getReturnBlock() {
|
|
|
|
return *ReturnBlock;
|
2011-04-21 03:18:00 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 04:15:05 +00:00
|
|
|
std::string getEdgeDestinations() {
|
|
|
|
std::string EdgeDestinations;
|
|
|
|
raw_string_ostream EDOS(EdgeDestinations);
|
|
|
|
Function *F = Blocks.begin()->first->getParent();
|
|
|
|
for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
|
|
|
|
GCOVBlock &Block = *Blocks[I];
|
|
|
|
for (int i = 0, e = Block.OutEdges.size(); i != e; ++i)
|
|
|
|
EDOS << Block.OutEdges[i]->Number;
|
|
|
|
}
|
|
|
|
return EdgeDestinations;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setCfgChecksum(uint32_t Checksum) {
|
|
|
|
CfgChecksum = Checksum;
|
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
void writeOut() {
|
2013-11-20 04:15:05 +00:00
|
|
|
writeBytes(FunctionTag, 4);
|
|
|
|
uint32_t BlockLen = 1 + 1 + 1 + lengthOfGCOVString(getFunctionName(SP)) +
|
|
|
|
1 + lengthOfGCOVString(SP.getFilename()) + 1;
|
|
|
|
if (UseCfgChecksum)
|
|
|
|
++BlockLen;
|
|
|
|
write(BlockLen);
|
|
|
|
write(Ident);
|
|
|
|
write(0); // lineno checksum
|
|
|
|
if (UseCfgChecksum)
|
|
|
|
write(CfgChecksum);
|
|
|
|
writeGCOVString(getFunctionName(SP));
|
|
|
|
writeGCOVString(SP.getFilename());
|
|
|
|
write(SP.getLineNumber());
|
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
// Emit count of blocks.
|
2011-04-26 03:54:16 +00:00
|
|
|
writeBytes(BlockTag, 4);
|
|
|
|
write(Blocks.size() + 1);
|
|
|
|
for (int i = 0, e = Blocks.size() + 1; i != e; ++i) {
|
|
|
|
write(0); // No flags on our blocks.
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
2011-11-27 23:22:20 +00:00
|
|
|
DEBUG(dbgs() << Blocks.size() << " blocks.\n");
|
2011-04-16 01:20:23 +00:00
|
|
|
|
|
|
|
// Emit edges between blocks.
|
2013-06-18 06:38:21 +00:00
|
|
|
if (Blocks.empty()) return;
|
|
|
|
Function *F = Blocks.begin()->first->getParent();
|
|
|
|
for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
|
|
|
|
GCOVBlock &Block = *Blocks[I];
|
2011-04-26 03:54:16 +00:00
|
|
|
if (Block.OutEdges.empty()) continue;
|
|
|
|
|
|
|
|
writeBytes(EdgeTag, 4);
|
|
|
|
write(Block.OutEdges.size() * 2 + 1);
|
|
|
|
write(Block.Number);
|
|
|
|
for (int i = 0, e = Block.OutEdges.size(); i != e; ++i) {
|
2011-11-27 23:22:20 +00:00
|
|
|
DEBUG(dbgs() << Block.Number << " -> " << Block.OutEdges[i]->Number
|
|
|
|
<< "\n");
|
2011-04-26 03:54:16 +00:00
|
|
|
write(Block.OutEdges[i]->Number);
|
|
|
|
write(0); // no flags
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit lines for each block.
|
2013-06-18 06:38:21 +00:00
|
|
|
for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
|
|
|
|
Blocks[I]->writeOut();
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-11-20 04:15:05 +00:00
|
|
|
DISubprogram SP;
|
|
|
|
uint32_t Ident;
|
|
|
|
bool UseCfgChecksum;
|
|
|
|
uint32_t CfgChecksum;
|
2011-04-26 03:54:16 +00:00
|
|
|
DenseMap<BasicBlock *, GCOVBlock *> Blocks;
|
|
|
|
GCOVBlock *ReturnBlock;
|
2011-04-16 01:20:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-03-28 22:40:08 +00:00
|
|
|
std::string GCOVProfiler::mangleName(DICompileUnit CU, const char *NewStem) {
|
2011-05-04 04:03:04 +00:00
|
|
|
if (NamedMDNode *GCov = M->getNamedMetadata("llvm.gcov")) {
|
|
|
|
for (int i = 0, e = GCov->getNumOperands(); i != e; ++i) {
|
|
|
|
MDNode *N = GCov->getOperand(i);
|
|
|
|
if (N->getNumOperands() != 2) continue;
|
2011-05-05 00:03:30 +00:00
|
|
|
MDString *GCovFile = dyn_cast<MDString>(N->getOperand(0));
|
2011-05-04 04:03:04 +00:00
|
|
|
MDNode *CompileUnit = dyn_cast<MDNode>(N->getOperand(1));
|
2011-05-05 00:03:30 +00:00
|
|
|
if (!GCovFile || !CompileUnit) continue;
|
|
|
|
if (CompileUnit == CU) {
|
2012-08-30 00:34:21 +00:00
|
|
|
SmallString<128> Filename = GCovFile->getString();
|
|
|
|
sys::path::replace_extension(Filename, NewStem);
|
|
|
|
return Filename.str();
|
2011-05-05 00:03:30 +00:00
|
|
|
}
|
2011-05-04 04:03:04 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-05 00:03:30 +00:00
|
|
|
|
2012-08-30 00:34:21 +00:00
|
|
|
SmallString<128> Filename = CU.getFilename();
|
2011-05-05 00:03:30 +00:00
|
|
|
sys::path::replace_extension(Filename, NewStem);
|
2013-03-26 22:47:50 +00:00
|
|
|
StringRef FName = sys::path::filename(Filename);
|
|
|
|
SmallString<128> CurPath;
|
|
|
|
if (sys::fs::current_path(CurPath)) return FName;
|
|
|
|
sys::path::append(CurPath, FName.str());
|
|
|
|
return CurPath.str();
|
2011-05-04 04:03:04 +00:00
|
|
|
}
|
|
|
|
|
2011-04-16 02:05:18 +00:00
|
|
|
bool GCOVProfiler::runOnModule(Module &M) {
|
2011-04-26 03:54:16 +00:00
|
|
|
this->M = &M;
|
2011-04-16 02:05:18 +00:00
|
|
|
Ctx = &M.getContext();
|
|
|
|
|
2013-03-14 05:13:26 +00:00
|
|
|
if (Options.EmitNotes) emitProfileNotes();
|
|
|
|
if (Options.EmitData) return emitProfileArcs();
|
2011-04-21 01:56:25 +00:00
|
|
|
return false;
|
2011-04-16 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 22:55:42 +00:00
|
|
|
void GCOVProfiler::emitProfileNotes() {
|
2011-08-17 22:49:38 +00:00
|
|
|
NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
|
2011-11-27 23:22:20 +00:00
|
|
|
if (!CU_Nodes) return;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
|
|
|
|
// Each compile unit gets its own .gcno file. This means that whether we run
|
|
|
|
// this pass over the original .o's as they're produced, or run it after
|
|
|
|
// LTO, we'll generate the same .gcno files.
|
|
|
|
|
|
|
|
DICompileUnit CU(CU_Nodes->getOperand(i));
|
|
|
|
std::string ErrorInfo;
|
2013-03-28 22:40:08 +00:00
|
|
|
raw_fd_ostream out(mangleName(CU, "gcno").c_str(), ErrorInfo,
|
2013-07-16 19:44:17 +00:00
|
|
|
sys::fs::F_Binary);
|
2013-11-20 04:15:05 +00:00
|
|
|
std::string EdgeDestinations;
|
2011-11-27 23:22:20 +00:00
|
|
|
|
|
|
|
DIArray SPs = CU.getSubprograms();
|
|
|
|
for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
|
|
|
|
DISubprogram SP(SPs.getElement(i));
|
2013-06-28 05:43:10 +00:00
|
|
|
assert((!SP || SP.isSubprogram()) &&
|
|
|
|
"A MDNode in subprograms of a CU should be null or a DISubprogram.");
|
|
|
|
if (!SP)
|
|
|
|
continue;
|
2011-11-27 23:22:20 +00:00
|
|
|
|
|
|
|
Function *F = SP.getFunction();
|
|
|
|
if (!F) continue;
|
2013-11-20 04:15:05 +00:00
|
|
|
GCOVFunction *Func =
|
|
|
|
new GCOVFunction(SP, &out, i, Options.UseCfgChecksum);
|
|
|
|
Funcs.push_back(Func);
|
2011-11-27 23:22:20 +00:00
|
|
|
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
2013-11-20 04:15:05 +00:00
|
|
|
GCOVBlock &Block = Func->getBlock(BB);
|
2011-11-27 23:22:20 +00:00
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
|
|
|
if (int successors = TI->getNumSuccessors()) {
|
|
|
|
for (int i = 0; i != successors; ++i) {
|
2013-11-20 04:15:05 +00:00
|
|
|
Block.addEdge(Func->getBlock(TI->getSuccessor(i)));
|
2011-08-17 22:49:38 +00:00
|
|
|
}
|
2011-11-27 23:22:20 +00:00
|
|
|
} else if (isa<ReturnInst>(TI)) {
|
2013-11-20 04:15:05 +00:00
|
|
|
Block.addEdge(Func->getReturnBlock());
|
2011-11-27 23:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Line = 0;
|
|
|
|
for (BasicBlock::iterator I = BB->begin(), IE = BB->end();
|
|
|
|
I != IE; ++I) {
|
|
|
|
const DebugLoc &Loc = I->getDebugLoc();
|
|
|
|
if (Loc.isUnknown()) continue;
|
|
|
|
if (Line == Loc.getLine()) continue;
|
|
|
|
Line = Loc.getLine();
|
|
|
|
if (SP != getDISubprogram(Loc.getScope(*Ctx))) continue;
|
|
|
|
|
|
|
|
GCOVLines &Lines = Block.getFile(SP.getFilename());
|
|
|
|
Lines.addLine(Loc.getLine());
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-20 04:15:05 +00:00
|
|
|
EdgeDestinations += Func->getEdgeDestinations();
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
2013-11-20 04:15:05 +00:00
|
|
|
|
2013-11-21 04:01:05 +00:00
|
|
|
FileChecksums.push_back(hash_value(EdgeDestinations));
|
2013-11-20 04:15:05 +00:00
|
|
|
out.write("oncg", 4);
|
|
|
|
out.write(ReversedVersion, 4);
|
2013-11-21 04:01:05 +00:00
|
|
|
out.write(reinterpret_cast<char*>(&FileChecksums.back()), 4);
|
2013-11-20 04:15:05 +00:00
|
|
|
|
|
|
|
for (SmallVectorImpl<GCOVFunction *>::iterator I = Funcs.begin(),
|
2013-11-21 04:01:05 +00:00
|
|
|
E = Funcs.end(); I != E; ++I) {
|
|
|
|
GCOVFunction *Func = *I;
|
|
|
|
Func->setCfgChecksum(FileChecksums.back());
|
|
|
|
Func->writeOut();
|
|
|
|
}
|
2013-11-20 04:15:05 +00:00
|
|
|
|
2011-11-27 23:22:20 +00:00
|
|
|
out.write("\0\0\0\0\0\0\0\0", 8); // EOF
|
|
|
|
out.close();
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-17 22:49:38 +00:00
|
|
|
bool GCOVProfiler::emitProfileArcs() {
|
|
|
|
NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
|
|
|
|
if (!CU_Nodes) return false;
|
|
|
|
|
|
|
|
bool Result = false;
|
2012-05-28 06:10:56 +00:00
|
|
|
bool InsertIndCounterIncrCode = false;
|
2011-08-17 22:49:38 +00:00
|
|
|
for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
|
|
|
|
DICompileUnit CU(CU_Nodes->getOperand(i));
|
|
|
|
DIArray SPs = CU.getSubprograms();
|
|
|
|
SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP;
|
|
|
|
for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
|
|
|
|
DISubprogram SP(SPs.getElement(i));
|
2013-06-28 05:43:10 +00:00
|
|
|
assert((!SP || SP.isSubprogram()) &&
|
|
|
|
"A MDNode in subprograms of a CU should be null or a DISubprogram.");
|
|
|
|
if (!SP)
|
|
|
|
continue;
|
2011-08-17 22:49:38 +00:00
|
|
|
Function *F = SP.getFunction();
|
|
|
|
if (!F) continue;
|
|
|
|
if (!Result) Result = true;
|
|
|
|
unsigned Edges = 0;
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
|
|
|
if (isa<ReturnInst>(TI))
|
|
|
|
++Edges;
|
|
|
|
else
|
|
|
|
Edges += TI->getNumSuccessors();
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayType *CounterTy =
|
2011-04-26 03:54:16 +00:00
|
|
|
ArrayType::get(Type::getInt64Ty(*Ctx), Edges);
|
2011-08-17 22:49:38 +00:00
|
|
|
GlobalVariable *Counters =
|
2011-04-26 03:54:16 +00:00
|
|
|
new GlobalVariable(*M, CounterTy, false,
|
2011-04-16 01:20:23 +00:00
|
|
|
GlobalValue::InternalLinkage,
|
2011-04-26 03:54:16 +00:00
|
|
|
Constant::getNullValue(CounterTy),
|
2012-06-23 11:37:03 +00:00
|
|
|
"__llvm_gcov_ctr");
|
2011-08-17 22:49:38 +00:00
|
|
|
CountersBySP.push_back(std::make_pair(Counters, (MDNode*)SP));
|
|
|
|
|
|
|
|
UniqueVector<BasicBlock *> ComplexEdgePreds;
|
|
|
|
UniqueVector<BasicBlock *> ComplexEdgeSuccs;
|
|
|
|
|
|
|
|
unsigned Edge = 0;
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
|
|
|
int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
|
|
|
|
if (Successors) {
|
|
|
|
if (Successors == 1) {
|
2013-08-20 23:52:00 +00:00
|
|
|
IRBuilder<> Builder(BB->getFirstInsertionPt());
|
2011-08-17 22:49:38 +00:00
|
|
|
Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
|
|
|
|
Edge);
|
|
|
|
Value *Count = Builder.CreateLoad(Counter);
|
2013-02-27 05:46:30 +00:00
|
|
|
Count = Builder.CreateAdd(Count, Builder.getInt64(1));
|
2011-08-17 22:49:38 +00:00
|
|
|
Builder.CreateStore(Count, Counter);
|
|
|
|
} else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
|
2013-08-20 23:52:00 +00:00
|
|
|
IRBuilder<> Builder(BI);
|
2013-02-27 05:46:30 +00:00
|
|
|
Value *Sel = Builder.CreateSelect(BI->getCondition(),
|
|
|
|
Builder.getInt64(Edge),
|
|
|
|
Builder.getInt64(Edge + 1));
|
2011-08-17 22:49:38 +00:00
|
|
|
SmallVector<Value *, 2> Idx;
|
2013-02-27 05:46:30 +00:00
|
|
|
Idx.push_back(Builder.getInt64(0));
|
2011-08-17 22:49:38 +00:00
|
|
|
Idx.push_back(Sel);
|
|
|
|
Value *Counter = Builder.CreateInBoundsGEP(Counters, Idx);
|
|
|
|
Value *Count = Builder.CreateLoad(Counter);
|
2013-02-27 05:46:30 +00:00
|
|
|
Count = Builder.CreateAdd(Count, Builder.getInt64(1));
|
2011-08-17 22:49:38 +00:00
|
|
|
Builder.CreateStore(Count, Counter);
|
|
|
|
} else {
|
|
|
|
ComplexEdgePreds.insert(BB);
|
|
|
|
for (int i = 0; i != Successors; ++i)
|
|
|
|
ComplexEdgeSuccs.insert(TI->getSuccessor(i));
|
|
|
|
}
|
2013-08-20 23:52:00 +00:00
|
|
|
|
2011-08-17 22:49:38 +00:00
|
|
|
Edge += Successors;
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-17 22:49:38 +00:00
|
|
|
|
|
|
|
if (!ComplexEdgePreds.empty()) {
|
|
|
|
GlobalVariable *EdgeTable =
|
2011-04-26 03:54:16 +00:00
|
|
|
buildEdgeLookupTable(F, Counters,
|
|
|
|
ComplexEdgePreds, ComplexEdgeSuccs);
|
2011-08-17 22:49:38 +00:00
|
|
|
GlobalVariable *EdgeState = getEdgeStateValue();
|
|
|
|
|
|
|
|
for (int i = 0, e = ComplexEdgePreds.size(); i != e; ++i) {
|
2013-08-20 23:52:00 +00:00
|
|
|
IRBuilder<> Builder(ComplexEdgePreds[i + 1]->getFirstInsertionPt());
|
2013-02-27 05:46:30 +00:00
|
|
|
Builder.CreateStore(Builder.getInt32(i), EdgeState);
|
2011-08-17 22:49:38 +00:00
|
|
|
}
|
2013-08-20 23:52:00 +00:00
|
|
|
|
2011-08-17 22:49:38 +00:00
|
|
|
for (int i = 0, e = ComplexEdgeSuccs.size(); i != e; ++i) {
|
2013-08-20 23:52:00 +00:00
|
|
|
// Call runtime to perform increment.
|
|
|
|
IRBuilder<> Builder(ComplexEdgeSuccs[i+1]->getFirstInsertionPt());
|
2011-08-17 22:49:38 +00:00
|
|
|
Value *CounterPtrArray =
|
2011-04-26 03:54:16 +00:00
|
|
|
Builder.CreateConstInBoundsGEP2_64(EdgeTable, 0,
|
|
|
|
i * ComplexEdgePreds.size());
|
2012-05-25 23:55:00 +00:00
|
|
|
|
|
|
|
// Build code to increment the counter.
|
2012-05-28 06:10:56 +00:00
|
|
|
InsertIndCounterIncrCode = true;
|
|
|
|
Builder.CreateCall2(getIncrementIndirectCounterFunc(),
|
|
|
|
EdgeState, CounterPtrArray);
|
2011-08-17 22:49:38 +00:00
|
|
|
}
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-01 23:14:32 +00:00
|
|
|
|
2013-03-18 23:04:39 +00:00
|
|
|
Function *WriteoutF = insertCounterWriteout(CountersBySP);
|
|
|
|
Function *FlushF = insertFlush(CountersBySP);
|
|
|
|
|
|
|
|
// Create a small bit of code that registers the "__llvm_gcov_writeout" to
|
2013-03-19 21:03:22 +00:00
|
|
|
// be executed at exit and the "__llvm_gcov_flush" function to be executed
|
|
|
|
// when "__gcov_flush" is called.
|
2013-03-18 23:04:39 +00:00
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
|
|
|
Function *F = Function::Create(FTy, GlobalValue::InternalLinkage,
|
|
|
|
"__llvm_gcov_init", M);
|
|
|
|
F->setUnnamedAddr(true);
|
|
|
|
F->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
F->addFnAttr(Attribute::NoInline);
|
|
|
|
if (Options.NoRedZone)
|
|
|
|
F->addFnAttr(Attribute::NoRedZone);
|
|
|
|
|
|
|
|
BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
|
|
|
|
IRBuilder<> Builder(BB);
|
|
|
|
|
|
|
|
FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
2013-03-20 21:13:59 +00:00
|
|
|
Type *Params[] = {
|
|
|
|
PointerType::get(FTy, 0),
|
|
|
|
PointerType::get(FTy, 0)
|
|
|
|
};
|
|
|
|
FTy = FunctionType::get(Builder.getVoidTy(), Params, false);
|
|
|
|
|
2013-10-23 20:35:00 +00:00
|
|
|
// Initialize the environment and register the local writeout and flush
|
2013-03-20 21:13:59 +00:00
|
|
|
// functions.
|
|
|
|
Constant *GCOVInit = M->getOrInsertFunction("llvm_gcov_init", FTy);
|
|
|
|
Builder.CreateCall2(GCOVInit, WriteoutF, FlushF);
|
2013-03-18 23:04:39 +00:00
|
|
|
Builder.CreateRetVoid();
|
|
|
|
|
|
|
|
appendToGlobalCtors(*M, F, 0);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
2012-05-28 06:10:56 +00:00
|
|
|
|
|
|
|
if (InsertIndCounterIncrCode)
|
|
|
|
insertIndirectCounterIncrement();
|
|
|
|
|
2011-08-17 22:49:38 +00:00
|
|
|
return Result;
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
// All edges with successors that aren't branches are "complex", because it
|
|
|
|
// requires complex logic to pick which counter to update.
|
|
|
|
GlobalVariable *GCOVProfiler::buildEdgeLookupTable(
|
|
|
|
Function *F,
|
|
|
|
GlobalVariable *Counters,
|
|
|
|
const UniqueVector<BasicBlock *> &Preds,
|
|
|
|
const UniqueVector<BasicBlock *> &Succs) {
|
|
|
|
// TODO: support invoke, threads. We rely on the fact that nothing can modify
|
|
|
|
// the whole-Module pred edge# between the time we set it and the time we next
|
|
|
|
// read it. Threads and invoke make this untrue.
|
|
|
|
|
|
|
|
// emit [(succs * preds) x i64*], logically [succ x [pred x i64*]].
|
2012-11-17 13:49:37 +00:00
|
|
|
size_t TableSize = Succs.size() * Preds.size();
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Int64PtrTy = Type::getInt64PtrTy(*Ctx);
|
2012-11-17 13:49:37 +00:00
|
|
|
ArrayType *EdgeTableTy = ArrayType::get(Int64PtrTy, TableSize);
|
2011-04-26 03:54:16 +00:00
|
|
|
|
2012-11-17 13:49:37 +00:00
|
|
|
OwningArrayPtr<Constant *> EdgeTable(new Constant*[TableSize]);
|
2011-04-26 03:54:16 +00:00
|
|
|
Constant *NullValue = Constant::getNullValue(Int64PtrTy);
|
2012-11-17 13:49:37 +00:00
|
|
|
for (size_t i = 0; i != TableSize; ++i)
|
2011-04-26 03:54:16 +00:00
|
|
|
EdgeTable[i] = NullValue;
|
|
|
|
|
|
|
|
unsigned Edge = 0;
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
|
|
|
int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
|
2011-04-28 21:35:49 +00:00
|
|
|
if (Successors > 1 && !isa<BranchInst>(TI) && !isa<ReturnInst>(TI)) {
|
2011-04-26 03:54:16 +00:00
|
|
|
for (int i = 0; i != Successors; ++i) {
|
|
|
|
BasicBlock *Succ = TI->getSuccessor(i);
|
2013-02-27 05:46:30 +00:00
|
|
|
IRBuilder<> Builder(Succ);
|
|
|
|
Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
|
2011-04-26 03:54:16 +00:00
|
|
|
Edge + i);
|
|
|
|
EdgeTable[((Succs.idFor(Succ)-1) * Preds.size()) +
|
|
|
|
(Preds.idFor(BB)-1)] = cast<Constant>(Counter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Edge += Successors;
|
|
|
|
}
|
|
|
|
|
2012-11-17 13:49:37 +00:00
|
|
|
ArrayRef<Constant*> V(&EdgeTable[0], TableSize);
|
2011-04-26 03:54:16 +00:00
|
|
|
GlobalVariable *EdgeTableGV =
|
|
|
|
new GlobalVariable(
|
|
|
|
*M, EdgeTableTy, true, GlobalValue::InternalLinkage,
|
2011-06-22 09:24:39 +00:00
|
|
|
ConstantArray::get(EdgeTableTy, V),
|
2011-04-26 03:54:16 +00:00
|
|
|
"__llvm_gcda_edge_table");
|
|
|
|
EdgeTableGV->setUnnamedAddr(true);
|
|
|
|
return EdgeTableGV;
|
|
|
|
}
|
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
Constant *GCOVProfiler::getStartFileFunc() {
|
2013-03-07 08:28:49 +00:00
|
|
|
Type *Args[] = {
|
|
|
|
Type::getInt8PtrTy(*Ctx), // const char *orig_filename
|
|
|
|
Type::getInt8PtrTy(*Ctx), // const char version[4]
|
2013-11-20 04:15:05 +00:00
|
|
|
Type::getInt32Ty(*Ctx), // uint32_t checksum
|
2013-03-07 08:28:49 +00:00
|
|
|
};
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
|
2011-04-26 03:54:16 +00:00
|
|
|
return M->getOrInsertFunction("llvm_gcda_start_file", FTy);
|
|
|
|
}
|
|
|
|
|
2012-05-28 06:10:56 +00:00
|
|
|
Constant *GCOVProfiler::getIncrementIndirectCounterFunc() {
|
|
|
|
Type *Int32Ty = Type::getInt32Ty(*Ctx);
|
2012-05-25 23:55:00 +00:00
|
|
|
Type *Int64Ty = Type::getInt64Ty(*Ctx);
|
2012-05-28 06:10:56 +00:00
|
|
|
Type *Args[] = {
|
2012-10-24 17:25:11 +00:00
|
|
|
Int32Ty->getPointerTo(), // uint32_t *predecessor
|
|
|
|
Int64Ty->getPointerTo()->getPointerTo() // uint64_t **counters
|
2012-05-28 06:10:56 +00:00
|
|
|
};
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
|
|
|
|
return M->getOrInsertFunction("__llvm_gcov_indirect_counter_increment", FTy);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *GCOVProfiler::getEmitFunctionFunc() {
|
2013-11-20 04:15:05 +00:00
|
|
|
Type *Args[] = {
|
2011-05-05 02:46:38 +00:00
|
|
|
Type::getInt32Ty(*Ctx), // uint32_t ident
|
|
|
|
Type::getInt8PtrTy(*Ctx), // const char *function_name
|
2013-03-09 01:33:06 +00:00
|
|
|
Type::getInt8Ty(*Ctx), // uint8_t use_extra_checksum
|
2013-11-20 04:15:05 +00:00
|
|
|
Type::getInt32Ty(*Ctx), // uint32_t cfg_checksum
|
2011-05-05 02:46:38 +00:00
|
|
|
};
|
2012-05-25 23:55:00 +00:00
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
|
2011-04-26 03:54:16 +00:00
|
|
|
return M->getOrInsertFunction("llvm_gcda_emit_function", FTy);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *GCOVProfiler::getEmitArcsFunc() {
|
2011-07-12 14:06:48 +00:00
|
|
|
Type *Args[] = {
|
2011-04-16 01:20:23 +00:00
|
|
|
Type::getInt32Ty(*Ctx), // uint32_t num_counters
|
|
|
|
Type::getInt64PtrTy(*Ctx), // uint64_t *counters
|
|
|
|
};
|
2013-03-07 08:28:49 +00:00
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), Args, false);
|
2011-04-26 03:54:16 +00:00
|
|
|
return M->getOrInsertFunction("llvm_gcda_emit_arcs", FTy);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 04:59:08 +00:00
|
|
|
Constant *GCOVProfiler::getSummaryInfoFunc() {
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
|
|
|
return M->getOrInsertFunction("llvm_gcda_summary_info", FTy);
|
|
|
|
}
|
|
|
|
|
2013-03-19 21:03:22 +00:00
|
|
|
Constant *GCOVProfiler::getDeleteWriteoutFunctionListFunc() {
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
|
|
|
return M->getOrInsertFunction("llvm_delete_writeout_function_list", FTy);
|
|
|
|
}
|
|
|
|
|
2013-03-18 23:04:39 +00:00
|
|
|
Constant *GCOVProfiler::getDeleteFlushFunctionListFunc() {
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
|
|
|
return M->getOrInsertFunction("llvm_delete_flush_function_list", FTy);
|
|
|
|
}
|
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
Constant *GCOVProfiler::getEndFileFunc() {
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
2011-04-26 03:54:16 +00:00
|
|
|
return M->getOrInsertFunction("llvm_gcda_end_file", FTy);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
GlobalVariable *GCOVProfiler::getEdgeStateValue() {
|
|
|
|
GlobalVariable *GV = M->getGlobalVariable("__llvm_gcov_global_state_pred");
|
|
|
|
if (!GV) {
|
|
|
|
GV = new GlobalVariable(*M, Type::getInt32Ty(*Ctx), false,
|
|
|
|
GlobalValue::InternalLinkage,
|
|
|
|
ConstantInt::get(Type::getInt32Ty(*Ctx),
|
|
|
|
0xffffffff),
|
|
|
|
"__llvm_gcov_global_state_pred");
|
|
|
|
GV->setUnnamedAddr(true);
|
|
|
|
}
|
|
|
|
return GV;
|
|
|
|
}
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2013-03-18 23:04:39 +00:00
|
|
|
Function *GCOVProfiler::insertCounterWriteout(
|
2012-08-29 18:45:41 +00:00
|
|
|
ArrayRef<std::pair<GlobalVariable *, MDNode *> > CountersBySP) {
|
2012-09-13 00:09:55 +00:00
|
|
|
FunctionType *WriteoutFTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
|
|
|
Function *WriteoutF = M->getFunction("__llvm_gcov_writeout");
|
|
|
|
if (!WriteoutF)
|
|
|
|
WriteoutF = Function::Create(WriteoutFTy, GlobalValue::InternalLinkage,
|
|
|
|
"__llvm_gcov_writeout", M);
|
2011-04-16 01:20:23 +00:00
|
|
|
WriteoutF->setUnnamedAddr(true);
|
2012-12-19 07:18:57 +00:00
|
|
|
WriteoutF->addFnAttr(Attribute::NoInline);
|
2013-03-14 05:13:26 +00:00
|
|
|
if (Options.NoRedZone)
|
2012-12-19 07:18:57 +00:00
|
|
|
WriteoutF->addFnAttr(Attribute::NoRedZone);
|
2012-09-13 00:09:55 +00:00
|
|
|
|
|
|
|
BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", WriteoutF);
|
2011-04-26 03:54:16 +00:00
|
|
|
IRBuilder<> Builder(BB);
|
2011-04-16 01:20:23 +00:00
|
|
|
|
|
|
|
Constant *StartFile = getStartFileFunc();
|
|
|
|
Constant *EmitFunction = getEmitFunctionFunc();
|
|
|
|
Constant *EmitArcs = getEmitArcsFunc();
|
2013-11-12 04:59:08 +00:00
|
|
|
Constant *SummaryInfo = getSummaryInfoFunc();
|
2011-04-16 01:20:23 +00:00
|
|
|
Constant *EndFile = getEndFileFunc();
|
|
|
|
|
2011-08-17 22:49:38 +00:00
|
|
|
NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
|
|
|
|
if (CU_Nodes) {
|
|
|
|
for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
|
2012-09-13 14:32:30 +00:00
|
|
|
DICompileUnit CU(CU_Nodes->getOperand(i));
|
2013-03-28 22:40:08 +00:00
|
|
|
std::string FilenameGcda = mangleName(CU, "gcda");
|
2013-11-20 04:15:05 +00:00
|
|
|
Builder.CreateCall3(StartFile,
|
2013-03-07 08:28:49 +00:00
|
|
|
Builder.CreateGlobalStringPtr(FilenameGcda),
|
2013-11-20 04:15:05 +00:00
|
|
|
Builder.CreateGlobalStringPtr(ReversedVersion),
|
2013-11-21 04:01:05 +00:00
|
|
|
Builder.getInt32(FileChecksums[i]));
|
2013-03-09 02:06:37 +00:00
|
|
|
for (unsigned j = 0, e = CountersBySP.size(); j != e; ++j) {
|
|
|
|
DISubprogram SP(CountersBySP[j].second);
|
2013-11-20 04:15:05 +00:00
|
|
|
Builder.CreateCall4(
|
2013-03-19 01:37:55 +00:00
|
|
|
EmitFunction, Builder.getInt32(j),
|
|
|
|
Options.FunctionNamesInData ?
|
|
|
|
Builder.CreateGlobalStringPtr(getFunctionName(SP)) :
|
|
|
|
Constant::getNullValue(Builder.getInt8PtrTy()),
|
2013-11-20 04:15:05 +00:00
|
|
|
Builder.getInt8(Options.UseCfgChecksum),
|
2013-11-21 04:01:05 +00:00
|
|
|
Builder.getInt32(FileChecksums[i]));
|
2013-03-09 01:33:06 +00:00
|
|
|
|
2013-03-09 02:06:37 +00:00
|
|
|
GlobalVariable *GV = CountersBySP[j].first;
|
2011-08-17 22:49:38 +00:00
|
|
|
unsigned Arcs =
|
2011-04-16 01:20:23 +00:00
|
|
|
cast<ArrayType>(GV->getType()->getElementType())->getNumElements();
|
2011-08-17 22:49:38 +00:00
|
|
|
Builder.CreateCall2(EmitArcs,
|
2013-02-27 05:46:30 +00:00
|
|
|
Builder.getInt32(Arcs),
|
2011-08-17 22:49:38 +00:00
|
|
|
Builder.CreateConstGEP2_64(GV, 0, 0));
|
|
|
|
}
|
2013-11-12 04:59:08 +00:00
|
|
|
Builder.CreateCall(SummaryInfo);
|
2011-08-17 22:49:38 +00:00
|
|
|
Builder.CreateCall(EndFile);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-01 23:14:32 +00:00
|
|
|
Builder.CreateRetVoid();
|
2013-03-18 23:04:39 +00:00
|
|
|
return WriteoutF;
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
2012-05-28 06:10:56 +00:00
|
|
|
|
|
|
|
void GCOVProfiler::insertIndirectCounterIncrement() {
|
|
|
|
Function *Fn =
|
|
|
|
cast<Function>(GCOVProfiler::getIncrementIndirectCounterFunc());
|
|
|
|
Fn->setUnnamedAddr(true);
|
|
|
|
Fn->setLinkage(GlobalValue::InternalLinkage);
|
2012-12-19 07:18:57 +00:00
|
|
|
Fn->addFnAttr(Attribute::NoInline);
|
2013-03-14 05:13:26 +00:00
|
|
|
if (Options.NoRedZone)
|
2012-12-19 07:18:57 +00:00
|
|
|
Fn->addFnAttr(Attribute::NoRedZone);
|
2012-05-28 06:10:56 +00:00
|
|
|
|
|
|
|
// Create basic blocks for function.
|
|
|
|
BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", Fn);
|
|
|
|
IRBuilder<> Builder(BB);
|
|
|
|
|
|
|
|
BasicBlock *PredNotNegOne = BasicBlock::Create(*Ctx, "", Fn);
|
|
|
|
BasicBlock *CounterEnd = BasicBlock::Create(*Ctx, "", Fn);
|
|
|
|
BasicBlock *Exit = BasicBlock::Create(*Ctx, "exit", Fn);
|
|
|
|
|
|
|
|
// uint32_t pred = *predecessor;
|
|
|
|
// if (pred == 0xffffffff) return;
|
|
|
|
Argument *Arg = Fn->arg_begin();
|
|
|
|
Arg->setName("predecessor");
|
|
|
|
Value *Pred = Builder.CreateLoad(Arg, "pred");
|
2013-02-27 05:46:30 +00:00
|
|
|
Value *Cond = Builder.CreateICmpEQ(Pred, Builder.getInt32(0xffffffff));
|
2012-05-28 06:10:56 +00:00
|
|
|
BranchInst::Create(Exit, PredNotNegOne, Cond, BB);
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(PredNotNegOne);
|
|
|
|
|
|
|
|
// uint64_t *counter = counters[pred];
|
|
|
|
// if (!counter) return;
|
2013-02-27 05:46:30 +00:00
|
|
|
Value *ZExtPred = Builder.CreateZExt(Pred, Builder.getInt64Ty());
|
2012-05-28 06:10:56 +00:00
|
|
|
Arg = llvm::next(Fn->arg_begin());
|
|
|
|
Arg->setName("counters");
|
|
|
|
Value *GEP = Builder.CreateGEP(Arg, ZExtPred);
|
|
|
|
Value *Counter = Builder.CreateLoad(GEP, "counter");
|
2013-02-27 06:21:30 +00:00
|
|
|
Cond = Builder.CreateICmpEQ(Counter,
|
|
|
|
Constant::getNullValue(
|
|
|
|
Builder.getInt64Ty()->getPointerTo()));
|
2012-05-28 06:10:56 +00:00
|
|
|
Builder.CreateCondBr(Cond, Exit, CounterEnd);
|
|
|
|
|
|
|
|
// ++*counter;
|
|
|
|
Builder.SetInsertPoint(CounterEnd);
|
|
|
|
Value *Add = Builder.CreateAdd(Builder.CreateLoad(Counter),
|
2013-02-27 05:46:30 +00:00
|
|
|
Builder.getInt64(1));
|
2012-05-28 06:10:56 +00:00
|
|
|
Builder.CreateStore(Add, Counter);
|
|
|
|
Builder.CreateBr(Exit);
|
|
|
|
|
|
|
|
// Fill in the exit block.
|
|
|
|
Builder.SetInsertPoint(Exit);
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
}
|
2012-09-13 00:09:55 +00:00
|
|
|
|
2013-03-18 23:04:39 +00:00
|
|
|
Function *GCOVProfiler::
|
2012-09-13 00:09:55 +00:00
|
|
|
insertFlush(ArrayRef<std::pair<GlobalVariable*, MDNode*> > CountersBySP) {
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
|
2013-03-18 23:04:39 +00:00
|
|
|
Function *FlushF = M->getFunction("__llvm_gcov_flush");
|
2012-09-13 00:09:55 +00:00
|
|
|
if (!FlushF)
|
|
|
|
FlushF = Function::Create(FTy, GlobalValue::InternalLinkage,
|
2013-03-18 23:04:39 +00:00
|
|
|
"__llvm_gcov_flush", M);
|
2012-09-13 00:09:55 +00:00
|
|
|
else
|
|
|
|
FlushF->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
FlushF->setUnnamedAddr(true);
|
2012-12-19 07:18:57 +00:00
|
|
|
FlushF->addFnAttr(Attribute::NoInline);
|
2013-03-14 05:13:26 +00:00
|
|
|
if (Options.NoRedZone)
|
2012-12-19 07:18:57 +00:00
|
|
|
FlushF->addFnAttr(Attribute::NoRedZone);
|
2012-09-13 00:09:55 +00:00
|
|
|
|
|
|
|
BasicBlock *Entry = BasicBlock::Create(*Ctx, "entry", FlushF);
|
|
|
|
|
|
|
|
// Write out the current counters.
|
|
|
|
Constant *WriteoutF = M->getFunction("__llvm_gcov_writeout");
|
|
|
|
assert(WriteoutF && "Need to create the writeout function first!");
|
|
|
|
|
|
|
|
IRBuilder<> Builder(Entry);
|
|
|
|
Builder.CreateCall(WriteoutF);
|
|
|
|
|
2012-09-13 14:32:30 +00:00
|
|
|
// Zero out the counters.
|
|
|
|
for (ArrayRef<std::pair<GlobalVariable *, MDNode *> >::iterator
|
|
|
|
I = CountersBySP.begin(), E = CountersBySP.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
GlobalVariable *GV = I->first;
|
|
|
|
Constant *Null = Constant::getNullValue(GV->getType()->getElementType());
|
2012-09-14 22:35:49 +00:00
|
|
|
Builder.CreateStore(Null, GV);
|
2012-09-13 14:32:30 +00:00
|
|
|
}
|
2012-09-13 00:09:55 +00:00
|
|
|
|
|
|
|
Type *RetTy = FlushF->getReturnType();
|
|
|
|
if (RetTy == Type::getVoidTy(*Ctx))
|
|
|
|
Builder.CreateRetVoid();
|
|
|
|
else if (RetTy->isIntegerTy())
|
2013-03-18 23:04:39 +00:00
|
|
|
// Used if __llvm_gcov_flush was implicitly declared.
|
2012-09-13 00:09:55 +00:00
|
|
|
Builder.CreateRet(ConstantInt::get(RetTy, 0));
|
|
|
|
else
|
2013-03-18 23:04:39 +00:00
|
|
|
report_fatal_error("invalid return type for __llvm_gcov_flush");
|
|
|
|
|
|
|
|
return FlushF;
|
2012-09-13 00:09:55 +00:00
|
|
|
}
|