Skip to content

Failed analysis of index dominance #80075

Closed

Description

@leonardo-m

rustc 1.50.0-nightly (f76ecd0 2020-12-15) on this code:

pub fn foo(data: &[u8], i: usize, j: usize) -> u8 {
    if i < data.len() && j <= i {
        data[j]
    } else {
        0
    }
}

Can't remove the array bound test (compiling even with aggressive optimizations):

foo:
    push    rax
    cmp     rdx, rsi
    jae     .LBB0_4
    cmp     rcx, rdx
    ja      .LBB0_4
    cmp     rcx, rsi
    jae     .LBB0_3
    mov     al, byte ptr [rdi + rcx]
    pop     rcx
    ret
.LBB0_4:
    xor     eax, eax
    pop     rcx
    ret
.LBB0_3:
    lea     rdx, [rip + .L__unnamed_1]
    mov     rdi, rcx
    call    qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
    ud2

This shows that value range analysis could be useful.

Godbolt: https://rust.godbolt.org/z/jPoY6Mnx6

Activity

  1. added
    A-mir-optArea: MIR optimizations
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    and removed
    C-bugCategory: This is a bug.
    on Dec 16, 2020
  2. camelid commented on Dec 16, 2020

    @camelid
    Member

    I relabeled this as an enhancement proposal since to my understanding this isn't a bug.

  3. jrmuizel commented on Dec 17, 2020

    @jrmuizel
    Contributor
  4. jrmuizel commented on Dec 17, 2020

    @jrmuizel
    Contributor

    It looks like the new ConstraintElimination llvm pass should fix this.

  5. leonardo-m commented on Dec 17, 2020

    @leonardo-m
    Author

    It looks like the new ConstraintElimination llvm pass should fix this.

    That went better than expected. LLVM devs often deliver.

  6. added
    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
    on Jan 23, 2021
  7. jrmuizel commented on Mar 1, 2021

    @jrmuizel
    Contributor

    I confirmed that: rustc --emit=asm -C passes=constraint-elimination -O test.rs with a rust built with llvm12 branch gives:

    	.section	__TEXT,__text,regular,pure_instructions
    	.macosx_version_min 10, 7
    	.globl	__ZN4test3foo17hbf89db3e2fd7b5fcE
    	.p2align	4, 0x90
    __ZN4test3foo17hbf89db3e2fd7b5fcE:
    	.cfi_startproc
    	xorl	%eax, %eax
    	cmpq	%rdx, %rsi
    	jbe	LBB0_3
    	cmpq	%rdx, %rcx
    	ja	LBB0_3
    	pushq	%rbp
    	.cfi_def_cfa_offset 16
    	.cfi_offset %rbp, -16
    	movq	%rsp, %rbp
    	.cfi_def_cfa_register %rbp
    	movb	(%rdi,%rcx), %al
    	popq	%rbp
    LBB0_3:
    	retq
    	.cfi_endproc
  8. leonardo-m commented on Mar 2, 2021

    @leonardo-m
    Author

    You could also test issue #80620 :-)

  9. leonardo-m commented on Mar 2, 2021

    @leonardo-m
    Author

    Is -C passes=constraint-elimination part of -C opt-level=3?

  10. jrmuizel commented on Mar 2, 2021

    @jrmuizel
    Contributor

    I would be surprised if it was, but it's probably worth adding.

  11. added
    I-slowIssue: Problems and improvements with respect to performance of generated code.
    on Aug 28, 2021
  12. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Apr 5, 2023
  13. veera-sivarajan commented on Mar 14, 2025

    @veera-sivarajan
    Contributor

    Fixed since rustc 1.73.0: https://rust.godbolt.org/z/r6rebEhWP

    @rustbot label +e-needs-test

  14. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Mar 14, 2025
  15. added 2 commits that reference this issue on Aug 22, 2025
  16. added a commit that references this issue on Aug 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-mir-optArea: MIR optimizationsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-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