public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: rguenth@tat.physik.uni-tuebingen.de
To: gcc-gnats@gcc.gnu.org
Subject: optimization/6883: Fails to optimize temporary objects.
Date: Fri, 31 May 2002 03:46:00 -0000	[thread overview]
Message-ID: <20020531100036.22507.qmail@sources.redhat.com> (raw)


>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);
  }
}


             reply	other threads:[~2002-05-31 10:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-31  3:46 rguenth [this message]
2003-05-10 23:36 Dara Hazeghi
2003-05-11 11:16 Richard Guenther
2003-05-11 12:06 Richard Guenther

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020531100036.22507.qmail@sources.redhat.com \
    --to=rguenth@tat.physik.uni-tuebingen.de \
    --cc=gcc-gnats@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).