public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/36235]  New: Invalid optimization related to heavy function inlining with -O3
@ 2008-05-14 16:52 cxl at ntllib dot org
  2008-05-14 16:53 ` [Bug c++/36235] " cxl at ntllib dot org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: cxl at ntllib dot org @ 2008-05-14 16:52 UTC (permalink / raw)
  To: gcc-bugs

g++ (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Used flags:
-ggdb -g2 -fexceptions -O3 -x c++

Code (has to be in 4 files to keep inlining constelation):

-----------------
CGGBug.h

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <new>

template <class T> inline const T& my_max(const T& a, const T& b) { return a >
b ? a : b; }
template <class T> inline const T& my_min(const T& a, const T& b) { return a <
b ? a : b; }

template <class T>
inline T minmax(T x, T _min, T _max) { return my_min(my_max(x, _min), _max); }

void *MemoryAllocSz(size_t& sz);
void  MemoryFree(void *);

template <class T>
class Vector {
        T       *vector;
        int      items;
        int      alloc;

        static void RawFree(T *ptr)            { if(ptr) MemoryFree(ptr); }
        static T   *RawAlloc(int& n);

        void RawInsert(int q, int count);


public:
        void  InsertN(int q, int count);
        const T& First() { return vector[0]; }
        int   GetCount() const { return items; }

        Vector() { vector = NULL; items = alloc = 0; }
};

template <class T>
T * Vector<T>::RawAlloc(int& n)
{
        size_t sz0 = n * sizeof(T);
        size_t sz = sz0;
        void *q = MemoryAllocSz(sz);
        n += (int)((sz - sz0) / sizeof(T));
        return (T *)q;
}

template <class T>
void Vector<T>::RawInsert(int q, int count)
{
        if(!count) return;
        if(items + count > alloc) {
                T *newvector = RawAlloc(alloc = alloc + my_max(alloc, count));
                if(vector) {
                        memcpy(newvector, vector, q * sizeof(T));
                        memcpy(newvector + q + count, vector + q, (items - q) *
sizeof(T));
                        RawFree(vector);
                }
                vector = newvector;
        }
        else {
                memmove(vector + q + count, vector + q, (items - q) *
sizeof(T));
        }
        items += count;
}

template <class T>
void Vector<T>::InsertN(int q, int count)
{
        RawInsert(q, count);
}

void *MemoryAllocSz(size_t& sz);
void  MemoryFree(void *);

struct Item {
        char h[32];
};

struct Bar {
        Vector<Item> li;

        void DoTest(int i, int count);
};

-------
GCCBug1.cpp:

#include "GCCBug.h"

char array[256];

void *MemoryAllocSz(size_t& sz)
{
        printf("%d\n", sz);
        return array;
}

void MemoryFree(void *) {}

---------
GCCBug2.cpp:

#include "GCCBug.h"

void Bar::DoTest(int i, int count)
{
        li.InsertN(minmax(i, 0, li.GetCount()), my_max(count, 0));
}
--------
GCCBug.cpp:

#include "GCCBug.h"

int main(int argc, char argv[])
{
        Bar b;
        b.DoTest(0, 1);
        return 0;
}
---------------

This code should print "32" (== sizeof(T)) and it does when compiled -Os, but
compiled -O3 it prints "0". In assembly there seems a bug at the beginning of
inlined "RawAlloc" method around the shift to perform "n * sizeof(T)" multiply.

Note that changing sizeof(T) to something else than power of two makes the code
work as expected.

AMD64 compiler seems to be affected as well.


-- 
           Summary: Invalid optimization related to heavy function inlining
                    with -O3
           Product: gcc
           Version: 4.1.3
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: cxl at ntllib dot org
 GCC build triplet: x86
  GCC host triplet: x86
GCC target triplet: x86


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


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

* [Bug c++/36235] Invalid optimization related to heavy function inlining with -O3
  2008-05-14 16:52 [Bug c++/36235] New: Invalid optimization related to heavy function inlining with -O3 cxl at ntllib dot org
@ 2008-05-14 16:53 ` cxl at ntllib dot org
  2008-05-14 16:55 ` [Bug middle-end/36235] " pinskia at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: cxl at ntllib dot org @ 2008-05-14 16:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from cxl at ntllib dot org  2008-05-14 16:52 -------
(fixed compiler version)


-- 

cxl at ntllib dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|4.1.3                       |4.2.3


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


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

* [Bug middle-end/36235] Invalid optimization related to heavy function inlining with -O3
  2008-05-14 16:52 [Bug c++/36235] New: Invalid optimization related to heavy function inlining with -O3 cxl at ntllib dot org
  2008-05-14 16:53 ` [Bug c++/36235] " cxl at ntllib dot org
