Make sure to consider alignment of variable sized objects.
[oota-llvm.git] / include / llvm / CodeGen / MachineFrameInfo.h
1 //===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
15 //
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.
21 //
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).
30 //
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
34 // program.
35 //
36 //===----------------------------------------------------------------------===//
37
38 #ifndef LLVM_CODEGEN_MACHINEFRAMEINFO_H
39 #define LLVM_CODEGEN_MACHINEFRAMEINFO_H
40
41 #include <vector>
42
43 namespace llvm {
44 class TargetData;
45 class TargetRegisterClass;
46 class Type;
47 class MachineFunction;
48
49 class MachineFrameInfo {
50
51   // StackObject - Represent a single object allocated on the stack.
52   struct StackObject {
53     // The size of this object on the stack. 0 means a variable sized object
54     unsigned Size;
55
56     // Alignment - The required alignment of this stack slot.
57     unsigned Alignment;
58
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.
61     int SPOffset;
62
63     StackObject(unsigned Sz, unsigned Al, int SP)
64       : Size(Sz), Alignment(Al), SPOffset(SP) {}
65   };
66
67   /// Objects - The list of stack objects allocated...
68   ///
69   std::vector<StackObject> Objects;
70
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.
74   ///
75   unsigned NumFixedObjects;
76
77   /// HasVarSizedObjects - This boolean keeps track of whether any variable
78   /// sized objects have been allocated yet.
79   ///
80   bool HasVarSizedObjects;
81
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.
86   ///
87   unsigned StackSize;
88   
89   /// MaxAlignment - The prolog/epilog code inserter may process objects 
90   /// that require greater alignment than the default alignment the target
91   /// provides. To handle this, MaxAlignment is set to the maximum alignment 
92   /// needed by the objects on the current frame.  If this is greater than the
93   /// native alignment maintained by the compiler, dynamic alignment code will
94   /// be needed.
95   ///
96   unsigned MaxAlignment;
97
98   /// HasCalls - Set to true if this function has any function calls.  This is
99   /// only valid during and after prolog/epilog code insertion.
100   bool HasCalls;
101
102   /// MaxCallFrameSize - This contains the size of the largest call frame if the
103   /// target uses frame setup/destroy pseudo instructions (as defined in the
104   /// TargetFrameInfo class).  This information is important for frame pointer
105   /// elimination.  If is only valid during and after prolog/epilog code
106   /// insertion.
107   ///
108   unsigned MaxCallFrameSize;
109 public:
110   MachineFrameInfo() {
111     NumFixedObjects = StackSize = MaxAlignment = 0;
112     HasVarSizedObjects = false;
113     HasCalls = false;
114     MaxCallFrameSize = 0;
115   }
116
117   /// hasStackObjects - Return true if there are any stack objects in this
118   /// function.
119   ///
120   bool hasStackObjects() const { return !Objects.empty(); }
121
122   /// hasVarSizedObjects - This method may be called any time after instruction
123   /// selection is complete to determine if the stack frame for this function
124   /// contains any variable sized objects.
125   ///
126   bool hasVarSizedObjects() const { return HasVarSizedObjects; }
127
128   /// getObjectIndexBegin - Return the minimum frame object index...
129   ///
130   int getObjectIndexBegin() const { return -NumFixedObjects; }
131
132   /// getObjectIndexEnd - Return one past the maximum frame object index...
133   ///
134   int getObjectIndexEnd() const { return Objects.size()-NumFixedObjects; }
135
136   /// getObjectSize - Return the size of the specified object
137   ///
138   int getObjectSize(int ObjectIdx) const {
139     assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
140     return Objects[ObjectIdx+NumFixedObjects].Size;
141   }
142
143   /// getObjectAlignment - Return the alignment of the specified stack object...
144   int getObjectAlignment(int ObjectIdx) const {
145     assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
146     return Objects[ObjectIdx+NumFixedObjects].Alignment;
147   }
148
149   /// getObjectOffset - Return the assigned stack offset of the specified object
150   /// from the incoming stack pointer.
151   ///
152   int getObjectOffset(int ObjectIdx) const {
153     assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
154     return Objects[ObjectIdx+NumFixedObjects].SPOffset;
155   }
156
157   /// setObjectOffset - Set the stack frame offset of the specified object.  The
158   /// offset is relative to the stack pointer on entry to the function.
159   ///
160   void setObjectOffset(int ObjectIdx, int SPOffset) {
161     assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
162     Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
163   }
164
165   /// getStackSize - Return the number of bytes that must be allocated to hold
166   /// all of the fixed size frame objects.  This is only valid after
167   /// Prolog/Epilog code insertion has finalized the stack frame layout.
168   ///
169   unsigned getStackSize() const { return StackSize; }
170
171   /// setStackSize - Set the size of the stack...
172   ///
173   void setStackSize(unsigned Size) { StackSize = Size; }
174
175   /// getMaxAlignment - Return the alignment in bytes that this function must be 
176   /// aligned to, which is greater than the default stack alignment provided by 
177   /// the target.
178   ///
179   unsigned getMaxAlignment() const { return MaxAlignment; }
180   
181   /// setMaxAlignment - Set the preferred alignment.
182   ///
183   void setMaxAlignment(unsigned Align) { MaxAlignment = Align; }
184   
185   /// hasCalls - Return true if the current function has no function calls.
186   /// This is only valid during or after prolog/epilog code emission.
187   ///
188   bool hasCalls() const { return HasCalls; }
189   void setHasCalls(bool V) { HasCalls = V; }
190
191   /// getMaxCallFrameSize - Return the maximum size of a call frame that must be
192   /// allocated for an outgoing function call.  This is only available if
193   /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
194   /// then only during or after prolog/epilog code insertion.
195   ///
196   unsigned getMaxCallFrameSize() const { return MaxCallFrameSize; }
197   void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
198
199   /// CreateFixedObject - Create a new object at a fixed location on the stack.
200   /// All fixed objects should be created before other objects are created for
201   /// efficiency.  This returns an index with a negative value.
202   ///
203   int CreateFixedObject(unsigned Size, int SPOffset) {
204     assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
205     Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset));
206     return -++NumFixedObjects;
207   }
208
209   /// CreateStackObject - Create a new statically sized stack object, returning
210   /// a postive identifier to represent it.
211   ///
212   int CreateStackObject(unsigned Size, unsigned Alignment) {
213     // Keep track of the maximum alignment.
214     if (MaxAlignment < Alignment) MaxAlignment = Alignment;
215     
216     assert(Size != 0 && "Cannot allocate zero size stack objects!");
217     Objects.push_back(StackObject(Size, Alignment, -1));
218     return Objects.size()-NumFixedObjects-1;
219   }
220
221   /// CreateVariableSizedObject - Notify the MachineFrameInfo object that a
222   /// variable sized object has been created.  This must be created whenever a
223   /// variable sized object is created, whether or not the index returned is
224   /// actually used.
225   ///
226   int CreateVariableSizedObject() {
227     HasVarSizedObjects = true;
228     if (MaxAlignment < 1) MaxAlignment = 1;
229     Objects.push_back(StackObject(0, 1, -1));
230     return Objects.size()-NumFixedObjects-1;
231   }
232
233   /// print - Used by the MachineFunction printer to print information about
234   /// stack objects.  Implemented in MachineFunction.cpp
235   ///
236   void print(const MachineFunction &MF, std::ostream &OS) const;
237
238   /// dump - Call print(MF, std::cerr) to be called from the debugger.
239   void dump(const MachineFunction &MF) const;
240 };
241
242 } // End llvm namespace
243
244 #endif