001/*
002 * Copyright (c) 2011, 2015, 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.java;
024
025import static com.oracle.graal.nodes.java.ForeignCallDescriptors.*;
026import jdk.internal.jvmci.meta.*;
027import jdk.internal.jvmci.meta.Assumptions.*;
028
029import com.oracle.graal.compiler.common.type.*;
030import com.oracle.graal.compiler.common.spi.*;
031import com.oracle.graal.graph.*;
032import com.oracle.graal.graph.spi.*;
033import com.oracle.graal.nodeinfo.*;
034import com.oracle.graal.nodes.*;
035import com.oracle.graal.nodes.spi.*;
036import com.oracle.graal.nodes.virtual.*;
037
038/**
039 * This node is used to perform the finalizer registration at the end of the java.lang.Object
040 * constructor.
041 */
042@NodeInfo
043public final class RegisterFinalizerNode extends AbstractStateSplit implements Canonicalizable.Unary<ValueNode>, LIRLowerable, Virtualizable, DeoptimizingNode.DeoptAfter {
044
045    public static final NodeClass<RegisterFinalizerNode> TYPE = NodeClass.create(RegisterFinalizerNode.class);
046    @OptionalInput(InputType.State) FrameState deoptState;
047    @Input ValueNode value;
048
049    public RegisterFinalizerNode(ValueNode value) {
050        super(TYPE, StampFactory.forVoid());
051        this.value = value;
052    }
053
054    public ValueNode getValue() {
055        return value;
056    }
057
058    @Override
059    public void generate(NodeLIRBuilderTool gen) {
060        ForeignCallLinkage linkage = gen.getLIRGeneratorTool().getForeignCalls().lookupForeignCall(REGISTER_FINALIZER);
061        gen.getLIRGeneratorTool().emitForeignCall(linkage, gen.state(this), gen.operand(getValue()));
062    }
063
064    /**
065     * Determines if the compiler should emit code to test whether a given object has a finalizer
066     * that must be registered with the runtime upon object initialization.
067     */
068    public static boolean mayHaveFinalizer(ValueNode object, Assumptions assumptions) {
069        ObjectStamp objectStamp = (ObjectStamp) object.stamp();
070        if (objectStamp.isExactType()) {
071            return objectStamp.type().hasFinalizer();
072        } else if (objectStamp.type() != null) {
073            AssumptionResult<Boolean> result = objectStamp.type().hasFinalizableSubclass();
074            if (result.isAssumptionFree()) {
075                return result.getResult();
076            } else if (assumptions != null) {
077                assumptions.record(result);
078                return result.getResult();
079            }
080        }
081        return true;
082    }
083
084    @Override
085    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
086        if (!(forValue.stamp() instanceof ObjectStamp)) {
087            return this;
088        }
089        if (!mayHaveFinalizer(forValue, graph().getAssumptions())) {
090            return null;
091        }
092
093        return this;
094    }
095
096    @Override
097    public void virtualize(VirtualizerTool tool) {
098        ValueNode alias = tool.getAlias(getValue());
099        if (alias instanceof VirtualObjectNode && !((VirtualObjectNode) alias).type().hasFinalizer()) {
100            tool.delete();
101        }
102    }
103
104    @Override
105    public boolean canDeoptimize() {
106        return true;
107    }
108
109    @NodeIntrinsic
110    public static native void register(Object thisObj);
111}