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