Skip to content

Instantly share code, notes, and snippets.

@hrkrshnn
Last active June 29, 2020 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrkrshnn/5d054ed71e068fbd02e9ead9599a6af9 to your computer and use it in GitHub Desktop.
Save hrkrshnn/5d054ed71e068fbd02e9ead9599a6af9 to your computer and use it in GitHub Desktop.
LoopInvariantCodeMotion
contract Arraysum {
uint256[] values;
function sumArray() public view returns(uint) {
uint sum = 0;
for(uint i = 0; i < values.length; i++)
sum += values[i];
}
}
diff -u /tmp/optimized1.yul /tmp/optimized.yul
--- /tmp/optimized1.yul 2020-06-29 21:47:13.459262630 +0530
+++ /tmp/optimized.yul 2020-06-29 21:47:15.511249787 +0530
@@ -227,15 +227,15 @@
if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) }
let vloc_sum := _1
let vloc_i := _1
+ let _2 := sload(_1)
for { }
- 1
+ lt(vloc_i, _2)
{
vloc_i := increment_t_uint256(vloc_i)
}
{
- if iszero(lt(vloc_i, sload(_1))) { break }
- let _2, _3 := storage_array_index_access_t_array$_t_uint256_$dyn_storage(_1, vloc_i)
- vloc_sum := checked_add_t_uint256(vloc_sum, extract_from_storage_value_dynamict_uint256(sload(_2), _3))
+ let _3, _4 := storage_array_index_access_t_array$_t_uint256_$dyn_storage(_1, vloc_i)
+ vloc_sum := checked_add_t_uint256(vloc_sum, extract_from_storage_value_dynamict_uint256(sload(_3), _4))
}
let memPos := allocateMemory(_1)
return(memPos, sub(abi_encode_tuple_t_uint256__to_t_uint256__fromStack(memPos, _1), memPos))
Diff finished. Mon Jun 29 21:54:34 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment