AArch64MachineFunctionInfo.h 16 KB

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