aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/SolidityOptimizer.cpp
blob: 71463f919b79be293fc62f959516b4db91d147ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/*
    This file is part of cpp-ethereum.

    cpp-ethereum is free software: you can redistribute it and/or modify
    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.

    cpp-ethereum is distributed in the hope that it will be useful,
    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
    along with cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @author Christian <c@ethdev.com>
 * @date 2014
 * Tests for the Solidity optimizer.
 */

#include <string>
#include <tuple>
#include <memory>
#include <boost/test/unit_test.hpp>
#include <boost/lexical_cast.hpp>
#include <test/libsolidity/solidityExecutionFramework.h>
#include <libevmasm/CommonSubexpressionEliminator.h>
#include <libevmasm/ControlFlowGraph.h>
#include <libevmasm/Assembly.h>

using namespace std;
using namespace dev::eth;

namespace dev
{
namespace solidity
{
namespace test
{

class OptimizerTestFramework: public ExecutionFramework
{
public:
    OptimizerTestFramework() { }
    /// Compiles the source code with and without optimizing.
    void compileBothVersions(
        std::string const& _sourceCode,
        u256 const& _value = 0,
        std::string const& _contractName = ""
    )
    {
        m_optimize = false;
        bytes nonOptimizedBytecode = compileAndRun(_sourceCode, _value, _contractName);
        m_nonOptimizedContract = m_contractAddress;
        m_optimize = true;
        bytes optimizedBytecode = compileAndRun(_sourceCode, _value, _contractName);
        size_t nonOptimizedSize = 0;
        eth::eachInstruction(nonOptimizedBytecode, [&](Instruction, u256 const&) {
            nonOptimizedSize++;
        });
        size_t optimizedSize = 0;
        eth::eachInstruction(optimizedBytecode, [&](Instruction, u256 const&) {
            optimizedSize++;
        });
        BOOST_CHECK_MESSAGE(
            nonOptimizedSize > optimizedSize,
            "Optimizer did not reduce bytecode size."
        );
        m_optimizedContract = m_contractAddress;
    }

    template <class... Args>
    void compareVersions(std::string _sig, Args const&... _arguments)
    {
        m_contractAddress = m_nonOptimizedContract;
        bytes nonOptimizedOutput = callContractFunction(_sig, _arguments...);
        m_contractAddress = m_optimizedContract;
        bytes optimizedOutput = callContractFunction(_sig, _arguments...);
        BOOST_CHECK_MESSAGE(nonOptimizedOutput == optimizedOutput, "Computed values do not match."
                            "\nNon-Optimized: " + toHex(nonOptimizedOutput) +
                            "\nOptimized:     " + toHex(optimizedOutput));
    }

    AssemblyItems getCSE(AssemblyItems const& _input)
    {
        // add dummy locations to each item so that we can check that they are not deleted
        AssemblyItems input = _input;
        for (AssemblyItem& item: input)
            item.setLocation(SourceLocation(1, 3, make_shared<string>("")));

        eth::KnownState state;
        eth::CommonSubexpressionEliminator cse(state);
        BOOST_REQUIRE(cse.feedItems(input.begin(), input.end()) == input.end());
        AssemblyItems output = cse.getOptimizedItems();

        for (AssemblyItem const& item: output)
        {
            BOOST_CHECK(item == Instruction::POP || !item.getLocation().isEmpty());
        }
        return output;
    }

    void checkCSE(AssemblyItems const& _input, AssemblyItems const& _expectation)
    {
        AssemblyItems output = getCSE(_input);
        BOOST_CHECK_EQUAL_COLLECTIONS(_expectation.begin(), _expectation.end(), output.begin(), output.end());
    }

