Thanks to visit codestin.com
Credit goes to llvm.org

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