Search in sources :

Example 41 with Local

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

the class BoissinotDestructor method sequentialize.

private void sequentialize(BasicBlock b) {
    // TODO: just rebuild the instruction list
    LinkedHashMap<ParallelCopyVarStmt, Integer> p = new LinkedHashMap<>();
    for (int i = 0; i < b.size(); i++) {
        Stmt stmt = b.get(i);
        if (stmt instanceof ParallelCopyVarStmt)
            p.put((ParallelCopyVarStmt) stmt, i);
    }
    if (p.isEmpty())
        return;
    int indexOffset = 0;
    Local spill = locals.makeLatestVersion(p.entrySet().iterator().next().getKey().pairs.get(0).targ);
    for (Entry<ParallelCopyVarStmt, Integer> e : p.entrySet()) {
        ParallelCopyVarStmt pcvs = e.getKey();
        int index = e.getValue();
        if (pcvs.pairs.size() == 0)
            throw new IllegalArgumentException("pcvs is empty");
        else if (pcvs.pairs.size() == 1) {
            // constant sequentialize for trivial parallel copies
            CopyPair pair = pcvs.pairs.get(0);
            CopyVarStmt newCopy = new CopyVarStmt(new VarExpr(pair.targ, pair.type), new VarExpr(pair.source, pair.type));
            b.set(index + indexOffset, newCopy);
        } else {
            List<CopyVarStmt> sequentialized = pcvs.sequentialize(spill);
            b.remove(index + indexOffset--);
            for (CopyVarStmt cvs : sequentialized) {
                // warning: O(N^2) operation
                b.add(index + ++indexOffset, cvs);
            }
        }
    }
}
Also used : CopyVarStmt(org.mapleir.ir.code.stmt.copy.CopyVarStmt) 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) VarExpr(org.mapleir.ir.code.expr.VarExpr)

Example 42 with Local

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

the class SSADefUseMap method buildIndex.

protected void buildIndex(BasicBlock b, Stmt stmt, int index, Set<Local> usedLocals) {
    if (stmt instanceof AbstractCopyStmt) {
        AbstractCopyStmt copy = (AbstractCopyStmt) stmt;
        defIndex.put(copy.getVariable().getLocal(), index);
        if (copy instanceof CopyPhiStmt) {
            PhiExpr phi = ((CopyPhiStmt) copy).getExpression();
            for (Entry<BasicBlock, Expr> en : phi.getArguments().entrySet()) {
                lastUseIndex.getNonNull(((VarExpr) en.getValue()).getLocal()).put(en.getKey(), en.getKey().size());
            // lastUseIndex.get(ul).put(b, -1);
            }
            return;
        }
    }
    for (Local usedLocal : usedLocals) lastUseIndex.getNonNull(usedLocal).put(b, index);
}
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) VarExpr(org.mapleir.ir.code.expr.VarExpr) Local(org.mapleir.ir.locals.Local) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Example 43 with Local

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

the class SSADefUseMap method compute.

public void compute() {
    defs.clear();
    uses.clear();
    phiDefs.clear();
    phiUses.clear();
    Set<Local> usedLocals = new HashSet<>();
    for (BasicBlock b : cfg.vertices()) {
        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());
            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 44 with Local

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

the class SreedharDestructor method init.

// ============================================================================================================= //
// =============================================== Initialization ============================================== //
// ============================================================================================================= //
private void init() {
    // init pccs
    for (CopyPhiStmt copyPhi : defuse.phiDefs.values()) {
        Local phiTarget = copyPhi.getVariable().getLocal();
        pccs.getNonNull(phiTarget).add(phiTarget);
        // System.out.println("Initphi " + phiTarget);
        for (Entry<BasicBlock, Expr> phiEntry : copyPhi.getExpression().getArguments().entrySet()) {
            if (phiEntry.getValue().getOpcode() != LOCAL_LOAD)
                throw new IllegalArgumentException("Phi arg is not local; instead is " + phiEntry.getValue().getClass().getSimpleName());
            Local phiSource = ((VarExpr) phiEntry.getValue()).getLocal();
            pccs.getNonNull(phiSource).add(phiSource);
        // System.out.println("Initphi " + phiSource);
        }
    }
    // System.out.println();
    // compute liveness
    (liveness = new SSABlockLivenessAnalyser(cfg)).compute();
    // writer.add("liveness", new LivenessDecorator<ControlFlowGraph, BasicBlock, FlowEdge<BasicBlock>>().setLiveness(liveness));
    buildInterference();
}
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) VarExpr(org.mapleir.ir.code.expr.VarExpr) CopyPhiStmt(org.mapleir.ir.code.stmt.copy.CopyPhiStmt)

Example 45 with Local

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

the class SreedharDestructor method resolvePhiSource.

private Local resolvePhiSource(Local xi, BasicBlock lk, Type phiType) {
    // Insert spill copy
    Local spill = insertEnd(xi, lk, phiType);
    // Update liveness
    GenericBitSet<Local> liveOut = liveness.out(lk);
    liveOut.add(spill);
    // xi can be removed from liveOut iff it isn't live into any succ or used in any succ phi.
    for (BasicBlock lj : succsCache.getNonNull(lk)) {
        if (!liveness.in(lj).contains(xi))
            removeFromOut: {
                for (int i = 0; i < lj.size() && lj.get(i).getOpcode() == Opcode.PHI_STORE; i++) if (((VarExpr) ((CopyPhiStmt) lj.get(i)).getExpression().getArguments().get(lk)).getLocal() == xi)
                    break removeFromOut;
                // poor man's for-else loop
                liveOut.remove(xi);
            }
    }
    // Reflexively update interference
    interfere.getNonNull(spill).addAll(liveOut);
    for (Local l : liveOut) interfere.get(l).add(spill);
    return spill;
}
Also used : BasicBlock(org.mapleir.ir.cfg.BasicBlock) Local(org.mapleir.ir.locals.Local) 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