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