//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "profile-info"
-#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/ProfileInfo.h"
+#include "llvm/ADT/SmallSet.h"
+#include "llvm/Analysis/Passes.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/Pass.h"
#include "llvm/Support/CFG.h"
-#include "llvm/ADT/SmallSet.h"
-#include <set>
-#include <queue>
#include <limits>
+#include <queue>
+#include <set>
using namespace llvm;
+namespace llvm {
+ template<> char ProfileInfoT<Function,BasicBlock>::ID = 0;
+}
+
// Register the ProfileInfo interface, providing a nice name to refer to.
-static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
+INITIALIZE_ANALYSIS_GROUP(ProfileInfo, "Profile Information", NoProfileInfo)
namespace llvm {
}
template<>
-char ProfileInfo::ID = 0;
-
-template<>
-char MachineProfileInfo::ID = 0;
+char ProfileInfoT<MachineFunction, MachineBasicBlock>::ID = 0;
template<>
-const double ProfileInfo::MissingValue = -1;
+const double ProfileInfoT<Function,BasicBlock>::MissingValue = -1;
-template<>
-const double MachineProfileInfo::MissingValue = -1;
+template<> const
+double ProfileInfoT<MachineFunction, MachineBasicBlock>::MissingValue = -1;
-template<>
-double ProfileInfo::getExecutionCount(const BasicBlock *BB) {
+template<> double
+ProfileInfoT<Function,BasicBlock>::getExecutionCount(const BasicBlock *BB) {
std::map<const Function*, BlockCounts>::iterator J =
BlockInformation.find(BB->getParent());
if (J != BlockInformation.end()) {
double Count = MissingValue;
- pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
+ const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
// Are there zero predecessors of this block?
if (PI == PE) {
- Edge e = getEdge(0,BB);
+ Edge e = getEdge(0, BB);
Count = getEdgeWeight(e);
} else {
// Otherwise, if there are predecessors, the execution count of this block is
// the sum of the edge frequencies from the incoming edges.
std::set<const BasicBlock*> ProcessedPreds;
Count = 0;
- for (; PI != PE; ++PI)
- if (ProcessedPreds.insert(*PI).second) {
- double w = getEdgeWeight(getEdge(*PI, BB));
+ for (; PI != PE; ++PI) {
+ const BasicBlock *P = *PI;
+ if (ProcessedPreds.insert(P).second) {
+ double w = getEdgeWeight(getEdge(P, BB));
if (w == MissingValue) {
Count = MissingValue;
break;
}
Count += w;
}
+ }
}
// If the predecessors did not suffice to get block weight, try successors.
}
template<>
-double MachineProfileInfo::getExecutionCount(const MachineBasicBlock *MBB) {
+double ProfileInfoT<MachineFunction, MachineBasicBlock>::
+ getExecutionCount(const MachineBasicBlock *MBB) {
std::map<const MachineFunction*, BlockCounts>::iterator J =
BlockInformation.find(MBB->getParent());
if (J != BlockInformation.end()) {
}
template<>
-double ProfileInfo::getExecutionCount(const Function *F) {
+double ProfileInfoT<Function,BasicBlock>::getExecutionCount(const Function *F) {
std::map<const Function*, double>::iterator J =
FunctionInformation.find(F);
if (J != FunctionInformation.end())
}
template<>
-double MachineProfileInfo::getExecutionCount(const MachineFunction *MF) {
+double ProfileInfoT<MachineFunction, MachineBasicBlock>::
+ getExecutionCount(const MachineFunction *MF) {
std::map<const MachineFunction*, double>::iterator J =
FunctionInformation.find(MF);
if (J != FunctionInformation.end())
}
template<>
-void ProfileInfo::setExecutionCount(const BasicBlock *BB, double w) {
- DEBUG(errs() << "Creating Block " << BB->getName()
+void ProfileInfoT<Function,BasicBlock>::
+ setExecutionCount(const BasicBlock *BB, double w) {
+ DEBUG(dbgs() << "Creating Block " << BB->getName()
<< " (weight: " << format("%.20g",w) << ")\n");
BlockInformation[BB->getParent()][BB] = w;
}
template<>
-void MachineProfileInfo::setExecutionCount(const MachineBasicBlock *MBB, double w) {
- DEBUG(errs() << "Creating Block " << MBB->getBasicBlock()->getName()
+void ProfileInfoT<MachineFunction, MachineBasicBlock>::
+ setExecutionCount(const MachineBasicBlock *MBB, double w) {
+ DEBUG(dbgs() << "Creating Block " << MBB->getBasicBlock()->getName()
<< " (weight: " << format("%.20g",w) << ")\n");
BlockInformation[MBB->getParent()][MBB] = w;
}
template<>
-void ProfileInfo::addEdgeWeight(Edge e, double w) {
+void ProfileInfoT<Function,BasicBlock>::addEdgeWeight(Edge e, double w) {
double oldw = getEdgeWeight(e);
assert (oldw != MissingValue && "Adding weight to Edge with no previous weight");
- DEBUG(errs() << "Adding to Edge " << e
+ DEBUG(dbgs() << "Adding to Edge " << e
<< " (new weight: " << format("%.20g",oldw + w) << ")\n");
EdgeInformation[getFunction(e)][e] = oldw + w;
}
template<>
-void ProfileInfo::addExecutionCount(const BasicBlock *BB, double w) {
+void ProfileInfoT<Function,BasicBlock>::
+ addExecutionCount(const BasicBlock *BB, double w) {
double oldw = getExecutionCount(BB);
assert (oldw != MissingValue && "Adding weight to Block with no previous weight");
- DEBUG(errs() << "Adding to Block " << BB->getName()
+ DEBUG(dbgs() << "Adding to Block " << BB->getName()
<< " (new weight: " << format("%.20g",oldw + w) << ")\n");
BlockInformation[BB->getParent()][BB] = oldw + w;
}
template<>
-void ProfileInfo::removeBlock(const BasicBlock *BB) {
+void ProfileInfoT<Function,BasicBlock>::removeBlock(const BasicBlock *BB) {
std::map<const Function*, BlockCounts>::iterator J =
BlockInformation.find(BB->getParent());
if (J == BlockInformation.end()) return;
- DEBUG(errs() << "Deleting " << BB->getName() << "\n");
+ DEBUG(dbgs() << "Deleting " << BB->getName() << "\n");
J->second.erase(BB);
}
template<>
-void ProfileInfo::removeEdge(Edge e) {
+void ProfileInfoT<Function,BasicBlock>::removeEdge(Edge e) {
std::map<const Function*, EdgeWeights>::iterator J =
EdgeInformation.find(getFunction(e));
if (J == EdgeInformation.end()) return;
- DEBUG(errs() << "Deleting" << e << "\n");
+ DEBUG(dbgs() << "Deleting" << e << "\n");
J->second.erase(e);
}
template<>
-void ProfileInfo::replaceEdge(const Edge &oldedge, const Edge &newedge) {
+void ProfileInfoT<Function,BasicBlock>::
+ replaceEdge(const Edge &oldedge, const Edge &newedge) {
double w;
if ((w = getEdgeWeight(newedge)) == MissingValue) {
w = getEdgeWeight(oldedge);
- DEBUG(errs() << "Replacing " << oldedge << " with " << newedge << "\n");
+ DEBUG(dbgs() << "Replacing " << oldedge << " with " << newedge << "\n");
} else {
w += getEdgeWeight(oldedge);
- DEBUG(errs() << "Adding " << oldedge << " to " << newedge << "\n");
+ DEBUG(dbgs() << "Adding " << oldedge << " to " << newedge << "\n");
}
setEdgeWeight(newedge,w);
removeEdge(oldedge);
}
template<>
-const BasicBlock *ProfileInfo::GetPath(const BasicBlock *Src, const BasicBlock *Dest,
- Path &P, unsigned Mode) {
+const BasicBlock *ProfileInfoT<Function,BasicBlock>::
+ GetPath(const BasicBlock *Src, const BasicBlock *Dest,
+ Path &P, unsigned Mode) {
const BasicBlock *BB = 0;
bool hasFoundPath = false;
}
template<>
-void ProfileInfo::divertFlow(const Edge &oldedge, const Edge &newedge) {
- DEBUG(errs() << "Diverting " << oldedge << " via " << newedge );
+void ProfileInfoT<Function,BasicBlock>::
+ divertFlow(const Edge &oldedge, const Edge &newedge) {
+ DEBUG(dbgs() << "Diverting " << oldedge << " via " << newedge );
// First check if the old edge was taken, if not, just delete it...
if (getEdgeWeight(oldedge) == 0) {
const BasicBlock *BB = GetPath(newedge.second,oldedge.second,P,GetPathToExit | GetPathToDest);
double w = getEdgeWeight (oldedge);
- DEBUG(errs() << ", Weight: " << format("%.20g",w) << "\n");
+ DEBUG(dbgs() << ", Weight: " << format("%.20g",w) << "\n");
do {
const BasicBlock *Parent = P.find(BB)->second;
Edge e = getEdge(Parent,BB);
removeEdge(oldedge);
}
-/// Replaces all occurences of RmBB in the ProfilingInfo with DestBB.
+/// Replaces all occurrences 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.
+/// occurrences of RmBB with DestBB.
template<>
-void ProfileInfo::replaceAllUses(const BasicBlock *RmBB,
- const BasicBlock *DestBB) {
- DEBUG(errs() << "Replacing " << RmBB->getName()
+void ProfileInfoT<Function,BasicBlock>::
+ replaceAllUses(const BasicBlock *RmBB, const BasicBlock *DestBB) {
+ DEBUG(dbgs() << "Replacing " << RmBB->getName()
<< " with " << DestBB->getName() << "\n");
const Function *F = DestBB->getParent();
std::map<const Function*, EdgeWeights>::iterator J =
/// Since its possible that there is more than one edge in the CFG from FristBB
/// to SecondBB its necessary to redirect the flow proporionally.
template<>
-void ProfileInfo::splitEdge(const BasicBlock *FirstBB,
- const BasicBlock *SecondBB,
- const BasicBlock *NewBB,
- bool MergeIdenticalEdges) {
+void ProfileInfoT<Function,BasicBlock>::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);
}
template<>
-void ProfileInfo::splitBlock(const BasicBlock *Old, const BasicBlock* New) {
+void ProfileInfoT<Function,BasicBlock>::splitBlock(const BasicBlock *Old,
+ const BasicBlock* New) {
const Function *F = Old->getParent();
std::map<const Function*, EdgeWeights>::iterator J =
EdgeInformation.find(F);
if (J == EdgeInformation.end()) return;
- DEBUG(errs() << "Splitting " << Old->getName() << " to " << New->getName() << "\n");
+ DEBUG(dbgs() << "Splitting " << Old->getName() << " to " << New->getName() << "\n");
std::set<Edge> Edges;
for (EdgeWeights::iterator ewi = J->second.begin(), ewe = J->second.end();
}
template<>
-void ProfileInfo::splitBlock(const BasicBlock *BB, const BasicBlock* NewBB,
- BasicBlock *const *Preds, unsigned NumPreds) {
+void ProfileInfoT<Function,BasicBlock>::splitBlock(const BasicBlock *BB,
+ const BasicBlock* NewBB,
+ BasicBlock *const *Preds,
+ unsigned NumPreds) {
const Function *F = BB->getParent();
std::map<const Function*, EdgeWeights>::iterator J =
EdgeInformation.find(F);
if (J == EdgeInformation.end()) return;
- DEBUG(errs() << "Splitting " << NumPreds << " Edges from " << BB->getName()
+ DEBUG(dbgs() << "Splitting " << NumPreds << " Edges from " << BB->getName()
<< " to " << NewBB->getName() << "\n");
// Collect weight that was redirected over NewBB.
}
template<>
-void ProfileInfo::transfer(const Function *Old, const Function *New) {
- DEBUG(errs() << "Replacing Function " << Old->getName() << " with "
+void ProfileInfoT<Function,BasicBlock>::transfer(const Function *Old,
+ const Function *New) {
+ DEBUG(dbgs() << "Replacing Function " << Old->getName() << " with "
<< New->getName() << "\n");
std::map<const Function*, EdgeWeights>::iterator J =
EdgeInformation.find(Old);
FunctionInformation.erase(Old);
}
-static double readEdgeOrRemember(ProfileInfo::Edge edge, double w, ProfileInfo::Edge &tocalc,
- unsigned &uncalc) {
+static double readEdgeOrRemember(ProfileInfo::Edge edge, double w,
+ ProfileInfo::Edge &tocalc, unsigned &uncalc) {
if (w == ProfileInfo::MissingValue) {
tocalc = edge;
uncalc++;
}
template<>
-bool ProfileInfo::CalculateMissingEdge(const BasicBlock *BB, Edge &removed, bool assumeEmptySelf) {
+bool ProfileInfoT<Function,BasicBlock>::
+ CalculateMissingEdge(const BasicBlock *BB, Edge &removed,
+ bool assumeEmptySelf) {
Edge edgetocalc;
unsigned uncalculated = 0;
// have no value
double incount = 0;
SmallSet<const BasicBlock*,8> pred_visited;
- pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
+ const_pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
if (bbi==bbe) {
Edge e = getEdge(0,BB);
incount += readEdgeOrRemember(e, getEdgeWeight(e) ,edgetocalc,uncalculated);
} else {
EdgeInformation[BB->getParent()][edgetocalc] = incount-outcount;
}
- DEBUG(errs() << "--Calc Edge Counter for " << edgetocalc << ": "
+ DEBUG(dbgs() << "--Calc Edge Counter for " << edgetocalc << ": "
<< format("%.20g", getEdgeWeight(edgetocalc)) << "\n");
removed = edgetocalc;
return true;
}
template<>
-bool ProfileInfo::EstimateMissingEdges(const BasicBlock *BB) {
- bool hasNoSuccessors = false;
-
+bool ProfileInfoT<Function,BasicBlock>::EstimateMissingEdges(const BasicBlock *BB) {
double inWeight = 0;
std::set<Edge> inMissing;
std::set<const BasicBlock*> ProcessedPreds;
- pred_const_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
+ const_pred_iterator bbi = pred_begin(BB), bbe = pred_end(BB);
if (bbi == bbe) {
readEdge(this,getEdge(0,BB),inWeight,inMissing);
}
std::set<Edge> outMissing;
std::set<const BasicBlock*> ProcessedSuccs;
succ_const_iterator sbbi = succ_begin(BB), sbbe = succ_end(BB);
- if (sbbi == sbbe) {
+ if (sbbi == sbbe)
readEdge(this,getEdge(BB,0),outWeight,outMissing);
- hasNoSuccessors = true;
- }
for ( ; sbbi != sbbe; ++sbbi ) {
if (ProcessedSuccs.insert(*sbbi).second) {
readEdge(this,getEdge(BB,*sbbi),outWeight,outMissing);
}
template<>
-void ProfileInfo::repair(const Function *F) {
+void ProfileInfoT<Function,BasicBlock>::repair(const Function *F) {
// if (getExecutionCount(&(F->getEntryBlock())) == 0) {
// for (Function::const_iterator FI = F->begin(), FE = F->end();
// FI != FE; ++FI) {
// const BasicBlock* BB = &(*FI);
// {
-// pred_const_iterator NBB = pred_begin(BB), End = pred_end(BB);
+// const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
// if (NBB == End) {
// setEdgeWeight(getEdge(0,BB),0);
// }
// Calculate incoming flow.
double iw = 0; unsigned inmissing = 0; unsigned incount = 0; unsigned invalid = 0;
std::set<const BasicBlock *> Processed;
- for (pred_const_iterator NBB = pred_begin(BB), End = pred_end(BB);
+ for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
NBB != End; ++NBB) {
if (Processed.insert(*NBB).second) {
Edge e = getEdge(*NBB, BB);
}
if (iw < 0) continue;
- // Check the recieving end of the path if it can handle the flow.
+ // Check the receiving end of the path if it can handle the flow.
double ow = getExecutionCount(Dest);
Processed.clear();
for (succ_const_iterator NBB = succ_begin(BB), End = succ_end(BB);
if (getEdgeWeight(e) == MissingValue) {
double iw = 0;
std::set<const BasicBlock *> Processed;
- for (pred_const_iterator NBB = pred_begin(BB), End = pred_end(BB);
+ for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
NBB != End; ++NBB) {
if (Processed.insert(*NBB).second) {
Edge e = getEdge(*NBB, BB);
FI = Unvisited.begin(), FE = Unvisited.end();
while(FI != FE && !FoundPath) {
const BasicBlock *BB = *FI; ++FI;
- const BasicBlock *Dest;
+ const BasicBlock *Dest = 0;
Path P;
bool BackEdgeFound = false;
- for (pred_const_iterator NBB = pred_begin(BB), End = pred_end(BB);
+ for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
NBB != End; ++NBB) {
Dest = GetPath(BB, *NBB, P, GetPathToDest | GetPathWithNewEdges);
if (Dest == *NBB) {
// Calculate incoming flow.
double iw = 0;
std::set<const BasicBlock *> Processed;
- for (pred_const_iterator NBB = pred_begin(BB), End = pred_end(BB);
+ for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
NBB != End; ++NBB) {
if (Processed.insert(*NBB).second) {
Edge e = getEdge(*NBB, BB);
while(FI != FE && !FoundPath) {
const BasicBlock *BB = *FI; ++FI;
- for (pred_const_iterator NBB = pred_begin(BB), End = pred_end(BB);
+ for (const_pred_iterator NBB = pred_begin(BB), End = pred_end(BB);
NBB != End; ++NBB) {
Edge e = getEdge(*NBB,BB);
double w = getEdgeWeight(e);
FI = Unvisited.begin(), FE = Unvisited.end();
while(FI != FE) {
const BasicBlock *BB = *FI; ++FI;
- errs() << BB->getName();
+ dbgs() << BB->getName();
if (FI != FE)
- errs() << ",";
+ dbgs() << ",";
}
errs() << "}";
}
}
-raw_ostream& operator<<(raw_ostream &O, const Function *F) {
- return O << F->getName();
-}
-
raw_ostream& operator<<(raw_ostream &O, const MachineFunction *MF) {
return O << MF->getFunction()->getName() << "(MF)";
}
-raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB) {
- return O << BB->getName();
-}
-
raw_ostream& operator<<(raw_ostream &O, const MachineBasicBlock *MBB) {
return O << MBB->getBasicBlock()->getName() << "(MB)";
}
-raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *, const BasicBlock *> E) {
- O << "(";
-
- if (E.first)
- O << E.first;
- else
- O << "0";
-
- O << ",";
-
- if (E.second)
- O << E.second;
- else
- O << "0";
-
- return O << ")";
-}
-
raw_ostream& operator<<(raw_ostream &O, std::pair<const MachineBasicBlock *, const MachineBasicBlock *> E) {
O << "(";
namespace {
struct NoProfileInfo : public ImmutablePass, public ProfileInfo {
static char ID; // Class identification, replacement for typeinfo
- NoProfileInfo() : ImmutablePass(&ID) {}
+ NoProfileInfo() : ImmutablePass(ID) {
+ initializeNoProfileInfoPass(*PassRegistry::getPassRegistry());
+ }
+
+ /// getAdjustedAnalysisPointer - This method is used when a pass implements
+ /// an analysis interface through multiple inheritance. If needed, it
+ /// should override this to adjust the this pointer as needed for the
+ /// specified pass info.
+ virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
+ if (PI == &ProfileInfo::ID)
+ return (ProfileInfo*)this;
+ return this;
+ }
+
+ virtual const char *getPassName() const {
+ return "NoProfileInfo";
+ }
};
} // End of anonymous namespace
char NoProfileInfo::ID = 0;
// Register this pass...
-static RegisterPass<NoProfileInfo>
-X("no-profile", "No Profile Information", false, true);
-
-// Declare that we implement the ProfileInfo interface
-static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
+INITIALIZE_AG_PASS(NoProfileInfo, ProfileInfo, "no-profile",
+ "No Profile Information", false, true, true)
ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }