blob: 7db87edff010adb26f9a49a9bed9518221f5cc30 [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
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +000018#include "llvm/ADT/DenseSet.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
Chandler Carruthe96dd892014-04-21 22:55:11 +000027#define DEBUG_TYPE "ir"
28
Owen Anderson9b676982009-08-04 22:55:26 +000029namespace llvm {
Owen Anderson9b676982009-08-04 22:55:26 +000030
31/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
32/// behind the scenes to implement unary constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000033class UnaryConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000034 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +000035 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +000036public:
37 // allocate space for exactly one operand
38 void *operator new(size_t s) {
39 return User::operator new(s, 1);
40 }
Chris Lattner229907c2011-07-18 04:54:35 +000041 UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
Owen Anderson9b676982009-08-04 22:55:26 +000042 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
43 Op<0>() = C;
44 }
45 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
46};
47
48/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
49/// behind the scenes to implement binary constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000050class BinaryConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000051 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +000052 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +000053public:
54 // allocate space for exactly two operands
55 void *operator new(size_t s) {
56 return User::operator new(s, 2);
57 }
Dan Gohman1b849082009-09-07 23:54:19 +000058 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
59 unsigned Flags)
Owen Anderson9b676982009-08-04 22:55:26 +000060 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
61 Op<0>() = C1;
62 Op<1>() = C2;
Dan Gohman1b849082009-09-07 23:54:19 +000063 SubclassOptionalData = Flags;
Owen Anderson9b676982009-08-04 22:55:26 +000064 }
65 /// Transparently provide more efficient getOperand methods.
66 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
67};
68
69/// SelectConstantExpr - This class is private to Constants.cpp, and is used
70/// behind the scenes to implement select constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000071class SelectConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000072 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +000073 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +000074public:
75 // allocate space for exactly three operands
76 void *operator new(size_t s) {
77 return User::operator new(s, 3);
78 }
79 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
80 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
81 Op<0>() = C1;
82 Op<1>() = C2;
83 Op<2>() = C3;
84 }
85 /// Transparently provide more efficient getOperand methods.
86 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
87};
88
89/// ExtractElementConstantExpr - This class is private to
90/// Constants.cpp, and is used behind the scenes to implement
91/// extractelement constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000092class ExtractElementConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000093 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +000094 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +000095public:
96 // allocate space for exactly two operands
97 void *operator new(size_t s) {
98 return User::operator new(s, 2);
99 }
100 ExtractElementConstantExpr(Constant *C1, Constant *C2)
Eric Christopher7a4d1092015-05-18 21:49:02 +0000101 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
Owen Anderson9b676982009-08-04 22:55:26 +0000102 Instruction::ExtractElement, &Op<0>(), 2) {
103 Op<0>() = C1;
104 Op<1>() = C2;
105 }
106 /// Transparently provide more efficient getOperand methods.
107 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
108};
109
110/// InsertElementConstantExpr - This class is private to
111/// Constants.cpp, and is used behind the scenes to implement
112/// insertelement constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000113class InsertElementConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000114 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000115 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +0000116public:
117 // allocate space for exactly three operands
118 void *operator new(size_t s) {
119 return User::operator new(s, 3);
120 }
121 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
Eric Christopher7a4d1092015-05-18 21:49:02 +0000122 : ConstantExpr(C1->getType(), Instruction::InsertElement,
Owen Anderson9b676982009-08-04 22:55:26 +0000123 &Op<0>(), 3) {
124 Op<0>() = C1;
125 Op<1>() = C2;
126 Op<2>() = C3;
127 }
128 /// Transparently provide more efficient getOperand methods.
129 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
130};
131
132/// ShuffleVectorConstantExpr - This class is private to
133/// Constants.cpp, and is used behind the scenes to implement
134/// shufflevector constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000135class ShuffleVectorConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000136 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000137 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +0000138public:
139 // allocate space for exactly three operands
140 void *operator new(size_t s) {
141 return User::operator new(s, 3);
142 }
143 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
144 : ConstantExpr(VectorType::get(
145 cast<VectorType>(C1->getType())->getElementType(),
146 cast<VectorType>(C3->getType())->getNumElements()),
Eric Christopher7a4d1092015-05-18 21:49:02 +0000147 Instruction::ShuffleVector,
Owen Anderson9b676982009-08-04 22:55:26 +0000148 &Op<0>(), 3) {
149 Op<0>() = C1;
150 Op<1>() = C2;
151 Op<2>() = C3;
152 }
153 /// Transparently provide more efficient getOperand methods.
154 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
155};
156
157/// ExtractValueConstantExpr - This class is private to
158/// Constants.cpp, and is used behind the scenes to implement
159/// extractvalue constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000160class ExtractValueConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000161 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000162 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +0000163public:
164 // allocate space for exactly one operand
165 void *operator new(size_t s) {
166 return User::operator new(s, 1);
167 }
Duncan P. N. Exon Smith1cd4aeb2014-08-19 00:23:17 +0000168 ExtractValueConstantExpr(Constant *Agg, ArrayRef<unsigned> IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000169 Type *DestTy)
Duncan P. N. Exon Smith1cd4aeb2014-08-19 00:23:17 +0000170 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
171 Indices(IdxList.begin(), IdxList.end()) {
Owen Anderson9b676982009-08-04 22:55:26 +0000172 Op<0>() = Agg;
173 }
174
175 /// Indices - These identify which value to extract.
176 const SmallVector<unsigned, 4> Indices;
177
178 /// Transparently provide more efficient getOperand methods.
179 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Craig Toppercc03b492015-12-15 06:11:36 +0000180
181 static bool classof(const ConstantExpr *CE) {
182 return CE->getOpcode() == Instruction::ExtractValue;
183 }
184 static bool classof(const Value *V) {
185 return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
186 }
Owen Anderson9b676982009-08-04 22:55:26 +0000187};
188
189/// InsertValueConstantExpr - This class is private to
190/// Constants.cpp, and is used behind the scenes to implement
191/// insertvalue constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000192class InsertValueConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000193 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000194 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +0000195public:
196 // allocate space for exactly one operand
197 void *operator new(size_t s) {
198 return User::operator new(s, 2);
199 }
200 InsertValueConstantExpr(Constant *Agg, Constant *Val,
Duncan P. N. Exon Smith1cd4aeb2014-08-19 00:23:17 +0000201 ArrayRef<unsigned> IdxList, Type *DestTy)
202 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
203 Indices(IdxList.begin(), IdxList.end()) {
Owen Anderson9b676982009-08-04 22:55:26 +0000204 Op<0>() = Agg;
205 Op<1>() = Val;
206 }
207
208 /// Indices - These identify the position for the insertion.
209 const SmallVector<unsigned, 4> Indices;
210
211 /// Transparently provide more efficient getOperand methods.
212 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Craig Toppercc03b492015-12-15 06:11:36 +0000213
214 static bool classof(const ConstantExpr *CE) {
215 return CE->getOpcode() == Instruction::InsertValue;
216 }
217 static bool classof(const Value *V) {
218 return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
219 }
Owen Anderson9b676982009-08-04 22:55:26 +0000220};
221
Owen Anderson9b676982009-08-04 22:55:26 +0000222/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
223/// used behind the scenes to implement getelementpr constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000224class GetElementPtrConstantExpr : public ConstantExpr {
David Blaikie60310f22015-05-08 00:42:26 +0000225 Type *SrcElementTy;
Eduard Burtescu19eb0312016-01-19 17:28:00 +0000226 Type *ResElementTy;
Craig Topperf398d7c2014-03-05 06:35:38 +0000227 void anchor() override;
David Blaikie60310f22015-05-08 00:42:26 +0000228 GetElementPtrConstantExpr(Type *SrcElementTy, Constant *C,
229 ArrayRef<Constant *> IdxList, Type *DestTy);
230
Owen Anderson9b676982009-08-04 22:55:26 +0000231public:
David Blaikie60310f22015-05-08 00:42:26 +0000232 static GetElementPtrConstantExpr *Create(Type *SrcElementTy, Constant *C,
233 ArrayRef<Constant *> IdxList,
234 Type *DestTy, unsigned Flags) {
235 GetElementPtrConstantExpr *Result = new (IdxList.size() + 1)
236 GetElementPtrConstantExpr(SrcElementTy, C, IdxList, DestTy);
Dan Gohman1b849082009-09-07 23:54:19 +0000237 Result->SubclassOptionalData = Flags;
238 return Result;
Owen Anderson9b676982009-08-04 22:55:26 +0000239 }
David Blaikie60310f22015-05-08 00:42:26 +0000240 Type *getSourceElementType() const;
Eduard Burtescu19eb0312016-01-19 17:28:00 +0000241 Type *getResultElementType() const;
Owen Anderson9b676982009-08-04 22:55:26 +0000242 /// Transparently provide more efficient getOperand methods.
243 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Craig Toppercc03b492015-12-15 06:11:36 +0000244
245 static bool classof(const ConstantExpr *CE) {
246 return CE->getOpcode() == Instruction::GetElementPtr;
247 }
248 static bool classof(const Value *V) {
249 return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
250 }
Owen Anderson9b676982009-08-04 22:55:26 +0000251};
252
253// CompareConstantExpr - This class is private to Constants.cpp, and is used
254// behind the scenes to implement ICmp and FCmp constant expressions. This is
255// needed in order to store the predicate value for these instructions.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000256class CompareConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000257 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000258 void *operator new(size_t, unsigned) = delete;
David Blaikiea379b1812011-12-20 02:50:00 +0000259public:
Owen Anderson9b676982009-08-04 22:55:26 +0000260 // allocate space for exactly two operands
261 void *operator new(size_t s) {
262 return User::operator new(s, 2);
263 }
264 unsigned short predicate;
Chris Lattner229907c2011-07-18 04:54:35 +0000265 CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
Owen Anderson9b676982009-08-04 22:55:26 +0000266 unsigned short pred, Constant* LHS, Constant* RHS)
267 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
268 Op<0>() = LHS;
269 Op<1>() = RHS;
270 }
271 /// Transparently provide more efficient getOperand methods.
272 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Craig Toppercc03b492015-12-15 06:11:36 +0000273
274 static bool classof(const ConstantExpr *CE) {
275 return CE->getOpcode() == Instruction::ICmp ||
276 CE->getOpcode() == Instruction::FCmp;
277 }
278 static bool classof(const Value *V) {
279 return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
280 }
Owen Anderson9b676982009-08-04 22:55:26 +0000281};
282
283template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000284struct OperandTraits<UnaryConstantExpr>
285 : public FixedNumOperandTraits<UnaryConstantExpr, 1> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000286DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
287
288template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000289struct OperandTraits<BinaryConstantExpr>
290 : public FixedNumOperandTraits<BinaryConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000291DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
292
293template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000294struct OperandTraits<SelectConstantExpr>
295 : public FixedNumOperandTraits<SelectConstantExpr, 3> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000296DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
297
298template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000299struct OperandTraits<ExtractElementConstantExpr>
300 : public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000301DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
302
303template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000304struct OperandTraits<InsertElementConstantExpr>
305 : public FixedNumOperandTraits<InsertElementConstantExpr, 3> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000306DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
307
308template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000309struct OperandTraits<ShuffleVectorConstantExpr>
310 : public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000311DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
312
313template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000314struct OperandTraits<ExtractValueConstantExpr>
315 : public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000316DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
317
318template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000319struct OperandTraits<InsertValueConstantExpr>
320 : public FixedNumOperandTraits<InsertValueConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000321DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
322
323template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000324struct OperandTraits<GetElementPtrConstantExpr>
325 : public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000326
327DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
328
Owen Anderson9b676982009-08-04 22:55:26 +0000329template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000330struct OperandTraits<CompareConstantExpr>
331 : public FixedNumOperandTraits<CompareConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000332DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
333
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000334template <class ConstantClass> struct ConstantAggrKeyType;
335struct InlineAsmKeyType;
336struct ConstantExprKeyType;
337
338template <class ConstantClass> struct ConstantInfo;
339template <> struct ConstantInfo<ConstantExpr> {
340 typedef ConstantExprKeyType ValType;
341 typedef Type TypeClass;
342};
343template <> struct ConstantInfo<InlineAsm> {
344 typedef InlineAsmKeyType ValType;
345 typedef PointerType TypeClass;
346};
347template <> struct ConstantInfo<ConstantArray> {
348 typedef ConstantAggrKeyType<ConstantArray> ValType;
349 typedef ArrayType TypeClass;
350};
351template <> struct ConstantInfo<ConstantStruct> {
352 typedef ConstantAggrKeyType<ConstantStruct> ValType;
353 typedef StructType TypeClass;
354};
355template <> struct ConstantInfo<ConstantVector> {
356 typedef ConstantAggrKeyType<ConstantVector> ValType;
357 typedef VectorType TypeClass;
358};
359
360template <class ConstantClass> struct ConstantAggrKeyType {
361 ArrayRef<Constant *> Operands;
362 ConstantAggrKeyType(ArrayRef<Constant *> Operands) : Operands(Operands) {}
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000363 ConstantAggrKeyType(ArrayRef<Constant *> Operands, const ConstantClass *)
364 : Operands(Operands) {}
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000365 ConstantAggrKeyType(const ConstantClass *C,
366 SmallVectorImpl<Constant *> &Storage) {
367 assert(Storage.empty() && "Expected empty storage");
368 for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
369 Storage.push_back(C->getOperand(I));
370 Operands = Storage;
Duncan P. N. Exon Smith344ebf72014-08-19 01:02:18 +0000371 }
372
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000373 bool operator==(const ConstantAggrKeyType &X) const {
374 return Operands == X.Operands;
375 }
376 bool operator==(const ConstantClass *C) const {
377 if (Operands.size() != C->getNumOperands())
378 return false;
379 for (unsigned I = 0, E = Operands.size(); I != E; ++I)
380 if (Operands[I] != C->getOperand(I))
381 return false;
382 return true;
383 }
384 unsigned getHash() const {
385 return hash_combine_range(Operands.begin(), Operands.end());
386 }
387
388 typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
389 ConstantClass *create(TypeClass *Ty) const {
390 return new (Operands.size()) ConstantClass(Ty, Operands);
Duncan P. N. Exon Smith344ebf72014-08-19 01:02:18 +0000391 }
392};
Owen Anderson9b676982009-08-04 22:55:26 +0000393
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000394struct InlineAsmKeyType {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000395 StringRef AsmString;
396 StringRef Constraints;
David Blaikie71c9c9c2015-07-28 00:06:38 +0000397 FunctionType *FTy;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000398 bool HasSideEffects;
399 bool IsAlignStack;
400 InlineAsm::AsmDialect AsmDialect;
401
402 InlineAsmKeyType(StringRef AsmString, StringRef Constraints,
David Blaikie71c9c9c2015-07-28 00:06:38 +0000403 FunctionType *FTy, bool HasSideEffects, bool IsAlignStack,
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000404 InlineAsm::AsmDialect AsmDialect)
David Blaikie71c9c9c2015-07-28 00:06:38 +0000405 : AsmString(AsmString), Constraints(Constraints), FTy(FTy),
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000406 HasSideEffects(HasSideEffects), IsAlignStack(IsAlignStack),
407 AsmDialect(AsmDialect) {}
408 InlineAsmKeyType(const InlineAsm *Asm, SmallVectorImpl<Constant *> &)
409 : AsmString(Asm->getAsmString()), Constraints(Asm->getConstraintString()),
David Blaikie71c9c9c2015-07-28 00:06:38 +0000410 FTy(Asm->getFunctionType()), HasSideEffects(Asm->hasSideEffects()),
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000411 IsAlignStack(Asm->isAlignStack()), AsmDialect(Asm->getDialect()) {}
412
413 bool operator==(const InlineAsmKeyType &X) const {
414 return HasSideEffects == X.HasSideEffects &&
415 IsAlignStack == X.IsAlignStack && AsmDialect == X.AsmDialect &&
David Blaikie71c9c9c2015-07-28 00:06:38 +0000416 AsmString == X.AsmString && Constraints == X.Constraints &&
417 FTy == X.FTy;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000418 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000419 bool operator==(const InlineAsm *Asm) const {
420 return HasSideEffects == Asm->hasSideEffects() &&
421 IsAlignStack == Asm->isAlignStack() &&
422 AsmDialect == Asm->getDialect() &&
423 AsmString == Asm->getAsmString() &&
David Blaikie71c9c9c2015-07-28 00:06:38 +0000424 Constraints == Asm->getConstraintString() &&
425 FTy == Asm->getFunctionType();
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000426 }
427 unsigned getHash() const {
428 return hash_combine(AsmString, Constraints, HasSideEffects, IsAlignStack,
David Blaikie71c9c9c2015-07-28 00:06:38 +0000429 AsmDialect, FTy);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000430 }
431
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000432 typedef ConstantInfo<InlineAsm>::TypeClass TypeClass;
433 InlineAsm *create(TypeClass *Ty) const {
David Blaikie71c9c9c2015-07-28 00:06:38 +0000434 assert(PointerType::getUnqual(FTy) == Ty);
435 return new InlineAsm(FTy, AsmString, Constraints, HasSideEffects,
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000436 IsAlignStack, AsmDialect);
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000437 }
438};
439
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000440struct ConstantExprKeyType {
441 uint8_t Opcode;
442 uint8_t SubclassOptionalData;
443 uint16_t SubclassData;
444 ArrayRef<Constant *> Ops;
445 ArrayRef<unsigned> Indexes;
David Blaikie60310f22015-05-08 00:42:26 +0000446 Type *ExplicitTy;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000447
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000448 ConstantExprKeyType(unsigned Opcode, ArrayRef<Constant *> Ops,
449 unsigned short SubclassData = 0,
450 unsigned short SubclassOptionalData = 0,
David Blaikie60310f22015-05-08 00:42:26 +0000451 ArrayRef<unsigned> Indexes = None,
452 Type *ExplicitTy = nullptr)
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000453 : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData),
David Blaikie60310f22015-05-08 00:42:26 +0000454 SubclassData(SubclassData), Ops(Ops), Indexes(Indexes),
455 ExplicitTy(ExplicitTy) {}
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000456 ConstantExprKeyType(ArrayRef<Constant *> Operands, const ConstantExpr *CE)
457 : Opcode(CE->getOpcode()),
458 SubclassOptionalData(CE->getRawSubclassOptionalData()),
459 SubclassData(CE->isCompare() ? CE->getPredicate() : 0), Ops(Operands),
460 Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {}
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000461 ConstantExprKeyType(const ConstantExpr *CE,
462 SmallVectorImpl<Constant *> &Storage)
463 : Opcode(CE->getOpcode()),
464 SubclassOptionalData(CE->getRawSubclassOptionalData()),
465 SubclassData(CE->isCompare() ? CE->getPredicate() : 0),
466 Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {
467 assert(Storage.empty() && "Expected empty storage");
468 for (unsigned I = 0, E = CE->getNumOperands(); I != E; ++I)
469 Storage.push_back(CE->getOperand(I));
470 Ops = Storage;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000471 }
472
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000473 bool operator==(const ConstantExprKeyType &X) const {
474 return Opcode == X.Opcode && SubclassData == X.SubclassData &&
475 SubclassOptionalData == X.SubclassOptionalData && Ops == X.Ops &&
476 Indexes == X.Indexes;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000477 }
478
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000479 bool operator==(const ConstantExpr *CE) const {
480 if (Opcode != CE->getOpcode())
481 return false;
482 if (SubclassOptionalData != CE->getRawSubclassOptionalData())
483 return false;
484 if (Ops.size() != CE->getNumOperands())
485 return false;
486 if (SubclassData != (CE->isCompare() ? CE->getPredicate() : 0))
487 return false;
488 for (unsigned I = 0, E = Ops.size(); I != E; ++I)
489 if (Ops[I] != CE->getOperand(I))
490 return false;
491 if (Indexes != (CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()))
492 return false;
493 return true;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000494 }
495
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000496 unsigned getHash() const {
497 return hash_combine(Opcode, SubclassOptionalData, SubclassData,
498 hash_combine_range(Ops.begin(), Ops.end()),
499 hash_combine_range(Indexes.begin(), Indexes.end()));
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000500 }
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000501
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000502 typedef ConstantInfo<ConstantExpr>::TypeClass TypeClass;
503 ConstantExpr *create(TypeClass *Ty) const {
504 switch (Opcode) {
505 default:
506 if (Instruction::isCast(Opcode))
507 return new UnaryConstantExpr(Opcode, Ops[0], Ty);
508 if ((Opcode >= Instruction::BinaryOpsBegin &&
509 Opcode < Instruction::BinaryOpsEnd))
510 return new BinaryConstantExpr(Opcode, Ops[0], Ops[1],
511 SubclassOptionalData);
512 llvm_unreachable("Invalid ConstantExpr!");
513 case Instruction::Select:
514 return new SelectConstantExpr(Ops[0], Ops[1], Ops[2]);
515 case Instruction::ExtractElement:
516 return new ExtractElementConstantExpr(Ops[0], Ops[1]);
517 case Instruction::InsertElement:
518 return new InsertElementConstantExpr(Ops[0], Ops[1], Ops[2]);
519 case Instruction::ShuffleVector:
520 return new ShuffleVectorConstantExpr(Ops[0], Ops[1], Ops[2]);
521 case Instruction::InsertValue:
522 return new InsertValueConstantExpr(Ops[0], Ops[1], Indexes, Ty);
523 case Instruction::ExtractValue:
524 return new ExtractValueConstantExpr(Ops[0], Indexes, Ty);
525 case Instruction::GetElementPtr:
David Blaikie60310f22015-05-08 00:42:26 +0000526 return GetElementPtrConstantExpr::Create(
527 ExplicitTy ? ExplicitTy
528 : cast<PointerType>(Ops[0]->getType()->getScalarType())
529 ->getElementType(),
530 Ops[0], Ops.slice(1), Ty, SubclassOptionalData);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000531 case Instruction::ICmp:
532 return new CompareConstantExpr(Ty, Instruction::ICmp, SubclassData,
533 Ops[0], Ops[1]);
534 case Instruction::FCmp:
535 return new CompareConstantExpr(Ty, Instruction::FCmp, SubclassData,
536 Ops[0], Ops[1]);
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000537 }
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000538 }
539};
540
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000541template <class ConstantClass> class ConstantUniqueMap {
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000542public:
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000543 typedef typename ConstantInfo<ConstantClass>::ValType ValType;
544 typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
545 typedef std::pair<TypeClass *, ValType> LookupKey;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000546
Mehdi Aminib923d642016-03-07 00:51:00 +0000547 /// Key and hash together, so that we compute the hash only once and reuse it.
548 typedef std::pair<unsigned, LookupKey> LookupKeyHashed;
549
Owen Anderson9b676982009-08-04 22:55:26 +0000550private:
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000551 struct MapInfo {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000552 typedef DenseMapInfo<ConstantClass *> ConstantClassInfo;
553 static inline ConstantClass *getEmptyKey() {
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000554 return ConstantClassInfo::getEmptyKey();
555 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000556 static inline ConstantClass *getTombstoneKey() {
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000557 return ConstantClassInfo::getTombstoneKey();
558 }
559 static unsigned getHashValue(const ConstantClass *CP) {
Mehdi Amini7212c5d2016-04-19 00:17:55 +0000560 SmallVector<Constant *, 32> Storage;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000561 return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage)));
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000562 }
563 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
564 return LHS == RHS;
565 }
566 static unsigned getHashValue(const LookupKey &Val) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000567 return hash_combine(Val.first, Val.second.getHash());
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000568 }
Mehdi Aminib923d642016-03-07 00:51:00 +0000569 static unsigned getHashValue(const LookupKeyHashed &Val) {
570 return Val.first;
571 }
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000572 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
573 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
574 return false;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000575 if (LHS.first != RHS->getType())
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000576 return false;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000577 return LHS.second == RHS;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000578 }
Mehdi Aminib923d642016-03-07 00:51:00 +0000579 static bool isEqual(const LookupKeyHashed &LHS, const ConstantClass *RHS) {
580 return isEqual(LHS.second, RHS);
581 }
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000582 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000583
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000584public:
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000585 typedef DenseSet<ConstantClass *, MapInfo> MapTy;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000586
587private:
Owen Anderson9b676982009-08-04 22:55:26 +0000588 MapTy Map;
Owen Anderson9b676982009-08-04 22:55:26 +0000589
Owen Anderson9b676982009-08-04 22:55:26 +0000590public:
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000591 typename MapTy::iterator begin() { return Map.begin(); }
592 typename MapTy::iterator end() { return Map.end(); }
Torok Edwind18e6682009-08-31 16:14:59 +0000593
594 void freeConstants() {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000595 for (auto &I : Map)
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000596 delete I; // Asserts that use_empty().
Owen Anderson9b676982009-08-04 22:55:26 +0000597 }
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000598private:
Mehdi Aminib923d642016-03-07 00:51:00 +0000599 ConstantClass *create(TypeClass *Ty, ValType V, LookupKeyHashed &HashKey) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000600 ConstantClass *Result = V.create(Ty);
Owen Anderson9b676982009-08-04 22:55:26 +0000601
602 assert(Result->getType() == Ty && "Type specified is not correct!");
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000603 Map.insert_as(Result, HashKey);
Owen Anderson9b676982009-08-04 22:55:26 +0000604
Owen Anderson9b676982009-08-04 22:55:26 +0000605 return Result;
606 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000607
Owen Anderson9b676982009-08-04 22:55:26 +0000608public:
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000609 /// Return the specified constant from the map, creating it if necessary.
610 ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
Mehdi Aminib923d642016-03-07 00:51:00 +0000611 LookupKey Key(Ty, V);
612 /// Hash once, and reuse it for the lookup and the insertion if needed.
613 LookupKeyHashed Lookup(MapInfo::getHashValue(Key), Key);
614
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000615 ConstantClass *Result = nullptr;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000616
Mehdi Aminib923d642016-03-07 00:51:00 +0000617 auto I = Map.find_as(Lookup);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000618 if (I == Map.end())
Mehdi Aminib923d642016-03-07 00:51:00 +0000619 Result = create(Ty, V, Lookup);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000620 else
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000621 Result = *I;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000622 assert(Result && "Unexpected nullptr");
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000623
Owen Anderson9b676982009-08-04 22:55:26 +0000624 return Result;
625 }
626
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000627 /// Remove this constant from the map
Owen Anderson9b676982009-08-04 22:55:26 +0000628 void remove(ConstantClass *CP) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000629 typename MapTy::iterator I = Map.find(CP);
Owen Anderson9b676982009-08-04 22:55:26 +0000630 assert(I != Map.end() && "Constant not found in constant table!");
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000631 assert(*I == CP && "Didn't find correct element?");
Owen Anderson9b676982009-08-04 22:55:26 +0000632 Map.erase(I);
633 }
634
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000635 ConstantClass *replaceOperandsInPlace(ArrayRef<Constant *> Operands,
636 ConstantClass *CP, Value *From,
637 Constant *To, unsigned NumUpdated = 0,
638 unsigned OperandNo = ~0u) {
Mehdi Aminib923d642016-03-07 00:51:00 +0000639 LookupKey Key(CP->getType(), ValType(Operands, CP));
640 /// Hash once, and reuse it for the lookup and the insertion if needed.
641 LookupKeyHashed Lookup(MapInfo::getHashValue(Key), Key);
642
643 auto I = Map.find_as(Lookup);
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000644 if (I != Map.end())
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000645 return *I;
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000646
647 // Update to the new value. Optimize for the case when we have a single
648 // operand that we're changing, but handle bulk updates efficiently.
649 remove(CP);
650 if (NumUpdated == 1) {
651 assert(OperandNo < CP->getNumOperands() && "Invalid index");
652 assert(CP->getOperand(OperandNo) != To && "I didn't contain From!");
653 CP->setOperand(OperandNo, To);
654 } else {
655 for (unsigned I = 0, E = CP->getNumOperands(); I != E; ++I)
656 if (CP->getOperand(I) == From)
657 CP->setOperand(I, To);
658 }
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000659 Map.insert_as(CP, Lookup);
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000660 return nullptr;
661 }
662
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000663 void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); }
Owen Anderson9b676982009-08-04 22:55:26 +0000664};
665
Duncan P. N. Exon Smith25fb1102014-08-19 00:21:04 +0000666} // end namespace llvm
Owen Anderson9b676982009-08-04 22:55:26 +0000667
668#endif