This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug tree-optimization/34417] New: simplify '(x & A) % B' if 'B > A/2'
- From: "wouter dot vermaelen at scarlet dot be" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 10 Dec 2007 10:56:12 -0000
- Subject: [Bug tree-optimization/34417] New: simplify '(x & A) % B' if 'B > A/2'
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Hi,
In one of my application I need the expression '(x & 0xf) % 9'. Because of the
restricted range of (x & 0xf) it's possible to replace the modulo operation
with an if (see examples below). It would be nice if gcc could do this
optimization automatically.
unsigned fooA(unsigned x)
{
return (x & 0xf) % 9;
}
unsigned fooB(unsigned x)
{
x &= 0xf;
if (x >= 9) x -= 9;
return x;
}
unsigned fooC(unsigned x)
{
x &= 0xf;
return std::min(x, x - 9);
}
The generated code is pasted below (SVN revision r130738, linux_x86_64, -O3).
Version B and C are very close (maybe C is slightly better) but they are
clearly better than version A.
fooA: andl $15, %edi
movl $954437177, %edx
movl %edi, %eax
mull %edx
shrl %edx
leal 0(,%rdx,8), %eax
addl %edx, %eax
subl %eax, %edi
movl %edi, %eax
ret
fooB: andl $15, %edi
leal -9(%rdi), %eax
cmpl $9, %edi
cmovae %eax, %edi
movl %edi, %eax
ret
fooC: andl $15, %edi
leal -9(%rdi), %eax
cmpl %edi, %eax
cmova %edi, %eax
ret
--
Summary: simplify '(x & A) % B' if 'B > A/2'
Product: gcc
Version: 4.3.0
Status: UNCONFIRMED
Severity: enhancement
Priority: P3
Component: tree-optimization
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: wouter dot vermaelen at scarlet dot be
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34417