Skip to content
Browse files

OptmizeReturn in order to replace gotos to a single return with a dup…

…licate return for each branch
  • Loading branch information...
1 parent 4797344 commit 0cf43014897b3fe735c720edb74c82445a5ee96d @flaub committed Mar 15, 2010
Showing with 28 additions and 0 deletions.
  1. +28 −0 src/DotWeb.Decompiler/Core/ControlFlowGraph.cs
View
28 src/DotWeb.Decompiler/Core/ControlFlowGraph.cs
@@ -33,6 +33,7 @@ public ControlFlowGraph()
public void Structure() {
CompoundConditionals();
+ OptimizeReturn();
StructureLoops();
StructureConditionals();
StructureExceptions();
@@ -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.
Something went wrong with that request. Please try again.