blob: fb49cd90b1e02b52d2364b8b186727659a692308 [file] [log] [blame]
Nick Lewyckyc3890d22015-07-29 22:32:47 +00001//=- AArch64MachineFunctionInfo.h - AArch64 machine function info -*- C++ -*-=//
Tim Northover3b0846e2014-05-24 12:50:23 +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
Tim Northover3b0846e2014-05-24 12:50:23 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file declares AArch64-specific per-machine-function information.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000013#ifndef LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H
14#define LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H
Tim Northover3b0846e2014-05-24 12:50:23 +000015
Eugene Zelenko049b0172017-01-06 00:30:53 +000016#include "llvm/ADT/ArrayRef.h"
Jessica Paquette642f6c62018-04-03 21:56:10 +000017#include "llvm/ADT/Optional.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000018#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallVector.h"
Mandeep Singh Grang71e0cc22018-10-30 20:46:10 +000020#include "llvm/CodeGen/CallingConvLower.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000021#include "llvm/CodeGen/MachineFunction.h"
Sander de Smalen33676862019-11-13 09:57:51 +000022#include "llvm/CodeGen/TargetFrameLowering.h"
Reid Kleckner1d7b4132019-10-19 00:22:07 +000023#include "llvm/IR/Function.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000024#include "llvm/MC/MCLinkerOptimizationHint.h"
Eugene Zelenko049b0172017-01-06 00:30:53 +000025#include <cassert>
Tim Northover3b0846e2014-05-24 12:50:23 +000026
27namespace llvm {
28
Eugene Zelenko96d933d2017-07-25 23:51:02 +000029class MachineInstr;
30
Tim Northover3b0846e2014-05-24 12:50:23 +000031/// AArch64FunctionInfo - This class is derived from MachineFunctionInfo and
32/// contains private AArch64-specific information for each MachineFunction.
Ahmed Bougacha5e402ee2016-07-27 14:31:46 +000033class AArch64FunctionInfo final : public MachineFunctionInfo {
Tim Northover3b0846e2014-05-24 12:50:23 +000034 /// Number of bytes of arguments this function has on the stack. If the callee
35 /// is expected to restore the argument stack this should be a multiple of 16,
36 /// all usable during a tail call.
37 ///
38 /// The alternative would forbid tail call optimisation in some cases: if we
39 /// want to transfer control from a function with 8-bytes of stack-argument
40 /// space to a function with 16-bytes then misalignment of this value would
41 /// make a stack adjustment necessary, which could not be undone by the
42 /// callee.
Eugene Zelenko049b0172017-01-06 00:30:53 +000043 unsigned BytesInStackArgArea = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +000044
45 /// The number of bytes to restore to deallocate space for incoming
46 /// arguments. Canonically 0 in the C calling convention, but non-zero when
47 /// callee is expected to pop the args.
Eugene Zelenko049b0172017-01-06 00:30:53 +000048 unsigned ArgumentStackToRestore = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +000049
50 /// HasStackFrame - True if this function has a stack frame. Set by
JF Bastienc8f48c12015-07-14 23:06:07 +000051 /// determineCalleeSaves().
Eugene Zelenko049b0172017-01-06 00:30:53 +000052 bool HasStackFrame = false;
Tim Northover3b0846e2014-05-24 12:50:23 +000053
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000054 /// Amount of stack frame size, not including callee-saved registers.
Simon Pilgrim0cc7c292019-11-11 11:24:09 +000055 unsigned LocalStackSize = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +000056
Sander de Smalen84a0c8e2019-11-05 16:54:54 +000057 /// The start and end frame indices for the SVE callee saves.
Simon Pilgrim0cc7c292019-11-11 11:24:09 +000058 int MinSVECSFrameIndex = 0;
59 int MaxSVECSFrameIndex = 0;
Sander de Smalen84a0c8e2019-11-05 16:54:54 +000060
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000061 /// Amount of stack frame size used for saving callee-saved registers.
Simon Pilgrim0cc7c292019-11-11 11:24:09 +000062 unsigned CalleeSavedStackSize = 0;
63 unsigned SVECalleeSavedStackSize = 0;
Sander de Smalend6a7da82019-10-29 12:49:34 +000064 bool HasCalleeSavedStackSize = false;
Geoff Berry04bf91a2016-02-01 16:29:19 +000065
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000066 /// Number of TLS accesses using the special (combinable)
Tim Northover3b0846e2014-05-24 12:50:23 +000067 /// _TLS_MODULE_BASE_ symbol.
Eugene Zelenko049b0172017-01-06 00:30:53 +000068 unsigned NumLocalDynamicTLSAccesses = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +000069
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000070 /// FrameIndex for start of varargs area for arguments passed on the
Tim Northover3b0846e2014-05-24 12:50:23 +000071 /// stack.
Eugene Zelenko049b0172017-01-06 00:30:53 +000072 int VarArgsStackIndex = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +000073
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000074 /// FrameIndex for start of varargs area for arguments passed in
Tim Northover3b0846e2014-05-24 12:50:23 +000075 /// general purpose registers.
Eugene Zelenko049b0172017-01-06 00:30:53 +000076 int VarArgsGPRIndex = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +000077
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000078 /// Size of the varargs area for arguments passed in general purpose
Tim Northover3b0846e2014-05-24 12:50:23 +000079 /// registers.
Eugene Zelenko049b0172017-01-06 00:30:53 +000080 unsigned VarArgsGPRSize = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +000081
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000082 /// FrameIndex for start of varargs area for arguments passed in
Tim Northover3b0846e2014-05-24 12:50:23 +000083 /// floating-point registers.
Eugene Zelenko049b0172017-01-06 00:30:53 +000084 int VarArgsFPRIndex = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +000085
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000086 /// Size of the varargs area for arguments passed in floating-point
Tim Northover3b0846e2014-05-24 12:50:23 +000087 /// registers.
Eugene Zelenko049b0172017-01-06 00:30:53 +000088 unsigned VarArgsFPRSize = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +000089
Manman Rencbe4f942015-12-16 21:04:19 +000090 /// True if this function has a subset of CSRs that is handled explicitly via
91 /// copies.
Eugene Zelenko049b0172017-01-06 00:30:53 +000092 bool IsSplitCSR = false;
Manman Rencbe4f942015-12-16 21:04:19 +000093
Chad Rosier6d986552016-03-14 18:17:41 +000094 /// True when the stack gets realigned dynamically because the size of stack
95 /// frame is unknown at compile time. e.g., in case of VLAs.
Eugene Zelenko049b0172017-01-06 00:30:53 +000096 bool StackRealigned = false;
Chad Rosier6d986552016-03-14 18:17:41 +000097
Geoff Berry66f6b652016-06-02 16:22:07 +000098 /// True when the callee-save stack area has unused gaps that may be used for
99 /// other stack allocations.
Eugene Zelenko049b0172017-01-06 00:30:53 +0000100 bool CalleeSaveStackHasFreeSpace = false;
Geoff Berry66f6b652016-06-02 16:22:07 +0000101
Mandeep Singh Grang5dc8aeb2019-05-03 21:12:36 +0000102 /// SRetReturnReg - sret lowering includes returning the value of the
103 /// returned struct in a register. This field holds the virtual register into
104 /// which the sret argument is passed.
105 unsigned SRetReturnReg = 0;
Sander de Smalen4f99b6f2019-10-03 11:33:50 +0000106 /// SVE stack size (for predicates and data vectors) are maintained here
107 /// rather than in FrameInfo, as the placement and Stack IDs are target
108 /// specific.
109 uint64_t StackSizeSVE = 0;
110
111 /// HasCalculatedStackSizeSVE indicates whether StackSizeSVE is valid.
112 bool HasCalculatedStackSizeSVE = false;
Mandeep Singh Grang5dc8aeb2019-05-03 21:12:36 +0000113
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000114 /// Has a value when it is known whether or not the function uses a
Jessica Paquette642f6c62018-04-03 21:56:10 +0000115 /// redzone, and no value otherwise.
116 /// Initialized during frame lowering, unless the function has the noredzone
117 /// attribute, in which case it is set to false at construction.
118 Optional<bool> HasRedZone;
119
Mandeep Singh Grang71e0cc22018-10-30 20:46:10 +0000120 /// ForwardedMustTailRegParms - A list of virtual and physical registers
121 /// that must be forwarded to every musttail call.
122 SmallVector<ForwardedRegister, 1> ForwardedMustTailRegParms;
Evgeniy Stepanovd752f5e2019-07-17 19:24:02 +0000123
124 // Offset from SP-at-entry to the tagged base pointer.
125 // Tagged base pointer is set up to point to the first (lowest address) tagged
126 // stack slot.
Simon Pilgrim0cc7c292019-11-11 11:24:09 +0000127 unsigned TaggedBasePointerOffset = 0;
Evgeniy Stepanovd752f5e2019-07-17 19:24:02 +0000128
Tim Northover3b0846e2014-05-24 12:50:23 +0000129public:
Eugene Zelenko049b0172017-01-06 00:30:53 +0000130 AArch64FunctionInfo() = default;
Tim Northover3b0846e2014-05-24 12:50:23 +0000131
Eugene Zelenko049b0172017-01-06 00:30:53 +0000132 explicit AArch64FunctionInfo(MachineFunction &MF) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000133 (void)MF;
Jessica Paquette642f6c62018-04-03 21:56:10 +0000134
135 // If we already know that the function doesn't have a redzone, set
136 // HasRedZone here.
137 if (MF.getFunction().hasFnAttribute(Attribute::NoRedZone))
138 HasRedZone = false;
Tim Northover3b0846e2014-05-24 12:50:23 +0000139 }
140
141 unsigned getBytesInStackArgArea() const { return BytesInStackArgArea; }
142 void setBytesInStackArgArea(unsigned bytes) { BytesInStackArgArea = bytes; }
143
144 unsigned getArgumentStackToRestore() const { return ArgumentStackToRestore; }
145 void setArgumentStackToRestore(unsigned bytes) {
146 ArgumentStackToRestore = bytes;
147 }
148
Sander de Smalen4f99b6f2019-10-03 11:33:50 +0000149 bool hasCalculatedStackSizeSVE() const { return HasCalculatedStackSizeSVE; }
150
151 void setStackSizeSVE(uint64_t S) {
152 HasCalculatedStackSizeSVE = true;
153 StackSizeSVE = S;
154 }
155
156 uint64_t getStackSizeSVE() const { return StackSizeSVE; }
157
Tim Northover3b0846e2014-05-24 12:50:23 +0000158 bool hasStackFrame() const { return HasStackFrame; }
159 void setHasStackFrame(bool s) { HasStackFrame = s; }
160
Chad Rosier6d986552016-03-14 18:17:41 +0000161 bool isStackRealigned() const { return StackRealigned; }
162 void setStackRealigned(bool s) { StackRealigned = s; }
163
Geoff Berry66f6b652016-06-02 16:22:07 +0000164 bool hasCalleeSaveStackFreeSpace() const {
165 return CalleeSaveStackHasFreeSpace;
166 }
167 void setCalleeSaveStackHasFreeSpace(bool s) {
168 CalleeSaveStackHasFreeSpace = s;
169 }
Manman Rencbe4f942015-12-16 21:04:19 +0000170 bool isSplitCSR() const { return IsSplitCSR; }
171 void setIsSplitCSR(bool s) { IsSplitCSR = s; }
172
Tim Northover3b0846e2014-05-24 12:50:23 +0000173 void setLocalStackSize(unsigned Size) { LocalStackSize = Size; }
174 unsigned getLocalStackSize() const { return LocalStackSize; }
175
Sander de Smalend6a7da82019-10-29 12:49:34 +0000176 void setCalleeSavedStackSize(unsigned Size) {
177 CalleeSavedStackSize = Size;
178 HasCalleeSavedStackSize = true;
179 }
180
181 // When CalleeSavedStackSize has not been set (for example when
182 // some MachineIR pass is run in isolation), then recalculate
183 // the CalleeSavedStackSize directly from the CalleeSavedInfo.
184 // Note: This information can only be recalculated after PEI
185 // has assigned offsets to the callee save objects.
186 unsigned getCalleeSavedStackSize(const MachineFrameInfo &MFI) const {
187 bool ValidateCalleeSavedStackSize = false;
188
189#ifndef NDEBUG
190 // Make sure the calculated size derived from the CalleeSavedInfo
191 // equals the cached size that was calculated elsewhere (e.g. in
192 // determineCalleeSaves).
193 ValidateCalleeSavedStackSize = HasCalleeSavedStackSize;
194#endif
195
196 if (!HasCalleeSavedStackSize || ValidateCalleeSavedStackSize) {
197 assert(MFI.isCalleeSavedInfoValid() && "CalleeSavedInfo not calculated");
198 if (MFI.getCalleeSavedInfo().empty())
199 return 0;
200
201 int64_t MinOffset = std::numeric_limits<int64_t>::max();
202 int64_t MaxOffset = std::numeric_limits<int64_t>::min();
203 for (const auto &Info : MFI.getCalleeSavedInfo()) {
204 int FrameIdx = Info.getFrameIdx();
Sander de Smalen33676862019-11-13 09:57:51 +0000205 if (MFI.getStackID(FrameIdx) != TargetStackID::Default)
206 continue;
Sander de Smalend6a7da82019-10-29 12:49:34 +0000207 int64_t Offset = MFI.getObjectOffset(FrameIdx);
208 int64_t ObjSize = MFI.getObjectSize(FrameIdx);
209 MinOffset = std::min<int64_t>(Offset, MinOffset);
210 MaxOffset = std::max<int64_t>(Offset + ObjSize, MaxOffset);
211 }
212
213 unsigned Size = alignTo(MaxOffset - MinOffset, 16);
214 assert((!HasCalleeSavedStackSize || getCalleeSavedStackSize() == Size) &&
215 "Invalid size calculated for callee saves");
216 return Size;
217 }
218
219 return getCalleeSavedStackSize();
220 }
221
222 unsigned getCalleeSavedStackSize() const {
223 assert(HasCalleeSavedStackSize &&
224 "CalleeSavedStackSize has not been calculated");
225 return CalleeSavedStackSize;
226 }
Geoff Berry04bf91a2016-02-01 16:29:19 +0000227
Sander de Smalen84a0c8e2019-11-05 16:54:54 +0000228 // Saves the CalleeSavedStackSize for SVE vectors in 'scalable bytes'
229 void setSVECalleeSavedStackSize(unsigned Size) {
230 SVECalleeSavedStackSize = Size;
231 }
232 unsigned getSVECalleeSavedStackSize() const {
233 return SVECalleeSavedStackSize;
234 }
235
236 void setMinMaxSVECSFrameIndex(int Min, int Max) {
237 MinSVECSFrameIndex = Min;
238 MaxSVECSFrameIndex = Max;
239 }
240
241 int getMinSVECSFrameIndex() const { return MinSVECSFrameIndex; }
242 int getMaxSVECSFrameIndex() const { return MaxSVECSFrameIndex; }
243
Tim Northover3b0846e2014-05-24 12:50:23 +0000244 void incNumLocalDynamicTLSAccesses() { ++NumLocalDynamicTLSAccesses; }
245 unsigned getNumLocalDynamicTLSAccesses() const {
246 return NumLocalDynamicTLSAccesses;
247 }
248
Jessica Paquette642f6c62018-04-03 21:56:10 +0000249 Optional<bool> hasRedZone() const { return HasRedZone; }
250 void setHasRedZone(bool s) { HasRedZone = s; }
Fangrui Songf78650a2018-07-30 19:41:25 +0000251
Tim Northover3b0846e2014-05-24 12:50:23 +0000252 int getVarArgsStackIndex() const { return VarArgsStackIndex; }
253 void setVarArgsStackIndex(int Index) { VarArgsStackIndex = Index; }
254
255 int getVarArgsGPRIndex() const { return VarArgsGPRIndex; }
256 void setVarArgsGPRIndex(int Index) { VarArgsGPRIndex = Index; }
257
258 unsigned getVarArgsGPRSize() const { return VarArgsGPRSize; }
259 void setVarArgsGPRSize(unsigned Size) { VarArgsGPRSize = Size; }
260
261 int getVarArgsFPRIndex() const { return VarArgsFPRIndex; }
262 void setVarArgsFPRIndex(int Index) { VarArgsFPRIndex = Index; }
263
264 unsigned getVarArgsFPRSize() const { return VarArgsFPRSize; }
265 void setVarArgsFPRSize(unsigned Size) { VarArgsFPRSize = Size; }
266
Mandeep Singh Grang5dc8aeb2019-05-03 21:12:36 +0000267 unsigned getSRetReturnReg() const { return SRetReturnReg; }
268 void setSRetReturnReg(unsigned Reg) { SRetReturnReg = Reg; }
269
Tim Northover1c353412018-10-24 20:19:09 +0000270 unsigned getJumpTableEntrySize(int Idx) const {
271 auto It = JumpTableEntryInfo.find(Idx);
272 if (It != JumpTableEntryInfo.end())
273 return It->second.first;
274 return 4;
275 }
276 MCSymbol *getJumpTableEntryPCRelSymbol(int Idx) const {
277 return JumpTableEntryInfo.find(Idx)->second.second;
278 }
279 void setJumpTableEntryInfo(int Idx, unsigned Size, MCSymbol *PCRelSym) {
280 JumpTableEntryInfo[Idx] = std::make_pair(Size, PCRelSym);
281 }
282
Eugene Zelenko96d933d2017-07-25 23:51:02 +0000283 using SetOfInstructions = SmallPtrSet<const MachineInstr *, 16>;
Tim Northover3b0846e2014-05-24 12:50:23 +0000284
285 const SetOfInstructions &getLOHRelated() const { return LOHRelated; }
286
287 // Shortcuts for LOH related types.
288 class MILOHDirective {
289 MCLOHType Kind;
290
291 /// Arguments of this directive. Order matters.
292 SmallVector<const MachineInstr *, 3> Args;
293
294 public:
Eugene Zelenko96d933d2017-07-25 23:51:02 +0000295 using LOHArgs = ArrayRef<const MachineInstr *>;
Tim Northover3b0846e2014-05-24 12:50:23 +0000296
Benjamin Kramer3bc1edf2016-07-02 11:41:39 +0000297 MILOHDirective(MCLOHType Kind, LOHArgs Args)
Tim Northover3b0846e2014-05-24 12:50:23 +0000298 : Kind(Kind), Args(Args.begin(), Args.end()) {
299 assert(isValidMCLOHType(Kind) && "Invalid LOH directive type!");
300 }
301
302 MCLOHType getKind() const { return Kind; }
Benjamin Kramer3bc1edf2016-07-02 11:41:39 +0000303 LOHArgs getArgs() const { return Args; }
Tim Northover3b0846e2014-05-24 12:50:23 +0000304 };
305
Eugene Zelenko96d933d2017-07-25 23:51:02 +0000306 using MILOHArgs = MILOHDirective::LOHArgs;
307 using MILOHContainer = SmallVector<MILOHDirective, 32>;
Tim Northover3b0846e2014-05-24 12:50:23 +0000308
309 const MILOHContainer &getLOHContainer() const { return LOHContainerSet; }
310
311 /// Add a LOH directive of this @p Kind and this @p Args.
Benjamin Kramer3bc1edf2016-07-02 11:41:39 +0000312 void addLOHDirective(MCLOHType Kind, MILOHArgs Args) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000313 LOHContainerSet.push_back(MILOHDirective(Kind, Args));
314 LOHRelated.insert(Args.begin(), Args.end());
315 }
316
Mandeep Singh Grang71e0cc22018-10-30 20:46:10 +0000317 SmallVectorImpl<ForwardedRegister> &getForwardedMustTailRegParms() {
318 return ForwardedMustTailRegParms;
319 }
320
Evgeniy Stepanovd752f5e2019-07-17 19:24:02 +0000321 unsigned getTaggedBasePointerOffset() const {
322 return TaggedBasePointerOffset;
323 }
324 void setTaggedBasePointerOffset(unsigned Offset) {
325 TaggedBasePointerOffset = Offset;
326 }
327
Tim Northover3b0846e2014-05-24 12:50:23 +0000328private:
329 // Hold the lists of LOHs.
330 MILOHContainer LOHContainerSet;
331 SetOfInstructions LOHRelated;
Tim Northover1c353412018-10-24 20:19:09 +0000332
333 DenseMap<int, std::pair<unsigned, MCSymbol *>> JumpTableEntryInfo;
Tim Northover3b0846e2014-05-24 12:50:23 +0000334};
Tim Northover3b0846e2014-05-24 12:50:23 +0000335
Eugene Zelenko049b0172017-01-06 00:30:53 +0000336} // end namespace llvm
337
338#endif // LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H