LiveRangeCalc.cpp revision b5a457c4cbc71db6ae313ef1bf8eadac65767ab0
1//===---- LiveRangeCalc.cpp - Calculate live ranges -----------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// Implementation of the LiveRangeCalc class. 11// 12//===----------------------------------------------------------------------===// 13 14#define DEBUG_TYPE "regalloc" 15#include "LiveRangeCalc.h" 16#include "llvm/CodeGen/MachineDominators.h" 17 18using namespace llvm; 19 20void LiveRangeCalc::reset(const MachineFunction *MF) { 21 unsigned N = MF->getNumBlockIDs(); 22 Seen.clear(); 23 Seen.resize(N); 24 LiveOut.resize(N); 25 LiveIn.clear(); 26} 27 28 29// Transfer information from the LiveIn vector to the live ranges. 30void LiveRangeCalc::updateLiveIns(VNInfo *OverrideVNI, SlotIndexes *Indexes) { 31 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(), 32 E = LiveIn.end(); I != E; ++I) { 33 if (!I->DomNode) 34 continue; 35 MachineBasicBlock *MBB = I->DomNode->getBlock(); 36 37 VNInfo *VNI = OverrideVNI ? OverrideVNI : I->Value; 38 assert(VNI && "No live-in value found"); 39 40 SlotIndex Start, End; 41 tie(Start, End) = Indexes->getMBBRange(MBB); 42 43 if (I->Kill.isValid()) 44 I->LI->addRange(LiveRange(Start, I->Kill, VNI)); 45 else { 46 I->LI->addRange(LiveRange(Start, End, VNI)); 47 // The value is live-through, update LiveOut as well. Defer the Domtree 48 // lookup until it is needed. 49 assert(Seen.test(MBB->getNumber())); 50 LiveOut[MBB] = LiveOutPair(VNI, 0); 51 } 52 } 53 LiveIn.clear(); 54} 55 56 57void LiveRangeCalc::extend(LiveInterval *LI, 58 SlotIndex Kill, 59 SlotIndexes *Indexes, 60 MachineDominatorTree *DomTree, 61 VNInfo::Allocator *Alloc) { 62 assert(LI && "Missing live range"); 63 assert(Kill.isValid() && "Invalid SlotIndex"); 64 assert(Indexes && "Missing SlotIndexes"); 65 assert(DomTree && "Missing dominator tree"); 66 SlotIndex LastUse = Kill.getPrevSlot(); 67 68 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(LastUse); 69 assert(Kill && "No MBB at Kill"); 70 71 // Is there a def in the same MBB we can extend? 72 if (LI->extendInBlock(Indexes->getMBBStartIdx(KillMBB), LastUse)) 73 return; 74 75 // Find the single reaching def, or determine if Kill is jointly dominated by 76 // multiple values, and we may need to create even more phi-defs to preserve 77 // VNInfo SSA form. Perform a search for all predecessor blocks where we 78 // know the dominating VNInfo. 79 VNInfo *VNI = findReachingDefs(LI, KillMBB, Kill, Indexes, DomTree); 80 81 // When there were multiple different values, we may need new PHIs. 82 if (!VNI) 83 updateSSA(Indexes, DomTree, Alloc); 84 85 updateLiveIns(VNI, Indexes); 86} 87 88 89// This function is called by a client after using the low-level API to add 90// live-out and live-in blocks. The unique value optimization is not 91// available, SplitEditor::transferValues handles that case directly anyway. 92void LiveRangeCalc::calculateValues(SlotIndexes *Indexes, 93 MachineDominatorTree *DomTree, 94 VNInfo::Allocator *Alloc) { 95 assert(Indexes && "Missing SlotIndexes"); 96 assert(DomTree && "Missing dominator tree"); 97 updateSSA(Indexes, DomTree, Alloc); 98 updateLiveIns(0, Indexes); 99} 100 101 102VNInfo *LiveRangeCalc::findReachingDefs(LiveInterval *LI, 103 MachineBasicBlock *KillMBB, 104 SlotIndex Kill, 105 SlotIndexes *Indexes, 106 MachineDominatorTree *DomTree) { 107 // Blocks where LI should be live-in. 108 SmallVector<MachineBasicBlock*, 16> WorkList(1, KillMBB); 109 110 // Remember if we have seen more than one value. 111 bool UniqueVNI = true; 112 VNInfo *TheVNI = 0; 113 114 // Using Seen as a visited set, perform a BFS for all reaching defs. 115 for (unsigned i = 0; i != WorkList.size(); ++i) { 116 MachineBasicBlock *MBB = WorkList[i]; 117 assert(!MBB->pred_empty() && "Value live-in to entry block?"); 118 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), 119 PE = MBB->pred_end(); PI != PE; ++PI) { 120 MachineBasicBlock *Pred = *PI; 121 122 // Is this a known live-out block? 123 if (Seen.test(Pred->getNumber())) { 124 if (VNInfo *VNI = LiveOut[Pred].first) { 125 if (TheVNI && TheVNI != VNI) 126 UniqueVNI = false; 127 TheVNI = VNI; 128 } 129 continue; 130 } 131 132 SlotIndex Start, End; 133 tie(Start, End) = Indexes->getMBBRange(Pred); 134 135 // First time we see Pred. Try to determine the live-out value, but set 136 // it as null if Pred is live-through with an unknown value. 137 VNInfo *VNI = LI->extendInBlock(Start, End.getPrevSlot()); 138 setLiveOutValue(Pred, VNI); 139 if (VNI) { 140 if (TheVNI && TheVNI != VNI) 141 UniqueVNI = false; 142 TheVNI = VNI; 143 continue; 144 } 145 146 // No, we need a live-in value for Pred as well 147 if (Pred != KillMBB) 148 WorkList.push_back(Pred); 149 else 150 // Loopback to KillMBB, so value is really live through. 151 Kill = SlotIndex(); 152 } 153 } 154 155 // Transfer WorkList to LiveInBlocks in reverse order. 156 // This ordering works best with updateSSA(). 157 LiveIn.clear(); 158 LiveIn.reserve(WorkList.size()); 159 while(!WorkList.empty()) 160 addLiveInBlock(LI, DomTree->getNode(WorkList.pop_back_val())); 161 162 // The kill block may not be live-through. 163 assert(LiveIn.back().DomNode->getBlock() == KillMBB); 164 LiveIn.back().Kill = Kill; 165 166 return UniqueVNI ? TheVNI : 0; 167} 168 169 170// This is essentially the same iterative algorithm that SSAUpdater uses, 171// except we already have a dominator tree, so we don't have to recompute it. 172void LiveRangeCalc::updateSSA(SlotIndexes *Indexes, 173 MachineDominatorTree *DomTree, 174 VNInfo::Allocator *Alloc) { 175 assert(Indexes && "Missing SlotIndexes"); 176 assert(DomTree && "Missing dominator tree"); 177 178 // Interate until convergence. 179 unsigned Changes; 180 do { 181 Changes = 0; 182 // Propagate live-out values down the dominator tree, inserting phi-defs 183 // when necessary. 184 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(), 185 E = LiveIn.end(); I != E; ++I) { 186 MachineDomTreeNode *Node = I->DomNode; 187 // Skip block if the live-in value has already been determined. 188 if (!Node) 189 continue; 190 MachineBasicBlock *MBB = Node->getBlock(); 191 MachineDomTreeNode *IDom = Node->getIDom(); 192 LiveOutPair IDomValue; 193 194 // We need a live-in value to a block with no immediate dominator? 195 // This is probably an unreachable block that has survived somehow. 196 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber()); 197 198 // IDom dominates all of our predecessors, but it may not be their 199 // immediate dominator. Check if any of them have live-out values that are 200 // properly dominated by IDom. If so, we need a phi-def here. 201 if (!needPHI) { 202 IDomValue = LiveOut[IDom->getBlock()]; 203 204 // Cache the DomTree node that defined the value. 205 if (IDomValue.first && !IDomValue.second) 206 LiveOut[IDom->getBlock()].second = IDomValue.second = 207 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def)); 208 209 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), 210 PE = MBB->pred_end(); PI != PE; ++PI) { 211 LiveOutPair &Value = LiveOut[*PI]; 212 if (!Value.first || Value.first == IDomValue.first) 213 continue; 214 215 // Cache the DomTree node that defined the value. 216 if (!Value.second) 217 Value.second = 218 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def)); 219 220 // This predecessor is carrying something other than IDomValue. 221 // It could be because IDomValue hasn't propagated yet, or it could be 222 // because MBB is in the dominance frontier of that value. 223 if (DomTree->dominates(IDom, Value.second)) { 224 needPHI = true; 225 break; 226 } 227 } 228 } 229 230 // The value may be live-through even if Kill is set, as can happen when 231 // we are called from extendRange. In that case LiveOutSeen is true, and 232 // LiveOut indicates a foreign or missing value. 233 LiveOutPair &LOP = LiveOut[MBB]; 234 235 // Create a phi-def if required. 236 if (needPHI) { 237 ++Changes; 238 assert(Alloc && "Need VNInfo allocator to create PHI-defs"); 239 SlotIndex Start, End; 240 tie(Start, End) = Indexes->getMBBRange(MBB); 241 VNInfo *VNI = I->LI->getNextValue(Start, 0, *Alloc); 242 VNI->setIsPHIDef(true); 243 I->Value = VNI; 244 // This block is done, we know the final value. 245 I->DomNode = 0; 246 247 // Add liveness since updateLiveIns now skips this node. 248 if (I->Kill.isValid()) 249 I->LI->addRange(LiveRange(Start, I->Kill, VNI)); 250 else { 251 I->LI->addRange(LiveRange(Start, End, VNI)); 252 LOP = LiveOutPair(VNI, Node); 253 } 254 } else if (IDomValue.first) { 255 // No phi-def here. Remember incoming value. 256 I->Value = IDomValue.first; 257 258 // If the IDomValue is killed in the block, don't propagate through. 259 if (I->Kill.isValid()) 260 continue; 261 262 // Propagate IDomValue if it isn't killed: 263 // MBB is live-out and doesn't define its own value. 264 if (LOP.first == IDomValue.first) 265 continue; 266 ++Changes; 267 LOP = IDomValue; 268 } 269 } 270 } while (Changes); 271} 272