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;
024
025import java.util.*;
026
027import jdk.internal.jvmci.options.*;
028
029import com.oracle.graal.nodes.*;
030import com.oracle.graal.phases.common.*;
031import com.oracle.graal.phases.common.inlining.policy.*;
032import com.oracle.graal.phases.common.inlining.walker.*;
033import com.oracle.graal.phases.tiers.*;
034
035public class InliningPhase extends AbstractInliningPhase {
036
037    public static class Options {
038
039        // @formatter:off
040        @Option(help = "Unconditionally inline intrinsics", type = OptionType.Debug)
041        public static final OptionValue<Boolean> AlwaysInlineIntrinsics = new OptionValue<>(false);
042        // @formatter:on
043    }
044
045    private final InliningPolicy inliningPolicy;
046    private final CanonicalizerPhase canonicalizer;
047
048    private int inliningCount;
049    private int maxMethodPerInlining = Integer.MAX_VALUE;
050
051    public InliningPhase(CanonicalizerPhase canonicalizer) {
052        this(new GreedyInliningPolicy(null), canonicalizer);
053    }
054
055    public InliningPhase(Map<Invoke, Double> hints, CanonicalizerPhase canonicalizer) {
056        this(new GreedyInliningPolicy(hints), canonicalizer);
057    }
058
059    public InliningPhase(InliningPolicy policy, CanonicalizerPhase canonicalizer) {
060        this.inliningPolicy = policy;
061        this.canonicalizer = canonicalizer;
062    }
063
064    public void setMaxMethodsPerInlining(int max) {
065        maxMethodPerInlining = max;
066    }
067
068    public int getInliningCount() {
069        return inliningCount;
070    }
071
072    /**
073     *
074     * This method sets in motion the inlining machinery.
075     *
076     * @see InliningData
077     * @see InliningData#moveForward()
078     *
079     */
080    @Override
081    protected void run(final StructuredGraph graph, final HighTierContext context) {
082        final InliningData data = new InliningData(graph, context, maxMethodPerInlining, canonicalizer, inliningPolicy);
083
084        assert data.repOK();
085        while (data.hasUnprocessedGraphs()) {
086            boolean wasInlined = data.moveForward();
087            assert data.repOK();
088            if (wasInlined) {
089                inliningCount++;
090            }
091        }
092
093        assert data.inliningDepth() == 0;
094        assert data.graphCount() == 0;
095    }
096}