blob: f0cfa2fbe4fddf3dc54b0a41b487141e2fa68ea5 [file] [log] [blame]
Eugene Zelenkofb69e662017-06-06 22:22:41 +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"
Anton Korobeynikov36590fc2010-11-20 16:14:57 +000015#include "llvm/CodeGen/MachineFrameInfo.h"
16#include "llvm/CodeGen/MachineFunction.h"
Matthias Braun02564862015-07-14 17:17:13 +000017#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000018#include "llvm/CodeGen/TargetFrameLowering.h"
19#include "llvm/CodeGen/TargetRegisterInfo.h"
20#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000021#include "llvm/IR/Attributes.h"
Maksim Panchenkocce239c2015-09-29 22:09:16 +000022#include "llvm/IR/CallingConv.h"
Akira Hatanakaddf76aa2015-05-23 01:14:08 +000023#include "llvm/IR/Function.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000024#include "llvm/MC/MCRegisterInfo.h"
25#include "llvm/Support/Compiler.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Target/TargetOptions.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000028
Misha Brukman505a0832004-03-11 23:52:43 +000029using namespace llvm;
30
Eugene Zelenkofb69e662017-06-06 22:22:41 +000031TargetFrameLowering::~TargetFrameLowering() = default;
Anton Korobeynikov14ee3442010-11-18 23:25:52 +000032
Akira Hatanakaddf76aa2015-05-23 01:14:08 +000033/// The default implementation just looks at attribute "no-frame-pointer-elim".
34bool TargetFrameLowering::noFramePointerElim(const MachineFunction &MF) const {
Matthias Braunf1caa282017-12-15 22:22:58 +000035 auto Attr = MF.getFunction().getFnAttribute("no-frame-pointer-elim");
Akira Hatanakaddf76aa2015-05-23 01:14:08 +000036 return Attr.getValueAsString() == "true";
37}
38
Tim Northovere25e4582018-04-07 10:57:03 +000039bool TargetFrameLowering::enableCalleeSaveSkip(const MachineFunction &MF) const {
40 assert(MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
41 MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
42 !MF.getFunction().hasFnAttribute(Attribute::UWTable));
43 return false;
44}
45
James Y Knight5567baf2015-08-15 02:32:35 +000046/// Returns the displacement from the frame register to the stack
47/// frame of the specified index, along with the frame register used
48/// (in output arg FrameReg). This is the default implementation which
49/// is overridden for some targets.
Anton Korobeynikov2f931282011-01-10 12:39:04 +000050int TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF,
51 int FI, unsigned &FrameReg) const {
Matthias Braun941a7052016-07-28 18:40:00 +000052 const MachineFrameInfo &MFI = MF.getFrameInfo();
Eric Christopherfc6de422014-08-05 02:39:49 +000053 const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo();
Anton Korobeynikov46877782010-11-20 15:59:32 +000054
55 // By default, assume all frame indices are referenced via whatever
56 // getFrameRegister() says. The target can override this if it's doing
57 // something different.
58 FrameReg = RI->getFrameRegister(MF);
James Y Knight5567baf2015-08-15 02:32:35 +000059
Matthias Braun941a7052016-07-28 18:40:00 +000060 return MFI.getObjectOffset(FI) + MFI.getStackSize() -
61 getOffsetOfLocalArea() + MFI.getOffsetAdjustment();
Anton Korobeynikov46877782010-11-20 15:59:32 +000062}
Michael Kuperstein13fbd452015-02-01 16:56:04 +000063
64bool TargetFrameLowering::needsFrameIndexResolution(
65 const MachineFunction &MF) const {
Matthias Braun941a7052016-07-28 18:40:00 +000066 return MF.getFrameInfo().hasStackObjects();
Michael Kuperstein13fbd452015-02-01 16:56:04 +000067}
Matthias Braun02564862015-07-14 17:17:13 +000068
69void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF,
70 BitVector &SavedRegs,
71 RegScavenger *RS) const {
Matthias Braun02564862015-07-14 17:17:13 +000072 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
Matthias Braun02564862015-07-14 17:17:13 +000073
Chuang-Yu Cheng98c18942016-04-08 12:04:32 +000074 // Resize before the early returns. Some backends expect that
75 // SavedRegs.size() == TRI.getNumRegs() after this call even if there are no
76 // saved registers.
77 SavedRegs.resize(TRI.getNumRegs());
78
Mehdi Amini4beea662016-07-13 23:39:34 +000079 // When interprocedural register allocation is enabled caller saved registers
80 // are preferred over callee saved registers.
Mehdi Aminicfed2562016-07-13 23:39:46 +000081 if (MF.getTarget().Options.EnableIPRA && isSafeForNoCSROpt(MF.getFunction()))
Mehdi Amini4beea662016-07-13 23:39:34 +000082 return;
83
84 // Get the callee saved register list...
Oren Ben Simhonfe34c5e2017-03-14 09:09:26 +000085 const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
Mehdi Amini4beea662016-07-13 23:39:34 +000086
Matthias Braun02564862015-07-14 17:17:13 +000087 // Early exit if there are no callee saved registers.
88 if (!CSRegs || CSRegs[0] == 0)
89 return;
90
Matthias Braun02564862015-07-14 17:17:13 +000091 // In Naked functions we aren't going to save any registers.
Matthias Braunf1caa282017-12-15 22:22:58 +000092 if (MF.getFunction().hasFnAttribute(Attribute::Naked))
Matthias Braun02564862015-07-14 17:17:13 +000093 return;
94
Tim Northovere25e4582018-04-07 10:57:03 +000095 // Noreturn+nounwind functions never restore CSR, so no saves are needed.
96 // Purely noreturn functions may still return through throws, so those must
97 // save CSR for caller exception handlers.
98 //
99 // If the function uses longjmp to break out of its current path of
100 // execution we do not need the CSR spills either: setjmp stores all CSRs
101 // it was called with into the jmp_buf, which longjmp then restores.
102 if (MF.getFunction().hasFnAttribute(Attribute::NoReturn) &&
103 MF.getFunction().hasFnAttribute(Attribute::NoUnwind) &&
104 !MF.getFunction().hasFnAttribute(Attribute::UWTable) &&
105 enableCalleeSaveSkip(MF))
106 return;
107
Matthias Braun02564862015-07-14 17:17:13 +0000108 // Functions which call __builtin_unwind_init get all their registers saved.
Matthias Braund0ee66c2016-12-01 19:32:15 +0000109 bool CallsUnwindInit = MF.callsUnwindInit();
Matthias Braun02564862015-07-14 17:17:13 +0000110 const MachineRegisterInfo &MRI = MF.getRegInfo();
111 for (unsigned i = 0; CSRegs[i]; ++i) {
112 unsigned Reg = CSRegs[i];
113 if (CallsUnwindInit || MRI.isPhysRegModified(Reg))
114 SavedRegs.set(Reg);
115 }
116}
Maksim Panchenkocce239c2015-09-29 22:09:16 +0000117
118unsigned TargetFrameLowering::getStackAlignmentSkew(
119 const MachineFunction &MF) const {
120 // When HHVM function is called, the stack is skewed as the return address
121 // is removed from the stack before we enter the function.
Matthias Braunf1caa282017-12-15 22:22:58 +0000122 if (LLVM_UNLIKELY(MF.getFunction().getCallingConv() == CallingConv::HHVM))
Matt Arsenault41e5ac42018-03-14 00:36:23 +0000123 return MF.getTarget().getAllocaPointerSize();
Maksim Panchenkocce239c2015-09-29 22:09:16 +0000124
125 return 0;
126}
Petar Jovanovice2bfcd62018-04-24 10:32:08 +0000127
128int TargetFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const {
129 llvm_unreachable("getInitialCFAOffset() not implemented!");
130}
131
132unsigned TargetFrameLowering::getInitialCFARegister(const MachineFunction &MF)
133 const {
134 llvm_unreachable("getInitialCFARegister() not implemented!");
135}