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