1 //===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The file defines the MachineFrameInfo class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEFRAMEINFO_H
15 #define LLVM_CODEGEN_MACHINEFRAMEINFO_H
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Support/DataTypes.h"
25 class TargetRegisterClass;
27 class MachineFunction;
28 class MachineBasicBlock;
29 class TargetFrameLowering;
35 /// The CalleeSavedInfo class tracks the information need to locate where a
36 /// callee saved register is in the current frame.
37 class CalleeSavedInfo {
42 explicit CalleeSavedInfo(unsigned R, int FI = 0)
43 : Reg(R), FrameIdx(FI) {}
46 unsigned getReg() const { return Reg; }
47 int getFrameIdx() const { return FrameIdx; }
48 void setFrameIdx(int FI) { FrameIdx = FI; }
51 /// The MachineFrameInfo class represents an abstract stack frame until
52 /// prolog/epilog code is inserted. This class is key to allowing stack frame
53 /// representation optimizations, such as frame pointer elimination. It also
54 /// allows more mundane (but still important) optimizations, such as reordering
55 /// of abstract objects on the stack frame.
57 /// To support this, the class assigns unique integer identifiers to stack
58 /// objects requested clients. These identifiers are negative integers for
59 /// fixed stack objects (such as arguments passed on the stack) or nonnegative
60 /// for objects that may be reordered. Instructions which refer to stack
61 /// objects use a special MO_FrameIndex operand to represent these frame
64 /// Because this class keeps track of all references to the stack frame, it
65 /// knows when a variable sized object is allocated on the stack. This is the
66 /// sole condition which prevents frame pointer elimination, which is an
67 /// important optimization on register-poor architectures. Because original
68 /// variable sized alloca's in the source program are the only source of
69 /// variable sized stack objects, it is safe to decide whether there will be
70 /// any variable sized objects before all stack objects are known (for
71 /// example, register allocator spill code never needs variable sized
74 /// When prolog/epilog code emission is performed, the final stack frame is
75 /// built and the machine instructions are modified to refer to the actual
76 /// stack offsets of the object, eliminating all MO_FrameIndex operands from
79 /// @brief Abstract Stack Frame Information
80 class MachineFrameInfo {
82 // StackObject - Represent a single object allocated on the stack.
84 // SPOffset - The offset of this object from the stack pointer on entry to
85 // the function. This field has no meaning for a variable sized element.
88 // The size of this object on the stack. 0 means a variable sized object,
89 // ~0ULL means a dead object.
92 // Alignment - The required alignment of this stack slot.
95 // isImmutable - If true, the value of the stack object is set before
96 // entering the function and is not modified inside the function. By
97 // default, fixed objects are immutable unless marked otherwise.
100 // isSpillSlot - If true the stack object is used as spill slot. It
101 // cannot alias any other memory objects.
104 /// Alloca - If this stack object is originated from an Alloca instruction
105 /// this value saves the original IR allocation. Can be NULL.
106 const AllocaInst *Alloca;
108 // PreAllocated - If true, the object was mapped into the local frame
109 // block and doesn't need additional handling for allocation beyond that.
112 StackObject(uint64_t Sz, unsigned Al, int64_t SP, bool IM,
113 bool isSS, const AllocaInst *Val)
114 : SPOffset(SP), Size(Sz), Alignment(Al), isImmutable(IM),
115 isSpillSlot(isSS), Alloca(Val), PreAllocated(false) {}
118 const TargetMachine &TM;
120 /// Objects - The list of stack objects allocated...
122 std::vector<StackObject> Objects;
124 /// NumFixedObjects - This contains the number of fixed objects contained on
125 /// the stack. Because fixed objects are stored at a negative index in the
126 /// Objects list, this is also the index to the 0th object in the list.
128 unsigned NumFixedObjects;
130 /// HasVarSizedObjects - This boolean keeps track of whether any variable
131 /// sized objects have been allocated yet.
133 bool HasVarSizedObjects;
135 /// FrameAddressTaken - This boolean keeps track of whether there is a call
136 /// to builtin \@llvm.frameaddress.
137 bool FrameAddressTaken;
139 /// ReturnAddressTaken - This boolean keeps track of whether there is a call
140 /// to builtin \@llvm.returnaddress.
141 bool ReturnAddressTaken;
143 /// HasStackMap - This boolean keeps track of whether there is a call
144 /// to builtin \@llvm.experimental.stackmap.
147 /// HasPatchPoint - This boolean keeps track of whether there is a call
148 /// to builtin \@llvm.experimental.patchpoint.
151 /// StackSize - The prolog/epilog code inserter calculates the final stack
152 /// offsets for all of the fixed size objects, updating the Objects list
153 /// above. It then updates StackSize to contain the number of bytes that need
154 /// to be allocated on entry to the function.
158 /// OffsetAdjustment - The amount that a frame offset needs to be adjusted to
159 /// have the actual offset from the stack/frame pointer. The exact usage of
160 /// this is target-dependent, but it is typically used to adjust between
161 /// SP-relative and FP-relative offsets. E.G., if objects are accessed via
162 /// SP then OffsetAdjustment is zero; if FP is used, OffsetAdjustment is set
163 /// to the distance between the initial SP and the value in FP. For many
164 /// targets, this value is only used when generating debug info (via
165 /// TargetRegisterInfo::getFrameIndexOffset); when generating code, the
166 /// corresponding adjustments are performed directly.
167 int OffsetAdjustment;
169 /// MaxAlignment - The prolog/epilog code inserter may process objects
170 /// that require greater alignment than the default alignment the target
171 /// provides. To handle this, MaxAlignment is set to the maximum alignment
172 /// needed by the objects on the current frame. If this is greater than the
173 /// native alignment maintained by the compiler, dynamic alignment code will
176 unsigned MaxAlignment;
178 /// AdjustsStack - Set to true if this function adjusts the stack -- e.g.,
179 /// when calling another function. This is only valid during and after
180 /// prolog/epilog code insertion.
183 /// HasCalls - Set to true if this function has any function calls.
186 /// StackProtectorIdx - The frame index for the stack protector.
187 int StackProtectorIdx;
189 /// FunctionContextIdx - The frame index for the function context. Used for
191 int FunctionContextIdx;
193 /// MaxCallFrameSize - This contains the size of the largest call frame if the
194 /// target uses frame setup/destroy pseudo instructions (as defined in the
195 /// TargetFrameInfo class). This information is important for frame pointer
196 /// elimination. If is only valid during and after prolog/epilog code
199 unsigned MaxCallFrameSize;
201 /// CSInfo - The prolog/epilog code inserter fills in this vector with each
202 /// callee saved register saved in the frame. Beyond its use by the prolog/
203 /// epilog code inserter, this data used for debug info and exception
205 std::vector<CalleeSavedInfo> CSInfo;
207 /// CSIValid - Has CSInfo been set yet?
210 /// LocalFrameObjects - References to frame indices which are mapped
211 /// into the local frame allocation block. <FrameIdx, LocalOffset>
212 SmallVector<std::pair<int, int64_t>, 32> LocalFrameObjects;
214 /// LocalFrameSize - Size of the pre-allocated local frame block.
215 int64_t LocalFrameSize;
217 /// Required alignment of the local object blob, which is the strictest
218 /// alignment of any object in it.
219 unsigned LocalFrameMaxAlign;
221 /// Whether the local object blob needs to be allocated together. If not,
222 /// PEI should ignore the isPreAllocated flags on the stack objects and
223 /// just allocate them normally.
224 bool UseLocalStackAllocationBlock;
226 /// Whether the "realign-stack" option is on.
229 /// True if the function includes inline assembly that adjusts the stack
231 bool HasInlineAsmWithSPAdjust;
233 const TargetFrameLowering *getFrameLowering() const;
235 explicit MachineFrameInfo(const TargetMachine &TM, bool RealignOpt)
236 : TM(TM), RealignOption(RealignOpt) {
237 StackSize = NumFixedObjects = OffsetAdjustment = MaxAlignment = 0;
238 HasVarSizedObjects = false;
239 FrameAddressTaken = false;
240 ReturnAddressTaken = false;
242 HasPatchPoint = false;
243 AdjustsStack = false;
245 StackProtectorIdx = -1;
246 FunctionContextIdx = -1;
247 MaxCallFrameSize = 0;
250 LocalFrameMaxAlign = 0;
251 UseLocalStackAllocationBlock = false;
254 /// hasStackObjects - Return true if there are any stack objects in this
257 bool hasStackObjects() const { return !Objects.empty(); }
259 /// hasVarSizedObjects - This method may be called any time after instruction
260 /// selection is complete to determine if the stack frame for this function
261 /// contains any variable sized objects.
263 bool hasVarSizedObjects() const { return HasVarSizedObjects; }
265 /// getStackProtectorIndex/setStackProtectorIndex - Return the index for the
266 /// stack protector object.
268 int getStackProtectorIndex() const { return StackProtectorIdx; }
269 void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
271 /// getFunctionContextIndex/setFunctionContextIndex - Return the index for the
272 /// function context object. This object is used for SjLj exceptions.
273 int getFunctionContextIndex() const { return FunctionContextIdx; }
274 void setFunctionContextIndex(int I) { FunctionContextIdx = I; }
276 /// isFrameAddressTaken - This method may be called any time after instruction
277 /// selection is complete to determine if there is a call to
278 /// \@llvm.frameaddress in this function.
279 bool isFrameAddressTaken() const { return FrameAddressTaken; }
280 void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; }
282 /// isReturnAddressTaken - This method may be called any time after
283 /// instruction selection is complete to determine if there is a call to
284 /// \@llvm.returnaddress in this function.
285 bool isReturnAddressTaken() const { return ReturnAddressTaken; }
286 void setReturnAddressIsTaken(bool s) { ReturnAddressTaken = s; }
288 /// hasStackMap - This method may be called any time after instruction
289 /// selection is complete to determine if there is a call to builtin
290 /// \@llvm.experimental.stackmap.
291 bool hasStackMap() const { return HasStackMap; }
292 void setHasStackMap(bool s = true) { HasStackMap = s; }
294 /// hasPatchPoint - This method may be called any time after instruction
295 /// selection is complete to determine if there is a call to builtin
296 /// \@llvm.experimental.patchpoint.
297 bool hasPatchPoint() const { return HasPatchPoint; }
298 void setHasPatchPoint(bool s = true) { HasPatchPoint = s; }
300 /// getObjectIndexBegin - Return the minimum frame object index.
302 int getObjectIndexBegin() const { return -NumFixedObjects; }
304 /// getObjectIndexEnd - Return one past the maximum frame object index.
306 int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
308 /// getNumFixedObjects - Return the number of fixed objects.
309 unsigned getNumFixedObjects() const { return NumFixedObjects; }
311 /// getNumObjects - Return the number of objects.
313 unsigned getNumObjects() const { return Objects.size(); }
315 /// mapLocalFrameObject - Map a frame index into the local object block
316 void mapLocalFrameObject(int ObjectIndex, int64_t Offset) {
317 LocalFrameObjects.push_back(std::pair<int, int64_t>(ObjectIndex, Offset));
318 Objects[ObjectIndex + NumFixedObjects].PreAllocated = true;
321 /// getLocalFrameObjectMap - Get the local offset mapping for a for an object
322 std::pair<int, int64_t> getLocalFrameObjectMap(int i) {
323 assert (i >= 0 && (unsigned)i < LocalFrameObjects.size() &&
324 "Invalid local object reference!");
325 return LocalFrameObjects[i];
328 /// getLocalFrameObjectCount - Return the number of objects allocated into
329 /// the local object block.
330 int64_t getLocalFrameObjectCount() { return LocalFrameObjects.size(); }
332 /// setLocalFrameSize - Set the size of the local object blob.
333 void setLocalFrameSize(int64_t sz) { LocalFrameSize = sz; }
335 /// getLocalFrameSize - Get the size of the local object blob.
336 int64_t getLocalFrameSize() const { return LocalFrameSize; }
338 /// setLocalFrameMaxAlign - Required alignment of the local object blob,
339 /// which is the strictest alignment of any object in it.
340 void setLocalFrameMaxAlign(unsigned Align) { LocalFrameMaxAlign = Align; }
342 /// getLocalFrameMaxAlign - Return the required alignment of the local
344 unsigned getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
346 /// getUseLocalStackAllocationBlock - Get whether the local allocation blob
347 /// should be allocated together or let PEI allocate the locals in it
349 bool getUseLocalStackAllocationBlock() {return UseLocalStackAllocationBlock;}
351 /// setUseLocalStackAllocationBlock - Set whether the local allocation blob
352 /// should be allocated together or let PEI allocate the locals in it
354 void setUseLocalStackAllocationBlock(bool v) {
355 UseLocalStackAllocationBlock = v;
358 /// isObjectPreAllocated - Return true if the object was pre-allocated into
360 bool isObjectPreAllocated(int ObjectIdx) const {
361 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
362 "Invalid Object Idx!");
363 return Objects[ObjectIdx+NumFixedObjects].PreAllocated;
366 /// getObjectSize - Return the size of the specified object.
368 int64_t getObjectSize(int ObjectIdx) const {
369 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
370 "Invalid Object Idx!");
371 return Objects[ObjectIdx+NumFixedObjects].Size;
374 /// setObjectSize - Change the size of the specified stack object.
375 void setObjectSize(int ObjectIdx, int64_t Size) {
376 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
377 "Invalid Object Idx!");
378 Objects[ObjectIdx+NumFixedObjects].Size = Size;
381 /// getObjectAlignment - Return the alignment of the specified stack object.
382 unsigned getObjectAlignment(int ObjectIdx) const {
383 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
384 "Invalid Object Idx!");
385 return Objects[ObjectIdx+NumFixedObjects].Alignment;
388 /// setObjectAlignment - Change the alignment of the specified stack object.
389 void setObjectAlignment(int ObjectIdx, unsigned Align) {
390 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
391 "Invalid Object Idx!");
392 Objects[ObjectIdx+NumFixedObjects].Alignment = Align;
393 ensureMaxAlignment(Align);
396 /// getObjectAllocation - Return the underlying Alloca of the specified
397 /// stack object if it exists. Returns 0 if none exists.
398 const AllocaInst* getObjectAllocation(int ObjectIdx) const {
399 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
400 "Invalid Object Idx!");
401 return Objects[ObjectIdx+NumFixedObjects].Alloca;
404 /// getObjectOffset - Return the assigned stack offset of the specified object
405 /// from the incoming stack pointer.
407 int64_t getObjectOffset(int ObjectIdx) const {
408 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
409 "Invalid Object Idx!");
410 assert(!isDeadObjectIndex(ObjectIdx) &&
411 "Getting frame offset for a dead object?");
412 return Objects[ObjectIdx+NumFixedObjects].SPOffset;
415 /// setObjectOffset - Set the stack frame offset of the specified object. The
416 /// offset is relative to the stack pointer on entry to the function.
418 void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
419 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
420 "Invalid Object Idx!");
421 assert(!isDeadObjectIndex(ObjectIdx) &&
422 "Setting frame offset for a dead object?");
423 Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
426 /// getStackSize - Return the number of bytes that must be allocated to hold
427 /// all of the fixed size frame objects. This is only valid after
428 /// Prolog/Epilog code insertion has finalized the stack frame layout.
430 uint64_t getStackSize() const { return StackSize; }
432 /// setStackSize - Set the size of the stack...
434 void setStackSize(uint64_t Size) { StackSize = Size; }
436 /// Estimate and return the size of the stack frame.
437 unsigned estimateStackSize(const MachineFunction &MF) const;
439 /// getOffsetAdjustment - Return the correction for frame offsets.
441 int getOffsetAdjustment() const { return OffsetAdjustment; }
443 /// setOffsetAdjustment - Set the correction for frame offsets.
445 void setOffsetAdjustment(int Adj) { OffsetAdjustment = Adj; }
447 /// getMaxAlignment - Return the alignment in bytes that this function must be
448 /// aligned to, which is greater than the default stack alignment provided by
451 unsigned getMaxAlignment() const { return MaxAlignment; }
453 /// ensureMaxAlignment - Make sure the function is at least Align bytes
455 void ensureMaxAlignment(unsigned Align);
457 /// AdjustsStack - Return true if this function adjusts the stack -- e.g.,
458 /// when calling another function. This is only valid during and after
459 /// prolog/epilog code insertion.
460 bool adjustsStack() const { return AdjustsStack; }
461 void setAdjustsStack(bool V) { AdjustsStack = V; }
463 /// hasCalls - Return true if the current function has any function calls.
464 bool hasCalls() const { return HasCalls; }
465 void setHasCalls(bool V) { HasCalls = V; }
467 /// Returns true if the function contains any stack-adjusting inline assembly.
468 bool hasInlineAsmWithSPAdjust() const { return HasInlineAsmWithSPAdjust; }
469 void setHasInlineAsmWithSPAdjust(bool B) { HasInlineAsmWithSPAdjust = B; }
471 /// getMaxCallFrameSize - Return the maximum size of a call frame that must be
472 /// allocated for an outgoing function call. This is only available if
473 /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
474 /// then only during or after prolog/epilog code insertion.
476 unsigned getMaxCallFrameSize() const { return MaxCallFrameSize; }
477 void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
479 /// CreateFixedObject - Create a new object at a fixed location on the stack.
480 /// All fixed objects should be created before other objects are created for
481 /// efficiency. By default, fixed objects are immutable. This returns an
482 /// index with a negative value.
484 int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool Immutable);
487 /// isFixedObjectIndex - Returns true if the specified index corresponds to a
488 /// fixed stack object.
489 bool isFixedObjectIndex(int ObjectIdx) const {
490 return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
493 /// isImmutableObjectIndex - Returns true if the specified index corresponds
494 /// to an immutable object.
495 bool isImmutableObjectIndex(int ObjectIdx) const {
496 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
497 "Invalid Object Idx!");
498 return Objects[ObjectIdx+NumFixedObjects].isImmutable;
501 /// isSpillSlotObjectIndex - Returns true if the specified index corresponds
502 /// to a spill slot..
503 bool isSpillSlotObjectIndex(int ObjectIdx) const {
504 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
505 "Invalid Object Idx!");
506 return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;
509 /// isDeadObjectIndex - Returns true if the specified index corresponds to
511 bool isDeadObjectIndex(int ObjectIdx) const {
512 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
513 "Invalid Object Idx!");
514 return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
517 /// CreateStackObject - Create a new statically sized stack object, returning
518 /// a nonnegative identifier to represent it.
520 int CreateStackObject(uint64_t Size, unsigned Alignment, bool isSS,
521 const AllocaInst *Alloca = 0);
523 /// CreateSpillStackObject - Create a new statically sized stack object that
524 /// represents a spill slot, returning a nonnegative identifier to represent
527 int CreateSpillStackObject(uint64_t Size, unsigned Alignment);
529 /// RemoveStackObject - Remove or mark dead a statically sized stack object.
531 void RemoveStackObject(int ObjectIdx) {
533 Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
536 /// CreateVariableSizedObject - Notify the MachineFrameInfo object that a
537 /// variable sized object has been created. This must be created whenever a
538 /// variable sized object is created, whether or not the index returned is
541 int CreateVariableSizedObject(unsigned Alignment, const AllocaInst *Alloca);
543 /// getCalleeSavedInfo - Returns a reference to call saved info vector for the
544 /// current function.
545 const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
549 /// setCalleeSavedInfo - Used by prolog/epilog inserter to set the function's
550 /// callee saved information.
551 void setCalleeSavedInfo(const std::vector<CalleeSavedInfo> &CSI) {
555 /// isCalleeSavedInfoValid - Has the callee saved info been calculated yet?
556 bool isCalleeSavedInfoValid() const { return CSIValid; }
558 void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
560 /// getPristineRegs - Return a set of physical registers that are pristine on
561 /// entry to the MBB.
563 /// Pristine registers hold a value that is useless to the current function,
564 /// but that must be preserved - they are callee saved registers that have not
567 /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
568 /// method always returns an empty set.
569 BitVector getPristineRegs(const MachineBasicBlock *MBB) const;
571 /// print - Used by the MachineFunction printer to print information about
572 /// stack objects. Implemented in MachineFunction.cpp
574 void print(const MachineFunction &MF, raw_ostream &OS) const;
576 /// dump - Print the function to stderr.
577 void dump(const MachineFunction &MF) const;
580 } // End llvm namespace