blob: 8c43c9f3f88462221b99d40ce16617d092d50652 [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"
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000015#include "llvm/ADT/SetVector.h"
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000016#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +000017#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000018
19using namespace llvm;
20
Chandler Carruth1b9dde02014-04-22 02:02:50 +000021#define DEBUG_TYPE "regalloc"
22
Krzysztof Parzyszek0b7688e2017-06-27 21:30:46 +000023// Reserve an address that indicates a value that is known to be "undef".
24static VNInfo UndefVNI(0xbad, SlotIndex());
25
Matthias Braun1aed6ff2014-12-16 04:03:38 +000026void LiveRangeCalc::resetLiveOutMap() {
27 unsigned NumBlocks = MF->getNumBlockIDs();
28 Seen.clear();
29 Seen.resize(NumBlocks);
Matthias Brauna6e77402017-06-27 18:05:26 +000030 EntryInfos.clear();
Matthias Braun1aed6ff2014-12-16 04:03:38 +000031 Map.resize(NumBlocks);
32}
33
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000034void LiveRangeCalc::reset(const MachineFunction *mf,
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +000035 SlotIndexes *SI,
36 MachineDominatorTree *MDT,
37 VNInfo::Allocator *VNIA) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000038 MF = mf;
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +000039 MRI = &MF->getRegInfo();
40 Indexes = SI;
41 DomTree = MDT;
42 Alloc = VNIA;
Matthias Braun1aed6ff2014-12-16 04:03:38 +000043 resetLiveOutMap();
Matthias Braunc3a72c22014-12-15 21:36:35 +000044 LiveIn.clear();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000045}
46
47
Matthias Braun1aed6ff2014-12-16 04:03:38 +000048static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc,
49 LiveRange &LR, const MachineOperand &MO) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +000050 const MachineInstr &MI = *MO.getParent();
51 SlotIndex DefIdx =
52 Indexes.getInstructionIndex(MI).getRegSlot(MO.isEarlyClobber());
Matthias Braunc3a72c22014-12-15 21:36:35 +000053
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +000054 // Create the def in LR. This may find an existing def.
55 LR.createDeadDef(DefIdx, Alloc);
Matthias Braun2f662322014-12-10 01:12:12 +000056}
57
Matthias Brauna25e13a2015-03-19 00:21:58 +000058void LiveRangeCalc::calculate(LiveInterval &LI, bool TrackSubRegs) {
Matthias Braun2f662322014-12-10 01:12:12 +000059 assert(MRI && Indexes && "call reset() first");
60
Matthias Braun1aed6ff2014-12-16 04:03:38 +000061 // Step 1: Create minimal live segments for every definition of Reg.
Matthias Braun2f662322014-12-10 01:12:12 +000062 // Visit all def operands. If the same instruction has multiple defs of Reg,
Matthias Braun1aed6ff2014-12-16 04:03:38 +000063 // createDeadDef() will deduplicate.
Matthias Braun2f662322014-12-10 01:12:12 +000064 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
65 unsigned Reg = LI.reg;
Matthias Braun1aed6ff2014-12-16 04:03:38 +000066 for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
67 if (!MO.isDef() && !MO.readsReg())
68 continue;
69
Matthias Braun2f662322014-12-10 01:12:12 +000070 unsigned SubReg = MO.getSubReg();
Matthias Brauna25e13a2015-03-19 00:21:58 +000071 if (LI.hasSubRanges() || (SubReg != 0 && TrackSubRegs)) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000072 LaneBitmask SubMask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg)
Krzysztof Parzyszek0a955d62016-08-29 13:15:35 +000073 : MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braun2f662322014-12-10 01:12:12 +000074 // If this is the first time we see a subregister def, initialize
75 // subranges by creating a copy of the main range.
76 if (!LI.hasSubRanges() && !LI.empty()) {
Matthias Braune6a24852015-09-25 21:51:14 +000077 LaneBitmask ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braun2f662322014-12-10 01:12:12 +000078 LI.createSubRangeFrom(*Alloc, ClassMask, LI);
79 }
80
Matthias Brauna04d7ad2017-03-03 19:05:34 +000081 LI.refineSubRanges(*Alloc, SubMask,
82 [&MO, this](LiveInterval::SubRange &SR) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +000083 if (MO.isDef())
Matthias Brauna04d7ad2017-03-03 19:05:34 +000084 createDeadDef(*Indexes, *Alloc, SR, MO);
85 });
Matthias Braun2f662322014-12-10 01:12:12 +000086 }
87
Matthias Braundbcca0d2014-12-24 02:11:51 +000088 // Create the def in the main liverange. We do not have to do this if
89 // subranges are tracked as we recreate the main range later in this case.
90 if (MO.isDef() && !LI.hasSubRanges())
Matthias Braun1aed6ff2014-12-16 04:03:38 +000091 createDeadDef(*Indexes, *Alloc, LI, MO);
Matthias Braun2f662322014-12-10 01:12:12 +000092 }
Matthias Braun1aed6ff2014-12-16 04:03:38 +000093
94 // We may have created empty live ranges for partially undefined uses, we
95 // can't keep them because we won't find defs in them later.
96 LI.removeEmptySubRanges();
97
98 // Step 2: Extend live segments to all uses, constructing SSA form as
99 // necessary.
Matthias Braundbcca0d2014-12-24 02:11:51 +0000100 if (LI.hasSubRanges()) {
101 for (LiveInterval::SubRange &S : LI.subranges()) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000102 LiveRangeCalc SubLRC;
103 SubLRC.reset(MF, Indexes, DomTree, Alloc);
104 SubLRC.extendToUses(S, Reg, S.LaneMask, &LI);
Matthias Braundbcca0d2014-12-24 02:11:51 +0000105 }
106 LI.clear();
Matthias Braun71f95642016-05-20 23:14:56 +0000107 constructMainRangeFromSubranges(LI);
Matthias Braundbcca0d2014-12-24 02:11:51 +0000108 } else {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000109 resetLiveOutMap();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000110 extendToUses(LI, Reg, LaneBitmask::getAll());
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000111 }
Matthias Braun2f662322014-12-10 01:12:12 +0000112}
113
Matthias Braun71f95642016-05-20 23:14:56 +0000114void LiveRangeCalc::constructMainRangeFromSubranges(LiveInterval &LI) {
115 // First create dead defs at all defs found in subranges.
116 LiveRange &MainRange = LI;
117 assert(MainRange.segments.empty() && MainRange.valnos.empty() &&
118 "Expect empty main liverange");
119
120 for (const LiveInterval::SubRange &SR : LI.subranges()) {
121 for (const VNInfo *VNI : SR.valnos) {
122 if (!VNI->isUnused() && !VNI->isPHIDef())
123 MainRange.createDeadDef(VNI->def, *Alloc);
124 }
125 }
Matthias Braun71f95642016-05-20 23:14:56 +0000126 resetLiveOutMap();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000127 extendToUses(MainRange, LI.reg, LaneBitmask::getAll(), &LI);
Matthias Braun71f95642016-05-20 23:14:56 +0000128}
Matthias Braun2f662322014-12-10 01:12:12 +0000129
Matthias Braunc3a72c22014-12-15 21:36:35 +0000130void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) {
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000131 assert(MRI && Indexes && "call reset() first");
132
133 // Visit all def operands. If the same instruction has multiple defs of Reg,
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000134 // LR.createDeadDef() will deduplicate.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000135 for (MachineOperand &MO : MRI->def_operands(Reg))
136 createDeadDef(*Indexes, *Alloc, LR, MO);
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000137}
138
139
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000140void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask Mask,
141 LiveInterval *LI) {
142 SmallVector<SlotIndex, 4> Undefs;
143 if (LI != nullptr)
144 LI->computeSubRangeUndefs(Undefs, Mask, *MRI, *Indexes);
145
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000146 // Visit all operands that read Reg. This may include partial defs.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000147 bool IsSubRange = !Mask.all();
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000148 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +0000149 for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
Jakob Stoklund Olesen4aed4702012-09-06 18:15:15 +0000150 // Clear all kill flags. They will be reinserted after register allocation
151 // by LiveIntervalAnalysis::addKillFlags().
152 if (MO.isUse())
153 MO.setIsKill(false);
Krzysztof Parzyszek3bf4aec2016-09-02 19:48:55 +0000154 // MO::readsReg returns "true" for subregister defs. This is for keeping
155 // liveness of the entire register (i.e. for the main range of the live
156 // interval). For subranges, definitions of non-overlapping subregisters
157 // do not count as uses.
158 if (!MO.readsReg() || (IsSubRange && MO.isDef()))
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000159 continue;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000160
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000161 unsigned SubReg = MO.getSubReg();
162 if (SubReg != 0) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000163 LaneBitmask SLM = TRI.getSubRegIndexLaneMask(SubReg);
164 if (MO.isDef())
Krzysztof Parzyszek0a955d62016-08-29 13:15:35 +0000165 SLM = ~SLM;
166 // Ignore uses not reading the current (sub)range.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000167 if ((SLM & Mask).none())
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000168 continue;
169 }
170
171 // Determine the actual place of the use.
172 const MachineInstr *MI = MO.getParent();
173 unsigned OpNo = (&MO - &MI->getOperand(0));
174 SlotIndex UseIdx;
175 if (MI->isPHI()) {
176 assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
177 // The actual place where a phi operand is used is the end of the pred
178 // MBB. PHI operands are paired: (Reg, PredMBB).
179 UseIdx = Indexes->getMBBEndIdx(MI->getOperand(OpNo+1).getMBB());
180 } else {
181 // Check for early-clobber redefs.
182 bool isEarlyClobber = false;
183 unsigned DefIdx;
184 if (MO.isDef())
185 isEarlyClobber = MO.isEarlyClobber();
186 else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
187 // FIXME: This would be a lot easier if tied early-clobber uses also
188 // had an early-clobber flag.
189 isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber();
190 }
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000191 UseIdx = Indexes->getInstructionIndex(*MI).getRegSlot(isEarlyClobber);
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000192 }
193
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000194 // MI is reading Reg. We may have visited MI before if it happens to be
195 // reading Reg multiple times. That is OK, extend() is idempotent.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000196 extend(LR, UseIdx, Reg, Undefs);
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000197 }
198}
199
200
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000201void LiveRangeCalc::updateFromLiveIns() {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000202 LiveRangeUpdater Updater;
Matthias Braun42fab342014-12-15 19:40:46 +0000203 for (const LiveInBlock &I : LiveIn) {
204 if (!I.DomNode)
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000205 continue;
Matthias Braun42fab342014-12-15 19:40:46 +0000206 MachineBasicBlock *MBB = I.DomNode->getBlock();
207 assert(I.Value && "No live-in value found");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000208 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000209 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000210
Matthias Braun42fab342014-12-15 19:40:46 +0000211 if (I.Kill.isValid())
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000212 // Value is killed inside this block.
Matthias Braun42fab342014-12-15 19:40:46 +0000213 End = I.Kill;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000214 else {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000215 // The value is live-through, update LiveOut as well.
216 // Defer the Domtree lookup until it is needed.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000217 assert(Seen.test(MBB->getNumber()));
218 Map[MBB] = LiveOutPair(I.Value, nullptr);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000219 }
Matthias Braun42fab342014-12-15 19:40:46 +0000220 Updater.setDest(&I.LR);
221 Updater.add(Start, End, I.Value);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000222 }
223 LiveIn.clear();
224}
225
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000226void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
227 ArrayRef<SlotIndex> Undefs) {
Matthias Braun11042c82015-02-18 01:50:52 +0000228 assert(Use.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000229 assert(Indexes && "Missing SlotIndexes");
230 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000231
Matthias Braun11042c82015-02-18 01:50:52 +0000232 MachineBasicBlock *UseMBB = Indexes->getMBBFromIndex(Use.getPrevSlot());
233 assert(UseMBB && "No MBB at Use");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000234
235 // Is there a def in the same MBB we can extend?
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000236 auto EP = LR.extendInBlock(Undefs, Indexes->getMBBStartIdx(UseMBB), Use);
237 if (EP.first != nullptr || EP.second)
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000238 return;
239
Matthias Braun11042c82015-02-18 01:50:52 +0000240 // Find the single reaching def, or determine if Use is jointly dominated by
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000241 // multiple values, and we may need to create even more phi-defs to preserve
242 // VNInfo SSA form. Perform a search for all predecessor blocks where we
243 // know the dominating VNInfo.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000244 if (findReachingDefs(LR, *UseMBB, Use, PhysReg, Undefs))
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000245 return;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000246
247 // When there were multiple different values, we may need new PHIs.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000248 calculateValues();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000249}
250
251
252// This function is called by a client after using the low-level API to add
253// live-out and live-in blocks. The unique value optimization is not
254// available, SplitEditor::transferValues handles that case directly anyway.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000255void LiveRangeCalc::calculateValues() {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000256 assert(Indexes && "Missing SlotIndexes");
257 assert(DomTree && "Missing dominator tree");
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000258 updateSSA();
259 updateFromLiveIns();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000260}
261
262
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000263bool LiveRangeCalc::isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
264 MachineBasicBlock &MBB, BitVector &DefOnEntry,
265 BitVector &UndefOnEntry) {
266 unsigned BN = MBB.getNumber();
267 if (DefOnEntry[BN])
268 return true;
269 if (UndefOnEntry[BN])
270 return false;
271
Malcolm Parsons17d266b2017-01-13 17:12:16 +0000272 auto MarkDefined = [BN, &DefOnEntry](MachineBasicBlock &B) -> bool {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000273 for (MachineBasicBlock *S : B.successors())
274 DefOnEntry[S->getNumber()] = true;
275 DefOnEntry[BN] = true;
276 return true;
277 };
278
279 SetVector<unsigned> WorkList;
280 // Checking if the entry of MBB is reached by some def: add all predecessors
281 // that are potentially defined-on-exit to the work list.
282 for (MachineBasicBlock *P : MBB.predecessors())
283 WorkList.insert(P->getNumber());
284
285 for (unsigned i = 0; i != WorkList.size(); ++i) {
286 // Determine if the exit from the block is reached by some def.
287 unsigned N = WorkList[i];
288 MachineBasicBlock &B = *MF->getBlockNumbered(N);
Krzysztof Parzyszek0b7688e2017-06-27 21:30:46 +0000289 if (Seen[N]) {
290 const LiveOutPair &LOB = Map[&B];
291 if (LOB.first != nullptr && LOB.first != &UndefVNI)
292 return MarkDefined(B);
293 }
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000294 SlotIndex Begin, End;
295 std::tie(Begin, End) = Indexes->getMBBRange(&B);
Krzysztof Parzyszekde44c9d2017-01-18 23:12:19 +0000296 // Treat End as not belonging to B.
297 // If LR has a segment S that starts at the next block, i.e. [End, ...),
298 // std::upper_bound will return the segment following S. Instead,
299 // S should be treated as the first segment that does not overlap B.
300 LiveRange::iterator UB = std::upper_bound(LR.begin(), LR.end(),
301 End.getPrevSlot());
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000302 if (UB != LR.begin()) {
303 LiveRange::Segment &Seg = *std::prev(UB);
304 if (Seg.end > Begin) {
305 // There is a segment that overlaps B. If the range is not explicitly
306 // undefined between the end of the segment and the end of the block,
307 // treat the block as defined on exit. If it is, go to the next block
308 // on the work list.
309 if (LR.isUndefIn(Undefs, Seg.end, End))
310 continue;
311 return MarkDefined(B);
312 }
313 }
314
315 // No segment overlaps with this block. If this block is not defined on
316 // entry, or it undefines the range, do not process its predecessors.
317 if (UndefOnEntry[N] || LR.isUndefIn(Undefs, Begin, End)) {
318 UndefOnEntry[N] = true;
319 continue;
320 }
321 if (DefOnEntry[N])
322 return MarkDefined(B);
323
324 // Still don't know: add all predecessors to the work list.
325 for (MachineBasicBlock *P : B.predecessors())
326 WorkList.insert(P->getNumber());
327 }
328
329 UndefOnEntry[BN] = true;
330 return false;
331}
332
Matthias Braun11042c82015-02-18 01:50:52 +0000333bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000334 SlotIndex Use, unsigned PhysReg,
335 ArrayRef<SlotIndex> Undefs) {
Matthias Braun11042c82015-02-18 01:50:52 +0000336 unsigned UseMBBNum = UseMBB.getNumber();
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000337
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000338 // Block numbers where LR should be live-in.
Matthias Braun11042c82015-02-18 01:50:52 +0000339 SmallVector<unsigned, 16> WorkList(1, UseMBBNum);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000340
341 // Remember if we have seen more than one value.
342 bool UniqueVNI = true;
Craig Topperc0196b12014-04-14 00:51:57 +0000343 VNInfo *TheVNI = nullptr;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000344
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000345 bool FoundUndef = false;
346
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000347 // Using Seen as a visited set, perform a BFS for all reaching defs.
348 for (unsigned i = 0; i != WorkList.size(); ++i) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000349 MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000350
351#ifndef NDEBUG
Matthias Braune1d96282016-09-19 16:49:45 +0000352 if (MBB->pred_empty()) {
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000353 MBB->getParent()->verify();
Matthias Braun53917542015-05-11 18:47:47 +0000354 errs() << "Use of " << PrintReg(PhysReg)
355 << " does not have a corresponding definition on every path:\n";
356 const MachineInstr *MI = Indexes->getInstructionFromIndex(Use);
357 if (MI != nullptr)
358 errs() << Use << " " << *MI;
Matthias Braune1d96282016-09-19 16:49:45 +0000359 report_fatal_error("Use not jointly dominated by defs.");
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000360 }
361
362 if (TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
363 !MBB->isLiveIn(PhysReg)) {
364 MBB->getParent()->verify();
Matt Arsenaultf3d1a1a2016-09-03 06:57:49 +0000365 const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
366 errs() << "The register " << PrintReg(PhysReg, TRI)
Matthias Braun53917542015-05-11 18:47:47 +0000367 << " needs to be live in to BB#" << MBB->getNumber()
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000368 << ", but is missing from the live-in list.\n";
Matthias Braune1d96282016-09-19 16:49:45 +0000369 report_fatal_error("Invalid global physical register");
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000370 }
371#endif
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000372 FoundUndef |= MBB->pred_empty();
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000373
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000374 for (MachineBasicBlock *Pred : MBB->predecessors()) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000375 // Is this a known live-out block?
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000376 if (Seen.test(Pred->getNumber())) {
377 if (VNInfo *VNI = Map[Pred].first) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000378 if (TheVNI && TheVNI != VNI)
379 UniqueVNI = false;
380 TheVNI = VNI;
381 }
382 continue;
383 }
384
385 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000386 std::tie(Start, End) = Indexes->getMBBRange(Pred);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000387
388 // First time we see Pred. Try to determine the live-out value, but set
389 // it as null if Pred is live-through with an unknown value.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000390 auto EP = LR.extendInBlock(Undefs, Start, End);
391 VNInfo *VNI = EP.first;
392 FoundUndef |= EP.second;
Krzysztof Parzyszek0b7688e2017-06-27 21:30:46 +0000393 setLiveOutValue(Pred, EP.second ? &UndefVNI : VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000394 if (VNI) {
395 if (TheVNI && TheVNI != VNI)
396 UniqueVNI = false;
397 TheVNI = VNI;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000398 }
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000399 if (VNI || EP.second)
400 continue;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000401
402 // No, we need a live-in value for Pred as well
Matthias Braun11042c82015-02-18 01:50:52 +0000403 if (Pred != &UseMBB)
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000404 WorkList.push_back(Pred->getNumber());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000405 else
Matthias Braun11042c82015-02-18 01:50:52 +0000406 // Loopback to UseMBB, so value is really live through.
407 Use = SlotIndex();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000408 }
409 }
410
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000411 LiveIn.clear();
Krzysztof Parzyszek30085942017-06-28 15:46:16 +0000412 FoundUndef |= (TheVNI == nullptr || TheVNI == &UndefVNI);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000413 if (Undefs.size() > 0 && FoundUndef)
414 UniqueVNI = false;
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000415
416 // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
417 // neither require it. Skip the sorting overhead for small updates.
418 if (WorkList.size() > 4)
419 array_pod_sort(WorkList.begin(), WorkList.end());
420
421 // If a unique reaching def was found, blit in the live ranges immediately.
422 if (UniqueVNI) {
Krzysztof Parzyszek0b7688e2017-06-27 21:30:46 +0000423 assert(TheVNI != nullptr && TheVNI != &UndefVNI);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000424 LiveRangeUpdater Updater(&LR);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000425 for (unsigned BN : WorkList) {
426 SlotIndex Start, End;
427 std::tie(Start, End) = Indexes->getMBBRange(BN);
428 // Trim the live range in UseMBB.
429 if (BN == UseMBBNum && Use.isValid())
430 End = Use;
431 else
432 Map[MF->getBlockNumbered(BN)] = LiveOutPair(TheVNI, nullptr);
433 Updater.add(Start, End, TheVNI);
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000434 }
435 return true;
436 }
437
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000438 // Prepare the defined/undefined bit vectors.
Matthias Brauna6e77402017-06-27 18:05:26 +0000439 EntryInfoMap::iterator Entry;
440 bool DidInsert;
441 std::tie(Entry, DidInsert) = EntryInfos.insert(
442 std::make_pair(&LR, std::make_pair(BitVector(), BitVector())));
443 if (DidInsert) {
444 // Initialize newly inserted entries.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000445 unsigned N = MF->getNumBlockIDs();
Matthias Brauna6e77402017-06-27 18:05:26 +0000446 Entry->second.first.resize(N);
447 Entry->second.second.resize(N);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000448 }
Matthias Brauna6e77402017-06-27 18:05:26 +0000449 BitVector &DefOnEntry = Entry->second.first;
450 BitVector &UndefOnEntry = Entry->second.second;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000451
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000452 // Multiple values were found, so transfer the work list to the LiveIn array
453 // where UpdateSSA will use it as a work list.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000454 LiveIn.reserve(WorkList.size());
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000455 for (unsigned BN : WorkList) {
456 MachineBasicBlock *MBB = MF->getBlockNumbered(BN);
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000457 if (Undefs.size() > 0 &&
458 !isDefOnEntry(LR, Undefs, *MBB, DefOnEntry, UndefOnEntry))
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000459 continue;
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000460 addLiveInBlock(LR, DomTree->getNode(MBB));
Matthias Braun11042c82015-02-18 01:50:52 +0000461 if (MBB == &UseMBB)
462 LiveIn.back().Kill = Use;
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000463 }
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000464
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000465 return false;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000466}
467
468
469// This is essentially the same iterative algorithm that SSAUpdater uses,
470// except we already have a dominator tree, so we don't have to recompute it.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000471void LiveRangeCalc::updateSSA() {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000472 assert(Indexes && "Missing SlotIndexes");
473 assert(DomTree && "Missing dominator tree");
474
475 // Interate until convergence.
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000476 bool Changed;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000477 do {
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000478 Changed = false;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000479 // Propagate live-out values down the dominator tree, inserting phi-defs
480 // when necessary.
Matthias Braun42fab342014-12-15 19:40:46 +0000481 for (LiveInBlock &I : LiveIn) {
482 MachineDomTreeNode *Node = I.DomNode;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000483 // Skip block if the live-in value has already been determined.
484 if (!Node)
485 continue;
486 MachineBasicBlock *MBB = Node->getBlock();
487 MachineDomTreeNode *IDom = Node->getIDom();
488 LiveOutPair IDomValue;
489
490 // We need a live-in value to a block with no immediate dominator?
491 // This is probably an unreachable block that has survived somehow.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000492 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000493
494 // IDom dominates all of our predecessors, but it may not be their
495 // immediate dominator. Check if any of them have live-out values that are
496 // properly dominated by IDom. If so, we need a phi-def here.
497 if (!needPHI) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000498 IDomValue = Map[IDom->getBlock()];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000499
500 // Cache the DomTree node that defined the value.
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000501 if (IDomValue.first && IDomValue.first != &UndefVNI &&
502 !IDomValue.second) {
503 Map[IDom->getBlock()].second = IDomValue.second =
504 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
505 }
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000506
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000507 for (MachineBasicBlock *Pred : MBB->predecessors()) {
508 LiveOutPair &Value = Map[Pred];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000509 if (!Value.first || Value.first == IDomValue.first)
510 continue;
Krzysztof Parzyszek0b7688e2017-06-27 21:30:46 +0000511 if (Value.first == &UndefVNI) {
512 needPHI = true;
513 break;
514 }
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000515
516 // Cache the DomTree node that defined the value.
517 if (!Value.second)
518 Value.second =
519 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
520
521 // This predecessor is carrying something other than IDomValue.
522 // It could be because IDomValue hasn't propagated yet, or it could be
523 // because MBB is in the dominance frontier of that value.
524 if (DomTree->dominates(IDom, Value.second)) {
525 needPHI = true;
526 break;
527 }
528 }
529 }
530
531 // The value may be live-through even if Kill is set, as can happen when
532 // we are called from extendRange. In that case LiveOutSeen is true, and
533 // LiveOut indicates a foreign or missing value.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000534 LiveOutPair &LOP = Map[MBB];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000535
536 // Create a phi-def if required.
537 if (needPHI) {
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000538 Changed = true;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000539 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
540 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000541 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Matthias Braun42fab342014-12-15 19:40:46 +0000542 LiveRange &LR = I.LR;
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000543 VNInfo *VNI = LR.getNextValue(Start, *Alloc);
Matthias Braun42fab342014-12-15 19:40:46 +0000544 I.Value = VNI;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000545 // This block is done, we know the final value.
Matthias Braun42fab342014-12-15 19:40:46 +0000546 I.DomNode = nullptr;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000547
Matthias Braun2f662322014-12-10 01:12:12 +0000548 // Add liveness since updateFromLiveIns now skips this node.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000549 if (I.Kill.isValid()) {
550 if (VNI)
551 LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI));
552 } else {
553 if (VNI)
554 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000555 LOP = LiveOutPair(VNI, Node);
556 }
Krzysztof Parzyszek0b7688e2017-06-27 21:30:46 +0000557 } else if (IDomValue.first && IDomValue.first != &UndefVNI) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000558 // No phi-def here. Remember incoming value.
Matthias Braun42fab342014-12-15 19:40:46 +0000559 I.Value = IDomValue.first;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000560
561 // If the IDomValue is killed in the block, don't propagate through.
Matthias Braun42fab342014-12-15 19:40:46 +0000562 if (I.Kill.isValid())
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000563 continue;
564
565 // Propagate IDomValue if it isn't killed:
566 // MBB is live-out and doesn't define its own value.
567 if (LOP.first == IDomValue.first)
568 continue;
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000569 Changed = true;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000570 LOP = IDomValue;
571 }
572 }
Krzysztof Parzyszek93cf2322017-06-28 16:02:00 +0000573 } while (Changed);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000574}