1 //===- TargetFrameLoweringImpl.cpp - Implement target frame interface ------==//
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 // Implements the layout of a stack frame on the target machine.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/BitVector.h"
15 #include "llvm/CodeGen/MachineFrameInfo.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/CodeGen/TargetFrameLowering.h"
19 #include "llvm/CodeGen/TargetRegisterInfo.h"
20 #include "llvm/CodeGen/TargetSubtargetInfo.h"
21 #include "llvm/IR/Attributes.h"
22 #include "llvm/IR/CallingConv.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetOptions.h"
28 
29 using namespace llvm;
30 
31 TargetFrameLowering::~TargetFrameLowering() = default;
32 
33 /// The default implementation just looks at attribute "no-frame-pointer-elim".
34 bool TargetFrameLowering::noFramePointerElim(const MachineFunction &MF) const {
35   auto Attr = MF.getFunction().getFnAttribute("no-frame-pointer-elim");
36   return Attr.getValueAsString() == "true";
37 }
38 
39 bool TargetFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const {
40   assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
41       MF.getFunction().hasFnAttribute(Attribute::NoUnwind));
42   return false;
43 }
44 
45 /// Returns the displacement from the frame register to the stack
46 /// frame of the specified index, along with the frame register used
47 /// (in output arg FrameReg). This is the default implementation which
48 /// is overridden for some targets.
49 int TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF,
50                                              int FI, unsigned &FrameReg) const {
51   const MachineFrameInfo &MFI = MF.getFrameInfo();
52   const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
53 
54   // By default, assume all frame indices are referenced via whatever
55   // getFrameRegister() says. The target can override this if it's doing
56   // something different.
57   FrameReg = RI->getFrameRegister(MF);
58 
59   return MFI.getObjectOffset(FI) + MFI.getStackSize() -
60          getOffsetOfLocalArea() + MFI.getOffsetAdjustment();
61 }
62 
63 bool TargetFrameLowering::needsFrameIndexResolution(
64     const MachineFunction &MF) const {
65   return MF.getFrameInfo().hasStackObjects();
66 }
67 
68 void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF,
69                                                BitVector &SavedRegs,
70                                                RegScavenger *RS) const {
71   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
72 
73   // Resize before the early returns. Some backends expect that
74   // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
75   // saved registers.
76   SavedRegs.resize(TRI.getNumRegs());
77 
78   // When interprocedural register allocation is enabled caller saved registers
79   // are preferred over callee saved registers.
80   if (MF.getTarget().Options.EnableIPRA && isSafeForNoCSROpt(MF.getFunction()))
81     return;
82 
83   // Get the callee saved register list...
84   const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
85 
86   // Early exit if there are no callee saved registers.
87   if (!CSRegs || CSRegs[0] == 0)
88     return;
89 
90   // In Naked functions we aren't going to save any registers.
91   if (MF.getFunction().hasFnAttribute(Attribute::Naked))
92     return;
93 
94   // Noreturn+nounwind functions never restore CSR, so no saves are needed.
95   // Purely noreturn functions may still return through throws, so those must
96   // save CSR for caller exception handlers.
97   //
98   // If the function uses longjmp to break out of its current path of
99   // execution we do not need the CSR spills either: setjmp stores all CSRs
100   // it was called with into the jmp_buf, which longjmp then restores.
101   if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
102         MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
103         enableCalleeSaveSkip(MF))
104     return;
105 
106   // Functions which call __builtin_unwind_init get all their registers saved.
107   bool CallsUnwindInit = MF.callsUnwindInit();
108   const MachineRegisterInfo &MRI = MF.getRegInfo();
109   for (unsigned i = 0; CSRegs[i]; ++i) {
110     unsigned Reg = CSRegs[i];
111     if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
112       SavedRegs.set(Reg);
113   }
114 }
115 
116 unsigned TargetFrameLowering::getStackAlignmentSkew(
117     const MachineFunction &MF) const {
118   // When HHVM function is called, the stack is skewed as the return address
119   // is removed from the stack before we enter the function.
120   if (LLVM_UNLIKELY(MF.getFunction().getCallingConv() == CallingConv::HHVM))
121     return MF.getTarget().getAllocaPointerSize();
122 
123   return 0;
124 }
125