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