Version 2.1.55

Cherry-pick: Conservatively identify instances with escaping phi users.
CL: https://r8-review.googlesource.com/c/r8/+/52703
Bug: 160901582

Change-Id: I85d42ea6c3175200c3865662baa18a2c87c3e3c0
diff --git a/src/main/java/com/android/tools/r8/Version.java b/src/main/java/com/android/tools/r8/Version.java
index 3cd22ef..ae9866f 100644
--- a/src/main/java/com/android/tools/r8/Version.java
+++ b/src/main/java/com/android/tools/r8/Version.java
@@ -11,7 +11,7 @@
 
   // This field is accessed from release scripts using simple pattern matching.
   // Therefore, changing this field could break our release scripts.
-  public static final String LABEL = "2.1.54";
+  public static final String LABEL = "2.1.55";
 
   private Version() {
   }
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/info/MethodOptimizationInfoCollector.java b/src/main/java/com/android/tools/r8/ir/optimize/info/MethodOptimizationInfoCollector.java
index cac5801..13f850e 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/info/MethodOptimizationInfoCollector.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/info/MethodOptimizationInfoCollector.java
@@ -544,7 +544,7 @@
               if (!value.onlyDependsOnArgument()) {
                 builder.setInstanceFieldInitializationMayDependOnEnvironment();
               }
-              if (value == receiver) {
+              if (couldBeReceiverValue(value, receiver, aliasesThroughAssumeAndCheckCasts)) {
                 builder.setReceiverMayEscapeOutsideConstructorChain();
               }
             }
@@ -572,7 +572,7 @@
                 for (int i = 1; i < invoke.arguments().size(); i++) {
                   Value argument =
                       invoke.arguments().get(i).getAliasedValue(aliasesThroughAssumeAndCheckCasts);
-                  if (argument == receiver) {
+                  if (couldBeReceiverValue(argument, receiver, aliasesThroughAssumeAndCheckCasts)) {
                     // In the analysis of the parent constructor, we don't consider the non-receiver
                     // arguments as being aliases of the receiver. Therefore, we explicitly mark
                     // that the receiver escapes from this constructor.
@@ -590,7 +590,7 @@
                     .markAllFieldsAsRead()
                     .setMayHaveOtherSideEffectsThanInstanceFieldAssignments();
                 for (Value inValue : invoke.inValues()) {
-                  if (inValue.getAliasedValue(aliasesThroughAssumeAndCheckCasts) == receiver) {
+                  if (couldBeReceiverValue(inValue, receiver, aliasesThroughAssumeAndCheckCasts)) {
                     builder.setReceiverMayEscapeOutsideConstructorChain();
                     break;
                   }
@@ -606,7 +606,7 @@
                 builder.setMayHaveOtherSideEffectsThanInstanceFieldAssignments();
               }
               for (Value argument : invoke.arguments()) {
-                if (argument.getAliasedValue(aliasesThroughAssumeAndCheckCasts) == receiver) {
+                if (couldBeReceiverValue(argument, receiver, aliasesThroughAssumeAndCheckCasts)) {
                   builder.setReceiverMayEscapeOutsideConstructorChain();
                   break;
                 }
@@ -623,7 +623,7 @@
                   .markAllFieldsAsRead()
                   .setMayHaveOtherSideEffectsThanInstanceFieldAssignments();
               for (Value argument : invoke.arguments()) {
-                if (argument.getAliasedValue(aliasesThroughAssumeAndCheckCasts) == receiver) {
+                if (couldBeReceiverValue(argument, receiver, aliasesThroughAssumeAndCheckCasts)) {
                   builder.setReceiverMayEscapeOutsideConstructorChain();
                   break;
                 }
@@ -672,6 +672,18 @@
     return builder.build();
   }
 
+  private static boolean couldBeReceiverValue(
+      Value value, Value receiver, AliasedValueConfiguration aliasing) {
+    if (value.isPhi() && receiver.hasPhiUsers()) {
+      // Conservatively assume that the receiver might be an input dependency of the phi value.
+      return true;
+    }
+    if (value.getAliasedValue(aliasing) == receiver) {
+      return true;
+    }
+    return false;
+  }
+
   private void identifyInvokeSemanticsForInlining(
       DexEncodedMethod method, IRCode code, OptimizationFeedback feedback, Timing timing) {
     timing.begin("Identify invoke semantics for inlining");
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/classinliner/ClassInlinerInstanceEscapeViaPhiTest.java b/src/test/java/com/android/tools/r8/ir/optimize/classinliner/ClassInlinerInstanceEscapeViaPhiTest.java
new file mode 100644
index 0000000..147d2ed
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/classinliner/ClassInlinerInstanceEscapeViaPhiTest.java
@@ -0,0 +1,69 @@
+// Copyright (c) 2019, 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.classinliner;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/** Reproduction of b/160901582 where we inline class with escaping instance variable. */
+@RunWith(Parameterized.class)
+public class ClassInlinerInstanceEscapeViaPhiTest extends TestBase {
+
+  private final TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimesAndApiLevels().build();
+  }
+
+  public ClassInlinerInstanceEscapeViaPhiTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testReference() throws Exception {
+    testForRuntime(parameters)
+        .addInnerClasses(ClassInlinerInstanceEscapeViaPhiTest.class)
+        .run(parameters.getRuntime(), TestClass.class)
+        .assertSuccessWithOutputLines("false");
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    testForR8(parameters.getBackend())
+        .addInnerClasses(ClassInlinerInstanceEscapeViaPhiTest.class)
+        .addKeepMainRule(TestClass.class)
+        .setMinApi(parameters.getApiLevel())
+        .compile()
+        .run(parameters.getRuntime(), TestClass.class)
+        .assertSuccessWithOutputLines("false");
+  }
+
+  static class A {
+
+    public A() {
+      B.foo(System.nanoTime() > 0 ? this : null);
+    }
+  }
+
+  static class B {
+
+    static void foo(A a) {
+      System.out.println((System.nanoTime() > 0 ? a : null) == null);
+    }
+  }
+
+  static class TestClass {
+
+    public static void main(String[] args) {
+      new A();
+    }
+  }
+}