Search in sources :

Example 11 with MethodInfo

use of com.jopdesign.common.MethodInfo in project jop by jop-devel.

the class LoopBounds method doInvoke.

private void doInvoke(String methodName, InstructionHandle stmt, Context context, Map<CallString, Map<Location, ValueMapping>> input, Interpreter<CallString, Map<Location, ValueMapping>> interpreter, Map<InstructionHandle, ContextMap<CallString, Map<Location, ValueMapping>>> state, Map<CallString, Map<Location, ValueMapping>> result) {
    DFATool p = interpreter.getDFATool();
    MethodInfo method = p.getMethod(methodName);
    if (method.isNative()) {
        handleNative(method, context, input, result);
    } else {
        // set up new context
        int varPtr = context.stackPtr - MethodHelper.getArgSize(method);
        Context c = new Context(context);
        c.stackPtr = method.getCode().getMaxLocals();
        if (method.isSynchronized()) {
            c.syncLevel = context.syncLevel + 1;
        }
        c.setMethodInfo(method);
        c.callString = c.callString.push(method, stmt, callStringLength);
        // carry only minimal information with call
        Map<Location, ValueMapping> in = input.get(context.callString);
        Map<Location, ValueMapping> out = new LinkedHashMap<Location, ValueMapping>();
        for (Location l : in.keySet()) {
            if (l.stackLoc < 0) {
                out.put(l, in.get(l));
            }
            if (l.stackLoc >= varPtr) {
                out.put(new Location(l.stackLoc - varPtr), new ValueMapping(in.get(l), false));
            }
        }
        ContextMap<CallString, Map<Location, ValueMapping>> tmpresult = new ContextMap<CallString, Map<Location, ValueMapping>>(c, new LinkedHashMap<CallString, Map<Location, ValueMapping>>());
        tmpresult.put(c.callString, out);
        InstructionHandle entry = p.getEntryHandle(method);
        state.put(entry, join(state.get(entry), tmpresult));
        // interpret method
        Map<InstructionHandle, ContextMap<CallString, Map<Location, ValueMapping>>> r = interpreter.interpret(c, entry, state, false);
        //System.out.println(">>>>>>>>");
        // pull out relevant information from call
        InstructionHandle exit = p.getExitHandle(method);
        if (r.get(exit) != null) {
            Map<Location, ValueMapping> returned = r.get(exit).get(c.callString);
            if (returned != null) {
                for (Location l : returned.keySet()) {
                    if (l.stackLoc < 0) {
                        ValueMapping m = new ValueMapping(returned.get(l), true);
                        m.join(result.get(context.callString).get(l));
                        result.get(context.callString).put(l, m);
                    }
                    if (l.stackLoc >= 0) {
                        ValueMapping m = new ValueMapping(returned.get(l), false);
                        Location loc = new Location(l.stackLoc + varPtr);
                        m.join(result.get(context.callString).get(loc));
                        result.get(context.callString).put(loc, m);
                    }
                }
            }
        }
        // add relevant information to result
        for (Location l : in.keySet()) {
            if (l.stackLoc >= 0 && l.stackLoc < context.stackPtr - MethodHelper.getArgSize(method)) {
                result.get(context.callString).put(l, new ValueMapping(in.get(l), true));
            }
        }
    }
}
Also used : Context(com.jopdesign.dfa.framework.Context) DFATool(com.jopdesign.dfa.DFATool) ContextMap(com.jopdesign.dfa.framework.ContextMap) InstructionHandle(org.apache.bcel.generic.InstructionHandle) LinkedHashMap(java.util.LinkedHashMap) CallString(com.jopdesign.common.code.CallString) MethodInfo(com.jopdesign.common.MethodInfo) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ContextMap(com.jopdesign.dfa.framework.ContextMap)

Example 12 with MethodInfo

use of com.jopdesign.common.MethodInfo 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)

Example 13 with MethodInfo

use of com.jopdesign.common.MethodInfo in project jop by jop-devel.

the class RebateSelector method initialize.

