2004-02-10 22:11:42 +00:00
|
|
|
//===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-02-10 22:11:42 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2004-02-10 22:11:42 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the abstract ProfileInfo interface, and the default
|
|
|
|
// "no profile" implementation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-01-08 22:01:16 +00:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2004-02-10 22:11:42 +00:00
|
|
|
#include "llvm/Analysis/ProfileInfo.h"
|
|
|
|
#include "llvm/Pass.h"
|
2004-03-08 22:04:08 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-02-05 23:42:17 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-03-08 22:04:08 +00:00
|
|
|
#include <set>
|
2004-02-10 22:11:42 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2004-02-11 06:10:18 +00:00
|
|
|
// Register the ProfileInfo interface, providing a nice name to refer to.
|
2008-05-13 00:00:25 +00:00
|
|
|
static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
|
2007-05-03 01:11:54 +00:00
|
|
|
char ProfileInfo::ID = 0;
|
2004-02-10 22:11:42 +00:00
|
|
|
|
|
|
|
ProfileInfo::~ProfileInfo() {}
|
|
|
|
|
2004-03-08 22:04:08 +00:00
|
|
|
unsigned ProfileInfo::getExecutionCount(BasicBlock *BB) const {
|
|
|
|
pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
|
|
|
|
|
|
|
|
// Are there zero predecessors of this block?
|
|
|
|
if (PI == PE) {
|
|
|
|
// If this is the entry block, look for the Null -> Entry edge.
|
2007-03-22 16:38:57 +00:00
|
|
|
if (BB == &BB->getParent()->getEntryBlock())
|
2004-03-08 22:04:08 +00:00
|
|
|
return getEdgeWeight(0, BB);
|
|
|
|
else
|
|
|
|
return 0; // Otherwise, this is a dead block.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if there are predecessors, the execution count of this block is
|
|
|
|
// the sum of the edge frequencies from the incoming edges. Note that if
|
|
|
|
// there are multiple edges from a predecessor to this block that we don't
|
|
|
|
// want to count its weight multiple times. For this reason, we keep track of
|
|
|
|
// the predecessors we've seen and only count them if we haven't run into them
|
|
|
|
// yet.
|
|
|
|
//
|
|
|
|
// We don't want to create an std::set unless we are dealing with a block that
|
|
|
|
// has a LARGE number of in-edges. Handle the common case of having only a
|
|
|
|
// few in-edges with special code.
|
|
|
|
//
|
|
|
|
BasicBlock *FirstPred = *PI;
|
|
|
|
unsigned Count = getEdgeWeight(FirstPred, BB);
|
|
|
|
++PI;
|
|
|
|
if (PI == PE) return Count; // Quick exit for single predecessor blocks
|
|
|
|
|
|
|
|
BasicBlock *SecondPred = *PI;
|
|
|
|
if (SecondPred != FirstPred) Count += getEdgeWeight(SecondPred, BB);
|
|
|
|
++PI;
|
|
|
|
if (PI == PE) return Count; // Quick exit for two predecessor blocks
|
|
|
|
|
|
|
|
BasicBlock *ThirdPred = *PI;
|
|
|
|
if (ThirdPred != FirstPred && ThirdPred != SecondPred)
|
|
|
|
Count += getEdgeWeight(ThirdPred, BB);
|
|
|
|
++PI;
|
|
|
|
if (PI == PE) return Count; // Quick exit for three predecessor blocks
|
|
|
|
|
|
|
|
std::set<BasicBlock*> ProcessedPreds;
|
|
|
|
ProcessedPreds.insert(FirstPred);
|
|
|
|
ProcessedPreds.insert(SecondPred);
|
|
|
|
ProcessedPreds.insert(ThirdPred);
|
|
|
|
for (; PI != PE; ++PI)
|
|
|
|
if (ProcessedPreds.insert(*PI).second)
|
|
|
|
Count += getEdgeWeight(*PI, BB);
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-10 22:11:42 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NoProfile ProfileInfo implementation
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace {
|
2007-02-05 23:42:17 +00:00
|
|
|
struct VISIBILITY_HIDDEN NoProfileInfo
|
2007-05-01 21:15:47 +00:00
|
|
|
: public ImmutablePass, public ProfileInfo {
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Class identification, replacement for typeinfo
|
2007-05-01 21:15:47 +00:00
|
|
|
NoProfileInfo() : ImmutablePass((intptr_t)&ID) {}
|
|
|
|
};
|
2008-05-13 00:00:25 +00:00
|
|
|
} // End of anonymous namespace
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char NoProfileInfo::ID = 0;
|
|
|
|
// Register this pass...
|
|
|
|
static RegisterPass<NoProfileInfo>
|
|
|
|
X("no-profile", "No Profile Information", false, true);
|
2004-02-10 22:11:42 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
// Declare that we implement the ProfileInfo interface
|
|
|
|
static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
|
2005-01-08 22:01:16 +00:00
|
|
|
|
|
|
|
ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }
|