xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/LiveRangeEdit.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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);
1100b57cec5SDimitry Andric   UseIdx = UseIdx.getRegSlot(true);
1110b57cec5SDimitry Andric   for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) {
1120b57cec5SDimitry Andric     const MachineOperand &MO = OrigMI->getOperand(i);
1130b57cec5SDimitry Andric     if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
1140b57cec5SDimitry Andric       continue;
1150b57cec5SDimitry Andric 
116*fe6060f1SDimitry Andric     // We can't remat physreg uses, unless it is a constant or target wants
117*fe6060f1SDimitry Andric     // to ignore this use.
1188bcb0991SDimitry Andric     if (Register::isPhysicalRegister(MO.getReg())) {
119*fe6060f1SDimitry Andric       if (MRI.isConstantPhysReg(MO.getReg()) || TII.isIgnorableUse(MO))
1200b57cec5SDimitry Andric         continue;
1210b57cec5SDimitry Andric       return false;
1220b57cec5SDimitry Andric     }
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric     LiveInterval &li = LIS.getInterval(MO.getReg());
1250b57cec5SDimitry Andric     const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
1260b57cec5SDimitry Andric     if (!OVNI)
1270b57cec5SDimitry Andric       continue;
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric     // Don't allow rematerialization immediately after the original def.
1300b57cec5SDimitry Andric     // It would be incorrect if OrigMI redefines the register.
1310b57cec5SDimitry Andric     // See PR14098.
1320b57cec5SDimitry Andric     if (SlotIndex::isSameInstr(OrigIdx, UseIdx))
1330b57cec5SDimitry Andric       return false;
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric     if (OVNI != li.getVNInfoAt(UseIdx))
1360b57cec5SDimitry Andric       return false;
1370b57cec5SDimitry Andric   }
1380b57cec5SDimitry Andric   return true;
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
1420b57cec5SDimitry Andric                                        SlotIndex UseIdx, bool cheapAsAMove) {
1430b57cec5SDimitry Andric   assert(ScannedRemattable && "Call anyRematerializable first");
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   // Use scanRemattable info.
1460b57cec5SDimitry Andric   if (!Remattable.count(OrigVNI))
1470b57cec5SDimitry Andric     return false;
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric   // No defining instruction provided.
1500b57cec5SDimitry Andric   SlotIndex DefIdx;
1510b57cec5SDimitry Andric   assert(RM.OrigMI && "No defining instruction for remattable value");
1520b57cec5SDimitry Andric   DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   // If only cheap remats were requested, bail out early.
1550b57cec5SDimitry Andric   if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI))
1560b57cec5SDimitry Andric     return false;
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   // Verify that all used registers are available with the same values.
1590b57cec5SDimitry Andric   if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
1600b57cec5SDimitry Andric     return false;
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   return true;
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
1660b57cec5SDimitry Andric                                          MachineBasicBlock::iterator MI,
1670b57cec5SDimitry Andric                                          unsigned DestReg,
1680b57cec5SDimitry Andric                                          const Remat &RM,
1690b57cec5SDimitry Andric                                          const TargetRegisterInfo &tri,
1700b57cec5SDimitry Andric                                          bool Late) {
1710b57cec5SDimitry Andric   assert(RM.OrigMI && "Invalid remat");
1720b57cec5SDimitry Andric   TII.reMaterialize(MBB, MI, DestReg, 0, *RM.OrigMI, tri);
1730b57cec5SDimitry Andric   // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg
1740b57cec5SDimitry Andric   // to false anyway in case the isDead flag of RM.OrigMI's dest register
1750b57cec5SDimitry Andric   // is true.
1760b57cec5SDimitry Andric   (*--MI).getOperand(0).setIsDead(false);
1770b57cec5SDimitry Andric   Rematted.insert(RM.ParentVNI);
1780b57cec5SDimitry Andric   return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
1790b57cec5SDimitry Andric }
1800b57cec5SDimitry Andric 
1815ffd83dbSDimitry Andric void LiveRangeEdit::eraseVirtReg(Register Reg) {
1820b57cec5SDimitry Andric   if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
1830b57cec5SDimitry Andric     LIS.removeInterval(Reg);
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
1870b57cec5SDimitry Andric                                SmallVectorImpl<MachineInstr*> &Dead) {
1880b57cec5SDimitry Andric   MachineInstr *DefMI = nullptr, *UseMI = nullptr;
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric   // Check that there is a single def and a single use.
191e8d8bef9SDimitry Andric   for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg())) {
1920b57cec5SDimitry Andric     MachineInstr *MI = MO.getParent();
1930b57cec5SDimitry Andric     if (MO.isDef()) {
1940b57cec5SDimitry Andric       if (DefMI && DefMI != MI)
1950b57cec5SDimitry Andric         return false;
1960b57cec5SDimitry Andric       if (!MI->canFoldAsLoad())
1970b57cec5SDimitry Andric         return false;
1980b57cec5SDimitry Andric       DefMI = MI;
1990b57cec5SDimitry Andric     } else if (!MO.isUndef()) {
2000b57cec5SDimitry Andric       if (UseMI && UseMI != MI)
2010b57cec5SDimitry Andric         return false;
2020b57cec5SDimitry Andric       // FIXME: Targets don't know how to fold subreg uses.
2030b57cec5SDimitry Andric       if (MO.getSubReg())
2040b57cec5SDimitry Andric         return false;
2050b57cec5SDimitry Andric       UseMI = MI;
2060b57cec5SDimitry Andric     }
2070b57cec5SDimitry Andric   }
2080b57cec5SDimitry Andric   if (!DefMI || !UseMI)
2090b57cec5SDimitry Andric     return false;
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric   // Since we're moving the DefMI load, make sure we're not extending any live
2120b57cec5SDimitry Andric   // ranges.
2130b57cec5SDimitry Andric   if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
2140b57cec5SDimitry Andric                           LIS.getInstructionIndex(*UseMI)))
2150b57cec5SDimitry Andric     return false;
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   // We also need to make sure it is safe to move the load.
2180b57cec5SDimitry Andric   // Assume there are stores between DefMI and UseMI.
2190b57cec5SDimitry Andric   bool SawStore = true;
2200b57cec5SDimitry Andric   if (!DefMI->isSafeToMove(nullptr, SawStore))
2210b57cec5SDimitry Andric     return false;
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Try to fold single def: " << *DefMI
2240b57cec5SDimitry Andric                     << "       into single use: " << *UseMI);
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   SmallVector<unsigned, 8> Ops;
227e8d8bef9SDimitry Andric   if (UseMI->readsWritesVirtualRegister(LI->reg(), &Ops).second)
2280b57cec5SDimitry Andric     return false;
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric   MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
2310b57cec5SDimitry Andric   if (!FoldMI)
2320b57cec5SDimitry Andric     return false;
2330b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "                folded: " << *FoldMI);
2340b57cec5SDimitry Andric   LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
2355ffd83dbSDimitry Andric   // Update the call site info.
2365ffd83dbSDimitry Andric   if (UseMI->shouldUpdateCallSiteInfo())
2378bcb0991SDimitry Andric     UseMI->getMF()->moveCallSiteInfo(UseMI, FoldMI);
2380b57cec5SDimitry Andric   UseMI->eraseFromParent();
239e8d8bef9SDimitry Andric   DefMI->addRegisterDead(LI->reg(), nullptr);
2400b57cec5SDimitry Andric   Dead.push_back(DefMI);
2410b57cec5SDimitry Andric   ++NumDCEFoldedLoads;
2420b57cec5SDimitry Andric   return true;
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
2460b57cec5SDimitry Andric                               const MachineOperand &MO) const {
2470b57cec5SDimitry Andric   const MachineInstr &MI = *MO.getParent();
2480b57cec5SDimitry Andric   SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
2490b57cec5SDimitry Andric   if (LI.Query(Idx).isKill())
2500b57cec5SDimitry Andric     return true;
2510b57cec5SDimitry Andric   const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
2520b57cec5SDimitry Andric   unsigned SubReg = MO.getSubReg();
2530b57cec5SDimitry Andric   LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
2540b57cec5SDimitry Andric   for (const LiveInterval::SubRange &S : LI.subranges()) {
2550b57cec5SDimitry Andric     if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
2560b57cec5SDimitry Andric       return true;
2570b57cec5SDimitry Andric   }
2580b57cec5SDimitry Andric   return false;
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric /// Find all live intervals that need to shrink, then remove the instruction.
2620b57cec5SDimitry Andric void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink,
2635ffd83dbSDimitry Andric                                      AAResults *AA) {
2640b57cec5SDimitry Andric   assert(MI->allDefsAreDead() && "Def isn't really dead");
2650b57cec5SDimitry Andric   SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric   // Never delete a bundled instruction.
2680b57cec5SDimitry Andric   if (MI->isBundled()) {
2690b57cec5SDimitry Andric     return;
2700b57cec5SDimitry Andric   }
2710b57cec5SDimitry Andric   // Never delete inline asm.
2720b57cec5SDimitry Andric   if (MI->isInlineAsm()) {
2730b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
2740b57cec5SDimitry Andric     return;
2750b57cec5SDimitry Andric   }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   // Use the same criteria as DeadMachineInstructionElim.
2780b57cec5SDimitry Andric   bool SawStore = false;
2790b57cec5SDimitry Andric   if (!MI->isSafeToMove(nullptr, SawStore)) {
2800b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
2810b57cec5SDimitry Andric     return;
2820b57cec5SDimitry Andric   }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric   // Collect virtual registers to be erased after MI is gone.
2870b57cec5SDimitry Andric   SmallVector<unsigned, 8> RegsToErase;
2880b57cec5SDimitry Andric   bool ReadsPhysRegs = false;
2890b57cec5SDimitry Andric   bool isOrigDef = false;
2900b57cec5SDimitry Andric   unsigned Dest;
2910b57cec5SDimitry Andric   // Only optimize rematerialize case when the instruction has one def, since
2920b57cec5SDimitry Andric   // otherwise we could leave some dead defs in the code.  This case is
2930b57cec5SDimitry Andric   // extremely rare.
2940b57cec5SDimitry Andric   if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
2950b57cec5SDimitry Andric       MI->getDesc().getNumDefs() == 1) {
2960b57cec5SDimitry Andric     Dest = MI->getOperand(0).getReg();
2970b57cec5SDimitry Andric     unsigned Original = VRM->getOriginal(Dest);
2980b57cec5SDimitry Andric     LiveInterval &OrigLI = LIS.getInterval(Original);
2990b57cec5SDimitry Andric     VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
3000b57cec5SDimitry Andric     // The original live-range may have been shrunk to
3010b57cec5SDimitry Andric     // an empty live-range. It happens when it is dead, but
3020b57cec5SDimitry Andric     // we still keep it around to be able to rematerialize
3030b57cec5SDimitry Andric     // other values that depend on it.
3040b57cec5SDimitry Andric     if (OrigVNI)
3050b57cec5SDimitry Andric       isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx);
3060b57cec5SDimitry Andric   }
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   // Check for live intervals that may shrink
3090b57cec5SDimitry Andric   for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
3100b57cec5SDimitry Andric          MOE = MI->operands_end(); MOI != MOE; ++MOI) {
3110b57cec5SDimitry Andric     if (!MOI->isReg())
3120b57cec5SDimitry Andric       continue;
3138bcb0991SDimitry Andric     Register Reg = MOI->getReg();
3148bcb0991SDimitry Andric     if (!Register::isVirtualRegister(Reg)) {
3150b57cec5SDimitry Andric       // Check if MI reads any unreserved physregs.
3160b57cec5SDimitry Andric       if (Reg && MOI->readsReg() && !MRI.isReserved(Reg))
3170b57cec5SDimitry Andric         ReadsPhysRegs = true;
3180b57cec5SDimitry Andric       else if (MOI->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.
3280b57cec5SDimitry Andric     if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MOI->isDef())) ||
3290b57cec5SDimitry Andric         (MOI->readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI))))
3300b57cec5SDimitry Andric       ToShrink.insert(&LI);
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric     // Remove defined value.
3330b57cec5SDimitry Andric     if (MOI->isDef()) {
3340b57cec5SDimitry Andric       if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
335e8d8bef9SDimitry Andric         TheDelegate->LRE_WillShrinkVirtReg(LI.reg());
3360b57cec5SDimitry Andric       LIS.removeVRegDefAt(LI, Idx);
3370b57cec5SDimitry Andric       if (LI.empty())
3380b57cec5SDimitry Andric         RegsToErase.push_back(Reg);
3390b57cec5SDimitry Andric     }
3400b57cec5SDimitry Andric   }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   // Currently, we don't support DCE of physreg live ranges. If MI reads
3430b57cec5SDimitry Andric   // any unreserved physregs, don't erase the instruction, but turn it into
3440b57cec5SDimitry Andric   // a KILL instead. This way, the physreg live ranges don't end up
3450b57cec5SDimitry Andric   // dangling.
3460b57cec5SDimitry Andric   // FIXME: It would be better to have something like shrinkToUses() for
3470b57cec5SDimitry Andric   // physregs. That could potentially enable more DCE and it would free up
3480b57cec5SDimitry Andric   // the physreg. It would not happen often, though.
3490b57cec5SDimitry Andric   if (ReadsPhysRegs) {
3500b57cec5SDimitry Andric     MI->setDesc(TII.get(TargetOpcode::KILL));
3510b57cec5SDimitry Andric     // Remove all operands that aren't physregs.
3520b57cec5SDimitry Andric     for (unsigned i = MI->getNumOperands(); i; --i) {
3530b57cec5SDimitry Andric       const MachineOperand &MO = MI->getOperand(i-1);
3548bcb0991SDimitry Andric       if (MO.isReg() && Register::isPhysicalRegister(MO.getReg()))
3550b57cec5SDimitry Andric         continue;
3560b57cec5SDimitry Andric       MI->RemoveOperand(i-1);
3570b57cec5SDimitry Andric     }
3580b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
3590b57cec5SDimitry Andric   } else {
3600b57cec5SDimitry Andric     // If the dest of MI is an original reg and MI is reMaterializable,
3610b57cec5SDimitry Andric     // don't delete the inst. Replace the dest with a new reg, and keep
3620b57cec5SDimitry Andric     // the inst for remat of other siblings. The inst is saved in
3630b57cec5SDimitry Andric     // LiveRangeEdit::DeadRemats and will be deleted after all the
3640b57cec5SDimitry Andric     // allocations of the func are done.
3650b57cec5SDimitry Andric     if (isOrigDef && DeadRemats && TII.isTriviallyReMaterializable(*MI, AA)) {
3660b57cec5SDimitry Andric       LiveInterval &NewLI = createEmptyIntervalFrom(Dest, false);
3670b57cec5SDimitry Andric       VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
3680b57cec5SDimitry Andric       NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI));
3690b57cec5SDimitry Andric       pop_back();
3700b57cec5SDimitry Andric       DeadRemats->insert(MI);
3710b57cec5SDimitry Andric       const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
372e8d8bef9SDimitry Andric       MI->substituteRegister(Dest, NewLI.reg(), 0, TRI);
3730b57cec5SDimitry Andric       MI->getOperand(0).setIsDead(true);
3740b57cec5SDimitry Andric     } else {
3750b57cec5SDimitry Andric       if (TheDelegate)
3760b57cec5SDimitry Andric         TheDelegate->LRE_WillEraseInstruction(MI);
3770b57cec5SDimitry Andric       LIS.RemoveMachineInstrFromMaps(*MI);
3780b57cec5SDimitry Andric       MI->eraseFromParent();
3790b57cec5SDimitry Andric       ++NumDCEDeleted;
3800b57cec5SDimitry Andric     }
3810b57cec5SDimitry Andric   }
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   // Erase any virtregs that are now empty and unused. There may be <undef>
3840b57cec5SDimitry Andric   // uses around. Keep the empty live range in that case.
3850b57cec5SDimitry Andric   for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
3865ffd83dbSDimitry Andric     Register Reg = RegsToErase[i];
3870b57cec5SDimitry Andric     if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
3880b57cec5SDimitry Andric       ToShrink.remove(&LIS.getInterval(Reg));
3890b57cec5SDimitry Andric       eraseVirtReg(Reg);
3900b57cec5SDimitry Andric     }
3910b57cec5SDimitry Andric   }
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
3955ffd83dbSDimitry Andric                                       ArrayRef<Register> RegsBeingSpilled,
3965ffd83dbSDimitry Andric                                       AAResults *AA) {
3970b57cec5SDimitry Andric   ToShrinkSet ToShrink;
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric   for (;;) {
4000b57cec5SDimitry Andric     // Erase all dead defs.
4010b57cec5SDimitry Andric     while (!Dead.empty())
4020b57cec5SDimitry Andric       eliminateDeadDef(Dead.pop_back_val(), ToShrink, AA);
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric     if (ToShrink.empty())
4050b57cec5SDimitry Andric       break;
4060b57cec5SDimitry Andric 
4070b57cec5SDimitry Andric     // Shrink just one live interval. Then delete new dead defs.
4080b57cec5SDimitry Andric     LiveInterval *LI = ToShrink.back();
4090b57cec5SDimitry Andric     ToShrink.pop_back();
4100b57cec5SDimitry Andric     if (foldAsLoad(LI, Dead))
4110b57cec5SDimitry Andric       continue;
412e8d8bef9SDimitry Andric     unsigned VReg = LI->reg();
4130b57cec5SDimitry Andric     if (TheDelegate)
4140b57cec5SDimitry Andric       TheDelegate->LRE_WillShrinkVirtReg(VReg);
4150b57cec5SDimitry Andric     if (!LIS.shrinkToUses(LI, &Dead))
4160b57cec5SDimitry Andric       continue;
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric     // Don't create new intervals for a register being spilled.
4190b57cec5SDimitry Andric     // The new intervals would have to be spilled anyway so its not worth it.
4200b57cec5SDimitry Andric     // Also they currently aren't spilled so creating them and not spilling
4210b57cec5SDimitry Andric     // them results in incorrect code.
4220b57cec5SDimitry Andric     bool BeingSpilled = false;
4230b57cec5SDimitry Andric     for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) {
4240b57cec5SDimitry Andric       if (VReg == RegsBeingSpilled[i]) {
4250b57cec5SDimitry Andric         BeingSpilled = true;
4260b57cec5SDimitry Andric         break;
4270b57cec5SDimitry Andric       }
4280b57cec5SDimitry Andric     }
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric     if (BeingSpilled) continue;
4310b57cec5SDimitry Andric 
4320b57cec5SDimitry Andric     // LI may have been separated, create new intervals.
4330b57cec5SDimitry Andric     LI->RenumberValues();
4340b57cec5SDimitry Andric     SmallVector<LiveInterval*, 8> SplitLIs;
4350b57cec5SDimitry Andric     LIS.splitSeparateComponents(*LI, SplitLIs);
4360b57cec5SDimitry Andric     if (!SplitLIs.empty())
4370b57cec5SDimitry Andric       ++NumFracRanges;
4380b57cec5SDimitry Andric 
439e8d8bef9SDimitry Andric     Register Original = VRM ? VRM->getOriginal(VReg) : Register();
4400b57cec5SDimitry Andric     for (const LiveInterval *SplitLI : SplitLIs) {
4410b57cec5SDimitry Andric       // If LI is an original interval that hasn't been split yet, make the new
4420b57cec5SDimitry Andric       // intervals their own originals instead of referring to LI. The original
4430b57cec5SDimitry Andric       // interval must contain all the split products, and LI doesn't.
4440b57cec5SDimitry Andric       if (Original != VReg && Original != 0)
445e8d8bef9SDimitry Andric         VRM->setIsSplitFromReg(SplitLI->reg(), Original);
4460b57cec5SDimitry Andric       if (TheDelegate)
447e8d8bef9SDimitry Andric         TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg(), VReg);
4480b57cec5SDimitry Andric     }
4490b57cec5SDimitry Andric   }
4500b57cec5SDimitry Andric }
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric // Keep track of new virtual registers created via
4530b57cec5SDimitry Andric // MachineRegisterInfo::createVirtualRegister.
4540b57cec5SDimitry Andric void
4555ffd83dbSDimitry Andric LiveRangeEdit::MRI_NoteNewVirtualRegister(Register VReg) {
4560b57cec5SDimitry Andric   if (VRM)
4570b57cec5SDimitry Andric     VRM->grow();
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   NewRegs.push_back(VReg);
4600b57cec5SDimitry Andric }
4610b57cec5SDimitry Andric 
462*fe6060f1SDimitry Andric void LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
463*fe6060f1SDimitry Andric                                              VirtRegAuxInfo &VRAI) {
4640b57cec5SDimitry Andric   for (unsigned I = 0, Size = size(); I < Size; ++I) {
4650b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(get(I));
466e8d8bef9SDimitry Andric     if (MRI.recomputeRegClass(LI.reg()))
4670b57cec5SDimitry Andric       LLVM_DEBUG({
4680b57cec5SDimitry Andric         const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
469e8d8bef9SDimitry Andric         dbgs() << "Inflated " << printReg(LI.reg()) << " to "
470e8d8bef9SDimitry Andric                << TRI->getRegClassName(MRI.getRegClass(LI.reg())) << '\n';
4710b57cec5SDimitry Andric       });
4720b57cec5SDimitry Andric     VRAI.calculateSpillWeightAndHint(LI);
4730b57cec5SDimitry Andric   }
4740b57cec5SDimitry Andric }
475