10b57cec5SDimitry Andric //===- TargetFrameLoweringImpl.cpp - Implement target frame interface ------==// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // Implements the layout of a stack frame on the target machine. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h" 140b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 150b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 170b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 200b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 21*8bcb0991SDimitry Andric #include "llvm/IR/CallSite.h" 220b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h" 230b57cec5SDimitry Andric #include "llvm/IR/Function.h" 240b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 250b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 260b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 270b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric using namespace llvm; 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric TargetFrameLowering::~TargetFrameLowering() = default; 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric bool TargetFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const { 340b57cec5SDimitry Andric assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) && 350b57cec5SDimitry Andric MF.getFunction().hasFnAttribute(Attribute::NoUnwind) && 360b57cec5SDimitry Andric !MF.getFunction().hasFnAttribute(Attribute::UWTable)); 370b57cec5SDimitry Andric return false; 380b57cec5SDimitry Andric } 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric /// Returns the displacement from the frame register to the stack 410b57cec5SDimitry Andric /// frame of the specified index, along with the frame register used 420b57cec5SDimitry Andric /// (in output arg FrameReg). This is the default implementation which 430b57cec5SDimitry Andric /// is overridden for some targets. 440b57cec5SDimitry Andric int TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF, 450b57cec5SDimitry Andric int FI, unsigned &FrameReg) const { 460b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 470b57cec5SDimitry Andric const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo(); 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric // By default, assume all frame indices are referenced via whatever 500b57cec5SDimitry Andric // getFrameRegister() says. The target can override this if it's doing 510b57cec5SDimitry Andric // something different. 520b57cec5SDimitry Andric FrameReg = RI->getFrameRegister(MF); 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric return MFI.getObjectOffset(FI) + MFI.getStackSize() - 550b57cec5SDimitry Andric getOffsetOfLocalArea() + MFI.getOffsetAdjustment(); 560b57cec5SDimitry Andric } 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric bool TargetFrameLowering::needsFrameIndexResolution( 590b57cec5SDimitry Andric const MachineFunction &MF) const { 600b57cec5SDimitry Andric return MF.getFrameInfo().hasStackObjects(); 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF, 640b57cec5SDimitry Andric BitVector &SavedRegs, 650b57cec5SDimitry Andric RegScavenger *RS) const { 660b57cec5SDimitry Andric const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric // Resize before the early returns. Some backends expect that 690b57cec5SDimitry Andric // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no 700b57cec5SDimitry Andric // saved registers. 710b57cec5SDimitry Andric SavedRegs.resize(TRI.getNumRegs()); 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric // When interprocedural register allocation is enabled caller saved registers 740b57cec5SDimitry Andric // are preferred over callee saved registers. 75*8bcb0991SDimitry Andric if (MF.getTarget().Options.EnableIPRA && 76*8bcb0991SDimitry Andric isSafeForNoCSROpt(MF.getFunction()) && 77*8bcb0991SDimitry Andric isProfitableForNoCSROpt(MF.getFunction())) 780b57cec5SDimitry Andric return; 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric // Get the callee saved register list... 810b57cec5SDimitry Andric const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs(); 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric // Early exit if there are no callee saved registers. 840b57cec5SDimitry Andric if (!CSRegs || CSRegs[0] == 0) 850b57cec5SDimitry Andric return; 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric // In Naked functions we aren't going to save any registers. 880b57cec5SDimitry Andric if (MF.getFunction().hasFnAttribute(Attribute::Naked)) 890b57cec5SDimitry Andric return; 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric // Noreturn+nounwind functions never restore CSR, so no saves are needed. 920b57cec5SDimitry Andric // Purely noreturn functions may still return through throws, so those must 930b57cec5SDimitry Andric // save CSR for caller exception handlers. 940b57cec5SDimitry Andric // 950b57cec5SDimitry Andric // If the function uses longjmp to break out of its current path of 960b57cec5SDimitry Andric // execution we do not need the CSR spills either: setjmp stores all CSRs 970b57cec5SDimitry Andric // it was called with into the jmp_buf, which longjmp then restores. 980b57cec5SDimitry Andric if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) && 990b57cec5SDimitry Andric MF.getFunction().hasFnAttribute(Attribute::NoUnwind) && 1000b57cec5SDimitry Andric !MF.getFunction().hasFnAttribute(Attribute::UWTable) && 1010b57cec5SDimitry Andric enableCalleeSaveSkip(MF)) 1020b57cec5SDimitry Andric return; 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric // Functions which call __builtin_unwind_init get all their registers saved. 1050b57cec5SDimitry Andric bool CallsUnwindInit = MF.callsUnwindInit(); 1060b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 1070b57cec5SDimitry Andric for (unsigned i = 0; CSRegs[i]; ++i) { 1080b57cec5SDimitry Andric unsigned Reg = CSRegs[i]; 1090b57cec5SDimitry Andric if (CallsUnwindInit || MRI.isPhysRegModified(Reg)) 1100b57cec5SDimitry Andric SavedRegs.set(Reg); 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric } 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric unsigned TargetFrameLowering::getStackAlignmentSkew( 1150b57cec5SDimitry Andric const MachineFunction &MF) const { 1160b57cec5SDimitry Andric // When HHVM function is called, the stack is skewed as the return address 1170b57cec5SDimitry Andric // is removed from the stack before we enter the function. 1180b57cec5SDimitry Andric if (LLVM_UNLIKELY(MF.getFunction().getCallingConv() == CallingConv::HHVM)) 1190b57cec5SDimitry Andric return MF.getTarget().getAllocaPointerSize(); 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric return 0; 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric 124*8bcb0991SDimitry Andric bool TargetFrameLowering::isSafeForNoCSROpt(const Function &F) { 125*8bcb0991SDimitry Andric if (!F.hasLocalLinkage() || F.hasAddressTaken() || 126*8bcb0991SDimitry Andric !F.hasFnAttribute(Attribute::NoRecurse)) 127*8bcb0991SDimitry Andric return false; 128*8bcb0991SDimitry Andric // Function should not be optimized as tail call. 129*8bcb0991SDimitry Andric for (const User *U : F.users()) 130*8bcb0991SDimitry Andric if (auto CS = ImmutableCallSite(U)) 131*8bcb0991SDimitry Andric if (CS.isTailCall()) 132*8bcb0991SDimitry Andric return false; 133*8bcb0991SDimitry Andric return true; 134*8bcb0991SDimitry Andric } 135*8bcb0991SDimitry Andric 1360b57cec5SDimitry Andric int TargetFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const { 1370b57cec5SDimitry Andric llvm_unreachable("getInitialCFAOffset() not implemented!"); 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric unsigned TargetFrameLowering::getInitialCFARegister(const MachineFunction &MF) 1410b57cec5SDimitry Andric const { 1420b57cec5SDimitry Andric llvm_unreachable("getInitialCFARegister() not implemented!"); 1430b57cec5SDimitry Andric } 144