001/*
002 * Copyright (c) 2014, 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.truffle.test.builtins;
024
025import java.util.*;
026
027import com.oracle.graal.truffle.*;
028import com.oracle.graal.truffle.TruffleInlining.CallTreeNodeVisitor;
029import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
030import com.oracle.truffle.api.dsl.*;
031import com.oracle.truffle.api.nodes.*;
032import com.oracle.truffle.sl.runtime.*;
033
034/**
035 * Returns <code>true</code> if a function got inlined for all calls from a given {@link SLFunction}
036 * . If no direct calls to the given {@link SLFunction} could be resolved or the call got inlined
037 * for some callsites and for some not then an {@link AssertionError} is thrown.
038 */
039@NodeInfo(shortName = "isInlined")
040public abstract class SLIsInlinedBuiltin extends SLGraalRuntimeBuiltin {
041
042    @Specialization
043    @TruffleBoundary
044    public Object isInlined(SLFunction rootFunction, SLFunction parentFunction, SLFunction inlinedFunction) {
045        InliningTrace trace = new InliningTrace();
046
047        for (OptimizedCallTarget target : findDuplicateCallTargets((OptimizedCallTarget) rootFunction.getCallTarget())) {
048            if (target.isValid()) {
049                searchInlined(trace, target, parentFunction, inlinedFunction);
050            }
051        }
052
053        if (trace.allFalse && trace.allTrue) {
054            throw new AssertionError(String.format("No optimized calls found from %s to %s .", parentFunction, inlinedFunction));
055        } else if (!trace.allFalse && !trace.allTrue) {
056            throw new AssertionError(String.format("Some optimized calls from %s to %s are inlined and some are not.", parentFunction, inlinedFunction));
057        }
058        if (trace.allTrue) {
059            return true;
060        } else {
061            return false;
062        }
063    }
064
065    private static void searchInlined(InliningTrace trace, OptimizedCallTarget rootTarget, SLFunction parent, SLFunction inlinedFunction) {
066        rootTarget.accept(new CallTreeNodeVisitor() {
067
068            public boolean visit(List<TruffleInlining> decisionStack, Node node) {
069                if (node instanceof OptimizedDirectCallNode) {
070                    OptimizedDirectCallNode callNode = (OptimizedDirectCallNode) node;
071                    if (callNode.getRootNode().getCallTarget() == parent.getCallTarget() && callNode.getCallTarget() == inlinedFunction.getCallTarget()) {
072                        TruffleInliningDecision decision = (TruffleInliningDecision) decisionStack.get(decisionStack.size() - 1);
073                        if (decision.isInline()) {
074                            trace.allFalse = false;
075                        } else {
076                            trace.allTrue = false;
077                        }
078                    }
079                }
080                return true;
081            }
082
083        }, true);
084    }
085
086    private static final class InliningTrace {
087        boolean allFalse = true;
088        boolean allTrue = true;
089    }
090}