<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64010>64010</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Missed optimization: expressions in if-else statement
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ZY546
      </td>
    </tr>
</table>

<pre>
    Test code: https://godbolt.org/z/96Y9zqfxx
```C++
int var_19,var_30;
void test(int var_0, int var_1, int var_2, int var_3) {
    int t=200;
 if(var_0){
        var_30 = var_1 + var_2;
    }
    else{
 if(var_3){
            t=100;
        }
 var_19=t/(var_1+var_2);
    }
}
```

In the test code, the calculation of the expression `var_1 + var_2` in the if-else statement can be extracted out of the branch and thus optimized. 
But looking at the intermediate code and final assembler code, we see that LLVM does not do this at O3:

IR:
```
if.then: ; preds = %entry
  %add = add nsw i32 %var_1, %var_2
  store i32 %add, ptr @var_30, align 4
  br label %if.end4

if.else: ; preds = %entry
  %tobool1.not = icmp eq i32 %var_3, 0
  %spec.select = select i1 %tobool1.not, i32 200, i32 100
 %add3 = add nsw i32 %var_1, %var_2
  %div = sdiv i32 %spec.select, %add3
  store i32 %div, ptr @var_19, align 4
  br label %if.end4
```

 Assembler code:
```asm
test(int, int, int, int):                            # @test(int, int, int, int)
        mov r8d, edx
        test    edi, edi
        je      .LBB0_2
 add     r8d, esi
        mov     dword ptr [rip + var_30], r8d
 ret
.LBB0_2:                                # %if.else
        test ecx, ecx
        mov     ecx, 200
        mov     eax, 100
 cmove   eax, ecx
        add     r8d, esi
        xor     edx, edx
 idiv    r8d
        mov     dword ptr [rip + var_19], eax
 ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVlGP4jYQ_jXmZbTIsRMgD3lYFiFV2lOlqqp0famceABfk5izDcvtr6_GMQRYdN2zEHE8nm8-f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