Rename Method to Function
[oota-llvm.git] / include / llvm / CodeGen / FunctionLiveVarInfo.h
1 /* Title:   MethodLiveVarInfo.h             -*- C++ -*-
2    Author:  Ruchira Sasanka
3    Date:    Jun 30, 01
4    Purpose: 
5
6    This is the interface for live variable info of a method that is required 
7    by any other part of the compiler
8
9    It must be called like:
10
11        MethodLiveVarInfo MLVI(Function *);  // initializes data structures
12        MLVI.analyze();                     // do the actural live variable anal
13
14  After the analysis, getInSetOfBB or getOutSetofBB can be called to get 
15  live var info of a BB.
16
17  The live var set before an instruction can be obtained in 2 ways:
18
19  1. Use the method getLiveVarSetAfterInst(Instruction *) to get the LV Info 
20     just after an instruction. (also exists getLiveVarSetBeforeInst(..))
21
22     This function caluclates the LV info for a BB only once and caches that 
23     info. If the cache does not contain the LV info of the instruction, it 
24     calculates the LV info for the whole BB and caches them.
25
26     Getting liveVar info this way uses more memory since, LV info should be 
27     cached. However, if you need LV info of nearly all the instructions of a
28     BB, this is the best and simplest interfrace.
29
30
31  2. Use the OutSet and applyTranferFuncForInst(const Instruction *const Inst) 
32     declared in LiveVarSet and  traverse the instructions of a basic block in 
33     reverse (using const_reverse_iterator in the BB class). 
34
35     This is the most memory efficient method if you need LV info for 
36     only several instructions in a BasicBlock. An example is given below:
37
38
39     LiveVarSet LVSet;  // this will be the set used to traverse through each BB
40
41     // Initialize LVSet so that it is the same as OutSet of the BB
42     LVSet.setUnion( LVI->getOutSetOfBB( *BBI ) );  
43  
44     BasicBlock::InstListType::const_reverse_iterator 
45       InstIterator = InstListInBB.rbegin(); // get the rev iter for inst in BB
46
47       // iterate over all the instructions in BB in reverse
48     for( ; InstIterator != InstListInBB.rend(); InstIterator++) {  
49
50       //...... all  code here which uses LVSet ........
51
52       LVSet.applyTranferFuncForInst(*InstIterator);
53
54       // Now LVSet contains live vars ABOVE the current instrution
55     }
56
57     See buildInterferenceGraph() for the above example.
58 */
59
60
61 #ifndef METH_LIVE_VAR_INFO_H
62 #define METH_LIVE_VAR_INFO_H
63
64 #include "llvm/Pass.h"
65 #include "llvm/Analysis/LiveVar/ValueSet.h"
66 #include "Support/CommandLine.h"
67
68 class BBLiveVar;
69 class MachineInstr;
70
71
72 enum LiveVarDebugLevel_t {
73   LV_DEBUG_None,
74   LV_DEBUG_Normal,
75   LV_DEBUG_Instr,
76   LV_DEBUG_Verbose
77 };
78
79 extern cl::Enum<LiveVarDebugLevel_t> DEBUG_LV;
80
81
82 class MethodLiveVarInfo : public MethodPass {
83   // Machine Instr to LiveVarSet Map for providing LVset BEFORE each inst
84   std::map<const MachineInstr *, const ValueSet *> MInst2LVSetBI; 
85
86   // Machine Instr to LiveVarSet Map for providing LVset AFTER each inst
87   std::map<const MachineInstr *, const ValueSet *> MInst2LVSetAI; 
88
89   // Stored Function that the data is computed with respect to
90   const Function *M;
91
92   // --------- private methods -----------------------------------------
93
94   // constructs BBLiveVars and init Def and In sets
95   void constructBBs(const Function *F);
96     
97   // do one backward pass over the CFG
98   bool doSingleBackwardPass(const Function *F, unsigned int iter); 
99
100   // calculates live var sets for instructions in a BB
101   void calcLiveVarSetsForBB(const BasicBlock *BB);
102   
103 public:
104   static AnalysisID ID;    // We are an analysis, we must have an ID
105
106   MethodLiveVarInfo(AnalysisID id = ID) { assert(id == ID); }
107
108   // --------- Implement the MethodPass interface ----------------------
109
110   // runOnMethod - Perform analysis, update internal data structures.
111   virtual bool runOnMethod(Function *F);
112
113   // releaseMemory - After LiveVariable analysis has been used, forget!
114   virtual void releaseMemory();
115
116   // getAnalysisUsageInfo - Provide self!
117   virtual void getAnalysisUsageInfo(AnalysisSet &Required,
118                                     AnalysisSet &Destroyed,
119                                     AnalysisSet &Provided) {
120     Provided.push_back(ID);
121   }
122
123   // --------- Functions to access analysis results -------------------
124
125   // gets OutSet of a BB
126   const ValueSet &getOutSetOfBB(const BasicBlock *BB) const;
127
128   // gets InSet of a BB
129   const ValueSet &getInSetOfBB(const BasicBlock *BB) const;
130
131   // gets the Live var set BEFORE an instruction
132   const ValueSet &getLiveVarSetBeforeMInst(const MachineInstr *MI,
133                                            const BasicBlock *BB);
134
135   // gets the Live var set AFTER an instruction
136   const ValueSet &getLiveVarSetAfterMInst(const MachineInstr *MI,
137                                           const BasicBlock *BB);
138 };
139
140 #endif