26 #define DEBUG_TYPE "regalloc" 28 STATISTIC(NumDCEDeleted,
"Number of instructions deleted by DCE");
29 STATISTIC(NumDCEFoldedLoads,
"Number of single use loads folded after DCE");
30 STATISTIC(NumFracRanges,
"Number of live ranges fractured by DCE");
32 void LiveRangeEdit::Delegate::anchor() { }
34 LiveInterval &LiveRangeEdit::createEmptyIntervalFrom(
unsigned OldReg,
35 bool createSubRanges) {
36 unsigned VReg =
MRI.createVirtualRegister(
MRI.getRegClass(OldReg));
38 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
41 if (Parent && !Parent->isSpillable())
43 if (createSubRanges) {
56 unsigned VReg =
MRI.createVirtualRegister(
MRI.getRegClass(OldReg));
58 VRM->setIsSplitFromReg(VReg, VRM->getOriginal(OldReg));
66 if (Parent && !Parent->isSpillable())
67 LIS.getInterval(VReg).markNotSpillable();
74 assert(DefMI &&
"Missing instruction");
75 ScannedRemattable =
true;
76 if (!
TII.isTriviallyReMaterializable(*DefMI, aa))
78 Remattable.insert(VNI);
86 unsigned Original = VRM->getOriginal(
getReg());
96 ScannedRemattable =
true;
100 if (!ScannedRemattable)
102 return !Remattable.empty();
107 bool LiveRangeEdit::allUsesAvailableAt(
const MachineInstr *OrigMI,
143 assert(ScannedRemattable &&
"Call anyRematerializable first");
146 if (!Remattable.count(OrigVNI))
151 assert(RM.
OrigMI &&
"No defining instruction for remattable value");
152 DefIdx = LIS.getInstructionIndex(*RM.
OrigMI);
155 if (cheapAsAMove && !
TII.isAsCheapAsAMove(*RM.
OrigMI))
159 if (!allUsesAvailableAt(RM.
OrigMI, DefIdx, UseIdx))
172 TII.reMaterialize(MBB, MI, DestReg, 0, *RM.
OrigMI, tri);
176 (*--
MI).getOperand(0).setIsDead(
false);
178 return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
182 if (TheDelegate && TheDelegate->LRE_CanEraseVirtReg(Reg))
183 LIS.removeInterval(Reg);
194 if (DefMI && DefMI != MI)
199 }
else if (!MO.isUndef()) {
208 if (!DefMI || !
UseMI)
213 if (!allUsesAvailableAt(DefMI, LIS.getInstructionIndex(*DefMI),
214 LIS.getInstructionIndex(*
UseMI)))
219 bool SawStore =
true;
224 <<
" into single use: " << *
UseMI);
234 LIS.ReplaceMachineInstrInMaps(*
UseMI, *FoldMI);
252 if ((S.LaneMask & LaneMask).any() && S.Query(Idx).isKill())
275 bool SawStore =
false;
285 bool ReadsPhysRegs =
false;
286 bool isOrigDef =
false;
294 unsigned Original = VRM->getOriginal(Dest);
310 unsigned Reg = MOI->getReg();
313 if (Reg && MOI->readsReg() && !
MRI.isReserved(Reg))
314 ReadsPhysRegs =
true;
315 else if (MOI->isDef())
316 LIS.removePhysRegDefAt(Reg, Idx);
326 (MOI->readsReg() && (
MRI.hasOneNonDBGUse(Reg) || useIsKill(LI, *MOI))))
332 TheDelegate->LRE_WillShrinkVirtReg(LI.
reg);
333 LIS.removeVRegDefAt(LI, Idx);
362 if (isOrigDef && DeadRemats &&
TII.isTriviallyReMaterializable(*MI, AA)) {
363 LiveInterval &NewLI = createEmptyIntervalFrom(Dest,
false);
367 DeadRemats->insert(MI);
373 TheDelegate->LRE_WillEraseInstruction(MI);
374 LIS.RemoveMachineInstrFromMaps(*MI);
382 for (
unsigned i = 0, e = RegsToErase.
size(); i != e; ++i) {
383 unsigned Reg = RegsToErase[i];
384 if (LIS.hasInterval(Reg) &&
MRI.reg_nodbg_empty(Reg)) {
385 ToShrink.
remove(&LIS.getInterval(Reg));
398 while (!Dead.
empty())
401 if (ToShrink.
empty())
407 if (foldAsLoad(LI, Dead))
409 unsigned VReg = LI->
reg;
411 TheDelegate->LRE_WillShrinkVirtReg(VReg);
412 if (!LIS.shrinkToUses(LI, &Dead))
419 bool BeingSpilled =
false;
420 for (
unsigned i = 0, e = RegsBeingSpilled.
size(); i != e; ++i) {
421 if (VReg == RegsBeingSpilled[i]) {
427 if (BeingSpilled)
continue;
432 LIS.splitSeparateComponents(*LI, SplitLIs);
433 if (!SplitLIs.
empty())
436 unsigned Original = VRM ? VRM->getOriginal(VReg) : 0;
441 if (Original != VReg && Original != 0)
442 VRM->setIsSplitFromReg(SplitLI->reg, Original);
444 TheDelegate->LRE_DidCloneVirtReg(SplitLI->reg, VReg);
452 LiveRangeEdit::MRI_NoteNewVirtualRegister(
unsigned VReg)
457 NewRegs.push_back(VReg);
467 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.
SlotIndex def
The index of the defining instruction.
This class represents lattice values for constants.
bool anyRematerializable(AliasAnalysis *)
anyRematerializable - Return true if any parent values may be rematerializable.
bool addRegisterDead(unsigned Reg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI defined a register without a use.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
void push_back(const T &Elt)
LiveInterval - This class represents the liveness of a register, or stack slot.
unsigned getReg() const
getReg - Returns the register number.
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...
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
unsigned getSubReg() const
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)
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
unsigned getNumOperands() const
Retuns the total number of operands.
Printable printReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
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
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 readsVirtualRegister(unsigned Reg) const
Return true if the MachineInstr reads the specified virtual register.
bool isBundled() const
Return true if this instruction part of a bundle.
MachineInstrBuilder & UseMI
size_t size() const
size - Get the array size.
void grow(size_t MinSize=0)
Grow the allocated memory (without initializing new elements), doubling the size of the allocated mem...
std::pair< bool, bool > readsWritesVirtualRegister(unsigned Reg, SmallVectorImpl< unsigned > *Ops=nullptr) const
Return a pair of bools (reads, writes) indicating if this instruction reads or writes Reg...
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...
void substituteRegister(unsigned FromReg, unsigned ToReg, unsigned SubIdx, const TargetRegisterInfo &RegInfo)
Replace all occurrences of FromReg with ToReg:SubIdx, properly composing subreg indices where necessa...
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.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
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())
mop_iterator operands_begin()
A vector that has set insertion semantics.
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...
const MachineOperand & getOperand(unsigned i) const
SlotIndex - An opaque wrapper around machine indexes.
bool isSafeToMove(AliasAnalysis *AA, bool &SawStore) const
Return true if it is safe to move this instruction.