1 //===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- C++ -*-===//
3 // The MachineFrameInfo class represents an abstract stack frame until
4 // prolog/epilog code is inserted. This class is key to allowing stack frame
5 // representation optimizations, such as frame pointer elimination. It also
6 // allows more mundane (but still important) optimizations, such as reordering
7 // of abstract objects on the stack frame.
9 // To support this, the class assigns unique integer identifiers to stack
10 // objects requested clients. These identifiers are negative integers for fixed
11 // stack objects (such as arguments passed on the stack) or positive for objects
12 // that may be reordered. Instructions which refer to stack objects use a
13 // special MO_FrameIndex operand to represent these frame indexes.
15 // Because this class keeps track of all references to the stack frame, it knows
16 // when a variable sized object is allocated on the stack. This is the sole
17 // condition which prevents frame pointer elimination, which is an important
18 // optimization on register-poor architectures. Because original variable sized
19 // alloca's in the source program are the only source of variable sized stack
20 // objects, it is safe to decide whether there will be any variable sized
21 // objects before all stack objects are known (for example, register allocator
22 // spill code never needs variable sized objects).
24 // When prolog/epilog code emission is performed, the final stack frame is built
25 // and the machine instructions are modified to refer to the actual stack
26 // offsets of the object, eliminating all MO_FrameIndex operands from the
29 //===----------------------------------------------------------------------===//
31 #ifndef LLVM_CODEGEN_FUNCTIONFRAMEINFO_H
32 #define LLVM_CODEGEN_FUNCTIONFRAMEINFO_H
34 class MachineFrameInfo {
36 // StackObject - Represent a single object allocated on the stack.
38 // The size of this object on the stack. 0 means a variable sized object
41 // Alignment - The required alignment of this stack slot.
44 // SPOffset - The offset of this object from the stack pointer on entry to
45 // the function. This field has no meaning for a variable sized element.
48 StackObject(unsigned Sz, unsigned Al, int SP)
49 : Size(Sz), Alignment(Al), SPOffset(SP) {}
52 /// Objects - The list of stack objects allocated...
54 std::vector<StackObject> Objects;
56 /// NumFixedObjects - This contains the number of fixed objects contained on
57 /// the stack. Because fixed objects are stored at a negative index in the
58 /// Objects list, this is also the index to the 0th object in the list.
60 unsigned NumFixedObjects;
62 /// HasVarSizedObjects - This boolean keeps track of whether any variable
63 /// sized objects have been allocated yet.
65 bool HasVarSizedObjects;
67 /// StackSize - The prolog/epilog code inserter calculates the final stack
68 /// offsets for all of the fixed size objects, updating the Objects list
69 /// above. It then updates StackSize to contain the number of bytes that need
70 /// to be allocated on entry to the function.
74 /// HasCalls - Set to true if this function has any function calls. This is
75 /// only valid during and after prolog/epilog code insertion.
78 /// MaxCallFrameSize - This contains the size of the largest call frame if the
79 /// target uses frame setup/destroy pseudo instructions (as defined in the
80 /// TargetFrameInfo class). This information is important for frame pointer
81 /// elimination. If is only valid during and after prolog/epilog code
84 unsigned MaxCallFrameSize;
87 NumFixedObjects = StackSize = 0;
88 HasVarSizedObjects = false;
93 /// hasStackObjects - Return true if there are any stack objects in this
96 bool hasStackObjects() const { return !Objects.empty(); }
98 /// hasVarSizedObjects - This method may be called any time after instruction
99 /// selection is complete to determine if the stack frame for this function
100 /// contains any variable sized objects.
102 bool hasVarSizedObjects() const { return HasVarSizedObjects; }
104 /// getObjectIndexBegin - Return the minimum frame object index...
106 int getObjectIndexBegin() const { return -NumFixedObjects; }
108 /// getObjectIndexEnd - Return one past the maximum frame object index...
110 int getObjectIndexEnd() const { return Objects.size()-NumFixedObjects; }
112 /// getObjectSize - Return the size of the specified object
114 int getObjectSize(int ObjectIdx) const {
115 assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
116 return Objects[ObjectIdx+NumFixedObjects].Size;
119 /// getObjectAlignment - Return the alignment of the specified stack object...
120 int getObjectAlignment(int ObjectIdx) const {
121 assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
122 return Objects[ObjectIdx+NumFixedObjects].Alignment;
125 /// getObjectOffset - Return the assigned stack offset of the specified object
126 /// from the incoming stack pointer.
128 int getObjectOffset(int ObjectIdx) const {
129 assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
130 return Objects[ObjectIdx+NumFixedObjects].SPOffset;
133 /// setObjectOffset - Set the stack frame offset of the specified object. The
134 /// offset is relative to the stack pointer on entry to the function.
136 void setObjectOffset(int ObjectIdx, int SPOffset) {
137 assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
138 Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
141 /// getStackSize - Return the number of bytes that must be allocated to hold
142 /// all of the fixed size frame objects. This is only valid after
143 /// Prolog/Epilog code insertion has finalized the stack frame layout.
145 unsigned getStackSize() const { return StackSize; }
147 /// setStackSize - Set the size of the stack...
149 void setStackSize(unsigned Size) { StackSize = Size; }
151 /// hasCalls - Return true if the current function has no function calls.
152 /// This is only valid during or after prolog/epilog code emission.
154 bool hasCalls() const { return HasCalls; }
155 void setHasCalls(bool V) { HasCalls = V; }
157 /// getMaxCallFrameSize - Return the maximum size of a call frame that must be
158 /// allocated for an outgoing function call. This is only available if
159 /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
160 /// then only during or after prolog/epilog code insertion.
162 unsigned getMaxCallFrameSize() const { return MaxCallFrameSize; }
163 void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
165 /// CreateFixedObject - Create a new object at a fixed location on the stack.
166 /// All fixed objects should be created before other objects are created for
167 /// efficiency. This returns an index with a negative value.
169 int CreateFixedObject(unsigned Size, int SPOffset) {
170 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
171 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset));
172 return -++NumFixedObjects;
175 /// CreateStackObject - Create a new statically sized stack object, returning
176 /// a postive identifier to represent it.
178 int CreateStackObject(unsigned Size, unsigned Alignment) {
179 assert(Size != 0 && "Cannot allocate zero size stack objects!");
180 Objects.push_back(StackObject(Size, Alignment, -1));
181 return Objects.size()-NumFixedObjects-1;
184 /// CreateVariableSizedObject - Notify the MachineFrameInfo object that a
185 /// variable sized object has been created. This must be created whenever a
186 /// variable sized object is created, whether or not the index returned is
189 int CreateVariableSizedObject() {
190 HasVarSizedObjects = true;
191 Objects.push_back(StackObject(0, 1, -1));
192 return Objects.size()-NumFixedObjects-1;
195 /// print - Used by the MachineFunction printer to print information about
196 /// stack objects. Implemented in MachineFunction.cpp
198 void print(std::ostream &OS) const;
200 /// dump - Call print(std::cerr) to be called from the debugger.