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