Ruchira Sasanka | e27c344 | 2001-08-20 21:12:49 +0000 | [diff] [blame] | 1 | /* Title: MethodLiveVarInfo.cpp |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 2 | Author: Ruchira Sasanka |
| 3 | Date: Jun 30, 01 |
| 4 | Purpose: |
| 5 | |
Ruchira Sasanka | e27c344 | 2001-08-20 21:12:49 +0000 | [diff] [blame] | 6 | This is the interface for live variable info of a method that is required |
| 7 | by any other part of the compiler. |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 8 | |
| 9 | */ |
| 10 | |
| 11 | |
| 12 | #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h" |
Chris Lattner | ab58411 | 2002-02-05 00:34:50 +0000 | [diff] [blame^] | 13 | #include "llvm/Analysis/LiveVar/BBLiveVar.h" |
Ruchira Sasanka | e27c344 | 2001-08-20 21:12:49 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 1164632 | 2002-02-04 16:35:12 +0000 | [diff] [blame] | 15 | #include "llvm/BasicBlock.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 16 | #include "Support/PostOrderIterator.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 17 | #include <iostream> |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 18 | |
Chris Lattner | 4fd2dbb | 2002-02-04 20:00:08 +0000 | [diff] [blame] | 19 | AnalysisID MethodLiveVarInfo::ID(AnalysisID::create<MethodLiveVarInfo>()); |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 20 | |
Chris Lattner | ab58411 | 2002-02-05 00:34:50 +0000 | [diff] [blame^] | 21 | //----------------------------------------------------------------------------- |
| 22 | // Accessor Functions |
| 23 | //----------------------------------------------------------------------------- |
| 24 | |
| 25 | // gets OutSet of a BB |
| 26 | const LiveVarSet *MethodLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const { |
| 27 | return BB2BBLVMap.find(BB)->second->getOutSet(); |
| 28 | } |
| 29 | |
| 30 | // gets InSet of a BB |
| 31 | const LiveVarSet *MethodLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const { |
| 32 | return BB2BBLVMap.find(BB)->second->getInSet(); |
| 33 | } |
| 34 | |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 35 | |
| 36 | //----------------------------------------------------------------------------- |
| 37 | // Performs live var analysis for a method |
| 38 | //----------------------------------------------------------------------------- |
| 39 | |
| 40 | bool MethodLiveVarInfo::runOnMethod(Method *M) { |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 41 | if (DEBUG_LV) std::cerr << "Analysing live variables ...\n"; |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 42 | |
| 43 | // create and initialize all the BBLiveVars of the CFG |
| 44 | constructBBs(M); |
| 45 | |
| 46 | while (doSingleBackwardPass(M)) |
| 47 | ; // Iterate until we are done. |
| 48 | |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 49 | if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n"; |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | //----------------------------------------------------------------------------- |
| 55 | // constructs BBLiveVars and init Def and In sets |
| 56 | //----------------------------------------------------------------------------- |
| 57 | |
| 58 | void MethodLiveVarInfo::constructBBs(const Method *M) { |
| 59 | unsigned int POId = 0; // Reverse Depth-first Order ID |
| 60 | |
| 61 | for(po_iterator<const Method*> BBI = po_begin(M), BBE = po_end(M); |
| 62 | BBI != BBE; ++BBI, ++POId) { |
| 63 | const BasicBlock *BB = *BBI; // get the current BB |
| 64 | |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 65 | if (DEBUG_LV) { std::cerr << " For BB "; printValue(BB); cerr << ":\n"; } |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 66 | |
| 67 | // create a new BBLiveVar |
| 68 | BBLiveVar *LVBB = new BBLiveVar(BB, POId); |
| 69 | BB2BBLVMap[BB] = LVBB; // insert the pair to Map |
| 70 | |
| 71 | LVBB->calcDefUseSets(); // calculates the def and in set |
| 72 | |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 73 | if (DEBUG_LV) |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 74 | LVBB->printAllSets(); |
| 75 | } |
| 76 | |
| 77 | // Since the PO iterator does not discover unreachable blocks, |
| 78 | // go over the random iterator and init those blocks as well. |
| 79 | // However, LV info is not correct for those blocks (they are not |
| 80 | // analyzed) |
| 81 | // |
| 82 | for (Method::const_iterator BBRI = M->begin(), BBRE = M->end(); |
| 83 | BBRI != BBRE; ++BBRI, ++POId) |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 84 | if (!BB2BBLVMap[*BBRI]) // Not yet processed? |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 85 | BB2BBLVMap[*BBRI] = new BBLiveVar(*BBRI, POId); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | //----------------------------------------------------------------------------- |
| 90 | // do one backward pass over the CFG (for iterative analysis) |
| 91 | //----------------------------------------------------------------------------- |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 92 | |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 93 | bool MethodLiveVarInfo::doSingleBackwardPass(const Method *M) { |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 94 | if (DEBUG_LV) std::cerr << "\n After Backward Pass ...\n"; |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 95 | |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 96 | bool NeedAnotherIteration = false; |
| 97 | for (po_iterator<const Method*> BBI = po_begin(M); BBI != po_end(M) ; ++BBI) { |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 98 | BBLiveVar *LVBB = BB2BBLVMap[*BBI]; |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 99 | assert(LVBB && "BasicBlock information not set for block!"); |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 100 | |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 101 | if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n"; |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 102 | |
| 103 | if(LVBB->isOutSetChanged()) |
| 104 | LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet |
| 105 | |
| 106 | if (LVBB->isInSetChanged()) // to calc Outsets of preds |
| 107 | NeedAnotherIteration |= LVBB->applyFlowFunc(BB2BBLVMap); |
| 108 | |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 109 | if (DEBUG_LV) LVBB->printInOutSets(); |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // true if we need to reiterate over the CFG |
| 113 | return NeedAnotherIteration; |
| 114 | } |
| 115 | |
| 116 | |
Chris Lattner | 4fd2dbb | 2002-02-04 20:00:08 +0000 | [diff] [blame] | 117 | void MethodLiveVarInfo::releaseMemory() { |
Ruchira Sasanka | 789cebb | 2001-12-08 21:05:27 +0000 | [diff] [blame] | 118 | // First delete all BBLiveVar objects created in constructBBs(). A new object |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 119 | // of type BBLiveVar is created for every BasicBlock in the method |
Ruchira Sasanka | 789cebb | 2001-12-08 21:05:27 +0000 | [diff] [blame] | 120 | // |
Chris Lattner | ab58411 | 2002-02-05 00:34:50 +0000 | [diff] [blame^] | 121 | for (std::map<const BasicBlock *, BBLiveVar *>::iterator |
| 122 | HMI = BB2BBLVMap.begin(), |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 123 | HME = BB2BBLVMap.end(); HMI != HME; ++HMI) |
Chris Lattner | 4fd2dbb | 2002-02-04 20:00:08 +0000 | [diff] [blame] | 124 | delete HMI->second; // delete all BBLiveVar in BB2BBLVMap |
Ruchira Sasanka | 789cebb | 2001-12-08 21:05:27 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 4fd2dbb | 2002-02-04 20:00:08 +0000 | [diff] [blame] | 126 | BB2BBLVMap.clear(); |
Ruchira Sasanka | 789cebb | 2001-12-08 21:05:27 +0000 | [diff] [blame] | 127 | |
| 128 | // Then delete all objects of type LiveVarSet created in calcLiveVarSetsForBB |
| 129 | // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches |
| 130 | // to return LiveVarSet's before/after a machine instruction quickly). It |
| 131 | // is sufficient to free up all LiveVarSet using only one cache since |
| 132 | // both caches refer to the same sets |
Ruchira Sasanka | 789cebb | 2001-12-08 21:05:27 +0000 | [diff] [blame] | 133 | // |
Chris Lattner | ab58411 | 2002-02-05 00:34:50 +0000 | [diff] [blame^] | 134 | for (std::map<const MachineInstr*, const LiveVarSet*>::iterator |
| 135 | MI = MInst2LVSetBI.begin(), |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 136 | ME = MInst2LVSetBI.end(); MI != ME; ++MI) |
Chris Lattner | 4fd2dbb | 2002-02-04 20:00:08 +0000 | [diff] [blame] | 137 | delete MI->second; // delete all LiveVarSets in MInst2LVSetBI |
| 138 | |
| 139 | MInst2LVSetBI.clear(); |
| 140 | MInst2LVSetAI.clear(); |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 144 | |
| 145 | |
Ruchira Sasanka | 789cebb | 2001-12-08 21:05:27 +0000 | [diff] [blame] | 146 | //----------------------------------------------------------------------------- |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 147 | // Following functions will give the LiveVar info for any machine instr in |
| 148 | // a method. It should be called after a call to analyze(). |
| 149 | // |
| 150 | // Thsese functions calucluates live var info for all the machine instrs in a |
| 151 | // BB when LVInfo for one inst is requested. Hence, this function is useful |
| 152 | // when live var info is required for many (or all) instructions in a basic |
| 153 | // block. Also, the arguments to this method does not require specific |
| 154 | // iterators. |
Ruchira Sasanka | 789cebb | 2001-12-08 21:05:27 +0000 | [diff] [blame] | 155 | //----------------------------------------------------------------------------- |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 156 | |
Ruchira Sasanka | 789cebb | 2001-12-08 21:05:27 +0000 | [diff] [blame] | 157 | //----------------------------------------------------------------------------- |
| 158 | // Gives live variable information before a machine instruction |
| 159 | //----------------------------------------------------------------------------- |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 160 | |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 161 | const LiveVarSet * |
| 162 | MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst, |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 163 | const BasicBlock *BB) { |
| 164 | if (const LiveVarSet *LVSet = MInst2LVSetBI[MInst]) { |
| 165 | return LVSet; // if found, just return the set |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 166 | } else { |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 167 | calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 168 | return MInst2LVSetBI[MInst]; |
Ruchira Sasanka | e27c344 | 2001-08-20 21:12:49 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 171 | |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 172 | |
Ruchira Sasanka | 789cebb | 2001-12-08 21:05:27 +0000 | [diff] [blame] | 173 | //----------------------------------------------------------------------------- |
| 174 | // Gives live variable information after a machine instruction |
| 175 | //----------------------------------------------------------------------------- |
Ruchira Sasanka | e27c344 | 2001-08-20 21:12:49 +0000 | [diff] [blame] | 176 | const LiveVarSet * |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 177 | MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI, |
| 178 | const BasicBlock *BB) { |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 179 | |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 180 | if (const LiveVarSet *LVSet = MInst2LVSetAI[MI]) { |
Chris Lattner | bdfd328 | 2002-02-04 20:49:04 +0000 | [diff] [blame] | 181 | return LVSet; // if found, just return the set |
| 182 | } else { |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 183 | calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB |
| 184 | return MInst2LVSetAI[MI]; |
Ruchira Sasanka | e27c344 | 2001-08-20 21:12:49 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 187 | |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 188 | |
Ruchira Sasanka | 789cebb | 2001-12-08 21:05:27 +0000 | [diff] [blame] | 189 | |
| 190 | //----------------------------------------------------------------------------- |
| 191 | // This method calculates the live variable information for all the |
| 192 | // instructions in a basic block and enter the newly constructed live |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 193 | // variable sets into a the caches (MInst2LVSetAI, MInst2LVSetBI) |
Ruchira Sasanka | 789cebb | 2001-12-08 21:05:27 +0000 | [diff] [blame] | 194 | //----------------------------------------------------------------------------- |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 195 | |
| 196 | void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) { |
| 197 | const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec(); |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 198 | |
| 199 | LiveVarSet *CurSet = new LiveVarSet(); |
Ruchira Sasanka | e27c344 | 2001-08-20 21:12:49 +0000 | [diff] [blame] | 200 | const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet |
| 201 | CurSet->setUnion(SetAI); // CurSet now contains OutSet |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 202 | |
Ruchira Sasanka | e27c344 | 2001-08-20 21:12:49 +0000 | [diff] [blame] | 203 | // iterate over all the machine instructions in BB |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 204 | for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(), |
| 205 | MIE = MIVec.rend(); MII != MIE; ++MII) { |
| 206 | // MI is cur machine inst |
| 207 | const MachineInstr *MI = *MII; |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 208 | |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 209 | MInst2LVSetAI[MI] = SetAI; // record in After Inst map |
Ruchira Sasanka | e27c344 | 2001-08-20 21:12:49 +0000 | [diff] [blame] | 210 | |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 211 | CurSet->applyTranferFuncForMInst(MI); // apply the transfer Func |
Ruchira Sasanka | e27c344 | 2001-08-20 21:12:49 +0000 | [diff] [blame] | 212 | LiveVarSet *NewSet = new LiveVarSet(); // create a new set and |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 213 | NewSet->setUnion(CurSet); // copy the set after T/F to it |
Ruchira Sasanka | e27c344 | 2001-08-20 21:12:49 +0000 | [diff] [blame] | 214 | |
Chris Lattner | a51c7a8 | 2002-02-04 23:31:16 +0000 | [diff] [blame] | 215 | MInst2LVSetBI[MI] = NewSet; // record in Before Inst map |
Ruchira Sasanka | e27c344 | 2001-08-20 21:12:49 +0000 | [diff] [blame] | 216 | |
| 217 | // SetAI will be used in the next iteration |
| 218 | SetAI = NewSet; |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 219 | } |
Ruchira Sasanka | 683847f | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 220 | } |