1 //=- AArch64MachineFunctionInfo.h - AArch64 machine function info -*- C++ -*-=//
3 // 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
7 //===----------------------------------------------------------------------===//
9 // This file declares AArch64-specific per-machine-function information.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H
14 #define LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/CodeGen/CallingConvLower.h"
21 #include "llvm/CodeGen/MIRYamlMapping.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/TargetFrameLowering.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/MC/MCLinkerOptimizationHint.h"
31 struct AArch64FunctionInfo;
32 } // end namespace yaml
36 /// AArch64FunctionInfo - This class is derived from MachineFunctionInfo and
37 /// contains private AArch64-specific information for each MachineFunction.
38 class AArch64FunctionInfo final : public MachineFunctionInfo {
39 /// Number of bytes of arguments this function has on the stack. If the callee
40 /// is expected to restore the argument stack this should be a multiple of 16,
41 /// all usable during a tail call.
43 /// The alternative would forbid tail call optimisation in some cases: if we
44 /// want to transfer control from a function with 8-bytes of stack-argument
45 /// space to a function with 16-bytes then misalignment of this value would
46 /// make a stack adjustment necessary, which could not be undone by the
48 unsigned BytesInStackArgArea = 0;
50 /// The number of bytes to restore to deallocate space for incoming
51 /// arguments. Canonically 0 in the C calling convention, but non-zero when
52 /// callee is expected to pop the args.
53 unsigned ArgumentStackToRestore = 0;
55 /// HasStackFrame - True if this function has a stack frame. Set by
56 /// determineCalleeSaves().
57 bool HasStackFrame = false;
59 /// Amount of stack frame size, not including callee-saved registers.
60 uint64_t LocalStackSize = 0;
62 /// The start and end frame indices for the SVE callee saves.
63 int MinSVECSFrameIndex = 0;
64 int MaxSVECSFrameIndex = 0;
66 /// Amount of stack frame size used for saving callee-saved registers.
67 unsigned CalleeSavedStackSize = 0;
68 unsigned SVECalleeSavedStackSize = 0;
69 bool HasCalleeSavedStackSize = false;
71 /// Number of TLS accesses using the special (combinable)
72 /// _TLS_MODULE_BASE_ symbol.
73 unsigned NumLocalDynamicTLSAccesses = 0;
75 /// FrameIndex for start of varargs area for arguments passed on the
77 int VarArgsStackIndex = 0;
79 /// FrameIndex for start of varargs area for arguments passed in
80 /// general purpose registers.
81 int VarArgsGPRIndex = 0;
83 /// Size of the varargs area for arguments passed in general purpose
85 unsigned VarArgsGPRSize = 0;
87 /// FrameIndex for start of varargs area for arguments passed in
88 /// floating-point registers.
89 int VarArgsFPRIndex = 0;
91 /// Size of the varargs area for arguments passed in floating-point
93 unsigned VarArgsFPRSize = 0;
95 /// True if this function has a subset of CSRs that is handled explicitly via
97 bool IsSplitCSR = false;
99 /// True when the stack gets realigned dynamically because the size of stack
100 /// frame is unknown at compile time. e.g., in case of VLAs.
101 bool StackRealigned = false;
103 /// True when the callee-save stack area has unused gaps that may be used for
104 /// other stack allocations.
105 bool CalleeSaveStackHasFreeSpace = false;
107 /// SRetReturnReg - sret lowering includes returning the value of the
108 /// returned struct in a register. This field holds the virtual register into
109 /// which the sret argument is passed.
110 unsigned SRetReturnReg = 0;
111 /// SVE stack size (for predicates and data vectors) are maintained here
112 /// rather than in FrameInfo, as the placement and Stack IDs are target
114 uint64_t StackSizeSVE = 0;
116 /// HasCalculatedStackSizeSVE indicates whether StackSizeSVE is valid.
117 bool HasCalculatedStackSizeSVE = false;
119 /// Has a value when it is known whether or not the function uses a
120 /// redzone, and no value otherwise.
121 /// Initialized during frame lowering, unless the function has the noredzone
122 /// attribute, in which case it is set to false at construction.
123 Optional<bool> HasRedZone;
125 /// ForwardedMustTailRegParms - A list of virtual and physical registers
126 /// that must be forwarded to every musttail call.
127 SmallVector<ForwardedRegister, 1> ForwardedMustTailRegParms;
129 // Offset from SP-at-entry to the tagged base pointer.
130 // Tagged base pointer is set up to point to the first (lowest address) tagged
132 unsigned TaggedBasePointerOffset = 0;
134 /// OutliningStyle denotes, if a function was outined, how it was outlined,
135 /// e.g. Tail Call, Thunk, or Function if none apply.
136 Optional<std::string> OutliningStyle;
139 AArch64FunctionInfo() = default;
141 explicit AArch64FunctionInfo(MachineFunction &MF) {
144 // If we already know that the function doesn't have a redzone, set
146 if (MF.getFunction().hasFnAttribute(Attribute::NoRedZone))
149 void initializeBaseYamlFields(const yaml::AArch64FunctionInfo &YamlMFI);
151 unsigned getBytesInStackArgArea() const { return BytesInStackArgArea; }
152 void setBytesInStackArgArea(unsigned bytes) { BytesInStackArgArea = bytes; }
154 unsigned getArgumentStackToRestore() const { return ArgumentStackToRestore; }
155 void setArgumentStackToRestore(unsigned bytes) {
156 ArgumentStackToRestore = bytes;
159 bool hasCalculatedStackSizeSVE() const { return HasCalculatedStackSizeSVE; }
161 void setStackSizeSVE(uint64_t S) {
162 HasCalculatedStackSizeSVE = true;
166 uint64_t getStackSizeSVE() const { return StackSizeSVE; }
168 bool hasStackFrame() const { return HasStackFrame; }
169 void setHasStackFrame(bool s) { HasStackFrame = s; }
171 bool isStackRealigned() const { return StackRealigned; }
172 void setStackRealigned(bool s) { StackRealigned = s; }
174 bool hasCalleeSaveStackFreeSpace() const {
175 return CalleeSaveStackHasFreeSpace;
177 void setCalleeSaveStackHasFreeSpace(bool s) {
178 CalleeSaveStackHasFreeSpace = s;
180 bool isSplitCSR() const { return IsSplitCSR; }
181 void setIsSplitCSR(bool s) { IsSplitCSR = s; }
183 void setLocalStackSize(uint64_t Size) { LocalStackSize = Size; }
184 uint64_t getLocalStackSize() const { return LocalStackSize; }
186 void setOutliningStyle(std::string Style) { OutliningStyle = Style; }
187 Optional<std::string> getOutliningStyle() const { return OutliningStyle; }
189 void setCalleeSavedStackSize(unsigned Size) {
190 CalleeSavedStackSize = Size;
191 HasCalleeSavedStackSize = true;
194 // When CalleeSavedStackSize has not been set (for example when
195 // some MachineIR pass is run in isolation), then recalculate
196 // the CalleeSavedStackSize directly from the CalleeSavedInfo.
197 // Note: This information can only be recalculated after PEI
198 // has assigned offsets to the callee save objects.
199 unsigned getCalleeSavedStackSize(const MachineFrameInfo &MFI) const {
200 bool ValidateCalleeSavedStackSize = false;
203 // Make sure the calculated size derived from the CalleeSavedInfo
204 // equals the cached size that was calculated elsewhere (e.g. in
205 // determineCalleeSaves).
206 ValidateCalleeSavedStackSize = HasCalleeSavedStackSize;
209 if (!HasCalleeSavedStackSize || ValidateCalleeSavedStackSize) {
210 assert(MFI.isCalleeSavedInfoValid() && "CalleeSavedInfo not calculated");
211 if (MFI.getCalleeSavedInfo().empty())
214 int64_t MinOffset = std::numeric_limits<int64_t>::max();
215 int64_t MaxOffset = std::numeric_limits<int64_t>::min();
216 for (const auto &Info : MFI.getCalleeSavedInfo()) {
217 int FrameIdx = Info.getFrameIdx();
218 if (MFI.getStackID(FrameIdx) != TargetStackID::Default)
220 int64_t Offset = MFI.getObjectOffset(FrameIdx);
221 int64_t ObjSize = MFI.getObjectSize(FrameIdx);
222 MinOffset = std::min<int64_t>(Offset, MinOffset);
223 MaxOffset = std::max<int64_t>(Offset + ObjSize, MaxOffset);
226 unsigned Size = alignTo(MaxOffset - MinOffset, 16);
227 assert((!HasCalleeSavedStackSize || getCalleeSavedStackSize() == Size) &&
228 "Invalid size calculated for callee saves");
232 return getCalleeSavedStackSize();
235 unsigned getCalleeSavedStackSize() const {
236 assert(HasCalleeSavedStackSize &&
237 "CalleeSavedStackSize has not been calculated");
238 return CalleeSavedStackSize;
241 // Saves the CalleeSavedStackSize for SVE vectors in 'scalable bytes'
242 void setSVECalleeSavedStackSize(unsigned Size) {
243 SVECalleeSavedStackSize = Size;
245 unsigned getSVECalleeSavedStackSize() const {
246 return SVECalleeSavedStackSize;
249 void setMinMaxSVECSFrameIndex(int Min, int Max) {
250 MinSVECSFrameIndex = Min;
251 MaxSVECSFrameIndex = Max;
254 int getMinSVECSFrameIndex() const { return MinSVECSFrameIndex; }
255 int getMaxSVECSFrameIndex() const { return MaxSVECSFrameIndex; }
257 void incNumLocalDynamicTLSAccesses() { ++NumLocalDynamicTLSAccesses; }
258 unsigned getNumLocalDynamicTLSAccesses() const {
259 return NumLocalDynamicTLSAccesses;
262 Optional<bool> hasRedZone() const { return HasRedZone; }
263 void setHasRedZone(bool s) { HasRedZone = s; }
265 int getVarArgsStackIndex() const { return VarArgsStackIndex; }
266 void setVarArgsStackIndex(int Index) { VarArgsStackIndex = Index; }
268 int getVarArgsGPRIndex() const { return VarArgsGPRIndex; }
269 void setVarArgsGPRIndex(int Index) { VarArgsGPRIndex = Index; }
271 unsigned getVarArgsGPRSize() const { return VarArgsGPRSize; }
272 void setVarArgsGPRSize(unsigned Size) { VarArgsGPRSize = Size; }
274 int getVarArgsFPRIndex() const { return VarArgsFPRIndex; }
275 void setVarArgsFPRIndex(int Index) { VarArgsFPRIndex = Index; }
277 unsigned getVarArgsFPRSize() const { return VarArgsFPRSize; }
278 void setVarArgsFPRSize(unsigned Size) { VarArgsFPRSize = Size; }
280 unsigned getSRetReturnReg() const { return SRetReturnReg; }
281 void setSRetReturnReg(unsigned Reg) { SRetReturnReg = Reg; }
283 unsigned getJumpTableEntrySize(int Idx) const {
284 auto It = JumpTableEntryInfo.find(Idx);
285 if (It != JumpTableEntryInfo.end())
286 return It->second.first;
289 MCSymbol *getJumpTableEntryPCRelSymbol(int Idx) const {
290 return JumpTableEntryInfo.find(Idx)->second.second;
292 void setJumpTableEntryInfo(int Idx, unsigned Size, MCSymbol *PCRelSym) {
293 JumpTableEntryInfo[Idx] = std::make_pair(Size, PCRelSym);
296 using SetOfInstructions = SmallPtrSet<const MachineInstr *, 16>;
298 const SetOfInstructions &getLOHRelated() const { return LOHRelated; }
300 // Shortcuts for LOH related types.
301 class MILOHDirective {
304 /// Arguments of this directive. Order matters.
305 SmallVector<const MachineInstr *, 3> Args;
308 using LOHArgs = ArrayRef<const MachineInstr *>;
310 MILOHDirective(MCLOHType Kind, LOHArgs Args)
311 : Kind(Kind), Args(Args.begin(), Args.end()) {
312 assert(isValidMCLOHType(Kind) && "Invalid LOH directive type!");
315 MCLOHType getKind() const { return Kind; }
316 LOHArgs getArgs() const { return Args; }
319 using MILOHArgs = MILOHDirective::LOHArgs;
320 using MILOHContainer = SmallVector<MILOHDirective, 32>;
322 const MILOHContainer &getLOHContainer() const { return LOHContainerSet; }
324 /// Add a LOH directive of this @p Kind and this @p Args.
325 void addLOHDirective(MCLOHType Kind, MILOHArgs Args) {
326 LOHContainerSet.push_back(MILOHDirective(Kind, Args));
327 LOHRelated.insert(Args.begin(), Args.end());
330 SmallVectorImpl<ForwardedRegister> &getForwardedMustTailRegParms() {
331 return ForwardedMustTailRegParms;
334 unsigned getTaggedBasePointerOffset() const {
335 return TaggedBasePointerOffset;
337 void setTaggedBasePointerOffset(unsigned Offset) {
338 TaggedBasePointerOffset = Offset;
342 // Hold the lists of LOHs.
343 MILOHContainer LOHContainerSet;
344 SetOfInstructions LOHRelated;
346 DenseMap<int, std::pair<unsigned, MCSymbol *>> JumpTableEntryInfo;
350 struct AArch64FunctionInfo final : public yaml::MachineFunctionInfo {
351 Optional<bool> HasRedZone;
353 AArch64FunctionInfo() = default;
354 AArch64FunctionInfo(const llvm::AArch64FunctionInfo &MFI);
356 void mappingImpl(yaml::IO &YamlIO) override;
357 ~AArch64FunctionInfo() = default;
360 template <> struct MappingTraits<AArch64FunctionInfo> {
361 static void mapping(IO &YamlIO, AArch64FunctionInfo &MFI) {
362 YamlIO.mapOptional("hasRedZone", MFI.HasRedZone);
366 } // end namespace yaml
368 } // end namespace llvm
370 #endif // LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H