1193323Sed//===-- CodeGen/MachineConstantPool.h - Abstract Constant Pool --*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed/// @file
11193323Sed/// This file declares the MachineConstantPool class which is an abstract
12193323Sed/// constant pool to keep track of constants referenced by a function.
13193323Sed//
14193323Sed//===----------------------------------------------------------------------===//
15193323Sed
16193323Sed#ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
17193323Sed#define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
18193323Sed
19219077Sdim#include "llvm/ADT/DenseSet.h"
20193323Sed#include <cassert>
21193323Sed#include <climits>
22193323Sed#include <vector>
23193323Sed
24193323Sednamespace llvm {
25193323Sed
26193323Sedclass Constant;
27193323Sedclass FoldingSetNodeID;
28243830Sdimclass DataLayout;
29193323Sedclass TargetMachine;
30193323Sedclass Type;
31193323Sedclass MachineConstantPool;
32193323Sedclass raw_ostream;
33193323Sed
34193323Sed/// Abstract base class for all machine specific constantpool value subclasses.
35193323Sed///
36193323Sedclass MachineConstantPoolValue {
37234353Sdim  virtual void anchor();
38226633Sdim  Type *Ty;
39193323Sed
40193323Sedpublic:
41226633Sdim  explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {}
42193323Sed  virtual ~MachineConstantPoolValue() {}
43193323Sed
44193323Sed  /// getType - get type of this MachineConstantPoolValue.
45193323Sed  ///
46226633Sdim  Type *getType() const { return Ty; }
47193323Sed
48198090Srdivacky
49198090Srdivacky  /// getRelocationInfo - This method classifies the entry according to
50198090Srdivacky  /// whether or not it may generate a relocation entry.  This must be
51198090Srdivacky  /// conservative, so if it might codegen to a relocatable entry, it should say
52198090Srdivacky  /// so.  The return values are the same as Constant::getRelocationInfo().
53198090Srdivacky  virtual unsigned getRelocationInfo() const = 0;
54198090Srdivacky
55193323Sed  virtual int getExistingMachineCPValue(MachineConstantPool *CP,
56193323Sed                                        unsigned Alignment) = 0;
57193323Sed
58226633Sdim  virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
59193323Sed
60193323Sed  /// print - Implement operator<<
61193323Sed  virtual void print(raw_ostream &O) const = 0;
62193323Sed};
63193323Sed
64193323Sedinline raw_ostream &operator<<(raw_ostream &OS,
65193323Sed                               const MachineConstantPoolValue &V) {
66193323Sed  V.print(OS);
67193323Sed  return OS;
68193323Sed}
69193323Sed
70193323Sed
71193323Sed/// This class is a data container for one entry in a MachineConstantPool.
72193323Sed/// It contains a pointer to the value and an offset from the start of
73193323Sed/// the constant pool.
74193323Sed/// @brief An entry in a MachineConstantPool
75193323Sedclass MachineConstantPoolEntry {
76193323Sedpublic:
77193323Sed  /// The constant itself.
78193323Sed  union {
79207618Srdivacky    const Constant *ConstVal;
80193323Sed    MachineConstantPoolValue *MachineCPVal;
81193323Sed  } Val;
82193323Sed
83193323Sed  /// The required alignment for this entry. The top bit is set when Val is
84221345Sdim  /// a target specific MachineConstantPoolValue.
85193323Sed  unsigned Alignment;
86193323Sed
87207618Srdivacky  MachineConstantPoolEntry(const Constant *V, unsigned A)
88193323Sed    : Alignment(A) {
89193323Sed    Val.ConstVal = V;
90193323Sed  }
91193323Sed  MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A)
92193323Sed    : Alignment(A) {
93193323Sed    Val.MachineCPVal = V;
94198090Srdivacky    Alignment |= 1U << (sizeof(unsigned)*CHAR_BIT-1);
95193323Sed  }
96193323Sed
97221345Sdim  /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
98221345Sdim  /// is indeed a target specific constantpool entry, not a wrapper over a
99221345Sdim  /// Constant.
100193323Sed  bool isMachineConstantPoolEntry() const {
101193323Sed    return (int)Alignment < 0;
102193323Sed  }
103193323Sed
104193323Sed  int getAlignment() const {
105193323Sed    return Alignment & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
106193323Sed  }
107193323Sed
108226633Sdim  Type *getType() const;
109198090Srdivacky
110198090Srdivacky  /// getRelocationInfo - This method classifies the entry according to
111198090Srdivacky  /// whether or not it may generate a relocation entry.  This must be
112198090Srdivacky  /// conservative, so if it might codegen to a relocatable entry, it should say
113198090Srdivacky  /// so.  The return values are:
114198090Srdivacky  ///
115198090Srdivacky  ///  0: This constant pool entry is guaranteed to never have a relocation
116198090Srdivacky  ///     applied to it (because it holds a simple constant like '4').
117198090Srdivacky  ///  1: This entry has relocations, but the entries are guaranteed to be
118198090Srdivacky  ///     resolvable by the static linker, so the dynamic linker will never see
119198090Srdivacky  ///     them.
120198090Srdivacky  ///  2: This entry may have arbitrary relocations.
121198090Srdivacky  unsigned getRelocationInfo() const;
122193323Sed};
123193323Sed
124193323Sed/// The MachineConstantPool class keeps track of constants referenced by a
125193323Sed/// function which must be spilled to memory.  This is used for constants which
126193323Sed/// are unable to be used directly as operands to instructions, which typically
127193323Sed/// include floating point and large integer constants.
128193323Sed///
129193323Sed/// Instructions reference the address of these constant pool constants through
130193323Sed/// the use of MO_ConstantPoolIndex values.  When emitting assembly or machine
131193323Sed/// code, these virtual address references are converted to refer to the
132193323Sed/// address of the function constant pool values.
133193323Sed/// @brief The machine constant pool.
134193323Sedclass MachineConstantPool {
135243830Sdim  const DataLayout *TD;   ///< The machine's DataLayout.
136193323Sed  unsigned PoolAlignment; ///< The alignment for the pool.
137193323Sed  std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
138219077Sdim  /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry.
139219077Sdim  DenseSet<MachineConstantPoolValue*> MachineCPVsSharingEntries;
140193323Sedpublic:
141193323Sed  /// @brief The only constructor.
142243830Sdim  explicit MachineConstantPool(const DataLayout *td)
143193323Sed    : TD(td), PoolAlignment(1) {}
144193323Sed  ~MachineConstantPool();
145193323Sed
146203954Srdivacky  /// getConstantPoolAlignment - Return the alignment required by
147193323Sed  /// the whole constant pool, of which the first element must be aligned.
148193323Sed  unsigned getConstantPoolAlignment() const { return PoolAlignment; }
149193323Sed
150193323Sed  /// getConstantPoolIndex - Create a new entry in the constant pool or return
151193323Sed  /// an existing one.  User must specify the minimum required alignment for
152193323Sed  /// the object.
153207618Srdivacky  unsigned getConstantPoolIndex(const Constant *C, unsigned Alignment);
154193323Sed  unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
155193323Sed
156193323Sed  /// isEmpty - Return true if this constant pool contains no constants.
157193323Sed  bool isEmpty() const { return Constants.empty(); }
158193323Sed
159193323Sed  const std::vector<MachineConstantPoolEntry> &getConstants() const {
160193323Sed    return Constants;
161193323Sed  }
162193323Sed
163193323Sed  /// print - Used by the MachineFunction printer to print information about
164193323Sed  /// constant pool objects.  Implemented in MachineFunction.cpp
165193323Sed  ///
166193323Sed  void print(raw_ostream &OS) const;
167193323Sed
168193323Sed  /// dump - Call print(cerr) to be called from the debugger.
169193323Sed  void dump() const;
170193323Sed};
171193323Sed
172193323Sed} // End llvm namespace
173193323Sed
174193323Sed#endif
175