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/IR/Attributes.h" 19 #include "llvm/IR/CallingConv.h" 20 #include "llvm/IR/Function.h" 21 #include "llvm/MC/MCRegisterInfo.h" 22 #include "llvm/Support/Compiler.h" 23 #include "llvm/Target/TargetFrameLowering.h" 24 #include "llvm/Target/TargetMachine.h" 25 #include "llvm/Target/TargetOptions.h" 26 #include "llvm/Target/TargetRegisterInfo.h" 27 #include "llvm/Target/TargetSubtargetInfo.h" 28 29 using namespace llvm; 30 31 TargetFrameLowering::~TargetFrameLowering() = default; 32 33 /// The default implementation just looks at attribute "no-frame-pointer-elim". 34 bool TargetFrameLowering::noFramePointerElim(const MachineFunction &MF) const { 35 auto Attr = MF.getFunction()->getFnAttribute("no-frame-pointer-elim"); 36 return Attr.getValueAsString() == "true"; 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 int TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF, 44 int FI, unsigned &FrameReg) const { 45 const MachineFrameInfo &MFI = MF.getFrameInfo(); 46 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo(); 47 48 // By default, assume all frame indices are referenced via whatever 49 // getFrameRegister() says. The target can override this if it's doing 50 // something different. 51 FrameReg = RI->getFrameRegister(MF); 52 53 return MFI.getObjectOffset(FI) + MFI.getStackSize() - 54 getOffsetOfLocalArea() + MFI.getOffsetAdjustment(); 55 } 56 57 bool TargetFrameLowering::needsFrameIndexResolution( 58 const MachineFunction &MF) const { 59 return MF.getFrameInfo().hasStackObjects(); 60 } 61 62 void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF, 63 BitVector &SavedRegs, 64 RegScavenger *RS) const { 65 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 66 67 // Resize before the early returns. Some backends expect that 68 // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no 69 // saved registers. 70 SavedRegs.resize(TRI.getNumRegs()); 71 72 // When interprocedural register allocation is enabled caller saved registers 73 // are preferred over callee saved registers. 74 if (MF.getTarget().Options.EnableIPRA && isSafeForNoCSROpt(MF.getFunction())) 75 return; 76 77 // Get the callee saved register list... 78 const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs(); 79 80 // Early exit if there are no callee saved registers. 81 if (!CSRegs || CSRegs[0] == 0) 82 return; 83 84 // In Naked functions we aren't going to save any registers. 85 if (MF.getFunction()->hasFnAttribute(Attribute::Naked)) 86 return; 87 88 // Functions which call __builtin_unwind_init get all their registers saved. 89 bool CallsUnwindInit = MF.callsUnwindInit(); 90 const MachineRegisterInfo &MRI = MF.getRegInfo(); 91 for (unsigned i = 0; CSRegs[i]; ++i) { 92 unsigned Reg = CSRegs[i]; 93 if (CallsUnwindInit || MRI.isPhysRegModified(Reg)) 94 SavedRegs.set(Reg); 95 } 96 } 97 98 unsigned TargetFrameLowering::getStackAlignmentSkew( 99 const MachineFunction &MF) const { 100 // When HHVM function is called, the stack is skewed as the return address 101 // is removed from the stack before we enter the function. 102 if (LLVM_UNLIKELY(MF.getFunction()->getCallingConv() == CallingConv::HHVM)) 103 return MF.getTarget().getPointerSize(); 104 105 return 0; 106 } 107