Open
Description
Rust unable to figure out that if
inside the nested loop is executed at most once per outer loop iteration.
https://users.rust-lang.org/t/how-to-make-compiler-automatically-elide-bound-checks/34148/6
pub fn foo() -> usize {
let mut index = 0;
for i in 0..100 {
assert!(index <= i); // This generates a panic jmp
for j in 0..100 {
if i == j {
index += 1;
break;
}
}
}
index
}
Generated asm:
foo::foo:
push rax
mov rcx, -1
mov dl, 1
xor r8d, r8d
xor esi, esi
test r8b, r8b
je .LBB7_2
jmp .LBB7_9 // here
.LBB7_7:
add rcx, -1
add dl, 1
mov rsi, rax
cmp rax, 100
je .LBB7_8
test r8b, r8b
jne .LBB7_9 // here
.LBB7_2:
lea rax, [rsi, +, 1]
mov edi, eax
and edi, 7
cmp rsi, 7
jb .LBB7_5
movzx esi, dl
and esi, 7
add rsi, rcx
.LBB7_4:
add rsi, 8
jne .LBB7_4
.LBB7_5:
test rdi, rdi
je .LBB7_7
.LBB7_6:
add rdi, -1
jne .LBB7_6
jmp .LBB7_7
.LBB7_8:
pop rcx
ret
.LBB7_9:
call std::panicking::begin_panic
ud2
Rust version: 1.38 (stable)
platform: Ubuntu 18.04