I misled Alkis: LLVM should have isnan, not isunordered.
[oota-llvm.git] / lib / VMCore / Constants.cpp
1 //===-- Constants.cpp - Implement Constant nodes --------------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Constant* classes...
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Constants.h"
15 #include "ConstantFolding.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/iMemory.h"
18 #include "llvm/SymbolTable.h"
19 #include "llvm/Module.h"
20 #include "Support/StringExtras.h"
21 #include <algorithm>
22 using namespace llvm;
23
24 ConstantBool *ConstantBool::True  = new ConstantBool(true);
25 ConstantBool *ConstantBool::False = new ConstantBool(false);
26
27
28 //===----------------------------------------------------------------------===//
29 //                              Constant Class
30 //===----------------------------------------------------------------------===//
31
32 // Specialize setName to take care of symbol table majik
33 void Constant::setName(const std::string &Name, SymbolTable *ST) {
34   assert(ST && "Type::setName - Must provide symbol table argument!");
35
36   if (Name.size()) ST->insert(Name, this);
37 }
38
39 void Constant::destroyConstantImpl() {
40   // When a Constant is destroyed, there may be lingering
41   // references to the constant by other constants in the constant pool.  These
42   // constants are implicitly dependent on the module that is being deleted,
43   // but they don't know that.  Because we only find out when the CPV is
44   // deleted, we must now notify all of our users (that should only be
45   // Constants) that they are, in fact, invalid now and should be deleted.
46   //
47   while (!use_empty()) {
48     Value *V = use_back();
49 #ifndef NDEBUG      // Only in -g mode...
50     if (!isa<Constant>(V))
51       std::cerr << "While deleting: " << *this
52                 << "\n\nUse still stuck around after Def is destroyed: "
53                 << *V << "\n\n";
54 #endif
55     assert(isa<Constant>(V) && "References remain to Constant being destroyed");
56     Constant *CPV = cast<Constant>(V);
57     CPV->destroyConstant();
58
59     // The constant should remove itself from our use list...
60     assert((use_empty() || use_back() != V) && "Constant not removed!");
61   }
62
63   // Value has no outstanding references it is safe to delete it now...
64   delete this;
65 }
66
67 // Static constructor to create a '0' constant of arbitrary type...
68 Constant *Constant::getNullValue(const Type *Ty) {
69   switch (Ty->getPrimitiveID()) {
70   case Type::BoolTyID: {
71     static Constant *NullBool = ConstantBool::get(false);
72     return NullBool;
73   }
74   case Type::SByteTyID: {
75     static Constant *NullSByte = ConstantSInt::get(Type::SByteTy, 0);
76     return NullSByte;
77   }
78   case Type::UByteTyID: {
79     static Constant *NullUByte = ConstantUInt::get(Type::UByteTy, 0);
80     return NullUByte;
81   }
82   case Type::ShortTyID: {
83     static Constant *NullShort = ConstantSInt::get(Type::ShortTy, 0);
84     return NullShort;
85   }
86   case Type::UShortTyID: {
87     static Constant *NullUShort = ConstantUInt::get(Type::UShortTy, 0);
88     return NullUShort;
89   }
90   case Type::IntTyID: {
91     static Constant *NullInt = ConstantSInt::get(Type::IntTy, 0);
92     return NullInt;
93   }
94   case Type::UIntTyID: {
95     static Constant *NullUInt = ConstantUInt::get(Type::UIntTy, 0);
96     return NullUInt;
97   }
98   case Type::LongTyID: {
99     static Constant *NullLong = ConstantSInt::get(Type::LongTy, 0);
100     return NullLong;
101   }
102   case Type::ULongTyID: {
103     static Constant *NullULong = ConstantUInt::get(Type::ULongTy, 0);
104     return NullULong;
105   }
106
107   case Type::FloatTyID: {
108     static Constant *NullFloat = ConstantFP::get(Type::FloatTy, 0);
109     return NullFloat;
110   }
111   case Type::DoubleTyID: {
112     static Constant *NullDouble = ConstantFP::get(Type::DoubleTy, 0);
113     return NullDouble;
114   }
115
116   case Type::PointerTyID: 
117     return ConstantPointerNull::get(cast<PointerType>(Ty));
118
119   case Type::StructTyID:
120   case Type::ArrayTyID:
121     return ConstantAggregateZero::get(Ty);
122   default:
123     // Function, Type, Label, or Opaque type?
124     assert(0 && "Cannot create a null constant of that type!");
125     return 0;
126   }
127 }
128
129 // Static constructor to create the maximum constant of an integral type...
130 ConstantIntegral *ConstantIntegral::getMaxValue(const Type *Ty) {
131   switch (Ty->getPrimitiveID()) {
132   case Type::BoolTyID:   return ConstantBool::True;
133   case Type::SByteTyID:
134   case Type::ShortTyID:
135   case Type::IntTyID:
136   case Type::LongTyID: {
137     // Calculate 011111111111111... 
138     unsigned TypeBits = Ty->getPrimitiveSize()*8;
139     int64_t Val = INT64_MAX;             // All ones
140     Val >>= 64-TypeBits;                 // Shift out unwanted 1 bits...
141     return ConstantSInt::get(Ty, Val);
142   }
143
144   case Type::UByteTyID:
145   case Type::UShortTyID:
146   case Type::UIntTyID:
147   case Type::ULongTyID:  return getAllOnesValue(Ty);
148
149   default: return 0;
150   }
151 }
152
153 // Static constructor to create the minimum constant for an integral type...
154 ConstantIntegral *ConstantIntegral::getMinValue(const Type *Ty) {
155   switch (Ty->getPrimitiveID()) {
156   case Type::BoolTyID:   return ConstantBool::False;
157   case Type::SByteTyID:
158   case Type::ShortTyID:
159   case Type::IntTyID:
160   case Type::LongTyID: {
161      // Calculate 1111111111000000000000 
162      unsigned TypeBits = Ty->getPrimitiveSize()*8;
163      int64_t Val = -1;                    // All ones
164      Val <<= TypeBits-1;                  // Shift over to the right spot
165      return ConstantSInt::get(Ty, Val);
166   }
167
168   case Type::UByteTyID:
169   case Type::UShortTyID:
170   case Type::UIntTyID:
171   case Type::ULongTyID:  return ConstantUInt::get(Ty, 0);
172
173   default: return 0;
174   }
175 }
176
177 // Static constructor to create an integral constant with all bits set
178 ConstantIntegral *ConstantIntegral::getAllOnesValue(const Type *Ty) {
179   switch (Ty->getPrimitiveID()) {
180   case Type::BoolTyID:   return ConstantBool::True;
181   case Type::SByteTyID:
182   case Type::ShortTyID:
183   case Type::IntTyID:
184   case Type::LongTyID:   return ConstantSInt::get(Ty, -1);
185
186   case Type::UByteTyID:
187   case Type::UShortTyID:
188   case Type::UIntTyID:
189   case Type::ULongTyID: {
190     // Calculate ~0 of the right type...
191     unsigned TypeBits = Ty->getPrimitiveSize()*8;
192     uint64_t Val = ~0ULL;                // All ones
193     Val >>= 64-TypeBits;                 // Shift out unwanted 1 bits...
194     return ConstantUInt::get(Ty, Val);
195   }
196   default: return 0;
197   }
198 }
199
200 bool ConstantUInt::isAllOnesValue() const {
201   unsigned TypeBits = getType()->getPrimitiveSize()*8;
202   uint64_t Val = ~0ULL;                // All ones
203   Val >>= 64-TypeBits;                 // Shift out inappropriate bits
204   return getValue() == Val;
205 }
206
207
208 //===----------------------------------------------------------------------===//
209 //                            ConstantXXX Classes
210 //===----------------------------------------------------------------------===//
211
212 //===----------------------------------------------------------------------===//
213 //                             Normal Constructors
214
215 ConstantBool::ConstantBool(bool V) : ConstantIntegral(Type::BoolTy) {
216   Val = V;
217 }
218
219 ConstantInt::ConstantInt(const Type *Ty, uint64_t V) : ConstantIntegral(Ty) {
220   Val.Unsigned = V;
221 }
222
223 ConstantSInt::ConstantSInt(const Type *Ty, int64_t V) : ConstantInt(Ty, V) {
224   assert(Ty->isInteger() && Ty->isSigned() &&
225          "Illegal type for unsigned integer constant!");
226   assert(isValueValidForType(Ty, V) && "Value too large for type!");
227 }
228
229 ConstantUInt::ConstantUInt(const Type *Ty, uint64_t V) : ConstantInt(Ty, V) {
230   assert(Ty->isInteger() && Ty->isUnsigned() &&
231          "Illegal type for unsigned integer constant!");
232   assert(isValueValidForType(Ty, V) && "Value too large for type!");
233 }
234
235 ConstantFP::ConstantFP(const Type *Ty, double V) : Constant(Ty) {
236   assert(isValueValidForType(Ty, V) && "Value too large for type!");
237   Val = V;
238 }
239
240 ConstantArray::ConstantArray(const ArrayType *T,
241                              const std::vector<Constant*> &V) : Constant(T) {
242   Operands.reserve(V.size());
243   for (unsigned i = 0, e = V.size(); i != e; ++i) {
244     assert(V[i]->getType() == T->getElementType() ||
245            (T->isAbstract() &&
246             V[i]->getType()->getPrimitiveID() ==
247             T->getElementType()->getPrimitiveID()));
248     Operands.push_back(Use(V[i], this));
249   }
250 }
251
252 ConstantStruct::ConstantStruct(const StructType *T,
253                                const std::vector<Constant*> &V) : Constant(T) {
254   assert(V.size() == T->getNumElements() &&
255          "Invalid initializer vector for constant structure");
256   Operands.reserve(V.size());
257   for (unsigned i = 0, e = V.size(); i != e; ++i) {
258     assert((V[i]->getType() == T->getElementType(i) ||
259             ((T->getElementType(i)->isAbstract() ||
260               V[i]->getType()->isAbstract()) &&
261              T->getElementType(i)->getPrimitiveID() == 
262                       V[i]->getType()->getPrimitiveID())) &&
263            "Initializer for struct element doesn't match struct element type!");
264     Operands.push_back(Use(V[i], this));
265   }
266 }
267
268 ConstantPointerRef::ConstantPointerRef(GlobalValue *GV)
269   : Constant(GV->getType()) {
270   Operands.reserve(1);
271   Operands.push_back(Use(GV, this));
272 }
273
274 ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C, const Type *Ty)
275   : Constant(Ty), iType(Opcode) {
276   Operands.reserve(1);
277   Operands.push_back(Use(C, this));
278 }
279
280 // Select instruction creation ctor
281 ConstantExpr::ConstantExpr(Constant *C, Constant *V1, Constant *V2)
282   : Constant(V1->getType()), iType(Instruction::Select) {
283   Operands.reserve(3);
284   Operands.push_back(Use(C, this));
285   Operands.push_back(Use(V1, this));
286   Operands.push_back(Use(V2, this));
287 }
288
289
290 static bool isSetCC(unsigned Opcode) {
291   return Opcode == Instruction::SetEQ || Opcode == Instruction::SetNE ||
292          Opcode == Instruction::SetLT || Opcode == Instruction::SetGT ||
293          Opcode == Instruction::SetLE || Opcode == Instruction::SetGE;
294 }
295
296 ConstantExpr::ConstantExpr(unsigned Opcode, Constant *C1, Constant *C2)
297   : Constant(isSetCC(Opcode) ? Type::BoolTy : C1->getType()), iType(Opcode) {
298   Operands.reserve(2);
299   Operands.push_back(Use(C1, this));
300   Operands.push_back(Use(C2, this));
301 }
302
303 ConstantExpr::ConstantExpr(Constant *C, const std::vector<Constant*> &IdxList,
304                            const Type *DestTy)
305   : Constant(DestTy), iType(Instruction::GetElementPtr) {
306   Operands.reserve(1+IdxList.size());
307   Operands.push_back(Use(C, this));
308   for (unsigned i = 0, E = IdxList.size(); i != E; ++i)
309     Operands.push_back(Use(IdxList[i], this));
310 }
311
312 /// ConstantExpr::get* - Return some common constants without having to
313 /// specify the full Instruction::OPCODE identifier.
314 ///
315 Constant *ConstantExpr::getNeg(Constant *C) {
316   if (!C->getType()->isFloatingPoint())
317     return get(Instruction::Sub, getNullValue(C->getType()), C);
318   else
319     return get(Instruction::Sub, ConstantFP::get(C->getType(), -0.0), C);
320 }
321 Constant *ConstantExpr::getNot(Constant *C) {
322   assert(isa<ConstantIntegral>(C) && "Cannot NOT a nonintegral type!");
323   return get(Instruction::Xor, C,
324              ConstantIntegral::getAllOnesValue(C->getType()));
325 }
326 Constant *ConstantExpr::getAdd(Constant *C1, Constant *C2) {
327   return get(Instruction::Add, C1, C2);
328 }
329 Constant *ConstantExpr::getSub(Constant *C1, Constant *C2) {
330   return get(Instruction::Sub, C1, C2);
331 }
332 Constant *ConstantExpr::getMul(Constant *C1, Constant *C2) {
333   return get(Instruction::Mul, C1, C2);
334 }
335 Constant *ConstantExpr::getDiv(Constant *C1, Constant *C2) {
336   return get(Instruction::Div, C1, C2);
337 }
338 Constant *ConstantExpr::getRem(Constant *C1, Constant *C2) {
339   return get(Instruction::Rem, C1, C2);
340 }
341 Constant *ConstantExpr::getAnd(Constant *C1, Constant *C2) {
342   return get(Instruction::And, C1, C2);
343 }
344 Constant *ConstantExpr::getOr(Constant *C1, Constant *C2) {
345   return get(Instruction::Or, C1, C2);
346 }
347 Constant *ConstantExpr::getXor(Constant *C1, Constant *C2) {
348   return get(Instruction::Xor, C1, C2);
349 }
350 Constant *ConstantExpr::getSetEQ(Constant *C1, Constant *C2) {
351   return get(Instruction::SetEQ, C1, C2);
352 }
353 Constant *ConstantExpr::getSetNE(Constant *C1, Constant *C2) {
354   return get(Instruction::SetNE, C1, C2);
355 }
356 Constant *ConstantExpr::getSetLT(Constant *C1, Constant *C2) {
357   return get(Instruction::SetLT, C1, C2);
358 }
359 Constant *ConstantExpr::getSetGT(Constant *C1, Constant *C2) {
360   return get(Instruction::SetGT, C1, C2);
361 }
362 Constant *ConstantExpr::getSetLE(Constant *C1, Constant *C2) {
363   return get(Instruction::SetLE, C1, C2);
364 }
365 Constant *ConstantExpr::getSetGE(Constant *C1, Constant *C2) {
366   return get(Instruction::SetGE, C1, C2);
367 }
368 Constant *ConstantExpr::getShl(Constant *C1, Constant *C2) {
369   return get(Instruction::Shl, C1, C2);
370 }
371 Constant *ConstantExpr::getShr(Constant *C1, Constant *C2) {
372   return get(Instruction::Shr, C1, C2);
373 }
374
375 Constant *ConstantExpr::getUShr(Constant *C1, Constant *C2) {
376   if (C1->getType()->isUnsigned()) return getShr(C1, C2);
377   return getCast(getShr(getCast(C1,
378                     C1->getType()->getUnsignedVersion()), C2), C1->getType());
379 }
380
381 Constant *ConstantExpr::getSShr(Constant *C1, Constant *C2) {
382   if (C1->getType()->isSigned()) return getShr(C1, C2);
383   return getCast(getShr(getCast(C1,
384                         C1->getType()->getSignedVersion()), C2), C1->getType());
385 }
386
387
388 //===----------------------------------------------------------------------===//
389 //                           classof implementations
390
391 bool ConstantIntegral::classof(const Constant *CPV) {
392   return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
393 }
394
395 bool ConstantInt::classof(const Constant *CPV) {
396   return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
397 }
398 bool ConstantSInt::classof(const Constant *CPV) {
399   return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
400 }
401 bool ConstantUInt::classof(const Constant *CPV) {
402   return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
403 }
404 bool ConstantFP::classof(const Constant *CPV) {
405   const Type *Ty = CPV->getType();
406   return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
407           !isa<ConstantExpr>(CPV));
408 }
409 bool ConstantAggregateZero::classof(const Constant *CPV) {
410   return (isa<ArrayType>(CPV->getType()) || isa<StructType>(CPV->getType())) &&
411          CPV->isNullValue();
412 }
413 bool ConstantArray::classof(const Constant *CPV) {
414   return isa<ArrayType>(CPV->getType()) && !CPV->isNullValue();
415 }
416 bool ConstantStruct::classof(const Constant *CPV) {
417   return isa<StructType>(CPV->getType()) && !CPV->isNullValue();
418 }
419
420 bool ConstantPointerNull::classof(const Constant *CPV) {
421   return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
422          CPV->getNumOperands() == 0;
423 }
424
425 bool ConstantPointerRef::classof(const Constant *CPV) {
426   return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
427          CPV->getNumOperands() == 1;
428 }
429
430
431
432 //===----------------------------------------------------------------------===//
433 //                      isValueValidForType implementations
434
435 bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
436   switch (Ty->getPrimitiveID()) {
437   default:
438     return false;         // These can't be represented as integers!!!
439     // Signed types...
440   case Type::SByteTyID:
441     return (Val <= INT8_MAX && Val >= INT8_MIN);
442   case Type::ShortTyID:
443     return (Val <= INT16_MAX && Val >= INT16_MIN);
444   case Type::IntTyID:
445     return (Val <= int(INT32_MAX) && Val >= int(INT32_MIN));
446   case Type::LongTyID:
447     return true;          // This is the largest type...
448   }
449 }
450
451 bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
452   switch (Ty->getPrimitiveID()) {
453   default:
454     return false;         // These can't be represented as integers!!!
455
456     // Unsigned types...
457   case Type::UByteTyID:
458     return (Val <= UINT8_MAX);
459   case Type::UShortTyID:
460     return (Val <= UINT16_MAX);
461   case Type::UIntTyID:
462     return (Val <= UINT32_MAX);
463   case Type::ULongTyID:
464     return true;          // This is the largest type...
465   }
466 }
467
468 bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
469   switch (Ty->getPrimitiveID()) {
470   default:
471     return false;         // These can't be represented as floating point!
472
473     // TODO: Figure out how to test if a double can be cast to a float!
474   case Type::FloatTyID:
475   case Type::DoubleTyID:
476     return true;          // This is the largest type...
477   }
478 };
479
480 //===----------------------------------------------------------------------===//
481 //                replaceUsesOfWithOnConstant implementations
482
483 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
484                                                 bool DisableChecking) {
485   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
486
487   std::vector<Constant*> Values;
488   Values.reserve(getValues().size());  // Build replacement array...
489   for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
490     Constant *Val = cast<Constant>(getValues()[i]);
491     if (Val == From) Val = cast<Constant>(To);
492     Values.push_back(Val);
493   }
494   
495   Constant *Replacement = ConstantArray::get(getType(), Values);
496   assert(Replacement != this && "I didn't contain From!");
497
498   // Everyone using this now uses the replacement...
499   if (DisableChecking)
500     uncheckedReplaceAllUsesWith(Replacement);
501   else
502     replaceAllUsesWith(Replacement);
503   
504   // Delete the old constant!
505   destroyConstant();  
506 }
507
508 void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
509                                                  bool DisableChecking) {
510   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
511
512   std::vector<Constant*> Values;
513   Values.reserve(getValues().size());
514   for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
515     Constant *Val = cast<Constant>(getValues()[i]);
516     if (Val == From) Val = cast<Constant>(To);
517     Values.push_back(Val);
518   }
519   
520   Constant *Replacement = ConstantStruct::get(getType(), Values);
521   assert(Replacement != this && "I didn't contain From!");
522
523   // Everyone using this now uses the replacement...
524   if (DisableChecking)
525     uncheckedReplaceAllUsesWith(Replacement);
526   else
527     replaceAllUsesWith(Replacement);
528   
529   // Delete the old constant!
530   destroyConstant();
531 }
532
533 void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
534                                                      bool DisableChecking) {
535   if (isa<GlobalValue>(To)) {
536     assert(From == getOperand(0) && "Doesn't contain from!");
537     ConstantPointerRef *Replacement =
538       ConstantPointerRef::get(cast<GlobalValue>(To));
539     
540     // Everyone using this now uses the replacement...
541     if (DisableChecking)
542       uncheckedReplaceAllUsesWith(Replacement);
543     else
544       replaceAllUsesWith(Replacement);
545     
546   } else {
547     // Just replace ourselves with the To value specified.
548     if (DisableChecking)
549       uncheckedReplaceAllUsesWith(To);
550     else
551       replaceAllUsesWith(To);
552   }
553
554   // Delete the old constant!
555   destroyConstant();
556 }
557
558 void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
559                                                bool DisableChecking) {
560   assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
561   Constant *To = cast<Constant>(ToV);
562
563   Constant *Replacement = 0;
564   if (getOpcode() == Instruction::GetElementPtr) {
565     std::vector<Constant*> Indices;
566     Constant *Pointer = getOperand(0);
567     Indices.reserve(getNumOperands()-1);
568     if (Pointer == From) Pointer = To;
569     
570     for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
571       Constant *Val = getOperand(i);
572       if (Val == From) Val = To;
573       Indices.push_back(Val);
574     }
575     Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
576   } else if (getOpcode() == Instruction::Cast) {
577     assert(getOperand(0) == From && "Cast only has one use!");
578     Replacement = ConstantExpr::getCast(To, getType());
579   } else if (getOpcode() == Instruction::Select) {
580     Constant *C1 = getOperand(0);
581     Constant *C2 = getOperand(1);
582     Constant *C3 = getOperand(2);
583     if (C1 == From) C1 = To;
584     if (C2 == From) C2 = To;
585     if (C3 == From) C3 = To;
586     Replacement = ConstantExpr::getSelect(C1, C2, C3);
587   } else if (getNumOperands() == 2) {
588     Constant *C1 = getOperand(0);
589     Constant *C2 = getOperand(1);
590     if (C1 == From) C1 = To;
591     if (C2 == From) C2 = To;
592     Replacement = ConstantExpr::get(getOpcode(), C1, C2);
593   } else {
594     assert(0 && "Unknown ConstantExpr type!");
595     return;
596   }
597   
598   assert(Replacement != this && "I didn't contain From!");
599
600   // Everyone using this now uses the replacement...
601   if (DisableChecking)
602     uncheckedReplaceAllUsesWith(Replacement);
603   else
604     replaceAllUsesWith(Replacement);
605   
606   // Delete the old constant!
607   destroyConstant();
608 }
609
610 //===----------------------------------------------------------------------===//
611 //                      Factory Function Implementation
612
613 // ConstantCreator - A class that is used to create constants by
614 // ValueMap*.  This class should be partially specialized if there is
615 // something strange that needs to be done to interface to the ctor for the
616 // constant.
617 //
618 namespace llvm {
619   template<class ConstantClass, class TypeClass, class ValType>
620   struct ConstantCreator {
621     static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
622       return new ConstantClass(Ty, V);
623     }
624   };
625   
626   template<class ConstantClass, class TypeClass>
627   struct ConvertConstantType {
628     static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
629       assert(0 && "This type cannot be converted!\n");
630       abort();
631     }
632   };
633 }
634
635 namespace {
636   template<class ValType, class TypeClass, class ConstantClass>
637   class ValueMap : public AbstractTypeUser {
638     typedef std::pair<const TypeClass*, ValType> MapKey;
639     typedef std::map<MapKey, ConstantClass *> MapTy;
640     typedef typename MapTy::iterator MapIterator;
641     MapTy Map;
642
643     typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
644     AbstractTypeMapTy AbstractTypeMap;
645   public:
646     // getOrCreate - Return the specified constant from the map, creating it if
647     // necessary.
648     ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
649       MapKey Lookup(Ty, V);
650       MapIterator I = Map.lower_bound(Lookup);
651       if (I != Map.end() && I->first == Lookup)
652         return I->second;  // Is it in the map?
653
654       // If no preexisting value, create one now...
655       ConstantClass *Result =
656         ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
657
658
659       /// FIXME: why does this assert fail when loading 176.gcc?
660       //assert(Result->getType() == Ty && "Type specified is not correct!");
661       I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
662
663       // If the type of the constant is abstract, make sure that an entry exists
664       // for it in the AbstractTypeMap.
665       if (Ty->isAbstract()) {
666         typename AbstractTypeMapTy::iterator TI =
667           AbstractTypeMap.lower_bound(Ty);
668
669         if (TI == AbstractTypeMap.end() || TI->first != Ty) {
670           // Add ourselves to the ATU list of the type.
671           cast<DerivedType>(Ty)->addAbstractTypeUser(this);
672
673           AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
674         }
675       }
676       return Result;
677     }
678     
679     void remove(ConstantClass *CP) {
680       // FIXME: This should not use a linear scan.  If this gets to be a
681       // performance problem, someone should look at this.
682       MapIterator I = Map.begin();
683       for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
684         /* empty */;
685       
686       assert(I != Map.end() && "Constant not found in constant table!");
687
688       // Now that we found the entry, make sure this isn't the entry that
689       // the AbstractTypeMap points to.
690       const TypeClass *Ty = I->first.first;
691       if (Ty->isAbstract()) {
692         assert(AbstractTypeMap.count(Ty) &&
693                "Abstract type not in AbstractTypeMap?");
694         MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
695         if (ATMEntryIt == I) {
696           // Yes, we are removing the representative entry for this type.
697           // See if there are any other entries of the same type.
698           MapIterator TmpIt = ATMEntryIt;
699           
700           // First check the entry before this one...
701           if (TmpIt != Map.begin()) {
702             --TmpIt;
703             if (TmpIt->first.first != Ty) // Not the same type, move back...
704               ++TmpIt;
705           }
706           
707           // If we didn't find the same type, try to move forward...
708           if (TmpIt == ATMEntryIt) {
709             ++TmpIt;
710             if (TmpIt == Map.end() || TmpIt->first.first != Ty)
711               --TmpIt;   // No entry afterwards with the same type
712           }
713
714           // If there is another entry in the map of the same abstract type,
715           // update the AbstractTypeMap entry now.
716           if (TmpIt != ATMEntryIt) {
717             ATMEntryIt = TmpIt;
718           } else {
719             // Otherwise, we are removing the last instance of this type
720             // from the table.  Remove from the ATM, and from user list.
721             cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
722             AbstractTypeMap.erase(Ty);
723           }
724         }
725       }
726       
727       Map.erase(I);
728     }
729
730     void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
731       typename AbstractTypeMapTy::iterator I = 
732         AbstractTypeMap.find(cast<TypeClass>(OldTy));
733
734       assert(I != AbstractTypeMap.end() &&
735              "Abstract type not in AbstractTypeMap?");
736
737       // Convert a constant at a time until the last one is gone.  The last one
738       // leaving will remove() itself, causing the AbstractTypeMapEntry to be
739       // eliminated eventually.
740       do {
741         ConvertConstantType<ConstantClass,
742                             TypeClass>::convert(I->second->second,
743                                                 cast<TypeClass>(NewTy));
744
745         I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
746       } while (I != AbstractTypeMap.end());
747     }
748
749     // If the type became concrete without being refined to any other existing
750     // type, we just remove ourselves from the ATU list.
751     void typeBecameConcrete(const DerivedType *AbsTy) {
752       AbsTy->removeAbstractTypeUser(this);
753     }
754
755     void dump() const {
756       std::cerr << "Constant.cpp: ValueMap\n";
757     }
758   };
759 }
760
761
762
763 //---- ConstantUInt::get() and ConstantSInt::get() implementations...
764 //
765 static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
766 static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
767
768 ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
769   return SIntConstants.getOrCreate(Ty, V);
770 }
771
772 ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
773   return UIntConstants.getOrCreate(Ty, V);
774 }
775
776 ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
777   assert(V <= 127 && "Can only be used with very small positive constants!");
778   if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
779   return ConstantUInt::get(Ty, V);
780 }
781
782 //---- ConstantFP::get() implementation...
783 //
784 namespace llvm {
785   template<>
786   struct ConstantCreator<ConstantFP, Type, uint64_t> {
787     static ConstantFP *create(const Type *Ty, uint64_t V) {
788       assert(Ty == Type::DoubleTy);
789       union {
790         double F;
791         uint64_t I;
792       } T;
793       T.I = V;
794       return new ConstantFP(Ty, T.F);
795     }
796   };
797   template<>
798   struct ConstantCreator<ConstantFP, Type, uint32_t> {
799     static ConstantFP *create(const Type *Ty, uint32_t V) {
800       assert(Ty == Type::FloatTy);
801       union {
802         float F;
803         uint32_t I;
804       } T;
805       T.I = V;
806       return new ConstantFP(Ty, T.F);
807     }
808   };
809 }
810
811 static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
812 static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
813
814 ConstantFP *ConstantFP::get(const Type *Ty, double V) {
815   if (Ty == Type::FloatTy) {
816     // Force the value through memory to normalize it.
817     union {
818       float F;
819       uint32_t I;
820     } T;
821     T.F = (float)V;
822     return FloatConstants.getOrCreate(Ty, T.I);
823   } else {
824     assert(Ty == Type::DoubleTy);
825     union {
826       double F;
827       uint64_t I;
828     } T;
829     T.F = V;
830     return DoubleConstants.getOrCreate(Ty, T.I);
831   }
832 }
833
834 //---- ConstantAggregateZero::get() implementation...
835 //
836 namespace llvm {
837   // ConstantAggregateZero does not take extra "value" argument...
838   template<class ValType>
839   struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
840     static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
841       return new ConstantAggregateZero(Ty);
842     }
843   };
844
845   template<>
846   struct ConvertConstantType<ConstantAggregateZero, Type> {
847     static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
848       // Make everyone now use a constant of the new type...
849       Constant *New = ConstantAggregateZero::get(NewTy);
850       assert(New != OldC && "Didn't replace constant??");
851       OldC->uncheckedReplaceAllUsesWith(New);
852       OldC->destroyConstant();     // This constant is now dead, destroy it.
853     }
854   };
855 }
856
857 static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
858
859 Constant *ConstantAggregateZero::get(const Type *Ty) {
860   return AggZeroConstants.getOrCreate(Ty, 0);
861 }
862
863 // destroyConstant - Remove the constant from the constant table...
864 //
865 void ConstantAggregateZero::destroyConstant() {
866   AggZeroConstants.remove(this);
867   destroyConstantImpl();
868 }
869
870 void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
871                                                         bool DisableChecking) {
872   assert(0 && "No uses!");
873   abort();
874 }
875
876
877
878 //---- ConstantArray::get() implementation...
879 //
880 namespace llvm {
881   template<>
882   struct ConvertConstantType<ConstantArray, ArrayType> {
883     static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
884       // Make everyone now use a constant of the new type...
885       std::vector<Constant*> C;
886       for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
887         C.push_back(cast<Constant>(OldC->getOperand(i)));
888       Constant *New = ConstantArray::get(NewTy, C);
889       assert(New != OldC && "Didn't replace constant??");
890       OldC->uncheckedReplaceAllUsesWith(New);
891       OldC->destroyConstant();    // This constant is now dead, destroy it.
892     }
893   };
894 }
895
896 static ValueMap<std::vector<Constant*>, ArrayType,
897                 ConstantArray> ArrayConstants;
898
899 Constant *ConstantArray::get(const ArrayType *Ty,
900                              const std::vector<Constant*> &V) {
901   // If this is an all-zero array, return a ConstantAggregateZero object
902   if (!V.empty()) {
903     Constant *C = V[0];
904     if (!C->isNullValue())
905       return ArrayConstants.getOrCreate(Ty, V);
906     for (unsigned i = 1, e = V.size(); i != e; ++i)
907       if (V[i] != C)
908         return ArrayConstants.getOrCreate(Ty, V);
909   }
910   return ConstantAggregateZero::get(Ty);
911 }
912
913 // destroyConstant - Remove the constant from the constant table...
914 //
915 void ConstantArray::destroyConstant() {
916   ArrayConstants.remove(this);
917   destroyConstantImpl();
918 }
919
920 // ConstantArray::get(const string&) - Return an array that is initialized to
921 // contain the specified string.  A null terminator is added to the specified
922 // string so that it may be used in a natural way...
923 //
924 Constant *ConstantArray::get(const std::string &Str) {
925   std::vector<Constant*> ElementVals;
926
927   for (unsigned i = 0; i < Str.length(); ++i)
928     ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
929
930   // Add a null terminator to the string...
931   ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
932
933   ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
934   return ConstantArray::get(ATy, ElementVals);
935 }
936
937 /// isString - This method returns true if the array is an array of sbyte or
938 /// ubyte, and if the elements of the array are all ConstantInt's.
939 bool ConstantArray::isString() const {
940   // Check the element type for sbyte or ubyte...
941   if (getType()->getElementType() != Type::UByteTy &&
942       getType()->getElementType() != Type::SByteTy)
943     return false;
944   // Check the elements to make sure they are all integers, not constant
945   // expressions.
946   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
947     if (!isa<ConstantInt>(getOperand(i)))
948       return false;
949   return true;
950 }
951
952 // getAsString - If the sub-element type of this array is either sbyte or ubyte,
953 // then this method converts the array to an std::string and returns it.
954 // Otherwise, it asserts out.
955 //
956 std::string ConstantArray::getAsString() const {
957   assert(isString() && "Not a string!");
958   std::string Result;
959   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
960     Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
961   return Result;
962 }
963
964
965 //---- ConstantStruct::get() implementation...
966 //
967
968 namespace llvm {
969   template<>
970   struct ConvertConstantType<ConstantStruct, StructType> {
971     static void convert(ConstantStruct *OldC, const StructType *NewTy) {
972       // Make everyone now use a constant of the new type...
973       std::vector<Constant*> C;
974       for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
975         C.push_back(cast<Constant>(OldC->getOperand(i)));
976       Constant *New = ConstantStruct::get(NewTy, C);
977       assert(New != OldC && "Didn't replace constant??");
978       
979       OldC->uncheckedReplaceAllUsesWith(New);
980       OldC->destroyConstant();    // This constant is now dead, destroy it.
981     }
982   };
983 }
984
985 static ValueMap<std::vector<Constant*>, StructType, 
986                 ConstantStruct> StructConstants;
987
988 Constant *ConstantStruct::get(const StructType *Ty,
989                               const std::vector<Constant*> &V) {
990   // Create a ConstantAggregateZero value if all elements are zeros...
991   for (unsigned i = 0, e = V.size(); i != e; ++i)
992     if (!V[i]->isNullValue())
993       return StructConstants.getOrCreate(Ty, V);
994
995   return ConstantAggregateZero::get(Ty);
996 }
997
998 // destroyConstant - Remove the constant from the constant table...
999 //
1000 void ConstantStruct::destroyConstant() {
1001   StructConstants.remove(this);
1002   destroyConstantImpl();
1003 }
1004
1005 //---- ConstantPointerNull::get() implementation...
1006 //
1007
1008 namespace llvm {
1009   // ConstantPointerNull does not take extra "value" argument...
1010   template<class ValType>
1011   struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1012     static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1013       return new ConstantPointerNull(Ty);
1014     }
1015   };
1016
1017   template<>
1018   struct ConvertConstantType<ConstantPointerNull, PointerType> {
1019     static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1020       // Make everyone now use a constant of the new type...
1021       Constant *New = ConstantPointerNull::get(NewTy);
1022       assert(New != OldC && "Didn't replace constant??");
1023       OldC->uncheckedReplaceAllUsesWith(New);
1024       OldC->destroyConstant();     // This constant is now dead, destroy it.
1025     }
1026   };
1027 }
1028
1029 static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
1030
1031 ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
1032   return NullPtrConstants.getOrCreate(Ty, 0);
1033 }
1034
1035 // destroyConstant - Remove the constant from the constant table...
1036 //
1037 void ConstantPointerNull::destroyConstant() {
1038   NullPtrConstants.remove(this);
1039   destroyConstantImpl();
1040 }
1041
1042
1043 //---- ConstantPointerRef::get() implementation...
1044 //
1045 ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
1046   assert(GV->getParent() && "Global Value must be attached to a module!");
1047   
1048   // The Module handles the pointer reference sharing...
1049   return GV->getParent()->getConstantPointerRef(GV);
1050 }
1051
1052 // destroyConstant - Remove the constant from the constant table...
1053 //
1054 void ConstantPointerRef::destroyConstant() {
1055   getValue()->getParent()->destroyConstantPointerRef(this);
1056   destroyConstantImpl();
1057 }
1058
1059
1060 //---- ConstantExpr::get() implementations...
1061 //
1062 typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
1063
1064 namespace llvm {
1065   template<>
1066   struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1067     static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1068       if (V.first == Instruction::Cast)
1069         return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
1070       if ((V.first >= Instruction::BinaryOpsBegin &&
1071            V.first < Instruction::BinaryOpsEnd) ||
1072           V.first == Instruction::Shl || V.first == Instruction::Shr)
1073         return new ConstantExpr(V.first, V.second[0], V.second[1]);
1074       if (V.first == Instruction::Select)
1075         return new ConstantExpr(V.second[0], V.second[1], V.second[2]);
1076       
1077       assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
1078       
1079       std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
1080       return new ConstantExpr(V.second[0], IdxList, Ty);
1081     }
1082   };
1083
1084   template<>
1085   struct ConvertConstantType<ConstantExpr, Type> {
1086     static void convert(ConstantExpr *OldC, const Type *NewTy) {
1087       Constant *New;
1088       switch (OldC->getOpcode()) {
1089       case Instruction::Cast:
1090         New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1091         break;
1092       case Instruction::Select:
1093         New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1094                                         OldC->getOperand(1),
1095                                         OldC->getOperand(2));
1096         break;
1097       case Instruction::Shl:
1098       case Instruction::Shr:
1099         New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1100                                      OldC->getOperand(0), OldC->getOperand(1));
1101         break;
1102       default:
1103         assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1104                OldC->getOpcode() < Instruction::BinaryOpsEnd);
1105         New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1106                                   OldC->getOperand(1));
1107         break;
1108       case Instruction::GetElementPtr:
1109         // Make everyone now use a constant of the new type... 
1110         std::vector<Constant*> C;
1111         for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
1112           C.push_back(cast<Constant>(OldC->getOperand(i)));
1113         New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
1114         break;
1115       }
1116       
1117       assert(New != OldC && "Didn't replace constant??");
1118       OldC->uncheckedReplaceAllUsesWith(New);
1119       OldC->destroyConstant();    // This constant is now dead, destroy it.
1120     }
1121   };
1122 } // end namespace llvm
1123
1124
1125 static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
1126
1127 Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
1128   assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1129
1130   if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1131     return FC;          // Fold a few common cases...
1132
1133   // Look up the constant in the table first to ensure uniqueness
1134   std::vector<Constant*> argVec(1, C);
1135   ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1136   return ExprConstants.getOrCreate(Ty, Key);
1137 }
1138
1139 Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
1140   assert(C->getType()->isInteger() && Ty->isInteger() &&
1141          C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1142          "This is an illegal sign extension!");
1143   C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1144   return ConstantExpr::getCast(C, Ty);
1145 }
1146
1147 Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
1148   assert(C->getType()->isInteger() && Ty->isInteger() &&
1149          C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1150          "This is an illegal zero extension!");
1151   C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
1152   return ConstantExpr::getCast(C, Ty);
1153 }
1154
1155 Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1156                               Constant *C1, Constant *C2) {
1157   if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1158     return getShiftTy(ReqTy, Opcode, C1, C2);
1159   // Check the operands for consistency first
1160   assert((Opcode >= Instruction::BinaryOpsBegin &&
1161           Opcode < Instruction::BinaryOpsEnd) &&
1162          "Invalid opcode in binary constant expression");
1163   assert(C1->getType() == C2->getType() &&
1164          "Operand types in binary constant expression should match");
1165
1166   if (ReqTy == C1->getType())
1167     if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1168       return FC;          // Fold a few common cases...
1169
1170   std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
1171   ExprMapKeyType Key = std::make_pair(Opcode, argVec);
1172   return ExprConstants.getOrCreate(ReqTy, Key);
1173 }
1174
1175 Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1176                                     Constant *V1, Constant *V2) {
1177   assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1178   assert(V1->getType() == V2->getType() && "Select value types must match!");
1179   assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1180
1181   if (ReqTy == V1->getType())
1182     if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1183       return SC;        // Fold common cases
1184
1185   std::vector<Constant*> argVec(3, C);
1186   argVec[1] = V1;
1187   argVec[2] = V2;
1188   ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1189   return ExprConstants.getOrCreate(ReqTy, Key);
1190 }
1191
1192 /// getShiftTy - Return a shift left or shift right constant expr
1193 Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1194                                    Constant *C1, Constant *C2) {
1195   // Check the operands for consistency first
1196   assert((Opcode == Instruction::Shl ||
1197           Opcode == Instruction::Shr) &&
1198          "Invalid opcode in binary constant expression");
1199   assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1200          "Invalid operand types for Shift constant expr!");
1201
1202   if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1203     return FC;          // Fold a few common cases...
1204
1205   // Look up the constant in the table first to ensure uniqueness
1206   std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
1207   ExprMapKeyType Key = std::make_pair(Opcode, argVec);
1208   return ExprConstants.getOrCreate(ReqTy, Key);
1209 }
1210
1211
1212 Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1213                                         const std::vector<Constant*> &IdxList) {
1214   assert(GetElementPtrInst::getIndexedType(C->getType(),
1215                    std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
1216          "GEP indices invalid!");
1217
1218   if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1219     return FC;          // Fold a few common cases...
1220
1221   assert(isa<PointerType>(C->getType()) &&
1222          "Non-pointer type for constant GetElementPtr expression");
1223   // Look up the constant in the table first to ensure uniqueness
1224   std::vector<Constant*> argVec(1, C);
1225   argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
1226   const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
1227   return ExprConstants.getOrCreate(ReqTy, Key);
1228 }
1229
1230 Constant *ConstantExpr::getGetElementPtr(Constant *C,
1231                                          const std::vector<Constant*> &IdxList){
1232   // Get the result type of the getelementptr!
1233   std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1234
1235   const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1236                                                      true);
1237   assert(Ty && "GEP indices invalid!");
1238   return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1239 }
1240
1241
1242 // destroyConstant - Remove the constant from the constant table...
1243 //
1244 void ConstantExpr::destroyConstant() {
1245   ExprConstants.remove(this);
1246   destroyConstantImpl();
1247 }
1248
1249 const char *ConstantExpr::getOpcodeName() const {
1250   return Instruction::getOpcodeName(getOpcode());
1251 }