001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.nodes;
024
025import jdk.internal.jvmci.meta.*;
026
027import com.oracle.graal.compiler.common.type.*;
028import com.oracle.graal.graph.*;
029import com.oracle.graal.graph.spi.*;
030import com.oracle.graal.nodeinfo.*;
031import com.oracle.graal.nodes.extended.*;
032import com.oracle.graal.nodes.util.*;
033
034@NodeInfo
035public abstract class AbstractFixedGuardNode extends DeoptimizingFixedWithNextNode implements Simplifiable, GuardingNode {
036
037    public static final NodeClass<AbstractFixedGuardNode> TYPE = NodeClass.create(AbstractFixedGuardNode.class);
038    @Input(InputType.Condition) protected LogicNode condition;
039    protected final DeoptimizationReason reason;
040    protected final DeoptimizationAction action;
041    protected JavaConstant speculation;
042    protected boolean negated;
043
044    public LogicNode condition() {
045        return condition;
046    }
047
048    public void setCondition(LogicNode x) {
049        updateUsages(condition, x);
050        condition = x;
051    }
052
053    protected AbstractFixedGuardNode(NodeClass<? extends AbstractFixedGuardNode> c, LogicNode condition, DeoptimizationReason deoptReason, DeoptimizationAction action, JavaConstant speculation,
054                    boolean negated) {
055        super(c, StampFactory.forVoid());
056        this.action = action;
057        this.speculation = speculation;
058        this.negated = negated;
059        this.condition = condition;
060        this.reason = deoptReason;
061    }
062
063    public DeoptimizationReason getReason() {
064        return reason;
065    }
066
067    public DeoptimizationAction getAction() {
068        return action;
069    }
070
071    public JavaConstant getSpeculation() {
072        return speculation;
073    }
074
075    public boolean isNegated() {
076        return negated;
077    }
078
079    @Override
080    public String toString(Verbosity verbosity) {
081        if (verbosity == Verbosity.Name && negated) {
082            return "!" + super.toString(verbosity);
083        } else {
084            return super.toString(verbosity);
085        }
086    }
087
088    @Override
089    public void simplify(SimplifierTool tool) {
090        while (condition instanceof LogicNegationNode) {
091            LogicNegationNode negation = (LogicNegationNode) condition;
092            setCondition(negation.getValue());
093            negated = !negated;
094        }
095    }
096
097    public DeoptimizeNode lowerToIf() {
098        FixedNode currentNext = next();
099        setNext(null);
100        DeoptimizeNode deopt = graph().add(new DeoptimizeNode(action, reason));
101        deopt.setStateBefore(stateBefore());
102        IfNode ifNode;
103        AbstractBeginNode noDeoptSuccessor;
104        if (negated) {
105            ifNode = graph().add(new IfNode(condition, deopt, currentNext, 0));
106            noDeoptSuccessor = ifNode.falseSuccessor();
107        } else {
108            ifNode = graph().add(new IfNode(condition, currentNext, deopt, 1));
109            noDeoptSuccessor = ifNode.trueSuccessor();
110        }
111        ((FixedWithNextNode) predecessor()).setNext(ifNode);
112        this.replaceAtUsages(noDeoptSuccessor);
113        GraphUtil.killWithUnusedFloatingInputs(this);
114
115        return deopt;
116    }
117
118    @Override
119    public boolean canDeoptimize() {
120        return true;
121    }
122}