Account for string-switch like code with non-local phi

Fixes: b/380109542
Change-Id: I4ee99607e4ff14659e3b18431968212d70200e1e
diff --git a/src/main/java/com/android/tools/r8/ir/code/Value.java b/src/main/java/com/android/tools/r8/ir/code/Value.java
index 2b027ab..b140e11 100644
--- a/src/main/java/com/android/tools/r8/ir/code/Value.java
+++ b/src/main/java/com/android/tools/r8/ir/code/Value.java
@@ -374,6 +374,10 @@
     return uniqueUsers().size() == 1;
   }
 
+  public boolean hasSingleUniqueUserAndNoOtherUsers() {
+    return hasSingleUniqueUser() && !hasPhiUsers() && !hasDebugUsers();
+  }
+
   public Instruction singleUniqueUser() {
     assert hasSingleUniqueUser();
     return firstUser();
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/passes/StringSwitchConverter.java b/src/main/java/com/android/tools/r8/ir/conversion/passes/StringSwitchConverter.java
index 10960e0..44cba60 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/passes/StringSwitchConverter.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/passes/StringSwitchConverter.java
@@ -775,6 +775,7 @@
 
         if (idValue == null
             || !idValue.getType().isInt()
+            || !idValue.hasSingleUniqueUserAndNoOtherUsers()
             || (toBeExtended != null && idValue != toBeExtended.idValue)) {
           // Not an extension of `toBeExtended`.
           return setFallthroughBlock(toBeExtended, fallthroughBlock);
@@ -808,7 +809,9 @@
       private IdToTargetMapping extendWithSwitch(
           IdToTargetMapping toBeExtended, IntSwitch theSwitch, BasicBlock fallthroughBlock) {
         Value switchValue = theSwitch.value();
-        if (!switchValue.isPhi() || (toBeExtended != null && switchValue != toBeExtended.idValue)) {
+        if (!switchValue.isPhi()
+            || !switchValue.hasSingleUniqueUserAndNoOtherUsers()
+            || (toBeExtended != null && switchValue != toBeExtended.idValue)) {
           // Not an extension of `toBeExtended`.
           return setFallthroughBlock(toBeExtended, fallthroughBlock);
         }
@@ -830,6 +833,7 @@
     private final Int2ReferenceMap<BasicBlock> mapping = new Int2ReferenceOpenHashMap<>();
 
     private IdToTargetMapping(Phi idValue) {
+      assert idValue.hasSingleUniqueUserAndNoOtherUsers();
       this.idValue = idValue;
     }
 
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/switches/StringSwitchWithNonLocalPhiTest.java b/src/test/java/com/android/tools/r8/ir/optimize/switches/StringSwitchWithNonLocalPhiTest.java
new file mode 100644
index 0000000..dc5c1d4
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/switches/StringSwitchWithNonLocalPhiTest.java
@@ -0,0 +1,84 @@
+// 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.optimize.switches;
+
+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.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class StringSwitchWithNonLocalPhiTest extends TestBase {
+
+  @Parameter(0)
+  public TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimesAndApiLevels().build();
+  }
+
+  @Test
+  public void testD8() throws Exception {
+    parameters.assumeDexRuntime();
+    testForD8()
+        .addProgramClasses(Main.class)
+        .release()
+        .setMinApi(parameters)
+        .run(parameters.getRuntime(), Main.class)
+        .assertSuccessWithOutputLines("Foo", "1", "Bar", "2", "Baz", "0");
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    testForR8(parameters.getBackend())
+        .addProgramClasses(Main.class)
+        .addKeepMainRule(Main.class)
+        .setMinApi(parameters)
+        .run(parameters.getRuntime(), Main.class)
+        .assertSuccessWithOutputLines("Foo", "1", "Bar", "2", "Baz", "0");
+  }
+
+  static class Main {
+
+    public static void main(String[] args) {
+      test("Foo");
+      test("Bar");
+      test("Baz");
+    }
+
+    static void test(String str) {
+      int hashCode = str.hashCode();
+      int id = 0;
+      switch (hashCode) {
+        case 70822: // "Foo".hashCode()
+          if (str.equals("Foo")) {
+            id = 1;
+          }
+          break;
+        case 66547: // "Bar".hashCode()
+          if (str.equals("Bar")) {
+            id = 2;
+          }
+          break;
+      }
+      switch (id) {
+        case 1:
+          System.out.println("Foo");
+          break;
+        case 2:
+          System.out.println("Bar");
+          break;
+        default:
+          System.out.println("Baz");
+          break;
+      }
+      System.out.println(id);
+    }
+  }
+}