public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/47579] New: STL size() == 0 does unnecessary shift
@ 2011-02-01 19:52 ian at airs dot com
  2011-02-01 22:54 ` [Bug tree-optimization/47579] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: ian at airs dot com @ 2011-02-01 19:52 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: STL size() == 0 does unnecessary shift
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: ian@airs.com


Consider this C++ code:

#include <vector>
extern void b1(), b2();
void foo(const std::vector<int>& v) { if (v.size() == 0) b1(); else b2(); }

When I compile it with current mainline with -O2 on x86_64, I get this:

        movq    8(%rdi), %rax
        subq    (%rdi), %rax
        sarq    $2, %rax
        testq   %rax, %rax
        ...

That sarq instruction is useless.  We know that the two values being subtracted
are both aligned pointers, so we should know that the two least significant
bits of the result are zero.  And that should be enough to let us know that we
don't need to shift before comparing for equality with zero.


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

* [Bug tree-optimization/47579] STL size() == 0 does unnecessary shift
  2011-02-01 19:52 [Bug tree-optimization/47579] New: STL size() == 0 does unnecessary shift ian at airs dot com
@ 2011-02-01 22:54 ` rguenth at gcc dot gnu.org
  2012-01-06  0:33 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-02-01 22:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-02-01 22:54:19 UTC ---
I think v.size() should be simply optimized to work on char * pointer
differences.  Recognizing the optimization opportunity requires to see
dereferences of the pointers to deduce their alignment.


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

* [Bug tree-optimization/47579] STL size() == 0 does unnecessary shift
  2011-02-01 19:52 [Bug tree-optimization/47579] New: STL size() == 0 does unnecessary shift ian at airs dot com
  2011-02-01 22:54 ` [Bug tree-optimization/47579] " rguenth at gcc dot gnu.org
@ 2012-01-06  0:33 ` pinskia at gcc dot gnu.org
  2020-12-25 12:43 ` vanyacpp at gmail dot com
  2022-11-27  6:32 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-01-06  0:33 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2012-01-06
     Ever Confirmed|0                           |1

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-01-06 00:32:36 UTC ---
Two more interesting testcases:
void f(int *a, int *b)  {if((b-a)==0) b1(); else b2();}
void f1(int *a, int *b)
{
  std::size_t t = a-b;
  if (t == 0)  b1(); else b2();
}
--- CUT ---
f produces:
    .cfi_startproc
    subq    %rdi, %rsi
    addq    $3, %rsi
    cmpq    $6, %rsi
    jbe    .L7
    jmp    _Z2b2v
    .p2align 4,,10
    .p2align 3
.L7:
    jmp    _Z2b1v
    .cfi_endproc

While f1 produces:
    .cfi_startproc
    subq    %rsi, %rdi
    sarq    $2, %rdi
    testq    %rdi, %rdi
    je    .L10
    jmp    _Z2b2v
    .p2align 4,,10
    .p2align 3
.L10:
    jmp    _Z2b1v
    .cfi_endproc
Which is the same as your foo.  So we should be able to do the same as f for
foo.
f:
  b.2_2 = (long int) b_1(D);
  a.3_4 = (long int) a_3(D);
  D.8126_5 = b.2_2 - a.3_4;
  D.8127_6 = (long unsigned int) D.8126_5;
  D.8128_7 = D.8127_6 + 3;
  if (D.8128_7 <= 6)
    goto <bb 3>;
  else
    goto <bb 4>;

f1 (and foo really):
  a.0_2 = (long int) a_1(D);
  b.1_4 = (long int) b_3(D);
  D.8119_5 = a.0_2 - b.1_4;
  D.8120_6 = D.8119_5 /[ex] 4;
  if (D.8120_6 == 0)


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

* [Bug tree-optimization/47579] STL size() == 0 does unnecessary shift
  2011-02-01 19:52 [Bug tree-optimization/47579] New: STL size() == 0 does unnecessary shift ian at airs dot com
  2011-02-01 22:54 ` [Bug tree-optimization/47579] " rguenth at gcc dot gnu.org
  2012-01-06  0:33 ` pinskia at gcc dot gnu.org
@ 2020-12-25 12:43 ` vanyacpp at gmail dot com
  2022-11-27  6:32 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: vanyacpp at gmail dot com @ 2020-12-25 12:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47579

Ivan Sorokin <vanyacpp at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |vanyacpp at gmail dot com

--- Comment #3 from Ivan Sorokin <vanyacpp at gmail dot com> ---
Since 7.1 GCC doesn't produce any shifts on the test code as well as on the
examples from comment #2. https://godbolt.org/z/f48EqP

I think the bug can be closed now.

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

* [Bug tree-optimization/47579] STL size() == 0 does unnecessary shift
  2011-02-01 19:52 [Bug tree-optimization/47579] New: STL size() == 0 does unnecessary shift ian at airs dot com
                   ` (2 preceding siblings ...)
  2020-12-25 12:43 ` vanyacpp at gmail dot com
@ 2022-11-27  6:32 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-27  6:32 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47579

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
           Severity|normal                      |enhancement
      Known to fail|                            |6.1.0
         Resolution|---                         |FIXED
   Target Milestone|---                         |7.0
      Known to work|                            |7.1.0

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed for GCC 7 by r7-4718-g40fd269ab128d1 .

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

end of thread, other threads:[~2022-11-27  6:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-01 19:52 [Bug tree-optimization/47579] New: STL size() == 0 does unnecessary shift ian at airs dot com
2011-02-01 22:54 ` [Bug tree-optimization/47579] " rguenth at gcc dot gnu.org
2012-01-06  0:33 ` pinskia at gcc dot gnu.org
2020-12-25 12:43 ` vanyacpp at gmail dot com
2022-11-27  6:32 ` pinskia at gcc dot gnu.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).