blob: db91ca113dc12c9fc4c189d701c34d0862f2c5f4 [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) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +000045 const MachineInstr &MI = *MO.getParent();
46 SlotIndex DefIdx =
47 Indexes.getInstructionIndex(MI).getRegSlot(MO.isEarlyClobber());
Matthias Braunc3a72c22014-12-15 21:36:35 +000048
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +000049 // Create the def in LR. This may find an existing def.
50 LR.createDeadDef(DefIdx, Alloc);
Matthias Braun2f662322014-12-10 01:12:12 +000051}
52
Matthias Brauna25e13a2015-03-19 00:21:58 +000053void LiveRangeCalc::calculate(LiveInterval &LI, bool TrackSubRegs) {
Matthias Braun2f662322014-12-10 01:12:12 +000054 assert(MRI && Indexes && "call reset() first");
55
Matthias Braun1aed6ff2014-12-16 04:03:38 +000056 // Step 1: Create minimal live segments for every definition of Reg.
Matthias Braun2f662322014-12-10 01:12:12 +000057 // Visit all def operands. If the same instruction has multiple defs of Reg,
Matthias Braun1aed6ff2014-12-16 04:03:38 +000058 // createDeadDef() will deduplicate.
Matthias Braun2f662322014-12-10 01:12:12 +000059 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
60 unsigned Reg = LI.reg;
Matthias Braun1aed6ff2014-12-16 04:03:38 +000061 for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
62 if (!MO.isDef() && !MO.readsReg())
63 continue;
64
Matthias Braun2f662322014-12-10 01:12:12 +000065 unsigned SubReg = MO.getSubReg();
Matthias Brauna25e13a2015-03-19 00:21:58 +000066 if (LI.hasSubRanges() || (SubReg != 0 && TrackSubRegs)) {
Matthias Braune6a24852015-09-25 21:51:14 +000067 LaneBitmask Mask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg)
68 : MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braun2f662322014-12-10 01:12:12 +000069
70 // If this is the first time we see a subregister def, initialize
71 // subranges by creating a copy of the main range.
72 if (!LI.hasSubRanges() && !LI.empty()) {
Matthias Braune6a24852015-09-25 21:51:14 +000073 LaneBitmask ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braun2f662322014-12-10 01:12:12 +000074 LI.createSubRangeFrom(*Alloc, ClassMask, LI);
75 }
76
Matthias Braun09afa1e2014-12-11 00:59:06 +000077 for (LiveInterval::SubRange &S : LI.subranges()) {
Matthias Braun2f662322014-12-10 01:12:12 +000078 // A Mask for subregs common to the existing subrange and current def.
Matthias Braune6a24852015-09-25 21:51:14 +000079 LaneBitmask Common = S.LaneMask & Mask;
Matthias Braun2f662322014-12-10 01:12:12 +000080 if (Common == 0)
81 continue;
82 // A Mask for subregs covered by the subrange but not the current def.
Matthias Braune6a24852015-09-25 21:51:14 +000083 LaneBitmask LRest = S.LaneMask & ~Mask;
Matthias Braun2f662322014-12-10 01:12:12 +000084 LiveInterval::SubRange *CommonRange;
85 if (LRest != 0) {
86 // Split current subrange into Common and LRest ranges.
Matthias Braun09afa1e2014-12-11 00:59:06 +000087 S.LaneMask = LRest;
88 CommonRange = LI.createSubRangeFrom(*Alloc, Common, S);
Matthias Braun2f662322014-12-10 01:12:12 +000089 } else {
Matthias Braun09afa1e2014-12-11 00:59:06 +000090 assert(Common == S.LaneMask);
91 CommonRange = &S;
Matthias Braun2f662322014-12-10 01:12:12 +000092 }
Matthias Braun1aed6ff2014-12-16 04:03:38 +000093 if (MO.isDef())
94 createDeadDef(*Indexes, *Alloc, *CommonRange, MO);
Matthias Braun2f662322014-12-10 01:12:12 +000095 Mask &= ~Common;
96 }
Matthias Braun1aed6ff2014-12-16 04:03:38 +000097 // Create a new SubRange for subregs we did not cover yet.
Matthias Braun2f662322014-12-10 01:12:12 +000098 if (Mask != 0) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +000099 LiveInterval::SubRange *NewRange = LI.createSubRange(*Alloc, Mask);
100 if (MO.isDef())
101 createDeadDef(*Indexes, *Alloc, *NewRange, MO);
Matthias Braun2f662322014-12-10 01:12:12 +0000102 }
103 }
104
Matthias Braundbcca0d2014-12-24 02:11:51 +0000105 // Create the def in the main liverange. We do not have to do this if
106 // subranges are tracked as we recreate the main range later in this case.
107 if (MO.isDef() && !LI.hasSubRanges())
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000108 createDeadDef(*Indexes, *Alloc, LI, MO);
Matthias Braun2f662322014-12-10 01:12:12 +0000109 }
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000110
111 // We may have created empty live ranges for partially undefined uses, we
112 // can't keep them because we won't find defs in them later.
113 LI.removeEmptySubRanges();
114
115 // Step 2: Extend live segments to all uses, constructing SSA form as
116 // necessary.
Matthias Braundbcca0d2014-12-24 02:11:51 +0000117 if (LI.hasSubRanges()) {
118 for (LiveInterval::SubRange &S : LI.subranges()) {
119 resetLiveOutMap();
120 extendToUses(S, Reg, S.LaneMask);
121 }
122 LI.clear();
Matthias Braun71f95642016-05-20 23:14:56 +0000123 constructMainRangeFromSubranges(LI);
Matthias Braundbcca0d2014-12-24 02:11:51 +0000124 } else {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000125 resetLiveOutMap();
Matthias Braundbcca0d2014-12-24 02:11:51 +0000126 extendToUses(LI, Reg, ~0u);
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000127 }
Matthias Braun2f662322014-12-10 01:12:12 +0000128}
129
Matthias Braun71f95642016-05-20 23:14:56 +0000130void LiveRangeCalc::constructMainRangeFromSubranges(LiveInterval &LI) {
131 // First create dead defs at all defs found in subranges.
132 LiveRange &MainRange = LI;
133 assert(MainRange.segments.empty() && MainRange.valnos.empty() &&
134 "Expect empty main liverange");
135
136 for (const LiveInterval::SubRange &SR : LI.subranges()) {
137 for (const VNInfo *VNI : SR.valnos) {
138 if (!VNI->isUnused() && !VNI->isPHIDef())
139 MainRange.createDeadDef(VNI->def, *Alloc);
140 }
141 }
142
143 resetLiveOutMap();
144 extendToUses(MainRange, LI.reg);
145}
Matthias Braun2f662322014-12-10 01:12:12 +0000146
Matthias Braunc3a72c22014-12-15 21:36:35 +0000147void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) {
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000148 assert(MRI && Indexes && "call reset() first");
149
150 // Visit all def operands. If the same instruction has multiple defs of Reg,
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000151 // LR.createDeadDef() will deduplicate.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000152 for (MachineOperand &MO : MRI->def_operands(Reg))
153 createDeadDef(*Indexes, *Alloc, LR, MO);
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000154}
155
156
Matthias Braune6a24852015-09-25 21:51:14 +0000157void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg,
158 LaneBitmask Mask) {
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000159 // Visit all operands that read Reg. This may include partial defs.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000160 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +0000161 for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
Jakob Stoklund Olesen4aed4702012-09-06 18:15:15 +0000162 // Clear all kill flags. They will be reinserted after register allocation
163 // by LiveIntervalAnalysis::addKillFlags().
164 if (MO.isUse())
165 MO.setIsKill(false);
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000166 else {
167 // We only care about uses, but on the main range (mask ~0u) this includes
168 // the "virtual" reads happening for subregister defs.
169 if (Mask != ~0u)
170 continue;
171 }
172
Matthias Braunc3a72c22014-12-15 21:36:35 +0000173 if (!MO.readsReg())
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000174 continue;
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000175 unsigned SubReg = MO.getSubReg();
176 if (SubReg != 0) {
Matthias Braune6a24852015-09-25 21:51:14 +0000177 LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(SubReg);
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000178 // Ignore uses not covering the current subrange.
179 if ((SubRegMask & Mask) == 0)
180 continue;
181 }
182
183 // Determine the actual place of the use.
184 const MachineInstr *MI = MO.getParent();
185 unsigned OpNo = (&MO - &MI->getOperand(0));
186 SlotIndex UseIdx;
187 if (MI->isPHI()) {
188 assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
189 // The actual place where a phi operand is used is the end of the pred
190 // MBB. PHI operands are paired: (Reg, PredMBB).
191 UseIdx = Indexes->getMBBEndIdx(MI->getOperand(OpNo+1).getMBB());
192 } else {
193 // Check for early-clobber redefs.
194 bool isEarlyClobber = false;
195 unsigned DefIdx;
196 if (MO.isDef())
197 isEarlyClobber = MO.isEarlyClobber();
198 else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
199 // FIXME: This would be a lot easier if tied early-clobber uses also
200 // had an early-clobber flag.
201 isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber();
202 }
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000203 UseIdx = Indexes->getInstructionIndex(*MI).getRegSlot(isEarlyClobber);
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000204 }
205
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000206 // MI is reading Reg. We may have visited MI before if it happens to be
207 // reading Reg multiple times. That is OK, extend() is idempotent.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000208 extend(LR, UseIdx, Reg);
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000209 }
210}
211
212
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000213void LiveRangeCalc::updateFromLiveIns() {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000214 LiveRangeUpdater Updater;
Matthias Braun42fab342014-12-15 19:40:46 +0000215 for (const LiveInBlock &I : LiveIn) {
216 if (!I.DomNode)
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000217 continue;
Matthias Braun42fab342014-12-15 19:40:46 +0000218 MachineBasicBlock *MBB = I.DomNode->getBlock();
219 assert(I.Value && "No live-in value found");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000220 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000221 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000222
Matthias Braun42fab342014-12-15 19:40:46 +0000223 if (I.Kill.isValid())
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000224 // Value is killed inside this block.
Matthias Braun42fab342014-12-15 19:40:46 +0000225 End = I.Kill;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000226 else {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000227 // The value is live-through, update LiveOut as well.
228 // Defer the Domtree lookup until it is needed.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000229 assert(Seen.test(MBB->getNumber()));
230 Map[MBB] = LiveOutPair(I.Value, nullptr);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000231 }
Matthias Braun42fab342014-12-15 19:40:46 +0000232 Updater.setDest(&I.LR);
233 Updater.add(Start, End, I.Value);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000234 }
235 LiveIn.clear();
236}
237
238
Matthias Braun11042c82015-02-18 01:50:52 +0000239void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg) {
240 assert(Use.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000241 assert(Indexes && "Missing SlotIndexes");
242 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000243
Matthias Braun11042c82015-02-18 01:50:52 +0000244 MachineBasicBlock *UseMBB = Indexes->getMBBFromIndex(Use.getPrevSlot());
245 assert(UseMBB && "No MBB at Use");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000246
247 // Is there a def in the same MBB we can extend?
Matthias Braun11042c82015-02-18 01:50:52 +0000248 if (LR.extendInBlock(Indexes->getMBBStartIdx(UseMBB), Use))
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000249 return;
250
Matthias Braun11042c82015-02-18 01:50:52 +0000251 // Find the single reaching def, or determine if Use is jointly dominated by
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000252 // multiple values, and we may need to create even more phi-defs to preserve
253 // VNInfo SSA form. Perform a search for all predecessor blocks where we
254 // know the dominating VNInfo.
Matthias Braun11042c82015-02-18 01:50:52 +0000255 if (findReachingDefs(LR, *UseMBB, Use, PhysReg))
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000256 return;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000257
258 // When there were multiple different values, we may need new PHIs.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000259 calculateValues();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000260}
261
262
263// This function is called by a client after using the low-level API to add
264// live-out and live-in blocks. The unique value optimization is not
265// available, SplitEditor::transferValues handles that case directly anyway.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000266void LiveRangeCalc::calculateValues() {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000267 assert(Indexes && "Missing SlotIndexes");
268 assert(DomTree && "Missing dominator tree");
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000269 updateSSA();
270 updateFromLiveIns();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000271}
272
273
Matthias Braun11042c82015-02-18 01:50:52 +0000274bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
275 SlotIndex Use, unsigned PhysReg) {
276 unsigned UseMBBNum = UseMBB.getNumber();
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000277
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000278 // Block numbers where LR should be live-in.
Matthias Braun11042c82015-02-18 01:50:52 +0000279 SmallVector<unsigned, 16> WorkList(1, UseMBBNum);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000280
281 // Remember if we have seen more than one value.
282 bool UniqueVNI = true;
Craig Topperc0196b12014-04-14 00:51:57 +0000283 VNInfo *TheVNI = nullptr;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000284
285 // Using Seen as a visited set, perform a BFS for all reaching defs.
286 for (unsigned i = 0; i != WorkList.size(); ++i) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000287 MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000288
289#ifndef NDEBUG
290 if (MBB->pred_empty()) {
291 MBB->getParent()->verify();
Matthias Braun53917542015-05-11 18:47:47 +0000292 errs() << "Use of " << PrintReg(PhysReg)
293 << " does not have a corresponding definition on every path:\n";
294 const MachineInstr *MI = Indexes->getInstructionFromIndex(Use);
295 if (MI != nullptr)
296 errs() << Use << " " << *MI;
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000297 llvm_unreachable("Use not jointly dominated by defs.");
298 }
299
300 if (TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
301 !MBB->isLiveIn(PhysReg)) {
302 MBB->getParent()->verify();
Matthias Braun53917542015-05-11 18:47:47 +0000303 errs() << "The register " << PrintReg(PhysReg)
304 << " needs to be live in to BB#" << MBB->getNumber()
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000305 << ", but is missing from the live-in list.\n";
306 llvm_unreachable("Invalid global physical register");
307 }
308#endif
309
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000310 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000311 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000312 MachineBasicBlock *Pred = *PI;
313
314 // Is this a known live-out block?
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000315 if (Seen.test(Pred->getNumber())) {
316 if (VNInfo *VNI = Map[Pred].first) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000317 if (TheVNI && TheVNI != VNI)
318 UniqueVNI = false;
319 TheVNI = VNI;
320 }
321 continue;
322 }
323
324 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000325 std::tie(Start, End) = Indexes->getMBBRange(Pred);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000326
327 // First time we see Pred. Try to determine the live-out value, but set
328 // it as null if Pred is live-through with an unknown value.
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000329 VNInfo *VNI = LR.extendInBlock(Start, End);
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000330 setLiveOutValue(Pred, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000331 if (VNI) {
332 if (TheVNI && TheVNI != VNI)
333 UniqueVNI = false;
334 TheVNI = VNI;
335 continue;
336 }
337
338 // No, we need a live-in value for Pred as well
Matthias Braun11042c82015-02-18 01:50:52 +0000339 if (Pred != &UseMBB)
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000340 WorkList.push_back(Pred->getNumber());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000341 else
Matthias Braun11042c82015-02-18 01:50:52 +0000342 // Loopback to UseMBB, so value is really live through.
343 Use = SlotIndex();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000344 }
345 }
346
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000347 LiveIn.clear();
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000348
349 // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
350 // neither require it. Skip the sorting overhead for small updates.
351 if (WorkList.size() > 4)
352 array_pod_sort(WorkList.begin(), WorkList.end());
353
354 // If a unique reaching def was found, blit in the live ranges immediately.
355 if (UniqueVNI) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000356 LiveRangeUpdater Updater(&LR);
357 for (SmallVectorImpl<unsigned>::const_iterator I = WorkList.begin(),
358 E = WorkList.end(); I != E; ++I) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000359 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000360 std::tie(Start, End) = Indexes->getMBBRange(*I);
Matthias Braun11042c82015-02-18 01:50:52 +0000361 // Trim the live range in UseMBB.
362 if (*I == UseMBBNum && Use.isValid())
363 End = Use;
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000364 else
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000365 Map[MF->getBlockNumbered(*I)] = LiveOutPair(TheVNI, nullptr);
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000366 Updater.add(Start, End, TheVNI);
367 }
368 return true;
369 }
370
371 // Multiple values were found, so transfer the work list to the LiveIn array
372 // where UpdateSSA will use it as a work list.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000373 LiveIn.reserve(WorkList.size());
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000374 for (SmallVectorImpl<unsigned>::const_iterator
375 I = WorkList.begin(), E = WorkList.end(); I != E; ++I) {
376 MachineBasicBlock *MBB = MF->getBlockNumbered(*I);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000377 addLiveInBlock(LR, DomTree->getNode(MBB));
Matthias Braun11042c82015-02-18 01:50:52 +0000378 if (MBB == &UseMBB)
379 LiveIn.back().Kill = Use;
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000380 }
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000381
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000382 return false;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000383}
384
385
386// This is essentially the same iterative algorithm that SSAUpdater uses,
387// except we already have a dominator tree, so we don't have to recompute it.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000388void LiveRangeCalc::updateSSA() {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000389 assert(Indexes && "Missing SlotIndexes");
390 assert(DomTree && "Missing dominator tree");
391
392 // Interate until convergence.
393 unsigned Changes;
394 do {
395 Changes = 0;
396 // Propagate live-out values down the dominator tree, inserting phi-defs
397 // when necessary.
Matthias Braun42fab342014-12-15 19:40:46 +0000398 for (LiveInBlock &I : LiveIn) {
399 MachineDomTreeNode *Node = I.DomNode;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000400 // Skip block if the live-in value has already been determined.
401 if (!Node)
402 continue;
403 MachineBasicBlock *MBB = Node->getBlock();
404 MachineDomTreeNode *IDom = Node->getIDom();
405 LiveOutPair IDomValue;
406
407 // We need a live-in value to a block with no immediate dominator?
408 // This is probably an unreachable block that has survived somehow.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000409 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000410
411 // IDom dominates all of our predecessors, but it may not be their
412 // immediate dominator. Check if any of them have live-out values that are
413 // properly dominated by IDom. If so, we need a phi-def here.
414 if (!needPHI) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000415 IDomValue = Map[IDom->getBlock()];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000416
417 // Cache the DomTree node that defined the value.
418 if (IDomValue.first && !IDomValue.second)
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000419 Map[IDom->getBlock()].second = IDomValue.second =
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000420 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
421
422 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
423 PE = MBB->pred_end(); PI != PE; ++PI) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000424 LiveOutPair &Value = Map[*PI];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000425 if (!Value.first || Value.first == IDomValue.first)
426 continue;
427
428 // Cache the DomTree node that defined the value.
429 if (!Value.second)
430 Value.second =
431 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
432
433 // This predecessor is carrying something other than IDomValue.
434 // It could be because IDomValue hasn't propagated yet, or it could be
435 // because MBB is in the dominance frontier of that value.
436 if (DomTree->dominates(IDom, Value.second)) {
437 needPHI = true;
438 break;
439 }
440 }
441 }
442
443 // The value may be live-through even if Kill is set, as can happen when
444 // we are called from extendRange. In that case LiveOutSeen is true, and
445 // LiveOut indicates a foreign or missing value.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000446 LiveOutPair &LOP = Map[MBB];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000447
448 // Create a phi-def if required.
449 if (needPHI) {
450 ++Changes;
451 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
452 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000453 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Matthias Braun42fab342014-12-15 19:40:46 +0000454 LiveRange &LR = I.LR;
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000455 VNInfo *VNI = LR.getNextValue(Start, *Alloc);
Matthias Braun42fab342014-12-15 19:40:46 +0000456 I.Value = VNI;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000457 // This block is done, we know the final value.
Matthias Braun42fab342014-12-15 19:40:46 +0000458 I.DomNode = nullptr;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000459
Matthias Braun2f662322014-12-10 01:12:12 +0000460 // Add liveness since updateFromLiveIns now skips this node.
Matthias Braun42fab342014-12-15 19:40:46 +0000461 if (I.Kill.isValid())
462 LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI));
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000463 else {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000464 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000465 LOP = LiveOutPair(VNI, Node);
466 }
467 } else if (IDomValue.first) {
468 // No phi-def here. Remember incoming value.
Matthias Braun42fab342014-12-15 19:40:46 +0000469 I.Value = IDomValue.first;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000470
471 // If the IDomValue is killed in the block, don't propagate through.
Matthias Braun42fab342014-12-15 19:40:46 +0000472 if (I.Kill.isValid())
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000473 continue;
474
475 // Propagate IDomValue if it isn't killed:
476 // MBB is live-out and doesn't define its own value.
477 if (LOP.first == IDomValue.first)
478 continue;
479 ++Changes;
480 LOP = IDomValue;
481 }
482 }
483 } while (Changes);
484}