Skip to content

Yul dataflow analysis for continue/break statements #6242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions libyul/optimiser/DataFlowAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,21 @@ void DataFlowAnalyzer::operator()(ForLoop& _for)
for (auto& statement: _for.pre.statements)
visit(statement);

AssignmentsSinceContinue assignmentsSinceCont;
assignmentsSinceCont(_for.body);

Assignments assignments;
assignments(_for.body);
assignments(_for.post);
clearValues(assignments.names());

visit(*_for.condition);
(*this)(_for.body);
clearValues(assignmentsSinceCont.names());
(*this)(_for.post);

clearValues(assignments.names());
popScope();
}

void DataFlowAnalyzer::operator()(Break&)
{
yulAssert(false, "Not implemented yet.");
}

void DataFlowAnalyzer::operator()(Continue&)
{
yulAssert(false, "Not implemented yet.");
popScope();
}

void DataFlowAnalyzer::operator()(Block& _block)
Expand Down
2 changes: 0 additions & 2 deletions libyul/optimiser/DataFlowAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class DataFlowAnalyzer: public ASTModifier
void operator()(Switch& _switch) override;
void operator()(FunctionDefinition&) override;
void operator()(ForLoop&) override;
void operator()(Break& _continue) override;
void operator()(Continue& _continue) override;
void operator()(Block& _block) override;

protected:
Expand Down
26 changes: 26 additions & 0 deletions libyul/optimiser/NameCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,29 @@ void Assignments::operator()(Assignment const& _assignment)
for (auto const& var: _assignment.variableNames)
m_names.emplace(var.name);
}


void AssignmentsSinceContinue::operator()(ForLoop const& _forLoop)
{
m_forLoopDepth++;
ASTWalker::operator()(_forLoop);
m_forLoopDepth--;
}

void AssignmentsSinceContinue::operator()(Continue const&)
{
if (m_forLoopDepth == 0)
m_continueFound = true;
}

void AssignmentsSinceContinue::operator()(Assignment const& _assignment)
{
if (m_continueFound)
for (auto const& var: _assignment.variableNames)
m_names.emplace(var.name);
}

void AssignmentsSinceContinue::operator()(FunctionDefinition const&)
{
yulAssert(false, "");
}
25 changes: 25 additions & 0 deletions libyul/optimiser/NameCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,29 @@ class Assignments: public ASTWalker
std::set<YulString> m_names;
};

/**
* Collects all names from a given continue statement on onwards.
*
* It makes only sense to be invoked from within a body of an outer for loop, that is,
* it will only collect all names from the beginning of the first continue statement
* of the outer-most ForLoop.
*/
class AssignmentsSinceContinue: public ASTWalker
{
public:
using ASTWalker::operator();
void operator()(ForLoop const& _forLoop) override;
void operator()(Continue const&) override;
void operator()(Assignment const& _assignment) override;
void operator()(FunctionDefinition const& _funDef) override;

std::set<YulString> const& names() const { return m_names; }
bool empty() const noexcept { return m_names.empty(); }

private:
size_t m_forLoopDepth = 0;
bool m_continueFound = false;
std::set<YulString> m_names;
};

}
38 changes: 38 additions & 0 deletions test/libyul/yulOptimizerTests/rematerialiser/for_break.yul
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
let a
let b
for {let i := 0} lt(i, 10) {i := add(a, b)} {
a := origin()
b := origin()
b := caller()
// a=origin, b=caller
if callvalue() { break }
// a=origin, b=caller
a := caller()
}
mstore(a, b)
}
// ----
// rematerialiser
// {
// let a
// let b
// for {
// let i := 0
// }
// lt(i, 10)
// {
// i := add(caller(), caller())
// }
// {
// a := origin()
// b := origin()
// b := caller()
// if callvalue()
// {
// break
// }
// a := caller()
// }
// mstore(a, b)
// }
40 changes: 40 additions & 0 deletions test/libyul/yulOptimizerTests/rematerialiser/for_continue.yul
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
let a
let b
for { let i := 0 }
lt(i, 10)
{ i := add(a, b) } // `b` is always known to be caller() but `a` may be origin() or caller().
{
a := origin()
b := origin()

b := caller()
if callvalue() { continue }
a := caller()
}
mstore(a, b)
}
// ----
// rematerialiser
// {
// let a
// let b
// for {
// let i := 0
// }
// lt(i, 10)
// {
// i := add(a, caller())
// }
// {
// a := origin()
// b := origin()
// b := caller()
// if callvalue()
// {
// continue
// }
// a := caller()
// }
// mstore(a, b)
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
let a
let b
let c
for {
let i := 0
b := origin()
c := origin()
}
lt(i, 10)
{
i := add(a, b)
b := callvalue()
c := caller()
}
{
a := origin()

b := caller()
if callvalue() { continue }
a := caller()
}
let x := b // does not rematerialize as b may be either origin() or callvalue() (btw: not caller())
let y := c // does not rematerialize as c may be either origin() or caller()
}
// ----
// rematerialiser
// {
// let a
// let b
// let c
// for {
// let i := 0
// b := origin()
// c := origin()
// }
// lt(i, 10)
// {
// i := add(a, caller())
// b := callvalue()
// c := caller()
// }
// {
// a := origin()
// b := caller()
// if callvalue()
// {
// continue
// }
// a := caller()
// }
// let x := b
// let y := c
// }