Update removal of argument restores

Change-Id: I900fa63c0e93444511ba4c8877f7df24ab35fbe8
diff --git a/src/main/java/com/android/tools/r8/ir/regalloc/LinearScanRegisterAllocator.java b/src/main/java/com/android/tools/r8/ir/regalloc/LinearScanRegisterAllocator.java
index 8ad7a32..509506b 100644
--- a/src/main/java/com/android/tools/r8/ir/regalloc/LinearScanRegisterAllocator.java
+++ b/src/main/java/com/android/tools/r8/ir/regalloc/LinearScanRegisterAllocator.java
@@ -116,7 +116,7 @@
   // The code for which to allocate registers.
   private final IRCode code;
   // Number of registers used for arguments.
-  private final int numberOfArgumentRegisters;
+  protected final int numberOfArgumentRegisters;
   // Compiler options.
   private final InternalOptions options;
 
@@ -1940,7 +1940,7 @@
   }
 
   private void insertMoves() {
-    SpillMoveSet spillMoves = new SpillMoveSet(this, code, numberOfArgumentRegisters);
+    SpillMoveSet spillMoves = new SpillMoveSet(this, code);
     for (LiveIntervals intervals : liveIntervals) {
       if (intervals.hasSplits()) {
         LiveIntervals current = intervals;
diff --git a/src/main/java/com/android/tools/r8/ir/regalloc/SpillMoveSet.java b/src/main/java/com/android/tools/r8/ir/regalloc/SpillMoveSet.java
index 3dc151d..3401b17 100644
--- a/src/main/java/com/android/tools/r8/ir/regalloc/SpillMoveSet.java
+++ b/src/main/java/com/android/tools/r8/ir/regalloc/SpillMoveSet.java
@@ -31,18 +31,14 @@
   private final IRCode code;
   // The register allocator generating moves.
   private final LinearScanRegisterAllocator allocator;
-  // All registers below this number are arguments.
-  private final int argumentRegisterLimit;
   // Mapping from instruction numbers to the block that start with that instruction if any.
   private final Map<Integer, BasicBlock> blockStartMap = new HashMap<>();
   // The number of temporary registers used for parallel moves when scheduling the moves.
   private int usedTempRegisters = 0;
 
-  public SpillMoveSet(
-      LinearScanRegisterAllocator allocator, IRCode code, int argumentRegisterLimit) {
+  public SpillMoveSet(LinearScanRegisterAllocator allocator, IRCode code) {
     this.allocator = allocator;
     this.code = code;
-    this.argumentRegisterLimit = argumentRegisterLimit;
     for (BasicBlock block : code.blocks) {
       blockStartMap.put(block.entry().getNumber(), block);
     }
@@ -139,10 +135,6 @@
     return usedTempRegisters;
   }
 
-  private boolean isArgumentRegister(int register) {
-    return register < argumentRegisterLimit;
-  }
-
   private MoveType moveTypeForIntervals(LiveIntervals to, LiveIntervals from) {
     MoveType toType = to.getMoveType();
     MoveType fromType = from.getMoveType();
@@ -277,7 +269,11 @@
     Iterator<SpillMove> moveIterator = moves.iterator();
     while (moveIterator.hasNext()) {
       SpillMove move = moveIterator.next();
-      if (isArgumentRegister(move.to.getRegister())) {
+      // The argument registers can be used for other values than the arguments in intervals where
+      // the arguments are not live, so it is insufficient to check that the destination register
+      // is in the argument register range.
+      if (move.to.getRegister() < allocator.numberOfArgumentRegisters
+          && move.to.isArgumentInterval()) {
         moveIterator.remove();
       }
     }