aboutsummaryrefslogtreecommitdiffstats
path: root/libjulia/optimiser/CommonSubexpressionEliminator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libjulia/optimiser/CommonSubexpressionEliminator.cpp')
-rw-r--r--libjulia/optimiser/CommonSubexpressionEliminator.cpp30
1 files changed, 26 insertions, 4 deletions
diff --git a/libjulia/optimiser/CommonSubexpressionEliminator.cpp b/libjulia/optimiser/CommonSubexpressionEliminator.cpp
index 3122280b..458b3437 100644
--- a/libjulia/optimiser/CommonSubexpressionEliminator.cpp
+++ b/libjulia/optimiser/CommonSubexpressionEliminator.cpp
@@ -33,9 +33,31 @@ using namespace dev::julia;
void CommonSubexpressionEliminator::visit(Expression& _e)
{
- // Single exception for substitution: We do not substitute one variable for another.
- if (_e.type() != typeid(Identifier))
- // TODO this search rather inefficient.
+ // We visit the inner expression first to first simplify inner expressions,
+ // which hopefully allows more matches.
+ // Note that the DataFlowAnalyzer itself only has code for visiting Statements,
+ // so this basically invokes the AST walker directly and thus post-visiting
+ // is also fine with regards to data flow analysis.
+ DataFlowAnalyzer::visit(_e);
+
+ if (_e.type() == typeid(Identifier))
+ {
+ Identifier& identifier = boost::get<Identifier>(_e);
+ string const& name = identifier.name;
+ if (m_value.count(name))
+ {
+ assertThrow(m_value.at(name), OptimizerException, "");
+ if (m_value.at(name)->type() == typeid(Identifier))
+ {
+ string const& value = boost::get<Identifier>(*m_value.at(name)).name;
+ if (inScope(value))
+ _e = Identifier{locationOf(_e), value};
+ }
+ }
+ }
+ else
+ {
+ // TODO this search is rather inefficient.
for (auto const& var: m_value)
{
assertThrow(var.second, OptimizerException, "");
@@ -45,5 +67,5 @@ void CommonSubexpressionEliminator::visit(Expression& _e)
break;
}
}
- DataFlowAnalyzer::visit(_e);
+ }
}