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 public: 163 explicit AArch64FunctionInfo(MachineFunction &MF); 164 165 void initializeBaseYamlFields(const yaml::AArch64FunctionInfo &YamlMFI); 166 167 unsigned getBytesInStackArgArea() const { return BytesInStackArgArea; } 168 void setBytesInStackArgArea(unsigned bytes) { BytesInStackArgArea = bytes; } 169 170 unsigned getArgumentStackToRestore() const { return ArgumentStackToRestore; } 171 void setArgumentStackToRestore(unsigned bytes) { 172 ArgumentStackToRestore = bytes; 173 } 174 175 bool hasCalculatedStackSizeSVE() const { return HasCalculatedStackSizeSVE; } 176 177 void setStackSizeSVE(uint64_t S) { 178 HasCalculatedStackSizeSVE = true; 179 StackSizeSVE = S; 180 } 181 182 uint64_t getStackSizeSVE() const { return StackSizeSVE; } 183 184 bool hasStackFrame() const { return HasStackFrame; } 185 void setHasStackFrame(bool s) { HasStackFrame = s; } 186 187 bool isStackRealigned() const { return StackRealigned; } 188 void setStackRealigned(bool s) { StackRealigned = s; } 189 190 bool hasCalleeSaveStackFreeSpace() const { 191 return CalleeSaveStackHasFreeSpace; 192 } 193 void setCalleeSaveStackHasFreeSpace(bool s) { 194 CalleeSaveStackHasFreeSpace = s; 195 } 196 bool isSplitCSR() const { return IsSplitCSR; } 197 void setIsSplitCSR(bool s) { IsSplitCSR = s; } 198 199 void setLocalStackSize(uint64_t Size) { LocalStackSize = Size; } 200 uint64_t getLocalStackSize() const { return LocalStackSize; } 201 202 void setOutliningStyle(std::string Style) { OutliningStyle = Style; } 203 Optional<std::string> getOutliningStyle() const { return OutliningStyle; } 204 205 void setCalleeSavedStackSize(unsigned Size) { 206 CalleeSavedStackSize = Size; 207 HasCalleeSavedStackSize = true; 208 } 209 210 // When CalleeSavedStackSize has not been set (for example when 211 // some MachineIR pass is run in isolation), then recalculate 212 // the CalleeSavedStackSize directly from the CalleeSavedInfo. 213 // Note: This information can only be recalculated after PEI 214 // has assigned offsets to the callee save objects. 215 unsigned getCalleeSavedStackSize(const MachineFrameInfo &MFI) const { 216 bool ValidateCalleeSavedStackSize = false; 217 218 #ifndef NDEBUG 219 // Make sure the calculated size derived from the CalleeSavedInfo 220 // equals the cached size that was calculated elsewhere (e.g. in 221 // determineCalleeSaves). 222 ValidateCalleeSavedStackSize = HasCalleeSavedStackSize; 223 #endif 224 225 if (!HasCalleeSavedStackSize || ValidateCalleeSavedStackSize) { 226 assert(MFI.isCalleeSavedInfoValid() && "CalleeSavedInfo not calculated"); 227 if (MFI.getCalleeSavedInfo().empty()) 228 return 0; 229 230 int64_t MinOffset = std::numeric_limits<int64_t>::max(); 231 int64_t MaxOffset = std::numeric_limits<int64_t>::min(); 232 for (const auto &Info : MFI.getCalleeSavedInfo()) { 233 int FrameIdx = Info.getFrameIdx(); 234 if (MFI.getStackID(FrameIdx) != TargetStackID::Default) 235 continue; 236 int64_t Offset = MFI.getObjectOffset(FrameIdx); 237 int64_t ObjSize = MFI.getObjectSize(FrameIdx); 238 MinOffset = std::min<int64_t>(Offset, MinOffset); 239 MaxOffset = std::max<int64_t>(Offset + ObjSize, MaxOffset); 240 } 241 242 unsigned Size = alignTo(MaxOffset - MinOffset, 16); 243 assert((!HasCalleeSavedStackSize || getCalleeSavedStackSize() == Size) && 244 "Invalid size calculated for callee saves"); 245 return Size; 246 } 247 248 return getCalleeSavedStackSize(); 249 } 250 251 unsigned getCalleeSavedStackSize() const { 252 assert(HasCalleeSavedStackSize && 253 "CalleeSavedStackSize has not been calculated"); 254 return CalleeSavedStackSize; 255 } 256 257 // Saves the CalleeSavedStackSize for SVE vectors in 'scalable bytes' 258 void setSVECalleeSavedStackSize(unsigned Size) { 259 SVECalleeSavedStackSize = Size; 260 } 261 unsigned getSVECalleeSavedStackSize() const { 262 return SVECalleeSavedStackSize; 263 } 264 265 void setMinMaxSVECSFrameIndex(int Min, int Max) { 266 MinSVECSFrameIndex = Min; 267 MaxSVECSFrameIndex = Max; 268 } 269 270 int getMinSVECSFrameIndex() const { return MinSVECSFrameIndex; } 271 int getMaxSVECSFrameIndex() const { return MaxSVECSFrameIndex; } 272 273 void incNumLocalDynamicTLSAccesses() { ++NumLocalDynamicTLSAccesses; } 274 unsigned getNumLocalDynamicTLSAccesses() const { 275 return NumLocalDynamicTLSAccesses; 276 } 277 278 Optional<bool> hasRedZone() const { return HasRedZone; } 279 void setHasRedZone(bool s) { HasRedZone = s; } 280 281 int getVarArgsStackIndex() const { return VarArgsStackIndex; } 282 void setVarArgsStackIndex(int Index) { VarArgsStackIndex = Index; } 283 284 int getVarArgsGPRIndex() const { return VarArgsGPRIndex; } 285 void setVarArgsGPRIndex(int Index) { VarArgsGPRIndex = Index; } 286 287 unsigned getVarArgsGPRSize() const { return VarArgsGPRSize; } 288 void setVarArgsGPRSize(unsigned Size) { VarArgsGPRSize = Size; } 289 290 int getVarArgsFPRIndex() const { return VarArgsFPRIndex; } 291 void setVarArgsFPRIndex(int Index) { VarArgsFPRIndex = Index; } 292 293 unsigned getVarArgsFPRSize() const { return VarArgsFPRSize; } 294 void setVarArgsFPRSize(unsigned Size) { VarArgsFPRSize = Size; } 295 296 unsigned getSRetReturnReg() const { return SRetReturnReg; } 297 void setSRetReturnReg(unsigned Reg) { SRetReturnReg = Reg; } 298 299 unsigned getJumpTableEntrySize(int Idx) const { 300 return JumpTableEntryInfo[Idx].first; 301 } 302 MCSymbol *getJumpTableEntryPCRelSymbol(int Idx) const { 303 return JumpTableEntryInfo[Idx].second; 304 } 305 void setJumpTableEntryInfo(int Idx, unsigned Size, MCSymbol *PCRelSym) { 306 if ((unsigned)Idx >= JumpTableEntryInfo.size()) 307 JumpTableEntryInfo.resize(Idx+1); 308 JumpTableEntryInfo[Idx] = std::make_pair(Size, PCRelSym); 309 } 310 311 using SetOfInstructions = SmallPtrSet<const MachineInstr *, 16>; 312 313 const SetOfInstructions &getLOHRelated() const { return LOHRelated; } 314 315 // Shortcuts for LOH related types. 316 class MILOHDirective { 317 MCLOHType Kind; 318 319 /// Arguments of this directive. Order matters. 320 SmallVector<const MachineInstr *, 3> Args; 321 322 public: 323 using LOHArgs = ArrayRef<const MachineInstr *>; 324 325 MILOHDirective(MCLOHType Kind, LOHArgs Args) 326 : Kind(Kind), Args(Args.begin(), Args.end()) { 327 assert(isValidMCLOHType(Kind) && "Invalid LOH directive type!"); 328 } 329 330 MCLOHType getKind() const { return Kind; } 331 LOHArgs getArgs() const { return Args; } 332 }; 333 334 using MILOHArgs = MILOHDirective::LOHArgs; 335 using MILOHContainer = SmallVector<MILOHDirective, 32>; 336 337 const MILOHContainer &getLOHContainer() const { return LOHContainerSet; } 338 339 /// Add a LOH directive of this @p Kind and this @p Args. 340 void addLOHDirective(MCLOHType Kind, MILOHArgs Args) { 341 LOHContainerSet.push_back(MILOHDirective(Kind, Args)); 342 LOHRelated.insert(Args.begin(), Args.end()); 343 } 344 345 SmallVectorImpl<ForwardedRegister> &getForwardedMustTailRegParms() { 346 return ForwardedMustTailRegParms; 347 } 348 349 Optional<int> getTaggedBasePointerIndex() const { 350 return TaggedBasePointerIndex; 351 } 352 void setTaggedBasePointerIndex(int Index) { TaggedBasePointerIndex = Index; } 353 354 unsigned getTaggedBasePointerOffset() const { 355 return TaggedBasePointerOffset; 356 } 357 void setTaggedBasePointerOffset(unsigned Offset) { 358 TaggedBasePointerOffset = Offset; 359 } 360 361 int getCalleeSaveBaseToFrameRecordOffset() const { 362 return CalleeSaveBaseToFrameRecordOffset; 363 } 364 void setCalleeSaveBaseToFrameRecordOffset(int Offset) { 365 CalleeSaveBaseToFrameRecordOffset = Offset; 366 } 367 368 bool shouldSignReturnAddress() const; 369 bool shouldSignReturnAddress(bool SpillsLR) const; 370 371 bool shouldSignWithBKey() const { return SignWithBKey; } 372 373 bool branchTargetEnforcement() const { return BranchTargetEnforcement; } 374 375 private: 376 // Hold the lists of LOHs. 377 MILOHContainer LOHContainerSet; 378 SetOfInstructions LOHRelated; 379 380 SmallVector<std::pair<unsigned, MCSymbol *>, 2> JumpTableEntryInfo; 381 }; 382 383 namespace yaml { 384 struct AArch64FunctionInfo final : public yaml::MachineFunctionInfo { 385 Optional<bool> HasRedZone; 386 387 AArch64FunctionInfo() = default; 388 AArch64FunctionInfo(const llvm::AArch64FunctionInfo &MFI); 389 390 void mappingImpl(yaml::IO &YamlIO) override; 391 ~AArch64FunctionInfo() = default; 392 }; 393 394 template <> struct MappingTraits<AArch64FunctionInfo> { 395 static void mapping(IO &YamlIO, AArch64FunctionInfo &MFI) { 396 YamlIO.mapOptional("hasRedZone", MFI.HasRedZone); 397 } 398 }; 399 400 } // end namespace yaml 401 402 } // end namespace llvm 403 404 #endif // LLVM_LIB_TARGET_AARCH64_AARCH64MACHINEFUNCTIONINFO_H 405