    void checkCFG(AssemblyItems const& _input, AssemblyItems const& _expectation)
    {
        AssemblyItems output = _input;
        // Running it four times should be enough for these tests.
        for (unsigned i = 0; i < 4; ++i)
        {
            eth::ControlFlowGraph cfg(output);
            output = cfg.optimisedItems();
        }
        BOOST_CHECK_EQUAL_COLLECTIONS(_expectation.begin(), _expectation.end(), output.begin(), output.end());
    }

protected:
    Address m_optimizedContract;
    Address m_nonOptimizedContract;
};

BOOST_FIXTURE_TEST_SUITE(SolidityOptimizer, OptimizerTestFramework)

BOOST_AUTO_TEST_CASE(smoke_test)
{
    char const* sourceCode = R"(
        contract test {
            function f(uint a) returns (uint b) {
                return a;
            }
        })";
    compileBothVersions(sourceCode);
    compareVersions("f(uint256)", u256(7));
}

BOOST_AUTO_TEST_CASE(identities)
{
    char const* sourceCode = R"(
        contract test {
            function f(int a) returns (int b) {
                return int(0) | (int(1) * (int(0) ^ (0 + a)));
            }
        })";
    compileBothVersions(sourceCode);
    compareVersions("f(uint256)", u256(0x12334664));
}

BOOST_AUTO_TEST_CASE(unused_expressions)
{
    char const* sourceCode = R"(
        contract test {
            uint data;
            function f() returns (uint a, uint b) {
                10 + 20;
                data;
            }
        })";
    compileBothVersions(sourceCode);
    compareVersions("f()");
}

BOOST_AUTO_TEST_CASE(constant_folding_both_sides)
{
    // if constants involving the same associative and commutative operator are applied from both
    // sides, the operator should be applied only once, because the expression compiler pushes
    // literals as late as possible
    char const* sourceCode = R"(
        contract test {
            function f(uint x) returns (uint y) {
                return 98 ^ (7 * ((1 | (x | 1000)) * 40) ^ 102);
            }
        })";
    compileBothVersions(sourceCode);
    compareVersions("f(uint256)");
}

BOOST_AUTO_TEST_CASE(storage_access)
{
    char const* sourceCode = R"(
        contract test {
            uint8[40] data;
            function f(uint x) returns (uint y) {
                data[2] = data[7] = uint8(x);
                data[4] = data[2] * 10 + data[3];
            }
        }
    )";
    compileBothVersions(sourceCode);
    compareVersions("f(uint256)");
}

BOOST_AUTO_TEST_CASE(array_copy)
{
    char const* sourceCode = R"(
        contract test {
            bytes2[] data1;
            bytes5[] data2;
            function f(uint x) returns (uint l, uint y) {
                for (uint i = 0; i < msg.data.length; ++i)
                    data1[i] = msg.data[i];
                data2 = data1;
                l = data2.length;
                y = uint(data2[x]);
            }
        }
    )";
    compileBothVersions(sourceCode);
    compareVersions("f(uint256)", 0);
    compareVersions("f(uint256)", 10);
    compareVersions("f(uint256)", 36);
}

BOOST_AUTO_TEST_CASE(function_calls)
{
    char const* sourceCode = R"(
        contract test {
            function f1(uint x) returns (uint) { return x*x; }
            function f(uint x) returns (uint) { return f1(7+x) - this.f1(x**9); }
        }
    )";
    compileBothVersions(sourceCode);
    compareVersions("f(uint256)", 0);
    compareVersions("f(uint256)", 10);
    compareVersions("f(uint256)", 36);
}

BOOST_AUTO_TEST_CASE(cse_intermediate_swap)
{
    eth::KnownState state;
    eth::CommonSubexpressionEliminator cse(state);
    AssemblyItems input{
        Instruction::SWAP1, Instruction::POP, Instruction::ADD, u256(0), Instruction::SWAP1,
        Instruction::SLOAD, Instruction::SWAP1, u256(100), Instruction::EXP, Instruction::SWAP1,
        Instruction::DIV, u256(0xff), Instruction::AND
    };
    BOOST_REQUIRE(cse.feedItems(input.begin(), input.end()) == input.end());
    AssemblyItems output = cse.getOptimizedItems();
    BOOST_CHECK(!output.empty());
}

BOOST_AUTO_TEST_CASE(cse_negative_stack_access)
{
    AssemblyItems input{Instruction::DUP2, u256(0)};
    checkCSE(input, input);
}

BOOST_AUTO_TEST_CASE(cse_negative_stack_end)
{
    AssemblyItems input{Instruction::ADD};
    checkCSE(input, input);
}

BOOST_AUTO_TEST_CASE(cse_intermediate_negative_stack)
{
    AssemblyItems input{Instruction::ADD, u256(1), Instruction::DUP1};
    checkCSE(input, input);
}

