Package org.teavm.common

Examples of org.teavm.common.Graph


        BasicBlock block;
    }

    private void renameVariables() {
        DominatorTree domTree = GraphUtils.buildDominatorTree(ProgramUtils.buildControlFlowGraph(program));
        Graph domGraph = GraphUtils.buildDominatorGraph(domTree, program.basicBlockCount());
        Task[] stack = new Task[cfg.size() * 2];
        int head = 0;
        for (int i = 0; i < program.basicBlockCount(); ++i) {
            if (domGraph.incomingEdgesCount(i) == 0) {
                Task task = new Task();
                task.block = program.basicBlockAt(i);
                task.variables = Arrays.copyOf(variableMap, variableMap.length);
                stack[head++] = task;
            }
        }

        List<List<TryCatchBlock>> caughtBlocks = new ArrayList<>();
        List<List<Phi>> specialPhis = new ArrayList<>();
        for (int i = 0; i < program.basicBlockCount(); ++i) {
            caughtBlocks.add(new ArrayList<TryCatchBlock>());
            specialPhis.add(new ArrayList<Phi>());
        }
        for (int i = 0; i < program.basicBlockCount(); ++i) {
            for (TryCatchBlock tryCatch : program.basicBlockAt(i).getTryCatchBlocks()) {
                caughtBlocks.get(tryCatch.getHandler().getIndex()).add(tryCatch);
            }
        }
        boolean[] processed = new boolean[program.basicBlockCount()];
        while (head > 0) {
            Task task = stack[--head];
            currentBlock = task.block;
            if (processed[currentBlock.getIndex()]) {
                continue;
            }
            processed[currentBlock.getIndex()] = true;
            variableMap = Arrays.copyOf(task.variables, task.variables.length);
            for (Phi phi : currentBlock.getPhis()) {
                Variable var = program.createVariable();
                var.getDebugNames().addAll(phi.getReceiver().getDebugNames());
                variableMap[phi.getReceiver().getIndex()] = var;
                phi.setReceiver(var);
            }
            if (!caughtBlocks.get(currentBlock.getIndex()).isEmpty()) {
                Phi phi = new Phi();
                phi.setReceiver(program.createVariable());
                for (TryCatchBlock tryCatch : caughtBlocks.get(currentBlock.getIndex())) {
                    variableMap[tryCatch.getExceptionVariable().getIndex()] = phi.getReceiver();
                    Set<String> debugNames = tryCatch.getExceptionVariable().getDebugNames();
                    tryCatch.setExceptionVariable(program.createVariable());
                    tryCatch.getExceptionVariable().getDebugNames().addAll(debugNames);
                    Incoming incoming = new Incoming();
                    incoming.setSource(tryCatch.getProtectedBlock());
                    incoming.setValue(tryCatch.getExceptionVariable());
                    phi.getIncomings().add(incoming);
                }
                specialPhis.get(currentBlock.getIndex()).add(phi);
            }
            for (Instruction insn : currentBlock.getInstructions()) {
                variableDebugMap.putAll(variableDebugInfo.getDebugNames(insn));
                insn.acceptVisitor(consumer);
            }
            int[] successors = domGraph.outgoingEdges(currentBlock.getIndex());
            for (int i = 0; i < successors.length; ++i) {
                Task next = new Task();
                next.variables = Arrays.copyOf(variableMap, variableMap.length);
                next.block = program.basicBlockAt(successors[i]);
                stack[head++] = next;
View Full Code Here


    public BitSet liveIn(int block) {
        return (BitSet)liveVars[block].clone();
    }

    public void analyze(Program program) {
        Graph cfg = ProgramUtils.buildControlFlowGraph(program);
        computeDomLeftRight(GraphUtils.buildDominatorGraph(GraphUtils.buildDominatorTree(cfg), cfg.size()));
        liveVars = new BitSet[cfg.size()];
        for (int i = 0; i < liveVars.length; ++i) {
            liveVars[i] = new BitSet(program.basicBlockCount());
        }

        UsageExtractor usageExtractor = new UsageExtractor();
        DefinitionExtractor defExtractor = new DefinitionExtractor();
        Deque<Task> stack = new ArrayDeque<>();
        int[] definitions = new int[program.variableCount()];
        for (int i = 0; i < program.basicBlockCount(); ++i) {
            BasicBlock block = program.basicBlockAt(i);
            for (Instruction insn : block.getInstructions()) {
                insn.acceptVisitor(usageExtractor);
                for (Variable var : usageExtractor.getUsedVariables()) {
                    Task task = new Task();
                    task.block = i;
                    task.var = var.getIndex();
                    stack.push(task);
                }
                insn.acceptVisitor(defExtractor);
                for (Variable var : defExtractor.getDefinedVariables()) {
                    definitions[var.getIndex()] = i;
                }
            }
            for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
                if (tryCatch.getExceptionVariable() != null) {
                    definitions[tryCatch.getExceptionVariable().getIndex()] = i;
                }
            }
            for (Phi phi : block.getPhis()) {
                definitions[phi.getReceiver().getIndex()] = i;
                for (Incoming incoming : phi.getIncomings()) {
                    Task task = new Task();
                    task.block = incoming.getSource().getIndex();
                    task.var = incoming.getValue().getIndex();
                    stack.push(task);
                }
            }
        }

        while (!stack.isEmpty()) {
            Task task = stack.pop();
            if (liveVars[task.block].get(task.var) || !dominates(definitions[task.var], task.block)) {
                continue;
            }
            liveVars[task.block].set(task.var, true);
            for (int pred : cfg.incomingEdges(task.block)) {
                Task nextTask = new Task();
                nextTask.block = pred;
                nextTask.var = task.var;
                stack.push(nextTask);
            }
View Full Code Here

    private List<Set<InstructionLocation>> startLocations;
    private List<AdditionalConnection> additionalConnections;

    public Map<InstructionLocation, InstructionLocation[]> build(Program program) {
        graphBuilder = new HashMap<>();
        Graph graph = ProgramUtils.buildControlFlowGraph(program);
        dfs(graph, program);
        return assemble();
    }
View Full Code Here

    @Override
    public void optimize(MethodReader method, Program program) {
        this.program = program;
        knownValues.clear();
        Graph cfg = ProgramUtils.buildControlFlowGraph(program);
        domTree = GraphUtils.buildDominatorTree(cfg);
        Graph dom = GraphUtils.buildDominatorGraph(domTree, cfg.size());
        map = new int[program.variableCount()];
        for (int i = 0; i < map.length; ++i) {
            map[i] = i;
        }
        List<List<Incoming>> outgoings = findOutgoings(program);

        int[] stack = new int[cfg.size() * 2];
        int top = 0;
        for (int i = 0; i < cfg.size(); ++i) {
            if (cfg.incomingEdgesCount(i) == 0) {
                stack[top++] = i;
            }
        }
        while (top > 0) {
            int v = stack[--top];
            currentBlockIndex = v;
            BasicBlock block = program.basicBlockAt(v);
            /*for (int i = 0; i < block.getPhis().size(); ++i) {
                Phi phi = block.getPhis().get(i);
                int sharedValue = -2;
                for (Incoming incoming : phi.getIncomings()) {
                    int value = map[incoming.getValue().getIndex()];
                    incoming.setValue(program.variableAt(value));
                    if (sharedValue != -2 && sharedValue != incoming.getValue().getIndex()) {
                        sharedValue = -1;
                    } else {
                        sharedValue = incoming.getValue().getIndex();
                    }
                }
                if (sharedValue != -1) {
                    if (sharedValue != -2) {
                        AssignInstruction assignInsn = new AssignInstruction();
                        assignInsn.setReceiver(phi.getReceiver());
                        assignInsn.setAssignee(program.variableAt(sharedValue));
                        block.getInstructions().add(0, assignInsn);
                    }
                    block.getPhis().remove(i--);
                }
            }*/
            for (int i = 0; i < block.getInstructions().size(); ++i) {
                Instruction currentInsn = block.getInstructions().get(i);
                currentInsn.acceptVisitor(optimizer);
                if (eliminate) {
                    block.getInstructions().set(i, new EmptyInstruction());
                    eliminate = false;
                }
            }
            for (Incoming incoming : outgoings.get(v)) {
                int value = map[incoming.getValue().getIndex()];
                incoming.setValue(program.variableAt(value));
            }
            for (TryCatchBlock tryCatch : block.getTryCatchBlocks()) {
                int var = map[tryCatch.getExceptionVariable().getIndex()];
                tryCatch.setExceptionVariable(program.variableAt(var));
            }
            for (int succ : dom.outgoingEdges(v)) {
                stack[top++] = succ;
            }
        }

        for (int i = 0; i < map.length; ++i) {
View Full Code Here

    @Override
    public void optimize(MethodReader method, Program program) {
        if (method.getProgram() == null) {
            return;
        }
        Graph graph = VariableUsageGraphBuilder.build(program);
        boolean[] escaping = VariableEscapeAnalyzer.findEscapingVariables(program);
        boolean[] used = new boolean[escaping.length];

        int[] stack = new int[graph.size() * 2];
        int top = 0;
        for (int i = 0; i < used.length; ++i) {
            if (escaping[i]) {
                stack[top++] = i;
            }
        }

        while (top > 0) {
            int var = stack[--top];
            if (used[var]) {
                continue;
            }
            used[var] = true;
            for (int arg : graph.incomingEdges(var)) {
                if (!used[arg]) {
                    stack[top++] = arg;
                }
            }
        }
View Full Code Here

TOP

Related Classes of org.teavm.common.Graph

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.