1db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner//===-- CodeGen/MachineConstantPool.h - Abstract Constant Pool --*- C++ -*-===//
2ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman//
36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//                     The LLVM Compiler Infrastructure
46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
7ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman//
86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===//
9ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman//
100f8b53f19d29013ab18f3d444cea1e6305405611Dan Gohman/// @file
110f8b53f19d29013ab18f3d444cea1e6305405611Dan Gohman/// This file declares the MachineConstantPool class which is an abstract
12f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// constant pool to keep track of constants referenced by a function.
13db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner//
14db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner//===----------------------------------------------------------------------===//
15db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner
16db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner#ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
17db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner#define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
18db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner
195567869637383969dced0b84dbd19da12682df6bCameron Zwarich#include "llvm/ADT/DenseSet.h"
20dc2fbddd9d204e904b8e61d1da1428579e7c55afDan Gohman#include <cassert>
21de551f91d8816632a76a065084caab9fab6aacffDan Gohman#include <climits>
22db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner#include <vector>
23d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
24d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
25d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
26db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattnerclass Constant;
2792faff2e4cd627ff980be6bd7c91e2f45eb48154Dan Gohmanclass FoldingSetNodeID;
283574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmowclass DataLayout;
29d6594ae54cfde4db4d30272192645c0a45fb9902Evan Chengclass TargetMachine;
30dc2fbddd9d204e904b8e61d1da1428579e7c55afDan Gohmanclass Type;
31d6594ae54cfde4db4d30272192645c0a45fb9902Evan Chengclass MachineConstantPool;
32944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattnerclass raw_ostream;
33d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng
34d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng/// Abstract base class for all machine specific constantpool value subclasses.
35d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng///
36d6594ae54cfde4db4d30272192645c0a45fb9902Evan Chengclass MachineConstantPoolValue {
372d24e2a396a1d211baaeedf32148a3b657240170David Blaikie  virtual void anchor();
38db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Type *Ty;
39d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng
40d6594ae54cfde4db4d30272192645c0a45fb9902Evan Chengpublic:
41db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {}
4281975f6dfd9d306d0ea7ce3ef22561c949de9af9Dan Gohman  virtual ~MachineConstantPoolValue() {}
43d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng
44d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng  /// getType - get type of this MachineConstantPoolValue.
45d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng  ///
46db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Type *getType() const { return Ty; }
47d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng
48cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner
49354c0165e755fd857bc89792243b82387ee3936dChris Lattner  /// getRelocationInfo - This method classifies the entry according to
50cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner  /// whether or not it may generate a relocation entry.  This must be
51cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner  /// conservative, so if it might codegen to a relocatable entry, it should say
52083a1e059768f6844b9f5292223bb49ad24f52d1Chris Lattner  /// so.  The return values are the same as Constant::getRelocationInfo().
53354c0165e755fd857bc89792243b82387ee3936dChris Lattner  virtual unsigned getRelocationInfo() const = 0;
54cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner
55d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng  virtual int getExistingMachineCPValue(MachineConstantPool *CP,
56d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng                                        unsigned Alignment) = 0;
57d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng
585405d58e21402a8ba3aaaa580ca65155bee00443Jim Grosbach  virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
59d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng
6062ca32540f950d500227f1863b95cd08ad28099eChris Lattner  /// print - Implement operator<<
61944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattner  virtual void print(raw_ostream &O) const = 0;
62d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng};
63d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng
64944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattnerinline raw_ostream &operator<<(raw_ostream &OS,
65944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattner                               const MachineConstantPoolValue &V) {
66944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattner  V.print(OS);
67944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattner  return OS;
68944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattner}
69944fac71e082cc2664cc71b4d3f6c72bab7143fbChris Lattner
70db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner
71f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// This class is a data container for one entry in a MachineConstantPool.
72f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// It contains a pointer to the value and an offset from the start of
73f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// the constant pool.
74f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// @brief An entry in a MachineConstantPool
75d41b30def3181bce4bf87e8bde664d15663165d0Jeff Cohenclass MachineConstantPoolEntry {
76d41b30def3181bce4bf87e8bde664d15663165d0Jeff Cohenpublic:
77d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng  /// The constant itself.
78d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng  union {
7946510a73e977273ec67747eb34cbdb43f815e451Dan Gohman    const Constant *ConstVal;
80d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng    MachineConstantPoolValue *MachineCPVal;
81d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng  } Val;
82d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng
831606e8e4cd937e6de6681f686c266cf61722d972Evan Cheng  /// The required alignment for this entry. The top bit is set when Val is
84acdddfedd163a868c3f4294534fc577285f42f5bEvan Cheng  /// a target specific MachineConstantPoolValue.
851606e8e4cd937e6de6681f686c266cf61722d972Evan Cheng  unsigned Alignment;
86d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng
8746510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  MachineConstantPoolEntry(const Constant *V, unsigned A)
881606e8e4cd937e6de6681f686c266cf61722d972Evan Cheng    : Alignment(A) {
89d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng    Val.ConstVal = V;
90d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng  }
911606e8e4cd937e6de6681f686c266cf61722d972Evan Cheng  MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A)
921606e8e4cd937e6de6681f686c266cf61722d972Evan Cheng    : Alignment(A) {
93d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng    Val.MachineCPVal = V;
9418f13c66bfb9cb2116be0f339fa955949dd60dd1Duncan Sands    Alignment |= 1U << (sizeof(unsigned)*CHAR_BIT-1);
95d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng  }
96d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng
97acdddfedd163a868c3f4294534fc577285f42f5bEvan Cheng  /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
98acdddfedd163a868c3f4294534fc577285f42f5bEvan Cheng  /// is indeed a target specific constantpool entry, not a wrapper over a
99acdddfedd163a868c3f4294534fc577285f42f5bEvan Cheng  /// Constant.
100d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng  bool isMachineConstantPoolEntry() const {
1011606e8e4cd937e6de6681f686c266cf61722d972Evan Cheng    return (int)Alignment < 0;
102d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng  }
10389abd77060d310967811b36c72d019a71c753485Evan Cheng
1041606e8e4cd937e6de6681f686c266cf61722d972Evan Cheng  int getAlignment() const {
105de551f91d8816632a76a065084caab9fab6aacffDan Gohman    return Alignment & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
106f37992428704e378854d0ca8f6aec5a8e22fd133Evan Cheng  }
107f37992428704e378854d0ca8f6aec5a8e22fd133Evan Cheng
108db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Type *getType() const;
109cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner
110354c0165e755fd857bc89792243b82387ee3936dChris Lattner  /// getRelocationInfo - This method classifies the entry according to
111cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner  /// whether or not it may generate a relocation entry.  This must be
112cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner  /// conservative, so if it might codegen to a relocatable entry, it should say
113cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner  /// so.  The return values are:
114cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner  ///
115cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner  ///  0: This constant pool entry is guaranteed to never have a relocation
116cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner  ///     applied to it (because it holds a simple constant like '4').
117cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner  ///  1: This entry has relocations, but the entries are guaranteed to be
118cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner  ///     resolvable by the static linker, so the dynamic linker will never see
119cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner  ///     them.
120cb4596364369ea36ff5675eb12fc62d0c6f1f0ddChris Lattner  ///  2: This entry may have arbitrary relocations.
121354c0165e755fd857bc89792243b82387ee3936dChris Lattner  unsigned getRelocationInfo() const;
12269a9563969156f9ea4345d21673f350ee237d4daChris Lattner};
12369a9563969156f9ea4345d21673f350ee237d4daChris Lattner
124f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// The MachineConstantPool class keeps track of constants referenced by a
125f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// function which must be spilled to memory.  This is used for constants which
126f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// are unable to be used directly as operands to instructions, which typically
127f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// include floating point and large integer constants.
128f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer///
129f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// Instructions reference the address of these constant pool constants through
130f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// the use of MO_ConstantPoolIndex values.  When emitting assembly or machine
131f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// code, these virtual address references are converted to refer to the
132f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// address of the function constant pool values.
133f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer/// @brief The machine constant pool.
134db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattnerclass MachineConstantPool {
135d10fa8b1caf010fe4943ae5526c2c3b921339f72Bill Wendling  const TargetMachine &TM;      ///< The target machine.
136d10fa8b1caf010fe4943ae5526c2c3b921339f72Bill Wendling  unsigned PoolAlignment;       ///< The alignment for the pool.
137f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer  std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
1385567869637383969dced0b84dbd19da12682df6bCameron Zwarich  /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry.
1395567869637383969dced0b84dbd19da12682df6bCameron Zwarich  DenseSet<MachineConstantPoolValue*> MachineCPVsSharingEntries;
140d10fa8b1caf010fe4943ae5526c2c3b921339f72Bill Wendling
141d10fa8b1caf010fe4943ae5526c2c3b921339f72Bill Wendling  const DataLayout *getDataLayout() const;
142db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattnerpublic:
143f1925cb05c0bab13cb79e1075389bb580c7f7d4aReid Spencer  /// @brief The only constructor.
144d10fa8b1caf010fe4943ae5526c2c3b921339f72Bill Wendling  explicit MachineConstantPool(const TargetMachine &TM)
145d10fa8b1caf010fe4943ae5526c2c3b921339f72Bill Wendling    : TM(TM), PoolAlignment(1) {}
146d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng  ~MachineConstantPool();
14704a0f60b230ca2a2c1aa128981b11d5a2c9b6e2cChris Lattner
148f451cb870efcf9e0302d25ed05f4cac6bb494e42Dan Gohman  /// getConstantPoolAlignment - Return the alignment required by
14904a0f60b230ca2a2c1aa128981b11d5a2c9b6e2cChris Lattner  /// the whole constant pool, of which the first element must be aligned.
15004a0f60b230ca2a2c1aa128981b11d5a2c9b6e2cChris Lattner  unsigned getConstantPoolAlignment() const { return PoolAlignment; }
15104a0f60b230ca2a2c1aa128981b11d5a2c9b6e2cChris Lattner
152db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner  /// getConstantPoolIndex - Create a new entry in the constant pool or return
1531606e8e4cd937e6de6681f686c266cf61722d972Evan Cheng  /// an existing one.  User must specify the minimum required alignment for
1541606e8e4cd937e6de6681f686c266cf61722d972Evan Cheng  /// the object.
15546510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  unsigned getConstantPoolIndex(const Constant *C, unsigned Alignment);
156d6594ae54cfde4db4d30272192645c0a45fb9902Evan Cheng  unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
15704a0f60b230ca2a2c1aa128981b11d5a2c9b6e2cChris Lattner
1581ed38fb9a71150343dc9625e1808836b7f86d8f6Chris Lattner  /// isEmpty - Return true if this constant pool contains no constants.
1591ed38fb9a71150343dc9625e1808836b7f86d8f6Chris Lattner  bool isEmpty() const { return Constants.empty(); }
1601ed38fb9a71150343dc9625e1808836b7f86d8f6Chris Lattner
16169a9563969156f9ea4345d21673f350ee237d4daChris Lattner  const std::vector<MachineConstantPoolEntry> &getConstants() const {
162b8973bd8f50d7321635e1e07b81a880a0828d185Evan Cheng    return Constants;
163b8973bd8f50d7321635e1e07b81a880a0828d185Evan Cheng  }
164db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner
165db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner  /// print - Used by the MachineFunction printer to print information about
1666baa2b7220567ee83901ff1830c003468ad4f248Nate Begeman  /// constant pool objects.  Implemented in MachineFunction.cpp
167db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner  ///
16862ca32540f950d500227f1863b95cd08ad28099eChris Lattner  void print(raw_ostream &OS) const;
169db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner
17062ca32540f950d500227f1863b95cd08ad28099eChris Lattner  /// dump - Call print(cerr) to be called from the debugger.
171db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner  void dump() const;
172db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner};
173db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner
174d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
175d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
176db00065fc8ab9fe8b7ac87640c57fd61e922c0f2Chris Lattner#endif
177