mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Extend knowledge base.
This commit is contained in:
parent
729db521a9
commit
4b038e3f02
@ -40,9 +40,8 @@ bool KnowledgeBase::knownToBeDifferent(YulString _a, YulString _b)
|
||||
// current values to turn `sub(_a, _b)` into a nonzero constant.
|
||||
// If that fails, try `eq(_a, _b)`.
|
||||
|
||||
Expression expr1 = simplify(FunctionCall{{}, {{}, "sub"_yulstring}, util::make_vector<Expression>(Identifier{{}, _a}, Identifier{{}, _b})});
|
||||
if (holds_alternative<Literal>(expr1))
|
||||
return valueOfLiteral(std::get<Literal>(expr1)) != 0;
|
||||
if (optional<u256> difference = differenceIfKnownConstant(_a, _b))
|
||||
return difference != 0;
|
||||
|
||||
Expression expr2 = simplify(FunctionCall{{}, {{}, "eq"_yulstring}, util::make_vector<Expression>(Identifier{{}, _a}, Identifier{{}, _b})});
|
||||
if (holds_alternative<Literal>(expr2))
|
||||
@ -51,21 +50,42 @@ bool KnowledgeBase::knownToBeDifferent(YulString _a, YulString _b)
|
||||
return false;
|
||||
}
|
||||
|
||||
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 {};
|
||||
}
|
||||
|
||||
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.
|
||||
|
||||
Expression expr1 = simplify(FunctionCall{{}, {{}, "sub"_yulstring}, util::make_vector<Expression>(Identifier{{}, _a}, Identifier{{}, _b})});
|
||||
if (holds_alternative<Literal>(expr1))
|
||||
{
|
||||
u256 val = valueOfLiteral(std::get<Literal>(expr1));
|
||||
return val >= 32 && val <= u256(0) - 32;
|
||||
}
|
||||
if (optional<u256> difference = differenceIfKnownConstant(_a, _b))
|
||||
return difference >= 32 && difference <= u256(0) - 32;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool KnowledgeBase::knownToBeZero(YulString _a)
|
||||
{
|
||||
return valueIfKnownConstant(_a) == u256{};
|
||||
}
|
||||
|
||||
optional<u256> KnowledgeBase::valueIfKnownConstant(YulString _a)
|
||||
{
|
||||
if (m_variableValues.count(_a))
|
||||
if (Literal const* literal = get_if<Literal>(m_variableValues.at(_a).value))
|
||||
return valueOfLiteral(*literal);
|
||||
return {};
|
||||
}
|
||||
|
||||
Expression KnowledgeBase::simplify(Expression _expression)
|
||||
{
|
||||
bool startedRecursion = (m_recursionCounter == 0);
|
||||
|
@ -24,6 +24,8 @@
|
||||
#include <libyul/ASTForward.h>
|
||||
#include <libyul/YulString.h>
|
||||
|
||||
#include <libsolutil/Common.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace solidity::yul
|
||||
@ -46,8 +48,11 @@ public:
|
||||
{}
|
||||
|
||||
bool knownToBeDifferent(YulString _a, YulString _b);
|
||||
std::optional<u256> differenceIfKnownConstant(YulString _a, YulString _b);
|
||||
bool knownToBeDifferentByAtLeast32(YulString _a, YulString _b);
|
||||
bool knownToBeEqual(YulString _a, YulString _b) const { return _a == _b; }
|
||||
bool knownToBeZero(YulString _a);
|
||||
std::optional<u256> valueIfKnownConstant(YulString _a);
|
||||
|
||||
private:
|
||||
Expression simplify(Expression _expression);
|
||||
|
@ -136,6 +136,7 @@ set(libyul_sources
|
||||
libyul/FunctionSideEffects.cpp
|
||||
libyul/FunctionSideEffects.h
|
||||
libyul/Inliner.cpp
|
||||
libyul/KnowledgeBaseTest.cpp
|
||||
libyul/Metrics.cpp
|
||||
libyul/ObjectCompilerTest.cpp
|
||||
libyul/ObjectCompilerTest.h
|
||||
|
84
test/libyul/KnowledgeBaseTest.cpp
Normal file
84
test/libyul/KnowledgeBaseTest.cpp
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
/**
|
||||
* Unit tests for KnowledgeBase
|
||||
*/
|
||||
|
||||
#include <test/Common.h>
|
||||
|
||||
#include <test/libyul/Common.h>
|
||||
|
||||
#include <libyul/Object.h>
|
||||
#include <libyul/optimiser/KnowledgeBase.h>
|
||||
#include <libyul/optimiser/SSAValueTracker.h>
|
||||
#include <libyul/optimiser/DataFlowAnalyzer.h>
|
||||
#include <libyul/backends/evm/EVMDialect.h>
|
||||
|
||||
#include <liblangutil/ErrorReporter.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity::langutil;
|
||||
|
||||
namespace solidity::yul::test
|
||||
{
|
||||
|
||||
class KnowledgeBaseTest
|
||||
{
|
||||
protected:
|
||||
KnowledgeBase constructKnowledgeBase(string const& _source)
|
||||
{
|
||||
ErrorList errorList;
|
||||
shared_ptr<Object> object;
|
||||
shared_ptr<AsmAnalysisInfo> analysisInfo;
|
||||
std::tie(object, analysisInfo) = yul::test::parse(_source, m_dialect, errorList);
|
||||
BOOST_REQUIRE(object && errorList.empty() && object->code);
|
||||
|
||||
SSAValueTracker ssaValues;
|
||||
ssaValues(*object->code);
|
||||
for (auto const& [name, expression]: ssaValues.values())
|
||||
m_values[name].value = expression;
|
||||
|
||||
return KnowledgeBase(m_dialect, m_values);
|
||||
}
|
||||
|
||||
EVMDialect m_dialect{EVMVersion{}, true};
|
||||
map<YulString, AssignedValue> m_values;
|
||||
};
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(KnowledgeBase, KnowledgeBaseTest)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(basic)
|
||||
{
|
||||
yul::KnowledgeBase kb = constructKnowledgeBase(R"({
|
||||
let a := calldataload(0)
|
||||
let b := calldataload(0)
|
||||
let zero := 0
|
||||
let c := add(b, 0)
|
||||
let d := mul(b, 0)
|
||||
let e := sub(a, b)
|
||||
})");
|
||||
|
||||
BOOST_CHECK(!kb.knownToBeDifferent("a"_yulstring, "b"_yulstring));
|
||||
BOOST_CHECK(kb.valueIfKnownConstant("zero"_yulstring) == u256(0));
|
||||
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user