-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Open
Labels
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
This generates the following ASM. Godbolt
fn foo(i: i32) -> i64 {
let i = i.abs() as i64;
i + i
}foo:
mov eax, edi
neg eax
cmovs eax, edi
cdqe
add rax, rax
retBut, this generates the following ASM. Godbolt
fn foo(i: i32) -> i64 {
i.abs() as i64 + i.abs() as i64
}foo:
mov eax, edi
neg eax
mov ecx, edi
cmovns ecx, eax
test edi, edi
cdqe
mov edx, edi
cmovs rdx, rax
movsxd rax, ecx
add rax, rdx
retDoing a small reordering makes sure they compile down to same ASM. Godbolt
fn foo(i: i32) -> i64 {
let i = i.abs() as i64;
i + i
}
fn foo_other(i: i32) -> i64 {
let i = i.abs();
i as i64 + i as i64
}foo:
mov eax, edi
neg eax
cmovs eax, edi
cdqe
add rax, rax
ret
foo_other = fooMeta
rustc --version --verbose:
1.92.0
Metadata
Metadata
Assignees
Labels
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.