Skip to content

Redundant instructions involving indexing with bools #123216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
alion02 opened this issue Mar 30, 2024 · 1 comment · May be fixed by #134626
Open

Redundant instructions involving indexing with bools #123216

alion02 opened this issue Mar 30, 2024 · 1 comment · May be fixed by #134626
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. I-heavy Issue: Problems and improvements with respect to binary size of generated code. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alion02
Copy link

alion02 commented Mar 30, 2024

I tried this code:

fn f(a: u32, b: bool, c: bool, d: &mut [u128; 2]) {
    let mut a = a & 1 != 0;
    if b {
        a ^= c;
        d[0] |= 1;
    }
    d[a as usize] |= 1;
}

It compiles to the following assembly:

f:
        test    esi, esi
        je      .LBB0_1
        xor     dil, dl
        or      byte ptr [rcx], 1
        jmp     .LBB0_3
.LBB0_1:
        and     dil, 1
.LBB0_3:
        movzx   eax, dil
        and     eax, 1
        shl     eax, 4
        or      byte ptr [rcx + rax], 1
        ret

The jmp .LBB0_3 and and dil, 1 instructions are useless, because dil is moved to eax and anded with 1 there anyway, and dil is not used anywhere.

The output improves if you avoid using bools.

godbolt

Meta

rustc --version --verbose:

rustc 1.79.0-nightly (c9f8f3438 2024-03-27)
binary: rustc
commit-hash: c9f8f3438a8134a413aa5d4903e0196e44e37bbc
commit-date: 2024-03-27
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.2
Compiler returned: 0

@rustbot modify labels: -C-bug +C-optimization +A-codegen +A-LLVM +I-slow +I-heavy

@alion02 alion02 added the C-bug Category: This is a bug. label Mar 30, 2024
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-heavy Issue: Problems and improvements with respect to binary size of generated code. I-slow Issue: Problems and improvements with respect to performance of generated code. and removed C-bug Category: This is a bug. labels Mar 30, 2024
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 30, 2024
@clubby789
Copy link
Contributor

This seems to be okay on current nightly:

f:
        test    esi, esi
        je      .LBB0_2
        xor     dil, dl
        or      byte ptr [rcx], 1
.LBB0_2:
        movzx   eax, dil
        and     eax, 1
        shl     eax, 4
        or      byte ptr [rcx + rax], 1
        ret

@clubby789 clubby789 added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Oct 13, 2024
@veera-sivarajan veera-sivarajan linked a pull request Dec 21, 2024 that will close this issue
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 27, 2024
…r=Mark-Simulacrum

Add Four Codegen Tests

Closes rust-lang#74615
Closes rust-lang#123216
Closes rust-lang#49572
Closes rust-lang#93514

This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 12, 2025
…r=Mark-Simulacrum

Add Four Codegen Tests

Closes rust-lang#74615
Closes rust-lang#123216
Closes rust-lang#49572
Closes rust-lang#93514

This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 19, 2025
…r=Mark-Simulacrum

Add Four Codegen Tests

Closes rust-lang#74615
Closes rust-lang#123216
Closes rust-lang#49572
Closes rust-lang#93514

This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Feb 10, 2025
…, r=Mark-Simulacrum

Add Four Codegen Tests

Closes rust-lang#74615
Closes rust-lang#123216
Closes rust-lang#49572
Closes rust-lang#93514

This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
Zalathar added a commit to Zalathar/rust that referenced this issue Feb 10, 2025
…, r=Mark-Simulacrum

Add Four Codegen Tests

Closes rust-lang#74615
Closes rust-lang#123216
Closes rust-lang#49572
Closes rust-lang#93514

This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Feb 10, 2025
…, r=Mark-Simulacrum

Add Four Codegen Tests

Closes rust-lang#74615
Closes rust-lang#123216
Closes rust-lang#49572
Closes rust-lang#93514

This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 11, 2025
…r=Mark-Simulacrum

Add Four Codegen Tests

Closes rust-lang#74615
Closes rust-lang#123216
Closes rust-lang#49572
Closes rust-lang#93514

This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
Noratrieb added a commit to Noratrieb/rust that referenced this issue Mar 6, 2025
…, r=Mark-Simulacrum

Add Four Codegen Tests

Closes rust-lang#74615
Closes rust-lang#123216
Closes rust-lang#49572
Closes rust-lang#93514

This PR adds four codegen tests. The FileCheck assertions were generated with the help of `update_test_checks.py` and `update_llc_test_checks.py` from the LLVM project.
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 15, 2025
…r=Mark-Simulacrum

Add Three Codegen Tests

Closes rust-lang#123216
Closes rust-lang#49572
Closes rust-lang#93514

This PR adds three codegen tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. I-heavy Issue: Problems and improvements with respect to binary size of generated code. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants