Search in sources :

Example 11 with ControlFlowElement

use of org.eclipse.n4js.n4JS.ControlFlowElement in project n4js by eclipse.

the class DataFlowBranchWalker method visit.

@Override
protected void visit(Node node) {
    if (node.effectInfos.isEmpty()) {
        return;
    }
    ControlFlowElement cfe = node.getControlFlowElement();
    Multimap<Symbol, Object> assgns = getDataFlowVisitorHost().getAssignmentRelationFactory().findAssignments(cfe);
    Set<Symbol> handledDataFlowSymbols = new HashSet<>();
    for (Symbol lhs : assgns.keySet()) {
        Collection<Object> rhss = assgns.get(lhs);
        boolean handledDataFlow = handleDataflow(lhs, rhss);
        if (handledDataFlow) {
            handledDataFlowSymbols.add(lhs);
        }
    }
    for (EffectInfo effect : node.effectInfos) {
        handleVisitEffect(cfe, effect, handledDataFlowSymbols);
    }
}
Also used : Symbol(org.eclipse.n4js.flowgraphs.dataflow.symbols.Symbol) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) HashSet(java.util.HashSet)

Example 12 with ControlFlowElement

use of org.eclipse.n4js.n4JS.ControlFlowElement in project n4js by eclipse.

the class GraphVisitorAnalysis method backwardAnalysis.

/**
 * see {@link N4JSFlowAnalyser#accept(GraphVisitor...)}
 */
public void backwardAnalysis(FlowAnalyser[] flowAnalysers) {
    if (!forwardAnalysisDone) {
        throw new IllegalStateException("Forward analysis must be performed first.");
    }
    List<GraphVisitorInternal> graphVisitors = getGraphVisitors(flowAnalysers, TraverseDirection.Backward);
    GraphVisitorGuideInternal guide = new GraphVisitorGuideInternal(flowAnalyzer, graphVisitors);
    guide.init();
    for (ControlFlowElement container : cfg.getAllContainers()) {
        ComplexNode cnContainer = cfg.getComplexNode(container);
        guide.walkthroughBackward(cnContainer);
    }
    guide.terminate();
}
Also used : ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement)

Example 13 with ControlFlowElement

use of org.eclipse.n4js.n4JS.ControlFlowElement in project n4js by eclipse.

the class PathWalkerGuide method visitNode.

@Override
public void visitNode(Node node) {
    if (node instanceof RepresentingNode) {
        ControlFlowElement cfeEnd = node.getRepresentedControlFlowElement();
        if (lastVisitedNode != null) {
            ControlFlowElement cfeStart = lastVisitedNode.getRepresentedControlFlowElement();
            FlowEdge edge = new FlowEdge(cfeStart, cfeEnd, cfTypes);
            walker.visit(edge);
        }
        cfTypes.clear();
        walker.visit(cfeEnd);
        lastVisitedNode = node;
    }
}
Also used : FlowEdge(org.eclipse.n4js.flowgraphs.FlowEdge) ControlFlowEdge(org.eclipse.n4js.flowgraphs.model.ControlFlowEdge) RepresentingNode(org.eclipse.n4js.flowgraphs.model.RepresentingNode) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement)

Example 14 with ControlFlowElement

use of org.eclipse.n4js.n4JS.ControlFlowElement in project n4js by eclipse.

the class DeadCodeAnalyser method isDeadCode.

/**
 * This method deals with the fact that {@link Statement}s are not represented in the control flow graph.
 *
 * @return true iff the given {@link ControlFlowElement} is dead code.
 */
