Code size optimization

This commit is contained in:
konsoletyper 2013-12-16 16:33:28 +04:00
parent 95242b346e
commit a736359a07
3 changed files with 27 additions and 28 deletions

View File

@ -155,7 +155,6 @@ public class Decompiler {
generator.program = program;
generator.blockMap = blockMap;
generator.indexer = indexer;
generator.outgoings = getPhiOutgoings(program);
parentNode = codeTree.getRoot();
currentNode = parentNode.getFirstChild();
for (int i = 0; i < this.graph.size(); ++i) {
@ -209,26 +208,6 @@ public class Decompiler {
return result;
}
private Incoming[][] getPhiOutgoings(Program program) {
List<List<Incoming>> outgoings = new ArrayList<>();
for (int i = 0; i < program.basicBlockCount(); ++i) {
outgoings.add(new ArrayList<Incoming>());
}
for (int i = 0; i < program.basicBlockCount(); ++i) {
BasicBlock basicBlock = program.basicBlockAt(i);
for (Phi phi : basicBlock.getPhis()) {
for (Incoming incoming : phi.getIncomings()) {
outgoings.get(incoming.getSource().getIndex()).add(incoming);
}
}
}
Incoming[][] result = new Incoming[outgoings.size()][];
for (int i = 0; i < outgoings.size(); ++i) {
result[i] = outgoings.get(i).toArray(new Incoming[0]);
}
return result;
}
private List<Block> createBlocks(int start) {
List<Block> result = new ArrayList<>();
while (currentNode != null && currentNode.getStart() == start) {

View File

@ -21,7 +21,6 @@ public class StatementGenerator implements InstructionVisitor {
Decompiler.Block[] blockMap;
Program program;
ClassHolderSource classSource;
Incoming[][] outgoings;
@Override
public void visit(EmptyInstruction insn) {
@ -580,11 +579,15 @@ public class StatementGenerator implements InstructionVisitor {
}
}
private Statement wrapWithPhis(Statement rawJump) {
private Statement wrapWithPhis(Statement rawJump, BasicBlock target) {
SequentialStatement seq = new SequentialStatement();
for (Incoming outgoing : outgoings[currentBlock.getIndex()]) {
seq.getSequence().add(Statement.assign(Expr.var(outgoing.getPhi().getReceiver().getIndex()),
Expr.var(outgoing.getValue().getIndex())));
for (Phi phi : target.getPhis()) {
for (Incoming outgoing : phi.getIncomings()) {
if (outgoing.getSource() == currentBlock) {
seq.getSequence().add(Statement.assign(Expr.var(outgoing.getPhi().getReceiver().getIndex()),
Expr.var(outgoing.getValue().getIndex())));
}
}
}
if (rawJump != null) {
seq.getSequence().add(rawJump);
@ -593,7 +596,7 @@ public class StatementGenerator implements InstructionVisitor {
}
private Statement generateJumpStatement(BasicBlock target) {
return wrapWithPhis(generateJumpStatementWithoutPhis(target));
return wrapWithPhis(generateJumpStatementWithoutPhis(target), target);
}
private Statement generateJumpStatementWithoutPhis(SwitchStatement stmt, int target) {
@ -607,7 +610,7 @@ public class StatementGenerator implements InstructionVisitor {
}
private Statement generateJumpStatement(SwitchStatement stmt, int target) {
return wrapWithPhis(generateJumpStatementWithoutPhis(stmt, target));
return wrapWithPhis(generateJumpStatementWithoutPhis(stmt, target), program.basicBlockAt(target));
}
private void branch(Expr condition, BasicBlock consequentBlock, BasicBlock alternativeBlock) {

View File

@ -49,6 +49,23 @@ public class CommonSubexpressionElimination implements MethodOptimization {
int v = stack[--top];
currentBlockIndex = v;
BasicBlock block = program.basicBlockAt(v);
for (int i = 0; i < block.getPhis().size(); ++i) {
Phi phi = block.getPhis().get(i);
int sharedValue = -2;
for (Incoming incoming : phi.getIncomings()) {
int value = map[incoming.getValue().getIndex()];
incoming.setValue(program.variableAt(value));
if (sharedValue != -2 && sharedValue != incoming.getValue().getIndex()) {
sharedValue = -1;
} else {
sharedValue = incoming.getValue().getIndex();
}
}
if (sharedValue != -1) {
map[phi.getReceiver().getIndex()] = sharedValue;
block.getPhis().remove(i--);
}
}
for (int i = 0; i < block.getInstructions().size(); ++i) {
Instruction currentInsn = block.getInstructions().get(i);
currentInsn.acceptVisitor(optimizer);