Repository: rust
Issue: Return branches not correctly merged in size-optimized x86-64 assembly
Labels: A-LLVM,A-codegen,T-compiler,I-heavy,C-optimization

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.
-->

> [Godbolt link (includes this and another buggy example)](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:rust,selection:(endColumn:1,endLineNumber:8,positionColumn:1,positionLineNumber:2,selectionStartColumn:1,selectionStartLineNumber:8,startColumn:1,startLineNumber:2),source:'%23%5Bno_mangle%5D%0Apub+fn+parse_u32_digit_1(acc:+u32,+byte:+u8)+-%3E+Option%3Cu32%3E+%7B%0A++++match+(acc.checked_mul(10),+byte.checked_sub(b!'0!'))+%7B%0A++++++++(Some(next),+Some(v+@+0..%3D9))+%3D%3E+next.checked_add(v+as+u32),%0A++++++++_+%3D%3E+None,%0A++++%7D%0A%7D%0A%0A%23%5Bno_mangle%5D%0Apub+fn+parse_u32_digit_2(acc:+u32,+byte:+u8)+-%3E+Option%3Cu32%3E+%7B%0A++++//+Not+sure+why+this+order+is+required+for+ensuring+proper+branch%0A++++let+max+%3D+if+acc+%3D%3D+429496729+%7B+5+%7D+else+%7B+9+%7D%3B%0A++++if+acc+%3C%3D+429496729+%7B%0A++++++++let+v+%3D+(byte+as+u32).wrapping_sub(b!'0!'+as+u32)%3B%0A++++++++if+v+%3C%3D+max+%7B%0A++++++++++++return+Some(acc.wrapping_mul(10).wrapping_add(v))%3B%0A++++++++%7D%0A++++%7D%0A++++None%0A%7D'),l:'5',n:'1',o:'Rust+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:r1790,filters:(b:'0',binary:'1',binaryObject:'1',commentOnly:'0',debugCalls:'1',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1',verboseDemangling:'0'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:rust,libs:!(),options:'-C+opt-level%3Ds',overrides:!(),selection:(endColumn:12,endLineNumber:20,positionColumn:1,positionLineNumber:1,selectionStartColumn:12,selectionStartLineNumber:20,startColumn:1,startLineNumber:1),source:1),l:'5',n:'0',o:'+rustc+1.79.0+(Editor+%231)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)

I tried this code:

```rust
pub fn parse_u32_digit(acc: u32, byte: u8) -> Option<u32> {
    let max = if acc == 429496729 { 5 } else { 9 };
    if acc <= 429496729 {
        let v = (byte as u32).wrapping_sub(b'0' as u32);
        if v <= max {
            return Some(acc.wrapping_mul(10).wrapping_add(v));
        }
    }
    None
}
```

I expected to see this happen: assembly that looks something like this:

```asm
parse_u32_digit:
        xor     eax, eax
        cmp     edi, 429496729
        ja      .LBB1_1
        setne   cl
        movzx   ecx, cl
        lea     r8d, [4*rcx + 5]
        movzx   ecx, sil
        add     ecx, -48
        cmp     ecx, r8d
        ja      .LBB1_4
        lea     eax, [rdi + 4*rdi]
        lea     edx, [rcx + 2*rax]
        mov     eax, 1
.LBB1_1:
.LBB1_4:
        ret
```

Instead, this happened: the following assembly:

```asm
parse_u32_digit:
        xor     eax, eax
        cmp     edi, 429496729
        ja      .LBB1_1
        setne   cl
        movzx   ecx, cl
        lea     r8d, [4*rcx + 5]
        movzx   ecx, sil
        add     ecx, -48
        cmp     ecx, r8d
        ja      .LBB1_4
        lea     eax, [rdi + 4*rdi]
        lea     edx, [rcx + 2*rax]
        mov     eax, 1
.LBB1_4:
        ret
.LBB1_1:
        ret
```

### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7
Compiler returned: 0
```