Search in sources :

Example 6 with CopyVarStmt

use of org.mapleir.ir.code.stmt.copy.CopyVarStmt in project maple-ir by LLVM-but-worse.

the class SreedharDestructor method coalesce.

// ============================================================================================================= //
// ================================================== Coalescing =============================================== //
// ============================================================================================================= //
private void coalesce() {
    for (BasicBlock b : cfg.vertices()) {
        for (Iterator<Stmt> it = b.iterator(); it.hasNext(); ) {
            Stmt stmt = it.next();
            if (stmt instanceof CopyVarStmt) {
                CopyVarStmt copy = (CopyVarStmt) stmt;
                // System.out.println("check " + copy);
                if (checkCoalesce(copy)) {
                    // System.out.println("  coalescing");
                    // Remove the copy
                    it.remove();
                    // Merge pccs
                    GenericBitSet<Local> pccX = pccs.get(copy.getVariable().getLocal());
                    Local localY = ((VarExpr) copy.getExpression()).getLocal();
                    GenericBitSet<Local> pccY = pccs.get(localY);
                    pccX.add(localY);
                    pccX.addAll(pccY);
                    for (Local l : pccY) pccs.put(l, pccX);
                }
            }
        }
    }
// System.out.println("post-coalsce pccs:");
// for (Entry<Local, GenericBitSet<Local>> entry : pccs.entrySet())
// System.out.println("  " + entry.getKey() + " : " + entry.getValue());
// System.out.println();
}
Also used : CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) BasicBlock(org.mapleir.ir.cfg.BasicBlock) Local(org.mapleir.ir.locals.Local) VarExpr(org.mapleir.ir.code.expr.VarExpr) Stmt(org.mapleir.ir.code.Stmt) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Example 7 with CopyVarStmt

use of org.mapleir.ir.code.stmt.copy.CopyVarStmt in project maple-ir by LLVM-but-worse.

the class SreedharDestructor method insertEnd.

private Local insertEnd(Local xi, BasicBlock lk, Type type) {
    Local spill = locals.makeLatestVersion(xi);
    CopyVarStmt newCopy = new CopyVarStmt(new VarExpr(spill, type), new VarExpr(xi, type));
    if (lk.isEmpty())
        lk.add(newCopy);
    else if (!lk.get(lk.size() - 1).canChangeFlow())
        lk.add(newCopy);
    else
        lk.add(lk.size() - 1, newCopy);
    return spill;
}
Also used : CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) Local(org.mapleir.ir.locals.Local) VarExpr(org.mapleir.ir.code.expr.VarExpr)

Example 8 with CopyVarStmt

use of org.mapleir.ir.code.stmt.copy.CopyVarStmt 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)

Example 9 with CopyVarStmt

use of org.mapleir.ir.code.stmt.copy.CopyVarStmt in project maple-ir by LLVM-but-worse.

the class SreedharDestructor method insertStart.

// replace the phi target xi with xi' and place a temp copy xi = xi' after all phi statements.
private Local insertStart(PhiResource res, Type type) {
    BasicBlock li = res.block;
    Local xi = res.local;
    if (li.isEmpty())
        throw new IllegalStateException("Trying to resolve phi target interference in empty block " + li);
    Local spill = locals.makeLatestVersion(xi);
    int i;
    for (i = 0; i < li.size() && li.get(i).getOpcode() == Opcode.PHI_STORE; i++) {
        CopyPhiStmt copyPhi = (CopyPhiStmt) li.get(i);
        VarExpr copyTarget = copyPhi.getVariable();
        if (copyTarget.getLocal() == xi)
            copyTarget.setLocal(spill);
    }
    li.add(i, new CopyVarStmt(new VarExpr(xi, type), new VarExpr(spill, type)));
    return spill;
}
Also used : CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) BasicBlock(org.mapleir.ir.cfg.BasicBlock) Local(org.mapleir.ir.locals.Local) VarExpr(org.mapleir.ir.code.expr.VarExpr) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Example 10 with CopyVarStmt

use of org.mapleir.ir.code.stmt.copy.CopyVarStmt in project maple-ir by LLVM-but-worse.

the class SreedharDestructor method buildInterference.

private void buildInterference() {
    for (BasicBlock b : cfg.vertices()) {
        // not a copy!
        GenericBitSet<Local> in = liveness.in(b);
        // not a copy!
        GenericBitSet<Local> out = liveness.out(b);
        // in interfere in
        for (Local l : in) interfere.getNonNull(l).addAll(in);
        // out interfere out
        for (Local l : out) interfere.getNonNull(l).addAll(out);
        // backwards traverse for dealing with variables that are defined and used in the same block
        GenericBitSet<Local> intraLive = out.copy();
        ListIterator<Stmt> it = b.listIterator(b.size());
        while (it.hasPrevious()) {
            Stmt stmt = it.previous();
            if (stmt instanceof CopyVarStmt) {
                CopyVarStmt copy = (CopyVarStmt) stmt;
                Local defLocal = copy.getVariable().getLocal();
                intraLive.remove(defLocal);
            }
            for (Expr child : stmt.enumerateOnlyChildren()) {
                if (stmt.getOpcode() == LOCAL_LOAD) {
                    Local usedLocal = ((VarExpr) child).getLocal();
                    if (intraLive.add(usedLocal)) {
                        interfere.getNonNull(usedLocal).addAll(intraLive);
                        for (Local l : intraLive) interfere.get(l).add(usedLocal);
                    }
                }
            }
        }
    }
// System.out.println("Interference:");
// for (Entry<Local, GenericBitSet<Local>> entry : interfere.entrySet())
// System.out.println("  " + entry.getKey() + " : " + entry.getValue());
// System.out.println();
}
Also used : 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) Local(org.mapleir.ir.locals.Local) VarExpr(org.mapleir.ir.code.expr.VarExpr) Stmt(org.mapleir.ir.code.Stmt) CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Aggregations

CopyVarStmt (org.mapleir.ir.code.stmt.copy.CopyVarStmt)24 Stmt (org.mapleir.ir.code.Stmt)18 BasicBlock (org.mapleir.ir.cfg.BasicBlock)17 VarExpr (org.mapleir.ir.code.expr.VarExpr)17 CopyPhiStmt (org.mapleir.ir.code.stmt.copy.CopyPhiStmt)16 AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)14 Local (org.mapleir.ir.locals.Local)13 VersionedLocal (org.mapleir.ir.locals.impl.VersionedLocal)12 Expr (org.mapleir.ir.code.Expr)10 PhiExpr (org.mapleir.ir.code.expr.PhiExpr)8 ConditionalJumpStmt (org.mapleir.ir.code.stmt.ConditionalJumpStmt)6 PopStmt (org.mapleir.ir.code.stmt.PopStmt)6 SwitchStmt (org.mapleir.ir.code.stmt.SwitchStmt)6 ThrowStmt (org.mapleir.ir.code.stmt.ThrowStmt)6 UnconditionalJumpStmt (org.mapleir.ir.code.stmt.UnconditionalJumpStmt)6 Constraint (org.mapleir.ir.cfg.builder.ssaopt.Constraint)5 BasicLocal (org.mapleir.ir.locals.impl.BasicLocal)5 NullPermeableHashMap (org.mapleir.stdlib.collections.map.NullPermeableHashMap)5 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)3 Type (org.objectweb.asm.Type)3