[all-commits] [llvm/llvm-project] 75cf67: [SDAG] Simplify is-power-of-2 codegen (#72275)

Tavian Barnes via All-commits all-commits at lists.llvm.org
Wed Nov 15 05:26:47 PST 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 75cf672b12d9dccf7b5f8730493e04d22f4ddbb7
      https://github.com/llvm/llvm-project/commit/75cf672b12d9dccf7b5f8730493e04d22f4ddbb7
  Author: Tavian Barnes <tavianator at tavianator.com>
  Date:   2023-11-15 (Wed, 15 Nov 2023)

  Changed paths:
    M llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
    M llvm/test/CodeGen/AArch64/arm64-popcnt.ll
    M llvm/test/CodeGen/ARM/popcnt.ll
    M llvm/test/CodeGen/RISCV/rv32zbb.ll
    M llvm/test/CodeGen/RISCV/rv64zbb.ll
    M llvm/test/CodeGen/RISCV/rvv/ctpop-sdnode.ll
    M llvm/test/CodeGen/RISCV/rvv/fixed-vectors-ctpop.ll
    M llvm/test/CodeGen/X86/ctpop-combine.ll
    M llvm/test/CodeGen/X86/ispow2.ll
    M llvm/test/CodeGen/X86/vector-popcnt-128.ll
    M llvm/test/CodeGen/X86/vector-popcnt-256.ll
    M llvm/test/CodeGen/X86/vector-popcnt-512.ll

  Log Message:
  -----------
  [SDAG] Simplify is-power-of-2 codegen (#72275)

When x is not known to be nonzero, ctpop(x) == 1 is expanded to

    x != 0 && (x & (x - 1)) == 0

resulting in codegen like

    leal    -1(%rdi), %eax
    testl   %eax, %edi
    sete    %cl
    testl   %edi, %edi
    setne   %al
    andb    %cl, %al

But another expression that works is

    (x ^ (x - 1)) > x - 1

which has nicer codegen:

    leal    -1(%rdi), %eax
    xorl    %eax, %edi
    cmpl    %eax, %edi
    seta    %al




More information about the All-commits mailing list