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