Skip to content

Commit

Permalink
OptmizeReturn in order to replace gotos to a single return with a dup…
Browse files Browse the repository at this point in the history
…licate return for each branch
  • Loading branch information
Frank Laub committed Mar 15, 2010
1 parent 4797344 commit 0cf4301
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/DotWeb.Decompiler/Core/ControlFlowGraph.cs
Expand Up @@ -33,6 +33,7 @@ public ControlFlowGraph()

public void Structure() {
CompoundConditionals();
OptimizeReturn();
StructureLoops();
StructureConditionals();
StructureExceptions();
Expand Down Expand Up @@ -329,5 +330,32 @@ public ControlFlowGraph()
}
}
}

public void OptimizeReturn() {
var returns = this.Nodes.Cast<BasicBlock>().Where(x => x.FlowControl == FlowControl.Return);
if (returns.Count() == 1) {
var exit = returns.Single();
if (exit.Statements.Count == 1) {
var returnStmt = exit.Statements.Single() as CodeReturnStatement;
if (returnStmt != null) {
var variableRef = returnStmt.Expression as CodeVariableReference;
if (variableRef != null) {
foreach (BasicBlock pred in exit.Predecessors) {
pred.Successors.Remove(exit);
var copy = new CodeReturnStatement {
Expression = returnStmt.Expression
};
pred.Statements.Add(copy);
if (pred.LastInstruction.OpCode.FlowControl == FlowControl.Branch) {
pred.Instructions.Remove(pred.LastInstruction);
}
pred.Instructions.AddRange(exit.Instructions);
}
this.Nodes.Remove(exit);
}
}
}
}
}
}
}

0 comments on commit 0cf4301

Please sign in to comment.