2004-02-11 06:11:42 +08:00
|
|
|
//===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
|
2005-04-22 05:13:18 +08:00
|
|
|
//
|
2004-02-11 06:11:42 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 05:13:18 +08:00
|
|
|
//
|
2004-02-11 06:11:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the abstract ProfileInfo interface, and the default
|
|
|
|
// "no profile" implementation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-01-09 06:01:16 +08:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2004-02-11 06:11:42 +08:00
|
|
|
#include "llvm/Analysis/ProfileInfo.h"
|
|
|
|
#include "llvm/Pass.h"
|
2004-03-09 06:04:08 +08:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-02-06 07:42:17 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-09-10 01:52:57 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-08-26 23:56:38 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-09-10 01:52:57 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2004-03-09 06:04:08 +08:00
|
|
|
#include <set>
|
2004-02-11 06:11:42 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2004-02-11 14:10:18 +08:00
|
|
|
// Register the ProfileInfo interface, providing a nice name to refer to.
|
2008-05-13 08:00:25 +08:00
|
|
|
static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
|
2007-05-03 09:11:54 +08:00
|
|
|
char ProfileInfo::ID = 0;
|
2004-02-11 06:11:42 +08:00
|
|
|
|
|
|
|
ProfileInfo::~ProfileInfo() {}
|
|
|
|
|
2009-08-25 05:37:48 +08:00
|
|
|
const double ProfileInfo::MissingValue = -1;
|
|
|
|
|
2009-08-09 01:43:09 +08:00
|
|
|
double ProfileInfo::getExecutionCount(const BasicBlock *BB) {
|
|
|
|
std::map<const Function*, BlockCounts>::iterator J =
|
|
|
|
BlockInformation.find(BB->getParent());
|
|
|
|
if (J != BlockInformation.end()) {
|
|
|
|
BlockCounts::iterator I = J->second.find(BB);
|
|
|
|
if (I != J->second.end())
|
|
|
|
return I->second;
|
|
|
|
}
|
2009-08-06 05:51:16 +08:00
|
|
|
|
2009-07-14 14:58:59 +08:00
|
|
|
pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
|
2004-03-09 06:04:08 +08:00
|
|
|
|
|
|
|
// Are there zero predecessors of this block?
|
|
|
|
if (PI == PE) {
|
|
|
|
// If this is the entry block, look for the Null -> Entry edge.
|
2007-03-23 00:38:57 +08:00
|
|
|
if (BB == &BB->getParent()->getEntryBlock())
|
2009-08-09 01:43:09 +08:00
|
|
|
return getEdgeWeight(getEdge(0, BB));
|
2004-03-09 06:04:08 +08:00
|
|
|
else
|
|
|
|
return 0; // Otherwise, this is a dead block.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if there are predecessors, the execution count of this block is
|
2009-08-09 01:43:09 +08:00
|
|
|
// the sum of the edge frequencies from the incoming edges.
|
2009-07-14 14:58:59 +08:00
|
|
|
std::set<const BasicBlock*> ProcessedPreds;
|
2009-08-09 01:43:09 +08:00
|
|
|
double Count = 0;
|
2004-03-09 06:04:08 +08:00
|
|
|
for (; PI != PE; ++PI)
|
2009-08-09 01:43:09 +08:00
|
|
|
if (ProcessedPreds.insert(*PI).second) {
|
|
|
|
double w = getEdgeWeight(getEdge(*PI, BB));
|
|
|
|
if (w == MissingValue) {
|
2009-08-19 13:44:39 +08:00
|
|
|
Count = MissingValue;
|
|
|
|
break;
|
2009-08-09 01:43:09 +08:00
|
|
|
}
|
|
|
|
Count += w;
|
|
|
|
}
|
|
|
|
|
2009-08-25 05:37:48 +08:00
|
|
|
if (Count != MissingValue) BlockInformation[BB->getParent()][BB] = Count;
|
2004-03-09 06:04:08 +08:00
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
2009-08-09 01:43:09 +08:00
|
|
|
double ProfileInfo::getExecutionCount(const Function *F) {
|
|
|
|
std::map<const Function*, double>::iterator J =
|
|
|
|
FunctionInformation.find(F);
|
|
|
|
if (J != FunctionInformation.end())
|
|
|
|
return J->second;
|
2009-08-06 05:51:16 +08:00
|
|
|
|
2009-08-26 21:33:09 +08:00
|
|
|
// isDeclaration() is checked here and not at start of function to allow
|
|
|
|
// functions without a body still to have a execution count.
|
|
|
|
if (F->isDeclaration()) return MissingValue;
|
|
|
|
|
2009-08-09 01:43:09 +08:00
|
|
|
double Count = getExecutionCount(&F->getEntryBlock());
|
2009-08-25 05:37:48 +08:00
|
|
|
if (Count != MissingValue) FunctionInformation[F] = Count;
|
2009-08-09 01:43:09 +08:00
|
|
|
return Count;
|
2009-07-14 14:58:59 +08:00
|
|
|
}
|
2004-03-09 06:04:08 +08:00
|
|
|
|
2009-09-10 01:52:57 +08:00
|
|
|
/// Replaces all occurences of RmBB in the ProfilingInfo with DestBB.
|
|
|
|
/// This checks all edges of the function the blocks reside in and replaces the
|
|
|
|
/// occurences of RmBB with DestBB.
|
|
|
|
void ProfileInfo::replaceAllUses(const BasicBlock *RmBB,
|
|
|
|
const BasicBlock *DestBB) {
|
|
|
|
DEBUG(errs() << "Replacing " << RmBB->getNameStr()
|
|
|
|
<< " with " << DestBB->getNameStr() << "\n");
|
|
|
|
const Function *F = DestBB->getParent();
|
|
|
|
std::map<const Function*, EdgeWeights>::iterator J =
|
|
|
|
EdgeInformation.find(F);
|
|
|
|
if (J == EdgeInformation.end()) return;
|
|
|
|
|
|
|
|
for (EdgeWeights::iterator I = J->second.begin(), E = J->second.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
Edge e = I->first;
|
|
|
|
Edge newedge; bool foundedge = false;
|
|
|
|
if (e.first == RmBB) {
|
|
|
|
newedge = getEdge(DestBB, e.second);
|
|
|
|
foundedge = true;
|
|
|
|
}
|
|
|
|
if (e.second == RmBB) {
|
|
|
|
newedge = getEdge(e.first, DestBB);
|
|
|
|
foundedge = true;
|
|
|
|
}
|
|
|
|
if (foundedge) {
|
|
|
|
double w = getEdgeWeight(e);
|
|
|
|
EdgeInformation[F][newedge] = w;
|
|
|
|
DEBUG(errs() << "Replacing " << e << " with " << newedge << "\n");
|
|
|
|
J->second.erase(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Splits an edge in the ProfileInfo and redirects flow over NewBB.
|
|
|
|
/// Since its possible that there is more than one edge in the CFG from FristBB
|
|
|
|
/// to SecondBB its necessary to redirect the flow proporionally.
|
|
|
|
void ProfileInfo::splitEdge(const BasicBlock *FirstBB,
|
|
|
|
const BasicBlock *SecondBB,
|
|
|
|
const BasicBlock *NewBB,
|
|
|
|
bool MergeIdenticalEdges) {
|
|
|
|
const Function *F = FirstBB->getParent();
|
|
|
|
std::map<const Function*, EdgeWeights>::iterator J =
|
|
|
|
EdgeInformation.find(F);
|
|
|
|
if (J == EdgeInformation.end()) return;
|
|
|
|
|
|
|
|
// Generate edges and read current weight.
|
|
|
|
Edge e = getEdge(FirstBB, SecondBB);
|
|
|
|
Edge n1 = getEdge(FirstBB, NewBB);
|
|
|
|
Edge n2 = getEdge(NewBB, SecondBB);
|
|
|
|
EdgeWeights &ECs = J->second;
|
|
|
|
double w = ECs[e];
|
|
|
|
|
|
|
|
int succ_count = 0;
|
|
|
|
if (!MergeIdenticalEdges) {
|
|
|
|
// First count the edges from FristBB to SecondBB, if there is more than
|
|
|
|
// one, only slice out a proporional part for NewBB.
|
|
|
|
for(succ_const_iterator BBI = succ_begin(FirstBB), BBE = succ_end(FirstBB);
|
|
|
|
BBI != BBE; ++BBI) {
|
|
|
|
if (*BBI == SecondBB) succ_count++;
|
|
|
|
}
|
|
|
|
// When the NewBB is completely new, increment the count by one so that
|
|
|
|
// the counts are properly distributed.
|
|
|
|
if (getExecutionCount(NewBB) == ProfileInfo::MissingValue) succ_count++;
|
|
|
|
} else {
|
|
|
|
// When the edges are merged anyway, then redirect all flow.
|
|
|
|
succ_count = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We know now how many edges there are from FirstBB to SecondBB, reroute a
|
|
|
|
// proportional part of the edge weight over NewBB.
|
|
|
|
double neww = w / succ_count;
|
|
|
|
ECs[n1] += neww;
|
|
|
|
ECs[n2] += neww;
|
|
|
|
BlockInformation[F][NewBB] += neww;
|
|
|
|
if (succ_count == 1) {
|
|
|
|
ECs.erase(e);
|
|
|
|
} else {
|
|
|
|
ECs[e] -= neww;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-26 23:56:38 +08:00
|
|
|
raw_ostream& llvm::operator<<(raw_ostream &O, ProfileInfo::Edge E) {
|
|
|
|
O << "(";
|
|
|
|
O << (E.first ? E.first->getNameStr() : "0");
|
|
|
|
O << ",";
|
|
|
|
O << (E.second ? E.second->getNameStr() : "0");
|
|
|
|
return O << ")";
|
|
|
|
}
|
2004-02-11 06:11:42 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NoProfile ProfileInfo implementation
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace {
|
2007-02-06 07:42:17 +08:00
|
|
|
struct VISIBILITY_HIDDEN NoProfileInfo
|
2007-05-02 05:15:47 +08:00
|
|
|
: public ImmutablePass, public ProfileInfo {
|
2007-05-03 09:11:54 +08:00
|
|
|
static char ID; // Class identification, replacement for typeinfo
|
2008-09-05 01:05:41 +08:00
|
|
|
NoProfileInfo() : ImmutablePass(&ID) {}
|
2007-05-02 05:15:47 +08:00
|
|
|
};
|
2008-05-13 08:00:25 +08:00
|
|
|
} // End of anonymous namespace
|
2005-04-22 05:13:18 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char NoProfileInfo::ID = 0;
|
|
|
|
// Register this pass...
|
|
|
|
static RegisterPass<NoProfileInfo>
|
|
|
|
X("no-profile", "No Profile Information", false, true);
|
2004-02-11 06:11:42 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
// Declare that we implement the ProfileInfo interface
|
|
|
|
static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
|
2005-01-09 06:01:16 +08:00
|
|
|
|
|
|
|
ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }
|