public boolean isDeadCode(ControlFlowElement cfe) {
    cfe = CFEMapper.map(cfe);
    if (FGUtils.isControlStatement(cfe)) {
        Set<ControlFlowElement> succs = flowAnalyzer.getSuccessors(cfe);
        for (ControlFlowElement succ : succs) {
            if (!isDeadCode(succ)) {
                return false;
            }
        }
        return true;
    }
    if (allLiveNodes.contains(cfe)) {
        return false;
    }
    Set<ControlFlowElement> preds = flowAnalyzer.getPredecessorsSkipInternal(cfe);
    if (preds.isEmpty()) {
        return true;
    }
    Set<ControlFlowElement> visited = new HashSet<>();
    while (!preds.isEmpty()) {
        ControlFlowElement pred = preds.iterator().next();
        preds.remove(pred);
        if (visited.contains(pred))
            continue;
        if (allLiveNodes.contains(pred)) {
            return false;
        }
        preds.addAll(flowAnalyzer.getPredecessorsSkipInternal(pred));
        visited.add(pred);
    }
    return true;
}
Also used : ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) HashSet(java.util.HashSet)

Example 15 with ControlFlowElement

use of org.eclipse.n4js.n4JS.ControlFlowElement in project n4js by eclipse.

the class DeadCodeAnalyser method separateOnTheirBlocks.

/**
 * Separates the given set into sets where all {@link ControlFlowElement}s of each set have the same containing
 * {@link Block}.
 * <p>
 * Note that the assumption is:<br/>
 * <i>No block can contain more than one single dead code region.</i>
 */
private Collection<Set<ControlFlowElement>> separateOnTheirBlocks(Set<ControlFlowElement> unreachableElems) {
    Map<EObject, Set<ControlFlowElement>> unreachablesMap = new HashMap<>();
    for (ControlFlowElement unreachableElem : unreachableElems) {
        HashSet<ControlFlowElement> moreUnreachableElems = new HashSet<>();
        EObject cfeBlock = getReachableContainer(unreachableElems, unreachableElem, moreUnreachableElems);
        if (cfeBlock == null)
            continue;
        if (!unreachablesMap.containsKey(cfeBlock)) {
            unreachablesMap.put(cfeBlock, new HashSet<>());
        }
        Set<ControlFlowElement> unreachableInBlock = unreachablesMap.get(cfeBlock);
        unreachableInBlock.add(unreachableElem);
        unreachableInBlock.addAll(moreUnreachableElems);
    }
    return unreachablesMap.values();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) EObject(org.eclipse.emf.ecore.EObject) ControlFlowElement(org.eclipse.n4js.n4JS.ControlFlowElement) HashSet(java.util.HashSet)

Aggregations

ControlFlowElement (org.eclipse.n4js.n4JS.ControlFlowElement)52 EObject (org.eclipse.emf.ecore.EObject)12 ComplexNode (org.eclipse.n4js.flowgraphs.model.ComplexNode)12 LinkedList (java.util.LinkedList)10 RepresentingNode (org.eclipse.n4js.flowgraphs.model.RepresentingNode)10 ParameterParser (org.eclipse.xpect.parameter.ParameterParser)9 Xpect (org.eclipse.xpect.runner.Xpect)9 Node (org.eclipse.n4js.flowgraphs.model.Node)8 HashSet (java.util.HashSet)7 ControlFlowType (org.eclipse.n4js.flowgraphs.ControlFlowType)5 ControlFlowEdge (org.eclipse.n4js.flowgraphs.model.ControlFlowEdge)4 HashMap (java.util.HashMap)3 FlowEdge (org.eclipse.n4js.flowgraphs.FlowEdge)3 FinallyBlock (org.eclipse.n4js.n4JS.FinallyBlock)3 LinkedHashSet (java.util.LinkedHashSet)2 N4JSFlowAnalyser (org.eclipse.n4js.flowgraphs.N4JSFlowAnalyser)2 Assumption (org.eclipse.n4js.flowgraphs.dataflow.Assumption)2 Symbol (org.eclipse.n4js.flowgraphs.dataflow.symbols.Symbol)2 Block (org.eclipse.n4js.n4JS.Block)2 ExpressionStatement (org.eclipse.n4js.n4JS.ExpressionStatement)2