public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: Feng Xue OS <fxue@os.amperecomputing.com>
Cc: "gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
	Jeff Law <law@redhat.com>
Subject: Re: [PATCH] Remove empty loop with assumed finiteness (PR tree-optimization/89713)
Date: Tue, 21 May 2019 14:24:00 -0000	[thread overview]
Message-ID: <CAFiYyc2ZtFnFMMf=b=HO=3=CruGchVgVedOVwFiLwqe-GeseKA@mail.gmail.com> (raw)
In-Reply-To: <CAFiYyc3Fu4yBjUgMNcNaM+NtDsB1yKACCKUo4RC4UpNdVRHtbQ@mail.gmail.com>

On Tue, May 21, 2019 at 12:12 PM Richard Biener
<richard.guenther@gmail.com> wrote:
>
> On Mon, May 20, 2019 at 4:51 PM Feng Xue OS <fxue@os.amperecomputing.com> wrote:
> >
> > > I don't see how it is safe in a late pass when it is not safe in an
> >
> > > earlier one.  Optimization is imperfect - we could fail to remove
> > > an "obvious" never taken exit and still have a loop that appears to be
> > > finite according to our definition.
> >
> > Yes. it is. This is somewhat similar to strict-alias option/loop dep pragma.
> > Compiler tries to do something based on hint you tell it, but does not ensure correctness.
> >
> > > The only way
> > > to define it would be if there was, at any point, an exit from the
> > > loop (and there it _may_ be exclude EH edges) then
> > > the loop is assumed to be finite.
> >
> > No catch your point. If we treat an infinite loop as finite, it's bad because the loop might be removed.
> >
> > Suppose we have a function:
> >
> >     void foo(int bound)
> >      { for (int i = 0; i <= bound; i++); }
> >
> >  In an early CD-DCE pass, "bound" is represented as a variable, and loop has a exit, so it is assumed to finite, and is removed.
> >
> > But in a late pass, this function is inlined into another one, and "bound" has value of INT_MAX, this loop is infinite, and here we can know it should not be removed.
>
> But if "bound" is always INT_MAX but that's not visible to the
> compiler we will still remove the
> loop so I see no difference with removing it always.
>
> > This is why I suggest doing the optimization as late as possible.
>
> But this will defeat the purpose of allowing followup optimizations.
>
> IMHO the only "sensible" thing is to do
>
> Index: gcc/tree-ssa-dce.c
> ===================================================================
> --- gcc/tree-ssa-dce.c  (revision 271415)
> +++ gcc/tree-ssa-dce.c  (working copy)
> @@ -417,7 +417,7 @@ find_obviously_necessary_stmts (bool agg
>           }
>
>        FOR_EACH_LOOP (loop, 0)
> -       if (!finite_loop_p (loop))
> +       if (!loop_has_exit_edges (loop))
>           {
>             if (dump_file)
>               fprintf (dump_file, "cannot prove finiteness of loop
> %i\n", loop->num);

Bootstrapped / tested on x86_64-unknown-linux-gnu.  Fallout:

FAIL: gcc.dg/loop-unswitch-1.c scan-tree-dump unswitch ";; Unswitching loop"
FAIL: gcc.dg/predict-9.c scan-tree-dump-times profile_estimate "first
match heuristics: 2.20%" 3
FAIL: gcc.dg/predict-9.c scan-tree-dump-times profile_estimate "first
match heuristics: 5.50%" 1
FAIL: gcc.dg/uninit-28-gimple.c  (test for bogus messages, line 9)
FAIL: gcc.dg/graphite/scop-19.c scan-tree-dump-times graphite "number
of SCoPs: 0" 2
...
UNRESOLVED: gcc.dg/tree-ssa/20040211-1.c scan-tree-dump cddce2 "if "
FAIL: gcc.dg/tree-ssa/loop-10.c scan-tree-dump-times optimized "if " 3
FAIL: gcc.dg/tree-ssa/pr84648.c scan-tree-dump-times cddce1 "Found
loop 1 to be finite: upper bound found" 1
FAIL: gcc.dg/tree-ssa/split-path-6.c scan-tree-dump-times split-paths
"Duplicating join block" 3
FAIL: gcc.dg/tree-ssa/ssa-thread-12.c scan-tree-dump thread2 "FSM"
FAIL: gcc.dg/tree-ssa/ssa-thread-12.c scan-tree-dump thread3 "FSM"

I didn't look if the testcases are sensible for loop removal (or what
actually happens).

Richard.

> that also has the obvious advantage that we don't need to replace the loop
> with a trap() but have a place to forward control flow to.  The loop in the
> following testcase is then successfully removed:
>
> int main(int argc, char **argv)
> {
>   unsigned i = argc;
>   while (i+=2);
>   return 0;
> }
>
> Likewise is the loop
>
> void **q;
> int main(int argc, char **argv)
> {
>   void **p = q;
>   while (p = (void **)*p);
>   return 0;
> }
>
> (that's the pointer-chasing).  Not with -fnon-call-exceptions
> -fexceptions though.
>
> Richard.
>
> > Feng
> >

  reply	other threads:[~2019-05-21 14:24 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-17  4:17 Feng Xue OS
2019-05-17 16:47 ` Jeff Law
2019-05-17 18:50   ` Richard Biener
2019-05-18 14:00     ` Marc Glisse
2019-05-20  7:50       ` Richard Biener
2019-05-20  8:27         ` Feng Xue OS
2019-05-20  9:19           ` Richard Biener
2019-05-20  9:48             ` Feng Xue OS
2019-05-20 11:54               ` Richard Biener
2019-05-20 14:00                 ` Feng Xue OS
2019-05-20 14:04                   ` Richard Biener
2019-05-20 14:51                     ` Feng Xue OS
2019-05-21 10:12                       ` Richard Biener
2019-05-21 14:24                         ` Richard Biener [this message]
2019-05-22 13:44                           ` Michael Matz
2019-05-24 16:02                             ` [PATCH V3] " Feng Xue OS
2019-05-24  9:15                           ` [PATCH V2] " Feng Xue OS
2019-05-29 11:16                             ` Richard Biener
2019-06-04  6:49                               ` [PATCH V4] " Feng Xue OS
2019-06-04  8:24                                 ` Marc Glisse
2019-06-04 15:16                                   ` [PATCH V5] " Feng Xue OS
2019-06-04 15:24                                     ` [PATCH V6] " Feng Xue OS
2019-06-05 11:05                                       ` Richard Biener
2019-06-06 10:00                                         ` [PATCH V7] " Feng Xue OS
2019-06-11  2:40                                           ` [PATCH V8] " Feng Xue OS
2019-06-12  9:43                                             ` Richard Biener
2019-06-15 12:05                                               ` [committed][nvptx, libgomp] Update pr85381-{2,4}.c test-cases Tom de Vries
2019-05-20 13:04         ` [PATCH] Remove empty loop with assumed finiteness (PR tree-optimization/89713) Marc Glisse
2019-05-20 13:26           ` Richard Biener
2019-05-20 14:49             ` Michael Matz
2019-05-21  8:06               ` Marc Glisse
2020-04-01 13:36 ` [PATCH][RFC] c/94392 - only enable -ffinite-loops for C++ Richard Biener
2020-04-01 13:47   ` Jakub Jelinek
2020-04-01 13:52     ` Richard Biener
2020-04-01 15:56       ` Jan Hubicka
2020-04-01 16:59         ` Richard Biener
2020-04-01 19:15   ` Jason Merrill
2020-04-02  9:12     ` Richard Biener
2020-04-02  9:17       ` Jakub Jelinek
2020-04-02  9:41         ` Richard Biener
2020-04-03  8:29       ` Revert "[nvptx, libgomp] Update pr85381-{2, 4}.c test-cases" [PR89713, PR94392] (was: [PATCH][RFC] c/94392 - only enable -ffinite-loops for C++) Thomas Schwinge
2020-04-03  9:36         ` Revert "[nvptx, libgomp] Update pr85381-{2,4}.c " Richard Biener
2020-04-03 10:34           ` Jakub Jelinek
2020-10-30 14:09           ` Revert "[nvptx, libgomp] Update pr85381-{2, 4}.c " Thomas Schwinge
2020-10-30 14:16             ` Revert "[nvptx, libgomp] Update pr85381-{2,4}.c " Jakub Jelinek

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='CAFiYyc2ZtFnFMMf=b=HO=3=CruGchVgVedOVwFiLwqe-GeseKA@mail.gmail.com' \
    --to=richard.guenther@gmail.com \
    --cc=fxue@os.amperecomputing.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=law@redhat.com \
    /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).