2004-02-10 22:11:42 +00:00
|
|
|
//===- llvm/Analysis/ProfileInfo.h - Profile Info Interface -----*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2004-02-10 22:11:42 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2004-02-10 22:11:42 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the generic ProfileInfo interface, which is used as the
|
|
|
|
// common interface used by all clients of profiling information, and
|
|
|
|
// implemented either by making static guestimations, or by actually reading in
|
|
|
|
// profiling information gathered by running the program.
|
|
|
|
//
|
|
|
|
// Note that to be useful, all profile-based optimizations should preserve
|
|
|
|
// ProfileInfo, which requires that they notify it when changes to the CFG are
|
2009-08-08 17:43:09 +00:00
|
|
|
// made. (This is not implemented yet.)
|
2004-02-10 22:11:42 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ANALYSIS_PROFILEINFO_H
|
|
|
|
#define LLVM_ANALYSIS_PROFILEINFO_H
|
|
|
|
|
2009-12-03 09:30:12 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-08-08 17:43:09 +00:00
|
|
|
#include <cassert>
|
2004-02-11 06:11:06 +00:00
|
|
|
#include <string>
|
2004-03-08 21:30:18 +00:00
|
|
|
#include <map>
|
2009-12-03 09:30:12 +00:00
|
|
|
#include <set>
|
2004-02-11 06:11:06 +00:00
|
|
|
|
2004-02-10 22:11:42 +00:00
|
|
|
namespace llvm {
|
2004-02-11 06:11:06 +00:00
|
|
|
class Pass;
|
2009-08-26 15:56:38 +00:00
|
|
|
class raw_ostream;
|
2004-02-11 06:11:06 +00:00
|
|
|
|
2009-12-03 09:30:12 +00:00
|
|
|
class BasicBlock;
|
|
|
|
class Function;
|
|
|
|
class MachineBasicBlock;
|
|
|
|
class MachineFunction;
|
|
|
|
|
2009-12-23 17:55:11 +00:00
|
|
|
// Helper for dumping edges to dbgs().
|
2009-12-03 09:30:12 +00:00
|
|
|
raw_ostream& operator<<(raw_ostream &O, std::pair<const BasicBlock *, const BasicBlock *> E);
|
|
|
|
raw_ostream& operator<<(raw_ostream &O, std::pair<const MachineBasicBlock *, const MachineBasicBlock *> E);
|
|
|
|
|
|
|
|
raw_ostream& operator<<(raw_ostream &O, const BasicBlock *BB);
|
|
|
|
raw_ostream& operator<<(raw_ostream &O, const MachineBasicBlock *MBB);
|
|
|
|
|
|
|
|
raw_ostream& operator<<(raw_ostream &O, const Function *F);
|
|
|
|
raw_ostream& operator<<(raw_ostream &O, const MachineFunction *MF);
|
|
|
|
|
2009-08-08 17:43:09 +00:00
|
|
|
/// ProfileInfo Class - This class holds and maintains profiling
|
2004-03-08 22:03:45 +00:00
|
|
|
/// information for some unit of code.
|
2009-12-03 09:30:12 +00:00
|
|
|
template<class FType, class BType>
|
|
|
|
class ProfileInfoT {
|
2009-07-14 06:58:59 +00:00
|
|
|
public:
|
|
|
|
// Types for handling profiling information.
|
2009-12-03 09:30:12 +00:00
|
|
|
typedef std::pair<const BType*, const BType*> Edge;
|
2009-08-24 21:37:48 +00:00
|
|
|
typedef std::pair<Edge, double> EdgeWeight;
|
|
|
|
typedef std::map<Edge, double> EdgeWeights;
|
2009-12-03 09:30:12 +00:00
|
|
|
typedef std::map<const BType*, double> BlockCounts;
|
|
|
|
typedef std::map<const BType*, const BType*> Path;
|
2009-07-14 06:58:59 +00:00
|
|
|
|
2004-03-08 21:30:18 +00:00
|
|
|
protected:
|
2009-08-24 21:37:48 +00:00
|
|
|
// EdgeInformation - Count the number of times a transition between two
|
|
|
|
// blocks is executed. As a special case, we also hold an edge from the
|
|
|
|
// null BasicBlock to the entry block to indicate how many times the
|
|
|
|
// function was entered.
|
2009-12-03 09:30:12 +00:00
|
|
|
std::map<const FType*, EdgeWeights> EdgeInformation;
|
2009-08-05 21:51:16 +00:00
|
|
|
|
2009-08-24 21:37:48 +00:00
|
|
|
// BlockInformation - Count the number of times a block is executed.
|
2009-12-03 09:30:12 +00:00
|
|
|
std::map<const FType*, BlockCounts> BlockInformation;
|
2009-08-05 21:51:16 +00:00
|
|
|
|
2009-08-24 21:37:48 +00:00
|
|
|
// FunctionInformation - Count the number of times a function is executed.
|
2009-12-03 09:30:12 +00:00
|
|
|
std::map<const FType*, double> FunctionInformation;
|
|
|
|
|
|
|
|
ProfileInfoT<MachineFunction, MachineBasicBlock> *MachineProfile;
|
2004-03-08 21:30:18 +00:00
|
|
|
public:
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID; // Class identification, replacement for typeinfo
|
2009-12-03 09:30:12 +00:00
|
|
|
ProfileInfoT();
|
|
|
|
~ProfileInfoT(); // We want to be subclassed
|
2005-04-21 20:19:05 +00:00
|
|
|
|
2009-08-08 17:43:09 +00:00
|
|
|
// MissingValue - The value that is returned for execution counts in case
|
|
|
|
// no value is available.
|
2009-08-24 21:37:48 +00:00
|
|
|
static const double MissingValue;
|
2009-08-08 17:43:09 +00:00
|
|
|
|
|
|
|
// getFunction() - Returns the Function for an Edge, checking for validity.
|
2009-12-03 09:30:12 +00:00
|
|
|
static const FType* getFunction(Edge e) {
|
2009-09-01 10:06:05 +00:00
|
|
|
if (e.first) {
|
|
|
|
return e.first->getParent();
|
|
|
|
} else if (e.second) {
|
|
|
|
return e.second->getParent();
|
|
|
|
}
|
|
|
|
assert(0 && "Invalid ProfileInfo::Edge");
|
2009-12-03 09:30:12 +00:00
|
|
|
return (const FType*)0;
|
2009-08-08 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getEdge() - Creates an Edge from two BasicBlocks.
|
2009-12-03 09:30:12 +00:00
|
|
|
static Edge getEdge(const BType *Src, const BType *Dest) {
|
2009-08-08 18:59:03 +00:00
|
|
|
return std::make_pair(Src, Dest);
|
2009-08-08 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
2004-02-10 22:11:42 +00:00
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
/// Profile Information Queries
|
|
|
|
///
|
2009-12-03 09:30:12 +00:00
|
|
|
double getExecutionCount(const FType *F);
|
|
|
|
|
|
|
|
double getExecutionCount(const BType *BB);
|
2009-08-08 17:43:09 +00:00
|
|
|
|
2009-12-03 09:30:12 +00:00
|
|
|
void setExecutionCount(const BType *BB, double w);
|
|
|
|
|
|
|
|
void addExecutionCount(const BType *BB, double w);
|
2009-08-08 17:43:09 +00:00
|
|
|
|
|
|
|
double getEdgeWeight(Edge e) const {
|
2009-12-03 09:30:12 +00:00
|
|
|
typename std::map<const FType*, EdgeWeights>::const_iterator J =
|
2009-08-08 17:43:09 +00:00
|
|
|
EdgeInformation.find(getFunction(e));
|
|
|
|
if (J == EdgeInformation.end()) return MissingValue;
|
2009-07-14 06:58:59 +00:00
|
|
|
|
2009-12-03 09:30:12 +00:00
|
|
|
typename EdgeWeights::const_iterator I = J->second.find(e);
|
2009-08-08 17:43:09 +00:00
|
|
|
if (I == J->second.end()) return MissingValue;
|
2004-03-08 22:03:45 +00:00
|
|
|
|
2009-08-08 17:43:09 +00:00
|
|
|
return I->second;
|
2004-03-08 21:30:18 +00:00
|
|
|
}
|
2004-03-08 22:03:45 +00:00
|
|
|
|
2009-12-03 09:30:12 +00:00
|
|
|
void setEdgeWeight(Edge e, double w) {
|
|
|
|
DEBUG_WITH_TYPE("profile-info",
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << "Creating Edge " << e
|
2009-12-03 09:30:12 +00:00
|
|
|
<< " (weight: " << format("%.20g",w) << ")\n");
|
|
|
|
EdgeInformation[getFunction(e)][e] = w;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addEdgeWeight(Edge e, double w);
|
|
|
|
|
|
|
|
EdgeWeights &getEdgeWeights (const FType *F) {
|
2009-08-24 21:37:48 +00:00
|
|
|
return EdgeInformation[F];
|
|
|
|
}
|
|
|
|
|
2004-02-10 22:11:42 +00:00
|
|
|
//===------------------------------------------------------------------===//
|
|
|
|
/// Analysis Update Methods
|
|
|
|
///
|
2009-12-03 09:30:12 +00:00
|
|
|
void removeBlock(const BType *BB);
|
|
|
|
|
|
|
|
void removeEdge(Edge e);
|
|
|
|
|
|
|
|
void replaceEdge(const Edge &, const Edge &);
|
|
|
|
|
|
|
|
enum GetPathMode {
|
|
|
|
GetPathToExit = 1,
|
|
|
|
GetPathToValue = 2,
|
|
|
|
GetPathToDest = 4,
|
|
|
|
GetPathWithNewEdges = 8
|
|
|
|
};
|
|
|
|
|
|
|
|
const BType *GetPath(const BType *Src, const BType *Dest,
|
|
|
|
Path &P, unsigned Mode);
|
|
|
|
|
|
|
|
void divertFlow(const Edge &, const Edge &);
|
|
|
|
|
|
|
|
void splitEdge(const BType *FirstBB, const BType *SecondBB,
|
|
|
|
const BType *NewBB, bool MergeIdenticalEdges = false);
|
|
|
|
|
|
|
|
void splitBlock(const BType *Old, const BType* New);
|
|
|
|
|
|
|
|
void splitBlock(const BType *BB, const BType* NewBB,
|
|
|
|
BType *const *Preds, unsigned NumPreds);
|
|
|
|
|
|
|
|
void replaceAllUses(const BType *RmBB, const BType *DestBB);
|
|
|
|
|
|
|
|
void transfer(const FType *Old, const FType *New);
|
|
|
|
|
|
|
|
void repair(const FType *F);
|
|
|
|
|
|
|
|
void dump(FType *F = 0, bool real = true) {
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << "**** This is ProfileInfo " << this << " speaking:\n";
|
2009-12-03 09:30:12 +00:00
|
|
|
if (!real) {
|
|
|
|
typename std::set<const FType*> Functions;
|
|
|
|
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << "Functions: \n";
|
2009-12-03 09:30:12 +00:00
|
|
|
if (F) {
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << F << "@" << format("%p", F) << ": " << format("%.20g",getExecutionCount(F)) << "\n";
|
2009-12-03 09:30:12 +00:00
|
|
|
Functions.insert(F);
|
|
|
|
} else {
|
|
|
|
for (typename std::map<const FType*, double>::iterator fi = FunctionInformation.begin(),
|
|
|
|
fe = FunctionInformation.end(); fi != fe; ++fi) {
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << fi->first << "@" << format("%p",fi->first) << ": " << format("%.20g",fi->second) << "\n";
|
2009-12-03 09:30:12 +00:00
|
|
|
Functions.insert(fi->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (typename std::set<const FType*>::iterator FI = Functions.begin(), FE = Functions.end();
|
|
|
|
FI != FE; ++FI) {
|
|
|
|
const FType *F = *FI;
|
|
|
|
typename std::map<const FType*, BlockCounts>::iterator bwi = BlockInformation.find(F);
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << "BasicBlocks for Function " << F << ":\n";
|
2009-12-03 09:30:12 +00:00
|
|
|
for (typename BlockCounts::const_iterator bi = bwi->second.begin(), be = bwi->second.end(); bi != be; ++bi) {
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << bi->first << "@" << format("%p", bi->first) << ": " << format("%.20g",bi->second) << "\n";
|
2009-12-03 09:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (typename std::set<const FType*>::iterator FI = Functions.begin(), FE = Functions.end();
|
|
|
|
FI != FE; ++FI) {
|
|
|
|
typename std::map<const FType*, EdgeWeights>::iterator ei = EdgeInformation.find(*FI);
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << "Edges for Function " << ei->first << ":\n";
|
2009-12-03 09:30:12 +00:00
|
|
|
for (typename EdgeWeights::iterator ewi = ei->second.begin(), ewe = ei->second.end();
|
|
|
|
ewi != ewe; ++ewi) {
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << ewi->first << ": " << format("%.20g",ewi->second) << "\n";
|
2009-12-03 09:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(F && "No function given, this is not supported!");
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << "Functions: \n";
|
|
|
|
dbgs() << F << "@" << format("%p", F) << ": " << format("%.20g",getExecutionCount(F)) << "\n";
|
2009-12-03 09:30:12 +00:00
|
|
|
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << "BasicBlocks for Function " << F << ":\n";
|
2009-12-03 09:30:12 +00:00
|
|
|
for (typename FType::const_iterator BI = F->begin(), BE = F->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
const BType *BB = &(*BI);
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << BB << "@" << format("%p", BB) << ": " << format("%.20g",getExecutionCount(BB)) << "\n";
|
2009-12-03 09:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-23 17:55:11 +00:00
|
|
|
dbgs() << "**** ProfileInfo " << this << ", over and out.\n";
|
2009-09-09 17:52:57 +00:00
|
|
|
}
|
|
|
|
|
2009-12-03 09:30:12 +00:00
|
|
|
bool CalculateMissingEdge(const BType *BB, Edge &removed, bool assumeEmptyExit = false);
|
2009-09-09 17:52:57 +00:00
|
|
|
|
2009-12-03 09:30:12 +00:00
|
|
|
bool EstimateMissingEdges(const BType *BB);
|
2009-09-09 17:52:57 +00:00
|
|
|
|
2009-12-03 09:30:12 +00:00
|
|
|
ProfileInfoT<MachineFunction, MachineBasicBlock> *MI() {
|
|
|
|
if (MachineProfile == 0)
|
|
|
|
MachineProfile = new ProfileInfoT<MachineFunction, MachineBasicBlock>();
|
|
|
|
return MachineProfile;
|
|
|
|
}
|
2009-09-09 17:52:57 +00:00
|
|
|
|
2009-12-03 09:30:12 +00:00
|
|
|
bool hasMI() const {
|
|
|
|
return (MachineProfile != 0);
|
|
|
|
}
|
2004-02-10 22:11:42 +00:00
|
|
|
};
|
|
|
|
|
2009-12-03 09:30:12 +00:00
|
|
|
typedef ProfileInfoT<Function, BasicBlock> ProfileInfo;
|
|
|
|
typedef ProfileInfoT<MachineFunction, MachineBasicBlock> MachineProfileInfo;
|
|
|
|
|
2004-03-08 22:03:45 +00:00
|
|
|
/// createProfileLoaderPass - This function returns a Pass that loads the
|
|
|
|
/// profiling information for the module from the specified filename, making
|
|
|
|
/// it available to the optimizers.
|
|
|
|
Pass *createProfileLoaderPass(const std::string &Filename);
|
2009-08-26 15:13:44 +00:00
|
|
|
|
2004-02-10 22:11:42 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|