public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/40697] inefficient code to extract least bits from an integer value
  2009-07-09  9:24 [Bug target/40697] New: inefficient code to extract least bits from an integer value carrot at google dot com
@ 2009-07-09  9:24 ` carrot at google dot com
  2009-07-09  9:59 ` steven at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: carrot at google dot com @ 2009-07-09  9:24 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from carrot at google dot com  2009-07-09 09:24 -------
Created an attachment (id=18166)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=18166&action=view)
test case


-- 


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


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

* [Bug target/40697]  New: inefficient code to extract least bits from an integer value
@ 2009-07-09  9:24 carrot at google dot com
  2009-07-09  9:24 ` [Bug target/40697] " carrot at google dot com
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: carrot at google dot com @ 2009-07-09  9:24 UTC (permalink / raw)
  To: gcc-bugs

Compile following function with options -Os -mthumb -march=armv5te

unsigned get_least_bits(unsigned value)
{
  return value << 9 >> 9;
}

Gcc generates:

        ldr     r3, .L2
        @ sp needed for prologue
        and     r0, r0, r3
        bx      lr
.L3:
        .align  2
.L2:
        .word   8388607

A better code sequence should be:

       lsl   r0, 9
       lsr   r0, 9
       bx    lr

It is smaller (without constant pool) and faster.

This transformation was done very early and we can see it in the first tree
dump shift.c.003t.original. Gcc thinks and with a constant is cheaper than two
shifts. It is not true for this case in thumb ISA. On the other hand if the
constant used to and is small, such as 7, it is definitely cheaper than two
shifts. So which method is better is highly depend on both the constant and the
target ISA. It is difficult to make a correct decision in the TREE level. Maybe
we should define a peephole rule to do it.


-- 
           Summary: inefficient code to extract least bits from an integer
                    value
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: carrot at google dot com
 GCC build triplet: i686-linux
  GCC host triplet: i686-linux
GCC target triplet: arm-eabi


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


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

* [Bug target/40697] inefficient code to extract least bits from an integer value
  2009-07-09  9:24 [Bug target/40697] New: inefficient code to extract least bits from an integer value carrot at google dot com
  2009-07-09  9:24 ` [Bug target/40697] " carrot at google dot com
@ 2009-07-09  9:59 ` steven at gcc dot gnu dot org
  2009-07-09 10:32 ` ramana at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: steven at gcc dot gnu dot org @ 2009-07-09  9:59 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from steven at gcc dot gnu dot org  2009-07-09 09:59 -------
Maybe we can fix this in expand instead: if we see (x & CONST) and CONST is a
masking constant that isn't a legitimate constant for the the target, then see
if the sum of the rtx_cost of expressing the mask as shifts is less than the
rtx_cost of a load and an AND.

