[X86] The TEST instruction is eliminated when BSF/TZCNT is used
authorCraig Topper <craig.topper@intel.com>
Wed, 11 Jul 2018 06:57:42 +0000 (06:57 +0000)
committerCraig Topper <craig.topper@intel.com>
Wed, 11 Jul 2018 06:57:42 +0000 (06:57 +0000)
commit02867f0fa3e6bb1586a313a3cf4540f80538b02a
tree9c33be1549286625bf7a64ac29b52988d1b8e83c
parent709f773a8884bb74239aac16d91deea8591a5ede
[X86] The TEST instruction is eliminated when BSF/TZCNT is used

Summary:
These changes cover the PR#31399.
Now the ffs(x) function is lowered to (x != 0) ? llvm.cttz(x) + 1 : 0
and it corresponds to the following llvm code:
  %cnt = tail call i32 @llvm.cttz.i32(i32 %v, i1 true)
  %tobool = icmp eq i32 %v, 0
  %.op = add nuw nsw i32 %cnt, 1
  %add = select i1 %tobool, i32 0, i32 %.op
and x86 asm code:
  bsfl     %edi, %ecx
  addl     $1, %ecx
  testl    %edi, %edi
  movl     $0, %eax
  cmovnel  %ecx, %eax
In this case the 'test' instruction can't be eliminated because
the 'add' instruction modifies the EFLAGS, namely, ZF flag
that is set by the 'bsf' instruction when 'x' is zero.

We now produce the following code:
  bsfl     %edi, %ecx
  movl     $-1, %eax
  cmovnel  %ecx, %eax
  addl     $1, %eax

Patch by Ivan Kulagin

Reviewers: davide, craig.topper, spatel, RKSimon

Reviewed By: craig.topper

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D48765

llvm-svn: 336768
llvm/lib/Target/X86/X86ISelLowering.cpp
llvm/lib/Target/X86/X86InstrInfo.cpp
llvm/test/CodeGen/X86/dagcombine-select.ll