blob: f3ddcd78d2658c0e33c8a0f0e846e027b331f6e2 [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);
182};
183
184/// InsertValueConstantExpr - This class is private to
185/// Constants.cpp, and is used behind the scenes to implement
186/// insertvalue constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000187class InsertValueConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000188 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000189 void *operator new(size_t, unsigned) = delete;
Owen Anderson9b676982009-08-04 22:55:26 +0000190public:
191 // allocate space for exactly one operand
192 void *operator new(size_t s) {
193 return User::operator new(s, 2);
194 }
195 InsertValueConstantExpr(Constant *Agg, Constant *Val,
Duncan P. N. Exon Smith1cd4aeb2014-08-19 00:23:17 +0000196 ArrayRef<unsigned> IdxList, Type *DestTy)
197 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
198 Indices(IdxList.begin(), IdxList.end()) {
Owen Anderson9b676982009-08-04 22:55:26 +0000199 Op<0>() = Agg;
200 Op<1>() = Val;
201 }
202
203 /// Indices - These identify the position for the insertion.
204 const SmallVector<unsigned, 4> Indices;
205
206 /// Transparently provide more efficient getOperand methods.
207 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
208};
209
Owen Anderson9b676982009-08-04 22:55:26 +0000210/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
211/// used behind the scenes to implement getelementpr constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000212class GetElementPtrConstantExpr : public ConstantExpr {
David Blaikie60310f22015-05-08 00:42:26 +0000213 Type *SrcElementTy;
Craig Topperf398d7c2014-03-05 06:35:38 +0000214 void anchor() override;
David Blaikie60310f22015-05-08 00:42:26 +0000215 GetElementPtrConstantExpr(Type *SrcElementTy, Constant *C,
216 ArrayRef<Constant *> IdxList, Type *DestTy);
217
Owen Anderson9b676982009-08-04 22:55:26 +0000218public:
219 static GetElementPtrConstantExpr *Create(Constant *C,
Chris Lattnera474bb22012-01-26 20:40:56 +0000220 ArrayRef<Constant*> IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000221 Type *DestTy,
Dan Gohman1b849082009-09-07 23:54:19 +0000222 unsigned Flags) {
David Blaikie60310f22015-05-08 00:42:26 +0000223 return Create(
224 cast<PointerType>(C->getType()->getScalarType())->getElementType(), C,
225 IdxList, DestTy, Flags);
226 }
227 static GetElementPtrConstantExpr *Create(Type *SrcElementTy, Constant *C,
228 ArrayRef<Constant *> IdxList,
229 Type *DestTy, unsigned Flags) {
230 GetElementPtrConstantExpr *Result = new (IdxList.size() + 1)
231 GetElementPtrConstantExpr(SrcElementTy, C, IdxList, DestTy);
Dan Gohman1b849082009-09-07 23:54:19 +0000232 Result->SubclassOptionalData = Flags;
233 return Result;
Owen Anderson9b676982009-08-04 22:55:26 +0000234 }
David Blaikie60310f22015-05-08 00:42:26 +0000235 Type *getSourceElementType() const;
Owen Anderson9b676982009-08-04 22:55:26 +0000236 /// Transparently provide more efficient getOperand methods.
237 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
238};
239
240// CompareConstantExpr - This class is private to Constants.cpp, and is used
241// behind the scenes to implement ICmp and FCmp constant expressions. This is
242// needed in order to store the predicate value for these instructions.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000243class CompareConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000244 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000245 void *operator new(size_t, unsigned) = delete;
David Blaikiea379b1812011-12-20 02:50:00 +0000246public:
Owen Anderson9b676982009-08-04 22:55:26 +0000247 // allocate space for exactly two operands
248 void *operator new(size_t s) {
249 return User::operator new(s, 2);
250 }
251 unsigned short predicate;
Chris Lattner229907c2011-07-18 04:54:35 +0000252 CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
Owen Anderson9b676982009-08-04 22:55:26 +0000253 unsigned short pred, Constant* LHS, Constant* RHS)
254 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
255 Op<0>() = LHS;
256 Op<1>() = RHS;
257 }
258 /// Transparently provide more efficient getOperand methods.
259 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
260};
261
262template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000263struct OperandTraits<UnaryConstantExpr>
264 : public FixedNumOperandTraits<UnaryConstantExpr, 1> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000265DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
266
267template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000268struct OperandTraits<BinaryConstantExpr>
269 : public FixedNumOperandTraits<BinaryConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000270DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
271
272template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000273struct OperandTraits<SelectConstantExpr>
274 : public FixedNumOperandTraits<SelectConstantExpr, 3> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000275DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
276
277template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000278struct OperandTraits<ExtractElementConstantExpr>
279 : public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000280DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
281
282template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000283struct OperandTraits<InsertElementConstantExpr>
284 : public FixedNumOperandTraits<InsertElementConstantExpr, 3> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000285DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
286
287template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000288struct OperandTraits<ShuffleVectorConstantExpr>
289 : public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000290DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
291
292template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000293struct OperandTraits<ExtractValueConstantExpr>
294 : public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000295DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
296
297template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000298struct OperandTraits<InsertValueConstantExpr>
299 : public FixedNumOperandTraits<InsertValueConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000300DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
301
302template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000303struct OperandTraits<GetElementPtrConstantExpr>
304 : public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000305
306DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
307
Owen Anderson9b676982009-08-04 22:55:26 +0000308template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000309struct OperandTraits<CompareConstantExpr>
310 : public FixedNumOperandTraits<CompareConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000311DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
312
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000313template <class ConstantClass> struct ConstantAggrKeyType;
314struct InlineAsmKeyType;
315struct ConstantExprKeyType;
316
317template <class ConstantClass> struct ConstantInfo;
318template <> struct ConstantInfo<ConstantExpr> {
319 typedef ConstantExprKeyType ValType;
320 typedef Type TypeClass;
321};
322template <> struct ConstantInfo<InlineAsm> {
323 typedef InlineAsmKeyType ValType;
324 typedef PointerType TypeClass;
325};
326template <> struct ConstantInfo<ConstantArray> {
327 typedef ConstantAggrKeyType<ConstantArray> ValType;
328 typedef ArrayType TypeClass;
329};
330template <> struct ConstantInfo<ConstantStruct> {
331 typedef ConstantAggrKeyType<ConstantStruct> ValType;
332 typedef StructType TypeClass;
333};
334template <> struct ConstantInfo<ConstantVector> {
335 typedef ConstantAggrKeyType<ConstantVector> ValType;
336 typedef VectorType TypeClass;
337};
338
339template <class ConstantClass> struct ConstantAggrKeyType {
340 ArrayRef<Constant *> Operands;
341 ConstantAggrKeyType(ArrayRef<Constant *> Operands) : Operands(Operands) {}
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000342 ConstantAggrKeyType(ArrayRef<Constant *> Operands, const ConstantClass *)
343 : Operands(Operands) {}
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000344 ConstantAggrKeyType(const ConstantClass *C,
345 SmallVectorImpl<Constant *> &Storage) {
346 assert(Storage.empty() && "Expected empty storage");
347 for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
348 Storage.push_back(C->getOperand(I));
349 Operands = Storage;
Duncan P. N. Exon Smith344ebf72014-08-19 01:02:18 +0000350 }
351
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000352 bool operator==(const ConstantAggrKeyType &X) const {
353 return Operands == X.Operands;
354 }
355 bool operator==(const ConstantClass *C) const {
356 if (Operands.size() != C->getNumOperands())
357 return false;
358 for (unsigned I = 0, E = Operands.size(); I != E; ++I)
359 if (Operands[I] != C->getOperand(I))
360 return false;
361 return true;
362 }
363 unsigned getHash() const {
364 return hash_combine_range(Operands.begin(), Operands.end());
365 }
366
367 typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
368 ConstantClass *create(TypeClass *Ty) const {
369 return new (Operands.size()) ConstantClass(Ty, Operands);
Duncan P. N. Exon Smith344ebf72014-08-19 01:02:18 +0000370 }
371};
Owen Anderson9b676982009-08-04 22:55:26 +0000372
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000373struct InlineAsmKeyType {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000374 StringRef AsmString;
375 StringRef Constraints;
376 bool HasSideEffects;
377 bool IsAlignStack;
378 InlineAsm::AsmDialect AsmDialect;
379
380 InlineAsmKeyType(StringRef AsmString, StringRef Constraints,
381 bool HasSideEffects, bool IsAlignStack,
382 InlineAsm::AsmDialect AsmDialect)
383 : AsmString(AsmString), Constraints(Constraints),
384 HasSideEffects(HasSideEffects), IsAlignStack(IsAlignStack),
385 AsmDialect(AsmDialect) {}
386 InlineAsmKeyType(const InlineAsm *Asm, SmallVectorImpl<Constant *> &)
387 : AsmString(Asm->getAsmString()), Constraints(Asm->getConstraintString()),
388 HasSideEffects(Asm->hasSideEffects()),
389 IsAlignStack(Asm->isAlignStack()), AsmDialect(Asm->getDialect()) {}
390
391 bool operator==(const InlineAsmKeyType &X) const {
392 return HasSideEffects == X.HasSideEffects &&
393 IsAlignStack == X.IsAlignStack && AsmDialect == X.AsmDialect &&
394 AsmString == X.AsmString && Constraints == X.Constraints;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000395 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000396 bool operator==(const InlineAsm *Asm) const {
397 return HasSideEffects == Asm->hasSideEffects() &&
398 IsAlignStack == Asm->isAlignStack() &&
399 AsmDialect == Asm->getDialect() &&
400 AsmString == Asm->getAsmString() &&
401 Constraints == Asm->getConstraintString();
402 }
403 unsigned getHash() const {
404 return hash_combine(AsmString, Constraints, HasSideEffects, IsAlignStack,
405 AsmDialect);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000406 }
407
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000408 typedef ConstantInfo<InlineAsm>::TypeClass TypeClass;
409 InlineAsm *create(TypeClass *Ty) const {
410 return new InlineAsm(Ty, AsmString, Constraints, HasSideEffects,
411 IsAlignStack, AsmDialect);
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000412 }
413};
414
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000415struct ConstantExprKeyType {
416 uint8_t Opcode;
417 uint8_t SubclassOptionalData;
418 uint16_t SubclassData;
419 ArrayRef<Constant *> Ops;
420 ArrayRef<unsigned> Indexes;
David Blaikie60310f22015-05-08 00:42:26 +0000421 Type *ExplicitTy;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000422
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000423 ConstantExprKeyType(unsigned Opcode, ArrayRef<Constant *> Ops,
424 unsigned short SubclassData = 0,
425 unsigned short SubclassOptionalData = 0,
David Blaikie60310f22015-05-08 00:42:26 +0000426 ArrayRef<unsigned> Indexes = None,
427 Type *ExplicitTy = nullptr)
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000428 : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData),
David Blaikie60310f22015-05-08 00:42:26 +0000429 SubclassData(SubclassData), Ops(Ops), Indexes(Indexes),
430 ExplicitTy(ExplicitTy) {}
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000431 ConstantExprKeyType(ArrayRef<Constant *> Operands, const ConstantExpr *CE)
432 : Opcode(CE->getOpcode()),
433 SubclassOptionalData(CE->getRawSubclassOptionalData()),
434 SubclassData(CE->isCompare() ? CE->getPredicate() : 0), Ops(Operands),
435 Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {}
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000436 ConstantExprKeyType(const ConstantExpr *CE,
437 SmallVectorImpl<Constant *> &Storage)
438 : Opcode(CE->getOpcode()),
439 SubclassOptionalData(CE->getRawSubclassOptionalData()),
440 SubclassData(CE->isCompare() ? CE->getPredicate() : 0),
441 Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {
442 assert(Storage.empty() && "Expected empty storage");
443 for (unsigned I = 0, E = CE->getNumOperands(); I != E; ++I)
444 Storage.push_back(CE->getOperand(I));
445 Ops = Storage;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000446 }
447
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000448 bool operator==(const ConstantExprKeyType &X) const {
449 return Opcode == X.Opcode && SubclassData == X.SubclassData &&
450 SubclassOptionalData == X.SubclassOptionalData && Ops == X.Ops &&
451 Indexes == X.Indexes;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000452 }
453
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000454 bool operator==(const ConstantExpr *CE) const {
455 if (Opcode != CE->getOpcode())
456 return false;
457 if (SubclassOptionalData != CE->getRawSubclassOptionalData())
458 return false;
459 if (Ops.size() != CE->getNumOperands())
460 return false;
461 if (SubclassData != (CE->isCompare() ? CE->getPredicate() : 0))
462 return false;
463 for (unsigned I = 0, E = Ops.size(); I != E; ++I)
464 if (Ops[I] != CE->getOperand(I))
465 return false;
466 if (Indexes != (CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()))
467 return false;
468 return true;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000469 }
470
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000471 unsigned getHash() const {
472 return hash_combine(Opcode, SubclassOptionalData, SubclassData,
473 hash_combine_range(Ops.begin(), Ops.end()),
474 hash_combine_range(Indexes.begin(), Indexes.end()));
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000475 }
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000476
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000477 typedef ConstantInfo<ConstantExpr>::TypeClass TypeClass;
478 ConstantExpr *create(TypeClass *Ty) const {
479 switch (Opcode) {
480 default:
481 if (Instruction::isCast(Opcode))
482 return new UnaryConstantExpr(Opcode, Ops[0], Ty);
483 if ((Opcode >= Instruction::BinaryOpsBegin &&
484 Opcode < Instruction::BinaryOpsEnd))
485 return new BinaryConstantExpr(Opcode, Ops[0], Ops[1],
486 SubclassOptionalData);
487 llvm_unreachable("Invalid ConstantExpr!");
488 case Instruction::Select:
489 return new SelectConstantExpr(Ops[0], Ops[1], Ops[2]);
490 case Instruction::ExtractElement:
491 return new ExtractElementConstantExpr(Ops[0], Ops[1]);
492 case Instruction::InsertElement:
493 return new InsertElementConstantExpr(Ops[0], Ops[1], Ops[2]);
494 case Instruction::ShuffleVector:
495 return new ShuffleVectorConstantExpr(Ops[0], Ops[1], Ops[2]);
496 case Instruction::InsertValue:
497 return new InsertValueConstantExpr(Ops[0], Ops[1], Indexes, Ty);
498 case Instruction::ExtractValue:
499 return new ExtractValueConstantExpr(Ops[0], Indexes, Ty);
500 case Instruction::GetElementPtr:
David Blaikie60310f22015-05-08 00:42:26 +0000501 return GetElementPtrConstantExpr::Create(
502 ExplicitTy ? ExplicitTy
503 : cast<PointerType>(Ops[0]->getType()->getScalarType())
504 ->getElementType(),
505 Ops[0], Ops.slice(1), Ty, SubclassOptionalData);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000506 case Instruction::ICmp:
507 return new CompareConstantExpr(Ty, Instruction::ICmp, SubclassData,
508 Ops[0], Ops[1]);
509 case Instruction::FCmp:
510 return new CompareConstantExpr(Ty, Instruction::FCmp, SubclassData,
511 Ops[0], Ops[1]);
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000512 }
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000513 }
514};
515
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000516template <class ConstantClass> class ConstantUniqueMap {
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000517public:
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000518 typedef typename ConstantInfo<ConstantClass>::ValType ValType;
519 typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
520 typedef std::pair<TypeClass *, ValType> LookupKey;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000521
Owen Anderson9b676982009-08-04 22:55:26 +0000522private:
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000523 struct MapInfo {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000524 typedef DenseMapInfo<ConstantClass *> ConstantClassInfo;
525 static inline ConstantClass *getEmptyKey() {
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000526 return ConstantClassInfo::getEmptyKey();
527 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000528 static inline ConstantClass *getTombstoneKey() {
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000529 return ConstantClassInfo::getTombstoneKey();
530 }
531 static unsigned getHashValue(const ConstantClass *CP) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000532 SmallVector<Constant *, 8> Storage;
533 return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage)));
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000534 }
535 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
536 return LHS == RHS;
537 }
538 static unsigned getHashValue(const LookupKey &Val) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000539 return hash_combine(Val.first, Val.second.getHash());
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000540 }
541 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
542 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
543 return false;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000544 if (LHS.first != RHS->getType())
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000545 return false;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000546 return LHS.second == RHS;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000547 }
548 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000549
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000550public:
551 typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
552
553private:
Owen Anderson9b676982009-08-04 22:55:26 +0000554 MapTy Map;
Owen Anderson9b676982009-08-04 22:55:26 +0000555
Owen Anderson9b676982009-08-04 22:55:26 +0000556public:
Devang Patelc5aa8c62009-08-11 06:31:57 +0000557 typename MapTy::iterator map_begin() { return Map.begin(); }
Owen Anderson9b676982009-08-04 22:55:26 +0000558 typename MapTy::iterator map_end() { return Map.end(); }
Torok Edwind18e6682009-08-31 16:14:59 +0000559
560 void freeConstants() {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000561 for (auto &I : Map)
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000562 // Asserts that use_empty().
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000563 delete I.first;
Owen Anderson9b676982009-08-04 22:55:26 +0000564 }
Dan Gohmane4532f32009-09-15 15:58:07 +0000565
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000566private:
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000567 ConstantClass *create(TypeClass *Ty, ValType V) {
568 ConstantClass *Result = V.create(Ty);
Owen Anderson9b676982009-08-04 22:55:26 +0000569
570 assert(Result->getType() == Ty && "Type specified is not correct!");
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000571 insert(Result);
Owen Anderson9b676982009-08-04 22:55:26 +0000572
Owen Anderson9b676982009-08-04 22:55:26 +0000573 return Result;
574 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000575
Owen Anderson9b676982009-08-04 22:55:26 +0000576public:
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000577 /// Return the specified constant from the map, creating it if necessary.
578 ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000579 LookupKey Lookup(Ty, V);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000580 ConstantClass *Result = nullptr;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000581
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000582 auto I = find(Lookup);
583 if (I == Map.end())
584 Result = create(Ty, V);
585 else
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000586 Result = I->first;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000587 assert(Result && "Unexpected nullptr");
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000588
Owen Anderson9b676982009-08-04 22:55:26 +0000589 return Result;
590 }
591
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000592 /// Find the constant by lookup key.
593 typename MapTy::iterator find(LookupKey Lookup) {
594 return Map.find_as(Lookup);
595 }
596
597 /// Insert the constant into its proper slot.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000598 void insert(ConstantClass *CP) { Map[CP] = '\0'; }
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000599
600 /// Remove this constant from the map
Owen Anderson9b676982009-08-04 22:55:26 +0000601 void remove(ConstantClass *CP) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000602 typename MapTy::iterator I = Map.find(CP);
Owen Anderson9b676982009-08-04 22:55:26 +0000603 assert(I != Map.end() && "Constant not found in constant table!");
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000604 assert(I->first == CP && "Didn't find correct element?");
Owen Anderson9b676982009-08-04 22:55:26 +0000605 Map.erase(I);
606 }
607
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000608 ConstantClass *replaceOperandsInPlace(ArrayRef<Constant *> Operands,
609 ConstantClass *CP, Value *From,
610 Constant *To, unsigned NumUpdated = 0,
611 unsigned OperandNo = ~0u) {
612 LookupKey Lookup(CP->getType(), ValType(Operands, CP));
613 auto I = find(Lookup);
614 if (I != Map.end())
615 return I->first;
616
617 // Update to the new value. Optimize for the case when we have a single
618 // operand that we're changing, but handle bulk updates efficiently.
619 remove(CP);
620 if (NumUpdated == 1) {
621 assert(OperandNo < CP->getNumOperands() && "Invalid index");
622 assert(CP->getOperand(OperandNo) != To && "I didn't contain From!");
623 CP->setOperand(OperandNo, To);
624 } else {
625 for (unsigned I = 0, E = CP->getNumOperands(); I != E; ++I)
626 if (CP->getOperand(I) == From)
627 CP->setOperand(I, To);
628 }
629 insert(CP);
630 return nullptr;
631 }
632
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000633 void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); }
Owen Anderson9b676982009-08-04 22:55:26 +0000634};
635
Duncan P. N. Exon Smith25fb1102014-08-19 00:21:04 +0000636} // end namespace llvm
Owen Anderson9b676982009-08-04 22:55:26 +0000637
638#endif