@ 2008-05-14 16:55 ` pinskia at gcc dot gnu dot org
  2008-05-15  8:52 ` [Bug middle-end/36235] [4.2 Regression] " rguenth at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-05-14 16:55 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2008-05-14 16:54 -------
Can you try 4.3.0?


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pinskia at gcc dot gnu dot
                   |                            |org
           Severity|critical                    |normal
          Component|c++                         |middle-end
           Keywords|                            |wrong-code


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


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

* [Bug middle-end/36235] [4.2 Regression] Invalid optimization related to heavy function inlining with -O3
  2008-05-14 16:52 [Bug c++/36235] New: Invalid optimization related to heavy function inlining with -O3 cxl at ntllib dot org
  2008-05-14 16:53 ` [Bug c++/36235] " cxl at ntllib dot org
  2008-05-14 16:55 ` [Bug middle-end/36235] " pinskia at gcc dot gnu dot org
@ 2008-05-15  8:52 ` rguenth at gcc dot gnu dot org
  2008-05-19 20:35 ` jsm28 at gcc dot gnu dot org
  2009-03-31 15:38 ` jsm28 at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-05-15  8:52 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from rguenth at gcc dot gnu dot org  2008-05-15 08:51 -------
Confirmed.  It fails with -O -finline-functions -fstrict-aliasing.  Works with
4.3.0.  I suppose this is a dup of one of the known alias bugs with 4.2.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
           Keywords|                            |alias
      Known to fail|                            |4.2.3
      Known to work|                            |4.1.2 4.3.0
   Last reconfirmed|0000-00-00 00:00:00         |2008-05-15 08:51:27
               date|                            |
            Summary|Invalid optimization related|[4.2 Regression] Invalid
                   |to heavy function inlining  |optimization related to
                   |with -O3                    |heavy function inlining with
                   |                            |-O3
   Target Milestone|---                         |4.2.4


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


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

* [Bug middle-end/36235] [4.2 Regression] Invalid optimization related to heavy function inlining with -O3
  2008-05-14 16:52 [Bug c++/36235] New: Invalid optimization related to heavy function inlining with -O3 cxl at ntllib dot org
                   ` (2 preceding siblings ...)
  2008-05-15  8:52 ` [Bug middle-end/36235] [4.2 Regression] " rguenth at gcc dot gnu dot org
@ 2008-05-19 20:35 ` jsm28 at gcc dot gnu dot org
  2009-03-31 15:38 ` jsm28 at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: jsm28 at gcc dot gnu dot org @ 2008-05-19 20:35 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from jsm28 at gcc dot gnu dot org  2008-05-19 20:25 -------
4.2.4 is being released, changing milestones to 4.2.5.


-- 

jsm28 at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|4.2.4                       |4.2.5


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


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

* [Bug middle-end/36235] [4.2 Regression] Invalid optimization related to heavy function inlining with -O3
  2008-05-14 16:52 [Bug c++/36235] New: Invalid optimization related to heavy function inlining with -O3 cxl at ntllib dot org
                   ` (3 preceding siblings ...)
  2008-05-19 20:35 ` jsm28 at gcc dot gnu dot org
@ 2009-03-31 15:38 ` jsm28 at gcc dot gnu dot org
  4 siblings, 0 replies; 6+ messages in thread
From: jsm28 at gcc dot gnu dot org @ 2009-03-31 15:38 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from jsm28 at gcc dot gnu dot org  2009-03-31 15:38 -------
Closing 4.2 branch, fixed in 4.3.


-- 

jsm28 at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
      Known to fail|4.2.3                       |4.2.3 4.2.5
         Resolution|                            |FIXED
   Target Milestone|4.2.5                       |4.3.0


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


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

end of thread, other threads:[~2009-03-31 15:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-14 16:52 [Bug c++/36235] New: Invalid optimization related to heavy function inlining with -O3 cxl at ntllib dot org
2008-05-14 16:53 ` [Bug c++/36235] " cxl at ntllib dot org
2008-05-14 16:55 ` [Bug middle-end/36235] " pinskia at gcc dot gnu dot org
2008-05-15  8:52 ` [Bug middle-end/36235] [4.2 Regression] " rguenth at gcc dot gnu dot org
2008-05-19 20:35 ` jsm28 at gcc dot gnu dot org
2009-03-31 15:38 ` jsm28 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).