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