[opaque pointer type] Explicit pointee type for GEPOperator/GEPConstantExpr.
[oota-llvm.git] / lib / IR / ConstantsContext.h
1 //===-- ConstantsContext.h - Constants-related Context Interals -----------===//
2 //
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_LIB_IR_CONSTANTSCONTEXT_H
16 #define LLVM_LIB_IR_CONSTANTSCONTEXT_H
17
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/Hashing.h"
20 #include "llvm/IR/InlineAsm.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Operator.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <map>
27 #include <tuple>
28
29 #define DEBUG_TYPE "ir"
30
31 namespace llvm {
32
33 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
34 /// behind the scenes to implement unary constant exprs.
35 class UnaryConstantExpr : public ConstantExpr {
36   void anchor() override;
37   void *operator new(size_t, unsigned) = delete;
38 public:
39   // allocate space for exactly one operand
40   void *operator new(size_t s) {
41     return User::operator new(s, 1);
42   }
43   UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
44     : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
45     Op<0>() = C;
46   }
47   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
48 };
49
50 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
51 /// behind the scenes to implement binary constant exprs.
52 class BinaryConstantExpr : public ConstantExpr {
53   void anchor() override;
54   void *operator new(size_t, unsigned) = delete;
55 public:
56   // allocate space for exactly two operands
57   void *operator new(size_t s) {
58     return User::operator new(s, 2);
59   }
60   BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
61                      unsigned Flags)
62     : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
63     Op<0>() = C1;
64     Op<1>() = C2;
65     SubclassOptionalData = Flags;
66   }
67   /// Transparently provide more efficient getOperand methods.
68   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
69 };
70
71 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
72 /// behind the scenes to implement select constant exprs.
73 class SelectConstantExpr : public ConstantExpr {
74   void anchor() override;
75   void *operator new(size_t, unsigned) = delete;
76 public:
77   // allocate space for exactly three operands
78   void *operator new(size_t s) {
79     return User::operator new(s, 3);
80   }
81   SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
82     : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
83     Op<0>() = C1;
84     Op<1>() = C2;
85     Op<2>() = C3;
86   }
87   /// Transparently provide more efficient getOperand methods.
88   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
89 };
90
91 /// ExtractElementConstantExpr - This class is private to
92 /// Constants.cpp, and is used behind the scenes to implement
93 /// extractelement constant exprs.
94 class ExtractElementConstantExpr : public ConstantExpr {
95   void anchor() override;
96   void *operator new(size_t, unsigned) = delete;
97 public:
98   // allocate space for exactly two operands
99   void *operator new(size_t s) {
100     return User::operator new(s, 2);
101   }
102   ExtractElementConstantExpr(Constant *C1, Constant *C2)
103     : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(), 
104                    Instruction::ExtractElement, &Op<0>(), 2) {
105     Op<0>() = C1;
106     Op<1>() = C2;
107   }
108   /// Transparently provide more efficient getOperand methods.
109   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
110 };
111
112 /// InsertElementConstantExpr - This class is private to
113 /// Constants.cpp, and is used behind the scenes to implement
114 /// insertelement constant exprs.
115 class InsertElementConstantExpr : public ConstantExpr {
116   void anchor() override;
117   void *operator new(size_t, unsigned) = delete;
118 public:
119   // allocate space for exactly three operands
120   void *operator new(size_t s) {
121     return User::operator new(s, 3);
122   }
123   InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
124     : ConstantExpr(C1->getType(), Instruction::InsertElement, 
125                    &Op<0>(), 3) {
126     Op<0>() = C1;
127     Op<1>() = C2;
128     Op<2>() = C3;
129   }
130   /// Transparently provide more efficient getOperand methods.
131   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
132 };
133
134 /// ShuffleVectorConstantExpr - This class is private to
135 /// Constants.cpp, and is used behind the scenes to implement
136 /// shufflevector constant exprs.
137 class ShuffleVectorConstantExpr : public ConstantExpr {
138   void anchor() override;
139   void *operator new(size_t, unsigned) = delete;
140 public:
141   // allocate space for exactly three operands
142   void *operator new(size_t s) {
143     return User::operator new(s, 3);
144   }
145   ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
146   : ConstantExpr(VectorType::get(
147                    cast<VectorType>(C1->getType())->getElementType(),
148                    cast<VectorType>(C3->getType())->getNumElements()),
149                  Instruction::ShuffleVector, 
150                  &Op<0>(), 3) {
151     Op<0>() = C1;
152     Op<1>() = C2;
153     Op<2>() = C3;
154   }
155   /// Transparently provide more efficient getOperand methods.
156   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
157 };
158
159 /// ExtractValueConstantExpr - This class is private to
160 /// Constants.cpp, and is used behind the scenes to implement
161 /// extractvalue constant exprs.
162 class ExtractValueConstantExpr : public ConstantExpr {
163   void anchor() override;
164   void *operator new(size_t, unsigned) = delete;
165 public:
166   // allocate space for exactly one operand
167   void *operator new(size_t s) {
168     return User::operator new(s, 1);
169   }
170   ExtractValueConstantExpr(Constant *Agg, ArrayRef<unsigned> IdxList,
171                            Type *DestTy)
172       : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
173         Indices(IdxList.begin(), IdxList.end()) {
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.
187 class InsertValueConstantExpr : public ConstantExpr {
188   void anchor() override;
189   void *operator new(size_t, unsigned) = delete;
190 public:
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                           ArrayRef<unsigned> IdxList, Type *DestTy)
197       : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
198         Indices(IdxList.begin(), IdxList.end()) {
199     Op<0>() = Agg;
200     Op<1>() = Val;
201   }
202
203   /// Indices - These identify the position for the insertion.
204   const SmallVector<unsigned, 4> Indices;
205
206   /// Transparently provide more efficient getOperand methods.
207   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
208 };
209
210
211 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
212 /// used behind the scenes to implement getelementpr constant exprs.
213 class GetElementPtrConstantExpr : public ConstantExpr {
214   Type *SrcElementTy;
215   void anchor() override;
216   GetElementPtrConstantExpr(Type *SrcElementTy, Constant *C,
217                             ArrayRef<Constant *> IdxList, Type *DestTy);
218
219 public:
220   static GetElementPtrConstantExpr *Create(Constant *C,
221                                            ArrayRef<Constant*> IdxList,
222                                            Type *DestTy,
223                                            unsigned Flags) {
224     return Create(
225         cast<PointerType>(C->getType()->getScalarType())->getElementType(), C,
226         IdxList, DestTy, Flags);
227   }
228   static GetElementPtrConstantExpr *Create(Type *SrcElementTy, Constant *C,
229                                            ArrayRef<Constant *> IdxList,
230                                            Type *DestTy, unsigned Flags) {
231     GetElementPtrConstantExpr *Result = new (IdxList.size() + 1)
232         GetElementPtrConstantExpr(SrcElementTy, C, IdxList, DestTy);
233     Result->SubclassOptionalData = Flags;
234     return Result;
235   }
236   Type *getSourceElementType() const;
237   /// Transparently provide more efficient getOperand methods.
238   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
239 };
240
241 // CompareConstantExpr - This class is private to Constants.cpp, and is used
242 // behind the scenes to implement ICmp and FCmp constant expressions. This is
243 // needed in order to store the predicate value for these instructions.
244 class CompareConstantExpr : public ConstantExpr {
245   void anchor() override;
246   void *operator new(size_t, unsigned) = delete;
247 public:
248   // allocate space for exactly two operands
249   void *operator new(size_t s) {
250     return User::operator new(s, 2);
251   }
252   unsigned short predicate;
253   CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
254                       unsigned short pred,  Constant* LHS, Constant* RHS)
255     : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
256     Op<0>() = LHS;
257     Op<1>() = RHS;
258   }
259   /// Transparently provide more efficient getOperand methods.
260   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
261 };
262
263 template <>
264 struct OperandTraits<UnaryConstantExpr> :
265   public FixedNumOperandTraits<UnaryConstantExpr, 1> {
266 };
267 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
268
269 template <>
270 struct OperandTraits<BinaryConstantExpr> :
271   public FixedNumOperandTraits<BinaryConstantExpr, 2> {
272 };
273 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
274
275 template <>
276 struct OperandTraits<SelectConstantExpr> :
277   public FixedNumOperandTraits<SelectConstantExpr, 3> {
278 };
279 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
280
281 template <>
282 struct OperandTraits<ExtractElementConstantExpr> :
283   public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
284 };
285 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
286
287 template <>
288 struct OperandTraits<InsertElementConstantExpr> :
289   public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
290 };
291 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
292
293 template <>
294 struct OperandTraits<ShuffleVectorConstantExpr> :
295     public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
296 };
297 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
298
299 template <>
300 struct OperandTraits<ExtractValueConstantExpr> :
301   public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
302 };
303 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
304
305 template <>
306 struct OperandTraits<InsertValueConstantExpr> :
307   public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
308 };
309 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
310
311 template <>
312 struct OperandTraits<GetElementPtrConstantExpr> :
313   public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
314 };
315
316 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
317
318
319 template <>
320 struct OperandTraits<CompareConstantExpr> :
321   public FixedNumOperandTraits<CompareConstantExpr, 2> {
322 };
323 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
324
325 template <class ConstantClass> struct ConstantAggrKeyType;
326 struct InlineAsmKeyType;
327 struct ConstantExprKeyType;
328
329 template <class ConstantClass> struct ConstantInfo;
330 template <> struct ConstantInfo<ConstantExpr> {
331   typedef ConstantExprKeyType ValType;
332   typedef Type TypeClass;
333 };
334 template <> struct ConstantInfo<InlineAsm> {
335   typedef InlineAsmKeyType ValType;
336   typedef PointerType TypeClass;
337 };
338 template <> struct ConstantInfo<ConstantArray> {
339   typedef ConstantAggrKeyType<ConstantArray> ValType;
340   typedef ArrayType TypeClass;
341 };
342 template <> struct ConstantInfo<ConstantStruct> {
343   typedef ConstantAggrKeyType<ConstantStruct> ValType;
344   typedef StructType TypeClass;
345 };
346 template <> struct ConstantInfo<ConstantVector> {
347   typedef ConstantAggrKeyType<ConstantVector> ValType;
348   typedef VectorType TypeClass;
349 };
350
351 template <class ConstantClass> struct ConstantAggrKeyType {
352   ArrayRef<Constant *> Operands;
353   ConstantAggrKeyType(ArrayRef<Constant *> Operands) : Operands(Operands) {}
354   ConstantAggrKeyType(ArrayRef<Constant *> Operands, const ConstantClass *)
355       : Operands(Operands) {}
356   ConstantAggrKeyType(const ConstantClass *C,
357                       SmallVectorImpl<Constant *> &Storage) {
358     assert(Storage.empty() && "Expected empty storage");
359     for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
360       Storage.push_back(C->getOperand(I));
361     Operands = Storage;
362   }
363
364   bool operator==(const ConstantAggrKeyType &X) const {
365     return Operands == X.Operands;
366   }
367   bool operator==(const ConstantClass *C) const {
368     if (Operands.size() != C->getNumOperands())
369       return false;
370     for (unsigned I = 0, E = Operands.size(); I != E; ++I)
371       if (Operands[I] != C->getOperand(I))
372         return false;
373     return true;
374   }
375   unsigned getHash() const {
376     return hash_combine_range(Operands.begin(), Operands.end());
377   }
378
379   typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
380   ConstantClass *create(TypeClass *Ty) const {
381     return new (Operands.size()) ConstantClass(Ty, Operands);
382   }
383 };
384
385 struct InlineAsmKeyType {
386   StringRef AsmString;
387   StringRef Constraints;
388   bool HasSideEffects;
389   bool IsAlignStack;
390   InlineAsm::AsmDialect AsmDialect;
391
392   InlineAsmKeyType(StringRef AsmString, StringRef Constraints,
393                    bool HasSideEffects, bool IsAlignStack,
394                    InlineAsm::AsmDialect AsmDialect)
395       : AsmString(AsmString), Constraints(Constraints),
396         HasSideEffects(HasSideEffects), IsAlignStack(IsAlignStack),
397         AsmDialect(AsmDialect) {}
398   InlineAsmKeyType(const InlineAsm *Asm, SmallVectorImpl<Constant *> &)
399       : AsmString(Asm->getAsmString()), Constraints(Asm->getConstraintString()),
400         HasSideEffects(Asm->hasSideEffects()),
401         IsAlignStack(Asm->isAlignStack()), AsmDialect(Asm->getDialect()) {}
402
403   bool operator==(const InlineAsmKeyType &X) const {
404     return HasSideEffects == X.HasSideEffects &&
405            IsAlignStack == X.IsAlignStack && AsmDialect == X.AsmDialect &&
406            AsmString == X.AsmString && Constraints == X.Constraints;
407   }
408   bool operator==(const InlineAsm *Asm) const {
409     return HasSideEffects == Asm->hasSideEffects() &&
410            IsAlignStack == Asm->isAlignStack() &&
411            AsmDialect == Asm->getDialect() &&
412            AsmString == Asm->getAsmString() &&
413            Constraints == Asm->getConstraintString();
414   }
415   unsigned getHash() const {
416     return hash_combine(AsmString, Constraints, HasSideEffects, IsAlignStack,
417                         AsmDialect);
418   }
419
420   typedef ConstantInfo<InlineAsm>::TypeClass TypeClass;
421   InlineAsm *create(TypeClass *Ty) const {
422     return new InlineAsm(Ty, AsmString, Constraints, HasSideEffects,
423                          IsAlignStack, AsmDialect);
424   }
425 };
426
427 struct ConstantExprKeyType {
428   uint8_t Opcode;
429   uint8_t SubclassOptionalData;
430   uint16_t SubclassData;
431   ArrayRef<Constant *> Ops;
432   ArrayRef<unsigned> Indexes;
433   Type *ExplicitTy;
434
435   ConstantExprKeyType(unsigned Opcode, ArrayRef<Constant *> Ops,
436                       unsigned short SubclassData = 0,
437                       unsigned short SubclassOptionalData = 0,
438                       ArrayRef<unsigned> Indexes = None,
439                       Type *ExplicitTy = nullptr)
440       : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData),
441         SubclassData(SubclassData), Ops(Ops), Indexes(Indexes),
442         ExplicitTy(ExplicitTy) {}
443   ConstantExprKeyType(ArrayRef<Constant *> Operands, const ConstantExpr *CE)
444       : Opcode(CE->getOpcode()),
445         SubclassOptionalData(CE->getRawSubclassOptionalData()),
446         SubclassData(CE->isCompare() ? CE->getPredicate() : 0), Ops(Operands),
447         Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {}
448   ConstantExprKeyType(const ConstantExpr *CE,
449                       SmallVectorImpl<Constant *> &Storage)
450       : Opcode(CE->getOpcode()),
451         SubclassOptionalData(CE->getRawSubclassOptionalData()),
452         SubclassData(CE->isCompare() ? CE->getPredicate() : 0),
453         Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {
454     assert(Storage.empty() && "Expected empty storage");
455     for (unsigned I = 0, E = CE->getNumOperands(); I != E; ++I)
456       Storage.push_back(CE->getOperand(I));
457     Ops = Storage;
458   }
459
460   bool operator==(const ConstantExprKeyType &X) const {
461     return Opcode == X.Opcode && SubclassData == X.SubclassData &&
462            SubclassOptionalData == X.SubclassOptionalData && Ops == X.Ops &&
463            Indexes == X.Indexes;
464   }
465
466   bool operator==(const ConstantExpr *CE) const {
467     if (Opcode != CE->getOpcode())
468       return false;
469     if (SubclassOptionalData != CE->getRawSubclassOptionalData())
470       return false;
471     if (Ops.size() != CE->getNumOperands())
472       return false;
473     if (SubclassData != (CE->isCompare() ? CE->getPredicate() : 0))
474       return false;
475     for (unsigned I = 0, E = Ops.size(); I != E; ++I)
476       if (Ops[I] != CE->getOperand(I))
477         return false;
478     if (Indexes != (CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()))
479       return false;
480     return true;
481   }
482
483   unsigned getHash() const {
484     return hash_combine(Opcode, SubclassOptionalData, SubclassData,
485                         hash_combine_range(Ops.begin(), Ops.end()),
486                         hash_combine_range(Indexes.begin(), Indexes.end()));
487   }
488
489   typedef ConstantInfo<ConstantExpr>::TypeClass TypeClass;
490   ConstantExpr *create(TypeClass *Ty) const {
491     switch (Opcode) {
492     default:
493       if (Instruction::isCast(Opcode))
494         return new UnaryConstantExpr(Opcode, Ops[0], Ty);
495       if ((Opcode >= Instruction::BinaryOpsBegin &&
496            Opcode < Instruction::BinaryOpsEnd))
497         return new BinaryConstantExpr(Opcode, Ops[0], Ops[1],
498                                       SubclassOptionalData);
499       llvm_unreachable("Invalid ConstantExpr!");
500     case Instruction::Select:
501       return new SelectConstantExpr(Ops[0], Ops[1], Ops[2]);
502     case Instruction::ExtractElement:
503       return new ExtractElementConstantExpr(Ops[0], Ops[1]);
504     case Instruction::InsertElement:
505       return new InsertElementConstantExpr(Ops[0], Ops[1], Ops[2]);
506     case Instruction::ShuffleVector:
507       return new ShuffleVectorConstantExpr(Ops[0], Ops[1], Ops[2]);
508     case Instruction::InsertValue:
509       return new InsertValueConstantExpr(Ops[0], Ops[1], Indexes, Ty);
510     case Instruction::ExtractValue:
511       return new ExtractValueConstantExpr(Ops[0], Indexes, Ty);
512     case Instruction::GetElementPtr:
513       return GetElementPtrConstantExpr::Create(
514           ExplicitTy ? ExplicitTy
515                      : cast<PointerType>(Ops[0]->getType()->getScalarType())
516                            ->getElementType(),
517           Ops[0], Ops.slice(1), Ty, SubclassOptionalData);
518     case Instruction::ICmp:
519       return new CompareConstantExpr(Ty, Instruction::ICmp, SubclassData,
520                                      Ops[0], Ops[1]);
521     case Instruction::FCmp:
522       return new CompareConstantExpr(Ty, Instruction::FCmp, SubclassData,
523                                      Ops[0], Ops[1]);
524     }
525   }
526 };
527
528 template <class ConstantClass> class ConstantUniqueMap {
529 public:
530   typedef typename ConstantInfo<ConstantClass>::ValType ValType;
531   typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
532   typedef std::pair<TypeClass *, ValType> LookupKey;
533
534 private:
535   struct MapInfo {
536     typedef DenseMapInfo<ConstantClass *> ConstantClassInfo;
537     static inline ConstantClass *getEmptyKey() {
538       return ConstantClassInfo::getEmptyKey();
539     }
540     static inline ConstantClass *getTombstoneKey() {
541       return ConstantClassInfo::getTombstoneKey();
542     }
543     static unsigned getHashValue(const ConstantClass *CP) {
544       SmallVector<Constant *, 8> Storage;
545       return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage)));
546     }
547     static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
548       return LHS == RHS;
549     }
550     static unsigned getHashValue(const LookupKey &Val) {
551       return hash_combine(Val.first, Val.second.getHash());
552     }
553     static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
554       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
555         return false;
556       if (LHS.first != RHS->getType())
557         return false;
558       return LHS.second == RHS;
559     }
560   };
561
562 public:
563   typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
564
565 private:
566   MapTy Map;
567
568 public:
569   typename MapTy::iterator map_begin() { return Map.begin(); }
570   typename MapTy::iterator map_end() { return Map.end(); }
571
572   void freeConstants() {
573     for (auto &I : Map)
574       // Asserts that use_empty().
575       delete I.first;
576   }
577
578 private:
579   ConstantClass *create(TypeClass *Ty, ValType V) {
580     ConstantClass *Result = V.create(Ty);
581
582     assert(Result->getType() == Ty && "Type specified is not correct!");
583     insert(Result);
584
585     return Result;
586   }
587
588 public:
589   /// Return the specified constant from the map, creating it if necessary.
590   ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
591     LookupKey Lookup(Ty, V);
592     ConstantClass *Result = nullptr;
593
594     auto I = find(Lookup);
595     if (I == Map.end())
596       Result = create(Ty, V);
597     else
598       Result = I->first;
599     assert(Result && "Unexpected nullptr");
600
601     return Result;
602   }
603
604   /// Find the constant by lookup key.
605   typename MapTy::iterator find(LookupKey Lookup) {
606     return Map.find_as(Lookup);
607   }
608
609   /// Insert the constant into its proper slot.
610   void insert(ConstantClass *CP) { Map[CP] = '\0'; }
611
612   /// Remove this constant from the map
613   void remove(ConstantClass *CP) {
614     typename MapTy::iterator I = Map.find(CP);
615     assert(I != Map.end() && "Constant not found in constant table!");
616     assert(I->first == CP && "Didn't find correct element?");
617     Map.erase(I);
618   }
619
620   ConstantClass *replaceOperandsInPlace(ArrayRef<Constant *> Operands,
621                                         ConstantClass *CP, Value *From,
622                                         Constant *To, unsigned NumUpdated = 0,
623                                         unsigned OperandNo = ~0u) {
624     LookupKey Lookup(CP->getType(), ValType(Operands, CP));
625     auto I = find(Lookup);
626     if (I != Map.end())
627       return I->first;
628
629     // Update to the new value.  Optimize for the case when we have a single
630     // operand that we're changing, but handle bulk updates efficiently.
631     remove(CP);
632     if (NumUpdated == 1) {
633       assert(OperandNo < CP->getNumOperands() && "Invalid index");
634       assert(CP->getOperand(OperandNo) != To && "I didn't contain From!");
635       CP->setOperand(OperandNo, To);
636     } else {
637       for (unsigned I = 0, E = CP->getNumOperands(); I != E; ++I)
638         if (CP->getOperand(I) == From)
639           CP->setOperand(I, To);
640     }
641     insert(CP);
642     return nullptr;
643   }
644
645   void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); }
646 };
647
648 } // end namespace llvm
649
650 #endif