Search in sources :

Example 1 with GreedyOrder

use of com.jopdesign.jcopter.greedy.GreedyConfig.GreedyOrder in project jop by jop-devel.

the class GreedyOptimizer method optimize.

public void optimize() {
    List<MethodInfo> rootMethods = config.getTargetMethods();
    // initialization
    resetCounters();
    boolean useWCAProvider = config.useWCA() && config.useWCAExecCount();
    GreedyOrder order = config.getOrder();
    if (order != GreedyOrder.WCAFirst) {
        //      updated as well.
        if (!config.getTargetMethodSet().equals(config.getWCATargetSet())) {
            logger.warn("Using the WCA for exec frequencies is currently only supported if order is WCAFirst " + "or if the target method is the WCA target method");
            useWCAProvider = false;
        }
    }
    AnalysisManager analyses = initializeAnalyses(config.useWCEP() || useWCAProvider);
    for (CodeOptimizer opt : optimizers) {
        opt.initialize(analyses, rootMethods);
    }
    CandidateSelector selector;
    if (config.useWCA()) {
        GainCalculator gc = new GainCalculator(analyses);
        if (config.useWCEP()) {
            logger.info("Using WCEP driven selector");
            selector = new WCEPRebateSelector(analyses, gc, config.getMaxCodesize());
        } else {
            logger.info("Using WCA driven selector");
            selector = new WCETRebateSelector(analyses, gc, config.getMaxCodesize());
        }
    } else {
        logger.info("WCA is disabled, not using WCA for optimization.");
        selector = new ACETRebateSelector(analyses, new GainCalculator(analyses), config.getMaxCodesize());
    }
    selector.initialize(config, true);
    ExecFrequencyProvider ecp = useWCAProvider ? analyses.getWCAInvoker() : analyses.getExecFrequencyAnalysis();
    if (config.useLocalExecCount()) {
        ecp = new LocalExecFrequencyProvider(ecp);
    }
    // dump initial callgraph
    logger.info("Initial number of methods in target callgraph: " + analyses.getTargetCallGraph().getMethodInfos().size());
    analyses.getTargetCallGraph().dumpCallgraph(jcopter.getJConfig().getConfig(), "greedy-target", config.getTargetCallgraphDumpType(), true);
    if (order == GreedyOrder.Global || (order == GreedyOrder.WCAFirst && !config.useWCA())) {
        optimizeMethods(analyses, ecp, selector, analyses.getTargetCallGraph().getMethodInfos());
    } else if (order == GreedyOrder.Targets) {
        for (MethodInfo target : config.getTargetMethods()) {
            optimizeMethods(analyses, ecp, selector, analyses.getTargetCallGraph().getReachableImplementationsSet(target));
        }
    } else if (order == GreedyOrder.WCAFirst) {
        logger.info("Optimizing WCA target");
        Set<MethodInfo> wcaMethods = analyses.getWCAMethods();
        optimizeMethods(analyses, ecp, selector, wcaMethods);
        selector.printStatistics();
        // We do not want to include the wca methods in the second pass because inlining there could have negative
        // effects on the WCET path due to the cache
        Set<MethodInfo> others = new LinkedHashSet<MethodInfo>(analyses.getTargetCallGraph().getMethodInfos());
        others.removeAll(wcaMethods);
        logger.info("Optimizing non-WCA code");
        //analyses.dumpTargetCallgraph("acet", true);
        selector = new ACETRebateSelector(analyses, new GainCalculator(analyses), config.getMaxCodesize());
        selector.initialize(config, false);
        ecp = analyses.getExecFrequencyAnalysis();
        if (config.useLocalExecCount()) {
            ecp = new LocalExecFrequencyProvider(ecp);
        }
        optimizeMethods(analyses, ecp, selector, others);
    } else if (order == GreedyOrder.TopDown || order == GreedyOrder.BottomUp) {
        if (config.useWCA() && !analyses.hasWCATargetsOnly()) {
            // TODO iterate over WCA and then non-wca graph or something in this case..
            throw new AppInfoError("Order " + order + " currently only works with WCA if the target method is the WCA target");
        }
        TopologicalOrderIterator<MethodNode, InvokeEdge> topOrder = new TopologicalOrderIterator<MethodNode, InvokeEdge>(analyses.getTargetCallGraph().getAcyclicMergedGraph(order == GreedyOrder.BottomUp));
        while (topOrder.hasNext()) {
            MethodNode node = topOrder.next();
            optimizeMethods(analyses, ecp, selector, Collections.singleton(node.getMethodInfo()));
        }
    } else {
        throw new AppInfoError("Order " + order + " not yet implemented.");
    }
    // dump final callgraph
    analyses.getTargetCallGraph().dumpCallgraph(jcopter.getJConfig().getConfig(), "greedy-target-opt", config.getTargetCallgraphDumpType(), true);
    selector.printStatistics();
    printStatistics();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LocalExecFrequencyProvider(com.jopdesign.jcopter.analysis.LocalExecFrequencyProvider) TopologicalOrderIterator(org.jgrapht.traverse.TopologicalOrderIterator) InvokeEdge(com.jopdesign.common.code.CallGraph.InvokeEdge) ExecFrequencyProvider(com.jopdesign.jcopter.analysis.ExecFrequencyProvider) LocalExecFrequencyProvider(com.jopdesign.jcopter.analysis.LocalExecFrequencyProvider) AppInfoError(com.jopdesign.common.misc.AppInfoError) MethodNode(com.jopdesign.common.code.CallGraph.MethodNode) MethodInfo(com.jopdesign.common.MethodInfo) AnalysisManager(com.jopdesign.jcopter.analysis.AnalysisManager) GreedyOrder(com.jopdesign.jcopter.greedy.GreedyConfig.GreedyOrder)

Aggregations

MethodInfo (com.jopdesign.common.MethodInfo)1 InvokeEdge (com.jopdesign.common.code.CallGraph.InvokeEdge)1 MethodNode (com.jopdesign.common.code.CallGraph.MethodNode)1 AppInfoError (com.jopdesign.common.misc.AppInfoError)1 AnalysisManager (com.jopdesign.jcopter.analysis.AnalysisManager)1 ExecFrequencyProvider (com.jopdesign.jcopter.analysis.ExecFrequencyProvider)1 LocalExecFrequencyProvider (com.jopdesign.jcopter.analysis.LocalExecFrequencyProvider)1 GreedyOrder (com.jopdesign.jcopter.greedy.GreedyConfig.GreedyOrder)1 LinkedHashSet (java.util.LinkedHashSet)1 TopologicalOrderIterator (org.jgrapht.traverse.TopologicalOrderIterator)1