ProfileInfo.h revision 4bac4b9899ea4e56f4b5dd99887d4e460e0f13ad
1//===- llvm/Analysis/ProfileInfo.h - Profile Info Interface -----*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 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. 14// 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.) 18// 19//===----------------------------------------------------------------------===// 20 21#ifndef LLVM_ANALYSIS_PROFILEINFO_H 22#define LLVM_ANALYSIS_PROFILEINFO_H 23 24#include "llvm/BasicBlock.h" 25#include <cassert> 26#include <string> 27#include <map> 28 29namespace llvm { 30 class Function; 31 class Pass; 32 class raw_ostream; 33 34 /// ProfileInfo Class - This class holds and maintains profiling 35 /// information for some unit of code. 36 class ProfileInfo { 37 public: 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; 43 44 protected: 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; 50 51 // BlockInformation - Count the number of times a block is executed. 52 std::map<const Function*, BlockCounts> BlockInformation; 53 54 // FunctionInformation - Count the number of times a function is executed. 55 std::map<const Function*, double> FunctionInformation; 56 public: 57 static char ID; // Class identification, replacement for typeinfo 58 virtual ~ProfileInfo(); // We want to be subclassed 59 60 // MissingValue - The value that is returned for execution counts in case 61 // no value is available. 62 static const double MissingValue; 63 64 // getFunction() - Returns the Function for an Edge, checking for validity. 65 static const Function* getFunction(Edge e) { 66 assert(e.second && "Invalid ProfileInfo::Edge"); 67 return e.second->getParent(); 68 } 69 70 // getEdge() - Creates an Edge from two BasicBlocks. 71 static Edge getEdge(const BasicBlock *Src, const BasicBlock *Dest) { 72 return std::make_pair(Src, Dest); 73 } 74 75 //===------------------------------------------------------------------===// 76 /// Profile Information Queries 77 /// 78 double getExecutionCount(const Function *F); 79 80 double getExecutionCount(const BasicBlock *BB); 81 82 double getEdgeWeight(Edge e) const { 83 std::map<const Function*, EdgeWeights>::const_iterator J = 84 EdgeInformation.find(getFunction(e)); 85 if (J == EdgeInformation.end()) return MissingValue; 86 87 EdgeWeights::const_iterator I = J->second.find(e); 88 if (I == J->second.end()) return MissingValue; 89 90 return I->second; 91 } 92 93 EdgeWeights &getEdgeWeights (const Function *F) { 94 return EdgeInformation[F]; 95 } 96 97 //===------------------------------------------------------------------===// 98 /// Analysis Update Methods 99 /// 100 101 }; 102 103 /// createProfileLoaderPass - This function returns a Pass that loads the 104 /// profiling information for the module from the specified filename, making 105 /// it available to the optimizers. 106 Pass *createProfileLoaderPass(const std::string &Filename); 107 108 raw_ostream& operator<<(raw_ostream &O, ProfileInfo::Edge E); 109 110} // End llvm namespace 111 112#endif 113