6d640a6b5a00bdc105c61e6886d7c3aaed13d542
[oota-llvm.git] / include / llvm / CodeGen / MachineFrameInfo.h
1 //===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- C++ -*-===//
2 // 
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.
8 //
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.
14 //
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).
23 //
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
27 // program.
28 //
29 //===----------------------------------------------------------------------===//
30
31 #ifndef LLVM_CODEGEN_FUNCTIONFRAMEINFO_H
32 #define LLVM_CODEGEN_FUNCTIONFRAMEINFO_H
33
34 class MachineFrameInfo {
35
36   // StackObject - Represent a single object allocated on the stack.
37   struct StackObject {
38     // The size of this object on the stack. 0 means a variable sized object
39     unsigned Size;
40
41     // Alignment - The required alignment of this stack slot.
42     unsigned Alignment;
43
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.
46     int SPOffset;
47
48     StackObject(unsigned Sz, unsigned Al, int SP)
49       : Size(Sz), Alignment(Al), SPOffset(SP) {}
50   };
51
52   /// Objects - The list of stack objects allocated...
53   ///
54   std::vector<StackObject> Objects;
55
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.
59   ///
60   unsigned NumFixedObjects;
61
62   /// HasVarSizedObjects - This boolean keeps track of whether any variable
63   /// sized objects have been allocated yet.
64   ///
65   bool HasVarSizedObjects;
66
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.
71   ///
72   unsigned StackSize;
73
74   /// HasCalls - Set to true if this function has any function calls.  This is
75   /// only valid during and after prolog/epilog code insertion.
76   bool HasCalls;
77
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
82   /// insertion.
83   ///
84   unsigned MaxCallFrameSize;
85 public:
86   MachineFrameInfo() {
87     NumFixedObjects = StackSize = 0;
88     HasVarSizedObjects = false;
89     HasCalls = false;
90     MaxCallFrameSize = 0;
91   }
92
93   /// hasStackObjects - Return true if there are any stack objects in this
94   /// function.
95   ///
96   bool hasStackObjects() const { return !Objects.empty(); }
97
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.
101   ///
102   bool hasVarSizedObjects() const { return HasVarSizedObjects; }
103
104   /// getObjectIndexBegin - Return the minimum frame object index...
105   ///
106   int getObjectIndexBegin() const { return -NumFixedObjects; }
107
108   /// getObjectIndexEnd - Return one past the maximum frame object index...
109   ///
110   int getObjectIndexEnd() const { return Objects.size()-NumFixedObjects; }
111
112   /// getObjectSize - Return the size of the specified object
113   ///
114   int getObjectSize(int ObjectIdx) const {
115     assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
116     return Objects[ObjectIdx+NumFixedObjects].Size;
117   }
118
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;
123   }
124
125   /// getObjectOffset - Return the assigned stack offset of the specified object
126   /// from the incoming stack pointer.
127   ///
128   int getObjectOffset(int ObjectIdx) const {
129     assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
130     return Objects[ObjectIdx+NumFixedObjects].SPOffset;
131   }
132
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.
135   ///
136   void setObjectOffset(int ObjectIdx, int SPOffset) {
137     assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
138     Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
139   }
140
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.
144   ///
145   unsigned getStackSize() const { return StackSize; }
146
147   /// setStackSize - Set the size of the stack...
148   ///
149   void setStackSize(unsigned Size) { StackSize = Size; }
150
151   /// hasCalls - Return true if the current function has no function calls.
152   /// This is only valid during or after prolog/epilog code emission.
153   ///
154   bool hasCalls() const { return HasCalls; }
155   void setHasCalls(bool V) { HasCalls = V; }
156   
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.
161   ///
162   unsigned getMaxCallFrameSize() const { return MaxCallFrameSize; }
163   void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
164
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.
168   ///
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;
173   }
174   
175   /// CreateStackObject - Create a new statically sized stack object, returning
176   /// a postive identifier to represent it.
177   ///
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;
182   }
183
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
187   /// actually used.
188   ///
189   int CreateVariableSizedObject() {
190     HasVarSizedObjects = true;
191     Objects.push_back(StackObject(0, 1, -1));
192     return Objects.size()-NumFixedObjects-1;
193   }
194
195   /// print - Used by the MachineFunction printer to print information about
196   /// stack objects.  Implemented in MachineFunction.cpp
197   ///
198   void print(std::ostream &OS) const;
199
200   /// dump - Call print(std::cerr) to be called from the debugger.
201   void dump() const;
202 };
203
204 #endif