1 //===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The MachineFrameInfo class represents an abstract stack frame until
11 // prolog/epilog code is inserted. This class is key to allowing stack frame
12 // representation optimizations, such as frame pointer elimination. It also
13 // allows more mundane (but still important) optimizations, such as reordering
14 // of abstract objects on the stack frame.
16 // To support this, the class assigns unique integer identifiers to stack
17 // objects requested clients. These identifiers are negative integers for fixed
18 // stack objects (such as arguments passed on the stack) or positive for objects
19 // that may be reordered. Instructions which refer to stack objects use a
20 // special MO_FrameIndex operand to represent these frame indexes.
22 // Because this class keeps track of all references to the stack frame, it knows
23 // when a variable sized object is allocated on the stack. This is the sole
24 // condition which prevents frame pointer elimination, which is an important
25 // optimization on register-poor architectures. Because original variable sized
26 // alloca's in the source program are the only source of variable sized stack
27 // objects, it is safe to decide whether there will be any variable sized
28 // objects before all stack objects are known (for example, register allocator
29 // spill code never needs variable sized objects).
31 // When prolog/epilog code emission is performed, the final stack frame is built
32 // and the machine instructions are modified to refer to the actual stack
33 // offsets of the object, eliminating all MO_FrameIndex operands from the
36 //===----------------------------------------------------------------------===//
38 #ifndef LLVM_CODEGEN_MACHINEFRAMEINFO_H
39 #define LLVM_CODEGEN_MACHINEFRAMEINFO_H
45 class TargetRegisterClass;
47 class MachineFunction;
49 class MachineFrameInfo {
51 // StackObject - Represent a single object allocated on the stack.
53 // The size of this object on the stack. 0 means a variable sized object
56 // Alignment - The required alignment of this stack slot.
59 // SPOffset - The offset of this object from the stack pointer on entry to
60 // the function. This field has no meaning for a variable sized element.
63 StackObject(unsigned Sz, unsigned Al, int SP)
64 : Size(Sz), Alignment(Al), SPOffset(SP) {}
67 /// Objects - The list of stack objects allocated...
69 std::vector<StackObject> Objects;
71 /// NumFixedObjects - This contains the number of fixed objects contained on
72 /// the stack. Because fixed objects are stored at a negative index in the
73 /// Objects list, this is also the index to the 0th object in the list.
75 unsigned NumFixedObjects;
77 /// HasVarSizedObjects - This boolean keeps track of whether any variable
78 /// sized objects have been allocated yet.
80 bool HasVarSizedObjects;
82 /// StackSize - The prolog/epilog code inserter calculates the final stack
83 /// offsets for all of the fixed size objects, updating the Objects list
84 /// above. It then updates StackSize to contain the number of bytes that need
85 /// to be allocated on entry to the function.
89 /// HasCalls - Set to true if this function has any function calls. This is
90 /// only valid during and after prolog/epilog code insertion.
93 /// MaxCallFrameSize - This contains the size of the largest call frame if the
94 /// target uses frame setup/destroy pseudo instructions (as defined in the
95 /// TargetFrameInfo class). This information is important for frame pointer
96 /// elimination. If is only valid during and after prolog/epilog code
99 unsigned MaxCallFrameSize;
102 NumFixedObjects = StackSize = 0;
103 HasVarSizedObjects = false;
105 MaxCallFrameSize = 0;
108 /// hasStackObjects - Return true if there are any stack objects in this
111 bool hasStackObjects() const { return !Objects.empty(); }
113 /// hasVarSizedObjects - This method may be called any time after instruction
114 /// selection is complete to determine if the stack frame for this function
115 /// contains any variable sized objects.
117 bool hasVarSizedObjects() const { return HasVarSizedObjects; }
119 /// getObjectIndexBegin - Return the minimum frame object index...
121 int getObjectIndexBegin() const { return -NumFixedObjects; }
123 /// getObjectIndexEnd - Return one past the maximum frame object index...
125 int getObjectIndexEnd() const { return Objects.size()-NumFixedObjects; }
127 /// getObjectSize - Return the size of the specified object
129 int getObjectSize(int ObjectIdx) const {
130 assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
131 return Objects[ObjectIdx+NumFixedObjects].Size;
134 /// getObjectAlignment - Return the alignment of the specified stack object...
135 int getObjectAlignment(int ObjectIdx) const {
136 assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
137 return Objects[ObjectIdx+NumFixedObjects].Alignment;
140 /// getObjectOffset - Return the assigned stack offset of the specified object
141 /// from the incoming stack pointer.
143 int getObjectOffset(int ObjectIdx) const {
144 assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
145 return Objects[ObjectIdx+NumFixedObjects].SPOffset;
148 /// setObjectOffset - Set the stack frame offset of the specified object. The
149 /// offset is relative to the stack pointer on entry to the function.
151 void setObjectOffset(int ObjectIdx, int SPOffset) {
152 assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
153 Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
156 /// getStackSize - Return the number of bytes that must be allocated to hold
157 /// all of the fixed size frame objects. This is only valid after
158 /// Prolog/Epilog code insertion has finalized the stack frame layout.
160 unsigned getStackSize() const { return StackSize; }
162 /// setStackSize - Set the size of the stack...
164 void setStackSize(unsigned Size) { StackSize = Size; }
166 /// hasCalls - Return true if the current function has no function calls.
167 /// This is only valid during or after prolog/epilog code emission.
169 bool hasCalls() const { return HasCalls; }
170 void setHasCalls(bool V) { HasCalls = V; }
172 /// getMaxCallFrameSize - Return the maximum size of a call frame that must be
173 /// allocated for an outgoing function call. This is only available if
174 /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
175 /// then only during or after prolog/epilog code insertion.
177 unsigned getMaxCallFrameSize() const { return MaxCallFrameSize; }
178 void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
180 /// CreateFixedObject - Create a new object at a fixed location on the stack.
181 /// All fixed objects should be created before other objects are created for
182 /// efficiency. This returns an index with a negative value.
184 int CreateFixedObject(unsigned Size, int SPOffset) {
185 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
186 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset));
187 return -++NumFixedObjects;
190 /// CreateStackObject - Create a new statically sized stack object, returning
191 /// a postive identifier to represent it.
193 int CreateStackObject(unsigned Size, unsigned Alignment) {
194 assert(Size != 0 && "Cannot allocate zero size stack objects!");
195 Objects.push_back(StackObject(Size, Alignment, -1));
196 return Objects.size()-NumFixedObjects-1;
199 /// CreateStackObject - Create a stack object for a value of the specified
202 int CreateStackObject(const Type *Ty, const TargetData &TD);
204 /// CreateVariableSizedObject - Notify the MachineFrameInfo object that a
205 /// variable sized object has been created. This must be created whenever a
206 /// variable sized object is created, whether or not the index returned is
209 int CreateVariableSizedObject() {
210 HasVarSizedObjects = true;
211 Objects.push_back(StackObject(0, 1, -1));
212 return Objects.size()-NumFixedObjects-1;
215 /// print - Used by the MachineFunction printer to print information about
216 /// stack objects. Implemented in MachineFunction.cpp
218 void print(const MachineFunction &MF, std::ostream &OS) const;
220 /// dump - Call print(MF, std::cerr) to be called from the debugger.
221 void dump(const MachineFunction &MF) const;
224 } // End llvm namespace