Changes For Bug 352
[oota-llvm.git] / lib / VMCore / Value.cpp
index fcf0b7a4c4a6d167a704e2448e8615b84d8863ff..60be53db5b6ad6fe9b3346a244dae97172c1eb79 100644 (file)
 #include "llvm/SymbolTable.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Constant.h"
-#include "Support/LeakDetector.h"
+#include "llvm/GlobalValue.h"
+#include "llvm/Support/LeakDetector.h"
 #include <algorithm>
+#include <iostream>
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -28,9 +30,14 @@ static inline const Type *checkType(const Type *Ty) {
   return Ty;
 }
 
-Value::Value(const Type *ty, ValueTy vty, const std::string &name)
-  : Name(name), Ty(checkType(ty)) {
-  VTy = vty;
+Value::Value(const Type *ty, unsigned scid, const std::string &name)
+  : SubclassID(scid), Ty(checkType(ty)), Name(name) {
+  if (!isa<Constant>(this) && !isa<BasicBlock>(this))
+    assert((Ty->isFirstClassType() || Ty == Type::VoidTy || 
+           isa<OpaqueType>(ty)) &&
+           "Cannot create non-first-class values except for constants!");
+  if (ty == Type::VoidTy)
+    assert(name.empty() && "Cannot have named void values!");
 }
 
 Value::~Value() {
@@ -67,7 +74,10 @@ void Value::uncheckedReplaceAllUsesWith(Value *New) {
     // Must handle Constants specially, we cannot call replaceUsesOfWith on a
     // constant!
     if (Constant *C = dyn_cast<Constant>(U.getUser())) {
-      C->replaceUsesOfWithOnConstant(this, New, true);
+      if (!isa<GlobalValue>(C))
+        C->replaceUsesOfWithOnConstant(this, New, true);
+      else 
+        U.set(New);
     } else {
       U.set(New);
     }
@@ -87,17 +97,13 @@ void Value::replaceAllUsesWith(Value *New) {
 //                                 User Class
 //===----------------------------------------------------------------------===//
 
-User::User(const Type *Ty, ValueTy vty, const std::string &name) 
-  : Value(Ty, vty, name) {
-}
-
 // replaceUsesOfWith - Replaces all references to the "From" definition with
 // references to the "To" definition.
 //
 void User::replaceUsesOfWith(Value *From, Value *To) {
   if (From == To) return;   // Duh what?
 
-  assert(!isa<Constant>(this) &&
+  assert(!isa<Constant>(this) || isa<GlobalValue>(this) &&
          "Cannot call User::replaceUsesofWith on a constant!");
 
   for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
@@ -108,3 +114,4 @@ void User::replaceUsesOfWith(Value *From, Value *To) {
       setOperand(i, To); // Fix it now...
     }
 }
+