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

[AArch64] Missed MADD folding opportunity (MUL + shifted-register ADD)

Open pzhengqc opened this issue 1 month ago • 3 comments

For the following function,

int f(int a, int b, int c) {
  return a*b + (c>>2);
}

LLVM currently generates:

f:
      mul     w8, w1, w0
      add     w0, w8, w2, asr #2
      ret

We could generate the following instead:

f:
      asr     w8, w2, #2
      madd    w0, w1, w0, w8
      ret

The ASR + MADD is more efficient than MUL + shifted-register ADD for some CPUs (e.g., Neoverse V2). This pattern shows up in benchmarks such as x264.

pzhengqc avatar Oct 13 '26 01:10 pzhengqc

@llvm/issue-subscribers-backend-aarch64

Author: pzhengqc

For the following function, ``` int f(int a, int b, int c) { return a*b + (c>>2); } ``` LLVM currently generates: ``` f: mul w8, w1, w0 add w0, w8, w2, asr #2 ret ``` We could generate the following instead: ``` f: asr w8, w2, #2 madd w0, w1, w0, w8 ret ``` The ASR + MADD is more efficient than MUL + shifted-register ADD for some CPUs (e.g., Neoverse V2). This pattern shows up in benchmarks such as x264.

Hi, I'm interested in picking this up if you're not planning to work on it right away.

StarOne01 avatar Oct 13 '26 01:10 StarOne01

6. Hi, I'm interested in picking this up if you're not planning to work on it right away.

Thanks for the help, @StarOne01. I have already started working on the fix.

pzhengqc avatar Oct 13 '26 01:10 pzhengqc