public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/66261] New: operations that simplify whether the pointers are the same or not (++p;++q;--p;--q)
@ 2015-05-23  7:22 glisse at gcc dot gnu.org
  2015-05-26 10:56 ` [Bug tree-optimization/66261] " rguenth at gcc dot gnu.org
  2023-12-08  7:46 ` pcordes at gmail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: glisse at gcc dot gnu.org @ 2015-05-23  7:22 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 66261
           Summary: operations that simplify whether the pointers are the
                    same or not (++p;++q;--p;--q)
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: glisse at gcc dot gnu.org
  Target Milestone: ---

In the example below, if p and q refer to different objects, the function does
nothing, and if they refer to the same object, it also does nothing. But we
don't manage to optimize it, and I don't know how we could do it (without being
incredibly specific). It might actually be slightly easier at the RTL level for
targets where adding a constant to an integer in memory is a single insn, so it
could be detected that ++q and --p commute. clang++, icpc and sunCC don't
simplify it either.

void f(int&p,int&q){
  ++p;++q;--p;--q;
}


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

* [Bug tree-optimization/66261] operations that simplify whether the pointers are the same or not (++p;++q;--p;--q)
  2015-05-23  7:22 [Bug tree-optimization/66261] New: operations that simplify whether the pointers are the same or not (++p;++q;--p;--q) glisse at gcc dot gnu.org
@ 2015-05-26 10:56 ` rguenth at gcc dot gnu.org
  2023-12-08  7:46 ` pcordes at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2015-05-26 10:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Interesting ;)


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

* [Bug tree-optimization/66261] operations that simplify whether the pointers are the same or not (++p;++q;--p;--q)
  2015-05-23  7:22 [Bug tree-optimization/66261] New: operations that simplify whether the pointers are the same or not (++p;++q;--p;--q) glisse at gcc dot gnu.org
  2015-05-26 10:56 ` [Bug tree-optimization/66261] " rguenth at gcc dot gnu.org
@ 2023-12-08  7:46 ` pcordes at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: pcordes at gmail dot com @ 2023-12-08  7:46 UTC (permalink / raw)
  To: gcc-bugs

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

Peter Cordes <pcordes at gmail dot com> changed:

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

--- Comment #3 from Peter Cordes <pcordes at gmail dot com> ---
Re: does this show up in real code: not exactly this, but incrementing two
array elements that might or might not be the same location defeats optimizing
an  x++  loop into x += n

From
https://stackoverflow.com/questions/77618348/why-does-gcc-fail-to-reduce-a-loop-that-increments-two-locations-of-the-same-buf

unsigned int getid();
void foo(unsigned int *counter, unsigned int n) {
        unsigned int A = getid();
        unsigned int B = getid();
        for (unsigned int i = 0; i < n; i++) {
            ++counter[A];
            ++counter[B];
        }
}



If the two locations are related so GCC knows they're not the same, 
such as arr[A%4 + 0] and arr[A%4 + 16], it can also optimize.
https://godbolt.org/z/PbeaW8nnE

But it doesn't help to do something like  if (A==B) return;
before the loop so it can't be entered with A==B

Source-level loop fission is required to get good asm (two increments to memory
locations that might be the same or not).

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

end of thread, other threads:[~2023-12-08  7:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-23  7:22 [Bug tree-optimization/66261] New: operations that simplify whether the pointers are the same or not (++p;++q;--p;--q) glisse at gcc dot gnu.org
2015-05-26 10:56 ` [Bug tree-optimization/66261] " rguenth at gcc dot gnu.org
2023-12-08  7:46 ` pcordes at gmail dot com

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).