163faed5bSDimitry Andric //===-- LiveRangeEdit.cpp - Basic tools for editing a register live range -===//
2cf099d11SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6cf099d11SDimitry Andric //
7cf099d11SDimitry Andric //===----------------------------------------------------------------------===//
8cf099d11SDimitry Andric //
9cf099d11SDimitry Andric // The LiveRangeEdit class represents changes done to a virtual register when it
10cf099d11SDimitry Andric // is spilled or split.
11cf099d11SDimitry Andric //===----------------------------------------------------------------------===//
12cf099d11SDimitry Andric
134a16efa3SDimitry Andric #include "llvm/CodeGen/LiveRangeEdit.h"
1456fe8f14SDimitry Andric #include "llvm/ADT/Statistic.h"
156b943ff3SDimitry Andric #include "llvm/CodeGen/CalcSpillWeights.h"
16044eb2f6SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
17cf099d11SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
18044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
194a16efa3SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
206b943ff3SDimitry Andric #include "llvm/Support/Debug.h"
216b943ff3SDimitry Andric #include "llvm/Support/raw_ostream.h"
22cf099d11SDimitry Andric
23cf099d11SDimitry Andric using namespace llvm;
24cf099d11SDimitry Andric
255ca98fd9SDimitry Andric #define DEBUG_TYPE "regalloc"
265ca98fd9SDimitry Andric
2756fe8f14SDimitry Andric STATISTIC(NumDCEDeleted, "Number of instructions deleted by DCE");
2856fe8f14SDimitry Andric STATISTIC(NumDCEFoldedLoads, "Number of single use loads folded after DCE");
2956fe8f14SDimitry Andric STATISTIC(NumFracRanges, "Number of live ranges fractured by DCE");
30e3b55780SDimitry Andric STATISTIC(NumReMaterialization, "Number of instructions rematerialized");
3156fe8f14SDimitry Andric
anchor()3263faed5bSDimitry Andric void LiveRangeEdit::Delegate::anchor() { }
3363faed5bSDimitry Andric
createEmptyIntervalFrom(Register OldReg,bool createSubRanges)34cfca06d7SDimitry Andric LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(Register OldReg,
35eb11fae6SDimitry Andric bool createSubRanges) {
36e3b55780SDimitry Andric Register VReg = MRI.cloneVirtualRegister(OldReg);
37eb11fae6SDimitry Andric if (VRM)
3863faed5bSDimitry Andric VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
39eb11fae6SDimitry Andric
40f8af5cf6SDimitry Andric LiveInterval &LI = LIS.createEmptyInterval(VReg);
4171d5a254SDimitry Andric if (Parent && !Parent->isSpillable())
4271d5a254SDimitry Andric LI.markNotSpillable();
43eb11fae6SDimitry Andric if (createSubRanges) {
44b915e9e0SDimitry Andric // Create empty subranges if the OldReg's interval has them. Do not create
45b915e9e0SDimitry Andric // the main range here---it will be constructed later after the subranges
46b915e9e0SDimitry Andric // have been finalized.
47b915e9e0SDimitry Andric LiveInterval &OldLI = LIS.getInterval(OldReg);
48b915e9e0SDimitry Andric VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator();
49b915e9e0SDimitry Andric for (LiveInterval::SubRange &S : OldLI.subranges())
50b915e9e0SDimitry Andric LI.createSubRange(Alloc, S.LaneMask);
51eb11fae6SDimitry Andric }
526b943ff3SDimitry Andric return LI;
536b943ff3SDimitry Andric }
546b943ff3SDimitry Andric
createFrom(Register OldReg)55cfca06d7SDimitry Andric Register LiveRangeEdit::createFrom(Register OldReg) {
56e3b55780SDimitry Andric Register VReg = MRI.cloneVirtualRegister(OldReg);
57f8af5cf6SDimitry Andric if (VRM) {
58f8af5cf6SDimitry Andric VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
59f8af5cf6SDimitry Andric }
6071d5a254SDimitry Andric // FIXME: Getting the interval here actually computes it.
6171d5a254SDimitry Andric // In theory, this may not be what we want, but in practice
6271d5a254SDimitry Andric // the createEmptyIntervalFrom API is used when this is not
6371d5a254SDimitry Andric // the case. Generally speaking we just want to annotate the
6471d5a254SDimitry Andric // LiveInterval when it gets created but we cannot do that at
6571d5a254SDimitry Andric // the moment.
6671d5a254SDimitry Andric if (Parent && !Parent->isSpillable())
6771d5a254SDimitry Andric LIS.getInterval(VReg).markNotSpillable();
68f8af5cf6SDimitry Andric return VReg;
69f8af5cf6SDimitry Andric }
70f8af5cf6SDimitry Andric
checkRematerializable(VNInfo * VNI,const MachineInstr * DefMI)716b943ff3SDimitry Andric bool LiveRangeEdit::checkRematerializable(VNInfo *VNI,
724b4fe385SDimitry Andric const MachineInstr *DefMI) {
736b943ff3SDimitry Andric assert(DefMI && "Missing instruction");
7458b69754SDimitry Andric ScannedRemattable = true;
754b4fe385SDimitry Andric if (!TII.isTriviallyReMaterializable(*DefMI))
766b943ff3SDimitry Andric return false;
7758b69754SDimitry Andric Remattable.insert(VNI);
786b943ff3SDimitry Andric return true;
79cf099d11SDimitry Andric }
80cf099d11SDimitry Andric
scanRemattable()814b4fe385SDimitry Andric void LiveRangeEdit::scanRemattable() {
8267c32a98SDimitry Andric for (VNInfo *VNI : getParent().valnos) {
83cf099d11SDimitry Andric if (VNI->isUnused())
84cf099d11SDimitry Andric continue;
857fa27ce4SDimitry Andric Register Original = VRM->getOriginal(getReg());
8601095a5dSDimitry Andric LiveInterval &OrigLI = LIS.getInterval(Original);
8701095a5dSDimitry Andric VNInfo *OrigVNI = OrigLI.getVNInfoAt(VNI->def);
88b915e9e0SDimitry Andric if (!OrigVNI)
89b915e9e0SDimitry Andric continue;
9001095a5dSDimitry Andric MachineInstr *DefMI = LIS.getInstructionFromIndex(OrigVNI->def);
91cf099d11SDimitry Andric if (!DefMI)
92cf099d11SDimitry Andric continue;
934b4fe385SDimitry Andric checkRematerializable(OrigVNI, DefMI);
94cf099d11SDimitry Andric }
9558b69754SDimitry Andric ScannedRemattable = true;
96cf099d11SDimitry Andric }
97cf099d11SDimitry Andric
anyRematerializable()984b4fe385SDimitry Andric bool LiveRangeEdit::anyRematerializable() {
9958b69754SDimitry Andric if (!ScannedRemattable)
1004b4fe385SDimitry Andric scanRemattable();
10158b69754SDimitry Andric return !Remattable.empty();
102cf099d11SDimitry Andric }
103cf099d11SDimitry Andric
104cf099d11SDimitry Andric /// allUsesAvailableAt - Return true if all registers used by OrigMI at
105cf099d11SDimitry Andric /// OrigIdx are also available with the same value at UseIdx.
allUsesAvailableAt(const MachineInstr * OrigMI,SlotIndex OrigIdx,SlotIndex UseIdx) const106cf099d11SDimitry Andric bool LiveRangeEdit::allUsesAvailableAt(const MachineInstr *OrigMI,
107cf099d11SDimitry Andric SlotIndex OrigIdx,
1084a16efa3SDimitry Andric SlotIndex UseIdx) const {
10963faed5bSDimitry Andric OrigIdx = OrigIdx.getRegSlot(true);
110c0981da4SDimitry Andric UseIdx = std::max(UseIdx, UseIdx.getRegSlot(true));
111f65dcba8SDimitry Andric for (const MachineOperand &MO : OrigMI->operands()) {
11258b69754SDimitry Andric if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
113cf099d11SDimitry Andric continue;
11458b69754SDimitry Andric
115344a3780SDimitry Andric // We can't remat physreg uses, unless it is a constant or target wants
116344a3780SDimitry Andric // to ignore this use.
117e3b55780SDimitry Andric if (MO.getReg().isPhysical()) {
118344a3780SDimitry Andric if (MRI.isConstantPhysReg(MO.getReg()) || TII.isIgnorableUse(MO))
119cf099d11SDimitry Andric continue;
12058b69754SDimitry Andric return false;
12158b69754SDimitry Andric }
122cf099d11SDimitry Andric
12363faed5bSDimitry Andric LiveInterval &li = LIS.getInterval(MO.getReg());
124cf099d11SDimitry Andric const VNInfo *OVNI = li.getVNInfoAt(OrigIdx);
125cf099d11SDimitry Andric if (!OVNI)
126cf099d11SDimitry Andric continue;
127522600a2SDimitry Andric
128522600a2SDimitry Andric // Don't allow rematerialization immediately after the original def.
129522600a2SDimitry Andric // It would be incorrect if OrigMI redefines the register.
130522600a2SDimitry Andric // See PR14098.
131522600a2SDimitry Andric if (SlotIndex::isSameInstr(OrigIdx, UseIdx))
132522600a2SDimitry Andric return false;
133522600a2SDimitry Andric
134cf099d11SDimitry Andric if (OVNI != li.getVNInfoAt(UseIdx))
135cf099d11SDimitry Andric return false;
13677fc4c14SDimitry Andric
13777fc4c14SDimitry Andric // Check that subrange is live at UseIdx.
138e3b55780SDimitry Andric if (li.hasSubRanges()) {
13977fc4c14SDimitry Andric const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
140e3b55780SDimitry Andric unsigned SubReg = MO.getSubReg();
141e3b55780SDimitry Andric LaneBitmask LM = SubReg ? TRI->getSubRegIndexLaneMask(SubReg)
142e3b55780SDimitry Andric : MRI.getMaxLaneMaskForVReg(MO.getReg());
14377fc4c14SDimitry Andric for (LiveInterval::SubRange &SR : li.subranges()) {
14477fc4c14SDimitry Andric if ((SR.LaneMask & LM).none())
14577fc4c14SDimitry Andric continue;
14677fc4c14SDimitry Andric if (!SR.liveAt(UseIdx))
14777fc4c14SDimitry Andric return false;
14877fc4c14SDimitry Andric // Early exit if all used lanes are checked. No need to continue.
14977fc4c14SDimitry Andric LM &= ~SR.LaneMask;
15077fc4c14SDimitry Andric if (LM.none())
15177fc4c14SDimitry Andric break;
15277fc4c14SDimitry Andric }
15377fc4c14SDimitry Andric }
154cf099d11SDimitry Andric }
155cf099d11SDimitry Andric return true;
156cf099d11SDimitry Andric }
157cf099d11SDimitry Andric
canRematerializeAt(Remat & RM,VNInfo * OrigVNI,SlotIndex UseIdx,bool cheapAsAMove)15801095a5dSDimitry Andric bool LiveRangeEdit::canRematerializeAt(Remat &RM, VNInfo *OrigVNI,
15901095a5dSDimitry Andric SlotIndex UseIdx, bool cheapAsAMove) {
16058b69754SDimitry Andric assert(ScannedRemattable && "Call anyRematerializable first");
161cf099d11SDimitry Andric
162cf099d11SDimitry Andric // Use scanRemattable info.
16301095a5dSDimitry Andric if (!Remattable.count(OrigVNI))
164cf099d11SDimitry Andric return false;
165cf099d11SDimitry Andric
1666b943ff3SDimitry Andric // No defining instruction provided.
1676b943ff3SDimitry Andric SlotIndex DefIdx;
1686b943ff3SDimitry Andric assert(RM.OrigMI && "No defining instruction for remattable value");
16901095a5dSDimitry Andric DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
170cf099d11SDimitry Andric
171cf099d11SDimitry Andric // If only cheap remats were requested, bail out early.
17201095a5dSDimitry Andric if (cheapAsAMove && !TII.isAsCheapAsAMove(*RM.OrigMI))
173cf099d11SDimitry Andric return false;
174cf099d11SDimitry Andric
175cf099d11SDimitry Andric // Verify that all used registers are available with the same values.
17663faed5bSDimitry Andric if (!allUsesAvailableAt(RM.OrigMI, DefIdx, UseIdx))
177cf099d11SDimitry Andric return false;
178cf099d11SDimitry Andric
179cf099d11SDimitry Andric return true;
180cf099d11SDimitry Andric }
181cf099d11SDimitry Andric
rematerializeAt(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,Register DestReg,const Remat & RM,const TargetRegisterInfo & tri,bool Late,unsigned SubIdx,MachineInstr * ReplaceIndexMI)182cf099d11SDimitry Andric SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
183cf099d11SDimitry Andric MachineBasicBlock::iterator MI,
1847fa27ce4SDimitry Andric Register DestReg, const Remat &RM,
1856b943ff3SDimitry Andric const TargetRegisterInfo &tri,
1867fa27ce4SDimitry Andric bool Late, unsigned SubIdx,
187e3b55780SDimitry Andric MachineInstr *ReplaceIndexMI) {
188cf099d11SDimitry Andric assert(RM.OrigMI && "Invalid remat");
189e3b55780SDimitry Andric TII.reMaterialize(MBB, MI, DestReg, SubIdx, *RM.OrigMI, tri);
19001095a5dSDimitry Andric // DestReg of the cloned instruction cannot be Dead. Set isDead of DestReg
19101095a5dSDimitry Andric // to false anyway in case the isDead flag of RM.OrigMI's dest register
19201095a5dSDimitry Andric // is true.
193b1c73532SDimitry Andric (*--MI).clearRegisterDeads(DestReg);
19458b69754SDimitry Andric Rematted.insert(RM.ParentVNI);
195e3b55780SDimitry Andric ++NumReMaterialization;
196e3b55780SDimitry Andric
197e3b55780SDimitry Andric if (ReplaceIndexMI)
198e3b55780SDimitry Andric return LIS.ReplaceMachineInstrInMaps(*ReplaceIndexMI, *MI).getRegSlot();
19901095a5dSDimitry Andric return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
200cf099d11SDimitry Andric }
201cf099d11SDimitry Andric
eraseVirtReg(Register Reg)202cfca06d7SDimitry Andric void LiveRangeEdit::eraseVirtReg(Register Reg) {
20358b69754SDimitry Andric if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
2046b943ff3SDimitry Andric LIS.removeInterval(Reg);
2056b943ff3SDimitry Andric }
2066b943ff3SDimitry Andric
foldAsLoad(LiveInterval * LI,SmallVectorImpl<MachineInstr * > & Dead)2076b943ff3SDimitry Andric bool LiveRangeEdit::foldAsLoad(LiveInterval *LI,
20863faed5bSDimitry Andric SmallVectorImpl<MachineInstr*> &Dead) {
2095ca98fd9SDimitry Andric MachineInstr *DefMI = nullptr, *UseMI = nullptr;
2106b943ff3SDimitry Andric
2116b943ff3SDimitry Andric // Check that there is a single def and a single use.
212b60736ecSDimitry Andric for (MachineOperand &MO : MRI.reg_nodbg_operands(LI->reg())) {
2136b943ff3SDimitry Andric MachineInstr *MI = MO.getParent();
2146b943ff3SDimitry Andric if (MO.isDef()) {
2156b943ff3SDimitry Andric if (DefMI && DefMI != MI)
2166b943ff3SDimitry Andric return false;
21763faed5bSDimitry Andric if (!MI->canFoldAsLoad())
2186b943ff3SDimitry Andric return false;
2196b943ff3SDimitry Andric DefMI = MI;
2206b943ff3SDimitry Andric } else if (!MO.isUndef()) {
2216b943ff3SDimitry Andric if (UseMI && UseMI != MI)
2226b943ff3SDimitry Andric return false;
2236b943ff3SDimitry Andric // FIXME: Targets don't know how to fold subreg uses.
2246b943ff3SDimitry Andric if (MO.getSubReg())
2256b943ff3SDimitry Andric return false;
2266b943ff3SDimitry Andric UseMI = MI;
2276b943ff3SDimitry Andric }
2286b943ff3SDimitry Andric }
2296b943ff3SDimitry Andric if (!DefMI || !UseMI)
2306b943ff3SDimitry Andric return false;
2316b943ff3SDimitry Andric
23258b69754SDimitry Andric // Since we're moving the DefMI load, make sure we're not extending any live
23358b69754SDimitry Andric // ranges.
23401095a5dSDimitry Andric if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
23501095a5dSDimitry Andric LIS.getInstructionIndex(*UseMI)))
23658b69754SDimitry Andric return false;
23758b69754SDimitry Andric
23858b69754SDimitry Andric // We also need to make sure it is safe to move the load.
23958b69754SDimitry Andric // Assume there are stores between DefMI and UseMI.
24058b69754SDimitry Andric bool SawStore = true;
2415a5ac124SDimitry Andric if (!DefMI->isSafeToMove(nullptr, SawStore))
24258b69754SDimitry Andric return false;
24358b69754SDimitry Andric
244eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "Try to fold single def: " << *DefMI
2456b943ff3SDimitry Andric << " into single use: " << *UseMI);
2466b943ff3SDimitry Andric
2476b943ff3SDimitry Andric SmallVector<unsigned, 8> Ops;
248b60736ecSDimitry Andric if (UseMI->readsWritesVirtualRegister(LI->reg(), &Ops).second)
2496b943ff3SDimitry Andric return false;
2506b943ff3SDimitry Andric
25101095a5dSDimitry Andric MachineInstr *FoldMI = TII.foldMemoryOperand(*UseMI, Ops, *DefMI, &LIS);
2526b943ff3SDimitry Andric if (!FoldMI)
2536b943ff3SDimitry Andric return false;
254eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << " folded: " << *FoldMI);
25501095a5dSDimitry Andric LIS.ReplaceMachineInstrInMaps(*UseMI, *FoldMI);
256cfca06d7SDimitry Andric // Update the call site info.
257cfca06d7SDimitry Andric if (UseMI->shouldUpdateCallSiteInfo())
2581d5ae102SDimitry Andric UseMI->getMF()->moveCallSiteInfo(UseMI, FoldMI);
2596b943ff3SDimitry Andric UseMI->eraseFromParent();
260b60736ecSDimitry Andric DefMI->addRegisterDead(LI->reg(), nullptr);
2616b943ff3SDimitry Andric Dead.push_back(DefMI);
26256fe8f14SDimitry Andric ++NumDCEFoldedLoads;
2636b943ff3SDimitry Andric return true;
2646b943ff3SDimitry Andric }
2656b943ff3SDimitry Andric
useIsKill(const LiveInterval & LI,const MachineOperand & MO) const26685d8b2bbSDimitry Andric bool LiveRangeEdit::useIsKill(const LiveInterval &LI,
26785d8b2bbSDimitry Andric const MachineOperand &MO) const {
26801095a5dSDimitry Andric const MachineInstr &MI = *MO.getParent();
26985d8b2bbSDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(MI).getRegSlot();
27085d8b2bbSDimitry Andric if (LI.Query(Idx).isKill())
27185d8b2bbSDimitry Andric return true;
27285d8b2bbSDimitry Andric const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
27385d8b2bbSDimitry Andric unsigned SubReg = MO.getSubReg();
274dd58ef01SDimitry Andric LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
27585d8b2bbSDimitry Andric for (const LiveInterval::SubRange &S : LI.subranges()) {
276b915e9e0SDimitry Andric if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
27785d8b2bbSDimitry Andric return true;
27885d8b2bbSDimitry Andric }
27985d8b2bbSDimitry Andric return false;
28085d8b2bbSDimitry Andric }
28185d8b2bbSDimitry Andric
282f8af5cf6SDimitry Andric /// Find all live intervals that need to shrink, then remove the instruction.
eliminateDeadDef(MachineInstr * MI,ToShrinkSet & ToShrink)2834b4fe385SDimitry Andric void LiveRangeEdit::eliminateDeadDef(MachineInstr *MI, ToShrinkSet &ToShrink) {
2846b943ff3SDimitry Andric assert(MI->allDefsAreDead() && "Def isn't really dead");
28501095a5dSDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(*MI).getRegSlot();
2866b943ff3SDimitry Andric
287f8af5cf6SDimitry Andric // Never delete a bundled instruction.
288f8af5cf6SDimitry Andric if (MI->isBundled()) {
2897fa27ce4SDimitry Andric // TODO: Handle deleting copy bundles
2907fa27ce4SDimitry Andric LLVM_DEBUG(dbgs() << "Won't delete dead bundled inst: " << Idx << '\t'
2917fa27ce4SDimitry Andric << *MI);
292f8af5cf6SDimitry Andric return;
293f8af5cf6SDimitry Andric }
2947fa27ce4SDimitry Andric
2956b943ff3SDimitry Andric // Never delete inline asm.
2966b943ff3SDimitry Andric if (MI->isInlineAsm()) {
297eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "Won't delete: " << Idx << '\t' << *MI);
298f8af5cf6SDimitry Andric return;
2996b943ff3SDimitry Andric }
3006b943ff3SDimitry Andric
3016b943ff3SDimitry Andric // Use the same criteria as DeadMachineInstructionElim.
3026b943ff3SDimitry Andric bool SawStore = false;
3035a5ac124SDimitry Andric if (!MI->isSafeToMove(nullptr, SawStore)) {
304eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "Can't delete: " << Idx << '\t' << *MI);
305f8af5cf6SDimitry Andric return;
3066b943ff3SDimitry Andric }
3076b943ff3SDimitry Andric
308eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "Deleting dead def " << Idx << '\t' << *MI);
3096b943ff3SDimitry Andric
31058b69754SDimitry Andric // Collect virtual registers to be erased after MI is gone.
3117fa27ce4SDimitry Andric SmallVector<Register, 8> RegsToErase;
31258b69754SDimitry Andric bool ReadsPhysRegs = false;
31301095a5dSDimitry Andric bool isOrigDef = false;
31408e8dd7bSDimitry Andric Register Dest;
31508e8dd7bSDimitry Andric unsigned DestSubReg;
316b915e9e0SDimitry Andric // Only optimize rematerialize case when the instruction has one def, since
317b915e9e0SDimitry Andric // otherwise we could leave some dead defs in the code. This case is
318b915e9e0SDimitry Andric // extremely rare.
319b915e9e0SDimitry Andric if (VRM && MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
320b915e9e0SDimitry Andric MI->getDesc().getNumDefs() == 1) {
32101095a5dSDimitry Andric Dest = MI->getOperand(0).getReg();
32208e8dd7bSDimitry Andric DestSubReg = MI->getOperand(0).getSubReg();
323e3b55780SDimitry Andric Register Original = VRM->getOriginal(Dest);
32401095a5dSDimitry Andric LiveInterval &OrigLI = LIS.getInterval(Original);
32501095a5dSDimitry Andric VNInfo *OrigVNI = OrigLI.getVNInfoAt(Idx);
32601095a5dSDimitry Andric // The original live-range may have been shrunk to
32701095a5dSDimitry Andric // an empty live-range. It happens when it is dead, but
32801095a5dSDimitry Andric // we still keep it around to be able to rematerialize
32901095a5dSDimitry Andric // other values that depend on it.
33001095a5dSDimitry Andric if (OrigVNI)
33101095a5dSDimitry Andric isOrigDef = SlotIndex::isSameInstr(OrigVNI->def, Idx);
33201095a5dSDimitry Andric }
33358b69754SDimitry Andric
334c0981da4SDimitry Andric bool HasLiveVRegUses = false;
335c0981da4SDimitry Andric
3366b943ff3SDimitry Andric // Check for live intervals that may shrink
337c0981da4SDimitry Andric for (const MachineOperand &MO : MI->operands()) {
338c0981da4SDimitry Andric if (!MO.isReg())
3396b943ff3SDimitry Andric continue;
340c0981da4SDimitry Andric Register Reg = MO.getReg();
341e3b55780SDimitry Andric if (!Reg.isVirtual()) {
34258b69754SDimitry Andric // Check if MI reads any unreserved physregs.
343c0981da4SDimitry Andric if (Reg && MO.readsReg() && !MRI.isReserved(Reg))
34458b69754SDimitry Andric ReadsPhysRegs = true;
345c0981da4SDimitry Andric else if (MO.isDef())
346b60736ecSDimitry Andric LIS.removePhysRegDefAt(Reg.asMCReg(), Idx);
3476b943ff3SDimitry Andric continue;
34858b69754SDimitry Andric }
3496b943ff3SDimitry Andric LiveInterval &LI = LIS.getInterval(Reg);
3506b943ff3SDimitry Andric
3516b943ff3SDimitry Andric // Shrink read registers, unless it is likely to be expensive and
3526b943ff3SDimitry Andric // unlikely to change anything. We typically don't want to shrink the
3536b943ff3SDimitry Andric // PIC base register that has lots of uses everywhere.
3546b943ff3SDimitry Andric // Always shrink COPY uses that probably come from live range splitting.
3557fa27ce4SDimitry Andric if ((MI->readsVirtualRegister(Reg) &&
3567fa27ce4SDimitry Andric (MO.isDef() || TII.isCopyInstr(*MI))) ||
357c0981da4SDimitry Andric (MO.readsReg() && (MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, MO))))
3586b943ff3SDimitry Andric ToShrink.insert(&LI);
359c0981da4SDimitry Andric else if (MO.readsReg())
360c0981da4SDimitry Andric HasLiveVRegUses = true;
3616b943ff3SDimitry Andric
3626b943ff3SDimitry Andric // Remove defined value.
363c0981da4SDimitry Andric if (MO.isDef()) {
3645a5ac124SDimitry Andric if (TheDelegate && LI.getVNInfoAt(Idx) != nullptr)
365b60736ecSDimitry Andric TheDelegate->LRE_WillShrinkVirtReg(LI.reg());
3665a5ac124SDimitry Andric LIS.removeVRegDefAt(LI, Idx);
3675a5ac124SDimitry Andric if (LI.empty())
36858b69754SDimitry Andric RegsToErase.push_back(Reg);
3696b943ff3SDimitry Andric }
3706b943ff3SDimitry Andric }
3716b943ff3SDimitry Andric
37258b69754SDimitry Andric // Currently, we don't support DCE of physreg live ranges. If MI reads
37358b69754SDimitry Andric // any unreserved physregs, don't erase the instruction, but turn it into
37458b69754SDimitry Andric // a KILL instead. This way, the physreg live ranges don't end up
37558b69754SDimitry Andric // dangling.
37658b69754SDimitry Andric // FIXME: It would be better to have something like shrinkToUses() for
37758b69754SDimitry Andric // physregs. That could potentially enable more DCE and it would free up
37858b69754SDimitry Andric // the physreg. It would not happen often, though.
37958b69754SDimitry Andric if (ReadsPhysRegs) {
38058b69754SDimitry Andric MI->setDesc(TII.get(TargetOpcode::KILL));
38158b69754SDimitry Andric // Remove all operands that aren't physregs.
38258b69754SDimitry Andric for (unsigned i = MI->getNumOperands(); i; --i) {
38358b69754SDimitry Andric const MachineOperand &MO = MI->getOperand(i-1);
384e3b55780SDimitry Andric if (MO.isReg() && MO.getReg().isPhysical())
38558b69754SDimitry Andric continue;
386145449b1SDimitry Andric MI->removeOperand(i-1);
38758b69754SDimitry Andric }
388eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "Converted physregs to:\t" << *MI);
38958b69754SDimitry Andric } else {
39001095a5dSDimitry Andric // If the dest of MI is an original reg and MI is reMaterializable,
39101095a5dSDimitry Andric // don't delete the inst. Replace the dest with a new reg, and keep
39201095a5dSDimitry Andric // the inst for remat of other siblings. The inst is saved in
39301095a5dSDimitry Andric // LiveRangeEdit::DeadRemats and will be deleted after all the
39401095a5dSDimitry Andric // allocations of the func are done.
395c0981da4SDimitry Andric // However, immediately delete instructions which have unshrunk virtual
396c0981da4SDimitry Andric // register uses. That may provoke RA to split an interval at the KILL
397c0981da4SDimitry Andric // and later result in an invalid live segment end.
398c0981da4SDimitry Andric if (isOrigDef && DeadRemats && !HasLiveVRegUses &&
3994b4fe385SDimitry Andric TII.isTriviallyReMaterializable(*MI)) {
400eb11fae6SDimitry Andric LiveInterval &NewLI = createEmptyIntervalFrom(Dest, false);
40108e8dd7bSDimitry Andric VNInfo::Allocator &Alloc = LIS.getVNInfoAllocator();
40208e8dd7bSDimitry Andric VNInfo *VNI = NewLI.getNextValue(Idx, Alloc);
40301095a5dSDimitry Andric NewLI.addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(), VNI));
40408e8dd7bSDimitry Andric
40508e8dd7bSDimitry Andric if (DestSubReg) {
40608e8dd7bSDimitry Andric const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
40708e8dd7bSDimitry Andric auto *SR = NewLI.createSubRange(
40808e8dd7bSDimitry Andric Alloc, TRI->getSubRegIndexLaneMask(DestSubReg));
40908e8dd7bSDimitry Andric SR->addSegment(LiveInterval::Segment(Idx, Idx.getDeadSlot(),
41008e8dd7bSDimitry Andric SR->getNextValue(Idx, Alloc)));
41108e8dd7bSDimitry Andric }
41208e8dd7bSDimitry Andric
41301095a5dSDimitry Andric pop_back();
414eb11fae6SDimitry Andric DeadRemats->insert(MI);
41501095a5dSDimitry Andric const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
416b60736ecSDimitry Andric MI->substituteRegister(Dest, NewLI.reg(), 0, TRI);
417ac9a064cSDimitry Andric assert(MI->registerDefIsDead(NewLI.reg(), &TRI));
41801095a5dSDimitry Andric } else {
41958b69754SDimitry Andric if (TheDelegate)
42058b69754SDimitry Andric TheDelegate->LRE_WillEraseInstruction(MI);
42101095a5dSDimitry Andric LIS.RemoveMachineInstrFromMaps(*MI);
4226b943ff3SDimitry Andric MI->eraseFromParent();
42356fe8f14SDimitry Andric ++NumDCEDeleted;
4246b943ff3SDimitry Andric }
42501095a5dSDimitry Andric }
4266b943ff3SDimitry Andric
42758b69754SDimitry Andric // Erase any virtregs that are now empty and unused. There may be <undef>
42858b69754SDimitry Andric // uses around. Keep the empty live range in that case.
42999aabd70SDimitry Andric for (Register Reg : RegsToErase) {
43058b69754SDimitry Andric if (LIS.hasInterval(Reg) && MRI.reg_nodbg_empty(Reg)) {
43158b69754SDimitry Andric ToShrink.remove(&LIS.getInterval(Reg));
43258b69754SDimitry Andric eraseVirtReg(Reg);
43358b69754SDimitry Andric }
43458b69754SDimitry Andric }
43558b69754SDimitry Andric }
43658b69754SDimitry Andric
eliminateDeadDefs(SmallVectorImpl<MachineInstr * > & Dead,ArrayRef<Register> RegsBeingSpilled)437f8af5cf6SDimitry Andric void LiveRangeEdit::eliminateDeadDefs(SmallVectorImpl<MachineInstr *> &Dead,
4384b4fe385SDimitry Andric ArrayRef<Register> RegsBeingSpilled) {
439f8af5cf6SDimitry Andric ToShrinkSet ToShrink;
440f8af5cf6SDimitry Andric
441f8af5cf6SDimitry Andric for (;;) {
442f8af5cf6SDimitry Andric // Erase all dead defs.
443f8af5cf6SDimitry Andric while (!Dead.empty())
4444b4fe385SDimitry Andric eliminateDeadDef(Dead.pop_back_val(), ToShrink);
445f8af5cf6SDimitry Andric
4466b943ff3SDimitry Andric if (ToShrink.empty())
4476b943ff3SDimitry Andric break;
4486b943ff3SDimitry Andric
4496b943ff3SDimitry Andric // Shrink just one live interval. Then delete new dead defs.
450c0981da4SDimitry Andric LiveInterval *LI = ToShrink.pop_back_val();
45163faed5bSDimitry Andric if (foldAsLoad(LI, Dead))
4526b943ff3SDimitry Andric continue;
453e3b55780SDimitry Andric Register VReg = LI->reg();
45458b69754SDimitry Andric if (TheDelegate)
455dd58ef01SDimitry Andric TheDelegate->LRE_WillShrinkVirtReg(VReg);
4566b943ff3SDimitry Andric if (!LIS.shrinkToUses(LI, &Dead))
4576b943ff3SDimitry Andric continue;
4586b943ff3SDimitry Andric
45963faed5bSDimitry Andric // Don't create new intervals for a register being spilled.
46063faed5bSDimitry Andric // The new intervals would have to be spilled anyway so its not worth it.
46163faed5bSDimitry Andric // Also they currently aren't spilled so creating them and not spilling
46263faed5bSDimitry Andric // them results in incorrect code.
463f65dcba8SDimitry Andric if (llvm::is_contained(RegsBeingSpilled, VReg))
464f65dcba8SDimitry Andric continue;
46563faed5bSDimitry Andric
4666b943ff3SDimitry Andric // LI may have been separated, create new intervals.
467f8af5cf6SDimitry Andric LI->RenumberValues();
468dd58ef01SDimitry Andric SmallVector<LiveInterval*, 8> SplitLIs;
469dd58ef01SDimitry Andric LIS.splitSeparateComponents(*LI, SplitLIs);
470dd58ef01SDimitry Andric if (!SplitLIs.empty())
47156fe8f14SDimitry Andric ++NumFracRanges;
472dd58ef01SDimitry Andric
473b60736ecSDimitry Andric Register Original = VRM ? VRM->getOriginal(VReg) : Register();
474dd58ef01SDimitry Andric for (const LiveInterval *SplitLI : SplitLIs) {
475411bd29eSDimitry Andric // If LI is an original interval that hasn't been split yet, make the new
476411bd29eSDimitry Andric // intervals their own originals instead of referring to LI. The original
477411bd29eSDimitry Andric // interval must contain all the split products, and LI doesn't.
478dd58ef01SDimitry Andric if (Original != VReg && Original != 0)
479b60736ecSDimitry Andric VRM->setIsSplitFromReg(SplitLI->reg(), Original);
48058b69754SDimitry Andric if (TheDelegate)
481b60736ecSDimitry Andric TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg(), VReg);
4826b943ff3SDimitry Andric }
4836b943ff3SDimitry Andric }
4846b943ff3SDimitry Andric }
4856b943ff3SDimitry Andric
486f8af5cf6SDimitry Andric // Keep track of new virtual registers created via
487f8af5cf6SDimitry Andric // MachineRegisterInfo::createVirtualRegister.
488f8af5cf6SDimitry Andric void
MRI_NoteNewVirtualRegister(Register VReg)489cfca06d7SDimitry Andric LiveRangeEdit::MRI_NoteNewVirtualRegister(Register VReg) {
490f8af5cf6SDimitry Andric if (VRM)
491f8af5cf6SDimitry Andric VRM->grow();
492f8af5cf6SDimitry Andric
493f8af5cf6SDimitry Andric NewRegs.push_back(VReg);
494f8af5cf6SDimitry Andric }
495f8af5cf6SDimitry Andric
calculateRegClassAndHint(MachineFunction & MF,VirtRegAuxInfo & VRAI)496344a3780SDimitry Andric void LiveRangeEdit::calculateRegClassAndHint(MachineFunction &MF,
497344a3780SDimitry Andric VirtRegAuxInfo &VRAI) {
498f8af5cf6SDimitry Andric for (unsigned I = 0, Size = size(); I < Size; ++I) {
499f8af5cf6SDimitry Andric LiveInterval &LI = LIS.getInterval(get(I));
500b60736ecSDimitry Andric if (MRI.recomputeRegClass(LI.reg()))
501eb11fae6SDimitry Andric LLVM_DEBUG({
50267c32a98SDimitry Andric const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
503b60736ecSDimitry Andric dbgs() << "Inflated " << printReg(LI.reg()) << " to "
504b60736ecSDimitry Andric << TRI->getRegClassName(MRI.getRegClass(LI.reg())) << '\n';
50567c32a98SDimitry Andric });
506f8af5cf6SDimitry Andric VRAI.calculateSpillWeightAndHint(LI);
5076b943ff3SDimitry Andric }
5086b943ff3SDimitry Andric }
509