1 //===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- 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 // The file defines the MachineFrameInfo class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CODEGEN_MACHINEFRAMEINFO_H 14 #define LLVM_CODEGEN_MACHINEFRAMEINFO_H 15 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/CodeGen/Register.h" 18 #include "llvm/CodeGen/TargetFrameLowering.h" 19 #include "llvm/Support/Alignment.h" 20 #include <cassert> 21 #include <vector> 22 23 namespace llvm { 24 class raw_ostream; 25 class MachineFunction; 26 class MachineBasicBlock; 27 class BitVector; 28 class AllocaInst; 29 30 /// The CalleeSavedInfo class tracks the information need to locate where a 31 /// callee saved register is in the current frame. 32 /// Callee saved reg can also be saved to a different register rather than 33 /// on the stack by setting DstReg instead of FrameIdx. 34 class CalleeSavedInfo { 35 Register Reg; 36 union { 37 int FrameIdx; 38 unsigned DstReg; 39 }; 40 /// Flag indicating whether the register is actually restored in the epilog. 41 /// In most cases, if a register is saved, it is also restored. There are 42 /// some situations, though, when this is not the case. For example, the 43 /// LR register on ARM is usually saved, but on exit from the function its 44 /// saved value may be loaded directly into PC. Since liveness tracking of 45 /// physical registers treats callee-saved registers are live outside of 46 /// the function, LR would be treated as live-on-exit, even though in these 47 /// scenarios it is not. This flag is added to indicate that the saved 48 /// register described by this object is not restored in the epilog. 49 /// The long-term solution is to model the liveness of callee-saved registers 50 /// by implicit uses on the return instructions, however, the required 51 /// changes in the ARM backend would be quite extensive. 52 bool Restored = true; 53 /// Flag indicating whether the register is spilled to stack or another 54 /// register. 55 bool SpilledToReg = false; 56 57 public: Reg(R)58 explicit CalleeSavedInfo(unsigned R, int FI = 0) : Reg(R), FrameIdx(FI) {} 59 60 // Accessors. getReg()61 Register getReg() const { return Reg; } getFrameIdx()62 int getFrameIdx() const { return FrameIdx; } getDstReg()63 unsigned getDstReg() const { return DstReg; } setFrameIdx(int FI)64 void setFrameIdx(int FI) { 65 FrameIdx = FI; 66 SpilledToReg = false; 67 } setDstReg(Register SpillReg)68 void setDstReg(Register SpillReg) { 69 DstReg = SpillReg; 70 SpilledToReg = true; 71 } isRestored()72 bool isRestored() const { return Restored; } setRestored(bool R)73 void setRestored(bool R) { Restored = R; } isSpilledToReg()74 bool isSpilledToReg() const { return SpilledToReg; } 75 }; 76 77 /// The MachineFrameInfo class represents an abstract stack frame until 78 /// prolog/epilog code is inserted. This class is key to allowing stack frame 79 /// representation optimizations, such as frame pointer elimination. It also 80 /// allows more mundane (but still important) optimizations, such as reordering 81 /// of abstract objects on the stack frame. 82 /// 83 /// To support this, the class assigns unique integer identifiers to stack 84 /// objects requested clients. These identifiers are negative integers for 85 /// fixed stack objects (such as arguments passed on the stack) or nonnegative 86 /// for objects that may be reordered. Instructions which refer to stack 87 /// objects use a special MO_FrameIndex operand to represent these frame 88 /// indexes. 89 /// 90 /// Because this class keeps track of all references to the stack frame, it 91 /// knows when a variable sized object is allocated on the stack. This is the 92 /// sole condition which prevents frame pointer elimination, which is an 93 /// important optimization on register-poor architectures. Because original 94 /// variable sized alloca's in the source program are the only source of 95 /// variable sized stack objects, it is safe to decide whether there will be 96 /// any variable sized objects before all stack objects are known (for 97 /// example, register allocator spill code never needs variable sized 98 /// objects). 99 /// 100 /// When prolog/epilog code emission is performed, the final stack frame is 101 /// built and the machine instructions are modified to refer to the actual 102 /// stack offsets of the object, eliminating all MO_FrameIndex operands from 103 /// the program. 104 /// 105 /// Abstract Stack Frame Information 106 class MachineFrameInfo { 107 public: 108 /// Stack Smashing Protection (SSP) rules require that vulnerable stack 109 /// allocations are located close the stack protector. 110 enum SSPLayoutKind { 111 SSPLK_None, ///< Did not trigger a stack protector. No effect on data 112 ///< layout. 113 SSPLK_LargeArray, ///< Array or nested array >= SSP-buffer-size. Closest 114 ///< to the stack protector. 115 SSPLK_SmallArray, ///< Array or nested array < SSP-buffer-size. 2nd closest 116 ///< to the stack protector. 117 SSPLK_AddrOf ///< The address of this allocation is exposed and 118 ///< triggered protection. 3rd closest to the protector. 119 }; 120 121 private: 122 // Represent a single object allocated on the stack. 123 struct StackObject { 124 // The offset of this object from the stack pointer on entry to 125 // the function. This field has no meaning for a variable sized element. 126 int64_t SPOffset; 127 128 // The size of this object on the stack. 0 means a variable sized object, 129 // ~0ULL means a dead object. 130 uint64_t Size; 131 132 // The required alignment of this stack slot. 133 Align Alignment; 134 135 // If true, the value of the stack object is set before 136 // entering the function and is not modified inside the function. By 137 // default, fixed objects are immutable unless marked otherwise. 138 bool isImmutable; 139 140 // If true the stack object is used as spill slot. It 141 // cannot alias any other memory objects. 142 bool isSpillSlot; 143 144 /// If true, this stack slot is used to spill a value (could be deopt 145 /// and/or GC related) over a statepoint. We know that the address of the 146 /// slot can't alias any LLVM IR value. This is very similar to a Spill 147 /// Slot, but is created by statepoint lowering is SelectionDAG, not the 148 /// register allocator. 149 bool isStatepointSpillSlot = false; 150 151 /// Identifier for stack memory type analagous to address space. If this is 152 /// non-0, the meaning is target defined. Offsets cannot be directly 153 /// compared between objects with different stack IDs. The object may not 154 /// necessarily reside in the same contiguous memory block as other stack 155 /// objects. Objects with differing stack IDs should not be merged or 156 /// replaced substituted for each other. 157 // 158 /// It is assumed a target uses consecutive, increasing stack IDs starting 159 /// from 1. 160 uint8_t StackID; 161 162 /// If this stack object is originated from an Alloca instruction 163 /// this value saves the original IR allocation. Can be NULL. 164 const AllocaInst *Alloca; 165 166 // If true, the object was mapped into the local frame 167 // block and doesn't need additional handling for allocation beyond that. 168 bool PreAllocated = false; 169 170 // If true, an LLVM IR value might point to this object. 171 // Normally, spill slots and fixed-offset objects don't alias IR-accessible 172 // objects, but there are exceptions (on PowerPC, for example, some byval 173 // arguments have ABI-prescribed offsets). 174 bool isAliased; 175 176 /// If true, the object has been zero-extended. 177 bool isZExt = false; 178 179 /// If true, the object has been sign-extended. 180 bool isSExt = false; 181 182 uint8_t SSPLayout = SSPLK_None; 183 184 StackObject(uint64_t Size, Align Alignment, int64_t SPOffset, 185 bool IsImmutable, bool IsSpillSlot, const AllocaInst *Alloca, 186 bool IsAliased, uint8_t StackID = 0) SPOffsetStackObject187 : SPOffset(SPOffset), Size(Size), Alignment(Alignment), 188 isImmutable(IsImmutable), isSpillSlot(IsSpillSlot), StackID(StackID), 189 Alloca(Alloca), isAliased(IsAliased) {} 190 }; 191 192 /// The alignment of the stack. 193 Align StackAlignment; 194 195 /// Can the stack be realigned. This can be false if the target does not 196 /// support stack realignment, or if the user asks us not to realign the 197 /// stack. In this situation, overaligned allocas are all treated as dynamic 198 /// allocations and the target must handle them as part of DYNAMIC_STACKALLOC 199 /// lowering. All non-alloca stack objects have their alignment clamped to the 200 /// base ABI stack alignment. 201 /// FIXME: There is room for improvement in this case, in terms of 202 /// grouping overaligned allocas into a "secondary stack frame" and 203 /// then only use a single alloca to allocate this frame and only a 204 /// single virtual register to access it. Currently, without such an 205 /// optimization, each such alloca gets its own dynamic realignment. 206 bool StackRealignable; 207 208 /// Whether the function has the \c alignstack attribute. 209 bool ForcedRealign; 210 211 /// The list of stack objects allocated. 212 std::vector<StackObject> Objects; 213 214 /// This contains the number of fixed objects contained on 215 /// the stack. Because fixed objects are stored at a negative index in the 216 /// Objects list, this is also the index to the 0th object in the list. 217 unsigned NumFixedObjects = 0; 218 219 /// This boolean keeps track of whether any variable 220 /// sized objects have been allocated yet. 221 bool HasVarSizedObjects = false; 222 223 /// This boolean keeps track of whether there is a call 224 /// to builtin \@llvm.frameaddress. 225 bool FrameAddressTaken = false; 226 227 /// This boolean keeps track of whether there is a call 228 /// to builtin \@llvm.returnaddress. 229 bool ReturnAddressTaken = false; 230 231 /// This boolean keeps track of whether there is a call 232 /// to builtin \@llvm.experimental.stackmap. 233 bool HasStackMap = false; 234 235 /// This boolean keeps track of whether there is a call 236 /// to builtin \@llvm.experimental.patchpoint. 237 bool HasPatchPoint = false; 238 239 /// The prolog/epilog code inserter calculates the final stack 240 /// offsets for all of the fixed size objects, updating the Objects list 241 /// above. It then updates StackSize to contain the number of bytes that need 242 /// to be allocated on entry to the function. 243 uint64_t StackSize = 0; 244 245 /// The amount that a frame offset needs to be adjusted to 246 /// have the actual offset from the stack/frame pointer. The exact usage of 247 /// this is target-dependent, but it is typically used to adjust between 248 /// SP-relative and FP-relative offsets. E.G., if objects are accessed via 249 /// SP then OffsetAdjustment is zero; if FP is used, OffsetAdjustment is set 250 /// to the distance between the initial SP and the value in FP. For many 251 /// targets, this value is only used when generating debug info (via 252 /// TargetRegisterInfo::getFrameIndexReference); when generating code, the 253 /// corresponding adjustments are performed directly. 254 int OffsetAdjustment = 0; 255 256 /// The prolog/epilog code inserter may process objects that require greater 257 /// alignment than the default alignment the target provides. 258 /// To handle this, MaxAlignment is set to the maximum alignment 259 /// needed by the objects on the current frame. If this is greater than the 260 /// native alignment maintained by the compiler, dynamic alignment code will 261 /// be needed. 262 /// 263 Align MaxAlignment; 264 265 /// Set to true if this function adjusts the stack -- e.g., 266 /// when calling another function. This is only valid during and after 267 /// prolog/epilog code insertion. 268 bool AdjustsStack = false; 269 270 /// Set to true if this function has any function calls. 271 bool HasCalls = false; 272 273 /// The frame index for the stack protector. 274 int StackProtectorIdx = -1; 275 276 struct ReturnProtector { 277 /// The register to use for return protector calculations 278 unsigned Register = 0; 279 /// Set to true if this function needs return protectors 280 bool Needed = false; 281 /// Does the return protector cookie need to be stored in frame 282 bool NeedsStore = true; 283 } RPI; 284 285 /// The frame index for the function context. Used for SjLj exceptions. 286 int FunctionContextIdx = -1; 287 288 /// This contains the size of the largest call frame if the target uses frame 289 /// setup/destroy pseudo instructions (as defined in the TargetFrameInfo 290 /// class). This information is important for frame pointer elimination. 291 /// It is only valid during and after prolog/epilog code insertion. 292 unsigned MaxCallFrameSize = ~0u; 293 294 /// The number of bytes of callee saved registers that the target wants to 295 /// report for the current function in the CodeView S_FRAMEPROC record. 296 unsigned CVBytesOfCalleeSavedRegisters = 0; 297 298 /// The prolog/epilog code inserter fills in this vector with each 299 /// callee saved register saved in either the frame or a different 300 /// register. Beyond its use by the prolog/ epilog code inserter, 301 /// this data is used for debug info and exception handling. 302 std::vector<CalleeSavedInfo> CSInfo; 303 304 /// Has CSInfo been set yet? 305 bool CSIValid = false; 306 307 /// References to frame indices which are mapped 308 /// into the local frame allocation block. <FrameIdx, LocalOffset> 309 SmallVector<std::pair<int, int64_t>, 32> LocalFrameObjects; 310 311 /// Size of the pre-allocated local frame block. 312 int64_t LocalFrameSize = 0; 313 314 /// Required alignment of the local object blob, which is the strictest 315 /// alignment of any object in it. 316 Align LocalFrameMaxAlign; 317 318 /// Whether the local object blob needs to be allocated together. If not, 319 /// PEI should ignore the isPreAllocated flags on the stack objects and 320 /// just allocate them normally. 321 bool UseLocalStackAllocationBlock = false; 322 323 /// True if the function dynamically adjusts the stack pointer through some 324 /// opaque mechanism like inline assembly or Win32 EH. 325 bool HasOpaqueSPAdjustment = false; 326 327 /// True if the function contains operations which will lower down to 328 /// instructions which manipulate the stack pointer. 329 bool HasCopyImplyingStackAdjustment = false; 330 331 /// True if the function contains a call to the llvm.vastart intrinsic. 332 bool HasVAStart = false; 333 334 /// True if this is a varargs function that contains a musttail call. 335 bool HasMustTailInVarArgFunc = false; 336 337 /// True if this function contains a tail call. If so immutable objects like 338 /// function arguments are no longer so. A tail call *can* override fixed 339 /// stack objects like arguments so we can't treat them as immutable. 340 bool HasTailCall = false; 341 342 /// Not null, if shrink-wrapping found a better place for the prologue. 343 MachineBasicBlock *Save = nullptr; 344 /// Not null, if shrink-wrapping found a better place for the epilogue. 345 MachineBasicBlock *Restore = nullptr; 346 347 /// Size of the UnsafeStack Frame 348 uint64_t UnsafeStackSize = 0; 349 350 public: MachineFrameInfo(Align StackAlignment,bool StackRealignable,bool ForcedRealign)351 explicit MachineFrameInfo(Align StackAlignment, bool StackRealignable, 352 bool ForcedRealign) 353 : StackAlignment(StackAlignment), 354 StackRealignable(StackRealignable), ForcedRealign(ForcedRealign) {} 355 356 MachineFrameInfo(const MachineFrameInfo &) = delete; 357 358 /// Return true if there are any stack objects in this function. hasStackObjects()359 bool hasStackObjects() const { return !Objects.empty(); } 360 361 /// This method may be called any time after instruction 362 /// selection is complete to determine if the stack frame for this function 363 /// contains any variable sized objects. hasVarSizedObjects()364 bool hasVarSizedObjects() const { return HasVarSizedObjects; } 365 366 /// Return the index for the stack protector object. getStackProtectorIndex()367 int getStackProtectorIndex() const { return StackProtectorIdx; } setStackProtectorIndex(int I)368 void setStackProtectorIndex(int I) { StackProtectorIdx = I; } hasStackProtectorIndex()369 bool hasStackProtectorIndex() const { return StackProtectorIdx != -1; } 370 371 /// Get / Set return protector calculation register getReturnProtectorRegister()372 unsigned getReturnProtectorRegister() const { return RPI.Register; } setReturnProtectorRegister(unsigned I)373 void setReturnProtectorRegister(unsigned I) { RPI.Register = I; } hasReturnProtectorRegister()374 bool hasReturnProtectorRegister() const { return RPI.Register != 0; } 375 /// Get / Set if this frame needs a return protector setReturnProtectorNeeded(bool I)376 void setReturnProtectorNeeded(bool I) { RPI.Needed = I; } getReturnProtectorNeeded()377 bool getReturnProtectorNeeded() const { return RPI.Needed; } 378 /// Get / Set if the return protector cookie needs to be stored in frame setReturnProtectorNeedsStore(bool I)379 void setReturnProtectorNeedsStore(bool I) { RPI.NeedsStore = I; } getReturnProtectorNeedsStore()380 bool getReturnProtectorNeedsStore() const { return RPI.NeedsStore; } 381 382 /// Return the index for the function context object. 383 /// This object is used for SjLj exceptions. getFunctionContextIndex()384 int getFunctionContextIndex() const { return FunctionContextIdx; } setFunctionContextIndex(int I)385 void setFunctionContextIndex(int I) { FunctionContextIdx = I; } hasFunctionContextIndex()386 bool hasFunctionContextIndex() const { return FunctionContextIdx != -1; } 387 388 /// This method may be called any time after instruction 389 /// selection is complete to determine if there is a call to 390 /// \@llvm.frameaddress in this function. isFrameAddressTaken()391 bool isFrameAddressTaken() const { return FrameAddressTaken; } setFrameAddressIsTaken(bool T)392 void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; } 393 394 /// This method may be called any time after 395 /// instruction selection is complete to determine if there is a call to 396 /// \@llvm.returnaddress in this function. isReturnAddressTaken()397 bool isReturnAddressTaken() const { return ReturnAddressTaken; } setReturnAddressIsTaken(bool s)398 void setReturnAddressIsTaken(bool s) { ReturnAddressTaken = s; } 399 400 /// This method may be called any time after instruction 401 /// selection is complete to determine if there is a call to builtin 402 /// \@llvm.experimental.stackmap. hasStackMap()403 bool hasStackMap() const { return HasStackMap; } 404 void setHasStackMap(bool s = true) { HasStackMap = s; } 405 406 /// This method may be called any time after instruction 407 /// selection is complete to determine if there is a call to builtin 408 /// \@llvm.experimental.patchpoint. hasPatchPoint()409 bool hasPatchPoint() const { return HasPatchPoint; } 410 void setHasPatchPoint(bool s = true) { HasPatchPoint = s; } 411 412 /// Return true if this function requires a split stack prolog, even if it 413 /// uses no stack space. This is only meaningful for functions where 414 /// MachineFunction::shouldSplitStack() returns true. 415 // 416 // For non-leaf functions we have to allow for the possibility that the call 417 // is to a non-split function, as in PR37807. This function could also take 418 // the address of a non-split function. When the linker tries to adjust its 419 // non-existent prologue, it would fail with an error. Mark the object file so 420 // that such failures are not errors. See this Go language bug-report 421 // https://go-review.googlesource.com/c/go/+/148819/ needsSplitStackProlog()422 bool needsSplitStackProlog() const { 423 return getStackSize() != 0 || hasTailCall(); 424 } 425 426 /// Return the minimum frame object index. getObjectIndexBegin()427 int getObjectIndexBegin() const { return -NumFixedObjects; } 428 429 /// Return one past the maximum frame object index. getObjectIndexEnd()430 int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; } 431 432 /// Return the number of fixed objects. getNumFixedObjects()433 unsigned getNumFixedObjects() const { return NumFixedObjects; } 434 435 /// Return the number of objects. getNumObjects()436 unsigned getNumObjects() const { return Objects.size(); } 437 438 /// Map a frame index into the local object block mapLocalFrameObject(int ObjectIndex,int64_t Offset)439 void mapLocalFrameObject(int ObjectIndex, int64_t Offset) { 440 LocalFrameObjects.push_back(std::pair<int, int64_t>(ObjectIndex, Offset)); 441 Objects[ObjectIndex + NumFixedObjects].PreAllocated = true; 442 } 443 444 /// Get the local offset mapping for a for an object. getLocalFrameObjectMap(int i)445 std::pair<int, int64_t> getLocalFrameObjectMap(int i) const { 446 assert (i >= 0 && (unsigned)i < LocalFrameObjects.size() && 447 "Invalid local object reference!"); 448 return LocalFrameObjects[i]; 449 } 450 451 /// Return the number of objects allocated into the local object block. getLocalFrameObjectCount()452 int64_t getLocalFrameObjectCount() const { return LocalFrameObjects.size(); } 453 454 /// Set the size of the local object blob. setLocalFrameSize(int64_t sz)455 void setLocalFrameSize(int64_t sz) { LocalFrameSize = sz; } 456 457 /// Get the size of the local object blob. getLocalFrameSize()458 int64_t getLocalFrameSize() const { return LocalFrameSize; } 459 460 /// Required alignment of the local object blob, 461 /// which is the strictest alignment of any object in it. setLocalFrameMaxAlign(Align Alignment)462 void setLocalFrameMaxAlign(Align Alignment) { 463 LocalFrameMaxAlign = Alignment; 464 } 465 466 /// Return the required alignment of the local object blob. getLocalFrameMaxAlign()467 Align getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; } 468 469 /// Get whether the local allocation blob should be allocated together or 470 /// let PEI allocate the locals in it directly. getUseLocalStackAllocationBlock()471 bool getUseLocalStackAllocationBlock() const { 472 return UseLocalStackAllocationBlock; 473 } 474 475 /// setUseLocalStackAllocationBlock - Set whether the local allocation blob 476 /// should be allocated together or let PEI allocate the locals in it 477 /// directly. setUseLocalStackAllocationBlock(bool v)478 void setUseLocalStackAllocationBlock(bool v) { 479 UseLocalStackAllocationBlock = v; 480 } 481 482 /// Return true if the object was pre-allocated into the local block. isObjectPreAllocated(int ObjectIdx)483 bool isObjectPreAllocated(int ObjectIdx) const { 484 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 485 "Invalid Object Idx!"); 486 return Objects[ObjectIdx+NumFixedObjects].PreAllocated; 487 } 488 489 /// Return the size of the specified object. getObjectSize(int ObjectIdx)490 int64_t getObjectSize(int ObjectIdx) const { 491 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 492 "Invalid Object Idx!"); 493 return Objects[ObjectIdx+NumFixedObjects].Size; 494 } 495 496 /// Change the size of the specified stack object. setObjectSize(int ObjectIdx,int64_t Size)497 void setObjectSize(int ObjectIdx, int64_t Size) { 498 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 499 "Invalid Object Idx!"); 500 Objects[ObjectIdx+NumFixedObjects].Size = Size; 501 } 502 503 /// Return the alignment of the specified stack object. getObjectAlign(int ObjectIdx)504 Align getObjectAlign(int ObjectIdx) const { 505 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() && 506 "Invalid Object Idx!"); 507 return Objects[ObjectIdx + NumFixedObjects].Alignment; 508 } 509 510 /// Should this stack ID be considered in MaxAlignment. contributesToMaxAlignment(uint8_t StackID)511 bool contributesToMaxAlignment(uint8_t StackID) { 512 return StackID == TargetStackID::Default || 513 StackID == TargetStackID::ScalableVector; 514 } 515 516 /// setObjectAlignment - Change the alignment of the specified stack object. setObjectAlignment(int ObjectIdx,Align Alignment)517 void setObjectAlignment(int ObjectIdx, Align Alignment) { 518 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() && 519 "Invalid Object Idx!"); 520 Objects[ObjectIdx + NumFixedObjects].Alignment = Alignment; 521 522 // Only ensure max alignment for the default and scalable vector stack. 523 uint8_t StackID = getStackID(ObjectIdx); 524 if (contributesToMaxAlignment(StackID)) 525 ensureMaxAlignment(Alignment); 526 } 527 528 /// Return the underlying Alloca of the specified 529 /// stack object if it exists. Returns 0 if none exists. getObjectAllocation(int ObjectIdx)530 const AllocaInst* getObjectAllocation(int ObjectIdx) const { 531 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 532 "Invalid Object Idx!"); 533 return Objects[ObjectIdx+NumFixedObjects].Alloca; 534 } 535 536 /// Remove the underlying Alloca of the specified stack object if it 537 /// exists. This generally should not be used and is for reduction tooling. clearObjectAllocation(int ObjectIdx)538 void clearObjectAllocation(int ObjectIdx) { 539 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() && 540 "Invalid Object Idx!"); 541 Objects[ObjectIdx + NumFixedObjects].Alloca = nullptr; 542 } 543 544 /// Return the assigned stack offset of the specified object 545 /// from the incoming stack pointer. getObjectOffset(int ObjectIdx)546 int64_t getObjectOffset(int ObjectIdx) const { 547 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 548 "Invalid Object Idx!"); 549 assert(!isDeadObjectIndex(ObjectIdx) && 550 "Getting frame offset for a dead object?"); 551 return Objects[ObjectIdx+NumFixedObjects].SPOffset; 552 } 553 isObjectZExt(int ObjectIdx)554 bool isObjectZExt(int ObjectIdx) const { 555 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 556 "Invalid Object Idx!"); 557 return Objects[ObjectIdx+NumFixedObjects].isZExt; 558 } 559 setObjectZExt(int ObjectIdx,bool IsZExt)560 void setObjectZExt(int ObjectIdx, bool IsZExt) { 561 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 562 "Invalid Object Idx!"); 563 Objects[ObjectIdx+NumFixedObjects].isZExt = IsZExt; 564 } 565 isObjectSExt(int ObjectIdx)566 bool isObjectSExt(int ObjectIdx) const { 567 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 568 "Invalid Object Idx!"); 569 return Objects[ObjectIdx+NumFixedObjects].isSExt; 570 } 571 setObjectSExt(int ObjectIdx,bool IsSExt)572 void setObjectSExt(int ObjectIdx, bool IsSExt) { 573 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 574 "Invalid Object Idx!"); 575 Objects[ObjectIdx+NumFixedObjects].isSExt = IsSExt; 576 } 577 578 /// Set the stack frame offset of the specified object. The 579 /// offset is relative to the stack pointer on entry to the function. setObjectOffset(int ObjectIdx,int64_t SPOffset)580 void setObjectOffset(int ObjectIdx, int64_t SPOffset) { 581 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 582 "Invalid Object Idx!"); 583 assert(!isDeadObjectIndex(ObjectIdx) && 584 "Setting frame offset for a dead object?"); 585 Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset; 586 } 587 getObjectSSPLayout(int ObjectIdx)588 SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const { 589 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 590 "Invalid Object Idx!"); 591 return (SSPLayoutKind)Objects[ObjectIdx+NumFixedObjects].SSPLayout; 592 } 593 setObjectSSPLayout(int ObjectIdx,SSPLayoutKind Kind)594 void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind) { 595 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 596 "Invalid Object Idx!"); 597 assert(!isDeadObjectIndex(ObjectIdx) && 598 "Setting SSP layout for a dead object?"); 599 Objects[ObjectIdx+NumFixedObjects].SSPLayout = Kind; 600 } 601 602 /// Return the number of bytes that must be allocated to hold 603 /// all of the fixed size frame objects. This is only valid after 604 /// Prolog/Epilog code insertion has finalized the stack frame layout. getStackSize()605 uint64_t getStackSize() const { return StackSize; } 606 607 /// Set the size of the stack. setStackSize(uint64_t Size)608 void setStackSize(uint64_t Size) { StackSize = Size; } 609 610 /// Estimate and return the size of the stack frame. 611 uint64_t estimateStackSize(const MachineFunction &MF) const; 612 613 /// Return the correction for frame offsets. getOffsetAdjustment()614 int getOffsetAdjustment() const { return OffsetAdjustment; } 615 616 /// Set the correction for frame offsets. setOffsetAdjustment(int Adj)617 void setOffsetAdjustment(int Adj) { OffsetAdjustment = Adj; } 618 619 /// Return the alignment in bytes that this function must be aligned to, 620 /// which is greater than the default stack alignment provided by the target. getMaxAlign()621 Align getMaxAlign() const { return MaxAlignment; } 622 623 /// Make sure the function is at least Align bytes aligned. 624 void ensureMaxAlignment(Align Alignment); 625 626 /// Return true if this function adjusts the stack -- e.g., 627 /// when calling another function. This is only valid during and after 628 /// prolog/epilog code insertion. adjustsStack()629 bool adjustsStack() const { return AdjustsStack; } setAdjustsStack(bool V)630 void setAdjustsStack(bool V) { AdjustsStack = V; } 631 632 /// Return true if the current function has any function calls. hasCalls()633 bool hasCalls() const { return HasCalls; } setHasCalls(bool V)634 void setHasCalls(bool V) { HasCalls = V; } 635 636 /// Returns true if the function contains opaque dynamic stack adjustments. hasOpaqueSPAdjustment()637 bool hasOpaqueSPAdjustment() const { return HasOpaqueSPAdjustment; } setHasOpaqueSPAdjustment(bool B)638 void setHasOpaqueSPAdjustment(bool B) { HasOpaqueSPAdjustment = B; } 639 640 /// Returns true if the function contains operations which will lower down to 641 /// instructions which manipulate the stack pointer. hasCopyImplyingStackAdjustment()642 bool hasCopyImplyingStackAdjustment() const { 643 return HasCopyImplyingStackAdjustment; 644 } setHasCopyImplyingStackAdjustment(bool B)645 void setHasCopyImplyingStackAdjustment(bool B) { 646 HasCopyImplyingStackAdjustment = B; 647 } 648 649 /// Returns true if the function calls the llvm.va_start intrinsic. hasVAStart()650 bool hasVAStart() const { return HasVAStart; } setHasVAStart(bool B)651 void setHasVAStart(bool B) { HasVAStart = B; } 652 653 /// Returns true if the function is variadic and contains a musttail call. hasMustTailInVarArgFunc()654 bool hasMustTailInVarArgFunc() const { return HasMustTailInVarArgFunc; } setHasMustTailInVarArgFunc(bool B)655 void setHasMustTailInVarArgFunc(bool B) { HasMustTailInVarArgFunc = B; } 656 657 /// Returns true if the function contains a tail call. hasTailCall()658 bool hasTailCall() const { return HasTailCall; } 659 void setHasTailCall(bool V = true) { HasTailCall = V; } 660 661 /// Computes the maximum size of a callframe and the AdjustsStack property. 662 /// This only works for targets defining 663 /// TargetInstrInfo::getCallFrameSetupOpcode(), getCallFrameDestroyOpcode(), 664 /// and getFrameSize(). 665 /// This is usually computed by the prologue epilogue inserter but some 666 /// targets may call this to compute it earlier. 667 void computeMaxCallFrameSize(const MachineFunction &MF); 668 669 /// Return the maximum size of a call frame that must be 670 /// allocated for an outgoing function call. This is only available if 671 /// CallFrameSetup/Destroy pseudo instructions are used by the target, and 672 /// then only during or after prolog/epilog code insertion. 673 /// getMaxCallFrameSize()674 unsigned getMaxCallFrameSize() const { 675 // TODO: Enable this assert when targets are fixed. 676 //assert(isMaxCallFrameSizeComputed() && "MaxCallFrameSize not computed yet"); 677 if (!isMaxCallFrameSizeComputed()) 678 return 0; 679 return MaxCallFrameSize; 680 } isMaxCallFrameSizeComputed()681 bool isMaxCallFrameSizeComputed() const { 682 return MaxCallFrameSize != ~0u; 683 } setMaxCallFrameSize(unsigned S)684 void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; } 685 686 /// Returns how many bytes of callee-saved registers the target pushed in the 687 /// prologue. Only used for debug info. getCVBytesOfCalleeSavedRegisters()688 unsigned getCVBytesOfCalleeSavedRegisters() const { 689 return CVBytesOfCalleeSavedRegisters; 690 } setCVBytesOfCalleeSavedRegisters(unsigned S)691 void setCVBytesOfCalleeSavedRegisters(unsigned S) { 692 CVBytesOfCalleeSavedRegisters = S; 693 } 694 695 /// Create a new object at a fixed location on the stack. 696 /// All fixed objects should be created before other objects are created for 697 /// efficiency. By default, fixed objects are not pointed to by LLVM IR 698 /// values. This returns an index with a negative value. 699 int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, 700 bool isAliased = false); 701 702 /// Create a spill slot at a fixed location on the stack. 703 /// Returns an index with a negative value. 704 int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset, 705 bool IsImmutable = false); 706 707 /// Returns true if the specified index corresponds to a fixed stack object. isFixedObjectIndex(int ObjectIdx)708 bool isFixedObjectIndex(int ObjectIdx) const { 709 return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects); 710 } 711 712 /// Returns true if the specified index corresponds 713 /// to an object that might be pointed to by an LLVM IR value. isAliasedObjectIndex(int ObjectIdx)714 bool isAliasedObjectIndex(int ObjectIdx) const { 715 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 716 "Invalid Object Idx!"); 717 return Objects[ObjectIdx+NumFixedObjects].isAliased; 718 } 719 720 /// Returns true if the specified index corresponds to an immutable object. isImmutableObjectIndex(int ObjectIdx)721 bool isImmutableObjectIndex(int ObjectIdx) const { 722 // Tail calling functions can clobber their function arguments. 723 if (HasTailCall) 724 return false; 725 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 726 "Invalid Object Idx!"); 727 return Objects[ObjectIdx+NumFixedObjects].isImmutable; 728 } 729 730 /// Marks the immutability of an object. setIsImmutableObjectIndex(int ObjectIdx,bool IsImmutable)731 void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable) { 732 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 733 "Invalid Object Idx!"); 734 Objects[ObjectIdx+NumFixedObjects].isImmutable = IsImmutable; 735 } 736 737 /// Returns true if the specified index corresponds to a spill slot. isSpillSlotObjectIndex(int ObjectIdx)738 bool isSpillSlotObjectIndex(int ObjectIdx) const { 739 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 740 "Invalid Object Idx!"); 741 return Objects[ObjectIdx+NumFixedObjects].isSpillSlot; 742 } 743 isStatepointSpillSlotObjectIndex(int ObjectIdx)744 bool isStatepointSpillSlotObjectIndex(int ObjectIdx) const { 745 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 746 "Invalid Object Idx!"); 747 return Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot; 748 } 749 750 /// \see StackID getStackID(int ObjectIdx)751 uint8_t getStackID(int ObjectIdx) const { 752 return Objects[ObjectIdx+NumFixedObjects].StackID; 753 } 754 755 /// \see StackID setStackID(int ObjectIdx,uint8_t ID)756 void setStackID(int ObjectIdx, uint8_t ID) { 757 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 758 "Invalid Object Idx!"); 759 Objects[ObjectIdx+NumFixedObjects].StackID = ID; 760 // If ID > 0, MaxAlignment may now be overly conservative. 761 // If ID == 0, MaxAlignment will need to be updated separately. 762 } 763 764 /// Returns true if the specified index corresponds to a dead object. isDeadObjectIndex(int ObjectIdx)765 bool isDeadObjectIndex(int ObjectIdx) const { 766 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 767 "Invalid Object Idx!"); 768 return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL; 769 } 770 771 /// Returns true if the specified index corresponds to a variable sized 772 /// object. isVariableSizedObjectIndex(int ObjectIdx)773 bool isVariableSizedObjectIndex(int ObjectIdx) const { 774 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() && 775 "Invalid Object Idx!"); 776 return Objects[ObjectIdx + NumFixedObjects].Size == 0; 777 } 778 markAsStatepointSpillSlotObjectIndex(int ObjectIdx)779 void markAsStatepointSpillSlotObjectIndex(int ObjectIdx) { 780 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() && 781 "Invalid Object Idx!"); 782 Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot = true; 783 assert(isStatepointSpillSlotObjectIndex(ObjectIdx) && "inconsistent"); 784 } 785 786 /// Create a new statically sized stack object, returning 787 /// a nonnegative identifier to represent it. 788 int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, 789 const AllocaInst *Alloca = nullptr, uint8_t ID = 0); 790 791 /// Create a new statically sized stack object that represents a spill slot, 792 /// returning a nonnegative identifier to represent it. 793 int CreateSpillStackObject(uint64_t Size, Align Alignment); 794 795 /// Remove or mark dead a statically sized stack object. RemoveStackObject(int ObjectIdx)796 void RemoveStackObject(int ObjectIdx) { 797 // Mark it dead. 798 Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL; 799 } 800 801 /// Notify the MachineFrameInfo object that a variable sized object has been 802 /// created. This must be created whenever a variable sized object is 803 /// created, whether or not the index returned is actually used. 804 int CreateVariableSizedObject(Align Alignment, const AllocaInst *Alloca); 805 806 /// Returns a reference to call saved info vector for the current function. getCalleeSavedInfo()807 const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const { 808 return CSInfo; 809 } 810 /// \copydoc getCalleeSavedInfo() getCalleeSavedInfo()811 std::vector<CalleeSavedInfo> &getCalleeSavedInfo() { return CSInfo; } 812 813 /// Used by prolog/epilog inserter to set the function's callee saved 814 /// information. setCalleeSavedInfo(std::vector<CalleeSavedInfo> CSI)815 void setCalleeSavedInfo(std::vector<CalleeSavedInfo> CSI) { 816 CSInfo = std::move(CSI); 817 } 818 819 /// Has the callee saved info been calculated yet? isCalleeSavedInfoValid()820 bool isCalleeSavedInfoValid() const { return CSIValid; } 821 setCalleeSavedInfoValid(bool v)822 void setCalleeSavedInfoValid(bool v) { CSIValid = v; } 823 getSavePoint()824 MachineBasicBlock *getSavePoint() const { return Save; } setSavePoint(MachineBasicBlock * NewSave)825 void setSavePoint(MachineBasicBlock *NewSave) { Save = NewSave; } getRestorePoint()826 MachineBasicBlock *getRestorePoint() const { return Restore; } setRestorePoint(MachineBasicBlock * NewRestore)827 void setRestorePoint(MachineBasicBlock *NewRestore) { Restore = NewRestore; } 828 getUnsafeStackSize()829 uint64_t getUnsafeStackSize() const { return UnsafeStackSize; } setUnsafeStackSize(uint64_t Size)830 void setUnsafeStackSize(uint64_t Size) { UnsafeStackSize = Size; } 831 832 /// Return a set of physical registers that are pristine. 833 /// 834 /// Pristine registers hold a value that is useless to the current function, 835 /// but that must be preserved - they are callee saved registers that are not 836 /// saved. 837 /// 838 /// Before the PrologueEpilogueInserter has placed the CSR spill code, this 839 /// method always returns an empty set. 840 BitVector getPristineRegs(const MachineFunction &MF) const; 841 842 /// Used by the MachineFunction printer to print information about 843 /// stack objects. Implemented in MachineFunction.cpp. 844 void print(const MachineFunction &MF, raw_ostream &OS) const; 845 846 /// dump - Print the function to stderr. 847 void dump(const MachineFunction &MF) const; 848 }; 849 850 } // End llvm namespace 851 852 #endif 853