[llvm-bugs] [Bug 48123] New: Redundant branches with ctlz and cttz

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Nov 9 15:03:34 PST 2020


https://bugs.llvm.org/show_bug.cgi?id=48123

            Bug ID: 48123
           Summary: Redundant branches with ctlz and cttz
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: diggsey at googlemail.com
                CC: craig.topper at gmail.com, llvm-bugs at lists.llvm.org,
                    llvm-dev at redking.me.uk, pengfei.wang at intel.com,
                    spatel+llvm at rotateright.com

Rust code:

```rust
pub fn can_represent_as_f64(x: u64) -> bool {
    x.leading_zeros() + x.trailing_zeros() >= 11
}
```

LLVM IR:
```llvm
define zeroext i1
@_ZN10playground20can_represent_as_f6417h8c9d47bab619cb5fE(i64 %x) unnamed_addr
#0 {
start:
  %0 = tail call i64 @llvm.ctlz.i64(i64 %x, i1 false) #2, !range !2
  %1 = trunc i64 %0 to i32
  %2 = tail call i64 @llvm.cttz.i64(i64 %x, i1 false) #2, !range !2
  %3 = trunc i64 %2 to i32
  %_2 = add nuw nsw i32 %1, %3
  %4 = icmp ugt i32 %_2, 10
  ret i1 %4
}
```

Assembly:
```asm
playground::can_represent_as_f64:
        mov     eax, 64
        mov     ecx, 64
        test    rdi, rdi    ; Initial test for zero and branch
        je      .LBB0_2
        bsr     rcx, rdi
        xor     rcx, 63

.LBB0_2:
        test    rdi, rdi    ; Second test for zero and branch
        je      .LBB0_4
        bsf     rax, rdi

.LBB0_4:
        add     ecx, eax
        cmp     ecx, 10
        seta    al
        ret
```

Instead of performing the comparison twice, the code should immediately branch
to LBB0_4.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201109/5b60dd79/attachment.html>


More information about the llvm-bugs mailing list