Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 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" |
Jakob Stoklund Olesen | 4e53a40 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 21 | void LiveRangeCalc::reset(const MachineFunction *mf, |
Jakob Stoklund Olesen | 631390e | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 22 | SlotIndexes *SI, |
| 23 | MachineDominatorTree *MDT, |
| 24 | VNInfo::Allocator *VNIA) { |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 25 | MF = mf; |
Jakob Stoklund Olesen | 631390e | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 26 | MRI = &MF->getRegInfo(); |
| 27 | Indexes = SI; |
| 28 | DomTree = MDT; |
| 29 | Alloc = VNIA; |
| 30 | |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 31 | unsigned N = MF->getNumBlockIDs(); |
| 32 | Seen.clear(); |
| 33 | Seen.resize(N); |
| 34 | LiveOut.resize(N); |
| 35 | LiveIn.clear(); |
| 36 | } |
| 37 | |
| 38 | |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 39 | void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) { |
Jakob Stoklund Olesen | 4e53a40 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 40 | assert(MRI && Indexes && "call reset() first"); |
| 41 | |
| 42 | // Visit all def operands. If the same instruction has multiple defs of Reg, |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 43 | // LR.createDeadDef() will deduplicate. |
Jakob Stoklund Olesen | 4e53a40 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 44 | for (MachineRegisterInfo::def_iterator |
| 45 | I = MRI->def_begin(Reg), E = MRI->def_end(); I != E; ++I) { |
| 46 | const MachineInstr *MI = &*I; |
| 47 | // Find the corresponding slot index. |
| 48 | SlotIndex Idx; |
| 49 | if (MI->isPHI()) |
| 50 | // PHI defs begin at the basic block start index. |
| 51 | Idx = Indexes->getMBBStartIdx(MI->getParent()); |
| 52 | else |
| 53 | // Instructions are either normal 'r', or early clobber 'e'. |
| 54 | Idx = Indexes->getInstructionIndex(MI) |
| 55 | .getRegSlot(I.getOperand().isEarlyClobber()); |
| 56 | |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 57 | // Create the def in LR. This may find an existing def. |
| 58 | LR.createDeadDef(Idx, *Alloc); |
Jakob Stoklund Olesen | 4e53a40 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 63 | void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg) { |
Jakob Stoklund Olesen | 4e53a40 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 64 | assert(MRI && Indexes && "call reset() first"); |
| 65 | |
| 66 | // Visit all operands that read Reg. This may include partial defs. |
| 67 | for (MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(Reg), |
| 68 | E = MRI->reg_nodbg_end(); I != E; ++I) { |
Jakob Stoklund Olesen | f9dff0e | 2012-09-06 18:15:15 +0000 | [diff] [blame] | 69 | MachineOperand &MO = I.getOperand(); |
| 70 | // Clear all kill flags. They will be reinserted after register allocation |
| 71 | // by LiveIntervalAnalysis::addKillFlags(). |
| 72 | if (MO.isUse()) |
| 73 | MO.setIsKill(false); |
Jakob Stoklund Olesen | 4e53a40 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 74 | if (!MO.readsReg()) |
| 75 | continue; |
| 76 | // MI is reading Reg. We may have visited MI before if it happens to be |
| 77 | // reading Reg multiple times. That is OK, extend() is idempotent. |
| 78 | const MachineInstr *MI = &*I; |
| 79 | |
| 80 | // Find the SlotIndex being read. |
| 81 | SlotIndex Idx; |
| 82 | if (MI->isPHI()) { |
| 83 | assert(!MO.isDef() && "Cannot handle PHI def of partial register."); |
| 84 | // PHI operands are paired: (Reg, PredMBB). |
| 85 | // Extend the live range to be live-out from PredMBB. |
| 86 | Idx = Indexes->getMBBEndIdx(MI->getOperand(I.getOperandNo()+1).getMBB()); |
| 87 | } else { |
| 88 | // This is a normal instruction. |
| 89 | Idx = Indexes->getInstructionIndex(MI).getRegSlot(); |
| 90 | // Check for early-clobber redefs. |
| 91 | unsigned DefIdx; |
| 92 | if (MO.isDef()) { |
| 93 | if (MO.isEarlyClobber()) |
| 94 | Idx = Idx.getRegSlot(true); |
| 95 | } else if (MI->isRegTiedToDefOperand(I.getOperandNo(), &DefIdx)) { |
| 96 | // FIXME: This would be a lot easier if tied early-clobber uses also |
| 97 | // had an early-clobber flag. |
| 98 | if (MI->getOperand(DefIdx).isEarlyClobber()) |
| 99 | Idx = Idx.getRegSlot(true); |
| 100 | } |
| 101 | } |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 102 | extend(LR, Idx, Reg); |
Jakob Stoklund Olesen | 4e53a40 | 2012-06-05 21:54:09 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 107 | // Transfer information from the LiveIn vector to the live ranges. |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 108 | void LiveRangeCalc::updateLiveIns() { |
| 109 | LiveRangeUpdater Updater; |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 110 | for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(), |
| 111 | E = LiveIn.end(); I != E; ++I) { |
| 112 | if (!I->DomNode) |
| 113 | continue; |
| 114 | MachineBasicBlock *MBB = I->DomNode->getBlock(); |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 115 | assert(I->Value && "No live-in value found"); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 116 | SlotIndex Start, End; |
| 117 | tie(Start, End) = Indexes->getMBBRange(MBB); |
| 118 | |
| 119 | if (I->Kill.isValid()) |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 120 | // Value is killed inside this block. |
| 121 | End = I->Kill; |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 122 | else { |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 123 | // The value is live-through, update LiveOut as well. |
| 124 | // Defer the Domtree lookup until it is needed. |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 125 | assert(Seen.test(MBB->getNumber())); |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 126 | LiveOut[MBB] = LiveOutPair(I->Value, (MachineDomTreeNode *)0); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 127 | } |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 128 | Updater.setDest(&I->LR); |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 129 | Updater.add(Start, End, I->Value); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 130 | } |
| 131 | LiveIn.clear(); |
| 132 | } |
| 133 | |
| 134 | |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 135 | void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Kill, unsigned PhysReg) { |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 136 | assert(Kill.isValid() && "Invalid SlotIndex"); |
| 137 | assert(Indexes && "Missing SlotIndexes"); |
| 138 | assert(DomTree && "Missing dominator tree"); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 139 | |
Jakob Stoklund Olesen | ee5655d | 2011-09-13 16:47:56 +0000 | [diff] [blame] | 140 | MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill.getPrevSlot()); |
Lang Hames | aa13482 | 2011-12-20 20:23:40 +0000 | [diff] [blame] | 141 | assert(KillMBB && "No MBB at Kill"); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 142 | |
| 143 | // Is there a def in the same MBB we can extend? |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 144 | if (LR.extendInBlock(Indexes->getMBBStartIdx(KillMBB), Kill)) |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 145 | return; |
| 146 | |
| 147 | // Find the single reaching def, or determine if Kill is jointly dominated by |
| 148 | // multiple values, and we may need to create even more phi-defs to preserve |
| 149 | // VNInfo SSA form. Perform a search for all predecessor blocks where we |
| 150 | // know the dominating VNInfo. |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 151 | if (findReachingDefs(LR, *KillMBB, Kill, PhysReg)) |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 152 | return; |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 153 | |
| 154 | // When there were multiple different values, we may need new PHIs. |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 155 | calculateValues(); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
| 159 | // This function is called by a client after using the low-level API to add |
| 160 | // live-out and live-in blocks. The unique value optimization is not |
| 161 | // available, SplitEditor::transferValues handles that case directly anyway. |
Jakob Stoklund Olesen | 631390e | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 162 | void LiveRangeCalc::calculateValues() { |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 163 | assert(Indexes && "Missing SlotIndexes"); |
| 164 | assert(DomTree && "Missing dominator tree"); |
Jakob Stoklund Olesen | 631390e | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 165 | updateSSA(); |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 166 | updateLiveIns(); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 170 | bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &KillMBB, |
| 171 | SlotIndex Kill, unsigned PhysReg) { |
| 172 | unsigned KillMBBNum = KillMBB.getNumber(); |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 173 | |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 174 | // Block numbers where LR should be live-in. |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 175 | SmallVector<unsigned, 16> WorkList(1, KillMBBNum); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 176 | |
| 177 | // Remember if we have seen more than one value. |
| 178 | bool UniqueVNI = true; |
| 179 | VNInfo *TheVNI = 0; |
| 180 | |
| 181 | // Using Seen as a visited set, perform a BFS for all reaching defs. |
| 182 | for (unsigned i = 0; i != WorkList.size(); ++i) { |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 183 | MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]); |
Jakob Stoklund Olesen | c8981f2 | 2012-07-13 23:39:05 +0000 | [diff] [blame] | 184 | |
| 185 | #ifndef NDEBUG |
| 186 | if (MBB->pred_empty()) { |
| 187 | MBB->getParent()->verify(); |
| 188 | llvm_unreachable("Use not jointly dominated by defs."); |
| 189 | } |
| 190 | |
| 191 | if (TargetRegisterInfo::isPhysicalRegister(PhysReg) && |
| 192 | !MBB->isLiveIn(PhysReg)) { |
| 193 | MBB->getParent()->verify(); |
| 194 | errs() << "The register needs to be live in to BB#" << MBB->getNumber() |
| 195 | << ", but is missing from the live-in list.\n"; |
| 196 | llvm_unreachable("Invalid global physical register"); |
| 197 | } |
| 198 | #endif |
| 199 | |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 200 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 201 | PE = MBB->pred_end(); PI != PE; ++PI) { |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 202 | MachineBasicBlock *Pred = *PI; |
| 203 | |
| 204 | // Is this a known live-out block? |
| 205 | if (Seen.test(Pred->getNumber())) { |
| 206 | if (VNInfo *VNI = LiveOut[Pred].first) { |
| 207 | if (TheVNI && TheVNI != VNI) |
| 208 | UniqueVNI = false; |
| 209 | TheVNI = VNI; |
| 210 | } |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | SlotIndex Start, End; |
| 215 | tie(Start, End) = Indexes->getMBBRange(Pred); |
| 216 | |
| 217 | // First time we see Pred. Try to determine the live-out value, but set |
| 218 | // it as null if Pred is live-through with an unknown value. |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 219 | VNInfo *VNI = LR.extendInBlock(Start, End); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 220 | setLiveOutValue(Pred, VNI); |
| 221 | if (VNI) { |
| 222 | if (TheVNI && TheVNI != VNI) |
| 223 | UniqueVNI = false; |
| 224 | TheVNI = VNI; |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | // No, we need a live-in value for Pred as well |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 229 | if (Pred != &KillMBB) |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 230 | WorkList.push_back(Pred->getNumber()); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 231 | else |
| 232 | // Loopback to KillMBB, so value is really live through. |
| 233 | Kill = SlotIndex(); |
| 234 | } |
| 235 | } |
| 236 | |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 237 | LiveIn.clear(); |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 238 | |
| 239 | // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but |
| 240 | // neither require it. Skip the sorting overhead for small updates. |
| 241 | if (WorkList.size() > 4) |
| 242 | array_pod_sort(WorkList.begin(), WorkList.end()); |
| 243 | |
| 244 | // If a unique reaching def was found, blit in the live ranges immediately. |
| 245 | if (UniqueVNI) { |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 246 | LiveRangeUpdater Updater(&LR); |
| 247 | for (SmallVectorImpl<unsigned>::const_iterator I = WorkList.begin(), |
| 248 | E = WorkList.end(); I != E; ++I) { |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 249 | SlotIndex Start, End; |
| 250 | tie(Start, End) = Indexes->getMBBRange(*I); |
| 251 | // Trim the live range in KillMBB. |
| 252 | if (*I == KillMBBNum && Kill.isValid()) |
| 253 | End = Kill; |
| 254 | else |
| 255 | LiveOut[MF->getBlockNumbered(*I)] = |
| 256 | LiveOutPair(TheVNI, (MachineDomTreeNode *)0); |
| 257 | Updater.add(Start, End, TheVNI); |
| 258 | } |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | // Multiple values were found, so transfer the work list to the LiveIn array |
| 263 | // where UpdateSSA will use it as a work list. |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 264 | LiveIn.reserve(WorkList.size()); |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 265 | for (SmallVectorImpl<unsigned>::const_iterator |
| 266 | I = WorkList.begin(), E = WorkList.end(); I != E; ++I) { |
| 267 | MachineBasicBlock *MBB = MF->getBlockNumbered(*I); |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 268 | addLiveInBlock(LR, DomTree->getNode(MBB)); |
| 269 | if (MBB == &KillMBB) |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 270 | LiveIn.back().Kill = Kill; |
| 271 | } |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 272 | |
Jakob Stoklund Olesen | beda6ab | 2013-02-20 23:08:26 +0000 | [diff] [blame] | 273 | return false; |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | |
| 277 | // This is essentially the same iterative algorithm that SSAUpdater uses, |
| 278 | // except we already have a dominator tree, so we don't have to recompute it. |
Jakob Stoklund Olesen | 631390e | 2012-06-04 18:21:16 +0000 | [diff] [blame] | 279 | void LiveRangeCalc::updateSSA() { |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 280 | assert(Indexes && "Missing SlotIndexes"); |
| 281 | assert(DomTree && "Missing dominator tree"); |
| 282 | |
| 283 | // Interate until convergence. |
| 284 | unsigned Changes; |
| 285 | do { |
| 286 | Changes = 0; |
| 287 | // Propagate live-out values down the dominator tree, inserting phi-defs |
| 288 | // when necessary. |
| 289 | for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(), |
| 290 | E = LiveIn.end(); I != E; ++I) { |
| 291 | MachineDomTreeNode *Node = I->DomNode; |
| 292 | // Skip block if the live-in value has already been determined. |
| 293 | if (!Node) |
| 294 | continue; |
| 295 | MachineBasicBlock *MBB = Node->getBlock(); |
| 296 | MachineDomTreeNode *IDom = Node->getIDom(); |
| 297 | LiveOutPair IDomValue; |
| 298 | |
| 299 | // We need a live-in value to a block with no immediate dominator? |
| 300 | // This is probably an unreachable block that has survived somehow. |
| 301 | bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber()); |
| 302 | |
| 303 | // IDom dominates all of our predecessors, but it may not be their |
| 304 | // immediate dominator. Check if any of them have live-out values that are |
| 305 | // properly dominated by IDom. If so, we need a phi-def here. |
| 306 | if (!needPHI) { |
| 307 | IDomValue = LiveOut[IDom->getBlock()]; |
| 308 | |
| 309 | // Cache the DomTree node that defined the value. |
| 310 | if (IDomValue.first && !IDomValue.second) |
| 311 | LiveOut[IDom->getBlock()].second = IDomValue.second = |
| 312 | DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def)); |
| 313 | |
| 314 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 315 | PE = MBB->pred_end(); PI != PE; ++PI) { |
| 316 | LiveOutPair &Value = LiveOut[*PI]; |
| 317 | if (!Value.first || Value.first == IDomValue.first) |
| 318 | continue; |
| 319 | |
| 320 | // Cache the DomTree node that defined the value. |
| 321 | if (!Value.second) |
| 322 | Value.second = |
| 323 | DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def)); |
| 324 | |
| 325 | // This predecessor is carrying something other than IDomValue. |
| 326 | // It could be because IDomValue hasn't propagated yet, or it could be |
| 327 | // because MBB is in the dominance frontier of that value. |
| 328 | if (DomTree->dominates(IDom, Value.second)) { |
| 329 | needPHI = true; |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // The value may be live-through even if Kill is set, as can happen when |
| 336 | // we are called from extendRange. In that case LiveOutSeen is true, and |
| 337 | // LiveOut indicates a foreign or missing value. |
| 338 | LiveOutPair &LOP = LiveOut[MBB]; |
| 339 | |
| 340 | // Create a phi-def if required. |
| 341 | if (needPHI) { |
| 342 | ++Changes; |
| 343 | assert(Alloc && "Need VNInfo allocator to create PHI-defs"); |
| 344 | SlotIndex Start, End; |
| 345 | tie(Start, End) = Indexes->getMBBRange(MBB); |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 346 | LiveRange &LR = I->LR; |
| 347 | VNInfo *VNI = LR.getNextValue(Start, *Alloc); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 348 | I->Value = VNI; |
| 349 | // This block is done, we know the final value. |
| 350 | I->DomNode = 0; |
| 351 | |
| 352 | // Add liveness since updateLiveIns now skips this node. |
| 353 | if (I->Kill.isValid()) |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 354 | LR.addSegment(LiveInterval::Segment(Start, I->Kill, VNI)); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 355 | else { |
Matthias Braun | e25dde5 | 2013-10-10 21:28:57 +0000 | [diff] [blame^] | 356 | LR.addSegment(LiveInterval::Segment(Start, End, VNI)); |
Jakob Stoklund Olesen | b5a457c | 2011-09-13 01:34:21 +0000 | [diff] [blame] | 357 | LOP = LiveOutPair(VNI, Node); |
| 358 | } |
| 359 | } else if (IDomValue.first) { |
| 360 | // No phi-def here. Remember incoming value. |
| 361 | I->Value = IDomValue.first; |
| 362 | |
| 363 | // If the IDomValue is killed in the block, don't propagate through. |
| 364 | if (I->Kill.isValid()) |
| 365 | continue; |
| 366 | |
| 367 | // Propagate IDomValue if it isn't killed: |
| 368 | // MBB is live-out and doesn't define its own value. |
| 369 | if (LOP.first == IDomValue.first) |
| 370 | continue; |
| 371 | ++Changes; |
| 372 | LOP = IDomValue; |
| 373 | } |
| 374 | } |
| 375 | } while (Changes); |
| 376 | } |