Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Failure to use condition to simplify branch #38198

Open Quuxplusone opened 6 years ago

Quuxplusone commented 6 years ago
Bugzilla Link PR39225
Status NEW
Importance P enhancement
Reported by Simon Pilgrim (llvm-dev@redking.me.uk)
Reported on 2018-10-09 06:31:41 -0700
Last modified on 2019-09-19 10:27:13 -0700
Version unspecified
Hardware PC Windows NT
CC andrea.dibiagio@gmail.com, filcab@gmail.com, llvm-bugs@lists.llvm.org, llvm-bugzilla@jfbastien.com, spatel+llvm@rotateright.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also PR39603, PR40527
https://godbolt.org/z/I7HTBx

void test(int &v, int i) {
  if (v == i)
    v = i;
}

clang -O3 -march=btver2

_Z4testRii: # @_Z4testRii
  cmpl %esi, (%rdi)
  jne .LBB0_2
  movl %esi, (%rdi)
.LBB0_2:
  retq

How come we couldn't remove this store and just return?

More complicated could also avoid some work:

void test1(int &v, int i, bool replace) {
  if (replace || v == i)
    v = i;
}

_Z5test1Riib: # @_Z5test1Riib
  cmpl %esi, (%rdi)
  je .LBB1_2
  testb %dl, %dl
  jne .LBB1_2
  retq
.LBB1_2:
  movl %esi, (%rdi)
  retq

How come we couldn't just store based on the test?

int test3(int &v, int i, bool replace) {
  if (replace || v == i) {
    v = i;
    return 1;
  }
  return 0;
}

_Z5test3Riib: # @_Z5test3Riib
  cmpl %esi, (%rdi)
  je .LBB2_2
  xorl %eax, %eax
  testb %dl, %dl
  jne .LBB2_2
  retq
.LBB2_2:
  movl $1, %eax
  movl %esi, (%rdi)
  retq

Trickier, but we could avoid the store and just set the returncode:

_Z5test3Riib: # @_Z5test3Riib
  cmpl %esi, (%rdi)
  je .LBB2_3
  xorl %eax, %eax
  testb %dl, %dl
  jne .LBB2_2
  retq
.LBB2_2:
  movl %esi, (%rdi)
.LBB2_3:
  movl $1, %eax
  retq
Quuxplusone commented 5 years ago

The examples here may require more work, but the most basic form is shown in bug 39603. Also, see discussion in bug 40527.