32#define DEBUG_TYPE "calcspillweights"
34bool VirtRegAuxInfo::getCachedOptimizeForSize() {
35 if (!CachedOptForSize.has_value())
37 return *CachedOptForSize;
41 LLVM_DEBUG(
dbgs() <<
"********** Compute Spill Weights **********\n"
42 <<
"********** Function: " << MF.getName() <<
'\n');
59 if (
MI->getOperand(0).getReg() == Reg) {
60 Sub =
MI->getOperand(0).getSubReg();
61 HReg =
MI->getOperand(1).getReg();
62 HSub =
MI->getOperand(1).getSubReg();
64 Sub =
MI->getOperand(1).getSubReg();
65 HReg =
MI->getOperand(0).getReg();
66 HSub =
MI->getOperand(0).getSubReg();
82 return TRI.getMatchingSuperReg(CopiedPReg,
Sub, RC);
94 Register Original = VRM.getOriginal(Reg);
99 const VNInfo *OrigVNI = VNI;
106 assert(
MI &&
"Dead valno in interval");
111 while (
TII.isFullCopyInstr(*
MI)) {
113 if (
MI->getOperand(0).getReg() != Reg)
117 Reg =
MI->getOperand(1).getReg();
121 if (!Reg.isVirtual() || VRM.getOriginal(Reg) != Original)
128 assert(VNI &&
"Copy from non-existing value");
131 MI = LIS.getInstructionFromIndex(VNI->
def);
132 assert(
MI &&
"Dead valno in interval");
135 if (!
TII.isReMaterializable(*
MI))
138 VNIDefs[OrigVNI->
id] =
MI;
146 SlotIndex UseIdx = LIS.getInstructionIndex(*MO.getParent());
148 assert(Def &&
"Use with no def");
162 UseIdx = std::max(UseIdx, UseIdx.
getRegSlot(
true));
164 if (!MO.isReg() || !MO.getReg() || !MO.readsReg())
169 if (MO.getReg().isPhysical()) {
171 TII.isIgnorableUse(*
MI,
MI->getOperandNo(&MO)))
197 if ((SR.LaneMask & LM).none())
199 if (!SR.liveAt(UseIdx))
211bool VirtRegAuxInfo::isLiveAtStatepointVarArg(
LiveInterval &LI) {
214 MachineInstr *MI = MO.getParent();
215 if (MI->getOpcode() != TargetOpcode::STATEPOINT)
217 return StatepointOpers(MI).getVarIdx() <= MO.getOperandNo();
233 if (
MI->isInlineAsm() &&
MI->mayFoldInlineAsmRegOp(
MI->getOperandNo(&MO)))
245 float TotalWeight = 0;
246 unsigned NumInstr = 0;
253 Register Original = VRM.getOriginal(Reg);
254 const LiveInterval &OrigInt = LIS.getInterval(Original);
270 CopyHint(
Register R,
float W,
bool IsCSR)
271 :
Reg(R), Weight(W), IsCSR(IsCSR) {}
272 bool operator<(
const CopyHint &Rhs)
const {
274 if (Reg.isPhysical() != Rhs.Reg.isPhysical())
275 return Reg.isPhysical();
276 if (Weight != Rhs.Weight)
277 return (Weight > Rhs.Weight);
279 if (Reg.isPhysical() && IsCSR != Rhs.IsCSR)
281 return Reg.id() < Rhs.Reg.id();
285 bool IsExiting =
false;
294 bool identityCopy =
false;
295 auto DestSrc =
TII.isCopyInstr(*
MI);
299 identityCopy = DestRegOp->
getReg() == SrcRegOp->
getReg() &&
303 if (identityCopy ||
MI->isImplicitDef())
310 if (
TII.isUnspillableTerminator(
MI) &&
311 MI->definesRegister(LI.
reg(),
nullptr)) {
320 if (
MI->getParent() !=
MBB) {
321 MBB =
MI->getParent();
328 std::tie(Reads, Writes) =
MI->readsWritesVirtualRegister(LI.
reg());
330 getCachedOptimizeForSize());
333 if (Writes && IsExiting && LIS.isLiveOutOfMBB(LI,
MBB))
336 TotalWeight += Weight;
340 if (!
TII.isCopyInstr(*
MI))
344 Hint[HintReg] += Weight;
350 if (TargetHint.first == 0 && TargetHint.second)
356 for (
const auto &[Reg, Weight] : Hint) {
360 Reg.isPhysical() ?
TRI.isCalleeSavedPhysReg(Reg, MF) :
false);
363 for (
const auto &[Reg,
_, __] : RegHints)
367 TotalWeight *= 1.01F;
398 TotalWeight *=
TRI.getSpillWeightScaleFactor(RC);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool canMemFoldInlineAsm(LiveInterval &LI, const MachineRegisterInfo &MRI)
const HexagonInstrInfo * TII
Register const TargetRegisterInfo * TRI
Promote Memory to Register
This file defines the SmallPtrSet class.
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 isSpillable() const
isSpillable - Can this interval be spilled?
bool hasSubRanges() const
Returns true if subregister liveness information is available.
LLVM_ABI unsigned getSize() const
getSize - Returns the sum of sizes of all the LiveRange's.
iterator_range< subrange_iterator > subranges()
void setWeight(float Value)
static LLVM_ABI float getSpillWeight(bool isDef, bool isUse, const MachineBlockFrequencyInfo *MBFI, const MachineInstr &MI, ProfileSummaryInfo *PSI=nullptr)
Calculate the spill weight to assign to a single instruction.
Result of a LiveRange query.
VNInfo * valueIn() const
Return the value that is live-in to the instruction.
LLVM_ABI bool isLiveAtIndexes(ArrayRef< SlotIndex > Slots) const
bool isZeroLength(SlotIndexes *Indexes) const
Returns true if the live range is zero length, i.e.
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
VNInfoList::const_iterator const_vni_iterator
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
bool isLoopExiting(const BlockT *BB) const
True if terminator in the block can branch to another block that is outside of the current loop.
Represents a single loop in the control flow graph.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
MCRegister getSubReg(MCRegister Reg, unsigned Idx) const
Returns the physical register number of sub-register "Index" for physical register RegNo.
Wrapper class representing physical registers. Should be passed by value.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
iterator_range< reg_iterator > reg_operands(Register Reg) const
static reg_instr_nodbg_iterator reg_instr_nodbg_end()
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
defusechain_instr_iterator< true, true, true, true > reg_instr_nodbg_iterator
reg_instr_nodbg_iterator/reg_instr_nodbg_begin/reg_instr_nodbg_end - Walk all defs and uses of the sp...
bool reg_nodbg_empty(Register RegNo) const
reg_nodbg_empty - Return true if the only instructions using or defining Reg are Debug instructions.
bool isAllocatable(MCRegister PhysReg) const
isAllocatable - Returns true when PhysReg belongs to an allocatable register class and it hasn't been...
std::pair< unsigned, Register > getRegAllocationHint(Register VReg) const
getRegAllocationHint - Return the register allocation hint for the specified virtual register.
const TargetRegisterInfo * getTargetRegisterInfo() const
LLVM_ABI LaneBitmask getMaxLaneMaskForVReg(Register Reg) const
Returns a mask covering all bits that can appear in lane masks of subregisters of the virtual registe...
void addRegAllocationHint(Register VReg, Register PrefReg)
addRegAllocationHint - Add a register allocation hint to the hints vector for VReg.
LLVM_ABI bool isConstantPhysReg(MCRegister PhysReg) const
Returns true if PhysReg is unallocatable and constant throughout the function.
iterator_range< reg_nodbg_iterator > reg_nodbg_operands(Register Reg) const
void clearSimpleHint(Register VReg)
reg_instr_nodbg_iterator reg_instr_nodbg_begin(Register RegNo) const
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
Wrapper class representing virtual and physical registers.
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
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.
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 getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
VNInfo - Value Number Information.
bool isUnused() const
Returns true if this value is unused.
unsigned id
The ID number of this value.
SlotIndex def
The index of the defining instruction.
bool isPHIDef() const
Returns true if this value is defined by a PHI instruction (or was, PHI instructions may have been el...
LLVM_ABI float weightCalcHelper(LiveInterval &LI)
Helper function for weight calculations.
static LLVM_ABI bool allUsesAvailableAt(const MachineInstr *MI, SlotIndex UseIdx, const LiveIntervals &LIS, const MachineRegisterInfo &MRI, const TargetInstrInfo &TII)
LLVM_ABI void calculateSpillWeightsAndHints()
Compute spill weights and allocation hints for all virtual register live intervals.
static LLVM_ABI bool isRematerializable(const LiveInterval &LI, const LiveIntervals &LIS, const VirtRegMap &VRM, const MachineRegisterInfo &MRI, const TargetInstrInfo &TII)
Determine if all values in LI are rematerializable.
virtual float normalize(float UseDefFreq, unsigned Size, unsigned NumInstr)
Weight normalization function.
static LLVM_ABI Register copyHint(const MachineInstr *MI, Register Reg, const TargetRegisterInfo &TRI, const MachineRegisterInfo &MRI)
Return the preferred allocation register for reg, given a COPY instruction.
LLVM_ABI void calculateSpillWeightAndHint(LiveInterval &LI)
(re)compute li's spill weight and allocation hint.
MachineRegisterInfo & getRegInfo() const
This is an optimization pass for GlobalISel generic memory operations.
bool operator<(int64_t V1, const APSInt &V2)
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
@ Sub
Subtraction of integers.
float stack_float_t
Type to force float point values onto the stack, so that x86 doesn't add hidden precision,...
MCRegisterClass TargetRegisterClass
constexpr bool none() const