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/CallingConv.h" 22 #include "llvm/IR/Function.h" 23 #include "llvm/IR/InstrTypes.h" 24 #include "llvm/Support/Compiler.h" 25 #include "llvm/Target/TargetMachine.h" 26 #include "llvm/Target/TargetOptions.h" 27 28 using namespace llvm; 29 30 TargetFrameLowering::~TargetFrameLowering() = default; 31 32 bool TargetFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const { 33 assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) && 34 MF.getFunction().hasFnAttribute(Attribute::NoUnwind) && 35 !MF.getFunction().hasFnAttribute(Attribute::UWTable)); 36 return false; 37 } 38 39 /// Returns the displacement from the frame register to the stack 40 /// frame of the specified index, along with the frame register used 41 /// (in output arg FrameReg). This is the default implementation which 42 /// is overridden for some targets. 43 StackOffset 44 TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, 45 Register &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 StackOffset::getFixed(MFI.getObjectOffset(FI) + MFI.getStackSize() - 55 getOffsetOfLocalArea() + 56 MFI.getOffsetAdjustment()); 57 } 58 59 bool TargetFrameLowering::needsFrameIndexResolution( 60 const MachineFunction &MF) const { 61 return MF.getFrameInfo().hasStackObjects(); 62 } 63 64 void TargetFrameLowering::getCalleeSaves(const MachineFunction &MF, 65 BitVector &CalleeSaves) const { 66 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 67 CalleeSaves.resize(TRI.getNumRegs()); 68 69 const MachineFrameInfo &MFI = MF.getFrameInfo(); 70 if (!MFI.isCalleeSavedInfoValid()) 71 return; 72 73 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) 74 CalleeSaves.set(Info.getReg()); 75 } 76 77 void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF, 78 BitVector &SavedRegs, 79 RegScavenger *RS) const { 80 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 81 82 // Resize before the early returns. Some backends expect that 83 // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no 84 // saved registers. 85 SavedRegs.resize(TRI.getNumRegs()); 86 87 // When interprocedural register allocation is enabled caller saved registers 88 // are preferred over callee saved registers. 89 if (MF.getTarget().Options.EnableIPRA && 90 isSafeForNoCSROpt(MF.getFunction()) && 91 isProfitableForNoCSROpt(MF.getFunction())) 92 return; 93 94 // Get the callee saved register list... 95 const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs(); 96 97 // Early exit if there are no callee saved registers. 98 if (!CSRegs || CSRegs[0] == 0) 99 return; 100 101 // In Naked functions we aren't going to save any registers. 102 if (MF.getFunction().hasFnAttribute(Attribute::Naked)) 103 return; 104 105 // Noreturn+nounwind functions never restore CSR, so no saves are needed. 106 // Purely noreturn functions may still return through throws, so those must 107 // save CSR for caller exception handlers. 108 // 109 // If the function uses longjmp to break out of its current path of 110 // execution we do not need the CSR spills either: setjmp stores all CSRs 111 // it was called with into the jmp_buf, which longjmp then restores. 112 if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) && 113 MF.getFunction().hasFnAttribute(Attribute::NoUnwind) && 114 !MF.getFunction().hasFnAttribute(Attribute::UWTable) && 115 enableCalleeSaveSkip(MF)) 116 return; 117 118 // Functions which call __builtin_unwind_init get all their registers saved. 119 bool CallsUnwindInit = MF.callsUnwindInit(); 120 const MachineRegisterInfo &MRI = MF.getRegInfo(); 121 for (unsigned i = 0; CSRegs[i]; ++i) { 122 unsigned Reg = CSRegs[i]; 123 if (CallsUnwindInit || MRI.isPhysRegModified(Reg)) 124 SavedRegs.set(Reg); 125 } 126 } 127 128 unsigned TargetFrameLowering::getStackAlignmentSkew( 129 const MachineFunction &MF) const { 130 // When HHVM function is called, the stack is skewed as the return address 131 // is removed from the stack before we enter the function. 132 if (LLVM_UNLIKELY(MF.getFunction().getCallingConv() == CallingConv::HHVM)) 133 return MF.getTarget().getAllocaPointerSize(); 134 135 return 0; 136 } 137 138 bool TargetFrameLowering::allocateScavengingFrameIndexesNearIncomingSP( 139 const MachineFunction &MF) const { 140 if (!hasFP(MF)) 141 return false; 142 143 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo(); 144 return RegInfo->useFPForScavengingIndex(MF) && 145 !RegInfo->hasStackRealignment(MF); 146 } 147 148 bool TargetFrameLowering::isSafeForNoCSROpt(const Function &F) { 149 if (!F.hasLocalLinkage() || F.hasAddressTaken() || 150 !F.hasFnAttribute(Attribute::NoRecurse)) 151 return false; 152 // Function should not be optimized as tail call. 153 for (const User *U : F.users()) 154 if (auto *CB = dyn_cast<CallBase>(U)) 155 if (CB->isTailCall()) 156 return false; 157 return true; 158 } 159 160 int TargetFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const { 161 llvm_unreachable("getInitialCFAOffset() not implemented!"); 162 } 163 164 Register 165 TargetFrameLowering::getInitialCFARegister(const MachineFunction &MF) const { 166 llvm_unreachable("getInitialCFARegister() not implemented!"); 167 } 168 169 TargetFrameLowering::DwarfFrameBase 170 TargetFrameLowering::getDwarfFrameBase(const MachineFunction &MF) const { 171 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo(); 172 return DwarfFrameBase{DwarfFrameBase::Register, {RI->getFrameRegister(MF)}}; 173 } 174