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