blob: e449b240d706542dc183fe548639a15daee3716f [file] [log] [blame]
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001//===---- 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
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000014#include "LiveRangeCalc.h"
15#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +000016#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000017
18using namespace llvm;
19
Chandler Carruth1b9dde02014-04-22 02:02:50 +000020#define DEBUG_TYPE "regalloc"
21
Matthias Braun1aed6ff2014-12-16 04:03:38 +000022void LiveRangeCalc::resetLiveOutMap() {
23 unsigned NumBlocks = MF->getNumBlockIDs();
24 Seen.clear();
25 Seen.resize(NumBlocks);
26 Map.resize(NumBlocks);
27}
28
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000029void LiveRangeCalc::reset(const MachineFunction *mf,
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +000030 SlotIndexes *SI,
31 MachineDominatorTree *MDT,
32 VNInfo::Allocator *VNIA) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000033 MF = mf;
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +000034 MRI = &MF->getRegInfo();
35 Indexes = SI;
36 DomTree = MDT;
37 Alloc = VNIA;
Matthias Braun1aed6ff2014-12-16 04:03:38 +000038 resetLiveOutMap();
Matthias Braunc3a72c22014-12-15 21:36:35 +000039 LiveIn.clear();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000040}
41
42
Matthias Braun1aed6ff2014-12-16 04:03:38 +000043static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc,
44 LiveRange &LR, const MachineOperand &MO) {
45 const MachineInstr *MI = MO.getParent();
46 SlotIndex DefIdx;
47 if (MI->isPHI())
48 DefIdx = Indexes.getMBBStartIdx(MI->getParent());
49 else
50 DefIdx = Indexes.getInstructionIndex(MI).getRegSlot(MO.isEarlyClobber());
Matthias Braunc3a72c22014-12-15 21:36:35 +000051
Matthias Braun1aed6ff2014-12-16 04:03:38 +000052 // Create the def in LR. This may find an existing def.
53 LR.createDeadDef(DefIdx, Alloc);
Matthias Braun2f662322014-12-10 01:12:12 +000054}
55
Matthias Braun1aed6ff2014-12-16 04:03:38 +000056void LiveRangeCalc::calculate(LiveInterval &LI) {
Matthias Braun2f662322014-12-10 01:12:12 +000057 assert(MRI && Indexes && "call reset() first");
58
Matthias Braun1aed6ff2014-12-16 04:03:38 +000059 // Step 1: Create minimal live segments for every definition of Reg.
Matthias Braun2f662322014-12-10 01:12:12 +000060 // Visit all def operands. If the same instruction has multiple defs of Reg,
Matthias Braun1aed6ff2014-12-16 04:03:38 +000061 // createDeadDef() will deduplicate.
Matthias Braun2f662322014-12-10 01:12:12 +000062 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
63 unsigned Reg = LI.reg;
Matthias Braun1aed6ff2014-12-16 04:03:38 +000064 for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
65 if (!MO.isDef() && !MO.readsReg())
66 continue;
67
Matthias Braun2f662322014-12-10 01:12:12 +000068 unsigned SubReg = MO.getSubReg();
Matthias Braune3d3b882014-12-10 01:12:30 +000069 if (LI.hasSubRanges() || (SubReg != 0 && MRI->tracksSubRegLiveness())) {
Matthias Braun2f662322014-12-10 01:12:12 +000070 unsigned Mask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg)
71 : MRI->getMaxLaneMaskForVReg(Reg);
72
73 // If this is the first time we see a subregister def, initialize
74 // subranges by creating a copy of the main range.
75 if (!LI.hasSubRanges() && !LI.empty()) {
76 unsigned ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
77 LI.createSubRangeFrom(*Alloc, ClassMask, LI);
78 }
79
Matthias Braun09afa1e2014-12-11 00:59:06 +000080 for (LiveInterval::SubRange &S : LI.subranges()) {
Matthias Braun2f662322014-12-10 01:12:12 +000081 // A Mask for subregs common to the existing subrange and current def.
Matthias Braun09afa1e2014-12-11 00:59:06 +000082 unsigned Common = S.LaneMask & Mask;
Matthias Braun2f662322014-12-10 01:12:12 +000083 if (Common == 0)
84 continue;
85 // A Mask for subregs covered by the subrange but not the current def.
Matthias Braun09afa1e2014-12-11 00:59:06 +000086 unsigned LRest = S.LaneMask & ~Mask;
Matthias Braun2f662322014-12-10 01:12:12 +000087 LiveInterval::SubRange *CommonRange;
88 if (LRest != 0) {
89 // Split current subrange into Common and LRest ranges.
Matthias Braun09afa1e2014-12-11 00:59:06 +000090 S.LaneMask = LRest;
91 CommonRange = LI.createSubRangeFrom(*Alloc, Common, S);
Matthias Braun2f662322014-12-10 01:12:12 +000092 } else {
Matthias Braun09afa1e2014-12-11 00:59:06 +000093 assert(Common == S.LaneMask);
94 CommonRange = &S;
Matthias Braun2f662322014-12-10 01:12:12 +000095 }
Matthias Braun1aed6ff2014-12-16 04:03:38 +000096 if (MO.isDef())
97 createDeadDef(*Indexes, *Alloc, *CommonRange, MO);
Matthias Braun2f662322014-12-10 01:12:12 +000098 Mask &= ~Common;
99 }
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000100 // Create a new SubRange for subregs we did not cover yet.
Matthias Braun2f662322014-12-10 01:12:12 +0000101 if (Mask != 0) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000102 LiveInterval::SubRange *NewRange = LI.createSubRange(*Alloc, Mask);
103 if (MO.isDef())
104 createDeadDef(*Indexes, *Alloc, *NewRange, MO);
Matthias Braun2f662322014-12-10 01:12:12 +0000105 }
106 }
107
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000108 // Create the def in the main liverange.
109 if (MO.isDef())
110 createDeadDef(*Indexes, *Alloc, LI, MO);
Matthias Braun2f662322014-12-10 01:12:12 +0000111 }
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000112
113 // We may have created empty live ranges for partially undefined uses, we
114 // can't keep them because we won't find defs in them later.
115 LI.removeEmptySubRanges();
116
117 // Step 2: Extend live segments to all uses, constructing SSA form as
118 // necessary.
119 for (LiveInterval::SubRange &S : LI.subranges()) {
120 resetLiveOutMap();
121 extendToUses(S, Reg, S.LaneMask);
122 }
123
124 resetLiveOutMap();
125 extendToUses(LI, Reg, ~0u);
Matthias Braun2f662322014-12-10 01:12:12 +0000126}
127
128
Matthias Braunc3a72c22014-12-15 21:36:35 +0000129void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) {
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000130 assert(MRI && Indexes && "call reset() first");
131
132 // Visit all def operands. If the same instruction has multiple defs of Reg,
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000133 // LR.createDeadDef() will deduplicate.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000134 for (MachineOperand &MO : MRI->def_operands(Reg))
135 createDeadDef(*Indexes, *Alloc, LR, MO);
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000136}
137
138
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000139void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg, unsigned Mask) {
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000140 // Visit all operands that read Reg. This may include partial defs.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000141 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +0000142 for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
Jakob Stoklund Olesen4aed4702012-09-06 18:15:15 +0000143 // Clear all kill flags. They will be reinserted after register allocation
144 // by LiveIntervalAnalysis::addKillFlags().
145 if (MO.isUse())
146 MO.setIsKill(false);
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000147 else {
148 // We only care about uses, but on the main range (mask ~0u) this includes
149 // the "virtual" reads happening for subregister defs.
150 if (Mask != ~0u)
151 continue;
152 }
153
Matthias Braunc3a72c22014-12-15 21:36:35 +0000154 if (!MO.readsReg())
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000155 continue;
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000156 unsigned SubReg = MO.getSubReg();
157 if (SubReg != 0) {
158 unsigned SubRegMask = TRI.getSubRegIndexLaneMask(SubReg);
159 // Ignore uses not covering the current subrange.
160 if ((SubRegMask & Mask) == 0)
161 continue;
162 }
163
164 // Determine the actual place of the use.
165 const MachineInstr *MI = MO.getParent();
166 unsigned OpNo = (&MO - &MI->getOperand(0));
167 SlotIndex UseIdx;
168 if (MI->isPHI()) {
169 assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
170 // The actual place where a phi operand is used is the end of the pred
171 // MBB. PHI operands are paired: (Reg, PredMBB).
172 UseIdx = Indexes->getMBBEndIdx(MI->getOperand(OpNo+1).getMBB());
173 } else {
174 // Check for early-clobber redefs.
175 bool isEarlyClobber = false;
176 unsigned DefIdx;
177 if (MO.isDef())
178 isEarlyClobber = MO.isEarlyClobber();
179 else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
180 // FIXME: This would be a lot easier if tied early-clobber uses also
181 // had an early-clobber flag.
182 isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber();
183 }
184 UseIdx = Indexes->getInstructionIndex(MI).getRegSlot(isEarlyClobber);
185 }
186
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000187 // MI is reading Reg. We may have visited MI before if it happens to be
188 // reading Reg multiple times. That is OK, extend() is idempotent.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000189 extend(LR, UseIdx, Reg);
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000190 }
191}
192
193
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000194void LiveRangeCalc::updateFromLiveIns() {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000195 LiveRangeUpdater Updater;
Matthias Braun42fab342014-12-15 19:40:46 +0000196 for (const LiveInBlock &I : LiveIn) {
197 if (!I.DomNode)
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000198 continue;
Matthias Braun42fab342014-12-15 19:40:46 +0000199 MachineBasicBlock *MBB = I.DomNode->getBlock();
200 assert(I.Value && "No live-in value found");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000201 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000202 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000203
Matthias Braun42fab342014-12-15 19:40:46 +0000204 if (I.Kill.isValid())
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000205 // Value is killed inside this block.
Matthias Braun42fab342014-12-15 19:40:46 +0000206 End = I.Kill;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000207 else {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000208 // The value is live-through, update LiveOut as well.
209 // Defer the Domtree lookup until it is needed.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000210 assert(Seen.test(MBB->getNumber()));
211 Map[MBB] = LiveOutPair(I.Value, nullptr);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000212 }
Matthias Braun42fab342014-12-15 19:40:46 +0000213 Updater.setDest(&I.LR);
214 Updater.add(Start, End, I.Value);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000215 }
216 LiveIn.clear();
217}
218
219
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000220void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Kill, unsigned PhysReg) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000221 assert(Kill.isValid() && "Invalid SlotIndex");
222 assert(Indexes && "Missing SlotIndexes");
223 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000224
Jakob Stoklund Olesen0494c5c2011-09-13 16:47:56 +0000225 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill.getPrevSlot());
Lang Hames6cee53d2011-12-20 20:23:40 +0000226 assert(KillMBB && "No MBB at Kill");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000227
228 // Is there a def in the same MBB we can extend?
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000229 if (LR.extendInBlock(Indexes->getMBBStartIdx(KillMBB), Kill))
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000230 return;
231
232 // Find the single reaching def, or determine if Kill is jointly dominated by
233 // multiple values, and we may need to create even more phi-defs to preserve
234 // VNInfo SSA form. Perform a search for all predecessor blocks where we
235 // know the dominating VNInfo.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000236 if (findReachingDefs(LR, *KillMBB, Kill, PhysReg))
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000237 return;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000238
239 // When there were multiple different values, we may need new PHIs.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000240 calculateValues();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000241}
242
243
244// This function is called by a client after using the low-level API to add
245// live-out and live-in blocks. The unique value optimization is not
246// available, SplitEditor::transferValues handles that case directly anyway.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000247void LiveRangeCalc::calculateValues() {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000248 assert(Indexes && "Missing SlotIndexes");
249 assert(DomTree && "Missing dominator tree");
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000250 updateSSA();
251 updateFromLiveIns();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000252}
253
254
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000255bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &KillMBB,
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000256 SlotIndex Kill, unsigned PhysReg) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000257 unsigned KillMBBNum = KillMBB.getNumber();
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000258
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000259 // Block numbers where LR should be live-in.
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000260 SmallVector<unsigned, 16> WorkList(1, KillMBBNum);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000261
262 // Remember if we have seen more than one value.
263 bool UniqueVNI = true;
Craig Topperc0196b12014-04-14 00:51:57 +0000264 VNInfo *TheVNI = nullptr;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000265
266 // Using Seen as a visited set, perform a BFS for all reaching defs.
267 for (unsigned i = 0; i != WorkList.size(); ++i) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000268 MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000269
270#ifndef NDEBUG
271 if (MBB->pred_empty()) {
272 MBB->getParent()->verify();
273 llvm_unreachable("Use not jointly dominated by defs.");
274 }
275
276 if (TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
277 !MBB->isLiveIn(PhysReg)) {
278 MBB->getParent()->verify();
279 errs() << "The register needs to be live in to BB#" << MBB->getNumber()
280 << ", but is missing from the live-in list.\n";
281 llvm_unreachable("Invalid global physical register");
282 }
283#endif
284
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000285 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000286 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000287 MachineBasicBlock *Pred = *PI;
288
289 // Is this a known live-out block?
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000290 if (Seen.test(Pred->getNumber())) {
291 if (VNInfo *VNI = Map[Pred].first) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000292 if (TheVNI && TheVNI != VNI)
293 UniqueVNI = false;
294 TheVNI = VNI;
295 }
296 continue;
297 }
298
299 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000300 std::tie(Start, End) = Indexes->getMBBRange(Pred);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000301
302 // First time we see Pred. Try to determine the live-out value, but set
303 // it as null if Pred is live-through with an unknown value.
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000304 VNInfo *VNI = LR.extendInBlock(Start, End);
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000305 setLiveOutValue(Pred, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000306 if (VNI) {
307 if (TheVNI && TheVNI != VNI)
308 UniqueVNI = false;
309 TheVNI = VNI;
310 continue;
311 }
312
313 // No, we need a live-in value for Pred as well
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000314 if (Pred != &KillMBB)
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000315 WorkList.push_back(Pred->getNumber());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000316 else
317 // Loopback to KillMBB, so value is really live through.
318 Kill = SlotIndex();
319 }
320 }
321
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000322 LiveIn.clear();
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000323
324 // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
325 // neither require it. Skip the sorting overhead for small updates.
326 if (WorkList.size() > 4)
327 array_pod_sort(WorkList.begin(), WorkList.end());
328
329 // If a unique reaching def was found, blit in the live ranges immediately.
330 if (UniqueVNI) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000331 LiveRangeUpdater Updater(&LR);
332 for (SmallVectorImpl<unsigned>::const_iterator I = WorkList.begin(),
333 E = WorkList.end(); I != E; ++I) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000334 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000335 std::tie(Start, End) = Indexes->getMBBRange(*I);
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000336 // Trim the live range in KillMBB.
337 if (*I == KillMBBNum && Kill.isValid())
338 End = Kill;
339 else
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000340 Map[MF->getBlockNumbered(*I)] = LiveOutPair(TheVNI, nullptr);
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000341 Updater.add(Start, End, TheVNI);
342 }
343 return true;
344 }
345
346 // Multiple values were found, so transfer the work list to the LiveIn array
347 // where UpdateSSA will use it as a work list.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000348 LiveIn.reserve(WorkList.size());
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000349 for (SmallVectorImpl<unsigned>::const_iterator
350 I = WorkList.begin(), E = WorkList.end(); I != E; ++I) {
351 MachineBasicBlock *MBB = MF->getBlockNumbered(*I);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000352 addLiveInBlock(LR, DomTree->getNode(MBB));
353 if (MBB == &KillMBB)
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000354 LiveIn.back().Kill = Kill;
355 }
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000356
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000357 return false;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000358}
359
360
361// This is essentially the same iterative algorithm that SSAUpdater uses,
362// except we already have a dominator tree, so we don't have to recompute it.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000363void LiveRangeCalc::updateSSA() {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000364 assert(Indexes && "Missing SlotIndexes");
365 assert(DomTree && "Missing dominator tree");
366
367 // Interate until convergence.
368 unsigned Changes;
369 do {
370 Changes = 0;
371 // Propagate live-out values down the dominator tree, inserting phi-defs
372 // when necessary.
Matthias Braun42fab342014-12-15 19:40:46 +0000373 for (LiveInBlock &I : LiveIn) {
374 MachineDomTreeNode *Node = I.DomNode;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000375 // Skip block if the live-in value has already been determined.
376 if (!Node)
377 continue;
378 MachineBasicBlock *MBB = Node->getBlock();
379 MachineDomTreeNode *IDom = Node->getIDom();
380 LiveOutPair IDomValue;
381
382 // We need a live-in value to a block with no immediate dominator?
383 // This is probably an unreachable block that has survived somehow.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000384 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000385
386 // IDom dominates all of our predecessors, but it may not be their
387 // immediate dominator. Check if any of them have live-out values that are
388 // properly dominated by IDom. If so, we need a phi-def here.
389 if (!needPHI) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000390 IDomValue = Map[IDom->getBlock()];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000391
392 // Cache the DomTree node that defined the value.
393 if (IDomValue.first && !IDomValue.second)
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000394 Map[IDom->getBlock()].second = IDomValue.second =
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000395 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
396
397 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
398 PE = MBB->pred_end(); PI != PE; ++PI) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000399 LiveOutPair &Value = Map[*PI];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000400 if (!Value.first || Value.first == IDomValue.first)
401 continue;
402
403 // Cache the DomTree node that defined the value.
404 if (!Value.second)
405 Value.second =
406 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
407
408 // This predecessor is carrying something other than IDomValue.
409 // It could be because IDomValue hasn't propagated yet, or it could be
410 // because MBB is in the dominance frontier of that value.
411 if (DomTree->dominates(IDom, Value.second)) {
412 needPHI = true;
413 break;
414 }
415 }
416 }
417
418 // The value may be live-through even if Kill is set, as can happen when
419 // we are called from extendRange. In that case LiveOutSeen is true, and
420 // LiveOut indicates a foreign or missing value.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000421 LiveOutPair &LOP = Map[MBB];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000422
423 // Create a phi-def if required.
424 if (needPHI) {
425 ++Changes;
426 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
427 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000428 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Matthias Braun42fab342014-12-15 19:40:46 +0000429 LiveRange &LR = I.LR;
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000430 VNInfo *VNI = LR.getNextValue(Start, *Alloc);
Matthias Braun42fab342014-12-15 19:40:46 +0000431 I.Value = VNI;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000432 // This block is done, we know the final value.
Matthias Braun42fab342014-12-15 19:40:46 +0000433 I.DomNode = nullptr;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000434
Matthias Braun2f662322014-12-10 01:12:12 +0000435 // Add liveness since updateFromLiveIns now skips this node.
Matthias Braun42fab342014-12-15 19:40:46 +0000436 if (I.Kill.isValid())
437 LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI));
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000438 else {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000439 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000440 LOP = LiveOutPair(VNI, Node);
441 }
442 } else if (IDomValue.first) {
443 // No phi-def here. Remember incoming value.
Matthias Braun42fab342014-12-15 19:40:46 +0000444 I.Value = IDomValue.first;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000445
446 // If the IDomValue is killed in the block, don't propagate through.
Matthias Braun42fab342014-12-15 19:40:46 +0000447 if (I.Kill.isValid())
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000448 continue;
449
450 // Propagate IDomValue if it isn't killed:
451 // MBB is live-out and doesn't define its own value.
452 if (LOP.first == IDomValue.first)
453 continue;
454 ++Changes;
455 LOP = IDomValue;
456 }
457 }
458 } while (Changes);
459}