xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/LiveRangeEdit.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
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");
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric void LiveRangeEdit::Delegate::anchor() { }
320b57cec5SDimitry Andric 
335ffd83dbSDimitry Andric LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(Register OldReg,
340b57cec5SDimitry Andric                                                      bool createSubRanges) {
358bcb0991SDimitry Andric   Register VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
360b57cec5SDimitry Andric   if (VRM)
370b57cec5SDimitry Andric     VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric   LiveInterval &LI = LIS.createEmptyInterval(VReg);
400b57cec5SDimitry Andric   if (Parent && !Parent->isSpillable())
410b57cec5SDimitry Andric     LI.markNotSpillable();
420b57cec5SDimitry Andric   if (createSubRanges) {
430b57cec5SDimitry Andric     // Create empty subranges if the OldReg's interval has them. Do not create
440b57cec5SDimitry Andric     // the main range here---it will be constructed later after the subranges
450b57cec5SDimitry Andric     // have been finalized.
460b57cec5SDimitry Andric     LiveInterval &OldLI = LIS.getInterval(OldReg);
470b57cec5SDimitry Andric     VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator();
480b57cec5SDimitry Andric     for (LiveInterval::SubRange &S : OldLI.subranges())
490b57cec5SDimitry Andric       LI.createSubRange(Alloc, S.LaneMask);
500b57cec5SDimitry Andric   }
510b57cec5SDimitry Andric   return LI;
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
545ffd83dbSDimitry Andric Register LiveRangeEdit::createFrom(Register OldReg) {
558bcb0991SDimitry Andric   Register VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
560b57cec5SDimitry Andric   if (VRM) {
570b57cec5SDimitry Andric     VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
580b57cec5SDimitry Andric   }
590b57cec5SDimitry Andric   // FIXME: Getting the interval here actually computes it.
600b57cec5SDimitry Andric   // In theory, this may not be what we want, but in practice
610b57cec5SDimitry Andric   // the createEmptyIntervalFrom API is used when this is not
620b57cec5SDimitry Andric   // the case. Generally speaking we just want to annotate the
630b57cec5SDimitry Andric   // LiveInterval when it gets created but we cannot do that at
640b57cec5SDimitry Andric   // the moment.
650b57cec5SDimitry Andric   if (Parent && !Parent->isSpillable())
660b57cec5SDimitry Andric     LIS.getInterval(VReg).markNotSpillable();
670b57cec5SDimitry Andric   return VReg;
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric bool LiveRangeEdit::checkRematerializable(VNInfo *VNI,
710b57cec5SDimitry Andric                                           const MachineInstr *DefMI,
725ffd83dbSDimitry Andric                                           AAResults *aa) {
730b57cec5SDimitry Andric   assert(DefMI && "Missing instruction");
740b57cec5SDimitry Andric   ScannedRemattable = true;
750b57cec5SDimitry Andric   if (!TII.isTriviallyReMaterializable(*DefMI, aa))
760b57cec5SDimitry Andric     return false;
770b57cec5SDimitry Andric   Remattable.insert(VNI);
780b57cec5SDimitry Andric   return true;
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
815ffd83dbSDimitry Andric void LiveRangeEdit::scanRemattable(AAResults *aa) {
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;
930b57cec5SDimitry Andric     checkRematerializable(OrigVNI, DefMI, aa);
940b57cec5SDimitry Andric   }
950b57cec5SDimitry Andric   ScannedRemattable = true;
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric 
985ffd83dbSDimitry Andric bool LiveRangeEdit::anyRematerializable(AAResults *aa) {
990b57cec5SDimitry Andric   if (!ScannedRemattable)
1000b57cec5SDimitry Andric     scanRemattable(aa);
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));
111*4824e7fdSDimitry 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.
1178bcb0991SDimitry Andric     if (Register::isPhysicalRegister(MO.getReg())) {
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;
1360b57cec5SDimitry Andric   }
1370b57cec5SDimitry Andric   return true;
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
1410b57cec5SDimitry Andric                                        SlotIndex UseIdx, bool cheapAsAMove) {
1420b57cec5SDimitry Andric   assert(ScannedRemattable && "Call anyRematerializable first");
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   // Use scanRemattable info.
1450b57cec5SDimitry Andric   if (!Remattable.count(OrigVNI))
1460b57cec5SDimitry Andric     return false;
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   // No defining instruction provided.
1490b57cec5SDimitry Andric   SlotIndex DefIdx;
1500b57cec5SDimitry Andric   assert(RM.OrigMI && "No defining instruction for remattable value");
1510b57cec5SDimitry Andric   DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   // If only cheap remats were requested, bail out early.
1540b57cec5SDimitry Andric   if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI))
1550b57cec5SDimitry Andric     return false;
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   // Verify that all used registers are available with the same values.
1580b57cec5SDimitry Andric   if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
1590b57cec5SDimitry Andric     return false;
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric   return true;
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
1650b57cec5SDimitry Andric                                          MachineBasicBlock::iterator MI,
1660b57cec5SDimitry Andric                                          unsigned DestReg,
1670b57cec5SDimitry Andric                                          const Remat &RM,
1680b57cec5SDimitry Andric                                          const TargetRegisterInfo &tri,
1690b57cec5SDimitry Andric                                          bool Late) {
1700b57cec5SDimitry Andric   assert(RM.OrigMI && "Invalid remat");
1710b57cec5SDimitry Andric   TII.reMaterialize(MBB, MI, DestReg, 0, *RM.OrigMI, tri);
1720b57cec5SDimitry Andric   // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg
1730b57cec5SDimitry Andric   // to false anyway in case the isDead flag of RM.OrigMI's dest register
1740b57cec5SDimitry Andric   // is true.
1750b57cec5SDimitry Andric   (*--MI).getOperand(0).setIsDead(false);
1760b57cec5SDimitry Andric   Rematted.insert(RM.ParentVNI);
1770b57cec5SDimitry Andric   return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric 
1805ffd83dbSDimitry Andric void LiveRangeEdit::eraseVirtReg(Register Reg) {
1810b57cec5SDimitry Andric   if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
1820b57cec5SDimitry Andric     LIS.removeInterval(Reg);
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
1860b57cec5SDimitry Andric                                SmallVectorImpl<MachineInstr*> &Dead) {
1870b57cec5SDimitry Andric   MachineInstr *DefMI = nullptr, *UseMI = nullptr;
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric   // Check that there is a single def and a single use.
190e8d8bef9SDimitry Andric   for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg())) {
1910b57cec5SDimitry Andric     MachineInstr *MI = MO.getParent();
1920b57cec5SDimitry Andric     if (MO.isDef()) {
1930b57cec5SDimitry Andric       if (DefMI && DefMI != MI)
1940b57cec5SDimitry Andric         return false;
1950b57cec5SDimitry Andric       if (!MI->canFoldAsLoad())
1960b57cec5SDimitry Andric         return false;
1970b57cec5SDimitry Andric       DefMI = MI;
1980b57cec5SDimitry Andric     } else if (!MO.isUndef()) {
1990b57cec5SDimitry Andric       if (UseMI && UseMI != MI)
2000b57cec5SDimitry Andric         return false;
2010b57cec5SDimitry Andric       // FIXME: Targets don't know how to fold subreg uses.
2020b57cec5SDimitry Andric       if (MO.getSubReg())
2030b57cec5SDimitry Andric         return false;
2040b57cec5SDimitry Andric       UseMI = MI;
2050b57cec5SDimitry Andric     }
2060b57cec5SDimitry Andric   }
2070b57cec5SDimitry Andric   if (!DefMI || !UseMI)
2080b57cec5SDimitry Andric     return false;
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   // Since we're moving the DefMI load, make sure we're not extending any live
2110b57cec5SDimitry Andric   // ranges.
2120b57cec5SDimitry Andric   if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
2130b57cec5SDimitry Andric                           LIS.getInstructionIndex(*UseMI)))
2140b57cec5SDimitry Andric     return false;
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric   // We also need to make sure it is safe to move the load.
2170b57cec5SDimitry Andric   // Assume there are stores between DefMI and UseMI.
2180b57cec5SDimitry Andric   bool SawStore = true;
2190b57cec5SDimitry Andric   if (!DefMI->isSafeToMove(nullptr, SawStore))
2200b57cec5SDimitry Andric     return false;
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Try to fold single def: " << *DefMI
2230b57cec5SDimitry Andric                     << "       into single use: " << *UseMI);
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric   SmallVector<unsigned, 8> Ops;
226e8d8bef9SDimitry Andric   if (UseMI->readsWritesVirtualRegister(LI->reg(), &Ops).second)
2270b57cec5SDimitry Andric     return false;
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
2300b57cec5SDimitry Andric   if (!FoldMI)
2310b57cec5SDimitry Andric     return false;
2320b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "                folded: " << *FoldMI);
2330b57cec5SDimitry Andric   LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
2345ffd83dbSDimitry Andric   // Update the call site info.
2355ffd83dbSDimitry Andric   if (UseMI->shouldUpdateCallSiteInfo())
2368bcb0991SDimitry Andric     UseMI->getMF()->moveCallSiteInfo(UseMI, FoldMI);
2370b57cec5SDimitry Andric   UseMI->eraseFromParent();
238e8d8bef9SDimitry Andric   DefMI->addRegisterDead(LI->reg(), nullptr);
2390b57cec5SDimitry Andric   Dead.push_back(DefMI);
2400b57cec5SDimitry Andric   ++NumDCEFoldedLoads;
2410b57cec5SDimitry Andric   return true;
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
2450b57cec5SDimitry Andric                               const MachineOperand &MO) const {
2460b57cec5SDimitry Andric   const MachineInstr &MI = *MO.getParent();
2470b57cec5SDimitry Andric   SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
2480b57cec5SDimitry Andric   if (LI.Query(Idx).isKill())
2490b57cec5SDimitry Andric     return true;
2500b57cec5SDimitry Andric   const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
2510b57cec5SDimitry Andric   unsigned SubReg = MO.getSubReg();
2520b57cec5SDimitry Andric   LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
2530b57cec5SDimitry Andric   for (const LiveInterval::SubRange &S : LI.subranges()) {
2540b57cec5SDimitry Andric     if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
2550b57cec5SDimitry Andric       return true;
2560b57cec5SDimitry Andric   }
2570b57cec5SDimitry Andric   return false;
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric /// Find all live intervals that need to shrink, then remove the instruction.
2610b57cec5SDimitry Andric void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink,
2625ffd83dbSDimitry Andric                                      AAResults *AA) {
2630b57cec5SDimitry Andric   assert(MI->allDefsAreDead() && "Def isn't really dead");
2640b57cec5SDimitry Andric   SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric   // Never delete a bundled instruction.
2670b57cec5SDimitry Andric   if (MI->isBundled()) {
2680b57cec5SDimitry Andric     return;
2690b57cec5SDimitry Andric   }
2700b57cec5SDimitry Andric   // Never delete inline asm.
2710b57cec5SDimitry Andric   if (MI->isInlineAsm()) {
2720b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
2730b57cec5SDimitry Andric     return;
2740b57cec5SDimitry Andric   }
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   // Use the same criteria as DeadMachineInstructionElim.
2770b57cec5SDimitry Andric   bool SawStore = false;
2780b57cec5SDimitry Andric   if (!MI->isSafeToMove(nullptr, SawStore)) {
2790b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
2800b57cec5SDimitry Andric     return;
2810b57cec5SDimitry Andric   }
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   // Collect virtual registers to be erased after MI is gone.
2860b57cec5SDimitry Andric   SmallVector<unsigned, 8> RegsToErase;
2870b57cec5SDimitry Andric   bool ReadsPhysRegs = false;
2880b57cec5SDimitry Andric   bool isOrigDef = false;
2890b57cec5SDimitry Andric   unsigned Dest;
2900b57cec5SDimitry Andric   // Only optimize rematerialize case when the instruction has one def, since
2910b57cec5SDimitry Andric   // otherwise we could leave some dead defs in the code.  This case is
2920b57cec5SDimitry Andric   // extremely rare.
2930b57cec5SDimitry Andric   if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
2940b57cec5SDimitry Andric       MI->getDesc().getNumDefs() == 1) {
2950b57cec5SDimitry Andric     Dest = MI->getOperand(0).getReg();
2960b57cec5SDimitry Andric     unsigned Original = VRM->getOriginal(Dest);
2970b57cec5SDimitry Andric     LiveInterval &OrigLI = LIS.getInterval(Original);
2980b57cec5SDimitry Andric     VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
2990b57cec5SDimitry Andric     // The original live-range may have been shrunk to
3000b57cec5SDimitry Andric     // an empty live-range. It happens when it is dead, but
3010b57cec5SDimitry Andric     // we still keep it around to be able to rematerialize
3020b57cec5SDimitry Andric     // other values that depend on it.
3030b57cec5SDimitry Andric     if (OrigVNI)
3040b57cec5SDimitry Andric       isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx);
3050b57cec5SDimitry Andric   }
3060b57cec5SDimitry Andric 
307349cc55cSDimitry Andric   bool HasLiveVRegUses = false;
308349cc55cSDimitry Andric 
3090b57cec5SDimitry Andric   // Check for live intervals that may shrink
310349cc55cSDimitry Andric   for (const MachineOperand &MO : MI->operands()) {
311349cc55cSDimitry Andric     if (!MO.isReg())
3120b57cec5SDimitry Andric       continue;
313349cc55cSDimitry Andric     Register Reg = MO.getReg();
3148bcb0991SDimitry Andric     if (!Register::isVirtualRegister(Reg)) {
3150b57cec5SDimitry Andric       // Check if MI reads any unreserved physregs.
316349cc55cSDimitry Andric       if (Reg && MO.readsReg() && !MRI.isReserved(Reg))
3170b57cec5SDimitry Andric         ReadsPhysRegs = true;
318349cc55cSDimitry Andric       else if (MO.isDef())
319e8d8bef9SDimitry Andric         LIS.removePhysRegDefAt(Reg.asMCReg(), Idx);
3200b57cec5SDimitry Andric       continue;
3210b57cec5SDimitry Andric     }
3220b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(Reg);
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric     // Shrink read registers, unless it is likely to be expensive and
3250b57cec5SDimitry Andric     // unlikely to change anything. We typically don't want to shrink the
3260b57cec5SDimitry Andric     // PIC base register that has lots of uses everywhere.
3270b57cec5SDimitry Andric     // Always shrink COPY uses that probably come from live range splitting.
328349cc55cSDimitry Andric     if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MO.isDef())) ||
329349cc55cSDimitry Andric         (MO.readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, MO))))
3300b57cec5SDimitry Andric       ToShrink.insert(&LI);
331349cc55cSDimitry Andric     else if (MO.readsReg())
332349cc55cSDimitry Andric       HasLiveVRegUses = true;
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric     // Remove defined value.
335349cc55cSDimitry Andric     if (MO.isDef()) {
3360b57cec5SDimitry Andric       if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
337e8d8bef9SDimitry Andric         TheDelegate->LRE_WillShrinkVirtReg(LI.reg());
3380b57cec5SDimitry Andric       LIS.removeVRegDefAt(LI, Idx);
3390b57cec5SDimitry Andric       if (LI.empty())
3400b57cec5SDimitry Andric         RegsToErase.push_back(Reg);
3410b57cec5SDimitry Andric     }
3420b57cec5SDimitry Andric   }
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric   // Currently, we don't support DCE of physreg live ranges. If MI reads
3450b57cec5SDimitry Andric   // any unreserved physregs, don't erase the instruction, but turn it into
3460b57cec5SDimitry Andric   // a KILL instead. This way, the physreg live ranges don't end up
3470b57cec5SDimitry Andric   // dangling.
3480b57cec5SDimitry Andric   // FIXME: It would be better to have something like shrinkToUses() for
3490b57cec5SDimitry Andric   // physregs. That could potentially enable more DCE and it would free up
3500b57cec5SDimitry Andric   // the physreg. It would not happen often, though.
3510b57cec5SDimitry Andric   if (ReadsPhysRegs) {
3520b57cec5SDimitry Andric     MI->setDesc(TII.get(TargetOpcode::KILL));
3530b57cec5SDimitry Andric     // Remove all operands that aren't physregs.
3540b57cec5SDimitry Andric     for (unsigned i = MI->getNumOperands(); i; --i) {
3550b57cec5SDimitry Andric       const MachineOperand &MO = MI->getOperand(i-1);
3568bcb0991SDimitry Andric       if (MO.isReg() && Register::isPhysicalRegister(MO.getReg()))
3570b57cec5SDimitry Andric         continue;
3580b57cec5SDimitry Andric       MI->RemoveOperand(i-1);
3590b57cec5SDimitry Andric     }
3600b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
3610b57cec5SDimitry Andric   } else {
3620b57cec5SDimitry Andric     // If the dest of MI is an original reg and MI is reMaterializable,
3630b57cec5SDimitry Andric     // don't delete the inst. Replace the dest with a new reg, and keep
3640b57cec5SDimitry Andric     // the inst for remat of other siblings. The inst is saved in
3650b57cec5SDimitry Andric     // LiveRangeEdit::DeadRemats and will be deleted after all the
3660b57cec5SDimitry Andric     // allocations of the func are done.
367349cc55cSDimitry Andric     // However, immediately delete instructions which have unshrunk virtual
368349cc55cSDimitry Andric     // register uses. That may provoke RA to split an interval at the KILL
369349cc55cSDimitry Andric     // and later result in an invalid live segment end.
370349cc55cSDimitry Andric     if (isOrigDef && DeadRemats && !HasLiveVRegUses &&
371349cc55cSDimitry Andric         TII.isTriviallyReMaterializable(*MI, AA)) {
3720b57cec5SDimitry Andric       LiveInterval &NewLI = createEmptyIntervalFrom(Dest, false);
3730b57cec5SDimitry Andric       VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
3740b57cec5SDimitry Andric       NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI));
3750b57cec5SDimitry Andric       pop_back();
3760b57cec5SDimitry Andric       DeadRemats->insert(MI);
3770b57cec5SDimitry Andric       const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
378e8d8bef9SDimitry Andric       MI->substituteRegister(Dest, NewLI.reg(), 0, TRI);
3790b57cec5SDimitry Andric       MI->getOperand(0).setIsDead(true);
3800b57cec5SDimitry Andric     } else {
3810b57cec5SDimitry Andric       if (TheDelegate)
3820b57cec5SDimitry Andric         TheDelegate->LRE_WillEraseInstruction(MI);
3830b57cec5SDimitry Andric       LIS.RemoveMachineInstrFromMaps(*MI);
3840b57cec5SDimitry Andric       MI->eraseFromParent();
3850b57cec5SDimitry Andric       ++NumDCEDeleted;
3860b57cec5SDimitry Andric     }
3870b57cec5SDimitry Andric   }
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric   // Erase any virtregs that are now empty and unused. There may be <undef>
3900b57cec5SDimitry Andric   // uses around. Keep the empty live range in that case.
3910b57cec5SDimitry Andric   for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
3925ffd83dbSDimitry Andric     Register Reg = RegsToErase[i];
3930b57cec5SDimitry Andric     if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
3940b57cec5SDimitry Andric       ToShrink.remove(&LIS.getInterval(Reg));
3950b57cec5SDimitry Andric       eraseVirtReg(Reg);
3960b57cec5SDimitry Andric     }
3970b57cec5SDimitry Andric   }
3980b57cec5SDimitry Andric }
3990b57cec5SDimitry Andric 
4000b57cec5SDimitry Andric void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
4015ffd83dbSDimitry Andric                                       ArrayRef<Register> RegsBeingSpilled,
4025ffd83dbSDimitry Andric                                       AAResults *AA) {
4030b57cec5SDimitry Andric   ToShrinkSet ToShrink;
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric   for (;;) {
4060b57cec5SDimitry Andric     // Erase all dead defs.
4070b57cec5SDimitry Andric     while (!Dead.empty())
4080b57cec5SDimitry Andric       eliminateDeadDef(Dead.pop_back_val(), ToShrink, AA);
4090b57cec5SDimitry Andric 
4100b57cec5SDimitry Andric     if (ToShrink.empty())
4110b57cec5SDimitry Andric       break;
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric     // Shrink just one live interval. Then delete new dead defs.
414349cc55cSDimitry Andric     LiveInterval *LI = ToShrink.pop_back_val();
4150b57cec5SDimitry Andric     if (foldAsLoad(LI, Dead))
4160b57cec5SDimitry Andric       continue;
417e8d8bef9SDimitry Andric     unsigned VReg = LI->reg();
4180b57cec5SDimitry Andric     if (TheDelegate)
4190b57cec5SDimitry Andric       TheDelegate->LRE_WillShrinkVirtReg(VReg);
4200b57cec5SDimitry Andric     if (!LIS.shrinkToUses(LI, &Dead))
4210b57cec5SDimitry Andric       continue;
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric     // Don't create new intervals for a register being spilled.
4240b57cec5SDimitry Andric     // The new intervals would have to be spilled anyway so its not worth it.
4250b57cec5SDimitry Andric     // Also they currently aren't spilled so creating them and not spilling
4260b57cec5SDimitry Andric     // them results in incorrect code.
427*4824e7fdSDimitry Andric     if (llvm::is_contained(RegsBeingSpilled, VReg))
428*4824e7fdSDimitry Andric       continue;
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric     // LI may have been separated, create new intervals.
4310b57cec5SDimitry Andric     LI->RenumberValues();
4320b57cec5SDimitry Andric     SmallVector<LiveInterval*, 8> SplitLIs;
4330b57cec5SDimitry Andric     LIS.splitSeparateComponents(*LI, SplitLIs);
4340b57cec5SDimitry Andric     if (!SplitLIs.empty())
4350b57cec5SDimitry Andric       ++NumFracRanges;
4360b57cec5SDimitry Andric 
437e8d8bef9SDimitry Andric     Register Original = VRM ? VRM->getOriginal(VReg) : Register();
4380b57cec5SDimitry Andric     for (const LiveInterval *SplitLI : SplitLIs) {
4390b57cec5SDimitry Andric       // If LI is an original interval that hasn't been split yet, make the new
4400b57cec5SDimitry Andric       // intervals their own originals instead of referring to LI. The original
4410b57cec5SDimitry Andric       // interval must contain all the split products, and LI doesn't.
4420b57cec5SDimitry Andric       if (Original != VReg && Original != 0)
443e8d8bef9SDimitry Andric         VRM->setIsSplitFromReg(SplitLI->reg(), Original);
4440b57cec5SDimitry Andric       if (TheDelegate)
445e8d8bef9SDimitry Andric         TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg(), VReg);
4460b57cec5SDimitry Andric     }
4470b57cec5SDimitry Andric   }
4480b57cec5SDimitry Andric }
4490b57cec5SDimitry Andric 
4500b57cec5SDimitry Andric // Keep track of new virtual registers created via
4510b57cec5SDimitry Andric // MachineRegisterInfo::createVirtualRegister.
4520b57cec5SDimitry Andric void
4535ffd83dbSDimitry Andric LiveRangeEdit::MRI_NoteNewVirtualRegister(Register VReg) {
4540b57cec5SDimitry Andric   if (VRM)
4550b57cec5SDimitry Andric     VRM->grow();
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric   NewRegs.push_back(VReg);
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric 
460fe6060f1SDimitry Andric void LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
461fe6060f1SDimitry Andric                                              VirtRegAuxInfo &VRAI) {
4620b57cec5SDimitry Andric   for (unsigned I = 0, Size = size(); I < Size; ++I) {
4630b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(get(I));
464e8d8bef9SDimitry Andric     if (MRI.recomputeRegClass(LI.reg()))
4650b57cec5SDimitry Andric       LLVM_DEBUG({
4660b57cec5SDimitry Andric         const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
467e8d8bef9SDimitry Andric         dbgs() << "Inflated " << printReg(LI.reg()) << " to "
468e8d8bef9SDimitry Andric                << TRI->getRegClassName(MRI.getRegClass(LI.reg())) << '\n';
4690b57cec5SDimitry Andric       });
4700b57cec5SDimitry Andric     VRAI.calculateSpillWeightAndHint(LI);
4710b57cec5SDimitry Andric   }
4720b57cec5SDimitry Andric }
473