use of org.mapleir.ir.code.expr.VarExpr in project maple-ir by LLVM-but-worse.
the class PoolLocalValueResolver method checkRecursive.
private void checkRecursive(Local l) {
Set<Local> visited = new HashSet<>();
Queue<Local> worklist = new LinkedList<>();
worklist.add(l);
while (!worklist.isEmpty()) {
l = worklist.poll();
AbstractCopyStmt copy = pool.defs.get(l);
Set<Local> set = new HashSet<>();
Expr rhs = copy.getExpression();
if (rhs.getOpcode() == Opcode.LOCAL_LOAD) {
set.add(((VarExpr) rhs).getLocal());
} else if (rhs.getOpcode() == Opcode.PHI) {
for (Expr e : ((PhiExpr) rhs).getArguments().values()) {
set.add(((VarExpr) e).getLocal());
}
}
for (Local v : set) {
if (visited.contains(v)) {
System.err.println(copy.getBlock().getGraph());
System.err.printf("visited: %s%n", visited);
System.err.printf(" copy: %s%n", copy);
System.err.printf(" dup: %s%n", v);
throw new RuntimeException();
}
}
worklist.addAll(set);
visited.addAll(set);
}
}
use of org.mapleir.ir.code.expr.VarExpr in project maple-ir by LLVM-but-worse.
the class AbstractCopyStmt method toCode.
@Override
public // todo: this probably needs a refactoring
void toCode(MethodVisitor visitor, ControlFlowGraph cfg) {
if (expression instanceof VarExpr) {
if (((VarExpr) expression).getLocal() == variable.getLocal()) {
return;
}
}
variable.getLocal().setTempLocal(false);
expression.toCode(visitor, cfg);
Type type = variable.getType();
if (TypeUtils.isPrimitive(type)) {
int[] cast = TypeUtils.getPrimitiveCastOpcodes(expression.getType(), type);
for (int i = 0; i < cast.length; i++) visitor.visitInsn(cast[i]);
}
Local local = variable.getLocal();
if (local.isStack()) {
visitor.visitVarInsn(TypeUtils.getVariableStoreOpcode(getType()), variable.getLocal().getCodeIndex());
variable.getLocal().setTempLocal(true);
} else {
visitor.visitVarInsn(TypeUtils.getVariableStoreOpcode(getType()), variable.getLocal().getCodeIndex());
}
}
use of org.mapleir.ir.code.expr.VarExpr in project maple-ir by LLVM-but-worse.
the class BoissinotDestructor method copyPhiOperands.
private void copyPhiOperands(BasicBlock b) {
NullPermeableHashMap<BasicBlock, List<PhiRes>> wl = new NullPermeableHashMap<>(new ListCreator<>());
ParallelCopyVarStmt dst_copy = new ParallelCopyVarStmt();
for (Stmt stmt : b) {
// phis only appear at the start of a block.
if (stmt.getOpcode() != Opcode.PHI_STORE) {
break;
}
CopyPhiStmt copy = (CopyPhiStmt) stmt;
PhiExpr phi = copy.getExpression();
// so that we can parallelise the copy when we insert it.
for (Entry<BasicBlock, Expr> e : phi.getArguments().entrySet()) {
BasicBlock h = e.getKey();
// these are validated in init().
VarExpr v = (VarExpr) e.getValue();
PhiRes r = new PhiRes(copy.getVariable().getLocal(), phi, h, v.getLocal(), v.getType());
wl.getNonNull(h).add(r);
}
// for each x0, where x0 is a phi copy target, create a new variable z0 for
// a copy x0 = z0 and replace the phi copy target to z0.
Local x0 = copy.getVariable().getLocal();
Local z0 = locals.makeLatestVersion(x0);
// x0 = z0
dst_copy.pairs.add(new CopyPair(x0, z0, copy.getVariable().getType()));
// z0 = phi(...)
copy.getVariable().setLocal(z0);
}
// resolve
if (dst_copy.pairs.size() > 0)
insertStart(b, dst_copy);
for (Entry<BasicBlock, List<PhiRes>> e : wl.entrySet()) {
BasicBlock p = e.getKey();
ParallelCopyVarStmt copy = new ParallelCopyVarStmt();
for (PhiRes r : e.getValue()) {
// for each xi source in a phi, create a new variable zi, and insert the copy
// zi = xi in the pred Li. then replace the phi arg from Li with zi.
Local xi = r.l;
Local zi = locals.makeLatestVersion(xi);
copy.pairs.add(new CopyPair(zi, xi, r.type));
// we consider phi args to be used in the pred instead of the block
// where the phi is, so we need to update the def/use maps here.
r.phi.setArgument(r.pred, new VarExpr(zi, r.type));
}
insertEnd(p, copy);
}
}
use of org.mapleir.ir.code.expr.VarExpr 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);
}
}
}
}
use of org.mapleir.ir.code.expr.VarExpr 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);
}
Aggregations