blob: 70e135ab1aff98dfe631a2d2b62d61da1b39279d [file] [log] [blame]
Eugene Zelenko2de563a2017-08-24 21:21:39 +00001//===- LiveRangeCalc.cpp - Calculate live ranges --------------------------===//
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +00002//
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 Olesenb5a457c2011-09-13 01:34:21 +000014#include "LiveRangeCalc.h"
Eugene Zelenko2de563a2017-08-24 21:21:39 +000015#include "llvm/ADT/BitVector.h"
16#include "llvm/ADT/STLExtras.h"
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +000017#include "llvm/ADT/SetVector.h"
Eugene Zelenko2de563a2017-08-24 21:21:39 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/CodeGen/LiveInterval.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000021#include "llvm/CodeGen/MachineDominators.h"
Eugene Zelenko2de563a2017-08-24 21:21:39 +000022#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstr.h"
24#include "llvm/CodeGen/MachineOperand.h"
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko2de563a2017-08-24 21:21:39 +000026#include "llvm/CodeGen/SlotIndexes.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000027#include "llvm/CodeGen/TargetRegisterInfo.h"
Eugene Zelenko2de563a2017-08-24 21:21:39 +000028#include "llvm/MC/LaneBitmask.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
Eugene Zelenko2de563a2017-08-24 21:21:39 +000031#include <algorithm>
32#include <cassert>
33#include <iterator>
34#include <tuple>
35#include <utility>
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000036
37using namespace llvm;
38
Chandler Carruth8677f2f2014-04-22 02:02:50 +000039#define DEBUG_TYPE "regalloc"
40
Krzysztof Parzyszeka432d582017-06-27 21:30:46 +000041// Reserve an address that indicates a value that is known to be "undef".
42static VNInfo UndefVNI(0xbad, SlotIndex());
43
Matthias Braun68445712014-12-16 04:03:38 +000044void LiveRangeCalc::resetLiveOutMap() {
45 unsigned NumBlocks = MF->getNumBlockIDs();
46 Seen.clear();
47 Seen.resize(NumBlocks);
Matthias Braun41308c92017-06-27 18:05:26 +000048 EntryInfos.clear();
Matthias Braun68445712014-12-16 04:03:38 +000049 Map.resize(NumBlocks);
50}
51
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +000052void LiveRangeCalc::reset(const MachineFunction *mf,
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000053 SlotIndexes *SI,
54 MachineDominatorTree *MDT,
55 VNInfo::Allocator *VNIA) {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +000056 MF = mf;
Jakob Stoklund Olesen631390e2012-06-04 18:21:16 +000057 MRI = &MF->getRegInfo();
58 Indexes = SI;
59 DomTree = MDT;
60 Alloc = VNIA;
Matthias Braun68445712014-12-16 04:03:38 +000061 resetLiveOutMap();
Matthias Braun4151e892014-12-15 21:36:35 +000062 LiveIn.clear();
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +000063}
64
Matthias Braun68445712014-12-16 04:03:38 +000065static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc,
66 LiveRange &LR, const MachineOperand &MO) {
Duncan P. N. Exon Smith42e18352016-02-27 06:40:41 +000067 const MachineInstr &MI = *MO.getParent();
68 SlotIndex DefIdx =
69 Indexes.getInstructionIndex(MI).getRegSlot(MO.isEarlyClobber());
Matthias Braun4151e892014-12-15 21:36:35 +000070
Duncan P. N. Exon Smith42e18352016-02-27 06:40:41 +000071 // Create the def in LR. This may find an existing def.
72 LR.createDeadDef(DefIdx, Alloc);
Matthias Braunc66fa842014-12-10 01:12:12 +000073}
74
Matthias Braun5101c892015-03-19 00:21:58 +000075void LiveRangeCalc::calculate(LiveInterval &LI, bool TrackSubRegs) {
Matthias Braunc66fa842014-12-10 01:12:12 +000076 assert(MRI && Indexes && "call reset() first");
77
Matthias Braun68445712014-12-16 04:03:38 +000078 // Step 1: Create minimal live segments for every definition of Reg.
Matthias Braunc66fa842014-12-10 01:12:12 +000079 // Visit all def operands. If the same instruction has multiple defs of Reg,
Matthias Braun68445712014-12-16 04:03:38 +000080 // createDeadDef() will deduplicate.
Matthias Braunc66fa842014-12-10 01:12:12 +000081 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
82 unsigned Reg = LI.reg;
Matthias Braun68445712014-12-16 04:03:38 +000083 for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
84 if (!MO.isDef() && !MO.readsReg())
85 continue;
86
Matthias Braunc66fa842014-12-10 01:12:12 +000087 unsigned SubReg = MO.getSubReg();
Matthias Braun5101c892015-03-19 00:21:58 +000088 if (LI.hasSubRanges() || (SubReg != 0 && TrackSubRegs)) {
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +000089 LaneBitmask SubMask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg)
Krzysztof Parzyszekadef5a62016-08-29 13:15:35 +000090 : MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braunc66fa842014-12-10 01:12:12 +000091 // If this is the first time we see a subregister def, initialize
92 // subranges by creating a copy of the main range.
93 if (!LI.hasSubRanges() && !LI.empty()) {
Matthias Braundfc5b652015-09-25 21:51:14 +000094 LaneBitmask ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
Matthias Braunc66fa842014-12-10 01:12:12 +000095 LI.createSubRangeFrom(*Alloc, ClassMask, LI);
96 }
97
Matthias Braunb6ebe7d2017-03-03 19:05:34 +000098 LI.refineSubRanges(*Alloc, SubMask,
99 [&MO, this](LiveInterval::SubRange &SR) {
Matthias Braun68445712014-12-16 04:03:38 +0000100 if (MO.isDef())
Matthias Braunb6ebe7d2017-03-03 19:05:34 +0000101 createDeadDef(*Indexes, *Alloc, SR, MO);
102 });
Matthias Braunc66fa842014-12-10 01:12:12 +0000103 }
104
Matthias Braun88824142014-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 Braun68445712014-12-16 04:03:38 +0000108 createDeadDef(*Indexes, *Alloc, LI, MO);
Matthias Braunc66fa842014-12-10 01:12:12 +0000109 }
Matthias Braun68445712014-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 Braun88824142014-12-24 02:11:51 +0000117 if (LI.hasSubRanges()) {
118 for (LiveInterval::SubRange &S : LI.subranges()) {
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000119 LiveRangeCalc SubLRC;
120 SubLRC.reset(MF, Indexes, DomTree, Alloc);
121 SubLRC.extendToUses(S, Reg, S.LaneMask, &LI);
Matthias Braun88824142014-12-24 02:11:51 +0000122 }
123 LI.clear();
Matthias Braun6054e842016-05-20 23:14:56 +0000124 constructMainRangeFromSubranges(LI);
Matthias Braun88824142014-12-24 02:11:51 +0000125 } else {
Matthias Braun68445712014-12-16 04:03:38 +0000126 resetLiveOutMap();
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +0000127 extendToUses(LI, Reg, LaneBitmask::getAll());
Matthias Braun68445712014-12-16 04:03:38 +0000128 }
Matthias Braunc66fa842014-12-10 01:12:12 +0000129}
130
Matthias Braun6054e842016-05-20 23:14:56 +0000131void LiveRangeCalc::constructMainRangeFromSubranges(LiveInterval &LI) {
132 // First create dead defs at all defs found in subranges.
133 LiveRange &MainRange = LI;
134 assert(MainRange.segments.empty() && MainRange.valnos.empty() &&
135 "Expect empty main liverange");
136
137 for (const LiveInterval::SubRange &SR : LI.subranges()) {
138 for (const VNInfo *VNI : SR.valnos) {
139 if (!VNI->isUnused() && !VNI->isPHIDef())
140 MainRange.createDeadDef(VNI->def, *Alloc);
141 }
142 }
Matthias Braun6054e842016-05-20 23:14:56 +0000143 resetLiveOutMap();
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +0000144 extendToUses(MainRange, LI.reg, LaneBitmask::getAll(), &LI);
Matthias Braun6054e842016-05-20 23:14:56 +0000145}
Matthias Braunc66fa842014-12-10 01:12:12 +0000146
Matthias Braun4151e892014-12-15 21:36:35 +0000147void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) {
Jakob Stoklund Olesen4e53a402012-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 Braune25dde52013-10-10 21:28:57 +0000151 // LR.createDeadDef() will deduplicate.
Matthias Braun68445712014-12-16 04:03:38 +0000152 for (MachineOperand &MO : MRI->def_operands(Reg))
153 createDeadDef(*Indexes, *Alloc, LR, MO);
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +0000154}
155
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000156void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask Mask,
157 LiveInterval *LI) {
158 SmallVector<SlotIndex, 4> Undefs;
159 if (LI != nullptr)
160 LI->computeSubRangeUndefs(Undefs, Mask, *MRI, *Indexes);
161
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +0000162 // Visit all operands that read Reg. This may include partial defs.
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +0000163 bool IsSubRange = !Mask.all();
Matthias Braun68445712014-12-16 04:03:38 +0000164 const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
Owen Anderson92fca732014-03-17 19:36:09 +0000165 for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
Jakob Stoklund Olesenf9dff0e2012-09-06 18:15:15 +0000166 // Clear all kill flags. They will be reinserted after register allocation
Matthias Braunfa621d22017-12-13 02:51:04 +0000167 // by LiveIntervals::addKillFlags().
Jakob Stoklund Olesenf9dff0e2012-09-06 18:15:15 +0000168 if (MO.isUse())
169 MO.setIsKill(false);
Krzysztof Parzyszek57c58d32016-09-02 19:48:55 +0000170 // MO::readsReg returns "true" for subregister defs. This is for keeping
171 // liveness of the entire register (i.e. for the main range of the live
172 // interval). For subranges, definitions of non-overlapping subregisters
173 // do not count as uses.
174 if (!MO.readsReg() || (IsSubRange && MO.isDef()))
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +0000175 continue;
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000176
Matthias Braun68445712014-12-16 04:03:38 +0000177 unsigned SubReg = MO.getSubReg();
178 if (SubReg != 0) {
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000179 LaneBitmask SLM = TRI.getSubRegIndexLaneMask(SubReg);
180 if (MO.isDef())
Krzysztof Parzyszekadef5a62016-08-29 13:15:35 +0000181 SLM = ~SLM;
182 // Ignore uses not reading the current (sub)range.
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +0000183 if ((SLM & Mask).none())
Matthias Braun68445712014-12-16 04:03:38 +0000184 continue;
185 }
186
187 // Determine the actual place of the use.
188 const MachineInstr *MI = MO.getParent();
189 unsigned OpNo = (&MO - &MI->getOperand(0));
190 SlotIndex UseIdx;
191 if (MI->isPHI()) {
192 assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
193 // The actual place where a phi operand is used is the end of the pred
194 // MBB. PHI operands are paired: (Reg, PredMBB).
195 UseIdx = Indexes->getMBBEndIdx(MI->getOperand(OpNo+1).getMBB());
196 } else {
197 // Check for early-clobber redefs.
198 bool isEarlyClobber = false;
199 unsigned DefIdx;
200 if (MO.isDef())
201 isEarlyClobber = MO.isEarlyClobber();
202 else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
203 // FIXME: This would be a lot easier if tied early-clobber uses also
204 // had an early-clobber flag.
205 isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber();
206 }
Duncan P. N. Exon Smith42e18352016-02-27 06:40:41 +0000207 UseIdx = Indexes->getInstructionIndex(*MI).getRegSlot(isEarlyClobber);
Matthias Braun68445712014-12-16 04:03:38 +0000208 }
209
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +0000210 // MI is reading Reg. We may have visited MI before if it happens to be
211 // reading Reg multiple times. That is OK, extend() is idempotent.
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000212 extend(LR, UseIdx, Reg, Undefs);
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +0000213 }
214}
215
Matthias Braun68445712014-12-16 04:03:38 +0000216void LiveRangeCalc::updateFromLiveIns() {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000217 LiveRangeUpdater Updater;
Matthias Braun908d6232014-12-15 19:40:46 +0000218 for (const LiveInBlock &I : LiveIn) {
219 if (!I.DomNode)
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000220 continue;
Matthias Braun908d6232014-12-15 19:40:46 +0000221 MachineBasicBlock *MBB = I.DomNode->getBlock();
222 assert(I.Value && "No live-in value found");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000223 SlotIndex Start, End;
Benjamin Kramera4f0aad2014-03-02 13:30:33 +0000224 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000225
Matthias Braun908d6232014-12-15 19:40:46 +0000226 if (I.Kill.isValid())
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000227 // Value is killed inside this block.
Matthias Braun908d6232014-12-15 19:40:46 +0000228 End = I.Kill;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000229 else {
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000230 // The value is live-through, update LiveOut as well.
231 // Defer the Domtree lookup until it is needed.
Matthias Braun68445712014-12-16 04:03:38 +0000232 assert(Seen.test(MBB->getNumber()));
233 Map[MBB] = LiveOutPair(I.Value, nullptr);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000234 }
Matthias Braun908d6232014-12-15 19:40:46 +0000235 Updater.setDest(&I.LR);
236 Updater.add(Start, End, I.Value);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000237 }
238 LiveIn.clear();
239}
240
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000241void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
242 ArrayRef<SlotIndex> Undefs) {
Matthias Braun62be98d2015-02-18 01:50:52 +0000243 assert(Use.isValid() && "Invalid SlotIndex");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000244 assert(Indexes && "Missing SlotIndexes");
245 assert(DomTree && "Missing dominator tree");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000246
Matthias Braun62be98d2015-02-18 01:50:52 +0000247 MachineBasicBlock *UseMBB = Indexes->getMBBFromIndex(Use.getPrevSlot());
248 assert(UseMBB && "No MBB at Use");
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000249
250 // Is there a def in the same MBB we can extend?
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000251 auto EP = LR.extendInBlock(Undefs, Indexes->getMBBStartIdx(UseMBB), Use);
252 if (EP.first != nullptr || EP.second)
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000253 return;
254
Matthias Braun62be98d2015-02-18 01:50:52 +0000255 // Find the single reaching def, or determine if Use is jointly dominated by
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000256 // multiple values, and we may need to create even more phi-defs to preserve
257 // VNInfo SSA form. Perform a search for all predecessor blocks where we
258 // know the dominating VNInfo.
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000259 if (findReachingDefs(LR, *UseMBB, Use, PhysReg, Undefs))
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000260 return;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000261
262 // When there were multiple different values, we may need new PHIs.
Matthias Braun68445712014-12-16 04:03:38 +0000263 calculateValues();
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000264}
265
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000266// This function is called by a client after using the low-level API to add
267// live-out and live-in blocks. The unique value optimization is not
268// available, SplitEditor::transferValues handles that case directly anyway.
Matthias Braun68445712014-12-16 04:03:38 +0000269void LiveRangeCalc::calculateValues() {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000270 assert(Indexes && "Missing SlotIndexes");
271 assert(DomTree && "Missing dominator tree");
Matthias Braun68445712014-12-16 04:03:38 +0000272 updateSSA();
273 updateFromLiveIns();
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000274}
275
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000276bool LiveRangeCalc::isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
277 MachineBasicBlock &MBB, BitVector &DefOnEntry,
278 BitVector &UndefOnEntry) {
279 unsigned BN = MBB.getNumber();
280 if (DefOnEntry[BN])
281 return true;
282 if (UndefOnEntry[BN])
283 return false;
284
Malcolm Parsons60f78e32017-01-13 17:12:16 +0000285 auto MarkDefined = [BN, &DefOnEntry](MachineBasicBlock &B) -> bool {
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000286 for (MachineBasicBlock *S : B.successors())
287 DefOnEntry[S->getNumber()] = true;
288 DefOnEntry[BN] = true;
289 return true;
290 };
291
292 SetVector<unsigned> WorkList;
293 // Checking if the entry of MBB is reached by some def: add all predecessors
294 // that are potentially defined-on-exit to the work list.
295 for (MachineBasicBlock *P : MBB.predecessors())
296 WorkList.insert(P->getNumber());
297
298 for (unsigned i = 0; i != WorkList.size(); ++i) {
299 // Determine if the exit from the block is reached by some def.
300 unsigned N = WorkList[i];
301 MachineBasicBlock &B = *MF->getBlockNumbered(N);
Krzysztof Parzyszeka432d582017-06-27 21:30:46 +0000302 if (Seen[N]) {
303 const LiveOutPair &LOB = Map[&B];
304 if (LOB.first != nullptr && LOB.first != &UndefVNI)
305 return MarkDefined(B);
306 }
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000307 SlotIndex Begin, End;
308 std::tie(Begin, End) = Indexes->getMBBRange(&B);
Krzysztof Parzyszeka8b917b2017-01-18 23:12:19 +0000309 // Treat End as not belonging to B.
310 // If LR has a segment S that starts at the next block, i.e. [End, ...),
311 // std::upper_bound will return the segment following S. Instead,
312 // S should be treated as the first segment that does not overlap B.
313 LiveRange::iterator UB = std::upper_bound(LR.begin(), LR.end(),
314 End.getPrevSlot());
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000315 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 Braun62be98d2015-02-18 01:50:52 +0000346bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000347 SlotIndex Use, unsigned PhysReg,
348 ArrayRef<SlotIndex> Undefs) {
Matthias Braun62be98d2015-02-18 01:50:52 +0000349 unsigned UseMBBNum = UseMBB.getNumber();
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000350
Matthias Braune25dde52013-10-10 21:28:57 +0000351 // Block numbers where LR should be live-in.
Matthias Braun62be98d2015-02-18 01:50:52 +0000352 SmallVector<unsigned, 16> WorkList(1, UseMBBNum);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000353
354 // Remember if we have seen more than one value.
355 bool UniqueVNI = true;
Craig Topper4ba84432014-04-14 00:51:57 +0000356 VNInfo *TheVNI = nullptr;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000357
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000358 bool FoundUndef = false;
359
Jakob Stoklund Olesenb5a457c2011-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 Olesenbeda6ab2013-02-20 23:08:26 +0000362 MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
Jakob Stoklund Olesenc8981f22012-07-13 23:39:05 +0000363
364#ifndef NDEBUG
Matthias Braunb0731962016-09-19 16:49:45 +0000365 if (MBB->pred_empty()) {
Jakob Stoklund Olesenc8981f22012-07-13 23:39:05 +0000366 MBB->getParent()->verify();
Matt Arsenault54d12f32018-10-30 01:11:31 +0000367 errs() << "Use of " << printReg(PhysReg, MRI->getTargetRegisterInfo())
Matthias Braun948b20e2015-05-11 18:47:47 +0000368 << " 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 Braunb0731962016-09-19 16:49:45 +0000372 report_fatal_error("Use not jointly dominated by defs.");
Jakob Stoklund Olesenc8981f22012-07-13 23:39:05 +0000373 }
374
375 if (TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
376 !MBB->isLiveIn(PhysReg)) {
377 MBB->getParent()->verify();
Matt Arsenault59ff7092016-09-03 06:57:49 +0000378 const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
Francis Visoiu Mistrihaccb3372017-11-28 12:42:37 +0000379 errs() << "The register " << printReg(PhysReg, TRI)
Francis Visoiu Mistrihca0df552017-12-04 17:18:51 +0000380 << " needs to be live in to " << printMBBReference(*MBB)
Jakob Stoklund Olesenc8981f22012-07-13 23:39:05 +0000381 << ", but is missing from the live-in list.\n";
Matthias Braunb0731962016-09-19 16:49:45 +0000382 report_fatal_error("Invalid global physical register");
Jakob Stoklund Olesenc8981f22012-07-13 23:39:05 +0000383 }
384#endif
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000385 FoundUndef |= MBB->pred_empty();
Jakob Stoklund Olesenc8981f22012-07-13 23:39:05 +0000386
Krzysztof Parzyszek7a4e5302017-06-28 16:02:00 +0000387 for (MachineBasicBlock *Pred : MBB->predecessors()) {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000388 // Is this a known live-out block?
Matthias Braun68445712014-12-16 04:03:38 +0000389 if (Seen.test(Pred->getNumber())) {
390 if (VNInfo *VNI = Map[Pred].first) {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000391 if (TheVNI && TheVNI != VNI)
392 UniqueVNI = false;
393 TheVNI = VNI;
394 }
395 continue;
396 }
397
398 SlotIndex Start, End;
Benjamin Kramera4f0aad2014-03-02 13:30:33 +0000399 std::tie(Start, End) = Indexes->getMBBRange(Pred);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000400
401 // First time we see Pred. Try to determine the live-out value, but set
402 // it as null if Pred is live-through with an unknown value.
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000403 auto EP = LR.extendInBlock(Undefs, Start, End);
404 VNInfo *VNI = EP.first;
405 FoundUndef |= EP.second;
Krzysztof Parzyszeka432d582017-06-27 21:30:46 +0000406 setLiveOutValue(Pred, EP.second ? &UndefVNI : VNI);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000407 if (VNI) {
408 if (TheVNI && TheVNI != VNI)
409 UniqueVNI = false;
410 TheVNI = VNI;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000411 }
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000412 if (VNI || EP.second)
413 continue;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000414
415 // No, we need a live-in value for Pred as well
Matthias Braun62be98d2015-02-18 01:50:52 +0000416 if (Pred != &UseMBB)
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000417 WorkList.push_back(Pred->getNumber());
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000418 else
Matthias Braun62be98d2015-02-18 01:50:52 +0000419 // Loopback to UseMBB, so value is really live through.
420 Use = SlotIndex();
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000421 }
422 }
423
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000424 LiveIn.clear();
Krzysztof Parzyszekd37294a2017-06-28 15:46:16 +0000425 FoundUndef |= (TheVNI == nullptr || TheVNI == &UndefVNI);
Eugene Zelenko2de563a2017-08-24 21:21:39 +0000426 if (!Undefs.empty() && FoundUndef)
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000427 UniqueVNI = false;
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000428
429 // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
430 // neither require it. Skip the sorting overhead for small updates.
431 if (WorkList.size() > 4)
432 array_pod_sort(WorkList.begin(), WorkList.end());
433
434 // If a unique reaching def was found, blit in the live ranges immediately.
435 if (UniqueVNI) {
Krzysztof Parzyszeka432d582017-06-27 21:30:46 +0000436 assert(TheVNI != nullptr && TheVNI != &UndefVNI);
Matthias Braune25dde52013-10-10 21:28:57 +0000437 LiveRangeUpdater Updater(&LR);
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000438 for (unsigned BN : WorkList) {
439 SlotIndex Start, End;
440 std::tie(Start, End) = Indexes->getMBBRange(BN);
441 // Trim the live range in UseMBB.
442 if (BN == UseMBBNum && Use.isValid())
443 End = Use;
444 else
445 Map[MF->getBlockNumbered(BN)] = LiveOutPair(TheVNI, nullptr);
446 Updater.add(Start, End, TheVNI);
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000447 }
448 return true;
449 }
450
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000451 // Prepare the defined/undefined bit vectors.
Matthias Braun41308c92017-06-27 18:05:26 +0000452 EntryInfoMap::iterator Entry;
453 bool DidInsert;
454 std::tie(Entry, DidInsert) = EntryInfos.insert(
455 std::make_pair(&LR, std::make_pair(BitVector(), BitVector())));
456 if (DidInsert) {
457 // Initialize newly inserted entries.
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000458 unsigned N = MF->getNumBlockIDs();
Matthias Braun41308c92017-06-27 18:05:26 +0000459 Entry->second.first.resize(N);
460 Entry->second.second.resize(N);
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000461 }
Matthias Braun41308c92017-06-27 18:05:26 +0000462 BitVector &DefOnEntry = Entry->second.first;
463 BitVector &UndefOnEntry = Entry->second.second;
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000464
Jakob Stoklund Olesenbeda6ab2013-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 Olesenb5a457c2011-09-13 01:34:21 +0000467 LiveIn.reserve(WorkList.size());
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000468 for (unsigned BN : WorkList) {
469 MachineBasicBlock *MBB = MF->getBlockNumbered(BN);
Eugene Zelenko2de563a2017-08-24 21:21:39 +0000470 if (!Undefs.empty() &&
Krzysztof Parzyszek7a4e5302017-06-28 16:02:00 +0000471 !isDefOnEntry(LR, Undefs, *MBB, DefOnEntry, UndefOnEntry))
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000472 continue;
Matthias Braune25dde52013-10-10 21:28:57 +0000473 addLiveInBlock(LR, DomTree->getNode(MBB));
Matthias Braun62be98d2015-02-18 01:50:52 +0000474 if (MBB == &UseMBB)
475 LiveIn.back().Kill = Use;
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000476 }
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000477
Jakob Stoklund Olesenbeda6ab2013-02-20 23:08:26 +0000478 return false;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000479}
480
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000481// 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 Braun68445712014-12-16 04:03:38 +0000483void LiveRangeCalc::updateSSA() {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000484 assert(Indexes && "Missing SlotIndexes");
485 assert(DomTree && "Missing dominator tree");
486
487 // Interate until convergence.
Krzysztof Parzyszek7a4e5302017-06-28 16:02:00 +0000488 bool Changed;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000489 do {
Krzysztof Parzyszek7a4e5302017-06-28 16:02:00 +0000490 Changed = false;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000491 // Propagate live-out values down the dominator tree, inserting phi-defs
492 // when necessary.
Matthias Braun908d6232014-12-15 19:40:46 +0000493 for (LiveInBlock &I : LiveIn) {
494 MachineDomTreeNode *Node = I.DomNode;
Jakob Stoklund Olesenb5a457c2011-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 Braun68445712014-12-16 04:03:38 +0000504 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
Jakob Stoklund Olesenb5a457c2011-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 Braun68445712014-12-16 04:03:38 +0000510 IDomValue = Map[IDom->getBlock()];
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000511
512 // Cache the DomTree node that defined the value.
Krzysztof Parzyszek7a4e5302017-06-28 16:02:00 +0000513 if (IDomValue.first && IDomValue.first != &UndefVNI &&
514 !IDomValue.second) {
515 Map[IDom->getBlock()].second = IDomValue.second =
516 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
517 }
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000518
Krzysztof Parzyszek7a4e5302017-06-28 16:02:00 +0000519 for (MachineBasicBlock *Pred : MBB->predecessors()) {
520 LiveOutPair &Value = Map[Pred];
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000521 if (!Value.first || Value.first == IDomValue.first)
522 continue;
Krzysztof Parzyszeka432d582017-06-27 21:30:46 +0000523 if (Value.first == &UndefVNI) {
524 needPHI = true;
525 break;
526 }
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000527
528 // Cache the DomTree node that defined the value.
529 if (!Value.second)
530 Value.second =
531 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
532
533 // This predecessor is carrying something other than IDomValue.
534 // It could be because IDomValue hasn't propagated yet, or it could be
535 // because MBB is in the dominance frontier of that value.
536 if (DomTree->dominates(IDom, Value.second)) {
537 needPHI = true;
538 break;
539 }
540 }
541 }
542
543 // The value may be live-through even if Kill is set, as can happen when
544 // we are called from extendRange. In that case LiveOutSeen is true, and
545 // LiveOut indicates a foreign or missing value.
Matthias Braun68445712014-12-16 04:03:38 +0000546 LiveOutPair &LOP = Map[MBB];
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000547
548 // Create a phi-def if required.
549 if (needPHI) {
Krzysztof Parzyszek7a4e5302017-06-28 16:02:00 +0000550 Changed = true;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000551 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
552 SlotIndex Start, End;
Benjamin Kramera4f0aad2014-03-02 13:30:33 +0000553 std::tie(Start, End) = Indexes->getMBBRange(MBB);
Matthias Braun908d6232014-12-15 19:40:46 +0000554 LiveRange &LR = I.LR;
Matthias Braune25dde52013-10-10 21:28:57 +0000555 VNInfo *VNI = LR.getNextValue(Start, *Alloc);
Matthias Braun908d6232014-12-15 19:40:46 +0000556 I.Value = VNI;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000557 // This block is done, we know the final value.
Matthias Braun908d6232014-12-15 19:40:46 +0000558 I.DomNode = nullptr;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000559
Matthias Braunc66fa842014-12-10 01:12:12 +0000560 // Add liveness since updateFromLiveIns now skips this node.
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000561 if (I.Kill.isValid()) {
562 if (VNI)
563 LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI));
564 } else {
565 if (VNI)
566 LR.addSegment(LiveInterval::Segment(Start, End, VNI));
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000567 LOP = LiveOutPair(VNI, Node);
568 }
Krzysztof Parzyszeka432d582017-06-27 21:30:46 +0000569 } else if (IDomValue.first && IDomValue.first != &UndefVNI) {
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000570 // No phi-def here. Remember incoming value.
Matthias Braun908d6232014-12-15 19:40:46 +0000571 I.Value = IDomValue.first;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000572
573 // If the IDomValue is killed in the block, don't propagate through.
Matthias Braun908d6232014-12-15 19:40:46 +0000574 if (I.Kill.isValid())
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000575 continue;
576
577 // Propagate IDomValue if it isn't killed:
578 // MBB is live-out and doesn't define its own value.
579 if (LOP.first == IDomValue.first)
580 continue;
Krzysztof Parzyszek7a4e5302017-06-28 16:02:00 +0000581 Changed = true;
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000582 LOP = IDomValue;
583 }
584 }
Krzysztof Parzyszek7a4e5302017-06-28 16:02:00 +0000585 } while (Changed);
Jakob Stoklund Olesenb5a457c2011-09-13 01:34:21 +0000586}
Krzysztof Parzyszek2c45bcb2018-06-26 14:37:16 +0000587
588bool LiveRangeCalc::isJointlyDominated(const MachineBasicBlock *MBB,
589 ArrayRef<SlotIndex> Defs,
590 const SlotIndexes &Indexes) {
591 const MachineFunction &MF = *MBB->getParent();
592 BitVector DefBlocks(MF.getNumBlockIDs());
593 for (SlotIndex I : Defs)
594 DefBlocks.set(Indexes.getMBBFromIndex(I)->getNumber());
595
596 SetVector<unsigned> PredQueue;
597 PredQueue.insert(MBB->getNumber());
598 for (unsigned i = 0; i != PredQueue.size(); ++i) {
599 unsigned BN = PredQueue[i];
600 if (DefBlocks[BN])
601 return true;
602 const MachineBasicBlock *B = MF.getBlockNumbered(BN);
603 for (const MachineBasicBlock *P : B->predecessors())
604 PredQueue.insert(P->getNumber());
605 }
606 return false;
607}