1 /* Title: MethodLiveVarInfo.h
2 Author: Ruchira Sasanka
6 This is the interface for live variable info of a method that is required by
7 any other part of the compiler
9 It should be called like:
11 MethodLiveVarInfo MLVI( Mehtod *); // initializes data structures
12 MLVI.analyze(); // do the actural live variable anal
14 After the analysis, getInSetOfBB or getOutSetofBB can be called to get
17 The live var set before an instruction can be constructed in several ways:
19 1. Use the OutSet and applyTranferFuncForInst(const Instruction *const Inst)
20 declared in LiveVarSet and traverse the instructions of a basic block in
21 reverse (using const_reverse_iterator in the BB class).
23 This is the most efficient method if you need LV info for several (all)
24 instructions in a BasicBlock. An example is given below:
27 LiveVarSet LVSet; // this will be the set used to traverse through each BB
29 // Initialize LVSet so that it is the same as OutSet of the BB
30 LVSet.setUnion( LVI->getOutSetOfBB( *BBI ) );
32 BasicBlock::InstListType::const_reverse_iterator
33 InstIterator = InstListInBB.rbegin(); // get the reverse it for inst in BB
35 // iterate over all the instructions in BB in reverse
36 for( ; InstIterator != InstListInBB.rend(); InstIterator++) {
38 //...... all code here which uses LVSet ........
40 LVSet.applyTranferFuncForInst(*InstIterator);
42 // Now LVSet contains live vars ABOVE the current instrution
45 See buildInterferenceGraph() for the above example.
48 2. Use the function getLiveVarSetBeforeInst(Instruction *) to get the LV Info
49 just before an instruction.
51 This function caluclates the LV info for a BB only once and caches that
52 info. If the cache does not contain the LV info of the instruction, it
53 calculates the LV info for the whole BB and caches them.
55 Getting liveVar info this way uses more memory since, LV info should be
59 **BUGS: Cannot be called on a method prototype because the BB front()
60 iterator causes a seg fault in CFG.h (Chris will fix this)
61 So, currently, DO NOT call this for method prototypes.
66 #ifndef METH_LIVE_VAR_INFO_H
67 #define METH_LIVE_VAR_INFO_H
69 // for printing out debug messages
72 #include "LiveVarSet.h"
73 #include "llvm/BasicBlock.h"
74 #include "llvm/Instruction.h"
75 #include "llvm/Method.h"
78 #include "LiveVarMap.h"
79 #include "BBLiveVar.h"
82 class MethodLiveVarInfo
85 const Method *Meth; // Live var anal is done on this method
88 BBToBBLiveVarMapType BB2BBLVMap; // A map betwn the BasicBlock and BBLiveVar
90 InstToLiveVarSetMapType Inst2LVSetMap; // Instruction to LiveVarSet Map
91 //- for providing LV info for each inst
93 void constructBBs(); // constructs BBLiveVars and init Def and In sets
94 bool doSingleBackwardPass(); // do one backward pass over the CFG
99 MethodLiveVarInfo(Method *const Meth); // constructor
101 ~MethodLiveVarInfo(); // destructor
103 void analyze(); // performs a liver var analysis of a single method
105 // gets OutSet of a BB
106 inline const LiveVarSet *getOutSetOfBB( const BasicBlock *const BB) const {
107 return ( (* (BB2BBLVMap.find(BB)) ).second )->getOutSet();
110 // gets InSet of a BB
111 inline const LiveVarSet *getInSetOfBB( const BasicBlock *const BB) const {
112 return ( (* (BB2BBLVMap.find(BB)) ).second )->getInSet();
114 // gets the Live var set before an instruction
116 MethodLiveVarInfo::getLiveVarSetBeforeInst(const Instruction *const Inst);