xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/LiveRangeEdit.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===-- LiveRangeEdit.cpp - Basic tools for editing a register live range -===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // The LiveRangeEdit class represents changes done to a virtual register when it
10*0b57cec5SDimitry Andric // is spilled or split.
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
14*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
15*0b57cec5SDimitry Andric #include "llvm/CodeGen/CalcSpillWeights.h"
16*0b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
17*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
18*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
19*0b57cec5SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
20*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
21*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
22*0b57cec5SDimitry Andric 
23*0b57cec5SDimitry Andric using namespace llvm;
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric #define DEBUG_TYPE "regalloc"
26*0b57cec5SDimitry Andric 
27*0b57cec5SDimitry Andric STATISTIC(NumDCEDeleted,     "Number of instructions deleted by DCE");
28*0b57cec5SDimitry Andric STATISTIC(NumDCEFoldedLoads, "Number of single use loads folded after DCE");
29*0b57cec5SDimitry Andric STATISTIC(NumFracRanges,     "Number of live ranges fractured by DCE");
30*0b57cec5SDimitry Andric 
31*0b57cec5SDimitry Andric void LiveRangeEdit::Delegate::anchor() { }
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(unsigned OldReg,
34*0b57cec5SDimitry Andric                                                      bool createSubRanges) {
35*0b57cec5SDimitry Andric   unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
36*0b57cec5SDimitry Andric   if (VRM)
37*0b57cec5SDimitry Andric     VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
38*0b57cec5SDimitry Andric 
39*0b57cec5SDimitry Andric   LiveInterval &LI = LIS.createEmptyInterval(VReg);
40*0b57cec5SDimitry Andric   if (Parent && !Parent->isSpillable())
41*0b57cec5SDimitry Andric     LI.markNotSpillable();
42*0b57cec5SDimitry Andric   if (createSubRanges) {
43*0b57cec5SDimitry Andric     // Create empty subranges if the OldReg's interval has them. Do not create
44*0b57cec5SDimitry Andric     // the main range here---it will be constructed later after the subranges
45*0b57cec5SDimitry Andric     // have been finalized.
46*0b57cec5SDimitry Andric     LiveInterval &OldLI = LIS.getInterval(OldReg);
47*0b57cec5SDimitry Andric     VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator();
48*0b57cec5SDimitry Andric     for (LiveInterval::SubRange &S : OldLI.subranges())
49*0b57cec5SDimitry Andric       LI.createSubRange(Alloc, S.LaneMask);
50*0b57cec5SDimitry Andric   }
51*0b57cec5SDimitry Andric   return LI;
52*0b57cec5SDimitry Andric }
53*0b57cec5SDimitry Andric 
54*0b57cec5SDimitry Andric unsigned LiveRangeEdit::createFrom(unsigned OldReg) {
55*0b57cec5SDimitry Andric   unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
56*0b57cec5SDimitry Andric   if (VRM) {
57*0b57cec5SDimitry Andric     VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
58*0b57cec5SDimitry Andric   }
59*0b57cec5SDimitry Andric   // FIXME: Getting the interval here actually computes it.
60*0b57cec5SDimitry Andric   // In theory, this may not be what we want, but in practice
61*0b57cec5SDimitry Andric   // the createEmptyIntervalFrom API is used when this is not
62*0b57cec5SDimitry Andric   // the case. Generally speaking we just want to annotate the
63*0b57cec5SDimitry Andric   // LiveInterval when it gets created but we cannot do that at
64*0b57cec5SDimitry Andric   // the moment.
65*0b57cec5SDimitry Andric   if (Parent && !Parent->isSpillable())
66*0b57cec5SDimitry Andric     LIS.getInterval(VReg).markNotSpillable();
67*0b57cec5SDimitry Andric   return VReg;
68*0b57cec5SDimitry Andric }
69*0b57cec5SDimitry Andric 
70*0b57cec5SDimitry Andric bool LiveRangeEdit::checkRematerializable(VNInfo *VNI,
71*0b57cec5SDimitry Andric                                           const MachineInstr *DefMI,
72*0b57cec5SDimitry Andric                                           AliasAnalysis *aa) {
73*0b57cec5SDimitry Andric   assert(DefMI && "Missing instruction");
74*0b57cec5SDimitry Andric   ScannedRemattable = true;
75*0b57cec5SDimitry Andric   if (!TII.isTriviallyReMaterializable(*DefMI, aa))
76*0b57cec5SDimitry Andric     return false;
77*0b57cec5SDimitry Andric   Remattable.insert(VNI);
78*0b57cec5SDimitry Andric   return true;
79*0b57cec5SDimitry Andric }
80*0b57cec5SDimitry Andric 
81*0b57cec5SDimitry Andric void LiveRangeEdit::scanRemattable(AliasAnalysis *aa) {
82*0b57cec5SDimitry Andric   for (VNInfo *VNI : getParent().valnos) {
83*0b57cec5SDimitry Andric     if (VNI->isUnused())
84*0b57cec5SDimitry Andric       continue;
85*0b57cec5SDimitry Andric     unsigned Original = VRM->getOriginal(getReg());
86*0b57cec5SDimitry Andric     LiveInterval &OrigLI = LIS.getInterval(Original);
87*0b57cec5SDimitry Andric     VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
88*0b57cec5SDimitry Andric     if (!OrigVNI)
89*0b57cec5SDimitry Andric       continue;
90*0b57cec5SDimitry Andric     MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
91*0b57cec5SDimitry Andric     if (!DefMI)
92*0b57cec5SDimitry Andric       continue;
93*0b57cec5SDimitry Andric     checkRematerializable(OrigVNI, DefMI, aa);
94*0b57cec5SDimitry Andric   }
95*0b57cec5SDimitry Andric   ScannedRemattable = true;
96*0b57cec5SDimitry Andric }
97*0b57cec5SDimitry Andric 
98*0b57cec5SDimitry Andric bool LiveRangeEdit::anyRematerializable(AliasAnalysis *aa) {
99*0b57cec5SDimitry Andric   if (!ScannedRemattable)
100*0b57cec5SDimitry Andric     scanRemattable(aa);
101*0b57cec5SDimitry Andric   return !Remattable.empty();
102*0b57cec5SDimitry Andric }
103*0b57cec5SDimitry Andric 
104*0b57cec5SDimitry Andric /// allUsesAvailableAt - Return true if all registers used by OrigMI at
105*0b57cec5SDimitry Andric /// OrigIdx are also available with the same value at UseIdx.
106*0b57cec5SDimitry Andric bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI,
107*0b57cec5SDimitry Andric                                        SlotIndex OrigIdx,
108*0b57cec5SDimitry Andric                                        SlotIndex UseIdx) const {
109*0b57cec5SDimitry Andric   OrigIdx = OrigIdx.getRegSlot(true);
110*0b57cec5SDimitry Andric   UseIdx = UseIdx.getRegSlot(true);
111*0b57cec5SDimitry Andric   for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) {
112*0b57cec5SDimitry Andric     const MachineOperand &MO = OrigMI->getOperand(i);
113*0b57cec5SDimitry Andric     if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
114*0b57cec5SDimitry Andric       continue;
115*0b57cec5SDimitry Andric 
116*0b57cec5SDimitry Andric     // We can't remat physreg uses, unless it is a constant.
117*0b57cec5SDimitry Andric     if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
118*0b57cec5SDimitry Andric       if (MRI.isConstantPhysReg(MO.getReg()))
119*0b57cec5SDimitry Andric         continue;
120*0b57cec5SDimitry Andric       return false;
121*0b57cec5SDimitry Andric     }
122*0b57cec5SDimitry Andric 
123*0b57cec5SDimitry Andric     LiveInterval &li = LIS.getInterval(MO.getReg());
124*0b57cec5SDimitry Andric     const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
125*0b57cec5SDimitry Andric     if (!OVNI)
126*0b57cec5SDimitry Andric       continue;
127*0b57cec5SDimitry Andric 
128*0b57cec5SDimitry Andric     // Don't allow rematerialization immediately after the original def.
129*0b57cec5SDimitry Andric     // It would be incorrect if OrigMI redefines the register.
130*0b57cec5SDimitry Andric     // See PR14098.
131*0b57cec5SDimitry Andric     if (SlotIndex::isSameInstr(OrigIdx, UseIdx))
132*0b57cec5SDimitry Andric       return false;
133*0b57cec5SDimitry Andric 
134*0b57cec5SDimitry Andric     if (OVNI != li.getVNInfoAt(UseIdx))
135*0b57cec5SDimitry Andric       return false;
136*0b57cec5SDimitry Andric   }
137*0b57cec5SDimitry Andric   return true;
138*0b57cec5SDimitry Andric }
139*0b57cec5SDimitry Andric 
140*0b57cec5SDimitry Andric bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
141*0b57cec5SDimitry Andric                                        SlotIndex UseIdx, bool cheapAsAMove) {
142*0b57cec5SDimitry Andric   assert(ScannedRemattable && "Call anyRematerializable first");
143*0b57cec5SDimitry Andric 
144*0b57cec5SDimitry Andric   // Use scanRemattable info.
145*0b57cec5SDimitry Andric   if (!Remattable.count(OrigVNI))
146*0b57cec5SDimitry Andric     return false;
147*0b57cec5SDimitry Andric 
148*0b57cec5SDimitry Andric   // No defining instruction provided.
149*0b57cec5SDimitry Andric   SlotIndex DefIdx;
150*0b57cec5SDimitry Andric   assert(RM.OrigMI && "No defining instruction for remattable value");
151*0b57cec5SDimitry Andric   DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
152*0b57cec5SDimitry Andric 
153*0b57cec5SDimitry Andric   // If only cheap remats were requested, bail out early.
154*0b57cec5SDimitry Andric   if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI))
155*0b57cec5SDimitry Andric     return false;
156*0b57cec5SDimitry Andric 
157*0b57cec5SDimitry Andric   // Verify that all used registers are available with the same values.
158*0b57cec5SDimitry Andric   if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
159*0b57cec5SDimitry Andric     return false;
160*0b57cec5SDimitry Andric 
161*0b57cec5SDimitry Andric   return true;
162*0b57cec5SDimitry Andric }
163*0b57cec5SDimitry Andric 
164*0b57cec5SDimitry Andric SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
165*0b57cec5SDimitry Andric                                          MachineBasicBlock::iterator MI,
166*0b57cec5SDimitry Andric                                          unsigned DestReg,
167*0b57cec5SDimitry Andric                                          const Remat &RM,
168*0b57cec5SDimitry Andric                                          const TargetRegisterInfo &tri,
169*0b57cec5SDimitry Andric                                          bool Late) {
170*0b57cec5SDimitry Andric   assert(RM.OrigMI && "Invalid remat");
171*0b57cec5SDimitry Andric   TII.reMaterialize(MBB, MI, DestReg, 0, *RM.OrigMI, tri);
172*0b57cec5SDimitry Andric   // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg
173*0b57cec5SDimitry Andric   // to false anyway in case the isDead flag of RM.OrigMI's dest register
174*0b57cec5SDimitry Andric   // is true.
175*0b57cec5SDimitry Andric   (*--MI).getOperand(0).setIsDead(false);
176*0b57cec5SDimitry Andric   Rematted.insert(RM.ParentVNI);
177*0b57cec5SDimitry Andric   return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
178*0b57cec5SDimitry Andric }
179*0b57cec5SDimitry Andric 
180*0b57cec5SDimitry Andric void LiveRangeEdit::eraseVirtReg(unsigned Reg) {
181*0b57cec5SDimitry Andric   if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
182*0b57cec5SDimitry Andric     LIS.removeInterval(Reg);
183*0b57cec5SDimitry Andric }
184*0b57cec5SDimitry Andric 
185*0b57cec5SDimitry Andric bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
186*0b57cec5SDimitry Andric                                SmallVectorImpl<MachineInstr*> &Dead) {
187*0b57cec5SDimitry Andric   MachineInstr *DefMI = nullptr, *UseMI = nullptr;
188*0b57cec5SDimitry Andric 
189*0b57cec5SDimitry Andric   // Check that there is a single def and a single use.
190*0b57cec5SDimitry Andric   for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg)) {
191*0b57cec5SDimitry Andric     MachineInstr *MI = MO.getParent();
192*0b57cec5SDimitry Andric     if (MO.isDef()) {
193*0b57cec5SDimitry Andric       if (DefMI && DefMI != MI)
194*0b57cec5SDimitry Andric         return false;
195*0b57cec5SDimitry Andric       if (!MI->canFoldAsLoad())
196*0b57cec5SDimitry Andric         return false;
197*0b57cec5SDimitry Andric       DefMI = MI;
198*0b57cec5SDimitry Andric     } else if (!MO.isUndef()) {
199*0b57cec5SDimitry Andric       if (UseMI && UseMI != MI)
200*0b57cec5SDimitry Andric         return false;
201*0b57cec5SDimitry Andric       // FIXME: Targets don't know how to fold subreg uses.
202*0b57cec5SDimitry Andric       if (MO.getSubReg())
203*0b57cec5SDimitry Andric         return false;
204*0b57cec5SDimitry Andric       UseMI = MI;
205*0b57cec5SDimitry Andric     }
206*0b57cec5SDimitry Andric   }
207*0b57cec5SDimitry Andric   if (!DefMI || !UseMI)
208*0b57cec5SDimitry Andric     return false;
209*0b57cec5SDimitry Andric 
210*0b57cec5SDimitry Andric   // Since we're moving the DefMI load, make sure we're not extending any live
211*0b57cec5SDimitry Andric   // ranges.
212*0b57cec5SDimitry Andric   if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
213*0b57cec5SDimitry Andric                           LIS.getInstructionIndex(*UseMI)))
214*0b57cec5SDimitry Andric     return false;
215*0b57cec5SDimitry Andric 
216*0b57cec5SDimitry Andric   // We also need to make sure it is safe to move the load.
217*0b57cec5SDimitry Andric   // Assume there are stores between DefMI and UseMI.
218*0b57cec5SDimitry Andric   bool SawStore = true;
219*0b57cec5SDimitry Andric   if (!DefMI->isSafeToMove(nullptr, SawStore))
220*0b57cec5SDimitry Andric     return false;
221*0b57cec5SDimitry Andric 
222*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Try to fold single def: " << *DefMI
223*0b57cec5SDimitry Andric                     << "       into single use: " << *UseMI);
224*0b57cec5SDimitry Andric 
225*0b57cec5SDimitry Andric   SmallVector<unsigned, 8> Ops;
226*0b57cec5SDimitry Andric   if (UseMI->readsWritesVirtualRegister(LI->reg, &Ops).second)
227*0b57cec5SDimitry Andric     return false;
228*0b57cec5SDimitry Andric 
229*0b57cec5SDimitry Andric   MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
230*0b57cec5SDimitry Andric   if (!FoldMI)
231*0b57cec5SDimitry Andric     return false;
232*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "                folded: " << *FoldMI);
233*0b57cec5SDimitry Andric   LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
234*0b57cec5SDimitry Andric   if (UseMI->isCall())
235*0b57cec5SDimitry Andric     UseMI->getMF()->updateCallSiteInfo(UseMI, FoldMI);
236*0b57cec5SDimitry Andric   UseMI->eraseFromParent();
237*0b57cec5SDimitry Andric   DefMI->addRegisterDead(LI->reg, nullptr);
238*0b57cec5SDimitry Andric   Dead.push_back(DefMI);
239*0b57cec5SDimitry Andric   ++NumDCEFoldedLoads;
240*0b57cec5SDimitry Andric   return true;
241*0b57cec5SDimitry Andric }
242*0b57cec5SDimitry Andric 
243*0b57cec5SDimitry Andric bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
244*0b57cec5SDimitry Andric                               const MachineOperand &MO) const {
245*0b57cec5SDimitry Andric   const MachineInstr &MI = *MO.getParent();
246*0b57cec5SDimitry Andric   SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
247*0b57cec5SDimitry Andric   if (LI.Query(Idx).isKill())
248*0b57cec5SDimitry Andric     return true;
249*0b57cec5SDimitry Andric   const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
250*0b57cec5SDimitry Andric   unsigned SubReg = MO.getSubReg();
251*0b57cec5SDimitry Andric   LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
252*0b57cec5SDimitry Andric   for (const LiveInterval::SubRange &S : LI.subranges()) {
253*0b57cec5SDimitry Andric     if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
254*0b57cec5SDimitry Andric       return true;
255*0b57cec5SDimitry Andric   }
256*0b57cec5SDimitry Andric   return false;
257*0b57cec5SDimitry Andric }
258*0b57cec5SDimitry Andric 
259*0b57cec5SDimitry Andric /// Find all live intervals that need to shrink, then remove the instruction.
260*0b57cec5SDimitry Andric void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink,
261*0b57cec5SDimitry Andric                                      AliasAnalysis *AA) {
262*0b57cec5SDimitry Andric   assert(MI->allDefsAreDead() && "Def isn't really dead");
263*0b57cec5SDimitry Andric   SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
264*0b57cec5SDimitry Andric 
265*0b57cec5SDimitry Andric   // Never delete a bundled instruction.
266*0b57cec5SDimitry Andric   if (MI->isBundled()) {
267*0b57cec5SDimitry Andric     return;
268*0b57cec5SDimitry Andric   }
269*0b57cec5SDimitry Andric   // Never delete inline asm.
270*0b57cec5SDimitry Andric   if (MI->isInlineAsm()) {
271*0b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
272*0b57cec5SDimitry Andric     return;
273*0b57cec5SDimitry Andric   }
274*0b57cec5SDimitry Andric 
275*0b57cec5SDimitry Andric   // Use the same criteria as DeadMachineInstructionElim.
276*0b57cec5SDimitry Andric   bool SawStore = false;
277*0b57cec5SDimitry Andric   if (!MI->isSafeToMove(nullptr, SawStore)) {
278*0b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
279*0b57cec5SDimitry Andric     return;
280*0b57cec5SDimitry Andric   }
281*0b57cec5SDimitry Andric 
282*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
283*0b57cec5SDimitry Andric 
284*0b57cec5SDimitry Andric   // Collect virtual registers to be erased after MI is gone.
285*0b57cec5SDimitry Andric   SmallVector<unsigned, 8> RegsToErase;
286*0b57cec5SDimitry Andric   bool ReadsPhysRegs = false;
287*0b57cec5SDimitry Andric   bool isOrigDef = false;
288*0b57cec5SDimitry Andric   unsigned Dest;
289*0b57cec5SDimitry Andric   // Only optimize rematerialize case when the instruction has one def, since
290*0b57cec5SDimitry Andric   // otherwise we could leave some dead defs in the code.  This case is
291*0b57cec5SDimitry Andric   // extremely rare.
292*0b57cec5SDimitry Andric   if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
293*0b57cec5SDimitry Andric       MI->getDesc().getNumDefs() == 1) {
294*0b57cec5SDimitry Andric     Dest = MI->getOperand(0).getReg();
295*0b57cec5SDimitry Andric     unsigned Original = VRM->getOriginal(Dest);
296*0b57cec5SDimitry Andric     LiveInterval &OrigLI = LIS.getInterval(Original);
297*0b57cec5SDimitry Andric     VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
298*0b57cec5SDimitry Andric     // The original live-range may have been shrunk to
299*0b57cec5SDimitry Andric     // an empty live-range. It happens when it is dead, but
300*0b57cec5SDimitry Andric     // we still keep it around to be able to rematerialize
301*0b57cec5SDimitry Andric     // other values that depend on it.
302*0b57cec5SDimitry Andric     if (OrigVNI)
303*0b57cec5SDimitry Andric       isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx);
304*0b57cec5SDimitry Andric   }
305*0b57cec5SDimitry Andric 
306*0b57cec5SDimitry Andric   // Check for live intervals that may shrink
307*0b57cec5SDimitry Andric   for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
308*0b57cec5SDimitry Andric          MOE = MI->operands_end(); MOI != MOE; ++MOI) {
309*0b57cec5SDimitry Andric     if (!MOI->isReg())
310*0b57cec5SDimitry Andric       continue;
311*0b57cec5SDimitry Andric     unsigned Reg = MOI->getReg();
312*0b57cec5SDimitry Andric     if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
313*0b57cec5SDimitry Andric       // Check if MI reads any unreserved physregs.
314*0b57cec5SDimitry Andric       if (Reg && MOI->readsReg() && !MRI.isReserved(Reg))
315*0b57cec5SDimitry Andric         ReadsPhysRegs = true;
316*0b57cec5SDimitry Andric       else if (MOI->isDef())
317*0b57cec5SDimitry Andric         LIS.removePhysRegDefAt(Reg, Idx);
318*0b57cec5SDimitry Andric       continue;
319*0b57cec5SDimitry Andric     }
320*0b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(Reg);
321*0b57cec5SDimitry Andric 
322*0b57cec5SDimitry Andric     // Shrink read registers, unless it is likely to be expensive and
323*0b57cec5SDimitry Andric     // unlikely to change anything. We typically don't want to shrink the
324*0b57cec5SDimitry Andric     // PIC base register that has lots of uses everywhere.
325*0b57cec5SDimitry Andric     // Always shrink COPY uses that probably come from live range splitting.
326*0b57cec5SDimitry Andric     if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MOI->isDef())) ||
327*0b57cec5SDimitry Andric         (MOI->readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI))))
328*0b57cec5SDimitry Andric       ToShrink.insert(&LI);
329*0b57cec5SDimitry Andric 
330*0b57cec5SDimitry Andric     // Remove defined value.
331*0b57cec5SDimitry Andric     if (MOI->isDef()) {
332*0b57cec5SDimitry Andric       if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
333*0b57cec5SDimitry Andric         TheDelegate->LRE_WillShrinkVirtReg(LI.reg);
334*0b57cec5SDimitry Andric       LIS.removeVRegDefAt(LI, Idx);
335*0b57cec5SDimitry Andric       if (LI.empty())
336*0b57cec5SDimitry Andric         RegsToErase.push_back(Reg);
337*0b57cec5SDimitry Andric     }
338*0b57cec5SDimitry Andric   }
339*0b57cec5SDimitry Andric 
340*0b57cec5SDimitry Andric   // Currently, we don't support DCE of physreg live ranges. If MI reads
341*0b57cec5SDimitry Andric   // any unreserved physregs, don't erase the instruction, but turn it into
342*0b57cec5SDimitry Andric   // a KILL instead. This way, the physreg live ranges don't end up
343*0b57cec5SDimitry Andric   // dangling.
344*0b57cec5SDimitry Andric   // FIXME: It would be better to have something like shrinkToUses() for
345*0b57cec5SDimitry Andric   // physregs. That could potentially enable more DCE and it would free up
346*0b57cec5SDimitry Andric   // the physreg. It would not happen often, though.
347*0b57cec5SDimitry Andric   if (ReadsPhysRegs) {
348*0b57cec5SDimitry Andric     MI->setDesc(TII.get(TargetOpcode::KILL));
349*0b57cec5SDimitry Andric     // Remove all operands that aren't physregs.
350*0b57cec5SDimitry Andric     for (unsigned i = MI->getNumOperands(); i; --i) {
351*0b57cec5SDimitry Andric       const MachineOperand &MO = MI->getOperand(i-1);
352*0b57cec5SDimitry Andric       if (MO.isReg() && TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
353*0b57cec5SDimitry Andric         continue;
354*0b57cec5SDimitry Andric       MI->RemoveOperand(i-1);
355*0b57cec5SDimitry Andric     }
356*0b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
357*0b57cec5SDimitry Andric   } else {
358*0b57cec5SDimitry Andric     // If the dest of MI is an original reg and MI is reMaterializable,
359*0b57cec5SDimitry Andric     // don't delete the inst. Replace the dest with a new reg, and keep
360*0b57cec5SDimitry Andric     // the inst for remat of other siblings. The inst is saved in
361*0b57cec5SDimitry Andric     // LiveRangeEdit::DeadRemats and will be deleted after all the
362*0b57cec5SDimitry Andric     // allocations of the func are done.
363*0b57cec5SDimitry Andric     if (isOrigDef && DeadRemats && TII.isTriviallyReMaterializable(*MI, AA)) {
364*0b57cec5SDimitry Andric       LiveInterval &NewLI = createEmptyIntervalFrom(Dest, false);
365*0b57cec5SDimitry Andric       VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
366*0b57cec5SDimitry Andric       NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI));
367*0b57cec5SDimitry Andric       pop_back();
368*0b57cec5SDimitry Andric       DeadRemats->insert(MI);
369*0b57cec5SDimitry Andric       const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
370*0b57cec5SDimitry Andric       MI->substituteRegister(Dest, NewLI.reg, 0, TRI);
371*0b57cec5SDimitry Andric       MI->getOperand(0).setIsDead(true);
372*0b57cec5SDimitry Andric     } else {
373*0b57cec5SDimitry Andric       if (TheDelegate)
374*0b57cec5SDimitry Andric         TheDelegate->LRE_WillEraseInstruction(MI);
375*0b57cec5SDimitry Andric       LIS.RemoveMachineInstrFromMaps(*MI);
376*0b57cec5SDimitry Andric       MI->eraseFromParent();
377*0b57cec5SDimitry Andric       ++NumDCEDeleted;
378*0b57cec5SDimitry Andric     }
379*0b57cec5SDimitry Andric   }
380*0b57cec5SDimitry Andric 
381*0b57cec5SDimitry Andric   // Erase any virtregs that are now empty and unused. There may be <undef>
382*0b57cec5SDimitry Andric   // uses around. Keep the empty live range in that case.
383*0b57cec5SDimitry Andric   for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
384*0b57cec5SDimitry Andric     unsigned Reg = RegsToErase[i];
385*0b57cec5SDimitry Andric     if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
386*0b57cec5SDimitry Andric       ToShrink.remove(&LIS.getInterval(Reg));
387*0b57cec5SDimitry Andric       eraseVirtReg(Reg);
388*0b57cec5SDimitry Andric     }
389*0b57cec5SDimitry Andric   }
390*0b57cec5SDimitry Andric }
391*0b57cec5SDimitry Andric 
392*0b57cec5SDimitry Andric void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
393*0b57cec5SDimitry Andric                                       ArrayRef<unsigned> RegsBeingSpilled,
394*0b57cec5SDimitry Andric                                       AliasAnalysis *AA) {
395*0b57cec5SDimitry Andric   ToShrinkSet ToShrink;
396*0b57cec5SDimitry Andric 
397*0b57cec5SDimitry Andric   for (;;) {
398*0b57cec5SDimitry Andric     // Erase all dead defs.
399*0b57cec5SDimitry Andric     while (!Dead.empty())
400*0b57cec5SDimitry Andric       eliminateDeadDef(Dead.pop_back_val(), ToShrink, AA);
401*0b57cec5SDimitry Andric 
402*0b57cec5SDimitry Andric     if (ToShrink.empty())
403*0b57cec5SDimitry Andric       break;
404*0b57cec5SDimitry Andric 
405*0b57cec5SDimitry Andric     // Shrink just one live interval. Then delete new dead defs.
406*0b57cec5SDimitry Andric     LiveInterval *LI = ToShrink.back();
407*0b57cec5SDimitry Andric     ToShrink.pop_back();
408*0b57cec5SDimitry Andric     if (foldAsLoad(LI, Dead))
409*0b57cec5SDimitry Andric       continue;
410*0b57cec5SDimitry Andric     unsigned VReg = LI->reg;
411*0b57cec5SDimitry Andric     if (TheDelegate)
412*0b57cec5SDimitry Andric       TheDelegate->LRE_WillShrinkVirtReg(VReg);
413*0b57cec5SDimitry Andric     if (!LIS.shrinkToUses(LI, &Dead))
414*0b57cec5SDimitry Andric       continue;
415*0b57cec5SDimitry Andric 
416*0b57cec5SDimitry Andric     // Don't create new intervals for a register being spilled.
417*0b57cec5SDimitry Andric     // The new intervals would have to be spilled anyway so its not worth it.
418*0b57cec5SDimitry Andric     // Also they currently aren't spilled so creating them and not spilling
419*0b57cec5SDimitry Andric     // them results in incorrect code.
420*0b57cec5SDimitry Andric     bool BeingSpilled = false;
421*0b57cec5SDimitry Andric     for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) {
422*0b57cec5SDimitry Andric       if (VReg == RegsBeingSpilled[i]) {
423*0b57cec5SDimitry Andric         BeingSpilled = true;
424*0b57cec5SDimitry Andric         break;
425*0b57cec5SDimitry Andric       }
426*0b57cec5SDimitry Andric     }
427*0b57cec5SDimitry Andric 
428*0b57cec5SDimitry Andric     if (BeingSpilled) continue;
429*0b57cec5SDimitry Andric 
430*0b57cec5SDimitry Andric     // LI may have been separated, create new intervals.
431*0b57cec5SDimitry Andric     LI->RenumberValues();
432*0b57cec5SDimitry Andric     SmallVector<LiveInterval*, 8> SplitLIs;
433*0b57cec5SDimitry Andric     LIS.splitSeparateComponents(*LI, SplitLIs);
434*0b57cec5SDimitry Andric     if (!SplitLIs.empty())
435*0b57cec5SDimitry Andric       ++NumFracRanges;
436*0b57cec5SDimitry Andric 
437*0b57cec5SDimitry Andric     unsigned Original = VRM ? VRM->getOriginal(VReg) : 0;
438*0b57cec5SDimitry Andric     for (const LiveInterval *SplitLI : SplitLIs) {
439*0b57cec5SDimitry Andric       // If LI is an original interval that hasn't been split yet, make the new
440*0b57cec5SDimitry Andric       // intervals their own originals instead of referring to LI. The original
441*0b57cec5SDimitry Andric       // interval must contain all the split products, and LI doesn't.
442*0b57cec5SDimitry Andric       if (Original != VReg && Original != 0)
443*0b57cec5SDimitry Andric         VRM->setIsSplitFromReg(SplitLI->reg, Original);
444*0b57cec5SDimitry Andric       if (TheDelegate)
445*0b57cec5SDimitry Andric         TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg, VReg);
446*0b57cec5SDimitry Andric     }
447*0b57cec5SDimitry Andric   }
448*0b57cec5SDimitry Andric }
449*0b57cec5SDimitry Andric 
450*0b57cec5SDimitry Andric // Keep track of new virtual registers created via
451*0b57cec5SDimitry Andric // MachineRegisterInfo::createVirtualRegister.
452*0b57cec5SDimitry Andric void
453*0b57cec5SDimitry Andric LiveRangeEdit::MRI_NoteNewVirtualRegister(unsigned VReg)
454*0b57cec5SDimitry Andric {
455*0b57cec5SDimitry Andric   if (VRM)
456*0b57cec5SDimitry Andric     VRM->grow();
457*0b57cec5SDimitry Andric 
458*0b57cec5SDimitry Andric   NewRegs.push_back(VReg);
459*0b57cec5SDimitry Andric }
460*0b57cec5SDimitry Andric 
461*0b57cec5SDimitry Andric void
462*0b57cec5SDimitry Andric LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
463*0b57cec5SDimitry Andric                                         const MachineLoopInfo &Loops,
464*0b57cec5SDimitry Andric                                         const MachineBlockFrequencyInfo &MBFI) {
465*0b57cec5SDimitry Andric   VirtRegAuxInfo VRAI(MF, LIS, VRM, Loops, MBFI);
466*0b57cec5SDimitry Andric   for (unsigned I = 0, Size = size(); I < Size; ++I) {
467*0b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(get(I));
468*0b57cec5SDimitry Andric     if (MRI.recomputeRegClass(LI.reg))
469*0b57cec5SDimitry Andric       LLVM_DEBUG({
470*0b57cec5SDimitry Andric         const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
471*0b57cec5SDimitry Andric         dbgs() << "Inflated " << printReg(LI.reg) << " to "
472*0b57cec5SDimitry Andric                << TRI->getRegClassName(MRI.getRegClass(LI.reg)) << '\n';
473*0b57cec5SDimitry Andric       });
474*0b57cec5SDimitry Andric     VRAI.calculateSpillWeightAndHint(LI);
475*0b57cec5SDimitry Andric   }
476*0b57cec5SDimitry Andric }
477