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