2004-02-10 23:11:42 +01:00
|
|
|
//===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2004-02-10 23:11:42 +01:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2004-02-10 23:11:42 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the abstract ProfileInfo interface, and the default
|
|
|
|
// "no profile" implementation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-01-08 23:01:16 +01:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2004-02-10 23:11:42 +01:00
|
|
|
#include "llvm/Analysis/ProfileInfo.h"
|
|
|
|
#include "llvm/Pass.h"
|
2004-03-08 23:04:08 +01:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-02-06 00:42:17 +01:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-03-08 23:04:08 +01:00
|
|
|
#include <set>
|
2004-02-10 23:11:42 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2004-02-11 07:10:18 +01:00
|
|
|
// Register the ProfileInfo interface, providing a nice name to refer to.
|
2008-05-13 02:00:25 +02:00
|
|
|
static RegisterAnalysisGroup<ProfileInfo> Z("Profile Information");
|
2007-05-03 03:11:54 +02:00
|
|
|
char ProfileInfo::ID = 0;
|
2004-02-10 23:11:42 +01:00
|
|
|
|
|
|
|
ProfileInfo::~ProfileInfo() {}
|
|
|
|
|
2004-03-08 23:04:08 +01: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 17:38:57 +01:00
|
|
|
if (BB == &BB->getParent()->getEntryBlock())
|
2004-03-08 23:04:08 +01: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 23:11:42 +01:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// NoProfile ProfileInfo implementation
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace {
|
2007-02-06 00:42:17 +01:00
|
|
|
struct VISIBILITY_HIDDEN NoProfileInfo
|
2007-05-01 23:15:47 +02:00
|
|
|
: public ImmutablePass, public ProfileInfo {
|
2007-05-03 03:11:54 +02:00
|
|
|
static char ID; // Class identification, replacement for typeinfo
|
2008-09-04 19:05:41 +02:00
|
|
|
NoProfileInfo() : ImmutablePass(&ID) {}
|
2007-05-01 23:15:47 +02:00
|
|
|
};
|
2008-05-13 02:00:25 +02:00
|
|
|
} // End of anonymous namespace
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
char NoProfileInfo::ID = 0;
|
|
|
|
// Register this pass...
|
|
|
|
static RegisterPass<NoProfileInfo>
|
|
|
|
X("no-profile", "No Profile Information", false, true);
|
2004-02-10 23:11:42 +01:00
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
// Declare that we implement the ProfileInfo interface
|
|
|
|
static RegisterAnalysisGroup<ProfileInfo, true> Y(X);
|
2005-01-08 23:01:16 +01:00
|
|
|
|
|
|
|
ImmutablePass *llvm::createNoProfileInfoPass() { return new NoProfileInfo(); }
|