1 //===----- TargetFrameLoweringImpl.cpp - Implement target frame interface --==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implements the layout of a stack frame on the target machine.
12 //===----------------------------------------------------------------------===//
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"
26 TargetFrameLowering::~TargetFrameLowering() {
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";
35 /// Returns the displacement from the frame register to the stack
36 /// frame of the specified index, along with the frame register used
37 /// (in output arg FrameReg). This is the default implementation which
38 /// is overridden for some targets.
39 int TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF,
40 int FI, unsigned &FrameReg) const {
41 const MachineFrameInfo *MFI = MF.getFrameInfo();
42 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
44 // By default, assume all frame indices are referenced via whatever
45 // getFrameRegister() says. The target can override this if it's doing
46 // something different.
47 FrameReg = RI->getFrameRegister(MF);
49 return MFI->getObjectOffset(FI) + MFI->getStackSize() -
50 getOffsetOfLocalArea() + MFI->getOffsetAdjustment();
53 bool TargetFrameLowering::needsFrameIndexResolution(
54 const MachineFunction &MF) const {
55 return MF.getFrameInfo()->hasStackObjects();
58 void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF,
60 RegScavenger *RS) const {
61 // Get the callee saved register list...
62 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
63 const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
65 // Early exit if there are no callee saved registers.
66 if (!CSRegs || CSRegs[0] == 0)
69 SavedRegs.resize(TRI.getNumRegs());
71 // In Naked functions we aren't going to save any registers.
72 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
75 // Functions which call __builtin_unwind_init get all their registers saved.
76 bool CallsUnwindInit = MF.getMMI().callsUnwindInit();
77 const MachineRegisterInfo &MRI = MF.getRegInfo();
78 for (unsigned i = 0; CSRegs[i]; ++i) {
79 unsigned Reg = CSRegs[i];
80 if (CallsUnwindInit || MRI.isPhysRegModified(Reg))