Skip to content

Commit

Permalink
Reduce redundant local variable reassignments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Washi1337 committed May 21, 2024
1 parent 824eb9f commit c38555a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Core/Echo.Ast/AstFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public void Visit(AssignmentStatement<TInstruction> statement, IndentedTextWrite
}

/// <inheritdoc />
public void Visit(ExpressionStatement<TInstruction> expression, IndentedTextWriter state)
public void Visit(ExpressionStatement<TInstruction> statement, IndentedTextWriter state)
{
expression.Expression.Accept(this, state);
statement.Expression.Accept(this, state);
state.Write(';');
}

Expand Down
6 changes: 5 additions & 1 deletion src/Core/Echo.Ast/Construction/ControlFlowGraphLifter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ private static void FlushStackAndPush(LiftedNode<TInstruction> node, Stack<Expre
{
var variables = FlushStackInternal(node, stack, static (n, value) =>
{
// Optimization: If we are simply reassigning variables, we can skip introducing a new variable (inlining).
if (value is VariableExpression<TInstruction> variableExpression)
return variableExpression.Variable;
var intermediate = n.DeclareStackIntermediate();
n.Transformed.Contents.Instructions.Add(Statement.Assignment(intermediate, value));
return intermediate;
Expand Down Expand Up @@ -245,7 +249,7 @@ private void FlushStackIfImpure(
private static IList<IVariable> FlushStackInternal(
LiftedNode<TInstruction> node,
Stack<Expression<TInstruction>> stack,
Func<LiftedNode<TInstruction>, Expression<TInstruction>, SyntheticVariable> flush)
Func<LiftedNode<TInstruction>, Expression<TInstruction>, IVariable> flush)
{
// Collect all values from the stack.
var values = new Expression<TInstruction>[stack.Count];
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Echo.Ast/IAstNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public interface IAstNodeVisitor<TInstruction, in TState>
/// <summary>
/// Visits a given <see cref="ExpressionStatement{TInstruction}"/>
/// </summary>
void Visit(ExpressionStatement<TInstruction> expression, TState state);
void Visit(ExpressionStatement<TInstruction> statement, TState state);

/// <summary>
/// Visits a given <see cref="PhiStatement{TInstruction}"/>
Expand Down

0 comments on commit c38555a

Please sign in to comment.