Don't do single caller inlining if shrinking is disallowed

Bug: b/344912795
Change-Id: I33719ce1bc6085f1c8b8cf6b4dd23285a60b16b1
diff --git a/src/main/java/com/android/tools/r8/shaking/KeepMethodInfo.java b/src/main/java/com/android/tools/r8/shaking/KeepMethodInfo.java
index f9038bf..879369c 100644
--- a/src/main/java/com/android/tools/r8/shaking/KeepMethodInfo.java
+++ b/src/main/java/com/android/tools/r8/shaking/KeepMethodInfo.java
@@ -168,7 +168,9 @@
   }
 
   public boolean isSingleCallerInliningAllowed(GlobalKeepInfoConfiguration configuration) {
-    return internalIsSingleCallerInliningAllowed();
+    return isOptimizationAllowed(configuration)
+        && isShrinkingAllowed(configuration)
+        && internalIsSingleCallerInliningAllowed();
   }
 
   boolean internalIsSingleCallerInliningAllowed() {
diff --git a/src/test/java/com/android/tools/r8/regress/Regress338346285.java b/src/test/java/com/android/tools/r8/regress/Regress338346285.java
new file mode 100644
index 0000000..c88ecf7
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/regress/Regress338346285.java
@@ -0,0 +1,60 @@
+// 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.regress;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+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;
+
+@RunWith(Parameterized.class)
+public class Regress338346285 extends TestBase {
+
+  private final TestParameters parameters;
+
+  @Parameterized.Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllApiLevels().withAllRuntimes().build();
+  }
+
+  public Regress338346285(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testR8() throws Exception {
+    testForR8(parameters.getBackend())
+        .addInnerClasses(getClass())
+        .addKeepRules("-keep,allowoptimization class " + TestClass.class.getTypeName() + " { *; }")
+        .setMinApi(parameters)
+        .compile()
+        .inspect(
+            codeInspector -> {
+              assertThat(
+                  codeInspector
+                      .clazz(TestClass.class)
+                      .uniqueMethodWithFinalName("shouldNotBeRemoved"),
+                  isPresent());
+            })
+        .run(parameters.getRuntime(), TestClass.class)
+        .assertSuccessWithOutputLines("foo", "bar", "foobar");
+  }
+
+  public static class TestClass {
+    public void shouldNotBeRemoved() {
+      System.out.println("foo");
+      System.out.println("bar");
+      System.out.println("foobar");
+    }
+
+    public static void main(String[] args) {
+      new TestClass().shouldNotBeRemoved();
+    }
+  }
+}