public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "drepper.fsp at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/54855] New: Unnecessary duplication when performing scalar operation on vector element
Date: Mon, 08 Oct 2012 13:58:00 -0000	[thread overview]
Message-ID: <bug-54855-4@http.gcc.gnu.org/bugzilla/> (raw)


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

             Bug #: 54855
           Summary: Unnecessary duplication when performing scalar
                    operation on vector element
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: drepper.fsp@gmail.com


Take the following code:


#include <stdio.h>

typedef double v2df __attribute__((vector_size(16)));

int
main(int argc, char *argv[])
{
  v2df v = { 2.0, 2.0 };
  v2df v2 = { 2.0, 2.0 };
  while (argc-- > 1)
    {
      v[0] -= 1.0;
      v *= v2;
    }
  printf("%g\n", v[0] + v[1]);
  return 0;
}

It compiles as C and C++, both compilers behave the same.

When compiling on x86-64 (therefore with SSE enabled) it generates for the loop
this code:


  4003f0:       66 0f 28 c1             movapd %xmm1,%xmm0
  4003f4:       83 e8 01                sub    $0x1,%eax
  4003f7:       f2 0f 5c c2             subsd  %xmm2,%xmm0
  4003fb:       f2 0f 10 c8             movsd  %xmm0,%xmm1
  4003ff:       66 0f 58 c9             addpd  %xmm1,%xmm1
  400403:       75 eb                   jne    4003f0 <main+0x20>


I.e., the value is pulled out of the vector, the subtraction is performed, and
then the scalar value is put back into the vector.

Instead the following sequence would have been completely sufficient:

sub    $0x1,%eax
subsd  %xmm2,%xmm1
addpd  %xmm1,%xmm1
jne    ...back

The subsd instruction doesn't touch the high parts of the register.


I know this is a special case, it only works if the scalar operation is for the
element zero of the vector.  But code can be designed like that.  I have some
code which would work nicely like this.  I don't know whether this translates
to other architectures as well.


             reply	other threads:[~2012-10-08 13:58 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-08 13:58 drepper.fsp at gmail dot com [this message]
2012-10-08 14:19 ` [Bug tree-optimization/54855] " rguenth at gcc dot gnu.org
2012-10-12 13:41 ` glisse at gcc dot gnu.org
2012-10-12 17:08 ` glisse at gcc dot gnu.org
2012-10-12 17:34 ` glisse at gcc dot gnu.org
2012-10-12 18:09 ` glisse at gcc dot gnu.org
2012-10-20 17:44 ` glisse at gcc dot gnu.org
2012-11-30  1:31 ` glisse at gcc dot gnu.org
2013-09-06  8:21 ` glisse at gcc dot gnu.org

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=bug-54855-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).