[PATCH] D151750: [DAGCombine] `select_cc seteq X, 0, 0, cttz_zero_undef(X) -> and(cttz(X), sizeof(X) - 1)`

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 31 10:31:47 PDT 2023


craig.topper added a comment.

This test

  define i32 @foo(i32 %x) {                                                        
  entry:                                                                           
    %0 = call i32 @llvm.cttz.i32(i32 %x, i1 true)                                  
    %1 = icmp eq i32 %x, 0                                                         
    %2 = select i1 %1, i32 0, i32 %0                                               
    ret i32 %2                                                                     
  }                                                                                
                                                                                   
  declare i32 @llvm.cttz.i32(i32, i1)

compiled with `llc -mtriple=riscv64  -o - -mattr=+m` produces

  foo:                                    # @foo
          .cfi_startproc
  # %bb.0:                                # %entry
          sext.w  a1, a0
          bnez    a1, .LBB0_2
  # %bb.1:                                # %entry
          li      a0, 32
          andi    a0, a0, 31
          ret
  .LBB0_2:
          negw    a1, a0
          and     a0, a0, a1
          lui     a1, 30667
          addiw   a1, a1, 1329
          mul     a0, a0, a1
          srliw   a0, a0, 27
          lui     a1, %hi(.LCPI0_0)
          addi    a1, a1, %lo(.LCPI0_0)
          add     a0, a1, a0
          lbu     a0, 0(a0)
          andi    a0, a0, 31
          ret
  .Lfunc_end0:
          .size   foo, .Lfunc_end0-foo
          .cfi_endproc
                                          # -- End function
          .section        ".note.GNU-stack","", at progbits

That looks to be the table based expansion of CTTZ_ZERO_UNDEF, followed by a select to pick 32 for the zero case, followed by an AND with 31. RISC-V doesn't have cmov so the select became control flow.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151750/new/

https://reviews.llvm.org/D151750



More information about the llvm-commits mailing list