1 /* 2 * Copyright (c) 2011, 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.java; 24 25 import static org.graalvm.compiler.nodeinfo.InputType.State; 26 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_UNKNOWN; 27 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_20; 28 import static org.graalvm.compiler.nodes.java.ForeignCallDescriptors.REGISTER_FINALIZER; 29 30 import org.graalvm.compiler.core.common.spi.ForeignCallLinkage; 31 import org.graalvm.compiler.core.common.type.ObjectStamp; 32 import org.graalvm.compiler.core.common.type.StampFactory; 33 import org.graalvm.compiler.graph.NodeClass; 34 import org.graalvm.compiler.graph.spi.Canonicalizable; 35 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 36 import org.graalvm.compiler.nodeinfo.NodeInfo; 37 import org.graalvm.compiler.nodes.AbstractStateSplit; 38 import org.graalvm.compiler.nodes.DeoptimizingNode; 39 import org.graalvm.compiler.nodes.FrameState; 40 import org.graalvm.compiler.nodes.ValueNode; 41 import org.graalvm.compiler.nodes.spi.LIRLowerable; 42 import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; 43 import org.graalvm.compiler.nodes.spi.Virtualizable; 44 import org.graalvm.compiler.nodes.spi.VirtualizerTool; 45 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 46 47 import jdk.vm.ci.meta.Assumptions; 48 import jdk.vm.ci.meta.Assumptions.AssumptionResult; 49 50 /** 51 * This node is used to perform the finalizer registration at the end of the java.lang.Object 52 * constructor. 53 */ 54 // @formatter:off 55 @NodeInfo(cycles = CYCLES_UNKNOWN, 56 cyclesRationale = "We cannot estimate the time of a runtime call.", 57 size = SIZE_20, 58 sizeRationale = "Rough estimation for register handling & calling") 59 // @formatter:on 60 public final class RegisterFinalizerNode extends AbstractStateSplit implements Canonicalizable.Unary<ValueNode>, LIRLowerable, Virtualizable, DeoptimizingNode.DeoptAfter { 61 62 public static final NodeClass<RegisterFinalizerNode> TYPE = NodeClass.create(RegisterFinalizerNode.class); 63 @OptionalInput(State) FrameState deoptState; 64 @Input ValueNode value; 65 66 public RegisterFinalizerNode(ValueNode value) { 67 super(TYPE, StampFactory.forVoid()); 68 this.value = value; 69 } 70 71 @Override 72 public ValueNode getValue() { 73 return value; 74 } 75 76 @Override 77 public void generate(NodeLIRBuilderTool gen) { 78 // Note that an unconditional call to the runtime routine is made without 79 // checking that the object actually has a finalizer. This requires the 80 // runtime routine to do the check. 81 ForeignCallLinkage linkage = gen.getLIRGeneratorTool().getForeignCalls().lookupForeignCall(REGISTER_FINALIZER); 82 gen.getLIRGeneratorTool().emitForeignCall(linkage, gen.state(this), gen.operand(getValue())); 83 } 84 85 /** 86 * Determines if the compiler should emit code to test whether a given object has a finalizer 87 * that must be registered with the runtime upon object initialization. 88 */ 89 public static boolean mayHaveFinalizer(ValueNode object, Assumptions assumptions) { 90 ObjectStamp objectStamp = (ObjectStamp) object.stamp(); 91 if (objectStamp.isExactType()) { 92 return objectStamp.type().hasFinalizer(); 93 } else if (objectStamp.type() != null) { 94 AssumptionResult<Boolean> result = objectStamp.type().hasFinalizableSubclass(); 95 if (result.canRecordTo(assumptions)) { 96 result.recordTo(assumptions); 97 return result.getResult(); 98 } 99 } 100 return true; 101 } 102 103 @Override 104 public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) { 105 if (!(forValue.stamp() instanceof ObjectStamp)) { 106 return this; 107 } 108 if (!mayHaveFinalizer(forValue, graph().getAssumptions())) { 109 return null; 110 } 111 112 return this; 113 } 114 115 @Override 116 public void virtualize(VirtualizerTool tool) { 117 ValueNode alias = tool.getAlias(getValue()); 118 if (alias instanceof VirtualObjectNode && !((VirtualObjectNode) alias).type().hasFinalizer()) { 119 tool.delete(); 120 } 121 } 122 123 @Override 124 public boolean canDeoptimize() { 125 return true; 126 } 127 128 @NodeIntrinsic 129 public static native void register(Object thisObj); 130 }