I have four identity functions which do essentially nothing. Only multiplication with 1
could be optimized by clang to a single ret
statement.
float id0(float x) {
return x + 1 - 1;
}
float id1(float x) {
return x + 0;
}
float id2(float x) {
return x * 2 / 2;
}
float id3(float x) {
return x * 1;
}
And the following compiler output is: (clang 10, at -O3)
.LCPI0_0:
.long 1065353216 # float 1
.LCPI0_1:
.long 3212836864 # float -1
id0(float): # @id0(float)
addss xmm0, dword ptr [rip + .LCPI0_0]
addss xmm0, dword ptr [rip + .LCPI0_1]
ret
id1(float): # @id1(float)
xorps xmm1, xmm1
addss xmm0, xmm1
ret
.LCPI2_0:
.long 1056964608 # float 0.5
id2(float): # @id2(float)
addss xmm0, xmm0
mulss xmm0, dword ptr [rip + .LCPI2_0]
ret
id3(float): # @id3(float)
ret
I can understand why id0
and id2
can't be optimized. They increase the value which could then turn into positive infinity and the second operation would not change it back.
But why can't id1
be optimized? Additon with infinity would yield infinity, addition with any regular number would yield that number and addition with NaN
would yield NaN
. So why is it not a "true" identity operation like * 1
.