Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Unnecessary add emitted for repeated multiplication #75413

Open
SkiFoD opened this issue Sep 11, 2022 · 4 comments
Open

Unnecessary add emitted for repeated multiplication #75413

SkiFoD opened this issue Sep 11, 2022 · 4 comments
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone

Comments

@SkiFoD
Copy link
Contributor

SkiFoD commented Sep 11, 2022

Description

Noted during work with #74020 , that a series of multiplications generates unnecessary operations. Here is the example
I played with the code for a little bit and figured out that GT_MUL nodes were being optimized, but if GT_MUL op had the right child as a value of power of 2, then the operation converted into GT_LSH (here is the code).
So if we have a series of multiplications by 2 (for simplicity) as an example above, then we end up with a series of GT_LSH operations.
I asume that we can fold the series into one GT_LSH operation with a proper right hand value.

Reproduction Steps

Use snipets like this:

public int T(int u1) {
        return u1*2*2*2*2;
    }

Expected behavior

public int T(int u1) {
        return u1*2*2*2*2;
    }

Generates:

G_M63441_IG02:
       mov      eax, ecx
       shl      eax, 4

Actual behavior

public int T(int u1) {
        return u1*2*2*2*2;
    }

Generates:

C.T(Int32)
    L0000: add edx, edx
    L0002: add edx, edx
    L0004: lea eax, [rdx+rdx]
    L0007: add eax, eax
    L0009: ret

category:cq
theme:basic-cq
skill-level:beginner
cost:small
impact:small

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Sep 11, 2022
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Sep 11, 2022
@ghost
Copy link

ghost commented Sep 11, 2022

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Noted during work with #74020 , that a series of multiplications generates unnecessary operations. Here is the example
I played with the code for a little bit and figured out that GT_MUL nodes were being optimized, but if GT_MUL op had the right child as a value of power of 2, then the operation converted into GT_LSH (here is the code).
So if we have a series of multiplications by 2 (for simplicity) as an example above, then we end up with a series of GT_LSH operations.
I asume that we can fold the series into one GT_LSH operation with a proper right hand value.

Reproduction Steps

Use snipets like this:

public int T(int u1) {
        return u1*2*2*2*2;
    }

Expected behavior

public int T(int u1) {
        return u1*2*2*2*2;
    }

Generates:

G_M63441_IG02:
       mov      eax, ecx
       shl      eax, 4

Actual behavior

public int T(int u1) {
        return u1*2*2*2*2;
    }

Generates:

C.T(Int32)
    L0000: add edx, edx
    L0002: add edx, edx
    L0004: lea eax, [rdx+rdx]
    L0007: add eax, eax
    L0009: ret

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: SkiFoD
Assignees: -
Labels:

area-CodeGen-coreclr, untriaged

Milestone: -

SkiFoD added a commit to SkiFoD/runtime that referenced this issue Sep 11, 2022
@JulieLeeMSFT JulieLeeMSFT added this to the 8.0.0 milestone Sep 12, 2022
@JulieLeeMSFT JulieLeeMSFT removed the untriaged New issue has not been triaged by the area owner label Sep 12, 2022
@GSPP
Copy link

GSPP commented Oct 4, 2022

Hm, I might have posted my annotation to the wrong place (I'm not too familiar with the GitHub code review system). Repeating it here:

SkiFoD@e248d75#diff-5b83397bbbdd17bb9457998b520fdaaa474d165390985b66f32371561b6d0bacR12766

mul->gtOp2->AsIntConCommon()->SetIntegralValue(root_val + child_val);

Is it possible that this computation would overflow? It could overflow like 30+30 > 32. But could this also cause undefined behavior in the C code of the JIT when computing int.MaxValue + int.MaxValue? Is such a large shift amount possible?

@SkiFoD
Copy link
Contributor Author

SkiFoD commented Oct 5, 2022

Hm, I might have posted my annotation to the wrong place (I'm not too familiar with the GitHub code review system). Repeating it here:

SkiFoD@e248d75#diff-5b83397bbbdd17bb9457998b520fdaaa474d165390985b66f32371561b6d0bacR12766

mul->gtOp2->AsIntConCommon()->SetIntegralValue(root_val + child_val);

Is it possible that this computation would overflow? It could overflow like 30+30 > 32. But could this also cause undefined behavior in the C code of the JIT when computing int.MaxValue + int.MaxValue? Is such a large shift amount possible?

Your concerns are correct. I'm not sure if such a situation possible so the op tree would have int.max shifts. I think we need an opinion of someone from the team.

SkiFoD added a commit to SkiFoD/runtime that referenced this issue Oct 12, 2022
@danmoseley
Copy link
Member

@EgorBo I think there is a question above.

SkiFoD added a commit to SkiFoD/runtime that referenced this issue Jan 14, 2023
SkiFoD added a commit to SkiFoD/runtime that referenced this issue Jan 22, 2023
@EgorBo EgorBo removed their assignment Mar 27, 2023
@EgorBo EgorBo modified the milestones: 8.0.0, Future Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

No branches or pull requests

5 participants