//===- ProfileInfo.cpp - Profile Info Interface ---------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the abstract ProfileInfo interface, and the default
// "no profile" implementation.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "profile-info"
#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/ProfileInfo.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>
using namespace llvm;
namespace llvm {
template<> char ProfileInfoT<Function,BasicBlock>::ID = 0;
}
// Register the ProfileInfo interface, providing a nice name to refer to.
INITIALIZE_ANALYSIS_GROUP(ProfileInfo, "Profile Information", NoProfileInfo)
namespace llvm {
template <>
ProfileInfoT<MachineFunction, MachineBasicBlock>::ProfileInfoT() {}
template <>
ProfileInfoT<MachineFunction, MachineBasicBlock>::~ProfileInfoT() {}
template <>
ProfileInfoT<Function, BasicBlock>::ProfileInfoT() {
MachineProfile = 0;
}
template <>
ProfileInfoT<Function, BasicBlock>::~ProfileInfoT() {
if (MachineProfile) delete MachineProfile;
}
template<>
char ProfileInfoT<MachineFunction, MachineBasicBlock>::ID = 0;
template<>
const double ProfileInfoT<Function,BasicBlock>::MissingValue = -1;
template<> const
double ProfileInfoT<MachineFunction, MachineBasicBlock>::MissingValue = -1;
template<> double
ProfileInfoT<Function,BasicBlock>::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;
}
double Count = MissingValue;
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);
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) {
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.
if (Count == MissingValue) {
succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB);
// Are there zero successors of this block?
if (SI == SE) {
Edge e = getEdge(BB,0);
Count = getEdgeWeight(e);
} else {
std::set<const BasicBlock*> ProcessedSuccs;
Count = 0;
for (; SI != SE; ++SI)
if (ProcessedSuccs.insert(*SI).second) {
double w = getEdgeWeight(getEdge(BB, <