@Override
public void initialize(GreedyConfig config, boolean dumpStats) {
    // calculate current global codesize
    globalCodesize = 0;
    if (usesCodeRemover) {
        for (MethodInfo method : AppInfo.getSingleton().getCallGraph().getMethodInfos()) {
            if (!method.hasCode())
                continue;
            globalCodesize += method.getCode().getNumberOfBytes();
        }
    } else {
        for (ClassInfo cls : AppInfo.getSingleton().getClassInfos()) {
            for (MethodInfo method : cls.getMethods()) {
                if (!method.hasCode())
                    continue;
                globalCodesize += method.getCode().getNumberOfBytes();
            }
        }
    }
    // we need a tie-breaker for the candidate selection, and to make the results deterministic
    // so we use a topological order of the (initial) callgraph
    DFSVisitor<ExecutionContext, ContextEdge> visitor = new EmptyDFSVisitor<ExecutionContext, ContextEdge>() {

        private int counter = 1;

        @Override
        public void postorder(ExecutionContext node) {
            depthMap.put(node.getMethodInfo(), counter++);
        }
    };
    DirectedGraph<ExecutionContext, ContextEdge> graph = analyses.getTargetCallGraph().getReversedGraph();
    DFSTraverser<ExecutionContext, ContextEdge> traverser = new DFSTraverser<ExecutionContext, ContextEdge>(visitor);
    traverser.traverse(graph);
    logger.info("Initial codesize: " + globalCodesize + " bytes");
    if (config.doDumpStats() && dumpStats) {
        try {
            File statsFile = config.getStatsFile();
            dump = new PrintWriter(statsFile);
            dump.print("total codesize, ");
            if (analyses.useWCAInvoker())
                dump.print("WCET, ");
            dump.println("candidate, ratio, gain, local gain, cache, local cache, delta codesize, frequency");
        } catch (FileNotFoundException e) {
            throw new AppInfoError("Could not initialize dump file", e);
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) EmptyDFSVisitor(com.jopdesign.common.graphutils.DFSTraverser.EmptyDFSVisitor) ContextEdge(com.jopdesign.common.code.CallGraph.ContextEdge) AppInfoError(com.jopdesign.common.misc.AppInfoError) DFSTraverser(com.jopdesign.common.graphutils.DFSTraverser) ExecutionContext(com.jopdesign.common.code.ExecutionContext) MethodInfo(com.jopdesign.common.MethodInfo) File(java.io.File) ClassInfo(com.jopdesign.common.ClassInfo) PrintWriter(java.io.PrintWriter)

Example 14 with MethodInfo

use of com.jopdesign.common.MethodInfo in project jop by jop-devel.

the class RebateSelector method onSuccessfulOptimize.

@Override
public void onSuccessfulOptimize(Candidate optimized, List<Candidate> newCandidates) {
    globalCodesize += getDeltaGlobalCodesize(optimized);
    // We can remove candidates from methods which are no longer reachable here already
    // This does not find everything, candidates which are only unreachable in the target graph are removed later
    Collection<MethodInfo> unreachable = optimized.getUnreachableMethods();
    if (unreachable != null && !unreachable.isEmpty()) {
        for (MethodInfo m : unreachable) {
            // codesize of removed candidates already handled above
            removeCandidates(m);
        }
    }
    // replace old candidates with new ones in range
    removeCandidate(optimized);
    removeCandidates(optimized.getMethod(), optimized.getStart(), optimized.getEnd());
    addCandidates(optimized.getMethod(), newCandidates);
}
Also used : MethodInfo(com.jopdesign.common.MethodInfo)

Example 15 with MethodInfo

use of com.jopdesign.common.MethodInfo in project jop by jop-devel.

the class WCEPRebateSelector method selectNextCandidates.

@Override
public Collection<Candidate> selectNextCandidates(ExecFrequencyProvider ecp) {
    WCAInvoker wcaInvoker = analyses.getWCAInvoker();
    Set<MethodInfo> visited = new LinkedHashSet<MethodInfo>();
    LinkedList<MethodInfo> queue = new LinkedList<MethodInfo>();
    // go down all methods in the callgraph which are on the WCET path, find best candidate
    RebateRatio next = null;
    queue.addAll(wcaInvoker.getWcaTargets());
    visited.addAll(queue);
    while (!queue.isEmpty()) {
        MethodInfo method = queue.removeFirst();
        MethodData data = methodData.get(method);
        if (data == null)
            continue;
        // skip methods not on the WCET path
        if (!wcaInvoker.isOnWCETPath(method)) {
            continue;
        }
        // select best candidate from method or use previous method
        next = selectCandidate(ecp, data, next);
        // add childs to queue if not already visited
        for (MethodInfo child : analyses.getTargetCallGraph().getInvokedMethods(method)) {
            if (visited.add(child)) {
                queue.add(child);
            }
        }
    }
    logSelection(ecp, next);
    return next != null ? next.getCandidates() : null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MethodInfo(com.jopdesign.common.MethodInfo) WCAInvoker(com.jopdesign.jcopter.analysis.WCAInvoker) LinkedList(java.util.LinkedList)

Aggregations

MethodInfo (com.jopdesign.common.MethodInfo)108 LinkedHashSet (java.util.LinkedHashSet)21 InstructionHandle (org.apache.bcel.generic.InstructionHandle)20 ClassInfo (com.jopdesign.common.ClassInfo)19 ExecutionContext (com.jopdesign.common.code.ExecutionContext)16 CFGNode (com.jopdesign.common.code.ControlFlowGraph.CFGNode)13 ArrayList (java.util.ArrayList)13 CallString (com.jopdesign.common.code.CallString)12 ControlFlowGraph (com.jopdesign.common.code.ControlFlowGraph)12 HashMap (java.util.HashMap)10 Set (java.util.Set)10 LinkedHashMap (java.util.LinkedHashMap)9 Instruction (org.apache.bcel.generic.Instruction)9 FieldInfo (com.jopdesign.common.FieldInfo)8 MethodCode (com.jopdesign.common.MethodCode)8 AppInfo (com.jopdesign.common.AppInfo)7 ContextEdge (com.jopdesign.common.code.CallGraph.ContextEdge)7 InvokeSite (com.jopdesign.common.code.InvokeSite)7 MemberID (com.jopdesign.common.type.MemberID)7 Context (com.jopdesign.dfa.framework.Context)7