RewrittenProto: add RemoveArgsInfo builder

Change-Id: Ieea44455611866bb409944e46076ccaf4b951a1b
diff --git a/src/main/java/com/android/tools/r8/graph/RewrittenPrototypeDescription.java b/src/main/java/com/android/tools/r8/graph/RewrittenPrototypeDescription.java
index a58aace..3beb9ba 100644
--- a/src/main/java/com/android/tools/r8/graph/RewrittenPrototypeDescription.java
+++ b/src/main/java/com/android/tools/r8/graph/RewrittenPrototypeDescription.java
@@ -81,14 +81,6 @@
       this.removedArguments = removedArguments;
     }
 
-    public static RemovedArgumentInfoCollection create(
-        Int2ReferenceSortedMap<RemovedArgumentInfo> removedArguments) {
-      if (removedArguments == null || removedArguments.isEmpty()) {
-        return EMPTY;
-      }
-      return new RemovedArgumentInfoCollection(removedArguments);
-    }
-
     public static RemovedArgumentInfoCollection empty() {
       return EMPTY;
     }
@@ -176,6 +168,30 @@
       }
       return null;
     }
+
+    public static Builder builder() {
+      return new Builder();
+    }
+
+    public static class Builder {
+      private Int2ReferenceSortedMap<RemovedArgumentInfo> removedArguments;
+
+      public Builder addRemovedArgument(int argIndex, RemovedArgumentInfo argInfo) {
+        if (removedArguments == null) {
+          removedArguments = new Int2ReferenceLinkedOpenHashMap<>();
+        }
+        assert !removedArguments.containsKey(argIndex);
+        removedArguments.put(argIndex, argInfo);
+        return this;
+      }
+
+      public RemovedArgumentInfoCollection build() {
+        if (removedArguments == null || removedArguments.isEmpty()) {
+          return EMPTY;
+        }
+        return new RemovedArgumentInfoCollection(removedArguments);
+      }
+    }
   }
 
   private static final RewrittenPrototypeDescription none = new RewrittenPrototypeDescription();
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/UninstantiatedTypeOptimization.java b/src/main/java/com/android/tools/r8/ir/optimize/UninstantiatedTypeOptimization.java
index 2cba82b..a56dac9 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/UninstantiatedTypeOptimization.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/UninstantiatedTypeOptimization.java
@@ -43,8 +43,6 @@
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
-import it.unimi.dsi.fastutil.ints.Int2ReferenceLinkedOpenHashMap;
-import it.unimi.dsi.fastutil.ints.Int2ReferenceSortedMap;
 import java.util.BitSet;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -310,20 +308,18 @@
       return RemovedArgumentInfoCollection.empty();
     }
 
-    Int2ReferenceSortedMap<RemovedArgumentInfo> removedArgumentsInfo = null;
+    RemovedArgumentInfoCollection.Builder argInfosBuilder = RemovedArgumentInfoCollection.builder();
     DexProto proto = encodedMethod.method.proto;
     int offset = encodedMethod.isStatic() ? 0 : 1;
     for (int i = 0; i < proto.parameters.size(); ++i) {
       DexType type = proto.parameters.values[i];
       if (type.isAlwaysNull(appView)) {
-        if (removedArgumentsInfo == null) {
-          removedArgumentsInfo = new Int2ReferenceLinkedOpenHashMap<>();
-        }
-        removedArgumentsInfo.put(
-            i + offset, RemovedArgumentInfo.builder().setIsAlwaysNull().setType(type).build());
+        RemovedArgumentInfo removedArg =
+            RemovedArgumentInfo.builder().setIsAlwaysNull().setType(type).build();
+        argInfosBuilder.addRemovedArgument(i + offset, removedArg);
       }
     }
-    return RemovedArgumentInfoCollection.create(removedArgumentsInfo);
+    return argInfosBuilder.build();
   }
 
   private DexMethod getNewMethodSignature(
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/UnusedArgumentsCollector.java b/src/main/java/com/android/tools/r8/ir/optimize/UnusedArgumentsCollector.java
index f50f7fc..e99ba9a 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/UnusedArgumentsCollector.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/UnusedArgumentsCollector.java
@@ -32,8 +32,6 @@
 import com.google.common.collect.ImmutableBiMap;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Streams;
-import it.unimi.dsi.fastutil.ints.Int2ReferenceLinkedOpenHashMap;
-import it.unimi.dsi.fastutil.ints.Int2ReferenceSortedMap;
 import java.util.BitSet;
 import java.util.HashSet;
 import java.util.IdentityHashMap;
@@ -316,17 +314,18 @@
     method.getCode().registerArgumentReferences(method, collector);
     BitSet used = collector.getUsedArguments();
     if (used.cardinality() < argumentCount) {
-      Int2ReferenceSortedMap<RemovedArgumentInfo> unused = new Int2ReferenceLinkedOpenHashMap<>();
+      RemovedArgumentInfoCollection.Builder argInfosBuilder =
+          RemovedArgumentInfoCollection.builder();
       for (int argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++) {
         if (!used.get(argumentIndex)) {
-          unused.put(
-              argumentIndex,
+          RemovedArgumentInfo removedArg =
               RemovedArgumentInfo.builder()
                   .setType(method.method.proto.parameters.values[argumentIndex - offset])
-                  .build());
+                  .build();
+          argInfosBuilder.addRemovedArgument(argumentIndex, removedArg);
         }
       }
-      return RemovedArgumentInfoCollection.create(unused);
+      return argInfosBuilder.build();
     }
     return null;
   }