1*9880d681SAndroid Build Coastguard Worker //===---- LiveRangeCalc.cpp - Calculate live ranges -----------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // Implementation of the LiveRangeCalc class.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "LiveRangeCalc.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
17*9880d681SAndroid Build Coastguard Worker
18*9880d681SAndroid Build Coastguard Worker using namespace llvm;
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "regalloc"
21*9880d681SAndroid Build Coastguard Worker
resetLiveOutMap()22*9880d681SAndroid Build Coastguard Worker void LiveRangeCalc::resetLiveOutMap() {
23*9880d681SAndroid Build Coastguard Worker unsigned NumBlocks = MF->getNumBlockIDs();
24*9880d681SAndroid Build Coastguard Worker Seen.clear();
25*9880d681SAndroid Build Coastguard Worker Seen.resize(NumBlocks);
26*9880d681SAndroid Build Coastguard Worker Map.resize(NumBlocks);
27*9880d681SAndroid Build Coastguard Worker }
28*9880d681SAndroid Build Coastguard Worker
reset(const MachineFunction * mf,SlotIndexes * SI,MachineDominatorTree * MDT,VNInfo::Allocator * VNIA)29*9880d681SAndroid Build Coastguard Worker void LiveRangeCalc::reset(const MachineFunction *mf,
30*9880d681SAndroid Build Coastguard Worker SlotIndexes *SI,
31*9880d681SAndroid Build Coastguard Worker MachineDominatorTree *MDT,
32*9880d681SAndroid Build Coastguard Worker VNInfo::Allocator *VNIA) {
33*9880d681SAndroid Build Coastguard Worker MF = mf;
34*9880d681SAndroid Build Coastguard Worker MRI = &MF->getRegInfo();
35*9880d681SAndroid Build Coastguard Worker Indexes = SI;
36*9880d681SAndroid Build Coastguard Worker DomTree = MDT;
37*9880d681SAndroid Build Coastguard Worker Alloc = VNIA;
38*9880d681SAndroid Build Coastguard Worker resetLiveOutMap();
39*9880d681SAndroid Build Coastguard Worker LiveIn.clear();
40*9880d681SAndroid Build Coastguard Worker }
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Worker
createDeadDef(SlotIndexes & Indexes,VNInfo::Allocator & Alloc,LiveRange & LR,const MachineOperand & MO)43*9880d681SAndroid Build Coastguard Worker static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc,
44*9880d681SAndroid Build Coastguard Worker LiveRange &LR, const MachineOperand &MO) {
45*9880d681SAndroid Build Coastguard Worker const MachineInstr &MI = *MO.getParent();
46*9880d681SAndroid Build Coastguard Worker SlotIndex DefIdx =
47*9880d681SAndroid Build Coastguard Worker Indexes.getInstructionIndex(MI).getRegSlot(MO.isEarlyClobber());
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker // Create the def in LR. This may find an existing def.
50*9880d681SAndroid Build Coastguard Worker LR.createDeadDef(DefIdx, Alloc);
51*9880d681SAndroid Build Coastguard Worker }
52*9880d681SAndroid Build Coastguard Worker
calculate(LiveInterval & LI,bool TrackSubRegs)53*9880d681SAndroid Build Coastguard Worker void LiveRangeCalc::calculate(LiveInterval &LI, bool TrackSubRegs) {
54*9880d681SAndroid Build Coastguard Worker assert(MRI && Indexes && "call reset() first");
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker // Step 1: Create minimal live segments for every definition of Reg.
57*9880d681SAndroid Build Coastguard Worker // Visit all def operands. If the same instruction has multiple defs of Reg,
58*9880d681SAndroid Build Coastguard Worker // createDeadDef() will deduplicate.
59*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
60*9880d681SAndroid Build Coastguard Worker unsigned Reg = LI.reg;
61*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
62*9880d681SAndroid Build Coastguard Worker if (!MO.isDef() && !MO.readsReg())
63*9880d681SAndroid Build Coastguard Worker continue;
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker unsigned SubReg = MO.getSubReg();
66*9880d681SAndroid Build Coastguard Worker if (LI.hasSubRanges() || (SubReg != 0 && TrackSubRegs)) {
67*9880d681SAndroid Build Coastguard Worker LaneBitmask Mask = SubReg != 0 ? TRI.getSubRegIndexLaneMask(SubReg)
68*9880d681SAndroid Build Coastguard Worker : MRI->getMaxLaneMaskForVReg(Reg);
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard Worker // If this is the first time we see a subregister def, initialize
71*9880d681SAndroid Build Coastguard Worker // subranges by creating a copy of the main range.
72*9880d681SAndroid Build Coastguard Worker if (!LI.hasSubRanges() && !LI.empty()) {
73*9880d681SAndroid Build Coastguard Worker LaneBitmask ClassMask = MRI->getMaxLaneMaskForVReg(Reg);
74*9880d681SAndroid Build Coastguard Worker LI.createSubRangeFrom(*Alloc, ClassMask, LI);
75*9880d681SAndroid Build Coastguard Worker }
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker for (LiveInterval::SubRange &S : LI.subranges()) {
78*9880d681SAndroid Build Coastguard Worker // A Mask for subregs common to the existing subrange and current def.
79*9880d681SAndroid Build Coastguard Worker LaneBitmask Common = S.LaneMask & Mask;
80*9880d681SAndroid Build Coastguard Worker if (Common == 0)
81*9880d681SAndroid Build Coastguard Worker continue;
82*9880d681SAndroid Build Coastguard Worker // A Mask for subregs covered by the subrange but not the current def.
83*9880d681SAndroid Build Coastguard Worker LaneBitmask LRest = S.LaneMask & ~Mask;
84*9880d681SAndroid Build Coastguard Worker LiveInterval::SubRange *CommonRange;
85*9880d681SAndroid Build Coastguard Worker if (LRest != 0) {
86*9880d681SAndroid Build Coastguard Worker // Split current subrange into Common and LRest ranges.
87*9880d681SAndroid Build Coastguard Worker S.LaneMask = LRest;
88*9880d681SAndroid Build Coastguard Worker CommonRange = LI.createSubRangeFrom(*Alloc, Common, S);
89*9880d681SAndroid Build Coastguard Worker } else {
90*9880d681SAndroid Build Coastguard Worker assert(Common == S.LaneMask);
91*9880d681SAndroid Build Coastguard Worker CommonRange = &S;
92*9880d681SAndroid Build Coastguard Worker }
93*9880d681SAndroid Build Coastguard Worker if (MO.isDef())
94*9880d681SAndroid Build Coastguard Worker createDeadDef(*Indexes, *Alloc, *CommonRange, MO);
95*9880d681SAndroid Build Coastguard Worker Mask &= ~Common;
96*9880d681SAndroid Build Coastguard Worker }
97*9880d681SAndroid Build Coastguard Worker // Create a new SubRange for subregs we did not cover yet.
98*9880d681SAndroid Build Coastguard Worker if (Mask != 0) {
99*9880d681SAndroid Build Coastguard Worker LiveInterval::SubRange *NewRange = LI.createSubRange(*Alloc, Mask);
100*9880d681SAndroid Build Coastguard Worker if (MO.isDef())
101*9880d681SAndroid Build Coastguard Worker createDeadDef(*Indexes, *Alloc, *NewRange, MO);
102*9880d681SAndroid Build Coastguard Worker }
103*9880d681SAndroid Build Coastguard Worker }
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker // Create the def in the main liverange. We do not have to do this if
106*9880d681SAndroid Build Coastguard Worker // subranges are tracked as we recreate the main range later in this case.
107*9880d681SAndroid Build Coastguard Worker if (MO.isDef() && !LI.hasSubRanges())
108*9880d681SAndroid Build Coastguard Worker createDeadDef(*Indexes, *Alloc, LI, MO);
109*9880d681SAndroid Build Coastguard Worker }
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker // We may have created empty live ranges for partially undefined uses, we
112*9880d681SAndroid Build Coastguard Worker // can't keep them because we won't find defs in them later.
113*9880d681SAndroid Build Coastguard Worker LI.removeEmptySubRanges();
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker // Step 2: Extend live segments to all uses, constructing SSA form as
116*9880d681SAndroid Build Coastguard Worker // necessary.
117*9880d681SAndroid Build Coastguard Worker if (LI.hasSubRanges()) {
118*9880d681SAndroid Build Coastguard Worker for (LiveInterval::SubRange &S : LI.subranges()) {
119*9880d681SAndroid Build Coastguard Worker resetLiveOutMap();
120*9880d681SAndroid Build Coastguard Worker extendToUses(S, Reg, S.LaneMask);
121*9880d681SAndroid Build Coastguard Worker }
122*9880d681SAndroid Build Coastguard Worker LI.clear();
123*9880d681SAndroid Build Coastguard Worker constructMainRangeFromSubranges(LI);
124*9880d681SAndroid Build Coastguard Worker } else {
125*9880d681SAndroid Build Coastguard Worker resetLiveOutMap();
126*9880d681SAndroid Build Coastguard Worker extendToUses(LI, Reg, ~0u);
127*9880d681SAndroid Build Coastguard Worker }
128*9880d681SAndroid Build Coastguard Worker }
129*9880d681SAndroid Build Coastguard Worker
constructMainRangeFromSubranges(LiveInterval & LI)130*9880d681SAndroid Build Coastguard Worker void LiveRangeCalc::constructMainRangeFromSubranges(LiveInterval &LI) {
131*9880d681SAndroid Build Coastguard Worker // First create dead defs at all defs found in subranges.
132*9880d681SAndroid Build Coastguard Worker LiveRange &MainRange = LI;
133*9880d681SAndroid Build Coastguard Worker assert(MainRange.segments.empty() && MainRange.valnos.empty() &&
134*9880d681SAndroid Build Coastguard Worker "Expect empty main liverange");
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker for (const LiveInterval::SubRange &SR : LI.subranges()) {
137*9880d681SAndroid Build Coastguard Worker for (const VNInfo *VNI : SR.valnos) {
138*9880d681SAndroid Build Coastguard Worker if (!VNI->isUnused() && !VNI->isPHIDef())
139*9880d681SAndroid Build Coastguard Worker MainRange.createDeadDef(VNI->def, *Alloc);
140*9880d681SAndroid Build Coastguard Worker }
141*9880d681SAndroid Build Coastguard Worker }
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker resetLiveOutMap();
144*9880d681SAndroid Build Coastguard Worker extendToUses(MainRange, LI.reg);
145*9880d681SAndroid Build Coastguard Worker }
146*9880d681SAndroid Build Coastguard Worker
createDeadDefs(LiveRange & LR,unsigned Reg)147*9880d681SAndroid Build Coastguard Worker void LiveRangeCalc::createDeadDefs(LiveRange &LR, unsigned Reg) {
148*9880d681SAndroid Build Coastguard Worker assert(MRI && Indexes && "call reset() first");
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard Worker // Visit all def operands. If the same instruction has multiple defs of Reg,
151*9880d681SAndroid Build Coastguard Worker // LR.createDeadDef() will deduplicate.
152*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : MRI->def_operands(Reg))
153*9880d681SAndroid Build Coastguard Worker createDeadDef(*Indexes, *Alloc, LR, MO);
154*9880d681SAndroid Build Coastguard Worker }
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker
extendToUses(LiveRange & LR,unsigned Reg,LaneBitmask Mask)157*9880d681SAndroid Build Coastguard Worker void LiveRangeCalc::extendToUses(LiveRange &LR, unsigned Reg,
158*9880d681SAndroid Build Coastguard Worker LaneBitmask Mask) {
159*9880d681SAndroid Build Coastguard Worker // Visit all operands that read Reg. This may include partial defs.
160*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &TRI = *MRI->getTargetRegisterInfo();
161*9880d681SAndroid Build Coastguard Worker for (MachineOperand &MO : MRI->reg_nodbg_operands(Reg)) {
162*9880d681SAndroid Build Coastguard Worker // Clear all kill flags. They will be reinserted after register allocation
163*9880d681SAndroid Build Coastguard Worker // by LiveIntervalAnalysis::addKillFlags().
164*9880d681SAndroid Build Coastguard Worker if (MO.isUse())
165*9880d681SAndroid Build Coastguard Worker MO.setIsKill(false);
166*9880d681SAndroid Build Coastguard Worker else {
167*9880d681SAndroid Build Coastguard Worker // We only care about uses, but on the main range (mask ~0u) this includes
168*9880d681SAndroid Build Coastguard Worker // the "virtual" reads happening for subregister defs.
169*9880d681SAndroid Build Coastguard Worker if (Mask != ~0u)
170*9880d681SAndroid Build Coastguard Worker continue;
171*9880d681SAndroid Build Coastguard Worker }
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker if (!MO.readsReg())
174*9880d681SAndroid Build Coastguard Worker continue;
175*9880d681SAndroid Build Coastguard Worker unsigned SubReg = MO.getSubReg();
176*9880d681SAndroid Build Coastguard Worker if (SubReg != 0) {
177*9880d681SAndroid Build Coastguard Worker LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(SubReg);
178*9880d681SAndroid Build Coastguard Worker // Ignore uses not covering the current subrange.
179*9880d681SAndroid Build Coastguard Worker if ((SubRegMask & Mask) == 0)
180*9880d681SAndroid Build Coastguard Worker continue;
181*9880d681SAndroid Build Coastguard Worker }
182*9880d681SAndroid Build Coastguard Worker
183*9880d681SAndroid Build Coastguard Worker // Determine the actual place of the use.
184*9880d681SAndroid Build Coastguard Worker const MachineInstr *MI = MO.getParent();
185*9880d681SAndroid Build Coastguard Worker unsigned OpNo = (&MO - &MI->getOperand(0));
186*9880d681SAndroid Build Coastguard Worker SlotIndex UseIdx;
187*9880d681SAndroid Build Coastguard Worker if (MI->isPHI()) {
188*9880d681SAndroid Build Coastguard Worker assert(!MO.isDef() && "Cannot handle PHI def of partial register.");
189*9880d681SAndroid Build Coastguard Worker // The actual place where a phi operand is used is the end of the pred
190*9880d681SAndroid Build Coastguard Worker // MBB. PHI operands are paired: (Reg, PredMBB).
191*9880d681SAndroid Build Coastguard Worker UseIdx = Indexes->getMBBEndIdx(MI->getOperand(OpNo+1).getMBB());
192*9880d681SAndroid Build Coastguard Worker } else {
193*9880d681SAndroid Build Coastguard Worker // Check for early-clobber redefs.
194*9880d681SAndroid Build Coastguard Worker bool isEarlyClobber = false;
195*9880d681SAndroid Build Coastguard Worker unsigned DefIdx;
196*9880d681SAndroid Build Coastguard Worker if (MO.isDef())
197*9880d681SAndroid Build Coastguard Worker isEarlyClobber = MO.isEarlyClobber();
198*9880d681SAndroid Build Coastguard Worker else if (MI->isRegTiedToDefOperand(OpNo, &DefIdx)) {
199*9880d681SAndroid Build Coastguard Worker // FIXME: This would be a lot easier if tied early-clobber uses also
200*9880d681SAndroid Build Coastguard Worker // had an early-clobber flag.
201*9880d681SAndroid Build Coastguard Worker isEarlyClobber = MI->getOperand(DefIdx).isEarlyClobber();
202*9880d681SAndroid Build Coastguard Worker }
203*9880d681SAndroid Build Coastguard Worker UseIdx = Indexes->getInstructionIndex(*MI).getRegSlot(isEarlyClobber);
204*9880d681SAndroid Build Coastguard Worker }
205*9880d681SAndroid Build Coastguard Worker
206*9880d681SAndroid Build Coastguard Worker // MI is reading Reg. We may have visited MI before if it happens to be
207*9880d681SAndroid Build Coastguard Worker // reading Reg multiple times. That is OK, extend() is idempotent.
208*9880d681SAndroid Build Coastguard Worker extend(LR, UseIdx, Reg);
209*9880d681SAndroid Build Coastguard Worker }
210*9880d681SAndroid Build Coastguard Worker }
211*9880d681SAndroid Build Coastguard Worker
212*9880d681SAndroid Build Coastguard Worker
updateFromLiveIns()213*9880d681SAndroid Build Coastguard Worker void LiveRangeCalc::updateFromLiveIns() {
214*9880d681SAndroid Build Coastguard Worker LiveRangeUpdater Updater;
215*9880d681SAndroid Build Coastguard Worker for (const LiveInBlock &I : LiveIn) {
216*9880d681SAndroid Build Coastguard Worker if (!I.DomNode)
217*9880d681SAndroid Build Coastguard Worker continue;
218*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = I.DomNode->getBlock();
219*9880d681SAndroid Build Coastguard Worker assert(I.Value && "No live-in value found");
220*9880d681SAndroid Build Coastguard Worker SlotIndex Start, End;
221*9880d681SAndroid Build Coastguard Worker std::tie(Start, End) = Indexes->getMBBRange(MBB);
222*9880d681SAndroid Build Coastguard Worker
223*9880d681SAndroid Build Coastguard Worker if (I.Kill.isValid())
224*9880d681SAndroid Build Coastguard Worker // Value is killed inside this block.
225*9880d681SAndroid Build Coastguard Worker End = I.Kill;
226*9880d681SAndroid Build Coastguard Worker else {
227*9880d681SAndroid Build Coastguard Worker // The value is live-through, update LiveOut as well.
228*9880d681SAndroid Build Coastguard Worker // Defer the Domtree lookup until it is needed.
229*9880d681SAndroid Build Coastguard Worker assert(Seen.test(MBB->getNumber()));
230*9880d681SAndroid Build Coastguard Worker Map[MBB] = LiveOutPair(I.Value, nullptr);
231*9880d681SAndroid Build Coastguard Worker }
232*9880d681SAndroid Build Coastguard Worker Updater.setDest(&I.LR);
233*9880d681SAndroid Build Coastguard Worker Updater.add(Start, End, I.Value);
234*9880d681SAndroid Build Coastguard Worker }
235*9880d681SAndroid Build Coastguard Worker LiveIn.clear();
236*9880d681SAndroid Build Coastguard Worker }
237*9880d681SAndroid Build Coastguard Worker
238*9880d681SAndroid Build Coastguard Worker
extend(LiveRange & LR,SlotIndex Use,unsigned PhysReg)239*9880d681SAndroid Build Coastguard Worker void LiveRangeCalc::extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg) {
240*9880d681SAndroid Build Coastguard Worker assert(Use.isValid() && "Invalid SlotIndex");
241*9880d681SAndroid Build Coastguard Worker assert(Indexes && "Missing SlotIndexes");
242*9880d681SAndroid Build Coastguard Worker assert(DomTree && "Missing dominator tree");
243*9880d681SAndroid Build Coastguard Worker
244*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *UseMBB = Indexes->getMBBFromIndex(Use.getPrevSlot());
245*9880d681SAndroid Build Coastguard Worker assert(UseMBB && "No MBB at Use");
246*9880d681SAndroid Build Coastguard Worker
247*9880d681SAndroid Build Coastguard Worker // Is there a def in the same MBB we can extend?
248*9880d681SAndroid Build Coastguard Worker if (LR.extendInBlock(Indexes->getMBBStartIdx(UseMBB), Use))
249*9880d681SAndroid Build Coastguard Worker return;
250*9880d681SAndroid Build Coastguard Worker
251*9880d681SAndroid Build Coastguard Worker // Find the single reaching def, or determine if Use is jointly dominated by
252*9880d681SAndroid Build Coastguard Worker // multiple values, and we may need to create even more phi-defs to preserve
253*9880d681SAndroid Build Coastguard Worker // VNInfo SSA form. Perform a search for all predecessor blocks where we
254*9880d681SAndroid Build Coastguard Worker // know the dominating VNInfo.
255*9880d681SAndroid Build Coastguard Worker if (findReachingDefs(LR, *UseMBB, Use, PhysReg))
256*9880d681SAndroid Build Coastguard Worker return;
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard Worker // When there were multiple different values, we may need new PHIs.
259*9880d681SAndroid Build Coastguard Worker calculateValues();
260*9880d681SAndroid Build Coastguard Worker }
261*9880d681SAndroid Build Coastguard Worker
262*9880d681SAndroid Build Coastguard Worker
263*9880d681SAndroid Build Coastguard Worker // This function is called by a client after using the low-level API to add
264*9880d681SAndroid Build Coastguard Worker // live-out and live-in blocks. The unique value optimization is not
265*9880d681SAndroid Build Coastguard Worker // available, SplitEditor::transferValues handles that case directly anyway.
calculateValues()266*9880d681SAndroid Build Coastguard Worker void LiveRangeCalc::calculateValues() {
267*9880d681SAndroid Build Coastguard Worker assert(Indexes && "Missing SlotIndexes");
268*9880d681SAndroid Build Coastguard Worker assert(DomTree && "Missing dominator tree");
269*9880d681SAndroid Build Coastguard Worker updateSSA();
270*9880d681SAndroid Build Coastguard Worker updateFromLiveIns();
271*9880d681SAndroid Build Coastguard Worker }
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard Worker
findReachingDefs(LiveRange & LR,MachineBasicBlock & UseMBB,SlotIndex Use,unsigned PhysReg)274*9880d681SAndroid Build Coastguard Worker bool LiveRangeCalc::findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
275*9880d681SAndroid Build Coastguard Worker SlotIndex Use, unsigned PhysReg) {
276*9880d681SAndroid Build Coastguard Worker unsigned UseMBBNum = UseMBB.getNumber();
277*9880d681SAndroid Build Coastguard Worker
278*9880d681SAndroid Build Coastguard Worker // Block numbers where LR should be live-in.
279*9880d681SAndroid Build Coastguard Worker SmallVector<unsigned, 16> WorkList(1, UseMBBNum);
280*9880d681SAndroid Build Coastguard Worker
281*9880d681SAndroid Build Coastguard Worker // Remember if we have seen more than one value.
282*9880d681SAndroid Build Coastguard Worker bool UniqueVNI = true;
283*9880d681SAndroid Build Coastguard Worker VNInfo *TheVNI = nullptr;
284*9880d681SAndroid Build Coastguard Worker
285*9880d681SAndroid Build Coastguard Worker // Using Seen as a visited set, perform a BFS for all reaching defs.
286*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != WorkList.size(); ++i) {
287*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = MF->getBlockNumbered(WorkList[i]);
288*9880d681SAndroid Build Coastguard Worker
289*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
290*9880d681SAndroid Build Coastguard Worker if (MBB->pred_empty()) {
291*9880d681SAndroid Build Coastguard Worker MBB->getParent()->verify();
292*9880d681SAndroid Build Coastguard Worker errs() << "Use of " << PrintReg(PhysReg)
293*9880d681SAndroid Build Coastguard Worker << " does not have a corresponding definition on every path:\n";
294*9880d681SAndroid Build Coastguard Worker const MachineInstr *MI = Indexes->getInstructionFromIndex(Use);
295*9880d681SAndroid Build Coastguard Worker if (MI != nullptr)
296*9880d681SAndroid Build Coastguard Worker errs() << Use << " " << *MI;
297*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Use not jointly dominated by defs.");
298*9880d681SAndroid Build Coastguard Worker }
299*9880d681SAndroid Build Coastguard Worker
300*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
301*9880d681SAndroid Build Coastguard Worker !MBB->isLiveIn(PhysReg)) {
302*9880d681SAndroid Build Coastguard Worker MBB->getParent()->verify();
303*9880d681SAndroid Build Coastguard Worker errs() << "The register " << PrintReg(PhysReg)
304*9880d681SAndroid Build Coastguard Worker << " needs to be live in to BB#" << MBB->getNumber()
305*9880d681SAndroid Build Coastguard Worker << ", but is missing from the live-in list.\n";
306*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Invalid global physical register");
307*9880d681SAndroid Build Coastguard Worker }
308*9880d681SAndroid Build Coastguard Worker #endif
309*9880d681SAndroid Build Coastguard Worker
310*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
311*9880d681SAndroid Build Coastguard Worker PE = MBB->pred_end(); PI != PE; ++PI) {
312*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Pred = *PI;
313*9880d681SAndroid Build Coastguard Worker
314*9880d681SAndroid Build Coastguard Worker // Is this a known live-out block?
315*9880d681SAndroid Build Coastguard Worker if (Seen.test(Pred->getNumber())) {
316*9880d681SAndroid Build Coastguard Worker if (VNInfo *VNI = Map[Pred].first) {
317*9880d681SAndroid Build Coastguard Worker if (TheVNI && TheVNI != VNI)
318*9880d681SAndroid Build Coastguard Worker UniqueVNI = false;
319*9880d681SAndroid Build Coastguard Worker TheVNI = VNI;
320*9880d681SAndroid Build Coastguard Worker }
321*9880d681SAndroid Build Coastguard Worker continue;
322*9880d681SAndroid Build Coastguard Worker }
323*9880d681SAndroid Build Coastguard Worker
324*9880d681SAndroid Build Coastguard Worker SlotIndex Start, End;
325*9880d681SAndroid Build Coastguard Worker std::tie(Start, End) = Indexes->getMBBRange(Pred);
326*9880d681SAndroid Build Coastguard Worker
327*9880d681SAndroid Build Coastguard Worker // First time we see Pred. Try to determine the live-out value, but set
328*9880d681SAndroid Build Coastguard Worker // it as null if Pred is live-through with an unknown value.
329*9880d681SAndroid Build Coastguard Worker VNInfo *VNI = LR.extendInBlock(Start, End);
330*9880d681SAndroid Build Coastguard Worker setLiveOutValue(Pred, VNI);
331*9880d681SAndroid Build Coastguard Worker if (VNI) {
332*9880d681SAndroid Build Coastguard Worker if (TheVNI && TheVNI != VNI)
333*9880d681SAndroid Build Coastguard Worker UniqueVNI = false;
334*9880d681SAndroid Build Coastguard Worker TheVNI = VNI;
335*9880d681SAndroid Build Coastguard Worker continue;
336*9880d681SAndroid Build Coastguard Worker }
337*9880d681SAndroid Build Coastguard Worker
338*9880d681SAndroid Build Coastguard Worker // No, we need a live-in value for Pred as well
339*9880d681SAndroid Build Coastguard Worker if (Pred != &UseMBB)
340*9880d681SAndroid Build Coastguard Worker WorkList.push_back(Pred->getNumber());
341*9880d681SAndroid Build Coastguard Worker else
342*9880d681SAndroid Build Coastguard Worker // Loopback to UseMBB, so value is really live through.
343*9880d681SAndroid Build Coastguard Worker Use = SlotIndex();
344*9880d681SAndroid Build Coastguard Worker }
345*9880d681SAndroid Build Coastguard Worker }
346*9880d681SAndroid Build Coastguard Worker
347*9880d681SAndroid Build Coastguard Worker LiveIn.clear();
348*9880d681SAndroid Build Coastguard Worker
349*9880d681SAndroid Build Coastguard Worker // Both updateSSA() and LiveRangeUpdater benefit from ordered blocks, but
350*9880d681SAndroid Build Coastguard Worker // neither require it. Skip the sorting overhead for small updates.
351*9880d681SAndroid Build Coastguard Worker if (WorkList.size() > 4)
352*9880d681SAndroid Build Coastguard Worker array_pod_sort(WorkList.begin(), WorkList.end());
353*9880d681SAndroid Build Coastguard Worker
354*9880d681SAndroid Build Coastguard Worker // If a unique reaching def was found, blit in the live ranges immediately.
355*9880d681SAndroid Build Coastguard Worker if (UniqueVNI) {
356*9880d681SAndroid Build Coastguard Worker LiveRangeUpdater Updater(&LR);
357*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<unsigned>::const_iterator I = WorkList.begin(),
358*9880d681SAndroid Build Coastguard Worker E = WorkList.end(); I != E; ++I) {
359*9880d681SAndroid Build Coastguard Worker SlotIndex Start, End;
360*9880d681SAndroid Build Coastguard Worker std::tie(Start, End) = Indexes->getMBBRange(*I);
361*9880d681SAndroid Build Coastguard Worker // Trim the live range in UseMBB.
362*9880d681SAndroid Build Coastguard Worker if (*I == UseMBBNum && Use.isValid())
363*9880d681SAndroid Build Coastguard Worker End = Use;
364*9880d681SAndroid Build Coastguard Worker else
365*9880d681SAndroid Build Coastguard Worker Map[MF->getBlockNumbered(*I)] = LiveOutPair(TheVNI, nullptr);
366*9880d681SAndroid Build Coastguard Worker Updater.add(Start, End, TheVNI);
367*9880d681SAndroid Build Coastguard Worker }
368*9880d681SAndroid Build Coastguard Worker return true;
369*9880d681SAndroid Build Coastguard Worker }
370*9880d681SAndroid Build Coastguard Worker
371*9880d681SAndroid Build Coastguard Worker // Multiple values were found, so transfer the work list to the LiveIn array
372*9880d681SAndroid Build Coastguard Worker // where UpdateSSA will use it as a work list.
373*9880d681SAndroid Build Coastguard Worker LiveIn.reserve(WorkList.size());
374*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<unsigned>::const_iterator
375*9880d681SAndroid Build Coastguard Worker I = WorkList.begin(), E = WorkList.end(); I != E; ++I) {
376*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = MF->getBlockNumbered(*I);
377*9880d681SAndroid Build Coastguard Worker addLiveInBlock(LR, DomTree->getNode(MBB));
378*9880d681SAndroid Build Coastguard Worker if (MBB == &UseMBB)
379*9880d681SAndroid Build Coastguard Worker LiveIn.back().Kill = Use;
380*9880d681SAndroid Build Coastguard Worker }
381*9880d681SAndroid Build Coastguard Worker
382*9880d681SAndroid Build Coastguard Worker return false;
383*9880d681SAndroid Build Coastguard Worker }
384*9880d681SAndroid Build Coastguard Worker
385*9880d681SAndroid Build Coastguard Worker
386*9880d681SAndroid Build Coastguard Worker // This is essentially the same iterative algorithm that SSAUpdater uses,
387*9880d681SAndroid Build Coastguard Worker // except we already have a dominator tree, so we don't have to recompute it.
updateSSA()388*9880d681SAndroid Build Coastguard Worker void LiveRangeCalc::updateSSA() {
389*9880d681SAndroid Build Coastguard Worker assert(Indexes && "Missing SlotIndexes");
390*9880d681SAndroid Build Coastguard Worker assert(DomTree && "Missing dominator tree");
391*9880d681SAndroid Build Coastguard Worker
392*9880d681SAndroid Build Coastguard Worker // Interate until convergence.
393*9880d681SAndroid Build Coastguard Worker unsigned Changes;
394*9880d681SAndroid Build Coastguard Worker do {
395*9880d681SAndroid Build Coastguard Worker Changes = 0;
396*9880d681SAndroid Build Coastguard Worker // Propagate live-out values down the dominator tree, inserting phi-defs
397*9880d681SAndroid Build Coastguard Worker // when necessary.
398*9880d681SAndroid Build Coastguard Worker for (LiveInBlock &I : LiveIn) {
399*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *Node = I.DomNode;
400*9880d681SAndroid Build Coastguard Worker // Skip block if the live-in value has already been determined.
401*9880d681SAndroid Build Coastguard Worker if (!Node)
402*9880d681SAndroid Build Coastguard Worker continue;
403*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = Node->getBlock();
404*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *IDom = Node->getIDom();
405*9880d681SAndroid Build Coastguard Worker LiveOutPair IDomValue;
406*9880d681SAndroid Build Coastguard Worker
407*9880d681SAndroid Build Coastguard Worker // We need a live-in value to a block with no immediate dominator?
408*9880d681SAndroid Build Coastguard Worker // This is probably an unreachable block that has survived somehow.
409*9880d681SAndroid Build Coastguard Worker bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Worker // IDom dominates all of our predecessors, but it may not be their
412*9880d681SAndroid Build Coastguard Worker // immediate dominator. Check if any of them have live-out values that are
413*9880d681SAndroid Build Coastguard Worker // properly dominated by IDom. If so, we need a phi-def here.
414*9880d681SAndroid Build Coastguard Worker if (!needPHI) {
415*9880d681SAndroid Build Coastguard Worker IDomValue = Map[IDom->getBlock()];
416*9880d681SAndroid Build Coastguard Worker
417*9880d681SAndroid Build Coastguard Worker // Cache the DomTree node that defined the value.
418*9880d681SAndroid Build Coastguard Worker if (IDomValue.first && !IDomValue.second)
419*9880d681SAndroid Build Coastguard Worker Map[IDom->getBlock()].second = IDomValue.second =
420*9880d681SAndroid Build Coastguard Worker DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
421*9880d681SAndroid Build Coastguard Worker
422*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
423*9880d681SAndroid Build Coastguard Worker PE = MBB->pred_end(); PI != PE; ++PI) {
424*9880d681SAndroid Build Coastguard Worker LiveOutPair &Value = Map[*PI];
425*9880d681SAndroid Build Coastguard Worker if (!Value.first || Value.first == IDomValue.first)
426*9880d681SAndroid Build Coastguard Worker continue;
427*9880d681SAndroid Build Coastguard Worker
428*9880d681SAndroid Build Coastguard Worker // Cache the DomTree node that defined the value.
429*9880d681SAndroid Build Coastguard Worker if (!Value.second)
430*9880d681SAndroid Build Coastguard Worker Value.second =
431*9880d681SAndroid Build Coastguard Worker DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
432*9880d681SAndroid Build Coastguard Worker
433*9880d681SAndroid Build Coastguard Worker // This predecessor is carrying something other than IDomValue.
434*9880d681SAndroid Build Coastguard Worker // It could be because IDomValue hasn't propagated yet, or it could be
435*9880d681SAndroid Build Coastguard Worker // because MBB is in the dominance frontier of that value.
436*9880d681SAndroid Build Coastguard Worker if (DomTree->dominates(IDom, Value.second)) {
437*9880d681SAndroid Build Coastguard Worker needPHI = true;
438*9880d681SAndroid Build Coastguard Worker break;
439*9880d681SAndroid Build Coastguard Worker }
440*9880d681SAndroid Build Coastguard Worker }
441*9880d681SAndroid Build Coastguard Worker }
442*9880d681SAndroid Build Coastguard Worker
443*9880d681SAndroid Build Coastguard Worker // The value may be live-through even if Kill is set, as can happen when
444*9880d681SAndroid Build Coastguard Worker // we are called from extendRange. In that case LiveOutSeen is true, and
445*9880d681SAndroid Build Coastguard Worker // LiveOut indicates a foreign or missing value.
446*9880d681SAndroid Build Coastguard Worker LiveOutPair &LOP = Map[MBB];
447*9880d681SAndroid Build Coastguard Worker
448*9880d681SAndroid Build Coastguard Worker // Create a phi-def if required.
449*9880d681SAndroid Build Coastguard Worker if (needPHI) {
450*9880d681SAndroid Build Coastguard Worker ++Changes;
451*9880d681SAndroid Build Coastguard Worker assert(Alloc && "Need VNInfo allocator to create PHI-defs");
452*9880d681SAndroid Build Coastguard Worker SlotIndex Start, End;
453*9880d681SAndroid Build Coastguard Worker std::tie(Start, End) = Indexes->getMBBRange(MBB);
454*9880d681SAndroid Build Coastguard Worker LiveRange &LR = I.LR;
455*9880d681SAndroid Build Coastguard Worker VNInfo *VNI = LR.getNextValue(Start, *Alloc);
456*9880d681SAndroid Build Coastguard Worker I.Value = VNI;
457*9880d681SAndroid Build Coastguard Worker // This block is done, we know the final value.
458*9880d681SAndroid Build Coastguard Worker I.DomNode = nullptr;
459*9880d681SAndroid Build Coastguard Worker
460*9880d681SAndroid Build Coastguard Worker // Add liveness since updateFromLiveIns now skips this node.
461*9880d681SAndroid Build Coastguard Worker if (I.Kill.isValid())
462*9880d681SAndroid Build Coastguard Worker LR.addSegment(LiveInterval::Segment(Start, I.Kill, VNI));
463*9880d681SAndroid Build Coastguard Worker else {
464*9880d681SAndroid Build Coastguard Worker LR.addSegment(LiveInterval::Segment(Start, End, VNI));
465*9880d681SAndroid Build Coastguard Worker LOP = LiveOutPair(VNI, Node);
466*9880d681SAndroid Build Coastguard Worker }
467*9880d681SAndroid Build Coastguard Worker } else if (IDomValue.first) {
468*9880d681SAndroid Build Coastguard Worker // No phi-def here. Remember incoming value.
469*9880d681SAndroid Build Coastguard Worker I.Value = IDomValue.first;
470*9880d681SAndroid Build Coastguard Worker
471*9880d681SAndroid Build Coastguard Worker // If the IDomValue is killed in the block, don't propagate through.
472*9880d681SAndroid Build Coastguard Worker if (I.Kill.isValid())
473*9880d681SAndroid Build Coastguard Worker continue;
474*9880d681SAndroid Build Coastguard Worker
475*9880d681SAndroid Build Coastguard Worker // Propagate IDomValue if it isn't killed:
476*9880d681SAndroid Build Coastguard Worker // MBB is live-out and doesn't define its own value.
477*9880d681SAndroid Build Coastguard Worker if (LOP.first == IDomValue.first)
478*9880d681SAndroid Build Coastguard Worker continue;
479*9880d681SAndroid Build Coastguard Worker ++Changes;
480*9880d681SAndroid Build Coastguard Worker LOP = IDomValue;
481*9880d681SAndroid Build Coastguard Worker }
482*9880d681SAndroid Build Coastguard Worker }
483*9880d681SAndroid Build Coastguard Worker } while (Changes);
484*9880d681SAndroid Build Coastguard Worker }
485