BOOST_AUTO_TEST_CASE(cse_pop)
{
    checkCSE({Instruction::POP}, {Instruction::POP});
}

BOOST_AUTO_TEST_CASE(cse_unneeded_items)
{
    AssemblyItems input{
        Instruction::ADD,
        Instruction::SWAP1,
        Instruction::POP,
        u256(7),
        u256(8),
    };
    checkCSE(input, input);
}

BOOST_AUTO_TEST_CASE(cse_constant_addition)
{
    AssemblyItems input{u256(7), u256(8), Instruction::ADD};
    checkCSE(input, {u256(7 + 8)});
}

BOOST_AUTO_TEST_CASE(cse_invariants)
{
    AssemblyItems input{
        Instruction::DUP1,
        Instruction::DUP1,
        u256(0),
        Instruction::OR,
        Instruction::OR
    };
    checkCSE(input, {Instruction::DUP1});
}

BOOST_AUTO_TEST_CASE(cse_subself)
{
    checkCSE({Instruction::DUP1, Instruction::SUB}, {Instruction::POP, u256(0)});
}

BOOST_AUTO_TEST_CASE(cse_subother)
{
    checkCSE({Instruction::SUB}, {Instruction::SUB});
}

BOOST_AUTO_TEST_CASE(cse_double_negation)
{
    checkCSE({Instruction::DUP5, Instruction::NOT, Instruction::NOT}, {Instruction::DUP5});
}

BOOST_AUTO_TEST_CASE(cse_associativity)
{
    AssemblyItems input{
        Instruction::DUP1,
        Instruction::DUP1,
        u256(0),
        Instruction::OR,
        Instruction::OR
    };
    checkCSE(input, {Instruction::DUP1});
}

BOOST_AUTO_TEST_CASE(cse_associativity2)
{
    AssemblyItems input{
        u256(0),
        Instruction::DUP2,
        u256(2),
        u256(1),
        Instruction::DUP6,
        Instruction::ADD,
        u256(2),
        Instruction::ADD,
        Instruction::ADD,
        Instruction::ADD,
        Instruction::ADD
    };
    checkCSE(input, {Instruction::DUP2, Instruction::DUP2, Instruction::ADD, u256(5), Instruction::ADD});
}

BOOST_AUTO_TEST_CASE(cse_storage)
{
    AssemblyItems input{
        u256(0),
        Instruction::SLOAD,
        u256(0),
        Instruction::SLOAD,
        Instruction::ADD,
        u256(0),
        Instruction::SSTORE
    };
    checkCSE(input, {
        u256(0),
        Instruction::DUP1,
        Instruction::SLOAD,
        Instruction::DUP1,
        Instruction::ADD,
        Instruction::SWAP1,
        Instruction::SSTORE
    });
}

BOOST_AUTO_TEST_CASE(cse_noninterleaved_storage)
{
    // two stores to the same location should be replaced by only one store, even if we
    // read in the meantime
    AssemblyItems input{
        u256(7),
        Instruction::DUP2,
        Instruction::SSTORE,
        Instruction::DUP1,
        Instruction::SLOAD,
        u256(8),
        Instruction::DUP3,
        Instruction::SSTORE
    };
    checkCSE(input, {
        u256(8),
        Instruction::DUP2,
        Instruction::SSTORE,
        u256(7)
    });
}

BOOST_AUTO_TEST_CASE(cse_interleaved_storage)
{
    // stores and reads to/from two unknown locations, should not optimize away the first store
    AssemblyItems input{
        u256(7),
        Instruction::DUP2,
        Instruction::SSTORE, // store to "DUP1"
        Instruction::DUP2,
        Instruction::SLOAD, // read from "DUP2", might be equal to "DUP1"
        u256(0),
        Instruction::DUP3,
        Instruction::SSTORE // store different value to "DUP1"
    };
    checkCSE(input, input);
}

