1db17bf38SDimitry Andric //===- TargetFrameLoweringImpl.cpp - Implement target frame interface ------==//
2dff0c46cSDimitry Andric //
3dff0c46cSDimitry Andric //                     The LLVM Compiler Infrastructure
4dff0c46cSDimitry Andric //
5dff0c46cSDimitry Andric // This file is distributed under the University of Illinois Open Source
6dff0c46cSDimitry Andric // License. See LICENSE.TXT for details.
7dff0c46cSDimitry Andric //
8dff0c46cSDimitry Andric //===----------------------------------------------------------------------===//
9dff0c46cSDimitry Andric //
10dff0c46cSDimitry Andric // Implements the layout of a stack frame on the target machine.
11dff0c46cSDimitry Andric //
12dff0c46cSDimitry Andric //===----------------------------------------------------------------------===//
13dff0c46cSDimitry Andric 
14875ed548SDimitry Andric #include "llvm/ADT/BitVector.h"
15dff0c46cSDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
16dff0c46cSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
17875ed548SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
182cab237bSDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
192cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
202cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
21db17bf38SDimitry Andric #include "llvm/IR/Attributes.h"
227d523365SDimitry Andric #include "llvm/IR/CallingConv.h"
23ff0cc061SDimitry Andric #include "llvm/IR/Function.h"
24db17bf38SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
25db17bf38SDimitry Andric #include "llvm/Support/Compiler.h"
26db17bf38SDimitry Andric #include "llvm/Target/TargetMachine.h"
27db17bf38SDimitry Andric #include "llvm/Target/TargetOptions.h"
28db17bf38SDimitry Andric 
29dff0c46cSDimitry Andric using namespace llvm;
30dff0c46cSDimitry Andric 
31db17bf38SDimitry Andric TargetFrameLowering::~TargetFrameLowering() = default;
32dff0c46cSDimitry Andric 
enableCalleeSaveSkip(const MachineFunction & MF) const33*4ba319b5SDimitry Andric bool TargetFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const {
34*4ba319b5SDimitry Andric   assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
35*4ba319b5SDimitry Andric          MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
36*4ba319b5SDimitry Andric          !MF.getFunction().hasFnAttribute(Attribute::UWTable));
37*4ba319b5SDimitry Andric   return false;
38*4ba319b5SDimitry Andric }
39*4ba319b5SDimitry Andric 
407d523365SDimitry Andric /// Returns the displacement from the frame register to the stack
417d523365SDimitry Andric /// frame of the specified index, along with the frame register used
427d523365SDimitry Andric /// (in output arg FrameReg). This is the default implementation which
437d523365SDimitry Andric /// is overridden for some targets.
getFrameIndexReference(const MachineFunction & MF,int FI,unsigned & FrameReg) const44dff0c46cSDimitry Andric int TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF,
45dff0c46cSDimitry Andric                                              int FI, unsigned &FrameReg) const {
46d88c1a5aSDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
4739d628a0SDimitry Andric   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
48dff0c46cSDimitry Andric 
49dff0c46cSDimitry Andric   // By default, assume all frame indices are referenced via whatever
50dff0c46cSDimitry Andric   // getFrameRegister() says. The target can override this if it's doing
51dff0c46cSDimitry Andric   // something different.
52dff0c46cSDimitry Andric   FrameReg = RI->getFrameRegister(MF);
537d523365SDimitry Andric 
54d88c1a5aSDimitry Andric   return MFI.getObjectOffset(FI) + MFI.getStackSize() -
55d88c1a5aSDimitry Andric          getOffsetOfLocalArea() + MFI.getOffsetAdjustment();
56dff0c46cSDimitry Andric }
570f0f2bfaSDimitry Andric 
needsFrameIndexResolution(const MachineFunction & MF) const580f0f2bfaSDimitry Andric bool TargetFrameLowering::needsFrameIndexResolution(
590f0f2bfaSDimitry Andric     const MachineFunction &MF) const {
60d88c1a5aSDimitry Andric   return MF.getFrameInfo().hasStackObjects();
610f0f2bfaSDimitry Andric }
62875ed548SDimitry Andric 
determineCalleeSaves(MachineFunction & MF,BitVector & SavedRegs,RegScavenger * RS) const63875ed548SDimitry Andric void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF,
64875ed548SDimitry Andric                                                BitVector &SavedRegs,
65875ed548SDimitry Andric                                                RegScavenger *RS) const {
66875ed548SDimitry Andric   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
673ca95b02SDimitry Andric 
683ca95b02SDimitry Andric   // Resize before the early returns. Some backends expect that
693ca95b02SDimitry Andric   // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
703ca95b02SDimitry Andric   // saved registers.
713ca95b02SDimitry Andric   SavedRegs.resize(TRI.getNumRegs());
723ca95b02SDimitry Andric 
733ca95b02SDimitry Andric   // When interprocedural register allocation is enabled caller saved registers
743ca95b02SDimitry Andric   // are preferred over callee saved registers.
753ca95b02SDimitry Andric   if (MF.getTarget().Options.EnableIPRA && isSafeForNoCSROpt(MF.getFunction()))
763ca95b02SDimitry Andric     return;
773ca95b02SDimitry Andric 
783ca95b02SDimitry Andric   // Get the callee saved register list...
797a7e6055SDimitry Andric   const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
80875ed548SDimitry Andric 
81875ed548SDimitry Andric   // Early exit if there are no callee saved registers.
82875ed548SDimitry Andric   if (!CSRegs || CSRegs[0] == 0)
83875ed548SDimitry Andric     return;
84875ed548SDimitry Andric 
85875ed548SDimitry Andric   // In Naked functions we aren't going to save any registers.
862cab237bSDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::Naked))
87875ed548SDimitry Andric     return;
88875ed548SDimitry Andric 
89*4ba319b5SDimitry Andric   // Noreturn+nounwind functions never restore CSR, so no saves are needed.
90*4ba319b5SDimitry Andric   // Purely noreturn functions may still return through throws, so those must
91*4ba319b5SDimitry Andric   // save CSR for caller exception handlers.
92*4ba319b5SDimitry Andric   //
93*4ba319b5SDimitry Andric   // If the function uses longjmp to break out of its current path of
94*4ba319b5SDimitry Andric   // execution we do not need the CSR spills either: setjmp stores all CSRs
95*4ba319b5SDimitry Andric   // it was called with into the jmp_buf, which longjmp then restores.
96*4ba319b5SDimitry Andric   if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
97*4ba319b5SDimitry Andric         MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
98*4ba319b5SDimitry Andric         !MF.getFunction().hasFnAttribute(Attribute::UWTable) &&
99*4ba319b5SDimitry Andric         enableCalleeSaveSkip(MF))
100*4ba319b5SDimitry Andric     return;
101*4ba319b5SDimitry Andric 
102875ed548SDimitry Andric   // Functions which call __builtin_unwind_init get all their registers saved.
103d88c1a5aSDimitry Andric   bool CallsUnwindInit = MF.callsUnwindInit();
104875ed548SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
105875ed548SDimitry Andric   for (unsigned i = 0; CSRegs[i]; ++i) {
106875ed548SDimitry Andric     unsigned Reg = CSRegs[i];
107875ed548SDimitry Andric     if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
108875ed548SDimitry Andric       SavedRegs.set(Reg);
109875ed548SDimitry Andric   }
110875ed548SDimitry Andric }
1117d523365SDimitry Andric 
getStackAlignmentSkew(const MachineFunction & MF) const1127d523365SDimitry Andric unsigned TargetFrameLowering::getStackAlignmentSkew(
1137d523365SDimitry Andric     const MachineFunction &MF) const {
1147d523365SDimitry Andric   // When HHVM function is called, the stack is skewed as the return address
1157d523365SDimitry Andric   // is removed from the stack before we enter the function.
1162cab237bSDimitry Andric   if (LLVM_UNLIKELY(MF.getFunction().getCallingConv() == CallingConv::HHVM))
117*4ba319b5SDimitry Andric     return MF.getTarget().getAllocaPointerSize();
1187d523365SDimitry Andric 
1197d523365SDimitry Andric   return 0;
1207d523365SDimitry Andric }
121*4ba319b5SDimitry Andric 
getInitialCFAOffset(const MachineFunction & MF) const122*4ba319b5SDimitry Andric int TargetFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const {
123*4ba319b5SDimitry Andric   llvm_unreachable("getInitialCFAOffset() not implemented!");
124*4ba319b5SDimitry Andric }
125*4ba319b5SDimitry Andric 
getInitialCFARegister(const MachineFunction & MF) const126*4ba319b5SDimitry Andric unsigned TargetFrameLowering::getInitialCFARegister(const MachineFunction &MF)
127*4ba319b5SDimitry Andric     const {
128*4ba319b5SDimitry Andric   llvm_unreachable("getInitialCFARegister() not implemented!");
129*4ba319b5SDimitry Andric }