blob: 33703f5ec2053b0acffaa34efb84e5b30ac0d50f [file] [log] [blame]
Alex Bradbury89718422017-10-19 21:37:38 +00001//===-- 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 Bradburyc85be0d2018-01-10 19:41:03 +000015#include "RISCVMachineFunctionInfo.h"
Alex Bradbury89718422017-10-19 21:37:38 +000016#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"
Alex Bradbury0715d352018-01-11 11:17:19 +000021#include "llvm/CodeGen/RegisterScavenging.h"
Alex Bradbury89718422017-10-19 21:37:38 +000022
23using namespace llvm;
24
Alex Bradbury7d6aa1f2018-01-18 11:34:02 +000025bool RISCVFrameLowering::hasFP(const MachineFunction &MF) const {
26 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
27
28 const MachineFrameInfo &MFI = MF.getFrameInfo();
29 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
30 RegInfo->needsStackRealignment(MF) || MFI.hasVarSizedObjects() ||
31 MFI.isFrameAddressTaken();
32}
Alex Bradbury89718422017-10-19 21:37:38 +000033
Alex Bradburyb014e3d2017-12-11 12:34:11 +000034// Determines the size of the frame and maximum call frame size.
35void RISCVFrameLowering::determineFrameLayout(MachineFunction &MF) const {
36 MachineFrameInfo &MFI = MF.getFrameInfo();
37 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
38
39 // Get the number of bytes to allocate from the FrameInfo.
40 uint64_t FrameSize = MFI.getStackSize();
41
42 // Get the alignment.
43 uint64_t StackAlign = RI->needsStackRealignment(MF) ? MFI.getMaxAlignment()
44 : getStackAlignment();
45
46 // Get the maximum call frame size of all the calls.
47 uint64_t MaxCallFrameSize = MFI.getMaxCallFrameSize();
48
49 // If we have dynamic alloca then MaxCallFrameSize needs to be aligned so
50 // that allocations will be aligned.
51 if (MFI.hasVarSizedObjects())
52 MaxCallFrameSize = alignTo(MaxCallFrameSize, StackAlign);
53
54 // Update maximum call frame size.
55 MFI.setMaxCallFrameSize(MaxCallFrameSize);
56
57 // Include call frame size in total.
58 if (!(hasReservedCallFrame(MF) && MFI.adjustsStack()))
59 FrameSize += MaxCallFrameSize;
60
61 // Make sure the frame is aligned.
62 FrameSize = alignTo(FrameSize, StackAlign);
63
64 // Update frame info.
65 MFI.setStackSize(FrameSize);
66}
67
68void RISCVFrameLowering::adjustReg(MachineBasicBlock &MBB,
69 MachineBasicBlock::iterator MBBI,
70 const DebugLoc &DL, unsigned DestReg,
71 unsigned SrcReg, int64_t Val,
72 MachineInstr::MIFlag Flag) const {
Alex Bradbury9fea4882018-01-10 19:53:46 +000073 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
Alex Bradburyb014e3d2017-12-11 12:34:11 +000074 const RISCVInstrInfo *TII = STI.getInstrInfo();
75
76 if (DestReg == SrcReg && Val == 0)
77 return;
78
Alex Bradbury9fea4882018-01-10 19:53:46 +000079 if (isInt<12>(Val)) {
80 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), DestReg)
81 .addReg(SrcReg)
82 .addImm(Val)
83 .setMIFlag(Flag);
84 } else if (isInt<32>(Val)) {
85 unsigned Opc = RISCV::ADD;
86 bool isSub = Val < 0;
87 if (isSub) {
88 Val = -Val;
89 Opc = RISCV::SUB;
90 }
Alex Bradburyb014e3d2017-12-11 12:34:11 +000091
Alex Bradbury9fea4882018-01-10 19:53:46 +000092 unsigned ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
93 TII->movImm32(MBB, MBBI, DL, ScratchReg, Val, Flag);
94 BuildMI(MBB, MBBI, DL, TII->get(Opc), DestReg)
95 .addReg(SrcReg)
96 .addReg(ScratchReg, RegState::Kill)
97 .setMIFlag(Flag);
98 } else {
99 report_fatal_error("adjustReg cannot yet handle adjustments >32 bits");
100 }
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000101}
102
103// Returns the register used to hold the frame pointer.
104static unsigned getFPReg(const RISCVSubtarget &STI) { return RISCV::X8; }
105
106// Returns the register used to hold the stack pointer.
107static unsigned getSPReg(const RISCVSubtarget &STI) { return RISCV::X2; }
108
Alex Bradbury89718422017-10-19 21:37:38 +0000109void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000110 MachineBasicBlock &MBB) const {
111 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
112
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000113 MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000114 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000115 MachineBasicBlock::iterator MBBI = MBB.begin();
116
117 unsigned FPReg = getFPReg(STI);
118 unsigned SPReg = getSPReg(STI);
119
120 // Debug location must be unknown since the first debug location is used
121 // to determine the end of the prologue.
122 DebugLoc DL;
123
124 // Determine the correct frame layout
125 determineFrameLayout(MF);
126
127 // FIXME (note copied from Lanai): This appears to be overallocating. Needs
128 // investigation. Get the number of bytes to allocate from the FrameInfo.
129 uint64_t StackSize = MFI.getStackSize();
130
131 // Early exit if there is no need to allocate on the stack
132 if (StackSize == 0 && !MFI.adjustsStack())
133 return;
134
135 // Allocate space on the stack if necessary.
136 adjustReg(MBB, MBBI, DL, SPReg, SPReg, -StackSize, MachineInstr::FrameSetup);
137
138 // The frame pointer is callee-saved, and code has been generated for us to
139 // save it to the stack. We need to skip over the storing of callee-saved
140 // registers as the frame pointer must be modified after it has been saved
141 // to the stack, not before.
142 // FIXME: assumes exactly one instruction is used to save each callee-saved
143 // register.
144 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
145 std::advance(MBBI, CSI.size());
146
147 // Generate new FP.
Alex Bradbury7d6aa1f2018-01-18 11:34:02 +0000148 if (hasFP(MF))
149 adjustReg(MBB, MBBI, DL, FPReg, SPReg,
150 StackSize - RVFI->getVarArgsSaveSize(), MachineInstr::FrameSetup);
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000151}
Alex Bradbury89718422017-10-19 21:37:38 +0000152
153void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000154 MachineBasicBlock &MBB) const {
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000155 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
156 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
157 MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000158 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000159 DebugLoc DL = MBBI->getDebugLoc();
160 unsigned FPReg = getFPReg(STI);
161 unsigned SPReg = getSPReg(STI);
162
163 // Skip to before the restores of callee-saved registers
164 // FIXME: assumes exactly one instruction is used to restore each
165 // callee-saved register.
166 MachineBasicBlock::iterator LastFrameDestroy = MBBI;
167 std::advance(LastFrameDestroy, -MFI.getCalleeSavedInfo().size());
168
169 uint64_t StackSize = MFI.getStackSize();
170
171 // Restore the stack pointer using the value of the frame pointer. Only
172 // necessary if the stack pointer was modified, meaning the stack size is
173 // unknown.
174 if (RI->needsStackRealignment(MF) || MFI.hasVarSizedObjects()) {
Alex Bradbury7d6aa1f2018-01-18 11:34:02 +0000175 assert(hasFP(MF) && "frame pointer should not have been eliminated");
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000176 adjustReg(MBB, LastFrameDestroy, DL, SPReg, FPReg,
177 -StackSize + RVFI->getVarArgsSaveSize(),
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000178 MachineInstr::FrameDestroy);
179 }
180
181 // Deallocate stack
182 adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackSize, MachineInstr::FrameDestroy);
183}
Alex Bradbury660bcce2017-12-11 11:53:54 +0000184
185int 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 Bradburyc85be0d2018-01-10 19:41:03 +0000190 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
Alex Bradbury660bcce2017-12-11 11:53:54 +0000191
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 Bradbury660bcce2017-12-11 11:53:54 +0000207 if (FI >= MinCSFI && FI <= MaxCSFI) {
208 FrameReg = RISCV::X2;
209 Offset += MF.getFrameInfo().getStackSize();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000210 } else {
211 FrameReg = RI->getFrameRegister(MF);
Alex Bradbury7d6aa1f2018-01-18 11:34:02 +0000212 if (hasFP(MF))
213 Offset += RVFI->getVarArgsSaveSize();
214 else
215 Offset += MF.getFrameInfo().getStackSize();
Alex Bradbury660bcce2017-12-11 11:53:54 +0000216 }
217 return Offset;
218}
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000219
220void RISCVFrameLowering::determineCalleeSaves(MachineFunction &MF,
221 BitVector &SavedRegs,
222 RegScavenger *RS) const {
223 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Alex Bradbury7d6aa1f2018-01-18 11:34:02 +0000224 // Unconditionally spill RA and FP only if the function uses a frame
225 // pointer.
226 if (hasFP(MF)) {
227 SavedRegs.set(RISCV::X1);
228 SavedRegs.set(RISCV::X8);
229 }
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000230}
Alex Bradbury0715d352018-01-11 11:17:19 +0000231
232void RISCVFrameLowering::processFunctionBeforeFrameFinalized(
233 MachineFunction &MF, RegScavenger *RS) const {
234 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
235 MachineFrameInfo &MFI = MF.getFrameInfo();
236 const TargetRegisterClass *RC = &RISCV::GPRRegClass;
237 // estimateStackSize has been observed to under-estimate the final stack
238 // size, so give ourselves wiggle-room by checking for stack size
239 // representable an 11-bit signed field rather than 12-bits.
240 // FIXME: It may be possible to craft a function with a small stack that
241 // still needs an emergency spill slot for branch relaxation. This case
242 // would currently be missed.
243 if (!isInt<11>(MFI.estimateStackSize(MF))) {
244 int RegScavFI = MFI.CreateStackObject(
245 RegInfo->getSpillSize(*RC), RegInfo->getSpillAlignment(*RC), false);
246 RS->addScavengingFrameIndex(RegScavFI);
247 }
248}