public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/31248]  New: Too much casting for char operands on tree level
@ 2007-03-17 19:09 ubizjak at gmail dot com
  2007-03-18  5:30 ` [Bug tree-optimization/31248] " pinskia at gcc dot gnu dot org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: ubizjak at gmail dot com @ 2007-03-17 19:09 UTC (permalink / raw)
  To: gcc-bugs

Following testcase produces unnecessary moves in short loop:

--cut here--
char table[256];

int test(void) {

  char val = 0;
  int i;

  for (i = 0; i < 10; i++)
    val += table[i];

  return val;
}
--cut here--

gcc -O2:

test:
.LFB2:
        movzbl  table(%rip), %ecx
        movl    $1, %edx
        .p2align 4,,7
.L2:
(*)     movl    %ecx, %eax
        addb    table(%rdx), %al
        addq    $1, %rdx
        cmpq    $10, %rdx
(*)     movl    %eax, %ecx
        jne     .L2
        movsbl  %al,%eax
        ret

Those unneccesary moves (*) are generated due to many (spurious?) casts in
optimized tree dump:

test ()
{
  long unsigned int ivtmp.31;
  char val;
  unsigned char D.1985;

<bb 2>:
  val = (char) (unsigned char) MEM[symbol: table];
  ivtmp.31 = 1;

<L0>:;
  D.1985 = (unsigned char) val + (unsigned char) MEM[symbol: table, index:
ivtmp.31];
  val = (char) D.1985;
  ivtmp.31 = ivtmp.31 + 1;
  if (ivtmp.31 != 10) goto <L0>; else goto <L2>;

<L2>:;
  return (int) val;


-- 
           Summary: Too much casting for char operands on tree level
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ubizjak at gmail dot com
 GCC build triplet: x86_64-pc-linux-gnu
  GCC host triplet: x86_64-pc-linux-gnu
GCC target triplet: x86_64-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31248


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug tree-optimization/31248] Too much casting for char operands on tree level
  2007-03-17 19:09 [Bug tree-optimization/31248] New: Too much casting for char operands on tree level ubizjak at gmail dot com
@ 2007-03-18  5:30 ` pinskia at gcc dot gnu dot org
  2007-03-29  6:16 ` [Bug rtl-optimization/31248] char adding gives an extra move or two pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-03-18  5:30 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2007-03-18 05:29 -------
You are wrong.  The casts are correct as char + char in C is actually defined
as (int)(char) + (int) (char) aka promoted to int while doing the addition so
what the optimizers do when it sees (char)((int)(char) + (int)(char)), it
changes the addition to be (unsigned char)(char) + (unsigned char)(char) so the
addition's overflow is defined as being wrapping instead of being undefined.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31248


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug rtl-optimization/31248] char adding gives an extra move or two
  2007-03-17 19:09 [Bug tree-optimization/31248] New: Too much casting for char operands on tree level ubizjak at gmail dot com
  2007-03-18  5:30 ` [Bug tree-optimization/31248] " pinskia at gcc dot gnu dot org
@ 2007-03-29  6:16 ` pinskia at gcc dot gnu dot org
  2007-08-28 12:02 ` [Bug rtl-optimization/31248] char adding (in loops) " ubizjak at gmail dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-03-29  6:16 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2007-03-29 07:15 -------
I am going to mark this as a rtl optimization issue because the tree level is
correct unless we get a PLUS_EXPR which is able to wrap without being
undefined.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
          Component|tree-optimization           |rtl-optimization
            Summary|Too much casting for char   |char adding gives an extra
                   |operands on tree level      |move or two


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31248


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug rtl-optimization/31248] char adding (in loops) gives an extra move or two
  2007-03-17 19:09 [Bug tree-optimization/31248] New: Too much casting for char operands on tree level ubizjak at gmail dot com
  2007-03-18  5:30 ` [Bug tree-optimization/31248] " pinskia at gcc dot gnu dot org
  2007-03-29  6:16 ` [Bug rtl-optimization/31248] char adding gives an extra move or two pinskia at gcc dot gnu dot org
