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