001/*
002 * Copyright (c) 2012, 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.hotspot.replacements;
024
025import jdk.internal.jvmci.code.*;
026import com.oracle.graal.debug.*;
027import static com.oracle.graal.hotspot.replacements.HotSpotReplacementsUtil.*;
028import static com.oracle.graal.hotspot.replacements.TypeCheckSnippetUtils.*;
029import static com.oracle.graal.nodes.PiNode.*;
030import static com.oracle.graal.nodes.extended.BranchProbabilityNode.*;
031import static com.oracle.graal.replacements.SnippetTemplate.*;
032import static jdk.internal.jvmci.meta.DeoptimizationAction.*;
033import static jdk.internal.jvmci.meta.DeoptimizationReason.*;
034
035import com.oracle.graal.compiler.common.type.*;
036import com.oracle.graal.hotspot.meta.*;
037import com.oracle.graal.hotspot.nodes.*;
038import com.oracle.graal.hotspot.word.*;
039import com.oracle.graal.nodes.*;
040import com.oracle.graal.nodes.extended.*;
041import com.oracle.graal.nodes.java.*;
042import com.oracle.graal.nodes.spi.*;
043import com.oracle.graal.replacements.*;
044import com.oracle.graal.replacements.SnippetTemplate.AbstractTemplates;
045import com.oracle.graal.replacements.SnippetTemplate.Arguments;
046import com.oracle.graal.replacements.SnippetTemplate.SnippetInfo;
047
048/**
049 * Snippet used for lowering {@link CheckCastDynamicNode}.
050 */
051public class CheckCastDynamicSnippets implements Snippets {
052
053    @Snippet
054    public static Object checkcastDynamic(KlassPointer hub, Object object) {
055        if (probability(NOT_FREQUENT_PROBABILITY, object == null)) {
056            isNull.inc();
057        } else {
058            GuardingNode anchorNode = SnippetAnchorNode.anchor();
059            KlassPointer objectHub = loadHubIntrinsic(object, anchorNode);
060            if (!checkUnknownSubType(hub, objectHub)) {
061                DeoptimizeNode.deopt(InvalidateReprofile, ClassCastException);
062            }
063        }
064        GuardingNode anchorNode = SnippetAnchorNode.anchor();
065        return piCast(verifyOop(object), StampFactory.forNodeIntrinsic(), anchorNode);
066    }
067
068    public static class Templates extends AbstractTemplates {
069
070        private final SnippetInfo dynamic = snippet(CheckCastDynamicSnippets.class, "checkcastDynamic", SECONDARY_SUPER_CACHE_LOCATION);
071
072        public Templates(HotSpotProviders providers, TargetDescription target) {
073            super(providers, providers.getSnippetReflection(), target);
074        }
075
076        public void lower(CheckCastDynamicNode checkcast, LoweringTool tool) {
077            StructuredGraph graph = checkcast.graph();
078            ValueNode object = checkcast.object();
079
080            Arguments args = new Arguments(dynamic, graph.getGuardsStage(), tool.getLoweringStage());
081            args.add("hub", checkcast.hub());
082            args.add("object", object);
083
084            SnippetTemplate template = template(args);
085            Debug.log("Lowering dynamic checkcast in %s: node=%s, template=%s, arguments=%s", graph, checkcast, template, args);
086            template.instantiate(providers.getMetaAccess(), checkcast, DEFAULT_REPLACER, args);
087        }
088    }
089}