Search in sources :

Example 56 with BasicBlock

use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.

the class ControlFlowGraphDumper method linearize.

private void linearize() {
    if (cfg.getEntries().size() != 1)
        throw new IllegalStateException("CFG doesn't have exactly 1 entry");
    BasicBlock entry = cfg.getEntries().iterator().next();
    // Build bundle graph
    Map<BasicBlock, BlockBundle> bundles = new HashMap<>();
    Map<BlockBundle, List<BlockBundle>> bunches = new HashMap<>();
    // Build bundles
    List<BasicBlock> topoorder = new SimpleDfs<>(cfg, entry, SimpleDfs.TOPO).getTopoOrder();
    for (BasicBlock b : topoorder) {
        if (// Already in a bundle
        bundles.containsKey(b))
            continue;
        if (// Look for heads of bundles only
        b.getIncomingImmediateEdge() != null)
            continue;
        BlockBundle bundle = new BlockBundle();
        while (b != null) {
            bundle.add(b);
            bundles.put(b, bundle);
            b = b.getImmediate();
        }
        List<BlockBundle> bunch = new ArrayList<>();
        bunch.add(bundle);
        bunches.put(bundle, bunch);
    }
    // Group bundles by exception ranges
    for (ExceptionRange<BasicBlock> range : cfg.getRanges()) {
        BlockBundle prevBundle = null;
        for (BasicBlock b : range.getNodes()) {
            BlockBundle curBundle = bundles.get(b);
            if (prevBundle == null) {
                prevBundle = curBundle;
                continue;
            }
            if (curBundle != prevBundle) {
                List<BlockBundle> bunchA = bunches.get(prevBundle);
                List<BlockBundle> bunchB = bunches.get(curBundle);
                if (bunchA != bunchB) {
                    bunchA.addAll(bunchB);
                    for (BlockBundle bundle : bunchB) {
                        bunches.put(bundle, bunchA);
                    }
                }
                prevBundle = curBundle;
            }
        }
    }
    // Rebuild bundles
    bundles.clear();
    for (Map.Entry<BlockBundle, List<BlockBundle>> e : bunches.entrySet()) {
        BlockBundle bundle = e.getKey();
        if (bundles.containsKey(bundle.getFirst()))
            continue;
        BlockBundle bunch = new BlockBundle();
        e.getValue().forEach(bunch::addAll);
        for (BasicBlock b : bunch) bundles.put(b, bunch);
    }
    // Connect bundle graph
    BundleGraph bundleGraph = new BundleGraph();
    BlockBundle entryBundle = bundles.get(entry);
    bundleGraph.addVertex(entryBundle);
    for (BasicBlock b : topoorder) {
        for (FlowEdge<BasicBlock> e : cfg.getEdges(b)) {
            if (e instanceof ImmediateEdge)
                continue;
            BlockBundle src = bundles.get(b);
            bundleGraph.addEdge(src, new FastGraphEdgeImpl<>(src, bundles.get(e.dst())));
        }
    }
    // Linearize & flatten
    order = new IndexedList<>();
    // for efficiency
    Set<BlockBundle> bundlesSet = new HashSet<>(bundles.values());
    ControlFlowGraphDumper.linearize(bundlesSet, bundleGraph, entryBundle).forEach(order::addAll);
}
Also used : ImmediateEdge(org.mapleir.flowgraph.edges.ImmediateEdge) BasicBlock(org.mapleir.ir.cfg.BasicBlock) IndexedList(org.mapleir.stdlib.util.IndexedList)

Example 57 with BasicBlock

use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.

the class DominanceLivenessAnalyser method computeStrictDominators.

private void computeStrictDominators() {
    for (BasicBlock b : reducedDfs.getPostOrder()) {
        BasicBlock idom = domc.idom(b);
        if (idom != null) {
            GenericBitSet<BasicBlock> set = sdoms.getNonNull(idom);
            set.add(b);
            set.addAll(sdoms.getNonNull(b));
        }
    }
}
Also used : BasicBlock(org.mapleir.ir.cfg.BasicBlock)

