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