Added virtual function to generate an instruction sequence to
[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( Mehtod *);  // 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
62
63 #ifndef METH_LIVE_VAR_INFO_H
64 #define METH_LIVE_VAR_INFO_H
65
66 // set DEBUG_LV for printing out debug messages
67 // if DEBUG_LV is 1 normal output messages
68 // if DEBUG_LV is 2 extensive debug info for each instr
69
70 static const int DEBUG_LV = 0;
71
72 #include "LiveVarSet.h"
73 #include "llvm/BasicBlock.h"
74 #include "llvm/Instruction.h"
75 #include "llvm/Method.h"
76
77 #include "LiveVarMap.h"
78 #include "BBLiveVar.h"
79
80
81 class MethodLiveVarInfo
82 {
83  private:
84
85   // Live var anal is done on this method - set by constructor
86   const Method *const Meth;   
87
88   // A map betwn the BasicBlock and BBLiveVar
89   BBToBBLiveVarMapType  BB2BBLVMap;  
90
91   // Machine Instr to LiveVarSet Map for providing LVset BEFORE each inst
92   MInstToLiveVarSetMapType MInst2LVSetBI; 
93
94   // Machine Instr to LiveVarSet Map for providing LVset AFTER each inst
95   MInstToLiveVarSetMapType MInst2LVSetAI; 
96
97   // True if the analyze() method has been called. This is checked when
98   // getInSet/OutSet is called to prevent calling those methods before analyze
99   bool HasAnalyzed;
100
101
102   // --------- private methods -----------------------------------------
103
104   // constructs BBLiveVars and init Def and In sets
105   void constructBBs();      
106     
107   // do one backward pass over the CFG
108   bool  doSingleBackwardPass(); 
109
110   // calculates live var sets for instructions in a BB
111   void calcLiveVarSetsForBB(const BasicBlock *const BB);
112   
113
114  public:
115   MethodLiveVarInfo(const Method *const Meth);    // constructor 
116
117   ~MethodLiveVarInfo();                           // destructor
118
119   // performs a liver var analysis of a single method
120   void analyze();            
121
122   // gets OutSet of a BB
123   inline const LiveVarSet *getOutSetOfBB( const BasicBlock *const BB) const { 
124     assert( HasAnalyzed && "call analyze() before calling this" );
125     return  ( (* (BB2BBLVMap.find(BB)) ).second ) ->getOutSet();
126   }
127
128   // gets InSet of a BB
129   inline const LiveVarSet *getInSetOfBB( const BasicBlock *const BB)  const { 
130     assert( HasAnalyzed && "call analyze() before calling this" );
131     return (   (* (BB2BBLVMap.find(BB)) ).second  )->getInSet();
132   }
133
134   // gets the Live var set BEFORE an instruction
135   const LiveVarSet * getLiveVarSetBeforeMInst(const MachineInstr *const Inst,
136                                               const BasicBlock *const CurBB);
137
138   // gets the Live var set AFTER an instruction
139   const LiveVarSet * getLiveVarSetAfterMInst(const MachineInstr *const MInst,
140                                              const BasicBlock *const CurBB);
141
142   // TODO: Put a method to invalidate all LVBBs, delete existin BBLivar in
143   //  BB2BBLVMap
144   // inline invalidate
145
146
147  
148 };
149
150
151
152
153
154 #endif
155
156
157
158