<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64001>64001</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization of available expression
</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/89b8sWrGr
```C++
extern int var_26;
extern int var_29;
void test(int var_1, int var_2, int var_3) {
var_29 = (var_1 + var_2) ? var_1 : var_2;
var_26 = var_1 + (var_2 + var_3);
}
```
IR at -O3
```
%add = sub i32 0, %var_2
%tobool.not = icmp eq i32 %add, %var_1
%cond = select i1 %tobool.not, i32 %var_2, i32 %var_1
store i32 %cond, ptr @var_29, align 4
%add1 = add i32 %var_2, %var_1
%add2 = add i32 %add1, %var_3
store i32 %add2, ptr @var_26, align 4
ret void
```
It appears to miss the identification of the available expression `var_1+var_2`.
If we just change the code by removing the parentheses:
```C++
extern int var_26;
extern int var_29;
void test(int var_1, int var_2, int var_3) {
var_29 = (var_1 + var_2) ? var_1 : var_2;
var_26 = var_1 + var_2 + var_3; //removing the parentheses
}
```
It gets the optimization we expected:
```
%add = add nsw i32 %var_2, %var_1
%tobool.not = icmp eq i32 %add, 0
%cond = select i1 %tobool.not, i32 %var_2, i32 %var_1
store i32 %cond, ptr @var_29, align 4
%add2 = add nsw i32 %add, %var_3
store i32 %add2, ptr @var_26, align 4
ret void
```
Therefore, in the test code, it seems to miss the optimization of the available expression `var_1 + var_2 `.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkVUuL40YQ_jWtSzGmVS3J1kGH8QwOOYRAWAjJJbTUZbsXWa10lz27--tDtzR-rTfZSyAQEAjV46vnp9Ih2N1A1IhyLcrXTB9573zz-29lUWWtM5-bDxQYOmdIqGfYM49BqGeBG4GbnTOt63nh_E7g5ovAzapuV-FX_4MX8lXIZ1HJ6XkRuI5PktInJj-AHRhO2v-BlVDf0NRnzclZA0yBBa7e1bnAl4vt9YcSWINYzr4AMMOBUK8gcJW8QeD63TVqNjCL1fMsVvcAVQK4eM9QeIaKgc9uYvl614bp88dfQDM8_aweagEEltqYFCocW7AKQcbqBJZTXpOdwJJd61y_GBwna9sdRqA_k8cEcuWWX8F3bpjxqaeOwea3aKmXE8ilt5fvM1Rg5-ldE0Gj4cgeRCHnAeIL6N7uBihuy8tT_FjnfaAH-Wpj8N4-YlyZq4c5Rcf7nKoHOXliiCv2cCLz1Bj0OJL2AdjBwYYAvCewhga2W9tptm4At01SfdK2121PQJ9GTyFEnajkvLbrqdhKLm4ibOGN4OMx8m2vhx0lqEg9aD-Dp4M72WGXhKP2NPCeAiU2_i_Ids80tY67Ef9D32zN39OQYUc8TdGNbA_2yzTDtzQ16pjM1819QNL4HsLbPy3y9_JV_reIio9qvP21_Ovc-7AnT1vnadq7NDI-36UoYwhEh1tq3gz1e4h5vWaVXGSmUaZWtc6oyatVXSuVyzrbNytVlysslqtCd0ZVlapVpUzVdoSoi1JltkGJSi4xl3WZF_ViW3ZlW29NqTqzLEoShaSDtv2i70-HeD8zG8KRmqqQMs963VIf0klGHOgNklIgxgvtm-jz1B53QRSyt4HDBYUt99T8ZEMg81X9j2rPjr5v7q665f2xXXTuIHATkefX0-jdR-pY4CblEwRuUr5_BQAA__-VvWJb">