LLVM  9.0.0svn
MachineConstantPool.h
Go to the documentation of this file.
1 //===- CodeGen/MachineConstantPool.h - Abstract Constant Pool ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// @file
10 /// This file declares the MachineConstantPool class which is an abstract
11 /// constant pool to keep track of constants referenced by a function.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
16 #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
17 
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/MC/SectionKind.h"
20 #include <climits>
21 #include <vector>
22 
23 namespace llvm {
24 
25 class Constant;
26 class DataLayout;
27 class FoldingSetNodeID;
28 class MachineConstantPool;
29 class raw_ostream;
30 class Type;
31 
32 /// Abstract base class for all machine specific constantpool value subclasses.
33 ///
35  virtual void anchor();
36 
37  Type *Ty;
38 
39 public:
40  explicit MachineConstantPoolValue(Type *ty) : Ty(ty) {}
41  virtual ~MachineConstantPoolValue() = default;
42 
43  /// getType - get type of this MachineConstantPoolValue.
44  ///
45  Type *getType() const { return Ty; }
46 
48  unsigned Alignment) = 0;
49 
50  virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
51 
52  /// print - Implement operator<<
53  virtual void print(raw_ostream &O) const = 0;
54 };
55 
57  const MachineConstantPoolValue &V) {
58  V.print(OS);
59  return OS;
60 }
61 
62 /// This class is a data container for one entry in a MachineConstantPool.
63 /// It contains a pointer to the value and an offset from the start of
64 /// the constant pool.
65 /// An entry in a MachineConstantPool
67 public:
68  /// The constant itself.
69  union {
72  } Val;
73 
74  /// The required alignment for this entry. The top bit is set when Val is
75  /// a target specific MachineConstantPoolValue.
76  unsigned Alignment;
77 
78  MachineConstantPoolEntry(const Constant *V, unsigned A)
79  : Alignment(A) {
80  Val.ConstVal = V;
81  }
82 
84  : Alignment(A) {
85  Val.MachineCPVal = V;
86  Alignment |= 1U << (sizeof(unsigned) * CHAR_BIT - 1);
87  }
88 
89  /// isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry
90  /// is indeed a target specific constantpool entry, not a wrapper over a
91  /// Constant.
93  return (int)Alignment < 0;
94  }
95 
96  int getAlignment() const {
97  return Alignment & ~(1 << (sizeof(unsigned) * CHAR_BIT - 1));
98  }
99 
100  Type *getType() const;
101 
102  /// This method classifies the entry according to whether or not it may
103  /// generate a relocation entry. This must be conservative, so if it might
104  /// codegen to a relocatable entry, it should say so.
105  bool needsRelocation() const;
106 
107  SectionKind getSectionKind(const DataLayout *DL) const;
108 };
109 
110 /// The MachineConstantPool class keeps track of constants referenced by a
111 /// function which must be spilled to memory. This is used for constants which
112 /// are unable to be used directly as operands to instructions, which typically
113 /// include floating point and large integer constants.
114 ///
115 /// Instructions reference the address of these constant pool constants through
116 /// the use of MO_ConstantPoolIndex values. When emitting assembly or machine
117 /// code, these virtual address references are converted to refer to the
118 /// address of the function constant pool values.
119 /// The machine constant pool.
121  unsigned PoolAlignment; ///< The alignment for the pool.
122  std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
123  /// MachineConstantPoolValues that use an existing MachineConstantPoolEntry.
124  DenseSet<MachineConstantPoolValue*> MachineCPVsSharingEntries;
125  const DataLayout &DL;
126 
127  const DataLayout &getDataLayout() const { return DL; }
128 
129 public:
130  /// The only constructor.
131  explicit MachineConstantPool(const DataLayout &DL)
132  : PoolAlignment(1), DL(DL) {}
134 
135  /// getConstantPoolAlignment - Return the alignment required by
136  /// the whole constant pool, of which the first element must be aligned.
137  unsigned getConstantPoolAlignment() const { return PoolAlignment; }
138 
139  /// getConstantPoolIndex - Create a new entry in the constant pool or return
140  /// an existing one. User must specify the minimum required alignment for
141  /// the object.
142  unsigned getConstantPoolIndex(const Constant *C, unsigned Alignment);
143  unsigned getConstantPoolIndex(MachineConstantPoolValue *V,
144  unsigned Alignment);
145 
146  /// isEmpty - Return true if this constant pool contains no constants.
147  bool isEmpty() const { return Constants.empty(); }
148 
149  const std::vector<MachineConstantPoolEntry> &getConstants() const {
150  return Constants;
151  }
152 
153  /// print - Used by the MachineFunction printer to print information about
154  /// constant pool objects. Implemented in MachineFunction.cpp
155  void print(raw_ostream &OS) const;
156 
157  /// dump - Call print(cerr) to be called from the debugger.
158  void dump() const;
159 };
160 
161 } // end namespace llvm
162 
163 #endif // LLVM_CODEGEN_MACHINECONSTANTPOOL_H
MachineConstantPoolValue * MachineCPVal
uint64_t CallInst * C
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
The MachineConstantPool class keeps track of constants referenced by a function which must be spilled...
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:48
bool isEmpty() const
isEmpty - Return true if this constant pool contains no constants.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Implements a dense probed hash-table based set.
Definition: DenseSet.h:249
virtual void print(raw_ostream &O) const =0
print - Implement operator<<
virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID)=0
MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A)
MachineConstantPoolEntry(const Constant *V, unsigned A)
bool isMachineConstantPoolEntry() const
isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry is indeed a target specific ...
virtual int getExistingMachineCPValue(MachineConstantPool *CP, unsigned Alignment)=0
This class is a data container for one entry in a MachineConstantPool.
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:305
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
unsigned getConstantPoolAlignment() const
getConstantPoolAlignment - Return the alignment required by the whole constant pool, of which the first element must be aligned.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important base class in LLVM.
Definition: Constant.h:41
virtual ~MachineConstantPoolValue()=default
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
MachineConstantPool(const DataLayout &DL)
The only constructor.
Abstract base class for all machine specific constantpool value subclasses.
const std::vector< MachineConstantPoolEntry > & getConstants() const
unsigned Alignment
The required alignment for this entry.
Type * getType() const
getType - get type of this MachineConstantPoolValue.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2038
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45