1 //===-- NVPTXPrologEpilogPass.cpp - NVPTX prolog/epilog inserter ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "NVPTX.h"
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/Pass.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26
27 using namespace llvm;
28
29 #define DEBUG_TYPE "nvptx-prolog-epilog"
30
31 namespace {
32 class NVPTXPrologEpilogPass : public MachineFunctionPass {
33 public:
34 static char ID;
NVPTXPrologEpilogPass()35 NVPTXPrologEpilogPass() : MachineFunctionPass(ID) {}
36
37 bool runOnMachineFunction(MachineFunction &MF) override;
38
39 private:
40 void calculateFrameObjectOffsets(MachineFunction &Fn);
41 };
42 }
43
createNVPTXPrologEpilogPass()44 MachineFunctionPass *llvm::createNVPTXPrologEpilogPass() {
45 return new NVPTXPrologEpilogPass();
46 }
47
48 char NVPTXPrologEpilogPass::ID = 0;
49
runOnMachineFunction(MachineFunction & MF)50 bool NVPTXPrologEpilogPass::runOnMachineFunction(MachineFunction &MF) {
51 const TargetSubtargetInfo &STI = MF.getSubtarget();
52 const TargetFrameLowering &TFI = *STI.getFrameLowering();
53 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
54 bool Modified = false;
55
56 calculateFrameObjectOffsets(MF);
57
58 for (MachineBasicBlock &MBB : MF) {
59 for (MachineInstr &MI : MBB) {
60 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
61 if (!MI.getOperand(i).isFI())
62 continue;
63 TRI.eliminateFrameIndex(MI, 0, i, nullptr);
64 Modified = true;
65 }
66 }
67 }
68
69 // Add function prolog/epilog
70 TFI.emitPrologue(MF, MF.front());
71
72 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
73 // If last instruction is a return instruction, add an epilogue
74 if (I->isReturnBlock())
75 TFI.emitEpilogue(MF, *I);
76 }
77
78 return Modified;
79 }
80
81 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
82 static inline void
AdjustStackOffset(MachineFrameInfo & MFI,int FrameIdx,bool StackGrowsDown,int64_t & Offset,unsigned & MaxAlign)83 AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
84 bool StackGrowsDown, int64_t &Offset,
85 unsigned &MaxAlign) {
86 // If the stack grows down, add the object size to find the lowest address.
87 if (StackGrowsDown)
88 Offset += MFI.getObjectSize(FrameIdx);
89
90 unsigned Align = MFI.getObjectAlignment(FrameIdx);
91
92 // If the alignment of this object is greater than that of the stack, then
93 // increase the stack alignment to match.
94 MaxAlign = std::max(MaxAlign, Align);
95
96 // Adjust to alignment boundary.
97 Offset = (Offset + Align - 1) / Align * Align;
98
99 if (StackGrowsDown) {
100 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
101 << "]\n");
102 MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
103 } else {
104 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
105 << "]\n");
106 MFI.setObjectOffset(FrameIdx, Offset);
107 Offset += MFI.getObjectSize(FrameIdx);
108 }
109 }
110
111 void
calculateFrameObjectOffsets(MachineFunction & Fn)112 NVPTXPrologEpilogPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
113 const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
114 const TargetRegisterInfo *RegInfo = Fn.getSubtarget().getRegisterInfo();
115
116 bool StackGrowsDown =
117 TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
118
119 // Loop over all of the stack objects, assigning sequential addresses...
120 MachineFrameInfo &MFI = Fn.getFrameInfo();
121
122 // Start at the beginning of the local area.
123 // The Offset is the distance from the stack top in the direction
124 // of stack growth -- so it's always nonnegative.
125 int LocalAreaOffset = TFI.getOffsetOfLocalArea();
126 if (StackGrowsDown)
127 LocalAreaOffset = -LocalAreaOffset;
128 assert(LocalAreaOffset >= 0
129 && "Local area offset should be in direction of stack growth");
130 int64_t Offset = LocalAreaOffset;
131
132 // If there are fixed sized objects that are preallocated in the local area,
133 // non-fixed objects can't be allocated right at the start of local area.
134 // We currently don't support filling in holes in between fixed sized
135 // objects, so we adjust 'Offset' to point to the end of last fixed sized
136 // preallocated object.
137 for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
138 int64_t FixedOff;
139 if (StackGrowsDown) {
140 // The maximum distance from the stack pointer is at lower address of
141 // the object -- which is given by offset. For down growing stack
142 // the offset is negative, so we negate the offset to get the distance.
143 FixedOff = -MFI.getObjectOffset(i);
144 } else {
145 // The maximum distance from the start pointer is at the upper
146 // address of the object.
147 FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
148 }
149 if (FixedOff > Offset) Offset = FixedOff;
150 }
151
152 // NOTE: We do not have a call stack
153
154 unsigned MaxAlign = MFI.getMaxAlignment();
155
156 // No scavenger
157
158 // FIXME: Once this is working, then enable flag will change to a target
159 // check for whether the frame is large enough to want to use virtual
160 // frame index registers. Functions which don't want/need this optimization
161 // will continue to use the existing code path.
162 if (MFI.getUseLocalStackAllocationBlock()) {
163 unsigned Align = MFI.getLocalFrameMaxAlign();
164
165 // Adjust to alignment boundary.
166 Offset = (Offset + Align - 1) / Align * Align;
167
168 LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
169
170 // Resolve offsets for objects in the local block.
171 for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
172 std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
173 int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
174 LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
175 << "]\n");
176 MFI.setObjectOffset(Entry.first, FIOffset);
177 }
178 // Allocate the local block
179 Offset += MFI.getLocalFrameSize();
180
181 MaxAlign = std::max(Align, MaxAlign);
182 }
183
184 // No stack protector
185
186 // Then assign frame offsets to stack objects that are not used to spill
187 // callee saved registers.
188 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
189 if (MFI.isObjectPreAllocated(i) &&
190 MFI.getUseLocalStackAllocationBlock())
191 continue;
192 if (MFI.isDeadObjectIndex(i))
193 continue;
194
195 AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
196 }
197
198 // No scavenger
199
200 if (!TFI.targetHandlesStackFrameRounding()) {
201 // If we have reserved argument space for call sites in the function
202 // immediately on entry to the current function, count it as part of the
203 // overall stack size.
204 if (MFI.adjustsStack() && TFI.hasReservedCallFrame(Fn))
205 Offset += MFI.getMaxCallFrameSize();
206
207 // Round up the size to a multiple of the alignment. If the function has
208 // any calls or alloca's, align to the target's StackAlignment value to
209 // ensure that the callee's frame or the alloca data is suitably aligned;
210 // otherwise, for leaf functions, align to the TransientStackAlignment
211 // value.
212 unsigned StackAlign;
213 if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
214 (RegInfo->needsStackRealignment(Fn) && MFI.getObjectIndexEnd() != 0))
215 StackAlign = TFI.getStackAlignment();
216 else
217 StackAlign = TFI.getTransientStackAlignment();
218
219 // If the frame pointer is eliminated, all frame offsets will be relative to
220 // SP not FP. Align to MaxAlign so this works.
221 StackAlign = std::max(StackAlign, MaxAlign);
222 unsigned AlignMask = StackAlign - 1;
223 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
224 }
225
226 // Update frame info to pretend that this is part of the stack...
227 int64_t StackSize = Offset - LocalAreaOffset;
228 MFI.setStackSize(StackSize);
229 }
230