http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55912
Bug #: 55912 Summary: missing optimization of x/x and x/std::abs(x) Classification: Unclassified Product: gcc Version: 4.8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization AssignedTo: unassig...@gcc.gnu.org ReportedBy: vincenzo.innoce...@cern.ch in the following examples I'm quite surprised that only (x/x)is optimized to "!". With Ofast (and even more with -mrecip) I would have expected that commutativity of multiplication to be used at least to optimized x*a/x Optimizing x/x and x/std::abs(x) would be beneficial with -mrecip to amortize a trivial loss of precision. cat one.cc;c++ -Ofast -S one.cc -msse4.2; cat one.s #include<cmath> int one1(float a, float x) { return x*a/x; } #include<cmath> int one2(float a, float x) { return a*(x/x); } int sign1(float a, float x) { return x*a/std::abs(x); } int sign2(float a, float x) { return a*(x/std::abs(x)); } .text .align 4,0x90 .globl __Z4one1ff __Z4one1ff: LFB86: mulss %xmm1, %xmm0 divss %xmm1, %xmm0 cvttss2si %xmm0, %eax ret LFE86: .align 4,0x90 .globl __Z4one2ff __Z4one2ff: LFB87: cvttss2si %xmm0, %eax ret LFE87: .align 4,0x90 .globl __Z5sign1ff __Z5sign1ff: LFB88: mulss %xmm1, %xmm0 movss LC0(%rip), %xmm2 andps %xmm2, %xmm1 divss %xmm1, %xmm0 cvttss2si %xmm0, %eax ret LFE88: .align 4,0x90 .globl __Z5sign2ff __Z5sign2ff: LFB89: movss LC0(%rip), %xmm2 andps %xmm1, %xmm2 divss %xmm2, %xmm1 mulss %xmm0, %xmm1 cvttss2si %xmm1, %eax ret