b7726944a22fcc21e80ed0d5c3186973d3ef3747
[oota-llvm.git] / lib / VMCore / 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_CONSTANTSCONTEXT_H
16 #define LLVM_CONSTANTSCONTEXT_H
17
18 #include "llvm/InlineAsm.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Operator.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <map>
25
26 namespace llvm {
27 template<class ValType>
28 struct ConstantTraits;
29
30 /// UnaryConstantExpr - This class is private to Constants.cpp, and is used
31 /// behind the scenes to implement unary constant exprs.
32 class UnaryConstantExpr : public ConstantExpr {
33   virtual void anchor();
34   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
35 public:
36   // allocate space for exactly one operand
37   void *operator new(size_t s) {
38     return User::operator new(s, 1);
39   }
40   UnaryConstantExpr(unsigned Opcode, Constant *C, Type *Ty)
41     : ConstantExpr(Ty, Opcode, &Op<0>(), 1) {
42     Op<0>() = C;
43   }
44   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
45 };
46
47 /// BinaryConstantExpr - This class is private to Constants.cpp, and is used
48 /// behind the scenes to implement binary constant exprs.
49 class BinaryConstantExpr : public ConstantExpr {
50   virtual void anchor();
51   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
52 public:
53   // allocate space for exactly two operands
54   void *operator new(size_t s) {
55     return User::operator new(s, 2);
56   }
57   BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2,
58                      unsigned Flags)
59     : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) {
60     Op<0>() = C1;
61     Op<1>() = C2;
62     SubclassOptionalData = Flags;
63   }
64   /// Transparently provide more efficient getOperand methods.
65   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
66 };
67
68 /// SelectConstantExpr - This class is private to Constants.cpp, and is used
69 /// behind the scenes to implement select constant exprs.
70 class SelectConstantExpr : public ConstantExpr {
71   virtual void anchor();
72   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
73 public:
74   // allocate space for exactly three operands
75   void *operator new(size_t s) {
76     return User::operator new(s, 3);
77   }
78   SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3)
79     : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) {
80     Op<0>() = C1;
81     Op<1>() = C2;
82     Op<2>() = C3;
83   }
84   /// Transparently provide more efficient getOperand methods.
85   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
86 };
87
88 /// ExtractElementConstantExpr - This class is private to
89 /// Constants.cpp, and is used behind the scenes to implement
90 /// extractelement constant exprs.
91 class ExtractElementConstantExpr : public ConstantExpr {
92   virtual void anchor();
93   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
94 public:
95   // allocate space for exactly two operands
96   void *operator new(size_t s) {
97     return User::operator new(s, 2);
98   }
99   ExtractElementConstantExpr(Constant *C1, Constant *C2)
100     : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(), 
101                    Instruction::ExtractElement, &Op<0>(), 2) {
102     Op<0>() = C1;
103     Op<1>() = C2;
104   }
105   /// Transparently provide more efficient getOperand methods.
106   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
107 };
108
109 /// InsertElementConstantExpr - This class is private to
110 /// Constants.cpp, and is used behind the scenes to implement
111 /// insertelement constant exprs.
112 class InsertElementConstantExpr : public ConstantExpr {
113   virtual void anchor();
114   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
115 public:
116   // allocate space for exactly three operands
117   void *operator new(size_t s) {
118     return User::operator new(s, 3);
119   }
120   InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3)
121     : ConstantExpr(C1->getType(), Instruction::InsertElement, 
122                    &Op<0>(), 3) {
123     Op<0>() = C1;
124     Op<1>() = C2;
125     Op<2>() = C3;
126   }
127   /// Transparently provide more efficient getOperand methods.
128   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
129 };
130
131 /// ShuffleVectorConstantExpr - This class is private to
132 /// Constants.cpp, and is used behind the scenes to implement
133 /// shufflevector constant exprs.
134 class ShuffleVectorConstantExpr : public ConstantExpr {
135   virtual void anchor();
136   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
137 public:
138   // allocate space for exactly three operands
139   void *operator new(size_t s) {
140     return User::operator new(s, 3);
141   }
142   ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3)
143   : ConstantExpr(VectorType::get(
144                    cast<VectorType>(C1->getType())->getElementType(),
145                    cast<VectorType>(C3->getType())->getNumElements()),
146                  Instruction::ShuffleVector, 
147                  &Op<0>(), 3) {
148     Op<0>() = C1;
149     Op<1>() = C2;
150     Op<2>() = C3;
151   }
152   /// Transparently provide more efficient getOperand methods.
153   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
154 };
155
156 /// ExtractValueConstantExpr - This class is private to
157 /// Constants.cpp, and is used behind the scenes to implement
158 /// extractvalue constant exprs.
159 class ExtractValueConstantExpr : public ConstantExpr {
160   virtual void anchor();
161   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
162 public:
163   // allocate space for exactly one operand
164   void *operator new(size_t s) {
165     return User::operator new(s, 1);
166   }
167   ExtractValueConstantExpr(Constant *Agg,
168                            const SmallVector<unsigned, 4> &IdxList,
169                            Type *DestTy)
170     : ConstantExpr(DestTy, Instruction::ExtractValue, &Op<0>(), 1),
171       Indices(IdxList) {
172     Op<0>() = Agg;
173   }
174
175   /// Indices - These identify which value to extract.
176   const SmallVector<unsigned, 4> Indices;
177
178   /// Transparently provide more efficient getOperand methods.
179   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
180 };
181
182 /// InsertValueConstantExpr - This class is private to
183 /// Constants.cpp, and is used behind the scenes to implement
184 /// insertvalue constant exprs.
185 class InsertValueConstantExpr : public ConstantExpr {
186   virtual void anchor();
187   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
188 public:
189   // allocate space for exactly one operand
190   void *operator new(size_t s) {
191     return User::operator new(s, 2);
192   }
193   InsertValueConstantExpr(Constant *Agg, Constant *Val,
194                           const SmallVector<unsigned, 4> &IdxList,
195                           Type *DestTy)
196     : ConstantExpr(DestTy, Instruction::InsertValue, &Op<0>(), 2),
197       Indices(IdxList) {
198     Op<0>() = Agg;
199     Op<1>() = Val;
200   }
201
202   /// Indices - These identify the position for the insertion.
203   const SmallVector<unsigned, 4> Indices;
204
205   /// Transparently provide more efficient getOperand methods.
206   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
207 };
208
209
210 /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is
211 /// used behind the scenes to implement getelementpr constant exprs.
212 class GetElementPtrConstantExpr : public ConstantExpr {
213   virtual void anchor();
214   GetElementPtrConstantExpr(Constant *C, ArrayRef<Constant*> IdxList,
215                             Type *DestTy);
216 public:
217   static GetElementPtrConstantExpr *Create(Constant *C,
218                                            ArrayRef<Constant*> IdxList,
219                                            Type *DestTy,
220                                            unsigned Flags) {
221     GetElementPtrConstantExpr *Result =
222       new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy);
223     Result->SubclassOptionalData = Flags;
224     return Result;
225   }
226   /// Transparently provide more efficient getOperand methods.
227   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
228 };
229
230 // CompareConstantExpr - This class is private to Constants.cpp, and is used
231 // behind the scenes to implement ICmp and FCmp constant expressions. This is
232 // needed in order to store the predicate value for these instructions.
233 class CompareConstantExpr : public ConstantExpr {
234   virtual void anchor();
235   void *operator new(size_t, unsigned);  // DO NOT IMPLEMENT
236 public:
237   // allocate space for exactly two operands
238   void *operator new(size_t s) {
239     return User::operator new(s, 2);
240   }
241   unsigned short predicate;
242   CompareConstantExpr(Type *ty, Instruction::OtherOps opc,
243                       unsigned short pred,  Constant* LHS, Constant* RHS)
244     : ConstantExpr(ty, opc, &Op<0>(), 2), predicate(pred) {
245     Op<0>() = LHS;
246     Op<1>() = RHS;
247   }
248   /// Transparently provide more efficient getOperand methods.
249   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
250 };
251
252 template <>
253 struct OperandTraits<UnaryConstantExpr> :
254   public FixedNumOperandTraits<UnaryConstantExpr, 1> {
255 };
256 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value)
257
258 template <>
259 struct OperandTraits<BinaryConstantExpr> :
260   public FixedNumOperandTraits<BinaryConstantExpr, 2> {
261 };
262 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value)
263
264 template <>
265 struct OperandTraits<SelectConstantExpr> :
266   public FixedNumOperandTraits<SelectConstantExpr, 3> {
267 };
268 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value)
269
270 template <>
271 struct OperandTraits<ExtractElementConstantExpr> :
272   public FixedNumOperandTraits<ExtractElementConstantExpr, 2> {
273 };
274 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value)
275
276 template <>
277 struct OperandTraits<InsertElementConstantExpr> :
278   public FixedNumOperandTraits<InsertElementConstantExpr, 3> {
279 };
280 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value)
281
282 template <>
283 struct OperandTraits<ShuffleVectorConstantExpr> :
284     public FixedNumOperandTraits<ShuffleVectorConstantExpr, 3> {
285 };
286 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value)
287
288 template <>
289 struct OperandTraits<ExtractValueConstantExpr> :
290   public FixedNumOperandTraits<ExtractValueConstantExpr, 1> {
291 };
292 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueConstantExpr, Value)
293
294 template <>
295 struct OperandTraits<InsertValueConstantExpr> :
296   public FixedNumOperandTraits<InsertValueConstantExpr, 2> {
297 };
298 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueConstantExpr, Value)
299
300 template <>
301 struct OperandTraits<GetElementPtrConstantExpr> :
302   public VariadicOperandTraits<GetElementPtrConstantExpr, 1> {
303 };
304
305 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value)
306
307
308 template <>
309 struct OperandTraits<CompareConstantExpr> :
310   public FixedNumOperandTraits<CompareConstantExpr, 2> {
311 };
312 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value)
313
314 struct ExprMapKeyType {
315   ExprMapKeyType(unsigned opc,
316       ArrayRef<Constant*> ops,
317       unsigned short flags = 0,
318       unsigned short optionalflags = 0,
319       ArrayRef<unsigned> inds = ArrayRef<unsigned>())
320         : opcode(opc), subclassoptionaldata(optionalflags), subclassdata(flags),
321         operands(ops.begin(), ops.end()), indices(inds.begin(), inds.end()) {}
322   uint8_t opcode;
323   uint8_t subclassoptionaldata;
324   uint16_t subclassdata;
325   std::vector<Constant*> operands;
326   SmallVector<unsigned, 4> indices;
327   bool operator==(const ExprMapKeyType& that) const {
328     return this->opcode == that.opcode &&
329            this->subclassdata == that.subclassdata &&
330            this->subclassoptionaldata == that.subclassoptionaldata &&
331            this->operands == that.operands &&
332            this->indices == that.indices;
333   }
334   bool operator<(const ExprMapKeyType & that) const {
335     if (this->opcode != that.opcode) return this->opcode < that.opcode;
336     if (this->operands != that.operands) return this->operands < that.operands;
337     if (this->subclassdata != that.subclassdata)
338       return this->subclassdata < that.subclassdata;
339     if (this->subclassoptionaldata != that.subclassoptionaldata)
340       return this->subclassoptionaldata < that.subclassoptionaldata;
341     if (this->indices != that.indices) return this->indices < that.indices;
342     return false;
343   }
344
345   bool operator!=(const ExprMapKeyType& that) const {
346     return !(*this == that);
347   }
348 };
349
350 struct InlineAsmKeyType {
351   InlineAsmKeyType(StringRef AsmString,
352                    StringRef Constraints, bool hasSideEffects,
353                    bool isAlignStack)
354     : asm_string(AsmString), constraints(Constraints),
355       has_side_effects(hasSideEffects), is_align_stack(isAlignStack) {}
356   std::string asm_string;
357   std::string constraints;
358   bool has_side_effects;
359   bool is_align_stack;
360   bool operator==(const InlineAsmKeyType& that) const {
361     return this->asm_string == that.asm_string &&
362            this->constraints == that.constraints &&
363            this->has_side_effects == that.has_side_effects &&
364            this->is_align_stack == that.is_align_stack;
365   }
366   bool operator<(const InlineAsmKeyType& that) const {
367     if (this->asm_string != that.asm_string)
368       return this->asm_string < that.asm_string;
369     if (this->constraints != that.constraints)
370       return this->constraints < that.constraints;
371     if (this->has_side_effects != that.has_side_effects)
372       return this->has_side_effects < that.has_side_effects;
373     if (this->is_align_stack != that.is_align_stack)
374       return this->is_align_stack < that.is_align_stack;
375     return false;
376   }
377
378   bool operator!=(const InlineAsmKeyType& that) const {
379     return !(*this == that);
380   }
381 };
382
383 // The number of operands for each ConstantCreator::create method is
384 // determined by the ConstantTraits template.
385 // ConstantCreator - A class that is used to create constants by
386 // ConstantUniqueMap*.  This class should be partially specialized if there is
387 // something strange that needs to be done to interface to the ctor for the
388 // constant.
389 //
390 template<typename T, typename Alloc>
391 struct ConstantTraits< std::vector<T, Alloc> > {
392   static unsigned uses(const std::vector<T, Alloc>& v) {
393     return v.size();
394   }
395 };
396
397 template<>
398 struct ConstantTraits<Constant *> {
399   static unsigned uses(Constant * const & v) {
400     return 1;
401   }
402 };
403
404 template<class ConstantClass, class TypeClass, class ValType>
405 struct ConstantCreator {
406   static ConstantClass *create(TypeClass *Ty, const ValType &V) {
407     return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V);
408   }
409 };
410
411 template<class ConstantClass>
412 struct ConstantKeyData {
413   typedef void ValType;
414   static ValType getValType(ConstantClass *C) {
415     llvm_unreachable("Unknown Constant type!");
416   }
417 };
418
419 template<>
420 struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
421   static ConstantExpr *create(Type *Ty, const ExprMapKeyType &V,
422       unsigned short pred = 0) {
423     if (Instruction::isCast(V.opcode))
424       return new UnaryConstantExpr(V.opcode, V.operands[0], Ty);
425     if ((V.opcode >= Instruction::BinaryOpsBegin &&
426          V.opcode < Instruction::BinaryOpsEnd))
427       return new BinaryConstantExpr(V.opcode, V.operands[0], V.operands[1],
428                                     V.subclassoptionaldata);
429     if (V.opcode == Instruction::Select)
430       return new SelectConstantExpr(V.operands[0], V.operands[1], 
431                                     V.operands[2]);
432     if (V.opcode == Instruction::ExtractElement)
433       return new ExtractElementConstantExpr(V.operands[0], V.operands[1]);
434     if (V.opcode == Instruction::InsertElement)
435       return new InsertElementConstantExpr(V.operands[0], V.operands[1],
436                                            V.operands[2]);
437     if (V.opcode == Instruction::ShuffleVector)
438       return new ShuffleVectorConstantExpr(V.operands[0], V.operands[1],
439                                            V.operands[2]);
440     if (V.opcode == Instruction::InsertValue)
441       return new InsertValueConstantExpr(V.operands[0], V.operands[1],
442                                          V.indices, Ty);
443     if (V.opcode == Instruction::ExtractValue)
444       return new ExtractValueConstantExpr(V.operands[0], V.indices, Ty);
445     if (V.opcode == Instruction::GetElementPtr) {
446       std::vector<Constant*> IdxList(V.operands.begin()+1, V.operands.end());
447       return GetElementPtrConstantExpr::Create(V.operands[0], IdxList, Ty,
448                                                V.subclassoptionaldata);
449     }
450
451     // The compare instructions are weird. We have to encode the predicate
452     // value and it is combined with the instruction opcode by multiplying
453     // the opcode by one hundred. We must decode this to get the predicate.
454     if (V.opcode == Instruction::ICmp)
455       return new CompareConstantExpr(Ty, Instruction::ICmp, V.subclassdata,
456                                      V.operands[0], V.operands[1]);
457     if (V.opcode == Instruction::FCmp) 
458       return new CompareConstantExpr(Ty, Instruction::FCmp, V.subclassdata,
459                                      V.operands[0], V.operands[1]);
460     llvm_unreachable("Invalid ConstantExpr!");
461   }
462 };
463
464 template<>
465 struct ConstantKeyData<ConstantExpr> {
466   typedef ExprMapKeyType ValType;
467   static ValType getValType(ConstantExpr *CE) {
468     std::vector<Constant*> Operands;
469     Operands.reserve(CE->getNumOperands());
470     for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i)
471       Operands.push_back(cast<Constant>(CE->getOperand(i)));
472     return ExprMapKeyType(CE->getOpcode(), Operands,
473         CE->isCompare() ? CE->getPredicate() : 0,
474         CE->getRawSubclassOptionalData(),
475         CE->hasIndices() ?
476           CE->getIndices() : ArrayRef<unsigned>());
477   }
478 };
479
480
481 template<>
482 struct ConstantKeyData<ConstantVector> {
483   typedef std::vector<Constant*> ValType;
484   static ValType getValType(ConstantVector *CP) {
485     std::vector<Constant*> Elements;
486     Elements.reserve(CP->getNumOperands());
487     for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
488       Elements.push_back(CP->getOperand(i));
489     return Elements;
490   }
491 };
492
493 template<>
494 struct ConstantKeyData<ConstantArray> {
495   typedef std::vector<Constant*> ValType;
496   static ValType getValType(ConstantArray *CA) {
497     std::vector<Constant*> Elements;
498     Elements.reserve(CA->getNumOperands());
499     for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
500       Elements.push_back(cast<Constant>(CA->getOperand(i)));
501     return Elements;
502   }
503 };
504
505 template<>
506 struct ConstantKeyData<ConstantStruct> {
507   typedef std::vector<Constant*> ValType;
508   static ValType getValType(ConstantStruct *CS) {
509     std::vector<Constant*> Elements;
510     Elements.reserve(CS->getNumOperands());
511     for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i)
512       Elements.push_back(cast<Constant>(CS->getOperand(i)));
513     return Elements;
514   }
515 };
516
517
518 template<>
519 struct ConstantCreator<InlineAsm, PointerType, InlineAsmKeyType> {
520   static InlineAsm *create(PointerType *Ty, const InlineAsmKeyType &Key) {
521     return new InlineAsm(Ty, Key.asm_string, Key.constraints,
522                          Key.has_side_effects, Key.is_align_stack);
523   }
524 };
525
526 template<>
527 struct ConstantKeyData<InlineAsm> {
528   typedef InlineAsmKeyType ValType;
529   static ValType getValType(InlineAsm *Asm) {
530     return InlineAsmKeyType(Asm->getAsmString(), Asm->getConstraintString(),
531                             Asm->hasSideEffects(), Asm->isAlignStack());
532   }
533 };
534
535 template<class ValType, class ValRefType, class TypeClass, class ConstantClass,
536          bool HasLargeKey = false /*true for arrays and structs*/ >
537 class ConstantUniqueMap {
538 public:
539   typedef std::pair<TypeClass*, ValType> MapKey;
540   typedef std::map<MapKey, ConstantClass *> MapTy;
541   typedef std::map<ConstantClass *, typename MapTy::iterator> InverseMapTy;
542 private:
543   /// Map - This is the main map from the element descriptor to the Constants.
544   /// This is the primary way we avoid creating two of the same shape
545   /// constant.
546   MapTy Map;
547     
548   /// InverseMap - If "HasLargeKey" is true, this contains an inverse mapping
549   /// from the constants to their element in Map.  This is important for
550   /// removal of constants from the array, which would otherwise have to scan
551   /// through the map with very large keys.
552   InverseMapTy InverseMap;
553
554 public:
555   typename MapTy::iterator map_begin() { return Map.begin(); }
556   typename MapTy::iterator map_end() { return Map.end(); }
557
558   void freeConstants() {
559     for (typename MapTy::iterator I=Map.begin(), E=Map.end();
560          I != E; ++I) {
561       // Asserts that use_empty().
562       delete I->second;
563     }
564   }
565     
566   /// InsertOrGetItem - Return an iterator for the specified element.
567   /// If the element exists in the map, the returned iterator points to the
568   /// entry and Exists=true.  If not, the iterator points to the newly
569   /// inserted entry and returns Exists=false.  Newly inserted entries have
570   /// I->second == 0, and should be filled in.
571   typename MapTy::iterator InsertOrGetItem(std::pair<MapKey, ConstantClass *>
572                                  &InsertVal,
573                                  bool &Exists) {
574     std::pair<typename MapTy::iterator, bool> IP = Map.insert(InsertVal);
575     Exists = !IP.second;
576     return IP.first;
577   }
578     
579 private:
580   typename MapTy::iterator FindExistingElement(ConstantClass *CP) {
581     if (HasLargeKey) {
582       typename InverseMapTy::iterator IMI = InverseMap.find(CP);
583       assert(IMI != InverseMap.end() && IMI->second != Map.end() &&
584              IMI->second->second == CP &&
585              "InverseMap corrupt!");
586       return IMI->second;
587     }
588       
589     typename MapTy::iterator I =
590       Map.find(MapKey(static_cast<TypeClass*>(CP->getType()),
591                       ConstantKeyData<ConstantClass>::getValType(CP)));
592     if (I == Map.end() || I->second != CP) {
593       // FIXME: This should not use a linear scan.  If this gets to be a
594       // performance problem, someone should look at this.
595       for (I = Map.begin(); I != Map.end() && I->second != CP; ++I)
596         /* empty */;
597     }
598     return I;
599   }
600
601   ConstantClass *Create(TypeClass *Ty, ValRefType V,
602                         typename MapTy::iterator I) {
603     ConstantClass* Result =
604       ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
605
606     assert(Result->getType() == Ty && "Type specified is not correct!");
607     I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
608
609     if (HasLargeKey)  // Remember the reverse mapping if needed.
610       InverseMap.insert(std::make_pair(Result, I));
611
612     return Result;
613   }
614 public:
615     
616   /// getOrCreate - Return the specified constant from the map, creating it if
617   /// necessary.
618   ConstantClass *getOrCreate(TypeClass *Ty, ValRefType V) {
619     MapKey Lookup(Ty, V);
620     ConstantClass* Result = 0;
621     
622     typename MapTy::iterator I = Map.find(Lookup);
623     // Is it in the map?  
624     if (I != Map.end())
625       Result = I->second;
626         
627     if (!Result) {
628       // If no preexisting value, create one now...
629       Result = Create(Ty, V, I);
630     }
631         
632     return Result;
633   }
634
635   void remove(ConstantClass *CP) {
636     typename MapTy::iterator I = FindExistingElement(CP);
637     assert(I != Map.end() && "Constant not found in constant table!");
638     assert(I->second == CP && "Didn't find correct element?");
639
640     if (HasLargeKey)  // Remember the reverse mapping if needed.
641       InverseMap.erase(CP);
642
643     Map.erase(I);
644   }
645
646   /// MoveConstantToNewSlot - If we are about to change C to be the element
647   /// specified by I, update our internal data structures to reflect this
648   /// fact.
649   void MoveConstantToNewSlot(ConstantClass *C, typename MapTy::iterator I) {
650     // First, remove the old location of the specified constant in the map.
651     typename MapTy::iterator OldI = FindExistingElement(C);
652     assert(OldI != Map.end() && "Constant not found in constant table!");
653     assert(OldI->second == C && "Didn't find correct element?");
654       
655      // Remove the old entry from the map.
656     Map.erase(OldI);
657     
658     // Update the inverse map so that we know that this constant is now
659     // located at descriptor I.
660     if (HasLargeKey) {
661       assert(I->second == C && "Bad inversemap entry!");
662       InverseMap[C] = I;
663     }
664   }
665
666   void dump() const {
667     DEBUG(dbgs() << "Constant.cpp: ConstantUniqueMap\n");
668   }
669 };
670
671 }
672
673 #endif