ControlCplusControlV / Scribe

Minimal Yul Transpilation to the Miden VM
GNU General Public License v3.0
51 stars 4 forks source link

Common subexpression elimination #46

Open ControlCplusControlV opened 1 year ago

ControlCplusControlV commented 1 year ago

Common subexpression elimination

In the expression (a + b) - (a + b)/4, "common subexpression" refers to the duplicated (a + b). Compilers implementing this technique realize that (a + b) will not change, and so only calculate its value once.

    let c := add(100, 15)
    let a := add(45, 50)
    let b := add(a, c)
    let d := add(a, c)

Test Cases

  1. Input Yul
    
    {
    let c := add(100, 15)
    let a := add(45, 50)
    let b := add(a, c)
    let d := add(a, c)
    }

1. Assembly Output
```clike=
use.std::math::u256

begin
    # Assigning to c #
        # add() #
        # u32 literal 100 #
        push.100

        # u32 literal 15 #
        push.15

        add
    # Assigning to a #
        # add() #
        # u32 literal 45 #
        push.45

        # u32 literal 50 #
        push.50

        add
    # Assigning to b #
        # add() #
        # pushing a to the top #
            dup.0
        # pushing c to the top #
            dup.2
        add
    # Assigning to d #
        dup
end

Should be compiled down to only evaluate a + c once, then Copy the value rather than re-computing it