Regression and fix for trivial-phi issue in field get removal
Bug: b/296654327
Change-Id: Ifca4c63f1a9a1dd3291abd7d27deb06fba929a7c
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/RedundantFieldLoadAndStoreElimination.java b/src/main/java/com/android/tools/r8/ir/optimize/RedundantFieldLoadAndStoreElimination.java
index e9c9747..b861cb7 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/RedundantFieldLoadAndStoreElimination.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/RedundantFieldLoadAndStoreElimination.java
@@ -50,6 +50,7 @@
import com.google.common.collect.Sets;
import it.unimi.dsi.fastutil.objects.Reference2IntMap;
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
+import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
@@ -269,7 +270,6 @@
public void eliminateRedundantRead(InstructionListIterator it, Instruction redundant) {
redundant.outValue().replaceUsers(value, affectedValues);
it.removeOrReplaceByDebugLocalRead();
- value.uniquePhiUsers().forEach(Phi::removeTrivialPhi);
hasChanged = true;
}
@@ -463,6 +463,14 @@
activeStates.recordActiveStateOnBlockExit(end, activeState);
}
processInstructionsToRemove();
+ List<Phi> affectedPhis = new ArrayList<>(affectedValues.size());
+ affectedValues.forEach(
+ value -> {
+ if (value.isPhi()) {
+ affectedPhis.add(value.asPhi());
+ }
+ });
+ affectedPhis.forEach(phi -> phi.removeTrivialPhi(null, affectedValues));
affectedValues.narrowingWithAssumeRemoval(appView, code);
if (hasChanged) {
code.removeRedundantBlocks();
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/redundantfieldloadelimination/RedundantFieldLoadEliminationTrivialPhiTest.java b/src/test/java/com/android/tools/r8/ir/optimize/redundantfieldloadelimination/RedundantFieldLoadEliminationTrivialPhiTest.java
new file mode 100644
index 0000000..69da363
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/redundantfieldloadelimination/RedundantFieldLoadEliminationTrivialPhiTest.java
@@ -0,0 +1,63 @@
+// Copyright (c) 2023, 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.optimize.redundantfieldloadelimination;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.AndroidApiLevel;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class RedundantFieldLoadEliminationTrivialPhiTest extends TestBase {
+
+ private final TestParameters parameters;
+
+ @Parameterized.Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withDefaultRuntimes().withApiLevel(AndroidApiLevel.B).build();
+ }
+
+ public RedundantFieldLoadEliminationTrivialPhiTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void test() throws Exception {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(RedundantFieldLoadEliminationTrivialPhiTest.class)
+ .addKeepMainRule(TestClass.class)
+ .setMinApi(parameters)
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutputLines("1", "1");
+ }
+
+ static class A {
+ int x;
+ int y;
+ }
+
+ static class TestClass {
+
+ public static void main(String[] args) {
+ int unknown = System.currentTimeMillis() > 0 ? 1 : 2;
+ A a = new A();
+ boolean b = true;
+ while (b) {
+ // Ensure we have a phi of a value that will become trivial once fields are optimized.
+ int copy = unknown;
+ a.x = unknown;
+ a.y = copy;
+ unknown = a.x; // Replacing this field read will make the phi trivial.
+ int y = a.y; // This read will be using the phi that becomes trivial.
+ System.out.println(y);
+ System.out.println(copy);
+ b = false;
+ }
+ }
+ }
+}