2015-06-01 10:32:59 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-06-01 10:32:59 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-06-01 10:32:59 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-06-01 10:32:59 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-06-01 10:32:59 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2015-06-01 10:32:59 +00:00
|
|
|
/** @file ConstantOptimiser.cpp
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
*/
|
|
|
|
|
2016-03-22 12:05:27 +00:00
|
|
|
#include <libevmasm/ConstantOptimiser.h>
|
|
|
|
#include <libevmasm/Assembly.h>
|
|
|
|
#include <libevmasm/GasMeter.h>
|
2019-12-11 16:31:36 +00:00
|
|
|
|
2015-06-01 10:32:59 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::evmasm;
|
2015-06-01 10:32:59 +00:00
|
|
|
|
|
|
|
unsigned ConstantOptimisationMethod::optimiseConstants(
|
|
|
|
bool _isCreation,
|
|
|
|
size_t _runs,
|
2019-02-25 14:29:57 +00:00
|
|
|
langutil::EVMVersion _evmVersion,
|
2019-01-28 16:29:10 +00:00
|
|
|
Assembly& _assembly
|
2015-06-01 10:32:59 +00:00
|
|
|
)
|
|
|
|
{
|
2019-01-28 16:29:10 +00:00
|
|
|
// TODO: design the optimiser in a way this is not needed
|
|
|
|
AssemblyItems& _items = _assembly.items();
|
|
|
|
|
2015-06-01 10:32:59 +00:00
|
|
|
unsigned optimisations = 0;
|
|
|
|
map<AssemblyItem, size_t> pushes;
|
|
|
|
for (AssemblyItem const& item: _items)
|
|
|
|
if (item.type() == Push)
|
|
|
|
pushes[item]++;
|
2017-01-06 16:05:27 +00:00
|
|
|
map<u256, AssemblyItems> pendingReplacements;
|
2015-06-01 10:32:59 +00:00
|
|
|
for (auto it: pushes)
|
|
|
|
{
|
|
|
|
AssemblyItem const& item = it.first;
|
|
|
|
if (item.data() < 0x100)
|
|
|
|
continue;
|
|
|
|
Params params;
|
|
|
|
params.multiplicity = it.second;
|
|
|
|
params.isCreation = _isCreation;
|
|
|
|
params.runs = _runs;
|
2018-03-01 11:06:36 +00:00
|
|
|
params.evmVersion = _evmVersion;
|
2015-06-01 10:32:59 +00:00
|
|
|
LiteralMethod lit(params, item.data());
|
|
|
|
bigint literalGas = lit.gasNeeded();
|
|
|
|
CodeCopyMethod copy(params, item.data());
|
|
|
|
bigint copyGas = copy.gasNeeded();
|
|
|
|
ComputeMethod compute(params, item.data());
|
|
|
|
bigint computeGas = compute.gasNeeded();
|
2017-01-06 16:05:27 +00:00
|
|
|
AssemblyItems replacement;
|
2015-06-01 10:32:59 +00:00
|
|
|
if (copyGas < literalGas && copyGas < computeGas)
|
|
|
|
{
|
2017-01-06 16:05:27 +00:00
|
|
|
replacement = copy.execute(_assembly);
|
2015-06-01 10:32:59 +00:00
|
|
|
optimisations++;
|
|
|
|
}
|
2017-01-12 16:52:23 +00:00
|
|
|
else if (computeGas < literalGas && computeGas <= copyGas)
|
2015-06-01 10:32:59 +00:00
|
|
|
{
|
2017-01-06 16:05:27 +00:00
|
|
|
replacement = compute.execute(_assembly);
|
2015-06-01 10:32:59 +00:00
|
|
|
optimisations++;
|
|
|
|
}
|
2017-01-06 16:05:27 +00:00
|
|
|
if (!replacement.empty())
|
|
|
|
pendingReplacements[item.data()] = replacement;
|
2015-06-01 10:32:59 +00:00
|
|
|
}
|
2017-01-06 16:05:27 +00:00
|
|
|
if (!pendingReplacements.empty())
|
|
|
|
replaceConstants(_items, pendingReplacements);
|
2015-06-01 10:32:59 +00:00
|
|
|
return optimisations;
|
|
|
|
}
|
|
|
|
|
|
|
|
bigint ConstantOptimisationMethod::simpleRunGas(AssemblyItems const& _items)
|
|
|
|
{
|
|
|
|
bigint gas = 0;
|
|
|
|
for (AssemblyItem const& item: _items)
|
|
|
|
if (item.type() == Push)
|
2016-04-06 18:55:46 +00:00
|
|
|
gas += GasMeter::runGas(Instruction::PUSH1);
|
2015-06-01 10:32:59 +00:00
|
|
|
else if (item.type() == Operation)
|
2018-03-01 11:06:36 +00:00
|
|
|
{
|
|
|
|
if (item.instruction() == Instruction::EXP)
|
|
|
|
gas += GasCosts::expGas;
|
|
|
|
else
|
|
|
|
gas += GasMeter::runGas(item.instruction());
|
|
|
|
}
|
2015-06-01 10:32:59 +00:00
|
|
|
return gas;
|
|
|
|
}
|
|
|
|
|
|
|
|
bigint ConstantOptimisationMethod::dataGas(bytes const& _data) const
|
|
|
|
{
|
2018-08-08 21:54:33 +00:00
|
|
|
assertThrow(_data.size() > 0, OptimizerException, "Empty bytecode generated.");
|
2019-11-07 14:10:08 +00:00
|
|
|
return bigint(GasMeter::dataGas(_data, m_params.isCreation, m_params.evmVersion));
|
2015-06-01 10:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t ConstantOptimisationMethod::bytesRequired(AssemblyItems const& _items)
|
|
|
|
{
|
2021-09-22 13:14:31 +00:00
|
|
|
return evmasm::bytesRequired(_items, 3, Precision::Approximate); // assume 3 byte addresses
|
2015-06-01 10:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConstantOptimisationMethod::replaceConstants(
|
|
|
|
AssemblyItems& _items,
|
2017-01-06 16:05:27 +00:00
|
|
|
map<u256, AssemblyItems> const& _replacements
|
|
|
|
)
|
2015-06-01 10:32:59 +00:00
|
|
|
{
|
2017-01-06 16:05:27 +00:00
|
|
|
AssemblyItems replaced;
|
|
|
|
for (AssemblyItem const& item: _items)
|
2015-06-01 10:32:59 +00:00
|
|
|
{
|
2017-01-06 16:05:27 +00:00
|
|
|
if (item.type() == Push)
|
|
|
|
{
|
|
|
|
auto it = _replacements.find(item.data());
|
|
|
|
if (it != _replacements.end())
|
|
|
|
{
|
|
|
|
replaced += it->second;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
replaced.push_back(item);
|
2015-06-01 10:32:59 +00:00
|
|
|
}
|
2017-01-06 16:05:27 +00:00
|
|
|
_items = std::move(replaced);
|
2015-06-01 10:32:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 00:14:15 +00:00
|
|
|
bigint LiteralMethod::gasNeeded() const
|
2015-06-01 10:32:59 +00:00
|
|
|
{
|
|
|
|
return combineGas(
|
2015-06-05 15:34:20 +00:00
|
|
|
simpleRunGas({Instruction::PUSH1}),
|
2015-06-01 10:32:59 +00:00
|
|
|
// PUSHX plus data
|
2021-09-16 14:33:28 +00:00
|
|
|
(m_params.isCreation ? GasCosts::txDataNonZeroGas(m_params.evmVersion) : GasCosts::createDataGas) + dataGas(toCompactBigEndian(m_value, 1)),
|
2015-06-01 10:32:59 +00:00
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-17 00:14:15 +00:00
|
|
|
bigint CodeCopyMethod::gasNeeded() const
|
2015-06-01 10:32:59 +00:00
|
|
|
{
|
|
|
|
return combineGas(
|
|
|
|
// Run gas: we ignore memory increase costs
|
2017-01-06 16:05:27 +00:00
|
|
|
simpleRunGas(copyRoutine()) + GasCosts::copyGas,
|
2015-06-01 10:32:59 +00:00
|
|
|
// Data gas for copy routines: Some bytes are zero, but we ignore them.
|
2019-11-07 14:10:08 +00:00
|
|
|
bytesRequired(copyRoutine()) * (m_params.isCreation ? GasCosts::txDataNonZeroGas(m_params.evmVersion) : GasCosts::createDataGas),
|
2015-06-01 10:32:59 +00:00
|
|
|
// Data gas for data itself
|
2021-09-16 14:33:28 +00:00
|
|
|
dataGas(toBigEndian(m_value))
|
2015-06-01 10:32:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-17 00:14:15 +00:00
|
|
|
AssemblyItems CodeCopyMethod::execute(Assembly& _assembly) const
|
2015-06-01 10:32:59 +00:00
|
|
|
{
|
2021-09-16 14:33:28 +00:00
|
|
|
bytes data = toBigEndian(m_value);
|
2019-05-22 14:18:57 +00:00
|
|
|
assertThrow(data.size() == 32, OptimizerException, "Invalid number encoding.");
|
2017-01-06 16:05:27 +00:00
|
|
|
AssemblyItems actualCopyRoutine = copyRoutine();
|
|
|
|
actualCopyRoutine[4] = _assembly.newData(data);
|
|
|
|
return actualCopyRoutine;
|
|
|
|
}
|
|
|
|
|
2017-08-21 21:05:24 +00:00
|
|
|
AssemblyItems const& CodeCopyMethod::copyRoutine()
|
2017-01-06 16:05:27 +00:00
|
|
|
{
|
|
|
|
AssemblyItems static copyRoutine{
|
2019-05-22 14:18:57 +00:00
|
|
|
// constant to be reused 3+ times
|
2017-01-06 16:05:27 +00:00
|
|
|
u256(0),
|
2019-05-22 14:18:57 +00:00
|
|
|
|
|
|
|
// back up memory
|
|
|
|
// mload(0)
|
2017-01-06 16:05:27 +00:00
|
|
|
Instruction::DUP1,
|
2019-05-22 14:18:57 +00:00
|
|
|
Instruction::MLOAD,
|
|
|
|
|
|
|
|
// codecopy(0, <offset>, 32)
|
2017-01-06 16:05:27 +00:00
|
|
|
u256(32),
|
2019-05-22 14:18:57 +00:00
|
|
|
AssemblyItem(PushData, u256(1) << 16), // replaced above in actualCopyRoutine[4]
|
2017-01-06 16:05:27 +00:00
|
|
|
Instruction::DUP4,
|
|
|
|
Instruction::CODECOPY,
|
2019-05-22 14:18:57 +00:00
|
|
|
|
|
|
|
// mload(0)
|
2017-01-06 16:05:27 +00:00
|
|
|
Instruction::DUP2,
|
|
|
|
Instruction::MLOAD,
|
2019-05-22 14:18:57 +00:00
|
|
|
|
|
|
|
// restore original memory
|
2017-01-06 16:05:27 +00:00
|
|
|
Instruction::SWAP2,
|
|
|
|
Instruction::MSTORE
|
|
|
|
};
|
|
|
|
return copyRoutine;
|
2015-06-01 10:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AssemblyItems ComputeMethod::findRepresentation(u256 const& _value)
|
|
|
|
{
|
|
|
|
if (_value < 0x10000)
|
|
|
|
// Very small value, not worth computing
|
|
|
|
return AssemblyItems{_value};
|
2021-09-16 14:33:28 +00:00
|
|
|
else if (numberEncodingSize(~_value) < numberEncodingSize(_value))
|
2015-06-01 10:32:59 +00:00
|
|
|
// Negated is shorter to represent
|
|
|
|
return findRepresentation(~_value) + AssemblyItems{Instruction::NOT};
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Decompose value into a * 2**k + b where abs(b) << 2**k
|
|
|
|
// Is not always better, try literal and decomposition method.
|
|
|
|
AssemblyItems routine{u256(_value)};
|
|
|
|
bigint bestGas = gasNeeded(routine);
|
2017-03-03 15:17:21 +00:00
|
|
|
for (unsigned bits = 255; bits > 8 && m_maxSteps > 0; --bits)
|
2015-06-01 10:32:59 +00:00
|
|
|
{
|
2018-11-27 14:28:20 +00:00
|
|
|
unsigned gapDetector = unsigned((_value >> (bits - 8)) & 0x1ff);
|
2015-06-01 10:32:59 +00:00
|
|
|
if (gapDetector != 0xff && gapDetector != 0x100)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
u256 powerOfTwo = u256(1) << bits;
|
|
|
|
u256 upperPart = _value >> bits;
|
|
|
|
bigint lowerPart = _value & (powerOfTwo - 1);
|
2017-05-02 14:56:12 +00:00
|
|
|
if ((powerOfTwo - lowerPart) < lowerPart)
|
2017-04-03 12:40:17 +00:00
|
|
|
{
|
2015-06-01 10:32:59 +00:00
|
|
|
lowerPart = lowerPart - powerOfTwo; // make it negative
|
2017-04-03 12:40:17 +00:00
|
|
|
upperPart++;
|
|
|
|
}
|
|
|
|
if (upperPart == 0)
|
|
|
|
continue;
|
2015-06-01 10:32:59 +00:00
|
|
|
if (abs(lowerPart) >= (powerOfTwo >> 8))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
AssemblyItems newRoutine;
|
|
|
|
if (lowerPart != 0)
|
|
|
|
newRoutine += findRepresentation(u256(abs(lowerPart)));
|
2019-02-21 23:50:37 +00:00
|
|
|
if (m_params.evmVersion.hasBitwiseShifting())
|
2019-05-14 13:14:24 +00:00
|
|
|
{
|
|
|
|
newRoutine += findRepresentation(upperPart);
|
|
|
|
newRoutine += AssemblyItems{u256(bits), Instruction::SHL};
|
|
|
|
}
|
2019-02-21 23:50:37 +00:00
|
|
|
else
|
2019-05-14 13:14:24 +00:00
|
|
|
{
|
2019-02-21 23:50:37 +00:00
|
|
|
newRoutine += AssemblyItems{u256(bits), u256(2), Instruction::EXP};
|
2019-05-14 13:14:24 +00:00
|
|
|
if (upperPart != 1)
|
|
|
|
newRoutine += findRepresentation(upperPart) + AssemblyItems{Instruction::MUL};
|
|
|
|
}
|
2015-06-01 10:32:59 +00:00
|
|
|
if (lowerPart > 0)
|
|
|
|
newRoutine += AssemblyItems{Instruction::ADD};
|
|
|
|
else if (lowerPart < 0)
|
2015-06-05 15:34:20 +00:00
|
|
|
newRoutine.push_back(Instruction::SUB);
|
2015-06-01 10:32:59 +00:00
|
|
|
|
2017-03-03 15:17:21 +00:00
|
|
|
if (m_maxSteps > 0)
|
|
|
|
m_maxSteps--;
|
2015-06-01 10:32:59 +00:00
|
|
|
bigint newGas = gasNeeded(newRoutine);
|
|
|
|
if (newGas < bestGas)
|
|
|
|
{
|
2022-08-23 17:28:45 +00:00
|
|
|
bestGas = std::move(newGas);
|
|
|
|
routine = std::move(newRoutine);
|
2015-06-01 10:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return routine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 23:50:37 +00:00
|
|
|
bool ComputeMethod::checkRepresentation(u256 const& _value, AssemblyItems const& _routine) const
|
2017-04-03 12:40:39 +00:00
|
|
|
{
|
|
|
|
// This is a tiny EVM that can only evaluate some instructions.
|
|
|
|
vector<u256> stack;
|
2017-04-27 10:29:26 +00:00
|
|
|
for (AssemblyItem const& item: _routine)
|
2017-04-03 12:40:39 +00:00
|
|
|
{
|
|
|
|
switch (item.type())
|
|
|
|
{
|
|
|
|
case Operation:
|
|
|
|
{
|
2020-06-02 13:10:14 +00:00
|
|
|
if (stack.size() < item.arguments())
|
2017-04-03 12:40:39 +00:00
|
|
|
return false;
|
|
|
|
u256* sp = &stack.back();
|
|
|
|
switch (item.instruction())
|
|
|
|
{
|
|
|
|
case Instruction::MUL:
|
|
|
|
sp[-1] = sp[0] * sp[-1];
|
|
|
|
break;
|
|
|
|
case Instruction::EXP:
|
|
|
|
if (sp[-1] > 0xff)
|
|
|
|
return false;
|
|
|
|
sp[-1] = boost::multiprecision::pow(sp[0], unsigned(sp[-1]));
|
|
|
|
break;
|
|
|
|
case Instruction::ADD:
|
|
|
|
sp[-1] = sp[0] + sp[-1];
|
|
|
|
break;
|
|
|
|
case Instruction::SUB:
|
|
|
|
sp[-1] = sp[0] - sp[-1];
|
|
|
|
break;
|
|
|
|
case Instruction::NOT:
|
|
|
|
sp[0] = ~sp[0];
|
|
|
|
break;
|
2019-02-21 23:50:37 +00:00
|
|
|
case Instruction::SHL:
|
|
|
|
assertThrow(
|
|
|
|
m_params.evmVersion.hasBitwiseShifting(),
|
|
|
|
OptimizerException,
|
|
|
|
"Shift generated for invalid EVM version."
|
|
|
|
);
|
|
|
|
assertThrow(sp[0] <= u256(255), OptimizerException, "Invalid shift generated.");
|
|
|
|
sp[-1] = u256(bigint(sp[-1]) << unsigned(sp[0]));
|
|
|
|
break;
|
|
|
|
case Instruction::SHR:
|
|
|
|
assertThrow(
|
|
|
|
m_params.evmVersion.hasBitwiseShifting(),
|
|
|
|
OptimizerException,
|
|
|
|
"Shift generated for invalid EVM version."
|
|
|
|
);
|
|
|
|
assertThrow(sp[0] <= u256(255), OptimizerException, "Invalid shift generated.");
|
|
|
|
sp[-1] = sp[-1] >> unsigned(sp[0]);
|
|
|
|
break;
|
2017-04-03 12:40:39 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
stack.resize(stack.size() + item.deposit());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Push:
|
|
|
|
stack.push_back(item.data());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stack.size() == 1 && stack.front() == _value;
|
|
|
|
}
|
|
|
|
|
2017-08-17 00:14:15 +00:00
|
|
|
bigint ComputeMethod::gasNeeded(AssemblyItems const& _routine) const
|
2015-06-01 10:32:59 +00:00
|
|
|
{
|
2020-06-02 13:10:14 +00:00
|
|
|
auto numExps = static_cast<size_t>(count(_routine.begin(), _routine.end(), Instruction::EXP));
|
2015-06-01 10:32:59 +00:00
|
|
|
return combineGas(
|
2018-03-01 11:06:36 +00:00
|
|
|
simpleRunGas(_routine) + numExps * (GasCosts::expGas + GasCosts::expByteGas(m_params.evmVersion)),
|
2015-06-01 10:32:59 +00:00
|
|
|
// Data gas for routine: Some bytes are zero, but we ignore them.
|
2019-11-07 14:10:08 +00:00
|
|
|
bytesRequired(_routine) * (m_params.isCreation ? GasCosts::txDataNonZeroGas(m_params.evmVersion) : GasCosts::createDataGas),
|
2015-06-01 10:32:59 +00:00
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|