1dff0c46cSDimitry Andric //===-- LiveRangeEdit.cpp - Basic tools for editing a register live range -===//
22754fe60SDimitry Andric //
32754fe60SDimitry Andric // The LLVM Compiler Infrastructure
42754fe60SDimitry Andric //
52754fe60SDimitry Andric // This file is distributed under the University of Illinois Open Source
62754fe60SDimitry Andric // License. See LICENSE.TXT for details.
72754fe60SDimitry Andric //
82754fe60SDimitry Andric //===----------------------------------------------------------------------===//
92754fe60SDimitry Andric //
102754fe60SDimitry Andric // The LiveRangeEdit class represents changes done to a virtual register when it
112754fe60SDimitry Andric // is spilled or split.
122754fe60SDimitry Andric //===----------------------------------------------------------------------===//
132754fe60SDimitry Andric
14139f7f9bSDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
15bd5abe19SDimitry Andric #include "llvm/ADT/Statistic.h"
163b0f4066SDimitry Andric #include "llvm/CodeGen/CalcSpillWeights.h"
172cab237bSDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
182754fe60SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
192cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
20139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
213b0f4066SDimitry Andric #include "llvm/Support/Debug.h"
223b0f4066SDimitry Andric #include "llvm/Support/raw_ostream.h"
232754fe60SDimitry Andric
242754fe60SDimitry Andric using namespace llvm;
252754fe60SDimitry Andric
2691bc56edSDimitry Andric #define DEBUG_TYPE "regalloc"
2791bc56edSDimitry Andric
28bd5abe19SDimitry Andric STATISTIC(NumDCEDeleted, "Number of instructions deleted by DCE");
29bd5abe19SDimitry Andric STATISTIC(NumDCEFoldedLoads, "Number of single use loads folded after DCE");
30bd5abe19SDimitry Andric STATISTIC(NumFracRanges, "Number of live ranges fractured by DCE");
31bd5abe19SDimitry Andric
anchor()32dff0c46cSDimitry Andric void LiveRangeEdit::Delegate::anchor() { }
33dff0c46cSDimitry Andric
createEmptyIntervalFrom(unsigned OldReg,bool createSubRanges)34*4ba319b5SDimitry Andric LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(unsigned OldReg,
35*4ba319b5SDimitry Andric bool createSubRanges) {
363b0f4066SDimitry Andric unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
37*4ba319b5SDimitry Andric if (VRM)
38dff0c46cSDimitry Andric VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
39*4ba319b5SDimitry Andric
40f785676fSDimitry Andric LiveInterval &LI = LIS.createEmptyInterval(VReg);
417a7e6055SDimitry Andric if (Parent && !Parent->isSpillable())
427a7e6055SDimitry Andric LI.markNotSpillable();
43*4ba319b5SDimitry Andric if (createSubRanges) {
44d88c1a5aSDimitry Andric // Create empty subranges if the OldReg's interval has them. Do not create
45d88c1a5aSDimitry Andric // the main range here---it will be constructed later after the subranges
46d88c1a5aSDimitry Andric // have been finalized.
47d88c1a5aSDimitry Andric LiveInterval &OldLI = LIS.getInterval(OldReg);
48d88c1a5aSDimitry Andric VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator();
49d88c1a5aSDimitry Andric for (LiveInterval::SubRange &S : OldLI.subranges())
50d88c1a5aSDimitry Andric LI.createSubRange(Alloc, S.LaneMask);
51*4ba319b5SDimitry Andric }
523b0f4066SDimitry Andric return LI;
533b0f4066SDimitry Andric }
543b0f4066SDimitry Andric
createFrom(unsigned OldReg)55f785676fSDimitry Andric unsigned LiveRangeEdit::createFrom(unsigned OldReg) {
56f785676fSDimitry Andric unsigned VReg = MRI.createVirtualRegister(MRI.getRegClass(OldReg));
57f785676fSDimitry Andric if (VRM) {
58f785676fSDimitry Andric VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
59f785676fSDimitry Andric }
607a7e6055SDimitry Andric // FIXME: Getting the interval here actually computes it.
617a7e6055SDimitry Andric // In theory, this may not be what we want, but in practice
627a7e6055SDimitry Andric // the createEmptyIntervalFrom API is used when this is not
637a7e6055SDimitry Andric // the case. Generally speaking we just want to annotate the
647a7e6055SDimitry Andric // LiveInterval when it gets created but we cannot do that at
657a7e6055SDimitry Andric // the moment.
667a7e6055SDimitry Andric if (Parent && !Parent->isSpillable())
677a7e6055SDimitry Andric LIS.getInterval(VReg).markNotSpillable();
68f785676fSDimitry Andric return VReg;
69f785676fSDimitry Andric }
70f785676fSDimitry Andric
checkRematerializable(VNInfo * VNI,const MachineInstr * DefMI,AliasAnalysis * aa)713b0f4066SDimitry Andric bool LiveRangeEdit::checkRematerializable(VNInfo *VNI,
723b0f4066SDimitry Andric const MachineInstr *DefMI,
733b0f4066SDimitry Andric AliasAnalysis *aa) {
743b0f4066SDimitry Andric assert(DefMI && "Missing instruction");
757ae0e2c9SDimitry Andric ScannedRemattable = true;
763ca95b02SDimitry Andric if (!TII.isTriviallyReMaterializable(*DefMI, aa))
773b0f4066SDimitry Andric return false;
787ae0e2c9SDimitry Andric Remattable.insert(VNI);
793b0f4066SDimitry Andric return true;
802754fe60SDimitry Andric }
812754fe60SDimitry Andric
scanRemattable(AliasAnalysis * aa)82dff0c46cSDimitry Andric void LiveRangeEdit::scanRemattable(AliasAnalysis *aa) {
8339d628a0SDimitry Andric for (VNInfo *VNI : getParent().valnos) {
842754fe60SDimitry Andric if (VNI->isUnused())
852754fe60SDimitry Andric continue;
863ca95b02SDimitry Andric unsigned Original = VRM->getOriginal(getReg());
873ca95b02SDimitry Andric LiveInterval &OrigLI = LIS.getInterval(Original);
883ca95b02SDimitry Andric VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
89d88c1a5aSDimitry Andric if (!OrigVNI)
90d88c1a5aSDimitry Andric continue;
913ca95b02SDimitry Andric MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
922754fe60SDimitry Andric if (!DefMI)
932754fe60SDimitry Andric continue;
943ca95b02SDimitry Andric checkRematerializable(OrigVNI, DefMI, aa);
952754fe60SDimitry Andric }
967ae0e2c9SDimitry Andric ScannedRemattable = true;
972754fe60SDimitry Andric }
982754fe60SDimitry Andric
anyRematerializable(AliasAnalysis * aa)99dff0c46cSDimitry Andric bool LiveRangeEdit::anyRematerializable(AliasAnalysis *aa) {
1007ae0e2c9SDimitry Andric if (!ScannedRemattable)
101dff0c46cSDimitry Andric scanRemattable(aa);
1027ae0e2c9SDimitry Andric return !Remattable.empty();
1032754fe60SDimitry Andric }
1042754fe60SDimitry Andric
1052754fe60SDimitry Andric /// allUsesAvailableAt - Return true if all registers used by OrigMI at
1062754fe60SDimitry Andric /// OrigIdx are also available with the same value at UseIdx.
allUsesAvailableAt(const MachineInstr * OrigMI,SlotIndex OrigIdx,SlotIndex UseIdx) const1072754fe60SDimitry Andric bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI,
1082754fe60SDimitry Andric SlotIndex OrigIdx,
109139f7f9bSDimitry Andric SlotIndex UseIdx) const {
110dff0c46cSDimitry Andric OrigIdx = OrigIdx.getRegSlot(true);
111dff0c46cSDimitry Andric UseIdx = UseIdx.getRegSlot(true);
1122754fe60SDimitry Andric for (unsigned i = 0, e = OrigMI->getNumOperands(); i != e; ++i) {
1132754fe60SDimitry Andric const MachineOperand &MO = OrigMI->getOperand(i);
1147ae0e2c9SDimitry Andric if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
1152754fe60SDimitry Andric continue;
1167ae0e2c9SDimitry Andric
1177ae0e2c9SDimitry Andric // We can't remat physreg uses, unless it is a constant.
1187ae0e2c9SDimitry Andric if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
119d88c1a5aSDimitry Andric if (MRI.isConstantPhysReg(MO.getReg()))
1202754fe60SDimitry Andric continue;
1217ae0e2c9SDimitry Andric return false;
1227ae0e2c9SDimitry Andric }
1232754fe60SDimitry Andric
124dff0c46cSDimitry Andric LiveInterval &li = LIS.getInterval(MO.getReg());
1252754fe60SDimitry Andric const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
1262754fe60SDimitry Andric if (!OVNI)
1272754fe60SDimitry Andric continue;
1283861d79fSDimitry Andric
1293861d79fSDimitry Andric // Don't allow rematerialization immediately after the original def.
1303861d79fSDimitry Andric // It would be incorrect if OrigMI redefines the register.
1313861d79fSDimitry Andric // See PR14098.
1323861d79fSDimitry Andric if (SlotIndex::isSameInstr(OrigIdx, UseIdx))
1333861d79fSDimitry Andric return false;
1343861d79fSDimitry Andric
1352754fe60SDimitry Andric if (OVNI != li.getVNInfoAt(UseIdx))
1362754fe60SDimitry Andric return false;
1372754fe60SDimitry Andric }
1382754fe60SDimitry Andric return true;
1392754fe60SDimitry Andric }
1402754fe60SDimitry Andric
canRematerializeAt(Remat & RM,VNInfo * OrigVNI,SlotIndex UseIdx,bool cheapAsAMove)1413ca95b02SDimitry Andric bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
1423ca95b02SDimitry Andric SlotIndex UseIdx, bool cheapAsAMove) {
1437ae0e2c9SDimitry Andric assert(ScannedRemattable && "Call anyRematerializable first");
1442754fe60SDimitry Andric
1452754fe60SDimitry Andric // Use scanRemattable info.
1463ca95b02SDimitry Andric if (!Remattable.count(OrigVNI))
1472754fe60SDimitry Andric return false;
1482754fe60SDimitry Andric
1493b0f4066SDimitry Andric // No defining instruction provided.
1503b0f4066SDimitry Andric SlotIndex DefIdx;
1513b0f4066SDimitry Andric assert(RM.OrigMI && "No defining instruction for remattable value");
1523ca95b02SDimitry Andric DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
1532754fe60SDimitry Andric
1542754fe60SDimitry Andric // If only cheap remats were requested, bail out early.
1553ca95b02SDimitry Andric if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI))
1562754fe60SDimitry Andric return false;
1572754fe60SDimitry Andric
1582754fe60SDimitry Andric // Verify that all used registers are available with the same values.
159dff0c46cSDimitry Andric if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
1602754fe60SDimitry Andric return false;
1612754fe60SDimitry Andric
1622754fe60SDimitry Andric return true;
1632754fe60SDimitry Andric }
1642754fe60SDimitry Andric
rematerializeAt(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,unsigned DestReg,const Remat & RM,const TargetRegisterInfo & tri,bool Late)1652754fe60SDimitry Andric SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
1662754fe60SDimitry Andric MachineBasicBlock::iterator MI,
1672754fe60SDimitry Andric unsigned DestReg,
1682754fe60SDimitry Andric const Remat &RM,
1693b0f4066SDimitry Andric const TargetRegisterInfo &tri,
1703b0f4066SDimitry Andric bool Late) {
1712754fe60SDimitry Andric assert(RM.OrigMI && "Invalid remat");
1723ca95b02SDimitry Andric TII.reMaterialize(MBB, MI, DestReg, 0, *RM.OrigMI, tri);
1733ca95b02SDimitry Andric // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg
1743ca95b02SDimitry Andric // to false anyway in case the isDead flag of RM.OrigMI's dest register
1753ca95b02SDimitry Andric // is true.
1763ca95b02SDimitry Andric (*--MI).getOperand(0).setIsDead(false);
1777ae0e2c9SDimitry Andric Rematted.insert(RM.ParentVNI);
1783ca95b02SDimitry Andric return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
1792754fe60SDimitry Andric }
1802754fe60SDimitry Andric
eraseVirtReg(unsigned Reg)181dff0c46cSDimitry Andric void LiveRangeEdit::eraseVirtReg(unsigned Reg) {
1827ae0e2c9SDimitry Andric if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
1833b0f4066SDimitry Andric LIS.removeInterval(Reg);
1843b0f4066SDimitry Andric }
1853b0f4066SDimitry Andric
foldAsLoad(LiveInterval * LI,SmallVectorImpl<MachineInstr * > & Dead)1863b0f4066SDimitry Andric bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
187dff0c46cSDimitry Andric SmallVectorImpl<MachineInstr*> &Dead) {
18891bc56edSDimitry Andric MachineInstr *DefMI = nullptr, *UseMI = nullptr;
1893b0f4066SDimitry Andric
1903b0f4066SDimitry Andric // Check that there is a single def and a single use.
19191bc56edSDimitry Andric for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg)) {
1923b0f4066SDimitry Andric MachineInstr *MI = MO.getParent();
1933b0f4066SDimitry Andric if (MO.isDef()) {
1943b0f4066SDimitry Andric if (DefMI && DefMI != MI)
1953b0f4066SDimitry Andric return false;
196dff0c46cSDimitry Andric if (!MI->canFoldAsLoad())
1973b0f4066SDimitry Andric return false;
1983b0f4066SDimitry Andric DefMI = MI;
1993b0f4066SDimitry Andric } else if (!MO.isUndef()) {
2003b0f4066SDimitry Andric if (UseMI && UseMI != MI)
2013b0f4066SDimitry Andric return false;
2023b0f4066SDimitry Andric // FIXME: Targets don't know how to fold subreg uses.
2033b0f4066SDimitry Andric if (MO.getSubReg())
2043b0f4066SDimitry Andric return false;
2053b0f4066SDimitry Andric UseMI = MI;
2063b0f4066SDimitry Andric }
2073b0f4066SDimitry Andric }
2083b0f4066SDimitry Andric if (!DefMI || !UseMI)
2093b0f4066SDimitry Andric return false;
2103b0f4066SDimitry Andric
2117ae0e2c9SDimitry Andric // Since we're moving the DefMI load, make sure we're not extending any live
2127ae0e2c9SDimitry Andric // ranges.
2133ca95b02SDimitry Andric if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
2143ca95b02SDimitry Andric LIS.getInstructionIndex(*UseMI)))
2157ae0e2c9SDimitry Andric return false;
2167ae0e2c9SDimitry Andric
2177ae0e2c9SDimitry Andric // We also need to make sure it is safe to move the load.
2187ae0e2c9SDimitry Andric // Assume there are stores between DefMI and UseMI.
2197ae0e2c9SDimitry Andric bool SawStore = true;
220ff0cc061SDimitry Andric if (!DefMI->isSafeToMove(nullptr, SawStore))
2217ae0e2c9SDimitry Andric return false;
2227ae0e2c9SDimitry Andric
223*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Try to fold single def: " << *DefMI
2243b0f4066SDimitry Andric << " into single use: " << *UseMI);
2253b0f4066SDimitry Andric
2263b0f4066SDimitry Andric SmallVector<unsigned, 8> Ops;
2273b0f4066SDimitry Andric if (UseMI->readsWritesVirtualRegister(LI->reg, &Ops).second)
2283b0f4066SDimitry Andric return false;
2293b0f4066SDimitry Andric
2303ca95b02SDimitry Andric MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
2313b0f4066SDimitry Andric if (!FoldMI)
2323b0f4066SDimitry Andric return false;
233*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " folded: " << *FoldMI);
2343ca95b02SDimitry Andric LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
2353b0f4066SDimitry Andric UseMI->eraseFromParent();
23691bc56edSDimitry Andric DefMI->addRegisterDead(LI->reg, nullptr);
2373b0f4066SDimitry Andric Dead.push_back(DefMI);
238bd5abe19SDimitry Andric ++NumDCEFoldedLoads;
2393b0f4066SDimitry Andric return true;
2403b0f4066SDimitry Andric }
2413b0f4066SDimitry Andric
useIsKill(const LiveInterval & LI,const MachineOperand & MO) const24297bc6c73SDimitry Andric bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
24397bc6c73SDimitry Andric const MachineOperand &MO) const {
2443ca95b02SDimitry Andric const MachineInstr &MI = *MO.getParent();
24597bc6c73SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
24697bc6c73SDimitry Andric if (LI.Query(Idx).isKill())
24797bc6c73SDimitry Andric return true;
24897bc6c73SDimitry Andric const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
24997bc6c73SDimitry Andric unsigned SubReg = MO.getSubReg();
2507d523365SDimitry Andric LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
25197bc6c73SDimitry Andric for (const LiveInterval::SubRange &S : LI.subranges()) {
252d88c1a5aSDimitry Andric if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
25397bc6c73SDimitry Andric return true;
25497bc6c73SDimitry Andric }
25597bc6c73SDimitry Andric return false;
25697bc6c73SDimitry Andric }
25797bc6c73SDimitry Andric
258f785676fSDimitry Andric /// Find all live intervals that need to shrink, then remove the instruction.
eliminateDeadDef(MachineInstr * MI,ToShrinkSet & ToShrink,AliasAnalysis * AA)2593ca95b02SDimitry Andric void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink,
2603ca95b02SDimitry Andric AliasAnalysis *AA) {
2613b0f4066SDimitry Andric assert(MI->allDefsAreDead() && "Def isn't really dead");
2623ca95b02SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
2633b0f4066SDimitry Andric
264f785676fSDimitry Andric // Never delete a bundled instruction.
265f785676fSDimitry Andric if (MI->isBundled()) {
266f785676fSDimitry Andric return;
267f785676fSDimitry Andric }
2683b0f4066SDimitry Andric // Never delete inline asm.
2693b0f4066SDimitry Andric if (MI->isInlineAsm()) {
270*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
271f785676fSDimitry Andric return;
2723b0f4066SDimitry Andric }
2733b0f4066SDimitry Andric
2743b0f4066SDimitry Andric // Use the same criteria as DeadMachineInstructionElim.
2753b0f4066SDimitry Andric bool SawStore = false;
276ff0cc061SDimitry Andric if (!MI->isSafeToMove(nullptr, SawStore)) {
277*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
278f785676fSDimitry Andric return;
2793b0f4066SDimitry Andric }
2803b0f4066SDimitry Andric
281*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
2823b0f4066SDimitry Andric
2837ae0e2c9SDimitry Andric // Collect virtual registers to be erased after MI is gone.
2847ae0e2c9SDimitry Andric SmallVector<unsigned, 8> RegsToErase;
2857ae0e2c9SDimitry Andric bool ReadsPhysRegs = false;
2863ca95b02SDimitry Andric bool isOrigDef = false;
2873ca95b02SDimitry Andric unsigned Dest;
288d88c1a5aSDimitry Andric // Only optimize rematerialize case when the instruction has one def, since
289d88c1a5aSDimitry Andric // otherwise we could leave some dead defs in the code. This case is
290d88c1a5aSDimitry Andric // extremely rare.
291d88c1a5aSDimitry Andric if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
292d88c1a5aSDimitry Andric MI->getDesc().getNumDefs() == 1) {
2933ca95b02SDimitry Andric Dest = MI->getOperand(0).getReg();
2943ca95b02SDimitry Andric unsigned Original = VRM->getOriginal(Dest);
2953ca95b02SDimitry Andric LiveInterval &OrigLI = LIS.getInterval(Original);
2963ca95b02SDimitry Andric VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
2973ca95b02SDimitry Andric // The original live-range may have been shrunk to
2983ca95b02SDimitry Andric // an empty live-range. It happens when it is dead, but
2993ca95b02SDimitry Andric // we still keep it around to be able to rematerialize
3003ca95b02SDimitry Andric // other values that depend on it.
3013ca95b02SDimitry Andric if (OrigVNI)
3023ca95b02SDimitry Andric isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx);
3033ca95b02SDimitry Andric }
3047ae0e2c9SDimitry Andric
3053b0f4066SDimitry Andric // Check for live intervals that may shrink
3063b0f4066SDimitry Andric for (MachineInstr::mop_iterator MOI = MI->operands_begin(),
3073b0f4066SDimitry Andric MOE = MI->operands_end(); MOI != MOE; ++MOI) {
3083b0f4066SDimitry Andric if (!MOI->isReg())
3093b0f4066SDimitry Andric continue;
3103b0f4066SDimitry Andric unsigned Reg = MOI->getReg();
3117ae0e2c9SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(Reg)) {
3127ae0e2c9SDimitry Andric // Check if MI reads any unreserved physregs.
3133861d79fSDimitry Andric if (Reg && MOI->readsReg() && !MRI.isReserved(Reg))
3147ae0e2c9SDimitry Andric ReadsPhysRegs = true;
315ff0cc061SDimitry Andric else if (MOI->isDef())
316ff0cc061SDimitry Andric LIS.removePhysRegDefAt(Reg, Idx);
3173b0f4066SDimitry Andric continue;
3187ae0e2c9SDimitry Andric }
3193b0f4066SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg);
3203b0f4066SDimitry Andric
3213b0f4066SDimitry Andric // Shrink read registers, unless it is likely to be expensive and
3223b0f4066SDimitry Andric // unlikely to change anything. We typically don't want to shrink the
3233b0f4066SDimitry Andric // PIC base register that has lots of uses everywhere.
3243b0f4066SDimitry Andric // Always shrink COPY uses that probably come from live range splitting.
32597bc6c73SDimitry Andric if ((MI->readsVirtualRegister(Reg) && (MI->isCopy() || MOI->isDef())) ||
32697bc6c73SDimitry Andric (MOI->readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI))))
3273b0f4066SDimitry Andric ToShrink.insert(&LI);
3283b0f4066SDimitry Andric
3293b0f4066SDimitry Andric // Remove defined value.
3303b0f4066SDimitry Andric if (MOI->isDef()) {
331ff0cc061SDimitry Andric if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
3327ae0e2c9SDimitry Andric TheDelegate->LRE_WillShrinkVirtReg(LI.reg);
333ff0cc061SDimitry Andric LIS.removeVRegDefAt(LI, Idx);
334ff0cc061SDimitry Andric if (LI.empty())
3357ae0e2c9SDimitry Andric RegsToErase.push_back(Reg);
3363b0f4066SDimitry Andric }
3373b0f4066SDimitry Andric }
3383b0f4066SDimitry Andric
3397ae0e2c9SDimitry Andric // Currently, we don't support DCE of physreg live ranges. If MI reads
3407ae0e2c9SDimitry Andric // any unreserved physregs, don't erase the instruction, but turn it into
3417ae0e2c9SDimitry Andric // a KILL instead. This way, the physreg live ranges don't end up
3427ae0e2c9SDimitry Andric // dangling.
3437ae0e2c9SDimitry Andric // FIXME: It would be better to have something like shrinkToUses() for
3447ae0e2c9SDimitry Andric // physregs. That could potentially enable more DCE and it would free up
3457ae0e2c9SDimitry Andric // the physreg. It would not happen often, though.
3467ae0e2c9SDimitry Andric if (ReadsPhysRegs) {
3477ae0e2c9SDimitry Andric MI->setDesc(TII.get(TargetOpcode::KILL));
3487ae0e2c9SDimitry Andric // Remove all operands that aren't physregs.
3497ae0e2c9SDimitry Andric for (unsigned i = MI->getNumOperands(); i; --i) {
3507ae0e2c9SDimitry Andric const MachineOperand &MO = MI->getOperand(i-1);
3517ae0e2c9SDimitry Andric if (MO.isReg() && TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
3527ae0e2c9SDimitry Andric continue;
3537ae0e2c9SDimitry Andric MI->RemoveOperand(i-1);
3547ae0e2c9SDimitry Andric }
355*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
3567ae0e2c9SDimitry Andric } else {
3573ca95b02SDimitry Andric // If the dest of MI is an original reg and MI is reMaterializable,
3583ca95b02SDimitry Andric // don't delete the inst. Replace the dest with a new reg, and keep
3593ca95b02SDimitry Andric // the inst for remat of other siblings. The inst is saved in
3603ca95b02SDimitry Andric // LiveRangeEdit::DeadRemats and will be deleted after all the
3613ca95b02SDimitry Andric // allocations of the func are done.
3623ca95b02SDimitry Andric if (isOrigDef && DeadRemats && TII.isTriviallyReMaterializable(*MI, AA)) {
363*4ba319b5SDimitry Andric LiveInterval &NewLI = createEmptyIntervalFrom(Dest, false);
3643ca95b02SDimitry Andric VNInfo *VNI = NewLI.getNextValue(Idx, LIS.getVNInfoAllocator());
3653ca95b02SDimitry Andric NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI));
3663ca95b02SDimitry Andric pop_back();
367*4ba319b5SDimitry Andric DeadRemats->insert(MI);
3683ca95b02SDimitry Andric const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
3693ca95b02SDimitry Andric MI->substituteRegister(Dest, NewLI.reg, 0, TRI);
3703ca95b02SDimitry Andric MI->getOperand(0).setIsDead(true);
3713ca95b02SDimitry Andric } else {
3727ae0e2c9SDimitry Andric if (TheDelegate)
3737ae0e2c9SDimitry Andric TheDelegate->LRE_WillEraseInstruction(MI);
3743ca95b02SDimitry Andric LIS.RemoveMachineInstrFromMaps(*MI);
3753b0f4066SDimitry Andric MI->eraseFromParent();
376bd5abe19SDimitry Andric ++NumDCEDeleted;
3773b0f4066SDimitry Andric }
3783ca95b02SDimitry Andric }
3793b0f4066SDimitry Andric
3807ae0e2c9SDimitry Andric // Erase any virtregs that are now empty and unused. There may be <undef>
3817ae0e2c9SDimitry Andric // uses around. Keep the empty live range in that case.
3827ae0e2c9SDimitry Andric for (unsigned i = 0, e = RegsToErase.size(); i != e; ++i) {
3837ae0e2c9SDimitry Andric unsigned Reg = RegsToErase[i];
3847ae0e2c9SDimitry Andric if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
3857ae0e2c9SDimitry Andric ToShrink.remove(&LIS.getInterval(Reg));
3867ae0e2c9SDimitry Andric eraseVirtReg(Reg);
3877ae0e2c9SDimitry Andric }
3887ae0e2c9SDimitry Andric }
3897ae0e2c9SDimitry Andric }
3907ae0e2c9SDimitry Andric
eliminateDeadDefs(SmallVectorImpl<MachineInstr * > & Dead,ArrayRef<unsigned> RegsBeingSpilled,AliasAnalysis * AA)391f785676fSDimitry Andric void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
3923ca95b02SDimitry Andric ArrayRef<unsigned> RegsBeingSpilled,
3933ca95b02SDimitry Andric AliasAnalysis *AA) {
394f785676fSDimitry Andric ToShrinkSet ToShrink;
395f785676fSDimitry Andric
396f785676fSDimitry Andric for (;;) {
397f785676fSDimitry Andric // Erase all dead defs.
398f785676fSDimitry Andric while (!Dead.empty())
3993ca95b02SDimitry Andric eliminateDeadDef(Dead.pop_back_val(), ToShrink, AA);
400f785676fSDimitry Andric
4013b0f4066SDimitry Andric if (ToShrink.empty())
4023b0f4066SDimitry Andric break;
4033b0f4066SDimitry Andric
4043b0f4066SDimitry Andric // Shrink just one live interval. Then delete new dead defs.
4053b0f4066SDimitry Andric LiveInterval *LI = ToShrink.back();
4063b0f4066SDimitry Andric ToShrink.pop_back();
407dff0c46cSDimitry Andric if (foldAsLoad(LI, Dead))
4083b0f4066SDimitry Andric continue;
4097d523365SDimitry Andric unsigned VReg = LI->reg;
4107ae0e2c9SDimitry Andric if (TheDelegate)
4117d523365SDimitry Andric TheDelegate->LRE_WillShrinkVirtReg(VReg);
4123b0f4066SDimitry Andric if (!LIS.shrinkToUses(LI, &Dead))
4133b0f4066SDimitry Andric continue;
4143b0f4066SDimitry Andric
415dff0c46cSDimitry Andric // Don't create new intervals for a register being spilled.
416dff0c46cSDimitry Andric // The new intervals would have to be spilled anyway so its not worth it.
417dff0c46cSDimitry Andric // Also they currently aren't spilled so creating them and not spilling
418dff0c46cSDimitry Andric // them results in incorrect code.
419dff0c46cSDimitry Andric bool BeingSpilled = false;
420dff0c46cSDimitry Andric for (unsigned i = 0, e = RegsBeingSpilled.size(); i != e; ++i) {
4217d523365SDimitry Andric if (VReg == RegsBeingSpilled[i]) {
422dff0c46cSDimitry Andric BeingSpilled = true;
423dff0c46cSDimitry Andric break;
424dff0c46cSDimitry Andric }
425dff0c46cSDimitry Andric }
426dff0c46cSDimitry Andric
427dff0c46cSDimitry Andric if (BeingSpilled) continue;
428dff0c46cSDimitry Andric
4293b0f4066SDimitry Andric // LI may have been separated, create new intervals.
430f785676fSDimitry Andric LI->RenumberValues();
4317d523365SDimitry Andric SmallVector<LiveInterval*, 8> SplitLIs;
4327d523365SDimitry Andric LIS.splitSeparateComponents(*LI, SplitLIs);
4337d523365SDimitry Andric if (!SplitLIs.empty())
434bd5abe19SDimitry Andric ++NumFracRanges;
4357d523365SDimitry Andric
4367d523365SDimitry Andric unsigned Original = VRM ? VRM->getOriginal(VReg) : 0;
4377d523365SDimitry Andric for (const LiveInterval *SplitLI : SplitLIs) {
43817a519f9SDimitry Andric // If LI is an original interval that hasn't been split yet, make the new
43917a519f9SDimitry Andric // intervals their own originals instead of referring to LI. The original
44017a519f9SDimitry Andric // interval must contain all the split products, and LI doesn't.
4417d523365SDimitry Andric if (Original != VReg && Original != 0)
4427d523365SDimitry Andric VRM->setIsSplitFromReg(SplitLI->reg, Original);
4437ae0e2c9SDimitry Andric if (TheDelegate)
4447d523365SDimitry Andric TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg, VReg);
4453b0f4066SDimitry Andric }
4463b0f4066SDimitry Andric }
4473b0f4066SDimitry Andric }
4483b0f4066SDimitry Andric
449f785676fSDimitry Andric // Keep track of new virtual registers created via
450f785676fSDimitry Andric // MachineRegisterInfo::createVirtualRegister.
451f785676fSDimitry Andric void
MRI_NoteNewVirtualRegister(unsigned VReg)452f785676fSDimitry Andric LiveRangeEdit::MRI_NoteNewVirtualRegister(unsigned VReg)
453f785676fSDimitry Andric {
454f785676fSDimitry Andric if (VRM)
455f785676fSDimitry Andric VRM->grow();
456f785676fSDimitry Andric
457f785676fSDimitry Andric NewRegs.push_back(VReg);
458f785676fSDimitry Andric }
459f785676fSDimitry Andric
460f785676fSDimitry Andric void
calculateRegClassAndHint(MachineFunction & MF,const MachineLoopInfo & Loops,const MachineBlockFrequencyInfo & MBFI)461f785676fSDimitry Andric LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
462f785676fSDimitry Andric const MachineLoopInfo &Loops,
463f785676fSDimitry Andric const MachineBlockFrequencyInfo &MBFI) {
4647d523365SDimitry Andric VirtRegAuxInfo VRAI(MF, LIS, VRM, Loops, MBFI);
465f785676fSDimitry Andric for (unsigned I = 0, Size = size(); I < Size; ++I) {
466f785676fSDimitry Andric LiveInterval &LI = LIS.getInterval(get(I));
467ff0cc061SDimitry Andric if (MRI.recomputeRegClass(LI.reg))
468*4ba319b5SDimitry Andric LLVM_DEBUG({
46939d628a0SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
4702cab237bSDimitry Andric dbgs() << "Inflated " << printReg(LI.reg) << " to "
47139d628a0SDimitry Andric << TRI->getRegClassName(MRI.getRegClass(LI.reg)) << '\n';
47239d628a0SDimitry Andric });
473f785676fSDimitry Andric VRAI.calculateSpillWeightAndHint(LI);
4743b0f4066SDimitry Andric }
4753b0f4066SDimitry Andric }
476