Search in sources :

Example 16 with Local

use of org.mapleir.ir.locals.Local 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 17 with Local

use of org.mapleir.ir.locals.Local 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 18 with Local

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

the class SreedharDestructor method resolvePhiTarget.

private void resolvePhiTarget(PhiResource res, Type phiType) {
    // Insert spill copy
    Local spill = insertStart(res, phiType);
    // Update liveness
    GenericBitSet<Local> liveIn = liveness.in(res.block);
    liveIn.remove(res.local);
    liveIn.add(spill);
    // Reflexively update interference
    interfere.getNonNull(spill).addAll(liveIn);
    for (Local l : liveIn) interfere.get(l).add(spill);
}
Also used : Local(org.mapleir.ir.locals.Local)

Example 19 with Local

use of org.mapleir.ir.locals.Local 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 20 with Local

use of org.mapleir.ir.locals.Local 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

Local (org.mapleir.ir.locals.Local)49 VarExpr (org.mapleir.ir.code.expr.VarExpr)33 BasicBlock (org.mapleir.ir.cfg.BasicBlock)29 CopyPhiStmt (org.mapleir.ir.code.stmt.copy.CopyPhiStmt)24 VersionedLocal (org.mapleir.ir.locals.impl.VersionedLocal)24 Expr (org.mapleir.ir.code.Expr)23 Stmt (org.mapleir.ir.code.Stmt)22 AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)21 PhiExpr (org.mapleir.ir.code.expr.PhiExpr)18 CopyVarStmt (org.mapleir.ir.code.stmt.copy.CopyVarStmt)18 BasicLocal (org.mapleir.ir.locals.impl.BasicLocal)10 NullPermeableHashMap (org.mapleir.stdlib.collections.map.NullPermeableHashMap)7 HashSet (java.util.HashSet)6 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)5 ConditionalJumpStmt (org.mapleir.ir.code.stmt.ConditionalJumpStmt)5 PopStmt (org.mapleir.ir.code.stmt.PopStmt)5 SwitchStmt (org.mapleir.ir.code.stmt.SwitchStmt)5 ThrowStmt (org.mapleir.ir.code.stmt.ThrowStmt)5 UnconditionalJumpStmt (org.mapleir.ir.code.stmt.UnconditionalJumpStmt)5 LocalsPool (org.mapleir.ir.locals.LocalsPool)5