[PATCH] D14590: [SimplifyLibCalls] Constant folding for fls, flsl, flsll

Davide Italiano via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 15 13:50:00 PST 2016


davide added a comment.

With `llvm.ctlz(x, true)` we end up producing slightly smaller code (at least on x86_64 FreeBSD)

i.e.

x != 0 ? (sizeInBits(x) - llvm.ctlz(x, true)) : 0

  bsrq    %rdi, %rcx
  xorl    $-64, %ecx
  addl    $65, %ecx
  xorl    %eax, %eax
  testq   %rdi, %rdi
  cmovnel %ecx, %eax
  retq

x != 0 ? (sizeInBits(x) - llvm.ctlz(x, false)) : 0

  # BB#0:
          testq   %rdi, %rdi
          je      .LBB0_1
  # BB#2:                                 # %cond.false
          bsrq    %rdi, %rax
          xorq    $63, %rax
          jmp     .LBB0_3
  .LBB0_1:
          movl    $64, %eax
  .LBB0_3:                                # %cond.end
          movl    $64, %ecx
          subl    %eax, %ecx
          xorl    %eax, %eax
          testq   %rdi, %rdi
          cmovnel %ecx, %eax
          retq 

sizeInBits(x) - llvm.ctlz(x, false)

  # BB#0:
          testq   %rdi, %rdi
          je      .LBB0_1
  # BB#2:                                 # %cond.false
          bsrq    %rdi, %rcx
          xorq    $63, %rcx
          jmp     .LBB0_3
  .LBB0_1:
          movl    $64, %ecx
  .LBB0_3:                                # %cond.end
          movl    $64, %eax
          subl    %ecx, %eax
          retq

Which one do you prefer, 1) or 3) ?



================
Comment at: lib/Transforms/Utils/SimplifyLibCalls.cpp:1551
+  // Constant fold.
+  if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
+    if (CI->isZero()) // fls(0) -> 0.
----------------
efriedma wrote:
> There isn't much point to keeping this around; the general-case code will constant-fold anyway.
Done.


https://reviews.llvm.org/D14590





More information about the llvm-commits mailing list