blob: c3aefb9ce2dab7684c1ca22d514aa8d5faae4498 [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 {
32template<class ValType>
33struct ConstantTraits;
34
35/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
36/// behind the scenes to implement unary constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000037class UnaryConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000038 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +000039 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +000040public:
41 // allocate space for exactly one operand
42 void *operator new(size_t s) {
43 return User::operator new(s, 1);
44 }
Chris Lattner229907c2011-07-18 04:54:35 +000045 UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
Owen Anderson9b676982009-08-04 22:55:26 +000046 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
47 Op<0>() = C;
48 }
49 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
50};
51
52/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
53/// behind the scenes to implement binary constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000054class BinaryConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000055 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +000056 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +000057public:
58 // allocate space for exactly two operands
59 void *operator new(size_t s) {
60 return User::operator new(s, 2);
61 }
Dan Gohman1b849082009-09-07 23:54:19 +000062 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
63 unsigned Flags)
Owen Anderson9b676982009-08-04 22:55:26 +000064 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
65 Op<0>() = C1;
66 Op<1>() = C2;
Dan Gohman1b849082009-09-07 23:54:19 +000067 SubclassOptionalData = Flags;
Owen Anderson9b676982009-08-04 22:55:26 +000068 }
69 /// Transparently provide more efficient getOperand methods.
70 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
71};
72
73/// SelectConstantExpr - This class is private to Constants.cpp, and is used
74/// behind the scenes to implement select constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000075class SelectConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000076 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +000077 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +000078public:
79 // allocate space for exactly three operands
80 void *operator new(size_t s) {
81 return User::operator new(s, 3);
82 }
83 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
84 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
85 Op<0>() = C1;
86 Op<1>() = C2;
87 Op<2>() = C3;
88 }
89 /// Transparently provide more efficient getOperand methods.
90 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
91};
92
93/// ExtractElementConstantExpr - This class is private to
94/// Constants.cpp, and is used behind the scenes to implement
95/// extractelement constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000096class ExtractElementConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +000097 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +000098 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +000099public:
100 // allocate space for exactly two operands
101 void *operator new(size_t s) {
102 return User::operator new(s, 2);
103 }
104 ExtractElementConstantExpr(Constant *C1, Constant *C2)
105 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
106 Instruction::ExtractElement, &Op<0>(), 2) {
107 Op<0>() = C1;
108 Op<1>() = C2;
109 }
110 /// Transparently provide more efficient getOperand methods.
111 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
112};
113
114/// InsertElementConstantExpr - This class is private to
115/// Constants.cpp, and is used behind the scenes to implement
116/// insertelement constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000117class InsertElementConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000118 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +0000119 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +0000120public:
121 // allocate space for exactly three operands
122 void *operator new(size_t s) {
123 return User::operator new(s, 3);
124 }
125 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
126 : ConstantExpr(C1->getType(), Instruction::InsertElement,
127 &Op<0>(), 3) {
128 Op<0>() = C1;
129 Op<1>() = C2;
130 Op<2>() = C3;
131 }
132 /// Transparently provide more efficient getOperand methods.
133 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
134};
135
136/// ShuffleVectorConstantExpr - This class is private to
137/// Constants.cpp, and is used behind the scenes to implement
138/// shufflevector constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000139class ShuffleVectorConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000140 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +0000141 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +0000142public:
143 // allocate space for exactly three operands
144 void *operator new(size_t s) {
145 return User::operator new(s, 3);
146 }
147 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
148 : ConstantExpr(VectorType::get(
149 cast<VectorType>(C1->getType())->getElementType(),
150 cast<VectorType>(C3->getType())->getNumElements()),
151 Instruction::ShuffleVector,
152 &Op<0>(), 3) {
153 Op<0>() = C1;
154 Op<1>() = C2;
155 Op<2>() = C3;
156 }
157 /// Transparently provide more efficient getOperand methods.
158 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
159};
160
161/// ExtractValueConstantExpr - This class is private to
162/// Constants.cpp, and is used behind the scenes to implement
163/// extractvalue constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000164class ExtractValueConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000165 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +0000166 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +0000167public:
168 // allocate space for exactly one operand
169 void *operator new(size_t s) {
170 return User::operator new(s, 1);
171 }
Duncan P. N. Exon Smith1cd4aeb2014-08-19 00:23:17 +0000172 ExtractValueConstantExpr(Constant *Agg, ArrayRef<unsigned> IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000173 Type *DestTy)
Duncan P. N. Exon Smith1cd4aeb2014-08-19 00:23:17 +0000174 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
175 Indices(IdxList.begin(), IdxList.end()) {
Owen Anderson9b676982009-08-04 22:55:26 +0000176 Op<0>() = Agg;
177 }
178
179 /// Indices - These identify which value to extract.
180 const SmallVector<unsigned, 4> Indices;
181
182 /// Transparently provide more efficient getOperand methods.
183 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
184};
185
186/// InsertValueConstantExpr - This class is private to
187/// Constants.cpp, and is used behind the scenes to implement
188/// insertvalue constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000189class InsertValueConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000190 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +0000191 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
Owen Anderson9b676982009-08-04 22:55:26 +0000192public:
193 // allocate space for exactly one operand
194 void *operator new(size_t s) {
195 return User::operator new(s, 2);
196 }
197 InsertValueConstantExpr(Constant *Agg, Constant *Val,
Duncan P. N. Exon Smith1cd4aeb2014-08-19 00:23:17 +0000198 ArrayRef<unsigned> IdxList, Type *DestTy)
199 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
200 Indices(IdxList.begin(), IdxList.end()) {
Owen Anderson9b676982009-08-04 22:55:26 +0000201 Op<0>() = Agg;
202 Op<1>() = Val;
203 }
204
205 /// Indices - These identify the position for the insertion.
206 const SmallVector<unsigned, 4> Indices;
207
208 /// Transparently provide more efficient getOperand methods.
209 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
210};
211
212
213/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
214/// used behind the scenes to implement getelementpr constant exprs.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000215class GetElementPtrConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000216 void anchor() override;
Chris Lattnera474bb22012-01-26 20:40:56 +0000217 GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000218 Type *DestTy);
Owen Anderson9b676982009-08-04 22:55:26 +0000219public:
220 static GetElementPtrConstantExpr *Create(Constant *C,
Chris Lattnera474bb22012-01-26 20:40:56 +0000221 ArrayRef<Constant*> IdxList,
Chris Lattner229907c2011-07-18 04:54:35 +0000222 Type *DestTy,
Dan Gohman1b849082009-09-07 23:54:19 +0000223 unsigned Flags) {
224 GetElementPtrConstantExpr *Result =
Owen Anderson9b676982009-08-04 22:55:26 +0000225 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Dan Gohman1b849082009-09-07 23:54:19 +0000226 Result->SubclassOptionalData = Flags;
227 return Result;
Owen Anderson9b676982009-08-04 22:55:26 +0000228 }
229 /// Transparently provide more efficient getOperand methods.
230 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
231};
232
233// CompareConstantExpr - This class is private to Constants.cpp, and is used
234// behind the scenes to implement ICmp and FCmp constant expressions. This is
235// needed in order to store the predicate value for these instructions.
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000236class CompareConstantExpr : public ConstantExpr {
Craig Topperf398d7c2014-03-05 06:35:38 +0000237 void anchor() override;
Craig Toppera60c0f12012-09-15 17:09:36 +0000238 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
David Blaikiea379b1812011-12-20 02:50:00 +0000239public:
Owen Anderson9b676982009-08-04 22:55:26 +0000240 // allocate space for exactly two operands
241 void *operator new(size_t s) {
242 return User::operator new(s, 2);
243 }
244 unsigned short predicate;
Chris Lattner229907c2011-07-18 04:54:35 +0000245 CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
Owen Anderson9b676982009-08-04 22:55:26 +0000246 unsigned short pred, Constant* LHS, Constant* RHS)
247 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
248 Op<0>() = LHS;
249 Op<1>() = RHS;
250 }
251 /// Transparently provide more efficient getOperand methods.
252 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
253};
254
255template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000256struct OperandTraits<UnaryConstantExpr> :
257 public FixedNumOperandTraits<UnaryConstantExpr, 1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000258};
259DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
260
261template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000262struct OperandTraits<BinaryConstantExpr> :
263 public FixedNumOperandTraits<BinaryConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000264};
265DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
266
267template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000268struct OperandTraits<SelectConstantExpr> :
269 public FixedNumOperandTraits<SelectConstantExpr, 3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000270};
271DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
272
273template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000274struct OperandTraits<ExtractElementConstantExpr> :
275 public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000276};
277DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
278
279template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000280struct OperandTraits<InsertElementConstantExpr> :
281 public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000282};
283DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
284
285template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000286struct OperandTraits<ShuffleVectorConstantExpr> :
287 public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000288};
289DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
290
291template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000292struct OperandTraits<ExtractValueConstantExpr> :
293 public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000294};
295DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
296
297template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000298struct OperandTraits<InsertValueConstantExpr> :
299 public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000300};
301DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
302
303template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000304struct OperandTraits<GetElementPtrConstantExpr> :
305 public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000306};
307
308DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
309
310
311template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000312struct OperandTraits<CompareConstantExpr> :
313 public FixedNumOperandTraits<CompareConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000314};
315DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
316
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000317struct ExprMapKeyType {
Owen Anderson9b676982009-08-04 22:55:26 +0000318 ExprMapKeyType(unsigned opc,
Jay Foad0091fe82011-04-13 15:22:40 +0000319 ArrayRef<Constant*> ops,
Dan Gohman1b849082009-09-07 23:54:19 +0000320 unsigned short flags = 0,
321 unsigned short optionalflags = 0,
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000322 ArrayRef<unsigned> inds = None)
Dan Gohman1b849082009-09-07 23:54:19 +0000323 : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
Jay Foad0091fe82011-04-13 15:22:40 +0000324 operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {}
Dan Gohman1b849082009-09-07 23:54:19 +0000325 uint8_t opcode;
326 uint8_t subclassoptionaldata;
327 uint16_t subclassdata;
Owen Anderson9b676982009-08-04 22:55:26 +0000328 std::vector<Constant*> operands;
Jay Foad0091fe82011-04-13 15:22:40 +0000329 SmallVector<unsigned, 4> indices;
Owen Anderson9b676982009-08-04 22:55:26 +0000330 bool operator==(const ExprMapKeyType& that) const {
331 return this->opcode == that.opcode &&
Dan Gohman1b849082009-09-07 23:54:19 +0000332 this->subclassdata == that.subclassdata &&
333 this->subclassoptionaldata == that.subclassoptionaldata &&
Owen Anderson9b676982009-08-04 22:55:26 +0000334 this->operands == that.operands &&
335 this->indices == that.indices;
336 }
337 bool operator<(const ExprMapKeyType & that) const {
Benjamin Kramerb2f034b2014-03-03 19:58:30 +0000338 return std::tie(opcode, operands, subclassdata, subclassoptionaldata,
339 indices) <
340 std::tie(that.opcode, that.operands, that.subclassdata,
341 that.subclassoptionaldata, that.indices);
Owen Anderson9b676982009-08-04 22:55:26 +0000342 }
343
344 bool operator!=(const ExprMapKeyType& that) const {
345 return !(*this == that);
346 }
347};
348
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000349struct InlineAsmKeyType {
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000350 InlineAsmKeyType(StringRef AsmString,
351 StringRef Constraints, bool hasSideEffects,
Chad Rosierd8c76102012-09-05 19:00:49 +0000352 bool isAlignStack, InlineAsm::AsmDialect asmDialect)
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000353 : asm_string(AsmString), constraints(Constraints),
Chad Rosier8b3014e2012-09-04 22:46:24 +0000354 has_side_effects(hasSideEffects), is_align_stack(isAlignStack),
355 asm_dialect(asmDialect) {}
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000356 std::string asm_string;
357 std::string constraints;
358 bool has_side_effects;
359 bool is_align_stack;
Chad Rosierd8c76102012-09-05 19:00:49 +0000360 InlineAsm::AsmDialect asm_dialect;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000361 bool operator==(const InlineAsmKeyType& that) const {
362 return this->asm_string == that.asm_string &&
363 this->constraints == that.constraints &&
364 this->has_side_effects == that.has_side_effects &&
Chad Rosier8b3014e2012-09-04 22:46:24 +0000365 this->is_align_stack == that.is_align_stack &&
366 this->asm_dialect == that.asm_dialect;
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000367 }
368 bool operator<(const InlineAsmKeyType& that) const {
Benjamin Kramerb2f034b2014-03-03 19:58:30 +0000369 return std::tie(asm_string, constraints, has_side_effects, is_align_stack,
370 asm_dialect) <
371 std::tie(that.asm_string, that.constraints, that.has_side_effects,
372 that.is_align_stack, that.asm_dialect);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000373 }
374
375 bool operator!=(const InlineAsmKeyType& that) const {
376 return !(*this == that);
377 }
378};
379
Owen Anderson9b676982009-08-04 22:55:26 +0000380// The number of operands for each ConstantCreator::create method is
381// determined by the ConstantTraits template.
382// ConstantCreator - A class that is used to create constants by
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000383// ConstantUniqueMap*. This class should be partially specialized if there is
Owen Anderson9b676982009-08-04 22:55:26 +0000384// something strange that needs to be done to interface to the ctor for the
385// constant.
386//
387template<typename T, typename Alloc>
388struct ConstantTraits< std::vector<T, Alloc> > {
389 static unsigned uses(const std::vector<T, Alloc>& v) {
390 return v.size();
391 }
392};
393
Chris Lattner392be582010-02-12 20:49:41 +0000394template<>
395struct ConstantTraits<Constant *> {
396 static unsigned uses(Constant * const & v) {
397 return 1;
398 }
399};
400
Owen Anderson9b676982009-08-04 22:55:26 +0000401template<class ConstantClass, class TypeClass, class ValType>
402struct ConstantCreator {
Chris Lattner229907c2011-07-18 04:54:35 +0000403 static ConstantClass *create(TypeClass *Ty, const ValType &V) {
Owen Anderson9b676982009-08-04 22:55:26 +0000404 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
405 }
406};
407
Talin46e9b442012-02-05 20:54:10 +0000408template<class ConstantClass, class TypeClass>
409struct ConstantArrayCreator {
410 static ConstantClass *create(TypeClass *Ty, ArrayRef<Constant*> V) {
411 return new(V.size()) ConstantClass(Ty, V);
412 }
413};
414
Dan Gohmane4532f32009-09-15 15:58:07 +0000415template<class ConstantClass>
416struct ConstantKeyData {
417 typedef void ValType;
418 static ValType getValType(ConstantClass *C) {
419 llvm_unreachable("Unknown Constant type!");
Owen Anderson9b676982009-08-04 22:55:26 +0000420 }
421};
422
423template<>
424struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
Chris Lattner229907c2011-07-18 04:54:35 +0000425 static ConstantExpr *create(Type *Ty, const ExprMapKeyType &V,
Owen Anderson9b676982009-08-04 22:55:26 +0000426 unsigned short pred = 0) {
427 if (Instruction::isCast(V.opcode))
428 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
429 if ((V.opcode >= Instruction::BinaryOpsBegin &&
430 V.opcode < Instruction::BinaryOpsEnd))
Dan Gohman1b849082009-09-07 23:54:19 +0000431 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
432 V.subclassoptionaldata);
Owen Anderson9b676982009-08-04 22:55:26 +0000433 if (V.opcode == Instruction::Select)
434 return new SelectConstantExpr(V.operands[0], V.operands[1],
435 V.operands[2]);
436 if (V.opcode == Instruction::ExtractElement)
437 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
438 if (V.opcode == Instruction::InsertElement)
439 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
440 V.operands[2]);
441 if (V.opcode == Instruction::ShuffleVector)
442 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
443 V.operands[2]);
444 if (V.opcode == Instruction::InsertValue)
445 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
446 V.indices, Ty);
447 if (V.opcode == Instruction::ExtractValue)
448 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
449 if (V.opcode == Instruction::GetElementPtr) {
450 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Dan Gohman1b849082009-09-07 23:54:19 +0000451 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
452 V.subclassoptionaldata);
Owen Anderson9b676982009-08-04 22:55:26 +0000453 }
454
455 // The compare instructions are weird. We have to encode the predicate
456 // value and it is combined with the instruction opcode by multiplying
457 // the opcode by one hundred. We must decode this to get the predicate.
458 if (V.opcode == Instruction::ICmp)
Dan Gohman1b849082009-09-07 23:54:19 +0000459 return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
Owen Anderson9b676982009-08-04 22:55:26 +0000460 V.operands[0], V.operands[1]);
461 if (V.opcode == Instruction::FCmp)
Dan Gohman1b849082009-09-07 23:54:19 +0000462 return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
Owen Anderson9b676982009-08-04 22:55:26 +0000463 V.operands[0], V.operands[1]);
464 llvm_unreachable("Invalid ConstantExpr!");
Owen Anderson9b676982009-08-04 22:55:26 +0000465 }
466};
467
468template<>
Dan Gohmane4532f32009-09-15 15:58:07 +0000469struct ConstantKeyData<ConstantExpr> {
470 typedef ExprMapKeyType ValType;
471 static ValType getValType(ConstantExpr *CE) {
472 std::vector<Constant*> Operands;
473 Operands.reserve(CE->getNumOperands());
474 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
475 Operands.push_back(cast<Constant>(CE->getOperand(i)));
476 return ExprMapKeyType(CE->getOpcode(), Operands,
477 CE->isCompare() ? CE->getPredicate() : 0,
478 CE->getRawSubclassOptionalData(),
479 CE->hasIndices() ?
Jay Foad0091fe82011-04-13 15:22:40 +0000480 CE->getIndices() : ArrayRef<unsigned>());
Owen Anderson9b676982009-08-04 22:55:26 +0000481 }
482};
483
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000484template<>
485struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {
Chris Lattner229907c2011-07-18 04:54:35 +0000486 static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) {
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000487 return new InlineAsm(Ty, Key.asm_string, Key.constraints,
Chad Rosier8b3014e2012-09-04 22:46:24 +0000488 Key.has_side_effects, Key.is_align_stack,
489 Key.asm_dialect);
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000490 }
491};
492
493template<>
494struct ConstantKeyData<InlineAsm> {
495 typedef InlineAsmKeyType ValType;
496 static ValType getValType(InlineAsm *Asm) {
497 return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
Chad Rosier8b3014e2012-09-04 22:46:24 +0000498 Asm->hasSideEffects(), Asm->isAlignStack(),
499 Asm->getDialect());
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000500 }
501};
502
Jay Foadc365eea2011-06-22 08:50:06 +0000503template<class ValType, class ValRefType, class TypeClass, class ConstantClass,
Owen Anderson9b676982009-08-04 22:55:26 +0000504 bool HasLargeKey = false /*true for arrays and structs*/ >
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000505class ConstantUniqueMap {
Owen Anderson9b676982009-08-04 22:55:26 +0000506public:
Chris Lattner229907c2011-07-18 04:54:35 +0000507 typedef std::pair<TypeClass*, ValType> MapKey;
Dan Gohmane4532f32009-09-15 15:58:07 +0000508 typedef std::map<MapKey, ConstantClass *> MapTy;
509 typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy;
Owen Anderson9b676982009-08-04 22:55:26 +0000510private:
511 /// Map - This is the main map from the element descriptor to the Constants.
512 /// This is the primary way we avoid creating two of the same shape
513 /// constant.
514 MapTy Map;
515
516 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
517 /// from the constants to their element in Map. This is important for
518 /// removal of constants from the array, which would otherwise have to scan
519 /// through the map with very large keys.
520 InverseMapTy InverseMap;
521
Owen Anderson9b676982009-08-04 22:55:26 +0000522public:
Devang Patelc5aa8c62009-08-11 06:31:57 +0000523 typename MapTy::iterator map_begin() { return Map.begin(); }
Owen Anderson9b676982009-08-04 22:55:26 +0000524 typename MapTy::iterator map_end() { return Map.end(); }
Torok Edwind18e6682009-08-31 16:14:59 +0000525
526 void freeConstants() {
527 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
528 I != E; ++I) {
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000529 // Asserts that use_empty().
530 delete I->second;
Torok Edwind18e6682009-08-31 16:14:59 +0000531 }
532 }
Owen Anderson9b676982009-08-04 22:55:26 +0000533
534 /// InsertOrGetItem - Return an iterator for the specified element.
535 /// If the element exists in the map, the returned iterator points to the
536 /// entry and Exists=true. If not, the iterator points to the newly
537 /// inserted entry and returns Exists=false. Newly inserted entries have
538 /// I->second == 0, and should be filled in.
Dan Gohmane4532f32009-09-15 15:58:07 +0000539 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *>
Owen Anderson9b676982009-08-04 22:55:26 +0000540 &InsertVal,
541 bool &Exists) {
542 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
543 Exists = !IP.second;
544 return IP.first;
545 }
546
547private:
548 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
549 if (HasLargeKey) {
550 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
551 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
552 IMI->second->second == CP &&
553 "InverseMap corrupt!");
554 return IMI->second;
555 }
556
557 typename MapTy::iterator I =
Chris Lattner229907c2011-07-18 04:54:35 +0000558 Map.find(MapKey(static_cast<TypeClass*>(CP->getType()),
Dan Gohmane4532f32009-09-15 15:58:07 +0000559 ConstantKeyData<ConstantClass>::getValType(CP)));
Owen Anderson9b676982009-08-04 22:55:26 +0000560 if (I == Map.end() || I->second != CP) {
561 // FIXME: This should not use a linear scan. If this gets to be a
562 // performance problem, someone should look at this.
563 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
564 /* empty */;
565 }
566 return I;
567 }
Dan Gohmane4532f32009-09-15 15:58:07 +0000568
Chris Lattner229907c2011-07-18 04:54:35 +0000569 ConstantClass *Create(TypeClass *Ty, ValRefType V,
Owen Anderson9b676982009-08-04 22:55:26 +0000570 typename MapTy::iterator I) {
571 ConstantClass* Result =
572 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
573
574 assert(Result->getType() == Ty && "Type specified is not correct!");
575 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
576
577 if (HasLargeKey) // Remember the reverse mapping if needed.
578 InverseMap.insert(std::make_pair(Result, I));
579
Owen Anderson9b676982009-08-04 22:55:26 +0000580 return Result;
581 }
582public:
583
584 /// getOrCreate - Return the specified constant from the map, creating it if
585 /// necessary.
Chris Lattner229907c2011-07-18 04:54:35 +0000586 ConstantClass *getOrCreate(TypeClass *Ty, ValRefType V) {
Owen Anderson9b676982009-08-04 22:55:26 +0000587 MapKey Lookup(Ty, V);
Craig Toppere73658d2014-04-28 04:05:08 +0000588 ConstantClass* Result = nullptr;
Owen Anderson9b676982009-08-04 22:55:26 +0000589
590 typename MapTy::iterator I = Map.find(Lookup);
591 // Is it in the map?
592 if (I != Map.end())
Dan Gohmane4532f32009-09-15 15:58:07 +0000593 Result = I->second;
Owen Anderson9b676982009-08-04 22:55:26 +0000594
595 if (!Result) {
596 // If no preexisting value, create one now...
597 Result = Create(Ty, V, I);
598 }
599
600 return Result;
601 }
602
603 void remove(ConstantClass *CP) {
Owen Anderson9b676982009-08-04 22:55:26 +0000604 typename MapTy::iterator I = FindExistingElement(CP);
605 assert(I != Map.end() && "Constant not found in constant table!");
606 assert(I->second == CP && "Didn't find correct element?");
607
608 if (HasLargeKey) // Remember the reverse mapping if needed.
609 InverseMap.erase(CP);
Owen Anderson9b676982009-08-04 22:55:26 +0000610
611 Map.erase(I);
612 }
613
Owen Anderson9b676982009-08-04 22:55:26 +0000614 /// MoveConstantToNewSlot - If we are about to change C to be the element
615 /// specified by I, update our internal data structures to reflect this
616 /// fact.
Owen Anderson9b676982009-08-04 22:55:26 +0000617 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
618 // First, remove the old location of the specified constant in the map.
619 typename MapTy::iterator OldI = FindExistingElement(C);
620 assert(OldI != Map.end() && "Constant not found in constant table!");
621 assert(OldI->second == C && "Didn't find correct element?");
622
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000623 // Remove the old entry from the map.
Owen Anderson9b676982009-08-04 22:55:26 +0000624 Map.erase(OldI);
625
626 // Update the inverse map so that we know that this constant is now
627 // located at descriptor I.
628 if (HasLargeKey) {
629 assert(I->second == C && "Bad inversemap entry!");
630 InverseMap[C] = I;
631 }
632 }
Owen Anderson9b676982009-08-04 22:55:26 +0000633
634 void dump() const {
David Greene338a9032010-01-05 01:34:26 +0000635 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
Owen Anderson9b676982009-08-04 22:55:26 +0000636 }
637};
638
Talin46e9b442012-02-05 20:54:10 +0000639// Unique map for aggregate constants
640template<class TypeClass, class ConstantClass>
Benjamin Kramer079b96e2013-09-11 18:05:11 +0000641class ConstantAggrUniqueMap {
Talin46e9b442012-02-05 20:54:10 +0000642public:
643 typedef ArrayRef<Constant*> Operands;
644 typedef std::pair<TypeClass*, Operands> LookupKey;
645private:
646 struct MapInfo {
647 typedef DenseMapInfo<ConstantClass*> ConstantClassInfo;
648 typedef DenseMapInfo<Constant*> ConstantInfo;
649 typedef DenseMapInfo<TypeClass*> TypeClassInfo;
650 static inline ConstantClass* getEmptyKey() {
651 return ConstantClassInfo::getEmptyKey();
652 }
653 static inline ConstantClass* getTombstoneKey() {
654 return ConstantClassInfo::getTombstoneKey();
655 }
656 static unsigned getHashValue(const ConstantClass *CP) {
Chandler Carruthd4ba3eb2012-03-07 03:22:32 +0000657 SmallVector<Constant*, 8> CPOperands;
658 CPOperands.reserve(CP->getNumOperands());
Jay Foadcc5fd3e2012-03-06 10:43:52 +0000659 for (unsigned I = 0, E = CP->getNumOperands(); I < E; ++I)
Chandler Carruthd4ba3eb2012-03-07 03:22:32 +0000660 CPOperands.push_back(CP->getOperand(I));
661 return getHashValue(LookupKey(CP->getType(), CPOperands));
Talin46e9b442012-02-05 20:54:10 +0000662 }
663 static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
664 return LHS == RHS;
665 }
666 static unsigned getHashValue(const LookupKey &Val) {
Chandler Carruthd4ba3eb2012-03-07 03:22:32 +0000667 return hash_combine(Val.first, hash_combine_range(Val.second.begin(),
668 Val.second.end()));
Talin46e9b442012-02-05 20:54:10 +0000669 }
670 static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
671 if (RHS == getEmptyKey() || RHS == getTombstoneKey())
672 return false;
673 if (LHS.first != RHS->getType()
674 || LHS.second.size() != RHS->getNumOperands())
675 return false;
676 for (unsigned I = 0, E = RHS->getNumOperands(); I < E; ++I) {
677 if (LHS.second[I] != RHS->getOperand(I))
678 return false;
679 }
680 return true;
681 }
682 };
683public:
684 typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
685
686private:
687 /// Map - This is the main map from the element descriptor to the Constants.
688 /// This is the primary way we avoid creating two of the same shape
689 /// constant.
690 MapTy Map;
691
692public:
693 typename MapTy::iterator map_begin() { return Map.begin(); }
694 typename MapTy::iterator map_end() { return Map.end(); }
695
696 void freeConstants() {
697 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
698 I != E; ++I) {
699 // Asserts that use_empty().
700 delete I->first;
701 }
702 }
703
704private:
705 typename MapTy::iterator findExistingElement(ConstantClass *CP) {
706 return Map.find(CP);
707 }
708
709 ConstantClass *Create(TypeClass *Ty, Operands V, typename MapTy::iterator I) {
710 ConstantClass* Result =
711 ConstantArrayCreator<ConstantClass,TypeClass>::create(Ty, V);
712
713 assert(Result->getType() == Ty && "Type specified is not correct!");
714 Map[Result] = '\0';
715
716 return Result;
717 }
718public:
719
720 /// getOrCreate - Return the specified constant from the map, creating it if
721 /// necessary.
722 ConstantClass *getOrCreate(TypeClass *Ty, Operands V) {
723 LookupKey Lookup(Ty, V);
Craig Toppere73658d2014-04-28 04:05:08 +0000724 ConstantClass* Result = nullptr;
Talin46e9b442012-02-05 20:54:10 +0000725
726 typename MapTy::iterator I = Map.find_as(Lookup);
727 // Is it in the map?
728 if (I != Map.end())
729 Result = I->first;
730
731 if (!Result) {
732 // If no preexisting value, create one now...
733 Result = Create(Ty, V, I);
734 }
735
736 return Result;
737 }
738
739 /// Find the constant by lookup key.
740 typename MapTy::iterator find(LookupKey Lookup) {
741 return Map.find_as(Lookup);
742 }
743
744 /// Insert the constant into its proper slot.
745 void insert(ConstantClass *CP) {
746 Map[CP] = '\0';
747 }
748
749 /// Remove this constant from the map
750 void remove(ConstantClass *CP) {
751 typename MapTy::iterator I = findExistingElement(CP);
752 assert(I != Map.end() && "Constant not found in constant table!");
753 assert(I->first == CP && "Didn't find correct element?");
754 Map.erase(I);
755 }
756
757 void dump() const {
758 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
759 }
760};
761
Duncan P. N. Exon Smith25fb1102014-08-19 00:21:04 +0000762} // end namespace llvm
Owen Anderson9b676982009-08-04 22:55:26 +0000763
764#endif