Search in sources :

Example 1 with VersionedLocal

use of org.mapleir.ir.locals.impl.VersionedLocal in project maple-ir by LLVM-but-worse.

the class BoissinotDestructor method coalesceCopies.

// Coalesce parallel and standard copies based on value interference, dropping coalesced copies
private void coalesceCopies() {
    // if they do not interfere merge the conClasses and those two vars can be coalesced. delete the copy.
    for (BasicBlock b : dom_dfs.getPreOrder()) {
        for (Iterator<Stmt> it = b.iterator(); it.hasNext(); ) {
            Stmt stmt = it.next();
            if (stmt instanceof CopyVarStmt) {
                CopyVarStmt copy = (CopyVarStmt) stmt;
                if (!copy.isSynthetic() && copy.getExpression() instanceof VarExpr) {
                    Local lhs = copy.getVariable().getLocal();
                    Local rhs = ((VarExpr) copy.getExpression()).getLocal();
                    if (!isReservedRegister((VersionedLocal) rhs)) {
                        if (tryCoalesceCopyValue(lhs, rhs)) {
                            // System.out.println("COPYKILL(1) " + lhs + " == " + rhs);
                            it.remove();
                        }
                        if (tryCoalesceCopySharing(lhs, rhs)) {
                            // System.out.println("SHAREKILL(1) " + lhs + " == " + rhs);
                            it.remove();
                        }
                    }
                }
            } else if (stmt instanceof ParallelCopyVarStmt) {
                // we need to do it for each one. if all of the copies are
                // removed then remove the pcvs
                ParallelCopyVarStmt copy = (ParallelCopyVarStmt) stmt;
                for (Iterator<CopyPair> pairIter = copy.pairs.listIterator(); pairIter.hasNext(); ) {
                    CopyPair pair = pairIter.next();
                    Local lhs = pair.targ, rhs = pair.source;
                    if (!isReservedRegister((VersionedLocal) rhs)) {
                        if (tryCoalesceCopyValue(lhs, rhs)) {
                            // System.out.println("COPYKILL(2) " + lhs + " == " + rhs);
                            pairIter.remove();
                        }
                        if (tryCoalesceCopySharing(lhs, rhs)) {
                            // System.out.println("SHAREKILL(2) " + lhs + " == " + rhs);
                            pairIter.remove();
                        }
                    }
                }
                if (copy.pairs.isEmpty())
                    it.remove();
            }
        }
    }
}
Also used : VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) BasicBlock(org.mapleir.ir.cfg.BasicBlock) VarExpr(org.mapleir.ir.code.expr.VarExpr) Local(org.mapleir.ir.locals.Local) VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) Stmt(org.mapleir.ir.code.Stmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)

Example 2 with VersionedLocal

use of org.mapleir.ir.locals.impl.VersionedLocal in project maple-ir by LLVM-but-worse.

the class BoissinotDestructor method liftPhiOperands.

private void liftPhiOperands() {
    for (BasicBlock b : cfg.vertices()) {
        for (Stmt stmt : new ArrayList<>(b)) {
            if (stmt.getOpcode() == Opcode.PHI_STORE) {
                CopyPhiStmt copy = (CopyPhiStmt) stmt;
                for (Entry<BasicBlock, Expr> e : copy.getExpression().getArguments().entrySet()) {
                    Expr expr = e.getValue();
                    int opcode = expr.getOpcode();
                    if (opcode == Opcode.CONST_LOAD || opcode == Opcode.CATCH) {
                        VersionedLocal vl = locals.makeLatestVersion(locals.get(0, false));
                        CopyVarStmt cvs = new CopyVarStmt(new VarExpr(vl, expr.getType()), expr);
                        e.setValue(new VarExpr(vl, expr.getType()));
                        insertEnd(e.getKey(), cvs);
                    } else if (opcode != Opcode.LOCAL_LOAD) {
                        throw new IllegalArgumentException("Non-variable expression in phi: " + copy);
                    }
                }
            }
        }
    }
}
Also used : VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) VarExpr(org.mapleir.ir.code.expr.VarExpr) Expr(org.mapleir.ir.code.Expr) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) BasicBlock(org.mapleir.ir.cfg.BasicBlock) VarExpr(org.mapleir.ir.code.expr.VarExpr) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) Stmt(org.mapleir.ir.code.Stmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Example 3 with VersionedLocal

use of org.mapleir.ir.locals.impl.VersionedLocal in project maple-ir by LLVM-but-worse.

the class ControlFlowGraph method overwrite.

/**
 * Replaces an expression and updates def/use information accordingly.
 * @param parent Statement containing expression to be replaced.
 * @param from Statement to be replaced.
 * @param to Statement to replace old statement with.
 */
public void overwrite(CodeUnit parent, Expr from, Expr to) {
    // remove uses in from
    for (Expr e : from.enumerateWithSelf()) {
        if (e.getOpcode() == Opcode.LOCAL_LOAD) {
            VersionedLocal l = (VersionedLocal) ((VarExpr) e).getLocal();
            locals.uses.get(l).remove(e);
        }
    }
    // add uses in to
    for (Expr e : to.enumerateWithSelf()) {
        if (e.getOpcode() == Opcode.LOCAL_LOAD) {
            VarExpr var = (VarExpr) e;
            locals.uses.get((VersionedLocal) var.getLocal()).add(var);
        }
    }
    parent.overwrite(to, parent.indexOf(from));
}
Also used : VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) 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)

Example 4 with VersionedLocal

use of org.mapleir.ir.locals.impl.VersionedLocal in project maple-ir by LLVM-but-worse.

the class ControlFlowGraph method exciseEdge.

/**
 * Properly removes the edge, and cleans up phi uses in fe.dst of phi arguments from fe.src.
 * @param fe Edge to excise phi uses.
 */
public void exciseEdge(FlowEdge<BasicBlock> fe) {
    if (!this.containsEdge(fe.src(), fe))
        throw new IllegalArgumentException("Graph does not contain the specified edge");
    removeEdge(fe.src(), fe);
    for (Stmt stmt : fe.dst()) {
        if (stmt.getOpcode() == PHI_STORE) {
            CopyPhiStmt phs = (CopyPhiStmt) stmt;
            PhiExpr phi = phs.getExpression();
            BasicBlock pred = fe.src();
            VarExpr arg = (VarExpr) phi.getArgument(pred);
            VersionedLocal l = (VersionedLocal) arg.getLocal();
            locals.uses.get(l).remove(arg);
            phi.removeArgument(pred);
        } else {
            return;
        }
    }
}
Also used : VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) PhiExpr(org.mapleir.ir.code.expr.PhiExpr) VarExpr(org.mapleir.ir.code.expr.VarExpr) Stmt(org.mapleir.ir.code.Stmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Example 5 with VersionedLocal

use of org.mapleir.ir.locals.impl.VersionedLocal in project maple-ir by LLVM-but-worse.

the class SSAGenPass method pruneStatements.

private int pruneStatements() {
    int s = pool.uses.size();
    Iterator<Entry<VersionedLocal, Set<VarExpr>>> it = pool.uses.entrySet().iterator();
    while (it.hasNext()) {
        Entry<VersionedLocal, Set<VarExpr>> e = it.next();
        VersionedLocal vl = e.getKey();
        if (e.getValue().size() == 0) {
            AbstractCopyStmt def = pool.defs.get(vl);
            /* i.e. it has not been shadowed. */
            if (def != null && def.getBlock() != null && prune(def)) {
                if (vl != def.getVariable().getLocal()) {
                    throw new RuntimeException(vl + ", " + def);
                }
                /* use pool remove */
                it.remove();
                pool.defs.remove(vl);
            }
        }
    }
    return s - pool.uses.size();
}
Also used : VersionedLocal(org.mapleir.ir.locals.impl.VersionedLocal) Entry(java.util.Map.Entry) VarExpr(org.mapleir.ir.code.expr.VarExpr) AbstractCopyStmt(org.mapleir.ir.code.stmt.copy.AbstractCopyStmt) Constraint(org.mapleir.ir.cfg.builder.ssaopt.Constraint)

Aggregations

VersionedLocal (org.mapleir.ir.locals.impl.VersionedLocal)21 VarExpr (org.mapleir.ir.code.expr.VarExpr)18 Expr (org.mapleir.ir.code.Expr)12 PhiExpr (org.mapleir.ir.code.expr.PhiExpr)12 AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)11 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)9 InitialisedObjectExpr (org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr)8 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)8 Stmt (org.mapleir.ir.code.Stmt)6 CopyPhiStmt (org.mapleir.ir.code.stmt.copy.CopyPhiStmt)6 BasicBlock (org.mapleir.ir.cfg.BasicBlock)5 Constraint (org.mapleir.ir.cfg.builder.ssaopt.Constraint)5 CopyVarStmt (org.mapleir.ir.code.stmt.copy.CopyVarStmt)5 Local (org.mapleir.ir.locals.Local)5 LocalsPool (org.mapleir.ir.locals.LocalsPool)5 LatestValue (org.mapleir.ir.cfg.builder.ssaopt.LatestValue)4 CodeUnit (org.mapleir.ir.code.CodeUnit)3 BasicLocal (org.mapleir.ir.locals.impl.BasicLocal)3 HashSet (java.util.HashSet)2 Entry (java.util.Map.Entry)2