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