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