public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug pending/27971]  New: eliminate shift in array[(x>>2)&3]
@ 2006-06-09  6:18 dean at arctic dot org
  2006-06-09  6:20 ` [Bug target/27971] " pinskia at gcc dot gnu dot org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: dean at arctic dot org @ 2006-06-09  6:18 UTC (permalink / raw)
  To: gcc-bugs

array[(x>>2)&3] can be arranged to avoid the right shift.

consider:

% cat shift-and-fold.c
unsigned array[4];

unsigned foo(unsigned long x)
{
          return array[(x>>2)&3ul];
}
% /home/odo/gcc/bin/gcc -g -O3 -Wall   -c -o shift-and-fold.o shift-and-fold.c
% objdump -dr shift-and-fold.o

shift-and-fold.o:     file format elf64-x86-64

Disassembly of section .text:

0000000000000000 <foo>:
   0:   48 c1 ef 02             shr    $0x2,%rdi
   4:   83 e7 03                and    $0x3,%edi
   7:   8b 04 bd 00 00 00 00    mov    0x0(,%rdi,4),%eax
                        a: R_X86_64_32S array
   e:   c3                      retq

could be:

  and $0xc,%edi
  mov 0(%rdi),%eax

gcc does the right thing when the left shift isn't part of an address...

thanks
-dean

% /home/odo/gcc/bin/gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../gcc/configure --prefix=/home/odo/gcc
--host=x86_64-linux-gnu --disable-multilib --enable-languages=c,c++
--disable-bootstrap
Thread model: posix
gcc version 4.2.0 20060603 (experimental)


-- 
           Summary: eliminate shift in array[(x>>2)&3]
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: pending
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dean at arctic dot org
 GCC build triplet: x86_64-linux-gnu
  GCC host triplet: x86_64-linux-gnu
GCC target triplet: x86_64-linux-gnu


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


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

* [Bug target/27971] eliminate shift in array[(x>>2)&3]
  2006-06-09  6:18 [Bug pending/27971] New: eliminate shift in array[(x>>2)&3] dean at arctic dot org
@ 2006-06-09  6:20 ` pinskia at gcc dot gnu dot org
  2006-06-09  6:43 ` pinskia at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-06-09  6:20 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2006-06-09 06:18 -------
This works correctly on powerpc.  The rtl we get on x86_64 is:
(insn 10 7 11 2 (parallel [
            (set (reg:DI 61)
                (lshiftrt:DI (reg/v:DI 59 [ x ])
                    (const_int 2 [0x2])))
            (clobber (reg:CC 17 flags))
        ]) 456 {*lshrdi3_1_rex64} (insn_list:REG_DEP_TRUE 6 (nil))
    (expr_list:REG_DEAD (reg/v:DI 59 [ x ])
        (expr_list:REG_UNUSED (reg:CC 17 flags)
            (nil))))

(insn 11 10 13 2 (parallel [
            (set (reg:DI 62)
                (and:DI (reg:DI 61)
                    (const_int 3 [0x3])))
            (clobber (reg:CC 17 flags))
        ]) 297 {*anddi_1_rex64} (insn_list:REG_DEP_TRUE 10 (nil))
    (expr_list:REG_DEAD (reg:DI 61)
        (expr_list:REG_UNUSED (reg:CC 17 flags)
            (nil))))

(insn 13 11 17 2 (set (reg:SI 64)
        (mem/s:SI (plus:DI (mult:DI (reg:DI 62)
                    (const_int 4 [0x4]))
                (symbol_ref:DI ("array") <var_decl 0x2aaaaaf4bbb0 array>)) [3
array S4 A32])) 40 {*movsi_1} (insn_list:REG_DEP_TRUE 11 (nil))
    (expr_list:REG_DEAD (reg:DI 62)
        (nil)))

so I wonder if the something is not being reduced correctly.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
          Component|pending                     |target
     Ever Confirmed|0                           |1
   GCC host triplet|x86_64-linux-gnu            |
   Last reconfirmed|0000-00-00 00:00:00         |2006-06-09 06:18:37
               date|                            |


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


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

* [Bug target/27971] eliminate shift in array[(x>>2)&3]
  2006-06-09  6:18 [Bug pending/27971] New: eliminate shift in array[(x>>2)&3] dean at arctic dot org
  2006-06-09  6:20 ` [Bug target/27971] " pinskia at gcc dot gnu dot org
@ 2006-06-09  6:43 ` pinskia at gcc dot gnu dot org
  2007-11-28 23:06 ` raksit at google dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-06-09  6:43 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2006-06-09 06:20 -------
Here is the RTL we get on PPC:
(insn 6 9 7 2 (set (reg/v:SI 120 [ x ])
        (reg:SI 3 r3 [ x ])) 327 {*movsi_internal1} (nil)
    (expr_list:REG_DEAD (reg:SI 3 r3 [ x ])
        (expr_list:REG_EQUIV (mem/c/i:SI (plus:SI (reg/f:SI 67 ap)
                    (const_int 24 [0x18])) [4 x+0 S4 A32])
            (nil))))
...

(insn 13 12 14 2 (set (reg:SI 124)
        (lshiftrt:SI (reg/v:SI 120 [ x ])
            (const_int 2 [0x2]))) 195 {lshrsi3_no_power}
(insn_list:REG_DEP_TRUE 6 (nil))
    (expr_list:REG_DEAD (reg/v:SI 120 [ x ])
        (nil)))

