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