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 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 && isSafeForNoCSROpt(MF.getFunction())) 76 return; 77 78 // Get the callee saved register list... 79 const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs(); 80 81 // Early exit if there are no callee saved registers. 82 if (!CSRegs || CSRegs[0] == 0) 83 return; 84 85 // In Naked functions we aren't going to save any registers. 86 if (MF.getFunction().hasFnAttribute(Attribute::Naked)) 87 return; 88 89 // Noreturn+nounwind functions never restore CSR, so no saves are needed. 90 // Purely noreturn functions may still return through throws, so those must 91 // save CSR for caller exception handlers. 92 // 93 // If the function uses longjmp to break out of its current path of 94 // execution we do not need the CSR spills either: setjmp stores all CSRs 95 // it was called with into the jmp_buf, which longjmp then restores. 96 if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) && 97 MF.getFunction().hasFnAttribute(Attribute::NoUnwind) && 98 !MF.getFunction().hasFnAttribute(Attribute::UWTable) && 99 enableCalleeSaveSkip(MF)) 100 return; 101 102 // Functions which call __builtin_unwind_init get all their registers saved. 103 bool CallsUnwindInit = MF.callsUnwindInit(); 104 const MachineRegisterInfo &MRI = MF.getRegInfo(); 105 for (unsigned i = 0; CSRegs[i]; ++i) { 106 unsigned Reg = CSRegs[i]; 107 if (CallsUnwindInit || MRI.isPhysRegModified(Reg)) 108 SavedRegs.set(Reg); 109 } 110 } 111 112 unsigned TargetFrameLowering::getStackAlignmentSkew( 113 const MachineFunction &MF) const { 114 // When HHVM function is called, the stack is skewed as the return address 115 // is removed from the stack before we enter the function. 116 if (LLVM_UNLIKELY(MF.getFunction().getCallingConv() == CallingConv::HHVM)) 117 return MF.getTarget().getAllocaPointerSize(); 118 119 return 0; 120 } 121 122 int TargetFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const { 123 llvm_unreachable("getInitialCFAOffset() not implemented!"); 124 } 125 126 unsigned TargetFrameLowering::getInitialCFARegister(const MachineFunction &MF) 127 const { 128 llvm_unreachable("getInitialCFARegister() not implemented!"); 129 }