BOOST_AUTO_TEST_CASE(cse_interleaved_storage_same_value)
{
    // stores and reads to/from two unknown locations, should not optimize away the first store
    // but it should optimize away the second, since we already know the value will be the same
    AssemblyItems input{
        u256(7),
        Instruction::DUP2,
        Instruction::SSTORE, // store to "DUP1"
        Instruction::DUP2,
        Instruction::SLOAD, // read from "DUP2", might be equal to "DUP1"
        u256(6),
        u256(1),
        Instruction::ADD,
        Instruction::DUP3,
        Instruction::SSTORE // store same value to "DUP1"
    };
    checkCSE(input, {
        u256(7),
        Instruction::DUP2,
        Instruction::SSTORE,
        Instruction::DUP2,
        Instruction::SLOAD
    });
}

BOOST_AUTO_TEST_CASE(cse_interleaved_storage_at_known_location)
{
    // stores and reads to/from two known locations, should optimize away the first store,
    // because we know that the location is different
    AssemblyItems input{
        u256(0x70),
        u256(1),
        Instruction::SSTORE, // store to 1
        u256(2),
        Instruction::SLOAD, // read from 2, is different from 1
        u256(0x90),
        u256(1),
        Instruction::SSTORE // store different value at 1
    };
    checkCSE(input, {
        u256(2),
        Instruction::SLOAD,
        u256(0x90),
        u256(1),
        Instruction::SSTORE
    });
}

BOOST_AUTO_TEST_CASE(cse_interleaved_storage_at_known_location_offset)
{
    // stores and reads to/from two locations which are known to be different,
    // should optimize away the first store, because we know that the location is different
    AssemblyItems input{
        u256(0x70),
        Instruction::DUP2,
        u256(1),
        Instruction::ADD,
        Instruction::SSTORE, // store to "DUP1"+1
        Instruction::DUP1,
        u256(2),
        Instruction::ADD,
        Instruction::SLOAD, // read from "DUP1"+2, is different from "DUP1"+1
        u256(0x90),
        Instruction::DUP3,
        u256(1),
        Instruction::ADD,
        Instruction::SSTORE // store different value at "DUP1"+1
    };
    checkCSE(input, {
        u256(2),
        Instruction::DUP2,
        Instruction::ADD,
        Instruction::SLOAD,
        u256(0x90),
        u256(1),
        Instruction::DUP4,
        Instruction::ADD,
        Instruction::SSTORE
    });
}

BOOST_AUTO_TEST_CASE(cse_interleaved_memory_at_known_location_offset)
{
    // stores and reads to/from two locations which are known to be different,
    // should not optimize away the first store, because the location overlaps with the load,
    // but it should optimize away the second, because we know that the location is different by 32
    AssemblyItems input{
        u256(0x50),
        Instruction::DUP2,
        u256(2),
        Instruction::ADD,
        Instruction::MSTORE, // ["DUP1"+2] = 0x50
        u256(0x60),
        Instruction::DUP2,
        u256(32),
        Instruction::ADD,
        Instruction::MSTORE, // ["DUP1"+32] = 0x60
        Instruction::DUP1,
        Instruction::MLOAD, // read from "DUP1"
        u256(0x70),
        Instruction::DUP3,
        u256(32),
        Instruction::ADD,
        Instruction::MSTORE, // ["DUP1"+32] = 0x70
        u256(0x80),
        Instruction::DUP3,
        u256(2),
        Instruction::ADD,
        Instruction::MSTORE, // ["DUP1"+2] = 0x80
    };
    // If the actual code changes too much, we could also simply check that the output contains
    // exactly 3 MSTORE and exactly 1 MLOAD instruction.
    checkCSE(input, {
        u256(0x50),
        u256(2),
        Instruction::DUP3,
        Instruction::ADD,
        Instruction::SWAP1,
        Instruction::DUP2,
        Instruction::MSTORE, // ["DUP1"+2] = 0x50
        Instruction::DUP2,
        Instruction::MLOAD, // read from "DUP1"
        u256(0x70),
        u256(32),
        Instruction::DUP5,
        Instruction::ADD,
        Instruction::MSTORE, // ["DUP1"+32] = 0x70
        u256(0x80),
        Instruction::SWAP1,
        Instruction::SWAP2,
        Instruction::MSTORE // ["DUP1"+2] = 0x80
    });
}

