public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/41244]  New: "&data[i] - data" isn't converted to "i"
@ 2009-09-03 13:49 zsojka at seznam dot cz
  2009-09-03 13:51 ` [Bug tree-optimization/41244] " zsojka at seznam dot cz
  2009-09-03 14:12 ` rguenth at gcc dot gnu dot org
  0 siblings, 2 replies; 3+ messages in thread
From: zsojka at seznam dot cz @ 2009-09-03 13:49 UTC (permalink / raw)
  To: gcc-bugs

In attached code:
in find(), generated code computes offset using multiplication/division.
in set(), generated code computes &data[10] and compares &data[i] with that (to
verify find() fails to be optimised because of overflow rules)

tested gcc: 4.3.4, 4.4.1, 4.5.0-alpha20090827

command line:
gcc main.c -o main.o -c -O3
-f(no-)strict-overflow or -m32 doesn't have any effect on that behaviour

---------------------------------
static struct {
        int a, b, c;
        char pad[200]; /* padding so offset computation has to use expensive
div/mul */
} data[1000]; /* 1000 so loop isn't unrolled */

int find(int val)
{
        int i;
        for (i = 0; i < 1000; i++) {
                if (data[i].a == val) return &data[i] - data; /* does *212,
/212 */
        }
        return -1;
}

void set(int val)
{
        int i;
        for (i = 0; i < 1000; i++) {
                data[i].b = &data[i] - data < 10 || data[i].a == val; /*
compares with &data[10] */
        }
}
---------------------------------

Relevant generated ASM:
(eax is "i", sar+imul is converted division by 212)
---------------------------------
        cdqe
        imul    rax, rax, 212
        sar     rax, 2
        imul    eax, eax, -1944890851
        ret
---------------------------------


-- 
           Summary: "&data[i] - data" isn't converted to "i"
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: zsojka at seznam dot cz
  GCC host triplet: x86_64-pc-linux-gnu


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


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

* [Bug tree-optimization/41244] "&data[i] - data" isn't converted to "i"
  2009-09-03 13:49 [Bug tree-optimization/41244] New: "&data[i] - data" isn't converted to "i" zsojka at seznam dot cz
@ 2009-09-03 13:51 ` zsojka at seznam dot cz
  2009-09-03 14:12 ` rguenth at gcc dot gnu dot org
  1 sibling, 0 replies; 3+ messages in thread
From: zsojka at seznam dot cz @ 2009-09-03 13:51 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from zsojka at seznam dot cz  2009-09-03 13:50 -------
Created an attachment (id=18475)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=18475&action=view)
preprocessed source


-- 


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


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

* [Bug tree-optimization/41244] "&data[i] - data" isn't converted to "i"
  2009-09-03 13:49 [Bug tree-optimization/41244] New: "&data[i] - data" isn't converted to "i" zsojka at seznam dot cz
  2009-09-03 13:51 ` [Bug tree-optimization/41244] " zsojka at seznam dot cz
@ 2009-09-03 14:12 ` rguenth at gcc dot gnu dot org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2009-09-03 14:12 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from rguenth at gcc dot gnu dot org  2009-09-03 14:12 -------
Because the index arithmetic is done unsigned.

  return (int) ((unsigned int) i * 212) /[ex] 212;

We lost the information that i * 212 cannot overflow.

Simpler testcase:

extern int data[];

int find(int i)
{
  return &data[i] - data;
}

from which we enter c_fully_fold_internal with

((int) ((int *) &data + (unsigned int) ((unsigned int) i * 4)) - (int) &data)
/[ex] 4

addressed on the no-undefined-overflow branch.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
           Keywords|                            |missed-optimization
   Last reconfirmed|0000-00-00 00:00:00         |2009-09-03 14:12:37
               date|                            |


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


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

end of thread, other threads:[~2009-09-03 14:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-09-03 13:49 [Bug tree-optimization/41244] New: "&data[i] - data" isn't converted to "i" zsojka at seznam dot cz
2009-09-03 13:51 ` [Bug tree-optimization/41244] " zsojka at seznam dot cz
2009-09-03 14:12 ` rguenth 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).