blob: 679ade185e1c69cfbd700282507133af37f91b7e [file] [log] [blame]
Nick Lewyckyc9e935c2011-12-15 22:58:58 +00001//===----- TargetFrameLoweringImpl.cpp - Implement target frame interface --==//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Misha Brukman505a0832004-03-11 23:52:43 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Misha Brukman505a0832004-03-11 23:52:43 +00008//===----------------------------------------------------------------------===//
9//
10// Implements the layout of a stack frame on the target machine.
11//
12//===----------------------------------------------------------------------===//
13
Matthias Braun02564862015-07-14 17:17:13 +000014#include "llvm/ADT/BitVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/Target/TargetFrameLowering.h"
Anton Korobeynikov36590fc2010-11-20 16:14:57 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineFunction.h"
Matthias Braun02564862015-07-14 17:17:13 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
Maksim Panchenkocce239c2015-09-29 22:09:16 +000020#include "llvm/IR/CallingConv.h"
Akira Hatanakaddf76aa2015-05-23 01:14:08 +000021#include "llvm/IR/Function.h"
Anton Korobeynikov46877782010-11-20 15:59:32 +000022#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000023#include "llvm/Target/TargetSubtargetInfo.h"
Misha Brukman505a0832004-03-11 23:52:43 +000024#include <cstdlib>
Misha Brukman505a0832004-03-11 23:52:43 +000025using namespace llvm;
26
Anton Korobeynikov2f931282011-01-10 12:39:04 +000027TargetFrameLowering::~TargetFrameLowering() {
Reid Spencerff7b16c2005-04-25 02:55:55 +000028}
Anton Korobeynikov14ee3442010-11-18 23:25:52 +000029
Akira Hatanakaddf76aa2015-05-23 01:14:08 +000030/// The default implementation just looks at attribute "no-frame-pointer-elim".
31bool TargetFrameLowering::noFramePointerElim(const MachineFunction &MF) const {
32 auto Attr = MF.getFunction()->getFnAttribute("no-frame-pointer-elim");
33 return Attr.getValueAsString() == "true";
34}
35
James Y Knight5567baf2015-08-15 02:32:35 +000036/// 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.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000040int TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF,
41 int FI, unsigned &FrameReg) const {
James Y Knight5567baf2015-08-15 02:32:35 +000042 const MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +000043 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikov46877782010-11-20 15:59:32 +000044
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);
James Y Knight5567baf2015-08-15 02:32:35 +000049
50 return MFI->getObjectOffset(FI) + MFI->getStackSize() -
51 getOffsetOfLocalArea() + MFI->getOffsetAdjustment();
Anton Korobeynikov46877782010-11-20 15:59:32 +000052}
Michael Kuperstein13fbd452015-02-01 16:56:04 +000053
54bool TargetFrameLowering::needsFrameIndexResolution(
55 const MachineFunction &MF) const {
56 return MF.getFrameInfo()->hasStackObjects();
57}
Matthias Braun02564862015-07-14 17:17:13 +000058
59void 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 // Early exit if there are no callee saved registers.
67 if (!CSRegs || CSRegs[0] == 0)
68 return;
69
70 SavedRegs.resize(TRI.getNumRegs());
71
72 // In Naked functions we aren't going to save any registers.
73 if (MF.getFunction()->hasFnAttribute(Attribute::Naked))
74 return;
75
76 // Functions which call __builtin_unwind_init get all their registers saved.
77 bool CallsUnwindInit = MF.getMMI().callsUnwindInit();
78 const MachineRegisterInfo &MRI = MF.getRegInfo();
79 for (unsigned i = 0; CSRegs[i]; ++i) {
80 unsigned Reg = CSRegs[i];
81 if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
82 SavedRegs.set(Reg);
83 }
84}
Maksim Panchenkocce239c2015-09-29 22:09:16 +000085
86unsigned TargetFrameLowering::getStackAlignmentSkew(
87 const MachineFunction &MF) const {
88 // When HHVM function is called, the stack is skewed as the return address
89 // is removed from the stack before we enter the function.
90 if (LLVM_UNLIKELY(MF.getFunction()->getCallingConv() == CallingConv::HHVM))
91 return MF.getTarget().getPointerSize();
92
93 return 0;
94}