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