1 //===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The file defines the MachineFrameInfo class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEFRAMEINFO_H
15 #define LLVM_CODEGEN_MACHINEFRAMEINFO_H
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Support/DataTypes.h"
24 class MachineFunction;
25 class MachineBasicBlock;
29 /// The CalleeSavedInfo class tracks the information need to locate where a
30 /// callee saved register is in the current frame.
31 /// Callee saved reg can also be saved to a different register rather than
32 /// on the stack by setting DstReg instead of FrameIdx.
33 class CalleeSavedInfo {
39 /// Flag indicating whether the register is actually restored in the epilog.
40 /// In most cases, if a register is saved, it is also restored. There are
41 /// some situations, though, when this is not the case. For example, the
42 /// LR register on ARM is usually saved, but on exit from the function its
43 /// saved value may be loaded directly into PC. Since liveness tracking of
44 /// physical registers treats callee-saved registers are live outside of
45 /// the function, LR would be treated as live-on-exit, even though in these
46 /// scenarios it is not. This flag is added to indicate that the saved
47 /// register described by this object is not restored in the epilog.
48 /// The long-term solution is to model the liveness of callee-saved registers
49 /// by implicit uses on the return instructions, however, the required
50 /// changes in the ARM backend would be quite extensive.
52 /// Flag indicating whether the register is spilled to stack or another
57 explicit CalleeSavedInfo(unsigned R, int FI = 0)
58 : Reg(R), FrameIdx(FI), Restored(true), SpilledToReg(false) {}
61 unsigned getReg() const { return Reg; }
62 int getFrameIdx() const { return FrameIdx; }
63 unsigned getDstReg() const { return DstReg; }
64 void setFrameIdx(int FI) {
68 void setDstReg(unsigned SpillReg) {
72 bool isRestored() const { return Restored; }
73 void setRestored(bool R) { Restored = R; }
74 bool isSpilledToReg() const { return SpilledToReg; }
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.
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
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
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
105 /// Abstract Stack Frame Information
106 class MachineFrameInfo {
108 /// Stack Smashing Protection (SSP) rules require that vulnerable stack
109 /// allocations are located close the stack protector.
111 SSPLK_None, ///< Did not trigger a stack protector. No effect on data
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.
122 // Represent a single object allocated on the stack.
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.
128 // The size of this object on the stack. 0 means a variable sized object,
129 // ~0ULL means a dead object.
132 // The required alignment of this stack slot.
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.
140 // If true the stack object is used as spill slot. It
141 // cannot alias any other memory objects.
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;
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.
158 /// It is assumed a target uses consecutive, increasing stack IDs starting
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;
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;
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).
176 /// If true, the object has been zero-extended.
179 /// If true, the object has been zero-extended.
184 StackObject(uint64_t Size, unsigned Alignment, int64_t SPOffset,
185 bool IsImmutable, bool IsSpillSlot, const AllocaInst *Alloca,
186 bool IsAliased, uint8_t StackID = 0)
187 : SPOffset(SPOffset), Size(Size), Alignment(Alignment),
188 isImmutable(IsImmutable), isSpillSlot(IsSpillSlot),
189 StackID(StackID), Alloca(Alloca), isAliased(IsAliased),
190 SSPLayout(SSPLK_None) {}
193 /// The alignment of the stack.
194 unsigned StackAlignment;
196 /// Can the stack be realigned. This can be false if the target does not
197 /// support stack realignment, or if the user asks us not to realign the
198 /// stack. In this situation, overaligned allocas are all treated as dynamic
199 /// allocations and the target must handle them as part of DYNAMIC_STACKALLOC
200 /// lowering. All non-alloca stack objects have their alignment clamped to the
201 /// base ABI stack alignment.
202 /// FIXME: There is room for improvement in this case, in terms of
203 /// grouping overaligned allocas into a "secondary stack frame" and
204 /// then only use a single alloca to allocate this frame and only a
205 /// single virtual register to access it. Currently, without such an
206 /// optimization, each such alloca gets its own dynamic realignment.
207 bool StackRealignable;
209 /// Whether the function has the \c alignstack attribute.
212 /// The list of stack objects allocated.
213 std::vector<StackObject> Objects;
215 /// This contains the number of fixed objects contained on
216 /// the stack. Because fixed objects are stored at a negative index in the
217 /// Objects list, this is also the index to the 0th object in the list.
218 unsigned NumFixedObjects = 0;
220 /// This boolean keeps track of whether any variable
221 /// sized objects have been allocated yet.
222 bool HasVarSizedObjects = false;
224 /// This boolean keeps track of whether there is a call
225 /// to builtin \@llvm.frameaddress.
226 bool FrameAddressTaken = false;
228 /// This boolean keeps track of whether there is a call
229 /// to builtin \@llvm.returnaddress.
230 bool ReturnAddressTaken = false;
232 /// This boolean keeps track of whether there is a call
233 /// to builtin \@llvm.experimental.stackmap.
234 bool HasStackMap = false;
236 /// This boolean keeps track of whether there is a call
237 /// to builtin \@llvm.experimental.patchpoint.
238 bool HasPatchPoint = false;
240 /// The prolog/epilog code inserter calculates the final stack
241 /// offsets for all of the fixed size objects, updating the Objects list
242 /// above. It then updates StackSize to contain the number of bytes that need
243 /// to be allocated on entry to the function.
244 uint64_t StackSize = 0;
246 /// The amount that a frame offset needs to be adjusted to
247 /// have the actual offset from the stack/frame pointer. The exact usage of
248 /// this is target-dependent, but it is typically used to adjust between
249 /// SP-relative and FP-relative offsets. E.G., if objects are accessed via
250 /// SP then OffsetAdjustment is zero; if FP is used, OffsetAdjustment is set
251 /// to the distance between the initial SP and the value in FP. For many
252 /// targets, this value is only used when generating debug info (via
253 /// TargetRegisterInfo::getFrameIndexReference); when generating code, the
254 /// corresponding adjustments are performed directly.
255 int OffsetAdjustment = 0;
257 /// The prolog/epilog code inserter may process objects that require greater
258 /// alignment than the default alignment the target provides.
259 /// To handle this, MaxAlignment is set to the maximum alignment
260 /// needed by the objects on the current frame. If this is greater than the
261 /// native alignment maintained by the compiler, dynamic alignment code will
264 unsigned MaxAlignment = 0;
266 /// Set to true if this function adjusts the stack -- e.g.,
267 /// when calling another function. This is only valid during and after
268 /// prolog/epilog code insertion.
269 bool AdjustsStack = false;
271 /// Set to true if this function has any function calls.
272 bool HasCalls = false;
274 /// The frame index for the stack protector.
275 int StackProtectorIdx = -1;
277 /// The frame index for the function context. Used for SjLj exceptions.
278 int FunctionContextIdx = -1;
280 /// This contains the size of the largest call frame if the target uses frame
281 /// setup/destroy pseudo instructions (as defined in the TargetFrameInfo
282 /// class). This information is important for frame pointer elimination.
283 /// It is only valid during and after prolog/epilog code insertion.
284 unsigned MaxCallFrameSize = ~0u;
286 /// The number of bytes of callee saved registers that the target wants to
287 /// report for the current function in the CodeView S_FRAMEPROC record.
288 unsigned CVBytesOfCalleeSavedRegisters = 0;
290 /// The prolog/epilog code inserter fills in this vector with each
291 /// callee saved register saved in either the frame or a different
292 /// register. Beyond its use by the prolog/ epilog code inserter,
293 /// this data is used for debug info and exception handling.
294 std::vector<CalleeSavedInfo> CSInfo;
296 /// Has CSInfo been set yet?
297 bool CSIValid = false;
299 /// References to frame indices which are mapped
300 /// into the local frame allocation block. <FrameIdx, LocalOffset>
301 SmallVector<std::pair<int, int64_t>, 32> LocalFrameObjects;
303 /// Size of the pre-allocated local frame block.
304 int64_t LocalFrameSize = 0;
306 /// Required alignment of the local object blob, which is the strictest
307 /// alignment of any object in it.
308 unsigned LocalFrameMaxAlign = 0;
310 /// Whether the local object blob needs to be allocated together. If not,
311 /// PEI should ignore the isPreAllocated flags on the stack objects and
312 /// just allocate them normally.
313 bool UseLocalStackAllocationBlock = false;
315 /// True if the function dynamically adjusts the stack pointer through some
316 /// opaque mechanism like inline assembly or Win32 EH.
317 bool HasOpaqueSPAdjustment = false;
319 /// True if the function contains operations which will lower down to
320 /// instructions which manipulate the stack pointer.
321 bool HasCopyImplyingStackAdjustment = false;
323 /// True if the function contains a call to the llvm.vastart intrinsic.
324 bool HasVAStart = false;
326 /// True if this is a varargs function that contains a musttail call.
327 bool HasMustTailInVarArgFunc = false;
329 /// True if this function contains a tail call. If so immutable objects like
330 /// function arguments are no longer so. A tail call *can* override fixed
331 /// stack objects like arguments so we can't treat them as immutable.
332 bool HasTailCall = false;
334 /// Not null, if shrink-wrapping found a better place for the prologue.
335 MachineBasicBlock *Save = nullptr;
336 /// Not null, if shrink-wrapping found a better place for the epilogue.
337 MachineBasicBlock *Restore = nullptr;
340 explicit MachineFrameInfo(unsigned StackAlignment, bool StackRealignable,
342 : StackAlignment(StackAlignment), StackRealignable(StackRealignable),
343 ForcedRealign(ForcedRealign) {}
345 /// Return true if there are any stack objects in this function.
346 bool hasStackObjects() const { return !Objects.empty(); }
348 /// This method may be called any time after instruction
349 /// selection is complete to determine if the stack frame for this function
350 /// contains any variable sized objects.
351 bool hasVarSizedObjects() const { return HasVarSizedObjects; }
353 /// Return the index for the stack protector object.
354 int getStackProtectorIndex() const { return StackProtectorIdx; }
355 void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
356 bool hasStackProtectorIndex() const { return StackProtectorIdx != -1; }
358 /// Return the index for the function context object.
359 /// This object is used for SjLj exceptions.
360 int getFunctionContextIndex() const { return FunctionContextIdx; }
361 void setFunctionContextIndex(int I) { FunctionContextIdx = I; }
363 /// This method may be called any time after instruction
364 /// selection is complete to determine if there is a call to
365 /// \@llvm.frameaddress in this function.
366 bool isFrameAddressTaken() const { return FrameAddressTaken; }
367 void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; }
369 /// This method may be called any time after
370 /// instruction selection is complete to determine if there is a call to
371 /// \@llvm.returnaddress in this function.
372 bool isReturnAddressTaken() const { return ReturnAddressTaken; }
373 void setReturnAddressIsTaken(bool s) { ReturnAddressTaken = s; }
375 /// This method may be called any time after instruction
376 /// selection is complete to determine if there is a call to builtin
377 /// \@llvm.experimental.stackmap.
378 bool hasStackMap() const { return HasStackMap; }
379 void setHasStackMap(bool s = true) { HasStackMap = s; }
381 /// This method may be called any time after instruction
382 /// selection is complete to determine if there is a call to builtin
383 /// \@llvm.experimental.patchpoint.
384 bool hasPatchPoint() const { return HasPatchPoint; }
385 void setHasPatchPoint(bool s = true) { HasPatchPoint = s; }
387 /// Return the minimum frame object index.
388 int getObjectIndexBegin() const { return -NumFixedObjects; }
390 /// Return one past the maximum frame object index.
391 int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
393 /// Return the number of fixed objects.
394 unsigned getNumFixedObjects() const { return NumFixedObjects; }
396 /// Return the number of objects.
397 unsigned getNumObjects() const { return Objects.size(); }
399 /// Map a frame index into the local object block
400 void mapLocalFrameObject(int ObjectIndex, int64_t Offset) {
401 LocalFrameObjects.push_back(std::pair<int, int64_t>(ObjectIndex, Offset));
402 Objects[ObjectIndex + NumFixedObjects].PreAllocated = true;
405 /// Get the local offset mapping for a for an object.
406 std::pair<int, int64_t> getLocalFrameObjectMap(int i) const {
407 assert (i >= 0 && (unsigned)i < LocalFrameObjects.size() &&
408 "Invalid local object reference!");
409 return LocalFrameObjects[i];
412 /// Return the number of objects allocated into the local object block.
413 int64_t getLocalFrameObjectCount() const { return LocalFrameObjects.size(); }
415 /// Set the size of the local object blob.
416 void setLocalFrameSize(int64_t sz) { LocalFrameSize = sz; }
418 /// Get the size of the local object blob.
419 int64_t getLocalFrameSize() const { return LocalFrameSize; }
421 /// Required alignment of the local object blob,
422 /// which is the strictest alignment of any object in it.
423 void setLocalFrameMaxAlign(unsigned Align) { LocalFrameMaxAlign = Align; }
425 /// Return the required alignment of the local object blob.
426 unsigned getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
428 /// Get whether the local allocation blob should be allocated together or
429 /// let PEI allocate the locals in it directly.
430 bool getUseLocalStackAllocationBlock() const {
431 return UseLocalStackAllocationBlock;
434 /// setUseLocalStackAllocationBlock - Set whether the local allocation blob
435 /// should be allocated together or let PEI allocate the locals in it
437 void setUseLocalStackAllocationBlock(bool v) {
438 UseLocalStackAllocationBlock = v;
441 /// Return true if the object was pre-allocated into the local block.
442 bool isObjectPreAllocated(int ObjectIdx) const {
443 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
444 "Invalid Object Idx!");
445 return Objects[ObjectIdx+NumFixedObjects].PreAllocated;
448 /// Return the size of the specified object.
449 int64_t getObjectSize(int ObjectIdx) const {
450 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
451 "Invalid Object Idx!");
452 return Objects[ObjectIdx+NumFixedObjects].Size;
455 /// Change the size of the specified stack object.
456 void setObjectSize(int ObjectIdx, int64_t Size) {
457 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
458 "Invalid Object Idx!");
459 Objects[ObjectIdx+NumFixedObjects].Size = Size;
462 /// Return the alignment of the specified stack object.
463 unsigned getObjectAlignment(int ObjectIdx) const {
464 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
465 "Invalid Object Idx!");
466 return Objects[ObjectIdx+NumFixedObjects].Alignment;
469 /// setObjectAlignment - Change the alignment of the specified stack object.
470 void setObjectAlignment(int ObjectIdx, unsigned Align) {
471 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
472 "Invalid Object Idx!");
473 Objects[ObjectIdx+NumFixedObjects].Alignment = Align;
474 ensureMaxAlignment(Align);
477 /// Return the underlying Alloca of the specified
478 /// stack object if it exists. Returns 0 if none exists.
479 const AllocaInst* getObjectAllocation(int ObjectIdx) const {
480 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
481 "Invalid Object Idx!");
482 return Objects[ObjectIdx+NumFixedObjects].Alloca;
485 /// Return the assigned stack offset of the specified object
486 /// from the incoming stack pointer.
487 int64_t getObjectOffset(int ObjectIdx) const {
488 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
489 "Invalid Object Idx!");
490 assert(!isDeadObjectIndex(ObjectIdx) &&
491 "Getting frame offset for a dead object?");
492 return Objects[ObjectIdx+NumFixedObjects].SPOffset;
495 bool isObjectZExt(int ObjectIdx) const {
496 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
497 "Invalid Object Idx!");
498 return Objects[ObjectIdx+NumFixedObjects].isZExt;
501 void setObjectZExt(int ObjectIdx, bool IsZExt) {
502 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
503 "Invalid Object Idx!");
504 Objects[ObjectIdx+NumFixedObjects].isZExt = IsZExt;
507 bool isObjectSExt(int ObjectIdx) const {
508 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
509 "Invalid Object Idx!");
510 return Objects[ObjectIdx+NumFixedObjects].isSExt;
513 void setObjectSExt(int ObjectIdx, bool IsSExt) {
514 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
515 "Invalid Object Idx!");
516 Objects[ObjectIdx+NumFixedObjects].isSExt = IsSExt;
519 /// Set the stack frame offset of the specified object. The
520 /// offset is relative to the stack pointer on entry to the function.
521 void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
522 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
523 "Invalid Object Idx!");
524 assert(!isDeadObjectIndex(ObjectIdx) &&
525 "Setting frame offset for a dead object?");
526 Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
529 SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const {
530 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
531 "Invalid Object Idx!");
532 return (SSPLayoutKind)Objects[ObjectIdx+NumFixedObjects].SSPLayout;
535 void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind) {
536 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
537 "Invalid Object Idx!");
538 assert(!isDeadObjectIndex(ObjectIdx) &&
539 "Setting SSP layout for a dead object?");
540 Objects[ObjectIdx+NumFixedObjects].SSPLayout = Kind;
543 /// Return the number of bytes that must be allocated to hold
544 /// all of the fixed size frame objects. This is only valid after
545 /// Prolog/Epilog code insertion has finalized the stack frame layout.
546 uint64_t getStackSize() const { return StackSize; }
548 /// Set the size of the stack.
549 void setStackSize(uint64_t Size) { StackSize = Size; }
551 /// Estimate and return the size of the stack frame.
552 unsigned estimateStackSize(const MachineFunction &MF) const;
554 /// Return the correction for frame offsets.
555 int getOffsetAdjustment() const { return OffsetAdjustment; }
557 /// Set the correction for frame offsets.
558 void setOffsetAdjustment(int Adj) { OffsetAdjustment = Adj; }
560 /// Return the alignment in bytes that this function must be aligned to,
561 /// which is greater than the default stack alignment provided by the target.
562 unsigned getMaxAlignment() const { return MaxAlignment; }
564 /// Make sure the function is at least Align bytes aligned.
565 void ensureMaxAlignment(unsigned Align);
567 /// Return true if this function adjusts the stack -- e.g.,
568 /// when calling another function. This is only valid during and after
569 /// prolog/epilog code insertion.
570 bool adjustsStack() const { return AdjustsStack; }
571 void setAdjustsStack(bool V) { AdjustsStack = V; }
573 /// Return true if the current function has any function calls.
574 bool hasCalls() const { return HasCalls; }
575 void setHasCalls(bool V) { HasCalls = V; }
577 /// Returns true if the function contains opaque dynamic stack adjustments.
578 bool hasOpaqueSPAdjustment() const { return HasOpaqueSPAdjustment; }
579 void setHasOpaqueSPAdjustment(bool B) { HasOpaqueSPAdjustment = B; }
581 /// Returns true if the function contains operations which will lower down to
582 /// instructions which manipulate the stack pointer.
583 bool hasCopyImplyingStackAdjustment() const {
584 return HasCopyImplyingStackAdjustment;
586 void setHasCopyImplyingStackAdjustment(bool B) {
587 HasCopyImplyingStackAdjustment = B;
590 /// Returns true if the function calls the llvm.va_start intrinsic.
591 bool hasVAStart() const { return HasVAStart; }
592 void setHasVAStart(bool B) { HasVAStart = B; }
594 /// Returns true if the function is variadic and contains a musttail call.
595 bool hasMustTailInVarArgFunc() const { return HasMustTailInVarArgFunc; }
596 void setHasMustTailInVarArgFunc(bool B) { HasMustTailInVarArgFunc = B; }
598 /// Returns true if the function contains a tail call.
599 bool hasTailCall() const { return HasTailCall; }
600 void setHasTailCall() { HasTailCall = true; }
602 /// Computes the maximum size of a callframe and the AdjustsStack property.
603 /// This only works for targets defining
604 /// TargetInstrInfo::getCallFrameSetupOpcode(), getCallFrameDestroyOpcode(),
605 /// and getFrameSize().
606 /// This is usually computed by the prologue epilogue inserter but some
607 /// targets may call this to compute it earlier.
608 void computeMaxCallFrameSize(const MachineFunction &MF);
610 /// Return the maximum size of a call frame that must be
611 /// allocated for an outgoing function call. This is only available if
612 /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
613 /// then only during or after prolog/epilog code insertion.
615 unsigned getMaxCallFrameSize() const {
616 // TODO: Enable this assert when targets are fixed.
617 //assert(isMaxCallFrameSizeComputed() && "MaxCallFrameSize not computed yet");
618 if (!isMaxCallFrameSizeComputed())
620 return MaxCallFrameSize;
622 bool isMaxCallFrameSizeComputed() const {
623 return MaxCallFrameSize != ~0u;
625 void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
627 /// Returns how many bytes of callee-saved registers the target pushed in the
628 /// prologue. Only used for debug info.
629 unsigned getCVBytesOfCalleeSavedRegisters() const {
630 return CVBytesOfCalleeSavedRegisters;
632 void setCVBytesOfCalleeSavedRegisters(unsigned S) {
633 CVBytesOfCalleeSavedRegisters = S;
636 /// Create a new object at a fixed location on the stack.
637 /// All fixed objects should be created before other objects are created for
638 /// efficiency. By default, fixed objects are not pointed to by LLVM IR
639 /// values. This returns an index with a negative value.
640 int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable,
641 bool isAliased = false);
643 /// Create a spill slot at a fixed location on the stack.
644 /// Returns an index with a negative value.
645 int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset,
646 bool IsImmutable = false);
648 /// Returns true if the specified index corresponds to a fixed stack object.
649 bool isFixedObjectIndex(int ObjectIdx) const {
650 return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
653 /// Returns true if the specified index corresponds
654 /// to an object that might be pointed to by an LLVM IR value.
655 bool isAliasedObjectIndex(int ObjectIdx) const {
656 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
657 "Invalid Object Idx!");
658 return Objects[ObjectIdx+NumFixedObjects].isAliased;
661 /// Returns true if the specified index corresponds to an immutable object.
662 bool isImmutableObjectIndex(int ObjectIdx) const {
663 // Tail calling functions can clobber their function arguments.
666 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
667 "Invalid Object Idx!");
668 return Objects[ObjectIdx+NumFixedObjects].isImmutable;
671 /// Marks the immutability of an object.
672 void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable) {
673 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
674 "Invalid Object Idx!");
675 Objects[ObjectIdx+NumFixedObjects].isImmutable = IsImmutable;
678 /// Returns true if the specified index corresponds to a spill slot.
679 bool isSpillSlotObjectIndex(int ObjectIdx) const {
680 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
681 "Invalid Object Idx!");
682 return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;
685 bool isStatepointSpillSlotObjectIndex(int ObjectIdx) const {
686 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
687 "Invalid Object Idx!");
688 return Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot;
692 uint8_t getStackID(int ObjectIdx) const {
693 return Objects[ObjectIdx+NumFixedObjects].StackID;
697 void setStackID(int ObjectIdx, uint8_t ID) {
698 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
699 "Invalid Object Idx!");
700 Objects[ObjectIdx+NumFixedObjects].StackID = ID;
703 /// Returns true if the specified index corresponds to a dead object.
704 bool isDeadObjectIndex(int ObjectIdx) const {
705 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
706 "Invalid Object Idx!");
707 return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
710 /// Returns true if the specified index corresponds to a variable sized
712 bool isVariableSizedObjectIndex(int ObjectIdx) const {
713 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
714 "Invalid Object Idx!");
715 return Objects[ObjectIdx + NumFixedObjects].Size == 0;
718 void markAsStatepointSpillSlotObjectIndex(int ObjectIdx) {
719 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
720 "Invalid Object Idx!");
721 Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot = true;
722 assert(isStatepointSpillSlotObjectIndex(ObjectIdx) && "inconsistent");
725 /// Create a new statically sized stack object, returning
726 /// a nonnegative identifier to represent it.
727 int CreateStackObject(uint64_t Size, unsigned Alignment, bool isSpillSlot,
728 const AllocaInst *Alloca = nullptr, uint8_t ID = 0);
730 /// Create a new statically sized stack object that represents a spill slot,
731 /// returning a nonnegative identifier to represent it.
732 int CreateSpillStackObject(uint64_t Size, unsigned Alignment);
734 /// Remove or mark dead a statically sized stack object.
735 void RemoveStackObject(int ObjectIdx) {
737 Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
740 /// Notify the MachineFrameInfo object that a variable sized object has been
741 /// created. This must be created whenever a variable sized object is
742 /// created, whether or not the index returned is actually used.
743 int CreateVariableSizedObject(unsigned Alignment, const AllocaInst *Alloca);
745 /// Returns a reference to call saved info vector for the current function.
746 const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
749 /// \copydoc getCalleeSavedInfo()
750 std::vector<CalleeSavedInfo> &getCalleeSavedInfo() { return CSInfo; }
752 /// Used by prolog/epilog inserter to set the function's callee saved
754 void setCalleeSavedInfo(const std::vector<CalleeSavedInfo> &CSI) {
758 /// Has the callee saved info been calculated yet?
759 bool isCalleeSavedInfoValid() const { return CSIValid; }
761 void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
763 MachineBasicBlock *getSavePoint() const { return Save; }
764 void setSavePoint(MachineBasicBlock *NewSave) { Save = NewSave; }
765 MachineBasicBlock *getRestorePoint() const { return Restore; }
766 void setRestorePoint(MachineBasicBlock *NewRestore) { Restore = NewRestore; }
768 /// Return a set of physical registers that are pristine.
770 /// Pristine registers hold a value that is useless to the current function,
771 /// but that must be preserved - they are callee saved registers that are not
774 /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
775 /// method always returns an empty set.
776 BitVector getPristineRegs(const MachineFunction &MF) const;
778 /// Used by the MachineFunction printer to print information about
779 /// stack objects. Implemented in MachineFunction.cpp.
780 void print(const MachineFunction &MF, raw_ostream &OS) const;
782 /// dump - Print the function to stderr.
783 void dump(const MachineFunction &MF) const;
786 } // End llvm namespace