[X86] Improve (select carry C1+1 C1)
authorKazu Hirata <kazu@google.com>
Tue, 21 Feb 2023 00:38:21 +0000 (16:38 -0800)
committerKazu Hirata <kazu@google.com>
Tue, 21 Feb 2023 00:38:21 +0000 (16:38 -0800)
commita942a944245374fc62a5af8ee3abbc579f5ee7a5
treead1d38698b4a0d56ad68de13e62b3e827fa5ccc5
parentc09e224c254c06f23a460f92abfc34e10a26192d
[X86] Improve (select carry C1+1 C1)

Without this patch:

  return X < 4 ? 3 : 2;

  return X < 9 ? 7 : 6;

are compiled as:

  31 c0                   xor    %eax,%eax
  83 ff 04                cmp    $0x4,%edi
  0f 93 c0                setae  %al
  83 f0 03                xor    $0x3,%eax

  31 c0                   xor    %eax,%eax
  83 ff 09                cmp    $0x9,%edi
  0f 92 c0                setb   %al
  83 c8 06                or     $0x6,%eax

respectively.  With this patch, we generate:

  31 c0                   xor    %eax,%eax
  83 ff 04                cmp    $0x4,%edi
  83 d0 02                adc    $0x2,%eax

  31 c0                   xor    %eax,%eax
  83 ff 04                cmp    $0x4,%edi
  83 d0 02                adc    $0x2,%eax

respectively, saving 3 bytes while reducing the tree height.

This patch recognizes the equivalence of OR and ADD
(if bits do not overlap) and delegates to combineAddOrSubToADCOrSBB
for further processing.  The same applies to the equivalence of XOR
and SUB.

Differential Revision: https://reviews.llvm.org/D143838
llvm/lib/Target/X86/X86ISelLowering.cpp
llvm/test/CodeGen/X86/select_const.ll