solidity/libyul/optimiser/KnowledgeBase.cpp

213 lines
6.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>
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
2022-11-10 13:22:40 +00:00
KnowledgeBase::KnowledgeBase(map<YulString, AssignedValue> const& _ssaValues):
m_valuesAreSSA(true),
m_variableValues([_ssaValues](YulString _var) { return util::valueOrNullptr(_ssaValues, _var); })
{}
2019-05-21 16:38:05 +00:00
bool KnowledgeBase::knownToBeDifferent(YulString _a, YulString _b)
2019-05-21 13:52:15 +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
return false;
}
2021-05-05 16:02:35 +00:00
optional<u256> KnowledgeBase::differenceIfKnownConstant(YulString _a, YulString _b)
{
VariableOffset offA = explore(_a);
VariableOffset offB = explore(_b);
if (offA.reference == offB.reference)
return offA.offset - offB.offset;
else
2022-11-10 17:02:56 +00:00
return nullopt;
2021-05-05 16:02:35 +00:00
}
2019-05-21 16:38:05 +00:00
bool KnowledgeBase::knownToBeDifferentByAtLeast32(YulString _a, YulString _b)
{
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)
{
2022-11-10 17:02:56 +00:00
return valueIfKnownConstant(_a) == 0;
2021-05-05 16:02:35 +00:00
}
optional<u256> KnowledgeBase::valueIfKnownConstant(YulString _a)
{
2023-02-09 10:21:37 +00:00
return explore(_a).absoluteValue();
}
optional<u256> KnowledgeBase::valueIfKnownConstant(Expression const& _expression)
{
if (Identifier const* ident = get_if<Identifier>(&_expression))
return valueIfKnownConstant(ident->name);
else if (Literal const* lit = get_if<Literal>(&_expression))
return valueOfLiteral(*lit);
else
2022-11-10 17:02:56 +00:00
return nullopt;
2021-05-05 16:02:35 +00:00
}
KnowledgeBase::VariableOffset KnowledgeBase::explore(YulString _var)
2019-05-21 16:38:05 +00:00
{
2022-11-10 13:22:40 +00:00
Expression const* value = nullptr;
if (m_valuesAreSSA)
{
// In SSA, a once determined offset is always valid, so we first see
// if we already computed it.
if (VariableOffset const* varOff = util::valueOrNullptr(m_offsets, _var))
return *varOff;
value = valueOf(_var);
}
else
{
// For non-SSA, we query the value first so that the variable is reset if it has changed
// since the last call.
value = valueOf(_var);
if (VariableOffset const* varOff = util::valueOrNullptr(m_offsets, _var))
return *varOff;
}
if (value)
if (optional<VariableOffset> offset = explore(*value))
return setOffset(_var, *offset);
return setOffset(_var, VariableOffset{_var, 0});
2021-08-19 10:14:50 +00:00
}
2019-05-21 16:38:05 +00:00
optional<KnowledgeBase::VariableOffset> KnowledgeBase::explore(Expression const& _value)
2021-08-19 10:14:50 +00:00
{
2022-11-10 17:02:56 +00:00
if (Literal const* literal = get_if<Literal>(&_value))
return VariableOffset{YulString{}, valueOfLiteral(*literal)};
2022-11-10 17:02:56 +00:00
else if (Identifier const* identifier = get_if<Identifier>(&_value))
return explore(identifier->name);
else if (FunctionCall const* f = get_if<FunctionCall>(&_value))
2022-11-10 15:04:19 +00:00
{
if (f->functionName.name == "add"_yulstring)
{
if (optional<VariableOffset> a = explore(f->arguments[0]))
if (optional<VariableOffset> b = explore(f->arguments[1]))
{
2022-11-10 15:04:19 +00:00
u256 offset = a->offset + b->offset;
2023-02-09 10:21:37 +00:00
if (a->isAbsolute())
// a is constant
return VariableOffset{b->reference, offset};
2023-02-09 10:21:37 +00:00
else if (b->isAbsolute())
// b is constant
return VariableOffset{a->reference, offset};
}
2022-11-10 15:04:19 +00:00
}
else if (f->functionName.name == "sub"_yulstring)
if (optional<VariableOffset> a = explore(f->arguments[0]))
if (optional<VariableOffset> b = explore(f->arguments[1]))
{
u256 offset = a->offset - b->offset;
if (a->reference == b->reference)
return VariableOffset{YulString{}, offset};
2023-02-09 10:21:37 +00:00
else if (b->isAbsolute())
2022-11-10 15:04:19 +00:00
// b is constant
return VariableOffset{a->reference, offset};
}
}
2019-05-21 16:38:05 +00:00
2022-11-10 17:02:56 +00:00
return nullopt;
}
2019-05-21 16:38:05 +00:00
Expression const* KnowledgeBase::valueOf(YulString _var)
{
AssignedValue const* assignedValue = m_variableValues(_var);
Expression const* currentValue = assignedValue ? assignedValue->value : nullptr;
2022-11-10 13:22:40 +00:00
if (m_valuesAreSSA)
return currentValue;
Expression const* lastValue = m_lastKnownValue[_var];
if (lastValue != currentValue)
reset(_var);
m_lastKnownValue[_var] = currentValue;
return currentValue;
}
2019-05-21 16:38:05 +00:00
void KnowledgeBase::reset(YulString _var)
{
2022-11-10 13:22:40 +00:00
yulAssert(!m_valuesAreSSA);
m_lastKnownValue.erase(_var);
if (VariableOffset const* offset = util::valueOrNullptr(m_offsets, _var))
{
// Remove var from its group
2023-02-09 10:21:37 +00:00
if (!offset->isAbsolute())
m_groupMembers[offset->reference].erase(_var);
m_offsets.erase(_var);
}
if (set<YulString>* group = util::valueOrNullptr(m_groupMembers, _var))
{
// _var was a representative, we might have to find a new one.
2022-11-10 17:02:56 +00:00
if (!group->empty())
{
YulString newRepresentative = *group->begin();
2022-11-10 17:02:56 +00:00
yulAssert(newRepresentative != _var);
u256 newOffset = m_offsets[newRepresentative].offset;
2022-11-10 17:02:56 +00:00
// newOffset = newRepresentative - _var
for (YulString groupMember: *group)
{
yulAssert(m_offsets[groupMember].reference == _var);
m_offsets[groupMember].reference = newRepresentative;
2022-11-10 17:02:56 +00:00
// groupMember = _var + m_offsets[groupMember].offset (old)
// = newRepresentative - newOffset + m_offsets[groupMember].offset (old)
// so subtracting newOffset from .offset yields the original relation again,
// just with _var replaced by newRepresentative
m_offsets[groupMember].offset -= newOffset;
}
2022-11-10 17:02:56 +00:00
m_groupMembers[newRepresentative] = std::move(*group);
}
2022-11-10 17:02:56 +00:00
m_groupMembers.erase(_var);
}
}
KnowledgeBase::VariableOffset KnowledgeBase::setOffset(YulString _variable, VariableOffset _value)
{
m_offsets[_variable] = _value;
// Constants are not tracked in m_groupMembers because
// the "representative" can never be reset.
2022-11-10 17:02:56 +00:00
if (!_value.reference.empty())
m_groupMembers[_value.reference].insert(_variable);
return _value;
2019-05-21 16:38:05 +00:00
}