LLVM  13.0.0git
LanaiFrameLowering.cpp
Go to the documentation of this file.
1 //===-- LanaiFrameLowering.cpp - Lanai Frame Information ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the Lanai implementation of TargetFrameLowering class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "LanaiFrameLowering.h"
14 
15 #include "LanaiAluCode.h"
16 #include "LanaiInstrInfo.h"
17 #include "LanaiSubtarget.h"
22 #include "llvm/IR/Function.h"
23 
24 using namespace llvm;
25 
26 // Determines the size of the frame and maximum call frame size.
27 void LanaiFrameLowering::determineFrameLayout(MachineFunction &MF) const {
28  MachineFrameInfo &MFI = MF.getFrameInfo();
29  const LanaiRegisterInfo *LRI = STI.getRegisterInfo();
30 
31  // Get the number of bytes to allocate from the FrameInfo.
32  unsigned FrameSize = MFI.getStackSize();
33 
34  // Get the alignment.
36  LRI->hasStackRealignment(MF) ? MFI.getMaxAlign() : getStackAlign();
37 
38  // Get the maximum call frame size of all the calls.
39  unsigned 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 // Iterates through each basic block in a machine function and replaces
61 // ADJDYNALLOC pseudo instructions with a Lanai:ADDI with the
62 // maximum call frame size as the immediate.
63 void LanaiFrameLowering::replaceAdjDynAllocPseudo(MachineFunction &MF) const {
64  const LanaiInstrInfo &LII =
65  *static_cast<const LanaiInstrInfo *>(STI.getInstrInfo());
66  unsigned MaxCallFrameSize = MF.getFrameInfo().getMaxCallFrameSize();
67 
68  for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); MBB != E;
69  ++MBB) {
71  while (MBBI != MBB->end()) {
72  MachineInstr &MI = *MBBI++;
73  if (MI.getOpcode() == Lanai::ADJDYNALLOC) {
74  DebugLoc DL = MI.getDebugLoc();
75  Register Dst = MI.getOperand(0).getReg();
76  Register Src = MI.getOperand(1).getReg();
77 
78  BuildMI(*MBB, MI, DL, LII.get(Lanai::ADD_I_LO), Dst)
79  .addReg(Src)
80  .addImm(MaxCallFrameSize);
81  MI.eraseFromParent();
82  }
83  }
84  }
85 }
86 
87 // Generates the following sequence for function entry:
88 // st %fp,-4[*%sp] !push old FP
89 // add %sp,8,%fp !generate new FP
90 // sub %sp,0x4,%sp !allocate stack space (as needed)
92  MachineBasicBlock &MBB) const {
93  assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
94 
95  MachineFrameInfo &MFI = MF.getFrameInfo();
96  const LanaiInstrInfo &LII =
97  *static_cast<const LanaiInstrInfo *>(STI.getInstrInfo());
99 
100  // Debug location must be unknown since the first debug location is used
101  // to determine the end of the prologue.
102  DebugLoc DL;
103 
104  // Determine the correct frame layout
105  determineFrameLayout(MF);
106 
107  // FIXME: This appears to be overallocating. Needs investigation.
108  // Get the number of bytes to allocate from the FrameInfo.
109  unsigned StackSize = MFI.getStackSize();
110 
111  // Push old FP
112  // st %fp,-4[*%sp]
113  BuildMI(MBB, MBBI, DL, LII.get(Lanai::SW_RI))
114  .addReg(Lanai::FP)
115  .addReg(Lanai::SP)
116  .addImm(-4)
119 
120  // Generate new FP
121  // add %sp,8,%fp
122  BuildMI(MBB, MBBI, DL, LII.get(Lanai::ADD_I_LO), Lanai::FP)
123  .addReg(Lanai::SP)
124  .addImm(8)
126 
127  // Allocate space on the stack if needed
128  // sub %sp,StackSize,%sp
129  if (StackSize != 0) {
130  BuildMI(MBB, MBBI, DL, LII.get(Lanai::SUB_I_LO), Lanai::SP)
131  .addReg(Lanai::SP)
132  .addImm(StackSize)
134  }
135 
136  // Replace ADJDYNANALLOC
137  if (MFI.hasVarSizedObjects())
138  replaceAdjDynAllocPseudo(MF);
139 }
140 
144  // Discard ADJCALLSTACKDOWN, ADJCALLSTACKUP instructions.
145  return MBB.erase(I);
146 }
147 
148 // The function epilogue should not depend on the current stack pointer!
149 // It should use the frame pointer only. This is mandatory because
150 // of alloca; we also take advantage of it to omit stack adjustments
151 // before returning.
152 //
153 // Note that when we go to restore the preserved register values we must
154 // not try to address their slots by using offsets from the stack pointer.
155 // That's because the stack pointer may have been moved during the function
156 // execution due to a call to alloca(). Rather, we must restore all
157 // preserved registers via offsets from the frame pointer value.
158 //
159 // Note also that when the current frame is being "popped" (by adjusting
160 // the value of the stack pointer) on function exit, we must (for the
161 // sake of alloca) set the new value of the stack pointer based upon
162 // the current value of the frame pointer. We can't just add what we
163 // believe to be the (static) frame size to the stack pointer because
164 // if we did that, and alloca() had been called during this function,
165 // we would end up returning *without* having fully deallocated all of
166 // the space grabbed by alloca. If that happened, and a function
167 // containing one or more alloca() calls was called over and over again,
168 // then the stack would grow without limit!
169 //
170 // RET is lowered to
171 // ld -4[%fp],%pc # modify %pc (two delay slots)
172 // as the return address is in the stack frame and mov to pc is allowed.
173 // emitEpilogue emits
174 // mov %fp,%sp # restore the stack pointer
175 // ld -8[%fp],%fp # restore the caller's frame pointer
176 // before RET and the delay slot filler will move RET such that these
177 // instructions execute in the delay slots of the load to PC.
179  MachineBasicBlock &MBB) const {
181  const LanaiInstrInfo &LII =
182  *static_cast<const LanaiInstrInfo *>(STI.getInstrInfo());
183  DebugLoc DL = MBBI->getDebugLoc();
184 
185  // Restore the stack pointer using the callee's frame pointer value.
186  BuildMI(MBB, MBBI, DL, LII.get(Lanai::ADD_I_LO), Lanai::SP)
187  .addReg(Lanai::FP)
188  .addImm(0);
189 
190  // Restore the frame pointer from the stack.
191  BuildMI(MBB, MBBI, DL, LII.get(Lanai::LDW_RI), Lanai::FP)
192  .addReg(Lanai::FP)
193  .addImm(-8)
194  .addImm(LPAC::ADD);
195 }
196 
198  BitVector &SavedRegs,
199  RegScavenger *RS) const {
201 
202  MachineFrameInfo &MFI = MF.getFrameInfo();
203  const LanaiRegisterInfo *LRI =
204  static_cast<const LanaiRegisterInfo *>(STI.getRegisterInfo());
205  int Offset = -4;
206 
207  // Reserve 4 bytes for the saved RCA
208  MFI.CreateFixedObject(4, Offset, true);
209  Offset -= 4;
210 
211  // Reserve 4 bytes for the saved FP
212  MFI.CreateFixedObject(4, Offset, true);
213  Offset -= 4;
214 
215  if (LRI->hasBasePointer(MF)) {
216  MFI.CreateFixedObject(4, Offset, true);
217  SavedRegs.reset(LRI->getBaseRegister());
218  }
219 }
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:148
llvm::MachineFrameInfo::hasVarSizedObjects
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
Definition: MachineFrameInfo.h:351
llvm::LPAC::ADD
@ ADD
Definition: LanaiAluCode.h:23
llvm::RISCVAttrs::StackAlign
StackAlign
Definition: RISCVAttributes.h:37
LanaiAluCode.h
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:100
llvm::MachineInstrBuilder::addImm
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Definition: MachineInstrBuilder.h:131
llvm
Definition: AllocatorList.h:23
llvm::LanaiFrameLowering::eliminateCallFramePseudoInstr
MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const override
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
Definition: LanaiFrameLowering.cpp:141
llvm::MachineFunction::end
iterator end()
Definition: MachineFunction.h:742
llvm::MachineFrameInfo::setStackSize
void setStackSize(uint64_t Size)
Set the size of the stack.
Definition: MachineFrameInfo.h:566
llvm::MachineFrameInfo::setMaxCallFrameSize
void setMaxCallFrameSize(unsigned S)
Definition: MachineFrameInfo.h:653
llvm::LanaiISD::ADJDYNALLOC
@ ADJDYNALLOC
Definition: LanaiISelLowering.h:27
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::LanaiFrameLowering::emitPrologue
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
Definition: LanaiFrameLowering.cpp:91
llvm::MachineFrameInfo::getMaxCallFrameSize
unsigned getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
Definition: MachineFrameInfo.h:643
MachineRegisterInfo.h
llvm::MachineBasicBlock::erase
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
Definition: MachineBasicBlock.cpp:1324
llvm::MachineFunction::front
const MachineBasicBlock & front() const
Definition: MachineFunction.h:752
LanaiFrameLowering.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::TargetFrameLowering::getStackAlign
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Definition: TargetFrameLowering.h:99
llvm::TargetFrameLowering::hasReservedCallFrame
virtual bool hasReservedCallFrame(const MachineFunction &MF) const
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
Definition: TargetFrameLowering.h:278
llvm::LPAC::makePreOp
static unsigned makePreOp(unsigned AluOp)
Definition: LanaiAluCode.h:62
llvm::MachineInstr::FrameSetup
@ FrameSetup
Definition: MachineInstr.h:82
llvm::LanaiFrameLowering::STI
const LanaiSubtarget & STI
Definition: LanaiFrameLowering.h:29
llvm::MachineFunction::begin
iterator begin()
Definition: MachineFunction.h:740
llvm::MachineFrameInfo::getStackSize
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
Definition: MachineFrameInfo.h:563
llvm::BitVector
Definition: BitVector.h:74
llvm::LanaiRegisterInfo::hasBasePointer
bool hasBasePointer(const MachineFunction &MF) const
Definition: LanaiRegisterInfo.cpp:244
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::MachineInstrBuilder::setMIFlag
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
Definition: MachineInstrBuilder.h:278
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::RegScavenger
Definition: RegisterScavenging.h:34
llvm::MachineBasicBlock::getLastNonDebugInstr
iterator getLastNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the last non-debug instruction in the basic block, or end().
Definition: MachineBasicBlock.cpp:266
llvm::LanaiInstrInfo
Definition: LanaiInstrInfo.h:25
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MachineFrameInfo::CreateFixedObject
int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
Definition: MachineFrameInfo.cpp:83
llvm::MachineFunction::getFrameInfo
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Definition: MachineFunction.h:574
llvm::MachineInstrBuilder::addReg
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Definition: MachineInstrBuilder.h:97
llvm::LanaiRegisterInfo::getBaseRegister
Register getBaseRegister() const
Definition: LanaiRegisterInfo.cpp:261
llvm::MachineFunction
Definition: MachineFunction.h:230
MBBI
MachineBasicBlock MachineBasicBlock::iterator MBBI
Definition: AArch64SLSHardening.cpp:75
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::LanaiRegisterInfo
Definition: LanaiRegisterInfo.h:23
llvm::MachineFrameInfo::getMaxAlign
Align getMaxAlign() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
Definition: MachineFrameInfo.h:585
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::LanaiSubtarget::getRegisterInfo
const LanaiRegisterInfo * getRegisterInfo() const override
Definition: LanaiSubtarget.h:54
llvm::ilist_iterator
Iterator for intrusive lists based on ilist_node.
Definition: ilist_iterator.h:57
MachineFrameInfo.h
Function.h
llvm::LanaiSubtarget::getInstrInfo
const LanaiInstrInfo * getInstrInfo() const override
Definition: LanaiSubtarget.h:48
llvm::BitVector::reset
BitVector & reset()
Definition: BitVector.h:384
llvm::MachineFrameInfo
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Definition: MachineFrameInfo.h:107
llvm::MachineBasicBlock::begin
iterator begin()
Definition: MachineBasicBlock.h:268
MachineInstrBuilder.h
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:328
llvm::TargetFrameLowering::determineCalleeSaves
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
Definition: TargetFrameLoweringImpl.cpp:78
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::MachineFrameInfo::adjustsStack
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
Definition: MachineFrameInfo.h:598
LanaiInstrInfo.h
MachineFunction.h
LanaiSubtarget.h
llvm::MachineInstrBundleIterator< MachineInstr >
llvm::MachineBasicBlock::end
iterator end()
Definition: MachineBasicBlock.h:270
llvm::LanaiFrameLowering::determineCalleeSaves
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
Definition: LanaiFrameLowering.cpp:197
llvm::LanaiFrameLowering::emitEpilogue
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
Definition: LanaiFrameLowering.cpp:178