diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp
index 46a76a2..dae4b4c 100644
--- a/lib/Linker/LinkModules.cpp
+++ b/lib/Linker/LinkModules.cpp
@@ -49,6 +49,8 @@ class TypeMapTy : public ValueMapTypeRemapper {
   SmallPtrSet<StructType*, 16> DstResolvedOpaqueTypes;
 public:
   
+  void makeConsistent();
+
   /// addTypeMapping - Indicate that the specified type in the destination
   /// module is conceptually equivalent to the specified type in the source
   /// module.
@@ -72,9 +74,46 @@ private:
   }
   
   bool areTypesIsomorphic(Type *DstTy, Type *SrcTy);
+  void makeRecursivelyEquivalent(SmallPtrSet<Type *, 32> &Visited, Type *T1,
+                                 Type *T2);
 };
 }
 
+void TypeMapTy::makeConsistent() {
+  SmallVector<std::pair<Type*, Type*>, 32> List;
+  for (DenseMap<Type*, Type*>::iterator I = MappedTypes.begin(),
+         E = MappedTypes.end(); I != E; ++I)
+    if (I->second)
+      List.push_back(std::make_pair(I->first, I->second));
+
+  SmallPtrSet<Type *, 32> Visited;
+  for (SmallVector<std::pair<Type*, Type*>, 32>::iterator I = List.begin(),
+         E = List.end(); I != E; ++I)
+    makeRecursivelyEquivalent(Visited, I->first, I->second);
+
+}
+
+void TypeMapTy::makeRecursivelyEquivalent(SmallPtrSet<Type *, 32> &Visited,
+                                          Type *T1, Type *T2) {
+  if (Visited.count(T1))
+    return;
+  Visited.insert(T1);
+
+  Type *&Entry = MappedTypes[T1];
+  if (!Entry)
+    Entry = T2;
+
+  assert(Entry == T2);
+
+  unsigned N = T1->getNumContainedTypes();
+  assert(N == T2->getNumContainedTypes());
+  for (unsigned I = 0; I < N; ++I) {
+    Type *T1C = T1->getContainedType(I);
+    Type *T2C = T2->getContainedType(I);
+    makeRecursivelyEquivalent(Visited, T1C, T2C);
+  }
+}
+
 void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) {
   Type *&Entry = MappedTypes[SrcTy];
   if (Entry) return;
@@ -601,6 +640,8 @@ void ModuleLinker::computeTypeMapping() {
         TypeMap.addTypeMapping(DST, ST);
   }
   
+
+  TypeMap.makeConsistent();
   
   // Don't bother incorporating aliases, they aren't generally typed well.
   
diff --git a/lib/Transforms/Utils/ValueMapper.cpp b/lib/Transforms/Utils/ValueMapper.cpp
index fc2538d..32d7e22 100644
--- a/lib/Transforms/Utils/ValueMapper.cpp
+++ b/lib/Transforms/Utils/ValueMapper.cpp
@@ -137,8 +137,11 @@ Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
                              Flags, TypeMapper));
   }
   
-  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
-    return VM[V] = CE->getWithOperands(Ops, NewTy);
+  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
+    Value *Ret = VM[V] = CE->getWithOperands(Ops, NewTy);
+    assert(Ret->getType() == NewTy);
+    return Ret;
+  }
   if (isa<ConstantArray>(C))
     return VM[V] = ConstantArray::get(cast<ArrayType>(NewTy), Ops);
   if (isa<ConstantStruct>(C))
