Skip to content

JIT Optimization: Merge multiple inc into one add #65025

Closed

Description

@deeprobin

https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBLANgHwAEAmARgFgAoQgZgAIS6BhOgbyrs4fuwDsM6AQQAUfAQEMAlGw5c54gNQKA3LLmdFKtesIB2OuNWU5AX23badMXQBCo/gentj6jXQUBeOsSOvOeg18uM0oTIA

The codegen of B is better (less cpu cycles).

    public int A(int a) {
        a++; // inc edx
        a++; // inc edx
        return a;
    }
    
    public int B(int a) {
        a += 2; // add edx, 2
        return a;
    }

Expected result

- inc edx
- inc edx
+ add edx, 2
  mov eax, edx
  ret

Activity

  1. ghost added
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    on Feb 8, 2022
  2. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Feb 8, 2022
  3. danmoseley commented on Feb 9, 2022

    @danmoseley
    Contributor

    This does not seem to be what C++ compilers do: https://godbolt.org/z/r1f7Pxdfa
    Curious to know why.

  4. omariom commented on Feb 9, 2022

    @omariom
    Contributor
  5. danmoseley commented on Feb 9, 2022

    @danmoseley
    Contributor

    @omariom thanks, I thought that was default.

  6. dubiousconst282 commented on Feb 9, 2022

    @dubiousconst282
    Contributor

    IMO this should be generalized to all operators.

    Interestingly, copying to temporary variables will result in optimal code, so maybe this is related to SSA/constant propagation:

    public int M(int x) {
        x *= 7;
        x *= 7;
        return x;
    }
    public int M2(int x) {
        int t1 = x * 7;
        int t2 = t1 * 7;
        return t2;
    }
    C.M(Int32)
        L0000: imul edx, 7
        L0003: imul edx, 7
        L0006: mov eax, edx
        L0008: ret
    
    C.M2(Int32)
        L0000: imul eax, edx, 0x31
        L0003: ret

    Sharplab

  7. JulieLeeMSFT commented on Feb 9, 2022

    @JulieLeeMSFT
    Member

    cc @dotnet/jit-contrib

  8. removed
    untriagedNew issue has not been triaged by the area owner
    on Feb 9, 2022
  9. added this to the Future milestone on Feb 9, 2022
  10. AndyAyersMS commented on Feb 9, 2022

    @AndyAyersMS
    Member

    Note forward sub (#63720) does not handle cases where a local has multiple definitions and uses, even if each definition has just one use. It might not be too hard to extend it to cover these cases.

    Eg if we run it again once SSA is built and track the number of uses of each SSA def.

  11. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Dec 11, 2022
  12. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jan 11, 2023
  13. ghost locked as resolved and limited conversation to collaborators on Feb 10, 2023
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

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions