Search in sources :

Example 1 with GenericBitSet

use of org.mapleir.stdlib.collections.bitset.GenericBitSet in project maple-ir by LLVM-but-worse.

the class SSABlockLivenessAnalyser method compute.

public void compute() {
    // negative handling always goes after positive and any adds
    while (!queue.isEmpty()) {
        BasicBlock b = queue.remove();
        // System.out.println("\n\nProcessing " + b.getId());
        GenericBitSet<Local> oldIn = new GenericBitSet<>(in.get(b));
        GenericBitSet<Local> curIn = new GenericBitSet<>(use.get(b));
        GenericBitSet<Local> curOut = locals.createBitSet();
        // out[n] = U(s in succ[n])(in[s])
        for (FlowEdge<BasicBlock> succEdge : cfg.getEdges(b)) curOut.addAll(in.get(succEdge.dst()));
        // negative phi handling for defs
        for (FlowEdge<BasicBlock> succEdge : cfg.getEdges(b)) curOut.removeAll(phiDef.get(succEdge.dst()));
        // positive phi handling for uses, see ยง5.4.2 "Meaning of copy statements in Sreedhar's method"
        for (FlowEdge<BasicBlock> succEdge : cfg.getEdges(b)) curOut.addAll(phiUse.get(succEdge.dst()).getNonNull(b));
        // negative phi handling for uses
        for (FlowEdge<BasicBlock> predEdge : cfg.getReverseEdges(b)) curIn.removeAll(phiUse.get(b).getNonNull(predEdge.src()).relativeComplement(use.get(b)));
        // positive phi handling for defs
        curIn.addAll(phiDef.get(b));
        oldIn.addAll(phiDef.get(b));
        // in[n] = use[n] U(out[n] - def[n])
        curIn.addAll(curOut.relativeComplement(def.get(b)));
        in.put(b, curIn);
        out.put(b, curOut);
        // queue preds if dataflow state changed
        if (!oldIn.equals(curIn)) {
            cfg.getReverseEdges(b).stream().map(e -> e.src()).forEach(this::enqueue);
        // for (BasicBlock b2 : cfg.vertices())
        // System.out.println(b2.getId() + " |||| IN: " + in.get(b2) + " ||||| OUT: " + out.get(b2));
        }
    }
}
Also used : Local(org.mapleir.ir.locals.Local) ListIterator(java.util.ListIterator) VarExpr(org.mapleir.ir.code.expr.VarExpr) GenericBitSet(org.mapleir.stdlib.collections.bitset.GenericBitSet) Expr(org.mapleir.ir.code.Expr) FlowEdge(org.mapleir.flowgraph.edges.FlowEdge) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) ControlFlowGraph(org.mapleir.ir.cfg.ControlFlowGraph) LocalsPool(org.mapleir.ir.locals.LocalsPool) Stmt(org.mapleir.ir.code.Stmt) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) NullPermeableHashMap(org.mapleir.stdlib.collections.map.NullPermeableHashMap) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) Opcode(org.mapleir.ir.code.Opcode) FlowEdges(org.mapleir.flowgraph.edges.FlowEdges) Map(java.util.Map) Queue(java.util.Queue) LinkedList(java.util.LinkedList) BasicBlock(org.mapleir.ir.cfg.BasicBlock) BasicBlock(org.mapleir.ir.cfg.BasicBlock) Local(org.mapleir.ir.locals.Local) GenericBitSet(org.mapleir.stdlib.collections.bitset.GenericBitSet)

Example 2 with GenericBitSet

use of org.mapleir.stdlib.collections.bitset.GenericBitSet in project maple-ir by LLVM-but-worse.

the class SreedharDestructor method leaveSSA.

// ============================================================================================================= //
// ================================================== Leave SSA ================================================ //
// ============================================================================================================= //
private void leaveSSA() {
    // Flatten pccs into one variable through remapping
    // System.out.println("remap:");
    Map<Local, Local> remap = new HashMap<>();
    for (Entry<Local, GenericBitSet<Local>> entry : pccs.entrySet()) {
        GenericBitSet<Local> pcc = entry.getValue();
        if (pcc.isEmpty())
            continue;
        Local local = entry.getKey();
        if (remap.containsKey(local))
            continue;
        Local newLocal = locals.makeLatestVersion(local);
        remap.put(local, newLocal);
        // System.out.println("  " + local + " -> " + newLocal);
        for (Local pccLocal : pcc) {
            remap.put(pccLocal, newLocal);
        // System.out.println("  " + pccLocal + " -> " + newLocal);
        }
    }
    for (BasicBlock b : cfg.vertices()) {
        for (Iterator<Stmt> it = b.iterator(); it.hasNext(); ) {
            Stmt stmt = it.next();
            // We can now simply drop all phi statements.
            if (stmt instanceof CopyPhiStmt) {
                it.remove();
                continue;
            }
            // Apply remappings
            if (stmt instanceof CopyVarStmt) {
                VarExpr lhs = ((CopyVarStmt) stmt).getVariable();
                Local copyTarget = lhs.getLocal();
                lhs.setLocal(remap.getOrDefault(copyTarget, copyTarget));
            }
            for (Expr child : stmt.enumerateOnlyChildren()) {
                if (child.getOpcode() == LOCAL_LOAD) {
                    VarExpr var = (VarExpr) child;
                    Local loadSource = var.getLocal();
                    var.setLocal(remap.getOrDefault(loadSource, loadSource));
                }
            }
        }
    }
// System.out.println();
}
Also used : HashMap(java.util.HashMap) NullPermeableHashMap(org.mapleir.stdlib.collections.map.NullPermeableHashMap) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) BasicBlock(org.mapleir.ir.cfg.BasicBlock) Local(org.mapleir.ir.locals.Local) GenericBitSet(org.mapleir.stdlib.collections.bitset.GenericBitSet) Stmt(org.mapleir.ir.code.Stmt) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) VarExpr(org.mapleir.ir.code.expr.VarExpr)

Aggregations

BasicBlock (org.mapleir.ir.cfg.BasicBlock)2 Expr (org.mapleir.ir.code.Expr)2 Stmt (org.mapleir.ir.code.Stmt)2 PhiExpr (org.mapleir.ir.code.expr.PhiExpr)2 VarExpr (org.mapleir.ir.code.expr.VarExpr)2 CopyPhiStmt (org.mapleir.ir.code.stmt.copy.CopyPhiStmt)2 CopyVarStmt (org.mapleir.ir.code.stmt.copy.CopyVarStmt)2 Local (org.mapleir.ir.locals.Local)2 GenericBitSet (org.mapleir.stdlib.collections.bitset.GenericBitSet)2 NullPermeableHashMap (org.mapleir.stdlib.collections.map.NullPermeableHashMap)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 ListIterator (java.util.ListIterator)1 Map (java.util.Map)1 Queue (java.util.Queue)1 FlowEdge (org.mapleir.flowgraph.edges.FlowEdge)1 FlowEdges (org.mapleir.flowgraph.edges.FlowEdges)1 ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)1 Opcode (org.mapleir.ir.code.Opcode)1 LocalsPool (org.mapleir.ir.locals.LocalsPool)1