Repository navigation
Inefficient bit mask codegen in combination with comparison operators. #78745
Copy link
Copy link
Closed
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-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.I-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
Hi. All four test cases should compile to what is essentially a no operation. However the compiler does not omit the unnecessary case_1 and case_2 bit mask operations as it does with case_3 and case_4.
// Inefficiently compiled.
pub fn case_1(mut u: u32) -> u32 {
if u <= 0x3F {
// At this point u is 0..=0x3F and the mask operation does nothing
u &= 0x7F; // also 0xFF, 0x01FF, ..., 0x7FFF_FFFF.
}
u
}
// Inefficiently compiled.
pub fn case_2(mut u: u32) -> u32 {
if u <= 0x3E {
// At this point u is 0..=0x3E and the mask operation does nothing
u &= 0x3F; // also 0x7F, 0xFF, ..., 0x7FFF_FFFF.
}
u
}
// Ok
pub fn case_3(mut u: u32) -> u32 {
if u <= 0x3F {
// At this point u is 0..=0x3F and the mask operation does nothing
u &= 0x3F;
}
u
}
// Ok
pub fn case_4(mut u: u32) -> u32 {
if u <= 0x3F {
// At this point u is 0..=0x3F and the mask operation does nothing
u &= 0xFFFF_FFFF;
}
u
}Generated assembly:
playground::case_1:
movl %edi, %eax
andl $127, %eax
cmpl $64, %edi
cmovael %edi, %eax
retq
playground::case_2:
movl %edi, %eax
andl $63, %eax
cmpl $63, %edi
cmovael %edi, %eax
retq
playground::case_3:
movl %edi, %eax
retq
playground::case_4:
movl %edi, %eax
retq
Reactions are currently unavailable
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-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.I-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.
Activity
shampoofactory commentedon Nov 4, 2020
Additional example, case_1 should compile as does case_2.
https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=0c412cd50128a2cca1539dd299192245
AngelicosPhosphoros commentedon Apr 4, 2021
Clang fails that too but GCC doesn't. Likely to be LLVM missed optimization.
godbolt
nikic commentedon Apr 4, 2021
Upstream fix for the additional case mentioned in #78745 (comment): llvm/llvm-project@72e0846
workingjubilee commentedon Oct 5, 2021
Current nightly for Playground A:
Current nightly for Playground B:
So the latter is definitely resolved.
klensy commentedon Jan 28, 2024
Starting from 1.70, all functions compiled into
https://rust.godbolt.org/z/zf8aTxd3v
jakubadamw commentedon Aug 19, 2026
Same as above, in the current nightly, marking as needing a test.
@rustbot label +E-needs-test
zakrad commentedon Aug 23, 2026
@rustbot claim
Rollup merge of rust-lang#161580 - zakrad:regr-test-78745, r=mu001999
Rollup merge of rust-lang#161580 - zakrad:regr-test-78745, r=mu001999
Unrolled build for #161580 in rollup 161638
Rollup merge of #161580 - zakrad:regr-test-78745, r=mu001999