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"); 30bdd1243dSDimitry 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) { 36bdd1243dSDimitry 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) { 56bdd1243dSDimitry 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; 8506c3fb27SDimitry Andric Register 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. 117bdd1243dSDimitry 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. 138bdd1243dSDimitry Andric if (li.hasSubRanges()) { 1390eae32dcSDimitry Andric const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo(); 140bdd1243dSDimitry Andric unsigned SubReg = MO.getSubReg(); 141bdd1243dSDimitry Andric LaneBitmask LM = SubReg ? TRI->getSubRegIndexLaneMask(SubReg) 142bdd1243dSDimitry 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, 18406c3fb27SDimitry Andric Register DestReg, const Remat &RM, 1850b57cec5SDimitry Andric const TargetRegisterInfo &tri, 18606c3fb27SDimitry Andric bool Late, unsigned SubIdx, 187bdd1243dSDimitry Andric MachineInstr *ReplaceIndexMI) { 1880b57cec5SDimitry Andric assert(RM.OrigMI && "Invalid remat"); 189bdd1243dSDimitry Andric TII.reMaterialize(MBB, MI, DestReg, SubIdx, *RM.OrigMI, tri); 1900b57cec5SDimitry Andric // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg 1910b57cec5SDimitry Andric // to false anyway in case the isDead flag of RM.OrigMI's dest register 1920b57cec5SDimitry Andric // is true. 1935f757f3fSDimitry Andric (*--MI).clearRegisterDeads(DestReg); 1940b57cec5SDimitry Andric Rematted.insert(RM.ParentVNI); 195bdd1243dSDimitry Andric ++NumReMaterialization; 196bdd1243dSDimitry Andric 197bdd1243dSDimitry Andric if (ReplaceIndexMI) 198bdd1243dSDimitry Andric return LIS.ReplaceMachineInstrInMaps(*ReplaceIndexMI, *MI).getRegSlot(); 1990b57cec5SDimitry Andric return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot(); 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric 2025ffd83dbSDimitry Andric void LiveRangeEdit::eraseVirtReg(Register Reg) { 2030b57cec5SDimitry Andric if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg)) 2040b57cec5SDimitry Andric LIS.removeInterval(Reg); 2050b57cec5SDimitry Andric } 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric bool LiveRangeEdit::foldAsLoad(LiveInterval *LI, 2080b57cec5SDimitry Andric SmallVectorImpl<MachineInstr*> &Dead) { 2090b57cec5SDimitry Andric MachineInstr *DefMI = nullptr, *UseMI = nullptr; 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric // Check that there is a single def and a single use. 212e8d8bef9SDimitry Andric for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg())) { 2130b57cec5SDimitry Andric MachineInstr *MI = MO.getParent(); 2140b57cec5SDimitry Andric if (MO.isDef()) { 2150b57cec5SDimitry Andric if (DefMI && DefMI != MI) 2160b57cec5SDimitry Andric return false; 2170b57cec5SDimitry Andric if (!MI->canFoldAsLoad()) 2180b57cec5SDimitry Andric return false; 2190b57cec5SDimitry Andric DefMI = MI; 2200b57cec5SDimitry Andric } else if (!MO.isUndef()) { 2210b57cec5SDimitry Andric if (UseMI && UseMI != MI) 2220b57cec5SDimitry Andric return false; 2230b57cec5SDimitry Andric // FIXME: Targets don't know how to fold subreg uses. 2240b57cec5SDimitry Andric if (MO.getSubReg()) 2250b57cec5SDimitry Andric return false; 2260b57cec5SDimitry Andric UseMI = MI; 2270b57cec5SDimitry Andric } 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric if (!DefMI || !UseMI) 2300b57cec5SDimitry Andric return false; 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric // Since we're moving the DefMI load, make sure we're not extending any live 2330b57cec5SDimitry Andric // ranges. 2340b57cec5SDimitry Andric if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI), 2350b57cec5SDimitry Andric LIS.getInstructionIndex(*UseMI))) 2360b57cec5SDimitry Andric return false; 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric // We also need to make sure it is safe to move the load. 2390b57cec5SDimitry Andric // Assume there are stores between DefMI and UseMI. 2400b57cec5SDimitry Andric bool SawStore = true; 2410b57cec5SDimitry Andric if (!DefMI->isSafeToMove(nullptr, SawStore)) 2420b57cec5SDimitry Andric return false; 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Try to fold single def: " << *DefMI 2450b57cec5SDimitry Andric << " into single use: " << *UseMI); 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric SmallVector<unsigned, 8> Ops; 248e8d8bef9SDimitry Andric if (UseMI->readsWritesVirtualRegister(LI->reg(), &Ops).second) 2490b57cec5SDimitry Andric return false; 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS); 2520b57cec5SDimitry Andric if (!FoldMI) 2530b57cec5SDimitry Andric return false; 2540b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " folded: " << *FoldMI); 2550b57cec5SDimitry Andric LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI); 2565ffd83dbSDimitry Andric // Update the call site info. 2575ffd83dbSDimitry Andric if (UseMI->shouldUpdateCallSiteInfo()) 2588bcb0991SDimitry Andric UseMI->getMF()->moveCallSiteInfo(UseMI, FoldMI); 2590b57cec5SDimitry Andric UseMI->eraseFromParent(); 260e8d8bef9SDimitry Andric DefMI->addRegisterDead(LI->reg(), nullptr); 2610b57cec5SDimitry Andric Dead.push_back(DefMI); 2620b57cec5SDimitry Andric ++NumDCEFoldedLoads; 2630b57cec5SDimitry Andric return true; 2640b57cec5SDimitry Andric } 2650b57cec5SDimitry Andric 2660b57cec5SDimitry Andric bool LiveRangeEdit::useIsKill(const LiveInterval &LI, 2670b57cec5SDimitry Andric const MachineOperand &MO) const { 2680b57cec5SDimitry Andric const MachineInstr &MI = *MO.getParent(); 2690b57cec5SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot(); 2700b57cec5SDimitry Andric if (LI.Query(Idx).isKill()) 2710b57cec5SDimitry Andric return true; 2720b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); 2730b57cec5SDimitry Andric unsigned SubReg = MO.getSubReg(); 2740b57cec5SDimitry Andric LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg); 2750b57cec5SDimitry Andric for (const LiveInterval::SubRange &S : LI.subranges()) { 2760b57cec5SDimitry Andric if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill()) 2770b57cec5SDimitry Andric return true; 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric return false; 2800b57cec5SDimitry Andric } 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric /// Find all live intervals that need to shrink, then remove the instruction. 283fcaf7f86SDimitry Andric void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink) { 2840b57cec5SDimitry Andric assert(MI->allDefsAreDead() && "Def isn't really dead"); 2850b57cec5SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot(); 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric // Never delete a bundled instruction. 2880b57cec5SDimitry Andric if (MI->isBundled()) { 28906c3fb27SDimitry Andric // TODO: Handle deleting copy bundles 29006c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "Won't delete dead bundled inst: " << Idx << '\t' 29106c3fb27SDimitry Andric << *MI); 2920b57cec5SDimitry Andric return; 2930b57cec5SDimitry Andric } 29406c3fb27SDimitry Andric 2950b57cec5SDimitry Andric // Never delete inline asm. 2960b57cec5SDimitry Andric if (MI->isInlineAsm()) { 2970b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI); 2980b57cec5SDimitry Andric return; 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric // Use the same criteria as DeadMachineInstructionElim. 3020b57cec5SDimitry Andric bool SawStore = false; 3030b57cec5SDimitry Andric if (!MI->isSafeToMove(nullptr, SawStore)) { 3040b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI); 3050b57cec5SDimitry Andric return; 3060b57cec5SDimitry Andric } 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI); 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric // Collect virtual registers to be erased after MI is gone. 31106c3fb27SDimitry Andric SmallVector<Register, 8> RegsToErase; 3120b57cec5SDimitry Andric bool ReadsPhysRegs = false; 3130b57cec5SDimitry Andric bool isOrigDef = false; 314972a253aSDimitry Andric Register Dest; 315972a253aSDimitry Andric unsigned DestSubReg; 3160b57cec5SDimitry Andric // Only optimize rematerialize case when the instruction has one def, since 3170b57cec5SDimitry Andric // otherwise we could leave some dead defs in the code. This case is 3180b57cec5SDimitry Andric // extremely rare. 3190b57cec5SDimitry Andric if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() && 3200b57cec5SDimitry Andric MI->getDesc().getNumDefs() == 1) { 3210b57cec5SDimitry Andric Dest = MI->getOperand(0).getReg(); 322972a253aSDimitry Andric DestSubReg = MI->getOperand(0).getSubReg(); 323bdd1243dSDimitry Andric Register Original = VRM->getOriginal(Dest); 3240b57cec5SDimitry Andric LiveInterval &OrigLI = LIS.getInterval(Original); 3250b57cec5SDimitry Andric VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx); 3260b57cec5SDimitry Andric // The original live-range may have been shrunk to 3270b57cec5SDimitry Andric // an empty live-range. It happens when it is dead, but 3280b57cec5SDimitry Andric // we still keep it around to be able to rematerialize 3290b57cec5SDimitry Andric // other values that depend on it. 3300b57cec5SDimitry Andric if (OrigVNI) 3310b57cec5SDimitry Andric isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx); 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric 334349cc55cSDimitry Andric bool HasLiveVRegUses = false; 335349cc55cSDimitry Andric 3360b57cec5SDimitry Andric // Check for live intervals that may shrink 337349cc55cSDimitry Andric for (const MachineOperand &MO : MI->operands()) { 338349cc55cSDimitry Andric if (!MO.isReg()) 3390b57cec5SDimitry Andric continue; 340349cc55cSDimitry Andric Register Reg = MO.getReg(); 341bdd1243dSDimitry Andric if (!Reg.isVirtual()) { 3420b57cec5SDimitry Andric // Check if MI reads any unreserved physregs. 343349cc55cSDimitry Andric if (Reg && MO.readsReg() && !MRI.isReserved(Reg)) 3440b57cec5SDimitry Andric ReadsPhysRegs = true; 345349cc55cSDimitry Andric else if (MO.isDef()) 346e8d8bef9SDimitry Andric LIS.removePhysRegDefAt(Reg.asMCReg(), Idx); 3470b57cec5SDimitry Andric continue; 3480b57cec5SDimitry Andric } 3490b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg); 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric // Shrink read registers, unless it is likely to be expensive and 3520b57cec5SDimitry Andric // unlikely to change anything. We typically don't want to shrink the 3530b57cec5SDimitry Andric // PIC base register that has lots of uses everywhere. 3540b57cec5SDimitry Andric // Always shrink COPY uses that probably come from live range splitting. 3555f757f3fSDimitry Andric if ((MI->readsVirtualRegister(Reg) && 3565f757f3fSDimitry Andric (MO.isDef() || TII.isCopyInstr(*MI))) || 357349cc55cSDimitry Andric (MO.readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, MO)))) 3580b57cec5SDimitry Andric ToShrink.insert(&LI); 359349cc55cSDimitry Andric else if (MO.readsReg()) 360349cc55cSDimitry Andric HasLiveVRegUses = true; 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric // Remove defined value. 363349cc55cSDimitry Andric if (MO.isDef()) { 3640b57cec5SDimitry Andric if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr) 365e8d8bef9SDimitry Andric TheDelegate->LRE_WillShrinkVirtReg(LI.reg()); 3660b57cec5SDimitry Andric LIS.removeVRegDefAt(LI, Idx); 3670b57cec5SDimitry Andric if (LI.empty()) 3680b57cec5SDimitry Andric RegsToErase.push_back(Reg); 3690b57cec5SDimitry Andric } 3700b57cec5SDimitry Andric } 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric // Currently, we don't support DCE of physreg live ranges. If MI reads 3730b57cec5SDimitry Andric // any unreserved physregs, don't erase the instruction, but turn it into 3740b57cec5SDimitry Andric // a KILL instead. This way, the physreg live ranges don't end up 3750b57cec5SDimitry Andric // dangling. 3760b57cec5SDimitry Andric // FIXME: It would be better to have something like shrinkToUses() for 3770b57cec5SDimitry Andric // physregs. That could potentially enable more DCE and it would free up 3780b57cec5SDimitry Andric // the physreg. It would not happen often, though. 3790b57cec5SDimitry Andric if (ReadsPhysRegs) { 3800b57cec5SDimitry Andric MI->setDesc(TII.get(TargetOpcode::KILL)); 3810b57cec5SDimitry Andric // Remove all operands that aren't physregs. 3820b57cec5SDimitry Andric for (unsigned i = MI->getNumOperands(); i; --i) { 3830b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(i-1); 384bdd1243dSDimitry Andric if (MO.isReg() && MO.getReg().isPhysical()) 3850b57cec5SDimitry Andric continue; 38681ad6265SDimitry Andric MI->removeOperand(i-1); 3870b57cec5SDimitry Andric } 3880b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Converted physregs to:\t" << *MI); 3890b57cec5SDimitry Andric } else { 3900b57cec5SDimitry Andric // If the dest of MI is an original reg and MI is reMaterializable, 3910b57cec5SDimitry Andric // don't delete the inst. Replace the dest with a new reg, and keep 3920b57cec5SDimitry Andric // the inst for remat of other siblings. The inst is saved in 3930b57cec5SDimitry Andric // LiveRangeEdit::DeadRemats and will be deleted after all the 3940b57cec5SDimitry Andric // allocations of the func are done. 395349cc55cSDimitry Andric // However, immediately delete instructions which have unshrunk virtual 396349cc55cSDimitry Andric // register uses. That may provoke RA to split an interval at the KILL 397349cc55cSDimitry Andric // and later result in an invalid live segment end. 398349cc55cSDimitry Andric if (isOrigDef && DeadRemats && !HasLiveVRegUses && 399fcaf7f86SDimitry Andric TII.isTriviallyReMaterializable(*MI)) { 4000b57cec5SDimitry Andric LiveInterval &NewLI = createEmptyIntervalFrom(Dest, false); 401972a253aSDimitry Andric VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator(); 402972a253aSDimitry Andric VNInfo *VNI = NewLI.getNextValue(Idx, Alloc); 4030b57cec5SDimitry Andric NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI)); 404972a253aSDimitry Andric 405972a253aSDimitry Andric if (DestSubReg) { 406972a253aSDimitry Andric const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo(); 407972a253aSDimitry Andric auto *SR = NewLI.createSubRange( 408972a253aSDimitry Andric Alloc, TRI->getSubRegIndexLaneMask(DestSubReg)); 409972a253aSDimitry Andric SR->addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), 410972a253aSDimitry Andric SR->getNextValue(Idx, Alloc))); 411972a253aSDimitry Andric } 412972a253aSDimitry Andric 4130b57cec5SDimitry Andric pop_back(); 4140b57cec5SDimitry Andric DeadRemats->insert(MI); 4150b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); 416e8d8bef9SDimitry Andric MI->substituteRegister(Dest, NewLI.reg(), 0, TRI); 417*0fca6ea1SDimitry Andric assert(MI->registerDefIsDead(NewLI.reg(), &TRI)); 4180b57cec5SDimitry Andric } else { 4190b57cec5SDimitry Andric if (TheDelegate) 4200b57cec5SDimitry Andric TheDelegate->LRE_WillEraseInstruction(MI); 4210b57cec5SDimitry Andric LIS.RemoveMachineInstrFromMaps(*MI); 4220b57cec5SDimitry Andric MI->eraseFromParent(); 4230b57cec5SDimitry Andric ++NumDCEDeleted; 4240b57cec5SDimitry Andric } 4250b57cec5SDimitry Andric } 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric // Erase any virtregs that are now empty and unused. There may be <undef> 4280b57cec5SDimitry Andric // uses around. Keep the empty live range in that case. 429cb14a3feSDimitry Andric for (Register Reg : RegsToErase) { 4300b57cec5SDimitry Andric if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) { 4310b57cec5SDimitry Andric ToShrink.remove(&LIS.getInterval(Reg)); 4320b57cec5SDimitry Andric eraseVirtReg(Reg); 4330b57cec5SDimitry Andric } 4340b57cec5SDimitry Andric } 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead, 438fcaf7f86SDimitry Andric ArrayRef<Register> RegsBeingSpilled) { 4390b57cec5SDimitry Andric ToShrinkSet ToShrink; 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric for (;;) { 4420b57cec5SDimitry Andric // Erase all dead defs. 4430b57cec5SDimitry Andric while (!Dead.empty()) 444fcaf7f86SDimitry Andric eliminateDeadDef(Dead.pop_back_val(), ToShrink); 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric if (ToShrink.empty()) 4470b57cec5SDimitry Andric break; 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric // Shrink just one live interval. Then delete new dead defs. 450349cc55cSDimitry Andric LiveInterval *LI = ToShrink.pop_back_val(); 4510b57cec5SDimitry Andric if (foldAsLoad(LI, Dead)) 4520b57cec5SDimitry Andric continue; 453bdd1243dSDimitry Andric Register VReg = LI->reg(); 4540b57cec5SDimitry Andric if (TheDelegate) 4550b57cec5SDimitry Andric TheDelegate->LRE_WillShrinkVirtReg(VReg); 4560b57cec5SDimitry Andric if (!LIS.shrinkToUses(LI, &Dead)) 4570b57cec5SDimitry Andric continue; 4580b57cec5SDimitry Andric 4590b57cec5SDimitry Andric // Don't create new intervals for a register being spilled. 4600b57cec5SDimitry Andric // The new intervals would have to be spilled anyway so its not worth it. 4610b57cec5SDimitry Andric // Also they currently aren't spilled so creating them and not spilling 4620b57cec5SDimitry Andric // them results in incorrect code. 4634824e7fdSDimitry Andric if (llvm::is_contained(RegsBeingSpilled, VReg)) 4644824e7fdSDimitry Andric continue; 4650b57cec5SDimitry Andric 4660b57cec5SDimitry Andric // LI may have been separated, create new intervals. 4670b57cec5SDimitry Andric LI->RenumberValues(); 4680b57cec5SDimitry Andric SmallVector<LiveInterval*, 8> SplitLIs; 4690b57cec5SDimitry Andric LIS.splitSeparateComponents(*LI, SplitLIs); 4700b57cec5SDimitry Andric if (!SplitLIs.empty()) 4710b57cec5SDimitry Andric ++NumFracRanges; 4720b57cec5SDimitry Andric 473e8d8bef9SDimitry Andric Register Original = VRM ? VRM->getOriginal(VReg) : Register(); 4740b57cec5SDimitry Andric for (const LiveInterval *SplitLI : SplitLIs) { 4750b57cec5SDimitry Andric // If LI is an original interval that hasn't been split yet, make the new 4760b57cec5SDimitry Andric // intervals their own originals instead of referring to LI. The original 4770b57cec5SDimitry Andric // interval must contain all the split products, and LI doesn't. 4780b57cec5SDimitry Andric if (Original != VReg && Original != 0) 479e8d8bef9SDimitry Andric VRM->setIsSplitFromReg(SplitLI->reg(), Original); 4800b57cec5SDimitry Andric if (TheDelegate) 481e8d8bef9SDimitry Andric TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg(), VReg); 4820b57cec5SDimitry Andric } 4830b57cec5SDimitry Andric } 4840b57cec5SDimitry Andric } 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric // Keep track of new virtual registers created via 4870b57cec5SDimitry Andric // MachineRegisterInfo::createVirtualRegister. 4880b57cec5SDimitry Andric void 4895ffd83dbSDimitry Andric LiveRangeEdit::MRI_NoteNewVirtualRegister(Register VReg) { 4900b57cec5SDimitry Andric if (VRM) 4910b57cec5SDimitry Andric VRM->grow(); 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric NewRegs.push_back(VReg); 4940b57cec5SDimitry Andric } 4950b57cec5SDimitry Andric 496fe6060f1SDimitry Andric void LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF, 497fe6060f1SDimitry Andric VirtRegAuxInfo &VRAI) { 4980b57cec5SDimitry Andric for (unsigned I = 0, Size = size(); I < Size; ++I) { 4990b57cec5SDimitry Andric LiveInterval &LI = LIS.getInterval(get(I)); 500e8d8bef9SDimitry Andric if (MRI.recomputeRegClass(LI.reg())) 5010b57cec5SDimitry Andric LLVM_DEBUG({ 5020b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); 503e8d8bef9SDimitry Andric dbgs() << "Inflated " << printReg(LI.reg()) << " to " 504e8d8bef9SDimitry Andric << TRI->getRegClassName(MRI.getRegClass(LI.reg())) << '\n'; 5050b57cec5SDimitry Andric }); 5060b57cec5SDimitry Andric VRAI.calculateSpillWeightAndHint(LI); 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric } 509