Search in sources :

Example 6 with Expr

use of org.mapleir.ir.code.Expr in project maple-ir by LLVM-but-worse.

the class SSADefUseMap method build.

protected void build(BasicBlock b, Stmt stmt, Set<Local> usedLocals) {
    if (stmt instanceof AbstractCopyStmt) {
        AbstractCopyStmt copy = (AbstractCopyStmt) stmt;
        Local l = copy.getVariable().getLocal();
        defs.put(l, b);
        if (copy instanceof CopyPhiStmt) {
            phiDefs.put(l, (CopyPhiStmt) copy);
            PhiExpr phi = (PhiExpr) copy.getExpression();
            for (Entry<BasicBlock, Expr> en : phi.getArguments().entrySet()) {
                Local ul = ((VarExpr) en.getValue()).getLocal();
                uses.getNonNull(ul).add(en.getKey());
                phiUses.get(b).add(ul);
            }
            return;
        }
    }
    for (Local usedLocal : usedLocals) uses.getNonNull(usedLocal).add(b);
}
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) Local(org.mapleir.ir.locals.Local) VarExpr(org.mapleir.ir.code.expr.VarExpr) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Example 7 with Expr

use of org.mapleir.ir.code.Expr in project maple-ir by LLVM-but-worse.

the class SSADefUseMap method computeWithIndices.

public void computeWithIndices(List<BasicBlock> preorder) {
    defs.clear();
    uses.clear();
    phiDefs.clear();
    phiUses.clear();
    lastUseIndex.clear();
    defIndex.clear();
    int index = 0;
    Set<Local> usedLocals = new HashSet<>();
    for (BasicBlock b : preorder) {
        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());
            buildIndex(b, stmt, index++, usedLocals);
            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)

Example 8 with Expr

use of org.mapleir.ir.code.Expr in project maple-ir by LLVM-but-worse.

the class SreedharDestructor method csaa_iii.

// ============================================================================================================= //
// =================================================== CSSA ==================================================== //
// ============================================================================================================= //
private void csaa_iii() {
    // iterate over each phi expression
    for (Entry<Local, CopyPhiStmt> entry : defuse.phiDefs.entrySet()) {
        // System.out.println("process phi " + entry.getValue());
        // x0
        Local phiTarget = entry.getKey();
        CopyPhiStmt copy = entry.getValue();
        // l0
        BasicBlock defBlock = defuse.defs.get(phiTarget);
        PhiExpr phi = copy.getExpression();
        candidateResourceSet.clear();
        unresolvedNeighborsMap.clear();
        // Initialize phiResources set for convenience
        final GenericBitSet<PhiResource> phiResources = phiResSetCreator.create();
        phiResources.add(new PhiResource(defBlock, phiTarget, true));
        for (Entry<BasicBlock, Expr> phiEntry : phi.getArguments().entrySet()) phiResources.add(new PhiResource(phiEntry.getKey(), ((VarExpr) phiEntry.getValue()).getLocal(), false));
        // Determine what copies are needed using the four cases.
        handleInterference(phiResources);
        // Process unresolved resources
        resolveDeferred();
        // System.out.println("  Cand: " + candidateResourceSet);
        // Resolve the candidate resources
        Type phiType = phi.getType();
        for (PhiResource toResolve : candidateResourceSet) {
            if (toResolve.isTarget)
                resolvePhiTarget(toResolve, phiType);
            else
                for (Entry<BasicBlock, Expr> phiArg : phi.getArguments().entrySet()) {
                    VarExpr phiVar = (VarExpr) phiArg.getValue();
                    if (phiVar.getLocal() == toResolve.local)
                        phiVar.setLocal(resolvePhiSource(toResolve.local, phiArg.getKey(), phiType));
                }
        }
        // System.out.println("  interference: ");
        // for (Entry<Local, GenericBitSet<Local>> entry2 : interfere.entrySet())
        // System.out.println("    " + entry2.getKey() + " : " + entry2.getValue());
        // System.out.println("  post-inserted: " + copy);
        // Merge pccs for all locals in phi
        final GenericBitSet<Local> phiLocals = locals.createBitSet();
        phiLocals.add(copy.getVariable().getLocal());
        for (Entry<BasicBlock, Expr> phiEntry : phi.getArguments().entrySet()) phiLocals.add(((VarExpr) phiEntry.getValue()).getLocal());
        for (Local phiLocal : phiLocals) pccs.put(phiLocal, phiLocals);
        // Nullify singleton pccs
        for (GenericBitSet<Local> pcc : pccs.values()) if (pcc.size() <= 1)
            pcc.clear();
    // System.out.println("  pccs:");
    // for (Entry<Local, GenericBitSet<Local>> entry2 : pccs.entrySet())
    // System.out.println("    " + entry2.getKey() + " : " + entry2.getValue());
    // System.out.println();
    }
}
Also used : BasicBlock(org.mapleir.ir.cfg.BasicBlock) Local(org.mapleir.ir.locals.Local) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt) Type(org.objectweb.asm.Type) Entry(java.util.Map.Entry) 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) VarExpr(org.mapleir.ir.code.expr.VarExpr)

Example 9 with Expr

use of org.mapleir.ir.code.Expr 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 10 with Expr

use of org.mapleir.ir.code.Expr 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

Expr (org.mapleir.ir.code.Expr)87 VarExpr (org.mapleir.ir.code.expr.VarExpr)46 InvocationExpr (org.mapleir.ir.code.expr.invoke.InvocationExpr)45 BasicBlock (org.mapleir.ir.cfg.BasicBlock)32 PhiExpr (org.mapleir.ir.code.expr.PhiExpr)31 Stmt (org.mapleir.ir.code.Stmt)29 ConstantExpr (org.mapleir.ir.code.expr.ConstantExpr)26 AbstractCopyStmt (org.mapleir.ir.code.stmt.copy.AbstractCopyStmt)26 Local (org.mapleir.ir.locals.Local)22 Type (org.objectweb.asm.Type)21 VersionedLocal (org.mapleir.ir.locals.impl.VersionedLocal)20 CopyPhiStmt (org.mapleir.ir.code.stmt.copy.CopyPhiStmt)19 InitialisedObjectExpr (org.mapleir.ir.code.expr.invoke.InitialisedObjectExpr)17 ComparisonType (org.mapleir.ir.code.stmt.ConditionalJumpStmt.ComparisonType)14 CopyVarStmt (org.mapleir.ir.code.stmt.copy.CopyVarStmt)14 ValueComparisonType (org.mapleir.ir.code.expr.ComparisonExpr.ValueComparisonType)13 ArrayType (org.mapleir.ir.TypeUtils.ArrayType)12 HashSet (java.util.HashSet)11 ArithmeticExpr (org.mapleir.ir.code.expr.ArithmeticExpr)11 ControlFlowGraph (org.mapleir.ir.cfg.ControlFlowGraph)9