LLVM API Documentation

MachineFrameInfo.h

Go to the documentation of this file.
00001 //===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // The MachineFrameInfo class represents an abstract stack frame until
00011 // prolog/epilog code is inserted.  This class is key to allowing stack frame
00012 // representation optimizations, such as frame pointer elimination.  It also
00013 // allows more mundane (but still important) optimizations, such as reordering
00014 // of abstract objects on the stack frame.
00015 //
00016 // To support this, the class assigns unique integer identifiers to stack
00017 // objects requested clients.  These identifiers are negative integers for fixed
00018 // stack objects (such as arguments passed on the stack) or positive for objects
00019 // that may be reordered.  Instructions which refer to stack objects use a
00020 // special MO_FrameIndex operand to represent these frame indexes.
00021 //
00022 // Because this class keeps track of all references to the stack frame, it knows
00023 // when a variable sized object is allocated on the stack.  This is the sole
00024 // condition which prevents frame pointer elimination, which is an important
00025 // optimization on register-poor architectures.  Because original variable sized
00026 // alloca's in the source program are the only source of variable sized stack
00027 // objects, it is safe to decide whether there will be any variable sized
00028 // objects before all stack objects are known (for example, register allocator
00029 // spill code never needs variable sized objects).
00030 //
00031 // When prolog/epilog code emission is performed, the final stack frame is built
00032 // and the machine instructions are modified to refer to the actual stack
00033 // offsets of the object, eliminating all MO_FrameIndex operands from the
00034 // program.
00035 //
00036 //===----------------------------------------------------------------------===//
00037 
00038 #ifndef LLVM_CODEGEN_MACHINEFRAMEINFO_H
00039 #define LLVM_CODEGEN_MACHINEFRAMEINFO_H
00040 
00041 #include <vector>
00042 
00043 namespace llvm {
00044 class TargetData;
00045 class TargetRegisterClass;
00046 class Type;
00047 class MachineDebugInfo;
00048 class MachineFunction;
00049 
00050 class MachineFrameInfo {
00051 
00052   // StackObject - Represent a single object allocated on the stack.
00053   struct StackObject {
00054     // The size of this object on the stack. 0 means a variable sized object
00055     unsigned Size;
00056 
00057     // Alignment - The required alignment of this stack slot.
00058     unsigned Alignment;
00059 
00060     // SPOffset - The offset of this object from the stack pointer on entry to
00061     // the function.  This field has no meaning for a variable sized element.
00062     int SPOffset;
00063 
00064     StackObject(unsigned Sz, unsigned Al, int SP)
00065       : Size(Sz), Alignment(Al), SPOffset(SP) {}
00066   };
00067 
00068   /// Objects - The list of stack objects allocated...
00069   ///
00070   std::vector<StackObject> Objects;
00071 
00072   /// NumFixedObjects - This contains the number of fixed objects contained on
00073   /// the stack.  Because fixed objects are stored at a negative index in the
00074   /// Objects list, this is also the index to the 0th object in the list.
00075   ///
00076   unsigned NumFixedObjects;
00077 
00078   /// HasVarSizedObjects - This boolean keeps track of whether any variable
00079   /// sized objects have been allocated yet.
00080   ///
00081   bool HasVarSizedObjects;
00082 
00083   /// StackSize - The prolog/epilog code inserter calculates the final stack
00084   /// offsets for all of the fixed size objects, updating the Objects list
00085   /// above.  It then updates StackSize to contain the number of bytes that need
00086   /// to be allocated on entry to the function.
00087   ///
00088   unsigned StackSize;
00089   
00090   /// MaxAlignment - The prolog/epilog code inserter may process objects 
00091   /// that require greater alignment than the default alignment the target
00092   /// provides. To handle this, MaxAlignment is set to the maximum alignment 
00093   /// needed by the objects on the current frame.  If this is greater than the
00094   /// native alignment maintained by the compiler, dynamic alignment code will
00095   /// be needed.
00096   ///
00097   unsigned MaxAlignment;
00098 
00099   /// HasCalls - Set to true if this function has any function calls.  This is
00100   /// only valid during and after prolog/epilog code insertion.
00101   bool HasCalls;
00102 
00103   /// MaxCallFrameSize - This contains the size of the largest call frame if the
00104   /// target uses frame setup/destroy pseudo instructions (as defined in the
00105   /// TargetFrameInfo class).  This information is important for frame pointer
00106   /// elimination.  If is only valid during and after prolog/epilog code
00107   /// insertion.
00108   ///
00109   unsigned MaxCallFrameSize;
00110   
00111   /// DebugInfo - This field is set (via setMachineDebugInfo) by a debug info
00112   /// consumer (ex. DwarfWriter) to indicate that frame layout information
00113   /// should be acquired.  Typically, it's the responsibility of the target's
00114   /// MRegisterInfo prologue/epilogue emitting code to inform MachineDebugInfo
00115   /// of frame layouts.
00116   MachineDebugInfo *DebugInfo;
00117   
00118 public:
00119   MachineFrameInfo() {
00120     NumFixedObjects = StackSize = MaxAlignment = 0;
00121     HasVarSizedObjects = false;
00122     HasCalls = false;
00123     MaxCallFrameSize = 0;
00124     DebugInfo = 0;
00125   }
00126 
00127   /// hasStackObjects - Return true if there are any stack objects in this
00128   /// function.
00129   ///
00130   bool hasStackObjects() const { return !Objects.empty(); }
00131 
00132   /// hasVarSizedObjects - This method may be called any time after instruction
00133   /// selection is complete to determine if the stack frame for this function
00134   /// contains any variable sized objects.
00135   ///
00136   bool hasVarSizedObjects() const { return HasVarSizedObjects; }
00137 
00138   /// getObjectIndexBegin - Return the minimum frame object index...
00139   ///
00140   int getObjectIndexBegin() const { return -NumFixedObjects; }
00141 
00142   /// getObjectIndexEnd - Return one past the maximum frame object index...
00143   ///
00144   int getObjectIndexEnd() const { return Objects.size()-NumFixedObjects; }
00145 
00146   /// getObjectSize - Return the size of the specified object
00147   ///
00148   int getObjectSize(int ObjectIdx) const {
00149     assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
00150     return Objects[ObjectIdx+NumFixedObjects].Size;
00151   }
00152 
00153   /// getObjectAlignment - Return the alignment of the specified stack object...
00154   int getObjectAlignment(int ObjectIdx) const {
00155     assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
00156     return Objects[ObjectIdx+NumFixedObjects].Alignment;
00157   }
00158 
00159   /// getObjectOffset - Return the assigned stack offset of the specified object
00160   /// from the incoming stack pointer.
00161   ///
00162   int getObjectOffset(int ObjectIdx) const {
00163     assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
00164     return Objects[ObjectIdx+NumFixedObjects].SPOffset;
00165   }
00166 
00167   /// setObjectOffset - Set the stack frame offset of the specified object.  The
00168   /// offset is relative to the stack pointer on entry to the function.
00169   ///
00170   void setObjectOffset(int ObjectIdx, int SPOffset) {
00171     assert(ObjectIdx+NumFixedObjects < Objects.size() && "Invalid Object Idx!");
00172     Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
00173   }
00174 
00175   /// getStackSize - Return the number of bytes that must be allocated to hold
00176   /// all of the fixed size frame objects.  This is only valid after
00177   /// Prolog/Epilog code insertion has finalized the stack frame layout.
00178   ///
00179   unsigned getStackSize() const { return StackSize; }
00180 
00181   /// setStackSize - Set the size of the stack...
00182   ///
00183   void setStackSize(unsigned Size) { StackSize = Size; }
00184 
00185   /// getMaxAlignment - Return the alignment in bytes that this function must be 
00186   /// aligned to, which is greater than the default stack alignment provided by 
00187   /// the target.
00188   ///
00189   unsigned getMaxAlignment() const { return MaxAlignment; }
00190   
00191   /// setMaxAlignment - Set the preferred alignment.
00192   ///
00193   void setMaxAlignment(unsigned Align) { MaxAlignment = Align; }
00194   
00195   /// hasCalls - Return true if the current function has no function calls.
00196   /// This is only valid during or after prolog/epilog code emission.
00197   ///
00198   bool hasCalls() const { return HasCalls; }
00199   void setHasCalls(bool V) { HasCalls = V; }
00200 
00201   /// getMaxCallFrameSize - Return the maximum size of a call frame that must be
00202   /// allocated for an outgoing function call.  This is only available if
00203   /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
00204   /// then only during or after prolog/epilog code insertion.
00205   ///
00206   unsigned getMaxCallFrameSize() const { return MaxCallFrameSize; }
00207   void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
00208 
00209   /// CreateFixedObject - Create a new object at a fixed location on the stack.
00210   /// All fixed objects should be created before other objects are created for
00211   /// efficiency.  This returns an index with a negative value.
00212   ///
00213   int CreateFixedObject(unsigned Size, int SPOffset) {
00214     assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
00215     Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset));
00216     return -++NumFixedObjects;
00217   }
00218 
00219   /// CreateStackObject - Create a new statically sized stack object, returning
00220   /// a postive identifier to represent it.
00221   ///
00222   int CreateStackObject(unsigned Size, unsigned Alignment) {
00223     // Keep track of the maximum alignment.
00224     if (MaxAlignment < Alignment) MaxAlignment = Alignment;
00225     
00226     assert(Size != 0 && "Cannot allocate zero size stack objects!");
00227     Objects.push_back(StackObject(Size, Alignment, -1));
00228     return Objects.size()-NumFixedObjects-1;
00229   }
00230 
00231   /// CreateVariableSizedObject - Notify the MachineFrameInfo object that a
00232   /// variable sized object has been created.  This must be created whenever a
00233   /// variable sized object is created, whether or not the index returned is
00234   /// actually used.
00235   ///
00236   int CreateVariableSizedObject() {
00237     HasVarSizedObjects = true;
00238     if (MaxAlignment < 1) MaxAlignment = 1;
00239     Objects.push_back(StackObject(0, 1, -1));
00240     return Objects.size()-NumFixedObjects-1;
00241   }
00242 
00243   /// getMachineDebugInfo - Used by a prologue/epilogue emitter (MRegisterInfo)
00244   /// to provide frame layout information. 
00245   MachineDebugInfo *getMachineDebugInfo() const { return DebugInfo; }
00246 
00247   /// setMachineDebugInfo - Used by a debug consumer (DwarfWriter) to indicate
00248   /// that frame layout information should be gathered.
00249   void setMachineDebugInfo(MachineDebugInfo *DI) { DebugInfo = DI; }
00250 
00251   /// print - Used by the MachineFunction printer to print information about
00252   /// stack objects.  Implemented in MachineFunction.cpp
00253   ///
00254   void print(const MachineFunction &MF, std::ostream &OS) const;
00255 
00256   /// dump - Call print(MF, std::cerr) to be called from the debugger.
00257   void dump(const MachineFunction &MF) const;
00258 };
00259 
00260 } // End llvm namespace
00261 
00262 #endif