Skip to content

Commit 15226e0

Browse files
author
Mohammad Rezaei
committed
Mask generator optimizations
auto-fix some of the tests -- these have lower gas in the test only optimize fully if runs param is less than 200
1 parent 40a4d5d commit 15226e0

File tree

90 files changed

+259
-238
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+259
-238
lines changed

Changelog.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Language Features:
44

55

66
Compiler Features:
7-
7+
* Constant Optimizer: Compute masks using shifts when optimizing for size; use an ``--optimizer-runs`` value less than 200 for maximum size reduction.
88

99
Bugfixes:
1010

libevmasm/ConstantOptimiser.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,28 @@ AssemblyItems ComputeMethod::findRepresentation(u256 const& _value)
253253
if (_value < 0x10000)
254254
// Very small value, not worth computing
255255
return AssemblyItems{_value};
256-
else if (numberEncodingSize(~_value) < numberEncodingSize(_value))
256+
257+
// check for masks first
258+
unsigned lowZeros = 0;
259+
unsigned highOnes = 0;
260+
//only fully optimize for size if the compiler parameters are not default of 200
261+
unsigned threshold1 = m_params.runs < 200 ? 32 : 128;
262+
unsigned threshold2 = m_params.runs < 200 ? 16 : 128;
263+
for (; ((_value >> lowZeros) & 1) == 0 && lowZeros < 256; lowZeros++) {}
264+
for (; ((_value >> (lowZeros + highOnes)) & 1) == 1 && highOnes < 256; highOnes++) {}
265+
if (m_params.evmVersion.hasBitwiseShifting() && highOnes > threshold1 &&
266+
((_value >> (lowZeros + highOnes)) == 0) &&
267+
((lowZeros + highOnes < 256) || lowZeros > threshold2))
268+
{
269+
// this is a big enough mask to use zero negation
270+
AssemblyItems newRoutine = AssemblyItems{u256(0), Instruction::NOT};
271+
if ((highOnes + lowZeros) != 256)
272+
newRoutine += AssemblyItems{u256(256 - highOnes), Instruction::SHR};
273+
if (lowZeros > 0)
274+
newRoutine += AssemblyItems{u256(lowZeros), Instruction::SHL};
275+
return newRoutine;
276+
}
277+
if (numberEncodingSize(~_value) < numberEncodingSize(_value))
257278
// Negated is shorter to represent
258279
return findRepresentation(~_value) + AssemblyItems{Instruction::NOT};
259280
else

test/cmdlineTests/optimizer_BlockDeDuplicator/output

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ EVM assembly:
77
0x00
88
dup1
99
sload
10-
not(sub(shl(0x40, 0x01), 0x01))
10+
shl(0x40, not(0x00))
1111
and
1212
/* "optimizer_BlockDeDuplicator/input.sol":201:206 fun_x */
1313
or(tag_0_7, shl(0x20, tag_2))
14-
sub(shl(0x40, 0x01), 0x01)
14+
shr(0xc0, not(0x00))
1515
/* "optimizer_BlockDeDuplicator/input.sol":179:210 function() r = true ? fun_x : f */
1616
and
1717
or

test/cmdlineTests/optimizer_inliner_dynamic_reference_constructor/output

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ tag_1:
1717
/* "optimizer_inliner_dynamic_reference_constructor/input.sol":93:98 x = f */
1818
dup1
1919
sload
20-
not(sub(shl(0x40, 0x01), 0x01))
20+
shl(0x40, not(0x00))
2121
and
2222
/* "optimizer_inliner_dynamic_reference_constructor/input.sol":97:98 f */
2323
or(tag_0_12, shl(0x20, tag_4))
24-
sub(shl(0x40, 0x01), 0x01)
24+
shr(0xc0, not(0x00))
2525
/* "optimizer_inliner_dynamic_reference_constructor/input.sol":93:98 x = f */
2626
and
2727
or

