[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / Target / NVPTX / NVPTXPrologEpilogPass.cpp
blob8e2299e652221a70b1ac11b700a2216a8045dc81
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.
13 //===----------------------------------------------------------------------===//
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"
27 using namespace llvm;
29 #define DEBUG_TYPE "nvptx-prolog-epilog"
31 namespace {
32 class NVPTXPrologEpilogPass : public MachineFunctionPass {
33 public:
34 static char ID;
35 NVPTXPrologEpilogPass() : MachineFunctionPass(ID) {}
37 bool runOnMachineFunction(MachineFunction &MF) override;
39 private:
40 void calculateFrameObjectOffsets(MachineFunction &Fn);
44 MachineFunctionPass *llvm::createNVPTXPrologEpilogPass() {
45 return new NVPTXPrologEpilogPass();
48 char NVPTXPrologEpilogPass::ID = 0;
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;
56 calculateFrameObjectOffsets(MF);
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;
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 MachineOperand &Op = MI.getOperand(i);
69 assert(
70 MI.isDebugOperand(&Op) &&
71 "Frame indices can only appear as a debug operand in a DBG_VALUE*"
72 " machine instruction");
73 Register Reg;
74 auto Offset =
75 TFI.getFrameIndexReference(MF, Op.getIndex(), Reg);
76 Op.ChangeToRegister(Reg, /*isDef=*/false);
77 Op.setIsDebug();
78 const DIExpression *DIExpr = MI.getDebugExpression();
79 if (MI.isNonListDebugValue()) {
80 DIExpr = TRI.prependOffsetExpression(MI.getDebugExpression(), DIExpression::ApplyOffset, Offset);
81 } else {
82 SmallVector<uint64_t, 3> Ops;
83 TRI.getOffsetOpcodes(Offset, Ops);
84 unsigned OpIdx = MI.getDebugOperandIndex(&Op);
85 DIExpr = DIExpression::appendOpsToArg(DIExpr, Ops, OpIdx);
87 MI.getDebugExpressionOp().setMetadata(DIExpr);
88 continue;
91 TRI.eliminateFrameIndex(MI, 0, i, nullptr);
92 Modified = true;
97 // Add function prolog/epilog
98 TFI.emitPrologue(MF, MF.front());
100 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
101 // If last instruction is a return instruction, add an epilogue
102 if (I->isReturnBlock())
103 TFI.emitEpilogue(MF, *I);
106 return Modified;
109 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
110 static inline void AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
111 bool StackGrowsDown, int64_t &Offset,
112 Align &MaxAlign) {
113 // If the stack grows down, add the object size to find the lowest address.
114 if (StackGrowsDown)
115 Offset += MFI.getObjectSize(FrameIdx);
117 Align Alignment = MFI.getObjectAlign(FrameIdx);
119 // If the alignment of this object is greater than that of the stack, then
120 // increase the stack alignment to match.
121 MaxAlign = std::max(MaxAlign, Alignment);
123 // Adjust to alignment boundary.
124 Offset = alignTo(Offset, Alignment);
126 if (StackGrowsDown) {
127 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
128 << "]\n");
129 MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
130 } else {
131 LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
132 << "]\n");
133 MFI.setObjectOffset(FrameIdx, Offset);
134 Offset += MFI.getObjectSize(FrameIdx);
138 void
139 NVPTXPrologEpilogPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
140 const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
141 const TargetRegisterInfo *RegInfo = Fn.getSubtarget().getRegisterInfo();
143 bool StackGrowsDown =
144 TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
146 // Loop over all of the stack objects, assigning sequential addresses...
147 MachineFrameInfo &MFI = Fn.getFrameInfo();
149 // Start at the beginning of the local area.
150 // The Offset is the distance from the stack top in the direction
151 // of stack growth -- so it's always nonnegative.
152 int LocalAreaOffset = TFI.getOffsetOfLocalArea();
153 if (StackGrowsDown)
154 LocalAreaOffset = -LocalAreaOffset;
155 assert(LocalAreaOffset >= 0
156 && "Local area offset should be in direction of stack growth");
157 int64_t Offset = LocalAreaOffset;
159 // If there are fixed sized objects that are preallocated in the local area,
160 // non-fixed objects can't be allocated right at the start of local area.
161 // We currently don't support filling in holes in between fixed sized
162 // objects, so we adjust 'Offset' to point to the end of last fixed sized
163 // preallocated object.
164 for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
165 int64_t FixedOff;
166 if (StackGrowsDown) {
167 // The maximum distance from the stack pointer is at lower address of
168 // the object -- which is given by offset. For down growing stack
169 // the offset is negative, so we negate the offset to get the distance.
170 FixedOff = -MFI.getObjectOffset(i);
171 } else {
172 // The maximum distance from the start pointer is at the upper
173 // address of the object.
174 FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
176 if (FixedOff > Offset) Offset = FixedOff;
179 // NOTE: We do not have a call stack
181 Align MaxAlign = MFI.getMaxAlign();
183 // No scavenger
185 // FIXME: Once this is working, then enable flag will change to a target
186 // check for whether the frame is large enough to want to use virtual
187 // frame index registers. Functions which don't want/need this optimization
188 // will continue to use the existing code path.
189 if (MFI.getUseLocalStackAllocationBlock()) {
190 Align Alignment = MFI.getLocalFrameMaxAlign();
192 // Adjust to alignment boundary.
193 Offset = alignTo(Offset, Alignment);
195 LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
197 // Resolve offsets for objects in the local block.
198 for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
199 std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
200 int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
201 LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
202 << "]\n");
203 MFI.setObjectOffset(Entry.first, FIOffset);
205 // Allocate the local block
206 Offset += MFI.getLocalFrameSize();
208 MaxAlign = std::max(Alignment, MaxAlign);
211 // No stack protector
213 // Then assign frame offsets to stack objects that are not used to spill
214 // callee saved registers.
215 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
216 if (MFI.isObjectPreAllocated(i) &&
217 MFI.getUseLocalStackAllocationBlock())
218 continue;
219 if (MFI.isDeadObjectIndex(i))
220 continue;
222 AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
225 // No scavenger
227 if (!TFI.targetHandlesStackFrameRounding()) {
228 // If we have reserved argument space for call sites in the function
229 // immediately on entry to the current function, count it as part of the
230 // overall stack size.
231 if (MFI.adjustsStack() && TFI.hasReservedCallFrame(Fn))
232 Offset += MFI.getMaxCallFrameSize();
234 // Round up the size to a multiple of the alignment. If the function has
235 // any calls or alloca's, align to the target's StackAlignment value to
236 // ensure that the callee's frame or the alloca data is suitably aligned;
237 // otherwise, for leaf functions, align to the TransientStackAlignment
238 // value.
239 Align StackAlign;
240 if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
241 (RegInfo->hasStackRealignment(Fn) && MFI.getObjectIndexEnd() != 0))
242 StackAlign = TFI.getStackAlign();
243 else
244 StackAlign = TFI.getTransientStackAlign();
246 // If the frame pointer is eliminated, all frame offsets will be relative to
247 // SP not FP. Align to MaxAlign so this works.
248 Offset = alignTo(Offset, std::max(StackAlign, MaxAlign));
251 // Update frame info to pretend that this is part of the stack...
252 int64_t StackSize = Offset - LocalAreaOffset;
253 MFI.setStackSize(StackSize);