blob: a5453449d165a0b84adfd854805b52bcda581762 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCFrameLowering.h - Define frame lowering for PowerPC --*- C++ -*-===//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukmanb4402432005-04-21 23:30:14 +00006//
Nate Begeman412602d2004-08-14 22:16:36 +00007//===----------------------------------------------------------------------===//
8//
9//
Nate Begeman6cca84e2005-10-16 05:39:50 +000010//===----------------------------------------------------------------------===//
Nate Begeman412602d2004-08-14 22:16:36 +000011
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000012#ifndef LLVM_LIB_TARGET_POWERPC_PPCFRAMELOWERING_H
13#define LLVM_LIB_TARGET_POWERPC_PPCFRAMELOWERING_H
Nate Begeman412602d2004-08-14 22:16:36 +000014
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000015#include "PPC.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000016#include "llvm/ADT/STLExtras.h"
David Blaikie1be62f02017-11-03 22:32:11 +000017#include "llvm/CodeGen/TargetFrameLowering.h"
Nate Begeman412602d2004-08-14 22:16:36 +000018#include "llvm/Target/TargetMachine.h"
Nate Begeman412602d2004-08-14 22:16:36 +000019
20namespace llvm {
Eric Christopherd104c312014-06-12 20:54:11 +000021class PPCSubtarget;
Nate Begeman412602d2004-08-14 22:16:36 +000022
Anton Korobeynikov2f931282011-01-10 12:39:04 +000023class PPCFrameLowering: public TargetFrameLowering {
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000024 const PPCSubtarget &Subtarget;
Eric Christopherf71609b2015-02-13 00:39:27 +000025 const unsigned ReturnSaveOffset;
Eric Christopher736d39e2015-02-13 00:39:36 +000026 const unsigned TOCSaveOffset;
Eric Christopherdc3a8a42015-02-13 00:39:38 +000027 const unsigned FramePointerSaveOffset;
Eric Christophera4ae2132015-02-13 22:22:57 +000028 const unsigned LinkageSize;
Eric Christopherfcd3d872015-02-13 22:48:53 +000029 const unsigned BasePointerSaveOffset;
Misha Brukmanb4402432005-04-21 23:30:14 +000030
Kit Barton9c432ae2015-11-16 20:22:15 +000031 /**
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000032 * Find register[s] that can be used in function prologue and epilogue
Kit Barton9c432ae2015-11-16 20:22:15 +000033 *
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +000034 * Find register[s] that can be use as scratch register[s] in function
Kit Barton9c432ae2015-11-16 20:22:15 +000035 * prologue and epilogue to save various registers (Link Register, Base
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +000036 * Pointer, etc.). Prefer R0/R12, if available. Otherwise choose whatever
37 * register[s] are available.
Kit Barton9c432ae2015-11-16 20:22:15 +000038 *
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +000039 * This method will return true if it is able to find enough unique scratch
40 * registers (1 or 2 depending on the requirement). If it is unable to find
41 * enough available registers in the block, it will return false and set
42 * any passed output parameter that corresponds to a required unique register
43 * to PPC::NoRegister.
Kit Barton9c432ae2015-11-16 20:22:15 +000044 *
45 * \param[in] MBB The machine basic block to find an available register for
46 * \param[in] UseAtEnd Specify whether the scratch register will be used at
47 * the end of the basic block (i.e., will the scratch
48 * register kill a register defined in the basic block)
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +000049 * \param[in] TwoUniqueRegsRequired Specify whether this basic block will
50 * require two unique scratch registers.
51 * \param[out] SR1 The scratch register to use
52 * \param[out] SR2 The second scratch register. If this pointer is not null
53 * the function will attempt to set it to an available
54 * register regardless of whether there is a hard requirement
55 * for two unique scratch registers.
56 * \return true if the required number of registers was found.
57 * false if the required number of scratch register weren't available.
58 * If either output parameter refers to a required scratch register
59 * that isn't available, it will be set to an invalid value.
Kit Barton9c432ae2015-11-16 20:22:15 +000060 */
61 bool findScratchRegister(MachineBasicBlock *MBB,
62 bool UseAtEnd,
Nemanja Ivanovicae22101c2016-02-20 18:16:25 +000063 bool TwoUniqueRegsRequired = false,
64 unsigned *SR1 = nullptr,
65 unsigned *SR2 = nullptr) const;
66 bool twoUniqueScratchRegsRequired(MachineBasicBlock *MBB) const;
Kit Barton9c432ae2015-11-16 20:22:15 +000067
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +000068 /**
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000069 * Create branch instruction for PPC::TCRETURN* (tail call return)
Chuang-Yu Chengf8b592f2016-04-01 06:44:32 +000070 *
71 * \param[in] MBB that is terminated by PPC::TCRETURN*
72 */
73 void createTailCallBranchInstr(MachineBasicBlock &MBB) const;
74
Stefan Pintiliebd5429e2019-02-28 12:23:28 +000075 /**
76 * Check if the conditions are correct to allow for the stack update
77 * to be moved past the CSR save/restore code.
78 */
79 bool stackUpdateCanBeMoved(MachineFunction &MF) const;
80
Nate Begeman412602d2004-08-14 22:16:36 +000081public:
Eric Christopherd104c312014-06-12 20:54:11 +000082 PPCFrameLowering(const PPCSubtarget &STI);
Nate Begeman412602d2004-08-14 22:16:36 +000083
Stefan Pintiliebd5429e2019-02-28 12:23:28 +000084 /**
85 * Determine the frame layout and update the machine function.
86 */
87 unsigned determineFrameLayoutAndUpdate(MachineFunction &MF,
88 bool UseEstimate = false) const;
89
90 /**
91 * Determine the frame layout but do not update the machine function.
92 * The MachineFunction object can be const in this case as it is not
93 * modified.
94 */
95 unsigned determineFrameLayout(const MachineFunction &MF,
96 bool UseEstimate = false,
97 unsigned *NewMaxCallFrameSize = nullptr) const;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000098
99 /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
100 /// the function.
Quentin Colombet61b305e2015-05-05 17:38:16 +0000101 void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
Craig Topper0d3fa922014-04-29 07:57:37 +0000102 void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000103
Craig Topper0d3fa922014-04-29 07:57:37 +0000104 bool hasFP(const MachineFunction &MF) const override;
Anton Korobeynikov3eb4fed2010-12-18 19:53:14 +0000105 bool needsFP(const MachineFunction &MF) const;
Hal Finkelaa03c032013-03-21 19:03:19 +0000106 void replaceFPWithRealFP(MachineFunction &MF) const;
Anton Korobeynikov0eecf5d2010-11-18 21:19:35 +0000107
Matthias Braun02564862015-07-14 17:17:13 +0000108 void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs,
109 RegScavenger *RS = nullptr) const override;
Hal Finkel5a765fd2013-03-14 20:33:40 +0000110 void processFunctionBeforeFrameFinalized(MachineFunction &MF,
Craig Topper0d3fa922014-04-29 07:57:37 +0000111 RegScavenger *RS = nullptr) const override;
Hal Finkelbb420f12013-03-15 05:06:04 +0000112 void addScavengingSpillSlot(MachineFunction &MF, RegScavenger *RS) const;
Anton Korobeynikov7283b8d2010-11-27 23:05:25 +0000113
Roman Divackyc9e23d92012-09-12 14:47:47 +0000114 bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
115 MachineBasicBlock::iterator MI,
116 const std::vector<CalleeSavedInfo> &CSI,
Craig Topper0d3fa922014-04-29 07:57:37 +0000117 const TargetRegisterInfo *TRI) const override;
Zaara Syeda5c179bf2018-11-09 16:36:24 +0000118 /// This function will assign callee saved gprs to volatile vector registers
119 /// for prologue spills when applicable. It returns false if there are any
120 /// registers which were not spilled to volatile vector registers.
121 bool
122 assignCalleeSavedSpillSlots(MachineFunction &MF,
123 const TargetRegisterInfo *TRI,
124 std::vector<CalleeSavedInfo> &CSI) const override;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000125
Hans Wennborge1a2e902016-03-31 18:33:38 +0000126 MachineBasicBlock::iterator
127 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
128 MachineBasicBlock::iterator I) const override;
Eli Bendersky8da87162013-02-21 20:05:00 +0000129
Roman Divackyc9e23d92012-09-12 14:47:47 +0000130 bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Craig Topper0d3fa922014-04-29 07:57:37 +0000131 MachineBasicBlock::iterator MI,
Krzysztof Parzyszekbea30c62017-08-10 16:17:32 +0000132 std::vector<CalleeSavedInfo> &CSI,
Craig Topper0d3fa922014-04-29 07:57:37 +0000133 const TargetRegisterInfo *TRI) const override;
Roman Divackyc9e23d92012-09-12 14:47:47 +0000134
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000135 /// targetHandlesStackFrameRounding - Returns true if the target is
136 /// responsible for rounding up the stack frame (probably at emitPrologue
137 /// time).
Craig Topper0d3fa922014-04-29 07:57:37 +0000138 bool targetHandlesStackFrameRounding() const override { return true; }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000139
Jim Laskey527c12f2006-12-06 17:42:06 +0000140 /// getReturnSaveOffset - Return the previous frame offset to save the
141 /// return address.
Eric Christopherf71609b2015-02-13 00:39:27 +0000142 unsigned getReturnSaveOffset() const { return ReturnSaveOffset; }
Jim Laskey527c12f2006-12-06 17:42:06 +0000143
Ulrich Weigandad0cb912014-06-18 17:52:49 +0000144 /// getTOCSaveOffset - Return the previous frame offset to save the
145 /// TOC register -- 64-bit SVR4 ABI only.
Eric Christopher736d39e2015-02-13 00:39:36 +0000146 unsigned getTOCSaveOffset() const { return TOCSaveOffset; }
Ulrich Weigandad0cb912014-06-18 17:52:49 +0000147
Jim Laskey48850c12006-11-16 22:43:37 +0000148 /// getFramePointerSaveOffset - Return the previous frame offset to save the
149 /// frame pointer.
Eric Christopherdc3a8a42015-02-13 00:39:38 +0000150 unsigned getFramePointerSaveOffset() const { return FramePointerSaveOffset; }
Anton Korobeynikova5ab8f12010-11-15 00:06:05 +0000151
Hal Finkela7c54e82013-07-17 00:45:52 +0000152 /// getBasePointerSaveOffset - Return the previous frame offset to save the
153 /// base pointer.
Eric Christopherfcd3d872015-02-13 22:48:53 +0000154 unsigned getBasePointerSaveOffset() const { return BasePointerSaveOffset; }
Hal Finkela7c54e82013-07-17 00:45:52 +0000155
Jim Laskey48850c12006-11-16 22:43:37 +0000156 /// getLinkageSize - Return the size of the PowerPC ABI linkage area.
157 ///
Eric Christophera4ae2132015-02-13 22:22:57 +0000158 unsigned getLinkageSize() const { return LinkageSize; }
Jim Laskey48850c12006-11-16 22:43:37 +0000159
Tilmann Scheller336e2bd2009-09-27 17:58:47 +0000160 const SpillSlot *
Eric Christopherd104c312014-06-12 20:54:11 +0000161 getCalleeSavedSpillSlots(unsigned &NumEntries) const override;
Kit Bartond3b904d2015-09-10 01:55:44 +0000162
163 bool enableShrinkWrapping(const MachineFunction &MF) const override;
Kit Barton9c432ae2015-11-16 20:22:15 +0000164
165 /// Methods used by shrink wrapping to determine if MBB can be used for the
166 /// function prologue/epilogue.
167 bool canUseAsPrologue(const MachineBasicBlock &MBB) const override;
168 bool canUseAsEpilogue(const MachineBasicBlock &MBB) const override;
Nate Begeman412602d2004-08-14 22:16:36 +0000169};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000170} // End llvm namespace
Nate Begeman412602d2004-08-14 22:16:36 +0000171
172#endif