Add @NeverInline annotation for ART, and use it

Methods annotated with @NeverInline will not be inlined, which
is useful for reducing .odex file sizes.

Add @NeverInline for fillInStackTrace in j.l.Throwable.

Bug: 246530973
Change-Id: Ib843d23c0d641c00767cf58bf4583fc1b3c9810a
diff --git a/api/module-lib-current.txt b/api/module-lib-current.txt
index 870ae24..bb8bf32 100644
--- a/api/module-lib-current.txt
+++ b/api/module-lib-current.txt
@@ -159,6 +159,9 @@
   @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.CLASS) @java.lang.annotation.Target(java.lang.annotation.ElementType.METHOD) public @interface NeverCompile {
   }
 
+  @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.CLASS) @java.lang.annotation.Target(java.lang.annotation.ElementType.METHOD) public @interface NeverInline {
+  }
+
 }
 
 package dalvik.system {
diff --git a/dalvik/src/main/java/dalvik/annotation/optimization/NeverInline.java b/dalvik/src/main/java/dalvik/annotation/optimization/NeverInline.java
new file mode 100644
index 0000000..b8f9e2c
--- /dev/null
+++ b/dalvik/src/main/java/dalvik/annotation/optimization/NeverInline.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dalvik.annotation.optimization;
+
+import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
+
+import android.annotation.SystemApi;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Indicates that an API should never be inlined.
+ *
+ * <p>
+ * NeverInline can be used to annotate methods that should not be inlined into other methods.
+ * Methods that are not called frequently, are never speed-critical, or are only used for
+ * debugging do not necessarily need to run quickly. Applying this annotation to prevent these
+ * methods from being inlined will return some size improvements in .odex files.
+ * </p>
+ *
+ * <p>
+ * The <code>fillInStackTrace</code> method in java.lang.Throwable can be used as a concrete
+ * example. This is a method that fills in the execution stack trace and it is not used for
+ * performance. Annotating this method with NeverInline can be seen to significantly reduce
+ * the size of services.odex.
+ * </p>
+ * @hide
+ */
+@SystemApi(client = MODULE_LIBRARIES)
+@Retention(RetentionPolicy.CLASS)
+@Target(ElementType.METHOD)
+public @interface NeverInline {}
diff --git a/non_openjdk_java_files.bp b/non_openjdk_java_files.bp
index dc8444c..fa1574c 100755
--- a/non_openjdk_java_files.bp
+++ b/non_openjdk_java_files.bp
@@ -33,6 +33,7 @@
         "dalvik/src/main/java/dalvik/annotation/optimization/DeadReferenceSafe.java",
         "dalvik/src/main/java/dalvik/annotation/optimization/FastNative.java",
         "dalvik/src/main/java/dalvik/annotation/optimization/NeverCompile.java",
+        "dalvik/src/main/java/dalvik/annotation/optimization/NeverInline.java",
         "dalvik/src/main/java/dalvik/annotation/optimization/ReachabilitySensitive.java",
         "dalvik/src/main/java/dalvik/bytecode/OpcodeInfo.java",
         "dalvik/src/main/java/dalvik/bytecode/Opcodes.java",
diff --git a/ojluni/src/main/java/java/lang/Throwable.java b/ojluni/src/main/java/java/lang/Throwable.java
index 6d84758..6c8c7d9 100644
--- a/ojluni/src/main/java/java/lang/Throwable.java
+++ b/ojluni/src/main/java/java/lang/Throwable.java
@@ -27,6 +27,7 @@
 package java.lang;
 
 import dalvik.annotation.optimization.FastNative;
+import dalvik.annotation.optimization.NeverInline;
 
 import java.io.*;
 import java.util.*;
@@ -808,6 +809,7 @@
      * @return  a reference to this {@code Throwable} instance.
      * @see     java.lang.Throwable#printStackTrace()
      */
+    @NeverInline
     public synchronized Throwable fillInStackTrace() {
         if (stackTrace != null ||
             backtrace != null /* Out of protocol state */ ) {