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