Skip to content

Inefficient bit mask codegen in combination with comparison operators. #78745

Closed

Description

@shampoofactory

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.

https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=5bc8761dc8bebe2fbf7797f47c4a2a26

// 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

Activity

  1. added
    A-LLVMArea: 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.
    I-slowIssue: 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.
    on Nov 4, 2020
  2. shampoofactory commented on Nov 4, 2020

    @shampoofactory
    ContributorAuthor

    Additional example, case_1 should compile as does case_2.

    https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=0c412cd50128a2cca1539dd299192245

    // Inefficiently compiled.
    pub fn case_1(mut u: u32) -> u32 {
        if u > 0x7F {
            u = 0x7F;
        }
        // At this point u is 0..=0x7F and the mask operation does nothing.
        u &= 0x7F;
        u
    }
    
    pub fn case_2(mut u: u32) -> u32 {
        if u > 0x7F {
            u = 0x7F;
        }
        u
    }
    playground::case_1:
    	cmpl	$127, %edi
    	movl	$127, %eax
    	cmovbl	%edi, %eax
    	andl	$127, %eax
    	retq
    
    playground::case_2:
    	cmpl	$127, %edi
    	movl	$127, %eax
    	cmovbl	%edi, %eax
    	retq
  3. AngelicosPhosphoros commented on Apr 4, 2021

    @AngelicosPhosphoros
    Contributor

    Clang fails that too but GCC doesn't. Likely to be LLVM missed optimization.
    godbolt

  4. nikic commented on Apr 4, 2021

    @nikic
    Contributor

    Upstream fix for the additional case mentioned in #78745 (comment): llvm/llvm-project@72e0846

  5. workingjubilee commented on Oct 5, 2021

    @workingjubilee
    Member

    Current nightly for Playground A:

    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

    Current nightly for Playground B:

    playground::case_1: # @playground::case_1
    # %bb.0:
    	cmpl	$127, %edi
    	movl	$127, %eax
    	cmovbl	%edi, %eax
    	retq
                                            # -- End function
    .set playground::case_2, playground::case_1

    So the latter is definitely resolved.

  6. added
    C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing such
    on Oct 8, 2023
  7. klensy commented on Jan 28, 2024

    @klensy
    Contributor

    Starting from 1.70, all functions compiled into

            mov     eax, edi
            ret

    https://rust.godbolt.org/z/zf8aTxd3v

  8. jakubadamw commented on Aug 19, 2026

    @jakubadamw
    Contributor

    Same as above, in the current nightly, marking as needing a test.
    @rustbot label +E-needs-test

  9. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Aug 19, 2026
  10. zakrad commented on Aug 23, 2026

    @zakrad
    Contributor

    @rustbot claim

  11. added 2 commits that reference this issue on Aug 24, 2026
    cb61a7d
    9a34abc
  12. added a commit that references this issue on Aug 24, 2026
    5aca658
  13. added a commit that references this issue on Aug 24, 2026
    5798f1b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-LLVMArea: 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.C-optimizationCategory: 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.I-slowIssue: 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.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions