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