001/*
002 * Copyright (c) 2011, 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.phases.common.inlining.walker;
024
025import java.util.*;
026
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.nodes.*;
030import com.oracle.graal.nodes.java.*;
031import com.oracle.graal.phases.common.inlining.info.*;
032import com.oracle.graal.phases.common.inlining.info.elem.*;
033
034/**
035 * <p>
036 * An instance of this class denotes a callsite being analyzed for inlining.
037 * </p>
038 * <p>
039 * Each element of the {@link InliningData} stack contains one such instance, the accompanying
040 * {@link CallsiteHolder}s in that element represent feasible targets for the callsite in question.
041 * </p>
042 *
043 * @see InliningData#moveForward()
044 */
045public class MethodInvocation {
046
047    private final InlineInfo callee;
048    private final double probability;
049    private final double relevance;
050
051    private int processedGraphs;
052
053    /**
054     * <p>
055     * The immutable positions of freshly instantiated arguments (ie, positions in
056     * <code>callee.invoke.callTarget.arguments</code>).
057     * </p>
058     *
059     * <p>
060     * A freshly instantiated argument is either:
061     * <uL>
062     * <li>an {@link InliningData#isFreshInstantiation(com.oracle.graal.nodes.ValueNode)}</li>
063     * <li>a fixed-param of the graph containing the callsite (ie, of <code>callee.graph()</code>
064     * that contains <code>callee.invoke</code>)</li>
065     * </uL>
066     * </p>
067     *
068     * <p>
069     * Given those positions, the
070     * {@link com.oracle.graal.phases.common.inlining.walker.CallsiteHolderExplorable} instantiated
071     * in {@link #buildCallsiteHolderForElement(int)} can determine which of <i>its</i> parameters
072     * are fixed.
073     * </p>
074     */
075    private final BitSet freshlyInstantiatedArguments;
076
077    private final int sizeFreshArgs;
078
079    public MethodInvocation(InlineInfo info, double probability, double relevance, BitSet freshlyInstantiatedArguments) {
080        this.callee = info;
081        this.probability = probability;
082        this.relevance = relevance;
083        this.freshlyInstantiatedArguments = freshlyInstantiatedArguments;
084        this.sizeFreshArgs = freshlyInstantiatedArguments == null ? 0 : freshlyInstantiatedArguments.cardinality();
085    }
086
087    public void incrementProcessedGraphs() {
088        processedGraphs++;
089        assert processedGraphs <= callee.numberOfMethods();
090    }
091
092    public int processedGraphs() {
093        assert processedGraphs <= callee.numberOfMethods();
094        return processedGraphs;
095    }
096
097    public int totalGraphs() {
098        return callee.numberOfMethods();
099    }
100
101    public InlineInfo callee() {
102        return callee;
103    }
104
105    public double probability() {
106        return probability;
107    }
108
109    public double relevance() {
110        return relevance;
111    }
112
113    public boolean isRoot() {
114        return callee == null;
115    }
116
117    public BitSet getFreshlyInstantiatedArguments() {
118        return freshlyInstantiatedArguments;
119    }
120
121    public int getSizeFreshArgs() {
122        return sizeFreshArgs;
123    }
124
125    public CallsiteHolder buildCallsiteHolderForElement(int index) {
126        Inlineable elem = callee.inlineableElementAt(index);
127        assert elem instanceof InlineableGraph;
128        InlineableGraph ig = (InlineableGraph) elem;
129        final double invokeProbability = probability * callee.probabilityAt(index);
130        final double invokeRelevance = relevance * callee.relevanceAt(index);
131        return new CallsiteHolderExplorable(ig.getGraph(), invokeProbability, invokeRelevance, freshlyInstantiatedArguments);
132    }
133
134    @Override
135    public String toString() {
136        if (isRoot()) {
137            return "<root>";
138        }
139        CallTargetNode callTarget = callee.invoke().callTarget();
140        if (callTarget instanceof MethodCallTargetNode) {
141            ResolvedJavaMethod calleeMethod = ((MethodCallTargetNode) callTarget).targetMethod();
142            return calleeMethod.format("Invoke#%H.%n(%p)");
143        } else {
144            return "Invoke#" + callTarget.targetName();
145        }
146    }
147}