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 bool operator<(
const FrameRef &RHS)
const {
66 return std::tie(LocalOffset, FrameIdx, Order) <
67 std::tie(
RHS.LocalOffset,
RHS.FrameIdx,
RHS.Order);
71 int64_t getLocalOffset()
const {
return LocalOffset; }
72 int getFrameIndex()
const {
return FrameIdx; }
75 class LocalStackSlotImpl {
82 bool StackGrowsDown,
Align &MaxAlign);
103 return LocalStackSlotImpl().runOnMachineFunction(MF);
117 bool Changed = LocalStackSlotImpl().runOnMachineFunction(MF);
125char LocalStackSlotPass::ID = 0;
129 "Local Stack Slot Allocation",
false,
false)
138 if (LocalObjectCount == 0 || !
TRI->requiresVirtualBaseRegisters(MF))
145 calculateFrameObjectOffsets(MF);
148 bool UsedBaseRegs = insertFrameReferenceRegisters(MF);
161void LocalStackSlotImpl::AdjustStackOffset(
MachineFrameInfo &MFI,
int FrameIdx,
162 int64_t &
Offset,
bool StackGrowsDown,
172 MaxAlign = std::max(MaxAlign, Alignment);
177 int64_t LocalOffset = StackGrowsDown ? -
Offset :
Offset;
178 LLVM_DEBUG(
dbgs() <<
"Allocate FI(" << FrameIdx <<
") to local offset "
179 << LocalOffset <<
"\n");
181 LocalOffsets[FrameIdx] = LocalOffset;
193void LocalStackSlotImpl::AssignProtectedObjSet(
197 for (
int i : UnassignedObjs) {
205void LocalStackSlotImpl::calculateFrameObjectOffsets(
MachineFunction &Fn) {
209 bool StackGrowsDown =
225 "Stack protector pre-allocated in LocalStackSlotAllocation");
241 if (StackProtectorFI == (
int)i)
277 if (ProtectedObjs.
count(i))
293 int64_t FrameSizeAdjust,
294 int64_t LocalFrameOffset,
299 int64_t
Offset = FrameSizeAdjust + LocalFrameOffset - BaseOffset;
300 return TRI->isFrameOffsetLegal(&
MI, BaseReg,
Offset);
303bool LocalStackSlotImpl::insertFrameReferenceRegisters(
MachineFunction &Fn) {
314 bool StackGrowsDown =
329 if (
MI.isDebugInstr() ||
MI.getOpcode() == TargetOpcode::STATEPOINT ||
330 MI.getOpcode() == TargetOpcode::STACKMAP ||
331 MI.getOpcode() == TargetOpcode::PATCHPOINT)
347 int Idx = MO.getIndex();
348 int64_t LocalOffset = LocalOffsets[
Idx];
349 if (!
TRI->needsFrameBaseReg(&
MI, LocalOffset))
351 FrameReferenceInsns.
push_back(FrameRef(&
MI, LocalOffset,
Idx, Order++));
365 int64_t BaseOffset = 0;
368 for (
int ref = 0, e = FrameReferenceInsns.
size(); ref < e ; ++ref) {
369 FrameRef &FR = FrameReferenceInsns[ref];
371 int64_t LocalOffset = FR.getLocalOffset();
372 int FrameIdx = FR.getFrameIndex();
374 "Only pre-allocated locals expected!");
387 for (
unsigned f =
MI.getNumOperands(); idx != f; ++idx) {
388 if (!
MI.getOperand(idx).isFI())
391 if (FrameIdx ==
MI.getOperand(idx).getIndex())
395 assert(idx <
MI.getNumOperands() &&
"Cannot find FI operand");
409 LocalOffset,
MI,
TRI)) {
410 LLVM_DEBUG(
dbgs() <<
" Reusing base register " << BaseReg <<
"\n");
412 Offset = FrameSizeAdjust + LocalOffset - BaseOffset;
415 int64_t InstrOffset =
TRI->getFrameIndexInstrOffset(&
MI, idx);
417 int64_t CandBaseOffset = FrameSizeAdjust + LocalOffset + InstrOffset;
426 BaseReg, CandBaseOffset, FrameSizeAdjust,
427 FrameReferenceInsns[ref + 1].getLocalOffset(),
432 BaseOffset = CandBaseOffset;
437 BaseReg =
TRI->materializeFrameBaseRegister(Entry, FrameIdx, InstrOffset);
439 LLVM_DEBUG(
dbgs() <<
" Materialized base register at frame local offset "
440 << LocalOffset + InstrOffset
450 assert(BaseReg &&
"Unable to allocate virtual base register!");
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static MachineInstr * getMachineInstr(MachineInstr *MI)
static bool lookupCandidateBaseReg(unsigned BaseReg, int64_t BaseOffset, int64_t FrameSizeAdjust, int64_t LocalFrameOffset, const MachineInstr &MI, const TargetRegisterInfo *TRI)
unsigned const TargetRegisterInfo * TRI
#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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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)
A container for analyses that lazily runs them and caches their results.
Represent the analysis usage information of a pass.
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 &)
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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.
constexpr bool isValid() const
bool insert(const value_type &X)
Insert a new element into the SetVector.
A SetVector that performs no allocations if smaller than a certain size.
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.
void push_back(const T &Elt)
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 TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetFrameLowering * getFrameLowering() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
bool operator<(int64_t V1, const APSInt &V2)
PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
void sort(IteratorTy Start, IteratorTy End)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
char & LocalStackSlotAllocationID
LocalStackSlotAllocation - This pass assigns local frame indices to stack slots relative to one anoth...
void initializeLocalStackSlotPassPass(PassRegistry &)
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.