AArch64MachineFunctionInfo.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. #ifndef LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H
  13. #define LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/SmallPtrSet.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/CodeGen/CallingConvLower.h"
  18. #include "llvm/CodeGen/MIRYamlMapping.h"
  19. #include "llvm/CodeGen/MachineFrameInfo.h"
  20. #include "llvm/CodeGen/MachineFunction.h"
  21. #include "llvm/IR/Function.h"
  22. #include "llvm/MC/MCLinkerOptimizationHint.h"
  23. #include <cassert>
  24. #include <optional>
  25. namespace llvm {
  26. namespace yaml {
  27. struct AArch64FunctionInfo;
  28. } // end namespace yaml
  29. class AArch64Subtarget;
  30. class MachineInstr;
  31. /// AArch64FunctionInfo - This class is derived from MachineFunctionInfo and
  32. /// contains private AArch64-specific information for each MachineFunction.
  33. class AArch64FunctionInfo final : public MachineFunctionInfo {
  34. /// 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.
  43. unsigned BytesInStackArgArea = 0;
  44. /// The number of bytes to restore to deallocate space for incoming
  45. /// arguments. Canonically 0 in the C calling convention, but non-zero when
  46. /// callee is expected to pop the args.
  47. unsigned ArgumentStackToRestore = 0;
  48. /// Space just below incoming stack pointer reserved for arguments being
  49. /// passed on the stack during a tail call. This will be the difference
  50. /// between the largest tail call argument space needed in this function and
  51. /// what's already available by reusing space of incoming arguments.
  52. unsigned TailCallReservedStack = 0;
  53. /// HasStackFrame - True if this function has a stack frame. Set by
  54. /// determineCalleeSaves().
  55. bool HasStackFrame = false;
  56. /// Amount of stack frame size, not including callee-saved registers.
  57. uint64_t LocalStackSize = 0;
  58. /// The start and end frame indices for the SVE callee saves.
  59. int MinSVECSFrameIndex = 0;
  60. int MaxSVECSFrameIndex = 0;
  61. /// Amount of stack frame size used for saving callee-saved registers.
  62. unsigned CalleeSavedStackSize = 0;
  63. unsigned SVECalleeSavedStackSize = 0;
  64. bool HasCalleeSavedStackSize = false;
  65. /// Number of TLS accesses using the special (combinable)
  66. /// _TLS_MODULE_BASE_ symbol.
  67. unsigned NumLocalDynamicTLSAccesses = 0;
  68. /// FrameIndex for start of varargs area for arguments passed on the
  69. /// stack.
  70. int VarArgsStackIndex = 0;
  71. /// Offset of start of varargs area for arguments passed on the stack.
  72. unsigned VarArgsStackOffset = 0;
  73. /// FrameIndex for start of varargs area for arguments passed in
  74. /// general purpose registers.
  75. int VarArgsGPRIndex = 0;
  76. /// Size of the varargs area for arguments passed in general purpose
  77. /// registers.
  78. unsigned VarArgsGPRSize = 0;
  79. /// FrameIndex for start of varargs area for arguments passed in
  80. /// floating-point registers.
  81. int VarArgsFPRIndex = 0;
  82. /// Size of the varargs area for arguments passed in floating-point
  83. /// registers.
  84. unsigned VarArgsFPRSize = 0;
  85. /// True if this function has a subset of CSRs that is handled explicitly via
  86. /// copies.
  87. bool IsSplitCSR = false;
  88. /// True when the stack gets realigned dynamically because the size of stack
  89. /// frame is unknown at compile time. e.g., in case of VLAs.
  90. bool StackRealigned = false;
  91. /// True when the callee-save stack area has unused gaps that may be used for
  92. /// other stack allocations.
  93. bool CalleeSaveStackHasFreeSpace = false;
  94. /// SRetReturnReg - sret lowering includes returning the value of the
  95. /// returned struct in a register. This field holds the virtual register into
  96. /// which the sret argument is passed.
  97. Register SRetReturnReg;
  98. /// SVE stack size (for predicates and data vectors) are maintained here
  99. /// rather than in FrameInfo, as the placement and Stack IDs are target
  100. /// specific.
  101. uint64_t StackSizeSVE = 0;
  102. /// HasCalculatedStackSizeSVE indicates whether StackSizeSVE is valid.
  103. bool HasCalculatedStackSizeSVE = false;
  104. /// Has a value when it is known whether or not the function uses a
  105. /// redzone, and no value otherwise.
  106. /// Initialized during frame lowering, unless the function has the noredzone
  107. /// attribute, in which case it is set to false at construction.
  108. std::optional<bool> HasRedZone;
  109. /// ForwardedMustTailRegParms - A list of virtual and physical registers
  110. /// that must be forwarded to every musttail call.
  111. SmallVector<ForwardedRegister, 1> ForwardedMustTailRegParms;
  112. /// FrameIndex for the tagged base pointer.
  113. std::optional<int> TaggedBasePointerIndex;
  114. /// Offset from SP-at-entry to the tagged base pointer.
  115. /// Tagged base pointer is set up to point to the first (lowest address)
  116. /// tagged stack slot.
  117. unsigned TaggedBasePointerOffset;
  118. /// OutliningStyle denotes, if a function was outined, how it was outlined,
  119. /// e.g. Tail Call, Thunk, or Function if none apply.
  120. std::optional<std::string> OutliningStyle;
  121. // Offset from SP-after-callee-saved-spills (i.e. SP-at-entry minus
  122. // CalleeSavedStackSize) to the address of the frame record.
  123. int CalleeSaveBaseToFrameRecordOffset = 0;
  124. /// SignReturnAddress is true if PAC-RET is enabled for the function with
  125. /// defaults being sign non-leaf functions only, with the B key.
  126. bool SignReturnAddress = false;
  127. /// SignReturnAddressAll modifies the default PAC-RET mode to signing leaf
  128. /// functions as well.
  129. bool SignReturnAddressAll = false;
  130. /// SignWithBKey modifies the default PAC-RET mode to signing with the B key.
  131. bool SignWithBKey = false;
  132. /// BranchTargetEnforcement enables placing BTI instructions at potential
  133. /// indirect branch destinations.
  134. bool BranchTargetEnforcement = false;
  135. /// Whether this function has an extended frame record [Ctx, FP, LR]. If so,
  136. /// bit 60 of the in-memory FP will be 1 to enable other tools to detect the
  137. /// extended record.
  138. bool HasSwiftAsyncContext = false;
  139. /// The stack slot where the Swift asynchronous context is stored.
  140. int SwiftAsyncContextFrameIdx = std::numeric_limits<int>::max();
  141. bool IsMTETagged = false;
  142. /// The function has Scalable Vector or Scalable Predicate register argument
  143. /// or return type
  144. bool IsSVECC = false;
  145. /// The frame-index for the TPIDR2 object used for lazy saves.
  146. Register LazySaveTPIDR2Obj = 0;
  147. /// True if the function need unwind information.
  148. mutable std::optional<bool> NeedsDwarfUnwindInfo;
  149. /// True if the function need asynchronous unwind information.
  150. mutable std::optional<bool> NeedsAsyncDwarfUnwindInfo;
  151. public:
  152. AArch64FunctionInfo(const Function &F, const AArch64Subtarget *STI);
  153. MachineFunctionInfo *
  154. clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF,
  155. const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
  156. const override;
  157. bool isSVECC() const { return IsSVECC; };
  158. void setIsSVECC(bool s) { IsSVECC = s; };
  159. unsigned getLazySaveTPIDR2Obj() const { return LazySaveTPIDR2Obj; }
  160. void setLazySaveTPIDR2Obj(unsigned Reg) { LazySaveTPIDR2Obj = Reg; }
  161. void initializeBaseYamlFields(const yaml::AArch64FunctionInfo &YamlMFI);
  162. unsigned getBytesInStackArgArea() const { return BytesInStackArgArea; }
  163. void setBytesInStackArgArea(unsigned bytes) { BytesInStackArgArea = bytes; }
  164. unsigned getArgumentStackToRestore() const { return ArgumentStackToRestore; }
  165. void setArgumentStackToRestore(unsigned bytes) {
  166. ArgumentStackToRestore = bytes;
  167. }
  168. unsigned getTailCallReservedStack() const { return TailCallReservedStack; }
  169. void setTailCallReservedStack(unsigned bytes) {
  170. TailCallReservedStack = bytes;
  171. }
  172. bool hasCalculatedStackSizeSVE() const { return HasCalculatedStackSizeSVE; }
  173. void setStackSizeSVE(uint64_t S) {
  174. HasCalculatedStackSizeSVE = true;
  175. StackSizeSVE = S;
  176. }
  177. uint64_t getStackSizeSVE() const { return StackSizeSVE; }
  178. bool hasStackFrame() const { return HasStackFrame; }
  179. void setHasStackFrame(bool s) { HasStackFrame = s; }
  180. bool isStackRealigned() const { return StackRealigned; }
  181. void setStackRealigned(bool s) { StackRealigned = s; }
  182. bool hasCalleeSaveStackFreeSpace() const {
  183. return CalleeSaveStackHasFreeSpace;
  184. }
  185. void setCalleeSaveStackHasFreeSpace(bool s) {
  186. CalleeSaveStackHasFreeSpace = s;
  187. }
  188. bool isSplitCSR() const { return IsSplitCSR; }
  189. void setIsSplitCSR(bool s) { IsSplitCSR = s; }
  190. void setLocalStackSize(uint64_t Size) { LocalStackSize = Size; }
  191. uint64_t getLocalStackSize() const { return LocalStackSize; }
  192. void setOutliningStyle(std::string Style) { OutliningStyle = Style; }
  193. std::optional<std::string> getOutliningStyle() const {
  194. return OutliningStyle;
  195. }
  196. void setCalleeSavedStackSize(unsigned Size) {
  197. CalleeSavedStackSize = Size;
  198. HasCalleeSavedStackSize = true;
  199. }
  200. // When CalleeSavedStackSize has not been set (for example when
  201. // some MachineIR pass is run in isolation), then recalculate
  202. // the CalleeSavedStackSize directly from the CalleeSavedInfo.
  203. // Note: This information can only be recalculated after PEI
  204. // has assigned offsets to the callee save objects.
  205. unsigned getCalleeSavedStackSize(const MachineFrameInfo &MFI) const {
  206. bool ValidateCalleeSavedStackSize = false;
  207. #ifndef NDEBUG
  208. // Make sure the calculated size derived from the CalleeSavedInfo
  209. // equals the cached size that was calculated elsewhere (e.g. in
  210. // determineCalleeSaves).
  211. ValidateCalleeSavedStackSize = HasCalleeSavedStackSize;
  212. #endif
  213. if (!HasCalleeSavedStackSize || ValidateCalleeSavedStackSize) {
  214. assert(MFI.isCalleeSavedInfoValid() && "CalleeSavedInfo not calculated");
  215. if (MFI.getCalleeSavedInfo().empty())
  216. return 0;
  217. int64_t MinOffset = std::numeric_limits<int64_t>::max();
  218. int64_t MaxOffset = std::numeric_limits<int64_t>::min();
  219. for (const auto &Info : MFI.getCalleeSavedInfo()) {
  220. int FrameIdx = Info.getFrameIdx();
  221. if (MFI.getStackID(FrameIdx) != TargetStackID::Default)
  222. continue;
  223. int64_t Offset = MFI.getObjectOffset(FrameIdx);
  224. int64_t ObjSize = MFI.getObjectSize(FrameIdx);
  225. MinOffset = std::min<int64_t>(Offset, MinOffset);
  226. MaxOffset = std::max<int64_t>(Offset + ObjSize, MaxOffset);
  227. }
  228. if (SwiftAsyncContextFrameIdx != std::numeric_limits<int>::max()) {
  229. int64_t Offset = MFI.getObjectOffset(getSwiftAsyncContextFrameIdx());
  230. int64_t ObjSize = MFI.getObjectSize(getSwiftAsyncContextFrameIdx());
  231. MinOffset = std::min<int64_t>(Offset, MinOffset);
  232. MaxOffset = std::max<int64_t>(Offset + ObjSize, MaxOffset);
  233. }
  234. unsigned Size = alignTo(MaxOffset - MinOffset, 16);
  235. assert((!HasCalleeSavedStackSize || getCalleeSavedStackSize() == Size) &&
  236. "Invalid size calculated for callee saves");
  237. return Size;
  238. }
  239. return getCalleeSavedStackSize();
  240. }
  241. unsigned getCalleeSavedStackSize() const {
  242. assert(HasCalleeSavedStackSize &&
  243. "CalleeSavedStackSize has not been calculated");
  244. return CalleeSavedStackSize;
  245. }
  246. // Saves the CalleeSavedStackSize for SVE vectors in 'scalable bytes'
  247. void setSVECalleeSavedStackSize(unsigned Size) {
  248. SVECalleeSavedStackSize = Size;
  249. }
  250. unsigned getSVECalleeSavedStackSize() const {
  251. return SVECalleeSavedStackSize;
  252. }
  253. void setMinMaxSVECSFrameIndex(int Min, int Max) {
  254. MinSVECSFrameIndex = Min;
  255. MaxSVECSFrameIndex = Max;
  256. }
  257. int getMinSVECSFrameIndex() const { return MinSVECSFrameIndex; }
  258. int getMaxSVECSFrameIndex() const { return MaxSVECSFrameIndex; }
  259. void incNumLocalDynamicTLSAccesses() { ++NumLocalDynamicTLSAccesses; }
  260. unsigned getNumLocalDynamicTLSAccesses() const {
  261. return NumLocalDynamicTLSAccesses;
  262. }
  263. std::optional<bool> hasRedZone() const { return HasRedZone; }
  264. void setHasRedZone(bool s) { HasRedZone = s; }
  265. int getVarArgsStackIndex() const { return VarArgsStackIndex; }
  266. void setVarArgsStackIndex(int Index) { VarArgsStackIndex = Index; }
  267. unsigned getVarArgsStackOffset() const { return VarArgsStackOffset; }
  268. void setVarArgsStackOffset(unsigned Offset) { VarArgsStackOffset = Offset; }
  269. int getVarArgsGPRIndex() const { return VarArgsGPRIndex; }
  270. void setVarArgsGPRIndex(int Index) { VarArgsGPRIndex = Index; }
  271. unsigned getVarArgsGPRSize() const { return VarArgsGPRSize; }
  272. void setVarArgsGPRSize(unsigned Size) { VarArgsGPRSize = Size; }
  273. int getVarArgsFPRIndex() const { return VarArgsFPRIndex; }
  274. void setVarArgsFPRIndex(int Index) { VarArgsFPRIndex = Index; }
  275. unsigned getVarArgsFPRSize() const { return VarArgsFPRSize; }
  276. void setVarArgsFPRSize(unsigned Size) { VarArgsFPRSize = Size; }
  277. unsigned getSRetReturnReg() const { return SRetReturnReg; }
  278. void setSRetReturnReg(unsigned Reg) { SRetReturnReg = Reg; }
  279. unsigned getJumpTableEntrySize(int Idx) const {
  280. return JumpTableEntryInfo[Idx].first;
  281. }
  282. MCSymbol *getJumpTableEntryPCRelSymbol(int Idx) const {
  283. return JumpTableEntryInfo[Idx].second;
  284. }
  285. void setJumpTableEntryInfo(int Idx, unsigned Size, MCSymbol *PCRelSym) {
  286. if ((unsigned)Idx >= JumpTableEntryInfo.size())
  287. JumpTableEntryInfo.resize(Idx+1);
  288. JumpTableEntryInfo[Idx] = std::make_pair(Size, PCRelSym);
  289. }
  290. using SetOfInstructions = SmallPtrSet<const MachineInstr *, 16>;
  291. const SetOfInstructions &getLOHRelated() const { return LOHRelated; }
  292. // Shortcuts for LOH related types.
  293. class MILOHDirective {
  294. MCLOHType Kind;
  295. /// Arguments of this directive. Order matters.
  296. SmallVector<const MachineInstr *, 3> Args;
  297. public:
  298. using LOHArgs = ArrayRef<const MachineInstr *>;
  299. MILOHDirective(MCLOHType Kind, LOHArgs Args)
  300. : Kind(Kind), Args(Args.begin(), Args.end()) {
  301. assert(isValidMCLOHType(Kind) && "Invalid LOH directive type!");
  302. }
  303. MCLOHType getKind() const { return Kind; }
  304. LOHArgs getArgs() const { return Args; }
  305. };
  306. using MILOHArgs = MILOHDirective::LOHArgs;
  307. using MILOHContainer = SmallVector<MILOHDirective, 32>;
  308. const MILOHContainer &getLOHContainer() const { return LOHContainerSet; }
  309. /// Add a LOH directive of this @p Kind and this @p Args.
  310. void addLOHDirective(MCLOHType Kind, MILOHArgs Args) {
  311. LOHContainerSet.push_back(MILOHDirective(Kind, Args));
  312. LOHRelated.insert(Args.begin(), Args.end());
  313. }
  314. SmallVectorImpl<ForwardedRegister> &getForwardedMustTailRegParms() {
  315. return ForwardedMustTailRegParms;
  316. }
  317. std::optional<int> getTaggedBasePointerIndex() const {
  318. return TaggedBasePointerIndex;
  319. }
  320. void setTaggedBasePointerIndex(int Index) { TaggedBasePointerIndex = Index; }
  321. unsigned getTaggedBasePointerOffset() const {
  322. return TaggedBasePointerOffset;
  323. }
  324. void setTaggedBasePointerOffset(unsigned Offset) {
  325. TaggedBasePointerOffset = Offset;
  326. }
  327. int getCalleeSaveBaseToFrameRecordOffset() const {
  328. return CalleeSaveBaseToFrameRecordOffset;
  329. }
  330. void setCalleeSaveBaseToFrameRecordOffset(int Offset) {
  331. CalleeSaveBaseToFrameRecordOffset = Offset;
  332. }
  333. bool shouldSignReturnAddress(const MachineFunction &MF) const;
  334. bool shouldSignReturnAddress(bool SpillsLR) const;
  335. bool shouldSignWithBKey() const { return SignWithBKey; }
  336. bool isMTETagged() const { return IsMTETagged; }
  337. bool branchTargetEnforcement() const { return BranchTargetEnforcement; }
  338. void setHasSwiftAsyncContext(bool HasContext) {
  339. HasSwiftAsyncContext = HasContext;
  340. }
  341. bool hasSwiftAsyncContext() const { return HasSwiftAsyncContext; }
  342. void setSwiftAsyncContextFrameIdx(int FI) {
  343. SwiftAsyncContextFrameIdx = FI;
  344. }
  345. int getSwiftAsyncContextFrameIdx() const { return SwiftAsyncContextFrameIdx; }
  346. bool needsDwarfUnwindInfo(const MachineFunction &MF) const;
  347. bool needsAsyncDwarfUnwindInfo(const MachineFunction &MF) const;
  348. private:
  349. // Hold the lists of LOHs.
  350. MILOHContainer LOHContainerSet;
  351. SetOfInstructions LOHRelated;
  352. SmallVector<std::pair<unsigned, MCSymbol *>, 2> JumpTableEntryInfo;
  353. };
  354. namespace yaml {
  355. struct AArch64FunctionInfo final : public yaml::MachineFunctionInfo {
  356. std::optional<bool> HasRedZone;
  357. AArch64FunctionInfo() = default;
  358. AArch64FunctionInfo(const llvm::AArch64FunctionInfo &MFI);
  359. void mappingImpl(yaml::IO &YamlIO) override;
  360. ~AArch64FunctionInfo() = default;
  361. };
  362. template <> struct MappingTraits<AArch64FunctionInfo> {
  363. static void mapping(IO &YamlIO, AArch64FunctionInfo &MFI) {
  364. YamlIO.mapOptional("hasRedZone", MFI.HasRedZone);
  365. }
  366. };
  367. } // end namespace yaml
  368. } // end namespace llvm
  369. #endif // LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H