001/*
002 * Copyright (c) 2013, 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.tiers;
024
025import static com.oracle.graal.phases.tiers.Suites.Options.*;
026
027import java.util.*;
028
029import jdk.internal.jvmci.common.*;
030import jdk.internal.jvmci.options.*;
031import jdk.internal.jvmci.service.*;
032
033import com.oracle.graal.lir.phases.*;
034import com.oracle.graal.phases.*;
035
036public final class Suites {
037
038    static class Options {
039
040        // @formatter:off
041        @Option(help = "The compiler configuration to use", type = OptionType.User)
042        static final OptionValue<String> CompilerConfiguration = new OptionValue<>("");
043        // @formatter:on
044    }
045
046    private final PhaseSuite<HighTierContext> highTier;
047    private final PhaseSuite<MidTierContext> midTier;
048    private final PhaseSuite<LowTierContext> lowTier;
049
050    private static final CompilerConfiguration defaultConfiguration;
051    private static final Map<String, CompilerConfiguration> configurations;
052
053    public PhaseSuite<HighTierContext> getHighTier() {
054        return highTier;
055    }
056
057    public PhaseSuite<MidTierContext> getMidTier() {
058        return midTier;
059    }
060
061    public PhaseSuite<LowTierContext> getLowTier() {
062        return lowTier;
063    }
064
065    static {
066        configurations = new HashMap<>();
067        CompilerConfiguration basic = null;
068        CompilerConfiguration nonBasic = null;
069        int nonBasicCount = 0;
070
071        for (CompilerConfiguration config : Services.load(CompilerConfiguration.class)) {
072            String name = config.getClass().getSimpleName();
073            if (name.endsWith("CompilerConfiguration")) {
074                name = name.substring(0, name.length() - "CompilerConfiguration".length());
075            }
076            name = name.toLowerCase();
077
078            configurations.put(name, config);
079            if (name.equals("economy")) {
080                // ignore economy configuration if not explicitely specified
081            } else if (name.equals("basic")) {
082                assert basic == null;
083                basic = config;
084            } else {
085                nonBasic = config;
086                nonBasicCount++;
087            }
088        }
089
090        if (nonBasicCount == 1) {
091            /*
092             * There is exactly one non-basic configuration. We use this one as default.
093             */
094            defaultConfiguration = nonBasic;
095        } else {
096            /*
097             * There is either no extended configuration available, or more than one. In that case,
098             * default to "basic".
099             */
100            defaultConfiguration = basic;
101            if (defaultConfiguration == null) {
102                throw new JVMCIError("unable to find basic compiler configuration");
103            }
104        }
105    }
106
107    public Suites(PhaseSuite<HighTierContext> highTier, PhaseSuite<MidTierContext> midTier, PhaseSuite<LowTierContext> lowTier) {
108        this.highTier = highTier;
109        this.midTier = midTier;
110        this.lowTier = lowTier;
111    }
112
113    private Suites(CompilerConfiguration config) {
114        highTier = config.createHighTier();
115        midTier = config.createMidTier();
116        lowTier = config.createLowTier();
117    }
118
119    public static Suites createDefaultSuites() {
120        String selected = CompilerConfiguration.getValue();
121        if (selected.equals("")) {
122            return new Suites(defaultConfiguration);
123        } else {
124            return createSuites(selected);
125        }
126    }
127
128    public static Suites createSuites(String name) {
129        CompilerConfiguration config = configurations.get(name);
130        if (config == null) {
131            throw new JVMCIError("unknown compiler configuration: " + name);
132        }
133        return new Suites(config);
134    }
135
136    public static LIRSuites createDefaultLIRSuites() {
137        String selected = CompilerConfiguration.getValue();
138        if (selected.equals("")) {
139            return new LIRSuites(defaultConfiguration.createPreAllocationOptimizationStage(), defaultConfiguration.createAllocationStage(),
140                            defaultConfiguration.createPostAllocationOptimizationStage());
141        } else {
142            return createLIRSuites(selected);
143        }
144    }
145
146    public static LIRSuites createLIRSuites(String name) {
147        CompilerConfiguration config = configurations.get(name);
148        if (config == null) {
149            throw new JVMCIError("unknown compiler configuration: " + name);
150        }
151        return new LIRSuites(config.createPreAllocationOptimizationStage(), config.createAllocationStage(), config.createPostAllocationOptimizationStage());
152    }
153
154}