solidity/libyul/optimiser/KnowledgeBase.cpp

109 lines
3.4 KiB
C++
Raw Normal View History

2019-05-21 13:52:15 +00:00
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2019-05-21 13:52:15 +00:00
/**
* Class that can answer questions about values of variables and their relations.
*/
#include <libyul/optimiser/KnowledgeBase.h>
#include <libyul/AST.h>
2019-05-21 16:38:05 +00:00
#include <libyul/Utilities.h>
#include <libyul/optimiser/SimplificationRules.h>
2019-11-28 13:22:17 +00:00
#include <libyul/optimiser/DataFlowAnalyzer.h>
2019-05-21 16:38:05 +00:00
#include <libsolutil/CommonData.h>
2019-05-21 16:38:05 +00:00
#include <variant>
using namespace std;
2019-12-11 16:31:36 +00:00
using namespace solidity;
using namespace solidity::yul;
2019-05-21 13:52:15 +00:00
2019-05-21 16:38:05 +00:00
bool KnowledgeBase::knownToBeDifferent(YulString _a, YulString _b)
2019-05-21 13:52:15 +00:00
{
2019-05-21 16:38:05 +00:00
// Try to use the simplification rules together with the
2019-05-21 13:52:15 +00:00
// current values to turn `sub(_a, _b)` into a nonzero constant.
// If that fails, try `eq(_a, _b)`.
2019-05-21 16:38:05 +00:00
2021-05-05 16:02:35 +00:00
if (optional<u256> difference = differenceIfKnownConstant(_a, _b))
return difference != 0;
2019-05-21 16:38:05 +00:00
2019-12-11 16:31:36 +00:00
Expression expr2 = simplify(FunctionCall{{}, {{}, "eq"_yulstring}, util::make_vector<Expression>(Identifier{{}, _a}, Identifier{{}, _b})});
if (holds_alternative<Literal>(expr2))
return valueOfLiteral(std::get<Literal>(expr2)) == 0;
2019-05-21 16:38:05 +00:00
return false;
}
2021-05-05 16:02:35 +00:00
optional<u256> KnowledgeBase::differenceIfKnownConstant(YulString _a, YulString _b)
{
// Try to use the simplification rules together with the
// current values to turn `sub(_a, _b)` into a constant.
Expression expr1 = simplify(FunctionCall{{}, {{}, "sub"_yulstring}, util::make_vector<Expression>(Identifier{{}, _a}, Identifier{{}, _b})});
if (Literal const* value = get_if<Literal>(&expr1))
return valueOfLiteral(*value);
return {};
}
2019-05-21 16:38:05 +00:00
bool KnowledgeBase::knownToBeDifferentByAtLeast32(YulString _a, YulString _b)
{
// Try to use the simplification rules together with the
// current values to turn `sub(_a, _b)` into a constant whose absolute value is at least 32.
2021-05-05 16:02:35 +00:00
if (optional<u256> difference = differenceIfKnownConstant(_a, _b))
return difference >= 32 && difference <= u256(0) - 32;
2019-05-21 16:38:05 +00:00
2019-05-21 13:52:15 +00:00
return false;
}
2019-05-21 16:38:05 +00:00
2021-05-05 16:02:35 +00:00
bool KnowledgeBase::knownToBeZero(YulString _a)
{
return valueIfKnownConstant(_a) == u256{};
}
optional<u256> KnowledgeBase::valueIfKnownConstant(YulString _a)
{
if (AssignedValue const* value = m_variableValues(_a))
if (Literal const* literal = get_if<Literal>(value->value))
2021-05-05 16:02:35 +00:00
return valueOfLiteral(*literal);
return {};
}
2019-05-21 16:38:05 +00:00
Expression KnowledgeBase::simplify(Expression _expression)
{
2021-08-19 10:14:50 +00:00
m_counter = 0;
2022-08-23 17:28:45 +00:00
return simplifyRecursively(std::move(_expression));
2021-08-19 10:14:50 +00:00
}
2019-05-21 16:38:05 +00:00
2021-08-19 10:14:50 +00:00
Expression KnowledgeBase::simplifyRecursively(Expression _expression)
{
if (m_counter++ > 100)
2019-05-21 16:38:05 +00:00
return _expression;
if (holds_alternative<FunctionCall>(_expression))
for (Expression& arg: std::get<FunctionCall>(_expression).arguments)
2021-08-19 10:14:50 +00:00
arg = simplifyRecursively(arg);
2019-05-21 16:38:05 +00:00
if (auto match = SimplificationRules::findFirstMatch(_expression, m_dialect, m_variableValues))
2021-08-19 10:14:50 +00:00
return simplifyRecursively(match->action().toExpression(debugDataOf(_expression)));
2019-05-21 16:38:05 +00:00
return _expression;
}