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 /// If true, this stack slot is used for spilling a callee saved register
157 /// in the calling convention of the containing function.
158 bool isCalleeSaved = false;
159
160 /// Identifier for stack memory type analagous to address space. If this is
161 /// non-0, the meaning is target defined. Offsets cannot be directly
162 /// compared between objects with different stack IDs. The object may not
163 /// necessarily reside in the same contiguous memory block as other stack
164 /// objects. Objects with differing stack IDs should not be merged or
165 /// replaced substituted for each other.
166 //
167 /// It is assumed a target uses consecutive, increasing stack IDs starting
168 /// from 1.
169 uint8_t StackID;
170
171 /// If this stack object is originated from an Alloca instruction
172 /// this value saves the original IR allocation. Can be NULL.
173 const AllocaInst *Alloca;
174
175 // If true, the object was mapped into the local frame
176 // block and doesn't need additional handling for allocation beyond that.
177 bool PreAllocated = false;
178
179 // If true, an LLVM IR value might point to this object.
180 // Normally, spill slots and fixed-offset objects don't alias IR-accessible
181 // objects, but there are exceptions (on PowerPC, for example, some byval
182 // arguments have ABI-prescribed offsets).
183 bool isAliased;
184
185 /// If true, the object has been zero-extended.
186 bool isZExt = false;
187
188 /// If true, the object has been sign-extended.
189 bool isSExt = false;
190
191 uint8_t SSPLayout = SSPLK_None;
192
193 StackObject(uint64_t Size, Align Alignment, int64_t SPOffset,
194 bool IsImmutable, bool IsSpillSlot, const AllocaInst *Alloca,
195 bool IsAliased, uint8_t StackID = 0)
196 : SPOffset(SPOffset), Size(Size), Alignment(Alignment),
197 isImmutable(IsImmutable), isSpillSlot(IsSpillSlot), StackID(StackID),
198 Alloca(Alloca), isAliased(IsAliased) {}
199 };
200
201 /// The alignment of the stack.
202 Align StackAlignment;
203
204 /// Can the stack be realigned. This can be false if the target does not
205 /// support stack realignment, or if the user asks us not to realign the
206 /// stack. In this situation, overaligned allocas are all treated as dynamic
207 /// allocations and the target must handle them as part of DYNAMIC_STACKALLOC
208 /// lowering. All non-alloca stack objects have their alignment clamped to the
209 /// base ABI stack alignment.
210 /// FIXME: There is room for improvement in this case, in terms of
211 /// grouping overaligned allocas into a "secondary stack frame" and
212 /// then only use a single alloca to allocate this frame and only a
213 /// single virtual register to access it. Currently, without such an
214 /// optimization, each such alloca gets its own dynamic realignment.
215 bool StackRealignable;
216
217 /// Whether the function has the \c alignstack attribute.
218 bool ForcedRealign;
219
220 /// The list of stack objects allocated.
221 std::vector<StackObject> Objects;
222
223 /// This contains the number of fixed objects contained on
224 /// the stack. Because fixed objects are stored at a negative index in the
225 /// Objects list, this is also the index to the 0th object in the list.
226 unsigned NumFixedObjects = 0;
227
228 /// This boolean keeps track of whether any variable
229 /// sized objects have been allocated yet.
230 bool HasVarSizedObjects = false;
231
232 /// This boolean keeps track of whether there is a call
233 /// to builtin \@llvm.frameaddress.
234 bool FrameAddressTaken = false;
235
236 /// This boolean keeps track of whether there is a call
237 /// to builtin \@llvm.returnaddress.
238 bool ReturnAddressTaken = false;
239
240 /// This boolean keeps track of whether there is a call
241 /// to builtin \@llvm.experimental.stackmap.
242 bool HasStackMap = false;
243
244 /// This boolean keeps track of whether there is a call
245 /// to builtin \@llvm.experimental.patchpoint.
246 bool HasPatchPoint = false;
247
248 /// The prolog/epilog code inserter calculates the final stack
249 /// offsets for all of the fixed size objects, updating the Objects list
250 /// above. It then updates StackSize to contain the number of bytes that need
251 /// to be allocated on entry to the function.
252 uint64_t StackSize = 0;
253
254 /// The amount that a frame offset needs to be adjusted to
255 /// have the actual offset from the stack/frame pointer. The exact usage of
256 /// this is target-dependent, but it is typically used to adjust between
257 /// SP-relative and FP-relative offsets. E.G., if objects are accessed via
258 /// SP then OffsetAdjustment is zero; if FP is used, OffsetAdjustment is set
259 /// to the distance between the initial SP and the value in FP. For many
260 /// targets, this value is only used when generating debug info (via
261 /// TargetRegisterInfo::getFrameIndexReference); when generating code, the
262 /// corresponding adjustments are performed directly.
263 int64_t OffsetAdjustment = 0;
264
265 /// The prolog/epilog code inserter may process objects that require greater
266 /// alignment than the default alignment the target provides.
267 /// To handle this, MaxAlignment is set to the maximum alignment
268 /// needed by the objects on the current frame. If this is greater than the
269 /// native alignment maintained by the compiler, dynamic alignment code will
270 /// be needed.
271 ///
272 Align MaxAlignment;
273
274 /// Set to true if this function adjusts the stack -- e.g.,
275 /// when calling another function. This is only valid during and after
276 /// prolog/epilog code insertion.
277 bool AdjustsStack = false;
278
279 /// Set to true if this function has any function calls.
280 bool HasCalls = false;
281
282 /// The frame index for the stack protector.
283 int StackProtectorIdx = -1;
284
285 /// The frame index for the function context. Used for SjLj exceptions.
286 int FunctionContextIdx = -1;
287
288 /// This contains the size of the largest call frame if the target uses frame
289 /// setup/destroy pseudo instructions (as defined in the TargetFrameInfo
290 /// class). This information is important for frame pointer elimination.
291 /// It is only valid during and after prolog/epilog code insertion.
292 uint64_t MaxCallFrameSize = ~UINT64_C(0);
293
294 /// The number of bytes of callee saved registers that the target wants to
295 /// report for the current function in the CodeView S_FRAMEPROC record.
296 unsigned CVBytesOfCalleeSavedRegisters = 0;
297
298 /// The prolog/epilog code inserter fills in this vector with each
299 /// callee saved register saved in either the frame or a different
300 /// register. Beyond its use by the prolog/ epilog code inserter,
301 /// this data is used for debug info and exception handling.
302 std::vector<CalleeSavedInfo> CSInfo;
303
304 /// Has CSInfo been set yet?
305 bool CSIValid = false;
306
307 /// References to frame indices which are mapped
308 /// into the local frame allocation block. <FrameIdx, LocalOffset>
309 SmallVector<std::pair<int, int64_t>, 32> LocalFrameObjects;
310
311 /// Size of the pre-allocated local frame block.
312 int64_t LocalFrameSize = 0;
313
314 /// Required alignment of the local object blob, which is the strictest
315 /// alignment of any object in it.
316 Align LocalFrameMaxAlign;
317
318 /// Whether the local object blob needs to be allocated together. If not,
319 /// PEI should ignore the isPreAllocated flags on the stack objects and
320 /// just allocate them normally.
321 bool UseLocalStackAllocationBlock = false;
322
323 /// True if the function dynamically adjusts the stack pointer through some
324 /// opaque mechanism like inline assembly or Win32 EH.
325 bool HasOpaqueSPAdjustment = false;
326
327 /// True if the function contains operations which will lower down to
328 /// instructions which manipulate the stack pointer.
329 bool HasCopyImplyingStackAdjustment = false;
330
331 /// True if the function contains a call to the llvm.vastart intrinsic.
332 bool HasVAStart = false;
333
334 /// True if this is a varargs function that contains a musttail call.
335 bool HasMustTailInVarArgFunc = false;
336
337 /// True if this function contains a tail call. If so immutable objects like
338 /// function arguments are no longer so. A tail call *can* override fixed
339 /// stack objects like arguments so we can't treat them as immutable.
340 bool HasTailCall = false;
341
342 /// Not empty, if shrink-wrapping found a better place for the prologue.
343 SaveRestorePoints SavePoints;
344 /// Not empty, if shrink-wrapping found a better place for the epilogue.
345 SaveRestorePoints RestorePoints;
346
347 /// Size of the UnsafeStack Frame
348 uint64_t UnsafeStackSize = 0;
349
350public:
351 explicit MachineFrameInfo(Align StackAlignment, bool StackRealignable,
352 bool ForcedRealign)
353 : StackAlignment(StackAlignment),
354 StackRealignable(StackRealignable), ForcedRealign(ForcedRealign) {}
355
357
358 bool isStackRealignable() const { return StackRealignable; }
359
360 /// Return true if there are any stack objects in this function.
361 bool hasStackObjects() const { return !Objects.empty(); }
362
363 /// This method may be called any time after instruction
364 /// selection is complete to determine if the stack frame for this function
365 /// contains any variable sized objects.
366 bool hasVarSizedObjects() const { return HasVarSizedObjects; }
367
368 /// Return the index for the stack protector object.
369 int getStackProtectorIndex() const { return StackProtectorIdx; }
370 void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
371 bool hasStackProtectorIndex() const { return StackProtectorIdx != -1; }
372
373 /// Return the index for the function context object.
374 /// This object is used for SjLj exceptions.
375 int getFunctionContextIndex() const { return FunctionContextIdx; }
376 void setFunctionContextIndex(int I) { FunctionContextIdx = I; }
377 bool hasFunctionContextIndex() const { return FunctionContextIdx != -1; }
378
379 /// This method may be called any time after instruction
380 /// selection is complete to determine if there is a call to
381 /// \@llvm.frameaddress in this function.
382 bool isFrameAddressTaken() const { return FrameAddressTaken; }
383 void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; }
384
385 /// This method may be called any time after
386 /// instruction selection is complete to determine if there is a call to
387 /// \@llvm.returnaddress in this function.
388 bool isReturnAddressTaken() const { return ReturnAddressTaken; }
389 void setReturnAddressIsTaken(bool s) { ReturnAddressTaken = s; }
390
391 /// This method may be called any time after instruction
392 /// selection is complete to determine if there is a call to builtin
393 /// \@llvm.experimental.stackmap.
394 bool hasStackMap() const { return HasStackMap; }
395 void setHasStackMap(bool s = true) { HasStackMap = s; }
396
397 /// This method may be called any time after instruction
398 /// selection is complete to determine if there is a call to builtin
399 /// \@llvm.experimental.patchpoint.
400 bool hasPatchPoint() const { return HasPatchPoint; }
401 void setHasPatchPoint(bool s = true) { HasPatchPoint = s; }
402
403 /// Return true if this function requires a split stack prolog, even if it
404 /// uses no stack space. This is only meaningful for functions where
405 /// MachineFunction::shouldSplitStack() returns true.
406 //
407 // For non-leaf functions we have to allow for the possibility that the call
408 // is to a non-split function, as in PR37807. This function could also take
409 // the address of a non-split function. When the linker tries to adjust its
410 // non-existent prologue, it would fail with an error. Mark the object file so
411 // that such failures are not errors. See this Go language bug-report
412 // https://go-review.googlesource.com/c/go/+/148819/
414 return getStackSize() != 0 || hasTailCall();
415 }
416
417 /// Return the minimum frame object index.
418 int getObjectIndexBegin() const { return -NumFixedObjects; }
419
420 /// Return one past the maximum frame object index.
421 int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
422
423 /// Return the number of fixed objects.
424 unsigned getNumFixedObjects() const { return NumFixedObjects; }
425
426 /// Return the number of objects.
427 unsigned getNumObjects() const { return Objects.size(); }
428
429 /// Map a frame index into the local object block
430 void mapLocalFrameObject(int ObjectIndex, int64_t Offset) {
431 LocalFrameObjects.push_back(std::pair<int, int64_t>(ObjectIndex, Offset));
432 Objects[ObjectIndex + NumFixedObjects].PreAllocated = true;
433 }
434
435 /// Get the local offset mapping for a for an object.
436 std::pair<int, int64_t> getLocalFrameObjectMap(int i) const {
437 assert (i >= 0 && (unsigned)i < LocalFrameObjects.size() &&
438 "Invalid local object reference!");
439 return LocalFrameObjects[i];
440 }
441
442 /// Return the number of objects allocated into the local object block.
443 int64_t getLocalFrameObjectCount() const { return LocalFrameObjects.size(); }
444
445 /// Set the size of the local object blob.
446 void setLocalFrameSize(int64_t sz) { LocalFrameSize = sz; }
447
448 /// Get the size of the local object blob.
449 int64_t getLocalFrameSize() const { return LocalFrameSize; }
450
451 /// Required alignment of the local object blob,
452 /// which is the strictest alignment of any object in it.
453 void setLocalFrameMaxAlign(Align Alignment) {
454 LocalFrameMaxAlign = Alignment;
455 }
456
457 /// Return the required alignment of the local object blob.
458 Align getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
459
460 /// Get whether the local allocation blob should be allocated together or
461 /// let PEI allocate the locals in it directly.
463 return UseLocalStackAllocationBlock;
464 }
465
466 /// setUseLocalStackAllocationBlock - Set whether the local allocation blob
467 /// should be allocated together or let PEI allocate the locals in it
468 /// directly.
470 UseLocalStackAllocationBlock = v;
471 }
472
473 /// Return true if the object was pre-allocated into the local block.
474 bool isObjectPreAllocated(int ObjectIdx) const {
475 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
476 "Invalid Object Idx!");
477 return Objects[ObjectIdx+NumFixedObjects].PreAllocated;
478 }
479
480 /// Return the size of the specified object.
481 int64_t getObjectSize(int ObjectIdx) const {
482 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
483 "Invalid Object Idx!");
484 return Objects[ObjectIdx+NumFixedObjects].Size;
485 }
486
487 /// Change the size of the specified stack object.
488 void setObjectSize(int ObjectIdx, int64_t Size) {
489 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
490 "Invalid Object Idx!");
491 Objects[ObjectIdx+NumFixedObjects].Size = Size;
492 }
493
494 /// Return the alignment of the specified stack object.
495 Align getObjectAlign(int ObjectIdx) const {
496 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
497 "Invalid Object Idx!");
498 return Objects[ObjectIdx + NumFixedObjects].Alignment;
499 }
500
501 /// Should this stack ID be considered in MaxAlignment.
503 return StackID == TargetStackID::Default ||
506 }
507
508 bool hasScalableStackID(int ObjectIdx) const {
509 uint8_t StackID = getStackID(ObjectIdx);
510 return isScalableStackID(StackID);
511 }
512
513 bool isScalableStackID(uint8_t StackID) const {
514 return StackID == TargetStackID::ScalableVector ||
516 }
517
518 /// setObjectAlignment - Change the alignment of the specified stack object.
519 void setObjectAlignment(int ObjectIdx, Align Alignment) {
520 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
521 "Invalid Object Idx!");
522 Objects[ObjectIdx + NumFixedObjects].Alignment = Alignment;
523
524 // Only ensure max alignment for the default and scalable vector stack.
525 uint8_t StackID = getStackID(ObjectIdx);
526 if (contributesToMaxAlignment(StackID))
527 ensureMaxAlignment(Alignment);
528 }
529
530 /// Return the underlying Alloca of the specified
531 /// stack object if it exists. Returns 0 if none exists.
532 const AllocaInst* getObjectAllocation(int ObjectIdx) const {
533 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
534 "Invalid Object Idx!");
535 return Objects[ObjectIdx+NumFixedObjects].Alloca;
536 }
537
538 /// Remove the underlying Alloca of the specified stack object if it
539 /// exists. This generally should not be used and is for reduction tooling.
540 void clearObjectAllocation(int ObjectIdx) {
541 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
542 "Invalid Object Idx!");
543 Objects[ObjectIdx + NumFixedObjects].Alloca = nullptr;
544 }
545
546 /// Return the assigned stack offset of the specified object
547 /// from the incoming stack pointer.
548 int64_t getObjectOffset(int ObjectIdx) const {
549 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
550 "Invalid Object Idx!");
551 assert(!isDeadObjectIndex(ObjectIdx) &&
552 "Getting frame offset for a dead object?");
553 return Objects[ObjectIdx+NumFixedObjects].SPOffset;
554 }
555
556 bool isObjectZExt(int ObjectIdx) const {
557 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
558 "Invalid Object Idx!");
559 return Objects[ObjectIdx+NumFixedObjects].isZExt;
560 }
561
562 void setObjectZExt(int ObjectIdx, bool IsZExt) {
563 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
564 "Invalid Object Idx!");
565 Objects[ObjectIdx+NumFixedObjects].isZExt = IsZExt;
566 }
567
568 bool isObjectSExt(int ObjectIdx) const {
569 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
570 "Invalid Object Idx!");
571 return Objects[ObjectIdx+NumFixedObjects].isSExt;
572 }
573
574 void setObjectSExt(int ObjectIdx, bool IsSExt) {
575 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
576 "Invalid Object Idx!");
577 Objects[ObjectIdx+NumFixedObjects].isSExt = IsSExt;
578 }
579
580 /// Set the stack frame offset of the specified object. The
581 /// offset is relative to the stack pointer on entry to the function.
582 void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
583 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
584 "Invalid Object Idx!");
585 assert(!isDeadObjectIndex(ObjectIdx) &&
586 "Setting frame offset for a dead object?");
587 Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
588 }
589
590 SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const {
591 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
592 "Invalid Object Idx!");
593 return (SSPLayoutKind)Objects[ObjectIdx+NumFixedObjects].SSPLayout;
594 }
595
596 void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind) {
597 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
598 "Invalid Object Idx!");
599 assert(!isDeadObjectIndex(ObjectIdx) &&
600 "Setting SSP layout for a dead object?");
601 Objects[ObjectIdx+NumFixedObjects].SSPLayout = Kind;
602 }
603
604 /// Return the number of bytes that must be allocated to hold
605 /// all of the fixed size frame objects. This is only valid after
606 /// Prolog/Epilog code insertion has finalized the stack frame layout.
607 uint64_t getStackSize() const { return StackSize; }
608
609 /// Set the size of the stack.
610 void setStackSize(uint64_t Size) { StackSize = Size; }
611
612 /// Estimate and return the size of the stack frame.
614
615 /// Return the correction for frame offsets.
616 int64_t getOffsetAdjustment() const { return OffsetAdjustment; }
617
618 /// Set the correction for frame offsets.
619 void setOffsetAdjustment(int64_t Adj) { OffsetAdjustment = Adj; }
620
621 /// Return the alignment in bytes that this function must be aligned to,
622 /// which is greater than the default stack alignment provided by the target.
623 Align getMaxAlign() const { return MaxAlignment; }
624
625 /// Make sure the function is at least Align bytes aligned.
626 LLVM_ABI void ensureMaxAlignment(Align Alignment);
627
628 /// Return true if stack realignment is forced by function attributes or if
629 /// the stack alignment.
630 bool shouldRealignStack() const {
631 return ForcedRealign || MaxAlignment > StackAlignment;
632 }
633
634 /// Return true if this function adjusts the stack -- e.g.,
635 /// when calling another function. This is only valid during and after
636 /// prolog/epilog code insertion.
637 bool adjustsStack() const { return AdjustsStack; }
638 void setAdjustsStack(bool V) { AdjustsStack = V; }
639
640 /// Return true if the current function has any function calls.
641 bool hasCalls() const { return HasCalls; }
642 void setHasCalls(bool V) { HasCalls = V; }
643
644 /// Returns true if the function contains opaque dynamic stack adjustments.
645 bool hasOpaqueSPAdjustment() const { return HasOpaqueSPAdjustment; }
646 void setHasOpaqueSPAdjustment(bool B) { HasOpaqueSPAdjustment = B; }
647
648 /// Returns true if the function contains operations which will lower down to
649 /// instructions which manipulate the stack pointer.
651 return HasCopyImplyingStackAdjustment;
652 }
654 HasCopyImplyingStackAdjustment = B;
655 }
656
657 /// Returns true if the function calls the llvm.va_start intrinsic.
658 bool hasVAStart() const { return HasVAStart; }
659 void setHasVAStart(bool B) { HasVAStart = B; }
660
661 /// Returns true if the function is variadic and contains a musttail call.
662 bool hasMustTailInVarArgFunc() const { return HasMustTailInVarArgFunc; }
663 void setHasMustTailInVarArgFunc(bool B) { HasMustTailInVarArgFunc = B; }
664
665 /// Returns true if the function contains a tail call.
666 bool hasTailCall() const { return HasTailCall; }
667 void setHasTailCall(bool V = true) { HasTailCall = V; }
668
669 /// Computes the maximum size of a callframe.
670 /// This only works for targets defining
671 /// TargetInstrInfo::getCallFrameSetupOpcode(), getCallFrameDestroyOpcode(),
672 /// and getFrameSize().
673 /// This is usually computed by the prologue epilogue inserter but some
674 /// targets may call this to compute it earlier.
675 /// If FrameSDOps is passed, the frame instructions in the MF will be
676 /// inserted into it.
678 MachineFunction &MF,
679 std::vector<MachineBasicBlock::iterator> *FrameSDOps = nullptr);
680
681 /// Return the maximum size of a call frame that must be
682 /// allocated for an outgoing function call. This is only available if
683 /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
684 /// then only during or after prolog/epilog code insertion.
685 ///
687 // TODO: Enable this assert when targets are fixed.
688 //assert(isMaxCallFrameSizeComputed() && "MaxCallFrameSize not computed yet");
690 return 0;
691 return MaxCallFrameSize;
692 }
694 return MaxCallFrameSize != ~UINT64_C(0);
695 }
696 void setMaxCallFrameSize(uint64_t S) { MaxCallFrameSize = S; }
697
698 /// Returns how many bytes of callee-saved registers the target pushed in the
699 /// prologue. Only used for debug info.
701 return CVBytesOfCalleeSavedRegisters;
702 }
704 CVBytesOfCalleeSavedRegisters = S;
705 }
706
707 /// Create a new object at a fixed location on the stack.
708 /// All fixed objects should be created before other objects are created for
709 /// efficiency. By default, fixed objects are not pointed to by LLVM IR
710 /// values. This returns an index with a negative value.
711 LLVM_ABI int CreateFixedObject(uint64_t Size, int64_t SPOffset,
712 bool IsImmutable, bool isAliased = false);
713
714 /// Create a spill slot at a fixed location on the stack.
715 /// Returns an index with a negative value.
717 bool IsImmutable = false);
718
719 /// Returns true if the specified index corresponds to a fixed stack object.
720 bool isFixedObjectIndex(int ObjectIdx) const {
721 return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
722 }
723
724 /// Returns true if the specified index corresponds
725 /// to an object that might be pointed to by an LLVM IR value.
726 bool isAliasedObjectIndex(int ObjectIdx) const {
727 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
728 "Invalid Object Idx!");
729 return Objects[ObjectIdx+NumFixedObjects].isAliased;
730 }
731
732 /// Set "maybe pointed to by an LLVM IR value" for an object.
733 void setIsAliasedObjectIndex(int ObjectIdx, bool IsAliased) {
734 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
735 "Invalid Object Idx!");
736 Objects[ObjectIdx+NumFixedObjects].isAliased = IsAliased;
737 }
738
739 /// Returns true if the specified index corresponds to an immutable object.
740 bool isImmutableObjectIndex(int ObjectIdx) const {
741 // Tail calling functions can clobber their function arguments.
742 if (HasTailCall)
743 return false;
744 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
745 "Invalid Object Idx!");
746 return Objects[ObjectIdx+NumFixedObjects].isImmutable;
747 }
748
749 /// Marks the immutability of an object.
750 void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable) {
751 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
752 "Invalid Object Idx!");
753 Objects[ObjectIdx+NumFixedObjects].isImmutable = IsImmutable;
754 }
755
756 /// Returns true if the specified index corresponds to a spill slot.
757 bool isSpillSlotObjectIndex(int ObjectIdx) const {
758 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
759 "Invalid Object Idx!");
760 return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;
761 }
762
763 bool isStatepointSpillSlotObjectIndex(int ObjectIdx) const {
764 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
765 "Invalid Object Idx!");
766 return Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot;
767 }
768
769 bool isCalleeSavedObjectIndex(int ObjectIdx) const {
770 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
771 "Invalid Object Idx!");
772 return Objects[ObjectIdx + NumFixedObjects].isCalleeSaved;
773 }
774
775 void setIsCalleeSavedObjectIndex(int ObjectIdx, bool IsCalleeSaved) {
776 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
777 "Invalid Object Idx!");
778 Objects[ObjectIdx + NumFixedObjects].isCalleeSaved = IsCalleeSaved;
779 }
780
781 /// \see StackID
782 uint8_t getStackID(int ObjectIdx) const {
783 return Objects[ObjectIdx+NumFixedObjects].StackID;
784 }
785
786 /// \see StackID
787 void setStackID(int ObjectIdx, uint8_t ID) {
788 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
789 "Invalid Object Idx!");
790 Objects[ObjectIdx+NumFixedObjects].StackID = ID;
791 // If ID > 0, MaxAlignment may now be overly conservative.
792 // If ID == 0, MaxAlignment will need to be updated separately.
793 }
794
795 /// Returns true if the specified index corresponds to a dead object.
796 bool isDeadObjectIndex(int ObjectIdx) const {
797 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
798 "Invalid Object Idx!");
799 return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
800 }
801
802 /// Returns true if the specified index corresponds to a variable sized
803 /// object.
804 bool isVariableSizedObjectIndex(int ObjectIdx) const {
805 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
806 "Invalid Object Idx!");
807 return Objects[ObjectIdx + NumFixedObjects].Size == 0;
808 }
809
811 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
812 "Invalid Object Idx!");
813 Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot = true;
814 assert(isStatepointSpillSlotObjectIndex(ObjectIdx) && "inconsistent");
815 }
816
817 /// Create a new statically sized stack object, returning
818 /// a nonnegative identifier to represent it.
820 bool isSpillSlot,
821 const AllocaInst *Alloca = nullptr,
822 uint8_t ID = 0);
823
824 /// Create a new statically sized stack object that represents a spill slot,
825 /// returning a nonnegative identifier to represent it.
827
828 /// Remove or mark dead a statically sized stack object.
829 void RemoveStackObject(int ObjectIdx) {
830 // Mark it dead.
831 Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
832 }
833
834 /// Notify the MachineFrameInfo object that a variable sized object has been
835 /// created. This must be created whenever a variable sized object is
836 /// created, whether or not the index returned is actually used.
838 const AllocaInst *Alloca);
839
840 /// Returns a reference to call saved info vector for the current function.
841 const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
842 return CSInfo;
843 }
844 /// \copydoc getCalleeSavedInfo()
845 std::vector<CalleeSavedInfo> &getCalleeSavedInfo() { return CSInfo; }
846
847 /// Used by prolog/epilog inserter to set the function's callee saved
848 /// information.
849 void setCalleeSavedInfo(std::vector<CalleeSavedInfo> CSI) {
850 CSInfo = std::move(CSI);
851 }
852
853 /// Has the callee saved info been calculated yet?
854 bool isCalleeSavedInfoValid() const { return CSIValid; }
855
856 void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
857
858 const SaveRestorePoints &getRestorePoints() const { return RestorePoints; }
859
860 const SaveRestorePoints &getSavePoints() const { return SavePoints; }
861
862 void setSavePoints(SaveRestorePoints NewSavePoints) {
863 SavePoints = std::move(NewSavePoints);
864 }
865
866 void setRestorePoints(SaveRestorePoints NewRestorePoints) {
867 RestorePoints = std::move(NewRestorePoints);
868 }
869
870 void clearSavePoints() { SavePoints.clear(); }
871 void clearRestorePoints() { RestorePoints.clear(); }
872
873 uint64_t getUnsafeStackSize() const { return UnsafeStackSize; }
874 void setUnsafeStackSize(uint64_t Size) { UnsafeStackSize = Size; }
875
876 /// Return a set of physical registers that are pristine.
877 ///
878 /// Pristine registers hold a value that is useless to the current function,
879 /// but that must be preserved - they are callee saved registers that are not
880 /// saved.
881 ///
882 /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
883 /// method always returns an empty set.
885
886 /// Used by the MachineFunction printer to print information about
887 /// stack objects. Implemented in MachineFunction.cpp.
888 LLVM_ABI void print(const MachineFunction &MF, raw_ostream &OS) const;
889
890 /// dump - Print the function to stderr.
891 LLVM_ABI void dump(const MachineFunction &MF) const;
892};
893
894} // End llvm namespace
895
896#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:57
#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:41
constexpr unsigned id() const
Definition MCRegister.h:82
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.
bool isCalleeSavedObjectIndex(int ObjectIdx) const
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.
void setIsCalleeSavedObjectIndex(int ObjectIdx, bool IsCalleeSaved)
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:532
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