1480093f4SDimitry Andric //===-- VEFrameLowering.cpp - VE Frame Information ------------------------===//
2480093f4SDimitry Andric //
3480093f4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4480093f4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5480093f4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6480093f4SDimitry Andric //
7480093f4SDimitry Andric //===----------------------------------------------------------------------===//
8480093f4SDimitry Andric //
9480093f4SDimitry Andric // This file contains the VE implementation of TargetFrameLowering class.
10480093f4SDimitry Andric //
11e8d8bef9SDimitry Andric // On VE, stack frames are structured as follows:
12e8d8bef9SDimitry Andric //
13e8d8bef9SDimitry Andric // The stack grows downward.
14e8d8bef9SDimitry Andric //
15e8d8bef9SDimitry Andric // All of the individual frame areas on the frame below are optional, i.e. it's
16e8d8bef9SDimitry Andric // possible to create a function so that the particular area isn't present
17e8d8bef9SDimitry Andric // in the frame.
18e8d8bef9SDimitry Andric //
19e8d8bef9SDimitry Andric // At function entry, the "frame" looks as follows:
20e8d8bef9SDimitry Andric //
21e8d8bef9SDimitry Andric // | | Higher address
22e8d8bef9SDimitry Andric // |----------------------------------------------|
23e8d8bef9SDimitry Andric // | Parameter area for this function |
24e8d8bef9SDimitry Andric // |----------------------------------------------|
25e8d8bef9SDimitry Andric // | Register save area (RSA) for this function |
26e8d8bef9SDimitry Andric // |----------------------------------------------|
27e8d8bef9SDimitry Andric // | Return address for this function |
28e8d8bef9SDimitry Andric // |----------------------------------------------|
29e8d8bef9SDimitry Andric // | Frame pointer for this function |
30e8d8bef9SDimitry Andric // |----------------------------------------------| <- sp
31e8d8bef9SDimitry Andric // | | Lower address
32e8d8bef9SDimitry Andric //
33e8d8bef9SDimitry Andric // VE doesn't use on demand stack allocation, so user code generated by LLVM
34e8d8bef9SDimitry Andric // needs to call VEOS to allocate stack frame. VE's ABI want to reduce the
35e8d8bef9SDimitry Andric // number of VEOS calls, so ABI requires to allocate not only RSA (in general
36e8d8bef9SDimitry Andric // CSR, callee saved register) area but also call frame at the prologue of
37e8d8bef9SDimitry Andric // caller function.
38e8d8bef9SDimitry Andric //
39e8d8bef9SDimitry Andric // After the prologue has run, the frame has the following general structure.
40e8d8bef9SDimitry Andric // Note that technically the last frame area (VLAs) doesn't get created until
41e8d8bef9SDimitry Andric // in the main function body, after the prologue is run. However, it's depicted
42e8d8bef9SDimitry Andric // here for completeness.
43e8d8bef9SDimitry Andric //
44e8d8bef9SDimitry Andric // | | Higher address
45e8d8bef9SDimitry Andric // |----------------------------------------------|
46e8d8bef9SDimitry Andric // | Parameter area for this function |
47e8d8bef9SDimitry Andric // |----------------------------------------------|
48e8d8bef9SDimitry Andric // | Register save area (RSA) for this function |
49e8d8bef9SDimitry Andric // |----------------------------------------------|
50e8d8bef9SDimitry Andric // | Return address for this function |
51e8d8bef9SDimitry Andric // |----------------------------------------------|
52e8d8bef9SDimitry Andric // | Frame pointer for this function |
53e8d8bef9SDimitry Andric // |----------------------------------------------| <- fp(=old sp)
54e8d8bef9SDimitry Andric // |.empty.space.to.make.part.below.aligned.in....|
55e8d8bef9SDimitry Andric // |.case.it.needs.more.than.the.standard.16-byte.| (size of this area is
56e8d8bef9SDimitry Andric // |.alignment....................................| unknown at compile time)
57e8d8bef9SDimitry Andric // |----------------------------------------------|
58e8d8bef9SDimitry Andric // | Local variables of fixed size including spill|
59e8d8bef9SDimitry Andric // | slots |
60e8d8bef9SDimitry Andric // |----------------------------------------------| <- bp(not defined by ABI,
61e8d8bef9SDimitry Andric // |.variable-sized.local.variables.(VLAs)........| LLVM chooses SX17)
62e8d8bef9SDimitry Andric // |..............................................| (size of this area is
63e8d8bef9SDimitry Andric // |..............................................| unknown at compile time)
64e8d8bef9SDimitry Andric // |----------------------------------------------| <- stack top (returned by
65e8d8bef9SDimitry Andric // | Parameter area for callee | alloca)
66e8d8bef9SDimitry Andric // |----------------------------------------------|
67e8d8bef9SDimitry Andric // | Register save area (RSA) for callee |
68e8d8bef9SDimitry Andric // |----------------------------------------------|
69e8d8bef9SDimitry Andric // | Return address for callee |
70e8d8bef9SDimitry Andric // |----------------------------------------------|
71e8d8bef9SDimitry Andric // | Frame pointer for callee |
72e8d8bef9SDimitry Andric // |----------------------------------------------| <- sp
73e8d8bef9SDimitry Andric // | | Lower address
74e8d8bef9SDimitry Andric //
75e8d8bef9SDimitry Andric // To access the data in a frame, at-compile time, a constant offset must be
76e8d8bef9SDimitry Andric // computable from one of the pointers (fp, bp, sp) to access it. The size
77e8d8bef9SDimitry Andric // of the areas with a dotted background cannot be computed at compile-time
78e8d8bef9SDimitry Andric // if they are present, making it required to have all three of fp, bp and
79e8d8bef9SDimitry Andric // sp to be set up to be able to access all contents in the frame areas,
80e8d8bef9SDimitry Andric // assuming all of the frame areas are non-empty.
81e8d8bef9SDimitry Andric //
82e8d8bef9SDimitry Andric // For most functions, some of the frame areas are empty. For those functions,
83e8d8bef9SDimitry Andric // it may not be necessary to set up fp or bp:
84e8d8bef9SDimitry Andric // * A base pointer is definitely needed when there are both VLAs and local
85e8d8bef9SDimitry Andric // variables with more-than-default alignment requirements.
86e8d8bef9SDimitry Andric // * A frame pointer is definitely needed when there are local variables with
87e8d8bef9SDimitry Andric // more-than-default alignment requirements.
88e8d8bef9SDimitry Andric //
89e8d8bef9SDimitry Andric // In addition, VE ABI defines RSA frame, return address, and frame pointer
90e8d8bef9SDimitry Andric // as follows:
91e8d8bef9SDimitry Andric //
92e8d8bef9SDimitry Andric // |----------------------------------------------| <- sp+176
93e8d8bef9SDimitry Andric // | %s18...%s33 |
94e8d8bef9SDimitry Andric // |----------------------------------------------| <- sp+48
95e8d8bef9SDimitry Andric // | Linkage area register (%s17) |
96e8d8bef9SDimitry Andric // |----------------------------------------------| <- sp+40
97e8d8bef9SDimitry Andric // | Procedure linkage table register (%plt=%s16) |
98e8d8bef9SDimitry Andric // |----------------------------------------------| <- sp+32
99e8d8bef9SDimitry Andric // | Global offset table register (%got=%s15) |
100e8d8bef9SDimitry Andric // |----------------------------------------------| <- sp+24
101e8d8bef9SDimitry Andric // | Thread pointer register (%tp=%s14) |
102e8d8bef9SDimitry Andric // |----------------------------------------------| <- sp+16
103e8d8bef9SDimitry Andric // | Return address |
104e8d8bef9SDimitry Andric // |----------------------------------------------| <- sp+8
105e8d8bef9SDimitry Andric // | Frame pointer |
106e8d8bef9SDimitry Andric // |----------------------------------------------| <- sp+0
107e8d8bef9SDimitry Andric //
108e8d8bef9SDimitry Andric // NOTE: This description is based on VE ABI and description in
109e8d8bef9SDimitry Andric // AArch64FrameLowering.cpp. Thanks a lot.
110480093f4SDimitry Andric //===----------------------------------------------------------------------===//
111480093f4SDimitry Andric
112480093f4SDimitry Andric #include "VEFrameLowering.h"
113480093f4SDimitry Andric #include "VEInstrInfo.h"
1145ffd83dbSDimitry Andric #include "VEMachineFunctionInfo.h"
115480093f4SDimitry Andric #include "VESubtarget.h"
116480093f4SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
117480093f4SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
118480093f4SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
119480093f4SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
120480093f4SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
121480093f4SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
122480093f4SDimitry Andric #include "llvm/IR/DataLayout.h"
123480093f4SDimitry Andric #include "llvm/IR/Function.h"
124480093f4SDimitry Andric #include "llvm/Support/CommandLine.h"
125480093f4SDimitry Andric #include "llvm/Target/TargetOptions.h"
126480093f4SDimitry Andric #include "llvm/Support/MathExtras.h"
127480093f4SDimitry Andric
128480093f4SDimitry Andric using namespace llvm;
129480093f4SDimitry Andric
VEFrameLowering(const VESubtarget & ST)130480093f4SDimitry Andric VEFrameLowering::VEFrameLowering(const VESubtarget &ST)
131480093f4SDimitry Andric : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(16), 0,
1325ffd83dbSDimitry Andric Align(16)),
1335ffd83dbSDimitry Andric STI(ST) {}
134480093f4SDimitry Andric
emitPrologueInsns(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,uint64_t NumBytes,bool RequireFPUpdate) const135480093f4SDimitry Andric void VEFrameLowering::emitPrologueInsns(MachineFunction &MF,
136480093f4SDimitry Andric MachineBasicBlock &MBB,
137480093f4SDimitry Andric MachineBasicBlock::iterator MBBI,
1385ffd83dbSDimitry Andric uint64_t NumBytes,
139480093f4SDimitry Andric bool RequireFPUpdate) const {
140e8d8bef9SDimitry Andric const VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();
141e8d8bef9SDimitry Andric DebugLoc DL;
142e8d8bef9SDimitry Andric const VEInstrInfo &TII = *STI.getInstrInfo();
143480093f4SDimitry Andric
144480093f4SDimitry Andric // Insert following codes here as prologue
145480093f4SDimitry Andric //
146e8d8bef9SDimitry Andric // st %fp, 0(, %sp) iff !isLeafProc
147e8d8bef9SDimitry Andric // st %lr, 8(, %sp) iff !isLeafProc
148e8d8bef9SDimitry Andric // st %got, 24(, %sp) iff hasGOT
149e8d8bef9SDimitry Andric // st %plt, 32(, %sp) iff hasGOT
150e8d8bef9SDimitry Andric // st %s17, 40(, %sp) iff hasBP
151e8d8bef9SDimitry Andric if (!FuncInfo->isLeafProc()) {
152e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::STrii))
153480093f4SDimitry Andric .addReg(VE::SX11)
154480093f4SDimitry Andric .addImm(0)
1555ffd83dbSDimitry Andric .addImm(0)
156480093f4SDimitry Andric .addReg(VE::SX9);
157e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::STrii))
158480093f4SDimitry Andric .addReg(VE::SX11)
1595ffd83dbSDimitry Andric .addImm(0)
160480093f4SDimitry Andric .addImm(8)
161480093f4SDimitry Andric .addReg(VE::SX10);
162e8d8bef9SDimitry Andric }
163e8d8bef9SDimitry Andric if (hasGOT(MF)) {
164e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::STrii))
165480093f4SDimitry Andric .addReg(VE::SX11)
1665ffd83dbSDimitry Andric .addImm(0)
167480093f4SDimitry Andric .addImm(24)
168480093f4SDimitry Andric .addReg(VE::SX15);
169e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::STrii))
170480093f4SDimitry Andric .addReg(VE::SX11)
1715ffd83dbSDimitry Andric .addImm(0)
172480093f4SDimitry Andric .addImm(32)
173480093f4SDimitry Andric .addReg(VE::SX16);
174e8d8bef9SDimitry Andric }
1755ffd83dbSDimitry Andric if (hasBP(MF))
176e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::STrii))
1775ffd83dbSDimitry Andric .addReg(VE::SX11)
1785ffd83dbSDimitry Andric .addImm(0)
1795ffd83dbSDimitry Andric .addImm(40)
1805ffd83dbSDimitry Andric .addReg(VE::SX17);
181480093f4SDimitry Andric }
182480093f4SDimitry Andric
emitEpilogueInsns(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,uint64_t NumBytes,bool RequireFPUpdate) const183480093f4SDimitry Andric void VEFrameLowering::emitEpilogueInsns(MachineFunction &MF,
184480093f4SDimitry Andric MachineBasicBlock &MBB,
185480093f4SDimitry Andric MachineBasicBlock::iterator MBBI,
1865ffd83dbSDimitry Andric uint64_t NumBytes,
187480093f4SDimitry Andric bool RequireFPUpdate) const {
188e8d8bef9SDimitry Andric const VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();
189e8d8bef9SDimitry Andric DebugLoc DL;
190e8d8bef9SDimitry Andric const VEInstrInfo &TII = *STI.getInstrInfo();
191480093f4SDimitry Andric
192480093f4SDimitry Andric // Insert following codes here as epilogue
193480093f4SDimitry Andric //
194e8d8bef9SDimitry Andric // ld %s17, 40(, %sp) iff hasBP
195e8d8bef9SDimitry Andric // ld %plt, 32(, %sp) iff hasGOT
196e8d8bef9SDimitry Andric // ld %got, 24(, %sp) iff hasGOT
197e8d8bef9SDimitry Andric // ld %lr, 8(, %sp) iff !isLeafProc
198e8d8bef9SDimitry Andric // ld %fp, 0(, %sp) iff !isLeafProc
1995ffd83dbSDimitry Andric if (hasBP(MF))
200e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX17)
201480093f4SDimitry Andric .addReg(VE::SX11)
2025ffd83dbSDimitry Andric .addImm(0)
2035ffd83dbSDimitry Andric .addImm(40);
204e8d8bef9SDimitry Andric if (hasGOT(MF)) {
205e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX16)
2065ffd83dbSDimitry Andric .addReg(VE::SX11)
2075ffd83dbSDimitry Andric .addImm(0)
208480093f4SDimitry Andric .addImm(32);
209e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX15)
210480093f4SDimitry Andric .addReg(VE::SX11)
2115ffd83dbSDimitry Andric .addImm(0)
212480093f4SDimitry Andric .addImm(24);
213e8d8bef9SDimitry Andric }
214e8d8bef9SDimitry Andric if (!FuncInfo->isLeafProc()) {
215e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX10)
216480093f4SDimitry Andric .addReg(VE::SX11)
2175ffd83dbSDimitry Andric .addImm(0)
218480093f4SDimitry Andric .addImm(8);
219e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::LDrii), VE::SX9)
220480093f4SDimitry Andric .addReg(VE::SX11)
2215ffd83dbSDimitry Andric .addImm(0)
222480093f4SDimitry Andric .addImm(0);
223480093f4SDimitry Andric }
224e8d8bef9SDimitry Andric }
225480093f4SDimitry Andric
emitSPAdjustment(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,int64_t NumBytes,MaybeAlign MaybeAlign) const226480093f4SDimitry Andric void VEFrameLowering::emitSPAdjustment(MachineFunction &MF,
227480093f4SDimitry Andric MachineBasicBlock &MBB,
228480093f4SDimitry Andric MachineBasicBlock::iterator MBBI,
2295ffd83dbSDimitry Andric int64_t NumBytes,
2305ffd83dbSDimitry Andric MaybeAlign MaybeAlign) const {
231e8d8bef9SDimitry Andric DebugLoc DL;
232e8d8bef9SDimitry Andric const VEInstrInfo &TII = *STI.getInstrInfo();
233480093f4SDimitry Andric
234e8d8bef9SDimitry Andric if (NumBytes == 0) {
235e8d8bef9SDimitry Andric // Nothing to do here.
236e8d8bef9SDimitry Andric } else if (isInt<7>(NumBytes)) {
237e8d8bef9SDimitry Andric // adds.l %s11, NumBytes@lo, %s11
238e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::ADDSLri), VE::SX11)
239480093f4SDimitry Andric .addReg(VE::SX11)
240480093f4SDimitry Andric .addImm(NumBytes);
241e8d8bef9SDimitry Andric } else if (isInt<32>(NumBytes)) {
242e8d8bef9SDimitry Andric // lea %s11, NumBytes@lo(, %s11)
243e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::LEArii), VE::SX11)
244e8d8bef9SDimitry Andric .addReg(VE::SX11)
245e8d8bef9SDimitry Andric .addImm(0)
246e8d8bef9SDimitry Andric .addImm(Lo_32(NumBytes));
247e8d8bef9SDimitry Andric } else {
248480093f4SDimitry Andric // Emit following codes. This clobbers SX13 which we always know is
249480093f4SDimitry Andric // available here.
250e8d8bef9SDimitry Andric // lea %s13, NumBytes@lo
251480093f4SDimitry Andric // and %s13, %s13, (32)0
252e8d8bef9SDimitry Andric // lea.sl %sp, NumBytes@hi(%s13, %sp)
253e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::LEAzii), VE::SX13)
2545ffd83dbSDimitry Andric .addImm(0)
2555ffd83dbSDimitry Andric .addImm(0)
2565ffd83dbSDimitry Andric .addImm(Lo_32(NumBytes));
257e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::ANDrm), VE::SX13)
258480093f4SDimitry Andric .addReg(VE::SX13)
2595ffd83dbSDimitry Andric .addImm(M0(32));
260e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::LEASLrri), VE::SX11)
261480093f4SDimitry Andric .addReg(VE::SX11)
262480093f4SDimitry Andric .addReg(VE::SX13)
2635ffd83dbSDimitry Andric .addImm(Hi_32(NumBytes));
264e8d8bef9SDimitry Andric }
2655ffd83dbSDimitry Andric
2665ffd83dbSDimitry Andric if (MaybeAlign) {
2675ffd83dbSDimitry Andric // and %sp, %sp, Align-1
268e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::ANDrm), VE::SX11)
2695ffd83dbSDimitry Andric .addReg(VE::SX11)
2705ffd83dbSDimitry Andric .addImm(M1(64 - Log2_64(MaybeAlign.valueOrOne().value())));
2715ffd83dbSDimitry Andric }
272480093f4SDimitry Andric }
273480093f4SDimitry Andric
emitSPExtend(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI) const274480093f4SDimitry Andric void VEFrameLowering::emitSPExtend(MachineFunction &MF, MachineBasicBlock &MBB,
2755ffd83dbSDimitry Andric MachineBasicBlock::iterator MBBI) const {
276e8d8bef9SDimitry Andric DebugLoc DL;
277e8d8bef9SDimitry Andric const VEInstrInfo &TII = *STI.getInstrInfo();
278480093f4SDimitry Andric
279480093f4SDimitry Andric // Emit following codes. It is not possible to insert multiple
280480093f4SDimitry Andric // BasicBlocks in PEI pass, so we emit two pseudo instructions here.
281480093f4SDimitry Andric //
282480093f4SDimitry Andric // EXTEND_STACK // pseudo instrcution
283480093f4SDimitry Andric // EXTEND_STACK_GUARD // pseudo instrcution
284480093f4SDimitry Andric //
285480093f4SDimitry Andric // EXTEND_STACK pseudo will be converted by ExpandPostRA pass into
286480093f4SDimitry Andric // following instructions with multiple basic blocks later.
287480093f4SDimitry Andric //
288480093f4SDimitry Andric // thisBB:
289480093f4SDimitry Andric // brge.l.t %sp, %sl, sinkBB
290480093f4SDimitry Andric // syscallBB:
291480093f4SDimitry Andric // ld %s61, 0x18(, %tp) // load param area
292480093f4SDimitry Andric // or %s62, 0, %s0 // spill the value of %s0
293480093f4SDimitry Andric // lea %s63, 0x13b // syscall # of grow
294480093f4SDimitry Andric // shm.l %s63, 0x0(%s61) // store syscall # at addr:0
295480093f4SDimitry Andric // shm.l %sl, 0x8(%s61) // store old limit at addr:8
296480093f4SDimitry Andric // shm.l %sp, 0x10(%s61) // store new limit at addr:16
297480093f4SDimitry Andric // monc // call monitor
298480093f4SDimitry Andric // or %s0, 0, %s62 // restore the value of %s0
299480093f4SDimitry Andric // sinkBB:
300480093f4SDimitry Andric //
301480093f4SDimitry Andric // EXTEND_STACK_GUARD pseudo will be simply eliminated by ExpandPostRA
302480093f4SDimitry Andric // pass. This pseudo is required to be at the next of EXTEND_STACK
303480093f4SDimitry Andric // pseudo in order to protect iteration loop in ExpandPostRA.
304e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::EXTEND_STACK));
305e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::EXTEND_STACK_GUARD));
306480093f4SDimitry Andric }
307480093f4SDimitry Andric
emitPrologue(MachineFunction & MF,MachineBasicBlock & MBB) const308480093f4SDimitry Andric void VEFrameLowering::emitPrologue(MachineFunction &MF,
309480093f4SDimitry Andric MachineBasicBlock &MBB) const {
310e8d8bef9SDimitry Andric const VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();
311480093f4SDimitry Andric assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
312480093f4SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo();
3135ffd83dbSDimitry Andric const VEInstrInfo &TII = *STI.getInstrInfo();
3145ffd83dbSDimitry Andric const VERegisterInfo &RegInfo = *STI.getRegisterInfo();
315480093f4SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin();
316fe6060f1SDimitry Andric bool NeedsStackRealignment = RegInfo.shouldRealignStack(MF);
317e8d8bef9SDimitry Andric
318480093f4SDimitry Andric // Debug location must be unknown since the first debug location is used
319480093f4SDimitry Andric // to determine the end of the prologue.
320e8d8bef9SDimitry Andric DebugLoc DL;
321480093f4SDimitry Andric
322fe6060f1SDimitry Andric if (NeedsStackRealignment && !RegInfo.canRealignStack(MF))
323480093f4SDimitry Andric report_fatal_error("Function \"" + Twine(MF.getName()) +
324480093f4SDimitry Andric "\" required "
325480093f4SDimitry Andric "stack re-alignment, but LLVM couldn't handle it "
326480093f4SDimitry Andric "(probably because it has a dynamic alloca).");
327480093f4SDimitry Andric
328e8d8bef9SDimitry Andric // Get the number of bytes to allocate from the FrameInfo.
329e8d8bef9SDimitry Andric // This number of bytes is already aligned to ABI stack alignment.
3305ffd83dbSDimitry Andric uint64_t NumBytes = MFI.getStackSize();
331480093f4SDimitry Andric
332e8d8bef9SDimitry Andric // Adjust stack size if this function is not a leaf function since the
333e8d8bef9SDimitry Andric // VE ABI requires a reserved area at the top of stack as described in
334e8d8bef9SDimitry Andric // VEFrameLowering.cpp.
335e8d8bef9SDimitry Andric if (!FuncInfo->isLeafProc()) {
336e8d8bef9SDimitry Andric // NOTE: The number is aligned to ABI stack alignment after adjustment.
3375ffd83dbSDimitry Andric NumBytes = STI.getAdjustedFrameSize(NumBytes);
338e8d8bef9SDimitry Andric }
339480093f4SDimitry Andric
340480093f4SDimitry Andric // Finally, ensure that the size is sufficiently aligned for the
341480093f4SDimitry Andric // data on the stack.
3425ffd83dbSDimitry Andric NumBytes = alignTo(NumBytes, MFI.getMaxAlign());
343480093f4SDimitry Andric
344480093f4SDimitry Andric // Update stack size with corrected value.
345480093f4SDimitry Andric MFI.setStackSize(NumBytes);
346480093f4SDimitry Andric
347e8d8bef9SDimitry Andric // Emit Prologue instructions to save multiple registers.
348480093f4SDimitry Andric emitPrologueInsns(MF, MBB, MBBI, NumBytes, true);
349480093f4SDimitry Andric
350e8d8bef9SDimitry Andric // Emit instructions to save SP in FP as follows if this is not a leaf
351e8d8bef9SDimitry Andric // function:
352e8d8bef9SDimitry Andric // or %fp, 0, %sp
353e8d8bef9SDimitry Andric if (!FuncInfo->isLeafProc())
354e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::ORri), VE::SX9)
355e8d8bef9SDimitry Andric .addReg(VE::SX11)
356e8d8bef9SDimitry Andric .addImm(0);
357e8d8bef9SDimitry Andric
358480093f4SDimitry Andric // Emit stack adjust instructions
3595ffd83dbSDimitry Andric MaybeAlign RuntimeAlign =
360*bdd1243dSDimitry Andric NeedsStackRealignment ? MaybeAlign(MFI.getMaxAlign()) : std::nullopt;
361*bdd1243dSDimitry Andric assert((RuntimeAlign == std::nullopt || !FuncInfo->isLeafProc()) &&
362e8d8bef9SDimitry Andric "SP has to be saved in order to align variable sized stack object!");
3635ffd83dbSDimitry Andric emitSPAdjustment(MF, MBB, MBBI, -(int64_t)NumBytes, RuntimeAlign);
3645ffd83dbSDimitry Andric
3655ffd83dbSDimitry Andric if (hasBP(MF)) {
3665ffd83dbSDimitry Andric // Copy SP to BP.
367e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::ORri), VE::SX17)
3685ffd83dbSDimitry Andric .addReg(VE::SX11)
3695ffd83dbSDimitry Andric .addImm(0);
3705ffd83dbSDimitry Andric }
371480093f4SDimitry Andric
372480093f4SDimitry Andric // Emit stack extend instructions
373e8d8bef9SDimitry Andric if (NumBytes != 0)
3745ffd83dbSDimitry Andric emitSPExtend(MF, MBB, MBBI);
375480093f4SDimitry Andric }
376480093f4SDimitry Andric
eliminateCallFramePseudoInstr(MachineFunction & MF,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const377480093f4SDimitry Andric MachineBasicBlock::iterator VEFrameLowering::eliminateCallFramePseudoInstr(
378480093f4SDimitry Andric MachineFunction &MF, MachineBasicBlock &MBB,
379480093f4SDimitry Andric MachineBasicBlock::iterator I) const {
380480093f4SDimitry Andric if (!hasReservedCallFrame(MF)) {
381480093f4SDimitry Andric MachineInstr &MI = *I;
3825ffd83dbSDimitry Andric int64_t Size = MI.getOperand(0).getImm();
383480093f4SDimitry Andric if (MI.getOpcode() == VE::ADJCALLSTACKDOWN)
384480093f4SDimitry Andric Size = -Size;
385480093f4SDimitry Andric
386480093f4SDimitry Andric if (Size)
387480093f4SDimitry Andric emitSPAdjustment(MF, MBB, I, Size);
388480093f4SDimitry Andric }
389480093f4SDimitry Andric return MBB.erase(I);
390480093f4SDimitry Andric }
391480093f4SDimitry Andric
emitEpilogue(MachineFunction & MF,MachineBasicBlock & MBB) const392480093f4SDimitry Andric void VEFrameLowering::emitEpilogue(MachineFunction &MF,
393480093f4SDimitry Andric MachineBasicBlock &MBB) const {
394e8d8bef9SDimitry Andric const VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();
395e8d8bef9SDimitry Andric DebugLoc DL;
396480093f4SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
397480093f4SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo();
398e8d8bef9SDimitry Andric const VEInstrInfo &TII = *STI.getInstrInfo();
399480093f4SDimitry Andric
4005ffd83dbSDimitry Andric uint64_t NumBytes = MFI.getStackSize();
401480093f4SDimitry Andric
402e8d8bef9SDimitry Andric // Emit instructions to retrieve original SP.
403e8d8bef9SDimitry Andric if (!FuncInfo->isLeafProc()) {
404e8d8bef9SDimitry Andric // If SP is saved in FP, retrieve it as follows:
405e8d8bef9SDimitry Andric // or %sp, 0, %fp iff !isLeafProc
406e8d8bef9SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(VE::ORri), VE::SX11)
407e8d8bef9SDimitry Andric .addReg(VE::SX9)
408e8d8bef9SDimitry Andric .addImm(0);
409e8d8bef9SDimitry Andric } else {
410e8d8bef9SDimitry Andric // Emit stack adjust instructions.
411*bdd1243dSDimitry Andric emitSPAdjustment(MF, MBB, MBBI, NumBytes, std::nullopt);
412e8d8bef9SDimitry Andric }
413e8d8bef9SDimitry Andric
414e8d8bef9SDimitry Andric // Emit Epilogue instructions to restore multiple registers.
415480093f4SDimitry Andric emitEpilogueInsns(MF, MBB, MBBI, NumBytes, true);
416480093f4SDimitry Andric }
417480093f4SDimitry Andric
418480093f4SDimitry Andric // hasFP - Return true if the specified function should have a dedicated frame
4195ffd83dbSDimitry Andric // pointer register. This is true if the function has variable sized allocas
420e8d8bef9SDimitry Andric // or if frame pointer elimination is disabled.
hasFP(const MachineFunction & MF) const421480093f4SDimitry Andric bool VEFrameLowering::hasFP(const MachineFunction &MF) const {
422480093f4SDimitry Andric const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
423480093f4SDimitry Andric
424480093f4SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo();
425480093f4SDimitry Andric return MF.getTarget().Options.DisableFramePointerElim(MF) ||
426fe6060f1SDimitry Andric RegInfo->hasStackRealignment(MF) || MFI.hasVarSizedObjects() ||
427480093f4SDimitry Andric MFI.isFrameAddressTaken();
428480093f4SDimitry Andric }
429480093f4SDimitry Andric
hasBP(const MachineFunction & MF) const4305ffd83dbSDimitry Andric bool VEFrameLowering::hasBP(const MachineFunction &MF) const {
4315ffd83dbSDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo();
4325ffd83dbSDimitry Andric const TargetRegisterInfo *TRI = STI.getRegisterInfo();
4335ffd83dbSDimitry Andric
434fe6060f1SDimitry Andric return MFI.hasVarSizedObjects() && TRI->hasStackRealignment(MF);
4355ffd83dbSDimitry Andric }
4365ffd83dbSDimitry Andric
hasGOT(const MachineFunction & MF) const437e8d8bef9SDimitry Andric bool VEFrameLowering::hasGOT(const MachineFunction &MF) const {
438e8d8bef9SDimitry Andric const VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();
439e8d8bef9SDimitry Andric
440e8d8bef9SDimitry Andric // If a global base register is assigned (!= 0), GOT is used.
441e8d8bef9SDimitry Andric return FuncInfo->getGlobalBaseReg() != 0;
442e8d8bef9SDimitry Andric }
443e8d8bef9SDimitry Andric
getFrameIndexReference(const MachineFunction & MF,int FI,Register & FrameReg) const444e8d8bef9SDimitry Andric StackOffset VEFrameLowering::getFrameIndexReference(const MachineFunction &MF,
445e8d8bef9SDimitry Andric int FI,
4465ffd83dbSDimitry Andric Register &FrameReg) const {
4475ffd83dbSDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo();
4485ffd83dbSDimitry Andric const VERegisterInfo *RegInfo = STI.getRegisterInfo();
4495ffd83dbSDimitry Andric bool isFixed = MFI.isFixedObjectIndex(FI);
4505ffd83dbSDimitry Andric
451480093f4SDimitry Andric int64_t FrameOffset = MF.getFrameInfo().getObjectOffset(FI);
4525ffd83dbSDimitry Andric
453e8d8bef9SDimitry Andric if (!hasFP(MF)) {
454e8d8bef9SDimitry Andric // If FP is not used, frame indexies are based on a %sp regiter.
455480093f4SDimitry Andric FrameReg = VE::SX11; // %sp
456e8d8bef9SDimitry Andric return StackOffset::getFixed(FrameOffset +
457e8d8bef9SDimitry Andric MF.getFrameInfo().getStackSize());
458480093f4SDimitry Andric }
459fe6060f1SDimitry Andric if (RegInfo->hasStackRealignment(MF) && !isFixed) {
460e8d8bef9SDimitry Andric // If data on stack require realignemnt, frame indexies are based on a %sp
461e8d8bef9SDimitry Andric // or %s17 (bp) register. If there is a variable sized object, bp is used.
4625ffd83dbSDimitry Andric if (hasBP(MF))
4635ffd83dbSDimitry Andric FrameReg = VE::SX17; // %bp
4645ffd83dbSDimitry Andric else
4655ffd83dbSDimitry Andric FrameReg = VE::SX11; // %sp
466e8d8bef9SDimitry Andric return StackOffset::getFixed(FrameOffset +
467e8d8bef9SDimitry Andric MF.getFrameInfo().getStackSize());
4685ffd83dbSDimitry Andric }
469e8d8bef9SDimitry Andric // Use %fp by default.
4705ffd83dbSDimitry Andric FrameReg = RegInfo->getFrameRegister(MF);
471e8d8bef9SDimitry Andric return StackOffset::getFixed(FrameOffset);
4725ffd83dbSDimitry Andric }
473480093f4SDimitry Andric
isLeafProc(MachineFunction & MF) const474480093f4SDimitry Andric bool VEFrameLowering::isLeafProc(MachineFunction &MF) const {
475480093f4SDimitry Andric
476480093f4SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo();
477480093f4SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo();
478480093f4SDimitry Andric
479480093f4SDimitry Andric return !MFI.hasCalls() // No calls
480480093f4SDimitry Andric && !MRI.isPhysRegUsed(VE::SX18) // Registers within limits
481480093f4SDimitry Andric // (s18 is first CSR)
482480093f4SDimitry Andric && !MRI.isPhysRegUsed(VE::SX11) // %sp un-used
483480093f4SDimitry Andric && !hasFP(MF); // Don't need %fp
484480093f4SDimitry Andric }
485480093f4SDimitry Andric
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const486480093f4SDimitry Andric void VEFrameLowering::determineCalleeSaves(MachineFunction &MF,
487480093f4SDimitry Andric BitVector &SavedRegs,
488480093f4SDimitry Andric RegScavenger *RS) const {
489480093f4SDimitry Andric TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
490480093f4SDimitry Andric
491e8d8bef9SDimitry Andric // Functions having BP need to emit prologue and epilogue to allocate local
492e8d8bef9SDimitry Andric // buffer on the stack even if the function is a leaf function.
493e8d8bef9SDimitry Andric if (isLeafProc(MF) && !hasBP(MF)) {
494e8d8bef9SDimitry Andric VEMachineFunctionInfo *FuncInfo = MF.getInfo<VEMachineFunctionInfo>();
495e8d8bef9SDimitry Andric FuncInfo->setLeafProc(true);
4965ffd83dbSDimitry Andric }
497480093f4SDimitry Andric }
498