Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 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" |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
| 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/TargetFrameLowering.h" |
| 21 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 22 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 23 | #include "llvm/Pass.h" |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "nvptx-prolog-epilog" |
| 30 | |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | class NVPTXPrologEpilogPass : public MachineFunctionPass { |
| 33 | public: |
| 34 | static char ID; |
| 35 | NVPTXPrologEpilogPass() : MachineFunctionPass(ID) {} |
| 36 | |
Craig Topper | 2865c98 | 2014-04-29 07:57:44 +0000 | [diff] [blame] | 37 | bool runOnMachineFunction(MachineFunction &MF) override; |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 38 | |
| 39 | private: |
| 40 | void calculateFrameObjectOffsets(MachineFunction &Fn); |
| 41 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 42 | } |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 43 | |
| 44 | MachineFunctionPass *llvm::createNVPTXPrologEpilogPass() { |
| 45 | return new NVPTXPrologEpilogPass(); |
| 46 | } |
| 47 | |
| 48 | char NVPTXPrologEpilogPass::ID = 0; |
| 49 | |
| 50 | bool NVPTXPrologEpilogPass::runOnMachineFunction(MachineFunction &MF) { |
Eric Christopher | 3084aff | 2015-01-30 01:50:09 +0000 | [diff] [blame] | 51 | const TargetSubtargetInfo &STI = MF.getSubtarget(); |
| 52 | const TargetFrameLowering &TFI = *STI.getFrameLowering(); |
| 53 | const TargetRegisterInfo &TRI = *STI.getRegisterInfo(); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 54 | bool Modified = false; |
| 55 | |
| 56 | calculateFrameObjectOffsets(MF); |
| 57 | |
Duncan P. N. Exon Smith | 68f499a | 2016-07-08 21:10:58 +0000 | [diff] [blame] | 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()) |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 62 | continue; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 63 | TRI.eliminateFrameIndex(MI, 0, i, nullptr); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 64 | Modified = true; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Add function prolog/epilog |
Quentin Colombet | 61b305e | 2015-05-05 17:38:16 +0000 | [diff] [blame] | 70 | TFI.emitPrologue(MF, MF.front()); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 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 |
Matthias Braun | c2d4bef | 2015-09-25 21:25:19 +0000 | [diff] [blame] | 74 | if (I->isReturnBlock()) |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 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 |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 83 | AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx, |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 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) |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 88 | Offset += MFI.getObjectSize(FrameIdx); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 89 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 90 | unsigned Align = MFI.getObjectAlignment(FrameIdx); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 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 | DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset << "]\n"); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 101 | MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 102 | } else { |
| 103 | DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset << "]\n"); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 104 | MFI.setObjectOffset(FrameIdx, Offset); |
| 105 | Offset += MFI.getObjectSize(FrameIdx); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | NVPTXPrologEpilogPass::calculateFrameObjectOffsets(MachineFunction &Fn) { |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 111 | const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering(); |
| 112 | const TargetRegisterInfo *RegInfo = Fn.getSubtarget().getRegisterInfo(); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 113 | |
| 114 | bool StackGrowsDown = |
| 115 | TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown; |
| 116 | |
| 117 | // Loop over all of the stack objects, assigning sequential addresses... |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 118 | MachineFrameInfo &MFI = Fn.getFrameInfo(); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 119 | |
| 120 | // Start at the beginning of the local area. |
| 121 | // The Offset is the distance from the stack top in the direction |
| 122 | // of stack growth -- so it's always nonnegative. |
| 123 | int LocalAreaOffset = TFI.getOffsetOfLocalArea(); |
| 124 | if (StackGrowsDown) |
| 125 | LocalAreaOffset = -LocalAreaOffset; |
| 126 | assert(LocalAreaOffset >= 0 |
| 127 | && "Local area offset should be in direction of stack growth"); |
| 128 | int64_t Offset = LocalAreaOffset; |
| 129 | |
| 130 | // If there are fixed sized objects that are preallocated in the local area, |
| 131 | // non-fixed objects can't be allocated right at the start of local area. |
| 132 | // We currently don't support filling in holes in between fixed sized |
| 133 | // objects, so we adjust 'Offset' to point to the end of last fixed sized |
| 134 | // preallocated object. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 135 | for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) { |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 136 | int64_t FixedOff; |
| 137 | if (StackGrowsDown) { |
| 138 | // The maximum distance from the stack pointer is at lower address of |
| 139 | // the object -- which is given by offset. For down growing stack |
| 140 | // the offset is negative, so we negate the offset to get the distance. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 141 | FixedOff = -MFI.getObjectOffset(i); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 142 | } else { |
| 143 | // The maximum distance from the start pointer is at the upper |
| 144 | // address of the object. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 145 | FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 146 | } |
| 147 | if (FixedOff > Offset) Offset = FixedOff; |
| 148 | } |
| 149 | |
| 150 | // NOTE: We do not have a call stack |
| 151 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 152 | unsigned MaxAlign = MFI.getMaxAlignment(); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 153 | |
| 154 | // No scavenger |
| 155 | |
| 156 | // FIXME: Once this is working, then enable flag will change to a target |
| 157 | // check for whether the frame is large enough to want to use virtual |
| 158 | // frame index registers. Functions which don't want/need this optimization |
| 159 | // will continue to use the existing code path. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 160 | if (MFI.getUseLocalStackAllocationBlock()) { |
| 161 | unsigned Align = MFI.getLocalFrameMaxAlign(); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 162 | |
| 163 | // Adjust to alignment boundary. |
| 164 | Offset = (Offset + Align - 1) / Align * Align; |
| 165 | |
| 166 | DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n"); |
| 167 | |
| 168 | // Resolve offsets for objects in the local block. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 169 | for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) { |
| 170 | std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 171 | int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second; |
| 172 | DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << |
| 173 | FIOffset << "]\n"); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 174 | MFI.setObjectOffset(Entry.first, FIOffset); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 175 | } |
| 176 | // Allocate the local block |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 177 | Offset += MFI.getLocalFrameSize(); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 178 | |
| 179 | MaxAlign = std::max(Align, MaxAlign); |
| 180 | } |
| 181 | |
| 182 | // No stack protector |
| 183 | |
| 184 | // Then assign frame offsets to stack objects that are not used to spill |
| 185 | // callee saved registers. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 186 | for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) { |
| 187 | if (MFI.isObjectPreAllocated(i) && |
| 188 | MFI.getUseLocalStackAllocationBlock()) |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 189 | continue; |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 190 | if (MFI.isDeadObjectIndex(i)) |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 191 | continue; |
| 192 | |
| 193 | AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign); |
| 194 | } |
| 195 | |
| 196 | // No scavenger |
| 197 | |
| 198 | if (!TFI.targetHandlesStackFrameRounding()) { |
| 199 | // If we have reserved argument space for call sites in the function |
| 200 | // immediately on entry to the current function, count it as part of the |
| 201 | // overall stack size. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 202 | if (MFI.adjustsStack() && TFI.hasReservedCallFrame(Fn)) |
| 203 | Offset += MFI.getMaxCallFrameSize(); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 204 | |
| 205 | // Round up the size to a multiple of the alignment. If the function has |
| 206 | // any calls or alloca's, align to the target's StackAlignment value to |
| 207 | // ensure that the callee's frame or the alloca data is suitably aligned; |
| 208 | // otherwise, for leaf functions, align to the TransientStackAlignment |
| 209 | // value. |
| 210 | unsigned StackAlign; |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 211 | if (MFI.adjustsStack() || MFI.hasVarSizedObjects() || |
| 212 | (RegInfo->needsStackRealignment(Fn) && MFI.getObjectIndexEnd() != 0)) |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 213 | StackAlign = TFI.getStackAlignment(); |
| 214 | else |
| 215 | StackAlign = TFI.getTransientStackAlignment(); |
| 216 | |
| 217 | // If the frame pointer is eliminated, all frame offsets will be relative to |
| 218 | // SP not FP. Align to MaxAlign so this works. |
| 219 | StackAlign = std::max(StackAlign, MaxAlign); |
| 220 | unsigned AlignMask = StackAlign - 1; |
| 221 | Offset = (Offset + AlignMask) & ~uint64_t(AlignMask); |
| 222 | } |
| 223 | |
| 224 | // Update frame info to pretend that this is part of the stack... |
| 225 | int64_t StackSize = Offset - LocalAreaOffset; |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 226 | MFI.setStackSize(StackSize); |
Justin Holewinski | dbb3b2f | 2013-05-31 12:14:49 +0000 | [diff] [blame] | 227 | } |