Closed
Description
This function foo:
type T = i16;
pub fn foo(start: T) -> T {
if start <= 0 { return 0; }
let mut count = 0;
for i in start .. 10_000 {
if 752 % i != 0 {
count += 1;
}
}
count
}
fn main() {
println!("{}", (T::MIN ..= T::MAX)
.map(foo)
.sum::<T>());
}
Compiled with optimizations by rustc (1.54.0-nightly 4de7572 2021-05-01) gives:
foo:
lea eax, [rdi - 1]
movzx eax, ax
cmp eax, 9998
ja .LBB0_1
mov r8d, 9999
sub r8d, edi
mov esi, edi
neg esi
xor ecx, ecx
.LBB0_4:
cmp r8w, si
jae .LBB0_6
mov ax, 752
xor edx, edx
idiv di
lea eax, [rdi + 1]
cmp dx, 1
sbb cx, -1
movzx edx, ax
mov edi, eax
cmp edx, 10000
jne .LBB0_4
mov eax, ecx
ret
.LBB0_1:
xor ecx, ecx
mov eax, ecx
ret
.LBB0_6:
push rax
lea rdi, [rip + str.0]
lea rdx, [rip + .L__unnamed_1]
mov esi, 57
call qword ptr [rip + core::panicking::panic@GOTPCREL]
ud2
str.0:
.ascii "attempt to calculate the remainder with a divisor of zero"
Running the code you confirm that the divisor can't be zero.
I think Rustc should be able to remove this test.
(The same code with T=u16
contains no division by zero test).
Metadata
Metadata
Assignees
Labels
Area: Code generationCategory: An issue proposing an enhancement or a PR with one.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.Issue: Problems and improvements with respect to performance of generated code.Relevant to the compiler team, which will review and decide on the PR/issue.