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