xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/LiveRangeEdit.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===-- LiveRangeEdit.cpp - Basic tools for editing a register live range -===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // The LiveRangeEdit class represents changes done to a virtual register when it
100b57cec5SDimitry Andric // is spilled or split.
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
140b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/CalcSpillWeights.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
200b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
210b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric using namespace llvm;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric #define DEBUG_TYPE "regalloc"
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric STATISTIC(NumDCEDeleted,        "Number of instructions deleted by DCE");
280b57cec5SDimitry Andric STATISTIC(NumDCEFoldedLoads,    "Number of single use loads folded after DCE");
290b57cec5SDimitry Andric STATISTIC(NumFracRanges,        "Number of live ranges fractured by DCE");
30*bdd1243dSDimitry Andric STATISTIC(NumReMaterialization, "Number of instructions rematerialized");
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric void LiveRangeEdit::Delegate::anchor() { }
330b57cec5SDimitry Andric 
345ffd83dbSDimitry Andric LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(Register OldReg,
350b57cec5SDimitry Andric                                                      bool createSubRanges) {
36*bdd1243dSDimitry Andric   Register VReg = MRI.cloneVirtualRegister(OldReg);
370b57cec5SDimitry Andric   if (VRM)
380b57cec5SDimitry Andric     VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric   LiveInterval &LI = LIS.createEmptyInterval(VReg);
410b57cec5SDimitry Andric   if (Parent && !Parent->isSpillable())
420b57cec5SDimitry Andric     LI.markNotSpillable();
430b57cec5SDimitry Andric   if (createSubRanges) {
440b57cec5SDimitry Andric     // Create empty subranges if the OldReg's interval has them. Do not create
450b57cec5SDimitry Andric     // the main range here---it will be constructed later after the subranges
460b57cec5SDimitry Andric     // have been finalized.
470b57cec5SDimitry Andric     LiveInterval &OldLI = LIS.getInterval(OldReg);
480b57cec5SDimitry Andric     VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator();
490b57cec5SDimitry Andric     for (LiveInterval::SubRange &S : OldLI.subranges())
500b57cec5SDimitry Andric       LI.createSubRange(Alloc, S.LaneMask);
510b57cec5SDimitry Andric   }
520b57cec5SDimitry Andric   return LI;
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric 
555ffd83dbSDimitry Andric Register LiveRangeEdit::createFrom(Register OldReg) {
56*bdd1243dSDimitry Andric   Register VReg = MRI.cloneVirtualRegister(OldReg);
570b57cec5SDimitry Andric   if (VRM) {
580b57cec5SDimitry Andric     VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
590b57cec5SDimitry Andric   }
600b57cec5SDimitry Andric   // FIXME: Getting the interval here actually computes it.
610b57cec5SDimitry Andric   // In theory, this may not be what we want, but in practice
620b57cec5SDimitry Andric   // the createEmptyIntervalFrom API is used when this is not
630b57cec5SDimitry Andric   // the case. Generally speaking we just want to annotate the
640b57cec5SDimitry Andric   // LiveInterval when it gets created but we cannot do that at
650b57cec5SDimitry Andric   // the moment.
660b57cec5SDimitry Andric   if (Parent && !Parent->isSpillable())
670b57cec5SDimitry Andric     LIS.getInterval(VReg).markNotSpillable();
680b57cec5SDimitry Andric   return VReg;
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric bool LiveRangeEdit::checkRematerializable(VNInfo *VNI,
72fcaf7f86SDimitry Andric                                           const MachineInstr *DefMI) {
730b57cec5SDimitry Andric   assert(DefMI && "Missing instruction");
740b57cec5SDimitry Andric   ScannedRemattable = true;
75fcaf7f86SDimitry Andric   if (!TII.isTriviallyReMaterializable(*DefMI))
760b57cec5SDimitry Andric     return false;
770b57cec5SDimitry Andric   Remattable.insert(VNI);
780b57cec5SDimitry Andric   return true;
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
81fcaf7f86SDimitry Andric void LiveRangeEdit::scanRemattable() {
820b57cec5SDimitry Andric   for (VNInfo *VNI : getParent().valnos) {
830b57cec5SDimitry Andric     if (VNI->isUnused())
840b57cec5SDimitry Andric       continue;
850b57cec5SDimitry Andric     unsigned Original = VRM->getOriginal(getReg());
860b57cec5SDimitry Andric     LiveInterval &OrigLI = LIS.getInterval(Original);
870b57cec5SDimitry Andric     VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
880b57cec5SDimitry Andric     if (!OrigVNI)
890b57cec5SDimitry Andric       continue;
900b57cec5SDimitry Andric     MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
910b57cec5SDimitry Andric     if (!DefMI)
920b57cec5SDimitry Andric       continue;
93fcaf7f86SDimitry Andric     checkRematerializable(OrigVNI, DefMI);
940b57cec5SDimitry Andric   }
950b57cec5SDimitry Andric   ScannedRemattable = true;
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric 
98fcaf7f86SDimitry Andric bool LiveRangeEdit::anyRematerializable() {
990b57cec5SDimitry Andric   if (!ScannedRemattable)
100fcaf7f86SDimitry Andric     scanRemattable();
1010b57cec5SDimitry Andric   return !Remattable.empty();
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric /// allUsesAvailableAt - Return true if all registers used by OrigMI at
1050b57cec5SDimitry Andric /// OrigIdx are also available with the same value at UseIdx.
1060b57cec5SDimitry Andric bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI,
1070b57cec5SDimitry Andric                                        SlotIndex OrigIdx,
1080b57cec5SDimitry Andric                                        SlotIndex UseIdx) const {
1090b57cec5SDimitry Andric   OrigIdx = OrigIdx.getRegSlot(true);
110349cc55cSDimitry Andric   UseIdx = std::max(UseIdx, UseIdx.getRegSlot(true));
1114824e7fdSDimitry Andric   for (const MachineOperand &MO : OrigMI->operands()) {
1120b57cec5SDimitry Andric     if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
1130b57cec5SDimitry Andric       continue;
1140b57cec5SDimitry Andric 
115fe6060f1SDimitry Andric     // We can't remat physreg uses, unless it is a constant or target wants
116fe6060f1SDimitry Andric     // to ignore this use.
117*bdd1243dSDimitry Andric     if (MO.getReg().isPhysical()) {
118fe6060f1SDimitry Andric       if (MRI.isConstantPhysReg(MO.getReg()) || TII.isIgnorableUse(MO))
1190b57cec5SDimitry Andric         continue;
1200b57cec5SDimitry Andric       return false;
1210b57cec5SDimitry Andric     }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric     LiveInterval &li = LIS.getInterval(MO.getReg());
1240b57cec5SDimitry Andric     const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
1250b57cec5SDimitry Andric     if (!OVNI)
1260b57cec5SDimitry Andric       continue;
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric     // Don't allow rematerialization immediately after the original def.
1290b57cec5SDimitry Andric     // It would be incorrect if OrigMI redefines the register.
1300b57cec5SDimitry Andric     // See PR14098.
1310b57cec5SDimitry Andric     if (SlotIndex::isSameInstr(OrigIdx, UseIdx))
1320b57cec5SDimitry Andric       return false;
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric     if (OVNI != li.getVNInfoAt(UseIdx))
1350b57cec5SDimitry Andric       return false;
1360eae32dcSDimitry Andric 
1370eae32dcSDimitry Andric     // Check that subrange is live at UseIdx.
138*bdd1243dSDimitry Andric     if (li.hasSubRanges()) {
1390eae32dcSDimitry Andric       const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
140*bdd1243dSDimitry Andric       unsigned SubReg = MO.getSubReg();
141*bdd1243dSDimitry Andric       LaneBitmask LM = SubReg ? TRI->getSubRegIndexLaneMask(SubReg)
142*bdd1243dSDimitry Andric                               : MRI.getMaxLaneMaskForVReg(MO.getReg());
1430eae32dcSDimitry Andric       for (LiveInterval::SubRange &SR : li.subranges()) {
1440eae32dcSDimitry Andric         if ((SR.LaneMask & LM).none())
1450eae32dcSDimitry Andric           continue;
1460eae32dcSDimitry Andric         if (!SR.liveAt(UseIdx))
1470eae32dcSDimitry Andric           return false;
1480eae32dcSDimitry Andric         // Early exit if all used lanes are checked. No need to continue.
1490eae32dcSDimitry Andric         LM &= ~SR.LaneMask;
1500eae32dcSDimitry Andric         if (LM.none())
1510eae32dcSDimitry Andric           break;
1520eae32dcSDimitry Andric       }
1530eae32dcSDimitry Andric     }
1540b57cec5SDimitry Andric   }
1550b57cec5SDimitry Andric   return true;
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
1590b57cec5SDimitry Andric                                        SlotIndex UseIdx, bool cheapAsAMove) {
1600b57cec5SDimitry Andric   assert(ScannedRemattable && "Call anyRematerializable first");
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   // Use scanRemattable info.
1630b57cec5SDimitry Andric   if (!Remattable.count(OrigVNI))
1640b57cec5SDimitry Andric     return false;
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   // No defining instruction provided.
1670b57cec5SDimitry Andric   SlotIndex DefIdx;
1680b57cec5SDimitry Andric   assert(RM.OrigMI && "No defining instruction for remattable value");
1690b57cec5SDimitry Andric   DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric   // If only cheap remats were requested, bail out early.
1720b57cec5SDimitry Andric   if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI))
1730b57cec5SDimitry Andric     return false;
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   // Verify that all used registers are available with the same values.
1760b57cec5SDimitry Andric   if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
1770b57cec5SDimitry Andric     return false;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   return true;
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
1830b57cec5SDimitry Andric                                          MachineBasicBlock::iterator MI,
1840b57cec5SDimitry Andric                                          unsigned DestReg,
1850b57cec5SDimitry Andric                                          const Remat &RM,
1860b57cec5SDimitry Andric                                          const TargetRegisterInfo &tri,
187*bdd1243dSDimitry Andric                                          bool Late,
188*bdd1243dSDimitry Andric                                          unsigned SubIdx,
189*bdd1243dSDimitry Andric                                          MachineInstr *ReplaceIndexMI) {
1900b57cec5SDimitry Andric   assert(RM.OrigMI && "Invalid remat");
191*bdd1243dSDimitry Andric   TII.reMaterialize(MBB, MI, DestReg, SubIdx, *RM.OrigMI, tri);
1920b57cec5SDimitry Andric   // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg
1930b57cec5SDimitry Andric   // to false anyway in case the isDead flag of RM.OrigMI's dest register
1940b57cec5SDimitry Andric   // is true.
1950b57cec5SDimitry Andric   (*--MI).getOperand(0).setIsDead(false);
1960b57cec5SDimitry Andric   Rematted.insert(RM.ParentVNI);
197*bdd1243dSDimitry Andric   ++NumReMaterialization;
198*bdd1243dSDimitry Andric 
199*bdd1243dSDimitry Andric   if (ReplaceIndexMI)
200*bdd1243dSDimitry Andric     return LIS.ReplaceMachineInstrInMaps(*ReplaceIndexMI, *MI).getRegSlot();
2010b57cec5SDimitry Andric   return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
2020b57cec5SDimitry Andric }
2030b57cec5SDimitry Andric 
2045ffd83dbSDimitry Andric void LiveRangeEdit::eraseVirtReg(Register Reg) {
2050b57cec5SDimitry Andric   if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
2060b57cec5SDimitry Andric     LIS.removeInterval(Reg);
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
2100b57cec5SDimitry Andric                                SmallVectorImpl<MachineInstr*> &Dead) {
2110b57cec5SDimitry Andric   MachineInstr *DefMI = nullptr, *UseMI = nullptr;
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   // Check that there is a single def and a single use.
214e8d8bef9SDimitry Andric   for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg())) {
2150b57cec5SDimitry Andric     MachineInstr *MI = MO.getParent();
2160b57cec5SDimitry Andric     if (MO.isDef()) {
2170b57cec5SDimitry Andric       if (DefMI && DefMI != MI)
2180b57cec5SDimitry Andric         return false;
2190b57cec5SDimitry Andric       if (!MI->canFoldAsLoad())
2200b57cec5SDimitry Andric         return false;
2210b57cec5SDimitry Andric       DefMI = MI;
2220b57cec5SDimitry Andric     } else if (!MO.isUndef()) {
2230b57cec5SDimitry Andric       if (UseMI && UseMI != MI)
2240b57cec5SDimitry Andric         return false;
2250b57cec5SDimitry Andric       // FIXME: Targets don't know how to fold subreg uses.
2260b57cec5SDimitry Andric       if (MO.getSubReg())
2270b57cec5SDimitry Andric         return false;
2280b57cec5SDimitry Andric       UseMI = MI;
2290b57cec5SDimitry Andric     }
2300b57cec5SDimitry Andric   }
2310b57cec5SDimitry Andric   if (!DefMI || !UseMI)
2320b57cec5SDimitry Andric     return false;
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   // Since we're moving the DefMI load, make sure we're not extending any live
2350b57cec5SDimitry Andric   // ranges.
2360b57cec5SDimitry Andric   if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
2370b57cec5SDimitry Andric                           LIS.getInstructionIndex(*UseMI)))
2380b57cec5SDimitry Andric     return false;
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric   // We also need to make sure it is safe to move the load.
2410b57cec5SDimitry Andric   // Assume there are stores between DefMI and UseMI.
2420b57cec5SDimitry Andric   bool SawStore = true;
2430b57cec5SDimitry Andric   if (!DefMI->isSafeToMove(nullptr, SawStore))
2440b57cec5SDimitry Andric     return false;
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Try to fold single def: " << *DefMI
2470b57cec5SDimitry Andric                     << "       into single use: " << *UseMI);
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric   SmallVector<unsigned, 8> Ops;
250e8d8bef9SDimitry Andric   if (UseMI->readsWritesVirtualRegister(LI->reg(), &Ops).second)
2510b57cec5SDimitry Andric     return false;
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric   MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
2540b57cec5SDimitry Andric   if (!FoldMI)
2550b57cec5SDimitry Andric     return false;
2560b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "                folded: " << *FoldMI);
2570b57cec5SDimitry Andric   LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
2585ffd83dbSDimitry Andric   // Update the call site info.
2595ffd83dbSDimitry Andric   if (UseMI->shouldUpdateCallSiteInfo())
2608bcb0991SDimitry Andric     UseMI->getMF()->moveCallSiteInfo(UseMI, FoldMI);
2610b57cec5SDimitry Andric   UseMI->eraseFromParent();
262e8d8bef9SDimitry Andric   DefMI->addRegisterDead(LI->reg(), nullptr);
2630b57cec5SDimitry Andric   Dead.push_back(DefMI);
2640b57cec5SDimitry Andric   ++NumDCEFoldedLoads;
2650b57cec5SDimitry Andric   return true;
2660b57cec5SDimitry Andric }
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
2690b57cec5SDimitry Andric                               const MachineOperand &MO) const {
2700b57cec5SDimitry Andric   const MachineInstr &MI = *MO.getParent();
2710b57cec5SDimitry Andric   SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
2720b57cec5SDimitry Andric   if (LI.Query(Idx).isKill())
2730b57cec5SDimitry Andric     return true;
2740b57cec5SDimitry Andric   const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
2750b57cec5SDimitry Andric   unsigned SubReg = MO.getSubReg();
2760b57cec5SDimitry Andric   LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
2770b57cec5SDimitry Andric   for (const LiveInterval::SubRange &S : LI.subranges()) {
2780b57cec5SDimitry Andric     if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
2790b57cec5SDimitry Andric       return true;
2800b57cec5SDimitry Andric   }
2810b57cec5SDimitry Andric   return false;
2820b57cec5SDimitry Andric }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric /// Find all live intervals that need to shrink, then remove the instruction.
285fcaf7f86SDimitry Andric void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink) {
2860b57cec5SDimitry Andric   assert(MI->allDefsAreDead() && "Def isn't really dead");
2870b57cec5SDimitry Andric   SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric   // Never delete a bundled instruction.
2900b57cec5SDimitry Andric   if (MI->isBundled()) {
2910b57cec5SDimitry Andric     return;
2920b57cec5SDimitry Andric   }
2930b57cec5SDimitry Andric   // Never delete inline asm.
2940b57cec5SDimitry Andric   if (MI->isInlineAsm()) {
2950b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
2960b57cec5SDimitry Andric     return;
2970b57cec5SDimitry Andric   }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   // Use the same criteria as DeadMachineInstructionElim.
3000b57cec5SDimitry Andric   bool SawStore = false;
3010b57cec5SDimitry Andric   if (!MI->isSafeToMove(nullptr, SawStore)) {
3020b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
3030b57cec5SDimitry Andric     return;
3040b57cec5SDimitry Andric   }
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   // Collect virtual registers to be erased after MI is gone.
3090b57cec5SDimitry Andric   SmallVector<unsigned, 8> RegsToErase;
3100b57cec5SDimitry Andric   bool ReadsPhysRegs = false;
3110b57cec5SDimitry Andric   bool isOrigDef = false;
312972a253aSDimitry Andric   Register Dest;
313972a253aSDimitry Andric   unsigned DestSubReg;
3140b57cec5SDimitry Andric   // Only optimize rematerialize case when the instruction has one def, since
3150b57cec5SDimitry Andric   // otherwise we could leave some dead defs in the code.  This case is
3160b57cec5SDimitry Andric   // extremely rare.
3170b57cec5SDimitry Andric   if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
3180b57cec5SDimitry Andric       MI->getDesc().getNumDefs() == 1) {
3190b57cec5SDimitry Andric     Dest = MI->getOperand(0).getReg();
320972a253aSDimitry Andric     DestSubReg = MI->getOperand(0).getSubReg();
321*bdd1243dSDimitry Andric     Register Original = VRM->getOriginal(Dest);
3220b57cec5SDimitry Andric     LiveInterval &OrigLI = LIS.getInterval(Original);
3230b57cec5SDimitry Andric     VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
3240b57cec5SDimitry Andric     // The original live-range may have been shrunk to
3250b57cec5SDimitry Andric     // an empty live-range. It happens when it is dead, but
3260b57cec5SDimitry Andric     // we still keep it around to be able to rematerialize
3270b57cec5SDimitry Andric     // other values that depend on it.
3280b57cec5SDimitry Andric     if (OrigVNI)
3290b57cec5SDimitry Andric       isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx);
3300b57cec5SDimitry Andric   }
3310b57cec5SDimitry Andric 
332349cc55cSDimitry Andric   bool HasLiveVRegUses = false;
333349cc55cSDimitry Andric 
3340b57cec5SDimitry Andric   // Check for live intervals that may shrink
335349cc55cSDimitry Andric   for (const MachineOperand &MO : MI->operands()) {
336349cc55cSDimitry Andric     if (!MO.isReg())
3370b57cec5SDimitry Andric       continue;
338349cc55cSDimitry Andric     Register Reg = MO.getReg();
339*bdd1243dSDimitry Andric     if (!Reg.isVirtual()) {
3400b57cec5SDimitry Andric       // Check if MI reads any unreserved physregs.
341349cc55cSDimitry Andric       if (Reg && MO.readsReg() && !MRI.isReserved(Reg))
3420b57cec5SDimitry Andric         ReadsPhysRegs = true;
343349cc55cSDimitry Andric       else if (MO.isDef())
344e8d8bef9SDimitry Andric         LIS.removePhysRegDefAt(Reg.asMCReg(), Idx);
3450b57cec5SDimitry Andric       continue;
3460b57cec5SDimitry Andric     }
3470b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(Reg);
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric     // Shrink read registers, unless it is likely to be expensive and
3500b57cec5SDimitry Andric     // unlikely to change anything. We typically don't want to shrink the
3510b57cec5SDimitry Andric     // PIC base register that has lots of uses everywhere.
3520b57cec5SDimitry Andric     // Always shrink COPY uses that probably come from live range splitting.
353349cc55cSDimitry Andric     if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MO.isDef())) ||
354349cc55cSDimitry Andric         (MO.readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, MO))))
3550b57cec5SDimitry Andric       ToShrink.insert(&LI);
356349cc55cSDimitry Andric     else if (MO.readsReg())
357349cc55cSDimitry Andric       HasLiveVRegUses = true;
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric     // Remove defined value.
360349cc55cSDimitry Andric     if (MO.isDef()) {
3610b57cec5SDimitry Andric       if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
362e8d8bef9SDimitry Andric         TheDelegate->LRE_WillShrinkVirtReg(LI.reg());
3630b57cec5SDimitry Andric       LIS.removeVRegDefAt(LI, Idx);
3640b57cec5SDimitry Andric       if (LI.empty())
3650b57cec5SDimitry Andric         RegsToErase.push_back(Reg);
3660b57cec5SDimitry Andric     }
3670b57cec5SDimitry Andric   }
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   // Currently, we don't support DCE of physreg live ranges. If MI reads
3700b57cec5SDimitry Andric   // any unreserved physregs, don't erase the instruction, but turn it into
3710b57cec5SDimitry Andric   // a KILL instead. This way, the physreg live ranges don't end up
3720b57cec5SDimitry Andric   // dangling.
3730b57cec5SDimitry Andric   // FIXME: It would be better to have something like shrinkToUses() for
3740b57cec5SDimitry Andric   // physregs. That could potentially enable more DCE and it would free up
3750b57cec5SDimitry Andric   // the physreg. It would not happen often, though.
3760b57cec5SDimitry Andric   if (ReadsPhysRegs) {
3770b57cec5SDimitry Andric     MI->setDesc(TII.get(TargetOpcode::KILL));
3780b57cec5SDimitry Andric     // Remove all operands that aren't physregs.
3790b57cec5SDimitry Andric     for (unsigned i = MI->getNumOperands(); i; --i) {
3800b57cec5SDimitry Andric       const MachineOperand &MO = MI->getOperand(i-1);
381*bdd1243dSDimitry Andric       if (MO.isReg() && MO.getReg().isPhysical())
3820b57cec5SDimitry Andric         continue;
38381ad6265SDimitry Andric       MI->removeOperand(i-1);
3840b57cec5SDimitry Andric     }
3850b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
3860b57cec5SDimitry Andric   } else {
3870b57cec5SDimitry Andric     // If the dest of MI is an original reg and MI is reMaterializable,
3880b57cec5SDimitry Andric     // don't delete the inst. Replace the dest with a new reg, and keep
3890b57cec5SDimitry Andric     // the inst for remat of other siblings. The inst is saved in
3900b57cec5SDimitry Andric     // LiveRangeEdit::DeadRemats and will be deleted after all the
3910b57cec5SDimitry Andric     // allocations of the func are done.
392349cc55cSDimitry Andric     // However, immediately delete instructions which have unshrunk virtual
393349cc55cSDimitry Andric     // register uses. That may provoke RA to split an interval at the KILL
394349cc55cSDimitry Andric     // and later result in an invalid live segment end.
395349cc55cSDimitry Andric     if (isOrigDef && DeadRemats && !HasLiveVRegUses &&
396fcaf7f86SDimitry Andric         TII.isTriviallyReMaterializable(*MI)) {
3970b57cec5SDimitry Andric       LiveInterval &NewLI = createEmptyIntervalFrom(Dest, false);
398972a253aSDimitry Andric       VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator();
399972a253aSDimitry Andric       VNInfo *VNI = NewLI.getNextValue(Idx, Alloc);
4000b57cec5SDimitry Andric       NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI));
401972a253aSDimitry Andric 
402972a253aSDimitry Andric       if (DestSubReg) {
403972a253aSDimitry Andric         const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
404972a253aSDimitry Andric         auto *SR = NewLI.createSubRange(
405972a253aSDimitry Andric             Alloc, TRI->getSubRegIndexLaneMask(DestSubReg));
406972a253aSDimitry Andric         SR->addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(),
407972a253aSDimitry Andric                                              SR->getNextValue(Idx, Alloc)));
408972a253aSDimitry Andric       }
409972a253aSDimitry Andric 
4100b57cec5SDimitry Andric       pop_back();
4110b57cec5SDimitry Andric       DeadRemats->insert(MI);
4120b57cec5SDimitry Andric       const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
413e8d8bef9SDimitry Andric       MI->substituteRegister(Dest, NewLI.reg(), 0, TRI);
4140b57cec5SDimitry Andric       MI->getOperand(0).setIsDead(true);
4150b57cec5SDimitry Andric     } else {
4160b57cec5SDimitry Andric       if (TheDelegate)
4170b57cec5SDimitry Andric         TheDelegate->LRE_WillEraseInstruction(MI);
4180b57cec5SDimitry Andric       LIS.RemoveMachineInstrFromMaps(*MI);
4190b57cec5SDimitry Andric       MI->eraseFromParent();
4200b57cec5SDimitry Andric       ++NumDCEDeleted;
4210b57cec5SDimitry Andric     }
4220b57cec5SDimitry Andric   }
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric   // Erase any virtregs that are now empty and unused. There may be <undef>
4250b57cec5SDimitry Andric   // uses around. Keep the empty live range in that case.
4260b57cec5SDimitry Andric   for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
4275ffd83dbSDimitry Andric     Register Reg = RegsToErase[i];
4280b57cec5SDimitry Andric     if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
4290b57cec5SDimitry Andric       ToShrink.remove(&LIS.getInterval(Reg));
4300b57cec5SDimitry Andric       eraseVirtReg(Reg);
4310b57cec5SDimitry Andric     }
4320b57cec5SDimitry Andric   }
4330b57cec5SDimitry Andric }
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
436fcaf7f86SDimitry Andric                                       ArrayRef<Register> RegsBeingSpilled) {
4370b57cec5SDimitry Andric   ToShrinkSet ToShrink;
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric   for (;;) {
4400b57cec5SDimitry Andric     // Erase all dead defs.
4410b57cec5SDimitry Andric     while (!Dead.empty())
442fcaf7f86SDimitry Andric       eliminateDeadDef(Dead.pop_back_val(), ToShrink);
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric     if (ToShrink.empty())
4450b57cec5SDimitry Andric       break;
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric     // Shrink just one live interval. Then delete new dead defs.
448349cc55cSDimitry Andric     LiveInterval *LI = ToShrink.pop_back_val();
4490b57cec5SDimitry Andric     if (foldAsLoad(LI, Dead))
4500b57cec5SDimitry Andric       continue;
451*bdd1243dSDimitry Andric     Register VReg = LI->reg();
4520b57cec5SDimitry Andric     if (TheDelegate)
4530b57cec5SDimitry Andric       TheDelegate->LRE_WillShrinkVirtReg(VReg);
4540b57cec5SDimitry Andric     if (!LIS.shrinkToUses(LI, &Dead))
4550b57cec5SDimitry Andric       continue;
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric     // Don't create new intervals for a register being spilled.
4580b57cec5SDimitry Andric     // The new intervals would have to be spilled anyway so its not worth it.
4590b57cec5SDimitry Andric     // Also they currently aren't spilled so creating them and not spilling
4600b57cec5SDimitry Andric     // them results in incorrect code.
4614824e7fdSDimitry Andric     if (llvm::is_contained(RegsBeingSpilled, VReg))
4624824e7fdSDimitry Andric       continue;
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric     // LI may have been separated, create new intervals.
4650b57cec5SDimitry Andric     LI->RenumberValues();
4660b57cec5SDimitry Andric     SmallVector<LiveInterval*, 8> SplitLIs;
4670b57cec5SDimitry Andric     LIS.splitSeparateComponents(*LI, SplitLIs);
4680b57cec5SDimitry Andric     if (!SplitLIs.empty())
4690b57cec5SDimitry Andric       ++NumFracRanges;
4700b57cec5SDimitry Andric 
471e8d8bef9SDimitry Andric     Register Original = VRM ? VRM->getOriginal(VReg) : Register();
4720b57cec5SDimitry Andric     for (const LiveInterval *SplitLI : SplitLIs) {
4730b57cec5SDimitry Andric       // If LI is an original interval that hasn't been split yet, make the new
4740b57cec5SDimitry Andric       // intervals their own originals instead of referring to LI. The original
4750b57cec5SDimitry Andric       // interval must contain all the split products, and LI doesn't.
4760b57cec5SDimitry Andric       if (Original != VReg && Original != 0)
477e8d8bef9SDimitry Andric         VRM->setIsSplitFromReg(SplitLI->reg(), Original);
4780b57cec5SDimitry Andric       if (TheDelegate)
479e8d8bef9SDimitry Andric         TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg(), VReg);
4800b57cec5SDimitry Andric     }
4810b57cec5SDimitry Andric   }
4820b57cec5SDimitry Andric }
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric // Keep track of new virtual registers created via
4850b57cec5SDimitry Andric // MachineRegisterInfo::createVirtualRegister.
4860b57cec5SDimitry Andric void
4875ffd83dbSDimitry Andric LiveRangeEdit::MRI_NoteNewVirtualRegister(Register VReg) {
4880b57cec5SDimitry Andric   if (VRM)
4890b57cec5SDimitry Andric     VRM->grow();
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric   NewRegs.push_back(VReg);
4920b57cec5SDimitry Andric }
4930b57cec5SDimitry Andric 
494fe6060f1SDimitry Andric void LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
495fe6060f1SDimitry Andric                                              VirtRegAuxInfo &VRAI) {
4960b57cec5SDimitry Andric   for (unsigned I = 0, Size = size(); I < Size; ++I) {
4970b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(get(I));
498e8d8bef9SDimitry Andric     if (MRI.recomputeRegClass(LI.reg()))
4990b57cec5SDimitry Andric       LLVM_DEBUG({
5000b57cec5SDimitry Andric         const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
501e8d8bef9SDimitry Andric         dbgs() << "Inflated " << printReg(LI.reg()) << " to "
502e8d8bef9SDimitry Andric                << TRI->getRegClassName(MRI.getRegClass(LI.reg())) << '\n';
5030b57cec5SDimitry Andric       });
5040b57cec5SDimitry Andric     VRAI.calculateSpillWeightAndHint(LI);
5050b57cec5SDimitry Andric   }
5060b57cec5SDimitry Andric }
507