@ 2007-08-28 12:02 ` ubizjak at gmail dot com
  2008-01-09  9:29 ` ubizjak at gmail dot com
  2008-01-09  9:46 ` ubizjak at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: ubizjak at gmail dot com @ 2007-08-28 12:02 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from ubizjak at gmail dot com  2007-08-28 12:02 -------
Current mainline [GCC: (GNU) 4.3.0 20070828] generates:

test:
.LFB2:
        xorl    %eax, %eax
        xorl    %edx, %edx
        .align 16
.L2:
        addb    table(%rdx), %al
        addq    $1, %rdx
        cmpq    $10, %rdx
        jne     .L2
        movsbl  %al,%eax
        ret

Now, there is an optimization problem in the intialization code in loop header.
To omit one iteration, it should start with:

.LFB2:
        movzbl  table(%rip), %ecx
        movl    $1, %edx
.L2:
        ...

BTW: By reversing the loop:

  for (i = 9; i; i--)
    val += table[i];

we could remove comparison from the loop, but instead we produce (x86_64):

test:
.LFB2:
        xorl    %eax, %eax
        xorl    %edx, %edx
        .align 16
.L2:
        addb    table+9(%rdx), %al
        subq    $1, %rdx
        cmpq    $-9, %rdx
        jne     .L2
        movsbl  %al,%eax
        ret

However, using -m32 we get:

test:
        xorl    %eax, %eax
        movl    $9, %edx
        .align 16
.L2:
        addb    table(%edx), %al
        subl    $1, %edx
        jne     .L2
        movsbl  %al,%eax
        ret

In reversed-loop case, we could generate:

test:
        movl    $9, %edx
        movzbl  table(%edx), %eax
        .align 16
.L2:
        addb    table(%edx), %al
        subl    $1, %edx
        jne     .L2
        movsbl  %al,%eax
        ret


-- 

ubizjak at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|char adding gives an extra  |char adding (in loops) gives
                   |move or two                 |an extra move or two


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31248


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug rtl-optimization/31248] char adding (in loops) gives an extra move or two
  2007-03-17 19:09 [Bug tree-optimization/31248] New: Too much casting for char operands on tree level ubizjak at gmail dot com
                   ` (2 preceding siblings ...)
  2007-08-28 12:02 ` [Bug rtl-optimization/31248] char adding (in loops) " ubizjak at gmail dot com
@ 2008-01-09  9:29 ` ubizjak at gmail dot com
  2008-01-09  9:46 ` ubizjak at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: ubizjak at gmail dot com @ 2008-01-09  9:29 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from ubizjak at gmail dot com  2008-01-09 08:23 -------
The problem with char moves is fixed in current SVN.

The problem with loop header from comment #3 is tree-optimization problem, I'll
open new PR for it.


-- 

ubizjak at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |FIXED


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31248


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug rtl-optimization/31248] char adding (in loops) gives an extra move or two
  2007-03-17 19:09 [Bug tree-optimization/31248] New: Too much casting for char operands on tree level ubizjak at gmail dot com
                   ` (3 preceding siblings ...)
  2008-01-09  9:29 ` ubizjak at gmail dot com
@ 2008-01-09  9:46 ` ubizjak at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: ubizjak at gmail dot com @ 2008-01-09  9:46 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from ubizjak at gmail dot com  2008-01-09 08:33 -------
-> PR tree-optimization/34723


-- 

ubizjak at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |4.3.0


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31248


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-01-09  8:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-17 19:09 [Bug tree-optimization/31248] New: Too much casting for char operands on tree level ubizjak at gmail dot com
2007-03-18  5:30 ` [Bug tree-optimization/31248] " pinskia at gcc dot gnu dot org
2007-03-29  6:16 ` [Bug rtl-optimization/31248] char adding gives an extra move or two pinskia at gcc dot gnu dot org
2007-08-28 12:02 ` [Bug rtl-optimization/31248] char adding (in loops) " ubizjak at gmail dot com
2008-01-09  9:29 ` ubizjak at gmail dot com
2008-01-09  9:46 ` ubizjak at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).