1 //===- llvm/Analysis/ProfileInfo.h - Profile Info Interface -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the generic ProfileInfo interface, which is used as the
11 // common interface used by all clients of profiling information, and
12 // implemented either by making static guestimations, or by actually reading in
13 // profiling information gathered by running the program.
15 // Note that to be useful, all profile-based optimizations should preserve
16 // ProfileInfo, which requires that they notify it when changes to the CFG are
17 // made. (This is not implemented yet.)
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_ANALYSIS_PROFILEINFO_H
22 #define LLVM_ANALYSIS_PROFILEINFO_H
24 #include "llvm/BasicBlock.h"
34 /// ProfileInfo Class - This class holds and maintains profiling
35 /// information for some unit of code.
38 // Types for handling profiling information.
39 typedef std::pair<const BasicBlock*, const BasicBlock*> Edge;
40 typedef std::pair<Edge, double> EdgeWeight;
41 typedef std::map<Edge, double> EdgeWeights;
42 typedef std::map<const BasicBlock*, double> BlockCounts;
45 // EdgeInformation - Count the number of times a transition between two
46 // blocks is executed. As a special case, we also hold an edge from the
47 // null BasicBlock to the entry block to indicate how many times the
48 // function was entered.
49 std::map<const Function*, EdgeWeights> EdgeInformation;
51 // BlockInformation - Count the number of times a block is executed.
52 std::map<const Function*, BlockCounts> BlockInformation;
54 // FunctionInformation - Count the number of times a function is executed.
55 std::map<const Function*, double> FunctionInformation;
57 static char ID; // Class identification, replacement for typeinfo
58 virtual ~ProfileInfo(); // We want to be subclassed
60 // MissingValue - The value that is returned for execution counts in case
61 // no value is available.
62 static const double MissingValue;
64 // getFunction() - Returns the Function for an Edge, checking for validity.
65 static const Function* getFunction(Edge e) {
67 return e.first->getParent();
68 } else if (e.second) {
69 return e.second->getParent();
71 assert(0 && "Invalid ProfileInfo::Edge");
72 return (const Function*)0;
75 // getEdge() - Creates an Edge from two BasicBlocks.
76 static Edge getEdge(const BasicBlock *Src, const BasicBlock *Dest) {
77 return std::make_pair(Src, Dest);
80 //===------------------------------------------------------------------===//
81 /// Profile Information Queries
83 double getExecutionCount(const Function *F);
85 double getExecutionCount(const BasicBlock *BB);
87 double getEdgeWeight(Edge e) const {
88 std::map<const Function*, EdgeWeights>::const_iterator J =
89 EdgeInformation.find(getFunction(e));
90 if (J == EdgeInformation.end()) return MissingValue;
92 EdgeWeights::const_iterator I = J->second.find(e);
93 if (I == J->second.end()) return MissingValue;
98 EdgeWeights &getEdgeWeights (const Function *F) {
99 return EdgeInformation[F];
102 //===------------------------------------------------------------------===//
103 /// Analysis Update Methods
105 void removeBlock(const BasicBlock *BB) {
106 std::map<const Function*, BlockCounts>::iterator J =
107 BlockInformation.find(BB->getParent());
108 if (J == BlockInformation.end()) return;
113 void removeEdge(Edge e) {
114 std::map<const Function*, EdgeWeights>::iterator J =
115 EdgeInformation.find(getFunction(e));
116 if (J == EdgeInformation.end()) return;
121 void splitEdge(const BasicBlock *FirstBB, const BasicBlock *SecondBB,
122 const BasicBlock *NewBB, bool MergeIdenticalEdges = false);
124 void replaceAllUses(const BasicBlock *RmBB, const BasicBlock *DestBB);
127 /// createProfileLoaderPass - This function returns a Pass that loads the
128 /// profiling information for the module from the specified filename, making
129 /// it available to the optimizers.
130 Pass *createProfileLoaderPass(const std::string &Filename);
132 raw_ostream& operator<<(raw_ostream &O, ProfileInfo::Edge E);
134 } // End llvm namespace