From f95388011b287497c3983b9110b871ab8b366e47 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 25 Feb 2019 11:31:35 +0000 Subject: [PATCH] Allow simplification patterns to signal failure --- libevmasm/ExpressionClasses.cpp | 1 + libevmasm/SimplificationRule.h | 13 +++++++++++++ libevmasm/SimplificationRules.cpp | 4 +++- libyul/optimiser/SimplificationRules.cpp | 3 ++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/libevmasm/ExpressionClasses.cpp b/libevmasm/ExpressionClasses.cpp index abbbbc2c5..db60d15bf 100644 --- a/libevmasm/ExpressionClasses.cpp +++ b/libevmasm/ExpressionClasses.cpp @@ -203,6 +203,7 @@ ExpressionClasses::Id ExpressionClasses::tryToSimplify(Expression const& _expr) //cout << "with rule " << match->first.toString() << endl; //ExpressionTemplate t(match->second()); //cout << "to " << match->second().toString() << endl; + return rebuildExpression(ExpressionTemplate(match->action(), _expr.item->location())); } diff --git a/libevmasm/SimplificationRule.h b/libevmasm/SimplificationRule.h index 7b4dea687..fa1d50691 100644 --- a/libevmasm/SimplificationRule.h +++ b/libevmasm/SimplificationRule.h @@ -36,9 +36,22 @@ namespace solidity template struct SimplificationRule { + SimplificationRule( + Pattern _pattern, + std::function _action, + bool _removesNonConstants, + std::function _feasible = {} + ): + pattern(std::move(_pattern)), + action(std::move(_action)), + removesNonConstants(_removesNonConstants), + feasible(std::move(_feasible)) + {} + Pattern pattern; std::function action; bool removesNonConstants; + std::function feasible; }; } diff --git a/libevmasm/SimplificationRules.cpp b/libevmasm/SimplificationRules.cpp index b812cecc3..9fee79cf4 100644 --- a/libevmasm/SimplificationRules.cpp +++ b/libevmasm/SimplificationRules.cpp @@ -51,7 +51,9 @@ SimplificationRule const* Rules::findFirstMatch( for (auto const& rule: m_rules[uint8_t(_expr.item->instruction())]) { if (rule.pattern.matches(_expr, _classes)) - return &rule; + if (!rule.feasible || rule.feasible()) + return &rule; + resetMatchGroups(); } return nullptr; diff --git a/libyul/optimiser/SimplificationRules.cpp b/libyul/optimiser/SimplificationRules.cpp index 1b620b641..c70ed2061 100644 --- a/libyul/optimiser/SimplificationRules.cpp +++ b/libyul/optimiser/SimplificationRules.cpp @@ -51,7 +51,8 @@ SimplificationRule const* SimplificationRules::findFirstMatch( { rules.resetMatchGroups(); if (rule.pattern.matches(_expr, _dialect, _ssaValues)) - return &rule; + if (!rule.feasible || rule.feasible()) + return &rule; } return nullptr; }