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"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/TargetFrameLowering.h"
20 #include "llvm/CodeGen/TargetRegisterInfo.h"
21 #include "llvm/CodeGen/TargetSubtargetInfo.h"
22 #include "llvm/IR/DebugInfoMetadata.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;
35   NVPTXPrologEpilogPass() : MachineFunctionPass(ID) {}
36 
37   bool runOnMachineFunction(MachineFunction &MF) override;
38 
39   StringRef getPassName() const override { return "NVPTX Prolog Epilog Pass"; }
40 
41 private:
42   void calculateFrameObjectOffsets(MachineFunction &Fn);
43 };
44 }
45 
46 MachineFunctionPass *llvm::createNVPTXPrologEpilogPass() {
47   return new NVPTXPrologEpilogPass();
48 }
49 
50 char NVPTXPrologEpilogPass::ID = 0;
51 
52 bool NVPTXPrologEpilogPass::runOnMachineFunction(MachineFunction &MF) {
53   const TargetSubtargetInfo &STI = MF.getSubtarget();
54   const TargetFrameLowering &TFI = *STI.getFrameLowering();
55   const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
56   bool Modified = false;
57 
58   calculateFrameObjectOffsets(MF);
59 
60   for (MachineBasicBlock &MBB : MF) {
61     for (MachineInstr &MI : MBB) {
62       for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
63         if (!MI.getOperand(i).isFI())
64           continue;
65 
66         // Frame indices in debug values are encoded in a target independent
67         // way with simply the frame index and offset rather than any
68         // target-specific addressing mode.
69         if (MI.isDebugValue()) {
70           MachineOperand &Op = MI.getOperand(i);
71           assert(
72               MI.isDebugOperand(&Op) &&
73               "Frame indices can only appear as a debug operand in a DBG_VALUE*"
74               " machine instruction");
75           Register Reg;
76           auto Offset =
77               TFI.getFrameIndexReference(MF, Op.getIndex(), Reg);
78           Op.ChangeToRegister(Reg, /*isDef=*/false);
79           const DIExpression *DIExpr = MI.getDebugExpression();
80           if (MI.isNonListDebugValue()) {
81             DIExpr = TRI.prependOffsetExpression(MI.getDebugExpression(), DIExpression::ApplyOffset, Offset);
82           } else {
83 	    SmallVector<uint64_t, 3> Ops;
84             TRI.getOffsetOpcodes(Offset, Ops);
85             unsigned OpIdx = MI.getDebugOperandIndex(&Op);
86             DIExpr = DIExpression::appendOpsToArg(DIExpr, Ops, OpIdx);
87           }
88           MI.getDebugExpressionOp().setMetadata(DIExpr);
89           continue;
90         }
91 
92         TRI.eliminateFrameIndex(MI, 0, i, nullptr);
93         Modified = true;
94       }
95     }
96   }
97 
98   // Add function prolog/epilog
99   TFI.emitPrologue(MF, MF.front());
100 
101   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
102     // If last instruction is a return instruction, add an epilogue
103     if (I->isReturnBlock())
104       TFI.emitEpilogue(MF, *I);
105   }
106 
107   return Modified;
108 }
109 
110 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
111 static inline void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
112                                      bool StackGrowsDown, int64_t &Offset,
113                                      Align &MaxAlign) {
114   // If the stack grows down, add the object size to find the lowest address.
115   if (StackGrowsDown)
116     Offset += MFI.getObjectSize(FrameIdx);
117 
118   Align Alignment = MFI.getObjectAlign(FrameIdx);
119 
120   // If the alignment of this object is greater than that of the stack, then
121   // increase the stack alignment to match.
122   MaxAlign = std::max(MaxAlign, Alignment);
123 
124   // Adjust to alignment boundary.
125   Offset = alignTo(Offset, Alignment);
126 
127   if (StackGrowsDown) {
128     LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
129                       << "]\n");
130     MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
131   } else {
132     LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
133                       << "]\n");
134     MFI.setObjectOffset(FrameIdx, Offset);
135     Offset += MFI.getObjectSize(FrameIdx);
136   }
137 }
138 
139 void
140 NVPTXPrologEpilogPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
141   const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
142   const TargetRegisterInfo *RegInfo = Fn.getSubtarget().getRegisterInfo();
143 
144   bool StackGrowsDown =
145     TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
146 
147   // Loop over all of the stack objects, assigning sequential addresses...
148   MachineFrameInfo &MFI = Fn.getFrameInfo();
149 
150   // Start at the beginning of the local area.
151   // The Offset is the distance from the stack top in the direction
152   // of stack growth -- so it's always nonnegative.
153   int LocalAreaOffset = TFI.getOffsetOfLocalArea();
154   if (StackGrowsDown)
155     LocalAreaOffset = -LocalAreaOffset;
156   assert(LocalAreaOffset >= 0
157          && "Local area offset should be in direction of stack growth");
158   int64_t Offset = LocalAreaOffset;
159 
160   // If there are fixed sized objects that are preallocated in the local area,
161   // non-fixed objects can't be allocated right at the start of local area.
162   // We currently don't support filling in holes in between fixed sized
163   // objects, so we adjust 'Offset' to point to the end of last fixed sized
164   // preallocated object.
165   for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
166     int64_t FixedOff;
167     if (StackGrowsDown) {
168       // The maximum distance from the stack pointer is at lower address of
169       // the object -- which is given by offset. For down growing stack
170       // the offset is negative, so we negate the offset to get the distance.
171       FixedOff = -MFI.getObjectOffset(i);
172     } else {
173       // The maximum distance from the start pointer is at the upper
174       // address of the object.
175       FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
176     }
177     if (FixedOff > Offset) Offset = FixedOff;
178   }
179 
180   // NOTE: We do not have a call stack
181 
182   Align MaxAlign = MFI.getMaxAlign();
183 
184   // No scavenger
185 
186   // FIXME: Once this is working, then enable flag will change to a target
187   // check for whether the frame is large enough to want to use virtual
188   // frame index registers. Functions which don't want/need this optimization
189   // will continue to use the existing code path.
190   if (MFI.getUseLocalStackAllocationBlock()) {
191     Align Alignment = MFI.getLocalFrameMaxAlign();
192 
193     // Adjust to alignment boundary.
194     Offset = alignTo(Offset, Alignment);
195 
196     LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
197 
198     // Resolve offsets for objects in the local block.
199     for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
200       std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
201       int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
202       LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
203                         << "]\n");
204       MFI.setObjectOffset(Entry.first, FIOffset);
205     }
206     // Allocate the local block
207     Offset += MFI.getLocalFrameSize();
208 
209     MaxAlign = std::max(Alignment, MaxAlign);
210   }
211 
212   // No stack protector
213 
214   // Then assign frame offsets to stack objects that are not used to spill
215   // callee saved registers.
216   for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
217     if (MFI.isObjectPreAllocated(i) &&
218         MFI.getUseLocalStackAllocationBlock())
219       continue;
220     if (MFI.isDeadObjectIndex(i))
221       continue;
222 
223     AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
224   }
225 
226   // No scavenger
227 
228   if (!TFI.targetHandlesStackFrameRounding()) {
229     // If we have reserved argument space for call sites in the function
230     // immediately on entry to the current function, count it as part of the
231     // overall stack size.
232     if (MFI.adjustsStack() && TFI.hasReservedCallFrame(Fn))
233       Offset += MFI.getMaxCallFrameSize();
234 
235     // Round up the size to a multiple of the alignment.  If the function has
236     // any calls or alloca's, align to the target's StackAlignment value to
237     // ensure that the callee's frame or the alloca data is suitably aligned;
238     // otherwise, for leaf functions, align to the TransientStackAlignment
239     // value.
240     Align StackAlign;
241     if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
242         (RegInfo->hasStackRealignment(Fn) && MFI.getObjectIndexEnd() != 0))
243       StackAlign = TFI.getStackAlign();
244     else
245       StackAlign = TFI.getTransientStackAlign();
246 
247     // If the frame pointer is eliminated, all frame offsets will be relative to
248     // SP not FP. Align to MaxAlign so this works.
249     Offset = alignTo(Offset, std::max(StackAlign, MaxAlign));
250   }
251 
252   // Update frame info to pretend that this is part of the stack...
253   int64_t StackSize = Offset - LocalAreaOffset;
254   MFI.setStackSize(StackSize);
255 }
256