src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.phases.common/src/org/graalvm/compiler/phases/common/UseTrappingNullChecksPhase.java
author iveresov
Fri, 02 Feb 2018 17:28:17 -0800
changeset 48861 47f19ff9903c
parent 47216 71c04702a3d5
child 50858 2d3e99a72541
permissions -rw-r--r--
8194819: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     1
/*
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     4
 *
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     8
 *
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    13
 * accompanied this code).
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    14
 *
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    18
 *
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    21
 * questions.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    22
 */
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    23
package org.graalvm.compiler.phases.common;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    24
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    25
import static org.graalvm.compiler.core.common.GraalOptions.OptImplicitNullChecks;
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    26
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    27
import java.util.List;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    28
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46344
diff changeset
    29
import org.graalvm.compiler.debug.CounterKey;
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46344
diff changeset
    30
import org.graalvm.compiler.debug.DebugContext;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    31
import org.graalvm.compiler.graph.Node;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    32
import org.graalvm.compiler.nodeinfo.InputType;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    33
import org.graalvm.compiler.nodes.AbstractBeginNode;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    34
import org.graalvm.compiler.nodes.AbstractDeoptimizeNode;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    35
import org.graalvm.compiler.nodes.AbstractEndNode;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    36
import org.graalvm.compiler.nodes.AbstractMergeNode;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    37
import org.graalvm.compiler.nodes.BeginNode;
46762
f7defa99f173 8185829: Update Graal
dlong
parents: 46640
diff changeset
    38
