Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 1 | /* Title: ValueSet.h |
| 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 by |
| 7 | any other part of the compiler. |
| 8 | |
| 9 | */ |
| 10 | |
| 11 | |
| 12 | #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h" |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | /************************** Constructor/Destructor ***************************/ |
| 18 | |
| 19 | |
| 20 | MethodLiveVarInfo::MethodLiveVarInfo(Method *const MethPtr) : BB2BBLVMap() |
| 21 | { |
| 22 | Meth = MethPtr; // init BB2BBLVMap and records Method for future use |
| 23 | } |
| 24 | |
| 25 | |
| 26 | |
| 27 | MethodLiveVarInfo:: ~MethodLiveVarInfo() |
| 28 | { |
| 29 | BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin(); // hash map iterator |
| 30 | |
| 31 | for( ; HMI != BB2BBLVMap.end() ; HMI ++ ) { |
| 32 | if( (*HMI).first ) // delete all LiveVarSets in BB2BBLVMap |
| 33 | delete (*HMI).second; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | |
| 38 | // -------------------------- support functions ------------------------------- |
| 39 | |
| 40 | |
| 41 | |
| 42 | // constructs BBLiveVars and init Def and In sets |
| 43 | void MethodLiveVarInfo::constructBBs() |
| 44 | { |
| 45 | unsigned int POId = 0; // Reverse Depth-first Order ID |
| 46 | |
| 47 | cfg::po_const_iterator BBI = cfg::po_begin(Meth); |
| 48 | |
| 49 | for( ; BBI != cfg::po_end(Meth) ; ++BBI, ++POId) |
| 50 | { |
| 51 | |
| 52 | if(DEBUG_LV) cout << "-- For BB " << (*BBI)->getName() << ":" << endl ; |
| 53 | |
| 54 | const BasicBlock *BB = *BBI; // get the current BB |
| 55 | BBLiveVar * LVBB = new BBLiveVar( BB, POId ); // create a new BBLiveVar |
| 56 | |
| 57 | BB2BBLVMap[ BB ] = LVBB; // insert the pair to Map |
| 58 | |
| 59 | LVBB->calcDefUseSets(); // calculates the def and in set |
| 60 | |
| 61 | if(DEBUG_LV) LVBB->printAllSets(); |
| 62 | //cout << "InSetChanged: " << LVBB->isInSetChanged() << endl; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | } |
| 67 | |
| 68 | // do one backward pass over the CFG |
| 69 | bool MethodLiveVarInfo::doSingleBackwardPass() |
| 70 | { |
| 71 | bool ResultFlow, NeedAnotherIteration = false; |
| 72 | |
| 73 | if(DEBUG_LV) cout << endl << "------- After Backward Pass --------" << endl; |
| 74 | |
| 75 | cfg::po_const_iterator BBI = cfg::po_begin(Meth); |
| 76 | |
| 77 | for( ; BBI != cfg::po_end(Meth) ; ++BBI) |
| 78 | { |
| 79 | |
| 80 | BBLiveVar* LVBB = BB2BBLVMap[*BBI]; |
| 81 | assert( LVBB ); |
| 82 | |
| 83 | if(DEBUG_LV) cout << "-- For BB " << (*BBI)->getName() << ":" << endl; |
| 84 | // cout << " (POId=" << LVBB->getPOId() << ")" << endl ; |
| 85 | |
| 86 | ResultFlow = false; |
| 87 | |
| 88 | if( LVBB->isOutSetChanged() ) |
| 89 | LVBB->applyTransferFunc(); // apply the Transfer Func to calc the InSet |
| 90 | if( LVBB->isInSetChanged() ) |
| 91 | ResultFlow = LVBB->applyFlowFunc( BB2BBLVMap ); // to calc Outsets of preds |
| 92 | |
| 93 | if(DEBUG_LV) LVBB->printInOutSets(); |
| 94 | //cout << "InChanged = " << LVBB->isInSetChanged() |
| 95 | //cout << " UpdatedBBwithLowerPOId = " << ResultFlow << endl; |
| 96 | |
| 97 | if( ResultFlow ) NeedAnotherIteration = true; |
| 98 | |
| 99 | } |
| 100 | |
| 101 | return NeedAnotherIteration; // true if we need to reiterate over the CFG |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | void MethodLiveVarInfo::analyze() // performs live var anal for a method |
| 109 | { |
| 110 | //cout << "In analyze . . ." << cout; |
| 111 | |
| 112 | constructBBs(); // create and initialize all the BBLiveVars of the CFG |
| 113 | |
| 114 | bool NeedAnotherIteration = false; |
| 115 | do { |
| 116 | NeedAnotherIteration = doSingleBackwardPass( ); // do one pass over CFG |
| 117 | } while (NeedAnotherIteration ); // repeat until we need more iterations |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | /* This function will give the LiveVar info for any instruction in a method. It |
| 124 | should be called after a call to analyze(). |
| 125 | |
| 126 | This function calucluates live var info for all the instructions in a BB, |
| 127 | when LVInfo for one inst is requested. Hence, this function is useful when |
| 128 | live var info is required for many (or all) instructions in a basic block |
| 129 | Also, the arguments to this method does not require specific iterators |
| 130 | */ |
| 131 | |
| 132 | |
| 133 | const LiveVarSet * |
| 134 | MethodLiveVarInfo::getLiveVarSetBeforeInst(const Instruction *const Inst) |
| 135 | { |
| 136 | // get the BB corresponding to the instruction |
| 137 | const BasicBlock *const CurBB = Inst->getParent(); |
| 138 | |
| 139 | const LiveVarSet *LVSet = Inst2LVSetMap[Inst]; |
| 140 | |
| 141 | if( LVSet ) return LVSet; // if found, just return the set |
| 142 | |
| 143 | const BasicBlock::InstListType& InstListInBB = CurBB->getInstList(); |
| 144 | BasicBlock::InstListType::const_reverse_iterator |
| 145 | InstItEnd= InstListInBB.rend() - 1; // InstItEnd is set to the first instr |
| 146 | |
| 147 | // LVSet of first instr = InSet |
| 148 | Inst2LVSetMap[*InstItEnd] = getInSetOfBB( CurBB ); |
| 149 | |
| 150 | // if the first instruction is requested, just return the InSet |
| 151 | if( Inst == *InstItEnd) return Inst2LVSetMap[Inst]; |
| 152 | |
| 153 | // else calculate for all other instruction in the BB |
| 154 | |
| 155 | BasicBlock::InstListType::const_reverse_iterator |
| 156 | InstIt= InstListInBB.rbegin(); // get the iterator for instructions in BB |
| 157 | |
| 158 | LiveVarSet *CurSet = new LiveVarSet(); |
| 159 | CurSet->setUnion( getOutSetOfBB( CurBB )); // LVSet now contains the OutSet |
| 160 | |
| 161 | // calculate LVSet for all instructions in the basic block (except the first) |
| 162 | for( ; InstIt != InstItEnd ; InstIt++) { |
| 163 | |
| 164 | CurSet->applyTranferFuncForInst( *InstIt ); // apply the transfer Func |
| 165 | LiveVarSet *NewSet = new LiveVarSet(); // create a new set and |
| 166 | NewSet->setUnion( CurSet ); // copy the set after T/F to it |
| 167 | Inst2LVSetMap[*InstIt] = NewSet; // record that in the map |
| 168 | } |
| 169 | |
| 170 | return Inst2LVSetMap[Inst]; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | /* |
| 176 | NOTES: delete all the LVBBs allocated by adding a destructor to the BB2BBLVMap??? |
| 177 | use the dfo_iterator in the doSingleBackwardPass |
| 178 | */ |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |