http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59424
Bug ID: 59424 Summary: Optimization issue on min/max Product: gcc Version: 4.7.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: jmvalin at jmvalin dot ca gcc seems to be having problems optimizing float min/max code and behaves unpredictably. If I compile the following code on an x86-64 (SSE enabled): #include <math.h> float func1(float a, float b) { return (a < b ? a : b); } float func2(float a, float b) { return sqrtf(a < b ? a : b); } float func3(float a, float b) { return fabsf(a < b ? a : b); } I get the following disassembly: 0000000000000000 <func1>: func1(): 0: f3 0f 5d c1 minss %xmm1,%xmm0 4: c3 retq 5: 66 66 2e 0f 1f 84 00 data32 nopw %cs:0x0(%rax,%rax,1) c: 00 00 00 00 0000000000000010 <func2>: func2(): 10: f3 0f 5d c1 minss %xmm1,%xmm0 14: f3 0f 51 c0 sqrtss %xmm0,%xmm0 18: c3 retq 19: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 0000000000000020 <func3>: func3(): 20: 0f 2f c8 comiss %xmm0,%xmm1 23: 77 13 ja 38 <func3+0x18> 25: f3 0f 10 05 00 00 00 movss 0x0(%rip),%xmm0 # 2d <func3+0xd> 2c: 00 2d: 0f 54 c1 andps %xmm1,%xmm0 30: c3 retq 31: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 38: f3 0f 10 0d 00 00 00 movss 0x0(%rip),%xmm1 # 40 <func3+0x20> 3f: 00 40: 0f 54 c1 andps %xmm1,%xmm0 43: c3 retq So gcc is correctly using maxss in func1 and func2, but not in func2. In practice, I'm using MIN/MAX macros extensively in libopus and gcc is missing the optimization most of the time, slowing down the code.