blob: ffc673fac0daed82b9e6a1f0fda8a0e7c2552b75 [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
Jeffrey Yasskinade270e2010-03-21 20:37:19 +000018#include "llvm/InlineAsm.h"
Owen Anderson9b676982009-08-04 22:55:26 +000019#include "llvm/Instructions.h"
20#include "llvm/Operator.h"
David Greene338a9032010-01-05 01:34:26 +000021#include "llvm/Support/Debug.h"
Owen Anderson9b676982009-08-04 22:55:26 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattner34822f62009-08-23 04:44:11 +000023#include "llvm/Support/raw_ostream.h"
Owen Anderson9b676982009-08-04 22:55:26 +000024#include <map>
25
26namespace llvm {
27template<class ValType>
28struct ConstantTraits;
29
30/// UnaryConstantExpr - This class is private to Constants.cpp, and is used
31/// behind the scenes to implement unary constant exprs.
32class UnaryConstantExpr : public ConstantExpr {
33 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
34public:
35 // allocate space for exactly one operand
36 void *operator new(size_t s) {
37 return User::operator new(s, 1);
38 }
39 UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
40 : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
41 Op<0>() = C;
42 }
43 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
44};
45
46/// BinaryConstantExpr - This class is private to Constants.cpp, and is used
47/// behind the scenes to implement binary constant exprs.
48class BinaryConstantExpr : public ConstantExpr {
49 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
50public:
51 // allocate space for exactly two operands
52 void *operator new(size_t s) {
53 return User::operator new(s, 2);
54 }
Dan Gohman1b849082009-09-07 23:54:19 +000055 BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
56 unsigned Flags)
Owen Anderson9b676982009-08-04 22:55:26 +000057 : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
58 Op<0>() = C1;
59 Op<1>() = C2;
Dan Gohman1b849082009-09-07 23:54:19 +000060 SubclassOptionalData = Flags;
Owen Anderson9b676982009-08-04 22:55:26 +000061 }
62 /// Transparently provide more efficient getOperand methods.
63 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
64};
65
66/// SelectConstantExpr - This class is private to Constants.cpp, and is used
67/// behind the scenes to implement select constant exprs.
68class SelectConstantExpr : public ConstantExpr {
69 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
70public:
71 // allocate space for exactly three operands
72 void *operator new(size_t s) {
73 return User::operator new(s, 3);
74 }
75 SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
76 : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
77 Op<0>() = C1;
78 Op<1>() = C2;
79 Op<2>() = C3;
80 }
81 /// Transparently provide more efficient getOperand methods.
82 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
83};
84
85/// ExtractElementConstantExpr - This class is private to
86/// Constants.cpp, and is used behind the scenes to implement
87/// extractelement constant exprs.
88class ExtractElementConstantExpr : public ConstantExpr {
89 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
90public:
91 // allocate space for exactly two operands
92 void *operator new(size_t s) {
93 return User::operator new(s, 2);
94 }
95 ExtractElementConstantExpr(Constant *C1, Constant *C2)
96 : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(),
97 Instruction::ExtractElement, &Op<0>(), 2) {
98 Op<0>() = C1;
99 Op<1>() = C2;
100 }
101 /// Transparently provide more efficient getOperand methods.
102 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
103};
104
105/// InsertElementConstantExpr - This class is private to
106/// Constants.cpp, and is used behind the scenes to implement
107/// insertelement constant exprs.
108class InsertElementConstantExpr : public ConstantExpr {
109 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
110public:
111 // allocate space for exactly three operands
112 void *operator new(size_t s) {
113 return User::operator new(s, 3);
114 }
115 InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
116 : ConstantExpr(C1->getType(), Instruction::InsertElement,
117 &Op<0>(), 3) {
118 Op<0>() = C1;
119 Op<1>() = C2;
120 Op<2>() = C3;
121 }
122 /// Transparently provide more efficient getOperand methods.
123 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
124};
125
126/// ShuffleVectorConstantExpr - This class is private to
127/// Constants.cpp, and is used behind the scenes to implement
128/// shufflevector constant exprs.
129class ShuffleVectorConstantExpr : public ConstantExpr {
130 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
131public:
132 // allocate space for exactly three operands
133 void *operator new(size_t s) {
134 return User::operator new(s, 3);
135 }
136 ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
137 : ConstantExpr(VectorType::get(
138 cast<VectorType>(C1->getType())->getElementType(),
139 cast<VectorType>(C3->getType())->getNumElements()),
140 Instruction::ShuffleVector,
141 &Op<0>(), 3) {
142 Op<0>() = C1;
143 Op<1>() = C2;
144 Op<2>() = C3;
145 }
146 /// Transparently provide more efficient getOperand methods.
147 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
148};
149
150/// ExtractValueConstantExpr - This class is private to
151/// Constants.cpp, and is used behind the scenes to implement
152/// extractvalue constant exprs.
153class ExtractValueConstantExpr : public ConstantExpr {
154 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
155public:
156 // allocate space for exactly one operand
157 void *operator new(size_t s) {
158 return User::operator new(s, 1);
159 }
160 ExtractValueConstantExpr(Constant *Agg,
161 const SmallVector<unsigned, 4> &IdxList,
162 const Type *DestTy)
163 : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
164 Indices(IdxList) {
165 Op<0>() = Agg;
166 }
167
168 /// Indices - These identify which value to extract.
169 const SmallVector<unsigned, 4> Indices;
170
171 /// Transparently provide more efficient getOperand methods.
172 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
173};
174
175/// InsertValueConstantExpr - This class is private to
176/// Constants.cpp, and is used behind the scenes to implement
177/// insertvalue constant exprs.
178class InsertValueConstantExpr : public ConstantExpr {
179 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
180public:
181 // allocate space for exactly one operand
182 void *operator new(size_t s) {
183 return User::operator new(s, 2);
184 }
185 InsertValueConstantExpr(Constant *Agg, Constant *Val,
186 const SmallVector<unsigned, 4> &IdxList,
187 const Type *DestTy)
188 : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
189 Indices(IdxList) {
190 Op<0>() = Agg;
191 Op<1>() = Val;
192 }
193
194 /// Indices - These identify the position for the insertion.
195 const SmallVector<unsigned, 4> Indices;
196
197 /// Transparently provide more efficient getOperand methods.
198 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
199};
200
201
202/// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
203/// used behind the scenes to implement getelementpr constant exprs.
204class GetElementPtrConstantExpr : public ConstantExpr {
205 GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
206 const Type *DestTy);
207public:
208 static GetElementPtrConstantExpr *Create(Constant *C,
209 const std::vector<Constant*>&IdxList,
Dan Gohman1b849082009-09-07 23:54:19 +0000210 const Type *DestTy,
211 unsigned Flags) {
212 GetElementPtrConstantExpr *Result =
Owen Anderson9b676982009-08-04 22:55:26 +0000213 new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
Dan Gohman1b849082009-09-07 23:54:19 +0000214 Result->SubclassOptionalData = Flags;
215 return Result;
Owen Anderson9b676982009-08-04 22:55:26 +0000216 }
217 /// Transparently provide more efficient getOperand methods.
218 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
219};
220
221// CompareConstantExpr - This class is private to Constants.cpp, and is used
222// behind the scenes to implement ICmp and FCmp constant expressions. This is
223// needed in order to store the predicate value for these instructions.
224struct CompareConstantExpr : public ConstantExpr {
225 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
226 // allocate space for exactly two operands
227 void *operator new(size_t s) {
228 return User::operator new(s, 2);
229 }
230 unsigned short predicate;
231 CompareConstantExpr(const Type *ty, Instruction::OtherOps opc,
232 unsigned short pred, Constant* LHS, Constant* RHS)
233 : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
234 Op<0>() = LHS;
235 Op<1>() = RHS;
236 }
237 /// Transparently provide more efficient getOperand methods.
238 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
239};
240
241template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000242struct OperandTraits<UnaryConstantExpr> :
243 public FixedNumOperandTraits<UnaryConstantExpr, 1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000244};
245DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
246
247template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000248struct OperandTraits<BinaryConstantExpr> :
249 public FixedNumOperandTraits<BinaryConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000250};
251DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
252
253template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000254struct OperandTraits<SelectConstantExpr> :
255 public FixedNumOperandTraits<SelectConstantExpr, 3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000256};
257DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
258
259template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000260struct OperandTraits<ExtractElementConstantExpr> :
261 public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000262};
263DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
264
265template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000266struct OperandTraits<InsertElementConstantExpr> :
267 public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000268};
269DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
270
271template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000272struct OperandTraits<ShuffleVectorConstantExpr> :
273 public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
Owen Anderson9b676982009-08-04 22:55:26 +0000274};
275DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
276
277template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000278struct OperandTraits<ExtractValueConstantExpr> :
279 public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000280};
281DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
282
283template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000284struct OperandTraits<InsertValueConstantExpr> :
285 public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000286};
287DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
288
289template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000290struct OperandTraits<GetElementPtrConstantExpr> :
291 public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
Owen Anderson9b676982009-08-04 22:55:26 +0000292};
293
294DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
295
296
297template <>
Jay Foadc8adf5f2011-01-11 15:07:38 +0000298struct OperandTraits<CompareConstantExpr> :
299 public FixedNumOperandTraits<CompareConstantExpr, 2> {
Owen Anderson9b676982009-08-04 22:55:26 +0000300};
301DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
302
303struct ExprMapKeyType {
304 typedef SmallVector<unsigned, 4> IndexList;
305
306 ExprMapKeyType(unsigned opc,
307 const std::vector<Constant*> &ops,
Dan Gohman1b849082009-09-07 23:54:19 +0000308 unsigned short flags = 0,
309 unsigned short optionalflags = 0,
Owen Anderson9b676982009-08-04 22:55:26 +0000310 const IndexList &inds = IndexList())
Dan Gohman1b849082009-09-07 23:54:19 +0000311 : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
312 operands(ops), indices(inds) {}
313 uint8_t opcode;
314 uint8_t subclassoptionaldata;
315 uint16_t subclassdata;
Owen Anderson9b676982009-08-04 22:55:26 +0000316 std::vector<Constant*> operands;
317 IndexList indices;
318 bool operator==(const ExprMapKeyType& that) const {
319 return this->opcode == that.opcode &&
Dan Gohman1b849082009-09-07 23:54:19 +0000320 this->subclassdata == that.subclassdata &&
321 this->subclassoptionaldata == that.subclassoptionaldata &&
Owen Anderson9b676982009-08-04 22:55:26 +0000322 this->operands == that.operands &&
323 this->indices == that.indices;
324 }
325 bool operator<(const ExprMapKeyType & that) const {
Dan Gohman1b849082009-09-07 23:54:19 +0000326 if (this->opcode != that.opcode) return this->opcode < that.opcode;
327 if (this->operands != that.operands) return this->operands < that.operands;
328 if (this->subclassdata != that.subclassdata)
329 return this->subclassdata < that.subclassdata;
330 if (this->subclassoptionaldata != that.subclassoptionaldata)
331 return this->subclassoptionaldata < that.subclassoptionaldata;
332 if (this->indices != that.indices) return this->indices < that.indices;
333 return false;
Owen Anderson9b676982009-08-04 22:55:26 +0000334 }
335
336 bool operator!=(const ExprMapKeyType& that) const {
337 return !(*this == that);
338 }
339};
340
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000341struct InlineAsmKeyType {
342 InlineAsmKeyType(StringRef AsmString,
343 StringRef Constraints, bool hasSideEffects,
344 bool isAlignStack)
345 : asm_string(AsmString), constraints(Constraints),
346 has_side_effects(hasSideEffects), is_align_stack(isAlignStack) {}
347 std::string asm_string;
348 std::string constraints;
349 bool has_side_effects;
350 bool is_align_stack;
351 bool operator==(const InlineAsmKeyType& that) const {
352 return this->asm_string == that.asm_string &&
353 this->constraints == that.constraints &&
354 this->has_side_effects == that.has_side_effects &&
355 this->is_align_stack == that.is_align_stack;
356 }
357 bool operator<(const InlineAsmKeyType& that) const {
358 if (this->asm_string != that.asm_string)
359 return this->asm_string < that.asm_string;
360 if (this->constraints != that.constraints)
361 return this->constraints < that.constraints;
362 if (this->has_side_effects != that.has_side_effects)
363 return this->has_side_effects < that.has_side_effects;
364 if (this->is_align_stack != that.is_align_stack)
365 return this->is_align_stack < that.is_align_stack;
366 return false;
367 }
368
369 bool operator!=(const InlineAsmKeyType& that) const {
370 return !(*this == that);
371 }
372};
373
Owen Anderson9b676982009-08-04 22:55:26 +0000374// The number of operands for each ConstantCreator::create method is
375// determined by the ConstantTraits template.
376// ConstantCreator - A class that is used to create constants by
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000377// ConstantUniqueMap*. This class should be partially specialized if there is
Owen Anderson9b676982009-08-04 22:55:26 +0000378// something strange that needs to be done to interface to the ctor for the
379// constant.
380//
381template<typename T, typename Alloc>
382struct ConstantTraits< std::vector<T, Alloc> > {
383 static unsigned uses(const std::vector<T, Alloc>& v) {
384 return v.size();
385 }
386};
387
Chris Lattner392be582010-02-12 20:49:41 +0000388template<>
389struct ConstantTraits<Constant *> {
390 static unsigned uses(Constant * const & v) {
391 return 1;
392 }
393};
394
Owen Anderson9b676982009-08-04 22:55:26 +0000395template<class ConstantClass, class TypeClass, class ValType>
396struct ConstantCreator {
397 static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
398 return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
399 }
400};
401
Dan Gohmane4532f32009-09-15 15:58:07 +0000402template<class ConstantClass>
403struct ConstantKeyData {
404 typedef void ValType;
405 static ValType getValType(ConstantClass *C) {
406 llvm_unreachable("Unknown Constant type!");
Owen Anderson9b676982009-08-04 22:55:26 +0000407 }
408};
409
410template<>
411struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
412 static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V,
413 unsigned short pred = 0) {
414 if (Instruction::isCast(V.opcode))
415 return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
416 if ((V.opcode >= Instruction::BinaryOpsBegin &&
417 V.opcode < Instruction::BinaryOpsEnd))
Dan Gohman1b849082009-09-07 23:54:19 +0000418 return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
419 V.subclassoptionaldata);
Owen Anderson9b676982009-08-04 22:55:26 +0000420 if (V.opcode == Instruction::Select)
421 return new SelectConstantExpr(V.operands[0], V.operands[1],
422 V.operands[2]);
423 if (V.opcode == Instruction::ExtractElement)
424 return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
425 if (V.opcode == Instruction::InsertElement)
426 return new InsertElementConstantExpr(V.operands[0], V.operands[1],
427 V.operands[2]);
428 if (V.opcode == Instruction::ShuffleVector)
429 return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
430 V.operands[2]);
431 if (V.opcode == Instruction::InsertValue)
432 return new InsertValueConstantExpr(V.operands[0], V.operands[1],
433 V.indices, Ty);
434 if (V.opcode == Instruction::ExtractValue)
435 return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
436 if (V.opcode == Instruction::GetElementPtr) {
437 std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
Dan Gohman1b849082009-09-07 23:54:19 +0000438 return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
439 V.subclassoptionaldata);
Owen Anderson9b676982009-08-04 22:55:26 +0000440 }
441
442 // The compare instructions are weird. We have to encode the predicate
443 // value and it is combined with the instruction opcode by multiplying
444 // the opcode by one hundred. We must decode this to get the predicate.
445 if (V.opcode == Instruction::ICmp)
Dan Gohman1b849082009-09-07 23:54:19 +0000446 return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
Owen Anderson9b676982009-08-04 22:55:26 +0000447 V.operands[0], V.operands[1]);
448 if (V.opcode == Instruction::FCmp)
Dan Gohman1b849082009-09-07 23:54:19 +0000449 return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
Owen Anderson9b676982009-08-04 22:55:26 +0000450 V.operands[0], V.operands[1]);
451 llvm_unreachable("Invalid ConstantExpr!");
452 return 0;
453 }
454};
455
456template<>
Dan Gohmane4532f32009-09-15 15:58:07 +0000457struct ConstantKeyData<ConstantExpr> {
458 typedef ExprMapKeyType ValType;
459 static ValType getValType(ConstantExpr *CE) {
460 std::vector<Constant*> Operands;
461 Operands.reserve(CE->getNumOperands());
462 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
463 Operands.push_back(cast<Constant>(CE->getOperand(i)));
464 return ExprMapKeyType(CE->getOpcode(), Operands,
465 CE->isCompare() ? CE->getPredicate() : 0,
466 CE->getRawSubclassOptionalData(),
467 CE->hasIndices() ?
468 CE->getIndices() : SmallVector<unsigned, 4>());
Owen Anderson9b676982009-08-04 22:55:26 +0000469 }
470};
471
472// ConstantAggregateZero does not take extra "value" argument...
473template<class ValType>
474struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
475 static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
476 return new ConstantAggregateZero(Ty);
477 }
478};
479
480template<>
Dan Gohmane4532f32009-09-15 15:58:07 +0000481struct ConstantKeyData<ConstantVector> {
482 typedef std::vector<Constant*> ValType;
483 static ValType getValType(ConstantVector *CP) {
484 std::vector<Constant*> Elements;
485 Elements.reserve(CP->getNumOperands());
486 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
487 Elements.push_back(CP->getOperand(i));
488 return Elements;
Owen Anderson9b676982009-08-04 22:55:26 +0000489 }
490};
491
492template<>
Dan Gohmane4532f32009-09-15 15:58:07 +0000493struct ConstantKeyData<ConstantAggregateZero> {
494 typedef char ValType;
495 static ValType getValType(ConstantAggregateZero *C) {
496 return 0;
Owen Anderson9b676982009-08-04 22:55:26 +0000497 }
498};
499
500template<>
Dan Gohmane4532f32009-09-15 15:58:07 +0000501struct ConstantKeyData<ConstantArray> {
502 typedef std::vector<Constant*> ValType;
503 static ValType getValType(ConstantArray *CA) {
504 std::vector<Constant*> Elements;
505 Elements.reserve(CA->getNumOperands());
506 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
507 Elements.push_back(cast<Constant>(CA->getOperand(i)));
508 return Elements;
Owen Anderson9b676982009-08-04 22:55:26 +0000509 }
510};
511
512template<>
Dan Gohmane4532f32009-09-15 15:58:07 +0000513struct ConstantKeyData<ConstantStruct> {
514 typedef std::vector<Constant*> ValType;
515 static ValType getValType(ConstantStruct *CS) {
516 std::vector<Constant*> Elements;
517 Elements.reserve(CS->getNumOperands());
518 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
519 Elements.push_back(cast<Constant>(CS->getOperand(i)));
520 return Elements;
Owen Anderson9b676982009-08-04 22:55:26 +0000521 }
522};
523
524// ConstantPointerNull does not take extra "value" argument...
525template<class ValType>
526struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
527 static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
528 return new ConstantPointerNull(Ty);
529 }
530};
531
532template<>
Dan Gohmane4532f32009-09-15 15:58:07 +0000533struct ConstantKeyData<ConstantPointerNull> {
534 typedef char ValType;
535 static ValType getValType(ConstantPointerNull *C) {
536 return 0;
Owen Anderson9b676982009-08-04 22:55:26 +0000537 }
538};
539
540// UndefValue does not take extra "value" argument...
541template<class ValType>
542struct ConstantCreator<UndefValue, Type, ValType> {
543 static UndefValue *create(const Type *Ty, const ValType &V) {
544 return new UndefValue(Ty);
545 }
546};
547
548template<>
Dan Gohmane4532f32009-09-15 15:58:07 +0000549struct ConstantKeyData<UndefValue> {
550 typedef char ValType;
551 static ValType getValType(UndefValue *C) {
552 return 0;
Owen Anderson9b676982009-08-04 22:55:26 +0000553 }
554};
555
Jeffrey Yasskinade270e2010-03-21 20:37:19 +0000556template<>
557struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {
558 static InlineAsm *create(const PointerType *Ty, const InlineAsmKeyType &Key) {
559 return new InlineAsm(Ty, Key.asm_string, Key.constraints,
560 Key.has_side_effects, Key.is_align_stack);
561 }
562};
563
564template<>
565struct ConstantKeyData<InlineAsm> {
566 typedef InlineAsmKeyType ValType;
567 static ValType getValType(InlineAsm *Asm) {
568 return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
569 Asm->hasSideEffects(), Asm->isAlignStack());
570 }
571};
572
Owen Anderson9b676982009-08-04 22:55:26 +0000573template<class ValType, class TypeClass, class ConstantClass,
574 bool HasLargeKey = false /*true for arrays and structs*/ >
Jeffrey Yasskinf6ee7be2009-10-27 23:45:55 +0000575class ConstantUniqueMap : public AbstractTypeUser {
Owen Anderson9b676982009-08-04 22:55:26 +0000576public:
Dan Gohmane4532f32009-09-15 15:58:07 +0000577 typedef std::pair<const TypeClass*, ValType> MapKey;
578 typedef std::map<MapKey, ConstantClass *> MapTy;
579 typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy;
580 typedef std::map<const DerivedType*, typename MapTy::iterator>
581 AbstractTypeMapTy;
Owen Anderson9b676982009-08-04 22:55:26 +0000582private:
583 /// Map - This is the main map from the element descriptor to the Constants.
584 /// This is the primary way we avoid creating two of the same shape
585 /// constant.
586 MapTy Map;
587
588 /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
589 /// from the constants to their element in Map. This is important for
590 /// removal of constants from the array, which would otherwise have to scan
591 /// through the map with very large keys.
592 InverseMapTy InverseMap;
593
594 /// AbstractTypeMap - Map for abstract type constants.
595 ///
596 AbstractTypeMapTy AbstractTypeMap;
597
Owen Anderson9b676982009-08-04 22:55:26 +0000598public:
Devang Patelc5aa8c62009-08-11 06:31:57 +0000599 typename MapTy::iterator map_begin() { return Map.begin(); }
Owen Anderson9b676982009-08-04 22:55:26 +0000600 typename MapTy::iterator map_end() { return Map.end(); }
Torok Edwind18e6682009-08-31 16:14:59 +0000601
602 void freeConstants() {
603 for (typename MapTy::iterator I=Map.begin(), E=Map.end();
604 I != E; ++I) {
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000605 // Asserts that use_empty().
606 delete I->second;
Torok Edwind18e6682009-08-31 16:14:59 +0000607 }
608 }
Owen Anderson9b676982009-08-04 22:55:26 +0000609
610 /// InsertOrGetItem - Return an iterator for the specified element.
611 /// If the element exists in the map, the returned iterator points to the
612 /// entry and Exists=true. If not, the iterator points to the newly
613 /// inserted entry and returns Exists=false. Newly inserted entries have
614 /// I->second == 0, and should be filled in.
Dan Gohmane4532f32009-09-15 15:58:07 +0000615 typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *>
Owen Anderson9b676982009-08-04 22:55:26 +0000616 &InsertVal,
617 bool &Exists) {
618 std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
619 Exists = !IP.second;
620 return IP.first;
621 }
622
623private:
624 typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
625 if (HasLargeKey) {
626 typename InverseMapTy::iterator IMI = InverseMap.find(CP);
627 assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
628 IMI->second->second == CP &&
629 "InverseMap corrupt!");
630 return IMI->second;
631 }
632
633 typename MapTy::iterator I =
634 Map.find(MapKey(static_cast<const TypeClass*>(CP->getRawType()),
Dan Gohmane4532f32009-09-15 15:58:07 +0000635 ConstantKeyData<ConstantClass>::getValType(CP)));
Owen Anderson9b676982009-08-04 22:55:26 +0000636 if (I == Map.end() || I->second != CP) {
637 // FIXME: This should not use a linear scan. If this gets to be a
638 // performance problem, someone should look at this.
639 for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
640 /* empty */;
641 }
642 return I;
643 }
644
Dan Gohmane4532f32009-09-15 15:58:07 +0000645 void AddAbstractTypeUser(const Type *Ty, typename MapTy::iterator I) {
646 // If the type of the constant is abstract, make sure that an entry
647 // exists for it in the AbstractTypeMap.
648 if (Ty->isAbstract()) {
649 const DerivedType *DTy = static_cast<const DerivedType *>(Ty);
650 typename AbstractTypeMapTy::iterator TI = AbstractTypeMap.find(DTy);
651
652 if (TI == AbstractTypeMap.end()) {
653 // Add ourselves to the ATU list of the type.
654 cast<DerivedType>(DTy)->addAbstractTypeUser(this);
655
656 AbstractTypeMap.insert(TI, std::make_pair(DTy, I));
657 }
658 }
659 }
660
Owen Anderson9b676982009-08-04 22:55:26 +0000661 ConstantClass* Create(const TypeClass *Ty, const ValType &V,
662 typename MapTy::iterator I) {
663 ConstantClass* Result =
664 ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
665
666 assert(Result->getType() == Ty && "Type specified is not correct!");
667 I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
668
669 if (HasLargeKey) // Remember the reverse mapping if needed.
670 InverseMap.insert(std::make_pair(Result, I));
671
Dan Gohmane4532f32009-09-15 15:58:07 +0000672 AddAbstractTypeUser(Ty, I);
Owen Anderson9b676982009-08-04 22:55:26 +0000673
674 return Result;
675 }
676public:
677
678 /// getOrCreate - Return the specified constant from the map, creating it if
679 /// necessary.
680 ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
Owen Anderson9b676982009-08-04 22:55:26 +0000681 MapKey Lookup(Ty, V);
682 ConstantClass* Result = 0;
683
684 typename MapTy::iterator I = Map.find(Lookup);
685 // Is it in the map?
686 if (I != Map.end())
Dan Gohmane4532f32009-09-15 15:58:07 +0000687 Result = I->second;
Owen Anderson9b676982009-08-04 22:55:26 +0000688
689 if (!Result) {
690 // If no preexisting value, create one now...
691 Result = Create(Ty, V, I);
692 }
693
694 return Result;
695 }
696
Dan Gohmane4532f32009-09-15 15:58:07 +0000697 void UpdateAbstractTypeMap(const DerivedType *Ty,
698 typename MapTy::iterator I) {
699 assert(AbstractTypeMap.count(Ty) &&
700 "Abstract type not in AbstractTypeMap?");
701 typename MapTy::iterator &ATMEntryIt = AbstractTypeMap[Ty];
702 if (ATMEntryIt == I) {
703 // Yes, we are removing the representative entry for this type.
704 // See if there are any other entries of the same type.
705 typename MapTy::iterator TmpIt = ATMEntryIt;
706
707 // First check the entry before this one...
708 if (TmpIt != Map.begin()) {
709 --TmpIt;
710 if (TmpIt->first.first != Ty) // Not the same type, move back...
711 ++TmpIt;
712 }
713
714 // If we didn't find the same type, try to move forward...
715 if (TmpIt == ATMEntryIt) {
716 ++TmpIt;
717 if (TmpIt == Map.end() || TmpIt->first.first != Ty)
718 --TmpIt; // No entry afterwards with the same type
719 }
720
721 // If there is another entry in the map of the same abstract type,
722 // update the AbstractTypeMap entry now.
723 if (TmpIt != ATMEntryIt) {
724 ATMEntryIt = TmpIt;
725 } else {
726 // Otherwise, we are removing the last instance of this type
727 // from the table. Remove from the ATM, and from user list.
728 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
729 AbstractTypeMap.erase(Ty);
730 }
731 }
732 }
733
Owen Anderson9b676982009-08-04 22:55:26 +0000734 void remove(ConstantClass *CP) {
Owen Anderson9b676982009-08-04 22:55:26 +0000735 typename MapTy::iterator I = FindExistingElement(CP);
736 assert(I != Map.end() && "Constant not found in constant table!");
737 assert(I->second == CP && "Didn't find correct element?");
738
739 if (HasLargeKey) // Remember the reverse mapping if needed.
740 InverseMap.erase(CP);
741
742 // Now that we found the entry, make sure this isn't the entry that
743 // the AbstractTypeMap points to.
Dan Gohmane4532f32009-09-15 15:58:07 +0000744 const TypeClass *Ty = I->first.first;
745 if (Ty->isAbstract())
746 UpdateAbstractTypeMap(static_cast<const DerivedType *>(Ty), I);
Owen Anderson9b676982009-08-04 22:55:26 +0000747
748 Map.erase(I);
749 }
750
Owen Anderson9b676982009-08-04 22:55:26 +0000751 /// MoveConstantToNewSlot - If we are about to change C to be the element
752 /// specified by I, update our internal data structures to reflect this
753 /// fact.
Owen Anderson9b676982009-08-04 22:55:26 +0000754 void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
755 // First, remove the old location of the specified constant in the map.
756 typename MapTy::iterator OldI = FindExistingElement(C);
757 assert(OldI != Map.end() && "Constant not found in constant table!");
758 assert(OldI->second == C && "Didn't find correct element?");
759
760 // If this constant is the representative element for its abstract type,
761 // update the AbstractTypeMap so that the representative element is I.
Chris Lattner718da702010-07-17 06:13:52 +0000762 //
763 // This must use getRawType() because if the type is under refinement, we
764 // will get the refineAbstractType callback below, and we don't want to
765 // kick union find in on the constant.
766 if (C->getRawType()->isAbstract()) {
Owen Anderson9b676982009-08-04 22:55:26 +0000767 typename AbstractTypeMapTy::iterator ATI =
Chris Lattner718da702010-07-17 06:13:52 +0000768 AbstractTypeMap.find(cast<DerivedType>(C->getRawType()));
Owen Anderson9b676982009-08-04 22:55:26 +0000769 assert(ATI != AbstractTypeMap.end() &&
770 "Abstract type not in AbstractTypeMap?");
771 if (ATI->second == OldI)
772 ATI->second = I;
773 }
774
775 // Remove the old entry from the map.
776 Map.erase(OldI);
777
778 // Update the inverse map so that we know that this constant is now
779 // located at descriptor I.
780 if (HasLargeKey) {
781 assert(I->second == C && "Bad inversemap entry!");
782 InverseMap[C] = I;
783 }
784 }
785
786 void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
Dan Gohmane4532f32009-09-15 15:58:07 +0000787 typename AbstractTypeMapTy::iterator I = AbstractTypeMap.find(OldTy);
Owen Anderson9b676982009-08-04 22:55:26 +0000788
789 assert(I != AbstractTypeMap.end() &&
790 "Abstract type not in AbstractTypeMap?");
791
792 // Convert a constant at a time until the last one is gone. The last one
793 // leaving will remove() itself, causing the AbstractTypeMapEntry to be
794 // eliminated eventually.
795 do {
Dan Gohmane4532f32009-09-15 15:58:07 +0000796 ConstantClass *C = I->second->second;
797 MapKey Key(cast<TypeClass>(NewTy),
798 ConstantKeyData<ConstantClass>::getValType(C));
Owen Anderson9b676982009-08-04 22:55:26 +0000799
Dan Gohmane4532f32009-09-15 15:58:07 +0000800 std::pair<typename MapTy::iterator, bool> IP =
801 Map.insert(std::make_pair(Key, C));
802 if (IP.second) {
803 // The map didn't previously have an appropriate constant in the
804 // new type.
805
806 // Remove the old entry.
807 typename MapTy::iterator OldI =
808 Map.find(MapKey(cast<TypeClass>(OldTy), IP.first->first.second));
809 assert(OldI != Map.end() && "Constant not in map!");
810 UpdateAbstractTypeMap(OldTy, OldI);
811 Map.erase(OldI);
812
813 // Set the constant's type. This is done in place!
814 setType(C, NewTy);
815
816 // Update the inverse map so that we know that this constant is now
817 // located at descriptor I.
818 if (HasLargeKey)
819 InverseMap[C] = IP.first;
820
821 AddAbstractTypeUser(NewTy, IP.first);
822 } else {
823 // The map already had an appropriate constant in the new type, so
824 // there's no longer a need for the old constant.
825 C->uncheckedReplaceAllUsesWith(IP.first->second);
826 C->destroyConstant(); // This constant is now dead, destroy it.
827 }
828 I = AbstractTypeMap.find(OldTy);
Owen Anderson9b676982009-08-04 22:55:26 +0000829 } while (I != AbstractTypeMap.end());
830 }
831
832 // If the type became concrete without being refined to any other existing
833 // type, we just remove ourselves from the ATU list.
834 void typeBecameConcrete(const DerivedType *AbsTy) {
835 AbsTy->removeAbstractTypeUser(this);
836 }
837
838 void dump() const {
David Greene338a9032010-01-05 01:34:26 +0000839 DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
Owen Anderson9b676982009-08-04 22:55:26 +0000840 }
841};
842
843}
844
845#endif