mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #6073 from ethereum/const-opt-shift
Support shifts in the constant optimiser when Constantinople is targeted
This commit is contained in:
commit
52ee955fba
@ -7,6 +7,7 @@ Compiler Features:
|
|||||||
* SMTChecker: Do not report underflow/overflow if they always revert. This removes false positives when using ``SafeMath``.
|
* SMTChecker: Do not report underflow/overflow if they always revert. This removes false positives when using ``SafeMath``.
|
||||||
* Static Analyzer: Warn about expressions with custom types when they have no effect.
|
* Static Analyzer: Warn about expressions with custom types when they have no effect.
|
||||||
* Optimizer: Add rule for shifts with constants for Constantinople.
|
* Optimizer: Add rule for shifts with constants for Constantinople.
|
||||||
|
* Optimizer: Support shifts in the constant optimiser for Constantinople.
|
||||||
|
|
||||||
|
|
||||||
Bugfixes:
|
Bugfixes:
|
||||||
|
@ -210,6 +210,9 @@ AssemblyItems ComputeMethod::findRepresentation(u256 const& _value)
|
|||||||
AssemblyItems newRoutine;
|
AssemblyItems newRoutine;
|
||||||
if (lowerPart != 0)
|
if (lowerPart != 0)
|
||||||
newRoutine += findRepresentation(u256(abs(lowerPart)));
|
newRoutine += findRepresentation(u256(abs(lowerPart)));
|
||||||
|
if (m_params.evmVersion.hasBitwiseShifting())
|
||||||
|
newRoutine += AssemblyItems{u256(1), u256(bits), Instruction::SHL};
|
||||||
|
else
|
||||||
newRoutine += AssemblyItems{u256(bits), u256(2), Instruction::EXP};
|
newRoutine += AssemblyItems{u256(bits), u256(2), Instruction::EXP};
|
||||||
if (upperPart != 1)
|
if (upperPart != 1)
|
||||||
newRoutine += findRepresentation(upperPart) + AssemblyItems{Instruction::MUL};
|
newRoutine += findRepresentation(upperPart) + AssemblyItems{Instruction::MUL};
|
||||||
@ -231,7 +234,7 @@ AssemblyItems ComputeMethod::findRepresentation(u256 const& _value)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ComputeMethod::checkRepresentation(u256 const& _value, AssemblyItems const& _routine)
|
bool ComputeMethod::checkRepresentation(u256 const& _value, AssemblyItems const& _routine) const
|
||||||
{
|
{
|
||||||
// This is a tiny EVM that can only evaluate some instructions.
|
// This is a tiny EVM that can only evaluate some instructions.
|
||||||
vector<u256> stack;
|
vector<u256> stack;
|
||||||
@ -263,6 +266,24 @@ bool ComputeMethod::checkRepresentation(u256 const& _value, AssemblyItems const&
|
|||||||
case Instruction::NOT:
|
case Instruction::NOT:
|
||||||
sp[0] = ~sp[0];
|
sp[0] = ~sp[0];
|
||||||
break;
|
break;
|
||||||
|
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;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -155,7 +155,7 @@ protected:
|
|||||||
/// Tries to recursively find a way to compute @a _value.
|
/// Tries to recursively find a way to compute @a _value.
|
||||||
AssemblyItems findRepresentation(u256 const& _value);
|
AssemblyItems findRepresentation(u256 const& _value);
|
||||||
/// Recomputes the value from the calculated representation and checks for correctness.
|
/// Recomputes the value from the calculated representation and checks for correctness.
|
||||||
static bool checkRepresentation(u256 const& _value, AssemblyItems const& _routine);
|
bool checkRepresentation(u256 const& _value, AssemblyItems const& _routine) const;
|
||||||
bigint gasNeeded(AssemblyItems const& _routine) const;
|
bigint gasNeeded(AssemblyItems const& _routine) const;
|
||||||
|
|
||||||
/// Counter for the complexity of optimization, will stop when it reaches zero.
|
/// Counter for the complexity of optimization, will stop when it reaches zero.
|
||||||
|
Loading…
Reference in New Issue
Block a user