blob: daa1ee05f6a38c53e22e7a2db20dca1f95337ade [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
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000022void LiveRangeCalc::reset(const MachineFunction *mf,
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +000023 SlotIndexes *SI,
24 MachineDominatorTree *MDT,
25 VNInfo::Allocator *VNIA) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000026 MF = mf;
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +000027 MRI = &MF->getRegInfo();
28 Indexes = SI;
29 DomTree = MDT;
30 Alloc = VNIA;
31
Matthias Braun2f662322014-12-10 01:12:12 +000032 MainLiveOutData.reset(MF->getNumBlockIDs());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000033 LiveIn.clear();
34}
35
36
Matthias Braun2f662322014-12-10 01:12:12 +000037static SlotIndex getDefIndex(const SlotIndexes &Indexes, const MachineInstr &MI,
38 bool EarlyClobber) {
39 // PHI defs begin at the basic block start index.
40 if (MI.isPHI())
41 return Indexes.getMBBStartIdx(MI.getParent());
42
43 // Instructions are either normal 'r', or early clobber 'e'.
44 return Indexes.getInstructionIndex(&MI).getRegSlot(EarlyClobber);
45}
46
47void LiveRangeCalc::createDeadDefs(LiveInterval &LI) {
48 assert(MRI && Indexes && "call reset() first");
49
50 // Visit all def operands. If the same instruction has multiple defs of Reg,
51 // LR.createDeadDef() will deduplicate.
52 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
53 unsigned Reg = LI.reg;
54 for (const MachineOperand &MO : MRI->def_operands(Reg)) {
55 const MachineInstr *MI = MO.getParent();
56 SlotIndex Idx = getDefIndex(*Indexes, *MI, MO.isEarlyClobber());
57 unsigned SubReg = MO.getSubReg();
Matthias Braune3d3b882014-12-10 01:12:30 +000058 if (LI.hasSubRanges() || (SubReg != 0 && MRI->tracksSubRegLiveness())) {
Matthias Braun2f662322014-12-10 01:12:12 +000059 unsigned Mask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg)
60 : MRI->getMaxLaneMaskForVReg(Reg);
61
62 // If this is the first time we see a subregister def, initialize
63 // subranges by creating a copy of the main range.
64 if (!LI.hasSubRanges() && !LI.empty()) {
65 unsigned ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
66 LI.createSubRangeFrom(*Alloc, ClassMask, LI);
67 }
68
69 for (LiveInterval::subrange_iterator S = LI.subrange_begin(),
70 SE = LI.subrange_end(); S != SE; ++S) {
71 // A Mask for subregs common to the existing subrange and current def.
72 unsigned Common = S->LaneMask & Mask;
73 if (Common == 0)
74 continue;
75 // A Mask for subregs covered by the subrange but not the current def.
76 unsigned LRest = S->LaneMask & ~Mask;
77 LiveInterval::SubRange *CommonRange;
78 if (LRest != 0) {
79 // Split current subrange into Common and LRest ranges.
80 S->LaneMask = LRest;
81 CommonRange = LI.createSubRangeFrom(*Alloc, Common, *S);
82 } else {
83 assert(Common == S->LaneMask);
84 CommonRange = &*S;
85 }
86 CommonRange->createDeadDef(Idx, *Alloc);
87 Mask &= ~Common;
88 }
89 if (Mask != 0) {
90 LiveInterval::SubRange *SubRange = LI.createSubRange(*Alloc, Mask);
91 SubRange->createDeadDef(Idx, *Alloc);
92 }
93 }
94
95 // Create the def in LR. This may find an existing def.
96 LI.createDeadDef(Idx, *Alloc);
97 }
98}
99
100
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000101void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) {
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000102 assert(MRI && Indexes && "call reset() first");
103
104 // Visit all def operands. If the same instruction has multiple defs of Reg,
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000105 // LR.createDeadDef() will deduplicate.
Owen Andersonb36376e2014-03-17 19:36:09 +0000106 for (MachineOperand &MO : MRI->def_operands(Reg)) {
107 const MachineInstr *MI = MO.getParent();
Matthias Braun2f662322014-12-10 01:12:12 +0000108 SlotIndex Idx = getDefIndex(*Indexes, *MI, MO.isEarlyClobber());
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000109 // Create the def in LR. This may find an existing def.
110 LR.createDeadDef(Idx, *Alloc);
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000111 }
112}
113
114
Matthias Braun2f662322014-12-10 01:12:12 +0000115static SlotIndex getUseIndex(const SlotIndexes &Indexes,
116 const MachineOperand &MO) {
117 const MachineInstr *MI = MO.getParent();
118 unsigned OpNo = (&MO - &MI->getOperand(0));
119 if (MI->isPHI()) {
120 assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
121 // The actual place where a phi operand is used is the end of the pred MBB.
122 // PHI operands are paired: (Reg, PredMBB).
123 return Indexes.getMBBEndIdx(MI->getOperand(OpNo+1).getMBB());
124 }
125
126 // Check for early-clobber redefs.
127 bool isEarlyClobber = false;
128 unsigned DefIdx;
129 if (MO.isDef()) {
130 isEarlyClobber = MO.isEarlyClobber();
131 } else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
132 // FIXME: This would be a lot easier if tied early-clobber uses also
133 // had an early-clobber flag.
134 isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber();
135 }
136 return Indexes.getInstructionIndex(MI).getRegSlot(isEarlyClobber);
137}
138
139
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000140void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg) {
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000141 assert(MRI && Indexes && "call reset() first");
142
143 // Visit all operands that read Reg. This may include partial defs.
Owen Andersonb36376e2014-03-17 19:36:09 +0000144 for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
Jakob Stoklund Olesen4aed4702012-09-06 18:15:15 +0000145 // Clear all kill flags. They will be reinserted after register allocation
146 // by LiveIntervalAnalysis::addKillFlags().
147 if (MO.isUse())
148 MO.setIsKill(false);
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000149 if (!MO.readsReg())
150 continue;
151 // MI is reading Reg. We may have visited MI before if it happens to be
152 // reading Reg multiple times. That is OK, extend() is idempotent.
Matthias Braun2f662322014-12-10 01:12:12 +0000153 SlotIndex Idx = getUseIndex(*Indexes, MO);
154 extend(LR, Idx, Reg, MainLiveOutData);
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000155 }
156}
157
158
Matthias Braun2f662322014-12-10 01:12:12 +0000159void LiveRangeCalc::extendToUses(LiveInterval &LI) {
160 assert(MRI && Indexes && "call reset() first");
161
162 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
163 SmallVector<LiveOutData,2> LiveOuts;
164 unsigned NumSubRanges = 0;
165 for (LiveInterval::subrange_iterator S = LI.subrange_begin(),
166 SE = LI.subrange_end(); S != SE; ++S, ++NumSubRanges) {
167 LiveOuts.push_back(LiveOutData());
168 LiveOuts.back().reset(MF->getNumBlockIDs());
169 }
170
171 // Visit all operands that read Reg. This may include partial defs.
172 unsigned Reg = LI.reg;
173 for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
174 // Clear all kill flags. They will be reinserted after register allocation
175 // by LiveIntervalAnalysis::addKillFlags().
176 if (MO.isUse())
177 MO.setIsKill(false);
178 if (!MO.readsReg())
179 continue;
180 SlotIndex Idx = getUseIndex(*Indexes, MO);
181 unsigned SubReg = MO.getSubReg();
Matthias Braune3d3b882014-12-10 01:12:30 +0000182 if (MO.isUse() && (LI.hasSubRanges() ||
183 (MRI->tracksSubRegLiveness() && SubReg != 0))) {
Matthias Braun2f662322014-12-10 01:12:12 +0000184 unsigned Mask = SubReg != 0
185 ? TRI.getSubRegIndexLaneMask(SubReg)
186 : Mask = MRI->getMaxLaneMaskForVReg(Reg);
187
188 // If this is the first time we see a subregister def/use. Initialize
189 // subranges by creating a copy of the main range.
190 if (!LI.hasSubRanges()) {
191 unsigned ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
192 LI.createSubRangeFrom(*Alloc, ClassMask, LI);
193 LiveOuts.insert(LiveOuts.begin(), LiveOutData());
194 LiveOuts.front().reset(MF->getNumBlockIDs());
195 ++NumSubRanges;
196 }
197 unsigned SubRangeIdx = 0;
198 for (LiveInterval::subrange_iterator S = LI.subrange_begin(),
199 SE = LI.subrange_end(); S != SE; ++S, ++SubRangeIdx) {
200 // A Mask for subregs common to the existing subrange and current def.
201 unsigned Common = S->LaneMask & Mask;
202 if (Common == 0)
203 continue;
204 // A Mask for subregs covered by the subrange but not the current def.
205 unsigned LRest = S->LaneMask & ~Mask;
206 LiveInterval::SubRange *CommonRange;
207 unsigned CommonRangeIdx;
208 if (LRest != 0) {
209 // Split current subrange into Common and LRest ranges.
210 S->LaneMask = LRest;
211 CommonRange = LI.createSubRangeFrom(*Alloc, Common, *S);
212 CommonRangeIdx = 0;
213 LiveOuts.insert(LiveOuts.begin(), LiveOutData());
214 LiveOuts.front().reset(MF->getNumBlockIDs());
215 ++NumSubRanges;
216 ++SubRangeIdx;
217 } else {
218 // The subrange and current def lanemasks match completely.
219 assert(Common == S->LaneMask);
220 CommonRange = &*S;
221 CommonRangeIdx = SubRangeIdx;
222 }
223 extend(*CommonRange, Idx, Reg, LiveOuts[CommonRangeIdx]);
224 Mask &= ~Common;
225 }
226 assert(SubRangeIdx == NumSubRanges);
227 }
228 extend(LI, Idx, Reg, MainLiveOutData);
229 }
230}
231
232
233void LiveRangeCalc::updateFromLiveIns(LiveOutData &LiveOuts) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000234 LiveRangeUpdater Updater;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000235 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
236 E = LiveIn.end(); I != E; ++I) {
237 if (!I->DomNode)
238 continue;
239 MachineBasicBlock *MBB = I->DomNode->getBlock();
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000240 assert(I->Value && "No live-in value found");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000241 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000242 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000243
244 if (I->Kill.isValid())
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000245 // Value is killed inside this block.
246 End = I->Kill;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000247 else {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000248 // The value is live-through, update LiveOut as well.
249 // Defer the Domtree lookup until it is needed.
Matthias Braun2f662322014-12-10 01:12:12 +0000250 assert(LiveOuts.Seen.test(MBB->getNumber()));
251 LiveOuts.Map[MBB] = LiveOutPair(I->Value, nullptr);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000252 }
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000253 Updater.setDest(&I->LR);
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000254 Updater.add(Start, End, I->Value);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000255 }
256 LiveIn.clear();
257}
258
259
Matthias Braun2f662322014-12-10 01:12:12 +0000260void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Kill, unsigned PhysReg,
261 LiveOutData &LiveOuts) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000262 assert(Kill.isValid() && "Invalid SlotIndex");
263 assert(Indexes && "Missing SlotIndexes");
264 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000265
Jakob Stoklund Olesen0494c5c2011-09-13 16:47:56 +0000266 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill.getPrevSlot());
Lang Hames6cee53d2011-12-20 20:23:40 +0000267 assert(KillMBB && "No MBB at Kill");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000268
269 // Is there a def in the same MBB we can extend?
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000270 if (LR.extendInBlock(Indexes->getMBBStartIdx(KillMBB), Kill))
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000271 return;
272
273 // Find the single reaching def, or determine if Kill is jointly dominated by
274 // multiple values, and we may need to create even more phi-defs to preserve
275 // VNInfo SSA form. Perform a search for all predecessor blocks where we
276 // know the dominating VNInfo.
Matthias Braun2f662322014-12-10 01:12:12 +0000277 if (findReachingDefs(LR, *KillMBB, Kill, PhysReg, LiveOuts))
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000278 return;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000279
280 // When there were multiple different values, we may need new PHIs.
Matthias Braun2f662322014-12-10 01:12:12 +0000281 calculateValues(LiveOuts);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000282}
283
284
285// This function is called by a client after using the low-level API to add
286// live-out and live-in blocks. The unique value optimization is not
287// available, SplitEditor::transferValues handles that case directly anyway.
Matthias Braun2f662322014-12-10 01:12:12 +0000288void LiveRangeCalc::calculateValues(LiveOutData &LiveOuts) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000289 assert(Indexes && "Missing SlotIndexes");
290 assert(DomTree && "Missing dominator tree");
Matthias Braun2f662322014-12-10 01:12:12 +0000291 updateSSA(LiveOuts);
292 updateFromLiveIns(LiveOuts);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000293}
294
295
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000296bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &KillMBB,
Matthias Braun2f662322014-12-10 01:12:12 +0000297 SlotIndex Kill, unsigned PhysReg,
298 LiveOutData &LiveOuts) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000299 unsigned KillMBBNum = KillMBB.getNumber();
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000300
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000301 // Block numbers where LR should be live-in.
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000302 SmallVector<unsigned, 16> WorkList(1, KillMBBNum);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000303
304 // Remember if we have seen more than one value.
305 bool UniqueVNI = true;
Craig Topperc0196b12014-04-14 00:51:57 +0000306 VNInfo *TheVNI = nullptr;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000307
308 // Using Seen as a visited set, perform a BFS for all reaching defs.
309 for (unsigned i = 0; i != WorkList.size(); ++i) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000310 MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000311
312#ifndef NDEBUG
313 if (MBB->pred_empty()) {
314 MBB->getParent()->verify();
315 llvm_unreachable("Use not jointly dominated by defs.");
316 }
317
318 if (TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
319 !MBB->isLiveIn(PhysReg)) {
320 MBB->getParent()->verify();
321 errs() << "The register needs to be live in to BB#" << MBB->getNumber()
322 << ", but is missing from the live-in list.\n";
323 llvm_unreachable("Invalid global physical register");
324 }
325#endif
326
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000327 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000328 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000329 MachineBasicBlock *Pred = *PI;
330
331 // Is this a known live-out block?
Matthias Braun2f662322014-12-10 01:12:12 +0000332 if (LiveOuts.Seen.test(Pred->getNumber())) {
333 if (VNInfo *VNI = LiveOuts.Map[Pred].first) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000334 if (TheVNI && TheVNI != VNI)
335 UniqueVNI = false;
336 TheVNI = VNI;
337 }
338 continue;
339 }
340
341 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000342 std::tie(Start, End) = Indexes->getMBBRange(Pred);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000343
344 // First time we see Pred. Try to determine the live-out value, but set
345 // it as null if Pred is live-through with an unknown value.
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000346 VNInfo *VNI = LR.extendInBlock(Start, End);
Matthias Braun2f662322014-12-10 01:12:12 +0000347 LiveOuts.setLiveOutValue(Pred, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000348 if (VNI) {
349 if (TheVNI && TheVNI != VNI)
350 UniqueVNI = false;
351 TheVNI = VNI;
352 continue;
353 }
354
355 // No, we need a live-in value for Pred as well
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000356 if (Pred != &KillMBB)
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000357 WorkList.push_back(Pred->getNumber());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000358 else
359 // Loopback to KillMBB, so value is really live through.
360 Kill = SlotIndex();
361 }
362 }
363
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000364 LiveIn.clear();
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000365
366 // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
367 // neither require it. Skip the sorting overhead for small updates.
368 if (WorkList.size() > 4)
369 array_pod_sort(WorkList.begin(), WorkList.end());
370
371 // If a unique reaching def was found, blit in the live ranges immediately.
372 if (UniqueVNI) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000373 LiveRangeUpdater Updater(&LR);
374 for (SmallVectorImpl<unsigned>::const_iterator I = WorkList.begin(),
375 E = WorkList.end(); I != E; ++I) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000376 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000377 std::tie(Start, End) = Indexes->getMBBRange(*I);
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000378 // Trim the live range in KillMBB.
379 if (*I == KillMBBNum && Kill.isValid())
380 End = Kill;
381 else
Matthias Braun2f662322014-12-10 01:12:12 +0000382 LiveOuts.Map[MF->getBlockNumbered(*I)] =
Craig Topperc0196b12014-04-14 00:51:57 +0000383 LiveOutPair(TheVNI, nullptr);
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000384 Updater.add(Start, End, TheVNI);
385 }
386 return true;
387 }
388
389 // Multiple values were found, so transfer the work list to the LiveIn array
390 // where UpdateSSA will use it as a work list.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000391 LiveIn.reserve(WorkList.size());
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000392 for (SmallVectorImpl<unsigned>::const_iterator
393 I = WorkList.begin(), E = WorkList.end(); I != E; ++I) {
394 MachineBasicBlock *MBB = MF->getBlockNumbered(*I);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000395 addLiveInBlock(LR, DomTree->getNode(MBB));
396 if (MBB == &KillMBB)
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000397 LiveIn.back().Kill = Kill;
398 }
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000399
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000400 return false;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000401}
402
403
404// This is essentially the same iterative algorithm that SSAUpdater uses,
405// except we already have a dominator tree, so we don't have to recompute it.
Matthias Braun2f662322014-12-10 01:12:12 +0000406void LiveRangeCalc::updateSSA(LiveOutData &LiveOuts) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000407 assert(Indexes && "Missing SlotIndexes");
408 assert(DomTree && "Missing dominator tree");
409
410 // Interate until convergence.
411 unsigned Changes;
412 do {
413 Changes = 0;
414 // Propagate live-out values down the dominator tree, inserting phi-defs
415 // when necessary.
416 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
417 E = LiveIn.end(); I != E; ++I) {
418 MachineDomTreeNode *Node = I->DomNode;
419 // Skip block if the live-in value has already been determined.
420 if (!Node)
421 continue;
422 MachineBasicBlock *MBB = Node->getBlock();
423 MachineDomTreeNode *IDom = Node->getIDom();
424 LiveOutPair IDomValue;
425
426 // We need a live-in value to a block with no immediate dominator?
427 // This is probably an unreachable block that has survived somehow.
Matthias Braun2f662322014-12-10 01:12:12 +0000428 bool needPHI = !IDom
429 || !LiveOuts.Seen.test(IDom->getBlock()->getNumber());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000430
431 // IDom dominates all of our predecessors, but it may not be their
432 // immediate dominator. Check if any of them have live-out values that are
433 // properly dominated by IDom. If so, we need a phi-def here.
434 if (!needPHI) {
Matthias Braun2f662322014-12-10 01:12:12 +0000435 IDomValue = LiveOuts.Map[IDom->getBlock()];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000436
437 // Cache the DomTree node that defined the value.
438 if (IDomValue.first && !IDomValue.second)
Matthias Braun2f662322014-12-10 01:12:12 +0000439 LiveOuts.Map[IDom->getBlock()].second = IDomValue.second =
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000440 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
441
442 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
443 PE = MBB->pred_end(); PI != PE; ++PI) {
Matthias Braun2f662322014-12-10 01:12:12 +0000444 LiveOutPair &Value = LiveOuts.Map[*PI];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000445 if (!Value.first || Value.first == IDomValue.first)
446 continue;
447
448 // Cache the DomTree node that defined the value.
449 if (!Value.second)
450 Value.second =
451 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
452
453 // This predecessor is carrying something other than IDomValue.
454 // It could be because IDomValue hasn't propagated yet, or it could be
455 // because MBB is in the dominance frontier of that value.
456 if (DomTree->dominates(IDom, Value.second)) {
457 needPHI = true;
458 break;
459 }
460 }
461 }
462
463 // The value may be live-through even if Kill is set, as can happen when
464 // we are called from extendRange. In that case LiveOutSeen is true, and
465 // LiveOut indicates a foreign or missing value.
Matthias Braun2f662322014-12-10 01:12:12 +0000466 LiveOutPair &LOP = LiveOuts.Map[MBB];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000467
468 // Create a phi-def if required.
469 if (needPHI) {
470 ++Changes;
471 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
472 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000473 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000474 LiveRange &LR = I->LR;
475 VNInfo *VNI = LR.getNextValue(Start, *Alloc);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000476 I->Value = VNI;
477 // This block is done, we know the final value.
Craig Topperc0196b12014-04-14 00:51:57 +0000478 I->DomNode = nullptr;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000479
Matthias Braun2f662322014-12-10 01:12:12 +0000480 // Add liveness since updateFromLiveIns now skips this node.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000481 if (I->Kill.isValid())
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000482 LR.addSegment(LiveInterval::Segment(Start, I->Kill, VNI));
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000483 else {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000484 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000485 LOP = LiveOutPair(VNI, Node);
486 }
487 } else if (IDomValue.first) {
488 // No phi-def here. Remember incoming value.
489 I->Value = IDomValue.first;
490
491 // If the IDomValue is killed in the block, don't propagate through.
492 if (I->Kill.isValid())
493 continue;
494
495 // Propagate IDomValue if it isn't killed:
496 // MBB is live-out and doesn't define its own value.
497 if (LOP.first == IDomValue.first)
498 continue;
499 ++Changes;
500 LOP = IDomValue;
501 }
502 }
503 } while (Changes);
504}