43#define DEBUG_TYPE "localstackalloc"
45STATISTIC(NumAllocations,
"Number of frame indices allocated into local block");
46STATISTIC(NumBaseRegisters,
"Number of virtual frame base registers allocated");
47STATISTIC(NumReplacements,
"Number of frame indices references replaced");
65 :
MI(
I), LocalOffset(
Offset), InstrOffset(InstrOffset), FrameIdx(Idx),
69 return std::tuple(LocalOffset + InstrOffset, FrameIdx, Order) <
70 std::tuple(
RHS.LocalOffset +
RHS.InstrOffset,
RHS.FrameIdx,
75 int64_t getLocalOffset()
const {
return LocalOffset; }
76 int64_t getInstrOffset()
const {
return InstrOffset; }
77 int getFrameIndex()
const {
return FrameIdx; }
80 class LocalStackSlotImpl {
84 using StackObjSet = SmallSetVector<int, 8>;
87 bool StackGrowsDown, Align &MaxAlign);
89 SmallSet<int, 16> &ProtectedObjs,
90 MachineFrameInfo &MFI,
bool StackGrowsDown,
91 int64_t &
Offset, Align &MaxAlign);
92 void calculateFrameObjectOffsets(MachineFunction &Fn);
93 bool insertFrameReferenceRegisters(MachineFunction &Fn);
96 bool runOnMachineFunction(MachineFunction &MF);
103 explicit LocalStackSlotPass() : MachineFunctionPass(ID) {}
105 bool runOnMachineFunction(MachineFunction &MF)
override {
106 return LocalStackSlotImpl().runOnMachineFunction(MF);
109 void getAnalysisUsage(AnalysisUsage &AU)
const override {
120 bool Changed = LocalStackSlotImpl().runOnMachineFunction(MF);
128char LocalStackSlotPass::ID = 0;
132 "Local Stack Slot Allocation",
false,
false)
141 if (LocalObjectCount == 0 || !
TRI->requiresVirtualBaseRegisters(MF))
148 calculateFrameObjectOffsets(MF);
151 bool UsedBaseRegs = insertFrameReferenceRegisters(MF);
164void LocalStackSlotImpl::AdjustStackOffset(
MachineFrameInfo &MFI,
int FrameIdx,
165 int64_t &
Offset,
bool StackGrowsDown,
175 MaxAlign = std::max(MaxAlign, Alignment);
180 int64_t LocalOffset = StackGrowsDown ? -
Offset :
Offset;
181 LLVM_DEBUG(
dbgs() <<
"Allocate FI(" << FrameIdx <<
") to local offset "
182 << LocalOffset <<
"\n");
184 LocalOffsets[FrameIdx] = LocalOffset;
196void LocalStackSlotImpl::AssignProtectedObjSet(
200 for (
int i : UnassignedObjs) {
208void LocalStackSlotImpl::calculateFrameObjectOffsets(
MachineFunction &Fn) {
212 bool StackGrowsDown =
228 "Stack protector pre-allocated in LocalStackSlotAllocation");
244 if (StackProtectorFI == (
int)i)
280 if (ProtectedObjs.
count(i))
294 int64_t FrameSizeAdjust,
295 int64_t LocalFrameOffset,
300 int64_t
Offset = FrameSizeAdjust + LocalFrameOffset - BaseOffset;
301 return TRI->isFrameOffsetLegal(&
MI, BaseReg,
Offset);
304bool LocalStackSlotImpl::insertFrameReferenceRegisters(
MachineFunction &Fn) {
315 bool StackGrowsDown =
330 if (
MI.isDebugInstr() ||
MI.getOpcode() == TargetOpcode::STATEPOINT ||
331 MI.getOpcode() == TargetOpcode::STACKMAP ||
332 MI.getOpcode() == TargetOpcode::PATCHPOINT)
341 for (
unsigned OpIdx = 0, OpEnd =
MI.getNumOperands();
OpIdx != OpEnd;
354 int64_t LocalOffset = LocalOffsets[FrameIdx];
355 if (!
TRI->needsFrameBaseReg(&
MI, LocalOffset))
358 int64_t InstrOffset =
TRI->getFrameIndexInstrOffset(&
MI,
OpIdx);
373 int64_t BaseOffset = 0;
376 for (
int ref = 0, e = FrameReferenceInsns.
size(); ref < e ; ++ref) {
377 FrameRef &FR = FrameReferenceInsns[ref];
379 int64_t LocalOffset = FR.getLocalOffset();
380 int FrameIdx = FR.getFrameIndex();
382 "Only pre-allocated locals expected!");
395 for (
unsigned f =
MI.getNumOperands(); idx != f; ++idx) {
396 if (!
MI.getOperand(idx).isFI())
399 if (FrameIdx ==
MI.getOperand(idx).getIndex())
403 assert(idx <
MI.getNumOperands() &&
"Cannot find FI operand");
417 LocalOffset,
MI,
TRI)) {
421 Offset = FrameSizeAdjust + LocalOffset - BaseOffset;
424 int64_t InstrOffset =
TRI->getFrameIndexInstrOffset(&
MI, idx);
426 int64_t CandBaseOffset = FrameSizeAdjust + LocalOffset + InstrOffset;
435 BaseReg, CandBaseOffset, FrameSizeAdjust,
436 FrameReferenceInsns[ref + 1].getLocalOffset(),
441 BaseOffset = CandBaseOffset;
446 BaseReg =
TRI->materializeFrameBaseRegister(Entry, FrameIdx, InstrOffset);
448 LLVM_DEBUG(
dbgs() <<
" Materialized base register at frame local offset "
449 << LocalOffset + InstrOffset
459 assert(BaseReg &&
"Unable to allocate virtual base register!");
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static MachineInstr * getMachineInstr(MachineInstr *MI)
static bool lookupCandidateBaseReg(Register BaseReg, int64_t BaseOffset, int64_t FrameSizeAdjust, int64_t LocalFrameOffset, const MachineInstr &MI, const TargetRegisterInfo *TRI)
Register const TargetRegisterInfo * TRI
MachineInstr unsigned OpIdx
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static void AssignProtectedObjSet(const StackObjSet &UnassignedObjs, SmallSet< int, 16 > &ProtectedObjs, MachineFrameInfo &MFI, bool StackGrowsDown, int64_t &Offset, Align &MaxAlign)
AssignProtectedObjSet - Helper function to assign large stack objects (i.e., those required to be clo...
static void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx, bool StackGrowsDown, int64_t &Offset, Align &MaxAlign)
AdjustStackOffset - Helper function used to adjust the stack frame offset.
SmallSetVector< int, 8 > StackObjSet
StackObjSet - A set of stack object indexes.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &)
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const
bool isObjectPreAllocated(int ObjectIdx) const
Return true if the object was pre-allocated into the local block.
void setUseLocalStackAllocationBlock(bool v)
setUseLocalStackAllocationBlock - Set whether the local allocation blob should be allocated together ...
void setLocalFrameSize(int64_t sz)
Set the size of the local object blob.
@ SSPLK_SmallArray
Array or nested array < SSP-buffer-size.
@ SSPLK_LargeArray
Array or nested array >= SSP-buffer-size.
@ SSPLK_AddrOf
The address of this allocation is exposed and triggered protection.
@ SSPLK_None
Did not trigger a stack protector.
void setLocalFrameMaxAlign(Align Alignment)
Required alignment of the local object blob, which is the strictest alignment of any object in it.
int getStackProtectorIndex() const
Return the index for the stack protector object.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
void mapLocalFrameObject(int ObjectIndex, int64_t Offset)
Map a frame index into the local object block.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
int64_t getLocalFrameSize() const
Get the size of the local object blob.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
bool hasStackProtectorIndex() const
uint8_t getStackID(int ObjectIdx) const
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Wrapper class representing virtual and physical registers.
bool insert(const value_type &X)
Insert a new element into the SetVector.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Information about stack frame layout on the target.
virtual bool isStackIdSafeForLocalArea(unsigned StackId) const
This method returns whether or not it is safe for an object with the given stack id to be bundled int...
StackDirection getStackGrowthDirection() const
getStackGrowthDirection - Return the direction the stack grows
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
This is an optimization pass for GlobalISel generic memory operations.
bool operator<(int64_t V1, const APSInt &V2)
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
void sort(IteratorTy Start, IteratorTy End)
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...
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
LLVM_ABI char & LocalStackSlotAllocationID
LocalStackSlotAllocation - This pass assigns local frame indices to stack slots relative to one anoth...
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.
This struct is a compact representation of a valid (non-zero power of two) alignment.