blob: 85a354d0c12d4d173d23ce9af6eaf6915027c5d2 [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"
21
22using namespace llvm;
23
24bool RISCVFrameLowering::hasFP(const MachineFunction &MF) const { return true; }
25
Alex Bradburyb014e3d2017-12-11 12:34:11 +000026// Determines the size of the frame and maximum call frame size.
27void 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
60void 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 Bradbury9fea4882018-01-10 19:53:46 +000065 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
Alex Bradburyb014e3d2017-12-11 12:34:11 +000066 const RISCVInstrInfo *TII = STI.getInstrInfo();
67
68 if (DestReg == SrcReg && Val == 0)
69 return;
70
Alex Bradbury9fea4882018-01-10 19:53:46 +000071 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 Bradburyb014e3d2017-12-11 12:34:11 +000083
Alex Bradbury9fea4882018-01-10 19:53:46 +000084 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 Bradburyb014e3d2017-12-11 12:34:11 +000093}
94
95// Returns the register used to hold the frame pointer.
96static unsigned getFPReg(const RISCVSubtarget &STI) { return RISCV::X8; }
97
98// Returns the register used to hold the stack pointer.
99static unsigned getSPReg(const RISCVSubtarget &STI) { return RISCV::X2; }
100
Alex Bradbury89718422017-10-19 21:37:38 +0000101void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000102 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 Bradburyc85be0d2018-01-10 19:41:03 +0000111 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000112 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 Bradburyc85be0d2018-01-10 19:41:03 +0000145 adjustReg(MBB, MBBI, DL, FPReg, SPReg, StackSize - RVFI->getVarArgsSaveSize(),
146 MachineInstr::FrameSetup);
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000147}
Alex Bradbury89718422017-10-19 21:37:38 +0000148
149void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000150 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 Bradburyc85be0d2018-01-10 19:41:03 +0000159 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000160 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 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);
212 assert(hasFP(MF) && "Offset calculation incorrect if !hasFP");
213 Offset += RVFI->getVarArgsSaveSize();
Alex Bradbury660bcce2017-12-11 11:53:54 +0000214 }
215 return Offset;
216}
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000217
218void 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}