blob: 59b9d4d3c5ed0dd0050a127ee0480b1d0fdc88df [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
15#ifndef LLVM_CONSTANTSCONTEXT_H
16#define LLVM_CONSTANTSCONTEXT_H
17
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>
27
28namespace llvm {
29template<class ValType>
30struct ConstantTraits;
31
32/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
33/// behind the scenes to implement unary constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000034class UnaryConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000035 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +000036 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +000037public:
38 // allocate space for exactly one operand
39 void *operator new(size_t s) {
40 return User::operator new(s, 1);
41 }
Chris Lattner229907c2011-07-18 04:54:35 +000042 UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
Owen Anderson9b676982009-08-04 22:55:26 +000043 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
44 Op<0>() = C;
45 }
46 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
47};
48
49/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
50/// behind the scenes to implement binary constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000051class BinaryConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000052 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +000053 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +000054public:
55 // allocate space for exactly two operands
56 void *operator new(size_t s) {
57 return User::operator new(s, 2);
58 }
Dan Gohman1b849082009-09-07 23:54:19 +000059 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
60 unsigned Flags)
Owen Anderson9b676982009-08-04 22:55:26 +000061 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
62 Op<0>() = C1;
63 Op<1>() = C2;
Dan Gohman1b849082009-09-07 23:54:19 +000064 SubclassOptionalData = Flags;
Owen Anderson9b676982009-08-04 22:55:26 +000065 }
66 /// Transparently provide more efficient getOperand methods.
67 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
68};
69
70/// SelectConstantExpr - This class is private to Constants.cpp, and is used
71/// behind the scenes to implement select constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000072class SelectConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000073 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +000074 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +000075public:
76 // allocate space for exactly three operands
77 void *operator new(size_t s) {
78 return User::operator new(s, 3);
79 }
80 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
81 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
82 Op<0>() = C1;
83 Op<1>() = C2;
84 Op<2>() = C3;
85 }
86 /// Transparently provide more efficient getOperand methods.
87 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
88};
89
90/// ExtractElementConstantExpr - This class is private to
91/// Constants.cpp, and is used behind the scenes to implement
92/// extractelement constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000093class ExtractElementConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000094 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +000095 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +000096public:
97 // allocate space for exactly two operands
98 void *operator new(size_t s) {
99 return User::operator new(s, 2);
100 }
101 ExtractElementConstantExpr(Constant *C1, Constant *C2)
102 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
103 Instruction::ExtractElement, &Op<0>(), 2) {
104 Op<0>() = C1;
105 Op<1>() = C2;
106 }
107 /// Transparently provide more efficient getOperand methods.
108 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
109};
110
111/// InsertElementConstantExpr - This class is private to
112/// Constants.cpp, and is used behind the scenes to implement
113/// insertelement constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000114class InsertElementConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000115 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +0000116 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +0000117public:
118 // allocate space for exactly three operands
119 void *operator new(size_t s) {
120 return User::operator new(s, 3);
121 }
122 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
123 : ConstantExpr(C1->getType(), Instruction::InsertElement,
124 &Op<0>(), 3) {
125 Op<0>() = C1;
126 Op<1>() = C2;
127 Op<2>() = C3;
128 }
129 /// Transparently provide more efficient getOperand methods.
130 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
131};
132
133/// ShuffleVectorConstantExpr - This class is private to
134/// Constants.cpp, and is used behind the scenes to implement
135/// shufflevector constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000136class ShuffleVectorConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000137 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +0000138 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +0000139public:
140 // allocate space for exactly three operands
141 void *operator new(size_t s) {
142 return User::operator new(s, 3);
143 }
144 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
145 : ConstantExpr(VectorType::get(
146 cast<VectorType>(C1->getType())->getElementType(),
147 cast<VectorType>(C3->getType())->getNumElements()),
148 Instruction::ShuffleVector,
149 &Op<0>(), 3) {
150 Op<0>() = C1;
151 Op<1>() = C2;
152 Op<2>() = C3;
153 }
154 /// Transparently provide more efficient getOperand methods.
155 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
156};
157
158/// ExtractValueConstantExpr - This class is private to
159/// Constants.cpp, and is used behind the scenes to implement
160/// extractvalue constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000161class ExtractValueConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000162 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +0000163 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +0000164public:
165 // allocate space for exactly one operand
166 void *operator new(size_t s) {
167 return User::operator new(s, 1);
168 }
169 ExtractValueConstantExpr(Constant *Agg,
170 const SmallVector<unsigned, 4> &IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000171 Type *DestTy)
Owen Anderson9b676982009-08-04 22:55:26 +0000172 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
173 Indices(IdxList) {
174 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;
Craig Toppera60c0f12012-09-15 17:09:36 +0000189 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
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,
196 const SmallVector<unsigned, 4> &IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000197 Type *DestTy)
Owen Anderson9b676982009-08-04 22:55:26 +0000198 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
199 Indices(IdxList) {
200 Op<0>() = Agg;
201 Op<1>() = Val;
202 }
203
204 /// Indices - These identify the position for the insertion.
205 const SmallVector<unsigned, 4> Indices;
206
207 /// Transparently provide more efficient getOperand methods.
208 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
209};
210
211
212/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
213/// used behind the scenes to implement getelementpr constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000214class GetElementPtrConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000215 void anchor() override;
Chris Lattnera474bb22012-01-26 20:40:56 +0000216 GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000217 Type *DestTy);
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) {
223 GetElementPtrConstantExpr *Result =
Owen Anderson9b676982009-08-04 22:55:26 +0000224 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Dan Gohman1b849082009-09-07 23:54:19 +0000225 Result->SubclassOptionalData = Flags;
226 return Result;
Owen Anderson9b676982009-08-04 22:55:26 +0000227 }
228 /// Transparently provide more efficient getOperand methods.
229 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
230};
231
232// CompareConstantExpr - This class is private to Constants.cpp, and is used
233// behind the scenes to implement ICmp and FCmp constant expressions. This is
234// needed in order to store the predicate value for these instructions.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000235class CompareConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000236 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +0000237 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
David Blaikiea379b1812011-12-20 02:50:00 +0000238public:
Owen Anderson9b676982009-08-04 22:55:26 +0000239 // allocate space for exactly two operands
240 void *operator new(size_t s) {
241 return User::operator new(s, 2);
242 }
243 unsigned short predicate;
Chris Lattner229907c2011-07-18 04:54:35 +0000244 CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
Owen Anderson9b676982009-08-04 22:55:26 +0000245 unsigned short pred, Constant* LHS, Constant* RHS)
246 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
247 Op<0>() = LHS;
248 Op<1>() = RHS;
249 }
250 /// Transparently provide more efficient getOperand methods.
251 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
252};
253
254template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000255struct OperandTraits<UnaryConstantExpr> :
256 public FixedNumOperandTraits<UnaryConstantExpr, 1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000257};
258DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
259
260template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000261struct OperandTraits<BinaryConstantExpr> :
262 public FixedNumOperandTraits<BinaryConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000263};
264DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
265
266template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000267struct OperandTraits<SelectConstantExpr> :
268 public FixedNumOperandTraits<SelectConstantExpr, 3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000269};
270DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
271
272template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000273struct OperandTraits<ExtractElementConstantExpr> :
274 public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000275};
276DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
277
278template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000279struct OperandTraits<InsertElementConstantExpr> :
280 public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000281};
282DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
283
284template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000285struct OperandTraits<ShuffleVectorConstantExpr> :
286 public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000287};
288DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
289
290template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000291struct OperandTraits<ExtractValueConstantExpr> :
292 public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000293};
294DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
295
296template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000297struct OperandTraits<InsertValueConstantExpr> :
298 public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000299};
300DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
301
302template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000303struct OperandTraits<GetElementPtrConstantExpr> :
304 public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000305};
306
307DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
308
309
310template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000311struct OperandTraits<CompareConstantExpr> :
312 public FixedNumOperandTraits<CompareConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000313};
314DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
315
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000316struct ExprMapKeyType {
Owen Anderson9b676982009-08-04 22:55:26 +0000317 ExprMapKeyType(unsigned opc,
Jay Foad0091fe82011-04-13 15:22:40 +0000318 ArrayRef<Constant*> ops,
Dan Gohman1b849082009-09-07 23:54:19 +0000319 unsigned short flags = 0,
320 unsigned short optionalflags = 0,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000321 ArrayRef<unsigned> inds = None)
Dan Gohman1b849082009-09-07 23:54:19 +0000322 : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
Jay Foad0091fe82011-04-13 15:22:40 +0000323 operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {}
Dan Gohman1b849082009-09-07 23:54:19 +0000324 uint8_t opcode;
325 uint8_t subclassoptionaldata;
326 uint16_t subclassdata;
Owen Anderson9b676982009-08-04 22:55:26 +0000327 std::vector<Constant*> operands;
Jay Foad0091fe82011-04-13 15:22:40 +0000328 SmallVector<unsigned, 4> indices;
Owen Anderson9b676982009-08-04 22:55:26 +0000329 bool operator==(const ExprMapKeyType& that) const {
330 return this->opcode == that.opcode &&
Dan Gohman1b849082009-09-07 23:54:19 +0000331 this->subclassdata == that.subclassdata &&
332 this->subclassoptionaldata == that.subclassoptionaldata &&
Owen Anderson9b676982009-08-04 22:55:26 +0000333 this->operands == that.operands &&
334 this->indices == that.indices;
335 }
336 bool operator<(const ExprMapKeyType & that) const {
Benjamin Kramerb2f034b2014-03-03 19:58:30 +0000337 return std::tie(opcode, operands, subclassdata, subclassoptionaldata,
338 indices) <
339 std::tie(that.opcode, that.operands, that.subclassdata,
340 that.subclassoptionaldata, that.indices);
Owen Anderson9b676982009-08-04 22:55:26 +0000341 }
342
343 bool operator!=(const ExprMapKeyType& that) const {
344 return !(*this == that);
345 }
346};
347
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000348struct InlineAsmKeyType {
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000349 InlineAsmKeyType(StringRef AsmString,
350 StringRef Constraints, bool hasSideEffects,
Chad Rosierd8c76102012-09-05 19:00:49 +0000351 bool isAlignStack, InlineAsm::AsmDialect asmDialect)
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000352 : asm_string(AsmString), constraints(Constraints),
Chad Rosier8b3014e2012-09-04 22:46:24 +0000353 has_side_effects(hasSideEffects), is_align_stack(isAlignStack),
354 asm_dialect(asmDialect) {}
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000355 std::string asm_string;
356 std::string constraints;
357 bool has_side_effects;
358 bool is_align_stack;
Chad Rosierd8c76102012-09-05 19:00:49 +0000359 InlineAsm::AsmDialect asm_dialect;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000360 bool operator==(const InlineAsmKeyType& that) const {
361 return this->asm_string == that.asm_string &&
362 this->constraints == that.constraints &&
363 this->has_side_effects == that.has_side_effects &&
Chad Rosier8b3014e2012-09-04 22:46:24 +0000364 this->is_align_stack == that.is_align_stack &&
365 this->asm_dialect == that.asm_dialect;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000366 }
367 bool operator<(const InlineAsmKeyType& that) const {
Benjamin Kramerb2f034b2014-03-03 19:58:30 +0000368 return std::tie(asm_string, constraints, has_side_effects, is_align_stack,
369 asm_dialect) <
370 std::tie(that.asm_string, that.constraints, that.has_side_effects,
371 that.is_align_stack, that.asm_dialect);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000372 }
373
374 bool operator!=(const InlineAsmKeyType& that) const {
375 return !(*this == that);
376 }
377};
378
Owen Anderson9b676982009-08-04 22:55:26 +0000379// The number of operands for each ConstantCreator::create method is
380// determined by the ConstantTraits template.
381// ConstantCreator - A class that is used to create constants by
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000382// ConstantUniqueMap*. This class should be partially specialized if there is
Owen Anderson9b676982009-08-04 22:55:26 +0000383// something strange that needs to be done to interface to the ctor for the
384// constant.
385//
386template<typename T, typename Alloc>
387struct ConstantTraits< std::vector<T, Alloc> > {
388 static unsigned uses(const std::vector<T, Alloc>& v) {
389 return v.size();
390 }
391};
392
Chris Lattner392be582010-02-12 20:49:41 +0000393template<>
394struct ConstantTraits<Constant *> {
395 static unsigned uses(Constant * const & v) {
396 return 1;
397 }
398};
399
Owen Anderson9b676982009-08-04 22:55:26 +0000400template<class ConstantClass, class TypeClass, class ValType>
401struct ConstantCreator {
Chris Lattner229907c2011-07-18 04:54:35 +0000402 static ConstantClass *create(TypeClass *Ty, const ValType &V) {
Owen Anderson9b676982009-08-04 22:55:26 +0000403 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
404 }
405};
406
Talin46e9b442012-02-05 20:54:10 +0000407template<class ConstantClass, class TypeClass>
408struct ConstantArrayCreator {
409 static ConstantClass *create(TypeClass *Ty, ArrayRef<Constant*> V) {
410 return new(V.size()) ConstantClass(Ty, V);
411 }
412};
413
Dan Gohmane4532f32009-09-15 15:58:07 +0000414template<class ConstantClass>
415struct ConstantKeyData {
416 typedef void ValType;
417 static ValType getValType(ConstantClass *C) {
418 llvm_unreachable("Unknown Constant type!");
Owen Anderson9b676982009-08-04 22:55:26 +0000419 }
420};
421
422template<>
423struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Chris Lattner229907c2011-07-18 04:54:35 +0000424 static ConstantExpr *create(Type *Ty, const ExprMapKeyType &V,
Owen Anderson9b676982009-08-04 22:55:26 +0000425 unsigned short pred = 0) {
426 if (Instruction::isCast(V.opcode))
427 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
428 if ((V.opcode >= Instruction::BinaryOpsBegin &&
429 V.opcode < Instruction::BinaryOpsEnd))
Dan Gohman1b849082009-09-07 23:54:19 +0000430 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
431 V.subclassoptionaldata);
Owen Anderson9b676982009-08-04 22:55:26 +0000432 if (V.opcode == Instruction::Select)
433 return new SelectConstantExpr(V.operands[0], V.operands[1],
434 V.operands[2]);
435 if (V.opcode == Instruction::ExtractElement)
436 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
437 if (V.opcode == Instruction::InsertElement)
438 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
439 V.operands[2]);
440 if (V.opcode == Instruction::ShuffleVector)
441 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
442 V.operands[2]);
443 if (V.opcode == Instruction::InsertValue)
444 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
445 V.indices, Ty);
446 if (V.opcode == Instruction::ExtractValue)
447 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
448 if (V.opcode == Instruction::GetElementPtr) {
449 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Dan Gohman1b849082009-09-07 23:54:19 +0000450 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
451 V.subclassoptionaldata);
Owen Anderson9b676982009-08-04 22:55:26 +0000452 }
453
454 // The compare instructions are weird. We have to encode the predicate
455 // value and it is combined with the instruction opcode by multiplying
456 // the opcode by one hundred. We must decode this to get the predicate.
457 if (V.opcode == Instruction::ICmp)
Dan Gohman1b849082009-09-07 23:54:19 +0000458 return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
Owen Anderson9b676982009-08-04 22:55:26 +0000459 V.operands[0], V.operands[1]);
460 if (V.opcode == Instruction::FCmp)
Dan Gohman1b849082009-09-07 23:54:19 +0000461 return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
Owen Anderson9b676982009-08-04 22:55:26 +0000462 V.operands[0], V.operands[1]);
463 llvm_unreachable("Invalid ConstantExpr!");
Owen Anderson9b676982009-08-04 22:55:26 +0000464 }
465};
466
467template<>
Dan Gohmane4532f32009-09-15 15:58:07 +0000468struct ConstantKeyData<ConstantExpr> {
469 typedef ExprMapKeyType ValType;
470 static ValType getValType(ConstantExpr *CE) {
471 std::vector<Constant*> Operands;
472 Operands.reserve(CE->getNumOperands());
473 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
474 Operands.push_back(cast<Constant>(CE->getOperand(i)));
475 return ExprMapKeyType(CE->getOpcode(), Operands,
476 CE->isCompare() ? CE->getPredicate() : 0,
477 CE->getRawSubclassOptionalData(),
478 CE->hasIndices() ?
Jay Foad0091fe82011-04-13 15:22:40 +0000479 CE->getIndices() : ArrayRef<unsigned>());
Owen Anderson9b676982009-08-04 22:55:26 +0000480 }
481};
482
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000483template<>
484struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {
Chris Lattner229907c2011-07-18 04:54:35 +0000485 static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) {
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000486 return new InlineAsm(Ty, Key.asm_string, Key.constraints,
Chad Rosier8b3014e2012-09-04 22:46:24 +0000487 Key.has_side_effects, Key.is_align_stack,
488 Key.asm_dialect);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000489 }
490};
491
492template<>
493struct ConstantKeyData<InlineAsm> {
494 typedef InlineAsmKeyType ValType;
495 static ValType getValType(InlineAsm *Asm) {
496 return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
Chad Rosier8b3014e2012-09-04 22:46:24 +0000497 Asm->hasSideEffects(), Asm->isAlignStack(),
498 Asm->getDialect());
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000499 }
500};
501
Jay Foadc365eea2011-06-22 08:50:06 +0000502template<class ValType, class ValRefType, class TypeClass, class ConstantClass,
Owen Anderson9b676982009-08-04 22:55:26 +0000503 bool HasLargeKey = false /*true for arrays and structs*/ >
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000504class ConstantUniqueMap {
Owen Anderson9b676982009-08-04 22:55:26 +0000505public:
Chris Lattner229907c2011-07-18 04:54:35 +0000506 typedef std::pair<TypeClass*, ValType> MapKey;
Dan Gohmane4532f32009-09-15 15:58:07 +0000507 typedef std::map<MapKey, ConstantClass *> MapTy;
508 typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy;
Owen Anderson9b676982009-08-04 22:55:26 +0000509private:
510 /// Map - This is the main map from the element descriptor to the Constants.
511 /// This is the primary way we avoid creating two of the same shape
512 /// constant.
513 MapTy Map;
514
515 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
516 /// from the constants to their element in Map. This is important for
517 /// removal of constants from the array, which would otherwise have to scan
518 /// through the map with very large keys.
519 InverseMapTy InverseMap;
520
Owen Anderson9b676982009-08-04 22:55:26 +0000521public:
Devang Patelc5aa8c62009-08-11 06:31:57 +0000522 typename MapTy::iterator map_begin() { return Map.begin(); }
Owen Anderson9b676982009-08-04 22:55:26 +0000523 typename MapTy::iterator map_end() { return Map.end(); }
Torok Edwind18e6682009-08-31 16:14:59 +0000524
525 void freeConstants() {
526 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
527 I != E; ++I) {
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000528 // Asserts that use_empty().
529 delete I->second;
Torok Edwind18e6682009-08-31 16:14:59 +0000530 }
531 }
Owen Anderson9b676982009-08-04 22:55:26 +0000532
533 /// InsertOrGetItem - Return an iterator for the specified element.
534 /// If the element exists in the map, the returned iterator points to the
535 /// entry and Exists=true. If not, the iterator points to the newly
536 /// inserted entry and returns Exists=false. Newly inserted entries have
537 /// I->second == 0, and should be filled in.
Dan Gohmane4532f32009-09-15 15:58:07 +0000538 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *>
Owen Anderson9b676982009-08-04 22:55:26 +0000539 &InsertVal,
540 bool &Exists) {
541 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
542 Exists = !IP.second;
543 return IP.first;
544 }
545
546private:
547 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
548 if (HasLargeKey) {
549 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
550 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
551 IMI->second->second == CP &&
552 "InverseMap corrupt!");
553 return IMI->second;
554 }
555
556 typename MapTy::iterator I =
Chris Lattner229907c2011-07-18 04:54:35 +0000557 Map.find(MapKey(static_cast<TypeClass*>(CP->getType()),
Dan Gohmane4532f32009-09-15 15:58:07 +0000558 ConstantKeyData<ConstantClass>::getValType(CP)));
Owen Anderson9b676982009-08-04 22:55:26 +0000559 if (I == Map.end() || I->second != CP) {
560 // FIXME: This should not use a linear scan. If this gets to be a
561 // performance problem, someone should look at this.
562 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
563 /* empty */;
564 }
565 return I;
566 }
Dan Gohmane4532f32009-09-15 15:58:07 +0000567
Chris Lattner229907c2011-07-18 04:54:35 +0000568 ConstantClass *Create(TypeClass *Ty, ValRefType V,
Owen Anderson9b676982009-08-04 22:55:26 +0000569 typename MapTy::iterator I) {
570 ConstantClass* Result =
571 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
572
573 assert(Result->getType() == Ty && "Type specified is not correct!");
574 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
575
576 if (HasLargeKey) // Remember the reverse mapping if needed.
577 InverseMap.insert(std::make_pair(Result, I));
578
Owen Anderson9b676982009-08-04 22:55:26 +0000579 return Result;
580 }
581public:
582
583 /// getOrCreate - Return the specified constant from the map, creating it if
584 /// necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000585 ConstantClass *getOrCreate(TypeClass *Ty, ValRefType V) {
Owen Anderson9b676982009-08-04 22:55:26 +0000586 MapKey Lookup(Ty, V);
587 ConstantClass* Result = 0;
588
589 typename MapTy::iterator I = Map.find(Lookup);
590 // Is it in the map?
591 if (I != Map.end())
Dan Gohmane4532f32009-09-15 15:58:07 +0000592 Result = I->second;
Owen Anderson9b676982009-08-04 22:55:26 +0000593
594 if (!Result) {
595 // If no preexisting value, create one now...
596 Result = Create(Ty, V, I);
597 }
598
599 return Result;
600 }
601
602 void remove(ConstantClass *CP) {
Owen Anderson9b676982009-08-04 22:55:26 +0000603 typename MapTy::iterator I = FindExistingElement(CP);
604 assert(I != Map.end() && "Constant not found in constant table!");
605 assert(I->second == CP && "Didn't find correct element?");
606
607 if (HasLargeKey) // Remember the reverse mapping if needed.
608 InverseMap.erase(CP);
Owen Anderson9b676982009-08-04 22:55:26 +0000609
610 Map.erase(I);
611 }
612
Owen Anderson9b676982009-08-04 22:55:26 +0000613 /// MoveConstantToNewSlot - If we are about to change C to be the element
614 /// specified by I, update our internal data structures to reflect this
615 /// fact.
Owen Anderson9b676982009-08-04 22:55:26 +0000616 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
617 // First, remove the old location of the specified constant in the map.
618 typename MapTy::iterator OldI = FindExistingElement(C);
619 assert(OldI != Map.end() && "Constant not found in constant table!");
620 assert(OldI->second == C && "Didn't find correct element?");
621
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000622 // Remove the old entry from the map.
Owen Anderson9b676982009-08-04 22:55:26 +0000623 Map.erase(OldI);
624
625 // Update the inverse map so that we know that this constant is now
626 // located at descriptor I.
627 if (HasLargeKey) {
628 assert(I->second == C && "Bad inversemap entry!");
629 InverseMap[C] = I;
630 }
631 }
Owen Anderson9b676982009-08-04 22:55:26 +0000632
633 void dump() const {
David Greene338a9032010-01-05 01:34:26 +0000634 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
Owen Anderson9b676982009-08-04 22:55:26 +0000635 }
636};
637
Talin46e9b442012-02-05 20:54:10 +0000638// Unique map for aggregate constants
639template<class TypeClass, class ConstantClass>
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000640class ConstantAggrUniqueMap {
Talin46e9b442012-02-05 20:54:10 +0000641public:
642 typedef ArrayRef<Constant*> Operands;
643 typedef std::pair<TypeClass*, Operands> LookupKey;
644private:
645 struct MapInfo {
646 typedef DenseMapInfo<ConstantClass*> ConstantClassInfo;
647 typedef DenseMapInfo<Constant*> ConstantInfo;
648 typedef DenseMapInfo<TypeClass*> TypeClassInfo;
649 static inline ConstantClass* getEmptyKey() {
650 return ConstantClassInfo::getEmptyKey();
651 }
652 static inline ConstantClass* getTombstoneKey() {
653 return ConstantClassInfo::getTombstoneKey();
654 }
655 static unsigned getHashValue(const ConstantClass *CP) {
Chandler Carruthd4ba3eb2012-03-07 03:22:32 +0000656 SmallVector<Constant*, 8> CPOperands;
657 CPOperands.reserve(CP->getNumOperands());
Jay Foadcc5fd3e2012-03-06 10:43:52 +0000658 for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I)
Chandler Carruthd4ba3eb2012-03-07 03:22:32 +0000659 CPOperands.push_back(CP->getOperand(I));
660 return getHashValue(LookupKey(CP->getType(), CPOperands));
Talin46e9b442012-02-05 20:54:10 +0000661 }
662 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
663 return LHS == RHS;
664 }
665 static unsigned getHashValue(const LookupKey &Val) {
Chandler Carruthd4ba3eb2012-03-07 03:22:32 +0000666 return hash_combine(Val.first, hash_combine_range(Val.second.begin(),
667 Val.second.end()));
Talin46e9b442012-02-05 20:54:10 +0000668 }
669 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
670 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
671 return false;
672 if (LHS.first != RHS->getType()
673 || LHS.second.size() != RHS->getNumOperands())
674 return false;
675 for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) {
676 if (LHS.second[I] != RHS->getOperand(I))
677 return false;
678 }
679 return true;
680 }
681 };
682public:
683 typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
684
685private:
686 /// Map - This is the main map from the element descriptor to the Constants.
687 /// This is the primary way we avoid creating two of the same shape
688 /// constant.
689 MapTy Map;
690
691public:
692 typename MapTy::iterator map_begin() { return Map.begin(); }
693 typename MapTy::iterator map_end() { return Map.end(); }
694
695 void freeConstants() {
696 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
697 I != E; ++I) {
698 // Asserts that use_empty().
699 delete I->first;
700 }
701 }
702
703private:
704 typename MapTy::iterator findExistingElement(ConstantClass *CP) {
705 return Map.find(CP);
706 }
707
708 ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) {
709 ConstantClass* Result =
710 ConstantArrayCreator<ConstantClass,TypeClass>::create(Ty, V);
711
712 assert(Result->getType() == Ty && "Type specified is not correct!");
713 Map[Result] = '\0';
714
715 return Result;
716 }
717public:
718
719 /// getOrCreate - Return the specified constant from the map, creating it if
720 /// necessary.
721 ConstantClass *getOrCreate(TypeClass *Ty, Operands V) {
722 LookupKey Lookup(Ty, V);
723 ConstantClass* Result = 0;
724
725 typename MapTy::iterator I = Map.find_as(Lookup);
726 // Is it in the map?
727 if (I != Map.end())
728 Result = I->first;
729
730 if (!Result) {
731 // If no preexisting value, create one now...
732 Result = Create(Ty, V, I);
733 }
734
735 return Result;
736 }
737
738 /// Find the constant by lookup key.
739 typename MapTy::iterator find(LookupKey Lookup) {
740 return Map.find_as(Lookup);
741 }
742
743 /// Insert the constant into its proper slot.
744 void insert(ConstantClass *CP) {
745 Map[CP] = '\0';
746 }
747
748 /// Remove this constant from the map
749 void remove(ConstantClass *CP) {
750 typename MapTy::iterator I = findExistingElement(CP);
751 assert(I != Map.end() && "Constant not found in constant table!");
752 assert(I->first == CP && "Didn't find correct element?");
753 Map.erase(I);
754 }
755
756 void dump() const {
757 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
758 }
759};
760
Owen Anderson9b676982009-08-04 22:55:26 +0000761}
762
763#endif