Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 1 | //===-- RISCVFrameLowering.cpp - RISCV Frame Information ------------------===// |
| 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 contains the RISCV implementation of TargetFrameLowering class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "RISCVFrameLowering.h" |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 15 | #include "RISCVMachineFunctionInfo.h" |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 16 | #include "RISCVSubtarget.h" |
| 17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
| 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | bool RISCVFrameLowering::hasFP(const MachineFunction &MF) const { return true; } |
| 25 | |
Alex Bradbury | b014e3d | 2017-12-11 12:34:11 +0000 | [diff] [blame] | 26 | // Determines the size of the frame and maximum call frame size. |
| 27 | void RISCVFrameLowering::determineFrameLayout(MachineFunction &MF) const { |
| 28 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 29 | const RISCVRegisterInfo *RI = STI.getRegisterInfo(); |
| 30 | |
| 31 | // Get the number of bytes to allocate from the FrameInfo. |
| 32 | uint64_t FrameSize = MFI.getStackSize(); |
| 33 | |
| 34 | // Get the alignment. |
| 35 | uint64_t StackAlign = RI->needsStackRealignment(MF) ? MFI.getMaxAlignment() |
| 36 | : getStackAlignment(); |
| 37 | |
| 38 | // Get the maximum call frame size of all the calls. |
| 39 | uint64_t MaxCallFrameSize = MFI.getMaxCallFrameSize(); |
| 40 | |
| 41 | // If we have dynamic alloca then MaxCallFrameSize needs to be aligned so |
| 42 | // that allocations will be aligned. |
| 43 | if (MFI.hasVarSizedObjects()) |
| 44 | MaxCallFrameSize = alignTo(MaxCallFrameSize, StackAlign); |
| 45 | |
| 46 | // Update maximum call frame size. |
| 47 | MFI.setMaxCallFrameSize(MaxCallFrameSize); |
| 48 | |
| 49 | // Include call frame size in total. |
| 50 | if (!(hasReservedCallFrame(MF) && MFI.adjustsStack())) |
| 51 | FrameSize += MaxCallFrameSize; |
| 52 | |
| 53 | // Make sure the frame is aligned. |
| 54 | FrameSize = alignTo(FrameSize, StackAlign); |
| 55 | |
| 56 | // Update frame info. |
| 57 | MFI.setStackSize(FrameSize); |
| 58 | } |
| 59 | |
| 60 | void RISCVFrameLowering::adjustReg(MachineBasicBlock &MBB, |
| 61 | MachineBasicBlock::iterator MBBI, |
| 62 | const DebugLoc &DL, unsigned DestReg, |
| 63 | unsigned SrcReg, int64_t Val, |
| 64 | MachineInstr::MIFlag Flag) const { |
Alex Bradbury | 9fea488 | 2018-01-10 19:53:46 +0000 | [diff] [blame^] | 65 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
Alex Bradbury | b014e3d | 2017-12-11 12:34:11 +0000 | [diff] [blame] | 66 | const RISCVInstrInfo *TII = STI.getInstrInfo(); |
| 67 | |
| 68 | if (DestReg == SrcReg && Val == 0) |
| 69 | return; |
| 70 | |
Alex Bradbury | 9fea488 | 2018-01-10 19:53:46 +0000 | [diff] [blame^] | 71 | if (isInt<12>(Val)) { |
| 72 | BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), DestReg) |
| 73 | .addReg(SrcReg) |
| 74 | .addImm(Val) |
| 75 | .setMIFlag(Flag); |
| 76 | } else if (isInt<32>(Val)) { |
| 77 | unsigned Opc = RISCV::ADD; |
| 78 | bool isSub = Val < 0; |
| 79 | if (isSub) { |
| 80 | Val = -Val; |
| 81 | Opc = RISCV::SUB; |
| 82 | } |
Alex Bradbury | b014e3d | 2017-12-11 12:34:11 +0000 | [diff] [blame] | 83 | |
Alex Bradbury | 9fea488 | 2018-01-10 19:53:46 +0000 | [diff] [blame^] | 84 | unsigned ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass); |
| 85 | TII->movImm32(MBB, MBBI, DL, ScratchReg, Val, Flag); |
| 86 | BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg) |
| 87 | .addReg(SrcReg) |
| 88 | .addReg(ScratchReg, RegState::Kill) |
| 89 | .setMIFlag(Flag); |
| 90 | } else { |
| 91 | report_fatal_error("adjustReg cannot yet handle adjustments >32 bits"); |
| 92 | } |
Alex Bradbury | b014e3d | 2017-12-11 12:34:11 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Returns the register used to hold the frame pointer. |
| 96 | static unsigned getFPReg(const RISCVSubtarget &STI) { return RISCV::X8; } |
| 97 | |
| 98 | // Returns the register used to hold the stack pointer. |
| 99 | static unsigned getSPReg(const RISCVSubtarget &STI) { return RISCV::X2; } |
| 100 | |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 101 | void RISCVFrameLowering::emitPrologue(MachineFunction &MF, |
Alex Bradbury | b014e3d | 2017-12-11 12:34:11 +0000 | [diff] [blame] | 102 | MachineBasicBlock &MBB) const { |
| 103 | assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported"); |
| 104 | |
| 105 | if (!hasFP(MF)) { |
| 106 | report_fatal_error( |
| 107 | "emitPrologue doesn't support framepointer-less functions"); |
| 108 | } |
| 109 | |
| 110 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 111 | auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>(); |
Alex Bradbury | b014e3d | 2017-12-11 12:34:11 +0000 | [diff] [blame] | 112 | MachineBasicBlock::iterator MBBI = MBB.begin(); |
| 113 | |
| 114 | unsigned FPReg = getFPReg(STI); |
| 115 | unsigned SPReg = getSPReg(STI); |
| 116 | |
| 117 | // Debug location must be unknown since the first debug location is used |
| 118 | // to determine the end of the prologue. |
| 119 | DebugLoc DL; |
| 120 | |
| 121 | // Determine the correct frame layout |
| 122 | determineFrameLayout(MF); |
| 123 | |
| 124 | // FIXME (note copied from Lanai): This appears to be overallocating. Needs |
| 125 | // investigation. Get the number of bytes to allocate from the FrameInfo. |
| 126 | uint64_t StackSize = MFI.getStackSize(); |
| 127 | |
| 128 | // Early exit if there is no need to allocate on the stack |
| 129 | if (StackSize == 0 && !MFI.adjustsStack()) |
| 130 | return; |
| 131 | |
| 132 | // Allocate space on the stack if necessary. |
| 133 | adjustReg(MBB, MBBI, DL, SPReg, SPReg, -StackSize, MachineInstr::FrameSetup); |
| 134 | |
| 135 | // The frame pointer is callee-saved, and code has been generated for us to |
| 136 | // save it to the stack. We need to skip over the storing of callee-saved |
| 137 | // registers as the frame pointer must be modified after it has been saved |
| 138 | // to the stack, not before. |
| 139 | // FIXME: assumes exactly one instruction is used to save each callee-saved |
| 140 | // register. |
| 141 | const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); |
| 142 | std::advance(MBBI, CSI.size()); |
| 143 | |
| 144 | // Generate new FP. |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 145 | adjustReg(MBB, MBBI, DL, FPReg, SPReg, StackSize - RVFI->getVarArgsSaveSize(), |
| 146 | MachineInstr::FrameSetup); |
Alex Bradbury | b014e3d | 2017-12-11 12:34:11 +0000 | [diff] [blame] | 147 | } |
Alex Bradbury | 8971842 | 2017-10-19 21:37:38 +0000 | [diff] [blame] | 148 | |
| 149 | void RISCVFrameLowering::emitEpilogue(MachineFunction &MF, |
Alex Bradbury | b014e3d | 2017-12-11 12:34:11 +0000 | [diff] [blame] | 150 | MachineBasicBlock &MBB) const { |
| 151 | if (!hasFP(MF)) { |
| 152 | report_fatal_error( |
| 153 | "emitEpilogue doesn't support framepointer-less functions"); |
| 154 | } |
| 155 | |
| 156 | MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); |
| 157 | const RISCVRegisterInfo *RI = STI.getRegisterInfo(); |
| 158 | MachineFrameInfo &MFI = MF.getFrameInfo(); |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 159 | auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>(); |
Alex Bradbury | b014e3d | 2017-12-11 12:34:11 +0000 | [diff] [blame] | 160 | DebugLoc DL = MBBI->getDebugLoc(); |
| 161 | unsigned FPReg = getFPReg(STI); |
| 162 | unsigned SPReg = getSPReg(STI); |
| 163 | |
| 164 | // Skip to before the restores of callee-saved registers |
| 165 | // FIXME: assumes exactly one instruction is used to restore each |
| 166 | // callee-saved register. |
| 167 | MachineBasicBlock::iterator LastFrameDestroy = MBBI; |
| 168 | std::advance(LastFrameDestroy, -MFI.getCalleeSavedInfo().size()); |
| 169 | |
| 170 | uint64_t StackSize = MFI.getStackSize(); |
| 171 | |
| 172 | // Restore the stack pointer using the value of the frame pointer. Only |
| 173 | // necessary if the stack pointer was modified, meaning the stack size is |
| 174 | // unknown. |
| 175 | if (RI->needsStackRealignment(MF) || MFI.hasVarSizedObjects()) { |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 176 | adjustReg(MBB, LastFrameDestroy, DL, SPReg, FPReg, |
| 177 | -StackSize + RVFI->getVarArgsSaveSize(), |
Alex Bradbury | b014e3d | 2017-12-11 12:34:11 +0000 | [diff] [blame] | 178 | MachineInstr::FrameDestroy); |
| 179 | } |
| 180 | |
| 181 | // Deallocate stack |
| 182 | adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackSize, MachineInstr::FrameDestroy); |
| 183 | } |
Alex Bradbury | 660bcce | 2017-12-11 11:53:54 +0000 | [diff] [blame] | 184 | |
| 185 | int RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, |
| 186 | int FI, |
| 187 | unsigned &FrameReg) const { |
| 188 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 189 | const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo(); |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 190 | const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>(); |
Alex Bradbury | 660bcce | 2017-12-11 11:53:54 +0000 | [diff] [blame] | 191 | |
| 192 | // Callee-saved registers should be referenced relative to the stack |
| 193 | // pointer (positive offset), otherwise use the frame pointer (negative |
| 194 | // offset). |
| 195 | const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); |
| 196 | int MinCSFI = 0; |
| 197 | int MaxCSFI = -1; |
| 198 | |
| 199 | int Offset = MFI.getObjectOffset(FI) - getOffsetOfLocalArea() + |
| 200 | MFI.getOffsetAdjustment(); |
| 201 | |
| 202 | if (CSI.size()) { |
| 203 | MinCSFI = CSI[0].getFrameIdx(); |
| 204 | MaxCSFI = CSI[CSI.size() - 1].getFrameIdx(); |
| 205 | } |
| 206 | |
Alex Bradbury | 660bcce | 2017-12-11 11:53:54 +0000 | [diff] [blame] | 207 | if (FI >= MinCSFI && FI <= MaxCSFI) { |
| 208 | FrameReg = RISCV::X2; |
| 209 | Offset += MF.getFrameInfo().getStackSize(); |
Alex Bradbury | c85be0d | 2018-01-10 19:41:03 +0000 | [diff] [blame] | 210 | } else { |
| 211 | FrameReg = RI->getFrameRegister(MF); |
| 212 | assert(hasFP(MF) && "Offset calculation incorrect if !hasFP"); |
| 213 | Offset += RVFI->getVarArgsSaveSize(); |
Alex Bradbury | 660bcce | 2017-12-11 11:53:54 +0000 | [diff] [blame] | 214 | } |
| 215 | return Offset; |
| 216 | } |
Alex Bradbury | b014e3d | 2017-12-11 12:34:11 +0000 | [diff] [blame] | 217 | |
| 218 | void RISCVFrameLowering::determineCalleeSaves(MachineFunction &MF, |
| 219 | BitVector &SavedRegs, |
| 220 | RegScavenger *RS) const { |
| 221 | TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); |
| 222 | // TODO: Once frame pointer elimination is implemented, don't |
| 223 | // unconditionally spill the frame pointer and return address. |
| 224 | SavedRegs.set(RISCV::X1); |
| 225 | SavedRegs.set(RISCV::X8); |
| 226 | } |