1 //=- AArch64MachineFunctionInfo.h - AArch64 machine function info -*- C++ -*-=//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares AArch64-specific per-machine-function information.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H
14 #define LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H
15 
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"
26 #include <cassert>
27 
28 namespace llvm {
29 
30 namespace yaml {
31 struct AArch64FunctionInfo;
32 } // end namespace yaml
33 
34 class MachineInstr;
35 
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.
42   ///
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
47   /// callee.
48   unsigned BytesInStackArgArea = 0;
49 
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;
54 
55   /// HasStackFrame - True if this function has a stack frame. Set by
56   /// determineCalleeSaves().
57   bool HasStackFrame = false;
58 
59   /// Amount of stack frame size, not including callee-saved registers.
60   uint64_t LocalStackSize = 0;
61 
62   /// The start and end frame indices for the SVE callee saves.
63   int MinSVECSFrameIndex = 0;
64   int MaxSVECSFrameIndex = 0;
65 
66   /// Amount of stack frame size used for saving callee-saved registers.
67   unsigned CalleeSavedStackSize = 0;
68   unsigned SVECalleeSavedStackSize = 0;
69   bool HasCalleeSavedStackSize = false;
70 
71   /// Number of TLS accesses using the special (combinable)
72   /// _TLS_MODULE_BASE_ symbol.
73   unsigned NumLocalDynamicTLSAccesses = 0;
74 
75   /// FrameIndex for start of varargs area for arguments passed on the
76   /// stack.
77   int VarArgsStackIndex = 0;
78 
79   /// FrameIndex for start of varargs area for arguments passed in
80   /// general purpose registers.
81   int VarArgsGPRIndex = 0;
82 
83   /// Size of the varargs area for arguments passed in general purpose
84   /// registers.
85   unsigned VarArgsGPRSize = 0;
86 
87   /// FrameIndex for start of varargs area for arguments passed in
88   /// floating-point registers.
89   int VarArgsFPRIndex = 0;
90 
91   /// Size of the varargs area for arguments passed in floating-point
92   /// registers.
93   unsigned VarArgsFPRSize = 0;
94 
95   /// True if this function has a subset of CSRs that is handled explicitly via
96   /// copies.
97   bool IsSplitCSR = false;
98 
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;
102 
103   /// True when the callee-save stack area has unused gaps that may be used for
104   /// other stack allocations.
105   bool CalleeSaveStackHasFreeSpace = false;
106 
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
113   /// specific.
114   uint64_t StackSizeSVE = 0;
115 
116   /// HasCalculatedStackSizeSVE indicates whether StackSizeSVE is valid.
117   bool HasCalculatedStackSizeSVE = false;
118 
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;
124 
125   /// ForwardedMustTailRegParms - A list of virtual and physical registers
126   /// that must be forwarded to every musttail call.
127   SmallVector<ForwardedRegister, 1> ForwardedMustTailRegParms;
128 
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
131   // stack slot.
132   unsigned TaggedBasePointerOffset = 0;
133 
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;
137 
138 public:
139   AArch64FunctionInfo() = default;
140 
AArch64FunctionInfo(MachineFunction & MF)141   explicit AArch64FunctionInfo(MachineFunction &MF) {
142     (void)MF;
143 
144     // If we already know that the function doesn't have a redzone, set
145     // HasRedZone here.
146     if (MF.getFunction().hasFnAttribute(Attribute::NoRedZone))
147       HasRedZone = false;
148   }
149   void initializeBaseYamlFields(const yaml::AArch64FunctionInfo &YamlMFI);
150 
getBytesInStackArgArea()151   unsigned getBytesInStackArgArea() const { return BytesInStackArgArea; }
setBytesInStackArgArea(unsigned bytes)152   void setBytesInStackArgArea(unsigned bytes) { BytesInStackArgArea = bytes; }
153 
getArgumentStackToRestore()154   unsigned getArgumentStackToRestore() const { return ArgumentStackToRestore; }
setArgumentStackToRestore(unsigned bytes)155   void setArgumentStackToRestore(unsigned bytes) {
156     ArgumentStackToRestore = bytes;
157   }
158 
hasCalculatedStackSizeSVE()159   bool hasCalculatedStackSizeSVE() const { return HasCalculatedStackSizeSVE; }
160 
setStackSizeSVE(uint64_t S)161   void setStackSizeSVE(uint64_t S) {
162     HasCalculatedStackSizeSVE = true;
163     StackSizeSVE = S;
164   }
165 
getStackSizeSVE()166   uint64_t getStackSizeSVE() const { return StackSizeSVE; }
167 
hasStackFrame()168   bool hasStackFrame() const { return HasStackFrame; }
setHasStackFrame(bool s)169   void setHasStackFrame(bool s) { HasStackFrame = s; }
170 
isStackRealigned()171   bool isStackRealigned() const { return StackRealigned; }
setStackRealigned(bool s)172   void setStackRealigned(bool s) { StackRealigned = s; }
173 
hasCalleeSaveStackFreeSpace()174   bool hasCalleeSaveStackFreeSpace() const {
175     return CalleeSaveStackHasFreeSpace;
176   }
setCalleeSaveStackHasFreeSpace(bool s)177   void setCalleeSaveStackHasFreeSpace(bool s) {
178     CalleeSaveStackHasFreeSpace = s;
179   }
isSplitCSR()180   bool isSplitCSR() const { return IsSplitCSR; }
setIsSplitCSR(bool s)181   void setIsSplitCSR(bool s) { IsSplitCSR = s; }
182 
setLocalStackSize(uint64_t Size)183   void setLocalStackSize(uint64_t Size) { LocalStackSize = Size; }
getLocalStackSize()184   uint64_t getLocalStackSize() const { return LocalStackSize; }
185 
setOutliningStyle(std::string Style)186   void setOutliningStyle(std::string Style) { OutliningStyle = Style; }
getOutliningStyle()187   Optional<std::string> getOutliningStyle() const { return OutliningStyle; }
188 
setCalleeSavedStackSize(unsigned Size)189   void setCalleeSavedStackSize(unsigned Size) {
190     CalleeSavedStackSize = Size;
191     HasCalleeSavedStackSize = true;
192   }
193 
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.
getCalleeSavedStackSize(const MachineFrameInfo & MFI)199   unsigned getCalleeSavedStackSize(const MachineFrameInfo &MFI) const {
200     bool ValidateCalleeSavedStackSize = false;
201 
202 #ifndef NDEBUG
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;
207 #endif
208 
209     if (!HasCalleeSavedStackSize || ValidateCalleeSavedStackSize) {
210       assert(MFI.isCalleeSavedInfoValid() && "CalleeSavedInfo not calculated");
211       if (MFI.getCalleeSavedInfo().empty())
212         return 0;
213 
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)
219           continue;
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);
224       }
225 
226       unsigned Size = alignTo(MaxOffset - MinOffset, 16);
227       assert((!HasCalleeSavedStackSize || getCalleeSavedStackSize() == Size) &&
228              "Invalid size calculated for callee saves");
229       return Size;
230     }
231 
232     return getCalleeSavedStackSize();
233   }
234 
getCalleeSavedStackSize()235   unsigned getCalleeSavedStackSize() const {
236     assert(HasCalleeSavedStackSize &&
237            "CalleeSavedStackSize has not been calculated");
238     return CalleeSavedStackSize;
239   }
240 
241   // Saves the CalleeSavedStackSize for SVE vectors in 'scalable bytes'
setSVECalleeSavedStackSize(unsigned Size)242   void setSVECalleeSavedStackSize(unsigned Size) {
243     SVECalleeSavedStackSize = Size;
244   }
getSVECalleeSavedStackSize()245   unsigned getSVECalleeSavedStackSize() const {
246     return SVECalleeSavedStackSize;
247   }
248 
setMinMaxSVECSFrameIndex(int Min,int Max)249   void setMinMaxSVECSFrameIndex(int Min, int Max) {
250     MinSVECSFrameIndex = Min;
251     MaxSVECSFrameIndex = Max;
252   }
253 
getMinSVECSFrameIndex()254   int getMinSVECSFrameIndex() const { return MinSVECSFrameIndex; }
getMaxSVECSFrameIndex()255   int getMaxSVECSFrameIndex() const { return MaxSVECSFrameIndex; }
256 
incNumLocalDynamicTLSAccesses()257   void incNumLocalDynamicTLSAccesses() { ++NumLocalDynamicTLSAccesses; }
getNumLocalDynamicTLSAccesses()258   unsigned getNumLocalDynamicTLSAccesses() const {
259     return NumLocalDynamicTLSAccesses;
260   }
261 
hasRedZone()262   Optional<bool> hasRedZone() const { return HasRedZone; }
setHasRedZone(bool s)263   void setHasRedZone(bool s) { HasRedZone = s; }
264 
getVarArgsStackIndex()265   int getVarArgsStackIndex() const { return VarArgsStackIndex; }
setVarArgsStackIndex(int Index)266   void setVarArgsStackIndex(int Index) { VarArgsStackIndex = Index; }
267 
getVarArgsGPRIndex()268   int getVarArgsGPRIndex() const { return VarArgsGPRIndex; }
setVarArgsGPRIndex(int Index)269   void setVarArgsGPRIndex(int Index) { VarArgsGPRIndex = Index; }
270 
getVarArgsGPRSize()271   unsigned getVarArgsGPRSize() const { return VarArgsGPRSize; }
setVarArgsGPRSize(unsigned Size)272   void setVarArgsGPRSize(unsigned Size) { VarArgsGPRSize = Size; }
273 
getVarArgsFPRIndex()274   int getVarArgsFPRIndex() const { return VarArgsFPRIndex; }
setVarArgsFPRIndex(int Index)275   void setVarArgsFPRIndex(int Index) { VarArgsFPRIndex = Index; }
276 
getVarArgsFPRSize()277   unsigned getVarArgsFPRSize() const { return VarArgsFPRSize; }
setVarArgsFPRSize(unsigned Size)278   void setVarArgsFPRSize(unsigned Size) { VarArgsFPRSize = Size; }
279 
getSRetReturnReg()280   unsigned getSRetReturnReg() const { return SRetReturnReg; }
setSRetReturnReg(unsigned Reg)281   void setSRetReturnReg(unsigned Reg) { SRetReturnReg = Reg; }
282 
getJumpTableEntrySize(int Idx)283   unsigned getJumpTableEntrySize(int Idx) const {
284     auto It = JumpTableEntryInfo.find(Idx);
285     if (It != JumpTableEntryInfo.end())
286       return It->second.first;
287     return 4;
288   }
getJumpTableEntryPCRelSymbol(int Idx)289   MCSymbol *getJumpTableEntryPCRelSymbol(int Idx) const {
290     return JumpTableEntryInfo.find(Idx)->second.second;
291   }
setJumpTableEntryInfo(int Idx,unsigned Size,MCSymbol * PCRelSym)292   void setJumpTableEntryInfo(int Idx, unsigned Size, MCSymbol *PCRelSym) {
293     JumpTableEntryInfo[Idx] = std::make_pair(Size, PCRelSym);
294   }
295 
296   using SetOfInstructions = SmallPtrSet<const MachineInstr *, 16>;
297 
getLOHRelated()298   const SetOfInstructions &getLOHRelated() const { return LOHRelated; }
299 
300   // Shortcuts for LOH related types.
301   class MILOHDirective {
302     MCLOHType Kind;
303 
304     /// Arguments of this directive. Order matters.
305     SmallVector<const MachineInstr *, 3> Args;
306 
307   public:
308     using LOHArgs = ArrayRef<const MachineInstr *>;
309 
MILOHDirective(MCLOHType Kind,LOHArgs Args)310     MILOHDirective(MCLOHType Kind, LOHArgs Args)
311         : Kind(Kind), Args(Args.begin(), Args.end()) {
312       assert(isValidMCLOHType(Kind) && "Invalid LOH directive type!");
313     }
314 
getKind()315     MCLOHType getKind() const { return Kind; }
getArgs()316     LOHArgs getArgs() const { return Args; }
317   };
318 
319   using MILOHArgs = MILOHDirective::LOHArgs;
320   using MILOHContainer = SmallVector<MILOHDirective, 32>;
321 
getLOHContainer()322   const MILOHContainer &getLOHContainer() const { return LOHContainerSet; }
323 
324   /// Add a LOH directive of this @p Kind and this @p Args.
addLOHDirective(MCLOHType Kind,MILOHArgs Args)325   void addLOHDirective(MCLOHType Kind, MILOHArgs Args) {
326     LOHContainerSet.push_back(MILOHDirective(Kind, Args));
327     LOHRelated.insert(Args.begin(), Args.end());
328   }
329 
getForwardedMustTailRegParms()330   SmallVectorImpl<ForwardedRegister> &getForwardedMustTailRegParms() {
331     return ForwardedMustTailRegParms;
332   }
333 
getTaggedBasePointerOffset()334   unsigned getTaggedBasePointerOffset() const {
335     return TaggedBasePointerOffset;
336   }
setTaggedBasePointerOffset(unsigned Offset)337   void setTaggedBasePointerOffset(unsigned Offset) {
338     TaggedBasePointerOffset = Offset;
339   }
340 
341 private:
342   // Hold the lists of LOHs.
343   MILOHContainer LOHContainerSet;
344   SetOfInstructions LOHRelated;
345 
346   DenseMap<int, std::pair<unsigned, MCSymbol *>> JumpTableEntryInfo;
347 };
348 
349 namespace yaml {
350 struct AArch64FunctionInfo final : public yaml::MachineFunctionInfo {
351   Optional<bool> HasRedZone;
352 
353   AArch64FunctionInfo() = default;
354   AArch64FunctionInfo(const llvm::AArch64FunctionInfo &MFI);
355 
356   void mappingImpl(yaml::IO &YamlIO) override;
357   ~AArch64FunctionInfo() = default;
358 };
359 
360 template <> struct MappingTraits<AArch64FunctionInfo> {
361   static void mapping(IO &YamlIO, AArch64FunctionInfo &MFI) {
362     YamlIO.mapOptional("hasRedZone", MFI.HasRedZone);
363   }
364 };
365 
366 } // end namespace yaml
367 
368 } // end namespace llvm
369 
370 #endif // LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H
371