LLVM 22.0.0git
NVPTXPrologEpilogPass.cpp
Go to the documentation of this file.
1//===-- NVPTXPrologEpilogPass.cpp - NVPTX prolog/epilog inserter ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a copy of the generic LLVM PrologEpilogInserter pass, modified
10// to remove unneeded functionality and to handle virtual registers. Most code
11// here is a copy of PrologEpilogInserter.cpp.
12//
13//===----------------------------------------------------------------------===//
14
15#include "NVPTX.h"
23#include "llvm/Pass.h"
24#include "llvm/Support/Debug.h"
26
27using namespace llvm;
28
29#define DEBUG_TYPE "nvptx-prolog-epilog"
30
31namespace {
32class NVPTXPrologEpilogPass : public MachineFunctionPass {
33public:
34 static char ID;
35 NVPTXPrologEpilogPass() : MachineFunctionPass(ID) {}
36
37 bool runOnMachineFunction(MachineFunction &MF) override;
38
39 StringRef getPassName() const override { return "NVPTX Prolog Epilog Pass"; }
40
41private:
42 void calculateFrameObjectOffsets(MachineFunction &Fn);
43};
44} // end anonymous namespace
45
47 return new NVPTXPrologEpilogPass();
48}
49
50char NVPTXPrologEpilogPass::ID = 0;
51
52INITIALIZE_PASS(NVPTXPrologEpilogPass, DEBUG_TYPE,
53 "NVPTX Prologue/Epilogue Insertion", false, false)
54
55static bool replaceFrameIndexDebugInstr(MachineFunction &MF, MachineInstr &MI,
56 unsigned OpIdx) {
57 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
58 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
60
63 "Frame indices can only appear as a debug operand in a DBG_VALUE*"
64 " machine instruction");
65 Register Reg;
66 unsigned FrameIdx = Op.getIndex();
67
68 StackOffset Offset = TFI->getFrameIndexReference(MF, FrameIdx, Reg);
69 Op.ChangeToRegister(Reg, false /*isDef*/);
70
71 const DIExpression *DIExpr = MI.getDebugExpression();
72 if (MI.isNonListDebugValue()) {
75 } else {
76 // The debug operand at DebugOpIndex was a frame index at offset
77 // `Offset`; now the operand has been replaced with the frame
78 // register, we must add Offset with `register x, plus Offset`.
79 unsigned DebugOpIndex = MI.getDebugOperandIndex(&Op);
82 DIExpr = DIExpression::appendOpsToArg(DIExpr, Ops, DebugOpIndex);
83 }
85 return true;
86 }
87 return false;
88}
89
90bool NVPTXPrologEpilogPass::runOnMachineFunction(MachineFunction &MF) {
91 const TargetSubtargetInfo &STI = MF.getSubtarget();
92 const TargetFrameLowering &TFI = *STI.getFrameLowering();
94 bool Modified = false;
95
96 calculateFrameObjectOffsets(MF);
97
98 for (MachineBasicBlock &BB : MF) {
99 for (MachineBasicBlock::iterator I = BB.end(); I != BB.begin();) {
100 MachineInstr &MI = *std::prev(I);
101
102 bool RemovedMI = false;
103 for (const auto &[Idx, Op] : enumerate(MI.operands())) {
104 if (!Op.isFI())
105 continue;
106
107 if (replaceFrameIndexDebugInstr(MF, MI, Idx))
108 continue;
109
110 // Eliminate this FrameIndex operand.
111 RemovedMI = TRI.eliminateFrameIndex(MI, 0, Idx, nullptr);
112 Modified = true;
113 if (RemovedMI)
114 break;
115 }
116
117 if (!RemovedMI)
118 --I;
119 }
120 }
121
122 // Add function prolog/epilog
123 TFI.emitPrologue(MF, MF.front());
124
125 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
126 // If last instruction is a return instruction, add an epilogue
127 if (I->isReturnBlock())
128 TFI.emitEpilogue(MF, *I);
129 }
130
131 return Modified;
132}
133
134/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
135static inline void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
136 bool StackGrowsDown, int64_t &Offset,
137 Align &MaxAlign) {
138 // If the stack grows down, add the object size to find the lowest address.
139 if (StackGrowsDown)
140 Offset += MFI.getObjectSize(FrameIdx);
141
142 Align Alignment = MFI.getObjectAlign(FrameIdx);
143
144 // If the alignment of this object is greater than that of the stack, then
145 // increase the stack alignment to match.
146 MaxAlign = std::max(MaxAlign, Alignment);
147
148 // Adjust to alignment boundary.
149 Offset = alignTo(Offset, Alignment);
150
151 if (StackGrowsDown) {
152 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
153 << "]\n");
154 MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
155 } else {
156 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
157 << "]\n");
158 MFI.setObjectOffset(FrameIdx, Offset);
159 Offset += MFI.getObjectSize(FrameIdx);
160 }
161}
162
163void
164NVPTXPrologEpilogPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
167
168 bool StackGrowsDown =
170
171 // Loop over all of the stack objects, assigning sequential addresses...
172 MachineFrameInfo &MFI = Fn.getFrameInfo();
173
174 // Start at the beginning of the local area.
175 // The Offset is the distance from the stack top in the direction
176 // of stack growth -- so it's always nonnegative.
177 int LocalAreaOffset = TFI.getOffsetOfLocalArea();
178 if (StackGrowsDown)
179 LocalAreaOffset = -LocalAreaOffset;
180 assert(LocalAreaOffset >= 0
181 && "Local area offset should be in direction of stack growth");
182 int64_t Offset = LocalAreaOffset;
183
184 // If there are fixed sized objects that are preallocated in the local area,
185 // non-fixed objects can't be allocated right at the start of local area.
186 // We currently don't support filling in holes in between fixed sized
187 // objects, so we adjust 'Offset' to point to the end of last fixed sized
188 // preallocated object.
189 for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
190 int64_t FixedOff;
191 if (StackGrowsDown) {
192 // The maximum distance from the stack pointer is at lower address of
193 // the object -- which is given by offset. For down growing stack
194 // the offset is negative, so we negate the offset to get the distance.
195 FixedOff = -MFI.getObjectOffset(i);
196 } else {
197 // The maximum distance from the start pointer is at the upper
198 // address of the object.
199 FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
200 }
201 if (FixedOff > Offset) Offset = FixedOff;
202 }
203
204 // NOTE: We do not have a call stack
205
206 Align MaxAlign = MFI.getMaxAlign();
207
208 // No scavenger
209
210 // FIXME: Once this is working, then enable flag will change to a target
211 // check for whether the frame is large enough to want to use virtual
212 // frame index registers. Functions which don't want/need this optimization
213 // will continue to use the existing code path.
215 Align Alignment = MFI.getLocalFrameMaxAlign();
216
217 // Adjust to alignment boundary.
218 Offset = alignTo(Offset, Alignment);
219
220 LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
221
222 // Resolve offsets for objects in the local block.
223 for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
224 std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
225 int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
226 LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
227 << "]\n");
228 MFI.setObjectOffset(Entry.first, FIOffset);
229 }
230 // Allocate the local block
231 Offset += MFI.getLocalFrameSize();
232
233 MaxAlign = std::max(Alignment, MaxAlign);
234 }
235
236 // No stack protector
237
238 // Then assign frame offsets to stack objects that are not used to spill
239 // callee saved registers.
240 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
241 if (MFI.isObjectPreAllocated(i) &&
243 continue;
244 if (MFI.isDeadObjectIndex(i))
245 continue;
246
247 AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
248 }
249
250 // No scavenger
251
253 // If we have reserved argument space for call sites in the function
254 // immediately on entry to the current function, count it as part of the
255 // overall stack size.
256 if (MFI.adjustsStack() && TFI.hasReservedCallFrame(Fn))
258
259 // Round up the size to a multiple of the alignment. If the function has
260 // any calls or alloca's, align to the target's StackAlignment value to
261 // ensure that the callee's frame or the alloca data is suitably aligned;
262 // otherwise, for leaf functions, align to the TransientStackAlignment
263 // value.
264 Align StackAlign;
265 if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
266 (RegInfo->hasStackRealignment(Fn) && MFI.getObjectIndexEnd() != 0))
267 StackAlign = TFI.getStackAlign();
268 else
269 StackAlign = TFI.getTransientStackAlign();
270
271 // If the frame pointer is eliminated, all frame offsets will be relative to
272 // SP not FP. Align to MaxAlign so this works.
273 Offset = alignTo(Offset, std::max(StackAlign, MaxAlign));
274 }
275
276 // Update frame info to pretend that this is part of the stack...
277 int64_t StackSize = Offset - LocalAreaOffset;
278 MFI.setStackSize(StackSize);
279}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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
#define I(x, y, z)
Definition: MD5.cpp:58
const TargetRegisterInfo & TRI
static void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx, bool StackGrowsDown, int64_t &Offset, Align &MaxAlign)
AdjustStackOffset - Helper function used to adjust the stack frame offset.
MachineInstr unsigned OpIdx
MachineInstr & MI
#define DEBUG_TYPE
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
#define LLVM_DEBUG(...)
Definition: Debug.h:119
DWARF expression.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
This class represents an Operation in the Expression.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
bool isObjectPreAllocated(int ObjectIdx) const
Return true if the object was pre-allocated into the local block.
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
int64_t getLocalFrameObjectCount() const
Return the number of objects allocated into the local object block.
Align getMaxAlign() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
Align getLocalFrameMaxAlign() const
Return the required alignment of the local object blob.
void setObjectOffset(int ObjectIdx, int64_t SPOffset)
Set the stack frame offset of the specified object.
std::pair< int, int64_t > getLocalFrameObjectMap(int i) const
Get the local offset mapping for a for an object.
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
bool getUseLocalStackAllocationBlock() const
Get whether the local allocation blob should be allocated together or let PEI allocate the locals in ...
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
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.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
void setStackSize(uint64_t Size)
Set the size of the stack.
int getObjectIndexBegin() const
Return the minimum frame object index.
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...
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.
const MachineBasicBlock & front() const
Representation of each machine instruction.
Definition: MachineInstr.h:72
LLVM_ABI const MachineOperand & getDebugExpressionOp() const
Return the operand for the complex address expression referenced by this DBG_VALUE instruction.
mop_range operands()
Definition: MachineInstr.h:693
LLVM_ABI const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
bool isNonListDebugValue() const
bool isDebugValue() const
unsigned getDebugOperandIndex(const MachineOperand *Op) const
Definition: MachineInstr.h:631
bool isDebugOperand(const MachineOperand *Op) const
Definition: MachineInstr.h:627
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:595
MachineOperand class - Representation of each machine instruction operand.
void setMetadata(const MDNode *MD)
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:85
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StackOffset holds a fixed and a scalable offset in bytes.
Definition: TypeSize.h:34
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Information about stack frame layout on the target.
virtual void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const =0
virtual bool hasReservedCallFrame(const MachineFunction &MF) const
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
Align getTransientStackAlign() const
getTransientStackAlignment - This method returns the number of bytes to which the stack pointer must ...
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
StackDirection getStackGrowthDirection() const
getStackGrowthDirection - Return the direction the stack grows
virtual bool targetHandlesStackFrameRounding() const
targetHandlesStackFrameRounding - Returns true if the target is responsible for rounding up the stack...
virtual void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const =0
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
virtual StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const
getFrameIndexReference - This method should return the base register and offset used to reference a f...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual bool eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, unsigned FIOperandNum, RegScavenger *RS=nullptr) const =0
This method must be overriden to eliminate abstract frame indices from instructions which may use the...
DIExpression * prependOffsetExpression(const DIExpression *Expr, unsigned PrependFlags, const StackOffset &Offset) const
Prepends a DWARF expression for Offset to DIExpression Expr.
virtual void getOffsetOpcodes(const StackOffset &Offset, SmallVectorImpl< uint64_t > &Ops) const
Gets the DWARF expression opcodes for Offset.
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
@ Entry
Definition: COFF.h:862
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2491
MachineFunctionPass * createNVPTXPrologEpilogPass()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39