Don't inline methods marked with -assumenosideeffects

Change-Id: Ib696bb3e070461886a6128d474537836cd4b7076
Bug: 157688676
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/DefaultInliningOracle.java b/src/main/java/com/android/tools/r8/ir/optimize/DefaultInliningOracle.java
index c4bf62f..f1ed11b 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/DefaultInliningOracle.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/DefaultInliningOracle.java
@@ -265,7 +265,8 @@
       return null;
     }
 
-    if (inliner.isBlacklisted(singleTarget, whyAreYouNotInliningReporter)) {
+    if (inliner.isBlacklisted(
+        invoke, resolutionResult, singleTarget, whyAreYouNotInliningReporter)) {
       return null;
     }
 
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/Inliner.java b/src/main/java/com/android/tools/r8/ir/optimize/Inliner.java
index 1c5ff8b..73da99f 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/Inliner.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/Inliner.java
@@ -118,29 +118,40 @@
   }
 
   boolean isBlacklisted(
-      ProgramMethod method, WhyAreYouNotInliningReporter whyAreYouNotInliningReporter) {
-    DexMethod reference = method.getReference();
-    if (method.getDefinition().getOptimizationInfo().forceInline()
-        && appView.appInfo().neverInline.contains(reference)) {
+      InvokeMethod invoke,
+      SingleResolutionResult resolutionResult,
+      ProgramMethod singleTarget,
+      WhyAreYouNotInliningReporter whyAreYouNotInliningReporter) {
+    AppInfoWithLiveness appInfo = appView.appInfo();
+    DexMethod singleTargetReference = singleTarget.getReference();
+    if (singleTarget.getDefinition().getOptimizationInfo().forceInline()
+        && appInfo.neverInline.contains(singleTargetReference)) {
       throw new Unreachable();
     }
 
-    if (appView.appInfo().isPinned(reference)) {
+    if (appInfo.isPinned(singleTargetReference)) {
       whyAreYouNotInliningReporter.reportPinned();
       return true;
     }
 
-    if (blacklist.contains(appView.graphLense().getOriginalMethodSignature(reference))
-        || TwrCloseResourceRewriter.isSynthesizedCloseResourceMethod(reference, appView)) {
+    if (blacklist.contains(appView.graphLense().getOriginalMethodSignature(singleTargetReference))
+        || TwrCloseResourceRewriter.isSynthesizedCloseResourceMethod(
+            singleTargetReference, appView)) {
       whyAreYouNotInliningReporter.reportBlacklisted();
       return true;
     }
 
-    if (appView.appInfo().neverInline.contains(reference)) {
+    if (appInfo.neverInline.contains(singleTargetReference)) {
       whyAreYouNotInliningReporter.reportMarkedAsNeverInline();
       return true;
     }
 
+    if (appInfo.noSideEffects.containsKey(invoke.getInvokedMethod())
+        || appInfo.noSideEffects.containsKey(resolutionResult.getResolvedMethod().getReference())
+        || appInfo.noSideEffects.containsKey(singleTargetReference)) {
+      return true;
+    }
+
     return false;
   }
 
diff --git a/src/test/java/com/android/tools/r8/AssumeNoSideEffects.java b/src/test/java/com/android/tools/r8/AssumeNoSideEffects.java
new file mode 100644
index 0000000..0513e17
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/AssumeNoSideEffects.java
@@ -0,0 +1,10 @@
+// Copyright (c) 2020, 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;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+
+@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
+public @interface AssumeNoSideEffects {}
diff --git a/src/test/java/com/android/tools/r8/R8TestBuilder.java b/src/test/java/com/android/tools/r8/R8TestBuilder.java
index a109e20..1a86ba0 100644
--- a/src/test/java/com/android/tools/r8/R8TestBuilder.java
+++ b/src/test/java/com/android/tools/r8/R8TestBuilder.java
@@ -48,6 +48,7 @@
 
   private AllowedDiagnosticMessages allowedDiagnosticMessages = AllowedDiagnosticMessages.NONE;
   private boolean allowUnusedProguardConfigurationRules = false;
+  private boolean enableAssumeNoSideEffectsAnnotations = false;
   private boolean enableConstantArgumentAnnotations = false;
   private boolean enableInliningAnnotations = false;
   private boolean enableMemberValuePropagationAnnotations = false;
@@ -349,6 +350,21 @@
     return self();
   }
 
+  public T enableAssumeNoSideEffectsAnnotations() {
+    return enableAssumeNoSideEffectsAnnotations(AssumeNoSideEffects.class.getPackage().getName());
+  }
+
+  public T enableAssumeNoSideEffectsAnnotations(String annotationPackageName) {
+    if (!enableAssumeNoSideEffectsAnnotations) {
+      enableAssumeNoSideEffectsAnnotations = true;
+      addInternalKeepRules(
+          "-assumenosideeffects class * { @"
+              + annotationPackageName
+              + ".AssumeNoSideEffects <methods>; }");
+    }
+    return self();
+  }
+
   public T enableInliningAnnotations() {
     return enableInliningAnnotations(NeverInline.class.getPackage().getName());
   }
diff --git a/src/test/java/com/android/tools/r8/TestRunResult.java b/src/test/java/com/android/tools/r8/TestRunResult.java
index bf3bb9d..16fd725 100644
--- a/src/test/java/com/android/tools/r8/TestRunResult.java
+++ b/src/test/java/com/android/tools/r8/TestRunResult.java
@@ -113,6 +113,10 @@
     return self();
   }
 
+  public RR assertSuccessWithEmptyOutput() {
+    return assertSuccessWithOutput("");
+  }
+
   public RR assertSuccessWithOutputLines(String... expected) {
     return assertSuccessWithOutputLines(Arrays.asList(expected));
   }
diff --git a/src/test/java/com/android/tools/r8/shaking/assumenosideeffects/B157688676.java b/src/test/java/com/android/tools/r8/shaking/assumenosideeffects/B157688676.java
new file mode 100644
index 0000000..85be324
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/shaking/assumenosideeffects/B157688676.java
@@ -0,0 +1,53 @@
+// Copyright (c) 2020, 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.shaking.assumenosideeffects;
+
+import com.android.tools.r8.AssumeNoSideEffects;
+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;
+
+@RunWith(Parameterized.class)
+public class B157688676 extends TestBase {
+
+  private final TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    return getTestParameters().withAllRuntimesAndApiLevels().build();
+  }
+
+  public B157688676(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void test() throws Exception {
+    testForR8(parameters.getBackend())
+        .addProgramClasses(TestClass.class)
+        .addKeepMainRule(TestClass.class)
+        .enableAssumeNoSideEffectsAnnotations()
+        .setMinApi(parameters.getApiLevel())
+        .compile()
+        .run(parameters.getRuntime(), TestClass.class)
+        .assertSuccessWithEmptyOutput();
+  }
+
+  static class TestClass {
+
+    public static void main(String[] args) {
+      greet();
+    }
+
+    @AssumeNoSideEffects
+    static void greet() {
+      System.out.println("Hello world!");
+    }
+  }
+}