BOOST_AUTO_TEST_CASE(cse_deep_stack)
{
    AssemblyItems input{
        Instruction::ADD,
        Instruction::SWAP1,
        Instruction::POP,
        Instruction::SWAP8,
        Instruction::POP,
        Instruction::SWAP8,
        Instruction::POP,
        Instruction::SWAP8,
        Instruction::SWAP5,
        Instruction::POP,
        Instruction::POP,
        Instruction::POP,
        Instruction::POP,
        Instruction::POP,
    };
    checkCSE(input, {
        Instruction::SWAP4,
        Instruction::SWAP12,
        Instruction::SWAP3,
        Instruction::SWAP11,
        Instruction::POP,
        Instruction::SWAP1,
        Instruction::SWAP3,
        Instruction::ADD,
        Instruction::SWAP8,
        Instruction::POP,
        Instruction::SWAP6,
        Instruction::POP,
        Instruction::POP,
        Instruction::POP,
        Instruction::POP,
        Instruction::POP,
        Instruction::POP,
    });
}

BOOST_AUTO_TEST_CASE(cse_jumpi_no_jump)
{
    AssemblyItems input{
        u256(0),
        u256(1),
        Instruction::DUP2,
        AssemblyItem(PushTag, 1),
        Instruction::JUMPI
    };
    checkCSE(input, {
        u256(0),
        u256(1)
    });
}

BOOST_AUTO_TEST_CASE(cse_jumpi_jump)
{
    AssemblyItems input{
        u256(1),
        u256(1),
        Instruction::DUP2,
        AssemblyItem(PushTag, 1),
        Instruction::JUMPI
    };
    checkCSE(input, {
        u256(1),
        Instruction::DUP1,
        AssemblyItem(PushTag, 1),
        Instruction::JUMP
    });
}

BOOST_AUTO_TEST_CASE(cse_empty_sha3)
{
    AssemblyItems input{
        u256(0),
        Instruction::DUP2,
        Instruction::SHA3
    };
    checkCSE(input, {
        u256(sha3(bytesConstRef()))
    });
}

BOOST_AUTO_TEST_CASE(cse_partial_sha3)
{
    AssemblyItems input{
        u256(0xabcd) << (256 - 16),
        u256(0),
        Instruction::MSTORE,
        u256(2),
        u256(0),
        Instruction::SHA3
    };
    checkCSE(input, {
        u256(0xabcd) << (256 - 16),
        u256(0),
        Instruction::MSTORE,
        u256(sha3(bytes{0xab, 0xcd}))
    });
}

BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_location)
{
    // sha3 twice from same dynamic location
    AssemblyItems input{
        Instruction::DUP2,
        Instruction::DUP1,
        Instruction::MSTORE,
        u256(64),
        Instruction::DUP2,
        Instruction::SHA3,
        u256(64),
        Instruction::DUP3,
        Instruction::SHA3
    };
    checkCSE(input, {
        Instruction::DUP2,
        Instruction::DUP1,
        Instruction::MSTORE,
        u256(64),
        Instruction::DUP2,
        Instruction::SHA3,
        Instruction::DUP1
    });
}

BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content)
{
    // sha3 twice from different dynamic location but with same content
    AssemblyItems input{
        Instruction::DUP1,
        u256(0x80),
        Instruction::MSTORE, // m[128] = DUP1
        u256(0x20),
        u256(0x80),
        Instruction::SHA3, // sha3(m[128..(128+32)])
        Instruction::DUP2,
        u256(12),
        Instruction::MSTORE, // m[12] = DUP1
        u256(0x20),
        u256(12),
        Instruction::SHA3 // sha3(m[12..(12+32)])
    };
    checkCSE(input, {
        u256(0x80),
        Instruction::DUP2,
        Instruction::DUP2,
        Instruction::MSTORE,
        u256(0x20),
        Instruction::SWAP1,
        Instruction::SHA3,
        u256(12),
        Instruction::DUP3,
        Instruction::SWAP1,
        Instruction::MSTORE,
        Instruction::DUP1
    });
}

BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content_dynamic_store_in_between)
{
    // sha3 twice from different dynamic location but with same content,
    // dynamic mstore in between, which forces us to re-calculate the sha3
    AssemblyItems input{
        u256(0x80),
        Instruction::DUP2,
        Instruction::DUP2,
        Instruction::MSTORE, // m[128] = DUP1
        u256(0x20),
        Instruction::DUP1,
        Instruction::DUP3,
        Instruction::SHA3, // sha3(m[128..(128+32)])
        u256(12),
        Instruction::DUP5,
        Instruction::DUP2,
        Instruction::MSTORE, // m[12] = DUP1
        Instruction::DUP12,
        Instruction::DUP14,
        Instruction::MSTORE, // destroys memory knowledge
        Instruction::SWAP2,
        Instruction::SWAP1,
        Instruction::SWAP2,
        Instruction::SHA3 // sha3(m[12..(12+32)])
    };
    checkCSE(input, input);
}

BOOST_AUTO_TEST_CASE(cse_sha3_twice_same_content_noninterfering_store_in_between)
{
    // sha3 twice from different dynamic location but with same content,
    // dynamic mstore in between, but does not force us to re-calculate the sha3
    AssemblyItems input{
        u256(0x80),
        Instruction::DUP2,
        Instruction::DUP2,
        Instruction::MSTORE, // m[128] = DUP1
        u256(0x20),
        Instruction::DUP1,
        Instruction::DUP3,
        Instruction::SHA3, // sha3(m[128..(128+32)])
        u256(12),
        Instruction::DUP5,
        Instruction::DUP2,
        Instruction::MSTORE, // m[12] = DUP1
        Instruction::DUP12,
        u256(12 + 32),
        Instruction::MSTORE, // does not destoy memory knowledge
        Instruction::DUP13,
        u256(128 - 32),
        Instruction::MSTORE, // does not destoy memory knowledge
        u256(0x20),
        u256(12),
        Instruction::SHA3 // sha3(m[12..(12+32)])
    };
    // if this changes too often, only count the number of SHA3 and MSTORE instructions
    AssemblyItems output = getCSE(input);
    BOOST_CHECK_EQUAL(4, count(output.begin(), output.end(), AssemblyItem(Instruction::MSTORE)));
    BOOST_CHECK_EQUAL(1, count(output.begin(), output.end(), AssemblyItem(Instruction::SHA3)));
}

BOOST_AUTO_TEST_CASE(control_flow_graph_remove_unused)
{
    // remove parts of the code that are unused
    AssemblyItems input{
        AssemblyItem(PushTag, 1),
        Instruction::JUMP,
        u256(7),
        AssemblyItem(Tag, 1),
    };
    checkCFG(input, {});
}

BOOST_AUTO_TEST_CASE(control_flow_graph_remove_unused_loop)
{
    AssemblyItems input{
        AssemblyItem(PushTag, 3),
        Instruction::JUMP,
        AssemblyItem(Tag, 1),
        u256(7),
        AssemblyItem(PushTag, 2),
        Instruction::JUMP,
        AssemblyItem(Tag, 2),
        u256(8),
        AssemblyItem(PushTag, 1),
        Instruction::JUMP,
        AssemblyItem(Tag, 3),
        u256(11)
    };
    checkCFG(input, {u256(11)});
}

BOOST_AUTO_TEST_CASE(control_flow_graph_reconnect_single_jump_source)
{
    // move code that has only one unconditional jump source
    AssemblyItems input{
        u256(1),
        AssemblyItem(PushTag, 1),
        Instruction::JUMP,
        AssemblyItem(Tag, 2),
        u256(2),
        AssemblyItem(PushTag, 3),
        Instruction::JUMP,
        AssemblyItem(Tag, 1),
        u256(3),
        AssemblyItem(PushTag, 2),
        Instruction::JUMP,
        AssemblyItem(Tag, 3),
        u256(4),
    };
    checkCFG(input, {u256(1), u256(3), u256(2), u256(4)});
}

BOOST_AUTO_TEST_CASE(control_flow_graph_do_not_remove_returned_to)
{
    // do not remove parts that are "returned to"
    AssemblyItems input{
        AssemblyItem(PushTag, 1),
        AssemblyItem(PushTag, 2),
        Instruction::JUMP,
        AssemblyItem(Tag, 2),
        Instruction::JUMP,
        AssemblyItem(Tag, 1),
        u256(2)
    };
    checkCFG(input, {u256(2)});
}

BOOST_AUTO_TEST_SUITE_END()

}
}
} // end namespaces