Path: blob/master/src/java.base/share/classes/jdk/internal/vm/annotation/IntrinsicCandidate.java
41161 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.vm.annotation;2627import java.lang.annotation.*;2829/**30* The {@code @IntrinsicCandidate} annotation is specific to the31* HotSpot Virtual Machine. It indicates that an annotated method32* may be (but is not guaranteed to be) intrinsified by the HotSpot VM. A method33* is intrinsified if the HotSpot VM replaces the annotated method with hand-written34* assembly and/or hand-written compiler IR -- a compiler intrinsic -- to improve35* performance. The {@code @IntrinsicCandidate} annotation is internal to the36* Java libraries and is therefore not supposed to have any relevance for application37* code.38*39* Maintainers of the Java libraries must consider the following when40* modifying methods annotated with {@code @IntrinsicCandidate}.41*42* <ul>43* <li>When modifying a method annotated with {@code @IntrinsicCandidate},44* the corresponding intrinsic code in the HotSpot VM implementation must be45* updated to match the semantics of the annotated method.</li>46* <li>For some annotated methods, the corresponding intrinsic may omit some low-level47* checks that would be performed as a matter of course if the intrinsic is implemented48* using Java bytecodes. This is because individual Java bytecodes implicitly check49* for exceptions like {@code NullPointerException} and {@code ArrayStoreException}.50* If such a method is replaced by an intrinsic coded in assembly language, any51* checks performed as a matter of normal bytecode operation must be performed52* before entry into the assembly code. These checks must be performed, as53* appropriate, on all arguments to the intrinsic, and on other values (if any) obtained54* by the intrinsic through those arguments. The checks may be deduced by inspecting55* the non-intrinsic Java code for the method, and determining exactly which exceptions56* may be thrown by the code, including undeclared implicit {@code RuntimeException}s.57* Therefore, depending on the data accesses performed by the intrinsic,58* the checks may include:59*60* <ul>61* <li>null checks on references</li>62* <li>range checks on primitive values used as array indexes</li>63* <li>other validity checks on primitive values (e.g., for divide-by-zero conditions)</li>64* <li>store checks on reference values stored into arrays</li>65* <li>array length checks on arrays indexed from within the intrinsic</li>66* <li>reference casts (when formal parameters are {@code Object} or some other weak type)</li>67* </ul>68*69* </li>70*71* <li>Note that the receiver value ({@code this}) is passed as a extra argument72* to all non-static methods. If a non-static method is an intrinsic, the receiver73* value does not need a null check, but (as stated above) any values loaded by the74* intrinsic from object fields must also be checked. As a matter of clarity, it is75* better to make intrinisics be static methods, to make the dependency on {@code this}76* clear. Also, it is better to explicitly load all required values from object77* fields before entering the intrinsic code, and pass those values as explicit arguments.78* First, this may be necessary for null checks (or other checks). Second, if the79* intrinsic reloads the values from fields and operates on those without checks,80* race conditions may be able to introduce unchecked invalid values into the intrinsic.81* If the intrinsic needs to store a value back to an object field, that value should be82* returned explicitly from the intrinsic; if there are multiple return values, coders83* should consider buffering them in an array. Removing field access from intrinsics84* not only clarifies the interface with between the JVM and JDK; it also helps decouple85* the HotSpot and JDK implementations, since if JDK code before and after the intrinsic86* manages all field accesses, then intrinsics can be coded to be agnostic of object87* layouts.</li>88*89* Maintainers of the HotSpot VM must consider the following when modifying90* intrinsics.91*92* <ul>93* <li>When adding a new intrinsic, make sure that the corresponding method94* in the Java libraries is annotated with {@code @IntrinsicCandidate}95* and that all possible call sequences that result in calling the intrinsic contain96* the checks omitted by the intrinsic (if any).</li>97* <li>When modifying an existing intrinsic, the Java libraries must be updated98* to match the semantics of the intrinsic and to execute all checks omitted99* by the intrinsic (if any).</li>100* </ul>101*102* Persons not directly involved with maintaining the Java libraries or the103* HotSpot VM can safely ignore the fact that a method is annotated with104* {@code @IntrinsicCandidate}.105*106* The HotSpot VM defines (internally) a list of intrinsics. Not all intrinsic107* are available on all platforms supported by the HotSpot VM. Furthermore,108* the availability of an intrinsic on a given platform depends on the109* configuration of the HotSpot VM (e.g., the set of VM flags enabled).110* Therefore, annotating a method with {@code @IntrinsicCandidate} does111* not guarantee that the marked method is intrinsified by the HotSpot VM.112*113* If the {@code CheckIntrinsics} VM flag is enabled, the HotSpot VM checks114* (when loading a class) that (1) all methods of that class that are also on115* the VM's list of intrinsics are annotated with {@code @IntrinsicCandidate}116* and that (2) for all methods of that class annotated with117* {@code @IntrinsicCandidate} there is an intrinsic in the list.118*119* @since 16120*/121@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})122@Retention(RetentionPolicy.RUNTIME)123public @interface IntrinsicCandidate {124}125126127