blob: 13fcbd2ece10a08bdecf7a18f8e1a9221a302085 [file] [log] [blame]
Owen Anderson3fb4aab2009-08-23 04:24:24 +00001//===-- ConstantsContext.h - Constants-related Context Interals -----------===//
Owen Anderson9b676982009-08-04 22:55:26 +00002//
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// This file defines various helper methods and classes used by
11// LLVMContextImpl for creating and managing constants.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_LIB_IR_CONSTANTSCONTEXT_H
16#define LLVM_LIB_IR_CONSTANTSCONTEXT_H
Owen Anderson9b676982009-08-04 22:55:26 +000017
Talin46e9b442012-02-05 20:54:10 +000018#include "llvm/ADT/DenseMap.h"
Jay Foadcc5fd3e2012-03-06 10:43:52 +000019#include "llvm/ADT/Hashing.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/InlineAsm.h"
21#include "llvm/IR/Instructions.h"
22#include "llvm/IR/Operator.h"
David Greene338a9032010-01-05 01:34:26 +000023#include "llvm/Support/Debug.h"
Owen Anderson9b676982009-08-04 22:55:26 +000024#include "llvm/Support/ErrorHandling.h"
Chris Lattner34822f62009-08-23 04:44:11 +000025#include "llvm/Support/raw_ostream.h"
Owen Anderson9b676982009-08-04 22:55:26 +000026#include <map>
Benjamin Kramer74996572014-04-29 23:37:02 +000027#include <tuple>
Owen Anderson9b676982009-08-04 22:55:26 +000028
Chandler Carruthe96dd892014-04-21 22:55:11 +000029#define DEBUG_TYPE "ir"
30
Owen Anderson9b676982009-08-04 22:55:26 +000031namespace llvm {
Owen Anderson9b676982009-08-04 22:55:26 +000032
33/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
34/// behind the scenes to implement unary constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000035class UnaryConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000036 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +000037 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +000038public:
39 // allocate space for exactly one operand
40 void *operator new(size_t s) {
41 return User::operator new(s, 1);
42 }
Chris Lattner229907c2011-07-18 04:54:35 +000043 UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
Owen Anderson9b676982009-08-04 22:55:26 +000044 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
45 Op<0>() = C;
46 }
47 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
48};
49
50/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
51/// behind the scenes to implement binary constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000052class BinaryConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000053 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +000054 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +000055public:
56 // allocate space for exactly two operands
57 void *operator new(size_t s) {
58 return User::operator new(s, 2);
59 }
Dan Gohman1b849082009-09-07 23:54:19 +000060 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
61 unsigned Flags)
Owen Anderson9b676982009-08-04 22:55:26 +000062 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
63 Op<0>() = C1;
64 Op<1>() = C2;
Dan Gohman1b849082009-09-07 23:54:19 +000065 SubclassOptionalData = Flags;
Owen Anderson9b676982009-08-04 22:55:26 +000066 }
67 /// Transparently provide more efficient getOperand methods.
68 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
69};
70
71/// SelectConstantExpr - This class is private to Constants.cpp, and is used
72/// behind the scenes to implement select constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000073class SelectConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000074 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +000075 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +000076public:
77 // allocate space for exactly three operands
78 void *operator new(size_t s) {
79 return User::operator new(s, 3);
80 }
81 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
82 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
83 Op<0>() = C1;
84 Op<1>() = C2;
85 Op<2>() = C3;
86 }
87 /// Transparently provide more efficient getOperand methods.
88 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
89};
90
91/// ExtractElementConstantExpr - This class is private to
92/// Constants.cpp, and is used behind the scenes to implement
93/// extractelement constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000094class ExtractElementConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000095 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +000096 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +000097public:
98 // allocate space for exactly two operands
99 void *operator new(size_t s) {
100 return User::operator new(s, 2);
101 }
102 ExtractElementConstantExpr(Constant *C1, Constant *C2)
Eric Christopher7a4d1092015-05-18 21:49:02 +0000103 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Owen Anderson9b676982009-08-04 22:55:26 +0000104 Instruction::ExtractElement, &Op<0>(), 2) {
105 Op<0>() = C1;
106 Op<1>() = C2;
107 }
108 /// Transparently provide more efficient getOperand methods.
109 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
110};
111
112/// InsertElementConstantExpr - This class is private to
113/// Constants.cpp, and is used behind the scenes to implement
114/// insertelement constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000115class InsertElementConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000116 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000117 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +0000118public:
119 // allocate space for exactly three operands
120 void *operator new(size_t s) {
121 return User::operator new(s, 3);
122 }
123 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Eric Christopher7a4d1092015-05-18 21:49:02 +0000124 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Owen Anderson9b676982009-08-04 22:55:26 +0000125 &Op<0>(), 3) {
126 Op<0>() = C1;
127 Op<1>() = C2;
128 Op<2>() = C3;
129 }
130 /// Transparently provide more efficient getOperand methods.
131 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
132};
133
134/// ShuffleVectorConstantExpr - This class is private to
135/// Constants.cpp, and is used behind the scenes to implement
136/// shufflevector constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000137class ShuffleVectorConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000138 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000139 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +0000140public:
141 // allocate space for exactly three operands
142 void *operator new(size_t s) {
143 return User::operator new(s, 3);
144 }
145 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
146 : ConstantExpr(VectorType::get(
147 cast<VectorType>(C1->getType())->getElementType(),
148 cast<VectorType>(C3->getType())->getNumElements()),
Eric Christopher7a4d1092015-05-18 21:49:02 +0000149 Instruction::ShuffleVector,
Owen Anderson9b676982009-08-04 22:55:26 +0000150 &Op<0>(), 3) {
151 Op<0>() = C1;
152 Op<1>() = C2;
153 Op<2>() = C3;
154 }
155 /// Transparently provide more efficient getOperand methods.
156 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
157};
158
159/// ExtractValueConstantExpr - This class is private to
160/// Constants.cpp, and is used behind the scenes to implement
161/// extractvalue constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000162class ExtractValueConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000163 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000164 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +0000165public:
166 // allocate space for exactly one operand
167 void *operator new(size_t s) {
168 return User::operator new(s, 1);
169 }
Duncan P. N. Exon Smith1cd4aeb2014-08-19 00:23:17 +0000170 ExtractValueConstantExpr(Constant *Agg, ArrayRef<unsigned> IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000171 Type *DestTy)
Duncan P. N. Exon Smith1cd4aeb2014-08-19 00:23:17 +0000172 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
173 Indices(IdxList.begin(), IdxList.end()) {
Owen Anderson9b676982009-08-04 22:55:26 +0000174 Op<0>() = Agg;
175 }
176
177 /// Indices - These identify which value to extract.
178 const SmallVector<unsigned, 4> Indices;
179
180 /// Transparently provide more efficient getOperand methods.
181 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Craig Toppercc03b492015-12-15 06:11:36 +0000182
183 static bool classof(const ConstantExpr *CE) {
184 return CE->getOpcode() == Instruction::ExtractValue;
185 }
186 static bool classof(const Value *V) {
187 return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
188 }
Owen Anderson9b676982009-08-04 22:55:26 +0000189};
190
191/// InsertValueConstantExpr - This class is private to
192/// Constants.cpp, and is used behind the scenes to implement
193/// insertvalue constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000194class InsertValueConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000195 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000196 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +0000197public:
198 // allocate space for exactly one operand
199 void *operator new(size_t s) {
200 return User::operator new(s, 2);
201 }
202 InsertValueConstantExpr(Constant *Agg, Constant *Val,
Duncan P. N. Exon Smith1cd4aeb2014-08-19 00:23:17 +0000203 ArrayRef<unsigned> IdxList, Type *DestTy)
204 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
205 Indices(IdxList.begin(), IdxList.end()) {
Owen Anderson9b676982009-08-04 22:55:26 +0000206 Op<0>() = Agg;
207 Op<1>() = Val;
208 }
209
210 /// Indices - These identify the position for the insertion.
211 const SmallVector<unsigned, 4> Indices;
212
213 /// Transparently provide more efficient getOperand methods.
214 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Craig Toppercc03b492015-12-15 06:11:36 +0000215
216 static bool classof(const ConstantExpr *CE) {
217 return CE->getOpcode() == Instruction::InsertValue;
218 }
219 static bool classof(const Value *V) {
220 return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
221 }
Owen Anderson9b676982009-08-04 22:55:26 +0000222};
223
Owen Anderson9b676982009-08-04 22:55:26 +0000224/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
225/// used behind the scenes to implement getelementpr constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000226class GetElementPtrConstantExpr : public ConstantExpr {
David Blaikie60310f22015-05-08 00:42:26 +0000227 Type *SrcElementTy;
Craig Topperf398d7c2014-03-05 06:35:38 +0000228 void anchor() override;
David Blaikie60310f22015-05-08 00:42:26 +0000229 GetElementPtrConstantExpr(Type *SrcElementTy, Constant *C,
230 ArrayRef<Constant *> IdxList, Type *DestTy);
231
Owen Anderson9b676982009-08-04 22:55:26 +0000232public:
233 static GetElementPtrConstantExpr *Create(Constant *C,
Chris Lattnera474bb22012-01-26 20:40:56 +0000234 ArrayRef<Constant*> IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000235 Type *DestTy,
Dan Gohman1b849082009-09-07 23:54:19 +0000236 unsigned Flags) {
David Blaikie60310f22015-05-08 00:42:26 +0000237 return Create(
238 cast<PointerType>(C->getType()->getScalarType())->getElementType(), C,
239 IdxList, DestTy, Flags);
240 }
241 static GetElementPtrConstantExpr *Create(Type *SrcElementTy, Constant *C,
242 ArrayRef<Constant *> IdxList,
243 Type *DestTy, unsigned Flags) {
244 GetElementPtrConstantExpr *Result = new (IdxList.size() + 1)
245 GetElementPtrConstantExpr(SrcElementTy, C, IdxList, DestTy);
Dan Gohman1b849082009-09-07 23:54:19 +0000246 Result->SubclassOptionalData = Flags;
247 return Result;
Owen Anderson9b676982009-08-04 22:55:26 +0000248 }
David Blaikie60310f22015-05-08 00:42:26 +0000249 Type *getSourceElementType() const;
Owen Anderson9b676982009-08-04 22:55:26 +0000250 /// Transparently provide more efficient getOperand methods.
251 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Craig Toppercc03b492015-12-15 06:11:36 +0000252
253 static bool classof(const ConstantExpr *CE) {
254 return CE->getOpcode() == Instruction::GetElementPtr;
255 }
256 static bool classof(const Value *V) {
257 return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
258 }
Owen Anderson9b676982009-08-04 22:55:26 +0000259};
260
261// CompareConstantExpr - This class is private to Constants.cpp, and is used
262// behind the scenes to implement ICmp and FCmp constant expressions. This is
263// needed in order to store the predicate value for these instructions.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000264class CompareConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000265 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000266 void *operator new(size_t, unsigned) = delete;
David Blaikiea379b1812011-12-20 02:50:00 +0000267public:
Owen Anderson9b676982009-08-04 22:55:26 +0000268 // allocate space for exactly two operands
269 void *operator new(size_t s) {
270 return User::operator new(s, 2);
271 }
272 unsigned short predicate;
Chris Lattner229907c2011-07-18 04:54:35 +0000273 CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
Owen Anderson9b676982009-08-04 22:55:26 +0000274 unsigned short pred, Constant* LHS, Constant* RHS)
275 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
276 Op<0>() = LHS;
277 Op<1>() = RHS;
278 }
279 /// Transparently provide more efficient getOperand methods.
280 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Craig Toppercc03b492015-12-15 06:11:36 +0000281
282 static bool classof(const ConstantExpr *CE) {
283 return CE->getOpcode() == Instruction::ICmp ||
284 CE->getOpcode() == Instruction::FCmp;
285 }
286 static bool classof(const Value *V) {
287 return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
288 }
Owen Anderson9b676982009-08-04 22:55:26 +0000289};
290
291template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000292struct OperandTraits<UnaryConstantExpr>
293 : public FixedNumOperandTraits<UnaryConstantExpr, 1> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000294DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
295
296template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000297struct OperandTraits<BinaryConstantExpr>
298 : public FixedNumOperandTraits<BinaryConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000299DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
300
301template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000302struct OperandTraits<SelectConstantExpr>
303 : public FixedNumOperandTraits<SelectConstantExpr, 3> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000304DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
305
306template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000307struct OperandTraits<ExtractElementConstantExpr>
308 : public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000309DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
310
311template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000312struct OperandTraits<InsertElementConstantExpr>
313 : public FixedNumOperandTraits<InsertElementConstantExpr, 3> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000314DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
315
316template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000317struct OperandTraits<ShuffleVectorConstantExpr>
318 : public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000319DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
320
321template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000322struct OperandTraits<ExtractValueConstantExpr>
323 : public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000324DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
325
326template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000327struct OperandTraits<InsertValueConstantExpr>
328 : public FixedNumOperandTraits<InsertValueConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000329DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
330
331template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000332struct OperandTraits<GetElementPtrConstantExpr>
333 : public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000334
335DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
336
Owen Anderson9b676982009-08-04 22:55:26 +0000337template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000338struct OperandTraits<CompareConstantExpr>
339 : public FixedNumOperandTraits<CompareConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000340DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
341
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000342template <class ConstantClass> struct ConstantAggrKeyType;
343struct InlineAsmKeyType;
344struct ConstantExprKeyType;
345
346template <class ConstantClass> struct ConstantInfo;
347template <> struct ConstantInfo<ConstantExpr> {
348 typedef ConstantExprKeyType ValType;
349 typedef Type TypeClass;
350};
351template <> struct ConstantInfo<InlineAsm> {
352 typedef InlineAsmKeyType ValType;
353 typedef PointerType TypeClass;
354};
355template <> struct ConstantInfo<ConstantArray> {
356 typedef ConstantAggrKeyType<ConstantArray> ValType;
357 typedef ArrayType TypeClass;
358};
359template <> struct ConstantInfo<ConstantStruct> {
360 typedef ConstantAggrKeyType<ConstantStruct> ValType;
361 typedef StructType TypeClass;
362};
363template <> struct ConstantInfo<ConstantVector> {
364 typedef ConstantAggrKeyType<ConstantVector> ValType;
365 typedef VectorType TypeClass;
366};
367
368template <class ConstantClass> struct ConstantAggrKeyType {
369 ArrayRef<Constant *> Operands;
370 ConstantAggrKeyType(ArrayRef<Constant *> Operands) : Operands(Operands) {}
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000371 ConstantAggrKeyType(ArrayRef<Constant *> Operands, const ConstantClass *)
372 : Operands(Operands) {}
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000373 ConstantAggrKeyType(const ConstantClass *C,
374 SmallVectorImpl<Constant *> &Storage) {
375 assert(Storage.empty() && "Expected empty storage");
376 for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
377 Storage.push_back(C->getOperand(I));
378 Operands = Storage;
Duncan P. N. Exon Smith344ebf72014-08-19 01:02:18 +0000379 }
380
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000381 bool operator==(const ConstantAggrKeyType &X) const {
382 return Operands == X.Operands;
383 }
384 bool operator==(const ConstantClass *C) const {
385 if (Operands.size() != C->getNumOperands())
386 return false;
387 for (unsigned I = 0, E = Operands.size(); I != E; ++I)
388 if (Operands[I] != C->getOperand(I))
389 return false;
390 return true;
391 }
392 unsigned getHash() const {
393 return hash_combine_range(Operands.begin(), Operands.end());
394 }
395
396 typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
397 ConstantClass *create(TypeClass *Ty) const {
398 return new (Operands.size()) ConstantClass(Ty, Operands);
Duncan P. N. Exon Smith344ebf72014-08-19 01:02:18 +0000399 }
400};
Owen Anderson9b676982009-08-04 22:55:26 +0000401
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000402struct InlineAsmKeyType {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000403 StringRef AsmString;
404 StringRef Constraints;
David Blaikie71c9c9c2015-07-28 00:06:38 +0000405 FunctionType *FTy;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000406 bool HasSideEffects;
407 bool IsAlignStack;
408 InlineAsm::AsmDialect AsmDialect;
409
410 InlineAsmKeyType(StringRef AsmString, StringRef Constraints,
David Blaikie71c9c9c2015-07-28 00:06:38 +0000411 FunctionType *FTy, bool HasSideEffects, bool IsAlignStack,
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000412 InlineAsm::AsmDialect AsmDialect)
David Blaikie71c9c9c2015-07-28 00:06:38 +0000413 : AsmString(AsmString), Constraints(Constraints), FTy(FTy),
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000414 HasSideEffects(HasSideEffects), IsAlignStack(IsAlignStack),
415 AsmDialect(AsmDialect) {}
416 InlineAsmKeyType(const InlineAsm *Asm, SmallVectorImpl<Constant *> &)
417 : AsmString(Asm->getAsmString()), Constraints(Asm->getConstraintString()),
David Blaikie71c9c9c2015-07-28 00:06:38 +0000418 FTy(Asm->getFunctionType()), HasSideEffects(Asm->hasSideEffects()),
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000419 IsAlignStack(Asm->isAlignStack()), AsmDialect(Asm->getDialect()) {}
420
421 bool operator==(const InlineAsmKeyType &X) const {
422 return HasSideEffects == X.HasSideEffects &&
423 IsAlignStack == X.IsAlignStack && AsmDialect == X.AsmDialect &&
David Blaikie71c9c9c2015-07-28 00:06:38 +0000424 AsmString == X.AsmString && Constraints == X.Constraints &&
425 FTy == X.FTy;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000426 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000427 bool operator==(const InlineAsm *Asm) const {
428 return HasSideEffects == Asm->hasSideEffects() &&
429 IsAlignStack == Asm->isAlignStack() &&
430 AsmDialect == Asm->getDialect() &&
431 AsmString == Asm->getAsmString() &&
David Blaikie71c9c9c2015-07-28 00:06:38 +0000432 Constraints == Asm->getConstraintString() &&
433 FTy == Asm->getFunctionType();
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000434 }
435 unsigned getHash() const {
436 return hash_combine(AsmString, Constraints, HasSideEffects, IsAlignStack,
David Blaikie71c9c9c2015-07-28 00:06:38 +0000437 AsmDialect, FTy);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000438 }
439
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000440 typedef ConstantInfo<InlineAsm>::TypeClass TypeClass;
441 InlineAsm *create(TypeClass *Ty) const {
David Blaikie71c9c9c2015-07-28 00:06:38 +0000442 assert(PointerType::getUnqual(FTy) == Ty);
443 return new InlineAsm(FTy, AsmString, Constraints, HasSideEffects,
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000444 IsAlignStack, AsmDialect);
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000445 }
446};
447
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000448struct ConstantExprKeyType {
449 uint8_t Opcode;
450 uint8_t SubclassOptionalData;
451 uint16_t SubclassData;
452 ArrayRef<Constant *> Ops;
453 ArrayRef<unsigned> Indexes;
David Blaikie60310f22015-05-08 00:42:26 +0000454 Type *ExplicitTy;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000455
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000456 ConstantExprKeyType(unsigned Opcode, ArrayRef<Constant *> Ops,
457 unsigned short SubclassData = 0,
458 unsigned short SubclassOptionalData = 0,
David Blaikie60310f22015-05-08 00:42:26 +0000459 ArrayRef<unsigned> Indexes = None,
460 Type *ExplicitTy = nullptr)
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000461 : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData),
David Blaikie60310f22015-05-08 00:42:26 +0000462 SubclassData(SubclassData), Ops(Ops), Indexes(Indexes),
463 ExplicitTy(ExplicitTy) {}
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000464 ConstantExprKeyType(ArrayRef<Constant *> Operands, const ConstantExpr *CE)
465 : Opcode(CE->getOpcode()),
466 SubclassOptionalData(CE->getRawSubclassOptionalData()),
467 SubclassData(CE->isCompare() ? CE->getPredicate() : 0), Ops(Operands),
468 Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {}
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000469 ConstantExprKeyType(const ConstantExpr *CE,
470 SmallVectorImpl<Constant *> &Storage)
471 : Opcode(CE->getOpcode()),
472 SubclassOptionalData(CE->getRawSubclassOptionalData()),
473 SubclassData(CE->isCompare() ? CE->getPredicate() : 0),
474 Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {
475 assert(Storage.empty() && "Expected empty storage");
476 for (unsigned I = 0, E = CE->getNumOperands(); I != E; ++I)
477 Storage.push_back(CE->getOperand(I));
478 Ops = Storage;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000479 }
480
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000481 bool operator==(const ConstantExprKeyType &X) const {
482 return Opcode == X.Opcode && SubclassData == X.SubclassData &&
483 SubclassOptionalData == X.SubclassOptionalData && Ops == X.Ops &&
484 Indexes == X.Indexes;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000485 }
486
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000487 bool operator==(const ConstantExpr *CE) const {
488 if (Opcode != CE->getOpcode())
489 return false;
490 if (SubclassOptionalData != CE->getRawSubclassOptionalData())
491 return false;
492 if (Ops.size() != CE->getNumOperands())
493 return false;
494 if (SubclassData != (CE->isCompare() ? CE->getPredicate() : 0))
495 return false;
496 for (unsigned I = 0, E = Ops.size(); I != E; ++I)
497 if (Ops[I] != CE->getOperand(I))
498 return false;
499 if (Indexes != (CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()))
500 return false;
501 return true;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000502 }
503
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000504 unsigned getHash() const {
505 return hash_combine(Opcode, SubclassOptionalData, SubclassData,
506 hash_combine_range(Ops.begin(), Ops.end()),
507 hash_combine_range(Indexes.begin(), Indexes.end()));
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000508 }
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000509
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000510 typedef ConstantInfo<ConstantExpr>::TypeClass TypeClass;
511 ConstantExpr *create(TypeClass *Ty) const {
512 switch (Opcode) {
513 default:
514 if (Instruction::isCast(Opcode))
515 return new UnaryConstantExpr(Opcode, Ops[0], Ty);
516 if ((Opcode >= Instruction::BinaryOpsBegin &&
517 Opcode < Instruction::BinaryOpsEnd))
518 return new BinaryConstantExpr(Opcode, Ops[0], Ops[1],
519 SubclassOptionalData);
520 llvm_unreachable("Invalid ConstantExpr!");
521 case Instruction::Select:
522 return new SelectConstantExpr(Ops[0], Ops[1], Ops[2]);
523 case Instruction::ExtractElement:
524 return new ExtractElementConstantExpr(Ops[0], Ops[1]);
525 case Instruction::InsertElement:
526 return new InsertElementConstantExpr(Ops[0], Ops[1], Ops[2]);
527 case Instruction::ShuffleVector:
528 return new ShuffleVectorConstantExpr(Ops[0], Ops[1], Ops[2]);
529 case Instruction::InsertValue:
530 return new InsertValueConstantExpr(Ops[0], Ops[1], Indexes, Ty);
531 case Instruction::ExtractValue:
532 return new ExtractValueConstantExpr(Ops[0], Indexes, Ty);
533 case Instruction::GetElementPtr:
David Blaikie60310f22015-05-08 00:42:26 +0000534 return GetElementPtrConstantExpr::Create(
535 ExplicitTy ? ExplicitTy
536 : cast<PointerType>(Ops[0]->getType()->getScalarType())
537 ->getElementType(),
538 Ops[0], Ops.slice(1), Ty, SubclassOptionalData);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000539 case Instruction::ICmp:
540 return new CompareConstantExpr(Ty, Instruction::ICmp, SubclassData,
541 Ops[0], Ops[1]);
542 case Instruction::FCmp:
543 return new CompareConstantExpr(Ty, Instruction::FCmp, SubclassData,
544 Ops[0], Ops[1]);
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000545 }
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000546 }
547};
548
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000549template <class ConstantClass> class ConstantUniqueMap {
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000550public:
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000551 typedef typename ConstantInfo<ConstantClass>::ValType ValType;
552 typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
553 typedef std::pair<TypeClass *, ValType> LookupKey;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000554
Owen Anderson9b676982009-08-04 22:55:26 +0000555private:
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000556 struct MapInfo {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000557 typedef DenseMapInfo<ConstantClass *> ConstantClassInfo;
558 static inline ConstantClass *getEmptyKey() {
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000559 return ConstantClassInfo::getEmptyKey();
560 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000561 static inline ConstantClass *getTombstoneKey() {
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000562 return ConstantClassInfo::getTombstoneKey();
563 }
564 static unsigned getHashValue(const ConstantClass *CP) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000565 SmallVector<Constant *, 8> Storage;
566 return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage)));
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000567 }
568 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
569 return LHS == RHS;
570 }
571 static unsigned getHashValue(const LookupKey &Val) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000572 return hash_combine(Val.first, Val.second.getHash());
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000573 }
574 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
575 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
576 return false;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000577 if (LHS.first != RHS->getType())
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000578 return false;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000579 return LHS.second == RHS;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000580 }
581 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000582
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000583public:
584 typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
585
586private:
Owen Anderson9b676982009-08-04 22:55:26 +0000587 MapTy Map;
Owen Anderson9b676982009-08-04 22:55:26 +0000588
Owen Anderson9b676982009-08-04 22:55:26 +0000589public:
Devang Patelc5aa8c62009-08-11 06:31:57 +0000590 typename MapTy::iterator map_begin() { return Map.begin(); }
Owen Anderson9b676982009-08-04 22:55:26 +0000591 typename MapTy::iterator map_end() { return Map.end(); }
Torok Edwind18e6682009-08-31 16:14:59 +0000592
593 void freeConstants() {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000594 for (auto &I : Map)
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000595 // Asserts that use_empty().
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000596 delete I.first;
Owen Anderson9b676982009-08-04 22:55:26 +0000597 }
Dan Gohmane4532f32009-09-15 15:58:07 +0000598
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000599private:
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000600 ConstantClass *create(TypeClass *Ty, ValType V) {
601 ConstantClass *Result = V.create(Ty);
Owen Anderson9b676982009-08-04 22:55:26 +0000602
603 assert(Result->getType() == Ty && "Type specified is not correct!");
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000604 insert(Result);
Owen Anderson9b676982009-08-04 22:55:26 +0000605
Owen Anderson9b676982009-08-04 22:55:26 +0000606 return Result;
607 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000608
Owen Anderson9b676982009-08-04 22:55:26 +0000609public:
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000610 /// Return the specified constant from the map, creating it if necessary.
611 ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000612 LookupKey Lookup(Ty, V);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000613 ConstantClass *Result = nullptr;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000614
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000615 auto I = find(Lookup);
616 if (I == Map.end())
617 Result = create(Ty, V);
618 else
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000619 Result = I->first;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000620 assert(Result && "Unexpected nullptr");
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000621
Owen Anderson9b676982009-08-04 22:55:26 +0000622 return Result;
623 }
624
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000625 /// Find the constant by lookup key.
626 typename MapTy::iterator find(LookupKey Lookup) {
627 return Map.find_as(Lookup);
628 }
629
630 /// Insert the constant into its proper slot.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000631 void insert(ConstantClass *CP) { Map[CP] = '\0'; }
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000632
633 /// Remove this constant from the map
Owen Anderson9b676982009-08-04 22:55:26 +0000634 void remove(ConstantClass *CP) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000635 typename MapTy::iterator I = Map.find(CP);
Owen Anderson9b676982009-08-04 22:55:26 +0000636 assert(I != Map.end() && "Constant not found in constant table!");
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000637 assert(I->first == CP && "Didn't find correct element?");
Owen Anderson9b676982009-08-04 22:55:26 +0000638 Map.erase(I);
639 }
640
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000641 ConstantClass *replaceOperandsInPlace(ArrayRef<Constant *> Operands,
642 ConstantClass *CP, Value *From,
643 Constant *To, unsigned NumUpdated = 0,
644 unsigned OperandNo = ~0u) {
645 LookupKey Lookup(CP->getType(), ValType(Operands, CP));
646 auto I = find(Lookup);
647 if (I != Map.end())
648 return I->first;
649
650 // Update to the new value. Optimize for the case when we have a single
651 // operand that we're changing, but handle bulk updates efficiently.
652 remove(CP);
653 if (NumUpdated == 1) {
654 assert(OperandNo < CP->getNumOperands() && "Invalid index");
655 assert(CP->getOperand(OperandNo) != To && "I didn't contain From!");
656 CP->setOperand(OperandNo, To);
657 } else {
658 for (unsigned I = 0, E = CP->getNumOperands(); I != E; ++I)
659 if (CP->getOperand(I) == From)
660 CP->setOperand(I, To);
661 }
662 insert(CP);
663 return nullptr;
664 }
665
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000666 void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); }
Owen Anderson9b676982009-08-04 22:55:26 +0000667};
668
Duncan P. N. Exon Smith25fb1102014-08-19 00:21:04 +0000669} // end namespace llvm
Owen Anderson9b676982009-08-04 22:55:26 +0000670
671#endif