1 /* Title: MethodLiveVarInfo.h -*- C++ -*-
2 Author: Ruchira Sasanka
6 This is the interface for live variable info of a method that is required
7 by any other part of the compiler
9 It must 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
15 live var info of a BB.
17 The live var set before an instruction can be obtained in 2 ways:
19 1. Use the method getLiveVarSetAfterInst(Instruction *) to get the LV Info
20 just after an instruction. (also exists getLiveVarSetBeforeInst(..))
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.
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.
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).
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:
39 LiveVarSet LVSet; // this will be the set used to traverse through each BB
41 // Initialize LVSet so that it is the same as OutSet of the BB
42 LVSet.setUnion( LVI->getOutSetOfBB( *BBI ) );
44 BasicBlock::InstListType::const_reverse_iterator
45 InstIterator = InstListInBB.rbegin(); // get the rev iter for inst in BB
47 // iterate over all the instructions in BB in reverse
48 for( ; InstIterator != InstListInBB.rend(); InstIterator++) {
50 //...... all code here which uses LVSet ........
52 LVSet.applyTranferFuncForInst(*InstIterator);
54 // Now LVSet contains live vars ABOVE the current instrution
57 See buildInterferenceGraph() for the above example.
61 #ifndef METH_LIVE_VAR_INFO_H
62 #define METH_LIVE_VAR_INFO_H
64 // set DEBUG_LV for printing out debug messages
65 // if DEBUG_LV is 1 normal output messages
66 // if DEBUG_LV is 2 extensive debug info for each instr
68 static const int DEBUG_LV = 0;
70 #include "llvm/Analysis/LiveVar/BBLiveVar.h"
71 #include "llvm/Pass.h"
73 class MethodLiveVarInfo : public MethodPass {
75 // Live var anal is done on this method - set by constructor
78 // A map betwn the BasicBlock and BBLiveVar
79 BBToBBLiveVarMapType BB2BBLVMap;
81 // Machine Instr to LiveVarSet Map for providing LVset BEFORE each inst
82 MInstToLiveVarSetMapType MInst2LVSetBI;
84 // Machine Instr to LiveVarSet Map for providing LVset AFTER each inst
85 MInstToLiveVarSetMapType MInst2LVSetAI;
88 // --------- private methods -----------------------------------------
90 // constructs BBLiveVars and init Def and In sets
93 // do one backward pass over the CFG
94 bool doSingleBackwardPass();
96 // calculates live var sets for instructions in a BB
97 void calcLiveVarSetsForBB(const BasicBlock *BB);
101 static AnalysisID ID; // We are an analysis, we must have an ID
103 MethodLiveVarInfo(AnalysisID id = ID) : Meth(0) { assert(id == ID); }
104 ~MethodLiveVarInfo() { releaseMemory(); }
106 // --------- Implement the MethodPass interface ----------------------
108 // runOnMethod - Perform analysis, update internal data structures.
109 virtual bool runOnMethod(Method *M);
111 // releaseMemory - After LiveVariable analysis has been used, forget!
112 virtual void releaseMemory();
114 // getAnalysisUsageInfo - Provide self!
115 virtual void getAnalysisUsageInfo(AnalysisSet &Required,
116 AnalysisSet &Destroyed,
117 AnalysisSet &Provided) {
118 Provided.push_back(ID);
121 // --------- Functions to access analysis results -------------------
123 // gets OutSet of a BB
124 inline const LiveVarSet *getOutSetOfBB( const BasicBlock *BB) const {
125 return BB2BBLVMap.find(BB)->second->getOutSet();
128 // gets InSet of a BB
129 inline const LiveVarSet *getInSetOfBB( const BasicBlock *BB) const {
130 return BB2BBLVMap.find(BB)->second->getInSet();
133 // gets the Live var set BEFORE an instruction
134 const LiveVarSet * getLiveVarSetBeforeMInst(const MachineInstr *Inst,
135 const BasicBlock *CurBB);
137 // gets the Live var set AFTER an instruction
138 const LiveVarSet * getLiveVarSetAfterMInst(const MachineInstr *MInst,
139 const BasicBlock *CurBB);