Search in sources :

Example 1 with InvokeNode

use of com.jopdesign.common.code.ControlFlowGraph.InvokeNode in project jop by jop-devel.

the class SuperGraph method outgoingEdgesOf.

/**
 * The outgoing edges are generated as follows:
 * <ul>
 * <li/>invoke node: superedge invoking the callee
 * <li/>exit node: superedge returning to the caller
 * <li/>other: intraprocedural CFG edge
 * </ul>
 * @param node
 * @return
 */
public Iterable<SuperGraphEdge> outgoingEdgesOf(SuperGraphNode node) {
    CFGNode cfgNode = node.getCFGNode();
    if (cfgNode instanceof InvokeNode) {
        /* invoke node: outgoing SuperInvoke edges */
        final InvokeNode invNode = (InvokeNode) cfgNode;
        Set<SuperEdge> outgoingInvokeEdges = superGraph.outgoingEdgesOf(node.getContextCFG());
        return new Filter<SuperGraphEdge>() {

            @Override
            protected boolean include(SuperGraphEdge e) {
                if (!(e instanceof SuperInvokeEdge))
                    return false;
                SuperInvokeEdge invoke = (SuperInvokeEdge) e;
                return invoke.getInvokeNode().equals(invNode);
            }
        }.<SuperGraphEdge>filter(outgoingInvokeEdges);
    } else if (cfgNode instanceof VirtualNode && ((VirtualNode) cfgNode).getKind() == VirtualNodeKind.EXIT) {
        /* exit node: outgoing SuperReturn edges */
        Set<SuperEdge> outgoingReturnEdges = superGraph.outgoingEdgesOf(node.getContextCFG());
        return new Filter<SuperGraphEdge>() {

            @Override
            protected boolean include(SuperGraphEdge e) {
                return (e instanceof SuperReturnEdge);
            }
        }.<SuperGraphEdge>filter(outgoingReturnEdges);
    } else {
        /* standard edges: outgoing edges of cfg node */
        Set<CFGEdge> outgoingCFGEdges = node.getContextCFG().getCfg().outgoingEdgesOf(cfgNode);
        return liftCFGEdges(node.getContextCFG(), outgoingCFGEdges);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) CFGNode(com.jopdesign.common.code.ControlFlowGraph.CFGNode) Filter(com.jopdesign.common.misc.Filter) VirtualNode(com.jopdesign.common.code.ControlFlowGraph.VirtualNode) InvokeNode(com.jopdesign.common.code.ControlFlowGraph.InvokeNode)

Example 2 with InvokeNode

use of com.jopdesign.common.code.ControlFlowGraph.InvokeNode in project jop by jop-devel.

the class WCAInvoker method updateWCEP.

private void updateWCEP() {
    if (!provideWCAExecCount)
        return;
    execCounts.clear();
    for (MethodInfo root : getWcaTargets()) {
        execCounts.put(root, 1L);
    }
    NodeVisitor<ExecutionContext> visitor = new NodeVisitor<ExecutionContext>() {

        @Override
        public boolean visitNode(ExecutionContext context) {
            MethodInfo method = context.getMethodInfo();
            MethodCode code = method.getCode();
            long ec = getExecCount(method);
            // skip methods which are not on the WCET path.. we can ship iterating over the childs too..
            if (ec == 0)
                return false;
            // iterate over all blocks in the CFG, find all invokes and add block execution counts to invokees
            ControlFlowGraph cfg = method.getCode().getControlFlowGraph(false);
            for (CFGNode node : cfg.getGraph().vertexSet()) {
                if (node instanceof InvokeNode) {
                    InvokeNode inv = (InvokeNode) node;
                    long ef = getExecFrequency(method, node);
                    for (MethodInfo invokee : inv.getImplementingMethods()) {
                        addExecCount(invokee, ec * ef);
                    }
                } else if (node instanceof BasicBlockNode) {
                    // check if we have a JVM invoke here (or an invoke not in a dedicated node..)
                    for (InstructionHandle ih : node.getBasicBlock().getInstructions()) {
                        if (!code.isInvokeSite(ih))
                            continue;
                        long ef = getExecFrequency(method, node);
                        for (MethodInfo invokee : method.getAppInfo().findImplementations(code.getInvokeSite(ih))) {
                            addExecCount(invokee, ec * ef);
                        }
                    }
                }
            }
            return true;
        }
    };
    TopologicalTraverser<ExecutionContext, ContextEdge> topOrder = new TopologicalTraverser<ExecutionContext, ContextEdge>(wcetTool.getCallGraph().getGraph(), visitor);
    topOrder.traverse();
}
Also used : CFGNode(com.jopdesign.common.code.ControlFlowGraph.CFGNode) BasicBlockNode(com.jopdesign.common.code.ControlFlowGraph.BasicBlockNode) ContextEdge(com.jopdesign.common.code.CallGraph.ContextEdge) InstructionHandle(org.apache.bcel.generic.InstructionHandle) NodeVisitor(com.jopdesign.common.graphutils.NodeVisitor) ExecutionContext(com.jopdesign.common.code.ExecutionContext) ControlFlowGraph(com.jopdesign.common.code.ControlFlowGraph) InvokeNode(com.jopdesign.common.code.ControlFlowGraph.InvokeNode) TopologicalTraverser(com.jopdesign.common.graphutils.TopologicalTraverser) MethodInfo(com.jopdesign.common.MethodInfo) MethodCode(com.jopdesign.common.MethodCode)

Example 3 with InvokeNode

use of com.jopdesign.common.code.ControlFlowGraph.InvokeNode in project jop by jop-devel.

the class SegmentTest method main.

public static void main(String[] args) {
    TestFramework testFramework = new TestFramework();
    AppSetup setup = testFramework.setupAppSetup("java/tools/test/test/cg1.zip", null);
    AppInfo appInfo = testFramework.setupAppInfo("wcet.devel.CallGraph1.run", true);
    SegmentTest testInst = new SegmentTest();
    testInst.appInfo = appInfo;
    MethodInfo mainMethod = appInfo.getMainMethod();
    /* count total number of CFG nodes */
    SuperGraph superGraph = new SuperGraph(testInst, testInst.getFlowGraph(mainMethod), 2);
    Segment segment = Segment.methodSegment(mainMethod, CallString.EMPTY, testInst, 2, superGraph.getInfeasibleEdgeProvider());
    int count = 0;
    for (ContextCFG cgNode : superGraph.getCallGraphNodes()) {
        try {
            cgNode.getCfg().exportDOT(new File("/tmp/cfg-" + cgNode.getCfg().getMethodInfo().getClassName() + "_" + cgNode.getCfg().getMethodInfo().getShortName() + ".dot"));
        } catch (IOException e) {
        }
        count += cgNode.getCfg().vertexSet().size();
    }
    checkEquals("[Segment 1] Expected node count", (count - 2), Iterators.size(segment.getNodes()));
    try {
        segment.exportDOT(new File("/tmp/cg1-segment.dot"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    /* root */
    ContextCFG root = superGraph.getRootNode();
    /* Build a segment cuts all invokes in those methods invoked by run() */
    Segment segment2;
    /* root entries */
    Set<SuperGraphEdge> entries = new HashSet<SuperGraphEdge>();
    Iterators.addAll(entries, superGraph.liftCFGEdges(root, root.getCfg().outgoingEdgesOf(root.getCfg().getEntry())));
    Set<SuperGraphEdge> exits = new HashSet<SuperGraphEdge>();
    int cfgNodeCandidateCount = root.getCfg().vertexSet().size();
    /* find callees */
    for (SuperEdge superEdge : superGraph.getCallGraph().outgoingEdgesOf(root)) {
        if (!(superEdge instanceof SuperInvokeEdge))
            continue;
        ContextCFG callee1 = superGraph.getCallGraph().getEdgeTarget(superEdge);
        cfgNodeCandidateCount += callee1.getCfg().vertexSet().size();
        /* find all edges from invoke nodes */
        for (CFGNode cfgNode : callee1.getCfg().vertexSet()) {
            if (cfgNode instanceof InvokeNode) {
                Iterators.addAll(exits, superGraph.outgoingEdgesOf(new SuperGraphNode(callee1, cfgNode)));
            }
        }
    }
    segment2 = new Segment(superGraph, entries, exits);
    exits = segment2.getExitEdges();
    /* reachable exits */
    try {
        segment2.exportDOT(new File("/tmp/cg1-segment2.dot"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    checkEquals("[Segment 2] Expected node count", 14, Iterators.size(segment2.getNodes()) + 2);
    checkLessEqual("[Segment 2] Expected node count <= |root + directly invoked|", Iterators.size(segment2.getNodes()) + 2, cfgNodeCandidateCount);
    /* Another segment, with entries the exits of the last segment, and exits all invokes in methods the entries */
    Segment segment3;
    entries = segment2.getExitEdges();
    exits = new HashSet<SuperGraphEdge>();
    cfgNodeCandidateCount = 0;
    for (SuperGraphEdge superEdge : entries) {
        SuperGraphNode node1 = superEdge.getTarget();
        for (SuperEdge superEdge2 : superGraph.getCallGraph().outgoingEdgesOf(node1.getContextCFG())) {
            if (!(superEdge2 instanceof SuperInvokeEdge))
                continue;
            ContextCFG callee2 = superGraph.getCallGraph().getEdgeTarget(superEdge2);
            /* find all edges from invoke nodes */
            for (CFGNode cfgNode : callee2.getCfg().vertexSet()) {
                if (cfgNode instanceof InvokeNode) {
                    Iterators.addAll(exits, superGraph.outgoingEdgesOf(new SuperGraphNode(callee2, cfgNode)));
                }
            }
        }
    }
    segment3 = new Segment(superGraph, entries, exits);
    try {
        segment3.exportDOT(new File("/tmp/cg1-segment3.dot"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    checkEquals("[Segment 2] 3 exits", 3, segment2.getExitEdges().size());
    checkEquals("[Segment 3] 3 entries", 3, segment3.getEntryEdges().size());
    checkEquals("[Segment 3] 4 exits", 4, segment3.getExitEdges().size());
}
Also used : CFGNode(com.jopdesign.common.code.ControlFlowGraph.CFGNode) IOException(java.io.IOException) SuperEdge(com.jopdesign.common.code.SuperGraph.SuperEdge) AppInfo(com.jopdesign.common.AppInfo) SuperInvokeEdge(com.jopdesign.common.code.SuperGraph.SuperInvokeEdge) TestFramework(com.jopdesign.common.TestFramework) ContextCFG(com.jopdesign.common.code.SuperGraph.ContextCFG) SuperGraphEdge(com.jopdesign.common.code.SuperGraph.SuperGraphEdge) InvokeNode(com.jopdesign.common.code.ControlFlowGraph.InvokeNode) SuperGraphNode(com.jopdesign.common.code.SuperGraph.SuperGraphNode) AppSetup(com.jopdesign.common.AppSetup) MethodInfo(com.jopdesign.common.MethodInfo) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

CFGNode (com.jopdesign.common.code.ControlFlowGraph.CFGNode)3 InvokeNode (com.jopdesign.common.code.ControlFlowGraph.InvokeNode)3 MethodInfo (com.jopdesign.common.MethodInfo)2 AppInfo (com.jopdesign.common.AppInfo)1 AppSetup (com.jopdesign.common.AppSetup)1 MethodCode (com.jopdesign.common.MethodCode)1 TestFramework (com.jopdesign.common.TestFramework)1 ContextEdge (com.jopdesign.common.code.CallGraph.ContextEdge)1 ControlFlowGraph (com.jopdesign.common.code.ControlFlowGraph)1 BasicBlockNode (com.jopdesign.common.code.ControlFlowGraph.BasicBlockNode)1 VirtualNode (com.jopdesign.common.code.ControlFlowGraph.VirtualNode)1 ExecutionContext (com.jopdesign.common.code.ExecutionContext)1 ContextCFG (com.jopdesign.common.code.SuperGraph.ContextCFG)1 SuperEdge (com.jopdesign.common.code.SuperGraph.SuperEdge)1 SuperGraphEdge (com.jopdesign.common.code.SuperGraph.SuperGraphEdge)1 SuperGraphNode (com.jopdesign.common.code.SuperGraph.SuperGraphNode)1 SuperInvokeEdge (com.jopdesign.common.code.SuperGraph.SuperInvokeEdge)1 NodeVisitor (com.jopdesign.common.graphutils.NodeVisitor)1 TopologicalTraverser (com.jopdesign.common.graphutils.TopologicalTraverser)1 Filter (com.jopdesign.common.misc.Filter)1