import org.graalvm.compiler.nodes.CompressionNode;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    39
import org.graalvm.compiler.nodes.DeoptimizeNode;
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    40
import org.graalvm.compiler.nodes.DeoptimizingFixedWithNextNode;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    41
import org.graalvm.compiler.nodes.DynamicDeoptimizeNode;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    42
import org.graalvm.compiler.nodes.FixedNode;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    43
import org.graalvm.compiler.nodes.IfNode;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    44
import org.graalvm.compiler.nodes.LogicNode;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    45
import org.graalvm.compiler.nodes.StructuredGraph;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    46
import org.graalvm.compiler.nodes.ValueNode;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    47
import org.graalvm.compiler.nodes.ValuePhiNode;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    48
import org.graalvm.compiler.nodes.calc.IsNullNode;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    49
import org.graalvm.compiler.nodes.extended.NullCheckNode;
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    50
import org.graalvm.compiler.nodes.memory.FixedAccessNode;
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    51
import org.graalvm.compiler.nodes.memory.address.AddressNode;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    52
import org.graalvm.compiler.nodes.util.GraphUtil;
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    53
import org.graalvm.compiler.options.Option;
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    54
import org.graalvm.compiler.options.OptionKey;
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    55
import org.graalvm.compiler.options.OptionType;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    56
import org.graalvm.compiler.phases.BasePhase;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    57
import org.graalvm.compiler.phases.tiers.LowTierContext;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    58
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    59
import jdk.vm.ci.meta.DeoptimizationReason;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    60
import jdk.vm.ci.meta.JavaConstant;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    61
import jdk.vm.ci.meta.MetaAccessProvider;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    62
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    63
public class UseTrappingNullChecksPhase extends BasePhase<LowTierContext> {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    64
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46344
diff changeset
    65
    private static final CounterKey counterTrappingNullCheck = DebugContext.counter("TrappingNullCheck");
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46344
diff changeset
    66
    private static final CounterKey counterTrappingNullCheckExistingRead = DebugContext.counter("TrappingNullCheckExistingRead");
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46344
diff changeset
    67
    private static final CounterKey counterTrappingNullCheckUnreached = DebugContext.counter("TrappingNullCheckUnreached");
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46344
diff changeset
    68
    private static final CounterKey counterTrappingNullCheckDynamicDeoptimize = DebugContext.counter("TrappingNullCheckDynamicDeoptimize");
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    69
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    70
    public static class Options {
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    71
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    72
        // @formatter:off
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    73
        @Option(help = "Use traps for null checks instead of explicit null-checks", type = OptionType.Expert)
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    74
        public static final OptionKey<Boolean> UseTrappingNullChecks = new OptionKey<>(true);
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    75
        // @formatter:on
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    76
    }
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    77
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    78
    @Override
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    79
    protected void run(StructuredGraph graph, LowTierContext context) {
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    80
        if (!Options.UseTrappingNullChecks.getValue(graph.getOptions()) || context.getTarget().implicitNullCheckLimit <= 0) {
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    81
            return;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    82
        }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    83
        assert graph.getGuardsStage().areFrameStatesAtDeopts();
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    84
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    85
        long implicitNullCheckLimit = context.getTarget().implicitNullCheckLimit;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    86
        for (DeoptimizeNode deopt : graph.getNodes(DeoptimizeNode.TYPE)) {
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    87
            tryUseTrappingNullCheck(deopt, deopt.predecessor(), deopt.getReason(), deopt.getSpeculation(), implicitNullCheckLimit);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    88
        }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    89
        for (DynamicDeoptimizeNode deopt : graph.getNodes(DynamicDeoptimizeNode.TYPE)) {
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    90
            tryUseTrappingNullCheck(context.getMetaAccess(), deopt, implicitNullCheckLimit);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    91
        }
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    92
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    93
    }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    94
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    95
    private static void tryUseTrappingNullCheck(MetaAccessProvider metaAccessProvider, DynamicDeoptimizeNode deopt, long implicitNullCheckLimit) {
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    96
        Node predecessor = deopt.predecessor();
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    97
        if (predecessor instanceof AbstractMergeNode) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    98
            AbstractMergeNode merge = (AbstractMergeNode) predecessor;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    99
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   100
            // Process each predecessor at the merge, unpacking the reasons and speculations as
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   101
            // needed.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   102
            ValueNode reason = deopt.getActionAndReason();
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   103
            ValuePhiNode reasonPhi = null;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   104
            List<ValueNode> reasons = null;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   105
            int expectedPhis = 0;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   106
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   107
            if (reason instanceof ValuePhiNode) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   108
                reasonPhi = (ValuePhiNode) reason;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   109
                if (reasonPhi.merge() != merge) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   110
                    return;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   111
                }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   112
                reasons = reasonPhi.values().snapshot();
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   113
                expectedPhis++;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   114
            } else if (!reason.isConstant()) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   115
                return;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   116
            }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   117
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   118
            ValueNode speculation = deopt.getSpeculation();
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   119
            ValuePhiNode speculationPhi = null;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   120
            List<ValueNode> speculations = null;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   121
            if (speculation instanceof ValuePhiNode) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   122
                speculationPhi = (ValuePhiNode) speculation;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   123
                if (speculationPhi.merge() != merge) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   124
                    return;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   125
                }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   126
                speculations = speculationPhi.values().snapshot();
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   127
                expectedPhis++;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   128
            }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   129
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   130
            if (merge.phis().count() != expectedPhis) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   131
                return;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   132
            }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   133
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   134
            int index = 0;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   135
            for (AbstractEndNode end : merge.cfgPredecessors().snapshot()) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   136
                ValueNode thisReason = reasons != null ? reasons.get(index) : reason;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   137
                ValueNode thisSpeculation = speculations != null ? speculations.get(index++) : speculation;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   138
                if (!thisReason.isConstant() || !thisSpeculation.isConstant() || !thisSpeculation.asConstant().equals(JavaConstant.NULL_POINTER)) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   139
                    continue;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   140
                }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   141
                DeoptimizationReason deoptimizationReason = metaAccessProvider.decodeDeoptReason(thisReason.asJavaConstant());
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   142
                tryUseTrappingNullCheck(deopt, end.predecessor(), deoptimizationReason, null, implicitNullCheckLimit);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   143
            }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   144
        }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   145
    }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   146
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   147
    private static void tryUseTrappingNullCheck(AbstractDeoptimizeNode deopt, Node predecessor, DeoptimizationReason deoptimizationReason, JavaConstant speculation, long implicitNullCheckLimit) {
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   148
        if (deoptimizationReason != DeoptimizationReason.NullCheckException && deoptimizationReason != DeoptimizationReason.UnreachedCode) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   149
            return;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   150
        }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   151
        if (speculation != null && !speculation.equals(JavaConstant.NULL_POINTER)) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   152
            return;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   153
        }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   154
        if (predecessor instanceof AbstractMergeNode) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   155
            AbstractMergeNode merge = (AbstractMergeNode) predecessor;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   156
            if (merge.phis().isEmpty()) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   157
                for (AbstractEndNode end : merge.cfgPredecessors().snapshot()) {
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   158
                    checkPredecessor(deopt, end.predecessor(), deoptimizationReason, implicitNullCheckLimit);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   159
                }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   160
            }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   161
        } else if (predecessor instanceof AbstractBeginNode) {
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   162
            checkPredecessor(deopt, predecessor, deoptimizationReason, implicitNullCheckLimit);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   163
        }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   164
    }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   165
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   166
    private static void checkPredecessor(AbstractDeoptimizeNode deopt, Node predecessor, DeoptimizationReason deoptimizationReason, long implicitNullCheckLimit) {
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   167
        Node current = predecessor;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   168
        AbstractBeginNode branch = null;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   169
        while (current instanceof AbstractBeginNode) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   170
            branch = (AbstractBeginNode) current;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   171
            if (branch.anchored().isNotEmpty()) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   172
                // some input of the deopt framestate is anchored to this branch
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   173
                return;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   174
            }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   175
            current = current.predecessor();
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   176
        }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   177
        if (current instanceof IfNode) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   178
            IfNode ifNode = (IfNode) current;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   179
            if (branch != ifNode.trueSuccessor()) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   180
                return;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   181
            }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   182
            LogicNode condition = ifNode.condition();
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   183
            if (condition instanceof IsNullNode) {
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   184
                replaceWithTrappingNullCheck(deopt, ifNode, condition, deoptimizationReason, implicitNullCheckLimit);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   185
            }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   186
        }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   187
    }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   188
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   189
    private static void replaceWithTrappingNullCheck(AbstractDeoptimizeNode deopt, IfNode ifNode, LogicNode condition, DeoptimizationReason deoptimizationReason, long implicitNullCheckLimit) {
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46344
diff changeset
   190
        DebugContext debug = deopt.getDebug();
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46344
diff changeset
   191
        counterTrappingNullCheck.increment(debug);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   192
        if (deopt instanceof DynamicDeoptimizeNode) {
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46344
diff changeset
   193
            counterTrappingNullCheckDynamicDeoptimize.increment(debug);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   194
        }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   195
        if (deoptimizationReason == DeoptimizationReason.UnreachedCode) {
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46344
diff changeset
   196
            counterTrappingNullCheckUnreached.increment(debug);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   197
        }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   198
        IsNullNode isNullNode = (IsNullNode) condition;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   199
        AbstractBeginNode nonTrappingContinuation = ifNode.falseSuccessor();
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   200
        AbstractBeginNode trappingContinuation = ifNode.trueSuccessor();
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   201
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   202
        DeoptimizingFixedWithNextNode trappingNullCheck = null;
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   203
        FixedNode nextNonTrapping = nonTrappingContinuation.next();
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   204
        ValueNode value = isNullNode.getValue();
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   205
        if (OptImplicitNullChecks.getValue(ifNode.graph().getOptions()) && implicitNullCheckLimit > 0) {
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   206
            if (nextNonTrapping instanceof FixedAccessNode) {
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   207
                FixedAccessNode fixedAccessNode = (FixedAccessNode) nextNonTrapping;
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   208
                if (fixedAccessNode.canNullCheck()) {
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   209
                    AddressNode address = fixedAccessNode.getAddress();
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   210
                    ValueNode base = address.getBase();
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   211
                    ValueNode index = address.getIndex();
46762
f7defa99f173 8185829: Update Graal
dlong
parents: 46640
diff changeset
   212
                    // allow for architectures which cannot fold an
f7defa99f173 8185829: Update Graal
dlong
parents: 46640
diff changeset
   213
                    // intervening uncompress out of the address chain
f7defa99f173 8185829: Update Graal
dlong
parents: 46640
diff changeset
   214
                    if (base != null && base instanceof CompressionNode) {
f7defa99f173 8185829: Update Graal
dlong
parents: 46640
diff changeset
   215
                        base = ((CompressionNode) base).getValue();
f7defa99f173 8185829: Update Graal
dlong
parents: 46640
diff changeset
   216
                    }
f7defa99f173 8185829: Update Graal
dlong
parents: 46640
diff changeset
   217
                    if (index != null && index instanceof CompressionNode) {
f7defa99f173 8185829: Update Graal
dlong
parents: 46640
diff changeset
   218
                        index = ((CompressionNode) index).getValue();
f7defa99f173 8185829: Update Graal
dlong
parents: 46640
diff changeset
   219
                    }
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   220
                    if (((base == value && index == null) || (base == null && index == value)) && address.getMaxConstantDisplacement() < implicitNullCheckLimit) {
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   221
                        // Opportunity for implicit null check as part of an existing read found!
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   222
                        fixedAccessNode.setStateBefore(deopt.stateBefore());
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   223
                        fixedAccessNode.setNullCheck(true);
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   224
                        deopt.graph().removeSplit(ifNode, nonTrappingContinuation);
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   225
                        trappingNullCheck = fixedAccessNode;
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46344
diff changeset
   226
                        counterTrappingNullCheckExistingRead.increment(debug);
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   227
                    }
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   228
                }
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   229
            }
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   230
        }
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   231
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   232
        if (trappingNullCheck == null) {
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   233
            // Need to add a null check node.
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   234
            trappingNullCheck = deopt.graph().add(new NullCheckNode(value));
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   235
            deopt.graph().replaceSplit(ifNode, trappingNullCheck, nonTrappingContinuation);
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   236
        }
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   237
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   238
        trappingNullCheck.setStateBefore(deopt.stateBefore());
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   239
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   240
        /*
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   241
         * We now have the pattern NullCheck/BeginNode/... It's possible some node is using the
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   242
         * BeginNode as a guard input, so replace guard users of the Begin with the NullCheck and
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   243
         * then remove the Begin from the graph.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   244
         */
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   245
        nonTrappingContinuation.replaceAtUsages(InputType.Guard, trappingNullCheck);
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   246
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   247
        if (nonTrappingContinuation instanceof BeginNode) {
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
   248
            GraphUtil.unlinkFixedNode(nonTrappingContinuation);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   249
            nonTrappingContinuation.safeDelete();
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   250
        }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   251
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   252
        GraphUtil.killCFG(trappingContinuation);
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   253
        GraphUtil.tryKillUnused(isNullNode);
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   254
    }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   255
}