<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/104827>104827</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Subtracting from an unsigned number and then rounding down should know the result is less than the original
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
shepmaster
</td>
</tr>
</table>
<pre>
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
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vl2P47rN_jXKDeHAlh3HucjFfLzzdoA9OMXuaYteDWSbttWRJUMfyWR-fUHZ-drtnu5NBwNHth5SJB9SpHBO9hpxzzaPbPO8EsEPxu7dgNMonEe7qk172r_CIA4IAr4G56E-eQSnZINwHNAivIIYwduT1D14A608yBZBepDaGxAa8IAaxqC8nBSC6aAskqilGYJ-d2v4Y0DAD28FHK3U7wpBOvCD8PAKR6E9qZ1N8KBQOA9GI3RBqVkFKOy8OaBl_AmcgVfGtyO4UHsrGk92lUW024GV_eBJjx8Qauyl1lL3LH9g6TNLz88ynf9tcH7-NIUaOg0fjFeC5Q_AeMk2j6Fim2fGd8C2jzMOAEB2oNDDNzMi45VCHRH5M4h1fKkY362bAZt3bN9cqBmvyuIHLfRHahTqKBx_-Qu5wvgDlAXLz2ixdpOS_k34y3nXTQBg2-fFscvi7OGt20SDsH0YcQ45K9OL3jIFN5igWhDqKE4OagSFLtKkYzClnoKfE4PxrSN7ez8sjPhBOmiEBo0HtDAJLRvaqsPMhEVH-aF7MJOXo_zEFhrTIjgviWWjvZDagYBGKEXGRRWUOqSYkKS9RnBynJTsJLbQmqOOUCWkBh3GGq37ZapHqeUcTnIjf4BAvEHC8v-D2hj1p5yfGZ-lf53sJvgoRzoWtiP2O8KXP8ZfCPO7neMrlEerhUfojB2DEl4a_Z8kbk-iY0pgPIuGQQLZbfbcS1v0wepF9Onq30-S7fLJ24C_koD_GGQzwGRNGxp0s1Nfvvz9N3j9-jPalDqMy6f8EX4zbVD4-rzEcIsfYpwUrnPeYFFseSOydleWmDR9WKeMb2dRZ4Jt8K2TCrUYcZHm_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