Scribe icon indicating copy to clipboard operation
Scribe copied to clipboard

Common subexpression elimination

Open ControlCplusControlV opened this issue 3 years ago • 1 comments

ControlCplusControlV avatar Dec 17 '22 22:12 ControlCplusControlV

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
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

ControlCplusControlV avatar Dec 17 '22 22:12 ControlCplusControlV