This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

optimization/6883: Fails to optimize temporary objects.



>Number:         6883
>Category:       optimization
>Synopsis:       Fails to optimize temporary objects.
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          pessimizes-code
>Submitter-Id:   net
>Arrival-Date:   Fri May 31 03:06:02 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Richard Guenther <rguenth@tat.physik.uni-tuebingen.de>
>Release:        gcc version 3.1.1 20020516 (prerelease)
>Organization:
>Environment:
Linux bellatrix 2.4.18-rc4-TATUP #2 Tue Mar 19 16:30:05 CET 2002 i686 unknown
>Description:
Temporary objects are not optimized away, which is a serious problem for medium-complex iterators. See attachment for an example.

The first loop body which uses temporary objects compiles (ix86, -O3 -fno-exceptions) to

        movl    -88(%ebp), %ecx
        movl    -80(%ebp), %eax
        movl    -140(%ebp), %esi
        movl    %ecx, -104(%ebp)
        movl    -56(%ebp), %ebx
        leal    0(,%eax,8), %edx
        movl    %ecx, -120(%ebp)
        movl    -40(%ebp), %edi
        movl    -140(%ebp), %ecx
        movl    %esi, -100(%ebp)
        leal    -1(%eax), %esi
        incl    %eax
        movl    %eax, -112(%ebp)
        addl    %edx, %edi
        movl    %esi, -96(%ebp)
        movl    %ecx, -116(%ebp)
        fldl    (%ebx,%eax,8)
        movl    -72(%ebp), %eax
        faddl   (%ebx,%esi,8)
        addl    %eax, %edx
        fmull   (%edx)
        fstpl   (%edi)

which fails to optimize the created temporary objects. It should be optimized to the equivalent code for the second implementation manually doing this optimization:

        movl    -128(%ebp), %edi
        movl    -56(%ebp), %eax
        movl    -40(%ebp), %esi
        movl    -72(%ebp), %ecx
        leal    0(,%edi,8), %ebx
        addl    %ebx, %esi
        fldl    8(%eax,%edi,8)
        addl    %ecx, %ebx
        faddl   -8(%eax,%edi,8)
        fmull   (%ebx)
        fstpl   (%esi)

This problem pessimizes code which uses iterators like that by about a factor of 3 compared to equivalent C code which is a serious problem.
>How-To-Repeat:

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="iterator-gnats.cpp"
Content-Disposition: inline; filename="iterator-gnats.cpp"


class Iterator {
public:
  Iterator(int i0, int i1)
    : m_i0(i0), m_i1(i1)
  {
    m_i = i0;
  }

  Iterator operator+(int i) const {
    Iterator it(*this);
    it.m_i += i;
    return it;
  }
  Iterator operator-(int i) const {
    Iterator it(*this);
    it.m_i -= i;
    return it;
  }

  bool operator++()
  {
    m_i++;
    if (m_i > m_i1)
      return false;
    return true;
  }

  int getIndex() const { return m_i; }

private:
  const int m_i0, m_i1;
  int m_i;
};

class Array {
public:
  Array(int size)
    : m_v(new double(size)) {}
  ~Array() { delete m_v; }

  double& operator()(const Iterator& i) { return m_v[i.getIndex()]; }
  double& operator()(const Iterator& i, int d) { return m_v[i.getIndex()+d]; }

private:
  double * const m_v;
};


int main(int argc, char **argv)
{
  Array a(256);
  Array b(256);
  Array c(256);

  // temporary objects not optimized
  {
    Iterator i(1, 254);
    do {
      a(i) = (b(i-1)+b(i+1))*c(i);
    } while (++i);
  }

  // optimized ok
  {
    Iterator i(1, 254);
    do {
      a(i) = (b(i,-1)+b(i,+1))*c(i);
    } while (++i);
  }
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]