Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Common subexpression elimination #46

Open
Tracked by #43
ControlCplusControlV opened this issue Dec 17, 2022 · 1 comment
Open
Tracked by #43

Common subexpression elimination #46

ControlCplusControlV opened this issue Dec 17, 2022 · 1 comment

Comments

@ControlCplusControlV
Copy link
Owner

No description provided.

@ControlCplusControlV
Copy link
Owner Author

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant