src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.meta/src/jdk/vm/ci/meta/SpeculationLog.java
changeset 54669 ad45b3802d4e
parent 52298 83039b8e6a42
equal deleted inserted replaced
54668:0bda2308eded 54669:ad45b3802d4e
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 package jdk.vm.ci.meta;
    23 package jdk.vm.ci.meta;
    24 
    24 
       
    25 import java.util.Map;
       
    26 import java.util.function.Supplier;
       
    27 
    25 /**
    28 /**
    26  * Manages unique deoptimization reasons. Reasons are embedded in compiled code and can be
    29  * Manages unique {@link SpeculationReason} objects that denote why a deoptimization occurred.
    27  * invalidated at run time. Subsequent compilations then should not speculate again on such
    30  * Reasons are embedded in compiled code for a method. If the compiled code deoptimizes at a
    28  * invalidated reasons to avoid repeated deoptimization.
    31  * position associated with a {@link SpeculationReason}, the reason is added to a set of failed
    29  *
    32  * speculations associated with the method. A subsequent compilation of the method can query the
    30  * All methods of this interface are called by the compiler. There is no need for API to register
    33  * failed speculations via a {@link SpeculationLog} to avoid making a speculation based on
    31  * failed speculations during deoptimization, since every VM has different needs there.
    34  * invalidated reasons. This avoids repeated deoptimizations.
    32  */
    35  */
    33 public interface SpeculationLog {
    36 public interface SpeculationLog {
    34     /**
    37     /**
    35      * Marker interface for speculation objects that can be added to the speculation log.
    38      * The specific attributes of a speculation that a compiler uses to denote a speculation in a
       
    39      * compiled method. Typical attributes of a speculation are a bytecode position, type
       
    40      * information about a variable being speculated on and an enum denoting the type of operation
       
    41      * to which the speculation applies. A {@link SpeculationReason} is used as a key in a
       
    42      * {@link Map} and so it must implement {@link Object#equals(Object)} and
       
    43      * {@link Object#hashCode()} in terms of its attributes.
       
    44      *
       
    45      * A JVMCI implementation may serialize speculations for storage off heap (e.g. in native memory
       
    46      * associated with an nmethod). For this reason, the attributes of a {@link SpeculationReason}
       
    47      * are restricted to those supported by the {@code add...} methods of
       
    48      * {@link SpeculationReasonEncoding}.
    36      */
    49      */
    37     public interface SpeculationReason {
    50     public interface SpeculationReason {
       
    51 
       
    52         /**
       
    53          * Encodes the attributes of this reason using a {@link SpeculationReasonEncoding}. For
       
    54          * efficiency, a {@link SpeculationReason} implementation should cache the returned value
       
    55          * and return it for all subsequent calls to this method. This also underlines the
       
    56          * requirement that the encoding for a specific reason instance should be stable.
       
    57          *
       
    58          * @param encodingSupplier source of a {@link SpeculationReasonEncoding}
       
    59          * @return a {@link SpeculationReasonEncoding} that encodes all the attributes that uniquely
       
    60          *         identify this reason
       
    61          */
       
    62         default SpeculationReasonEncoding encode(Supplier<SpeculationReasonEncoding> encodingSupplier) {
       
    63             return null;
       
    64         }
       
    65     }
       
    66 
       
    67     /**
       
    68      * Provides a facility for encoding the attributes of a {@link SpeculationReason}. The encoding
       
    69      * format is determined by the implementation of this interface.
       
    70      */
       
    71     public interface SpeculationReasonEncoding {
       
    72         void addByte(int value);
       
    73 
       
    74         void addShort(int value);
       
    75 
       
    76         void addInt(int value);
       
    77 
       
    78         void addLong(long value);
       
    79 
       
    80         void addMethod(ResolvedJavaMethod method);
       
    81 
       
    82         void addType(ResolvedJavaType type);
       
    83 
       
    84         void addString(String value);
       
    85 
       
    86         default void addField(ResolvedJavaField field) {
       
    87             addType(field.getDeclaringClass());
       
    88             addInt(field.getModifiers());
       
    89             addInt(field.getOffset());
       
    90         }
    38     }
    91     }
    39 
    92 
    40     /**
    93     /**
    41      * Marker class that indicates that a speculation has no reason.
    94      * Marker class that indicates that a speculation has no reason.
    42      */
    95      */
    43     final class NoSpeculationReason implements SpeculationReason {
    96     final class NoSpeculationReason implements SpeculationReason {
    44     }
    97     }
    45 
    98 
    46     class Speculation {
    99     class Speculation {
    47         private SpeculationReason reason;
   100         private final SpeculationReason reason;
    48 
   101 
    49         public Speculation(SpeculationReason reason) {
   102         public Speculation(SpeculationReason reason) {
    50             this.reason = reason;
   103             this.reason = reason;
    51         }
   104         }
    52 
   105 
    75     }
   128     }
    76 
   129 
    77     Speculation NO_SPECULATION = new Speculation(new NoSpeculationReason());
   130     Speculation NO_SPECULATION = new Speculation(new NoSpeculationReason());
    78 
   131 
    79     /**
   132     /**
    80      * Must be called before compilation, i.e., before a compiler calls {@link #maySpeculate}.
   133      * Updates the set of failed speculations recorded in this log. This must be called before
       
   134      * compilation.
    81      */
   135      */
    82     void collectFailedSpeculations();
   136     void collectFailedSpeculations();
    83 
   137 
    84     /**
   138     /**
    85      * If this method returns true, the compiler is allowed to {@link #speculate} with the given
   139      * If this method returns true, the compiler is allowed to {@link #speculate} with the given