[PATCH] D122968: [AArch64][SelectionDAG] Add target-specific implementation of srem

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 17 14:52:12 PDT 2022


mstorsjo added a comment.

In D122968#3455783 <https://reviews.llvm.org/D122968#3455783>, @mstorsjo wrote:

> FYI, I’ve bisected a misoptimization down to this commit. I’ll follow up later with a proper repro…

This commit causes misoptimizations of the following function:

  int func(int val) {
      val += val % 2;
      return val;
  }

Tested with a caller like this:

  #include <stdio.h>
  int func(int val);
  int main(int argc, char* argv[]) {
      for (int i = -3; i <= 3; i++)
          printf("%d -> %d\n", i, (int)func(i));
      return 0;
  }

Originally this function returns the following values:

  -3 -> -4
  -2 -> -2
  -1 -> -2 
  0 -> 0
  1 -> 2
  2 -> 2
  3 -> 4

After this commit, it prints the following:

  -3 -> -2
  -2 -> -2
  -1 -> 0 
  0 -> 0
  1 -> 0
  2 -> 2
  3 -> 2

Originally, the function was compiled into the following (with `-O2`):

  func:
          cmp     w0, #0
          cinc    w8, w0, lt
          and     w8, w8, #0xfffffffe
          sub     w8, w0, w8
          add     w0, w8, w0
          ret

After this change, it gets incorrectly compiled into this:

  func:
          and     w8, w0, #0x1
          cmp     w0, #0
          cneg    w8, w8, ge
          add     w0, w8, w0
          ret


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122968/new/

https://reviews.llvm.org/D122968



More information about the llvm-commits mailing list