1 /*
   2  * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.nodes;
  24 
  25 import org.graalvm.compiler.debug.Debug;
  26 import org.graalvm.compiler.graph.NodeClass;
  27 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  28 import org.graalvm.compiler.nodeinfo.NodeInfo;
  29 import org.graalvm.compiler.nodes.spi.LIRLowerable;
  30 import org.graalvm.compiler.nodes.spi.Lowerable;
  31 import org.graalvm.compiler.nodes.spi.LoweringTool;
  32 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
  33 
  34 import jdk.vm.ci.meta.DeoptimizationAction;
  35 import jdk.vm.ci.meta.DeoptimizationReason;
  36 import jdk.vm.ci.meta.JavaConstant;
  37 import jdk.vm.ci.meta.JavaKind;
  38 import jdk.vm.ci.meta.MetaAccessProvider;
  39 import jdk.vm.ci.meta.Value;
  40 
  41 @NodeInfo(shortName = "Deopt", nameTemplate = "Deopt {p#reason/s}")
  42 public final class DeoptimizeNode extends AbstractDeoptimizeNode implements Lowerable, LIRLowerable {
  43     public static final int DEFAULT_DEBUG_ID = 0;
  44 
  45     public static final NodeClass<DeoptimizeNode> TYPE = NodeClass.create(DeoptimizeNode.class);
  46     protected final DeoptimizationAction action;
  47     protected final DeoptimizationReason reason;
  48     protected final int debugId;
  49     protected final JavaConstant speculation;
  50 
  51     public DeoptimizeNode(DeoptimizationAction action, DeoptimizationReason reason) {
  52         this(action, reason, DEFAULT_DEBUG_ID, JavaConstant.NULL_POINTER, null);
  53     }
  54 
  55     public DeoptimizeNode(DeoptimizationAction action, DeoptimizationReason reason, JavaConstant speculation) {
  56         this(action, reason, DEFAULT_DEBUG_ID, speculation, null);
  57     }
  58 
  59     public DeoptimizeNode(DeoptimizationAction action, DeoptimizationReason reason, int debugId, JavaConstant speculation, FrameState stateBefore) {
  60         super(TYPE, stateBefore);
  61         assert action != null;
  62         assert reason != null;
  63         assert speculation.getJavaKind() == JavaKind.Object;
  64         this.action = action;
  65         this.reason = reason;
  66         this.debugId = debugId;
  67         this.speculation = speculation;
  68     }
  69 
  70     public DeoptimizationAction action() {
  71         return action;
  72     }
  73 
  74     public DeoptimizationReason reason() {
  75         return reason;
  76     }
  77 
  78     @Override
  79     public void lower(LoweringTool tool) {
  80         tool.getLowerer().lower(this, tool);
  81     }
  82 
  83     @SuppressWarnings("deprecation")
  84     public int getDebugId() {
  85         int deoptDebugId = debugId;
  86         if (deoptDebugId == DEFAULT_DEBUG_ID && (Debug.isDumpEnabledForMethod() || Debug.isLogEnabledForMethod())) {
  87             deoptDebugId = this.getId();
  88         }
  89         return deoptDebugId;
  90     }
  91 
  92     @Override
  93     public void generate(NodeLIRBuilderTool gen) {
  94         LIRGeneratorTool tool = gen.getLIRGeneratorTool();
  95         Value actionAndReason = tool.emitJavaConstant(tool.getMetaAccess().encodeDeoptActionAndReason(action, reason, getDebugId()));
  96         Value speculationValue = tool.emitJavaConstant(speculation);
  97         gen.getLIRGeneratorTool().emitDeoptimize(actionAndReason, speculationValue, gen.state(this));
  98     }
  99 
 100     @Override
 101     public ValueNode getActionAndReason(MetaAccessProvider metaAccess) {
 102         return ConstantNode.forConstant(metaAccess.encodeDeoptActionAndReason(action, reason, getDebugId()), metaAccess, graph());
 103     }
 104 
 105     @Override
 106     public ValueNode getSpeculation(MetaAccessProvider metaAccess) {
 107         return ConstantNode.forConstant(speculation, metaAccess, graph());
 108     }
 109 
 110     public JavaConstant getSpeculation() {
 111         return speculation;
 112     }
 113 
 114     @NodeIntrinsic
 115     public static native void deopt(@ConstantNodeParameter DeoptimizationAction action, @ConstantNodeParameter DeoptimizationReason reason);
 116 }