Add ConstantExpr::get(Sign|Zero)Extend methods
[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
376
377
378 //===----------------------------------------------------------------------===//
379 //                           classof implementations
380
381 bool ConstantIntegral::classof(const Constant *CPV) {
382   return CPV->getType()->isIntegral() && !isa<ConstantExpr>(CPV);
383 }
384
385 bool ConstantInt::classof(const Constant *CPV) {
386   return CPV->getType()->isInteger() && !isa<ConstantExpr>(CPV);
387 }
388 bool ConstantSInt::classof(const Constant *CPV) {
389   return CPV->getType()->isSigned() && !isa<ConstantExpr>(CPV);
390 }
391 bool ConstantUInt::classof(const Constant *CPV) {
392   return CPV->getType()->isUnsigned() && !isa<ConstantExpr>(CPV);
393 }
394 bool ConstantFP::classof(const Constant *CPV) {
395   const Type *Ty = CPV->getType();
396   return ((Ty == Type::FloatTy || Ty == Type::DoubleTy) &&
397           !isa<ConstantExpr>(CPV));
398 }
399 bool ConstantAggregateZero::classof(const Constant *CPV) {
400   return (isa<ArrayType>(CPV->getType()) || isa<StructType>(CPV->getType())) &&
401          CPV->isNullValue();
402 }
403 bool ConstantArray::classof(const Constant *CPV) {
404   return isa<ArrayType>(CPV->getType()) && !CPV->isNullValue();
405 }
406 bool ConstantStruct::classof(const Constant *CPV) {
407   return isa<StructType>(CPV->getType()) && !CPV->isNullValue();
408 }
409
410 bool ConstantPointerNull::classof(const Constant *CPV) {
411   return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
412          CPV->getNumOperands() == 0;
413 }
414
415 bool ConstantPointerRef::classof(const Constant *CPV) {
416   return isa<PointerType>(CPV->getType()) && !isa<ConstantExpr>(CPV) &&
417          CPV->getNumOperands() == 1;
418 }
419
420
421
422 //===----------------------------------------------------------------------===//
423 //                      isValueValidForType implementations
424
425 bool ConstantSInt::isValueValidForType(const Type *Ty, int64_t Val) {
426   switch (Ty->getPrimitiveID()) {
427   default:
428     return false;         // These can't be represented as integers!!!
429
430     // Signed types...
431   case Type::SByteTyID:
432     return (Val <= INT8_MAX && Val >= INT8_MIN);
433   case Type::ShortTyID:
434     return (Val <= INT16_MAX && Val >= INT16_MIN);
435   case Type::IntTyID:
436     return (Val <= INT32_MAX && Val >= INT32_MIN);
437   case Type::LongTyID:
438     return true;          // This is the largest type...
439   }
440   assert(0 && "WTF?");
441   return false;
442 }
443
444 bool ConstantUInt::isValueValidForType(const Type *Ty, uint64_t Val) {
445   switch (Ty->getPrimitiveID()) {
446   default:
447     return false;         // These can't be represented as integers!!!
448
449     // Unsigned types...
450   case Type::UByteTyID:
451     return (Val <= UINT8_MAX);
452   case Type::UShortTyID:
453     return (Val <= UINT16_MAX);
454   case Type::UIntTyID:
455     return (Val <= UINT32_MAX);
456   case Type::ULongTyID:
457     return true;          // This is the largest type...
458   }
459   assert(0 && "WTF?");
460   return false;
461 }
462
463 bool ConstantFP::isValueValidForType(const Type *Ty, double Val) {
464   switch (Ty->getPrimitiveID()) {
465   default:
466     return false;         // These can't be represented as floating point!
467
468     // TODO: Figure out how to test if a double can be cast to a float!
469   case Type::FloatTyID:
470   case Type::DoubleTyID:
471     return true;          // This is the largest type...
472   }
473 };
474
475 //===----------------------------------------------------------------------===//
476 //                replaceUsesOfWithOnConstant implementations
477
478 void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To,
479                                                 bool DisableChecking) {
480   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
481
482   std::vector<Constant*> Values;
483   Values.reserve(getValues().size());  // Build replacement array...
484   for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
485     Constant *Val = cast<Constant>(getValues()[i]);
486     if (Val == From) Val = cast<Constant>(To);
487     Values.push_back(Val);
488   }
489   
490   Constant *Replacement = ConstantArray::get(getType(), Values);
491   assert(Replacement != this && "I didn't contain From!");
492
493   // Everyone using this now uses the replacement...
494   if (DisableChecking)
495     uncheckedReplaceAllUsesWith(Replacement);
496   else
497     replaceAllUsesWith(Replacement);
498   
499   // Delete the old constant!
500   destroyConstant();  
501 }
502
503 void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To,
504                                                  bool DisableChecking) {
505   assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!");
506
507   std::vector<Constant*> Values;
508   Values.reserve(getValues().size());
509   for (unsigned i = 0, e = getValues().size(); i != e; ++i) {
510     Constant *Val = cast<Constant>(getValues()[i]);
511     if (Val == From) Val = cast<Constant>(To);
512     Values.push_back(Val);
513   }
514   
515   Constant *Replacement = ConstantStruct::get(getType(), Values);
516   assert(Replacement != this && "I didn't contain From!");
517
518   // Everyone using this now uses the replacement...
519   if (DisableChecking)
520     uncheckedReplaceAllUsesWith(Replacement);
521   else
522     replaceAllUsesWith(Replacement);
523   
524   // Delete the old constant!
525   destroyConstant();
526 }
527
528 void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To,
529                                                      bool DisableChecking) {
530   if (isa<GlobalValue>(To)) {
531     assert(From == getOperand(0) && "Doesn't contain from!");
532     ConstantPointerRef *Replacement =
533       ConstantPointerRef::get(cast<GlobalValue>(To));
534     
535     // Everyone using this now uses the replacement...
536     if (DisableChecking)
537       uncheckedReplaceAllUsesWith(Replacement);
538     else
539       replaceAllUsesWith(Replacement);
540     
541   } else {
542     // Just replace ourselves with the To value specified.
543     if (DisableChecking)
544       uncheckedReplaceAllUsesWith(To);
545     else
546       replaceAllUsesWith(To);
547   }
548
549   // Delete the old constant!
550   destroyConstant();
551 }
552
553 void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV,
554                                                bool DisableChecking) {
555   assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
556   Constant *To = cast<Constant>(ToV);
557
558   Constant *Replacement = 0;
559   if (getOpcode() == Instruction::GetElementPtr) {
560     std::vector<Constant*> Indices;
561     Constant *Pointer = getOperand(0);
562     Indices.reserve(getNumOperands()-1);
563     if (Pointer == From) Pointer = To;
564     
565     for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
566       Constant *Val = getOperand(i);
567       if (Val == From) Val = To;
568       Indices.push_back(Val);
569     }
570     Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices);
571   } else if (getOpcode() == Instruction::Cast) {
572     assert(getOperand(0) == From && "Cast only has one use!");
573     Replacement = ConstantExpr::getCast(To, getType());
574   } else if (getOpcode() == Instruction::Select) {
575     Constant *C1 = getOperand(0);
576     Constant *C2 = getOperand(1);
577     Constant *C3 = getOperand(2);
578     if (C1 == From) C1 = To;
579     if (C2 == From) C2 = To;
580     if (C3 == From) C3 = To;
581     Replacement = ConstantExpr::getSelect(C1, C2, C3);
582   } else if (getNumOperands() == 2) {
583     Constant *C1 = getOperand(0);
584     Constant *C2 = getOperand(1);
585     if (C1 == From) C1 = To;
586     if (C2 == From) C2 = To;
587     Replacement = ConstantExpr::get(getOpcode(), C1, C2);
588   } else {
589     assert(0 && "Unknown ConstantExpr type!");
590     return;
591   }
592   
593   assert(Replacement != this && "I didn't contain From!");
594
595   // Everyone using this now uses the replacement...
596   if (DisableChecking)
597     uncheckedReplaceAllUsesWith(Replacement);
598   else
599     replaceAllUsesWith(Replacement);
600   
601   // Delete the old constant!
602   destroyConstant();
603 }
604
605 //===----------------------------------------------------------------------===//
606 //                      Factory Function Implementation
607
608 // ConstantCreator - A class that is used to create constants by
609 // ValueMap*.  This class should be partially specialized if there is
610 // something strange that needs to be done to interface to the ctor for the
611 // constant.
612 //
613 namespace llvm {
614   template<class ConstantClass, class TypeClass, class ValType>
615   struct ConstantCreator {
616     static ConstantClass *create(const TypeClass *Ty, const ValType &V) {
617       return new ConstantClass(Ty, V);
618     }
619   };
620   
621   template<class ConstantClass, class TypeClass>
622   struct ConvertConstantType {
623     static void convert(ConstantClass *OldC, const TypeClass *NewTy) {
624       assert(0 && "This type cannot be converted!\n");
625       abort();
626     }
627   };
628 }
629
630 namespace {
631   template<class ValType, class TypeClass, class ConstantClass>
632   class ValueMap : public AbstractTypeUser {
633     typedef std::pair<const TypeClass*, ValType> MapKey;
634     typedef std::map<MapKey, ConstantClass *> MapTy;
635     typedef typename MapTy::iterator MapIterator;
636     MapTy Map;
637
638     typedef std::map<const TypeClass*, MapIterator> AbstractTypeMapTy;
639     AbstractTypeMapTy AbstractTypeMap;
640   public:
641     // getOrCreate - Return the specified constant from the map, creating it if
642     // necessary.
643     ConstantClass *getOrCreate(const TypeClass *Ty, const ValType &V) {
644       MapKey Lookup(Ty, V);
645       MapIterator I = Map.lower_bound(Lookup);
646       if (I != Map.end() && I->first == Lookup)
647         return I->second;  // Is it in the map?
648
649       // If no preexisting value, create one now...
650       ConstantClass *Result =
651         ConstantCreator<ConstantClass,TypeClass,ValType>::create(Ty, V);
652
653
654       /// FIXME: why does this assert fail when loading 176.gcc?
655       //assert(Result->getType() == Ty && "Type specified is not correct!");
656       I = Map.insert(I, std::make_pair(MapKey(Ty, V), Result));
657
658       // If the type of the constant is abstract, make sure that an entry exists
659       // for it in the AbstractTypeMap.
660       if (Ty->isAbstract()) {
661         typename AbstractTypeMapTy::iterator TI =
662           AbstractTypeMap.lower_bound(Ty);
663
664         if (TI == AbstractTypeMap.end() || TI->first != Ty) {
665           // Add ourselves to the ATU list of the type.
666           cast<DerivedType>(Ty)->addAbstractTypeUser(this);
667
668           AbstractTypeMap.insert(TI, std::make_pair(Ty, I));
669         }
670       }
671       return Result;
672     }
673     
674     void remove(ConstantClass *CP) {
675       // FIXME: This should not use a linear scan.  If this gets to be a
676       // performance problem, someone should look at this.
677       MapIterator I = Map.begin();
678       for (MapIterator E = Map.end(); I != E && I->second != CP; ++I)
679         /* empty */;
680       
681       assert(I != Map.end() && "Constant not found in constant table!");
682
683       // Now that we found the entry, make sure this isn't the entry that
684       // the AbstractTypeMap points to.
685       const TypeClass *Ty = I->first.first;
686       if (Ty->isAbstract()) {
687         assert(AbstractTypeMap.count(Ty) &&
688                "Abstract type not in AbstractTypeMap?");
689         MapIterator &ATMEntryIt = AbstractTypeMap[Ty];
690         if (ATMEntryIt == I) {
691           // Yes, we are removing the representative entry for this type.
692           // See if there are any other entries of the same type.
693           MapIterator TmpIt = ATMEntryIt;
694           
695           // First check the entry before this one...
696           if (TmpIt != Map.begin()) {
697             --TmpIt;
698             if (TmpIt->first.first != Ty) // Not the same type, move back...
699               ++TmpIt;
700           }
701           
702           // If we didn't find the same type, try to move forward...
703           if (TmpIt == ATMEntryIt) {
704             ++TmpIt;
705             if (TmpIt == Map.end() || TmpIt->first.first != Ty)
706               --TmpIt;   // No entry afterwards with the same type
707           }
708
709           // If there is another entry in the map of the same abstract type,
710           // update the AbstractTypeMap entry now.
711           if (TmpIt != ATMEntryIt) {
712             ATMEntryIt = TmpIt;
713           } else {
714             // Otherwise, we are removing the last instance of this type
715             // from the table.  Remove from the ATM, and from user list.
716             cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
717             AbstractTypeMap.erase(Ty);
718           }
719         }
720       }
721       
722       Map.erase(I);
723     }
724
725     void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
726       typename AbstractTypeMapTy::iterator I = 
727         AbstractTypeMap.find(cast<TypeClass>(OldTy));
728
729       assert(I != AbstractTypeMap.end() &&
730              "Abstract type not in AbstractTypeMap?");
731
732       // Convert a constant at a time until the last one is gone.  The last one
733       // leaving will remove() itself, causing the AbstractTypeMapEntry to be
734       // eliminated eventually.
735       do {
736         ConvertConstantType<ConstantClass,
737                             TypeClass>::convert(I->second->second,
738                                                 cast<TypeClass>(NewTy));
739
740         I = AbstractTypeMap.find(cast<TypeClass>(OldTy));
741       } while (I != AbstractTypeMap.end());
742     }
743
744     // If the type became concrete without being refined to any other existing
745     // type, we just remove ourselves from the ATU list.
746     void typeBecameConcrete(const DerivedType *AbsTy) {
747       AbsTy->removeAbstractTypeUser(this);
748     }
749
750     void dump() const {
751       std::cerr << "Constant.cpp: ValueMap\n";
752     }
753   };
754 }
755
756
757
758 //---- ConstantUInt::get() and ConstantSInt::get() implementations...
759 //
760 static ValueMap< int64_t, Type, ConstantSInt> SIntConstants;
761 static ValueMap<uint64_t, Type, ConstantUInt> UIntConstants;
762
763 ConstantSInt *ConstantSInt::get(const Type *Ty, int64_t V) {
764   return SIntConstants.getOrCreate(Ty, V);
765 }
766
767 ConstantUInt *ConstantUInt::get(const Type *Ty, uint64_t V) {
768   return UIntConstants.getOrCreate(Ty, V);
769 }
770
771 ConstantInt *ConstantInt::get(const Type *Ty, unsigned char V) {
772   assert(V <= 127 && "Can only be used with very small positive constants!");
773   if (Ty->isSigned()) return ConstantSInt::get(Ty, V);
774   return ConstantUInt::get(Ty, V);
775 }
776
777 //---- ConstantFP::get() implementation...
778 //
779 namespace llvm {
780   template<>
781   struct ConstantCreator<ConstantFP, Type, uint64_t> {
782     static ConstantFP *create(const Type *Ty, uint64_t V) {
783       assert(Ty == Type::DoubleTy);
784       union {
785         double F;
786         uint64_t I;
787       } T;
788       T.I = V;
789       return new ConstantFP(Ty, T.F);
790     }
791   };
792   template<>
793   struct ConstantCreator<ConstantFP, Type, uint32_t> {
794     static ConstantFP *create(const Type *Ty, uint32_t V) {
795       assert(Ty == Type::FloatTy);
796       union {
797         float F;
798         uint32_t I;
799       } T;
800       T.I = V;
801       return new ConstantFP(Ty, T.F);
802     }
803   };
804 }
805
806 static ValueMap<uint64_t, Type, ConstantFP> DoubleConstants;
807 static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
808
809 ConstantFP *ConstantFP::get(const Type *Ty, double V) {
810   if (Ty == Type::FloatTy) {
811     // Force the value through memory to normalize it.
812     union {
813       float F;
814       uint32_t I;
815     } T;
816     T.F = (float)V;
817     return FloatConstants.getOrCreate(Ty, T.I);
818   } else {
819     assert(Ty == Type::DoubleTy);
820     union {
821       double F;
822       uint64_t I;
823     } T;
824     T.F = V;
825     return DoubleConstants.getOrCreate(Ty, T.I);
826   }
827 }
828
829 //---- ConstantAggregateZero::get() implementation...
830 //
831 namespace llvm {
832   // ConstantAggregateZero does not take extra "value" argument...
833   template<class ValType>
834   struct ConstantCreator<ConstantAggregateZero, Type, ValType> {
835     static ConstantAggregateZero *create(const Type *Ty, const ValType &V){
836       return new ConstantAggregateZero(Ty);
837     }
838   };
839
840   template<>
841   struct ConvertConstantType<ConstantAggregateZero, Type> {
842     static void convert(ConstantAggregateZero *OldC, const Type *NewTy) {
843       // Make everyone now use a constant of the new type...
844       Constant *New = ConstantAggregateZero::get(NewTy);
845       assert(New != OldC && "Didn't replace constant??");
846       OldC->uncheckedReplaceAllUsesWith(New);
847       OldC->destroyConstant();     // This constant is now dead, destroy it.
848     }
849   };
850 }
851
852 static ValueMap<char, Type, ConstantAggregateZero> AggZeroConstants;
853
854 Constant *ConstantAggregateZero::get(const Type *Ty) {
855   return AggZeroConstants.getOrCreate(Ty, 0);
856 }
857
858 // destroyConstant - Remove the constant from the constant table...
859 //
860 void ConstantAggregateZero::destroyConstant() {
861   AggZeroConstants.remove(this);
862   destroyConstantImpl();
863 }
864
865 void ConstantAggregateZero::replaceUsesOfWithOnConstant(Value *From, Value *To,
866                                                         bool DisableChecking) {
867   assert(0 && "No uses!");
868   abort();
869 }
870
871
872
873 //---- ConstantArray::get() implementation...
874 //
875 namespace llvm {
876   template<>
877   struct ConvertConstantType<ConstantArray, ArrayType> {
878     static void convert(ConstantArray *OldC, const ArrayType *NewTy) {
879       // Make everyone now use a constant of the new type...
880       std::vector<Constant*> C;
881       for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
882         C.push_back(cast<Constant>(OldC->getOperand(i)));
883       Constant *New = ConstantArray::get(NewTy, C);
884       assert(New != OldC && "Didn't replace constant??");
885       OldC->uncheckedReplaceAllUsesWith(New);
886       OldC->destroyConstant();    // This constant is now dead, destroy it.
887     }
888   };
889 }
890
891 static ValueMap<std::vector<Constant*>, ArrayType,
892                 ConstantArray> ArrayConstants;
893
894 Constant *ConstantArray::get(const ArrayType *Ty,
895                              const std::vector<Constant*> &V) {
896   // If this is an all-zero array, return a ConstantAggregateZero object
897   if (!V.empty()) {
898     Constant *C = V[0];
899     if (!C->isNullValue())
900       return ArrayConstants.getOrCreate(Ty, V);
901     for (unsigned i = 1, e = V.size(); i != e; ++i)
902       if (V[i] != C)
903         return ArrayConstants.getOrCreate(Ty, V);
904   }
905   return ConstantAggregateZero::get(Ty);
906 }
907
908 // destroyConstant - Remove the constant from the constant table...
909 //
910 void ConstantArray::destroyConstant() {
911   ArrayConstants.remove(this);
912   destroyConstantImpl();
913 }
914
915 // ConstantArray::get(const string&) - Return an array that is initialized to
916 // contain the specified string.  A null terminator is added to the specified
917 // string so that it may be used in a natural way...
918 //
919 Constant *ConstantArray::get(const std::string &Str) {
920   std::vector<Constant*> ElementVals;
921
922   for (unsigned i = 0; i < Str.length(); ++i)
923     ElementVals.push_back(ConstantSInt::get(Type::SByteTy, Str[i]));
924
925   // Add a null terminator to the string...
926   ElementVals.push_back(ConstantSInt::get(Type::SByteTy, 0));
927
928   ArrayType *ATy = ArrayType::get(Type::SByteTy, Str.length()+1);
929   return ConstantArray::get(ATy, ElementVals);
930 }
931
932 /// isString - This method returns true if the array is an array of sbyte or
933 /// ubyte, and if the elements of the array are all ConstantInt's.
934 bool ConstantArray::isString() const {
935   // Check the element type for sbyte or ubyte...
936   if (getType()->getElementType() != Type::UByteTy &&
937       getType()->getElementType() != Type::SByteTy)
938     return false;
939   // Check the elements to make sure they are all integers, not constant
940   // expressions.
941   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
942     if (!isa<ConstantInt>(getOperand(i)))
943       return false;
944   return true;
945 }
946
947 // getAsString - If the sub-element type of this array is either sbyte or ubyte,
948 // then this method converts the array to an std::string and returns it.
949 // Otherwise, it asserts out.
950 //
951 std::string ConstantArray::getAsString() const {
952   assert(isString() && "Not a string!");
953   std::string Result;
954   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
955     Result += (char)cast<ConstantInt>(getOperand(i))->getRawValue();
956   return Result;
957 }
958
959
960 //---- ConstantStruct::get() implementation...
961 //
962
963 namespace llvm {
964   template<>
965   struct ConvertConstantType<ConstantStruct, StructType> {
966     static void convert(ConstantStruct *OldC, const StructType *NewTy) {
967       // Make everyone now use a constant of the new type...
968       std::vector<Constant*> C;
969       for (unsigned i = 0, e = OldC->getNumOperands(); i != e; ++i)
970         C.push_back(cast<Constant>(OldC->getOperand(i)));
971       Constant *New = ConstantStruct::get(NewTy, C);
972       assert(New != OldC && "Didn't replace constant??");
973       
974       OldC->uncheckedReplaceAllUsesWith(New);
975       OldC->destroyConstant();    // This constant is now dead, destroy it.
976     }
977   };
978 }
979
980 static ValueMap<std::vector<Constant*>, StructType, 
981                 ConstantStruct> StructConstants;
982
983 Constant *ConstantStruct::get(const StructType *Ty,
984                               const std::vector<Constant*> &V) {
985   // Create a ConstantAggregateZero value if all elements are zeros...
986   for (unsigned i = 0, e = V.size(); i != e; ++i)
987     if (!V[i]->isNullValue())
988       return StructConstants.getOrCreate(Ty, V);
989
990   return ConstantAggregateZero::get(Ty);
991 }
992
993 // destroyConstant - Remove the constant from the constant table...
994 //
995 void ConstantStruct::destroyConstant() {
996   StructConstants.remove(this);
997   destroyConstantImpl();
998 }
999
1000 //---- ConstantPointerNull::get() implementation...
1001 //
1002
1003 namespace llvm {
1004   // ConstantPointerNull does not take extra "value" argument...
1005   template<class ValType>
1006   struct ConstantCreator<ConstantPointerNull, PointerType, ValType> {
1007     static ConstantPointerNull *create(const PointerType *Ty, const ValType &V){
1008       return new ConstantPointerNull(Ty);
1009     }
1010   };
1011
1012   template<>
1013   struct ConvertConstantType<ConstantPointerNull, PointerType> {
1014     static void convert(ConstantPointerNull *OldC, const PointerType *NewTy) {
1015       // Make everyone now use a constant of the new type...
1016       Constant *New = ConstantPointerNull::get(NewTy);
1017       assert(New != OldC && "Didn't replace constant??");
1018       OldC->uncheckedReplaceAllUsesWith(New);
1019       OldC->destroyConstant();     // This constant is now dead, destroy it.
1020     }
1021   };
1022 }
1023
1024 static ValueMap<char, PointerType, ConstantPointerNull> NullPtrConstants;
1025
1026 ConstantPointerNull *ConstantPointerNull::get(const PointerType *Ty) {
1027   return NullPtrConstants.getOrCreate(Ty, 0);
1028 }
1029
1030 // destroyConstant - Remove the constant from the constant table...
1031 //
1032 void ConstantPointerNull::destroyConstant() {
1033   NullPtrConstants.remove(this);
1034   destroyConstantImpl();
1035 }
1036
1037
1038 //---- ConstantPointerRef::get() implementation...
1039 //
1040 ConstantPointerRef *ConstantPointerRef::get(GlobalValue *GV) {
1041   assert(GV->getParent() && "Global Value must be attached to a module!");
1042   
1043   // The Module handles the pointer reference sharing...
1044   return GV->getParent()->getConstantPointerRef(GV);
1045 }
1046
1047 // destroyConstant - Remove the constant from the constant table...
1048 //
1049 void ConstantPointerRef::destroyConstant() {
1050   getValue()->getParent()->destroyConstantPointerRef(this);
1051   destroyConstantImpl();
1052 }
1053
1054
1055 //---- ConstantExpr::get() implementations...
1056 //
1057 typedef std::pair<unsigned, std::vector<Constant*> > ExprMapKeyType;
1058
1059 namespace llvm {
1060   template<>
1061   struct ConstantCreator<ConstantExpr, Type, ExprMapKeyType> {
1062     static ConstantExpr *create(const Type *Ty, const ExprMapKeyType &V) {
1063       if (V.first == Instruction::Cast)
1064         return new ConstantExpr(Instruction::Cast, V.second[0], Ty);
1065       if ((V.first >= Instruction::BinaryOpsBegin &&
1066            V.first < Instruction::BinaryOpsEnd) ||
1067           V.first == Instruction::Shl || V.first == Instruction::Shr)
1068         return new ConstantExpr(V.first, V.second[0], V.second[1]);
1069       if (V.first == Instruction::Select)
1070         return new ConstantExpr(V.second[0], V.second[1], V.second[2]);
1071       
1072       assert(V.first == Instruction::GetElementPtr && "Invalid ConstantExpr!");
1073       
1074       std::vector<Constant*> IdxList(V.second.begin()+1, V.second.end());
1075       return new ConstantExpr(V.second[0], IdxList, Ty);
1076     }
1077   };
1078
1079   template<>
1080   struct ConvertConstantType<ConstantExpr, Type> {
1081     static void convert(ConstantExpr *OldC, const Type *NewTy) {
1082       Constant *New;
1083       switch (OldC->getOpcode()) {
1084       case Instruction::Cast:
1085         New = ConstantExpr::getCast(OldC->getOperand(0), NewTy);
1086         break;
1087       case Instruction::Select:
1088         New = ConstantExpr::getSelectTy(NewTy, OldC->getOperand(0),
1089                                         OldC->getOperand(1),
1090                                         OldC->getOperand(2));
1091         break;
1092       case Instruction::Shl:
1093       case Instruction::Shr:
1094         New = ConstantExpr::getShiftTy(NewTy, OldC->getOpcode(),
1095                                      OldC->getOperand(0), OldC->getOperand(1));
1096         break;
1097       default:
1098         assert(OldC->getOpcode() >= Instruction::BinaryOpsBegin &&
1099                OldC->getOpcode() < Instruction::BinaryOpsEnd);
1100         New = ConstantExpr::getTy(NewTy, OldC->getOpcode(), OldC->getOperand(0),
1101                                   OldC->getOperand(1));
1102         break;
1103       case Instruction::GetElementPtr:
1104         // Make everyone now use a constant of the new type... 
1105         std::vector<Constant*> C;
1106         for (unsigned i = 1, e = OldC->getNumOperands(); i != e; ++i)
1107           C.push_back(cast<Constant>(OldC->getOperand(i)));
1108         New = ConstantExpr::getGetElementPtrTy(NewTy, OldC->getOperand(0), C);
1109         break;
1110       }
1111       
1112       assert(New != OldC && "Didn't replace constant??");
1113       OldC->uncheckedReplaceAllUsesWith(New);
1114       OldC->destroyConstant();    // This constant is now dead, destroy it.
1115     }
1116   };
1117 } // end namespace llvm
1118
1119
1120 static ValueMap<ExprMapKeyType, Type, ConstantExpr> ExprConstants;
1121
1122 Constant *ConstantExpr::getCast(Constant *C, const Type *Ty) {
1123   assert(Ty->isFirstClassType() && "Cannot cast to an aggregate type!");
1124
1125   if (Constant *FC = ConstantFoldCastInstruction(C, Ty))
1126     return FC;          // Fold a few common cases...
1127
1128   // Look up the constant in the table first to ensure uniqueness
1129   std::vector<Constant*> argVec(1, C);
1130   ExprMapKeyType Key = std::make_pair(Instruction::Cast, argVec);
1131   return ExprConstants.getOrCreate(Ty, Key);
1132 }
1133
1134 Constant *ConstantExpr::getSignExtend(Constant *C, const Type *Ty) {
1135   assert(C->getType()->isInteger() && Ty->isInteger() &&
1136          C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1137          "This is an illegal sign extension!");
1138   C = ConstantExpr::getCast(C, C->getType()->getSignedVersion());
1139   return ConstantExpr::getCast(C, Ty);
1140 }
1141
1142 Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
1143   assert(C->getType()->isInteger() && Ty->isInteger() &&
1144          C->getType()->getPrimitiveSize() <= Ty->getPrimitiveSize() &&
1145          "This is an illegal zero extension!");
1146   C = ConstantExpr::getCast(C, C->getType()->getUnsignedVersion());
1147   return ConstantExpr::getCast(C, Ty);
1148 }
1149
1150 Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
1151                               Constant *C1, Constant *C2) {
1152   if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)
1153     return getShiftTy(ReqTy, Opcode, C1, C2);
1154   // Check the operands for consistency first
1155   assert((Opcode >= Instruction::BinaryOpsBegin &&
1156           Opcode < Instruction::BinaryOpsEnd) &&
1157          "Invalid opcode in binary constant expression");
1158   assert(C1->getType() == C2->getType() &&
1159          "Operand types in binary constant expression should match");
1160
1161   if (ReqTy == C1->getType())
1162     if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1163       return FC;          // Fold a few common cases...
1164
1165   std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
1166   ExprMapKeyType Key = std::make_pair(Opcode, argVec);
1167   return ExprConstants.getOrCreate(ReqTy, Key);
1168 }
1169
1170 Constant *ConstantExpr::getSelectTy(const Type *ReqTy, Constant *C,
1171                                     Constant *V1, Constant *V2) {
1172   assert(C->getType() == Type::BoolTy && "Select condition must be bool!");
1173   assert(V1->getType() == V2->getType() && "Select value types must match!");
1174   assert(V1->getType()->isFirstClassType() && "Cannot select aggregate type!");
1175
1176   if (ReqTy == V1->getType())
1177     if (Constant *SC = ConstantFoldSelectInstruction(C, V1, V2))
1178       return SC;        // Fold common cases
1179
1180   std::vector<Constant*> argVec(3, C);
1181   argVec[1] = V1;
1182   argVec[2] = V2;
1183   ExprMapKeyType Key = std::make_pair(Instruction::Select, argVec);
1184   return ExprConstants.getOrCreate(ReqTy, Key);
1185 }
1186
1187 /// getShiftTy - Return a shift left or shift right constant expr
1188 Constant *ConstantExpr::getShiftTy(const Type *ReqTy, unsigned Opcode,
1189                                    Constant *C1, Constant *C2) {
1190   // Check the operands for consistency first
1191   assert((Opcode == Instruction::Shl ||
1192           Opcode == Instruction::Shr) &&
1193          "Invalid opcode in binary constant expression");
1194   assert(C1->getType()->isIntegral() && C2->getType() == Type::UByteTy &&
1195          "Invalid operand types for Shift constant expr!");
1196
1197   if (Constant *FC = ConstantFoldBinaryInstruction(Opcode, C1, C2))
1198     return FC;          // Fold a few common cases...
1199
1200   // Look up the constant in the table first to ensure uniqueness
1201   std::vector<Constant*> argVec(1, C1); argVec.push_back(C2);
1202   ExprMapKeyType Key = std::make_pair(Opcode, argVec);
1203   return ExprConstants.getOrCreate(ReqTy, Key);
1204 }
1205
1206
1207 Constant *ConstantExpr::getGetElementPtrTy(const Type *ReqTy, Constant *C,
1208                                         const std::vector<Constant*> &IdxList) {
1209   assert(GetElementPtrInst::getIndexedType(C->getType(),
1210                    std::vector<Value*>(IdxList.begin(), IdxList.end()), true) &&
1211          "GEP indices invalid!");
1212
1213   if (Constant *FC = ConstantFoldGetElementPtr(C, IdxList))
1214     return FC;          // Fold a few common cases...
1215
1216   assert(isa<PointerType>(C->getType()) &&
1217          "Non-pointer type for constant GetElementPtr expression");
1218   // Look up the constant in the table first to ensure uniqueness
1219   std::vector<Constant*> argVec(1, C);
1220   argVec.insert(argVec.end(), IdxList.begin(), IdxList.end());
1221   const ExprMapKeyType &Key = std::make_pair(Instruction::GetElementPtr,argVec);
1222   return ExprConstants.getOrCreate(ReqTy, Key);
1223 }
1224
1225 Constant *ConstantExpr::getGetElementPtr(Constant *C,
1226                                          const std::vector<Constant*> &IdxList){
1227   // Get the result type of the getelementptr!
1228   std::vector<Value*> VIdxList(IdxList.begin(), IdxList.end());
1229
1230   const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), VIdxList,
1231                                                      true);
1232   assert(Ty && "GEP indices invalid!");
1233   return getGetElementPtrTy(PointerType::get(Ty), C, IdxList);
1234 }
1235
1236
1237 // destroyConstant - Remove the constant from the constant table...
1238 //
1239 void ConstantExpr::destroyConstant() {
1240   ExprConstants.remove(this);
1241   destroyConstantImpl();
1242 }
1243
1244 const char *ConstantExpr::getOpcodeName() const {
1245   return Instruction::getOpcodeName(getOpcode());
1246 }