Reapply r215966, r215965, r215964, r215963, r215960, r215959, r215958, and r215957
[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) LLVM_DELETED_FUNCTION;
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) LLVM_DELETED_FUNCTION;
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) LLVM_DELETED_FUNCTION;
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) LLVM_DELETED_FUNCTION;
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) LLVM_DELETED_FUNCTION;
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) LLVM_DELETED_FUNCTION;
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) LLVM_DELETED_FUNCTION;
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) LLVM_DELETED_FUNCTION;
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   void anchor() override;
215   GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList,
216                             Type *DestTy);
217 public:
218   static GetElementPtrConstantExpr *Create(Constant *C,
219                                            ArrayRef<Constant*> IdxList,
220                                            Type *DestTy,
221                                            unsigned Flags) {
222     GetElementPtrConstantExpr *Result =
223       new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
224     Result->SubclassOptionalData = Flags;
225     return Result;
226   }
227   /// Transparently provide more efficient getOperand methods.
228   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
229 };
230
231 // CompareConstantExpr - This class is private to Constants.cpp, and is used
232 // behind the scenes to implement ICmp and FCmp constant expressions. This is
233 // needed in order to store the predicate value for these instructions.
234 class CompareConstantExpr : public ConstantExpr {
235   void anchor() override;
236   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
237 public:
238   // allocate space for exactly two operands
239   void *operator new(size_t s) {
240     return User::operator new(s, 2);
241   }
242   unsigned short predicate;
243   CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
244                       unsigned short pred,  Constant* LHS, Constant* RHS)
245     : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
246     Op<0>() = LHS;
247     Op<1>() = RHS;
248   }
249   /// Transparently provide more efficient getOperand methods.
250   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
251 };
252
253 template <>
254 struct OperandTraits<UnaryConstantExpr> :
255   public FixedNumOperandTraits<UnaryConstantExpr, 1> {
256 };
257 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
258
259 template <>
260 struct OperandTraits<BinaryConstantExpr> :
261   public FixedNumOperandTraits<BinaryConstantExpr, 2> {
262 };
263 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
264
265 template <>
266 struct OperandTraits<SelectConstantExpr> :
267   public FixedNumOperandTraits<SelectConstantExpr, 3> {
268 };
269 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
270
271 template <>
272 struct OperandTraits<ExtractElementConstantExpr> :
273   public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
274 };
275 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
276
277 template <>
278 struct OperandTraits<InsertElementConstantExpr> :
279   public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
280 };
281 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
282
283 template <>
284 struct OperandTraits<ShuffleVectorConstantExpr> :
285     public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
286 };
287 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
288
289 template <>
290 struct OperandTraits<ExtractValueConstantExpr> :
291   public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
292 };
293 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
294
295 template <>
296 struct OperandTraits<InsertValueConstantExpr> :
297   public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
298 };
299 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
300
301 template <>
302 struct OperandTraits<GetElementPtrConstantExpr> :
303   public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
304 };
305
306 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
307
308
309 template <>
310 struct OperandTraits<CompareConstantExpr> :
311   public FixedNumOperandTraits<CompareConstantExpr, 2> {
312 };
313 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
314
315 template <class ConstantClass> struct ConstantAggrKeyType;
316 struct InlineAsmKeyType;
317 struct ConstantExprKeyType;
318
319 template <class ConstantClass> struct ConstantInfo;
320 template <> struct ConstantInfo<ConstantExpr> {
321   typedef ConstantExprKeyType ValType;
322   typedef Type TypeClass;
323 };
324 template <> struct ConstantInfo<InlineAsm> {
325   typedef InlineAsmKeyType ValType;
326   typedef PointerType TypeClass;
327 };
328 template <> struct ConstantInfo<ConstantArray> {
329   typedef ConstantAggrKeyType<ConstantArray> ValType;
330   typedef ArrayType TypeClass;
331 };
332 template <> struct ConstantInfo<ConstantStruct> {
333   typedef ConstantAggrKeyType<ConstantStruct> ValType;
334   typedef StructType TypeClass;
335 };
336 template <> struct ConstantInfo<ConstantVector> {
337   typedef ConstantAggrKeyType<ConstantVector> ValType;
338   typedef VectorType TypeClass;
339 };
340
341 template <class ConstantClass> struct ConstantAggrKeyType {
342   ArrayRef<Constant *> Operands;
343   ConstantAggrKeyType(ArrayRef<Constant *> Operands) : Operands(Operands) {}
344   ConstantAggrKeyType(const ConstantClass *C,
345                       SmallVectorImpl<Constant *> &Storage) {
346     assert(Storage.empty() && "Expected empty storage");
347     for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
348       Storage.push_back(C->getOperand(I));
349     Operands = Storage;
350   }
351
352   bool operator==(const ConstantAggrKeyType &X) const {
353     return Operands == X.Operands;
354   }
355   bool operator==(const ConstantClass *C) const {
356     if (Operands.size() != C->getNumOperands())
357       return false;
358     for (unsigned I = 0, E = Operands.size(); I != E; ++I)
359       if (Operands[I] != C->getOperand(I))
360         return false;
361     return true;
362   }
363   unsigned getHash() const {
364     return hash_combine_range(Operands.begin(), Operands.end());
365   }
366
367   typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
368   ConstantClass *create(TypeClass *Ty) const {
369     return new (Operands.size()) ConstantClass(Ty, Operands);
370   }
371 };
372
373 struct InlineAsmKeyType {
374   StringRef AsmString;
375   StringRef Constraints;
376   bool HasSideEffects;
377   bool IsAlignStack;
378   InlineAsm::AsmDialect AsmDialect;
379
380   InlineAsmKeyType(StringRef AsmString, StringRef Constraints,
381                    bool HasSideEffects, bool IsAlignStack,
382                    InlineAsm::AsmDialect AsmDialect)
383       : AsmString(AsmString), Constraints(Constraints),
384         HasSideEffects(HasSideEffects), IsAlignStack(IsAlignStack),
385         AsmDialect(AsmDialect) {}
386   InlineAsmKeyType(const InlineAsm *Asm, SmallVectorImpl<Constant *> &)
387       : AsmString(Asm->getAsmString()), Constraints(Asm->getConstraintString()),
388         HasSideEffects(Asm->hasSideEffects()),
389         IsAlignStack(Asm->isAlignStack()), AsmDialect(Asm->getDialect()) {}
390
391   bool operator==(const InlineAsmKeyType &X) const {
392     return HasSideEffects == X.HasSideEffects &&
393            IsAlignStack == X.IsAlignStack && AsmDialect == X.AsmDialect &&
394            AsmString == X.AsmString && Constraints == X.Constraints;
395   }
396   bool operator==(const InlineAsm *Asm) const {
397     return HasSideEffects == Asm->hasSideEffects() &&
398            IsAlignStack == Asm->isAlignStack() &&
399            AsmDialect == Asm->getDialect() &&
400            AsmString == Asm->getAsmString() &&
401            Constraints == Asm->getConstraintString();
402   }
403   unsigned getHash() const {
404     return hash_combine(AsmString, Constraints, HasSideEffects, IsAlignStack,
405                         AsmDialect);
406   }
407
408   typedef ConstantInfo<InlineAsm>::TypeClass TypeClass;
409   InlineAsm *create(TypeClass *Ty) const {
410     return new InlineAsm(Ty, AsmString, Constraints, HasSideEffects,
411                          IsAlignStack, AsmDialect);
412   }
413 };
414
415 struct ConstantExprKeyType {
416   uint8_t Opcode;
417   uint8_t SubclassOptionalData;
418   uint16_t SubclassData;
419   ArrayRef<Constant *> Ops;
420   ArrayRef<unsigned> Indexes;
421
422   ConstantExprKeyType(unsigned Opcode, ArrayRef<Constant *> Ops,
423                       unsigned short SubclassData = 0,
424                       unsigned short SubclassOptionalData = 0,
425                       ArrayRef<unsigned> Indexes = None)
426       : Opcode(Opcode), SubclassOptionalData(SubclassOptionalData),
427         SubclassData(SubclassData), Ops(Ops), Indexes(Indexes) {}
428   ConstantExprKeyType(const ConstantExpr *CE,
429                       SmallVectorImpl<Constant *> &Storage)
430       : Opcode(CE->getOpcode()),
431         SubclassOptionalData(CE->getRawSubclassOptionalData()),
432         SubclassData(CE->isCompare() ? CE->getPredicate() : 0),
433         Indexes(CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()) {
434     assert(Storage.empty() && "Expected empty storage");
435     for (unsigned I = 0, E = CE->getNumOperands(); I != E; ++I)
436       Storage.push_back(CE->getOperand(I));
437     Ops = Storage;
438   }
439
440   bool operator==(const ConstantExprKeyType &X) const {
441     return Opcode == X.Opcode && SubclassData == X.SubclassData &&
442            SubclassOptionalData == X.SubclassOptionalData && Ops == X.Ops &&
443            Indexes == X.Indexes;
444   }
445
446   bool operator==(const ConstantExpr *CE) const {
447     if (Opcode != CE->getOpcode())
448       return false;
449     if (SubclassOptionalData != CE->getRawSubclassOptionalData())
450       return false;
451     if (Ops.size() != CE->getNumOperands())
452       return false;
453     if (SubclassData != (CE->isCompare() ? CE->getPredicate() : 0))
454       return false;
455     for (unsigned I = 0, E = Ops.size(); I != E; ++I)
456       if (Ops[I] != CE->getOperand(I))
457         return false;
458     if (Indexes != (CE->hasIndices() ? CE->getIndices() : ArrayRef<unsigned>()))
459       return false;
460     return true;
461   }
462
463   unsigned getHash() const {
464     return hash_combine(Opcode, SubclassOptionalData, SubclassData,
465                         hash_combine_range(Ops.begin(), Ops.end()),
466                         hash_combine_range(Indexes.begin(), Indexes.end()));
467   }
468
469   typedef ConstantInfo<ConstantExpr>::TypeClass TypeClass;
470   ConstantExpr *create(TypeClass *Ty) const {
471     switch (Opcode) {
472     default:
473       if (Instruction::isCast(Opcode))
474         return new UnaryConstantExpr(Opcode, Ops[0], Ty);
475       if ((Opcode >= Instruction::BinaryOpsBegin &&
476            Opcode < Instruction::BinaryOpsEnd))
477         return new BinaryConstantExpr(Opcode, Ops[0], Ops[1],
478                                       SubclassOptionalData);
479       llvm_unreachable("Invalid ConstantExpr!");
480     case Instruction::Select:
481       return new SelectConstantExpr(Ops[0], Ops[1], Ops[2]);
482     case Instruction::ExtractElement:
483       return new ExtractElementConstantExpr(Ops[0], Ops[1]);
484     case Instruction::InsertElement:
485       return new InsertElementConstantExpr(Ops[0], Ops[1], Ops[2]);
486     case Instruction::ShuffleVector:
487       return new ShuffleVectorConstantExpr(Ops[0], Ops[1], Ops[2]);
488     case Instruction::InsertValue:
489       return new InsertValueConstantExpr(Ops[0], Ops[1], Indexes, Ty);
490     case Instruction::ExtractValue:
491       return new ExtractValueConstantExpr(Ops[0], Indexes, Ty);
492     case Instruction::GetElementPtr:
493       return GetElementPtrConstantExpr::Create(Ops[0], Ops.slice(1), Ty,
494                                                SubclassOptionalData);
495     case Instruction::ICmp:
496       return new CompareConstantExpr(Ty, Instruction::ICmp, SubclassData,
497                                      Ops[0], Ops[1]);
498     case Instruction::FCmp:
499       return new CompareConstantExpr(Ty, Instruction::FCmp, SubclassData,
500                                      Ops[0], Ops[1]);
501     }
502   }
503 };
504
505 template <class ConstantClass> class ConstantUniqueMap {
506 public:
507   typedef typename ConstantInfo<ConstantClass>::ValType ValType;
508   typedef typename ConstantInfo<ConstantClass>::TypeClass TypeClass;
509   typedef std::pair<TypeClass *, ValType> LookupKey;
510
511 private:
512   struct MapInfo {
513     typedef DenseMapInfo<ConstantClass *> ConstantClassInfo;
514     static inline ConstantClass *getEmptyKey() {
515       return ConstantClassInfo::getEmptyKey();
516     }
517     static inline ConstantClass *getTombstoneKey() {
518       return ConstantClassInfo::getTombstoneKey();
519     }
520     static unsigned getHashValue(const ConstantClass *CP) {
521       SmallVector<Constant *, 8> Storage;
522       return getHashValue(LookupKey(CP->getType(), ValType(CP, Storage)));
523     }
524     static bool isEqual(const ConstantClass *LHS, const ConstantClass *RHS) {
525       return LHS == RHS;
526     }
527     static unsigned getHashValue(const LookupKey &Val) {
528       return hash_combine(Val.first, Val.second.getHash());
529     }
530     static bool isEqual(const LookupKey &LHS, const ConstantClass *RHS) {
531       if (RHS == getEmptyKey() || RHS == getTombstoneKey())
532         return false;
533       if (LHS.first != RHS->getType())
534         return false;
535       return LHS.second == RHS;
536     }
537   };
538
539 public:
540   typedef DenseMap<ConstantClass *, char, MapInfo> MapTy;
541
542 private:
543   MapTy Map;
544
545 public:
546   typename MapTy::iterator map_begin() { return Map.begin(); }
547   typename MapTy::iterator map_end() { return Map.end(); }
548
549   void freeConstants() {
550     for (auto &I : Map)
551       // Asserts that use_empty().
552       delete I.first;
553   }
554
555 private:
556   ConstantClass *create(TypeClass *Ty, ValType V) {
557     ConstantClass *Result = V.create(Ty);
558
559     assert(Result->getType() == Ty && "Type specified is not correct!");
560     insert(Result);
561
562     return Result;
563   }
564
565 public:
566   /// Return the specified constant from the map, creating it if necessary.
567   ConstantClass *getOrCreate(TypeClass *Ty, ValType V) {
568     LookupKey Lookup(Ty, V);
569     ConstantClass *Result = nullptr;
570
571     auto I = find(Lookup);
572     if (I == Map.end())
573       Result = create(Ty, V);
574     else
575       Result = I->first;
576     assert(Result && "Unexpected nullptr");
577
578     return Result;
579   }
580
581   /// Find the constant by lookup key.
582   typename MapTy::iterator find(LookupKey Lookup) {
583     return Map.find_as(Lookup);
584   }
585
586   /// Insert the constant into its proper slot.
587   void insert(ConstantClass *CP) { Map[CP] = '\0'; }
588
589   /// Remove this constant from the map
590   void remove(ConstantClass *CP) {
591     typename MapTy::iterator I = Map.find(CP);
592     assert(I != Map.end() && "Constant not found in constant table!");
593     assert(I->first == CP && "Didn't find correct element?");
594     Map.erase(I);
595   }
596
597   void dump() const { DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n"); }
598 };
599
600 } // end namespace llvm
601
602 #endif