Unify Value.replaceUser and Value.replaceInUser.

R=ager

Change-Id: I4e8ed9a384cc7f7dfd10ef95b178dc441b0b5d83
diff --git a/src/main/java/com/android/tools/r8/ir/code/Instruction.java b/src/main/java/com/android/tools/r8/ir/code/Instruction.java
index a8004a1..52fff08 100644
--- a/src/main/java/com/android/tools/r8/ir/code/Instruction.java
+++ b/src/main/java/com/android/tools/r8/ir/code/Instruction.java
@@ -95,19 +95,17 @@
 
   public abstract void buildDex(DexBuilder builder);
 
-  public void replaceValue(Value oldValue, Value newValue, List<Instruction> toRemove) {
+  public void replaceValue(Value oldValue, Value newValue) {
     for (int i = 0; i < inValues.size(); i++) {
       if (oldValue == inValues.get(i)) {
         inValues.set(i, newValue);
         newValue.addUser(this);
-        toRemove.add(this);
       }
     }
   }
 
-  public void replaceDebugValue(Value oldValue, Value newValue, List<Instruction> toRemove) {
+  public void replaceDebugValue(Value oldValue, Value newValue) {
     if (debugValues.remove(oldValue)) {
-      toRemove.add(this);
       if (newValue.getLocalInfo() != null) {
         // TODO(zerny): Insert a write if replacing a phi with different debug-local info.
         addDebugValue(newValue);
diff --git a/src/main/java/com/android/tools/r8/ir/code/Phi.java b/src/main/java/com/android/tools/r8/ir/code/Phi.java
index 96069bf..20dc3c7 100644
--- a/src/main/java/com/android/tools/r8/ir/code/Phi.java
+++ b/src/main/java/com/android/tools/r8/ir/code/Phi.java
@@ -155,22 +155,20 @@
     current.removePhiUser(this);
   }
 
-  void replaceTrivialPhi(Value current, Value newValue, List<Phi> toRemove) {
+  void replaceOperand(Value current, Value newValue) {
     for (int i = 0; i < operands.size(); i++) {
       if (operands.get(i) == current) {
         operands.set(i, newValue);
         newValue.addPhiUser(this);
-        toRemove.add(this);
       }
     }
   }
 
-  void replaceTrivialDebugPhi(Value current, Value newValue, List<Phi> toRemove) {
+  void replaceDebugValue(Value current, Value newValue) {
     assert current.getLocalInfo() != null;
     assert current.getLocalInfo() == newValue.getLocalInfo();
     if (debugValues.remove(current)) {
       addDebugValue(newValue);
-      toRemove.add(this);
     }
   }
 
@@ -228,7 +226,7 @@
     {
       Set<Phi> phiUsersToSimplify = uniquePhiUsers();
       // Replace this phi with the unique value in all users.
-      replaceInUsers(same);
+      replaceUsers(same);
       // Try to simplify phi users that might now have become trivial.
       for (Phi user : phiUsersToSimplify) {
         user.removeTrivialPhi();
diff --git a/src/main/java/com/android/tools/r8/ir/code/Value.java b/src/main/java/com/android/tools/r8/ir/code/Value.java
index 7412dd7..62a1152 100644
--- a/src/main/java/com/android/tools/r8/ir/code/Value.java
+++ b/src/main/java/com/android/tools/r8/ir/code/Value.java
@@ -362,71 +362,22 @@
       return;
     }
     for (Instruction user : uniqueUsers()) {
-      user.inValues.replaceAll(v -> {
-        if (v == this) {
-          newValue.addUser(user);
-          return newValue;
-        }
-        return v;
-      });
+      user.replaceValue(this, newValue);
     }
     for (Phi user : uniquePhiUsers()) {
-      user.getOperands().replaceAll(v -> {
-        if (v == this) {
-          newValue.addPhiUser(user);
-          return newValue;
-        }
-        return v;
-      });
+      user.replaceOperand(this, newValue);
     }
     if (debugData != null) {
       for (Instruction user : debugUsers()) {
-        if (user.getDebugValues().remove(this)) {
-          user.addDebugValue(newValue);
-        }
+        user.replaceDebugValue(this, newValue);
       }
       for (Phi user : debugPhiUsers()) {
-        if (user.getDebugValues().remove(this)) {
-          user.addDebugValue(newValue);
-        }
+        user.replaceDebugValue(this, newValue);
       }
     }
     clearUsers();
   }
 
-  public void replaceInUsers(Value newValue) {
-    if (!uniqueUsers().isEmpty()) {
-      List<Instruction> toRemove = new ArrayList<>(uniqueUsers().size());
-      for (Instruction user : uniqueUsers()) {
-        user.replaceValue(this, newValue, toRemove);
-      }
-      toRemove.forEach(this::removeUser);
-    }
-    if (!uniquePhiUsers().isEmpty()) {
-      List<Phi> toRemove = new ArrayList<>(uniquePhiUsers().size());
-      for (Phi user : uniquePhiUsers()) {
-        user.replaceTrivialPhi(this, newValue, toRemove);
-      }
-      toRemove.forEach(this::removePhiUser);
-    }
-    if (debugData != null) {
-      if (!debugUsers().isEmpty()) {
-        List<Instruction> toRemove = new ArrayList<>(debugUsers().size());
-        for (Instruction user : debugUsers()) {
-          user.replaceDebugValue(this, newValue, toRemove);
-        }
-        toRemove.forEach(this::removeDebugUser);
-      }
-      if (!debugPhiUsers().isEmpty()) {
-        List<Phi> toRemove = new ArrayList<>(debugPhiUsers().size());
-        for (Phi user : debugPhiUsers()) {
-          user.replaceTrivialDebugPhi(this, newValue, toRemove);
-        }
-        toRemove.forEach(this::removeDebugPhiUser);
-      }
-    }
-  }
-
   public void replaceDebugUser(Instruction oldUser, Instruction newUser) {
     DebugUse use = debugData.users.remove(oldUser);
     if (use != null) {
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
index d44ecd9..a758223 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/CodeRewriter.java
@@ -1101,18 +1101,18 @@
             // Add constant into the dominator block of usages.
             insertConstantInBlock(instruction, entry.getKey());
           } else {
-            assert instruction.outValue().numberOfUsers() != 0;
             ConstNumber constNumber = instruction.asConstNumber();
             Value constantValue = instruction.outValue();
-            List<Instruction> toRemove = new ArrayList<>(constantValue.uniqueUsers().size());
+            assert constantValue.numberOfUsers() != 0;
+            assert constantValue.numberOfUsers() == constantValue.numberOfAllUsers();
             for (Instruction user : constantValue.uniqueUsers()) {
               ConstNumber newCstNum = ConstNumber.copyOf(code, constNumber);
               InstructionListIterator iterator = user.getBlock().listIterator(user);
               iterator.previous();
               iterator.add(newCstNum);
-              user.replaceValue(constantValue, newCstNum.outValue(), toRemove);
+              user.replaceValue(constantValue, newCstNum.outValue());
             }
-            toRemove.forEach(constantValue::removeUser);
+            constantValue.clearUsers();
           }
         }
       } else {