Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1 | //===- TargetFrameLoweringImpl.cpp - Implement target frame interface ------==// |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Misha Brukman | 505a083 | 2004-03-11 23:52:43 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Misha Brukman | 505a083 | 2004-03-11 23:52:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implements the layout of a stack frame on the target machine. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/BitVector.h" |
Anton Korobeynikov | 36590fc | 2010-11-20 16:14:57 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 16 | #include "llvm/CodeGen/MachineFunction.h" |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/TargetFrameLowering.h" |
| 19 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 20 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Attributes.h" |
Maksim Panchenko | cce239c | 2015-09-29 22:09:16 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CallingConv.h" |
Akira Hatanaka | ddf76aa | 2015-05-23 01:14:08 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Function.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCRegisterInfo.h" |
| 25 | #include "llvm/Support/Compiler.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
| 27 | #include "llvm/Target/TargetOptions.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 28 | |
Misha Brukman | 505a083 | 2004-03-11 23:52:43 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 31 | TargetFrameLowering::~TargetFrameLowering() = default; |
Anton Korobeynikov | 14ee344 | 2010-11-18 23:25:52 +0000 | [diff] [blame] | 32 | |
Akira Hatanaka | ddf76aa | 2015-05-23 01:14:08 +0000 | [diff] [blame] | 33 | /// The default implementation just looks at attribute "no-frame-pointer-elim". |
| 34 | bool TargetFrameLowering::noFramePointerElim(const MachineFunction &MF) const { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 35 | auto Attr = MF.getFunction().getFnAttribute("no-frame-pointer-elim"); |
Akira Hatanaka | ddf76aa | 2015-05-23 01:14:08 +0000 | [diff] [blame] | 36 | return Attr.getValueAsString() == "true"; |
| 37 | } |
| 38 | |
Tim Northover | e25e458 | 2018-04-07 10:57:03 +0000 | [diff] [blame] | 39 | bool 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 Knight | 5567baf | 2015-08-15 02:32:35 +0000 | [diff] [blame] | 46 | /// 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 Korobeynikov | 2f93128 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 50 | int TargetFrameLowering::getFrameIndexReference(const MachineFunction &MF, |
| 51 | int FI, unsigned &FrameReg) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 52 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 53 | const TargetRegisterInfo *RI = MF.getSubtarget().getRegisterInfo(); |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 54 | |
| 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 Knight | 5567baf | 2015-08-15 02:32:35 +0000 | [diff] [blame] | 59 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 60 | return MFI.getObjectOffset(FI) + MFI.getStackSize() - |
| 61 | getOffsetOfLocalArea() + MFI.getOffsetAdjustment(); |
Anton Korobeynikov | 4687778 | 2010-11-20 15:59:32 +0000 | [diff] [blame] | 62 | } |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 63 | |
| 64 | bool TargetFrameLowering::needsFrameIndexResolution( |
| 65 | const MachineFunction &MF) const { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 66 | return MF.getFrameInfo().hasStackObjects(); |
Michael Kuperstein | 13fbd45 | 2015-02-01 16:56:04 +0000 | [diff] [blame] | 67 | } |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 68 | |
| 69 | void TargetFrameLowering::determineCalleeSaves(MachineFunction &MF, |
| 70 | BitVector &SavedRegs, |
| 71 | RegScavenger *RS) const { |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 72 | const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 73 | |
Chuang-Yu Cheng | 98c1894 | 2016-04-08 12:04:32 +0000 | [diff] [blame] | 74 | // 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 Amini | 4beea66 | 2016-07-13 23:39:34 +0000 | [diff] [blame] | 79 | // When interprocedural register allocation is enabled caller saved registers |
| 80 | // are preferred over callee saved registers. |
Mehdi Amini | cfed256 | 2016-07-13 23:39:46 +0000 | [diff] [blame] | 81 | if (MF.getTarget().Options.EnableIPRA && isSafeForNoCSROpt(MF.getFunction())) |
Mehdi Amini | 4beea66 | 2016-07-13 23:39:34 +0000 | [diff] [blame] | 82 | return; |
| 83 | |
| 84 | // Get the callee saved register list... |
Oren Ben Simhon | fe34c5e | 2017-03-14 09:09:26 +0000 | [diff] [blame] | 85 | const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs(); |
Mehdi Amini | 4beea66 | 2016-07-13 23:39:34 +0000 | [diff] [blame] | 86 | |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 87 | // Early exit if there are no callee saved registers. |
| 88 | if (!CSRegs || CSRegs[0] == 0) |
| 89 | return; |
| 90 | |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 91 | // In Naked functions we aren't going to save any registers. |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 92 | if (MF.getFunction().hasFnAttribute(Attribute::Naked)) |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 93 | return; |
| 94 | |
Tim Northover | e25e458 | 2018-04-07 10:57:03 +0000 | [diff] [blame] | 95 | // 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 Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 108 | // Functions which call __builtin_unwind_init get all their registers saved. |
Matthias Braun | d0ee66c | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 109 | bool CallsUnwindInit = MF.callsUnwindInit(); |
Matthias Braun | 0256486 | 2015-07-14 17:17:13 +0000 | [diff] [blame] | 110 | 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 Panchenko | cce239c | 2015-09-29 22:09:16 +0000 | [diff] [blame] | 117 | |
| 118 | unsigned 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 Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 122 | if (LLVM_UNLIKELY(MF.getFunction().getCallingConv() == CallingConv::HHVM)) |
Matt Arsenault | 41e5ac4 | 2018-03-14 00:36:23 +0000 | [diff] [blame] | 123 | return MF.getTarget().getAllocaPointerSize(); |
Maksim Panchenko | cce239c | 2015-09-29 22:09:16 +0000 | [diff] [blame] | 124 | |
| 125 | return 0; |
| 126 | } |
Petar Jovanovic | e2bfcd6 | 2018-04-24 10:32:08 +0000 | [diff] [blame] | 127 | |
| 128 | int TargetFrameLowering::getInitialCFAOffset(const MachineFunction &MF) const { |
| 129 | llvm_unreachable("getInitialCFAOffset() not implemented!"); |
| 130 | } |
| 131 | |
| 132 | unsigned TargetFrameLowering::getInitialCFARegister(const MachineFunction &MF) |
| 133 | const { |
| 134 | llvm_unreachable("getInitialCFARegister() not implemented!"); |
| 135 | } |