LLVM API Documentation

MachineConstantPool.h

Go to the documentation of this file.
00001 //===-- CodeGen/MachineConstantPool.h - Abstract Constant Pool --*- 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 /// @file
00011 /// This file declares the MachineConstantPool class which is an abstract
00012 /// constant pool to keep track of constants referenced by a function.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
00017 #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
00018 
00019 #include <cassert>
00020 #include <climits>
00021 #include <vector>
00022 
00023 namespace llvm {
00024 
00025 class Constant;
00026 class FoldingSetNodeID;
00027 class TargetData;
00028 class TargetMachine;
00029 class Type;
00030 class MachineConstantPool;
00031 class raw_ostream;
00032 
00033 /// Abstract base class for all machine specific constantpool value subclasses.
00034 ///
00035 class MachineConstantPoolValue {
00036   const Type *Ty;
00037 
00038 public:
00039   explicit MachineConstantPoolValue(const Type *ty) : Ty(ty) {}
00040   virtual ~MachineConstantPoolValue() {}
00041 
00042   /// getType - get type of this MachineConstantPoolValue.
00043   ///
00044   const Type *getType() const { return Ty; }
00045 
00046   
00047   /// getRelocationInfo - This method classifies the entry according to
00048   /// whether or not it may generate a relocation entry.  This must be
00049   /// conservative, so if it might codegen to a relocatable entry, it should say
00050   /// so.  The return values are the same as Constant::getRelocationInfo().
00051   virtual unsigned getRelocationInfo() const = 0;
00052   
00053   virtual int getExistingMachineCPValue(MachineConstantPool *CP,
00054                                         unsigned Alignment) = 0;
00055 
00056   virtual void AddSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
00057 
00058   /// print - Implement operator<<
00059   virtual void print(raw_ostream &O) const = 0;
00060 };
00061 
00062 inline raw_ostream &operator<<(raw_ostream &OS,
00063                                const MachineConstantPoolValue &V) {
00064   V.print(OS);
00065   return OS;
00066 }
00067   
00068 
00069 /// This class is a data container for one entry in a MachineConstantPool.
00070 /// It contains a pointer to the value and an offset from the start of
00071 /// the constant pool.
00072 /// @brief An entry in a MachineConstantPool
00073 class MachineConstantPoolEntry {
00074 public:
00075   /// The constant itself.
00076   union {
00077     Constant *ConstVal;
00078     MachineConstantPoolValue *MachineCPVal;
00079   } Val;
00080 
00081   /// The required alignment for this entry. The top bit is set when Val is
00082   /// a MachineConstantPoolValue.
00083   unsigned Alignment;
00084 
00085   MachineConstantPoolEntry(Constant *V, unsigned A)
00086     : Alignment(A) {
00087     Val.ConstVal = V;
00088   }
00089   MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A)
00090     : Alignment(A) {
00091     Val.MachineCPVal = V; 
00092     Alignment |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
00093   }
00094 
00095   bool isMachineConstantPoolEntry() const {
00096     return (int)Alignment < 0;
00097   }
00098 
00099   int getAlignment() const { 
00100     return Alignment & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
00101   }
00102 
00103   const Type *getType() const;
00104   
00105   /// getRelocationInfo - This method classifies the entry according to
00106   /// whether or not it may generate a relocation entry.  This must be
00107   /// conservative, so if it might codegen to a relocatable entry, it should say
00108   /// so.  The return values are:
00109   /// 
00110   ///  0: This constant pool entry is guaranteed to never have a relocation
00111   ///     applied to it (because it holds a simple constant like '4').
00112   ///  1: This entry has relocations, but the entries are guaranteed to be
00113   ///     resolvable by the static linker, so the dynamic linker will never see
00114   ///     them.
00115   ///  2: This entry may have arbitrary relocations. 
00116   unsigned getRelocationInfo() const;
00117 };
00118   
00119 /// The MachineConstantPool class keeps track of constants referenced by a
00120 /// function which must be spilled to memory.  This is used for constants which
00121 /// are unable to be used directly as operands to instructions, which typically
00122 /// include floating point and large integer constants.
00123 ///
00124 /// Instructions reference the address of these constant pool constants through
00125 /// the use of MO_ConstantPoolIndex values.  When emitting assembly or machine
00126 /// code, these virtual address references are converted to refer to the
00127 /// address of the function constant pool values.
00128 /// @brief The machine constant pool.
00129 class MachineConstantPool {
00130   const TargetData *TD;   ///< The machine's TargetData.
00131   unsigned PoolAlignment; ///< The alignment for the pool.
00132   std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
00133 public:
00134   /// @brief The only constructor.
00135   explicit MachineConstantPool(const TargetData *td)
00136     : TD(td), PoolAlignment(1) {}
00137   ~MachineConstantPool();
00138     
00139   /// getConstantPoolAlignment - Return the the alignment required by
00140   /// the whole constant pool, of which the first element must be aligned.
00141   unsigned getConstantPoolAlignment() const { return PoolAlignment; }
00142   
00143   /// getConstantPoolIndex - Create a new entry in the constant pool or return
00144   /// an existing one.  User must specify the minimum required alignment for
00145   /// the object.
00146   unsigned getConstantPoolIndex(Constant *C, unsigned Alignment);
00147   unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
00148   
00149   /// isEmpty - Return true if this constant pool contains no constants.
00150   bool isEmpty() const { return Constants.empty(); }
00151 
00152   const std::vector<MachineConstantPoolEntry> &getConstants() const {
00153     return Constants;
00154   }
00155 
00156   /// print - Used by the MachineFunction printer to print information about
00157   /// constant pool objects.  Implemented in MachineFunction.cpp
00158   ///
00159   void print(raw_ostream &OS) const;
00160 
00161   /// dump - Call print(cerr) to be called from the debugger.
00162   void dump() const;
00163 };
00164 
00165 } // End llvm namespace
00166 
00167 #endif