blob: 7f0ec0df57c52cb60104a6e25d69fe941dc5f6cc [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020017#include "llvm/CodeGen/Register.h"
18#include "llvm/Support/Alignment.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010019#include "llvm/Support/DataTypes.h"
20#include <cassert>
21#include <vector>
22
23namespace llvm {
24class raw_ostream;
25class MachineFunction;
26class MachineBasicBlock;
27class BitVector;
28class AllocaInst;
29
30/// The CalleeSavedInfo class tracks the information need to locate where a
31/// callee saved register is in the current frame.
Andrew Walbran16937d02019-10-22 13:54:20 +010032/// Callee saved reg can also be saved to a different register rather than
33/// on the stack by setting DstReg instead of FrameIdx.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010034class CalleeSavedInfo {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020035 Register Reg;
Andrew Walbran16937d02019-10-22 13:54:20 +010036 union {
37 int FrameIdx;
38 unsigned DstReg;
39 };
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010040 /// Flag indicating whether the register is actually restored in the epilog.
41 /// In most cases, if a register is saved, it is also restored. There are
42 /// some situations, though, when this is not the case. For example, the
43 /// LR register on ARM is usually saved, but on exit from the function its
44 /// saved value may be loaded directly into PC. Since liveness tracking of
45 /// physical registers treats callee-saved registers are live outside of
46 /// the function, LR would be treated as live-on-exit, even though in these
47 /// scenarios it is not. This flag is added to indicate that the saved
48 /// register described by this object is not restored in the epilog.
49 /// The long-term solution is to model the liveness of callee-saved registers
50 /// by implicit uses on the return instructions, however, the required
51 /// changes in the ARM backend would be quite extensive.
52 bool Restored;
Andrew Walbran16937d02019-10-22 13:54:20 +010053 /// Flag indicating whether the register is spilled to stack or another
54 /// register.
55 bool SpilledToReg;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010056
57public:
58 explicit CalleeSavedInfo(unsigned R, int FI = 0)
Andrew Walbran16937d02019-10-22 13:54:20 +010059 : Reg(R), FrameIdx(FI), Restored(true), SpilledToReg(false) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060
61 // Accessors.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020062 Register getReg() const { return Reg; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010063 int getFrameIdx() const { return FrameIdx; }
Andrew Walbran16937d02019-10-22 13:54:20 +010064 unsigned getDstReg() const { return DstReg; }
65 void setFrameIdx(int FI) {
66 FrameIdx = FI;
67 SpilledToReg = false;
68 }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020069 void setDstReg(Register SpillReg) {
Andrew Walbran16937d02019-10-22 13:54:20 +010070 DstReg = SpillReg;
71 SpilledToReg = true;
72 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073 bool isRestored() const { return Restored; }
74 void setRestored(bool R) { Restored = R; }
Andrew Walbran16937d02019-10-22 13:54:20 +010075 bool isSpilledToReg() const { return SpilledToReg; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010076};
77
78/// The MachineFrameInfo class represents an abstract stack frame until
79/// prolog/epilog code is inserted. This class is key to allowing stack frame
80/// representation optimizations, such as frame pointer elimination. It also
81/// allows more mundane (but still important) optimizations, such as reordering
82/// of abstract objects on the stack frame.
83///
84/// To support this, the class assigns unique integer identifiers to stack
85/// objects requested clients. These identifiers are negative integers for
86/// fixed stack objects (such as arguments passed on the stack) or nonnegative
87/// for objects that may be reordered. Instructions which refer to stack
88/// objects use a special MO_FrameIndex operand to represent these frame
89/// indexes.
90///
91/// Because this class keeps track of all references to the stack frame, it
92/// knows when a variable sized object is allocated on the stack. This is the
93/// sole condition which prevents frame pointer elimination, which is an
94/// important optimization on register-poor architectures. Because original
95/// variable sized alloca's in the source program are the only source of
96/// variable sized stack objects, it is safe to decide whether there will be
97/// any variable sized objects before all stack objects are known (for
98/// example, register allocator spill code never needs variable sized
99/// objects).
100///
101/// When prolog/epilog code emission is performed, the final stack frame is
102/// built and the machine instructions are modified to refer to the actual
103/// stack offsets of the object, eliminating all MO_FrameIndex operands from
104/// the program.
105///
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100106/// Abstract Stack Frame Information
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100107class MachineFrameInfo {
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100108public:
109 /// Stack Smashing Protection (SSP) rules require that vulnerable stack
110 /// allocations are located close the stack protector.
111 enum SSPLayoutKind {
112 SSPLK_None, ///< Did not trigger a stack protector. No effect on data
113 ///< layout.
114 SSPLK_LargeArray, ///< Array or nested array >= SSP-buffer-size. Closest
115 ///< to the stack protector.
116 SSPLK_SmallArray, ///< Array or nested array < SSP-buffer-size. 2nd closest
117 ///< to the stack protector.
118 SSPLK_AddrOf ///< The address of this allocation is exposed and
119 ///< triggered protection. 3rd closest to the protector.
120 };
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100121
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100122private:
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100123 // Represent a single object allocated on the stack.
124 struct StackObject {
125 // The offset of this object from the stack pointer on entry to
126 // the function. This field has no meaning for a variable sized element.
127 int64_t SPOffset;
128
129 // The size of this object on the stack. 0 means a variable sized object,
130 // ~0ULL means a dead object.
131 uint64_t Size;
132
133 // The required alignment of this stack slot.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200134 Align Alignment;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100135
136 // If true, the value of the stack object is set before
137 // entering the function and is not modified inside the function. By
138 // default, fixed objects are immutable unless marked otherwise.
139 bool isImmutable;
140
141 // If true the stack object is used as spill slot. It
142 // cannot alias any other memory objects.
143 bool isSpillSlot;
144
145 /// If true, this stack slot is used to spill a value (could be deopt
146 /// and/or GC related) over a statepoint. We know that the address of the
147 /// slot can't alias any LLVM IR value. This is very similar to a Spill
148 /// Slot, but is created by statepoint lowering is SelectionDAG, not the
149 /// register allocator.
150 bool isStatepointSpillSlot = false;
151
152 /// Identifier for stack memory type analagous to address space. If this is
153 /// non-0, the meaning is target defined. Offsets cannot be directly
154 /// compared between objects with different stack IDs. The object may not
155 /// necessarily reside in the same contiguous memory block as other stack
156 /// objects. Objects with differing stack IDs should not be merged or
157 /// replaced substituted for each other.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100158 //
159 /// It is assumed a target uses consecutive, increasing stack IDs starting
160 /// from 1.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100161 uint8_t StackID;
162
163 /// If this stack object is originated from an Alloca instruction
164 /// this value saves the original IR allocation. Can be NULL.
165 const AllocaInst *Alloca;
166
167 // If true, the object was mapped into the local frame
168 // block and doesn't need additional handling for allocation beyond that.
169 bool PreAllocated = false;
170
171 // If true, an LLVM IR value might point to this object.
172 // Normally, spill slots and fixed-offset objects don't alias IR-accessible
173 // objects, but there are exceptions (on PowerPC, for example, some byval
174 // arguments have ABI-prescribed offsets).
175 bool isAliased;
176
177 /// If true, the object has been zero-extended.
178 bool isZExt = false;
179
180 /// If true, the object has been zero-extended.
181 bool isSExt = false;
182
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100183 uint8_t SSPLayout;
184
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200185 StackObject(uint64_t Size, Align Alignment, int64_t SPOffset,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100186 bool IsImmutable, bool IsSpillSlot, const AllocaInst *Alloca,
187 bool IsAliased, uint8_t StackID = 0)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200188 : SPOffset(SPOffset), Size(Size), Alignment(Alignment),
189 isImmutable(IsImmutable), isSpillSlot(IsSpillSlot), StackID(StackID),
190 Alloca(Alloca), isAliased(IsAliased), SSPLayout(SSPLK_None) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100191 };
192
193 /// The alignment of the stack.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200194 Align StackAlignment;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100195
196 /// Can the stack be realigned. This can be false if the target does not
197 /// support stack realignment, or if the user asks us not to realign the
198 /// stack. In this situation, overaligned allocas are all treated as dynamic
199 /// allocations and the target must handle them as part of DYNAMIC_STACKALLOC
200 /// lowering. All non-alloca stack objects have their alignment clamped to the
201 /// base ABI stack alignment.
202 /// FIXME: There is room for improvement in this case, in terms of
203 /// grouping overaligned allocas into a "secondary stack frame" and
204 /// then only use a single alloca to allocate this frame and only a
205 /// single virtual register to access it. Currently, without such an
206 /// optimization, each such alloca gets its own dynamic realignment.
207 bool StackRealignable;
208
209 /// Whether the function has the \c alignstack attribute.
210 bool ForcedRealign;
211
212 /// The list of stack objects allocated.
213 std::vector<StackObject> Objects;
214
215 /// This contains the number of fixed objects contained on
216 /// the stack. Because fixed objects are stored at a negative index in the
217 /// Objects list, this is also the index to the 0th object in the list.
218 unsigned NumFixedObjects = 0;
219
220 /// This boolean keeps track of whether any variable
221 /// sized objects have been allocated yet.
222 bool HasVarSizedObjects = false;
223
224 /// This boolean keeps track of whether there is a call
225 /// to builtin \@llvm.frameaddress.
226 bool FrameAddressTaken = false;
227
228 /// This boolean keeps track of whether there is a call
229 /// to builtin \@llvm.returnaddress.
230 bool ReturnAddressTaken = false;
231
232 /// This boolean keeps track of whether there is a call
233 /// to builtin \@llvm.experimental.stackmap.
234 bool HasStackMap = false;
235
236 /// This boolean keeps track of whether there is a call
237 /// to builtin \@llvm.experimental.patchpoint.
238 bool HasPatchPoint = false;
239
240 /// The prolog/epilog code inserter calculates the final stack
241 /// offsets for all of the fixed size objects, updating the Objects list
242 /// above. It then updates StackSize to contain the number of bytes that need
243 /// to be allocated on entry to the function.
244 uint64_t StackSize = 0;
245
246 /// The amount that a frame offset needs to be adjusted to
247 /// have the actual offset from the stack/frame pointer. The exact usage of
248 /// this is target-dependent, but it is typically used to adjust between
249 /// SP-relative and FP-relative offsets. E.G., if objects are accessed via
250 /// SP then OffsetAdjustment is zero; if FP is used, OffsetAdjustment is set
251 /// to the distance between the initial SP and the value in FP. For many
252 /// targets, this value is only used when generating debug info (via
253 /// TargetRegisterInfo::getFrameIndexReference); when generating code, the
254 /// corresponding adjustments are performed directly.
255 int OffsetAdjustment = 0;
256
257 /// The prolog/epilog code inserter may process objects that require greater
258 /// alignment than the default alignment the target provides.
259 /// To handle this, MaxAlignment is set to the maximum alignment
260 /// needed by the objects on the current frame. If this is greater than the
261 /// native alignment maintained by the compiler, dynamic alignment code will
262 /// be needed.
263 ///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200264 Align MaxAlignment;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100265
266 /// Set to true if this function adjusts the stack -- e.g.,
267 /// when calling another function. This is only valid during and after
268 /// prolog/epilog code insertion.
269 bool AdjustsStack = false;
270
271 /// Set to true if this function has any function calls.
272 bool HasCalls = false;
273
274 /// The frame index for the stack protector.
275 int StackProtectorIdx = -1;
276
277 /// The frame index for the function context. Used for SjLj exceptions.
278 int FunctionContextIdx = -1;
279
280 /// This contains the size of the largest call frame if the target uses frame
281 /// setup/destroy pseudo instructions (as defined in the TargetFrameInfo
282 /// class). This information is important for frame pointer elimination.
283 /// It is only valid during and after prolog/epilog code insertion.
284 unsigned MaxCallFrameSize = ~0u;
285
Andrew Scull0372a572018-11-16 15:47:06 +0000286 /// The number of bytes of callee saved registers that the target wants to
287 /// report for the current function in the CodeView S_FRAMEPROC record.
288 unsigned CVBytesOfCalleeSavedRegisters = 0;
289
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100290 /// The prolog/epilog code inserter fills in this vector with each
Andrew Walbran16937d02019-10-22 13:54:20 +0100291 /// callee saved register saved in either the frame or a different
292 /// register. Beyond its use by the prolog/ epilog code inserter,
293 /// this data is used for debug info and exception handling.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100294 std::vector<CalleeSavedInfo> CSInfo;
295
296 /// Has CSInfo been set yet?
297 bool CSIValid = false;
298
299 /// References to frame indices which are mapped
300 /// into the local frame allocation block. <FrameIdx, LocalOffset>
301 SmallVector<std::pair<int, int64_t>, 32> LocalFrameObjects;
302
303 /// Size of the pre-allocated local frame block.
304 int64_t LocalFrameSize = 0;
305
306 /// Required alignment of the local object blob, which is the strictest
307 /// alignment of any object in it.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200308 Align LocalFrameMaxAlign;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100309
310 /// Whether the local object blob needs to be allocated together. If not,
311 /// PEI should ignore the isPreAllocated flags on the stack objects and
312 /// just allocate them normally.
313 bool UseLocalStackAllocationBlock = false;
314
315 /// True if the function dynamically adjusts the stack pointer through some
316 /// opaque mechanism like inline assembly or Win32 EH.
317 bool HasOpaqueSPAdjustment = false;
318
319 /// True if the function contains operations which will lower down to
320 /// instructions which manipulate the stack pointer.
321 bool HasCopyImplyingStackAdjustment = false;
322
323 /// True if the function contains a call to the llvm.vastart intrinsic.
324 bool HasVAStart = false;
325
326 /// True if this is a varargs function that contains a musttail call.
327 bool HasMustTailInVarArgFunc = false;
328
329 /// True if this function contains a tail call. If so immutable objects like
330 /// function arguments are no longer so. A tail call *can* override fixed
331 /// stack objects like arguments so we can't treat them as immutable.
332 bool HasTailCall = false;
333
334 /// Not null, if shrink-wrapping found a better place for the prologue.
335 MachineBasicBlock *Save = nullptr;
336 /// Not null, if shrink-wrapping found a better place for the epilogue.
337 MachineBasicBlock *Restore = nullptr;
338
339public:
340 explicit MachineFrameInfo(unsigned StackAlignment, bool StackRealignable,
341 bool ForcedRealign)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200342 : StackAlignment(assumeAligned(StackAlignment)),
343 StackRealignable(StackRealignable), ForcedRealign(ForcedRealign) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100344
345 /// Return true if there are any stack objects in this function.
346 bool hasStackObjects() const { return !Objects.empty(); }
347
348 /// This method may be called any time after instruction
349 /// selection is complete to determine if the stack frame for this function
350 /// contains any variable sized objects.
351 bool hasVarSizedObjects() const { return HasVarSizedObjects; }
352
353 /// Return the index for the stack protector object.
354 int getStackProtectorIndex() const { return StackProtectorIdx; }
355 void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
356 bool hasStackProtectorIndex() const { return StackProtectorIdx != -1; }
357
358 /// Return the index for the function context object.
359 /// This object is used for SjLj exceptions.
360 int getFunctionContextIndex() const { return FunctionContextIdx; }
361 void setFunctionContextIndex(int I) { FunctionContextIdx = I; }
362
363 /// This method may be called any time after instruction
364 /// selection is complete to determine if there is a call to
365 /// \@llvm.frameaddress in this function.
366 bool isFrameAddressTaken() const { return FrameAddressTaken; }
367 void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; }
368
369 /// This method may be called any time after
370 /// instruction selection is complete to determine if there is a call to
371 /// \@llvm.returnaddress in this function.
372 bool isReturnAddressTaken() const { return ReturnAddressTaken; }
373 void setReturnAddressIsTaken(bool s) { ReturnAddressTaken = s; }
374
375 /// This method may be called any time after instruction
376 /// selection is complete to determine if there is a call to builtin
377 /// \@llvm.experimental.stackmap.
378 bool hasStackMap() const { return HasStackMap; }
379 void setHasStackMap(bool s = true) { HasStackMap = s; }
380
381 /// This method may be called any time after instruction
382 /// selection is complete to determine if there is a call to builtin
383 /// \@llvm.experimental.patchpoint.
384 bool hasPatchPoint() const { return HasPatchPoint; }
385 void setHasPatchPoint(bool s = true) { HasPatchPoint = s; }
386
387 /// Return the minimum frame object index.
388 int getObjectIndexBegin() const { return -NumFixedObjects; }
389
390 /// Return one past the maximum frame object index.
391 int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
392
393 /// Return the number of fixed objects.
394 unsigned getNumFixedObjects() const { return NumFixedObjects; }
395
396 /// Return the number of objects.
397 unsigned getNumObjects() const { return Objects.size(); }
398
399 /// Map a frame index into the local object block
400 void mapLocalFrameObject(int ObjectIndex, int64_t Offset) {
401 LocalFrameObjects.push_back(std::pair<int, int64_t>(ObjectIndex, Offset));
402 Objects[ObjectIndex + NumFixedObjects].PreAllocated = true;
403 }
404
405 /// Get the local offset mapping for a for an object.
406 std::pair<int, int64_t> getLocalFrameObjectMap(int i) const {
407 assert (i >= 0 && (unsigned)i < LocalFrameObjects.size() &&
408 "Invalid local object reference!");
409 return LocalFrameObjects[i];
410 }
411
412 /// Return the number of objects allocated into the local object block.
413 int64_t getLocalFrameObjectCount() const { return LocalFrameObjects.size(); }
414
415 /// Set the size of the local object blob.
416 void setLocalFrameSize(int64_t sz) { LocalFrameSize = sz; }
417
418 /// Get the size of the local object blob.
419 int64_t getLocalFrameSize() const { return LocalFrameSize; }
420
421 /// Required alignment of the local object blob,
422 /// which is the strictest alignment of any object in it.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200423 void setLocalFrameMaxAlign(Align Alignment) {
424 LocalFrameMaxAlign = Alignment;
425 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100426
427 /// Return the required alignment of the local object blob.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200428 Align getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100429
430 /// Get whether the local allocation blob should be allocated together or
431 /// let PEI allocate the locals in it directly.
432 bool getUseLocalStackAllocationBlock() const {
433 return UseLocalStackAllocationBlock;
434 }
435
436 /// setUseLocalStackAllocationBlock - Set whether the local allocation blob
437 /// should be allocated together or let PEI allocate the locals in it
438 /// directly.
439 void setUseLocalStackAllocationBlock(bool v) {
440 UseLocalStackAllocationBlock = v;
441 }
442
443 /// Return true if the object was pre-allocated into the local block.
444 bool isObjectPreAllocated(int ObjectIdx) const {
445 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
446 "Invalid Object Idx!");
447 return Objects[ObjectIdx+NumFixedObjects].PreAllocated;
448 }
449
450 /// Return the size of the specified object.
451 int64_t getObjectSize(int ObjectIdx) const {
452 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
453 "Invalid Object Idx!");
454 return Objects[ObjectIdx+NumFixedObjects].Size;
455 }
456
457 /// Change the size of the specified stack object.
458 void setObjectSize(int ObjectIdx, int64_t Size) {
459 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
460 "Invalid Object Idx!");
461 Objects[ObjectIdx+NumFixedObjects].Size = Size;
462 }
463
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200464 LLVM_ATTRIBUTE_DEPRECATED(inline unsigned getObjectAlignment(int ObjectIdx)
465 const,
466 "Use getObjectAlign instead") {
467 return getObjectAlign(ObjectIdx).value();
468 }
469
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100470 /// Return the alignment of the specified stack object.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200471 Align getObjectAlign(int ObjectIdx) const {
472 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100473 "Invalid Object Idx!");
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200474 return Objects[ObjectIdx + NumFixedObjects].Alignment;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100475 }
476
477 /// setObjectAlignment - Change the alignment of the specified stack object.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200478 void setObjectAlignment(int ObjectIdx, Align Alignment) {
479 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100480 "Invalid Object Idx!");
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200481 Objects[ObjectIdx + NumFixedObjects].Alignment = Alignment;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100482
483 // Only ensure max alignment for the default stack.
484 if (getStackID(ObjectIdx) == 0)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200485 ensureMaxAlignment(Alignment);
486 }
487
488 LLVM_ATTRIBUTE_DEPRECATED(inline void setObjectAlignment(int ObjectIdx,
489 unsigned Align),
490 "Use the version that takes Align instead") {
491 setObjectAlignment(ObjectIdx, assumeAligned(Align));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100492 }
493
494 /// Return the underlying Alloca of the specified
495 /// stack object if it exists. Returns 0 if none exists.
496 const AllocaInst* getObjectAllocation(int ObjectIdx) const {
497 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
498 "Invalid Object Idx!");
499 return Objects[ObjectIdx+NumFixedObjects].Alloca;
500 }
501
502 /// Return the assigned stack offset of the specified object
503 /// from the incoming stack pointer.
504 int64_t getObjectOffset(int ObjectIdx) const {
505 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
506 "Invalid Object Idx!");
507 assert(!isDeadObjectIndex(ObjectIdx) &&
508 "Getting frame offset for a dead object?");
509 return Objects[ObjectIdx+NumFixedObjects].SPOffset;
510 }
511
512 bool isObjectZExt(int ObjectIdx) const {
513 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
514 "Invalid Object Idx!");
515 return Objects[ObjectIdx+NumFixedObjects].isZExt;
516 }
517
518 void setObjectZExt(int ObjectIdx, bool IsZExt) {
519 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
520 "Invalid Object Idx!");
521 Objects[ObjectIdx+NumFixedObjects].isZExt = IsZExt;
522 }
523
524 bool isObjectSExt(int ObjectIdx) const {
525 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
526 "Invalid Object Idx!");
527 return Objects[ObjectIdx+NumFixedObjects].isSExt;
528 }
529
530 void setObjectSExt(int ObjectIdx, bool IsSExt) {
531 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
532 "Invalid Object Idx!");
533 Objects[ObjectIdx+NumFixedObjects].isSExt = IsSExt;
534 }
535
536 /// Set the stack frame offset of the specified object. The
537 /// offset is relative to the stack pointer on entry to the function.
538 void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
539 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
540 "Invalid Object Idx!");
541 assert(!isDeadObjectIndex(ObjectIdx) &&
542 "Setting frame offset for a dead object?");
543 Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
544 }
545
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100546 SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const {
547 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
548 "Invalid Object Idx!");
549 return (SSPLayoutKind)Objects[ObjectIdx+NumFixedObjects].SSPLayout;
550 }
551
552 void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind) {
553 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
554 "Invalid Object Idx!");
555 assert(!isDeadObjectIndex(ObjectIdx) &&
556 "Setting SSP layout for a dead object?");
557 Objects[ObjectIdx+NumFixedObjects].SSPLayout = Kind;
558 }
559
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100560 /// Return the number of bytes that must be allocated to hold
561 /// all of the fixed size frame objects. This is only valid after
562 /// Prolog/Epilog code insertion has finalized the stack frame layout.
563 uint64_t getStackSize() const { return StackSize; }
564
565 /// Set the size of the stack.
566 void setStackSize(uint64_t Size) { StackSize = Size; }
567
568 /// Estimate and return the size of the stack frame.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200569 uint64_t estimateStackSize(const MachineFunction &MF) const;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100570
571 /// Return the correction for frame offsets.
572 int getOffsetAdjustment() const { return OffsetAdjustment; }
573
574 /// Set the correction for frame offsets.
575 void setOffsetAdjustment(int Adj) { OffsetAdjustment = Adj; }
576
577 /// Return the alignment in bytes that this function must be aligned to,
578 /// which is greater than the default stack alignment provided by the target.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200579 LLVM_ATTRIBUTE_DEPRECATED(unsigned getMaxAlignment() const,
580 "Use getMaxAlign instead") {
581 return MaxAlignment.value();
582 }
583 /// Return the alignment in bytes that this function must be aligned to,
584 /// which is greater than the default stack alignment provided by the target.
585 Align getMaxAlign() const { return MaxAlignment; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100586
587 /// Make sure the function is at least Align bytes aligned.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200588 void ensureMaxAlignment(Align Alignment);
589
590 LLVM_ATTRIBUTE_DEPRECATED(inline void ensureMaxAlignment(unsigned Align),
591 "Use the version that uses Align instead") {
592 ensureMaxAlignment(assumeAligned(Align));
593 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100594
595 /// Return true if this function adjusts the stack -- e.g.,
596 /// when calling another function. This is only valid during and after
597 /// prolog/epilog code insertion.
598 bool adjustsStack() const { return AdjustsStack; }
599 void setAdjustsStack(bool V) { AdjustsStack = V; }
600
601 /// Return true if the current function has any function calls.
602 bool hasCalls() const { return HasCalls; }
603 void setHasCalls(bool V) { HasCalls = V; }
604
605 /// Returns true if the function contains opaque dynamic stack adjustments.
606 bool hasOpaqueSPAdjustment() const { return HasOpaqueSPAdjustment; }
607 void setHasOpaqueSPAdjustment(bool B) { HasOpaqueSPAdjustment = B; }
608
609 /// Returns true if the function contains operations which will lower down to
610 /// instructions which manipulate the stack pointer.
611 bool hasCopyImplyingStackAdjustment() const {
612 return HasCopyImplyingStackAdjustment;
613 }
614 void setHasCopyImplyingStackAdjustment(bool B) {
615 HasCopyImplyingStackAdjustment = B;
616 }
617
618 /// Returns true if the function calls the llvm.va_start intrinsic.
619 bool hasVAStart() const { return HasVAStart; }
620 void setHasVAStart(bool B) { HasVAStart = B; }
621
622 /// Returns true if the function is variadic and contains a musttail call.
623 bool hasMustTailInVarArgFunc() const { return HasMustTailInVarArgFunc; }
624 void setHasMustTailInVarArgFunc(bool B) { HasMustTailInVarArgFunc = B; }
625
626 /// Returns true if the function contains a tail call.
627 bool hasTailCall() const { return HasTailCall; }
628 void setHasTailCall() { HasTailCall = true; }
629
630 /// Computes the maximum size of a callframe and the AdjustsStack property.
631 /// This only works for targets defining
632 /// TargetInstrInfo::getCallFrameSetupOpcode(), getCallFrameDestroyOpcode(),
633 /// and getFrameSize().
634 /// This is usually computed by the prologue epilogue inserter but some
635 /// targets may call this to compute it earlier.
636 void computeMaxCallFrameSize(const MachineFunction &MF);
637
638 /// Return the maximum size of a call frame that must be
639 /// allocated for an outgoing function call. This is only available if
640 /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
641 /// then only during or after prolog/epilog code insertion.
642 ///
643 unsigned getMaxCallFrameSize() const {
644 // TODO: Enable this assert when targets are fixed.
645 //assert(isMaxCallFrameSizeComputed() && "MaxCallFrameSize not computed yet");
646 if (!isMaxCallFrameSizeComputed())
647 return 0;
648 return MaxCallFrameSize;
649 }
650 bool isMaxCallFrameSizeComputed() const {
651 return MaxCallFrameSize != ~0u;
652 }
653 void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
654
Andrew Scull0372a572018-11-16 15:47:06 +0000655 /// Returns how many bytes of callee-saved registers the target pushed in the
656 /// prologue. Only used for debug info.
657 unsigned getCVBytesOfCalleeSavedRegisters() const {
658 return CVBytesOfCalleeSavedRegisters;
659 }
660 void setCVBytesOfCalleeSavedRegisters(unsigned S) {
661 CVBytesOfCalleeSavedRegisters = S;
662 }
663
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100664 /// Create a new object at a fixed location on the stack.
665 /// All fixed objects should be created before other objects are created for
666 /// efficiency. By default, fixed objects are not pointed to by LLVM IR
667 /// values. This returns an index with a negative value.
668 int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable,
669 bool isAliased = false);
670
671 /// Create a spill slot at a fixed location on the stack.
672 /// Returns an index with a negative value.
673 int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset,
674 bool IsImmutable = false);
675
676 /// Returns true if the specified index corresponds to a fixed stack object.
677 bool isFixedObjectIndex(int ObjectIdx) const {
678 return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
679 }
680
681 /// Returns true if the specified index corresponds
682 /// to an object that might be pointed to by an LLVM IR value.
683 bool isAliasedObjectIndex(int ObjectIdx) const {
684 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
685 "Invalid Object Idx!");
686 return Objects[ObjectIdx+NumFixedObjects].isAliased;
687 }
688
689 /// Returns true if the specified index corresponds to an immutable object.
690 bool isImmutableObjectIndex(int ObjectIdx) const {
691 // Tail calling functions can clobber their function arguments.
692 if (HasTailCall)
693 return false;
694 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
695 "Invalid Object Idx!");
696 return Objects[ObjectIdx+NumFixedObjects].isImmutable;
697 }
698
699 /// Marks the immutability of an object.
700 void setIsImmutableObjectIndex(int ObjectIdx, bool IsImmutable) {
701 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
702 "Invalid Object Idx!");
703 Objects[ObjectIdx+NumFixedObjects].isImmutable = IsImmutable;
704 }
705
706 /// Returns true if the specified index corresponds to a spill slot.
707 bool isSpillSlotObjectIndex(int ObjectIdx) const {
708 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
709 "Invalid Object Idx!");
710 return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;
711 }
712
713 bool isStatepointSpillSlotObjectIndex(int ObjectIdx) const {
714 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
715 "Invalid Object Idx!");
716 return Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot;
717 }
718
719 /// \see StackID
720 uint8_t getStackID(int ObjectIdx) const {
721 return Objects[ObjectIdx+NumFixedObjects].StackID;
722 }
723
724 /// \see StackID
725 void setStackID(int ObjectIdx, uint8_t ID) {
726 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
727 "Invalid Object Idx!");
728 Objects[ObjectIdx+NumFixedObjects].StackID = ID;
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100729 // If ID > 0, MaxAlignment may now be overly conservative.
730 // If ID == 0, MaxAlignment will need to be updated separately.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100731 }
732
733 /// Returns true if the specified index corresponds to a dead object.
734 bool isDeadObjectIndex(int ObjectIdx) const {
735 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
736 "Invalid Object Idx!");
737 return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
738 }
739
740 /// Returns true if the specified index corresponds to a variable sized
741 /// object.
742 bool isVariableSizedObjectIndex(int ObjectIdx) const {
743 assert(unsigned(ObjectIdx + NumFixedObjects) < Objects.size() &&
744 "Invalid Object Idx!");
745 return Objects[ObjectIdx + NumFixedObjects].Size == 0;
746 }
747
748 void markAsStatepointSpillSlotObjectIndex(int ObjectIdx) {
749 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
750 "Invalid Object Idx!");
751 Objects[ObjectIdx+NumFixedObjects].isStatepointSpillSlot = true;
752 assert(isStatepointSpillSlotObjectIndex(ObjectIdx) && "inconsistent");
753 }
754
755 /// Create a new statically sized stack object, returning
756 /// a nonnegative identifier to represent it.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200757 int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100758 const AllocaInst *Alloca = nullptr, uint8_t ID = 0);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200759 LLVM_ATTRIBUTE_DEPRECATED(
760 inline int CreateStackObject(uint64_t Size, unsigned Alignment,
761 bool isSpillSlot,
762 const AllocaInst *Alloca = nullptr,
763 uint8_t ID = 0),
764 "Use CreateStackObject that takes an Align instead") {
765 return CreateStackObject(Size, assumeAligned(Alignment), isSpillSlot,
766 Alloca, ID);
767 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100768
769 /// Create a new statically sized stack object that represents a spill slot,
770 /// returning a nonnegative identifier to represent it.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200771 int CreateSpillStackObject(uint64_t Size, Align Alignment);
772 LLVM_ATTRIBUTE_DEPRECATED(
773 inline int CreateSpillStackObject(uint64_t Size, unsigned Alignment),
774 "Use CreateSpillStackObject that takes an Align instead") {
775 return CreateSpillStackObject(Size, assumeAligned(Alignment));
776 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100777
778 /// Remove or mark dead a statically sized stack object.
779 void RemoveStackObject(int ObjectIdx) {
780 // Mark it dead.
781 Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
782 }
783
784 /// Notify the MachineFrameInfo object that a variable sized object has been
785 /// created. This must be created whenever a variable sized object is
786 /// created, whether or not the index returned is actually used.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200787 int CreateVariableSizedObject(Align Alignment, const AllocaInst *Alloca);
788 /// FIXME: Remove this function when transition to Align is over.
789 LLVM_ATTRIBUTE_DEPRECATED(int CreateVariableSizedObject(
790 unsigned Alignment, const AllocaInst *Alloca),
791 "Use the version that takes an Align instead") {
792 return CreateVariableSizedObject(assumeAligned(Alignment), Alloca);
793 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100794
795 /// Returns a reference to call saved info vector for the current function.
796 const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
797 return CSInfo;
798 }
799 /// \copydoc getCalleeSavedInfo()
800 std::vector<CalleeSavedInfo> &getCalleeSavedInfo() { return CSInfo; }
801
802 /// Used by prolog/epilog inserter to set the function's callee saved
803 /// information.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200804 void setCalleeSavedInfo(std::vector<CalleeSavedInfo> CSI) {
805 CSInfo = std::move(CSI);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100806 }
807
808 /// Has the callee saved info been calculated yet?
809 bool isCalleeSavedInfoValid() const { return CSIValid; }
810
811 void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
812
813 MachineBasicBlock *getSavePoint() const { return Save; }
814 void setSavePoint(MachineBasicBlock *NewSave) { Save = NewSave; }
815 MachineBasicBlock *getRestorePoint() const { return Restore; }
816 void setRestorePoint(MachineBasicBlock *NewRestore) { Restore = NewRestore; }
817
818 /// Return a set of physical registers that are pristine.
819 ///
820 /// Pristine registers hold a value that is useless to the current function,
821 /// but that must be preserved - they are callee saved registers that are not
822 /// saved.
823 ///
824 /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
825 /// method always returns an empty set.
826 BitVector getPristineRegs(const MachineFunction &MF) const;
827
828 /// Used by the MachineFunction printer to print information about
829 /// stack objects. Implemented in MachineFunction.cpp.
830 void print(const MachineFunction &MF, raw_ostream &OS) const;
831
832 /// dump - Print the function to stderr.
833 void dump(const MachineFunction &MF) const;
834};
835
836} // End llvm namespace
837
838#endif