This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/52802] New: Equality rewrites pessimizes code
- From: "gcc at magfr dot user.lysator.liu.se" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 31 Mar 2012 00:29:50 +0000
- Subject: [Bug c/52802] New: Equality rewrites pessimizes code
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52802
Bug #: 52802
Summary: Equality rewrites pessimizes code
Classification: Unclassified
Product: gcc
Version: 4.8.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: gcc@magfr.user.lysator.liu.se
I have a function (in foo.c)
unsigned f(unsigned a, unsigned b)
{
if (a < 8)
return a;
else if (a == 8)
return b;
else
return 4711;
}
that GCC compiles to (x86_64, -Os)
cmpl $7, %edi #, a
movl %edi, %eax # a, a
jbe .L2 #,
cmpl $8, %edi #, a
movl $4711, %eax #, tmp63
cmove %esi, %eax # b,, a
.L2:
ret
Here it is confusing to see that it starts by doing a "cmp $7, %edi" and then
goes on to do a "cmp $8, %edi" since I never said anything but 8, so the big
question is why there are two compares.
The reason seems to be premature optimization. The C parser tries to minimize
the value of all comparision values so that -fdump-tree-original shows
;; Function f (null)
;; enabled by -tree-original
{
if (a <= 7)
{
return a;
}
else
{
if (a == 8)
{
return b;
}
else
{
return 4711;
}
}
}
and there the 7 have magically appeared.