FixedGuardNode.java revision 12651:6ef01bd40ce2
1193326Sed/*
2193326Sed * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3193326Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4193326Sed *
5193326Sed * This code is free software; you can redistribute it and/or modify it
6193326Sed * under the terms of the GNU General Public License version 2 only, as
7193326Sed * published by the Free Software Foundation.
8193326Sed *
9193326Sed * This code is distributed in the hope that it will be useful, but WITHOUT
10193326Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11193326Sed * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12193326Sed * version 2 for more details (a copy is included in the LICENSE file that
13193326Sed * accompanied this code).
14193326Sed *
15239462Sdim * You should have received a copy of the GNU General Public License version
16249423Sdim * 2 along with this work; if not, write to the Free Software Foundation,
17193326Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18193326Sed *
19218893Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20207619Srdivacky * or visit www.oracle.com if you need additional information or have any
21218893Sdim * questions.
22249423Sdim */
23249423Sdimpackage org.graalvm.compiler.nodes;
24249423Sdim
25234353Sdimimport static org.graalvm.compiler.nodeinfo.InputType.Guard;
26249423Sdimimport static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
27193326Sedimport static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_2;
28193326Sed
29193326Sedimport org.graalvm.compiler.graph.IterableNodeType;
30193326Sedimport org.graalvm.compiler.graph.NodeClass;
31193326Sedimport org.graalvm.compiler.graph.spi.SimplifierTool;
32193326Sedimport org.graalvm.compiler.nodeinfo.NodeInfo;
33193326Sedimport org.graalvm.compiler.nodes.extended.ValueAnchorNode;
34199990Srdivackyimport org.graalvm.compiler.nodes.spi.Lowerable;
35226633Sdimimport org.graalvm.compiler.nodes.spi.LoweringTool;
36193326Sed
37193326Sedimport jdk.vm.ci.meta.DeoptimizationAction;
38193326Sedimport jdk.vm.ci.meta.DeoptimizationReason;
39193326Sedimport jdk.vm.ci.meta.JavaConstant;
40193326Sed
41239462Sdim@NodeInfo(nameTemplate = "FixedGuard(!={p#negated}) {p#reason/s}", allowedUsageTypes = Guard, size = SIZE_2, cycles = CYCLES_2)
42195341Sedpublic final class FixedGuardNode extends AbstractFixedGuardNode implements Lowerable, IterableNodeType {
43193326Sed    public static final NodeClass<FixedGuardNode> TYPE = NodeClass.create(FixedGuardNode.class);
44239462Sdim
45198092Srdivacky    public FixedGuardNode(LogicNode condition, DeoptimizationReason deoptReason, DeoptimizationAction action) {
46193326Sed        this(condition, deoptReason, action, JavaConstant.NULL_POINTER, false);
47193326Sed    }
48193326Sed
49193326Sed    public FixedGuardNode(LogicNode condition, DeoptimizationReason deoptReason, DeoptimizationAction action, boolean negated) {
50193326Sed        this(condition, deoptReason, action, JavaConstant.NULL_POINTER, negated);
51193326Sed    }
52193326Sed
53193326Sed    public FixedGuardNode(LogicNode condition, DeoptimizationReason deoptReason, DeoptimizationAction action, JavaConstant speculation, boolean negated) {
54193326Sed        super(TYPE, condition, deoptReason, action, speculation, negated);
55193326Sed    }
56193326Sed
57193326Sed    @Override
58193326Sed    public void simplify(SimplifierTool tool) {
59193326Sed        super.simplify(tool);
60193326Sed
61193326Sed        if (getCondition() instanceof LogicConstantNode) {
62193326Sed            LogicConstantNode c = (LogicConstantNode) getCondition();
63193326Sed            if (c.getValue() == isNegated()) {
64193326Sed                FixedNode currentNext = this.next();
65193326Sed                if (currentNext != null) {
66193326Sed                    tool.deleteBranch(currentNext);
67243830Sdim                }
68193326Sed
69193326Sed                DeoptimizeNode deopt = graph().add(new DeoptimizeNode(getAction(), getReason(), getSpeculation()));
70218893Sdim                deopt.setStateBefore(stateBefore());
71221345Sdim                setNext(deopt);
72221345Sdim            }
73276479Sdim            this.replaceAtUsages(null);
74198092Srdivacky            graph().removeFixed(this);
75193326Sed        } else if (getCondition() instanceof ShortCircuitOrNode) {
76193326Sed            ShortCircuitOrNode shortCircuitOr = (ShortCircuitOrNode) getCondition();
77193326Sed            if (isNegated() && hasNoUsages()) {
78193326Sed                graph().addAfterFixed(this, graph().add(new FixedGuardNode(shortCircuitOr.getY(), getReason(), getAction(), getSpeculation(), !shortCircuitOr.isYNegated())));
79193326Sed                graph().replaceFixedWithFixed(this, graph().add(new FixedGuardNode(shortCircuitOr.getX(), getReason(), getAction(), getSpeculation(), !shortCircuitOr.isXNegated())));
80193326Sed            }
81198092Srdivacky        }
82226633Sdim    }
83193326Sed
84193326Sed    @Override
85193326Sed    public void lower(LoweringTool tool) {
86193326Sed        if (graph().getGuardsStage().allowsFloatingGuards()) {
87198092Srdivacky            /*
88198092Srdivacky             * Don't allow guards with action None and reason RuntimeConstraint to float. In cases
89193326Sed             * where 2 guards are testing equivalent conditions they might be lowered at the same
90193326Sed             * location. If the guard with the None action is lowered before the the other guard
91193326Sed             * then the code will be stuck repeatedly deoptimizing without invalidating the code.
92193326Sed             * Conditional elimination will eliminate the guard if it's truly redundant in this
93276479Sdim             * case.
94218893Sdim             */
95212904Sdim            if (getAction() != DeoptimizationAction.None || getReason() != DeoptimizationReason.RuntimeConstraint) {
96212904Sdim                ValueNode guard = tool.createGuard(this, getCondition(), getReason(), getAction(), getSpeculation(), isNegated()).asNode();
97218893Sdim                this.replaceAtUsages(guard);
98212904Sdim                ValueAnchorNode newAnchor = graph().add(new ValueAnchorNode(guard.asNode()));
99212904Sdim                graph().replaceFixedWithFixed(this, newAnchor);
100212904Sdim            }
101198092Srdivacky        } else {
102212904Sdim            lowerToIf().lower(tool);
103193326Sed        }
104193326Sed    }
105208600Srdivacky
106193326Sed    @Override
107193326Sed    public boolean canDeoptimize() {
108193326Sed        return true;
109193326Sed    }
110193326Sed}
111193326Sed