Skip to content

Common subexpression elimination #46

Open

Description

@ControlCplusControlV
No description provided.

Activity

  1. ControlCplusControlV commented on Dec 17, 2022

    @ControlCplusControlV
    OwnerAuthor

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions