25#define DEBUG_TYPE "regalloc"
27STATISTIC(NumDCEDeleted,
"Number of instructions deleted by DCE");
28STATISTIC(NumDCEFoldedLoads,
"Number of single use loads folded after DCE");
29STATISTIC(NumFracRanges,
"Number of live ranges fractured by DCE");
30STATISTIC(NumReMaterialization,
"Number of instructions rematerialized");
32void LiveRangeEdit::Delegate::anchor() { }
34LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(
Register OldReg,
35 bool createSubRanges) {
36 Register VReg = MRI.cloneVirtualRegister(OldReg);
38 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
40 LiveInterval &LI = LIS.createEmptyInterval(VReg);
41 if (Parent && !Parent->isSpillable())
43 if (createSubRanges) {
47 LiveInterval &OldLI = LIS.getInterval(OldReg);
49 for (LiveInterval::SubRange &S : OldLI.
subranges())
56 Register VReg = MRI.cloneVirtualRegister(OldReg);
58 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
66 if (Parent && !Parent->isSpillable())
67 LIS.getInterval(VReg).markNotSpillable();
74 ScannedRemattable =
true;
75 if (!TII.isTriviallyReMaterializable(*
DefMI))
77 Remattable.insert(VNI);
81void LiveRangeEdit::scanRemattable() {
95 ScannedRemattable =
true;
99 if (!ScannedRemattable)
101 return !Remattable.empty();
110 UseIdx = std::max(UseIdx, UseIdx.
getRegSlot(
true));
112 if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
117 if (MO.getReg().isPhysical()) {
118 if (MRI.isConstantPhysReg(MO.getReg()) || TII.isIgnorableUse(MO))
140 unsigned SubReg = MO.getSubReg();
142 : MRI.getMaxLaneMaskForVReg(MO.getReg());
144 if ((SR.LaneMask & LM).none())
146 if (!SR.liveAt(UseIdx))
160 assert(ScannedRemattable &&
"Call anyRematerializable first");
163 if (!Remattable.count(OrigVNI))
168 assert(RM.OrigMI &&
"No defining instruction for remattable value");
169 DefIdx = LIS.getInstructionIndex(*RM.OrigMI);
182 bool Late,
unsigned SubIdx,
184 assert(RM.OrigMI &&
"Invalid remat");
185 TII.reMaterialize(
MBB,
MI, DestReg, SubIdx, *RM.OrigMI, tri);
189 (*--
MI).clearRegisterDeads(DestReg);
190 Rematted.insert(RM.ParentVNI);
191 ++NumReMaterialization;
193 bool EarlyClobber =
MI->getOperand(0).isEarlyClobber();
195 return LIS.ReplaceMachineInstrInMaps(*ReplaceIndexMI, *
MI)
196 .getRegSlot(EarlyClobber);
197 return LIS.getSlotIndexes()->insertMachineInstrInMaps(*
MI, Late).getRegSlot(
202 if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
203 LIS.removeInterval(Reg);
216 if (!
MI->canFoldAsLoad())
219 }
else if (!MO.isUndef()) {
234 LIS.getInstructionIndex(*
UseMI)))
239 bool SawStore =
true;
244 <<
" into single use: " << *
UseMI);
246 SmallVector<unsigned, 8>
Ops;
250 MachineInstr *FoldMI = TII.foldMemoryOperand(*
UseMI,
Ops, *
DefMI, &LIS);
254 LIS.ReplaceMachineInstrInMaps(*
UseMI, *FoldMI);
265bool LiveRangeEdit::useIsKill(
const LiveInterval &LI,
266 const MachineOperand &MO)
const {
268 SlotIndex Idx = LIS.getInstructionIndex(
MI).getRegSlot();
271 const TargetRegisterInfo &
TRI = *MRI.getTargetRegisterInfo();
273 LaneBitmask LaneMask =
TRI.getSubRegIndexLaneMask(
SubReg);
274 for (
const LiveInterval::SubRange &S : LI.
subranges()) {
275 if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
282void LiveRangeEdit::eliminateDeadDef(MachineInstr *
MI, ToShrinkSet &ToShrink) {
283 assert(
MI->allDefsAreDead() &&
"Def isn't really dead");
284 SlotIndex Idx = LIS.getInstructionIndex(*MI).
getRegSlot();
287 if (
MI->isBundled()) {
289 LLVM_DEBUG(
dbgs() <<
"Won't delete dead bundled inst: " << Idx <<
'\t'
295 if (
MI->isInlineAsm()) {
301 bool SawStore =
false;
302 if (!
MI->isSafeToMove(SawStore)) {
311 bool ReadsPhysRegs =
false;
312 bool isOrigDef =
false;
318 if (VRM &&
MI->getOperand(0).isReg() &&
MI->getOperand(0).isDef() &&
319 MI->getDesc().getNumDefs() == 1) {
320 Dest =
MI->getOperand(0).getReg();
321 DestSubReg =
MI->getOperand(0).getSubReg();
322 Register Original = VRM->getOriginal(Dest);
323 LiveInterval &OrigLI = LIS.getInterval(Original);
333 bool HasLiveVRegUses =
false;
336 for (
const MachineOperand &MO :
MI->operands()) {
343 ReadsPhysRegs =
true;
348 LiveInterval &LI = LIS.getInterval(
Reg);
354 if ((
MI->readsVirtualRegister(
Reg) &&
355 (MO.
isDef() || TII.isCopyInstr(*
MI))) ||
356 (MO.
readsReg() && (MRI.hasOneNonDBGUse(
Reg) || useIsKill(LI, MO))))
357 ToShrink.insert(&LI);
359 HasLiveVRegUses =
true;
364 TheDelegate->LRE_WillShrinkVirtReg(LI.
reg());
365 LIS.removeVRegDefAt(LI, Idx);
379 MI->setDesc(TII.get(TargetOpcode::KILL));
381 for (
unsigned i =
MI->getNumOperands(); i; --i) {
382 const MachineOperand &MO =
MI->getOperand(i-1);
385 MI->removeOperand(i-1);
387 MI->dropMemRefs(*
MI->getMF());
398 if (isOrigDef && DeadRemats && !HasLiveVRegUses &&
399 TII.isTriviallyReMaterializable(*
MI)) {
400 LiveInterval &NewLI = createEmptyIntervalFrom(Dest,
false);
406 const TargetRegisterInfo *
TRI = MRI.getTargetRegisterInfo();
408 Alloc,
TRI->getSubRegIndexLaneMask(DestSubReg));
410 SR->getNextValue(Idx,
Alloc)));
414 DeadRemats->insert(
MI);
415 const TargetRegisterInfo &
TRI = *MRI.getTargetRegisterInfo();
416 MI->substituteRegister(Dest, NewLI.
reg(), 0,
TRI);
420 TheDelegate->LRE_WillEraseInstruction(
MI);
421 LIS.RemoveMachineInstrFromMaps(*
MI);
422 MI->eraseFromParent();
430 if (LIS.hasInterval(
Reg) && MRI.reg_nodbg_empty(
Reg)) {
431 ToShrink.remove(&LIS.getInterval(
Reg));
439 ToShrinkSet ToShrink;
443 while (!Dead.empty())
444 eliminateDeadDef(Dead.pop_back_val(), ToShrink);
446 if (ToShrink.
empty())
451 if (foldAsLoad(LI, Dead))
455 TheDelegate->LRE_WillShrinkVirtReg(VReg);
456 if (!LIS.shrinkToUses(LI, &Dead))
469 LIS.splitSeparateComponents(*LI, SplitLIs);
470 if (!SplitLIs.
empty())
478 if (Original != VReg && Original != 0)
479 VRM->setIsSplitFromReg(SplitLI->reg(), Original);
481 TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg(), VReg);
489LiveRangeEdit::MRI_NoteNewVirtualRegister(
Register VReg) {
493 NewRegs.push_back(VReg);
500 if (MRI.recomputeRegClass(LI.
reg()))
504 <<
TRI->getRegClassName(MRI.getRegClass(LI.
reg())) <<
'\n';
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
Register const TargetRegisterInfo * TRI
Promote Memory to Register
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
A live range for subregisters.
LiveInterval - This class represents the liveness of a register, or stack slot.
void markNotSpillable()
markNotSpillable - Mark interval as not spillable
bool hasSubRanges() const
Returns true if subregister liveness information is available.
iterator_range< subrange_iterator > subranges()
SubRange * createSubRange(BumpPtrAllocator &Allocator, LaneBitmask LaneMask)
Creates a new empty subregister live range.
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction associated with the given index.
LiveInterval & getInterval(Register Reg)
bool isKill() const
Return true if the live-in value is killed by this instruction.
void eraseVirtReg(Register Reg)
eraseVirtReg - Notify the delegate that Reg is no longer in use, and try to erase it from LIS.
bool canRematerializeAt(Remat &RM, VNInfo *OrigVNI, SlotIndex UseIdx)
canRematerializeAt - Determine if ParentVNI can be rematerialized at UseIdx.
Register get(unsigned idx) const
Register createFrom(Register OldReg)
createFrom - Create a new virtual register based on OldReg.
void calculateRegClassAndHint(MachineFunction &, VirtRegAuxInfo &)
calculateRegClassAndHint - Recompute register class and hint for each new register.
const LiveInterval & getParent() const
SlotIndex rematerializeAt(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, Register DestReg, const Remat &RM, const TargetRegisterInfo &, bool Late=false, unsigned SubIdx=0, MachineInstr *ReplaceIndexMI=nullptr)
rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an instruction into MBB before...
bool allUsesAvailableAt(const MachineInstr *OrigMI, SlotIndex OrigIdx, SlotIndex UseIdx) const
allUsesAvailableAt - Return true if all registers used by OrigMI at OrigIdx are also available with t...
void eliminateDeadDefs(SmallVectorImpl< MachineInstr * > &Dead, ArrayRef< Register > RegsBeingSpilled={})
eliminateDeadDefs - Try to delete machine instructions that are now dead (allDefsAreDead returns true...
void pop_back()
pop_back - It allows LiveRangeEdit users to drop new registers.
bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI)
checkRematerializable - Manually add VNI to the list of rematerializable values if DefMI may be remat...
bool anyRematerializable()
anyRematerializable - Return true if any parent values may be rematerializable.
LLVM_ABI iterator addSegment(Segment S)
Add the specified Segment to this range, merging segments as appropriate.
LLVM_ABI void RenumberValues()
RenumberValues - Renumber all values in order of appearance and remove unused values.
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
VNInfo * getNextValue(SlotIndex Def, VNInfo::Allocator &VNInfoAllocator)
getNextValue - Create a new value number and return it.
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
MachineInstrBundleIterator< MachineInstr > iterator
void moveAdditionalCallInfo(const MachineInstr *Old, const MachineInstr *New)
Move the call site info from Old to \New call site info.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Representation of each machine instruction.
LLVM_ABI std::pair< bool, bool > readsWritesVirtualRegister(Register Reg, SmallVectorImpl< unsigned > *Ops=nullptr) const
Return a pair of bools (reads, writes) indicating if this instruction reads or writes Reg.
LLVM_ABI bool isSafeToMove(bool &SawStore) const
Return true if it is safe to move this instruction.
LLVM_ABI const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
LLVM_ABI void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
LLVM_ABI bool shouldUpdateAdditionalCallInfo() const
Return true if copying, moving, or erasing this instruction requires updating additional call info (s...
LLVM_ABI bool addRegisterDead(Register Reg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI defined a register without a use.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
Register getReg() const
getReg - Returns the register number.
Wrapper class representing virtual and physical registers.
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
bool empty() const
Determine if the SetVector is empty or not.
value_type pop_back_val()
SlotIndex - An opaque wrapper around machine indexes.
static bool isSameInstr(SlotIndex A, SlotIndex B)
isSameInstr - Return true if A and B refer to the same instruction.
SlotIndex getDeadSlot() const
Returns the dead def kill slot for the current instruction.
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
VNInfo - Value Number Information.
BumpPtrAllocator Allocator
SlotIndex def
The index of the defining instruction.
Calculate auxiliary information for a virtual register such as its spill weight and allocation hint.
void calculateSpillWeightAndHint(LiveInterval &LI)
(re)compute li's spill weight and allocation hint.
Register getOriginal(Register VirtReg) const
getOriginal - Return the original virtual register that VirtReg descends from through splitting.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
constexpr bool none() const
Remat - Information needed to rematerialize at a specific location.