MachineConstantPool.h revision 1606e8e4cd937e6de6681f686c266cf61722d972
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 <cassert>
20#include <vector>
21
22namespace llvm {
23
24class Constant;
25class FoldingSetNodeID;
26class TargetData;
27class TargetMachine;
28class Type;
29class MachineConstantPool;
30class raw_ostream;
31
32/// Abstract base class for all machine specific constantpool value subclasses.
33///
34class MachineConstantPoolValue {
35  const Type *Ty;
36
37public:
38  explicit MachineConstantPoolValue(const Type *ty) : Ty(ty) {}
39  virtual ~MachineConstantPoolValue() {}
40
41  /// getType - get type of this MachineConstantPoolValue.
42  ///
43  inline const Type *getType() const { return Ty; }
44
45  virtual int getExistingMachineCPValue(MachineConstantPool *CP,
46                                        unsigned Alignment) = 0;
47
48  virtual void AddSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
49
50  /// print - Implement operator<<
51  virtual void print(raw_ostream &O) const = 0;
52};
53
54inline raw_ostream &operator<<(raw_ostream &OS,
55                               const MachineConstantPoolValue &V) {
56  V.print(OS);
57  return OS;
58}
59
60
61/// This class is a data container for one entry in a MachineConstantPool.
62/// It contains a pointer to the value and an offset from the start of
63/// the constant pool.
64/// @brief An entry in a MachineConstantPool
65class MachineConstantPoolEntry {
66public:
67  /// The constant itself.
68  union {
69    Constant *ConstVal;
70    MachineConstantPoolValue *MachineCPVal;
71  } Val;
72
73  /// The required alignment for this entry. The top bit is set when Val is
74  /// a MachineConstantPoolValue.
75  unsigned Alignment;
76
77  MachineConstantPoolEntry(Constant *V, unsigned A)
78    : Alignment(A) {
79    Val.ConstVal = V;
80  }
81  MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A)
82    : Alignment(A) {
83    Val.MachineCPVal = V;
84    Alignment |= 1 << (sizeof(unsigned)*8-1);
85  }
86
87  bool isMachineConstantPoolEntry() const {
88    return (int)Alignment < 0;
89  }
90
91  int getAlignment() const {
92    return Alignment & ~(1 << (sizeof(unsigned)*8-1));
93  }
94
95  const Type *getType() const;
96};
97
98/// The MachineConstantPool class keeps track of constants referenced by a
99/// function which must be spilled to memory.  This is used for constants which
100/// are unable to be used directly as operands to instructions, which typically
101/// include floating point and large integer constants.
102///
103/// Instructions reference the address of these constant pool constants through
104/// the use of MO_ConstantPoolIndex values.  When emitting assembly or machine
105/// code, these virtual address references are converted to refer to the
106/// address of the function constant pool values.
107/// @brief The machine constant pool.
108class MachineConstantPool {
109  const TargetData *TD;   ///< The machine's TargetData.
110  unsigned PoolAlignment; ///< The alignment for the pool.
111  std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
112public:
113  /// @brief The only constructor.
114  explicit MachineConstantPool(const TargetData *td)
115    : TD(td), PoolAlignment(1) {}
116  ~MachineConstantPool();
117
118  /// getConstantPoolAlignment - Return the the alignment required by
119  /// the whole constant pool, of which the first element must be aligned.
120  unsigned getConstantPoolAlignment() const { return PoolAlignment; }
121
122  /// getConstantPoolIndex - Create a new entry in the constant pool or return
123  /// an existing one.  User must specify the minimum required alignment for
124  /// the object.
125  unsigned getConstantPoolIndex(Constant *C, unsigned Alignment);
126  unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
127
128  /// isEmpty - Return true if this constant pool contains no constants.
129  bool isEmpty() const { return Constants.empty(); }
130
131  const std::vector<MachineConstantPoolEntry> &getConstants() const {
132    return Constants;
133  }
134
135  /// print - Used by the MachineFunction printer to print information about
136  /// constant pool objects.  Implemented in MachineFunction.cpp
137  ///
138  void print(raw_ostream &OS) const;
139
140  /// dump - Call print(cerr) to be called from the debugger.
141  void dump() const;
142};
143
144} // End llvm namespace
145
146#endif
147