(insn 14 13 15 2 (parallel [
            (set (reg:SI 125)
                (and:SI (reg:SI 124)
                    (const_int 3 [0x3])))
            (clobber (scratch:CC))
        ]) 146 {andsi3} (insn_list:REG_DEP_TRUE 13 (nil))
    (expr_list:REG_DEAD (reg:SI 124)
        (expr_list:REG_UNUSED (scratch:CC)
            (nil))))

(insn 15 14 17 2 (set (reg:SI 126)
        (ashift:SI (reg:SI 125)
            (const_int 2 [0x2]))) 189 {ashlsi3_no_power}
(insn_list:REG_DEP_TRUE 14 (nil))
    (expr_list:REG_DEAD (reg:SI 125)
        (nil)))

Which gets reduced to:
(insn 15 14 17 2 (parallel [
            (set (reg:SI 126)
                (and:SI (reg:SI 3 r3 [ x ])
                    (const_int 12 [0xc])))
            (clobber (scratch:CC))
        ]) 146 {andsi3} (nil)
    (expr_list:REG_UNUSED (scratch:CC)
        (expr_list:REG_DEAD (reg:SI 3 r3 [ x ])
            (nil))))


-- 


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


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

* [Bug target/27971] eliminate shift in array[(x>>2)&3]
  2006-06-09  6:18 [Bug pending/27971] New: eliminate shift in array[(x>>2)&3] dean at arctic dot org
  2006-06-09  6:20 ` [Bug target/27971] " pinskia at gcc dot gnu dot org
  2006-06-09  6:43 ` pinskia at gcc dot gnu dot org
@ 2007-11-28 23:06 ` raksit at google dot com
  2007-12-05 19:28 ` raksit at google dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: raksit at google dot com @ 2007-11-28 23:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from raksit at google dot com  2007-11-28 23:06 -------
I am looking into this.


-- 

raksit at google dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |raksit at google dot com


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


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

* [Bug target/27971] eliminate shift in array[(x>>2)&3]
  2006-06-09  6:18 [Bug pending/27971] New: eliminate shift in array[(x>>2)&3] dean at arctic dot org
                   ` (2 preceding siblings ...)
  2007-11-28 23:06 ` raksit at google dot com
@ 2007-12-05 19:28 ` raksit at google dot com
  2008-01-11  4:51 ` raksit at gcc dot gnu dot org
  2008-01-14 19:57 ` raksit at gcc dot gnu dot org
  5 siblings, 0 replies; 7+ messages in thread
From: raksit at google dot com @ 2007-12-05 19:28 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from raksit at google dot com  2007-12-05 19:27 -------
For the rtl emitted on x86 processors, the combiner is almost able to optimize
the shift away. It combines and simplifies the 3 instructions down to:

Failed to match this instruction:
(set (reg:SI 64)
   (mem/s:SI (plus:SI (and:SI (reg/v:SI 59 [ x ])
               (const_int 12 [0xc]))
           (symbol_ref:SI ("array") <var_decl 0xf7ef20b0 array>)) [3
array S4 A32]))

It gives up at this point. The solution is to introduce a split to do the AND,
followed by the load. This can be done in machine-independent way by modifying
find_split_point in combine.c. Patch coming up shortly.

-raksit


-- 


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


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

* [Bug target/27971] eliminate shift in array[(x>>2)&3]
  2006-06-09  6:18 [Bug pending/27971] New: eliminate shift in array[(x>>2)&3] dean at arctic dot org
                   ` (3 preceding siblings ...)
  2007-12-05 19:28 ` raksit at google dot com
@ 2008-01-11  4:51 ` raksit at gcc dot gnu dot org
  2008-01-14 19:57 ` raksit at gcc dot gnu dot org
  5 siblings, 0 replies; 7+ messages in thread
From: raksit at gcc dot gnu dot org @ 2008-01-11  4:51 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from raksit at gcc dot gnu dot org  2008-01-11 04:21 -------
Subject: Bug 27971

Author: raksit
Date: Fri Jan 11 04:20:32 2008
New Revision: 131460

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=131460
Log:
gcc/ChangeLog
       PR rtl-optimization/27971
       * combine.c (find_split_point): introduced a new split for certain
       types of mem rtx.

gcc/testsuite/ChangeLog

       PR rtl-optimization/27971
       * gcc.target/i386/pr27971.c: New testcase.


Added:
    trunk/gcc/testsuite/gcc.target/i386/pr27971.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/combine.c
    trunk/gcc/testsuite/ChangeLog


-- 


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


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

* [Bug target/27971] eliminate shift in array[(x>>2)&3]
  2006-06-09  6:18 [Bug pending/27971] New: eliminate shift in array[(x>>2)&3] dean at arctic dot org
                   ` (4 preceding siblings ...)
  2008-01-11  4:51 ` raksit at gcc dot gnu dot org
@ 2008-01-14 19:57 ` raksit at gcc dot gnu dot org
  5 siblings, 0 replies; 7+ messages in thread
From: raksit at gcc dot gnu dot org @ 2008-01-14 19:57 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from raksit at gcc dot gnu dot org  2008-01-14 19:27 -------
This was fixed in revision 131460.


-- 

raksit at gcc dot gnu dot org changed:

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


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


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

end of thread, other threads:[~2008-01-14 19:27 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-09  6:18 [Bug pending/27971] New: eliminate shift in array[(x>>2)&3] dean at arctic dot org
2006-06-09  6:20 ` [Bug target/27971] " pinskia at gcc dot gnu dot org
2006-06-09  6:43 ` pinskia at gcc dot gnu dot org
2007-11-28 23:06 ` raksit at google dot com
2007-12-05 19:28 ` raksit at google dot com
2008-01-11  4:51 ` raksit at gcc dot gnu dot org
2008-01-14 19:57 ` raksit at gcc dot gnu dot org

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).