Merge pull request #6807 from ethereum/storageKnowledge

Knowledge about storage and memory.
This commit is contained in:
chriseth
2019-06-24 14:09:21 +02:00
committed by GitHub
28 changed files with 890 additions and 6 deletions
+16
View File
@@ -35,6 +35,7 @@
#include <libyul/optimiser/FullInliner.h>
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
#include <libyul/optimiser/ForLoopInitRewriter.h>
#include <libyul/optimiser/LoadResolver.h>
#include <libyul/optimiser/MainFunction.h>
#include <libyul/optimiser/NameDisplacer.h>
#include <libyul/optimiser/Rematerialiser.h>
@@ -252,6 +253,21 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
SSATransform::run(*m_ast, nameDispenser);
RedundantAssignEliminator::run(*m_dialect, *m_ast);
}
else if (m_optimizerStep == "loadResolver")
{
disambiguate();
ForLoopInitRewriter{}(*m_ast);
NameDispenser nameDispenser{*m_dialect, *m_ast};
ExpressionSplitter{*m_dialect, nameDispenser}(*m_ast);
CommonSubexpressionEliminator{*m_dialect}(*m_ast);
ExpressionSimplifier::run(*m_dialect, *m_ast);
LoadResolver::run(*m_dialect, *m_ast);
UnusedPruner::runUntilStabilised(*m_dialect, *m_ast);
ExpressionJoiner::run(*m_ast);
ExpressionJoiner::run(*m_ast);
}
else if (m_optimizerStep == "controlFlowSimplifier")
{
disambiguate();
@@ -0,0 +1,15 @@
{
sstore(4, 5)
sstore(4, 3)
sstore(8, sload(4))
}
// ====
// step: fullSuite
// ----
// {
// {
// sstore(4, 5)
// sstore(4, 3)
// sstore(8, sload(4))
// }
// }
@@ -0,0 +1,38 @@
{
mstore(2, 9)
sstore(0, mload(2))
pop(call(0, 0, 0, 0, 0, 0, 0))
sstore(0, mload(2))
mstore(2, 10)
mstore8(calldataload(0), 4)
sstore(0, mload(2))
mstore(2, 10)
g()
sstore(0, mload(2))
function g() {}
}
// ====
// step: loadResolver
// ----
// {
// let _1 := 9
// let _2 := 2
// mstore(_2, _1)
// let _4 := _1
// let _5 := 0
// sstore(_5, _4)
// pop(call(_5, _5, _5, _5, _5, _5, _5))
// sstore(_5, mload(_2))
// let _17 := 10
// mstore(_2, _17)
// mstore8(calldataload(_5), 4)
// sstore(_5, mload(_2))
// mstore(_2, _17)
// g()
// sstore(_5, mload(_2))
// function g()
// { }
// }
@@ -0,0 +1,17 @@
{
// No mload removal because of msize
mstore(calldataload(0), msize())
let t := mload(calldataload(10))
let q := mload(calldataload(0))
sstore(t, q)
}
// ====
// step: loadResolver
// ----
// {
// let _1 := msize()
// let _3 := calldataload(0)
// mstore(_3, _1)
// let t := mload(calldataload(10))
// sstore(t, mload(_3))
// }
@@ -0,0 +1,22 @@
{
mstore(calldataload(0), calldataload(10))
if calldataload(1) {
mstore(calldataload(0), 1)
}
let t := mload(0)
let q := mload(calldataload(0))
sstore(t, q)
}
// ====
// step: loadResolver
// ----
// {
// let _2 := calldataload(10)
// let _3 := 0
// let _4 := calldataload(_3)
// mstore(_4, _2)
// let _5 := 1
// if calldataload(_5) { mstore(_4, _5) }
// let t := mload(_3)
// sstore(t, mload(_4))
// }
@@ -0,0 +1,20 @@
{
mstore(calldataload(0), calldataload(10))
if calldataload(1) {
mstore(add(calldataload(0), 0x20), 1)
}
let t := mload(add(calldataload(0), 0x20))
let q := mload(calldataload(0))
sstore(t, q)
}
// ====
// step: loadResolver
// ----
// {
// let _2 := calldataload(10)
// let _4 := calldataload(0)
// mstore(_4, _2)
// let _5 := 1
// if calldataload(_5) { mstore(add(_4, 0x20), _5) }
// sstore(mload(add(_4, 0x20)), _2)
// }
@@ -0,0 +1,22 @@
{
mstore(calldataload(0), calldataload(10))
if calldataload(1) {
mstore(0, 1)
}
let t := mload(0)
let q := mload(calldataload(0))
sstore(t, q)
}
// ====
// step: loadResolver
// ----
// {
// let _2 := calldataload(10)
// let _3 := 0
// let _4 := calldataload(_3)
// mstore(_4, _2)
// let _5 := 1
// if calldataload(_5) { mstore(_3, _5) }
// let t := mload(_3)
// sstore(t, mload(_4))
// }
@@ -0,0 +1,16 @@
{
let a := calldataload(0)
sstore(a, 6)
a := calldataload(2)
mstore(0, sload(a))
}
// ====
// step: loadResolver
// ----
// {
// let _1 := 0
// let a := calldataload(_1)
// sstore(a, 6)
// a := calldataload(2)
// mstore(_1, sload(a))
// }
@@ -0,0 +1,32 @@
{
let x := calldataload(1)
let a := add(x, 10)
sstore(a, 7)
// This clears the expression assigned to ``a`` but
// should not clear storage knowledge
x := 9
mstore(sload(a), 11)
// This, on the other hand, actually clears knowledge
a := 33
mstore(sload(a), 11)
// Try again with different expression to avoid
// clearing because we cannot know if it is different
a := 39
mstore(sload(a), 11)
}
// ====
// step: loadResolver
// ----
// {
// let x := calldataload(1)
// let a := add(x, 10)
// let _3 := 7
// sstore(a, _3)
// x := 9
// let _4 := 11
// mstore(_3, _4)
// a := 33
// mstore(sload(a), _4)
// a := 39
// mstore(sload(a), _4)
// }
@@ -0,0 +1,24 @@
{
let x := calldataload(1)
let a := add(x, 10)
let b := add(x, 42)
mstore(a, 7)
// does not invalidate the first store, because the
// difference is larger than 32, even if the absolute
// values are unknown
mstore(b, 8)
sstore(mload(a), mload(b))
}
// ====
// step: loadResolver
// ----
// {
// let x := calldataload(1)
// let a := add(x, 10)
// let b := add(x, 42)
// let _4 := 7
// mstore(a, _4)
// let _5 := 8
// mstore(b, _5)
// sstore(_4, _5)
// }
@@ -0,0 +1,19 @@
{
let x := calldataload(1)
sstore(x, 7)
sstore(calldataload(0), 6)
// We cannot replace this because we do not know
// if the two slots are different.
mstore(0, sload(x))
}
// ====
// step: loadResolver
// ----
// {
// let x := calldataload(1)
// sstore(x, 7)
// let _3 := 6
// let _4 := 0
// sstore(calldataload(_4), _3)
// mstore(_4, sload(x))
// }
@@ -0,0 +1,19 @@
{
let x := calldataload(1)
sstore(x, 7)
sstore(calldataload(0), 7)
// We can replace this because both values that were
// written are 7.
mstore(0, sload(x))
}
// ====
// step: loadResolver
// ----
// {
// let x := calldataload(1)
// let _2 := 7
// sstore(x, _2)
// let _4 := 0
// sstore(calldataload(_4), _2)
// mstore(_4, _2)
// }
@@ -0,0 +1,24 @@
{
let x := calldataload(1)
let a := add(x, 10)
let b := add(x, 20)
sstore(a, 7)
// does not invalidate the first store, because the
// difference is a constant, even if the absolute
// values are unknown
sstore(b, 8)
mstore(sload(a), sload(b))
}
// ====
// step: loadResolver
// ----
// {
// let x := calldataload(1)
// let a := add(x, 10)
// let b := add(x, 20)
// let _4 := 7
// sstore(a, _4)
// let _5 := 8
// sstore(b, _5)
// mstore(_4, _5)
// }
@@ -0,0 +1,14 @@
{
sstore(calldataload(0), calldataload(10))
let t := sload(calldataload(10))
let q := sload(calldataload(0))
mstore(t, q)
}
// ====
// step: loadResolver
// ----
// {
// let _2 := calldataload(10)
// sstore(calldataload(0), _2)
// mstore(sload(_2), _2)
// }
@@ -0,0 +1,14 @@
{
mstore(calldataload(0), calldataload(10))
let t := mload(calldataload(10))
let q := mload(calldataload(0))
sstore(t, q)
}
// ====
// step: loadResolver
// ----
// {
// let _2 := calldataload(10)
// mstore(calldataload(0), _2)
// sstore(mload(_2), _2)
// }