test/cmdlineTests/standard_debug_info_in_evm_asm_via_ir_location/output.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
dup4
2121
add
2222
swap2
23-
sub(shl(0x40, 0x01), 0x01)
23+
shr(0xc0, not(0x00))
2424
dup4
2525
gt
2626
dup5
@@ -104,7 +104,7 @@ sub_0: assembly {
104104
jumpi(tag_26, callvalue)
105105
jumpi(tag_26, slt(add(not(0x03), calldatasize), 0x00))
106106
sload(0x00)
107-
sub(shl(0xff, 0x01), 0x01)
107+
shr(0x01, not(0x00))
108108
dup2
109109
eq
110110
tag_14
@@ -350,7 +350,7 @@ sub_0: assembly {
350350
dup4
351351
add
352352
swap2
353-
sub(shl(0x40, 0x01), 0x01)
353+
shr(0xc0, not(0x00))
354354
dup4
355355
gt
356356
dup5
@@ -450,7 +450,7 @@ sub_0: assembly {
450450
jumpi(tag_26, callvalue)
451451
jumpi(tag_26, slt(add(not(0x03), calldatasize), 0x00))
452452
sload(0x00)
453-
sub(shl(0xff, 0x01), 0x01)
453+
shr(0x01, not(0x00))
454454
dup2
455455
eq
456456
tag_14

test/cmdlineTests/viair_subobject_optimization/output

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ EVM assembly:
1818
dup4
1919
add
2020
swap2
21-
sub(shl(0x40, 0x01), 0x01)
21+
shr(0xc0, not(0x00))
2222
dup4
2323
gt
2424
dup5
@@ -223,7 +223,7 @@ sub_0: assembly {
223223
dup4
224224
add
225225
swap2
226-
sub(shl(0x40, 0x01), 0x01)
226+
shr(0xc0, not(0x00))
227227
dup4
228228
gt
229229
dup5

test/libsolidity/gasTests/abiv2_optimised.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ contract C {
1919
// optimize-yul: true
2020
// ----
2121
// creation:
22-
// codeDepositCost: 618200
22+
// codeDepositCost: 617600
2323
// executionCost: 649
24-
// totalCost: 618849
24+
// totalCost: 618249
2525
// external:
2626
// a(): 2283
2727
// b(uint256): 4649

test/libsolidity/semanticTests/abiEncoderV1/abi_encode_calldata_slice.sol

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ contract C {
5959
// EVMVersion: >homestead
6060
// ----
6161
// test_bytes() ->
62-
// gas irOptimized: 314884
62+
// gas irOptimized: 308660
6363
// gas legacy: 305816
64-
// gas legacyOptimized: 253573
64+
// gas legacyOptimized: 247337
6565
// test_uint256() ->
66-
// gas irOptimized: 448346
66+
// gas irOptimized: 438474
6767
// gas legacy: 421304
68-
// gas legacyOptimized: 351544
68+
// gas legacyOptimized: 341672

test/libsolidity/semanticTests/abiEncoderV2/abi_encode_calldata_slice.sol

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ contract C {
6060
// EVMVersion: >homestead
6161
// ----
6262
// test_bytes() ->
63-
// gas irOptimized: 314884
63+
// gas irOptimized: 308660
6464
// gas legacy: 305816
65-
// gas legacyOptimized: 253573
65+
// gas legacyOptimized: 247337
6666
// test_uint256() ->
67-
// gas irOptimized: 448346
67+
// gas irOptimized: 438474
6868
// gas legacy: 421304
69-
// gas legacyOptimized: 351544
69+
// gas legacyOptimized: 341672

test/libsolidity/semanticTests/abiEncoderV2/abi_encode_v2.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ contract C {
5050
// f2() -> 0x20, 0xa0, 0x1, 0x60, 0x2, 0x3, "abc"
5151
// f3() -> 0x20, 0xa0, 0x1, 0x60, 0x2, 0x3, "abc"
5252
// f4() -> 0x20, 0x160, 0x1, 0x80, 0xc0, 0x2, 0x3, "abc", 0x7, 0x40, 0x2, 0x2, 0x3
53-
// gas irOptimized: 111816
53+
// gas irOptimized: 111812
5454
// gas legacy: 113890
55-
// gas legacyOptimized: 111658
55+
// gas legacyOptimized: 111654

test/libsolidity/semanticTests/abiEncoderV2/abi_encode_v2_in_function_inherited_in_v1_contract.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ contract C is B {
3030
}
3131
// ----
3232
// test() -> 77
33-
// gas irOptimized: 55117
33+
// gas irOptimized: 55113
3434
// gas irOptimized code: 56800
3535
// gas legacy: 57266
3636
// gas legacy code: 94600
37-
// gas legacyOptimized: 55195
37+
// gas legacyOptimized: 55191
3838
// gas legacyOptimized code: 55000

test/libsolidity/semanticTests/abiEncoderV2/calldata_array.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ contract C {
2020
// f(uint256[][1]): 32, 32, 0 -> true
2121
// f(uint256[][1]): 32, 32, 1, 42 -> true
2222
// f(uint256[][1]): 32, 32, 8, 421, 422, 423, 424, 425, 426, 427, 428 -> true
23-
// gas irOptimized: 120978
23+
// gas irOptimized: 118382
2424
// gas legacy: 101568
2525
// gas legacyOptimized: 119092

test/libsolidity/semanticTests/array/byte_array_transitional_2.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ contract c {
1919
// test() -> 0
2020
// gas irOptimized: 122717
2121
// gas legacy: 147108
22-
// gas legacyOptimized: 144200
22+
// gas legacyOptimized: 143790

test/libsolidity/semanticTests/array/constant_var_as_array_length.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ contract C {
88
}
99
// ----
1010
// constructor(): 1, 2, 3 ->
11-
// gas irOptimized: 124991
11+
// gas irOptimized: 124887
1212
// gas irOptimized code: 14800
1313
// gas legacy: 134317
1414
// gas legacy code: 46200
15-
// gas legacyOptimized: 127166
15+
// gas legacyOptimized: 127114
1616
// gas legacyOptimized code: 23400
1717
// a(uint256): 0 -> 1
1818
// a(uint256): 1 -> 2

test/libsolidity/semanticTests/array/copying/array_copy_different_packing.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ contract c {
1818
}
1919
// ----
2020
// test() -> 0x01000000000000000000000000000000000000000000000000, 0x02000000000000000000000000000000000000000000000000, 0x03000000000000000000000000000000000000000000000000, 0x04000000000000000000000000000000000000000000000000, 0x05000000000000000000000000000000000000000000000000
21-
// gas irOptimized: 208415
21+
// gas irOptimized: 208407
2222
// gas legacy: 220711
23-
// gas legacyOptimized: 220097
23+
// gas legacyOptimized: 220093

test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_different_base_nested.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ contract c {
2121
}
2222
// ----
2323
// test() -> 3, 4
24-
// gas irOptimized: 169669
24+
// gas irOptimized: 169665
2525
// gas legacy: 175415
26-
// gas legacyOptimized: 172533
26+
// gas legacyOptimized: 172529

test/libsolidity/semanticTests/array/copying/array_copy_storage_storage_struct.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ contract c {
1717
}
1818
// ----
1919
// test() -> 4, 5
20-
// gas irOptimized: 190628
20+
// gas irOptimized: 190621
2121
// gas legacy: 190828
2222
// gas legacyOptimized: 189657
2323
// storageEmpty -> 1

test/libsolidity/semanticTests/array/copying/array_copy_target_leftover.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ contract c {
1717
}
1818
// ----
1919
// test() -> 0xffffffff, 0x0000000000000000000000000a00090008000700060005000400030002000100, 0x0000000000000000000000000000000000000000000000000000000000000000
20-
// gas irOptimized: 100496
20+
// gas irOptimized: 100492
2121
// gas legacy: 158109
22-
// gas legacyOptimized: 141019
22+
// gas legacyOptimized: 140891

test/libsolidity/semanticTests/array/copying/array_copy_target_simple.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ contract c {
1818
}
1919
// ----
2020
// test() -> 0x01000000000000000000000000000000000000000000000000, 0x02000000000000000000000000000000000000000000000000, 0x03000000000000000000000000000000000000000000000000, 0x04000000000000000000000000000000000000000000000000, 0x0
21-
// gas irOptimized: 273543
21+
// gas irOptimized: 273535
2222
// gas legacy: 282601
23-
// gas legacyOptimized: 281510
23+
// gas legacyOptimized: 281458

test/libsolidity/semanticTests/array/copying/array_copy_target_simple_2.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ contract c {
2020
// test() -> 0x01000000000000000000000000000000000000000000000000, 0x02000000000000000000000000000000000000000000000000, 0x03000000000000000000000000000000000000000000000000, 0x04000000000000000000000000000000000000000000000000, 0x00
2121
// gas irOptimized: 232995
2222
// gas legacy: 235694
23-
// gas legacyOptimized: 235193
23+
// gas legacyOptimized: 235129

test/libsolidity/semanticTests/array/copying/array_of_function_external_storage_to_storage_dynamic.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ contract C {
4545
}
4646
// ----
4747
// copyExternalStorageArrayOfFunctionType() -> true
48-
// gas irOptimized: 104566
48+
// gas irOptimized: 104554
4949
// gas legacy: 108554
5050
// gas legacyOptimized: 102405
5151
// copyInternalArrayOfFunctionType() -> true

test/libsolidity/semanticTests/array/copying/array_of_function_external_storage_to_storage_dynamic_different_mutability.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ contract C {
4848
}
4949
// ----
5050
// copyExternalStorageArraysOfFunctionType() -> true
51-
// gas irOptimized: 104238
51+
// gas irOptimized: 104226
5252
// gas legacy: 108295
5353
// gas legacyOptimized: 102162
5454
// copyInternalArrayOfFunctionType() -> true

test/libsolidity/semanticTests/array/copying/array_of_struct_memory_to_storage.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ contract C {
1919
// compileViaYul: true
2020
// ----
2121
// f() -> 10, 11, 12
22-
// gas irOptimized: 118796
22+
// gas irOptimized: 118784

test/libsolidity/semanticTests/array/copying/array_of_structs_containing_arrays_memory_to_storage.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ contract C {
2626
// compileViaYul: true
2727
// ----
2828
// f() -> 3, 3, 3, 1
29-
// gas irOptimized: 181928
29+
// gas irOptimized: 181904

test/libsolidity/semanticTests/array/copying/array_storage_multi_items_per_slot.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ contract C {
1212
}
1313
// ----
1414
// f() -> 1, 2, 3
15-
// gas irOptimized: 131939
15+
// gas irOptimized: 131935
1616
// gas legacy: 134605
17-
// gas legacyOptimized: 131938
17+
// gas legacyOptimized: 131934

test/libsolidity/semanticTests/array/copying/bytes_storage_to_storage.sol

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ contract c {
1717
// ----
1818
// f(uint256): 0 -> 0x20, 0x00
1919
// f(uint256): 31 -> 0x20, 0x1f, 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e00
20-
// gas irOptimized: 103268
20+
// gas irOptimized: 103144
2121
// gas legacy: 112904
22-
// gas legacyOptimized: 112647
22+
// gas legacyOptimized: 112027
2323
// f(uint256): 32 -> 0x20, 0x20, 1780731860627700044960722568376592200742329637303199754547598369979440671
24-
// gas irOptimized: 117825
24+
// gas irOptimized: 117697
2525
// gas legacy: 128964
26-
// gas legacyOptimized: 128854
26+
// gas legacyOptimized: 128214
2727
// f(uint256): 33 -> 0x20, 33, 1780731860627700044960722568376592200742329637303199754547598369979440671, 0x2000000000000000000000000000000000000000000000000000000000000000
28-
// gas irOptimized: 124091
28+
// gas irOptimized: 123959
2929
// gas legacy: 136092
30-
// gas legacyOptimized: 135469
30+
// gas legacyOptimized: 134809
3131
// f(uint256): 63 -> 0x20, 0x3f, 1780731860627700044960722568376592200742329637303199754547598369979440671, 14532552714582660066924456880521368950258152170031413196862950297402215316992
32-
// gas irOptimized: 127151
32+
// gas irOptimized: 126899
3333
// gas legacy: 148692
34-
// gas legacyOptimized: 148699
34+
// gas legacyOptimized: 147439
3535
// f(uint256): 12 -> 0x20, 0x0c, 0x0102030405060708090a0b0000000000000000000000000000000000000000
3636
// gas legacy: 59345
3737
// gas legacyOptimized: 57279
3838
// f(uint256): 129 -> 0x20, 0x81, 1780731860627700044960722568376592200742329637303199754547598369979440671, 0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f, 29063324697304692433803953038474361308315562010425523193971352996434451193439, 0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f, -57896044618658097711785492504343953926634992332820282019728792003956564819968
39-
// gas irOptimized: 416918
39+
// gas irOptimized: 416402
4040
// gas legacy: 458997
41-
// gas legacyOptimized: 460664
41+
// gas legacyOptimized: 458084

test/libsolidity/semanticTests/array/copying/function_type_array_to_storage.sol

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ contract C {
4848
// test() -> 0x20, 0x14, "[a called][b called]"
4949
// gas irOptimized: 116518
5050
// gas legacy: 118841
51-
// gas legacyOptimized: 116843
51+
// gas legacyOptimized: 116827
5252
// test2() -> 0x20, 0x14, "[b called][a called]"
5353
// test3() -> 0x20, 0x14, "[b called][a called]"
54-
// gas irOptimized: 103144
54+
// gas irOptimized: 103128
5555
// gas legacy: 102654
56-
// gas legacyOptimized: 101556
56+
// gas legacyOptimized: 101548

test/libsolidity/semanticTests/array/copying/memory_dyn_2d_bytes_to_storage.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ contract C {
1818
}
1919
// ----
2020
// f() -> 3
21-
// gas irOptimized: 128296
21+
// gas irOptimized: 128256
2222
// gas legacy: 129077
23-
// gas legacyOptimized: 128210
23+
// gas legacyOptimized: 128130

test/libsolidity/semanticTests/array/create_memory_array.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ contract C {
1818
}
1919
// ----
2020
// f() -> "A", 8, 4, "B"
21-
// gas irOptimized: 136664
21+
// gas irOptimized: 136660
2222
// gas legacy: 121380
23-
// gas legacyOptimized: 115488
23+
// gas legacyOptimized: 115472

test/libsolidity/semanticTests/array/delete/bytes_delete_element.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ contract c {
1818
// test1() -> true
1919
// gas irOptimized: 218449
2020
// gas legacy: 242263
21-
// gas legacyOptimized: 241182
21+
// gas legacyOptimized: 241174

test/libsolidity/semanticTests/array/fixed_arrays_as_return_type.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ contract B {
1818
}
1919
// ----
2020
// f() -> 2, 3, 4, 5, 6, 1000, 1001, 1002, 1003, 1004
21-
// gas irOptimized: 59212
21+
// gas irOptimized: 59208
2222
// gas irOptimized code: 56600
2323
// gas legacy: 68001
2424
// gas legacy code: 162000
25-
// gas legacyOptimized: 59997
25+
// gas legacyOptimized: 59989
2626
// gas legacyOptimized code: 70600

0 commit comments

Comments
 (0)