blob: e1448ba7e2bff8f8cfc02305353fc1650e02b46e [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 {
65 const RISCVInstrInfo *TII = STI.getInstrInfo();
66
67 if (DestReg == SrcReg && Val == 0)
68 return;
69
70 if (!isInt<12>(Val))
71 report_fatal_error("adjustReg cannot yet handle adjustments >12 bits");
72
73 BuildMI(MBB, MBBI, DL, TII->get(RISCV::ADDI), DestReg)
74 .addReg(SrcReg)
75 .addImm(Val)
76 .setMIFlag(Flag);
77}
78
79// Returns the register used to hold the frame pointer.
80static unsigned getFPReg(const RISCVSubtarget &STI) { return RISCV::X8; }
81
82// Returns the register used to hold the stack pointer.
83static unsigned getSPReg(const RISCVSubtarget &STI) { return RISCV::X2; }
84
Alex Bradbury89718422017-10-19 21:37:38 +000085void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
Alex Bradburyb014e3d2017-12-11 12:34:11 +000086 MachineBasicBlock &MBB) const {
87 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
88
89 if (!hasFP(MF)) {
90 report_fatal_error(
91 "emitPrologue doesn't support framepointer-less functions");
92 }
93
94 MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Bradburyc85be0d2018-01-10 19:41:03 +000095 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
Alex Bradburyb014e3d2017-12-11 12:34:11 +000096 MachineBasicBlock::iterator MBBI = MBB.begin();
97
98 unsigned FPReg = getFPReg(STI);
99 unsigned SPReg = getSPReg(STI);
100
101 // Debug location must be unknown since the first debug location is used
102 // to determine the end of the prologue.
103 DebugLoc DL;
104
105 // Determine the correct frame layout
106 determineFrameLayout(MF);
107
108 // FIXME (note copied from Lanai): This appears to be overallocating. Needs
109 // investigation. Get the number of bytes to allocate from the FrameInfo.
110 uint64_t StackSize = MFI.getStackSize();
111
112 // Early exit if there is no need to allocate on the stack
113 if (StackSize == 0 && !MFI.adjustsStack())
114 return;
115
116 // Allocate space on the stack if necessary.
117 adjustReg(MBB, MBBI, DL, SPReg, SPReg, -StackSize, MachineInstr::FrameSetup);
118
119 // The frame pointer is callee-saved, and code has been generated for us to
120 // save it to the stack. We need to skip over the storing of callee-saved
121 // registers as the frame pointer must be modified after it has been saved
122 // to the stack, not before.
123 // FIXME: assumes exactly one instruction is used to save each callee-saved
124 // register.
125 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
126 std::advance(MBBI, CSI.size());
127
128 // Generate new FP.
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000129 adjustReg(MBB, MBBI, DL, FPReg, SPReg, StackSize - RVFI->getVarArgsSaveSize(),
130 MachineInstr::FrameSetup);
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000131}
Alex Bradbury89718422017-10-19 21:37:38 +0000132
133void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000134 MachineBasicBlock &MBB) const {
135 if (!hasFP(MF)) {
136 report_fatal_error(
137 "emitEpilogue doesn't support framepointer-less functions");
138 }
139
140 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
141 const RISCVRegisterInfo *RI = STI.getRegisterInfo();
142 MachineFrameInfo &MFI = MF.getFrameInfo();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000143 auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000144 DebugLoc DL = MBBI->getDebugLoc();
145 unsigned FPReg = getFPReg(STI);
146 unsigned SPReg = getSPReg(STI);
147
148 // Skip to before the restores of callee-saved registers
149 // FIXME: assumes exactly one instruction is used to restore each
150 // callee-saved register.
151 MachineBasicBlock::iterator LastFrameDestroy = MBBI;
152 std::advance(LastFrameDestroy, -MFI.getCalleeSavedInfo().size());
153
154 uint64_t StackSize = MFI.getStackSize();
155
156 // Restore the stack pointer using the value of the frame pointer. Only
157 // necessary if the stack pointer was modified, meaning the stack size is
158 // unknown.
159 if (RI->needsStackRealignment(MF) || MFI.hasVarSizedObjects()) {
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000160 adjustReg(MBB, LastFrameDestroy, DL, SPReg, FPReg,
161 -StackSize + RVFI->getVarArgsSaveSize(),
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000162 MachineInstr::FrameDestroy);
163 }
164
165 // Deallocate stack
166 adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackSize, MachineInstr::FrameDestroy);
167}
Alex Bradbury660bcce2017-12-11 11:53:54 +0000168
169int RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF,
170 int FI,
171 unsigned &FrameReg) const {
172 const MachineFrameInfo &MFI = MF.getFrameInfo();
173 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000174 const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
Alex Bradbury660bcce2017-12-11 11:53:54 +0000175
176 // Callee-saved registers should be referenced relative to the stack
177 // pointer (positive offset), otherwise use the frame pointer (negative
178 // offset).
179 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
180 int MinCSFI = 0;
181 int MaxCSFI = -1;
182
183 int Offset = MFI.getObjectOffset(FI) - getOffsetOfLocalArea() +
184 MFI.getOffsetAdjustment();
185
186 if (CSI.size()) {
187 MinCSFI = CSI[0].getFrameIdx();
188 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
189 }
190
Alex Bradbury660bcce2017-12-11 11:53:54 +0000191 if (FI >= MinCSFI && FI <= MaxCSFI) {
192 FrameReg = RISCV::X2;
193 Offset += MF.getFrameInfo().getStackSize();
Alex Bradburyc85be0d2018-01-10 19:41:03 +0000194 } else {
195 FrameReg = RI->getFrameRegister(MF);
196 assert(hasFP(MF) && "Offset calculation incorrect if !hasFP");
197 Offset += RVFI->getVarArgsSaveSize();
Alex Bradbury660bcce2017-12-11 11:53:54 +0000198 }
199 return Offset;
200}
Alex Bradburyb014e3d2017-12-11 12:34:11 +0000201
202void RISCVFrameLowering::determineCalleeSaves(MachineFunction &MF,
203 BitVector &SavedRegs,
204 RegScavenger *RS) const {
205 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
206 // TODO: Once frame pointer elimination is implemented, don't
207 // unconditionally spill the frame pointer and return address.
208 SavedRegs.set(RISCV::X1);
209 SavedRegs.set(RISCV::X8);
210}