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 // MayNeedSP - If true the stack object triggered the creation of the stack
105 // protector. We should allocate this object right after the stack
109 /// Alloca - If this stack object is originated from an Alloca instruction
110 /// this value saves the original IR allocation. Can be NULL.
111 const AllocaInst *Alloca;
113 // PreAllocated - If true, the object was mapped into the local frame
114 // block and doesn't need additional handling for allocation beyond that.
117 StackObject(uint64_t Sz, unsigned Al, int64_t SP, bool IM,
118 bool isSS, bool NSP, const AllocaInst *Val)
119 : SPOffset(SP), Size(Sz), Alignment(Al), isImmutable(IM),
120 isSpillSlot(isSS), MayNeedSP(NSP), Alloca(Val), PreAllocated(false) {}
123 const TargetMachine &TM;
125 /// Objects - The list of stack objects allocated...
127 std::vector<StackObject> Objects;
129 /// NumFixedObjects - This contains the number of fixed objects contained on
130 /// the stack. Because fixed objects are stored at a negative index in the
131 /// Objects list, this is also the index to the 0th object in the list.
133 unsigned NumFixedObjects;
135 /// HasVarSizedObjects - This boolean keeps track of whether any variable
136 /// sized objects have been allocated yet.
138 bool HasVarSizedObjects;
140 /// FrameAddressTaken - This boolean keeps track of whether there is a call
141 /// to builtin \@llvm.frameaddress.
142 bool FrameAddressTaken;
144 /// ReturnAddressTaken - This boolean keeps track of whether there is a call
145 /// to builtin \@llvm.returnaddress.
146 bool ReturnAddressTaken;
148 /// HasStackmap - This boolean keeps track of whether there is a call
149 /// to builtin \@llvm.experimental.stackmap or \@llvm.experimental.patchpoint.
152 /// StackSize - The prolog/epilog code inserter calculates the final stack
153 /// offsets for all of the fixed size objects, updating the Objects list
154 /// above. It then updates StackSize to contain the number of bytes that need
155 /// to be allocated on entry to the function.
159 /// OffsetAdjustment - The amount that a frame offset needs to be adjusted to
160 /// have the actual offset from the stack/frame pointer. The exact usage of
161 /// this is target-dependent, but it is typically used to adjust between
162 /// SP-relative and FP-relative offsets. E.G., if objects are accessed via
163 /// SP then OffsetAdjustment is zero; if FP is used, OffsetAdjustment is set
164 /// to the distance between the initial SP and the value in FP. For many
165 /// targets, this value is only used when generating debug info (via
166 /// TargetRegisterInfo::getFrameIndexOffset); when generating code, the
167 /// corresponding adjustments are performed directly.
168 int OffsetAdjustment;
170 /// MaxAlignment - The prolog/epilog code inserter may process objects
171 /// that require greater alignment than the default alignment the target
172 /// provides. To handle this, MaxAlignment is set to the maximum alignment
173 /// needed by the objects on the current frame. If this is greater than the
174 /// native alignment maintained by the compiler, dynamic alignment code will
177 unsigned MaxAlignment;
179 /// AdjustsStack - Set to true if this function adjusts the stack -- e.g.,
180 /// when calling another function. This is only valid during and after
181 /// prolog/epilog code insertion.
184 /// HasCalls - Set to true if this function has any function calls.
187 /// StackProtectorIdx - The frame index for the stack protector.
188 int StackProtectorIdx;
190 /// FunctionContextIdx - The frame index for the function context. Used for
192 int FunctionContextIdx;
194 /// MaxCallFrameSize - This contains the size of the largest call frame if the
195 /// target uses frame setup/destroy pseudo instructions (as defined in the
196 /// TargetFrameInfo class). This information is important for frame pointer
197 /// elimination. If is only valid during and after prolog/epilog code
200 unsigned MaxCallFrameSize;
202 /// CSInfo - The prolog/epilog code inserter fills in this vector with each
203 /// callee saved register saved in the frame. Beyond its use by the prolog/
204 /// epilog code inserter, this data used for debug info and exception
206 std::vector<CalleeSavedInfo> CSInfo;
208 /// CSIValid - Has CSInfo been set yet?
211 /// LocalFrameObjects - References to frame indices which are mapped
212 /// into the local frame allocation block. <FrameIdx, LocalOffset>
213 SmallVector<std::pair<int, int64_t>, 32> LocalFrameObjects;
215 /// LocalFrameSize - Size of the pre-allocated local frame block.
216 int64_t LocalFrameSize;
218 /// Required alignment of the local object blob, which is the strictest
219 /// alignment of any object in it.
220 unsigned LocalFrameMaxAlign;
222 /// Whether the local object blob needs to be allocated together. If not,
223 /// PEI should ignore the isPreAllocated flags on the stack objects and
224 /// just allocate them normally.
225 bool UseLocalStackAllocationBlock;
227 /// Whether the "realign-stack" option is on.
230 /// True if the function includes inline assembly that adjusts the stack
232 bool HasInlineAsmWithSPAdjust;
234 const TargetFrameLowering *getFrameLowering() const;
236 explicit MachineFrameInfo(const TargetMachine &TM, bool RealignOpt)
237 : TM(TM), RealignOption(RealignOpt) {
238 StackSize = NumFixedObjects = OffsetAdjustment = MaxAlignment = 0;
239 HasVarSizedObjects = false;
240 FrameAddressTaken = false;
241 ReturnAddressTaken = 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 or \@llvm.experimental.patchpoint.
291 bool hasStackMap() const { return HasStackMap; }
292 void setHasStackMap(bool s = true) { HasStackMap = s; }
294 /// getObjectIndexBegin - Return the minimum frame object index.
296 int getObjectIndexBegin() const { return -NumFixedObjects; }
298 /// getObjectIndexEnd - Return one past the maximum frame object index.
300 int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
302 /// getNumFixedObjects - Return the number of fixed objects.
303 unsigned getNumFixedObjects() const { return NumFixedObjects; }
305 /// getNumObjects - Return the number of objects.
307 unsigned getNumObjects() const { return Objects.size(); }
309 /// mapLocalFrameObject - Map a frame index into the local object block
310 void mapLocalFrameObject(int ObjectIndex, int64_t Offset) {
311 LocalFrameObjects.push_back(std::pair<int, int64_t>(ObjectIndex, Offset));
312 Objects[ObjectIndex + NumFixedObjects].PreAllocated = true;
315 /// getLocalFrameObjectMap - Get the local offset mapping for a for an object
316 std::pair<int, int64_t> getLocalFrameObjectMap(int i) {
317 assert (i >= 0 && (unsigned)i < LocalFrameObjects.size() &&
318 "Invalid local object reference!");
319 return LocalFrameObjects[i];
322 /// getLocalFrameObjectCount - Return the number of objects allocated into
323 /// the local object block.
324 int64_t getLocalFrameObjectCount() { return LocalFrameObjects.size(); }
326 /// setLocalFrameSize - Set the size of the local object blob.
327 void setLocalFrameSize(int64_t sz) { LocalFrameSize = sz; }
329 /// getLocalFrameSize - Get the size of the local object blob.
330 int64_t getLocalFrameSize() const { return LocalFrameSize; }
332 /// setLocalFrameMaxAlign - Required alignment of the local object blob,
333 /// which is the strictest alignment of any object in it.
334 void setLocalFrameMaxAlign(unsigned Align) { LocalFrameMaxAlign = Align; }
336 /// getLocalFrameMaxAlign - Return the required alignment of the local
338 unsigned getLocalFrameMaxAlign() const { return LocalFrameMaxAlign; }
340 /// getUseLocalStackAllocationBlock - Get whether the local allocation blob
341 /// should be allocated together or let PEI allocate the locals in it
343 bool getUseLocalStackAllocationBlock() {return UseLocalStackAllocationBlock;}
345 /// setUseLocalStackAllocationBlock - Set whether the local allocation blob
346 /// should be allocated together or let PEI allocate the locals in it
348 void setUseLocalStackAllocationBlock(bool v) {
349 UseLocalStackAllocationBlock = v;
352 /// isObjectPreAllocated - Return true if the object was pre-allocated into
354 bool isObjectPreAllocated(int ObjectIdx) const {
355 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
356 "Invalid Object Idx!");
357 return Objects[ObjectIdx+NumFixedObjects].PreAllocated;
360 /// getObjectSize - Return the size of the specified object.
362 int64_t getObjectSize(int ObjectIdx) const {
363 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
364 "Invalid Object Idx!");
365 return Objects[ObjectIdx+NumFixedObjects].Size;
368 /// setObjectSize - Change the size of the specified stack object.
369 void setObjectSize(int ObjectIdx, int64_t Size) {
370 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
371 "Invalid Object Idx!");
372 Objects[ObjectIdx+NumFixedObjects].Size = Size;
375 /// getObjectAlignment - Return the alignment of the specified stack object.
376 unsigned getObjectAlignment(int ObjectIdx) const {
377 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
378 "Invalid Object Idx!");
379 return Objects[ObjectIdx+NumFixedObjects].Alignment;
382 /// setObjectAlignment - Change the alignment of the specified stack object.
383 void setObjectAlignment(int ObjectIdx, unsigned Align) {
384 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
385 "Invalid Object Idx!");
386 Objects[ObjectIdx+NumFixedObjects].Alignment = Align;
387 ensureMaxAlignment(Align);
390 /// getObjectAllocation - Return the underlying Alloca of the specified
391 /// stack object if it exists. Returns 0 if none exists.
392 const AllocaInst* getObjectAllocation(int ObjectIdx) const {
393 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
394 "Invalid Object Idx!");
395 return Objects[ObjectIdx+NumFixedObjects].Alloca;
398 /// NeedsStackProtector - Returns true if the object may need stack
400 bool MayNeedStackProtector(int ObjectIdx) const {
401 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
402 "Invalid Object Idx!");
403 return Objects[ObjectIdx+NumFixedObjects].MayNeedSP;
406 /// getObjectOffset - Return the assigned stack offset of the specified object
407 /// from the incoming stack pointer.
409 int64_t getObjectOffset(int ObjectIdx) const {
410 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
411 "Invalid Object Idx!");
412 assert(!isDeadObjectIndex(ObjectIdx) &&
413 "Getting frame offset for a dead object?");
414 return Objects[ObjectIdx+NumFixedObjects].SPOffset;
417 /// setObjectOffset - Set the stack frame offset of the specified object. The
418 /// offset is relative to the stack pointer on entry to the function.
420 void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
421 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
422 "Invalid Object Idx!");
423 assert(!isDeadObjectIndex(ObjectIdx) &&
424 "Setting frame offset for a dead object?");
425 Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
428 /// getStackSize - Return the number of bytes that must be allocated to hold
429 /// all of the fixed size frame objects. This is only valid after
430 /// Prolog/Epilog code insertion has finalized the stack frame layout.
432 uint64_t getStackSize() const { return StackSize; }
434 /// setStackSize - Set the size of the stack...
436 void setStackSize(uint64_t Size) { StackSize = Size; }
438 /// Estimate and return the size of the stack frame.
439 unsigned estimateStackSize(const MachineFunction &MF) const;
441 /// getOffsetAdjustment - Return the correction for frame offsets.
443 int getOffsetAdjustment() const { return OffsetAdjustment; }
445 /// setOffsetAdjustment - Set the correction for frame offsets.
447 void setOffsetAdjustment(int Adj) { OffsetAdjustment = Adj; }
449 /// getMaxAlignment - Return the alignment in bytes that this function must be
450 /// aligned to, which is greater than the default stack alignment provided by
453 unsigned getMaxAlignment() const { return MaxAlignment; }
455 /// ensureMaxAlignment - Make sure the function is at least Align bytes
457 void ensureMaxAlignment(unsigned Align);
459 /// AdjustsStack - Return true if this function adjusts the stack -- e.g.,
460 /// when calling another function. This is only valid during and after
461 /// prolog/epilog code insertion.
462 bool adjustsStack() const { return AdjustsStack; }
463 void setAdjustsStack(bool V) { AdjustsStack = V; }
465 /// hasCalls - Return true if the current function has any function calls.
466 bool hasCalls() const { return HasCalls; }
467 void setHasCalls(bool V) { HasCalls = V; }
469 /// Returns true if the function contains any stack-adjusting inline assembly.
470 bool hasInlineAsmWithSPAdjust() const { return HasInlineAsmWithSPAdjust; }
471 void setHasInlineAsmWithSPAdjust(bool B) { HasInlineAsmWithSPAdjust = B; }
473 /// getMaxCallFrameSize - Return the maximum size of a call frame that must be
474 /// allocated for an outgoing function call. This is only available if
475 /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
476 /// then only during or after prolog/epilog code insertion.
478 unsigned getMaxCallFrameSize() const { return MaxCallFrameSize; }
479 void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
481 /// CreateFixedObject - Create a new object at a fixed location on the stack.
482 /// All fixed objects should be created before other objects are created for
483 /// efficiency. By default, fixed objects are immutable. This returns an
484 /// index with a negative value.
486 int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool Immutable);
489 /// isFixedObjectIndex - Returns true if the specified index corresponds to a
490 /// fixed stack object.
491 bool isFixedObjectIndex(int ObjectIdx) const {
492 return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
495 /// isImmutableObjectIndex - Returns true if the specified index corresponds
496 /// to an immutable object.
497 bool isImmutableObjectIndex(int ObjectIdx) const {
498 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
499 "Invalid Object Idx!");
500 return Objects[ObjectIdx+NumFixedObjects].isImmutable;
503 /// isSpillSlotObjectIndex - Returns true if the specified index corresponds
504 /// to a spill slot..
505 bool isSpillSlotObjectIndex(int ObjectIdx) const {
506 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
507 "Invalid Object Idx!");
508 return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;
511 /// isDeadObjectIndex - Returns true if the specified index corresponds to
513 bool isDeadObjectIndex(int ObjectIdx) const {
514 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
515 "Invalid Object Idx!");
516 return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
519 /// CreateStackObject - Create a new statically sized stack object, returning
520 /// a nonnegative identifier to represent it.
522 int CreateStackObject(uint64_t Size, unsigned Alignment, bool isSS,
523 bool MayNeedSP = false, const AllocaInst *Alloca = 0);
525 /// CreateSpillStackObject - Create a new statically sized stack object that
526 /// represents a spill slot, returning a nonnegative identifier to represent
529 int CreateSpillStackObject(uint64_t Size, unsigned Alignment);
531 /// RemoveStackObject - Remove or mark dead a statically sized stack object.
533 void RemoveStackObject(int ObjectIdx) {
535 Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
538 /// CreateVariableSizedObject - Notify the MachineFrameInfo object that a
539 /// variable sized object has been created. This must be created whenever a
540 /// variable sized object is created, whether or not the index returned is
543 int CreateVariableSizedObject(unsigned Alignment);
545 /// getCalleeSavedInfo - Returns a reference to call saved info vector for the
546 /// current function.
547 const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
551 /// setCalleeSavedInfo - Used by prolog/epilog inserter to set the function's
552 /// callee saved information.
553 void setCalleeSavedInfo(const std::vector<CalleeSavedInfo> &CSI) {
557 /// isCalleeSavedInfoValid - Has the callee saved info been calculated yet?
558 bool isCalleeSavedInfoValid() const { return CSIValid; }
560 void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
562 /// getPristineRegs - Return a set of physical registers that are pristine on
563 /// entry to the MBB.
565 /// Pristine registers hold a value that is useless to the current function,
566 /// but that must be preserved - they are callee saved registers that have not
569 /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
570 /// method always returns an empty set.
571 BitVector getPristineRegs(const MachineBasicBlock *MBB) const;
573 /// print - Used by the MachineFunction printer to print information about
574 /// stack objects. Implemented in MachineFunction.cpp
576 void print(const MachineFunction &MF, raw_ostream &OS) const;
578 /// dump - Print the function to stderr.
579 void dump(const MachineFunction &MF) const;
582 } // End llvm namespace