25 #define DEBUG_TYPE "regalloc" 27 STATISTIC(NumDCEDeleted,
"Number of instructions deleted by DCE");
28 STATISTIC(NumDCEFoldedLoads,
"Number of single use loads folded after DCE");
29 STATISTIC(NumFracRanges,
"Number of live ranges fractured by DCE");
31 void LiveRangeEdit::Delegate::anchor() { }
33 LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(
unsigned OldReg,
34 bool createSubRanges) {
35 Register VReg =
MRI.createVirtualRegister(
MRI.getRegClass(OldReg));
37 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
40 if (Parent && !Parent->isSpillable())
42 if (createSubRanges) {
55 Register VReg =
MRI.createVirtualRegister(
MRI.getRegClass(OldReg));
57 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
65 if (Parent && !Parent->isSpillable())
66 LIS.getInterval(VReg).markNotSpillable();
73 assert(DefMI &&
"Missing instruction");
74 ScannedRemattable =
true;
75 if (!
TII.isTriviallyReMaterializable(*DefMI, aa))
77 Remattable.insert(VNI);
85 unsigned Original = VRM->getOriginal(
getReg());
95 ScannedRemattable =
true;
99 if (!ScannedRemattable)
101 return !Remattable.empty();
106 bool LiveRangeEdit::allUsesAvailableAt(
const MachineInstr *OrigMI,
142 assert(ScannedRemattable &&
"Call anyRematerializable first");
145 if (!Remattable.count(OrigVNI))
150 assert(RM.
OrigMI &&
"No defining instruction for remattable value");
151 DefIdx = LIS.getInstructionIndex(*RM.
OrigMI);
154 if (cheapAsAMove && !
TII.isAsCheapAsAMove(*RM.
OrigMI))
158 if (!allUsesAvailableAt(RM.
OrigMI, DefIdx, UseIdx))
171 TII.reMaterialize(MBB, MI, DestReg, 0, *RM.
OrigMI, tri);
175 (*--
MI).getOperand(0).setIsDead(
false);
177 return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
181 if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
182 LIS.removeInterval(Reg);
193 if (DefMI && DefMI != MI)
198 }
else if (!MO.isUndef()) {
207 if (!DefMI || !
UseMI)
212 if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
213 LIS.getInstructionIndex(*
UseMI)))
218 bool SawStore =
true;
223 <<
" into single use: " << *
UseMI);
233 LIS.ReplaceMachineInstrInMaps(*
UseMI, *FoldMI);
253 if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
276 bool SawStore =
false;
286 bool ReadsPhysRegs =
false;
287 bool isOrigDef =
false;
295 unsigned Original = VRM->getOriginal(Dest);
314 if (Reg && MOI->readsReg() && !
MRI.isReserved(Reg))
315 ReadsPhysRegs =
true;
316 else if (MOI->isDef())
317 LIS.removePhysRegDefAt(Reg, Idx);
327 (MOI->readsReg() && (
MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI))))
333 TheDelegate->LRE_WillShrinkVirtReg(LI.
reg);
334 LIS.removeVRegDefAt(LI, Idx);
363 if (isOrigDef && DeadRemats &&
TII.isTriviallyReMaterializable(*MI, AA)) {
364 LiveInterval &NewLI = createEmptyIntervalFrom(Dest,
false);
368 DeadRemats->insert(MI);
374 TheDelegate->LRE_WillEraseInstruction(MI);
375 LIS.RemoveMachineInstrFromMaps(*MI);
383 for (
unsigned i = 0,
e = RegsToErase.
size(); i !=
e; ++i) {
384 unsigned Reg = RegsToErase[i];
385 if (LIS.hasInterval(Reg) &&
MRI.reg_nodbg_empty(Reg)) {
386 ToShrink.
remove(&LIS.getInterval(Reg));
399 while (!Dead.
empty())
402 if (ToShrink.
empty())
408 if (foldAsLoad(LI, Dead))
410 unsigned VReg = LI->
reg;
412 TheDelegate->LRE_WillShrinkVirtReg(VReg);
413 if (!LIS.shrinkToUses(LI, &Dead))
420 bool BeingSpilled =
false;
421 for (
unsigned i = 0,
e = RegsBeingSpilled.
size(); i !=
e; ++i) {
422 if (VReg == RegsBeingSpilled[i]) {
428 if (BeingSpilled)
continue;
433 LIS.splitSeparateComponents(*LI, SplitLIs);
434 if (!SplitLIs.
empty())
437 unsigned Original = VRM ? VRM->getOriginal(VReg) : 0;
442 if (Original != VReg && Original != 0)
443 VRM->setIsSplitFromReg(SplitLI->reg, Original);
445 TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg, VReg);
453 LiveRangeEdit::MRI_NoteNewVirtualRegister(
unsigned VReg)
458 NewRegs.push_back(VReg);
468 if (
MRI.recomputeRegClass(LI.
reg))
void RenumberValues()
RenumberValues - Renumber all values in order of appearance and remove unused values.
mop_iterator operands_end()
Calculate auxiliary information for a virtual register such as its spill weight and allocation hint...
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
bool isCall(QueryType Type=AnyInBundle) const
SlotIndex def
The index of the defining instruction.
const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
This class represents lattice values for constants.
bool anyRematerializable(AliasAnalysis *)
anyRematerializable - Return true if any parent values may be rematerializable.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
void push_back(const T &Elt)
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
LiveInterval - This class represents the liveness of a register, or stack slot.
void eliminateDeadDefs(SmallVectorImpl< MachineInstr *> &Dead, ArrayRef< unsigned > RegsBeingSpilled=None, AliasAnalysis *AA=nullptr)
eliminateDeadDefs - Try to delete machine instructions that are now dead (allDefsAreDead returns true...
bool addRegisterDead(Register Reg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI defined a register without a use.
unsigned getSubReg() const
bool readsVirtualRegister(Register Reg) const
Return true if the MachineInstr reads the specified virtual register.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
A live range for subregisters.
This represents a simple continuous liveness interval for a value.
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
void setIsDead(bool Val=true)
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.
VNInfo - Value Number Information.
const T & back() const
Return the last element of the SetVector.
bool checkRematerializable(VNInfo *VNI, const MachineInstr *DefMI, AliasAnalysis *)
checkRematerializable - Manually add VNI to the list of rematerializable values if DefMI may be remat...
const char * getRegClassName(const TargetRegisterClass *Class) const
Returns the name of the register class.
const HexagonInstrInfo * TII
void substituteRegister(Register FromReg, Register ToReg, unsigned SubIdx, const TargetRegisterInfo &RegInfo)
Replace all occurrences of FromReg with ToReg:SubIdx, properly composing subreg indices where necessa...
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...
unsigned getNumOperands() const
Retuns the total number of operands.
iterator_range< subrange_iterator > subranges()
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
bool remove(const value_type &X)
Remove an item from the set vector.
void pop_back()
Remove the last element of the SetVector.
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register. ...
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
bool insert(const value_type &X)
Insert a new element into the SetVector.
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def...
iterator addSegment(Segment S)
Add the specified Segment to this range, merging segments as appropriate.
bool canRematerializeAt(Remat &RM, VNInfo *OrigVNI, SlotIndex UseIdx, bool cheapAsAMove)
canRematerializeAt - Determine if ParentVNI can be rematerialized at UseIdx.
void pop_back()
pop_back - It allows LiveRangeEdit users to drop new registers.
bool isKill() const
Return true if the live-in value is killed by this instruction.
bool allDefsAreDead() const
Return true if all the defs of this instruction are dead.
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
unsigned createFrom(unsigned OldReg)
createFrom - Create a new virtual register based on OldReg.
SubRange * createSubRange(BumpPtrAllocator &Allocator, LaneBitmask LaneMask)
Creates a new empty subregister live range.
unsigned const MachineRegisterInfo * MRI
void grow(size_t MinSize=0)
Grow the allocated memory (without initializing new elements), doubling the size of the allocated mem...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Allocate memory in an ever growing pool, as if by bump-pointer.
bool isBundled() const
Return true if this instruction part of a bundle.
MachineInstrBuilder & UseMI
size_t size() const
size - Get the array size.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
LiveInterval & getParent() const
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
static bool isSameInstr(SlotIndex A, SlotIndex B)
isSameInstr - Return true if A and B refer to the same instruction.
MachineOperand class - Representation of each machine instruction operand.
MachineInstrBuilder MachineInstrBuilder & DefMI
void calculateSpillWeightAndHint(LiveInterval &li)
(re)compute li's spill weight and allocation hint.
LLVM_NODISCARD T pop_back_val()
LaneBitmask getSubRegIndexLaneMask(unsigned SubIdx) const
Return a bitmask representing the parts of a register that are covered by SubIdx. ...
void markNotSpillable()
markNotSpillable - Mark interval as not spillable
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Representation of each machine instruction.
SlotIndex rematerializeAt(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, unsigned DestReg, const Remat &RM, const TargetRegisterInfo &, bool Late=false)
rematerializeAt - Rematerialize RM.ParentVNI into DestReg by inserting an instruction into MBB before...
LLVM_NODISCARD bool empty() const
bool canFoldAsLoad(QueryType Type=IgnoreBundle) const
Return true for instructions that can be folded as memory operands in other instructions.
bool empty() const
Determine if the SetVector is empty or not.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Remat - Information needed to rematerialize at a specific location.
VNInfo * getNextValue(SlotIndex def, VNInfo::Allocator &VNInfoAllocator)
getNextValue - Create a new value number and return it.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isSafeToMove(AAResults *AA, bool &SawStore) const
Return true if it is safe to move this instruction.
mop_iterator operands_begin()
A vector that has set insertion semantics.
void moveCallSiteInfo(const MachineInstr *Old, const MachineInstr *New)
Following functions update call site info.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
void calculateRegClassAndHint(MachineFunction &, const MachineLoopInfo &, const MachineBlockFrequencyInfo &)
calculateRegClassAndHint - Recompute register class and hint for each new register.
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
void eraseVirtReg(unsigned Reg)
eraseVirtReg - Notify the delegate that Reg is no longer in use, and try to erase it from LIS...
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
SlotIndex - An opaque wrapper around machine indexes.
Wrapper class representing virtual and physical registers.