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 | |
16 | #include "llvm/ADT/SmallVector.h" |
17 | #include "llvm/CodeGen/Register.h" |
18 | #include "llvm/CodeGen/TargetFrameLowering.h" |
19 | #include "llvm/Support/Alignment.h" |
20 | #include "llvm/Support/Compiler.h" |
21 | #include <cassert> |
22 | #include <vector> |
23 | |
24 | namespace llvm { |
25 | class raw_ostream; |
26 | class MachineFunction; |
27 | class MachineBasicBlock; |
28 | class BitVector; |
29 | class 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. |
35 | class CalleeSavedInfo { |
36 | MCRegister Reg; |
37 | union { |
38 | int FrameIdx; |
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 | |
58 | public: |
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 |
108 | class MachineFrameInfo { |
109 | public: |
110 | /// Stack Smashing Protection (SSP) rules require that vulnerable stack |
111 | /// allocations are located close the stack protector. |
112 | enum SSPLayoutKind { |
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 | |
123 | private: |
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 null, if shrink-wrapping found a better place for the prologue. |
336 | MachineBasicBlock *Save = nullptr; |
337 | /// Not null, if shrink-wrapping found a better place for the epilogue. |
338 | MachineBasicBlock *Restore = nullptr; |
339 | |
340 | /// Size of the UnsafeStack Frame |
341 | uint64_t UnsafeStackSize = 0; |
342 | |
343 | public: |
344 | explicit MachineFrameInfo(Align StackAlignment, bool StackRealignable, |
345 | bool ForcedRealign) |
346 | : StackAlignment(StackAlignment), |
347 | StackRealignable(StackRealignable), ForcedRealign(ForcedRealign) {} |
348 | |
349 | MachineFrameInfo(const MachineFrameInfo &) = delete; |
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/ |
406 | bool needsSplitStackProlog() const { |
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(Elt: 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. |
455 | bool getUseLocalStackAllocationBlock() const { |
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. |
462 | void setUseLocalStackAllocationBlock(bool v) { |
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. |
495 | bool contributesToMaxAlignment(uint8_t StackID) { |
496 | return StackID == TargetStackID::Default || |
497 | StackID == TargetStackID::ScalableVector; |
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. |
595 | LLVM_ABI uint64_t estimateStackSize(const MachineFunction &MF) const; |
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. |
632 | bool hasCopyImplyingStackAdjustment() const { |
633 | return HasCopyImplyingStackAdjustment; |
634 | } |
635 | void setHasCopyImplyingStackAdjustment(bool B) { |
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. |
659 | LLVM_ABI void computeMaxCallFrameSize( |
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 | /// |
668 | uint64_t getMaxCallFrameSize() const { |
669 | // TODO: Enable this assert when targets are fixed. |
670 | //assert(isMaxCallFrameSizeComputed() && "MaxCallFrameSize not computed yet"); |
671 | if (!isMaxCallFrameSizeComputed()) |
672 | return 0; |
673 | return MaxCallFrameSize; |
674 | } |
675 | bool isMaxCallFrameSizeComputed() const { |
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. |
682 | unsigned getCVBytesOfCalleeSavedRegisters() const { |
683 | return CVBytesOfCalleeSavedRegisters; |
684 | } |
685 | void setCVBytesOfCalleeSavedRegisters(unsigned S) { |
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. |
698 | LLVM_ABI int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset, |
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 | |
780 | void markAsStatepointSpillSlotObjectIndex(int ObjectIdx) { |
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. |
789 | LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, |
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. |
796 | LLVM_ABI int CreateSpillStackObject(uint64_t Size, Align Alignment); |
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. |
807 | LLVM_ABI int CreateVariableSizedObject(Align Alignment, |
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 | MachineBasicBlock *getSavePoint() const { return Save; } |
829 | void setSavePoint(MachineBasicBlock *NewSave) { Save = NewSave; } |
830 | MachineBasicBlock *getRestorePoint() const { return Restore; } |
831 | void setRestorePoint(MachineBasicBlock *NewRestore) { Restore = NewRestore; } |
832 | |
833 | uint64_t getUnsafeStackSize() const { return UnsafeStackSize; } |
834 | void setUnsafeStackSize(uint64_t Size) { UnsafeStackSize = Size; } |
835 | |
836 | /// Return a set of physical registers that are pristine. |
837 | /// |
838 | /// Pristine registers hold a value that is useless to the current function, |
839 | /// but that must be preserved - they are callee saved registers that are not |
840 | /// saved. |
841 | /// |
842 | /// Before the PrologueEpilogueInserter has placed the CSR spill code, this |
843 | /// method always returns an empty set. |
844 | LLVM_ABI BitVector getPristineRegs(const MachineFunction &MF) const; |
845 | |
846 | /// Used by the MachineFunction printer to print information about |
847 | /// stack objects. Implemented in MachineFunction.cpp. |
848 | LLVM_ABI void print(const MachineFunction &MF, raw_ostream &OS) const; |
849 | |
850 | /// dump - Print the function to stderr. |
851 | LLVM_ABI void dump(const MachineFunction &MF) const; |
852 | }; |
853 | |
854 | } // End llvm namespace |
855 | |
856 | #endif |
857 | |