This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug optimization/14617] New: [tree-ssa] suboptimal code ('0' <= c && c <= '9') ? c - '0' : 0
- From: "kazu at cs dot umass dot edu" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 17 Mar 2004 09:03:22 -0000
- Subject: [Bug optimization/14617] New: [tree-ssa] suboptimal code ('0' <= c && c <= '9') ? c - '0' : 0
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Here is what I get with the current tree-ssa:
int
foo (char c)
{
return ('0' <= c && c <= '9') ? c - '0' : 0;
}
/*
movzbl 4(%esp), %ecx
xorl %eax, %eax
movb %cl, %dl
subb $48, %dl
cmpb $9, %dl
ja .L4
movsbl %cl,%eax ; extending again (but this time sign-ext)?
subl $48, %eax ; again subtracting 48 (but in different mode)?
.L4:
ret
*/
int
bar (char c)
{
c -= '0';
return ((unsigned char) c <= 9) ? c : 0;
}
/*
movzbl 4(%esp), %ecx
xorl %eax, %eax
subb $48, %cl
movsbl %cl,%edx ; extending again (but this time sign-ext)?
cmpb $10, %cl
cmovb %edx, %eax
*/
int
baz (char c)
{
int tem = c;
tem -= '0';
return ((unsigned char) tem <= 9) ? tem : 0;
}
/*
movsbl 4(%esp), %edx
xorl %eax, %eax
subl $48, %edx
cmpb $10, %dl
cmovb %edx, %eax
*/
For "foo", here is what I get right before TER:
foo (c)
{
unsigned int tem;
unsigned char T.6;
int iftmp.5;
unsigned char c.4;
int T.3;
unsigned char T.2;
char T.1;
int iftmp.0;
<bb 0>:
T.1_3 = c_2 - 48;
T.2_4 = (unsigned char)T.1_3;
if (T.2_4 <= 9) goto <L0>; else goto <L2>;
<L0>:;
T.3_7 = (int)c_2;
iftmp.0_8 = T.3_7 - 48;
# iftmp.0_1 = PHI <0(0), iftmp.0_8(1)>;
<L2>:;
return iftmp.0_1;
}
Note that if we extended c_2 earlier and did all the computation in int
wherever possible, it would be pretty easy to avoid subtracting of 48 twice.
The idea of extending everything first comes from a paper
Effective Sign Extension Elimination
Kawahito, Komatsu, and Nakatani
http://www.trl.ibm.com/projects/jit/paper/sxt.pdf
--
Summary: [tree-ssa] suboptimal code ('0' <= c && c <= '9') ? c -
'0' : 0
Product: gcc
Version: tree-ssa
Status: UNCONFIRMED
Keywords: pessimizes-code
Severity: enhancement
Priority: P2
Component: optimization
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: kazu at cs dot umass dot edu
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14617