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 "ProfilingUtils.h"
|
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
|
|
|
#include "llvm/Analysis/DebugInfo.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/DebugLoc.h"
|
|
|
|
#include "llvm/Support/InstIterator.h"
|
|
|
|
#include "llvm/Support/IRBuilder.h"
|
|
|
|
#include "llvm/Support/PathV2.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/UniqueVector.h"
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class GCOVProfiler : public ModulePass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
2011-04-21 01:56:25 +00:00
|
|
|
GCOVProfiler()
|
2011-05-17 23:05:13 +00:00
|
|
|
: ModulePass(ID), EmitNotes(true), EmitData(true), Use402Format(false) {
|
2011-04-21 01:56:25 +00:00
|
|
|
initializeGCOVProfilerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2011-05-17 23:05:13 +00:00
|
|
|
GCOVProfiler(bool EmitNotes, bool EmitData, bool use402Format = false)
|
|
|
|
: ModulePass(ID), EmitNotes(EmitNotes), EmitData(EmitData),
|
|
|
|
Use402Format(use402Format) {
|
2011-04-21 01:56:25 +00:00
|
|
|
assert((EmitNotes || EmitData) && "GCOVProfiler asked to do nothing?");
|
2011-04-16 01:20:23 +00:00
|
|
|
initializeGCOVProfilerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "GCOV Profiler";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-05-04 04:03:04 +00:00
|
|
|
bool runOnModule(Module &M);
|
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
// Create the GCNO files for the Module based on DebugInfo.
|
2011-04-26 03:54:16 +00:00
|
|
|
void emitGCNO(DebugInfoFinder &DIF);
|
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-04-26 03:54:16 +00:00
|
|
|
bool emitProfileArcs(DebugInfoFinder &DIF);
|
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();
|
2011-04-26 03:54:16 +00:00
|
|
|
Constant *getIncrementIndirectCounterFunc();
|
2011-04-16 01:20:23 +00:00
|
|
|
Constant *getEmitFunctionFunc();
|
|
|
|
Constant *getEmitArcsFunc();
|
|
|
|
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,
|
|
|
|
const UniqueVector<BasicBlock *> &Preds,
|
|
|
|
const UniqueVector<BasicBlock *> &Succs);
|
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
// Add the function to write out all our counters to the global destructor
|
|
|
|
// list.
|
2011-04-26 03:54:16 +00:00
|
|
|
void insertCounterWriteout(DebugInfoFinder &,
|
2011-04-16 01:20:23 +00:00
|
|
|
SmallVector<std::pair<GlobalVariable *,
|
2011-05-05 02:46:38 +00:00
|
|
|
MDNode *>, 8> &);
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2011-05-04 04:03:04 +00:00
|
|
|
std::string mangleName(DICompileUnit CU, std::string NewStem);
|
|
|
|
|
2011-04-21 01:56:25 +00:00
|
|
|
bool EmitNotes;
|
|
|
|
bool EmitData;
|
2011-05-17 23:05:13 +00:00
|
|
|
bool Use402Format;
|
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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char GCOVProfiler::ID = 0;
|
|
|
|
INITIALIZE_PASS(GCOVProfiler, "insert-gcov-profiling",
|
|
|
|
"Insert instrumentation for GCOV profiling", false, false)
|
|
|
|
|
2011-05-17 23:05:13 +00:00
|
|
|
ModulePass *llvm::createGCOVProfilerPass(bool EmitNotes, bool EmitData,
|
|
|
|
bool Use402Format) {
|
|
|
|
return new GCOVProfiler(EmitNotes, EmitData, Use402Format);
|
2011-04-21 01:56:25 +00:00
|
|
|
}
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
static DISubprogram findSubprogram(DIScope Scope) {
|
|
|
|
while (!Scope.isSubprogram()) {
|
|
|
|
assert(Scope.isLexicalBlock() &&
|
2011-04-16 01:20:23 +00:00
|
|
|
"Debug location not lexical block or subprogram");
|
2011-04-26 03:54:16 +00:00
|
|
|
Scope = DILexicalBlock(Scope).getContext();
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
2011-04-26 03:54:16 +00:00
|
|
|
return DISubprogram(Scope);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class GCOVRecord {
|
|
|
|
protected:
|
2011-04-26 03:54:16 +00:00
|
|
|
static const char *LinesTag;
|
|
|
|
static const char *FunctionTag;
|
|
|
|
static const char *BlockTag;
|
|
|
|
static const char *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
|
2011-04-26 03:54:16 +00:00
|
|
|
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;
|
|
|
|
};
|
2011-04-26 03:54:16 +00:00
|
|
|
const char *GCOVRecord::LinesTag = "\0\0\x45\x01";
|
|
|
|
const char *GCOVRecord::FunctionTag = "\0\0\0\1";
|
|
|
|
const char *GCOVRecord::BlockTag = "\0\0\x41\x01";
|
|
|
|
const char *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
|
|
|
|
// list of line numbers and a single filename, representing lines that belong
|
|
|
|
// to the block.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
uint32_t length() {
|
|
|
|
return lengthOfGCOVString(Filename) + 2 + Lines.size();
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class GCOVBlock;
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
GCOVLines(std::string Filename, raw_ostream *os)
|
|
|
|
: Filename(Filename) {
|
2011-04-16 01:20:23 +00:00
|
|
|
this->os = os;
|
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
std::string Filename;
|
|
|
|
SmallVector<uint32_t, 32> Lines;
|
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-04-26 03:54:16 +00:00
|
|
|
GCOVLines &getFile(std::string Filename) {
|
|
|
|
GCOVLines *&Lines = LinesByFile[Filename];
|
|
|
|
if (!Lines) {
|
|
|
|
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;
|
|
|
|
for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(),
|
|
|
|
E = LinesByFile.end(); I != E; ++I) {
|
|
|
|
Len += I->second->length();
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
writeBytes(LinesTag, 4);
|
|
|
|
write(Len);
|
|
|
|
write(Number);
|
|
|
|
for (StringMap<GCOVLines *>::iterator I = LinesByFile.begin(),
|
|
|
|
E = LinesByFile.end(); I != E; ++I) {
|
|
|
|
write(0);
|
|
|
|
writeGCOVString(I->second->Filename);
|
|
|
|
for (int i = 0, e = I->second->Lines.size(); i != e; ++i) {
|
|
|
|
write(I->second->Lines[i]);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
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:
|
2011-05-17 23:05:13 +00:00
|
|
|
GCOVFunction(DISubprogram SP, raw_ostream *os, bool Use402Format) {
|
2011-04-16 01:20:23 +00:00
|
|
|
this->os = os;
|
|
|
|
|
|
|
|
Function *F = SP.getFunction();
|
|
|
|
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);
|
|
|
|
|
|
|
|
writeBytes(FunctionTag, 4);
|
2011-05-17 23:05:13 +00:00
|
|
|
uint32_t BlockLen = 1 + 1 + 1 + lengthOfGCOVString(SP.getName()) +
|
2011-04-26 03:54:16 +00:00
|
|
|
1 + lengthOfGCOVString(SP.getFilename()) + 1;
|
2011-05-17 23:05:13 +00:00
|
|
|
if (!Use402Format)
|
|
|
|
++BlockLen; // For second checksum.
|
2011-04-26 03:54:16 +00:00
|
|
|
write(BlockLen);
|
|
|
|
uint32_t Ident = reinterpret_cast<intptr_t>((MDNode*)SP);
|
|
|
|
write(Ident);
|
2011-05-05 02:46:38 +00:00
|
|
|
write(0); // checksum #1
|
2011-05-17 23:05:13 +00:00
|
|
|
if (!Use402Format)
|
|
|
|
write(0); // checksum #2
|
2011-04-26 03:54:16 +00:00
|
|
|
writeGCOVString(SP.getName());
|
|
|
|
writeGCOVString(SP.getFilename());
|
|
|
|
write(SP.getLineNumber());
|
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
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
void writeOut() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Emit edges between blocks.
|
2011-04-26 03:54:16 +00:00
|
|
|
for (DenseMap<BasicBlock *, GCOVBlock *>::iterator I = Blocks.begin(),
|
|
|
|
E = Blocks.end(); I != E; ++I) {
|
|
|
|
GCOVBlock &Block = *I->second;
|
|
|
|
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) {
|
|
|
|
write(Block.OutEdges[i]->Number);
|
|
|
|
write(0); // no flags
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit lines for each block.
|
2011-04-26 03:54:16 +00:00
|
|
|
for (DenseMap<BasicBlock *, GCOVBlock *>::iterator I = Blocks.begin(),
|
|
|
|
E = Blocks.end(); I != E; ++I) {
|
|
|
|
I->second->writeOut();
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-04-26 03:54:16 +00:00
|
|
|
DenseMap<BasicBlock *, GCOVBlock *> Blocks;
|
|
|
|
GCOVBlock *ReturnBlock;
|
2011-04-16 01:20:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-05-04 04:03:04 +00:00
|
|
|
std::string GCOVProfiler::mangleName(DICompileUnit CU, std::string NewStem) {
|
|
|
|
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) {
|
|
|
|
SmallString<128> Filename = GCovFile->getString();
|
|
|
|
sys::path::replace_extension(Filename, NewStem);
|
|
|
|
return Filename.str();
|
|
|
|
}
|
2011-05-04 04:03:04 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-05 00:03:30 +00:00
|
|
|
|
|
|
|
SmallString<128> Filename = CU.getFilename();
|
|
|
|
sys::path::replace_extension(Filename, NewStem);
|
|
|
|
return sys::path::filename(Filename.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();
|
|
|
|
|
|
|
|
DebugInfoFinder DIF;
|
2011-04-26 03:54:16 +00:00
|
|
|
DIF.processModule(M);
|
2011-04-16 02:05:18 +00:00
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
if (EmitNotes) emitGCNO(DIF);
|
|
|
|
if (EmitData) return emitProfileArcs(DIF);
|
2011-04-21 01:56:25 +00:00
|
|
|
return false;
|
2011-04-16 02:05:18 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
void GCOVProfiler::emitGCNO(DebugInfoFinder &DIF) {
|
|
|
|
DenseMap<const MDNode *, raw_fd_ostream *> GcnoFiles;
|
2011-04-16 01:20:23 +00:00
|
|
|
for (DebugInfoFinder::iterator I = DIF.compile_unit_begin(),
|
|
|
|
E = DIF.compile_unit_end(); 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(*I);
|
2011-04-26 03:54:16 +00:00
|
|
|
raw_fd_ostream *&out = GcnoFiles[CU];
|
2011-04-16 01:20:23 +00:00
|
|
|
std::string ErrorInfo;
|
2011-05-04 04:03:04 +00:00
|
|
|
out = new raw_fd_ostream(mangleName(CU, "gcno").c_str(), ErrorInfo,
|
|
|
|
raw_fd_ostream::F_Binary);
|
2011-05-17 23:05:13 +00:00
|
|
|
if (!Use402Format)
|
|
|
|
out->write("oncg*404MVLL", 12);
|
|
|
|
else
|
|
|
|
out->write("oncg*402MVLL", 12);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (DebugInfoFinder::iterator SPI = DIF.subprogram_begin(),
|
|
|
|
SPE = DIF.subprogram_end(); SPI != SPE; ++SPI) {
|
|
|
|
DISubprogram SP(*SPI);
|
2011-04-26 03:54:16 +00:00
|
|
|
raw_fd_ostream *&os = GcnoFiles[SP.getCompileUnit()];
|
2011-04-16 01:20:23 +00:00
|
|
|
|
|
|
|
Function *F = SP.getFunction();
|
2011-04-28 21:35:49 +00:00
|
|
|
if (!F) continue;
|
2011-05-17 23:05:13 +00:00
|
|
|
GCOVFunction Func(SP, os, Use402Format);
|
2011-04-28 21:35:49 +00:00
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
2011-04-26 03:54:16 +00:00
|
|
|
GCOVBlock &Block = Func.getBlock(BB);
|
2011-04-16 01:20:23 +00:00
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
|
|
|
if (int successors = TI->getNumSuccessors()) {
|
|
|
|
for (int i = 0; i != successors; ++i) {
|
2011-04-26 03:54:16 +00:00
|
|
|
Block.addEdge(Func.getBlock(TI->getSuccessor(i)));
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
2011-04-21 03:18:00 +00:00
|
|
|
} else if (isa<ReturnInst>(TI)) {
|
2011-04-26 03:54:16 +00:00
|
|
|
Block.addEdge(Func.getReturnBlock());
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
uint32_t Line = 0;
|
2011-04-16 01:20:23 +00:00
|
|
|
for (BasicBlock::iterator I = BB->begin(), IE = BB->end(); I != IE; ++I) {
|
2011-04-26 03:54:16 +00:00
|
|
|
const DebugLoc &Loc = I->getDebugLoc();
|
|
|
|
if (Loc.isUnknown()) continue;
|
|
|
|
if (Line == Loc.getLine()) continue;
|
|
|
|
Line = Loc.getLine();
|
|
|
|
if (SP != findSubprogram(DIScope(Loc.getScope(*Ctx)))) continue;
|
|
|
|
|
|
|
|
GCOVLines &Lines = Block.getFile(SP.getFilename());
|
|
|
|
Lines.addLine(Loc.getLine());
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
2011-04-26 03:54:16 +00:00
|
|
|
Func.writeOut();
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (DenseMap<const MDNode *, raw_fd_ostream *>::iterator
|
2011-04-26 03:54:16 +00:00
|
|
|
I = GcnoFiles.begin(), E = GcnoFiles.end(); I != E; ++I) {
|
|
|
|
raw_fd_ostream *&out = I->second;
|
|
|
|
out->write("\0\0\0\0\0\0\0\0", 8); // EOF
|
|
|
|
out->close();
|
|
|
|
delete out;
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
bool GCOVProfiler::emitProfileArcs(DebugInfoFinder &DIF) {
|
2011-04-16 02:05:18 +00:00
|
|
|
if (DIF.subprogram_begin() == DIF.subprogram_end())
|
|
|
|
return false;
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2011-05-05 02:46:38 +00:00
|
|
|
SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> CountersBySP;
|
2011-04-16 01:20:23 +00:00
|
|
|
for (DebugInfoFinder::iterator SPI = DIF.subprogram_begin(),
|
|
|
|
SPE = DIF.subprogram_end(); SPI != SPE; ++SPI) {
|
|
|
|
DISubprogram SP(*SPI);
|
|
|
|
Function *F = SP.getFunction();
|
2011-04-28 21:35:49 +00:00
|
|
|
if (!F) continue;
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
unsigned Edges = 0;
|
2011-04-16 01:20:23 +00:00
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
2011-04-21 03:18:00 +00:00
|
|
|
if (isa<ReturnInst>(TI))
|
2011-04-26 03:54:16 +00:00
|
|
|
++Edges;
|
2011-04-21 03:18:00 +00:00
|
|
|
else
|
2011-04-26 03:54:16 +00:00
|
|
|
Edges += TI->getNumSuccessors();
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
ArrayType *CounterTy =
|
2011-04-26 03:54:16 +00:00
|
|
|
ArrayType::get(Type::getInt64Ty(*Ctx), Edges);
|
|
|
|
GlobalVariable *Counters =
|
|
|
|
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),
|
2011-04-16 01:20:23 +00:00
|
|
|
"__llvm_gcov_ctr", 0, false, 0);
|
2011-05-05 02:46:38 +00:00
|
|
|
CountersBySP.push_back(std::make_pair(Counters, (MDNode*)SP));
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
UniqueVector<BasicBlock *> ComplexEdgePreds;
|
|
|
|
UniqueVector<BasicBlock *> ComplexEdgeSuccs;
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
unsigned Edge = 0;
|
2011-04-16 01:20:23 +00:00
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
|
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
2011-04-26 03:54:16 +00:00
|
|
|
int Successors = isa<ReturnInst>(TI) ? 1 : TI->getNumSuccessors();
|
|
|
|
if (Successors) {
|
|
|
|
IRBuilder<> Builder(TI);
|
|
|
|
|
|
|
|
if (Successors == 1) {
|
|
|
|
Value *Counter = Builder.CreateConstInBoundsGEP2_64(Counters, 0,
|
|
|
|
Edge);
|
|
|
|
Value *Count = Builder.CreateLoad(Counter);
|
|
|
|
Count = Builder.CreateAdd(Count,
|
2011-04-16 01:20:23 +00:00
|
|
|
ConstantInt::get(Type::getInt64Ty(*Ctx),1));
|
2011-04-26 03:54:16 +00:00
|
|
|
Builder.CreateStore(Count, Counter);
|
2011-04-16 01:20:23 +00:00
|
|
|
} else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
|
2011-04-26 03:54:16 +00:00
|
|
|
Value *Sel = Builder.CreateSelect(
|
2011-04-16 01:20:23 +00:00
|
|
|
BI->getCondition(),
|
2011-04-26 03:54:16 +00:00
|
|
|
ConstantInt::get(Type::getInt64Ty(*Ctx), Edge),
|
|
|
|
ConstantInt::get(Type::getInt64Ty(*Ctx), Edge + 1));
|
|
|
|
SmallVector<Value *, 2> Idx;
|
|
|
|
Idx.push_back(Constant::getNullValue(Type::getInt64Ty(*Ctx)));
|
|
|
|
Idx.push_back(Sel);
|
2011-07-22 08:16:57 +00:00
|
|
|
Value *Counter = Builder.CreateInBoundsGEP(Counters, Idx);
|
2011-04-26 03:54:16 +00:00
|
|
|
Value *Count = Builder.CreateLoad(Counter);
|
|
|
|
Count = Builder.CreateAdd(Count,
|
2011-04-16 01:20:23 +00:00
|
|
|
ConstantInt::get(Type::getInt64Ty(*Ctx),1));
|
2011-04-26 03:54:16 +00:00
|
|
|
Builder.CreateStore(Count, Counter);
|
2011-04-16 01:20:23 +00:00
|
|
|
} else {
|
2011-04-26 03:54:16 +00:00
|
|
|
ComplexEdgePreds.insert(BB);
|
|
|
|
for (int i = 0; i != Successors; ++i)
|
|
|
|
ComplexEdgeSuccs.insert(TI->getSuccessor(i));
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
2011-04-26 03:54:16 +00:00
|
|
|
Edge += Successors;
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
if (!ComplexEdgePreds.empty()) {
|
|
|
|
GlobalVariable *EdgeTable =
|
|
|
|
buildEdgeLookupTable(F, Counters,
|
|
|
|
ComplexEdgePreds, ComplexEdgeSuccs);
|
|
|
|
GlobalVariable *EdgeState = getEdgeStateValue();
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Int32Ty = Type::getInt32Ty(*Ctx);
|
2011-04-26 03:54:16 +00:00
|
|
|
for (int i = 0, e = ComplexEdgePreds.size(); i != e; ++i) {
|
|
|
|
IRBuilder<> Builder(ComplexEdgePreds[i+1]->getTerminator());
|
2011-04-28 21:35:49 +00:00
|
|
|
Builder.CreateStore(ConstantInt::get(Int32Ty, i), EdgeState);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
2011-04-26 03:54:16 +00:00
|
|
|
for (int i = 0, e = ComplexEdgeSuccs.size(); i != e; ++i) {
|
2011-04-16 01:20:23 +00:00
|
|
|
// call runtime to perform increment
|
2011-04-26 03:54:16 +00:00
|
|
|
IRBuilder<> Builder(ComplexEdgeSuccs[i+1]->getFirstNonPHI());
|
|
|
|
Value *CounterPtrArray =
|
|
|
|
Builder.CreateConstInBoundsGEP2_64(EdgeTable, 0,
|
|
|
|
i * ComplexEdgePreds.size());
|
|
|
|
Builder.CreateCall2(getIncrementIndirectCounterFunc(),
|
|
|
|
EdgeState, CounterPtrArray);
|
|
|
|
// clear the predecessor number
|
|
|
|
Builder.CreateStore(ConstantInt::get(Int32Ty, 0xffffffff), EdgeState);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-05 02:46:38 +00:00
|
|
|
insertCounterWriteout(DIF, CountersBySP);
|
2011-04-16 01:20:23 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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*]].
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Int64PtrTy = Type::getInt64PtrTy(*Ctx);
|
|
|
|
ArrayType *EdgeTableTy = ArrayType::get(
|
2011-04-26 03:54:16 +00:00
|
|
|
Int64PtrTy, Succs.size() * Preds.size());
|
|
|
|
|
|
|
|
Constant **EdgeTable = new Constant*[Succs.size() * Preds.size()];
|
|
|
|
Constant *NullValue = Constant::getNullValue(Int64PtrTy);
|
|
|
|
for (int i = 0, ie = Succs.size() * Preds.size(); i != ie; ++i)
|
|
|
|
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);
|
|
|
|
IRBuilder<> builder(Succ);
|
|
|
|
Value *Counter = builder.CreateConstInBoundsGEP2_64(Counters, 0,
|
|
|
|
Edge + i);
|
|
|
|
EdgeTable[((Succs.idFor(Succ)-1) * Preds.size()) +
|
|
|
|
(Preds.idFor(BB)-1)] = cast<Constant>(Counter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Edge += Successors;
|
|
|
|
}
|
|
|
|
|
2011-06-22 09:24:39 +00:00
|
|
|
ArrayRef<Constant*> V(&EdgeTable[0], Succs.size() * Preds.size());
|
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() {
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
|
2011-07-12 14:06:48 +00:00
|
|
|
Type::getInt8PtrTy(*Ctx), false);
|
2011-04-26 03:54:16 +00:00
|
|
|
return M->getOrInsertFunction("llvm_gcda_start_file", FTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant *GCOVProfiler::getIncrementIndirectCounterFunc() {
|
2011-07-12 14:06:48 +00:00
|
|
|
Type *Args[] = {
|
2011-04-26 03:54:16 +00:00
|
|
|
Type::getInt32PtrTy(*Ctx), // uint32_t *predecessor
|
|
|
|
Type::getInt64PtrTy(*Ctx)->getPointerTo(), // uint64_t **state_table_row
|
|
|
|
};
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
|
2011-04-26 03:54:16 +00:00
|
|
|
Args, false);
|
|
|
|
return M->getOrInsertFunction("llvm_gcda_increment_indirect_counter", FTy);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *GCOVProfiler::getEmitFunctionFunc() {
|
2011-07-12 14:06:48 +00:00
|
|
|
Type *Args[2] = {
|
2011-05-05 02:46:38 +00:00
|
|
|
Type::getInt32Ty(*Ctx), // uint32_t ident
|
|
|
|
Type::getInt8PtrTy(*Ctx), // const char *function_name
|
|
|
|
};
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
|
2011-04-16 01:20:23 +00:00
|
|
|
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
|
|
|
|
};
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Ctx),
|
2011-04-16 01:20:23 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
void GCOVProfiler::insertCounterWriteout(
|
|
|
|
DebugInfoFinder &DIF,
|
2011-05-05 02:46:38 +00:00
|
|
|
SmallVector<std::pair<GlobalVariable *, MDNode *>, 8> &CountersBySP) {
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *WriteoutFTy =
|
2011-04-16 01:20:23 +00:00
|
|
|
FunctionType::get(Type::getVoidTy(*Ctx), false);
|
|
|
|
Function *WriteoutF = Function::Create(WriteoutFTy,
|
|
|
|
GlobalValue::InternalLinkage,
|
2011-04-26 03:54:16 +00:00
|
|
|
"__llvm_gcov_writeout", M);
|
2011-04-16 01:20:23 +00:00
|
|
|
WriteoutF->setUnnamedAddr(true);
|
|
|
|
BasicBlock *BB = BasicBlock::Create(*Ctx, "", 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();
|
|
|
|
Constant *EndFile = getEndFileFunc();
|
|
|
|
|
|
|
|
for (DebugInfoFinder::iterator CUI = DIF.compile_unit_begin(),
|
|
|
|
CUE = DIF.compile_unit_end(); CUI != CUE; ++CUI) {
|
|
|
|
DICompileUnit compile_unit(*CUI);
|
2011-05-04 04:03:04 +00:00
|
|
|
std::string FilenameGcda = mangleName(compile_unit, "gcda");
|
2011-04-26 03:54:16 +00:00
|
|
|
Builder.CreateCall(StartFile,
|
|
|
|
Builder.CreateGlobalStringPtr(FilenameGcda));
|
2011-05-05 02:46:38 +00:00
|
|
|
for (SmallVector<std::pair<GlobalVariable *, MDNode *>, 8>::iterator
|
|
|
|
I = CountersBySP.begin(), E = CountersBySP.end();
|
2011-04-16 01:20:23 +00:00
|
|
|
I != E; ++I) {
|
2011-05-05 02:46:38 +00:00
|
|
|
DISubprogram SP(I->second);
|
|
|
|
intptr_t ident = reinterpret_cast<intptr_t>(I->second);
|
|
|
|
Builder.CreateCall2(EmitFunction,
|
|
|
|
ConstantInt::get(Type::getInt32Ty(*Ctx), ident),
|
|
|
|
Builder.CreateGlobalStringPtr(SP.getName()));
|
|
|
|
|
2011-04-16 01:20:23 +00:00
|
|
|
GlobalVariable *GV = I->first;
|
2011-04-26 03:54:16 +00:00
|
|
|
unsigned Arcs =
|
2011-04-16 01:20:23 +00:00
|
|
|
cast<ArrayType>(GV->getType()->getElementType())->getNumElements();
|
2011-04-26 03:54:16 +00:00
|
|
|
Builder.CreateCall2(EmitArcs,
|
|
|
|
ConstantInt::get(Type::getInt32Ty(*Ctx), Arcs),
|
|
|
|
Builder.CreateConstGEP2_64(GV, 0, 0));
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
2011-04-26 03:54:16 +00:00
|
|
|
Builder.CreateCall(EndFile);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|
2011-04-26 03:54:16 +00:00
|
|
|
Builder.CreateRetVoid();
|
2011-04-16 01:20:23 +00:00
|
|
|
|
2011-04-26 03:54:16 +00:00
|
|
|
InsertProfilingShutdownCall(WriteoutF, M);
|
2011-04-16 01:20:23 +00:00
|
|
|
}
|