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