blob: a03279554c5cecc7102133fa554c58ab63ead7fd [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;
Eduard Burtescu19eb0312016-01-19 17:28:00 +0000228 Type *ResElementTy;
Craig Topperf398d7c2014-03-05 06:35:38 +0000229 void anchor() override;
David Blaikie60310f22015-05-08 00:42:26 +0000230 GetElementPtrConstantExpr(Type *SrcElementTy, Constant *C,
231 ArrayRef<Constant *> IdxList, Type *DestTy);
232
Owen Anderson9b676982009-08-04 22:55:26 +0000233public:
David Blaikie60310f22015-05-08 00:42:26 +0000234 static GetElementPtrConstantExpr *Create(Type *SrcElementTy, Constant *C,
235 ArrayRef<Constant *> IdxList,
236 Type *DestTy, unsigned Flags) {
237 GetElementPtrConstantExpr *Result = new (IdxList.size() + 1)
238 GetElementPtrConstantExpr(SrcElementTy, C, IdxList, DestTy);
Dan Gohman1b849082009-09-07 23:54:19 +0000239 Result->SubclassOptionalData = Flags;
240 return Result;
Owen Anderson9b676982009-08-04 22:55:26 +0000241 }
David Blaikie60310f22015-05-08 00:42:26 +0000242 Type *getSourceElementType() const;
Eduard Burtescu19eb0312016-01-19 17:28:00 +0000243 Type *getResultElementType() const;
Owen Anderson9b676982009-08-04 22:55:26 +0000244 /// Transparently provide more efficient getOperand methods.
245 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Craig Toppercc03b492015-12-15 06:11:36 +0000246
247 static bool classof(const ConstantExpr *CE) {
248 return CE->getOpcode() == Instruction::GetElementPtr;
249 }
250 static bool classof(const Value *V) {
251 return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
252 }
Owen Anderson9b676982009-08-04 22:55:26 +0000253};
254
255// CompareConstantExpr - This class is private to Constants.cpp, and is used
256// behind the scenes to implement ICmp and FCmp constant expressions. This is
257// needed in order to store the predicate value for these instructions.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000258class CompareConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000259 void anchor() override;
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000260 void *operator new(size_t, unsigned) = delete;
David Blaikiea379b1812011-12-20 02:50:00 +0000261public:
Owen Anderson9b676982009-08-04 22:55:26 +0000262 // allocate space for exactly two operands
263 void *operator new(size_t s) {
264 return User::operator new(s, 2);
265 }
266 unsigned short predicate;
Chris Lattner229907c2011-07-18 04:54:35 +0000267 CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
Owen Anderson9b676982009-08-04 22:55:26 +0000268 unsigned short pred, Constant* LHS, Constant* RHS)
269 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
270 Op<0>() = LHS;
271 Op<1>() = RHS;
272 }
273 /// Transparently provide more efficient getOperand methods.
274 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Craig Toppercc03b492015-12-15 06:11:36 +0000275
276 static bool classof(const ConstantExpr *CE) {
277 return CE->getOpcode() == Instruction::ICmp ||
278 CE->getOpcode() == Instruction::FCmp;
279 }
280 static bool classof(const Value *V) {
281 return isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V));
282 }
Owen Anderson9b676982009-08-04 22:55:26 +0000283};
284
285template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000286struct OperandTraits<UnaryConstantExpr>
287 : public FixedNumOperandTraits<UnaryConstantExpr, 1> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000288DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
289
290template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000291struct OperandTraits<BinaryConstantExpr>
292 : public FixedNumOperandTraits<BinaryConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000293DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
294
295template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000296struct OperandTraits<SelectConstantExpr>
297 : public FixedNumOperandTraits<SelectConstantExpr, 3> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000298DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
299
300template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000301struct OperandTraits<ExtractElementConstantExpr>
302 : public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000303DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
304
305template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000306struct OperandTraits<InsertElementConstantExpr>
307 : public FixedNumOperandTraits<InsertElementConstantExpr, 3> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000308DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
309
310template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000311struct OperandTraits<ShuffleVectorConstantExpr>
312 : public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000313DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
314
315template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000316struct OperandTraits<ExtractValueConstantExpr>
317 : public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000318DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
319
320template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000321struct OperandTraits<InsertValueConstantExpr>
322 : public FixedNumOperandTraits<InsertValueConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000323DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
324
325template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000326struct OperandTraits<GetElementPtrConstantExpr>
327 : public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000328
329DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
330
Owen Anderson9b676982009-08-04 22:55:26 +0000331template <>
Eric Christopher7a4d1092015-05-18 21:49:02 +0000332struct OperandTraits<CompareConstantExpr>
333 : public FixedNumOperandTraits<CompareConstantExpr, 2> {};
Owen Anderson9b676982009-08-04 22:55:26 +0000334DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
335
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000336template <class ConstantClass> struct ConstantAggrKeyType;
337struct InlineAsmKeyType;
338struct ConstantExprKeyType;
339
340template <class ConstantClass> struct ConstantInfo;
341template <> struct ConstantInfo<ConstantExpr> {
342 typedef ConstantExprKeyType ValType;
343 typedef Type TypeClass;
344};
345template <> struct ConstantInfo<InlineAsm> {
346 typedef InlineAsmKeyType ValType;
347 typedef PointerType TypeClass;
348};
349template <> struct ConstantInfo<ConstantArray> {
350 typedef ConstantAggrKeyType<ConstantArray> ValType;
351 typedef ArrayType TypeClass;
352};
353template <> struct ConstantInfo<ConstantStruct> {
354 typedef ConstantAggrKeyType<ConstantStruct> ValType;
355 typedef StructType TypeClass;
356};
357template <> struct ConstantInfo<ConstantVector> {
358 typedef ConstantAggrKeyType<ConstantVector> ValType;
359 typedef VectorType TypeClass;
360};
361
362template <class ConstantClass> struct ConstantAggrKeyType {
363 ArrayRef<Constant *> Operands;
364 ConstantAggrKeyType(ArrayRef<Constant *> Operands) : Operands(Operands) {}
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000365 ConstantAggrKeyType(ArrayRef<Constant *> Operands, const ConstantClass *)
366 : Operands(Operands) {}
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000367 ConstantAggrKeyType(const ConstantClass *C,
368 SmallVectorImpl<Constant *> &Storage) {
369 assert(Storage.empty() && "Expected empty storage");
370 for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
371 Storage.push_back(C->getOperand(I));
372 Operands = Storage;
Duncan P. N. Exon Smith344ebf72014-08-19 01:02:18 +0000373 }
374
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000375 bool operator==(const ConstantAggrKeyType &X) const {
376 return Operands == X.Operands;
377 }
378 bool operator==(const ConstantClass *C) const {
379 if (Operands.size() != C->getNumOperands())
380 return false;
381 for (unsigned I = 0, E = Operands.size(); I != E; ++I)
382 if (Operands[I] != C->getOperand(I))
383 return false;
384 return true;
385 }
386 unsigned getHash() const {
387 return hash_combine_range(Operands.begin(), Operands.end());
388 }
389
390 typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
391 ConstantClass *create(TypeClass *Ty) const {
392 return new (Operands.size()) ConstantClass(Ty, Operands);
Duncan P. N. Exon Smith344ebf72014-08-19 01:02:18 +0000393 }
394};
Owen Anderson9b676982009-08-04 22:55:26 +0000395
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000396struct InlineAsmKeyType {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000397 StringRef AsmString;
398 StringRef Constraints;
David Blaikie71c9c9c2015-07-28 00:06:38 +0000399 FunctionType *FTy;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000400 bool HasSideEffects;
401 bool IsAlignStack;
402 InlineAsm::AsmDialect AsmDialect;
403
404 InlineAsmKeyType(StringRef AsmString, StringRef Constraints,
David Blaikie71c9c9c2015-07-28 00:06:38 +0000405 FunctionType *FTy, bool HasSideEffects, bool IsAlignStack,
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000406 InlineAsm::AsmDialect AsmDialect)
David Blaikie71c9c9c2015-07-28 00:06:38 +0000407 : AsmString(AsmString), Constraints(Constraints), FTy(FTy),
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000408 HasSideEffects(HasSideEffects), IsAlignStack(IsAlignStack),
409 AsmDialect(AsmDialect) {}
410 InlineAsmKeyType(const InlineAsm *Asm, SmallVectorImpl<Constant *> &)
411 : AsmString(Asm->getAsmString()), Constraints(Asm->getConstraintString()),
David Blaikie71c9c9c2015-07-28 00:06:38 +0000412 FTy(Asm->getFunctionType()), HasSideEffects(Asm->hasSideEffects()),
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000413 IsAlignStack(Asm->isAlignStack()), AsmDialect(Asm->getDialect()) {}
414
415 bool operator==(const InlineAsmKeyType &X) const {
416 return HasSideEffects == X.HasSideEffects &&
417 IsAlignStack == X.IsAlignStack && AsmDialect == X.AsmDialect &&
David Blaikie71c9c9c2015-07-28 00:06:38 +0000418 AsmString == X.AsmString && Constraints == X.Constraints &&
419 FTy == X.FTy;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000420 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000421 bool operator==(const InlineAsm *Asm) const {
422 return HasSideEffects == Asm->hasSideEffects() &&
423 IsAlignStack == Asm->isAlignStack() &&
424 AsmDialect == Asm->getDialect() &&
425 AsmString == Asm->getAsmString() &&
David Blaikie71c9c9c2015-07-28 00:06:38 +0000426 Constraints == Asm->getConstraintString() &&
427 FTy == Asm->getFunctionType();
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000428 }
429 unsigned getHash() const {
430 return hash_combine(AsmString, Constraints, HasSideEffects, IsAlignStack,
David Blaikie71c9c9c2015-07-28 00:06:38 +0000431 AsmDialect, FTy);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000432 }
433
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000434 typedef ConstantInfo<InlineAsm>::TypeClass TypeClass;
435 InlineAsm *create(TypeClass *Ty) const {
David Blaikie71c9c9c2015-07-28 00:06:38 +0000436 assert(PointerType::getUnqual(FTy) == Ty);
437 return new InlineAsm(FTy, AsmString, Constraints, HasSideEffects,
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000438 IsAlignStack, AsmDialect);
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000439 }
440};
441
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000442struct ConstantExprKeyType {
443 uint8_t Opcode;
444 uint8_t SubclassOptionalData;
445 uint16_t SubclassData;
446 ArrayRef<Constant *> Ops;
447 ArrayRef<unsigned> Indexes;
David Blaikie60310f22015-05-08 00:42:26 +0000448 Type *ExplicitTy;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000449
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000450 ConstantExprKeyType(unsigned Opcode, ArrayRef<Constant *> Ops,
451 unsigned short SubclassData = 0,
452 unsigned short SubclassOptionalData = 0,
David Blaikie60310f22015-05-08 00:42:26 +0000453 ArrayRef<unsigned> Indexes = None,
454 Type *ExplicitTy = nullptr)
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000455 : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData),
David Blaikie60310f22015-05-08 00:42:26 +0000456 SubclassData(SubclassData), Ops(Ops), Indexes(Indexes),
457 ExplicitTy(ExplicitTy) {}
Duncan P. N. Exon Smith909620a2014-08-19 19:13:30 +0000458 ConstantExprKeyType(ArrayRef<Constant *> Operands, const ConstantExpr *CE)
459 : Opcode(CE->getOpcode()),
460 SubclassOptionalData(CE->getRawSubclassOptionalData()),
461 SubclassData(CE->isCompare() ? CE->getPredicate() : 0), Ops(Operands),
462 Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {}
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000463 ConstantExprKeyType(const ConstantExpr *CE,
464 SmallVectorImpl<Constant *> &Storage)
465 : Opcode(CE->getOpcode()),
466 SubclassOptionalData(CE->getRawSubclassOptionalData()),
467 SubclassData(CE->isCompare() ? CE->getPredicate() : 0),
468 Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {
469 assert(Storage.empty() && "Expected empty storage");
470 for (unsigned I = 0, E = CE->getNumOperands(); I != E; ++I)
471 Storage.push_back(CE->getOperand(I));
472 Ops = Storage;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000473 }
474
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000475 bool operator==(const ConstantExprKeyType &X) const {
476 return Opcode == X.Opcode && SubclassData == X.SubclassData &&
477 SubclassOptionalData == X.SubclassOptionalData && Ops == X.Ops &&
478 Indexes == X.Indexes;
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 ConstantExpr *CE) const {
482 if (Opcode != CE->getOpcode())
483 return false;
484 if (SubclassOptionalData != CE->getRawSubclassOptionalData())
485 return false;
486 if (Ops.size() != CE->getNumOperands())
487 return false;
488 if (SubclassData != (CE->isCompare() ? CE->getPredicate() : 0))
489 return false;
490 for (unsigned I = 0, E = Ops.size(); I != E; ++I)
491 if (Ops[I] != CE->getOperand(I))
492 return false;
493 if (Indexes != (CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()))
494 return false;
495 return true;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000496 }
497
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000498 unsigned getHash() const {
499 return hash_combine(Opcode, SubclassOptionalData, SubclassData,
500 hash_combine_range(Ops.begin(), Ops.end()),
501 hash_combine_range(Indexes.begin(), Indexes.end()));
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000502 }
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000503
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000504 typedef ConstantInfo<ConstantExpr>::TypeClass TypeClass;
505 ConstantExpr *create(TypeClass *Ty) const {
506 switch (Opcode) {
507 default:
508 if (Instruction::isCast(Opcode))
509 return new UnaryConstantExpr(Opcode, Ops[0], Ty);
510 if ((Opcode >= Instruction::BinaryOpsBegin &&
511 Opcode < Instruction::BinaryOpsEnd))
512 return new BinaryConstantExpr(Opcode, Ops[0], Ops[1],
513 SubclassOptionalData);
514 llvm_unreachable("Invalid ConstantExpr!");
515 case Instruction::Select:
516 return new SelectConstantExpr(Ops[0], Ops[1], Ops[2]);
517 case Instruction::ExtractElement:
518 return new ExtractElementConstantExpr(Ops[0], Ops[1]);
519 case Instruction::InsertElement:
520 return new InsertElementConstantExpr(Ops[0], Ops[1], Ops[2]);
521 case Instruction::ShuffleVector:
522 return new ShuffleVectorConstantExpr(Ops[0], Ops[1], Ops[2]);
523 case Instruction::InsertValue:
524 return new InsertValueConstantExpr(Ops[0], Ops[1], Indexes, Ty);
525 case Instruction::ExtractValue:
526 return new ExtractValueConstantExpr(Ops[0], Indexes, Ty);
527 case Instruction::GetElementPtr:
David Blaikie60310f22015-05-08 00:42:26 +0000528 return GetElementPtrConstantExpr::Create(
529 ExplicitTy ? ExplicitTy
530 : cast<PointerType>(Ops[0]->getType()->getScalarType())
531 ->getElementType(),
532 Ops[0], Ops.slice(1), Ty, SubclassOptionalData);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000533 case Instruction::ICmp:
534 return new CompareConstantExpr(Ty, Instruction::ICmp, SubclassData,
535 Ops[0], Ops[1]);
536 case Instruction::FCmp:
537 return new CompareConstantExpr(Ty, Instruction::FCmp, SubclassData,
538 Ops[0], Ops[1]);
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000539 }
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000540 }
541};
542
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000543template <class ConstantClass> class ConstantUniqueMap {
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000544public:
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000545 typedef typename ConstantInfo<ConstantClass>::ValType ValType;
546 typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
547 typedef std::pair<TypeClass *, ValType> LookupKey;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000548
Owen Anderson9b676982009-08-04 22:55:26 +0000549private:
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000550 struct MapInfo {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000551 typedef DenseMapInfo<ConstantClass *> ConstantClassInfo;
552 static inline ConstantClass *getEmptyKey() {
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000553 return ConstantClassInfo::getEmptyKey();
554 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000555 static inline ConstantClass *getTombstoneKey() {
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000556 return ConstantClassInfo::getTombstoneKey();
557 }
558 static unsigned getHashValue(const ConstantClass *CP) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000559 SmallVector<Constant *, 8> Storage;
560 return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage)));
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000561 }
562 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
563 return LHS == RHS;
564 }
565 static unsigned getHashValue(const LookupKey &Val) {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000566 return hash_combine(Val.first, Val.second.getHash());
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000567 }
568 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
569 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
570 return false;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000571 if (LHS.first != RHS->getType())
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000572 return false;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000573 return LHS.second == RHS;
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000574 }
575 };
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000576
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000577public:
578 typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
579
580private:
Owen Anderson9b676982009-08-04 22:55:26 +0000581 MapTy Map;
Owen Anderson9b676982009-08-04 22:55:26 +0000582
Owen Anderson9b676982009-08-04 22:55:26 +0000583public:
Devang Patelc5aa8c62009-08-11 06:31:57 +0000584 typename MapTy::iterator map_begin() { return Map.begin(); }
Owen Anderson9b676982009-08-04 22:55:26 +0000585 typename MapTy::iterator map_end() { return Map.end(); }
Torok Edwind18e6682009-08-31 16:14:59 +0000586
587 void freeConstants() {
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000588 for (auto &I : Map)
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000589 // Asserts that use_empty().
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000590 delete I.first;
Owen Anderson9b676982009-08-04 22:55:26 +0000591 }
Dan Gohmane4532f32009-09-15 15:58:07 +0000592
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000593private:
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000594 ConstantClass *create(TypeClass *Ty, ValType V) {
595 ConstantClass *Result = V.create(Ty);
Owen Anderson9b676982009-08-04 22:55:26 +0000596
597 assert(Result->getType() == Ty && "Type specified is not correct!");
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000598 insert(Result);
Owen Anderson9b676982009-08-04 22:55:26 +0000599
Owen Anderson9b676982009-08-04 22:55:26 +0000600 return Result;
601 }
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000602
Owen Anderson9b676982009-08-04 22:55:26 +0000603public:
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000604 /// Return the specified constant from the map, creating it if necessary.
605 ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000606 LookupKey Lookup(Ty, V);
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000607 ConstantClass *Result = nullptr;
Aaron Ballmane4b91dc2014-08-19 14:59:02 +0000608
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000609 auto I = find(Lookup);
610 if (I == Map.end())
611 Result = create(Ty, V);
612 else
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000613 Result = I->first;
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000614 assert(Result && "Unexpected nullptr");
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000615
Owen Anderson9b676982009-08-04 22:55:26 +0000616 return Result;
617 }
618
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000619 /// Find the constant by lookup key.
620 typename MapTy::iterator find(LookupKey Lookup) {
621 return Map.find_as(Lookup);
622 }
623
624 /// Insert the constant into its proper slot.
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000625 void insert(ConstantClass *CP) { Map[CP] = '\0'; }
Duncan P. N. Exon Smith8d125582014-08-19 00:42:32 +0000626
627 /// 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 Smith8d125582014-08-19 00:42:32 +0000631 assert(I->first == 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) {
639 LookupKey Lookup(CP->getType(), ValType(Operands, CP));
640 auto I = find(Lookup);
641 if (I != Map.end())
642 return I->first;
643
644 // Update to the new value. Optimize for the case when we have a single
645 // operand that we're changing, but handle bulk updates efficiently.
646 remove(CP);
647 if (NumUpdated == 1) {
648 assert(OperandNo < CP->getNumOperands() && "Invalid index");
649 assert(CP->getOperand(OperandNo) != To && "I didn't contain From!");
650 CP->setOperand(OperandNo, To);
651 } else {
652 for (unsigned I = 0, E = CP->getNumOperands(); I != E; ++I)
653 if (CP->getOperand(I) == From)
654 CP->setOperand(I, To);
655 }
656 insert(CP);
657 return nullptr;
658 }
659
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000660 void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); }
Owen Anderson9b676982009-08-04 22:55:26 +0000661};
662
Duncan P. N. Exon Smith25fb1102014-08-19 00:21:04 +0000663} // end namespace llvm
Owen Anderson9b676982009-08-04 22:55:26 +0000664
665#endif