[PM/AA] Hoist ScopedNoAliasAA's interface into a header and move the
[oota-llvm.git] / lib / CodeGen / TargetFrameLoweringImpl.cpp
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/Function.h"
21 #include "llvm/Target/TargetRegisterInfo.h"
22 #include "llvm/Target/TargetSubtargetInfo.h"
23 #include <cstdlib>
24 using namespace llvm;
25
26 TargetFrameLowering::~TargetFrameLowering() {
27 }
28
29 /// The default implementation just looks at attribute "no-frame-pointer-elim".
30 bool TargetFrameLowering::noFramePointerElim(const MachineFunction &MF) const {
31   auto Attr = MF.getFunction()->getFnAttribute("no-frame-pointer-elim");
32   return Attr.getValueAsString() == "true";
33 }
34
35 /// getFrameIndexOffset - Returns the displacement from the frame register to
36 /// the stack frame of the specified index. This is the default implementation
37 /// which is overridden for some targets.
38 int TargetFrameLowering::getFrameIndexOffset(const MachineFunction &MF,
39                                              int FI) const {
40   const MachineFrameInfo *MFI = MF.getFrameInfo();
41   return MFI->getObjectOffset(FI) + MFI->getStackSize() -
42     getOffsetOfLocalArea() + MFI->getOffsetAdjustment();
43 }
44
45 int TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF,
46                                              int FI, unsigned &FrameReg) const {
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   return getFrameIndexOffset(MF, FI);
54 }
55
56 bool TargetFrameLowering::needsFrameIndexResolution(
57     const MachineFunction &MF) const {
58   return MF.getFrameInfo()->hasStackObjects();
59 }
60
61 void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF,
62                                                BitVector &SavedRegs,
63                                                RegScavenger *RS) const {
64   // Get the callee saved register list...
65   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
66   const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
67
68   // Early exit if there are no callee saved registers.
69   if (!CSRegs || CSRegs[0] == 0)
70     return;
71
72   SavedRegs.resize(TRI.getNumRegs());
73
74   // In Naked functions we aren't going to save any registers.
75   if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
76     return;
77
78   // Functions which call __builtin_unwind_init get all their registers saved.
79   bool CallsUnwindInit = MF.getMMI().callsUnwindInit();
80   const MachineRegisterInfo &MRI = MF.getRegInfo();
81   for (unsigned i = 0; CSRegs[i]; ++i) {
82     unsigned Reg = CSRegs[i];
83     if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
84       SavedRegs.set(Reg);
85   }
86 }