I think (but I'm not sure...) that if you do this with a peephole, you're too
late to avoid the constant pool.

Is this also a size issue?


-- 

steven at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2009-07-09 09:59:30
               date|                            |


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


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

* [Bug target/40697] inefficient code to extract least bits from an integer value
  2009-07-09  9:24 [Bug target/40697] New: inefficient code to extract least bits from an integer value carrot at google dot com
  2009-07-09  9:24 ` [Bug target/40697] " carrot at google dot com
  2009-07-09  9:59 ` steven at gcc dot gnu dot org
@ 2009-07-09 10:32 ` ramana at gcc dot gnu dot org
  2010-03-16 10:56 ` bernds at codesourcery dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: ramana at gcc dot gnu dot org @ 2009-07-09 10:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from ramana at gcc dot gnu dot org  2009-07-09 10:32 -------
(In reply to comment #2)
> Maybe we can fix this in expand instead: if we see (x & CONST) and CONST is a
> masking constant that isn't a legitimate constant for the the target, then see
> if the sum of the rtx_cost of expressing the mask as shifts is less than the
> rtx_cost of a load and an AND.
> 
> I think (but I'm not sure...) that if you do this with a peephole, you're too
> late to avoid the constant pool.

A peephole2 will do the trick . Constant pools in the ARM port are generated
and placed as a part of the reorg pass.


> 
> Is this also a size issue?

Indeed - instead of 6 bytes (2 bytes for the ldr and 4 bytes for the .word
which ends up in the .text section anyway.) instructions and you save a memory
access. 



> 


-- 


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


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

* [Bug target/40697] inefficient code to extract least bits from an integer value
  2009-07-09  9:24 [Bug target/40697] New: inefficient code to extract least bits from an integer value carrot at google dot com
                   ` (2 preceding siblings ...)
  2009-07-09 10:32 ` ramana at gcc dot gnu dot org
@ 2010-03-16 10:56 ` bernds at codesourcery dot com
  2010-03-19 18:41 ` bernds at gcc dot gnu dot org
  2010-03-20 19:32 ` ramana at gcc dot gnu dot org
  5 siblings, 0 replies; 7+ messages in thread
From: bernds at codesourcery dot com @ 2010-03-16 10:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from bernds at codesourcery dot com  2010-03-16 10:56 -------
Created an attachment (id=20117)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=20117&action=view)
A patch to fix it.

The andsi3 expander has code to do the right thing, but
avoid_expensive_constant prevents it from seeing the constant.  This can be
fixed by tweaking the rtx_costs to detect the same cases as the expander.


-- 


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


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

* [Bug target/40697] inefficient code to extract least bits from an integer value
  2009-07-09  9:24 [Bug target/40697] New: inefficient code to extract least bits from an integer value carrot at google dot com
                   ` (3 preceding siblings ...)
  2010-03-16 10:56 ` bernds at codesourcery dot com
@ 2010-03-19 18:41 ` bernds at gcc dot gnu dot org
  2010-03-20 19:32 ` ramana at gcc dot gnu dot org
  5 siblings, 0 replies; 7+ messages in thread
From: bernds at gcc dot gnu dot org @ 2010-03-19 18:41 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from bernds at gcc dot gnu dot org  2010-03-19 18:41 -------
Subject: Bug 40697

Author: bernds
Date: Fri Mar 19 18:41:22 2010
New Revision: 157582

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=157582
Log:
gcc/
        PR target/40697
        * optabs.c (avoid_expensive_constant): Use rtx_cost to find out
        the cost of loading the constant rather than assuming
        COSTS_N_INSNS (1).
        * config/arm/arm.c (thumb1_rtx_costs) <case CONST_INT>: If the
        outer code is AND, do the same tests as the andsi3 expander and
        return COSTS_N_INSNS (1) if and is cheap.

testsuite/
        PR target/40697
        * gcc.target/arm/thumb-andsi.c: New test.


Added:
    trunk/gcc/testsuite/gcc.target/arm/thumb-andsi.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/arm/arm.c
    trunk/gcc/optabs.c
    trunk/gcc/testsuite/ChangeLog


-- 


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


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

* [Bug target/40697] inefficient code to extract least bits from an integer value
  2009-07-09  9:24 [Bug target/40697] New: inefficient code to extract least bits from an integer value carrot at google dot com
                   ` (4 preceding siblings ...)
  2010-03-19 18:41 ` bernds at gcc dot gnu dot org
@ 2010-03-20 19:32 ` ramana at gcc dot gnu dot org
  5 siblings, 0 replies; 7+ messages in thread
From: ramana at gcc dot gnu dot org @ 2010-03-20 19:32 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from ramana at gcc dot gnu dot org  2010-03-20 19:32 -------
Now Fixed . Yay. 


-- 

ramana at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
           Keywords|                            |missed-optimization
      Known to fail|                            |4.5.0
         Resolution|                            |FIXED
   Target Milestone|---                         |4.5.0


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


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

end of thread, other threads:[~2010-03-20 19:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-09  9:24 [Bug target/40697] New: inefficient code to extract least bits from an integer value carrot at google dot com
2009-07-09  9:24 ` [Bug target/40697] " carrot at google dot com
2009-07-09  9:59 ` steven at gcc dot gnu dot org
2009-07-09 10:32 ` ramana at gcc dot gnu dot org
2010-03-16 10:56 ` bernds at codesourcery dot com
2010-03-19 18:41 ` bernds at gcc dot gnu dot org
2010-03-20 19:32 ` ramana 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).