Make block and function count available via ProfileInfo.
[oota-llvm.git] / include / llvm / Analysis / ProfileInfo.h
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.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_ANALYSIS_PROFILEINFO_H
22 #define LLVM_ANALYSIS_PROFILEINFO_H
23
24 #include <string>
25 #include <map>
26
27 namespace llvm {
28   class BasicBlock;
29   class Function;
30   class Pass;
31
32   /// ProfileInfo Class - This class holds and maintains edge profiling
33   /// information for some unit of code.
34   class ProfileInfo {
35   public:
36     // Types for handling profiling information.
37     typedef std::pair<const BasicBlock*, const BasicBlock*> Edge;
38
39   protected:
40     // EdgeCounts - Count the number of times a transition between two blocks is
41     // executed.  As a special case, we also hold an edge from the null
42     // BasicBlock to the entry block to indicate how many times the function was
43     // entered.
44     std::map<Edge, unsigned> EdgeCounts;
45
46     // BlockCounts - Count the number of times a block is executed.
47     std::map<const BasicBlock*, unsigned> BlockCounts;
48
49     // FunctionCounts - Count the number of times a function is executed.
50     std::map<const Function*, unsigned> FunctionCounts;
51   public:
52     static char ID; // Class identification, replacement for typeinfo
53     virtual ~ProfileInfo();  // We want to be subclassed
54
55     //===------------------------------------------------------------------===//
56     /// Profile Information Queries
57     ///
58     unsigned getExecutionCount(const Function *F) const;
59
60     unsigned getExecutionCount(const BasicBlock *BB) const;
61
62     unsigned getEdgeWeight(const BasicBlock *Src, 
63                            const BasicBlock *Dest) const {
64       std::map<Edge, unsigned>::const_iterator I = 
65         EdgeCounts.find(std::make_pair(Src, Dest));
66       return I != EdgeCounts.end() ? I->second : 0;
67     }
68
69     //===------------------------------------------------------------------===//
70     /// Analysis Update Methods
71     ///
72
73   };
74
75   /// createProfileLoaderPass - This function returns a Pass that loads the
76   /// profiling information for the module from the specified filename, making
77   /// it available to the optimizers.
78   Pass *createProfileLoaderPass(const std::string &Filename);
79 } // End llvm namespace
80
81 #endif