Skip to content

Unnecessary add emitted for repeated multiplication #75413

@SkiFoD

Description

@SkiFoD

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

Metadata

Metadata

Assignees

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