Add a bool flag to StackObjects telling whether they reference spill
[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 is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // The file defines the MachineFrameInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_MACHINEFRAMEINFO_H
15 #define LLVM_CODEGEN_MACHINEFRAMEINFO_H
16
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/System/DataTypes.h"
21 #include <cassert>
22 #include <limits>
23 #include <vector>
24
25 namespace llvm {
26 class raw_ostream;
27 class TargetData;
28 class TargetRegisterClass;
29 class Type;
30 class MachineModuleInfo;
31 class MachineFunction;
32 class MachineBasicBlock;
33 class TargetFrameInfo;
34
35 /// The CalleeSavedInfo class tracks the information need to locate where a
36 /// callee saved register in the current frame.  
37 class CalleeSavedInfo {
38
39 private:
40   unsigned Reg;
41   const TargetRegisterClass *RegClass;
42   int FrameIdx;
43   
44 public:
45   CalleeSavedInfo(unsigned R, const TargetRegisterClass *RC, int FI = 0)
46   : Reg(R)
47   , RegClass(RC)
48   , FrameIdx(FI)
49   {}
50   
51   // Accessors.
52   unsigned getReg()                        const { return Reg; }
53   const TargetRegisterClass *getRegClass() const { return RegClass; }
54   int getFrameIdx()                        const { return FrameIdx; }
55   void setFrameIdx(int FI)                       { FrameIdx = FI; }
56 };
57
58 /// The MachineFrameInfo class represents an abstract stack frame until
59 /// prolog/epilog code is inserted.  This class is key to allowing stack frame
60 /// representation optimizations, such as frame pointer elimination.  It also
61 /// allows more mundane (but still important) optimizations, such as reordering
62 /// of abstract objects on the stack frame.
63 ///
64 /// To support this, the class assigns unique integer identifiers to stack
65 /// objects requested clients.  These identifiers are negative integers for
66 /// fixed stack objects (such as arguments passed on the stack) or nonnegative
67 /// for objects that may be reordered.  Instructions which refer to stack
68 /// objects use a special MO_FrameIndex operand to represent these frame
69 /// indexes.
70 ///
71 /// Because this class keeps track of all references to the stack frame, it
72 /// knows when a variable sized object is allocated on the stack.  This is the
73 /// sole condition which prevents frame pointer elimination, which is an
74 /// important optimization on register-poor architectures.  Because original
75 /// variable sized alloca's in the source program are the only source of
76 /// variable sized stack objects, it is safe to decide whether there will be
77 /// any variable sized objects before all stack objects are known (for
78 /// example, register allocator spill code never needs variable sized
79 /// objects).
80 ///
81 /// When prolog/epilog code emission is performed, the final stack frame is
82 /// built and the machine instructions are modified to refer to the actual
83 /// stack offsets of the object, eliminating all MO_FrameIndex operands from
84 /// the program.
85 ///
86 /// @brief Abstract Stack Frame Information
87 class MachineFrameInfo {
88
89   // StackObject - Represent a single object allocated on the stack.
90   struct StackObject {
91     // SPOffset - The offset of this object from the stack pointer on entry to
92     // the function.  This field has no meaning for a variable sized element.
93     int64_t SPOffset;
94     
95     // The size of this object on the stack. 0 means a variable sized object,
96     // ~0ULL means a dead object.
97     uint64_t Size;
98
99     // Alignment - The required alignment of this stack slot.
100     unsigned Alignment;
101
102     // isImmutable - If true, the value of the stack object is set before
103     // entering the function and is not modified inside the function. By
104     // default, fixed objects are immutable unless marked otherwise.
105     bool isImmutable;
106
107     // isSpillSlot - If true, the stack object is used as spill slot. It
108     // cannot alias any other memory objects.
109     bool isSpillSlot;
110
111     StackObject(uint64_t Sz, unsigned Al, int64_t SP, bool IM,
112                 bool isSS)
113       : SPOffset(SP), Size(Sz), Alignment(Al), isImmutable(IM),
114         isSpillSlot(isSS) {}
115   };
116
117   /// Objects - The list of stack objects allocated...
118   ///
119   std::vector<StackObject> Objects;
120
121   /// NumFixedObjects - This contains the number of fixed objects contained on
122   /// the stack.  Because fixed objects are stored at a negative index in the
123   /// Objects list, this is also the index to the 0th object in the list.
124   ///
125   unsigned NumFixedObjects;
126
127   /// HasVarSizedObjects - This boolean keeps track of whether any variable
128   /// sized objects have been allocated yet.
129   ///
130   bool HasVarSizedObjects;
131
132   /// FrameAddressTaken - This boolean keeps track of whether there is a call
133   /// to builtin \@llvm.frameaddress.
134   bool FrameAddressTaken;
135
136   /// StackSize - The prolog/epilog code inserter calculates the final stack
137   /// offsets for all of the fixed size objects, updating the Objects list
138   /// above.  It then updates StackSize to contain the number of bytes that need
139   /// to be allocated on entry to the function.
140   ///
141   uint64_t StackSize;
142   
143   /// OffsetAdjustment - The amount that a frame offset needs to be adjusted to
144   /// have the actual offset from the stack/frame pointer.  The exact usage of
145   /// this is target-dependent, but it is typically used to adjust between
146   /// SP-relative and FP-relative offsets.  E.G., if objects are accessed via
147   /// SP then OffsetAdjustment is zero; if FP is used, OffsetAdjustment is set
148   /// to the distance between the initial SP and the value in FP.  For many
149   /// targets, this value is only used when generating debug info (via
150   /// TargetRegisterInfo::getFrameIndexOffset); when generating code, the
151   /// corresponding adjustments are performed directly.
152   int OffsetAdjustment;
153   
154   /// MaxAlignment - The prolog/epilog code inserter may process objects 
155   /// that require greater alignment than the default alignment the target
156   /// provides. To handle this, MaxAlignment is set to the maximum alignment 
157   /// needed by the objects on the current frame.  If this is greater than the
158   /// native alignment maintained by the compiler, dynamic alignment code will
159   /// be needed.
160   ///
161   unsigned MaxAlignment;
162
163   /// HasCalls - Set to true if this function has any function calls.  This is
164   /// only valid during and after prolog/epilog code insertion.
165   bool HasCalls;
166
167   /// StackProtectorIdx - The frame index for the stack protector.
168   int StackProtectorIdx;
169
170   /// MaxCallFrameSize - This contains the size of the largest call frame if the
171   /// target uses frame setup/destroy pseudo instructions (as defined in the
172   /// TargetFrameInfo class).  This information is important for frame pointer
173   /// elimination.  If is only valid during and after prolog/epilog code
174   /// insertion.
175   ///
176   unsigned MaxCallFrameSize;
177   
178   /// CSInfo - The prolog/epilog code inserter fills in this vector with each
179   /// callee saved register saved in the frame.  Beyond its use by the prolog/
180   /// epilog code inserter, this data used for debug info and exception
181   /// handling.
182   std::vector<CalleeSavedInfo> CSInfo;
183
184   /// CSIValid - Has CSInfo been set yet?
185   bool CSIValid;
186
187   /// SpillObjects - A vector indicating which frame indices refer to
188   /// spill slots.
189   SmallVector<bool, 8> SpillObjects;
190
191   /// MMI - This field is set (via setMachineModuleInfo) by a module info
192   /// consumer (ex. DwarfWriter) to indicate that frame layout information
193   /// should be acquired.  Typically, it's the responsibility of the target's
194   /// TargetRegisterInfo prologue/epilogue emitting code to inform
195   /// MachineModuleInfo of frame layouts.
196   MachineModuleInfo *MMI;
197   
198   /// TargetFrameInfo - Target information about frame layout.
199   ///
200   const TargetFrameInfo &TFI;
201
202 public:
203   explicit MachineFrameInfo(const TargetFrameInfo &tfi) : TFI(tfi) {
204     StackSize = NumFixedObjects = OffsetAdjustment = MaxAlignment = 0;
205     HasVarSizedObjects = false;
206     FrameAddressTaken = false;
207     HasCalls = false;
208     StackProtectorIdx = -1;
209     MaxCallFrameSize = 0;
210     CSIValid = false;
211     MMI = 0;
212   }
213
214   /// hasStackObjects - Return true if there are any stack objects in this
215   /// function.
216   ///
217   bool hasStackObjects() const { return !Objects.empty(); }
218
219   /// hasVarSizedObjects - This method may be called any time after instruction
220   /// selection is complete to determine if the stack frame for this function
221   /// contains any variable sized objects.
222   ///
223   bool hasVarSizedObjects() const { return HasVarSizedObjects; }
224
225   /// getStackProtectorIndex/setStackProtectorIndex - Return the index for the
226   /// stack protector object.
227   ///
228   int getStackProtectorIndex() const { return StackProtectorIdx; }
229   void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
230
231   /// isFrameAddressTaken - This method may be called any time after instruction
232   /// selection is complete to determine if there is a call to
233   /// \@llvm.frameaddress in this function.
234   bool isFrameAddressTaken() const { return FrameAddressTaken; }
235   void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; }
236
237   /// getObjectIndexBegin - Return the minimum frame object index.
238   ///
239   int getObjectIndexBegin() const { return -NumFixedObjects; }
240
241   /// getObjectIndexEnd - Return one past the maximum frame object index.
242   ///
243   int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
244
245   /// getNumFixedObjects() - Return the number of fixed objects.
246   unsigned getNumFixedObjects() const { return NumFixedObjects; }
247
248   /// getNumObjects() - Return the number of objects.
249   ///
250   unsigned getNumObjects() const { return Objects.size(); }
251
252   /// getObjectSize - Return the size of the specified object.
253   ///
254   int64_t getObjectSize(int ObjectIdx) const {
255     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
256            "Invalid Object Idx!");
257     return Objects[ObjectIdx+NumFixedObjects].Size;
258   }
259
260   /// setObjectSize - Change the size of the specified stack object.
261   void setObjectSize(int ObjectIdx, int64_t Size) {
262     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
263            "Invalid Object Idx!");
264     Objects[ObjectIdx+NumFixedObjects].Size = Size;
265   }
266
267   /// getObjectAlignment - Return the alignment of the specified stack object.
268   unsigned getObjectAlignment(int ObjectIdx) const {
269     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
270            "Invalid Object Idx!");
271     return Objects[ObjectIdx+NumFixedObjects].Alignment;
272   }
273
274   /// setObjectAlignment - Change the alignment of the specified stack object.
275   void setObjectAlignment(int ObjectIdx, unsigned Align) {
276     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
277            "Invalid Object Idx!");
278     Objects[ObjectIdx+NumFixedObjects].Alignment = Align;
279   }
280
281   /// getObjectOffset - Return the assigned stack offset of the specified object
282   /// from the incoming stack pointer.
283   ///
284   int64_t getObjectOffset(int ObjectIdx) const {
285     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
286            "Invalid Object Idx!");
287     assert(!isDeadObjectIndex(ObjectIdx) &&
288            "Getting frame offset for a dead object?");
289     return Objects[ObjectIdx+NumFixedObjects].SPOffset;
290   }
291
292   /// setObjectOffset - Set the stack frame offset of the specified object.  The
293   /// offset is relative to the stack pointer on entry to the function.
294   ///
295   void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
296     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
297            "Invalid Object Idx!");
298     assert(!isDeadObjectIndex(ObjectIdx) &&
299            "Setting frame offset for a dead object?");
300     Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
301   }
302
303   /// getStackSize - Return the number of bytes that must be allocated to hold
304   /// all of the fixed size frame objects.  This is only valid after
305   /// Prolog/Epilog code insertion has finalized the stack frame layout.
306   ///
307   uint64_t getStackSize() const { return StackSize; }
308
309   /// setStackSize - Set the size of the stack...
310   ///
311   void setStackSize(uint64_t Size) { StackSize = Size; }
312   
313   /// getOffsetAdjustment - Return the correction for frame offsets.
314   ///
315   int getOffsetAdjustment() const { return OffsetAdjustment; }
316   
317   /// setOffsetAdjustment - Set the correction for frame offsets.
318   ///
319   void setOffsetAdjustment(int Adj) { OffsetAdjustment = Adj; }
320
321   /// getMaxAlignment - Return the alignment in bytes that this function must be 
322   /// aligned to, which is greater than the default stack alignment provided by 
323   /// the target.
324   ///
325   unsigned getMaxAlignment() const { return MaxAlignment; }
326   
327   /// setMaxAlignment - Set the preferred alignment.
328   ///
329   void setMaxAlignment(unsigned Align) { MaxAlignment = Align; }
330   
331   /// hasCalls - Return true if the current function has no function calls.
332   /// This is only valid during or after prolog/epilog code emission.
333   ///
334   bool hasCalls() const { return HasCalls; }
335   void setHasCalls(bool V) { HasCalls = V; }
336
337   /// getMaxCallFrameSize - Return the maximum size of a call frame that must be
338   /// allocated for an outgoing function call.  This is only available if
339   /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
340   /// then only during or after prolog/epilog code insertion.
341   ///
342   unsigned getMaxCallFrameSize() const { return MaxCallFrameSize; }
343   void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
344
345   /// CreateFixedObject - Create a new object at a fixed location on the stack.
346   /// All fixed objects should be created before other objects are created for
347   /// efficiency. By default, fixed objects are immutable. This returns an
348   /// index with a negative value.
349   ///
350   int CreateFixedObject(uint64_t Size, int64_t SPOffset,
351                         bool Immutable, bool isSS);
352   
353   
354   /// isFixedObjectIndex - Returns true if the specified index corresponds to a
355   /// fixed stack object.
356   bool isFixedObjectIndex(int ObjectIdx) const {
357     return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
358   }
359
360   /// isImmutableObjectIndex - Returns true if the specified index corresponds
361   /// to an immutable object.
362   bool isImmutableObjectIndex(int ObjectIdx) const {
363     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
364            "Invalid Object Idx!");
365     return Objects[ObjectIdx+NumFixedObjects].isImmutable;
366   }
367
368   /// isSpillSlotObjectIndex - Returns true if the specified index corresponds
369   /// to a spill slot..
370   bool isSpillSlotObjectIndex(int ObjectIdx) const {
371     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
372            "Invalid Object Idx!");
373     return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;;
374   }
375
376   /// isDeadObjectIndex - Returns true if the specified index corresponds to
377   /// a dead object.
378   bool isDeadObjectIndex(int ObjectIdx) const {
379     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
380            "Invalid Object Idx!");
381     return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
382   }
383
384   /// CreateStackObject - Create a new statically sized stack object,
385   /// returning a nonnegative identifier to represent it.
386   ///
387   int CreateStackObject(uint64_t Size, unsigned Alignment, bool isSS) {
388     assert(Size != 0 && "Cannot allocate zero size stack objects!");
389     Objects.push_back(StackObject(Size, Alignment, 0, false, isSS));
390     int Index = (int)Objects.size()-NumFixedObjects-1;
391     assert(Index >= 0 && "Bad frame index!");
392     if (SpillObjects.size() <= static_cast<unsigned>(Index))
393       SpillObjects.resize(Index+1);
394     SpillObjects[Index] = false;
395     return Index;
396   }
397
398   /// CreateSpillStackObject - Create a new statically sized stack
399   /// object that represents a spill slot, returning a nonnegative
400   /// identifier to represent it.
401   ///
402   int CreateSpillStackObject(uint64_t Size, unsigned Alignment) {
403     CreateStackObject(Size, Alignment, true);
404     int Index = (int)Objects.size()-NumFixedObjects-1;
405     if (SpillObjects.size() <= static_cast<unsigned>(Index))
406       SpillObjects.resize(Index+1);
407     SpillObjects[Index] = true;
408     return Index;
409   }
410
411   /// RemoveStackObject - Remove or mark dead a statically sized stack object.
412   ///
413   void RemoveStackObject(int ObjectIdx) {
414     // Mark it dead.
415     Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
416   }
417
418   /// CreateVariableSizedObject - Notify the MachineFrameInfo object that a
419   /// variable sized object has been created.  This must be created whenever a
420   /// variable sized object is created, whether or not the index returned is
421   /// actually used.
422   ///
423   int CreateVariableSizedObject() {
424     HasVarSizedObjects = true;
425     Objects.push_back(StackObject(0, 1, 0, false, false));
426     return (int)Objects.size()-NumFixedObjects-1;
427   }
428
429   /// isSpillObject - Return whether the index refers to a spill slot.
430   ///
431   bool isSpillObject(int Index) const {
432     // Negative indices can't be spill slots.
433     if (Index < 0) return false;
434     assert(static_cast<unsigned>(Index) < SpillObjects.size() &&
435            "Invalid frame index!");
436     return SpillObjects[Index];
437   }
438
439   /// getCalleeSavedInfo - Returns a reference to call saved info vector for the
440   /// current function.
441   const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
442     return CSInfo;
443   }
444
445   /// setCalleeSavedInfo - Used by prolog/epilog inserter to set the function's
446   /// callee saved information.
447   void  setCalleeSavedInfo(const std::vector<CalleeSavedInfo> &CSI) {
448     CSInfo = CSI;
449   }
450
451   /// isCalleeSavedInfoValid - Has the callee saved info been calculated yet?
452   bool isCalleeSavedInfoValid() const { return CSIValid; }
453
454   void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
455
456   /// getPristineRegs - Return a set of physical registers that are pristine on
457   /// entry to the MBB.
458   ///
459   /// Pristine registers hold a value that is useless to the current function,
460   /// but that must be preserved - they are callee saved registers that have not
461   /// been saved yet.
462   ///
463   /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
464   /// method always returns an empty set.
465   BitVector getPristineRegs(const MachineBasicBlock *MBB) const;
466
467   /// getMachineModuleInfo - Used by a prologue/epilogue
468   /// emitter (TargetRegisterInfo) to provide frame layout information. 
469   MachineModuleInfo *getMachineModuleInfo() const { return MMI; }
470
471   /// setMachineModuleInfo - Used by a meta info consumer (DwarfWriter) to
472   /// indicate that frame layout information should be gathered.
473   void setMachineModuleInfo(MachineModuleInfo *mmi) { MMI = mmi; }
474
475   /// print - Used by the MachineFunction printer to print information about
476   /// stack objects.  Implemented in MachineFunction.cpp
477   ///
478   void print(const MachineFunction &MF, raw_ostream &OS) const;
479
480   /// dump - Print the function to stderr.
481   void dump(const MachineFunction &MF) const;
482 };
483
484 } // End llvm namespace
485
486 #endif