- Dramatically simplify the Constant::mutateReferences implementation,
authorChris Lattner <sabre@nondot.org>
Mon, 14 Oct 2002 03:30:23 +0000 (03:30 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 14 Oct 2002 03:30:23 +0000 (03:30 +0000)
    allowing it to be called on all constant types (structures/arrays)

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4160 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Constant.h
include/llvm/Constants.h
include/llvm/Module.h
lib/VMCore/Constants.cpp

index 3105cea319d9943fa254decf246a0d8cf18d636c..a7ec6331bd5c658fc7a910f94b0cba01571c1d26 100644 (file)
@@ -79,7 +79,7 @@ public:
   // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
   // NOT USE THIS!!
   // Returns the number of uses of OldV that were replaced.
-  virtual unsigned mutateReferences(Value* OldV, Value *NewV) { return 0; }
+  unsigned mutateReferences(Value* OldV, Value *NewV);
   // END WARNING!!
 };
 
index a91c1971fa4f7712b3d9b571dca0b49a8866c75e..ec6345846e334358fb891593d58884d22379547d 100644 (file)
@@ -439,12 +439,6 @@ public:
   static inline bool classof(const Value *V) {
     return isa<ConstantPointer>(V) && classof(cast<ConstantPointer>(V));
   }
-
-  // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
-  // NOT USE THIS!!
-  // Returns the number of uses of OldV that were replaced.
-  virtual unsigned mutateReferences(Value* OldV, Value *NewV);
-  // END WARNING!!
 };
 
 
@@ -502,14 +496,6 @@ public:
   static inline bool classof(const Value *V) {
     return isa<Constant>(V) && classof(cast<Constant>(V));
   }
-
-public:
-  // WARNING: Only to be used by Bytecode & Assembly Parsers!  USER CODE SHOULD
-  // NOT USE THIS!!
-  // Returns the number of uses of OldV that were replaced.
-  virtual unsigned mutateReferences(Value* OldV, Value *NewV);
-  // END WARNING!!
 };
 
-
 #endif
index b34c06b9f57a0b07fc81d929a1f4933a81df76cc..2b8c0ab5d0eb1c80cd7d06b3eea923a056a4d47d 100644 (file)
@@ -59,7 +59,8 @@ private:
   SymbolTable *SymTab;
 
   // Accessor for the underlying GlobalValRefMap... only through the
-  // ConstantPointerRef class...
+  // Constant class...
+  friend class Constant;
   friend class ConstantPointerRef;
   void mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV);
   ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
index 8464c437fcb4e68734bed325203dbb87e430b051..3887e33274a693d72cae36299a8f2ac9e491822c 100644 (file)
@@ -685,27 +685,25 @@ const char *ConstantExpr::getOpcodeName() const {
   return Instruction::getOpcodeName(getOpcode());
 }
 
+unsigned Constant::mutateReferences(Value *OldV, Value *NewV) {
+  // Uses of constant pointer refs are global values, not constants!
+  if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(this)) {
+    GlobalValue *NewGV = cast<GlobalValue>(NewV);
+    GlobalValue *OldGV = CPR->getValue();
 
-//---- ConstantPointerRef::mutateReferences() implementation...
-//
-unsigned ConstantPointerRef::mutateReferences(Value *OldV, Value *NewV) {
-  assert(getValue() == OldV && "Cannot mutate old value if I'm not using it!");
-  GlobalValue *NewGV = cast<GlobalValue>(NewV);
-  getValue()->getParent()->mutateConstantPointerRef(getValue(), NewGV);
-  Operands[0] = NewGV;
-  return 1;
-}
+    assert(OldGV == OldV && "Cannot mutate old value if I'm not using it!");
 
-
-//---- ConstantPointerExpr::mutateReferences() implementation...
-//
-unsigned ConstantExpr::mutateReferences(Value* OldV, Value *NewV) {
-  unsigned NumReplaced = 0;
-  Constant *NewC = cast<Constant>(NewV);
-  for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
-    if (Operands[i] == OldV) {
-      ++NumReplaced;
-      Operands[i] = NewC;
-    }
-  return NumReplaced;
+    OldGV->getParent()->mutateConstantPointerRef(OldGV, NewGV);
+    Operands[0] = NewGV;
+    return 1;
+  } else {
+    Constant *NewC = cast<Constant>(NewV);
+    unsigned NumReplaced = 0;
+    for (unsigned i = 0, N = getNumOperands(); i != N; ++i)
+      if (Operands[i] == OldV) {
+        ++NumReplaced;
+        Operands[i] = NewC;
+      }
+    return NumReplaced;
+  }
 }