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();
}
}
}
}
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);
}
}
}
}
}
}
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));
}
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;
}
}
}
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();
}
Aggregations