blob: e849d285545a84cfd72316766cc3ce1ea4283550 [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
Matthias Braun1aed6ff2014-12-16 04:03:38 +000023void LiveRangeCalc::resetLiveOutMap() {
24 unsigned NumBlocks = MF->getNumBlockIDs();
25 Seen.clear();
26 Seen.resize(NumBlocks);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000027 EntryInfoMap.clear();
Matthias Braun1aed6ff2014-12-16 04:03:38 +000028 Map.resize(NumBlocks);
29}
30
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000031void LiveRangeCalc::reset(const MachineFunction *mf,
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +000032 SlotIndexes *SI,
33 MachineDominatorTree *MDT,
34 VNInfo::Allocator *VNIA) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000035 MF = mf;
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +000036 MRI = &MF->getRegInfo();
37 Indexes = SI;
38 DomTree = MDT;
39 Alloc = VNIA;
Matthias Braun1aed6ff2014-12-16 04:03:38 +000040 resetLiveOutMap();
Matthias Braunc3a72c22014-12-15 21:36:35 +000041 LiveIn.clear();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000042}
43
44
Matthias Braun1aed6ff2014-12-16 04:03:38 +000045static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc,
46 LiveRange &LR, const MachineOperand &MO) {
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +000047 const MachineInstr &MI = *MO.getParent();
48 SlotIndex DefIdx =
49 Indexes.getInstructionIndex(MI).getRegSlot(MO.isEarlyClobber());
Matthias Braunc3a72c22014-12-15 21:36:35 +000050
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +000051 // Create the def in LR. This may find an existing def.
52 LR.createDeadDef(DefIdx, Alloc);
Matthias Braun2f662322014-12-10 01:12:12 +000053}
54
Matthias Brauna25e13a2015-03-19 00:21:58 +000055void LiveRangeCalc::calculate(LiveInterval &LI, bool TrackSubRegs) {
Matthias Braun2f662322014-12-10 01:12:12 +000056 assert(MRI && Indexes && "call reset() first");
57
Matthias Braun1aed6ff2014-12-16 04:03:38 +000058 // Step 1: Create minimal live segments for every definition of Reg.
Matthias Braun2f662322014-12-10 01:12:12 +000059 // Visit all def operands. If the same instruction has multiple defs of Reg,
Matthias Braun1aed6ff2014-12-16 04:03:38 +000060 // createDeadDef() will deduplicate.
Matthias Braun2f662322014-12-10 01:12:12 +000061 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
62 unsigned Reg = LI.reg;
Matthias Braun1aed6ff2014-12-16 04:03:38 +000063 for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
64 if (!MO.isDef() && !MO.readsReg())
65 continue;
66
Matthias Braun2f662322014-12-10 01:12:12 +000067 unsigned SubReg = MO.getSubReg();
Matthias Brauna25e13a2015-03-19 00:21:58 +000068 if (LI.hasSubRanges() || (SubReg != 0 && TrackSubRegs)) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000069 LaneBitmask SubMask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg)
Krzysztof Parzyszek0a955d62016-08-29 13:15:35 +000070 : MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braun2f662322014-12-10 01:12:12 +000071 // If this is the first time we see a subregister def, initialize
72 // subranges by creating a copy of the main range.
73 if (!LI.hasSubRanges() && !LI.empty()) {
Matthias Braune6a24852015-09-25 21:51:14 +000074 LaneBitmask ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braun2f662322014-12-10 01:12:12 +000075 LI.createSubRangeFrom(*Alloc, ClassMask, LI);
76 }
77
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000078 LaneBitmask Mask = SubMask;
Matthias Braun09afa1e2014-12-11 00:59:06 +000079 for (LiveInterval::SubRange &S : LI.subranges()) {
Matthias Braun2f662322014-12-10 01:12:12 +000080 // A Mask for subregs common to the existing subrange and current def.
Matthias Braune6a24852015-09-25 21:51:14 +000081 LaneBitmask Common = S.LaneMask & Mask;
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +000082 if (Common.none())
Matthias Braun2f662322014-12-10 01:12:12 +000083 continue;
Matthias Braun2f662322014-12-10 01:12:12 +000084 LiveInterval::SubRange *CommonRange;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000085 // A Mask for subregs covered by the subrange but not the current def.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +000086 LaneBitmask RM = S.LaneMask & ~Mask;
87 if (!RM.none()) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000088 // Split the subrange S into two parts: one covered by the current
89 // def (CommonRange), and the one not affected by it (updated S).
90 S.LaneMask = RM;
Matthias Braun09afa1e2014-12-11 00:59:06 +000091 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.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000101 if (!Mask.none()) {
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 Braundbcca0d2014-12-24 02:11:51 +0000108 // Create the def in the main liverange. We do not have to do this if
109 // subranges are tracked as we recreate the main range later in this case.
110 if (MO.isDef() && !LI.hasSubRanges())
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000111 createDeadDef(*Indexes, *Alloc, LI, MO);
Matthias Braun2f662322014-12-10 01:12:12 +0000112 }
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000113
114 // We may have created empty live ranges for partially undefined uses, we
115 // can't keep them because we won't find defs in them later.
116 LI.removeEmptySubRanges();
117
118 // Step 2: Extend live segments to all uses, constructing SSA form as
119 // necessary.
Matthias Braundbcca0d2014-12-24 02:11:51 +0000120 if (LI.hasSubRanges()) {
121 for (LiveInterval::SubRange &S : LI.subranges()) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000122 LiveRangeCalc SubLRC;
123 SubLRC.reset(MF, Indexes, DomTree, Alloc);
124 SubLRC.extendToUses(S, Reg, S.LaneMask, &LI);
Matthias Braundbcca0d2014-12-24 02:11:51 +0000125 }
126 LI.clear();
Matthias Braun71f95642016-05-20 23:14:56 +0000127 constructMainRangeFromSubranges(LI);
Matthias Braundbcca0d2014-12-24 02:11:51 +0000128 } else {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000129 resetLiveOutMap();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000130 extendToUses(LI, Reg, LaneBitmask::getAll());
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000131 }
Matthias Braun2f662322014-12-10 01:12:12 +0000132}
133
Matthias Braun71f95642016-05-20 23:14:56 +0000134void LiveRangeCalc::constructMainRangeFromSubranges(LiveInterval &LI) {
135 // First create dead defs at all defs found in subranges.
136 LiveRange &MainRange = LI;
137 assert(MainRange.segments.empty() && MainRange.valnos.empty() &&
138 "Expect empty main liverange");
139
140 for (const LiveInterval::SubRange &SR : LI.subranges()) {
141 for (const VNInfo *VNI : SR.valnos) {
142 if (!VNI->isUnused() && !VNI->isPHIDef())
143 MainRange.createDeadDef(VNI->def, *Alloc);
144 }
145 }
Matthias Braun71f95642016-05-20 23:14:56 +0000146 resetLiveOutMap();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000147 extendToUses(MainRange, LI.reg, LaneBitmask::getAll(), &LI);
Matthias Braun71f95642016-05-20 23:14:56 +0000148}
Matthias Braun2f662322014-12-10 01:12:12 +0000149
Matthias Braunc3a72c22014-12-15 21:36:35 +0000150void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) {
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000151 assert(MRI && Indexes && "call reset() first");
152
153 // Visit all def operands. If the same instruction has multiple defs of Reg,
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000154 // LR.createDeadDef() will deduplicate.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000155 for (MachineOperand &MO : MRI->def_operands(Reg))
156 createDeadDef(*Indexes, *Alloc, LR, MO);
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000157}
158
159
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000160void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask Mask,
161 LiveInterval *LI) {
162 SmallVector<SlotIndex, 4> Undefs;
163 if (LI != nullptr)
164 LI->computeSubRangeUndefs(Undefs, Mask, *MRI, *Indexes);
165
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000166 // Visit all operands that read Reg. This may include partial defs.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000167 bool IsSubRange = !Mask.all();
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000168 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +0000169 for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
Jakob Stoklund Olesen4aed4702012-09-06 18:15:15 +0000170 // Clear all kill flags. They will be reinserted after register allocation
171 // by LiveIntervalAnalysis::addKillFlags().
172 if (MO.isUse())
173 MO.setIsKill(false);
Krzysztof Parzyszek3bf4aec2016-09-02 19:48:55 +0000174 // MO::readsReg returns "true" for subregister defs. This is for keeping
175 // liveness of the entire register (i.e. for the main range of the live
176 // interval). For subranges, definitions of non-overlapping subregisters
177 // do not count as uses.
178 if (!MO.readsReg() || (IsSubRange && MO.isDef()))
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000179 continue;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000180
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000181 unsigned SubReg = MO.getSubReg();
182 if (SubReg != 0) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000183 LaneBitmask SLM = TRI.getSubRegIndexLaneMask(SubReg);
184 if (MO.isDef())
Krzysztof Parzyszek0a955d62016-08-29 13:15:35 +0000185 SLM = ~SLM;
186 // Ignore uses not reading the current (sub)range.
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000187 if ((SLM & Mask).none())
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000188 continue;
189 }
190
191 // Determine the actual place of the use.
192 const MachineInstr *MI = MO.getParent();
193 unsigned OpNo = (&MO - &MI->getOperand(0));
194 SlotIndex UseIdx;
195 if (MI->isPHI()) {
196 assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
197 // The actual place where a phi operand is used is the end of the pred
198 // MBB. PHI operands are paired: (Reg, PredMBB).
199 UseIdx = Indexes->getMBBEndIdx(MI->getOperand(OpNo+1).getMBB());
200 } else {
201 // Check for early-clobber redefs.
202 bool isEarlyClobber = false;
203 unsigned DefIdx;
204 if (MO.isDef())
205 isEarlyClobber = MO.isEarlyClobber();
206 else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
207 // FIXME: This would be a lot easier if tied early-clobber uses also
208 // had an early-clobber flag.
209 isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber();
210 }
Duncan P. N. Exon Smith3ac9cc62016-02-27 06:40:41 +0000211 UseIdx = Indexes->getInstructionIndex(*MI).getRegSlot(isEarlyClobber);
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000212 }
213
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000214 // MI is reading Reg. We may have visited MI before if it happens to be
215 // reading Reg multiple times. That is OK, extend() is idempotent.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000216 extend(LR, UseIdx, Reg, Undefs);
Jakob Stoklund Olesen989b3b12012-06-05 21:54:09 +0000217 }
218}
219
220
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000221void LiveRangeCalc::updateFromLiveIns() {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000222 LiveRangeUpdater Updater;
Matthias Braun42fab342014-12-15 19:40:46 +0000223 for (const LiveInBlock &I : LiveIn) {
224 if (!I.DomNode)
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000225 continue;
Matthias Braun42fab342014-12-15 19:40:46 +0000226 MachineBasicBlock *MBB = I.DomNode->getBlock();
227 assert(I.Value && "No live-in value found");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000228 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000229 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000230
Matthias Braun42fab342014-12-15 19:40:46 +0000231 if (I.Kill.isValid())
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000232 // Value is killed inside this block.
Matthias Braun42fab342014-12-15 19:40:46 +0000233 End = I.Kill;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000234 else {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000235 // The value is live-through, update LiveOut as well.
236 // Defer the Domtree lookup until it is needed.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000237 assert(Seen.test(MBB->getNumber()));
238 Map[MBB] = LiveOutPair(I.Value, nullptr);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000239 }
Matthias Braun42fab342014-12-15 19:40:46 +0000240 Updater.setDest(&I.LR);
241 Updater.add(Start, End, I.Value);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000242 }
243 LiveIn.clear();
244}
245
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000246void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
247 ArrayRef<SlotIndex> Undefs) {
Matthias Braun11042c82015-02-18 01:50:52 +0000248 assert(Use.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000249 assert(Indexes && "Missing SlotIndexes");
250 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000251
Matthias Braun11042c82015-02-18 01:50:52 +0000252 MachineBasicBlock *UseMBB = Indexes->getMBBFromIndex(Use.getPrevSlot());
253 assert(UseMBB && "No MBB at Use");
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000254
255 // Is there a def in the same MBB we can extend?
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000256 auto EP = LR.extendInBlock(Undefs, Indexes->getMBBStartIdx(UseMBB), Use);
257 if (EP.first != nullptr || EP.second)
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000258 return;
259
Matthias Braun11042c82015-02-18 01:50:52 +0000260 // Find the single reaching def, or determine if Use is jointly dominated by
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000261 // multiple values, and we may need to create even more phi-defs to preserve
262 // VNInfo SSA form. Perform a search for all predecessor blocks where we
263 // know the dominating VNInfo.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000264 if (findReachingDefs(LR, *UseMBB, Use, PhysReg, Undefs))
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000265 return;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000266
267 // When there were multiple different values, we may need new PHIs.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000268 calculateValues();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000269}
270
271
272// This function is called by a client after using the low-level API to add
273// live-out and live-in blocks. The unique value optimization is not
274// available, SplitEditor::transferValues handles that case directly anyway.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000275void LiveRangeCalc::calculateValues() {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000276 assert(Indexes && "Missing SlotIndexes");
277 assert(DomTree && "Missing dominator tree");
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000278 updateSSA();
279 updateFromLiveIns();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000280}
281
282
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000283bool LiveRangeCalc::isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
284 MachineBasicBlock &MBB, BitVector &DefOnEntry,
285 BitVector &UndefOnEntry) {
286 unsigned BN = MBB.getNumber();
287 if (DefOnEntry[BN])
288 return true;
289 if (UndefOnEntry[BN])
290 return false;
291
292 auto MarkDefined =
293 [this,BN,&DefOnEntry,&UndefOnEntry] (MachineBasicBlock &B) -> bool {
294 for (MachineBasicBlock *S : B.successors())
295 DefOnEntry[S->getNumber()] = true;
296 DefOnEntry[BN] = true;
297 return true;
298 };
299
300 SetVector<unsigned> WorkList;
301 // Checking if the entry of MBB is reached by some def: add all predecessors
302 // that are potentially defined-on-exit to the work list.
303 for (MachineBasicBlock *P : MBB.predecessors())
304 WorkList.insert(P->getNumber());
305
306 for (unsigned i = 0; i != WorkList.size(); ++i) {
307 // Determine if the exit from the block is reached by some def.
308 unsigned N = WorkList[i];
309 MachineBasicBlock &B = *MF->getBlockNumbered(N);
310 if (Seen[N] && Map[&B].first != nullptr)
311 return MarkDefined(B);
312 SlotIndex Begin, End;
313 std::tie(Begin, End) = Indexes->getMBBRange(&B);
314 LiveRange::iterator UB = std::upper_bound(LR.begin(), LR.end(), End);
315 if (UB != LR.begin()) {
316 LiveRange::Segment &Seg = *std::prev(UB);
317 if (Seg.end > Begin) {
318 // There is a segment that overlaps B. If the range is not explicitly
319 // undefined between the end of the segment and the end of the block,
320 // treat the block as defined on exit. If it is, go to the next block
321 // on the work list.
322 if (LR.isUndefIn(Undefs, Seg.end, End))
323 continue;
324 return MarkDefined(B);
325 }
326 }
327
328 // No segment overlaps with this block. If this block is not defined on
329 // entry, or it undefines the range, do not process its predecessors.
330 if (UndefOnEntry[N] || LR.isUndefIn(Undefs, Begin, End)) {
331 UndefOnEntry[N] = true;
332 continue;
333 }
334 if (DefOnEntry[N])
335 return MarkDefined(B);
336
337 // Still don't know: add all predecessors to the work list.
338 for (MachineBasicBlock *P : B.predecessors())
339 WorkList.insert(P->getNumber());
340 }
341
342 UndefOnEntry[BN] = true;
343 return false;
344}
345
Matthias Braun11042c82015-02-18 01:50:52 +0000346bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000347 SlotIndex Use, unsigned PhysReg,
348 ArrayRef<SlotIndex> Undefs) {
Matthias Braun11042c82015-02-18 01:50:52 +0000349 unsigned UseMBBNum = UseMBB.getNumber();
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000350
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000351 // Block numbers where LR should be live-in.
Matthias Braun11042c82015-02-18 01:50:52 +0000352 SmallVector<unsigned, 16> WorkList(1, UseMBBNum);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000353
354 // Remember if we have seen more than one value.
355 bool UniqueVNI = true;
Craig Topperc0196b12014-04-14 00:51:57 +0000356 VNInfo *TheVNI = nullptr;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000357
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000358 bool FoundUndef = false;
359
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000360 // Using Seen as a visited set, perform a BFS for all reaching defs.
361 for (unsigned i = 0; i != WorkList.size(); ++i) {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000362 MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000363
364#ifndef NDEBUG
Matthias Braune1d96282016-09-19 16:49:45 +0000365 if (MBB->pred_empty()) {
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000366 MBB->getParent()->verify();
Matthias Braun53917542015-05-11 18:47:47 +0000367 errs() << "Use of " << PrintReg(PhysReg)
368 << " does not have a corresponding definition on every path:\n";
369 const MachineInstr *MI = Indexes->getInstructionFromIndex(Use);
370 if (MI != nullptr)
371 errs() << Use << " " << *MI;
Matthias Braune1d96282016-09-19 16:49:45 +0000372 report_fatal_error("Use not jointly dominated by defs.");
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000373 }
374
375 if (TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
376 !MBB->isLiveIn(PhysReg)) {
377 MBB->getParent()->verify();
Matt Arsenaultf3d1a1a2016-09-03 06:57:49 +0000378 const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
379 errs() << "The register " << PrintReg(PhysReg, TRI)
Matthias Braun53917542015-05-11 18:47:47 +0000380 << " needs to be live in to BB#" << MBB->getNumber()
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000381 << ", but is missing from the live-in list.\n";
Matthias Braune1d96282016-09-19 16:49:45 +0000382 report_fatal_error("Invalid global physical register");
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000383 }
384#endif
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000385 FoundUndef |= MBB->pred_empty();
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000386
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000387 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000388 PE = MBB->pred_end(); PI != PE; ++PI) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000389 MachineBasicBlock *Pred = *PI;
390
391 // Is this a known live-out block?
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000392 if (Seen.test(Pred->getNumber())) {
393 if (VNInfo *VNI = Map[Pred].first) {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000394 if (TheVNI && TheVNI != VNI)
395 UniqueVNI = false;
396 TheVNI = VNI;
397 }
398 continue;
399 }
400
401 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000402 std::tie(Start, End) = Indexes->getMBBRange(Pred);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000403
404 // First time we see Pred. Try to determine the live-out value, but set
405 // it as null if Pred is live-through with an unknown value.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000406 auto EP = LR.extendInBlock(Undefs, Start, End);
407 VNInfo *VNI = EP.first;
408 FoundUndef |= EP.second;
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000409 setLiveOutValue(Pred, VNI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000410 if (VNI) {
411 if (TheVNI && TheVNI != VNI)
412 UniqueVNI = false;
413 TheVNI = VNI;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000414 }
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000415 if (VNI || EP.second)
416 continue;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000417
418 // No, we need a live-in value for Pred as well
Matthias Braun11042c82015-02-18 01:50:52 +0000419 if (Pred != &UseMBB)
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000420 WorkList.push_back(Pred->getNumber());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000421 else
Matthias Braun11042c82015-02-18 01:50:52 +0000422 // Loopback to UseMBB, so value is really live through.
423 Use = SlotIndex();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000424 }
425 }
426
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000427 LiveIn.clear();
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000428 FoundUndef |= (TheVNI == nullptr);
429 if (Undefs.size() > 0 && FoundUndef)
430 UniqueVNI = false;
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000431
432 // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
433 // neither require it. Skip the sorting overhead for small updates.
434 if (WorkList.size() > 4)
435 array_pod_sort(WorkList.begin(), WorkList.end());
436
437 // If a unique reaching def was found, blit in the live ranges immediately.
438 if (UniqueVNI) {
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000439 assert(TheVNI != nullptr);
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000440 LiveRangeUpdater Updater(&LR);
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000441 for (unsigned BN : WorkList) {
442 SlotIndex Start, End;
443 std::tie(Start, End) = Indexes->getMBBRange(BN);
444 // Trim the live range in UseMBB.
445 if (BN == UseMBBNum && Use.isValid())
446 End = Use;
447 else
448 Map[MF->getBlockNumbered(BN)] = LiveOutPair(TheVNI, nullptr);
449 Updater.add(Start, End, TheVNI);
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000450 }
451 return true;
452 }
453
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000454 // Prepare the defined/undefined bit vectors.
455 auto EF = EntryInfoMap.find(&LR);
456 if (EF == EntryInfoMap.end()) {
457 unsigned N = MF->getNumBlockIDs();
458 EF = EntryInfoMap.insert({&LR, {BitVector(), BitVector()}}).first;
459 EF->second.first.resize(N);
460 EF->second.second.resize(N);
461 }
462 BitVector &DefOnEntry = EF->second.first;
463 BitVector &UndefOnEntry = EF->second.second;
464
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000465 // Multiple values were found, so transfer the work list to the LiveIn array
466 // where UpdateSSA will use it as a work list.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000467 LiveIn.reserve(WorkList.size());
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000468 for (unsigned BN : WorkList) {
469 MachineBasicBlock *MBB = MF->getBlockNumbered(BN);
470 if (Undefs.size() > 0 && !isDefOnEntry(LR, Undefs, *MBB, DefOnEntry, UndefOnEntry))
471 continue;
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000472 addLiveInBlock(LR, DomTree->getNode(MBB));
Matthias Braun11042c82015-02-18 01:50:52 +0000473 if (MBB == &UseMBB)
474 LiveIn.back().Kill = Use;
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000475 }
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000476
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000477 return false;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000478}
479
480
481// This is essentially the same iterative algorithm that SSAUpdater uses,
482// except we already have a dominator tree, so we don't have to recompute it.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000483void LiveRangeCalc::updateSSA() {
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000484 assert(Indexes && "Missing SlotIndexes");
485 assert(DomTree && "Missing dominator tree");
486
487 // Interate until convergence.
488 unsigned Changes;
489 do {
490 Changes = 0;
491 // Propagate live-out values down the dominator tree, inserting phi-defs
492 // when necessary.
Matthias Braun42fab342014-12-15 19:40:46 +0000493 for (LiveInBlock &I : LiveIn) {
494 MachineDomTreeNode *Node = I.DomNode;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000495 // Skip block if the live-in value has already been determined.
496 if (!Node)
497 continue;
498 MachineBasicBlock *MBB = Node->getBlock();
499 MachineDomTreeNode *IDom = Node->getIDom();
500 LiveOutPair IDomValue;
501
502 // We need a live-in value to a block with no immediate dominator?
503 // This is probably an unreachable block that has survived somehow.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000504 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000505
506 // IDom dominates all of our predecessors, but it may not be their
507 // immediate dominator. Check if any of them have live-out values that are
508 // properly dominated by IDom. If so, we need a phi-def here.
509 if (!needPHI) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000510 IDomValue = Map[IDom->getBlock()];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000511
512 // Cache the DomTree node that defined the value.
513 if (IDomValue.first && !IDomValue.second)
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000514 Map[IDom->getBlock()].second = IDomValue.second =
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000515 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
516
517 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
518 PE = MBB->pred_end(); PI != PE; ++PI) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000519 LiveOutPair &Value = Map[*PI];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000520 if (!Value.first || Value.first == IDomValue.first)
521 continue;
522
523 // Cache the DomTree node that defined the value.
524 if (!Value.second)
525 Value.second =
526 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
527
528 // This predecessor is carrying something other than IDomValue.
529 // It could be because IDomValue hasn't propagated yet, or it could be
530 // because MBB is in the dominance frontier of that value.
531 if (DomTree->dominates(IDom, Value.second)) {
532 needPHI = true;
533 break;
534 }
535 }
536 }
537
538 // The value may be live-through even if Kill is set, as can happen when
539 // we are called from extendRange. In that case LiveOutSeen is true, and
540 // LiveOut indicates a foreign or missing value.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000541 LiveOutPair &LOP = Map[MBB];
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000542
543 // Create a phi-def if required.
544 if (needPHI) {
545 ++Changes;
546 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
547 SlotIndex Start, End;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +0000548 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Matthias Braun42fab342014-12-15 19:40:46 +0000549 LiveRange &LR = I.LR;
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000550 VNInfo *VNI = LR.getNextValue(Start, *Alloc);
Matthias Braun42fab342014-12-15 19:40:46 +0000551 I.Value = VNI;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000552 // This block is done, we know the final value.
Matthias Braun42fab342014-12-15 19:40:46 +0000553 I.DomNode = nullptr;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000554
Matthias Braun2f662322014-12-10 01:12:12 +0000555 // Add liveness since updateFromLiveIns now skips this node.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000556 if (I.Kill.isValid()) {
557 if (VNI)
558 LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI));
559 } else {
560 if (VNI)
561 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000562 LOP = LiveOutPair(VNI, Node);
563 }
564 } else if (IDomValue.first) {
565 // No phi-def here. Remember incoming value.
Matthias Braun42fab342014-12-15 19:40:46 +0000566 I.Value = IDomValue.first;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000567
568 // If the IDomValue is killed in the block, don't propagate through.
Matthias Braun42fab342014-12-15 19:40:46 +0000569 if (I.Kill.isValid())
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000570 continue;
571
572 // Propagate IDomValue if it isn't killed:
573 // MBB is live-out and doesn't define its own value.
574 if (LOP.first == IDomValue.first)
575 continue;
576 ++Changes;
577 LOP = IDomValue;
578 }
579 }
580 } while (Changes);
581}