llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[AArch64] Missed folding opportunity for (sub b, (sra (shl a, C1), C2))

Open pzhengqc opened this issue 1 month ago • 1 comments

For the following function,

int f(int a, int b) {
  b -= (a << 26) >> 29;
  return b;
}

LLVM generates:

f:
    lsl w8, w0, #26
    sub w0, w1, w8, asr #29
    ret

GCC generates the following instead:

foo:
    sbfx    w0, w0, 3, 3
    sub     w0, w1, w0
    ret

The shifted-register sub ( sub w0, w1, w8, asr #29) is more expensive for some cores and can be avoided in this case. This pattern shows up in benchmarks such as DCPerf's VideoTranscodeBench.

pzhengqc avatar Oct 22 '26 19:10 pzhengqc

@llvm/issue-subscribers-backend-aarch64

Author: pzhengqc

For the following function, ``` int f(int a, int b) { b -= (a << 26) >> 29; return b; } ``` LLVM generates: ``` f: lsl w8, w0, #26 sub w0, w1, w8, asr #29 ret ``` GCC generates the following instead: ``` foo: sbfx w0, w0, 3, 3 sub w0, w1, w0 ret ``` The shifted-register `sub` (` sub w0, w1, w8, asr #29`) is more expensive for some cores and can be avoided in this case. This pattern shows up in benchmarks such as DCPerf's VideoTranscodeBench.