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 is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // The file defines the MachineFrameInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CODEGEN_MACHINEFRAMEINFO_H
00015 #define LLVM_CODEGEN_MACHINEFRAMEINFO_H
00016 
00017 #include "llvm/ADT/BitVector.h"
00018 #include "llvm/ADT/DenseSet.h"
00019 #include "llvm/Support/DataTypes.h"
00020 #include <cassert>
00021 #include <iosfwd>
00022 #include <vector>
00023 
00024 namespace llvm {
00025 class TargetData;
00026 class TargetRegisterClass;
00027 class Type;
00028 class MachineModuleInfo;
00029 class MachineFunction;
00030 class MachineBasicBlock;
00031 class TargetFrameInfo;
00032 
00033 /// The CalleeSavedInfo class tracks the information need to locate where a
00034 /// callee saved register in the current frame.  
00035 class CalleeSavedInfo {
00036 
00037 private:
00038   unsigned Reg;
00039   const TargetRegisterClass *RegClass;
00040   int FrameIdx;
00041   
00042 public:
00043   CalleeSavedInfo(unsigned R, const TargetRegisterClass *RC, int FI = 0)
00044   : Reg(R)
00045   , RegClass(RC)
00046   , FrameIdx(FI)
00047   {}
00048   
00049   // Accessors.
00050   unsigned getReg()                        const { return Reg; }
00051   const TargetRegisterClass *getRegClass() const { return RegClass; }
00052   int getFrameIdx()                        const { return FrameIdx; }
00053   void setFrameIdx(int FI)                       { FrameIdx = FI; }
00054 };
00055 
00056 /// The MachineFrameInfo class represents an abstract stack frame until
00057 /// prolog/epilog code is inserted.  This class is key to allowing stack frame
00058 /// representation optimizations, such as frame pointer elimination.  It also
00059 /// allows more mundane (but still important) optimizations, such as reordering
00060 /// of abstract objects on the stack frame.
00061 ///
00062 /// To support this, the class assigns unique integer identifiers to stack
00063 /// objects requested clients.  These identifiers are negative integers for
00064 /// fixed stack objects (such as arguments passed on the stack) or nonnegative
00065 /// for objects that may be reordered.  Instructions which refer to stack
00066 /// objects use a special MO_FrameIndex operand to represent these frame
00067 /// indexes.
00068 ///
00069 /// Because this class keeps track of all references to the stack frame, it
00070 /// knows when a variable sized object is allocated on the stack.  This is the
00071 /// sole condition which prevents frame pointer elimination, which is an
00072 /// important optimization on register-poor architectures.  Because original
00073 /// variable sized alloca's in the source program are the only source of
00074 /// variable sized stack objects, it is safe to decide whether there will be
00075 /// any variable sized objects before all stack objects are known (for
00076 /// example, register allocator spill code never needs variable sized
00077 /// objects).
00078 ///
00079 /// When prolog/epilog code emission is performed, the final stack frame is
00080 /// built and the machine instructions are modified to refer to the actual
00081 /// stack offsets of the object, eliminating all MO_FrameIndex operands from
00082 /// the program.
00083 ///
00084 /// @brief Abstract Stack Frame Information
00085 class MachineFrameInfo {
00086 
00087   // StackObject - Represent a single object allocated on the stack.
00088   struct StackObject {
00089     // The size of this object on the stack. 0 means a variable sized object,
00090     // ~0ULL means a dead object.
00091     uint64_t Size;
00092 
00093     // Alignment - The required alignment of this stack slot.
00094     unsigned Alignment;
00095 
00096     // isImmutable - If true, the value of the stack object is set before
00097     // entering the function and is not modified inside the function. By
00098     // default, fixed objects are immutable unless marked otherwise.
00099     bool isImmutable;
00100 
00101     // SPOffset - The offset of this object from the stack pointer on entry to
00102     // the function.  This field has no meaning for a variable sized element.
00103     int64_t SPOffset;
00104     
00105     StackObject(uint64_t Sz, unsigned Al, int64_t SP = 0, bool IM = false)
00106       : Size(Sz), Alignment(Al), isImmutable(IM), SPOffset(SP) {}
00107   };
00108 
00109   /// Objects - The list of stack objects allocated...
00110   ///
00111   std::vector<StackObject> Objects;
00112 
00113   /// NumFixedObjects - This contains the number of fixed objects contained on
00114   /// the stack.  Because fixed objects are stored at a negative index in the
00115   /// Objects list, this is also the index to the 0th object in the list.
00116   ///
00117   unsigned NumFixedObjects;
00118 
00119   /// HasVarSizedObjects - This boolean keeps track of whether any variable
00120   /// sized objects have been allocated yet.
00121   ///
00122   bool HasVarSizedObjects;
00123 
00124   /// FrameAddressTaken - This boolean keeps track of whether there is a call
00125   /// to builtin \@llvm.frameaddress.
00126   bool FrameAddressTaken;
00127 
00128   /// StackSize - The prolog/epilog code inserter calculates the final stack
00129   /// offsets for all of the fixed size objects, updating the Objects list
00130   /// above.  It then updates StackSize to contain the number of bytes that need
00131   /// to be allocated on entry to the function.
00132   ///
00133   uint64_t StackSize;
00134   
00135   /// OffsetAdjustment - The amount that a frame offset needs to be adjusted to
00136   /// have the actual offset from the stack/frame pointer.  The calculation is 
00137   /// MFI->getObjectOffset(Index) + StackSize - TFI.getOffsetOfLocalArea() +
00138   /// OffsetAdjustment.  If OffsetAdjustment is zero (default) then offsets are
00139   /// away from TOS. If OffsetAdjustment == StackSize then offsets are toward
00140   /// TOS.
00141   int OffsetAdjustment;
00142   
00143   /// MaxAlignment - The prolog/epilog code inserter may process objects 
00144   /// that require greater alignment than the default alignment the target
00145   /// provides. To handle this, MaxAlignment is set to the maximum alignment 
00146   /// needed by the objects on the current frame.  If this is greater than the
00147   /// native alignment maintained by the compiler, dynamic alignment code will
00148   /// be needed.
00149   ///
00150   unsigned MaxAlignment;
00151 
00152   /// HasCalls - Set to true if this function has any function calls.  This is
00153   /// only valid during and after prolog/epilog code insertion.
00154   bool HasCalls;
00155 
00156   /// StackProtectorIdx - The frame index for the stack protector.
00157   int StackProtectorIdx;
00158 
00159   /// MaxCallFrameSize - This contains the size of the largest call frame if the
00160   /// target uses frame setup/destroy pseudo instructions (as defined in the
00161   /// TargetFrameInfo class).  This information is important for frame pointer
00162   /// elimination.  If is only valid during and after prolog/epilog code
00163   /// insertion.
00164   ///
00165   unsigned MaxCallFrameSize;
00166   
00167   /// CSInfo - The prolog/epilog code inserter fills in this vector with each
00168   /// callee saved register saved in the frame.  Beyond its use by the prolog/
00169   /// epilog code inserter, this data used for debug info and exception
00170   /// handling.
00171   std::vector<CalleeSavedInfo> CSInfo;
00172 
00173   /// CSIValid - Has CSInfo been set yet?
00174   bool CSIValid;
00175 
00176   /// MMI - This field is set (via setMachineModuleInfo) by a module info
00177   /// consumer (ex. DwarfWriter) to indicate that frame layout information
00178   /// should be acquired.  Typically, it's the responsibility of the target's
00179   /// TargetRegisterInfo prologue/epilogue emitting code to inform
00180   /// MachineModuleInfo of frame layouts.
00181   MachineModuleInfo *MMI;
00182   
00183   /// TargetFrameInfo - Target information about frame layout.
00184   ///
00185   const TargetFrameInfo &TFI;
00186 public:
00187   explicit MachineFrameInfo(const TargetFrameInfo &tfi) : TFI(tfi) {
00188     StackSize = NumFixedObjects = OffsetAdjustment = MaxAlignment = 0;
00189     HasVarSizedObjects = false;
00190     FrameAddressTaken = false;
00191     HasCalls = false;
00192     StackProtectorIdx = -1;
00193     MaxCallFrameSize = 0;
00194     CSIValid = false;
00195     MMI = 0;
00196   }
00197 
00198   /// hasStackObjects - Return true if there are any stack objects in this
00199   /// function.
00200   ///
00201   bool hasStackObjects() const { return !Objects.empty(); }
00202 
00203   /// hasVarSizedObjects - This method may be called any time after instruction
00204   /// selection is complete to determine if the stack frame for this function
00205   /// contains any variable sized objects.
00206   ///
00207   bool hasVarSizedObjects() const { return HasVarSizedObjects; }
00208 
00209   /// getStackProtectorIndex/setStackProtectorIndex - Return the index for the
00210   /// stack protector object.
00211   ///
00212   int getStackProtectorIndex() const { return StackProtectorIdx; }
00213   void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
00214 
00215   /// isFrameAddressTaken - This method may be called any time after instruction
00216   /// selection is complete to determine if there is a call to
00217   /// \@llvm.frameaddress in this function.
00218   bool isFrameAddressTaken() const { return FrameAddressTaken; }
00219   void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; }
00220 
00221   /// getObjectIndexBegin - Return the minimum frame object index.
00222   ///
00223   int getObjectIndexBegin() const { return -NumFixedObjects; }
00224 
00225   /// getObjectIndexEnd - Return one past the maximum frame object index.
00226   ///
00227   int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
00228 
00229   /// getNumFixedObjects() - Return the number of fixed objects.
00230   unsigned getNumFixedObjects() const { return NumFixedObjects; }
00231 
00232   /// getNumObjects() - Return the number of objects.
00233   ///
00234   unsigned getNumObjects() const { return Objects.size(); }
00235 
00236   /// getObjectSize - Return the size of the specified object.
00237   ///
00238   int64_t getObjectSize(int ObjectIdx) const {
00239     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
00240            "Invalid Object Idx!");
00241     return Objects[ObjectIdx+NumFixedObjects].Size;
00242   }
00243 
00244   /// setObjectSize - Change the size of the specified stack object.
00245   void setObjectSize(int ObjectIdx, int64_t Size) {
00246     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
00247            "Invalid Object Idx!");
00248     Objects[ObjectIdx+NumFixedObjects].Size = Size;
00249   }
00250 
00251   /// getObjectAlignment - Return the alignment of the specified stack object.
00252   unsigned getObjectAlignment(int ObjectIdx) const {
00253     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
00254            "Invalid Object Idx!");
00255     return Objects[ObjectIdx+NumFixedObjects].Alignment;
00256   }
00257 
00258   /// setObjectAlignment - Change the alignment of the specified stack object.
00259   void setObjectAlignment(int ObjectIdx, unsigned Align) {
00260     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
00261            "Invalid Object Idx!");
00262     Objects[ObjectIdx+NumFixedObjects].Alignment = Align;
00263   }
00264 
00265   /// getObjectOffset - Return the assigned stack offset of the specified object
00266   /// from the incoming stack pointer.
00267   ///
00268   int64_t getObjectOffset(int ObjectIdx) const {
00269     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
00270            "Invalid Object Idx!");
00271     assert(!isDeadObjectIndex(ObjectIdx) &&
00272            "Getting frame offset for a dead object?");
00273     return Objects[ObjectIdx+NumFixedObjects].SPOffset;
00274   }
00275 
00276   /// setObjectOffset - Set the stack frame offset of the specified object.  The
00277   /// offset is relative to the stack pointer on entry to the function.
00278   ///
00279   void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
00280     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
00281            "Invalid Object Idx!");
00282     assert(!isDeadObjectIndex(ObjectIdx) &&
00283            "Setting frame offset for a dead object?");
00284     Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
00285   }
00286 
00287   /// getStackSize - Return the number of bytes that must be allocated to hold
00288   /// all of the fixed size frame objects.  This is only valid after
00289   /// Prolog/Epilog code insertion has finalized the stack frame layout.
00290   ///
00291   uint64_t getStackSize() const { return StackSize; }
00292 
00293   /// setStackSize - Set the size of the stack...
00294   ///
00295   void setStackSize(uint64_t Size) { StackSize = Size; }
00296   
00297   /// getOffsetAdjustment - Return the correction for frame offsets.
00298   ///
00299   int getOffsetAdjustment() const { return OffsetAdjustment; }
00300   
00301   /// setOffsetAdjustment - Set the correction for frame offsets.
00302   ///
00303   void setOffsetAdjustment(int Adj) { OffsetAdjustment = Adj; }
00304 
00305   /// getMaxAlignment - Return the alignment in bytes that this function must be 
00306   /// aligned to, which is greater than the default stack alignment provided by 
00307   /// the target.
00308   ///
00309   unsigned getMaxAlignment() const { return MaxAlignment; }
00310   
00311   /// setMaxAlignment - Set the preferred alignment.
00312   ///
00313   void setMaxAlignment(unsigned Align) { MaxAlignment = Align; }
00314   
00315   /// hasCalls - Return true if the current function has no function calls.
00316   /// This is only valid during or after prolog/epilog code emission.
00317   ///
00318   bool hasCalls() const { return HasCalls; }
00319   void setHasCalls(bool V) { HasCalls = V; }
00320 
00321   /// getMaxCallFrameSize - Return the maximum size of a call frame that must be
00322   /// allocated for an outgoing function call.  This is only available if
00323   /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
00324   /// then only during or after prolog/epilog code insertion.
00325   ///
00326   unsigned getMaxCallFrameSize() const { return MaxCallFrameSize; }
00327   void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
00328 
00329   /// CreateFixedObject - Create a new object at a fixed location on the stack.
00330   /// All fixed objects should be created before other objects are created for
00331   /// efficiency. By default, fixed objects are immutable. This returns an
00332   /// index with a negative value.
00333   ///
00334   int CreateFixedObject(uint64_t Size, int64_t SPOffset,
00335                         bool Immutable = true);
00336   
00337   
00338   /// isFixedObjectIndex - Returns true if the specified index corresponds to a
00339   /// fixed stack object.
00340   bool isFixedObjectIndex(int ObjectIdx) const {
00341     return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
00342   }
00343 
00344   /// isImmutableObjectIndex - Returns true if the specified index corresponds
00345   /// to an immutable object.
00346   bool isImmutableObjectIndex(int ObjectIdx) const {
00347     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
00348            "Invalid Object Idx!");
00349     return Objects[ObjectIdx+NumFixedObjects].isImmutable;
00350   }
00351 
00352   /// isDeadObjectIndex - Returns true if the specified index corresponds to
00353   /// a dead object.
00354   bool isDeadObjectIndex(int ObjectIdx) const {
00355     assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
00356            "Invalid Object Idx!");
00357     return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
00358   }
00359 
00360   /// CreateStackObject - Create a new statically sized stack object, returning
00361   /// a nonnegative identifier to represent it.
00362   ///
00363   int CreateStackObject(uint64_t Size, unsigned Alignment) {
00364     assert(Size != 0 && "Cannot allocate zero size stack objects!");
00365     Objects.push_back(StackObject(Size, Alignment));
00366     return (int)Objects.size()-NumFixedObjects-1;
00367   }
00368 
00369   /// RemoveStackObject - Remove or mark dead a statically sized stack object.
00370   ///
00371   void RemoveStackObject(int ObjectIdx) {
00372     // Mark it dead.
00373     Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
00374   }
00375 
00376   /// CreateVariableSizedObject - Notify the MachineFrameInfo object that a
00377   /// variable sized object has been created.  This must be created whenever a
00378   /// variable sized object is created, whether or not the index returned is
00379   /// actually used.
00380   ///
00381   int CreateVariableSizedObject() {
00382     HasVarSizedObjects = true;
00383     Objects.push_back(StackObject(0, 1));
00384     return (int)Objects.size()-NumFixedObjects-1;
00385   }
00386   
00387   /// getCalleeSavedInfo - Returns a reference to call saved info vector for the
00388   /// current function.
00389   const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
00390     return CSInfo;
00391   }
00392 
00393   /// setCalleeSavedInfo - Used by prolog/epilog inserter to set the function's
00394   /// callee saved information.
00395   void  setCalleeSavedInfo(const std::vector<CalleeSavedInfo> &CSI) {
00396     CSInfo = CSI;
00397   }
00398 
00399   /// isCalleeSavedInfoValid - Has the callee saved info been calculated yet?
00400   bool isCalleeSavedInfoValid() const { return CSIValid; }
00401 
00402   void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
00403 
00404   /// getPristineRegs - Return a set of physical registers that are pristine on
00405   /// entry to the MBB.
00406   ///
00407   /// Pristine registers hold a value that is useless to the current function,
00408   /// but that must be preserved - they are callee saved registers that have not
00409   /// been saved yet.
00410   ///
00411   /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
00412   /// method always returns an empty set.
00413   BitVector getPristineRegs(const MachineBasicBlock *MBB) const;
00414 
00415   /// getMachineModuleInfo - Used by a prologue/epilogue
00416   /// emitter (TargetRegisterInfo) to provide frame layout information. 
00417   MachineModuleInfo *getMachineModuleInfo() const { return MMI; }
00418 
00419   /// setMachineModuleInfo - Used by a meta info consumer (DwarfWriter) to
00420   /// indicate that frame layout information should be gathered.
00421   void setMachineModuleInfo(MachineModuleInfo *mmi) { MMI = mmi; }
00422 
00423   /// print - Used by the MachineFunction printer to print information about
00424   /// stack objects.  Implemented in MachineFunction.cpp
00425   ///
00426   void print(const MachineFunction &MF, std::ostream &OS) const;
00427 
00428   /// dump - Print the function to stderr.
00429   void dump(const MachineFunction &MF) const;
00430 };
00431 
00432 } // End llvm namespace
00433 
00434 #endif