Merge "Do not record uses of unconstrained arguments in register allocation"
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 1e010ce..31eb337 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
@@ -2176,7 +2176,22 @@
             if (options.isGeneratingDex()) {
               int inConstraint = instruction.maxInValueRegister();
               LiveIntervals useIntervals = use.getLiveIntervals();
-              useIntervals.addUse(new LiveIntervalsUse(instruction.getNumber(), inConstraint));
+              // Arguments are always kept in their original, incoming register. For every
+              // unconstrained use of an argument we therefore use its incoming register.
+              // As a result, we do not need to record that the argument is being used at the
+              // current instruction.
+              //
+              // For ranged invoke instructions that use a subset of the arguments in the current
+              // order, registering a use for the arguments at the invoke can cause us to run out of
+              // registers. That is because all arguments are forced back into a chosen register at
+              // all uses. Therefore, if we register a use of an argument where we can actually use
+              // it in the argument register, the register allocator would use two registers for the
+              // argument but in reality only use one.
+              boolean isUnconstrainedArgumentUse =
+                  use.isArgument() && inConstraint == Constants.U16BIT_MAX;
+              if (!isUnconstrainedArgumentUse) {
+                useIntervals.addUse(new LiveIntervalsUse(instruction.getNumber(), inConstraint));
+              }
             }
           }
         }
diff --git a/src/test/java/com/android/tools/r8/ir/regalloc/B77240639.java b/src/test/java/com/android/tools/r8/ir/regalloc/B77240639.java
index 440f4f3..28a6fa3 100644
--- a/src/test/java/com/android/tools/r8/ir/regalloc/B77240639.java
+++ b/src/test/java/com/android/tools/r8/ir/regalloc/B77240639.java
@@ -16,7 +16,6 @@
 import org.junit.Test;
 
 public class B77240639 extends TestBase {
-  @Ignore("b/77240639")
   @Test
   public void test() throws Exception {
     AndroidApp app = compileWithD8(readClasses(TestClass.class));