Fix linking bug in xboard
[oota-llvm.git] / lib / VMCore / Constants.cpp
index 7bbbdb32023493b018e593113246ad4bfedf6baf..eb93da3c1d0a9dbc009a77da20119f410eb11a01 100644 (file)
@@ -213,7 +213,10 @@ ConstantArray::ConstantArray(const ArrayType *T,
                              const std::vector<Constant*> &V) : Constant(T) {
   Operands.reserve(V.size());
   for (unsigned i = 0, e = V.size(); i != e; ++i) {
-    assert(V[i]->getType() == T->getElementType());
+    assert(V[i]->getType() == T->getElementType() ||
+           (T->isAbstract() &&
+            V[i]->getType()->getPrimitiveID() ==
+            T->getElementType()->getPrimitiveID()));
     Operands.push_back(Use(V[i], this));
   }
 }
@@ -463,6 +466,26 @@ void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV) {
 //===----------------------------------------------------------------------===//
 //                      Factory Function Implementation
 
+// ReplaceUsesOfWith - This is exactly the same as Value::replaceAllUsesWith,
+// except that it doesn't have all of the asserts.  The asserts fail because we
+// are half-way done resolving types, which causes some types to exist as two
+// different Type*'s at the same time.  This is a sledgehammer to work around
+// this problem.
+//
+static void ReplaceUsesOfWith(Value *Old, Value *New) {
+  while (!Old->use_empty()) {
+    User *Use = Old->use_back();
+    // Must handle Constants specially, we cannot call replaceUsesOfWith on a
+    // constant!
+    if (Constant *C = dyn_cast<Constant>(Use)) {
+      C->replaceUsesOfWithOnConstant(Old, New);
+    } else {
+      Use->replaceUsesOfWith(Old, New);
+    }
+  }
+}
+
+
 // ConstantCreator - A class that is used to create constants by
 // ValueMap*.  This class should be partially specialized if there is
 // something strange that needs to be done to interface to the ctor for the
@@ -573,7 +596,7 @@ void ConstantArray::refineAbstractType(const DerivedType *OldTy,
     C.push_back(cast<Constant>(getOperand(i)));
   Constant *New = ConstantArray::get(cast<ArrayType>(NewTy), C);
   if (New != this) {
-    replaceAllUsesWith(New);
+    ReplaceUsesOfWith(this, New);
     destroyConstant();    // This constant is now dead, destroy it.
   }
 }
@@ -642,7 +665,7 @@ void ConstantStruct::refineAbstractType(const DerivedType *OldTy,
     C.push_back(cast<Constant>(getOperand(i)));
   Constant *New = ConstantStruct::get(cast<StructType>(NewTy), C);
   if (New != this) {
-    replaceAllUsesWith(New);
+    ReplaceUsesOfWith(this, New);
     destroyConstant();    // This constant is now dead, destroy it.
   }
 }
@@ -683,7 +706,7 @@ void ConstantPointerNull::refineAbstractType(const DerivedType *OldTy,
   // Make everyone now use a constant of the new type...
   Constant *New = ConstantPointerNull::get(cast<PointerType>(NewTy));
   if (New != this) {
-    replaceAllUsesWith(New);
+    ReplaceUsesOfWith(this, New);
     
     // This constant is now dead, destroy it.
     destroyConstant();
@@ -836,7 +859,7 @@ void ConstantExpr::refineAbstractType(const DerivedType *OldTy,
     New = ConstantExpr::getGetElementPtr(getOperand(0), C);
   }
   if (New != this) {
-    replaceAllUsesWith(New);
+    ReplaceUsesOfWith(this, New);
     destroyConstant();    // This constant is now dead, destroy it.
   }
 }