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