1 //===-- NVPTXPrologEpilogPass.cpp - NVPTX prolog/epilog inserter ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a copy of the generic LLVM PrologEpilogInserter pass, modified
11 // to remove unneeded functionality and to handle virtual registers. Most code
12 // here is a copy of PrologEpilogInserter.cpp.
14 //===----------------------------------------------------------------------===//
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/TargetFrameLowering.h"
21 #include "llvm/CodeGen/TargetRegisterInfo.h"
22 #include "llvm/CodeGen/TargetSubtargetInfo.h"
23 #include "llvm/IR/DebugInfoMetadata.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/raw_ostream.h"
30 #define DEBUG_TYPE "nvptx-prolog-epilog"
33 class NVPTXPrologEpilogPass : public MachineFunctionPass {
36 NVPTXPrologEpilogPass() : MachineFunctionPass(ID) {}
38 bool runOnMachineFunction(MachineFunction &MF) override;
41 void calculateFrameObjectOffsets(MachineFunction &Fn);
45 MachineFunctionPass *llvm::createNVPTXPrologEpilogPass() {
46 return new NVPTXPrologEpilogPass();
49 char NVPTXPrologEpilogPass::ID = 0;
51 bool NVPTXPrologEpilogPass::runOnMachineFunction(MachineFunction &MF) {
52 const TargetSubtargetInfo &STI = MF.getSubtarget();
53 const TargetFrameLowering &TFI = *STI.getFrameLowering();
54 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
55 bool Modified = false;
57 calculateFrameObjectOffsets(MF);
59 for (MachineBasicBlock &MBB : MF) {
60 for (MachineInstr &MI : MBB) {
61 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
62 if (!MI.getOperand(i).isFI())
65 // Frame indices in debug values are encoded in a target independent
66 // way with simply the frame index and offset rather than any
67 // target-specific addressing mode.
68 if (MI.isDebugValue()) {
69 assert(i == 0 && "Frame indices can only appear as the first "
70 "operand of a DBG_VALUE machine instruction");
73 TFI.getFrameIndexReference(MF, MI.getOperand(0).getIndex(), Reg);
74 MI.getOperand(0).ChangeToRegister(Reg, /*isDef=*/false);
75 MI.getOperand(0).setIsDebug();
76 auto *DIExpr = DIExpression::prepend(MI.getDebugExpression(),
77 DIExpression::NoDeref, Offset);
78 MI.getOperand(3).setMetadata(DIExpr);
82 TRI.eliminateFrameIndex(MI, 0, i, nullptr);
88 // Add function prolog/epilog
89 TFI.emitPrologue(MF, MF.front());
91 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
92 // If last instruction is a return instruction, add an epilogue
93 if (I->isReturnBlock())
94 TFI.emitEpilogue(MF, *I);
100 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
102 AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
103 bool StackGrowsDown, int64_t &Offset,
104 unsigned &MaxAlign) {
105 // If the stack grows down, add the object size to find the lowest address.
107 Offset += MFI.getObjectSize(FrameIdx);
109 unsigned Align = MFI.getObjectAlignment(FrameIdx);
111 // If the alignment of this object is greater than that of the stack, then
112 // increase the stack alignment to match.
113 MaxAlign = std::max(MaxAlign, Align);
115 // Adjust to alignment boundary.
116 Offset = (Offset + Align - 1) / Align * Align;
118 if (StackGrowsDown) {
119 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
121 MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
123 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
125 MFI.setObjectOffset(FrameIdx, Offset);
126 Offset += MFI.getObjectSize(FrameIdx);
131 NVPTXPrologEpilogPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
132 const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
133 const TargetRegisterInfo *RegInfo = Fn.getSubtarget().getRegisterInfo();
135 bool StackGrowsDown =
136 TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
138 // Loop over all of the stack objects, assigning sequential addresses...
139 MachineFrameInfo &MFI = Fn.getFrameInfo();
141 // Start at the beginning of the local area.
142 // The Offset is the distance from the stack top in the direction
143 // of stack growth -- so it's always nonnegative.
144 int LocalAreaOffset = TFI.getOffsetOfLocalArea();
146 LocalAreaOffset = -LocalAreaOffset;
147 assert(LocalAreaOffset >= 0
148 && "Local area offset should be in direction of stack growth");
149 int64_t Offset = LocalAreaOffset;
151 // If there are fixed sized objects that are preallocated in the local area,
152 // non-fixed objects can't be allocated right at the start of local area.
153 // We currently don't support filling in holes in between fixed sized
154 // objects, so we adjust 'Offset' to point to the end of last fixed sized
155 // preallocated object.
156 for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
158 if (StackGrowsDown) {
159 // The maximum distance from the stack pointer is at lower address of
160 // the object -- which is given by offset. For down growing stack
161 // the offset is negative, so we negate the offset to get the distance.
162 FixedOff = -MFI.getObjectOffset(i);
164 // The maximum distance from the start pointer is at the upper
165 // address of the object.
166 FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
168 if (FixedOff > Offset) Offset = FixedOff;
171 // NOTE: We do not have a call stack
173 unsigned MaxAlign = MFI.getMaxAlignment();
177 // FIXME: Once this is working, then enable flag will change to a target
178 // check for whether the frame is large enough to want to use virtual
179 // frame index registers. Functions which don't want/need this optimization
180 // will continue to use the existing code path.
181 if (MFI.getUseLocalStackAllocationBlock()) {
182 unsigned Align = MFI.getLocalFrameMaxAlign();
184 // Adjust to alignment boundary.
185 Offset = (Offset + Align - 1) / Align * Align;
187 LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
189 // Resolve offsets for objects in the local block.
190 for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
191 std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
192 int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
193 LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
195 MFI.setObjectOffset(Entry.first, FIOffset);
197 // Allocate the local block
198 Offset += MFI.getLocalFrameSize();
200 MaxAlign = std::max(Align, MaxAlign);
203 // No stack protector
205 // Then assign frame offsets to stack objects that are not used to spill
206 // callee saved registers.
207 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
208 if (MFI.isObjectPreAllocated(i) &&
209 MFI.getUseLocalStackAllocationBlock())
211 if (MFI.isDeadObjectIndex(i))
214 AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
219 if (!TFI.targetHandlesStackFrameRounding()) {
220 // If we have reserved argument space for call sites in the function
221 // immediately on entry to the current function, count it as part of the
222 // overall stack size.
223 if (MFI.adjustsStack() && TFI.hasReservedCallFrame(Fn))
224 Offset += MFI.getMaxCallFrameSize();
226 // Round up the size to a multiple of the alignment. If the function has
227 // any calls or alloca's, align to the target's StackAlignment value to
228 // ensure that the callee's frame or the alloca data is suitably aligned;
229 // otherwise, for leaf functions, align to the TransientStackAlignment
232 if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
233 (RegInfo->needsStackRealignment(Fn) && MFI.getObjectIndexEnd() != 0))
234 StackAlign = TFI.getStackAlignment();
236 StackAlign = TFI.getTransientStackAlignment();
238 // If the frame pointer is eliminated, all frame offsets will be relative to
239 // SP not FP. Align to MaxAlign so this works.
240 StackAlign = std::max(StackAlign, MaxAlign);
241 unsigned AlignMask = StackAlign - 1;
242 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
245 // Update frame info to pretend that this is part of the stack...
246 int64_t StackSize = Offset - LocalAreaOffset;
247 MFI.setStackSize(StackSize);