Do not replace zeros by return variable.

This commit is contained in:
chriseth
2021-04-22 16:54:02 +02:00
parent cd13fcd758
commit 00fb2d390b
75 changed files with 150 additions and 130 deletions
@@ -29,6 +29,7 @@
#include <libyul/Exceptions.h>
#include <libyul/AST.h>
#include <libyul/Dialect.h>
#include <libyul/Utilities.h>
using namespace std;
using namespace solidity;
@@ -52,6 +53,16 @@ CommonSubexpressionEliminator::CommonSubexpressionEliminator(
{
}
void CommonSubexpressionEliminator::operator()(FunctionDefinition& _fun)
{
ScopedSaveAndRestore returnVariables(m_returnVariables, {});
for (auto const& v: _fun.returnVariables)
m_returnVariables.insert(v.name);
DataFlowAnalyzer::operator()(_fun);
}
void CommonSubexpressionEliminator::visit(Expression& _e)
{
bool descend = true;
@@ -82,10 +93,9 @@ void CommonSubexpressionEliminator::visit(Expression& _e)
if (descend)
DataFlowAnalyzer::visit(_e);
if (holds_alternative<Identifier>(_e))
if (Identifier const* identifier = get_if<Identifier>(&_e))
{
Identifier& identifier = std::get<Identifier>(_e);
YulString name = identifier.name;
YulString name = identifier->name;
if (m_value.count(name))
{
assertThrow(m_value.at(name).value, OptimizerException, "");
@@ -100,6 +110,14 @@ void CommonSubexpressionEliminator::visit(Expression& _e)
for (auto const& [variable, value]: m_value)
{
assertThrow(value.value, OptimizerException, "");
// Prevent using the default value of return variables
// instead of literal zeros.
if (
m_returnVariables.count(variable) &&
holds_alternative<Literal>(*value.value) &&
valueOfLiteral(get<Literal>(*value.value)) == 0
)
continue;
if (SyntacticallyEqual{}(_e, *value.value) && inScope(variable))
{
_e = Identifier{locationOf(_e), variable};
@@ -25,6 +25,8 @@
#include <libyul/optimiser/DataFlowAnalyzer.h>
#include <libyul/optimiser/OptimiserStep.h>
#include <stack>
namespace solidity::yul
{
@@ -43,6 +45,9 @@ public:
static constexpr char const* name{"CommonSubexpressionEliminator"};
static void run(OptimiserStepContext&, Block& _ast);
using DataFlowAnalyzer::operator();
void operator()(FunctionDefinition&) override;
private:
CommonSubexpressionEliminator(
Dialect const& _dialect,
@@ -52,6 +57,9 @@ private:
protected:
using ASTModifier::visit;
void visit(Expression& _e) override;
private:
std::set<YulString> m_returnVariables;
};
}