Reproduce redundant argument to phi move
Bug: b/374262806
Change-Id: I73ce36a23b9b2a31b07bcd3d32e541f842dfbc92
diff --git a/src/test/java/com/android/tools/r8/ir/regalloc/RedundantArgumentToPhiMoveIn16BitRegisterAllocationTest.java b/src/test/java/com/android/tools/r8/ir/regalloc/RedundantArgumentToPhiMoveIn16BitRegisterAllocationTest.java
new file mode 100644
index 0000000..d1977ab
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/regalloc/RedundantArgumentToPhiMoveIn16BitRegisterAllocationTest.java
@@ -0,0 +1,61 @@
+// Copyright (c) 2024, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.android.tools.r8.ir.regalloc;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.codeinspector.InstructionSubject;
+import com.android.tools.r8.utils.codeinspector.MethodSubject;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class RedundantArgumentToPhiMoveIn16BitRegisterAllocationTest extends TestBase {
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withDefaultDexRuntime().withMaximumApiLevel().build();
+ }
+
+ @Test
+ public void testD8() throws Exception {
+ testForD8()
+ .addInnerClasses(getClass())
+ .release()
+ .setMinApi(parameters)
+ .compile()
+ .inspect(
+ inspector -> {
+ MethodSubject testMethodSubject =
+ inspector.clazz(Main.class).uniqueMethodWithOriginalName("test");
+ assertThat(testMethodSubject, isPresent());
+ // TODO(b/374262806): We should not need to move the argument value to a local
+ // register. Instead, we should use the argument register for the phi and load the
+ // constant 42 into this register, thereby avoiding any moves.
+ assertTrue(
+ testMethodSubject.streamInstructions().anyMatch(InstructionSubject::isMove));
+ });
+ }
+
+ static class Main {
+
+ static void test(long a, long b, long c, long d, long e, long f, long g, long h, int def) {
+ long i = (def & 1) == 0 ? a : 42;
+ accept(i);
+ }
+
+ static void accept(long a) {}
+ }
+}
diff --git a/src/test/testbase/java/com/android/tools/r8/utils/codeinspector/CfInstructionSubject.java b/src/test/testbase/java/com/android/tools/r8/utils/codeinspector/CfInstructionSubject.java
index 9c37579..2c24643 100644
--- a/src/test/testbase/java/com/android/tools/r8/utils/codeinspector/CfInstructionSubject.java
+++ b/src/test/testbase/java/com/android/tools/r8/utils/codeinspector/CfInstructionSubject.java
@@ -397,6 +397,11 @@
}
@Override
+ public boolean isMove() {
+ return false;
+ }
+
+ @Override
public boolean isFilledNewArray() {
return false;
}
diff --git a/src/test/testbase/java/com/android/tools/r8/utils/codeinspector/DexInstructionSubject.java b/src/test/testbase/java/com/android/tools/r8/utils/codeinspector/DexInstructionSubject.java
index 5afb54f..f4e6a57 100644
--- a/src/test/testbase/java/com/android/tools/r8/utils/codeinspector/DexInstructionSubject.java
+++ b/src/test/testbase/java/com/android/tools/r8/utils/codeinspector/DexInstructionSubject.java
@@ -95,6 +95,15 @@
import com.android.tools.r8.dex.code.DexIputWide;
import com.android.tools.r8.dex.code.DexMonitorEnter;
import com.android.tools.r8.dex.code.DexMonitorExit;
+import com.android.tools.r8.dex.code.DexMove;
+import com.android.tools.r8.dex.code.DexMove16;
+import com.android.tools.r8.dex.code.DexMoveFrom16;
+import com.android.tools.r8.dex.code.DexMoveObject;
+import com.android.tools.r8.dex.code.DexMoveObject16;
+import com.android.tools.r8.dex.code.DexMoveObjectFrom16;
+import com.android.tools.r8.dex.code.DexMoveWide;
+import com.android.tools.r8.dex.code.DexMoveWide16;
+import com.android.tools.r8.dex.code.DexMoveWideFrom16;
import com.android.tools.r8.dex.code.DexMulDouble;
import com.android.tools.r8.dex.code.DexMulDouble2Addr;
import com.android.tools.r8.dex.code.DexMulFloat;
@@ -642,6 +651,19 @@
}
@Override
+ public boolean isMove() {
+ return instruction instanceof DexMove
+ || instruction instanceof DexMove16
+ || instruction instanceof DexMoveFrom16
+ || instruction instanceof DexMoveObject
+ || instruction instanceof DexMoveObject16
+ || instruction instanceof DexMoveObjectFrom16
+ || instruction instanceof DexMoveWide
+ || instruction instanceof DexMoveWide16
+ || instruction instanceof DexMoveWideFrom16;
+ }
+
+ @Override
public boolean isFilledNewArray() {
return instruction instanceof DexFilledNewArray
|| instruction instanceof DexFilledNewArrayRange;
diff --git a/src/test/testbase/java/com/android/tools/r8/utils/codeinspector/InstructionSubject.java b/src/test/testbase/java/com/android/tools/r8/utils/codeinspector/InstructionSubject.java
index 0dbe112..1852710 100644
--- a/src/test/testbase/java/com/android/tools/r8/utils/codeinspector/InstructionSubject.java
+++ b/src/test/testbase/java/com/android/tools/r8/utils/codeinspector/InstructionSubject.java
@@ -164,6 +164,8 @@
boolean isMonitorExit();
+ boolean isMove();
+
boolean isFilledNewArray();
boolean isFilledNewArrayData();