<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/57053>57053</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
-fassociative-math optimization issue with clang 13+
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
bkaradzic
</td>
</tr>
</table>
<pre>
I ran into optimizer issue (clang 13 and above) where `else` branch partially gets optimized out when compiling with `-fassociative-math -O2` compiler options.
This code
```cpp
if (bits == 0)
{
c0 = 1.0f;
c6 = 2.0f;
c8 = 3.0f;
c10 = 4.0f;
}
else
{
c0 = _a;
c6 = 5.0f;
c8 = 6.0f;
c10 = 7.0f;
}
```
Optimizer turns into:
```cpp
c6 = 2.0f;
c8 = 3.0f;
c10 = 4.0f;
if (bits == 0)
{
c0 = 1.0f;
}
else
{
c0 = _a;
}
```
The issue occurs with clang 13 and above (tested with 13, 14, 15, 16), but not in clang 12.
Compiler options: `-fassociative-math -O2`
Compiler Explorer: https://godbolt.org/z/9zvfYo3xa
```cpp
float test(float _a)
{
int bits = int(_a);
float c0, c6, c8, c10;
if (bits == 0)
{
c0 = 1.0f;
c6 = 2.0f;
c8 = 3.0f;
c10 = 4.0f;
}
else
{
c0 = _a;
c6 = 5.0f;
c8 = 6.0f;
c10 = 7.0f;
}
float tmp0 = c10 * _a + c8;
float tmp1 = tmp0 * _a + c6;
float result = tmp1 * c0;
return result;
}
```
Output for clang 12.0.1:
```asm
.LCPI0_0:
.long 0x3f800000 # float 1
.LCPI0_1:
.long 0x40000000 # float 2
.LCPI0_2:
.long 0x40400000 # float 3
.LCPI0_3:
.long 0x40800000 # float 4
.LCPI0_4:
.long 0x40a00000 # float 5
.LCPI0_5:
.long 0x40c00000 # float 6
.LCPI0_6:
.long 0x40e00000 # float 7
test(float): # @test(float)
cvttss2si eax, xmm0
test eax, eax
je .LBB0_1
movss xmm2, dword ptr [rip + .LCPI0_4] # xmm2 = mem[0],zero,zero,zero
movss xmm4, dword ptr [rip + .LCPI0_5] # xmm4 = mem[0],zero,zero,zero
movss xmm1, dword ptr [rip + .LCPI0_6] # xmm1 = mem[0],zero,zero,zero
movaps xmm3, xmm0
jmp .LBB0_3
.LBB0_1:
movss xmm3, dword ptr [rip + .LCPI0_0] # xmm3 = mem[0],zero,zero,zero
movss xmm2, dword ptr [rip + .LCPI0_1] # xmm2 = mem[0],zero,zero,zero
movss xmm4, dword ptr [rip + .LCPI0_2] # xmm4 = mem[0],zero,zero,zero
movss xmm1, dword ptr [rip + .LCPI0_3] # xmm1 = mem[0],zero,zero,zero
.LBB0_3:
mulss xmm1, xmm0
addss xmm1, xmm4
mulss xmm1, xmm0
addss xmm1, xmm2
mulss xmm1, xmm3
movaps xmm0, xmm1
ret
```
Output for clang 14.0.0
```asm
.LCPI0_0:
.long 0x3f800000 # float 1
.LCPI0_1:
.long 0x40e00000 # float 7
.long 0x40800000 # float 4
.LCPI0_2:
.long 0x40c00000 # float 6
.long 0x40400000 # float 3
.LCPI0_3:
.long 0x40a00000 # float 5
.long 0x40000000 # float 2
test(float): # @test(float)
cvttss2si ecx, xmm0
xor eax, eax
test ecx, ecx
sete cl
je .LBB0_1
movaps xmm2, xmm0
jmp .LBB0_3
.LBB0_1:
movss xmm2, dword ptr [rip + .LCPI0_0] # xmm2 = mem[0],zero,zero,zero
.LBB0_3:
mov al, cl
movss xmm1, dword ptr [4*rax + .LCPI0_1] # xmm1 = mem[0],zero,zero,zero
mulss xmm1, xmm0
addss xmm1, dword ptr [4*rax + .LCPI0_2]
mulss xmm1, xmm0
addss xmm1, dword ptr [4*rax + .LCPI0_3]
mulss xmm1, xmm2
movaps xmm0, xmm1
ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWE-PmzoQ_zTkYm1kMLDJgUOy-ypVqtR36OWdKgMm8T4TI9tk0_30b2ySFBIgf7pd9fAQi2Nm5jfjGc8M61T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