LLVM 20.0.0git
MachineFrameInfo.h
Go to the documentation of this file.
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
20#include <cassert>
21#include <vector>
22
23namespace llvm {
24class raw_ostream;
25class MachineFunction;
26class MachineBasicBlock;
27class BitVector;
28class 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.
35 Register Reg;
36 union {
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
57public:
58 explicit CalleeSavedInfo(unsigned R, int FI = 0) : Reg(R), FrameIdx(FI) {}
59
60 // Accessors.
61 Register getReg() const { return Reg; }
62 int getFrameIdx() const { return FrameIdx; }
63 unsigned getDstReg() const { return DstReg; }
64 void setFrameIdx(int FI) {
65 FrameIdx = FI;
66 SpilledToReg = false;
67 }
68 void setDstReg(Register SpillReg) {
69 DstReg = SpillReg;
70 SpilledToReg = true;
71 }
72 bool isRestored() const { return Restored; }
73 void setRestored(bool R) { Restored = R; }
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
107public:
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
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
121private:
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)
187 : 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 int64_t 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 /// The frame index for the function context. Used for SjLj exceptions.
277 int FunctionContextIdx = -1;
278
279 /// This contains the size of the largest call frame if the target uses frame
280 /// setup/destroy pseudo instructions (as defined in the TargetFrameInfo
281 /// class). This information is important for frame pointer elimination.
282 /// It is only valid during and after prolog/epilog code insertion.
283 uint64_t MaxCallFrameSize = ~UINT64_C(0);
284
285 /// The number of bytes of callee saved registers that the target wants to
286 /// report for the current function in the CodeView S_FRAMEPROC record.
287 unsigned CVBytesOfCalleeSavedRegisters = 0;
288
289 /// The prolog/epilog code inserter fills in this vector with each
290 /// callee saved register saved in either the frame or a different
291 /// register. Beyond its use by the prolog/ epilog code inserter,
292 /// this data is used for debug info and exception handling.
293 std::vector<CalleeSavedInfo> CSInfo;
294
295 /// Has CSInfo been set yet?
296 bool CSIValid = false;
297
298 /// References to frame indices which are mapped
299 /// into the local frame allocation block. <FrameIdx, LocalOffset>
300 SmallVector<std::pair<int, int64_t>, 32> LocalFrameObjects;
301
302 /// Size of the pre-allocated local frame block.
303 int64_t LocalFrameSize = 0;
304
305 /// Required alignment of the local object blob, which is the strictest
306 /// alignment of any object in it.
307 Align LocalFrameMaxAlign;
308
309 /// Whether the local object blob needs to be allocated together. If not,
310 /// PEI should ignore the isPreAllocated flags on the stack objects and
311 /// just allocate them normally.
312 bool UseLocalStackAllocationBlock = false;
313
314 /// True if the function dynamically adjusts the stack pointer through some
315 /// opaque mechanism like inline assembly or Win32 EH.
316 bool HasOpaqueSPAdjustment = false;
317
318 /// True if the function contains operations which will lower down to
319 /// instructions which manipulate the stack pointer.
320 bool HasCopyImplyingStackAdjustment = false;
321
322 /// True if the function contains a call to the llvm.vastart intrinsic.
323 bool HasVAStart = false;
324
325 /// True if this is a varargs function that contains a musttail call.
326 bool HasMustTailInVarArgFunc = false;
327
328 /// True if this function contains a tail call. If so immutable objects like
329 /// function arguments are no longer so. A tail call *can* override fixed
330 /// stack objects like arguments so we can't treat them as immutable.
331 bool HasTailCall = false;
332
333 /// Not null, if shrink-wrapping found a better place for the prologue.
334 MachineBasicBlock *Save = nullptr;
335 /// Not null, if shrink-wrapping found a better place for the epilogue.
336 MachineBasicBlock *Restore = nullptr;
337
338 /// Size of the UnsafeStack Frame
339 uint64_t UnsafeStackSize = 0;
340
341public:
342 explicit MachineFrameInfo(Align StackAlignment, bool StackRealignable,
343 bool ForcedRealign)
344 : StackAlignment(StackAlignment),
345 StackRealignable(StackRealignable), ForcedRealign(ForcedRealign) {}
346
348
349 bool isStackRealignable() const { return StackRealignable; }
350
351 /// Return true if there are any stack objects in this function.
352 bool hasStackObjects() const { return !Objects.empty(); }
353
354 /// This method may be called any time after instruction
355 /// selection is complete to determine if the stack frame for this function
356 /// contains any variable sized objects.
357 bool hasVarSizedObjects() const { return HasVarSizedObjects; }
358
359 /// Return the index for the stack protector object.
360 int getStackProtectorIndex() const { return StackProtectorIdx; }
361 void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
362 bool hasStackProtectorIndex() const { return StackProtectorIdx != -1; }
363
364 /// Return the index for the function context object.
365 /// This object is used for SjLj exceptions.
366 int getFunctionContextIndex() const { return FunctionContextIdx; }
367 void setFunctionContextIndex(int I) { FunctionContextIdx = I; }
368 bool hasFunctionContextIndex() const { return FunctionContextIdx != -1; }
369
370 /// This method may be called any time after instruction
371 /// selection is complete to determine if there is a call to
372 /// \@llvm.frameaddress in this function.
373 bool isFrameAddressTaken() const { return FrameAddressTaken; }
374 void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; }
375
376 /// This method may be called any time after
377 /// instruction selection is complete to determine if there is a call to
378 /// \@llvm.returnaddress in this function.
379 bool isReturnAddressTaken() const { return ReturnAddressTaken; }
380 void setReturnAddressIsTaken(bool s) { ReturnAddressTaken = s; }
381
382 /// This method may be called any time after instruction
383 /// selection is complete to determine if there is a call to builtin
384 /// \@llvm.experimental.stackmap.
385 bool hasStackMap() const { return HasStackMap; }
386 void setHasStackMap(bool s = true) { HasStackMap = s; }
387
388 /// This method may be called any time after instruction
389 /// selection is complete to determine if there is a call to builtin
390 /// \@llvm.experimental.patchpoint.
391 bool hasPatchPoint() const { return HasPatchPoint; }
392 void setHasPatchPoint(bool s = true) { HasPatchPoint = s; }
393
394 /// Return true if this function requires a split stack prolog, even if it
395 /// uses no stack space. This is only meaningful for functions where
396 /// MachineFunction::shouldSplitStack() returns true.
397 //
398 // For non-leaf functions we have to allow for the possibility that the call
399 // is to a non-split function, as in PR37807. This function could also take
400 // the address of a non-split function. When the linker tries to adjust its
401 // non-existent prologue, it would fail with an error. Mark the object file so
402 // that such failures are not errors. See this Go language bug-report
403 // https://go-review.googlesource.com/c/go/+/148819/
405 return getStackSize() != 0 || hasTailCall();
406 }
407
408 /// Return the minimum frame object index.
409 int getObjectIndexBegin() const { return -NumFixedObjects; }
410
411 /// Return one past the maximum frame object index.
412 int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
413
414 /// Return the number of fixed objects.
415 unsigned getNumFixedObjects() const { return NumFixedObjects; }
416
417 /// Return the number of objects.
418 unsigned getNumObjects() const { return Objects.size(); }
419
420 /// Map a frame index into the local object block
421 void mapLocalFrameObject(int ObjectIndex, int64_t Offset) {
422 LocalFrameObjects.push_back(std::pair<int, int64_t>(ObjectIndex, Offset));
423 Objects[ObjectIndex + NumFixedObjects].PreAllocated = true;
424 }
425
426 /// Get the local offset mapping for a for an object.
427 std::pair<int, int64_t> getLocalFrameObjectMap(int i) const {
428 assert (i >= 0 && (unsigned)i < LocalFrameObjects.size() &&
429 "Invalid local object reference!");
430 return LocalFrameObjects[i];
431 }
432
433 /// Return the number of objects allocated into the local object block.
434 int64_t getLocalFrameObjectCount() const { return LocalFrameObjects.size(); }
435
436 /// Set the size of the local object blob.
437 void setLocalFrameSize(int64_t sz) { LocalFrameSize = sz; }
438
439 /// Get the size of the local object blob.
440 int64_t getLocalFrameSize() const { return LocalFrameSize; }
441
442 /// Required alignment of the local object blob,
443 /// which is the strictest alignment of any object in it.
444 void setLocalFrameMaxAlign(Align Alignment) {
445 LocalFrameMaxAlign = Alignment;
446 }
447
448 /// Return the required alignment of the local object blob.
449 Align getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
450
451 /// Get whether the local allocation blob should be allocated together or
452 /// let PEI allocate the locals in it directly.
454 return UseLocalStackAllocationBlock;
455 }
456
457 /// setUseLocalStackAllocationBlock - Set whether the local allocation blob
458 /// should be allocated together or let PEI allocate the locals in it
459 /// directly.
461 UseLocalStackAllocationBlock = v;
462 }
463
464 /// Return true if the object was pre-allocated into the local block.
465 bool isObjectPreAllocated(int ObjectIdx) const {
466 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
467 "Invalid Object Idx!");
468 return Objects[ObjectIdx+NumFixedObjects].PreAllocated;
469 }
470
471 /// Return the size of the specified object.
472 int64_t getObjectSize(int ObjectIdx) const {
473 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
474 "Invalid Object Idx!");
475 return Objects[ObjectIdx+NumFixedObjects].Size;
476 }
477
478 /// Change the size of the specified stack object.
479 void setObjectSize(int ObjectIdx, int64_t Size) {
480 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
481 "Invalid Object Idx!");
482 Objects[ObjectIdx+NumFixedObjects].Size = Size;
483 }
484
485 /// Return the alignment of the specified stack object.
486 Align getObjectAlign(int ObjectIdx) const {
487 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
488 "Invalid Object Idx!");
489 return Objects[ObjectIdx + NumFixedObjects].Alignment;
490 }
491
492 /// Should this stack ID be considered in MaxAlignment.
493 bool contributesToMaxAlignment(uint8_t StackID) {
494 return StackID == TargetStackID::Default ||
496 }
497
498 /// setObjectAlignment - Change the alignment of the specified stack object.
499 void setObjectAlignment(int ObjectIdx, Align Alignment) {
500 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
501 "Invalid Object Idx!");
502 Objects[ObjectIdx + NumFixedObjects].Alignment = Alignment;
503
504 // Only ensure max alignment for the default and scalable vector stack.
505 uint8_t StackID = getStackID(ObjectIdx);
506 if (contributesToMaxAlignment(StackID))
507 ensureMaxAlignment(Alignment);
508 }
509
510 /// Return the underlying Alloca of the specified
511 /// stack object if it exists. Returns 0 if none exists.
512 const AllocaInst* getObjectAllocation(int ObjectIdx) const {
513 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
514 "Invalid Object Idx!");
515 return Objects[ObjectIdx+NumFixedObjects].Alloca;
516 }
517
518 /// Remove the underlying Alloca of the specified stack object if it
519 /// exists. This generally should not be used and is for reduction tooling.
520 void clearObjectAllocation(int ObjectIdx) {
521 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
522 "Invalid Object Idx!");
523 Objects[ObjectIdx + NumFixedObjects].Alloca = nullptr;
524 }
525
526 /// Return the assigned stack offset of the specified object
527 /// from the incoming stack pointer.
528 int64_t getObjectOffset(int ObjectIdx) const {
529 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
530 "Invalid Object Idx!");
531 assert(!isDeadObjectIndex(ObjectIdx) &&
532 "Getting frame offset for a dead object?");
533 return Objects[ObjectIdx+NumFixedObjects].SPOffset;
534 }
535
536 bool isObjectZExt(int ObjectIdx) const {
537 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
538 "Invalid Object Idx!");
539 return Objects[ObjectIdx+NumFixedObjects].isZExt;
540 }
541
542 void setObjectZExt(int ObjectIdx, bool IsZExt) {
543 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
544 "Invalid Object Idx!");
545 Objects[ObjectIdx+NumFixedObjects].isZExt = IsZExt;
546 }
547
548 bool isObjectSExt(int ObjectIdx) const {
549 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
550 "Invalid Object Idx!");
551 return Objects[ObjectIdx+NumFixedObjects].isSExt;
552 }
553
554 void setObjectSExt(int ObjectIdx, bool IsSExt) {
555 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
556 "Invalid Object Idx!");
557 Objects[ObjectIdx+NumFixedObjects].isSExt = IsSExt;
558 }
559
560 /// Set the stack frame offset of the specified object. The
561 /// offset is relative to the stack pointer on entry to the function.
562 void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
563 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
564 "Invalid Object Idx!");
565 assert(!isDeadObjectIndex(ObjectIdx) &&
566 "Setting frame offset for a dead object?");
567 Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
568 }
569
570 SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const {
571 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
572 "Invalid Object Idx!");
573 return (SSPLayoutKind)Objects[ObjectIdx+NumFixedObjects].SSPLayout;
574 }
575
576 void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind) {
577 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
578 "Invalid Object Idx!");
579 assert(!isDeadObjectIndex(ObjectIdx) &&
580 "Setting SSP layout for a dead object?");
581 Objects[ObjectIdx+NumFixedObjects].SSPLayout = Kind;
582 }
583
584 /// Return the number of bytes that must be allocated to hold
585 /// all of the fixed size frame objects. This is only valid after
586 /// Prolog/Epilog code insertion has finalized the stack frame layout.
587 uint64_t getStackSize() const { return StackSize; }
588
589 /// Set the size of the stack.
590 void setStackSize(uint64_t Size) { StackSize = Size; }
591
592 /// Estimate and return the size of the stack frame.
594
595 /// Return the correction for frame offsets.
596 int64_t getOffsetAdjustment() const { return OffsetAdjustment; }
597
598 /// Set the correction for frame offsets.
599 void setOffsetAdjustment(int64_t Adj) { OffsetAdjustment = Adj; }
600
601 /// Return the alignment in bytes that this function must be aligned to,
602 /// which is greater than the default stack alignment provided by the target.
603 Align getMaxAlign() const { return MaxAlignment; }
604
605 /// Make sure the function is at least Align bytes aligned.
606 void ensureMaxAlignment(Align Alignment);
607
608 /// Return true if stack realignment is forced by function attributes or if
609 /// the stack alignment.
610 bool shouldRealignStack() const {
611 return ForcedRealign || MaxAlignment > StackAlignment;
612 }
613
614 /// Return true if this function adjusts the stack -- e.g.,
615 /// when calling another function. This is only valid during and after
616 /// prolog/epilog code insertion.
617 bool adjustsStack() const { return AdjustsStack; }
618 void setAdjustsStack(bool V) { AdjustsStack = V; }
619
620 /// Return true if the current function has any function calls.
621 bool hasCalls() const { return HasCalls; }
622 void setHasCalls(bool V) { HasCalls = V; }
623
624 /// Returns true if the function contains opaque dynamic stack adjustments.
625 bool hasOpaqueSPAdjustment() const { return HasOpaqueSPAdjustment; }
626 void setHasOpaqueSPAdjustment(bool B) { HasOpaqueSPAdjustment = B; }
627
628 /// Returns true if the function contains operations which will lower down to
629 /// instructions which manipulate the stack pointer.
631 return HasCopyImplyingStackAdjustment;
632 }
634 HasCopyImplyingStackAdjustment = B;
635 }
636
637 /// Returns true if the function calls the llvm.va_start intrinsic.
638 bool hasVAStart() const { return HasVAStart; }
639 void setHasVAStart(bool B) { HasVAStart = B; }
640
641 /// Returns true if the function is variadic and contains a musttail call.
642 bool hasMustTailInVarArgFunc() const { return HasMustTailInVarArgFunc; }
643 void setHasMustTailInVarArgFunc(bool B) { HasMustTailInVarArgFunc = B; }
644
645 /// Returns true if the function contains a tail call.
646 bool hasTailCall() const { return HasTailCall; }
647 void setHasTailCall(bool V = true) { HasTailCall = V; }
648
649 /// Computes the maximum size of a callframe.
650 /// This only works for targets defining
651 /// TargetInstrInfo::getCallFrameSetupOpcode(), getCallFrameDestroyOpcode(),
652 /// and getFrameSize().
653 /// This is usually computed by the prologue epilogue inserter but some
654 /// targets may call this to compute it earlier.
655 /// If FrameSDOps is passed, the frame instructions in the MF will be
656 /// inserted into it.
658 MachineFunction &MF,
659 std::vector<MachineBasicBlock::iterator> *FrameSDOps = nullptr);
660
661 /// Return the maximum size of a call frame that must be
662 /// allocated for an outgoing function call. This is only available if
663 /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
664 /// then only during or after prolog/epilog code insertion.
665 ///
667 // TODO: Enable this assert when targets are fixed.
668 //assert(isMaxCallFrameSizeComputed() && "MaxCallFrameSize not computed yet");
670 return 0;
671 return MaxCallFrameSize;
672 }
674 return MaxCallFrameSize != ~UINT64_C(0);
675 }
676 void setMaxCallFrameSize(uint64_t S) { MaxCallFrameSize = S; }
677
678 /// Returns how many bytes of callee-saved registers the target pushed in the
679 /// prologue. Only used for debug info.
681 return CVBytesOfCalleeSavedRegisters;
682 }
684 CVBytesOfCalleeSavedRegisters = S;
685 }
686
687 /// Create a new object at a fixed location on the stack.
688 /// All fixed objects should be created before other objects are created for
689 /// efficiency. By default, fixed objects are not pointed to by LLVM IR
690 /// values. This returns an index with a negative value.
691 int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable,
692 bool isAliased = false);
693
694 /// Create a spill slot at a fixed location on the stack.
695 /// Returns an index with a negative value.
696 int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset,
697 bool IsImmutable = false);
698
699 /// Returns true if the specified index corresponds to a fixed stack object.
700 bool isFixedObjectIndex(int ObjectIdx) const {
701 return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
702 }
703
704 /// Returns true if the specified index corresponds
705 /// to an object that might be pointed to by an LLVM IR value.
706 bool isAliasedObjectIndex(int ObjectIdx) const {
707 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
708 "Invalid Object Idx!");
709 return Objects[ObjectIdx+NumFixedObjects].isAliased;
710 }
711
712 /// Set "maybe pointed to by an LLVM IR value" for an object.
713 void setIsAliasedObjectIndex(int ObjectIdx, bool IsAliased) {
714 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
715 "Invalid Object Idx!");
716 Objects[ObjectIdx+NumFixedObjects].isAliased = IsAliased;
717 }
718
719 /// Returns true if the specified index corresponds to an immutable object.
720 bool isImmutableObjectIndex(int ObjectIdx) const {
721 // Tail calling functions can clobber their function arguments.
722 if (HasTailCall)
723 return false;
724 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
725 "Invalid Object Idx!");
726 return Objects[ObjectIdx+NumFixedObjects].isImmutable;
727 }
728
729 /// Marks the immutability of an object.
730 void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable) {
731 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
732 "Invalid Object Idx!");
733 Objects[ObjectIdx+NumFixedObjects].isImmutable = IsImmutable;
734 }
735
736 /// Returns true if the specified index corresponds to a spill slot.
737 bool isSpillSlotObjectIndex(int ObjectIdx) const {
738 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
739 "Invalid Object Idx!");
740 return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;
741 }
742
743 bool isStatepointSpillSlotObjectIndex(int ObjectIdx) const {
744 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
745 "Invalid Object Idx!");
746 return Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot;
747 }
748
749 /// \see StackID
750 uint8_t getStackID(int ObjectIdx) const {
751 return Objects[ObjectIdx+NumFixedObjects].StackID;
752 }
753
754 /// \see StackID
755 void setStackID(int ObjectIdx, uint8_t ID) {
756 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
757 "Invalid Object Idx!");
758 Objects[ObjectIdx+NumFixedObjects].StackID = ID;
759 // If ID > 0, MaxAlignment may now be overly conservative.
760 // If ID == 0, MaxAlignment will need to be updated separately.
761 }
762
763 /// Returns true if the specified index corresponds to a dead object.
764 bool isDeadObjectIndex(int ObjectIdx) const {
765 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
766 "Invalid Object Idx!");
767 return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
768 }
769
770 /// Returns true if the specified index corresponds to a variable sized
771 /// object.
772 bool isVariableSizedObjectIndex(int ObjectIdx) const {
773 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
774 "Invalid Object Idx!");
775 return Objects[ObjectIdx + NumFixedObjects].Size == 0;
776 }
777
779 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
780 "Invalid Object Idx!");
781 Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot = true;
782 assert(isStatepointSpillSlotObjectIndex(ObjectIdx) && "inconsistent");
783 }
784
785 /// Create a new statically sized stack object, returning
786 /// a nonnegative identifier to represent it.
787 int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot,
788 const AllocaInst *Alloca = nullptr, uint8_t ID = 0);
789
790 /// Create a new statically sized stack object that represents a spill slot,
791 /// returning a nonnegative identifier to represent it.
793
794 /// Remove or mark dead a statically sized stack object.
795 void RemoveStackObject(int ObjectIdx) {
796 // Mark it dead.
797 Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
798 }
799
800 /// Notify the MachineFrameInfo object that a variable sized object has been
801 /// created. This must be created whenever a variable sized object is
802 /// created, whether or not the index returned is actually used.
803 int CreateVariableSizedObject(Align Alignment, const AllocaInst *Alloca);
804
805 /// Returns a reference to call saved info vector for the current function.
806 const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
807 return CSInfo;
808 }
809 /// \copydoc getCalleeSavedInfo()
810 std::vector<CalleeSavedInfo> &getCalleeSavedInfo() { return CSInfo; }
811
812 /// Used by prolog/epilog inserter to set the function's callee saved
813 /// information.
814 void setCalleeSavedInfo(std::vector<CalleeSavedInfo> CSI) {
815 CSInfo = std::move(CSI);
816 }
817
818 /// Has the callee saved info been calculated yet?
819 bool isCalleeSavedInfoValid() const { return CSIValid; }
820
821 void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
822
823 MachineBasicBlock *getSavePoint() const { return Save; }
824 void setSavePoint(MachineBasicBlock *NewSave) { Save = NewSave; }
825 MachineBasicBlock *getRestorePoint() const { return Restore; }
826 void setRestorePoint(MachineBasicBlock *NewRestore) { Restore = NewRestore; }
827
828 uint64_t getUnsafeStackSize() const { return UnsafeStackSize; }
829 void setUnsafeStackSize(uint64_t Size) { UnsafeStackSize = Size; }
830
831 /// Return a set of physical registers that are pristine.
832 ///
833 /// Pristine registers hold a value that is useless to the current function,
834 /// but that must be preserved - they are callee saved registers that are not
835 /// saved.
836 ///
837 /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
838 /// method always returns an empty set.
840
841 /// Used by the MachineFunction printer to print information about
842 /// stack objects. Implemented in MachineFunction.cpp.
843 void print(const MachineFunction &MF, raw_ostream &OS) const;
844
845 /// dump - Print the function to stderr.
846 void dump(const MachineFunction &MF) const;
847};
848
849} // End llvm namespace
850
851#endif
@ HasCalls
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
uint64_t Size
#define I(x, y, z)
Definition: MD5.cpp:58
#define T
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallVector class.
an instruction to allocate memory on the stack
Definition: Instructions.h:61
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
void setDstReg(Register SpillReg)
unsigned getDstReg() const
bool isSpilledToReg() const
Register getReg() const
CalleeSavedInfo(unsigned R, int FI=0)
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool needsSplitStackProlog() const
Return true if this function requires a split stack prolog, even if it uses no stack space.
void setMaxCallFrameSize(uint64_t S)
int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
void clearObjectAllocation(int ObjectIdx)
Remove the underlying Alloca of the specified stack object if it exists.
void setObjectZExt(int ObjectIdx, bool IsZExt)
void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable)
Marks the immutability of an object.
SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const
bool isObjectPreAllocated(int ObjectIdx) const
Return true if the object was pre-allocated into the local block.
void computeMaxCallFrameSize(MachineFunction &MF, std::vector< MachineBasicBlock::iterator > *FrameSDOps=nullptr)
Computes the maximum size of a callframe.
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
const AllocaInst * getObjectAllocation(int ObjectIdx) const
Return the underlying Alloca of the specified stack object if it exists.
void setRestorePoint(MachineBasicBlock *NewRestore)
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
void ensureMaxAlignment(Align Alignment)
Make sure the function is at least Align bytes aligned.
bool isReturnAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
MachineFrameInfo(Align StackAlignment, bool StackRealignable, bool ForcedRealign)
int64_t getLocalFrameObjectCount() const
Return the number of objects allocated into the local object block.
void setHasPatchPoint(bool s=true)
bool hasCalls() const
Return true if the current function has any function calls.
bool isFrameAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
std::vector< CalleeSavedInfo > & getCalleeSavedInfo()
void setUseLocalStackAllocationBlock(bool v)
setUseLocalStackAllocationBlock - Set whether the local allocation blob should be allocated together ...
Align getMaxAlign() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
Align getLocalFrameMaxAlign() const
Return the required alignment of the local object blob.
void setLocalFrameSize(int64_t sz)
Set the size of the local object blob.
void setObjectOffset(int ObjectIdx, int64_t SPOffset)
Set the stack frame offset of the specified object.
SSPLayoutKind
Stack Smashing Protection (SSP) rules require that vulnerable stack allocations are located close the...
@ SSPLK_SmallArray
Array or nested array < SSP-buffer-size.
@ SSPLK_LargeArray
Array or nested array >= SSP-buffer-size.
@ SSPLK_AddrOf
The address of this allocation is exposed and triggered protection.
@ SSPLK_None
Did not trigger a stack protector.
void markAsStatepointSpillSlotObjectIndex(int ObjectIdx)
std::pair< int, int64_t > getLocalFrameObjectMap(int i) const
Get the local offset mapping for a for an object.
bool contributesToMaxAlignment(uint8_t StackID)
Should this stack ID be considered in MaxAlignment.
void setFrameAddressIsTaken(bool T)
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
bool shouldRealignStack() const
Return true if stack realignment is forced by function attributes or if the stack alignment.
bool hasPatchPoint() const
This method may be called any time after instruction selection is complete to determine if there is a...
void setHasStackMap(bool s=true)
bool hasOpaqueSPAdjustment() const
Returns true if the function contains opaque dynamic stack adjustments.
void setObjectSExt(int ObjectIdx, bool IsSExt)
void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind)
bool getUseLocalStackAllocationBlock() const
Get whether the local allocation blob should be allocated together or let PEI allocate the locals in ...
MachineBasicBlock * getRestorePoint() const
void setLocalFrameMaxAlign(Align Alignment)
Required alignment of the local object blob, which is the strictest alignment of any object in it.
bool isImmutableObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to an immutable object.
void setCVBytesOfCalleeSavedRegisters(unsigned S)
int getStackProtectorIndex() const
Return the index for the stack protector object.
int64_t getOffsetAdjustment() const
Return the correction for frame offsets.
int CreateSpillStackObject(uint64_t Size, Align Alignment)
Create a new statically sized stack object that represents a spill slot, returning a nonnegative iden...
void setObjectSize(int ObjectIdx, int64_t Size)
Change the size of the specified stack object.
uint64_t estimateStackSize(const MachineFunction &MF) const
Estimate and return the size of the stack frame.
void setStackID(int ObjectIdx, uint8_t ID)
void setHasTailCall(bool V=true)
bool hasTailCall() const
Returns true if the function contains a tail call.
bool hasMustTailInVarArgFunc() const
Returns true if the function is variadic and contains a musttail call.
void setStackProtectorIndex(int I)
void setIsAliasedObjectIndex(int ObjectIdx, bool IsAliased)
Set "maybe pointed to by an LLVM IR value" for an object.
void setCalleeSavedInfoValid(bool v)
bool isStatepointSpillSlotObjectIndex(int ObjectIdx) const
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
void setReturnAddressIsTaken(bool s)
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isObjectZExt(int ObjectIdx) const
void mapLocalFrameObject(int ObjectIndex, int64_t Offset)
Map a frame index into the local object block.
bool isSpillSlotObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a spill slot.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
bool isMaxCallFrameSizeComputed() const
void setHasOpaqueSPAdjustment(bool B)
int64_t getLocalFrameSize() const
Get the size of the local object blob.
bool isObjectSExt(int ObjectIdx) const
BitVector getPristineRegs(const MachineFunction &MF) const
Return a set of physical registers that are pristine.
bool hasStackMap() const
This method may be called any time after instruction selection is complete to determine if there is a...
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
void RemoveStackObject(int ObjectIdx)
Remove or mark dead a statically sized stack object.
void setCalleeSavedInfo(std::vector< CalleeSavedInfo > CSI)
Used by prolog/epilog inserter to set the function's callee saved information.
void setHasCopyImplyingStackAdjustment(bool B)
void print(const MachineFunction &MF, raw_ostream &OS) const
Used by the MachineFunction printer to print information about stack objects.
unsigned getNumObjects() const
Return the number of objects.
bool hasVAStart() const
Returns true if the function calls the llvm.va_start intrinsic.
unsigned getCVBytesOfCalleeSavedRegisters() const
Returns how many bytes of callee-saved registers the target pushed in the prologue.
bool isVariableSizedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a variable sized object.
int CreateVariableSizedObject(Align Alignment, const AllocaInst *Alloca)
Notify the MachineFrameInfo object that a variable sized object has been created.
void setSavePoint(MachineBasicBlock *NewSave)
uint64_t getUnsafeStackSize() const
void dump(const MachineFunction &MF) const
dump - Print the function to stderr.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
bool hasStackProtectorIndex() const
bool hasCopyImplyingStackAdjustment() const
Returns true if the function contains operations which will lower down to instructions which manipula...
bool hasStackObjects() const
Return true if there are any stack objects in this function.
int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset, bool IsImmutable=false)
Create a spill slot at a fixed location on the stack.
MachineFrameInfo(const MachineFrameInfo &)=delete
bool isStackRealignable() const
uint8_t getStackID(int ObjectIdx) const
unsigned getNumFixedObjects() const
Return the number of fixed objects.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
bool hasFunctionContextIndex() const
void setStackSize(uint64_t Size)
Set the size of the stack.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
int getObjectIndexBegin() const
Return the minimum frame object index.
void setUnsafeStackSize(uint64_t Size)
void setHasMustTailInVarArgFunc(bool B)
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
MachineBasicBlock * getSavePoint() const
void setOffsetAdjustment(int64_t Adj)
Set the correction for frame offsets.
int getFunctionContextIndex() const
Return the index for the function context object.
bool isAliasedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to an object that might be pointed to by an LLVM IR v...
void setFunctionContextIndex(int I)
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39