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