Home | History | Annotate | Download | only in CodeGen
      1 //===-- CodeGen/MachineConstantPool.h - Abstract Constant Pool --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 /// @file
     11 /// This file declares the MachineConstantPool class which is an abstract
     12 /// constant pool to keep track of constants referenced by a function.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
     17 #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
     18 
     19 #include "llvm/ADT/DenseSet.h"
     20 #include <cassert>
     21 #include <climits>
     22 #include <vector>
     23 
     24 namespace llvm {
     25 
     26 class Constant;
     27 class FoldingSetNodeID;
     28 class DataLayout;
     29 class TargetMachine;
     30 class Type;
     31 class MachineConstantPool;
     32 class raw_ostream;
     33 
     34 /// Abstract base class for all machine specific constantpool value subclasses.
     35 ///
     36 class MachineConstantPoolValue {
     37   virtual void anchor();
     38   Type *Ty;
     39 
     40 public:
     41   explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {}
     42   virtual ~MachineConstantPoolValue() {}
     43 
     44   /// getType - get type of this MachineConstantPoolValue.
     45   ///
     46   Type *getType() const { return Ty; }
     47 
     48 
     49   /// getRelocationInfo - This method classifies the entry according to
     50   /// whether or not it may generate a relocation entry.  This must be
     51   /// conservative, so if it might codegen to a relocatable entry, it should say
     52   /// so.  The return values are the same as Constant::getRelocationInfo().
     53   virtual unsigned getRelocationInfo() const = 0;
     54 
     55   virtual int getExistingMachineCPValue(MachineConstantPool *CP,
     56                                         unsigned Alignment) = 0;
     57 
     58   virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
     59 
     60   /// print - Implement operator<<
     61   virtual void print(raw_ostream &O) const = 0;
     62 };
     63 
     64 inline raw_ostream &operator<<(raw_ostream &OS,
     65                                const MachineConstantPoolValue &V) {
     66   V.print(OS);
     67   return OS;
     68 }
     69 
     70 
     71 /// This class is a data container for one entry in a MachineConstantPool.
     72 /// It contains a pointer to the value and an offset from the start of
     73 /// the constant pool.
     74 /// @brief An entry in a MachineConstantPool
     75 class MachineConstantPoolEntry {
     76 public:
     77   /// The constant itself.
     78   union {
     79     const Constant *ConstVal;
     80     MachineConstantPoolValue *MachineCPVal;
     81   } Val;
     82 
     83   /// The required alignment for this entry. The top bit is set when Val is
     84   /// a target specific MachineConstantPoolValue.
     85   unsigned Alignment;
     86 
     87   MachineConstantPoolEntry(const Constant *V, unsigned A)
     88     : Alignment(A) {
     89     Val.ConstVal = V;
     90   }
     91   MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A)
     92     : Alignment(A) {
     93     Val.MachineCPVal = V;
     94     Alignment |= 1U << (sizeof(unsigned)*CHAR_BIT-1);
     95   }
     96 
     97   /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
     98   /// is indeed a target specific constantpool entry, not a wrapper over a
     99   /// Constant.
    100   bool isMachineConstantPoolEntry() const {
    101     return (int)Alignment < 0;
    102   }
    103 
    104   int getAlignment() const {
    105     return Alignment & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
    106   }
    107 
    108   Type *getType() const;
    109 
    110   /// getRelocationInfo - This method classifies the entry according to
    111   /// whether or not it may generate a relocation entry.  This must be
    112   /// conservative, so if it might codegen to a relocatable entry, it should say
    113   /// so.  The return values are:
    114   ///
    115   ///  0: This constant pool entry is guaranteed to never have a relocation
    116   ///     applied to it (because it holds a simple constant like '4').
    117   ///  1: This entry has relocations, but the entries are guaranteed to be
    118   ///     resolvable by the static linker, so the dynamic linker will never see
    119   ///     them.
    120   ///  2: This entry may have arbitrary relocations.
    121   unsigned getRelocationInfo() const;
    122 };
    123 
    124 /// The MachineConstantPool class keeps track of constants referenced by a
    125 /// function which must be spilled to memory.  This is used for constants which
    126 /// are unable to be used directly as operands to instructions, which typically
    127 /// include floating point and large integer constants.
    128 ///
    129 /// Instructions reference the address of these constant pool constants through
    130 /// the use of MO_ConstantPoolIndex values.  When emitting assembly or machine
    131 /// code, these virtual address references are converted to refer to the
    132 /// address of the function constant pool values.
    133 /// @brief The machine constant pool.
    134 class MachineConstantPool {
    135   const TargetMachine &TM;      ///< The target machine.
    136   unsigned PoolAlignment;       ///< The alignment for the pool.
    137   std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
    138   /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry.
    139   DenseSet<MachineConstantPoolValue*> MachineCPVsSharingEntries;
    140 
    141   const DataLayout *getDataLayout() const;
    142 public:
    143   /// @brief The only constructor.
    144   explicit MachineConstantPool(const TargetMachine &TM)
    145     : TM(TM), PoolAlignment(1) {}
    146   ~MachineConstantPool();
    147 
    148   /// getConstantPoolAlignment - Return the alignment required by
    149   /// the whole constant pool, of which the first element must be aligned.
    150   unsigned getConstantPoolAlignment() const { return PoolAlignment; }
    151 
    152   /// getConstantPoolIndex - Create a new entry in the constant pool or return
    153   /// an existing one.  User must specify the minimum required alignment for
    154   /// the object.
    155   unsigned getConstantPoolIndex(const Constant *C, unsigned Alignment);
    156   unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
    157 
    158   /// isEmpty - Return true if this constant pool contains no constants.
    159   bool isEmpty() const { return Constants.empty(); }
    160 
    161   const std::vector<MachineConstantPoolEntry> &getConstants() const {
    162     return Constants;
    163   }
    164 
    165   /// print - Used by the MachineFunction printer to print information about
    166   /// constant pool objects.  Implemented in MachineFunction.cpp
    167   ///
    168   void print(raw_ostream &OS) const;
    169 
    170   /// dump - Call print(cerr) to be called from the debugger.
    171   void dump() const;
    172 };
    173 
    174 } // End llvm namespace
    175 
    176 #endif
    177