Skip to content

Commit 2270fec

Browse files
committed
Remove trivial blocks
1 parent 1993ed6 commit 2270fec

File tree

95 files changed

+424
-305
lines changed

Some content is hidden

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

95 files changed

+424
-305
lines changed

libevmasm/Assembly.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <libevmasm/JumpdestRemover.h>
3030
#include <libevmasm/BlockDeduplicator.h>
3131
#include <libevmasm/ConstantOptimiser.h>
32+
#include <libevmasm/TrivialBlockRemover.h>
3233

3334
#include <liblangutil/CharStream.h>
3435
#include <liblangutil/Exceptions.h>
@@ -933,6 +934,16 @@ std::map<u256, u256> const& Assembly::optimiseInternal(
933934
}
934935
}
935936

937+
if (_settings.runInliner && !m_eofVersion.has_value())
938+
{
939+
for (auto& codeSection: m_codeSections)
940+
{
941+
TrivialBlockRemover trivialBlockRemover{codeSection.items};
942+
if (trivialBlockRemover.optimise(_tagsReferencedFromOutside))
943+
count++;
944+
}
945+
}
946+
936947
// TODO: investigate for EOF
937948
if (_settings.runCSE && !m_eofVersion.has_value())
938949
{

libevmasm/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ set(sources
4545
SimplificationRules.cpp
4646
SimplificationRules.h
4747
SubAssemblyID.h
48+
TrivialBlockRemover.cpp
49+
TrivialBlockRemover.h
4850
)
4951

5052
add_library(evmasm ${sources})

libevmasm/TrivialBlockRemover.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
// SPDX-License-Identifier: GPL-3.0
18+
19+
#include <libevmasm/TrivialBlockRemover.h>
20+
21+
#include <libevmasm/AssemblyItem.h>
22+
#include <libevmasm/BlockDeduplicator.h>
23+
#include <libevmasm/SemanticInformation.h>
24+
25+
#include <range/v3/view/drop.hpp>
26+
#include <range/v3/view/enumerate.hpp>
27+
28+
bool solidity::evmasm::TrivialBlockRemover::optimise(std::set<size_t> const& _tagsReferencedFromOutside)
29+
{
30+
std::map<u256, u256> replacedTags;
31+
for (auto&& [index, item]: m_items | ranges::views::enumerate | ranges::views::drop(1))
32+
{
33+
if (item.type() != Tag)
34+
continue;
35+
if (index >= m_items.size() - 2)
36+
continue;
37+
auto const & next = m_items[index + 1];
38+
if (next.type() != PushTag)
39+
continue;
40+
if (next.data() == item.data())
41+
continue;
42+
auto const & nextNext = m_items[index + 2];
43+
if (nextNext.type() != Operation)
44+
continue;
45+
if (nextNext.instruction() != Instruction::JUMP)
46+
continue;
47+
auto const & previous = m_items[index - 1];
48+
if (previous.type() != Operation)
49+
continue;
50+
if (previous.instruction() != Instruction::JUMP && !SemanticInformation::terminatesControlFlow(previous))
51+
continue;
52+
auto const [subId, tag] = item.splitForeignPushTag();
53+
solAssert(subId.empty(), "Sub-assembly tag used as label.");
54+
if (_tagsReferencedFromOutside.contains(tag))
55+
continue;
56+
replacedTags.insert({item.data(), next.data()});
57+
}
58+
if (!replacedTags.empty())
59+
{
60+
return BlockDeduplicator::applyTagReplacement(m_items, replacedTags);
61+
}
62+
return false;
63+
}

libevmasm/TrivialBlockRemover.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
// SPDX-License-Identifier: GPL-3.0
18+
19+
#pragma once
20+
21+
#include <libevmasm/SubAssemblyID.h>
22+
23+
#include <set>
24+
25+
namespace solidity::evmasm
26+
{
27+
28+
class AssemblyItem;
29+
using AssemblyItems = std::vector<AssemblyItem>;
30+
31+
class TrivialBlockRemover {
32+
public:
33+
explicit TrivialBlockRemover(AssemblyItems& _items): m_items(_items) {}
34+
35+
bool optimise(std::set<size_t> const& _tagsReferencedFromOutside);
36+
37+
private:
38+
AssemblyItems& m_items;
39+
};
40+
41+
}
42+
43+
44+

test/cmdlineTests/function_debug_info/output

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"function-debug-runtime": {
1717
"@f_14": {
18-
"entryPoint": 124,
18+
"entryPoint": 119,
1919
"id": 14,
2020
"parameterSlots": 2,
2121
"returnSlots": 1
@@ -26,12 +26,12 @@
2626
"returnSlots": 0
2727
},
2828
"abi_decode_tuple_t_array$_t_uint256_$dyn_calldata_ptr": {
29-
"entryPoint": 158,
29+
"entryPoint": 153,
3030
"parameterSlots": 2,
3131
"returnSlots": 2
3232
},
3333
"abi_decode_tuple_t_uint256": {
34-
"entryPoint": 271,
34+
"entryPoint": 266,
3535
"parameterSlots": 2,
3636
"returnSlots": 1
3737
},
@@ -40,7 +40,7 @@
4040
"returnSlots": 1
4141
},
4242
"panic_error_0x32": {
43-
"entryPoint": 294,
43+
"entryPoint": 289,
4444
"parameterSlots": 0,
4545
"returnSlots": 0
4646
}

test/libsolidity/gasTests/abiv2_optimised.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ contract C {
1919
// optimize-yul: true
2020
// ----
2121
// creation:
22-
// codeDepositCost: 618200
22+
// codeDepositCost: 615200
2323
// executionCost: 649
24-
// totalCost: 618849
24+
// totalCost: 615849
2525
// external:
2626
// a(): 2283
27-
// b(uint256): 4649
27+
// b(uint256): 4637
2828
// f1(uint256): 304
2929
// f2(uint256[],string[],uint16,address): infinite
3030
// f3(uint16[],string[],uint16,address): infinite

test/libsolidity/gasTests/dispatch_large_optimised.sol

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,29 @@ contract Large {
2929
// optimize-runs: 2
3030
// ----
3131
// creation:
32-
// codeDepositCost: 224600
33-
// executionCost: 267
34-
// totalCost: 224867
32+
// codeDepositCost: 213600
33+
// executionCost: 255
34+
// totalCost: 213855
3535
// external:
3636
// a(): 2281
37-
// b(uint256): 4934
38-
// f0(uint256): 363
39-
// f1(uint256): 47002
40-
// f2(uint256): 24967
41-
// f3(uint256): 25055
42-
// f4(uint256): 25033
43-
// f5(uint256): 25011
44-
// f6(uint256): 24923
45-
// f7(uint256): 24703
46-
// f8(uint256): 24835
47-
// f9(uint256): 24857
48-
// g0(uint256): 603
49-
// g1(uint256): 46714
50-
// g2(uint256): 24701
51-
// g3(uint256): 24789
52-
// g4(uint256): 24767
53-
// g5(uint256): 24855
54-
// g6(uint256): 24635
55-
// g7(uint256): 24745
56-
// g8(uint256): 24723
57-
// g9(uint256): 24569
37+
// b(uint256): 4922
38+
// f0(uint256): 351
39+
// f1(uint256): 46990
40+
// f2(uint256): 24955
41+
// f3(uint256): 25043
42+
// f4(uint256): 25021
43+
// f5(uint256): 24999
44+
// f6(uint256): 24911
45+
// f7(uint256): 24691
46+
// f8(uint256): 24823
47+
// f9(uint256): 24845
48+
// g0(uint256): 591
49+
// g1(uint256): 46702
50+
// g2(uint256): 24689
51+
// g3(uint256): 24777
52+
// g4(uint256): 24755
53+
// g5(uint256): 24843
54+
// g6(uint256): 24623
55+
// g7(uint256): 24733
56+
// g8(uint256): 24711
57+
// g9(uint256): 24557

test/libsolidity/gasTests/dispatch_medium_optimised.sol

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@ contract Medium {
1212
// ====
1313
// EVMVersion: =current
1414
// bytecodeFormat: legacy
15-
// ====
1615
// optimize: true
1716
// optimize-runs: 2
1817
// ----
1918
// creation:
20-
// codeDepositCost: 126000
21-
// executionCost: 169
22-
// totalCost: 126169
19+
// codeDepositCost: 118000
20+
// executionCost: 163
21+
// totalCost: 118163
2322
// external:
2423
// a(): 2281
25-
// b(uint256): 4692
26-
// f1(uint256): 46782
27-
// f2(uint256): 24725
28-
// f3(uint256): 24769
29-
// g0(uint256): 361
30-
// g7(uint256): 24635
31-
// g8(uint256): 24613
32-
// g9(uint256): 24569
24+
// b(uint256): 4680
25+
// f1(uint256): 46770
26+
// f2(uint256): 24713
27+
// f3(uint256): 24757
28+
// g0(uint256): 349
29+
// g7(uint256): 24623
30+
// g8(uint256): 24601
31+
// g9(uint256): 24557

test/libsolidity/gasTests/dispatch_small_optimised.sol

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ contract Small {
66
}
77
// ====
88
// EVMVersion: =current
9+
// bytecodeFormat: legacy
910
// optimize: true
1011
// optimize-runs: 2
11-
// bytecodeFormat: legacy
1212
// ----
1313
// creation:
14-
// codeDepositCost: 58200
15-
// executionCost: 109
16-
// totalCost: 58309
14+
// codeDepositCost: 56200
15+
// executionCost: 103
16+
// totalCost: 56303
1717
// external:
1818
// fallback: 117
1919
// a(): 2259
20-
// b(uint256): 4582
21-
// f1(uint256): 46716
20+
// b(uint256): 4570
21+
// f1(uint256): 46704

test/libsolidity/semanticTests/abiEncodeDecode/abi_decode_simple_storage.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ contract C {
1010
// f(bytes): 0x20, 0x80, 0x21, 0x40, 0x7, "abcdefg" -> 0x21, 0x40, 0x7, "abcdefg"
1111
// gas irOptimized: 135441
1212
// gas legacy: 137095
13-
// gas legacyOptimized: 135828
13+
// gas legacyOptimized: 135792

0 commit comments

Comments
 (0)