Merge remote-tracking branch 'origin/develop' into develop_060

This commit is contained in:
chriseth
2019-11-04 19:09:11 +01:00
56 changed files with 2540 additions and 1197 deletions
+4
View File
@@ -66,6 +66,10 @@ add_library(yul
optimiser/CallGraphGenerator.h
optimiser/CommonSubexpressionEliminator.cpp
optimiser/CommonSubexpressionEliminator.h
optimiser/ConditionalSimplifier.cpp
optimiser/ConditionalSimplifier.h
optimiser/ConditionalUnsimplifier.cpp
optimiser/ConditionalUnsimplifier.h
optimiser/ControlFlowSimplifier.cpp
optimiser/ControlFlowSimplifier.h
optimiser/DataFlowAnalyzer.cpp
+113 -55
View File
@@ -48,7 +48,7 @@ namespace
{
static string const polyfill{R"({
function or_bool(a, b, c, d) -> r {
r := i64.ne(0, i64.or(i64.or(a, b), i64.or(c, d)))
r := i64.or(i64.or(a, b), i64.or(c, d))
}
// returns a + y + c plus carry value on 64 bit values.
// c should be at most 1
@@ -205,9 +205,7 @@ function cmp(a, b) -> r {
switch i64.lt_u(a, b)
case 1 { r := 0xffffffffffffffff }
default {
switch i64.gt_u(a, b)
case 1 { r := 1 }
default { r := 0 }
r := i64.ne(a, b)
}
}
@@ -218,10 +216,7 @@ function lt(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
case 0 {
switch cmp(x3, y3)
case 0 {
switch cmp(x4, y4)
case 0 { z4 := 0 }
case 1 { z4 := 0 }
default { z4 := 1 }
z4 := i64.lt_u(x4, y4)
}
case 1 { z4 := 0 }
default { z4 := 1 }
@@ -245,13 +240,78 @@ function sgt(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
z1, z2, z3, z4 := slt(y1, y2, y3, y4, x1, x2, x3, x4)
}
function shl(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
// TODO implement
unreachable()
function shl_single(a, amount) -> x, y {
// amount < 64
x := i64.shr_u(a, i64.sub(64, amount))
y := i64.shl(a, amount)
}
function shl(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
if i64.and(i64.eqz(x1), i64.eqz(x2)) {
if i64.eqz(x3) {
if i64.lt_u(x4, 256) {
if i64.ge_u(x4, 128) {
y1 := y3
y2 := y4
y3 := 0
y4 := 0
x4 := i64.sub(x4, 128)
}
if i64.ge_u(x4, 64) {
y1 := y2
y2 := y3
y3 := y4
y4 := 0
x4 := i64.sub(x4, 64)
}
let t, r
t, z4 := shl_single(y4, x4)
r, z3 := shl_single(y3, x4)
z3 := i64.or(z3, t)
t, z2 := shl_single(y2, x4)
z2 := i64.or(z2, r)
r, z1 := shl_single(y1, x4)
z1 := i64.or(z1, t)
}
}
}
}
function shr_single(a, amount) -> x, y {
// amount < 64
y := i64.shl(a, i64.sub(64, amount))
x := i64.shr_u(a, amount)
}
function shr(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
// TODO implement
unreachable()
if i64.and(i64.eqz(x1), i64.eqz(x2)) {
if i64.eqz(x3) {
if i64.lt_u(x4, 256) {
if i64.ge_u(x4, 128) {
y4 := y2
y3 := y1
y2 := 0
y1 := 0
x4 := i64.sub(x4, 128)
}
if i64.ge_u(x4, 64) {
y4 := y3
y3 := y2
y2 := y1
y1 := 0
x4 := i64.sub(x4, 64)
}
let t
z4, t := shr_single(y4, x4)
z3, t := shr_single(y3, x4)
z4 := i64.or(z4, t)
z2, t := shr_single(y2, x4)
z3 := i64.or(z3, t)
z1, t := shr_single(y1, x4)
z2 := i64.or(z2, t)
}
}
}
}
function sar(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
// TODO implement
@@ -291,45 +351,36 @@ function keccak256(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
}
function address() -> z1, z2, z3, z4 {
let t1, t2, t3, t4 := save_temp_mem_32()
eth.getAddress(0)
z1, z2, z3, z4 := mload(0, 0, 0, 0)
restore_temp_mem_32(t1, t2, t3, t4)
z1, z2, z3, z4 := mload_internal(0)
}
function balance(x1, x2, x3, x4) -> z1, z2, z3, z4 {
// TODO implement
unreachable()
}
function origin() -> z1, z2, z3, z4 {
let t1, t2, t3, t4 := save_temp_mem_32()
eth.getTxOrigin(0)
z1, z2, z3, z4 := mload(0, 0, 0, 0)
restore_temp_mem_32(t1, t2, t3, t4)
z1, z2, z3, z4 := mload_internal(0)
}
function caller() -> z1, z2, z3, z4 {
let t1, t2, t3, t4 := save_temp_mem_32()
eth.getCaller(0)
z1, z2, z3, z4 := mload(0, 0, 0, 0)
restore_temp_mem_32(t1, t2, t3, t4)
z1, z2, z3, z4 := mload_internal(0)
}
function callvalue() -> z1, z2, z3, z4 {
let t1, t2, t3, t4 := save_temp_mem_32()
eth.getCallValue(0)
z1, z2, z3, z4 := mload(0, 0, 0, 0)
restore_temp_mem_32(t1, t2, t3, t4)
z1, z2, z3, z4 := mload_internal(0)
}
function calldataload(x1, x2, x3, x4) -> z1, z2, z3, z4 {
let t1, t2, t3, t4 := save_temp_mem_32()
eth.callDataCopy(0, u256_to_i32(x1, x2, x3, x4), 32)
z1, z2, z3, z4 := mload(0, 0, 0, 0)
restore_temp_mem_32(t1, t2, t3, t4)
z1, z2, z3, z4 := mload_internal(0)
}
function calldatasize() -> z1, z2, z3, z4 {
z4 := eth.getCallDataSize()
}
function calldatacopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {
eth.callDataCopy(
u256_to_i32ptr(x1, x2, x3, x4),
// scratch - TODO: overflow check
i64.add(u256_to_i32ptr(x1, x2, x3, x4), 64),
u256_to_i32(y1, y2, y3, y4),
u256_to_i32(z1, z2, z3, z4)
)
@@ -337,14 +388,13 @@ function calldatacopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {
// Needed?
function codesize() -> z1, z2, z3, z4 {
let t1, t2, t3, t4 := save_temp_mem_32()
eth.getCodeSize(0)
z1, z2, z3, z4 := mload(0, 0, 0, 0)
restore_temp_mem_32(t1, t2, t3, t4)
z1, z2, z3, z4 := mload_internal(0)
}
function codecopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {
eth.codeCopy(
u256_to_i32ptr(x1, x2, x3, x4),
// scratch - TODO: overflow check
i64.add(u256_to_i32ptr(x1, x2, x3, x4), 64),
u256_to_i32(y1, y2, y3, y4),
u256_to_i32(z1, z2, z3, z4)
)
@@ -355,10 +405,8 @@ function datacopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {
}
function gasprice() -> z1, z2, z3, z4 {
let t1, t2, t3, t4 := save_temp_mem_32()
eth.getTxGasPrice(0)
z1, z2, z3, z4 := mload(0, 0, 0, 0)
restore_temp_mem_32(t1, t2, t3, t4)
z1, z2, z3, z4 := mload_internal(0)
}
function extcodesize(x1, x2, x3, x4) -> z1, z2, z3, z4 {
// TODO implement
@@ -378,7 +426,8 @@ function returndatasize() -> z1, z2, z3, z4 {
}
function returndatacopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {
eth.returnDataCopy(
u256_to_i32ptr(x1, x2, x3, x4),
// scratch - TODO: overflow check
i64.add(u256_to_i32ptr(x1, x2, x3, x4), 64),
u256_to_i32(y1, y2, y3, y4),
u256_to_i32(z1, z2, z3, z4)
)
@@ -399,10 +448,8 @@ function number() -> z1, z2, z3, z4 {
z4 := eth.getBlockNumber()
}
function difficulty() -> z1, z2, z3, z4 {
let t1, t2, t3, t4 := save_temp_mem_32()
eth.getBlockDifficulty(0)
z1, z2, z3, z4 := mload(0, 0, 0, 0)
restore_temp_mem_32(t1, t2, t3, t4)
z1, z2, z3, z4 := mload_internal(0)
}
function gaslimit() -> z1, z2, z3, z4 {
z4 := eth.getBlockGasLimit()
@@ -463,6 +510,12 @@ function restore_temp_mem_64(t1, t2, t3, t4, t5, t6, t7, t8) {
}
function mload(x1, x2, x3, x4) -> z1, z2, z3, z4 {
let pos := u256_to_i32ptr(x1, x2, x3, x4)
// Make room for the scratch space
// TODO do we need to check for overflow?
pos := i64.add(pos, 64)
z1, z2, z3, z4 := mload_internal(pos)
}
function mload_internal(pos) -> z1, z2, z3, z4 {
z1 := endian_swap(i64.load(pos))
z2 := endian_swap(i64.load(i64.add(pos, 8)))
z3 := endian_swap(i64.load(i64.add(pos, 16)))
@@ -470,10 +523,16 @@ function mload(x1, x2, x3, x4) -> z1, z2, z3, z4 {
}
function mstore(x1, x2, x3, x4, y1, y2, y3, y4) {
let pos := u256_to_i32ptr(x1, x2, x3, x4)
i64.store(pos, endian_swap(x1))
i64.store(i64.add(pos, 8), endian_swap(x2))
i64.store(i64.add(pos, 16), endian_swap(x3))
i64.store(i64.add(pos, 24), endian_swap(x4))
// Make room for the scratch space
// TODO do we need to check for overflow?
pos := i64.add(pos, 64)
mstore_internal(pos, y1, y2, y3, y4)
}
function mstore_internal(pos, y1, y2, y3, y4) {
i64.store(pos, endian_swap(y1))
i64.store(i64.add(pos, 8), endian_swap(y2))
i64.store(i64.add(pos, 16), endian_swap(y3))
i64.store(i64.add(pos, 24), endian_swap(y4))
}
function mstore8(x1, x2, x3, x4, y1, y2, y3, y4) {
// TODO implement
@@ -485,19 +544,15 @@ function msize() -> z1, z2, z3, z4 {
unreachable()
}
function sload(x1, x2, x3, x4) -> z1, z2, z3, z4 {
let t1, t2, t3, t4, t5, t6, t7, t8 := save_temp_mem_64()
mstore(0, 0, 0, 0, x1, x2, x3, x4)
eth.storageLoad(0, 16)
z1, z2, z3, z4 := mload(0, 0, 0, 16)
restore_temp_mem_64(t1, t2, t3, t4, t5, t6, t7, t8)
mstore_internal(0, x1, x2, x3, x4)
eth.storageLoad(0, 32)
z1, z2, z3, z4 := mload_internal(32)
}
function sstore(x1, x2, x3, x4, y1, y2, y3, y4) {
let t1, t2, t3, t4, t5, t6, t7, t8 := save_temp_mem_64()
mstore(0, 0, 0, 0, x1, x2, x3, x4)
mstore(0, 0, 0, 32, y1, y2, y3, y4)
mstore_internal(0, x1, x2, x3, x4)
mstore_internal(32, y1, y2, y3, y4)
eth.storageStore(0, 32)
restore_temp_mem_64(t1, t2, t3, t4, t5, t6, t7, t8)
}
// Needed?
@@ -615,13 +670,15 @@ function selfdestruct(a1, a2, a3, a4) {
function return(x1, x2, x3, x4, y1, y2, y3, y4) {
eth.finish(
u256_to_i32ptr(x1, x2, x3, x4),
// scratch - TODO: overflow check
i64.add(u256_to_i32ptr(x1, x2, x3, x4), 64),
u256_to_i32(y1, y2, y3, y4)
)
}
function revert(x1, x2, x3, x4, y1, y2, y3, y4) {
eth.revert(
u256_to_i32ptr(x1, x2, x3, x4),
// scratch - TODO: overflow check
i64.add(u256_to_i32ptr(x1, x2, x3, x4), 64),
u256_to_i32(y1, y2, y3, y4)
)
}
@@ -653,6 +710,7 @@ Object EVMToEWasmTranslator::run(Object const& _object)
ast.statements.emplace_back(ASTCopier{}.translate(st));
Object ret;
ret.name = _object.name;
ret.code = make_shared<Block>(move(ast));
ret.analysisInfo = make_shared<AsmAnalysisInfo>();
+67 -3
View File
@@ -121,6 +121,8 @@ wasm::Expression EWasmCodeTransform::operator()(FunctionalInstruction const& _f)
wasm::Expression EWasmCodeTransform::operator()(FunctionCall const& _call)
{
bool typeConversionNeeded = false;
if (BuiltinFunction const* builtin = m_dialect.builtin(_call.functionName.name))
{
if (_call.functionName.name.str().substr(0, 4) == "eth.")
@@ -140,6 +142,7 @@ wasm::Expression EWasmCodeTransform::operator()(FunctionCall const& _call)
imp.paramTypes.emplace_back(param.str());
m_functionsToImport[builtin->name] = std::move(imp);
}
typeConversionNeeded = true;
}
else if (builtin->literalArguments)
{
@@ -149,14 +152,32 @@ wasm::Expression EWasmCodeTransform::operator()(FunctionCall const& _call)
return wasm::BuiltinCall{_call.functionName.name.str(), std::move(literals)};
}
else
return wasm::BuiltinCall{_call.functionName.name.str(), visit(_call.arguments)};
{
wasm::BuiltinCall call{
_call.functionName.name.str(),
injectTypeConversionIfNeeded(visit(_call.arguments), builtin->parameters)
};
if (!builtin->returns.empty() && !builtin->returns.front().empty() && builtin->returns.front() != "i64"_yulstring)
{
yulAssert(builtin->returns.front() == "i32"_yulstring, "Invalid type " + builtin->returns.front().str());
call = wasm::BuiltinCall{"i64.extend_i32_u", make_vector<wasm::Expression>(std::move(call))};
}
return {std::move(call)};
}
}
// If this function returns multiple values, then the first one will
// be returned in the expression itself and the others in global variables.
// The values have to be used right away in an assignment or variable declaration,
// so it is handled there.
return wasm::FunctionCall{_call.functionName.name.str(), visit(_call.arguments)};
wasm::FunctionCall funCall{_call.functionName.name.str(), visit(_call.arguments)};
if (typeConversionNeeded)
// Inject type conversion if needed on the fly. This is just a temporary measure
// and can be removed once we have proper types in Yul.
return injectTypeConversionIfNeeded(std::move(funCall));
else
return {std::move(funCall)};
}
wasm::Expression EWasmCodeTransform::operator()(Identifier const& _identifier)
@@ -173,7 +194,16 @@ wasm::Expression EWasmCodeTransform::operator()(Literal const& _literal)
wasm::Expression EWasmCodeTransform::operator()(If const& _if)
{
return wasm::If{visit(*_if.condition), visit(_if.body.statements), {}};
// TODO converting i64 to i32 might not always be needed.
vector<wasm::Expression> args;
args.emplace_back(visitReturnByValue(*_if.condition));
args.emplace_back(wasm::Literal{0});
return wasm::If{
make_unique<wasm::Expression>(wasm::BuiltinCall{"i64.ne", std::move(args)}),
visit(_if.body.statements),
{}
};
}
wasm::Expression EWasmCodeTransform::operator()(Switch const& _switch)
@@ -323,6 +353,40 @@ wasm::FunctionDefinition EWasmCodeTransform::translateFunction(yul::FunctionDefi
return fun;
}
wasm::Expression EWasmCodeTransform::injectTypeConversionIfNeeded(wasm::FunctionCall _call) const
{
wasm::FunctionImport const& import = m_functionsToImport.at(YulString{_call.functionName});
for (size_t i = 0; i < _call.arguments.size(); ++i)
if (import.paramTypes.at(i) == "i32")
_call.arguments[i] = wasm::BuiltinCall{"i32.wrap_i64", make_vector<wasm::Expression>(std::move(_call.arguments[i]))};
else
yulAssert(import.paramTypes.at(i) == "i64", "Unknown type " + import.paramTypes.at(i));
if (import.returnType && *import.returnType != "i64")
{
yulAssert(*import.returnType == "i32", "Invalid type " + *import.returnType);
return wasm::BuiltinCall{"i64.extend_i32_u", make_vector<wasm::Expression>(std::move(_call))};
}
return {std::move(_call)};
}
vector<wasm::Expression> EWasmCodeTransform::injectTypeConversionIfNeeded(
vector<wasm::Expression> _arguments,
vector<Type> const& _parameterTypes
) const
{
for (size_t i = 0; i < _arguments.size(); ++i)
if (_parameterTypes.at(i) == "i32"_yulstring)
_arguments[i] = wasm::BuiltinCall{"i32.wrap_i64", make_vector<wasm::Expression>(std::move(_arguments[i]))};
else
yulAssert(
_parameterTypes.at(i).empty() || _parameterTypes.at(i) == "i64"_yulstring,
"Unknown type " + _parameterTypes.at(i).str()
);
return _arguments;
}
string EWasmCodeTransform::newLabel()
{
return m_nameDispenser.newName("label_"_yulstring).str();
@@ -80,6 +80,12 @@ private:
wasm::FunctionDefinition translateFunction(yul::FunctionDefinition const& _funDef);
wasm::Expression injectTypeConversionIfNeeded(wasm::FunctionCall _call) const;
std::vector<wasm::Expression> injectTypeConversionIfNeeded(
std::vector<wasm::Expression> _arguments,
std::vector<yul::Type> const& _parameterTypes
) const;
std::string newLabel();
/// Makes sure that there are at least @a _amount global variables.
void allocateGlobals(size_t _amount);
+4 -4
View File
@@ -74,12 +74,12 @@ string EWasmToText::operator()(wasm::StringLiteral const& _literal)
string EWasmToText::operator()(wasm::LocalVariable const& _identifier)
{
return "(get_local $" + _identifier.name + ")";
return "(local.get $" + _identifier.name + ")";
}
string EWasmToText::operator()(wasm::GlobalVariable const& _identifier)
{
return "(get_global $" + _identifier.name + ")";
return "(global.get $" + _identifier.name + ")";
}
string EWasmToText::operator()(wasm::BuiltinCall const& _builtinCall)
@@ -96,12 +96,12 @@ string EWasmToText::operator()(wasm::FunctionCall const& _functionCall)
string EWasmToText::operator()(wasm::LocalAssignment const& _assignment)
{
return "(set_local $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n";
return "(local.set $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n";
}
string EWasmToText::operator()(wasm::GlobalAssignment const& _assignment)
{
return "(set_global $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n";
return "(global.set $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n";
}
string EWasmToText::operator()(wasm::If const& _if)
+12 -2
View File
@@ -46,12 +46,22 @@ WasmDialect::WasmDialect():
})
addFunction(name, 2, 1);
m_functions["i64.lt_u"_yulstring].returns.front() = "i32"_yulstring;
m_functions["i64.gt_u"_yulstring].returns.front() = "i32"_yulstring;
m_functions["i64.le_u"_yulstring].returns.front() = "i32"_yulstring;
m_functions["i64.ge_u"_yulstring].returns.front() = "i32"_yulstring;
m_functions["i64.eq"_yulstring].returns.front() = "i32"_yulstring;
m_functions["i64.ne"_yulstring].returns.front() = "i32"_yulstring;
addFunction("i64.eqz", 1, 1);
m_functions["i64.eqz"_yulstring].returns.front() = "i32"_yulstring;
addFunction("i64.store", 2, 0, false);
m_functions["i64.store"_yulstring].parameters.front() = "i32"_yulstring;
m_functions["i64.store"_yulstring].sideEffects.invalidatesStorage = false;
addFunction("i64.load", 1, 1, false);
m_functions["i64.load"_yulstring].parameters.front() = "i32"_yulstring;
m_functions["i64.load"_yulstring].sideEffects.invalidatesStorage = false;
m_functions["i64.load"_yulstring].sideEffects.invalidatesMemory = false;
m_functions["i64.load"_yulstring].sideEffects.sideEffectFree = true;
@@ -63,8 +73,8 @@ WasmDialect::WasmDialect():
m_functions["unreachable"_yulstring].sideEffects.invalidatesStorage = false;
m_functions["unreachable"_yulstring].sideEffects.invalidatesMemory = false;
addFunction("datasize", 1, 4, true, true);
addFunction("dataoffset", 1, 4, true, true);
addFunction("datasize", 1, 1, true, true);
addFunction("dataoffset", 1, 1, true, true);
addEthereumExternals();
}
@@ -87,6 +87,30 @@ void WordSizeTransform::operator()(Block& _block)
if (_s.type() == typeid(VariableDeclaration))
{
VariableDeclaration& varDecl = boost::get<VariableDeclaration>(_s);
// Special handling for datasize and dataoffset - they will only need one variable.
if (varDecl.value && varDecl.value->type() == typeid(FunctionCall))
if (BuiltinFunction const* f = m_inputDialect.builtin(boost::get<FunctionCall>(*varDecl.value).functionName.name))
if (f->literalArguments)
{
yulAssert(f->name == "datasize"_yulstring || f->name == "dataoffset"_yulstring, "");
yulAssert(varDecl.variables.size() == 1, "");
auto newLhs = generateU64IdentifierNames(varDecl.variables[0].name);
vector<Statement> ret;
for (int i = 0; i < 3; i++)
ret.push_back(VariableDeclaration{
varDecl.location,
{TypedName{varDecl.location, newLhs[i], "u64"_yulstring}},
make_unique<Expression>(Literal{locationOf(*varDecl.value), LiteralKind::Number, "0"_yulstring, "u64"_yulstring})
});
ret.push_back(VariableDeclaration{
varDecl.location,
{TypedName{varDecl.location, newLhs[3], "u64"_yulstring}},
std::move(varDecl.value)
});
return {std::move(ret)};
}
if (
!varDecl.value ||
varDecl.value->type() == typeid(FunctionalInstruction) ||
@@ -123,6 +147,30 @@ void WordSizeTransform::operator()(Block& _block)
{
Assignment& assignment = boost::get<Assignment>(_s);
yulAssert(assignment.value, "");
// Special handling for datasize and dataoffset - they will only need one variable.
if (assignment.value->type() == typeid(FunctionCall))
if (BuiltinFunction const* f = m_inputDialect.builtin(boost::get<FunctionCall>(*assignment.value).functionName.name))
if (f->literalArguments)
{
yulAssert(f->name == "datasize"_yulstring || f->name == "dataoffset"_yulstring, "");
yulAssert(assignment.variableNames.size() == 1, "");
auto newLhs = generateU64IdentifierNames(assignment.variableNames[0].name);
vector<Statement> ret;
for (int i = 0; i < 3; i++)
ret.push_back(Assignment{
assignment.location,
{Identifier{assignment.location, newLhs[i]}},
make_unique<Expression>(Literal{locationOf(*assignment.value), LiteralKind::Number, "0"_yulstring, "u64"_yulstring})
});
ret.push_back(Assignment{
assignment.location,
{Identifier{assignment.location, newLhs[3]}},
std::move(assignment.value)
});
return {std::move(ret)};
}
if (
assignment.value->type() == typeid(FunctionalInstruction) ||
assignment.value->type() == typeid(FunctionCall)
@@ -0,0 +1,92 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libyul/optimiser/ConditionalSimplifier.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/AsmData.h>
#include <libyul/Utilities.h>
#include <libyul/optimiser/NameCollector.h>
#include <libdevcore/CommonData.h>
#include <libdevcore/Visitor.h>
using namespace std;
using namespace dev;
using namespace yul;
void ConditionalSimplifier::operator()(Switch& _switch)
{
visit(*_switch.expression);
if (_switch.expression->type() != typeid(Identifier))
{
ASTModifier::operator()(_switch);
return;
}
YulString expr = boost::get<Identifier>(*_switch.expression).name;
for (auto& _case: _switch.cases)
{
if (_case.value)
{
(*this)(*_case.value);
_case.body.statements.insert(_case.body.statements.begin(),
Assignment{
_case.body.location,
{Identifier{_case.body.location, expr}},
make_unique<Expression>(*_case.value)
}
);
}
(*this)(_case.body);
}
}
void ConditionalSimplifier::operator()(Block& _block)
{
iterateReplacing(
_block.statements,
[&](Statement& _s) -> std::optional<vector<Statement>>
{
visit(_s);
if (_s.type() == typeid(If))
{
If& _if = boost::get<If>(_s);
if (
_if.condition->type() == typeid(Identifier) &&
!_if.body.statements.empty() &&
TerminationFinder(m_dialect).controlFlowKind(_if.body.statements.back()) !=
TerminationFinder::ControlFlow::FlowOut
)
{
YulString condition = boost::get<Identifier>(*_if.condition).name;
langutil::SourceLocation location = _if.location;
return make_vector<Statement>(
std::move(_s),
Assignment{
location,
{Identifier{location, condition}},
make_unique<Expression>(Literal{
location,
LiteralKind::Number,
"0"_yulstring,
{}
})
}
);
}
}
return {};
}
);
}
+71
View File
@@ -0,0 +1,71 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/optimiser/OptimiserStep.h>
#include <libyul/Dialect.h>
#include <libdevcore/Common.h>
namespace yul
{
/**
* Conditional simplifier.
*
* Inserts assignments to condition variables if the value can be determined
* from the control-flow.
*
* Destroys SSA form.
*
* Currently, this tool is very limited, mostly because we do not yet have support
* for boolean types. Since conditions only check for expressions being nonzero,
* we cannot assign a specific value.
*
* Current features:
* - switch cases: insert "<condition> := <caseLabel>"
* - after if statement with terminating control-flow, insert "<condition> := 0"
*
* Future features:
* - allow replacements by "1"
* - take termination of user-defined functions into account
*
* Works best with SSA form and if dead code removal has run before.
*
* Prerequisite: Disambiguator.
*/
class ConditionalSimplifier: public ASTModifier
{
public:
static constexpr char const* name{"ConditionalSimplifier"};
static void run(OptimiserStepContext& _context, Block& _ast)
{
ConditionalSimplifier{_context.dialect}(_ast);
}
using ASTModifier::operator();
void operator()(Switch& _switch) override;
void operator()(Block& _block) override;
private:
explicit ConditionalSimplifier(Dialect const& _dialect):
m_dialect(_dialect)
{}
Dialect const& m_dialect;
};
}
@@ -0,0 +1,98 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libyul/optimiser/ConditionalUnsimplifier.h>
#include <libyul/optimiser/Semantics.h>
#include <libyul/AsmData.h>
#include <libyul/Utilities.h>
#include <libyul/optimiser/NameCollector.h>
#include <libdevcore/CommonData.h>
#include <libdevcore/Visitor.h>
using namespace std;
using namespace dev;
using namespace yul;
void ConditionalUnsimplifier::operator()(Switch& _switch)
{
visit(*_switch.expression);
if (_switch.expression->type() != typeid(Identifier))
{
ASTModifier::operator()(_switch);
return;
}
YulString expr = boost::get<Identifier>(*_switch.expression).name;
for (auto& _case: _switch.cases)
{
if (_case.value)
{
(*this)(*_case.value);
if (
!_case.body.statements.empty() &&
_case.body.statements.front().type() == typeid(Assignment)
)
{
Assignment const& assignment = boost::get<Assignment>(_case.body.statements.front());
if (
assignment.variableNames.size() == 1 &&
assignment.variableNames.front().name == expr &&
assignment.value->type() == typeid(Literal) &&
valueOfLiteral(boost::get<Literal>(*assignment.value)) == valueOfLiteral(*_case.value)
)
_case.body.statements.erase(_case.body.statements.begin());
}
}
(*this)(_case.body);
}
}
void ConditionalUnsimplifier::operator()(Block& _block)
{
walkVector(_block.statements);
iterateReplacingWindow<2>(
_block.statements,
[&](Statement& _stmt1, Statement& _stmt2) -> std::optional<vector<Statement>>
{
if (_stmt1.type() == typeid(If))
{
If& _if = boost::get<If>(_stmt1);
if (
_if.condition->type() == typeid(Identifier) &&
!_if.body.statements.empty()
)
{
YulString condition = boost::get<Identifier>(*_if.condition).name;
if (
_stmt2.type() == typeid(Assignment) &&
TerminationFinder(m_dialect).controlFlowKind(_if.body.statements.back()) !=
TerminationFinder::ControlFlow::FlowOut
)
{
Assignment const& assignment = boost::get<Assignment>(_stmt2);
if (
assignment.variableNames.size() == 1 &&
assignment.variableNames.front().name == condition &&
assignment.value->type() == typeid(Literal) &&
valueOfLiteral(boost::get<Literal>(*assignment.value)) == 0
)
return {make_vector<Statement>(std::move(_stmt1))};
}
}
}
return {};
}
);
}
@@ -0,0 +1,51 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/optimiser/OptimiserStep.h>
#include <libyul/Dialect.h>
#include <libdevcore/Common.h>
namespace yul
{
/**
* Reverse of conditional simplifier.
*
*/
class ConditionalUnsimplifier: public ASTModifier
{
public:
static constexpr char const* name{"ConditionalUnsimplifier"};
static void run(OptimiserStepContext& _context, Block& _ast)
{
ConditionalUnsimplifier{_context.dialect}(_ast);
}
using ASTModifier::operator();
void operator()(Switch& _switch) override;
void operator()(Block& _block) override;
private:
explicit ConditionalUnsimplifier(Dialect const& _dialect):
m_dialect(_dialect)
{}
Dialect const& m_dialect;
};
}
@@ -31,7 +31,11 @@ void ForLoopConditionIntoBody::run(OptimiserStepContext& _context, Block& _ast)
void ForLoopConditionIntoBody::operator()(ForLoop& _forLoop)
{
if (m_dialect.booleanNegationFunction() && _forLoop.condition->type() != typeid(Literal))
if (
m_dialect.booleanNegationFunction() &&
_forLoop.condition->type() != typeid(Literal) &&
_forLoop.condition->type() != typeid(Identifier)
)
{
langutil::SourceLocation const loc = locationOf(*_forLoop.condition);
+16 -1
View File
@@ -25,6 +25,8 @@
#include <libyul/optimiser/BlockFlattener.h>
#include <libyul/optimiser/CallGraphGenerator.h>
#include <libyul/optimiser/ControlFlowSimplifier.h>
#include <libyul/optimiser/ConditionalSimplifier.h>
#include <libyul/optimiser/ConditionalUnsimplifier.h>
#include <libyul/optimiser/DeadCodeEliminator.h>
#include <libyul/optimiser/FunctionGrouper.h>
#include <libyul/optimiser/FunctionHoister.h>
@@ -36,6 +38,7 @@
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
#include <libyul/optimiser/ForLoopInitRewriter.h>
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
#include <libyul/optimiser/Rematerialiser.h>
#include <libyul/optimiser/UnusedPruner.h>
#include <libyul/optimiser/ExpressionSimplifier.h>
@@ -98,6 +101,7 @@ void OptimiserSuite::run(
BlockFlattener::name,
ControlFlowSimplifier::name,
LiteralRematerialiser::name,
ConditionalUnsimplifier::name,
StructuralSimplifier::name,
ControlFlowSimplifier::name,
ForLoopConditionIntoBody::name,
@@ -130,8 +134,13 @@ void OptimiserSuite::run(
}
{
// still in SSA, perform structural simplification
// perform structural simplification
suite.runSequence({
CommonSubexpressionEliminator::name,
ConditionalSimplifier::name,
LiteralRematerialiser::name,
ConditionalUnsimplifier::name,
StructuralSimplifier::name,
LiteralRematerialiser::name,
ForLoopConditionOutOfBody::name,
ControlFlowSimplifier::name,
@@ -200,6 +209,10 @@ void OptimiserSuite::run(
{
// SSA plus simplify
suite.runSequence({
ConditionalSimplifier::name,
LiteralRematerialiser::name,
ConditionalUnsimplifier::name,
CommonSubexpressionEliminator::name,
SSATransform::name,
RedundantAssignEliminator::name,
RedundantAssignEliminator::name,
@@ -315,6 +328,8 @@ map<string, unique_ptr<OptimiserStep>> const& OptimiserSuite::allSteps()
instance = optimiserStepCollection<
BlockFlattener,
CommonSubexpressionEliminator,
ConditionalSimplifier,
ConditionalUnsimplifier,
ControlFlowSimplifier,
DeadCodeEliminator,
EquivalentFunctionCombiner,