xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/LiveRangeEdit.cpp (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
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));
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.
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;
136*0eae32dcSDimitry Andric 
137*0eae32dcSDimitry Andric     // Check that subrange is live at UseIdx.
138*0eae32dcSDimitry Andric     if (MO.getSubReg()) {
139*0eae32dcSDimitry Andric       const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
140*0eae32dcSDimitry Andric       LaneBitmask LM = TRI->getSubRegIndexLaneMask(MO.getSubReg());
141*0eae32dcSDimitry Andric       for (LiveInterval::SubRange &SR : li.subranges()) {
142*0eae32dcSDimitry Andric         if ((SR.LaneMask & LM).none())
143*0eae32dcSDimitry Andric           continue;
144*0eae32dcSDimitry Andric         if (!SR.liveAt(UseIdx))
145*0eae32dcSDimitry Andric           return false;
146*0eae32dcSDimitry Andric         // Early exit if all used lanes are checked. No need to continue.
147*0eae32dcSDimitry Andric         LM &= ~SR.LaneMask;
148*0eae32dcSDimitry Andric         if (LM.none())
149*0eae32dcSDimitry Andric           break;
150*0eae32dcSDimitry Andric       }
151*0eae32dcSDimitry Andric     }
1520b57cec5SDimitry Andric   }
1530b57cec5SDimitry Andric   return true;
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
1570b57cec5SDimitry Andric                                        SlotIndex UseIdx, bool cheapAsAMove) {
1580b57cec5SDimitry Andric   assert(ScannedRemattable && "Call anyRematerializable first");
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   // Use scanRemattable info.
1610b57cec5SDimitry Andric   if (!Remattable.count(OrigVNI))
1620b57cec5SDimitry Andric     return false;
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   // No defining instruction provided.
1650b57cec5SDimitry Andric   SlotIndex DefIdx;
1660b57cec5SDimitry Andric   assert(RM.OrigMI && "No defining instruction for remattable value");
1670b57cec5SDimitry Andric   DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   // If only cheap remats were requested, bail out early.
1700b57cec5SDimitry Andric   if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI))
1710b57cec5SDimitry Andric     return false;
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   // Verify that all used registers are available with the same values.
1740b57cec5SDimitry Andric   if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
1750b57cec5SDimitry Andric     return false;
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   return true;
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
1810b57cec5SDimitry Andric                                          MachineBasicBlock::iterator MI,
1820b57cec5SDimitry Andric                                          unsigned DestReg,
1830b57cec5SDimitry Andric                                          const Remat &RM,
1840b57cec5SDimitry Andric                                          const TargetRegisterInfo &tri,
1850b57cec5SDimitry Andric                                          bool Late) {
1860b57cec5SDimitry Andric   assert(RM.OrigMI && "Invalid remat");
1870b57cec5SDimitry Andric   TII.reMaterialize(MBB, MI, DestReg, 0, *RM.OrigMI, tri);
1880b57cec5SDimitry Andric   // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg
1890b57cec5SDimitry Andric   // to false anyway in case the isDead flag of RM.OrigMI's dest register
1900b57cec5SDimitry Andric   // is true.
1910b57cec5SDimitry Andric   (*--MI).getOperand(0).setIsDead(false);
1920b57cec5SDimitry Andric   Rematted.insert(RM.ParentVNI);
1930b57cec5SDimitry Andric   return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric 
1965ffd83dbSDimitry Andric void LiveRangeEdit::eraseVirtReg(Register Reg) {
1970b57cec5SDimitry Andric   if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
1980b57cec5SDimitry Andric     LIS.removeInterval(Reg);
1990b57cec5SDimitry Andric }
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
2020b57cec5SDimitry Andric                                SmallVectorImpl<MachineInstr*> &Dead) {
2030b57cec5SDimitry Andric   MachineInstr *DefMI = nullptr, *UseMI = nullptr;
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   // Check that there is a single def and a single use.
206e8d8bef9SDimitry Andric   for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg())) {
2070b57cec5SDimitry Andric     MachineInstr *MI = MO.getParent();
2080b57cec5SDimitry Andric     if (MO.isDef()) {
2090b57cec5SDimitry Andric       if (DefMI && DefMI != MI)
2100b57cec5SDimitry Andric         return false;
2110b57cec5SDimitry Andric       if (!MI->canFoldAsLoad())
2120b57cec5SDimitry Andric         return false;
2130b57cec5SDimitry Andric       DefMI = MI;
2140b57cec5SDimitry Andric     } else if (!MO.isUndef()) {
2150b57cec5SDimitry Andric       if (UseMI && UseMI != MI)
2160b57cec5SDimitry Andric         return false;
2170b57cec5SDimitry Andric       // FIXME: Targets don't know how to fold subreg uses.
2180b57cec5SDimitry Andric       if (MO.getSubReg())
2190b57cec5SDimitry Andric         return false;
2200b57cec5SDimitry Andric       UseMI = MI;
2210b57cec5SDimitry Andric     }
2220b57cec5SDimitry Andric   }
2230b57cec5SDimitry Andric   if (!DefMI || !UseMI)
2240b57cec5SDimitry Andric     return false;
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   // Since we're moving the DefMI load, make sure we're not extending any live
2270b57cec5SDimitry Andric   // ranges.
2280b57cec5SDimitry Andric   if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
2290b57cec5SDimitry Andric                           LIS.getInstructionIndex(*UseMI)))
2300b57cec5SDimitry Andric     return false;
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric   // We also need to make sure it is safe to move the load.
2330b57cec5SDimitry Andric   // Assume there are stores between DefMI and UseMI.
2340b57cec5SDimitry Andric   bool SawStore = true;
2350b57cec5SDimitry Andric   if (!DefMI->isSafeToMove(nullptr, SawStore))
2360b57cec5SDimitry Andric     return false;
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Try to fold single def: " << *DefMI
2390b57cec5SDimitry Andric                     << "       into single use: " << *UseMI);
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric   SmallVector<unsigned, 8> Ops;
242e8d8bef9SDimitry Andric   if (UseMI->readsWritesVirtualRegister(LI->reg(), &Ops).second)
2430b57cec5SDimitry Andric     return false;
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
2460b57cec5SDimitry Andric   if (!FoldMI)
2470b57cec5SDimitry Andric     return false;
2480b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "                folded: " << *FoldMI);
2490b57cec5SDimitry Andric   LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
2505ffd83dbSDimitry Andric   // Update the call site info.
2515ffd83dbSDimitry Andric   if (UseMI->shouldUpdateCallSiteInfo())
2528bcb0991SDimitry Andric     UseMI->getMF()->moveCallSiteInfo(UseMI, FoldMI);
2530b57cec5SDimitry Andric   UseMI->eraseFromParent();
254e8d8bef9SDimitry Andric   DefMI->addRegisterDead(LI->reg(), nullptr);
2550b57cec5SDimitry Andric   Dead.push_back(DefMI);
2560b57cec5SDimitry Andric   ++NumDCEFoldedLoads;
2570b57cec5SDimitry Andric   return true;
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
2610b57cec5SDimitry Andric                               const MachineOperand &MO) const {
2620b57cec5SDimitry Andric   const MachineInstr &MI = *MO.getParent();
2630b57cec5SDimitry Andric   SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
2640b57cec5SDimitry Andric   if (LI.Query(Idx).isKill())
2650b57cec5SDimitry Andric     return true;
2660b57cec5SDimitry Andric   const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
2670b57cec5SDimitry Andric   unsigned SubReg = MO.getSubReg();
2680b57cec5SDimitry Andric   LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
2690b57cec5SDimitry Andric   for (const LiveInterval::SubRange &S : LI.subranges()) {
2700b57cec5SDimitry Andric     if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
2710b57cec5SDimitry Andric       return true;
2720b57cec5SDimitry Andric   }
2730b57cec5SDimitry Andric   return false;
2740b57cec5SDimitry Andric }
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric /// Find all live intervals that need to shrink, then remove the instruction.
2770b57cec5SDimitry Andric void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink,
2785ffd83dbSDimitry Andric                                      AAResults *AA) {
2790b57cec5SDimitry Andric   assert(MI->allDefsAreDead() && "Def isn't really dead");
2800b57cec5SDimitry Andric   SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   // Never delete a bundled instruction.
2830b57cec5SDimitry Andric   if (MI->isBundled()) {
2840b57cec5SDimitry Andric     return;
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric   // Never delete inline asm.
2870b57cec5SDimitry Andric   if (MI->isInlineAsm()) {
2880b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
2890b57cec5SDimitry Andric     return;
2900b57cec5SDimitry Andric   }
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric   // Use the same criteria as DeadMachineInstructionElim.
2930b57cec5SDimitry Andric   bool SawStore = false;
2940b57cec5SDimitry Andric   if (!MI->isSafeToMove(nullptr, SawStore)) {
2950b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
2960b57cec5SDimitry Andric     return;
2970b57cec5SDimitry Andric   }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric   // Collect virtual registers to be erased after MI is gone.
3020b57cec5SDimitry Andric   SmallVector<unsigned, 8> RegsToErase;
3030b57cec5SDimitry Andric   bool ReadsPhysRegs = false;
3040b57cec5SDimitry Andric   bool isOrigDef = false;
3050b57cec5SDimitry Andric   unsigned Dest;
3060b57cec5SDimitry Andric   // Only optimize rematerialize case when the instruction has one def, since
3070b57cec5SDimitry Andric   // otherwise we could leave some dead defs in the code.  This case is
3080b57cec5SDimitry Andric   // extremely rare.
3090b57cec5SDimitry Andric   if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
3100b57cec5SDimitry Andric       MI->getDesc().getNumDefs() == 1) {
3110b57cec5SDimitry Andric     Dest = MI->getOperand(0).getReg();
3120b57cec5SDimitry Andric     unsigned Original = VRM->getOriginal(Dest);
3130b57cec5SDimitry Andric     LiveInterval &OrigLI = LIS.getInterval(Original);
3140b57cec5SDimitry Andric     VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
3150b57cec5SDimitry Andric     // The original live-range may have been shrunk to
3160b57cec5SDimitry Andric     // an empty live-range. It happens when it is dead, but
3170b57cec5SDimitry Andric     // we still keep it around to be able to rematerialize
3180b57cec5SDimitry Andric     // other values that depend on it.
3190b57cec5SDimitry Andric     if (OrigVNI)
3200b57cec5SDimitry Andric       isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx);
3210b57cec5SDimitry Andric   }
3220b57cec5SDimitry Andric 
323349cc55cSDimitry Andric   bool HasLiveVRegUses = false;
324349cc55cSDimitry Andric 
3250b57cec5SDimitry Andric   // Check for live intervals that may shrink
326349cc55cSDimitry Andric   for (const MachineOperand &MO : MI->operands()) {
327349cc55cSDimitry Andric     if (!MO.isReg())
3280b57cec5SDimitry Andric       continue;
329349cc55cSDimitry Andric     Register Reg = MO.getReg();
3308bcb0991SDimitry Andric     if (!Register::isVirtualRegister(Reg)) {
3310b57cec5SDimitry Andric       // Check if MI reads any unreserved physregs.
332349cc55cSDimitry Andric       if (Reg && MO.readsReg() && !MRI.isReserved(Reg))
3330b57cec5SDimitry Andric         ReadsPhysRegs = true;
334349cc55cSDimitry Andric       else if (MO.isDef())
335e8d8bef9SDimitry Andric         LIS.removePhysRegDefAt(Reg.asMCReg(), Idx);
3360b57cec5SDimitry Andric       continue;
3370b57cec5SDimitry Andric     }
3380b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(Reg);
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric     // Shrink read registers, unless it is likely to be expensive and
3410b57cec5SDimitry Andric     // unlikely to change anything. We typically don't want to shrink the
3420b57cec5SDimitry Andric     // PIC base register that has lots of uses everywhere.
3430b57cec5SDimitry Andric     // Always shrink COPY uses that probably come from live range splitting.
344349cc55cSDimitry Andric     if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MO.isDef())) ||
345349cc55cSDimitry Andric         (MO.readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, MO))))
3460b57cec5SDimitry Andric       ToShrink.insert(&LI);
347349cc55cSDimitry Andric     else if (MO.readsReg())
348349cc55cSDimitry Andric       HasLiveVRegUses = true;
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric     // Remove defined value.
351349cc55cSDimitry Andric     if (MO.isDef()) {
3520b57cec5SDimitry Andric       if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
353e8d8bef9SDimitry Andric         TheDelegate->LRE_WillShrinkVirtReg(LI.reg());
3540b57cec5SDimitry Andric       LIS.removeVRegDefAt(LI, Idx);
3550b57cec5SDimitry Andric       if (LI.empty())
3560b57cec5SDimitry Andric         RegsToErase.push_back(Reg);
3570b57cec5SDimitry Andric     }
3580b57cec5SDimitry Andric   }
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric   // Currently, we don't support DCE of physreg live ranges. If MI reads
3610b57cec5SDimitry Andric   // any unreserved physregs, don't erase the instruction, but turn it into
3620b57cec5SDimitry Andric   // a KILL instead. This way, the physreg live ranges don't end up
3630b57cec5SDimitry Andric   // dangling.
3640b57cec5SDimitry Andric   // FIXME: It would be better to have something like shrinkToUses() for
3650b57cec5SDimitry Andric   // physregs. That could potentially enable more DCE and it would free up
3660b57cec5SDimitry Andric   // the physreg. It would not happen often, though.
3670b57cec5SDimitry Andric   if (ReadsPhysRegs) {
3680b57cec5SDimitry Andric     MI->setDesc(TII.get(TargetOpcode::KILL));
3690b57cec5SDimitry Andric     // Remove all operands that aren't physregs.
3700b57cec5SDimitry Andric     for (unsigned i = MI->getNumOperands(); i; --i) {
3710b57cec5SDimitry Andric       const MachineOperand &MO = MI->getOperand(i-1);
3728bcb0991SDimitry Andric       if (MO.isReg() && Register::isPhysicalRegister(MO.getReg()))
3730b57cec5SDimitry Andric         continue;
3740b57cec5SDimitry Andric       MI->RemoveOperand(i-1);
3750b57cec5SDimitry Andric     }
3760b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
3770b57cec5SDimitry Andric   } else {
3780b57cec5SDimitry Andric     // If the dest of MI is an original reg and MI is reMaterializable,
3790b57cec5SDimitry Andric     // don't delete the inst. Replace the dest with a new reg, and keep
3800b57cec5SDimitry Andric     // the inst for remat of other siblings. The inst is saved in
3810b57cec5SDimitry Andric     // LiveRangeEdit::DeadRemats and will be deleted after all the
3820b57cec5SDimitry Andric     // allocations of the func are done.
383349cc55cSDimitry Andric     // However, immediately delete instructions which have unshrunk virtual
384349cc55cSDimitry Andric     // register uses. That may provoke RA to split an interval at the KILL
385349cc55cSDimitry Andric     // and later result in an invalid live segment end.
386349cc55cSDimitry Andric     if (isOrigDef && DeadRemats && !HasLiveVRegUses &&
387349cc55cSDimitry Andric         TII.isTriviallyReMaterializable(*MI, AA)) {
3880b57cec5SDimitry Andric       LiveInterval &NewLI = createEmptyIntervalFrom(Dest, false);
3890b57cec5SDimitry Andric       VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
3900b57cec5SDimitry Andric       NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI));
3910b57cec5SDimitry Andric       pop_back();
3920b57cec5SDimitry Andric       DeadRemats->insert(MI);
3930b57cec5SDimitry Andric       const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
394e8d8bef9SDimitry Andric       MI->substituteRegister(Dest, NewLI.reg(), 0, TRI);
3950b57cec5SDimitry Andric       MI->getOperand(0).setIsDead(true);
3960b57cec5SDimitry Andric     } else {
3970b57cec5SDimitry Andric       if (TheDelegate)
3980b57cec5SDimitry Andric         TheDelegate->LRE_WillEraseInstruction(MI);
3990b57cec5SDimitry Andric       LIS.RemoveMachineInstrFromMaps(*MI);
4000b57cec5SDimitry Andric       MI->eraseFromParent();
4010b57cec5SDimitry Andric       ++NumDCEDeleted;
4020b57cec5SDimitry Andric     }
4030b57cec5SDimitry Andric   }
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric   // Erase any virtregs that are now empty and unused. There may be <undef>
4060b57cec5SDimitry Andric   // uses around. Keep the empty live range in that case.
4070b57cec5SDimitry Andric   for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
4085ffd83dbSDimitry Andric     Register Reg = RegsToErase[i];
4090b57cec5SDimitry Andric     if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
4100b57cec5SDimitry Andric       ToShrink.remove(&LIS.getInterval(Reg));
4110b57cec5SDimitry Andric       eraseVirtReg(Reg);
4120b57cec5SDimitry Andric     }
4130b57cec5SDimitry Andric   }
4140b57cec5SDimitry Andric }
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
4175ffd83dbSDimitry Andric                                       ArrayRef<Register> RegsBeingSpilled,
4185ffd83dbSDimitry Andric                                       AAResults *AA) {
4190b57cec5SDimitry Andric   ToShrinkSet ToShrink;
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric   for (;;) {
4220b57cec5SDimitry Andric     // Erase all dead defs.
4230b57cec5SDimitry Andric     while (!Dead.empty())
4240b57cec5SDimitry Andric       eliminateDeadDef(Dead.pop_back_val(), ToShrink, AA);
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric     if (ToShrink.empty())
4270b57cec5SDimitry Andric       break;
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric     // Shrink just one live interval. Then delete new dead defs.
430349cc55cSDimitry Andric     LiveInterval *LI = ToShrink.pop_back_val();
4310b57cec5SDimitry Andric     if (foldAsLoad(LI, Dead))
4320b57cec5SDimitry Andric       continue;
433e8d8bef9SDimitry Andric     unsigned VReg = LI->reg();
4340b57cec5SDimitry Andric     if (TheDelegate)
4350b57cec5SDimitry Andric       TheDelegate->LRE_WillShrinkVirtReg(VReg);
4360b57cec5SDimitry Andric     if (!LIS.shrinkToUses(LI, &Dead))
4370b57cec5SDimitry Andric       continue;
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric     // Don't create new intervals for a register being spilled.
4400b57cec5SDimitry Andric     // The new intervals would have to be spilled anyway so its not worth it.
4410b57cec5SDimitry Andric     // Also they currently aren't spilled so creating them and not spilling
4420b57cec5SDimitry Andric     // them results in incorrect code.
4434824e7fdSDimitry Andric     if (llvm::is_contained(RegsBeingSpilled, VReg))
4444824e7fdSDimitry Andric       continue;
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric     // LI may have been separated, create new intervals.
4470b57cec5SDimitry Andric     LI->RenumberValues();
4480b57cec5SDimitry Andric     SmallVector<LiveInterval*, 8> SplitLIs;
4490b57cec5SDimitry Andric     LIS.splitSeparateComponents(*LI, SplitLIs);
4500b57cec5SDimitry Andric     if (!SplitLIs.empty())
4510b57cec5SDimitry Andric       ++NumFracRanges;
4520b57cec5SDimitry Andric 
453e8d8bef9SDimitry Andric     Register Original = VRM ? VRM->getOriginal(VReg) : Register();
4540b57cec5SDimitry Andric     for (const LiveInterval *SplitLI : SplitLIs) {
4550b57cec5SDimitry Andric       // If LI is an original interval that hasn't been split yet, make the new
4560b57cec5SDimitry Andric       // intervals their own originals instead of referring to LI. The original
4570b57cec5SDimitry Andric       // interval must contain all the split products, and LI doesn't.
4580b57cec5SDimitry Andric       if (Original != VReg && Original != 0)
459e8d8bef9SDimitry Andric         VRM->setIsSplitFromReg(SplitLI->reg(), Original);
4600b57cec5SDimitry Andric       if (TheDelegate)
461e8d8bef9SDimitry Andric         TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg(), VReg);
4620b57cec5SDimitry Andric     }
4630b57cec5SDimitry Andric   }
4640b57cec5SDimitry Andric }
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric // Keep track of new virtual registers created via
4670b57cec5SDimitry Andric // MachineRegisterInfo::createVirtualRegister.
4680b57cec5SDimitry Andric void
4695ffd83dbSDimitry Andric LiveRangeEdit::MRI_NoteNewVirtualRegister(Register VReg) {
4700b57cec5SDimitry Andric   if (VRM)
4710b57cec5SDimitry Andric     VRM->grow();
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric   NewRegs.push_back(VReg);
4740b57cec5SDimitry Andric }
4750b57cec5SDimitry Andric 
476fe6060f1SDimitry Andric void LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
477fe6060f1SDimitry Andric                                              VirtRegAuxInfo &VRAI) {
4780b57cec5SDimitry Andric   for (unsigned I = 0, Size = size(); I < Size; ++I) {
4790b57cec5SDimitry Andric     LiveInterval &LI = LIS.getInterval(get(I));
480e8d8bef9SDimitry Andric     if (MRI.recomputeRegClass(LI.reg()))
4810b57cec5SDimitry Andric       LLVM_DEBUG({
4820b57cec5SDimitry Andric         const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
483e8d8bef9SDimitry Andric         dbgs() << "Inflated " << printReg(LI.reg()) << " to "
484e8d8bef9SDimitry Andric                << TRI->getRegClassName(MRI.getRegClass(LI.reg())) << '\n';
4850b57cec5SDimitry Andric       });
4860b57cec5SDimitry Andric     VRAI.calculateSpillWeightAndHint(LI);
4870b57cec5SDimitry Andric   }
4880b57cec5SDimitry Andric }
489