Example 58 with BasicBlock

use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.

the class DominanceLivenessAnalyser method computeTargetReachability.

private void computeTargetReachability() {
    for (BasicBlock b : reducedDfs.getPreOrder()) {
        tq.getNonNull(b).add(b);
        // Tup(t) = set of unreachable backedge targets from reachable sources
        GenericBitSet<BasicBlock> tup = backEdges.getNonNull(b).relativeComplement(rv.get(b));
        for (BasicBlock w : tup) {
            tq.get(b).addAll(tq.get(w));
        }
    }
}
Also used : BasicBlock(org.mapleir.ir.cfg.BasicBlock)

Example 59 with BasicBlock

use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.

the class SSADefUseMap method buildIndex.

protected void buildIndex(BasicBlock b, Stmt stmt, int index, Set<Local> usedLocals) {
    if (stmt instanceof AbstractCopyStmt) {
        AbstractCopyStmt copy = (AbstractCopyStmt) stmt;
        defIndex.put(copy.getVariable().getLocal(), index);
        if (copy instanceof CopyPhiStmt) {
            PhiExpr phi = ((CopyPhiStmt) copy).getExpression();
            for (Entry<BasicBlock, Expr> en : phi.getArguments().entrySet()) {
                lastUseIndex.getNonNull(((VarExpr) en.getValue()).getLocal()).put(en.getKey(), en.getKey().size());
            // lastUseIndex.get(ul).put(b, -1);
            }
            return;
        }
    }
    for (Local usedLocal : usedLocals) lastUseIndex.getNonNull(usedLocal).put(b, index);
}
Also used : VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) BasicBlock(org.mapleir.ir.cfg.BasicBlock) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) VarExpr(org.mapleir.ir.code.expr.VarExpr) Local(org.mapleir.ir.locals.Local) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Example 60 with BasicBlock

use of org.mapleir.ir.cfg.BasicBlock in project maple-ir by LLVM-but-worse.

the class SSADefUseMap method compute.

public void compute() {
    defs.clear();
    uses.clear();
    phiDefs.clear();
    phiUses.clear();
    Set<Local> usedLocals = new HashSet<>();
    for (BasicBlock b : cfg.vertices()) {
        for (Stmt stmt : b) {
            phiUses.getNonNull(b);
            usedLocals.clear();
            for (Expr s : stmt.enumerateOnlyChildren()) if (s.getOpcode() == Opcode.LOCAL_LOAD)
                usedLocals.add(((VarExpr) s).getLocal());
            build(b, stmt, usedLocals);
        }
    }
}
Also used : VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) BasicBlock(org.mapleir.ir.cfg.BasicBlock) Local(org.mapleir.ir.locals.Local) HashSet(java.util.HashSet) Stmt(org.mapleir.ir.code.Stmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)

Aggregations

BasicBlock (org.mapleir.ir.cfg.BasicBlock)70 Stmt (org.mapleir.ir.code.Stmt)37 Expr (org.mapleir.ir.code.Expr)34 Local (org.mapleir.ir.locals.Local)30 VarExpr (org.mapleir.ir.code.expr.VarExpr)29 CopyPhiStmt (org.mapleir.ir.code.stmt.copy.CopyPhiStmt)25 AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)24 CopyVarStmt (org.mapleir.ir.code.stmt.copy.CopyVarStmt)22 PhiExpr (org.mapleir.ir.code.expr.PhiExpr)19 VersionedLocal (org.mapleir.ir.locals.impl.VersionedLocal)19 HashSet (java.util.HashSet)12 ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)11 UnconditionalJumpStmt (org.mapleir.ir.code.stmt.UnconditionalJumpStmt)10 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)9 BasicLocal (org.mapleir.ir.locals.impl.BasicLocal)9 NullPermeableHashMap (org.mapleir.stdlib.collections.map.NullPermeableHashMap)9 ConditionalJumpStmt (org.mapleir.ir.code.stmt.ConditionalJumpStmt)8 Type (org.objectweb.asm.Type)7 ArrayList (java.util.ArrayList)6 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)6