Issue 104827
Summary Subtracting from an unsigned number and then rounding down should know the result is less than the original
Labels new issue
Assignees
Reporter shepmaster
    I have a Rust byte slice where I am trying to divide it into an even multiple of 64-byte chunks. The extra wrinkle is that I want to have at least one full chunk leftover, so I'm subtracting 64 bytes right at the beginning:

```rust
pub fn x(a: &[u8]) {
    if let Some(len) = a.len().checked_sub(64) {
        let len = len / 64 * 64;
 a.split_at(len);
    }
}
```

The argument to `split_at` should always be less than the input slice's length, so this can never panic, but the resulting optimized code still contains a call to panic. This code can be simplified down to plain numbers:

```rust
pub fn mini(length: u8) -> bool {
    if let Some(l) = length.checked_sub(64) {
        let cut = (l / 64) * 64;
        // Or this alternate formulation
        //let cut = l & !(64 - 1);

        return cut <= length;
    }

    true
}
```

Which produces this LLVM IR:

```llvm
; ModuleID = 'example.32ce4472ca1d966e-cgu.0'
source_filename = "example.32ce4472ca1d966e-cgu.0"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: mustprogress nofree norecurse nosync nounwind nonlazybind willreturn memory(none) uwtable
define noundef zeroext i1 @mini(i8 noundef %length) unnamed_addr #0 {
start:
  %_6 = icmp ult i8 %length, 64
  %0 = and i8 %length, -64
  %_51 = add i8 %0, -64
  %1 = icmp ule i8 %_51, %length
 %_0.sroa.0.0 = or i1 %_6, %1
  ret i1 %_0.sroa.0.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind nonlazybind willreturn memory(none) uwtable "probe-stack"="inline-asm" "target-cpu"="x86-64" }

!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}

!0 = !{i32 8, !"PIC Level", i32 2}
!1 = !{i32 2, !"RtLibUseGOT", i32 1}
!2 = !{!"rustc version 1.82.0-nightly (6de928dce 2024-08-18)"}
```

However, this code should be able to be simplified down to `return true`, and [alive2 agrees](https://alive2.llvm.org/ce/z/yzuYvR).

Similar in spirit to #91527 
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to