PR tree-optimization/89713 - Assume loop with an exit is finite
authorFeng Xue <fxue@os.amperecomputing.com>
Thu, 13 Jun 2019 04:17:42 +0000 (04:17 +0000)
committerFeng Xue <fxue@gcc.gnu.org>
Thu, 13 Jun 2019 04:17:42 +0000 (04:17 +0000)
gcc/ChangeLog:

        * doc/invoke.texi (-ffinite-loops): Document new option.
        * common.opt (-ffinite-loops): New option.
        * tree-ssa-dce.c (mark_stmt_if_obviously_necessary): Mark
        IFN_GOACC_LOOP calls as necessary.
        * tree-ssa-loop-niter.c (finite_loop_p): Assume loop with an exit
        is finite.
        * omp-offload.c (oacc_xform_loop): Skip lowering if return value of
        IFN_GOACC_LOOP call is not used.
        * opts.c (default_options_table): Enable -ffinite-loops at -O2+.

gcc/testsuite/ChangeLog:

        * g++.dg/tree-ssa/empty-loop.C: New test.
        * gcc.dg/tree-ssa/dce-2.c: New test.
        * gcc.dg/const-1.c: Add -fno-finite-loops option.
        * gcc.dg/graphite/graphite.exp: Likewise.
        * gcc.dg/loop-unswitch-1.c: Likewise.
        * gcc.dg/predict-9.c: Likewise.
        * gcc.dg/pure-2.c: Likewise.
        * gcc.dg/tree-ssa/20040211-1.c: Likewise.
        * gcc.dg/tree-ssa/loop-10.c: Likewise.
        * gcc.dg/tree-ssa/split-path-6.c: Likewise.
        * gcc.dg/tree-ssa/ssa-thread-12.c: Likewise.

libgomp/ChangeLog:

        * testsuite/libgomp.oacc-c-c++-common/pr84955-1.c: New test.

From-SVN: r272234

21 files changed:
gcc/ChangeLog
gcc/common.opt
gcc/doc/invoke.texi
gcc/omp-offload.c
gcc/opts.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/tree-ssa/empty-loop.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/const-1.c
gcc/testsuite/gcc.dg/graphite/graphite.exp
gcc/testsuite/gcc.dg/loop-unswitch-1.c
gcc/testsuite/gcc.dg/predict-9.c
gcc/testsuite/gcc.dg/pure-2.c
gcc/testsuite/gcc.dg/tree-ssa/20040211-1.c
gcc/testsuite/gcc.dg/tree-ssa/dce-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/loop-10.c
gcc/testsuite/gcc.dg/tree-ssa/split-path-6.c
gcc/testsuite/gcc.dg/tree-ssa/ssa-thread-12.c
gcc/tree-ssa-dce.c
gcc/tree-ssa-loop-niter.c
libgomp/ChangeLog
libgomp/testsuite/libgomp.oacc-c-c++-common/pr84955-1.c [new file with mode: 0644]

index 079379e45dc3dab842ce98a5fa8623579611c9a4..efb3b7361f281840da5edca80225dc13767f63a7 100644 (file)
@@ -1,3 +1,16 @@
+2019-06-13  Feng Xue  <fxue@os.amperecomputing.com>
+
+       PR tree-optimization/89713
+       * doc/invoke.texi (-ffinite-loops): Document new option.
+       * common.opt (-ffinite-loops): New option.
+       * tree-ssa-dce.c (mark_stmt_if_obviously_necessary): Mark
+       IFN_GOACC_LOOP calls as necessary.
+       * tree-ssa-loop-niter.c (finite_loop_p): Assume loop with an exit
+       is finite.
+       * omp-offload.c (oacc_xform_loop): Skip lowering if return value of
+       IFN_GOACC_LOOP call is not used.
+       * opts.c (default_options_table): Enable -ffinite-loops at -O2+.
+
 2019-06-13  Kugan Vivekanandarajah  <kugan.vivekanandarajah@linaro.org>
 
        PR target/88838
index e1404165febc936b37b31333f9a890f6799454a9..a1544d06824eebf41a76e014c98b949933f18752 100644 (file)
@@ -1437,6 +1437,10 @@ ffinite-math-only
 Common Report Var(flag_finite_math_only) Optimization SetByCombined
 Assume no NaNs or infinities are generated.
 
+ffinite-loops
+Common Report Var(flag_finite_loops) Optimization
+Assume that loops with an exit will terminate and not loop indefinitely.
+
 ffixed-
 Common Joined RejectNegative Var(common_deferred_options) Defer
 -ffixed-<register>     Mark <register> as being unavailable to the compiler.
index 090d606b3ba68cc8ad86e2c68dcb647f1b0767c7..bf9da0f0a6eb9eaebeb0cb9acefcd9784be23dd0 100644 (file)
@@ -413,6 +413,7 @@ Objective-C and Objective-C++ Dialects}.
 -fdevirtualize-at-ltrans  -fdse @gol
 -fearly-inlining  -fipa-sra  -fexpensive-optimizations  -ffat-lto-objects @gol
 -ffast-math  -ffinite-math-only  -ffloat-store  -fexcess-precision=@var{style} @gol
+-ffinite-loops @gol
 -fforward-propagate  -ffp-contract=@var{style}  -ffunction-sections @gol
 -fgcse  -fgcse-after-reload  -fgcse-las  -fgcse-lm  -fgraphite-identity @gol
 -fgcse-sm  -fhoist-adjacent-loads  -fif-conversion @gol
@@ -8303,6 +8304,7 @@ also turns on the following optimization flags:
 -fdelete-null-pointer-checks @gol
 -fdevirtualize  -fdevirtualize-speculatively @gol
 -fexpensive-optimizations @gol
+-ffinite-loops @gol
 -fgcse  -fgcse-lm  @gol
 -fhoist-adjacent-loads @gol
 -finline-small-functions @gol
@@ -9524,6 +9526,15 @@ that may set @code{errno} but are otherwise free of side effects.  This flag is
 enabled by default at @option{-O2} and higher if @option{-Os} is not also
 specified.
 
+@item -ffinite-loops
+@opindex ffinite-loops
+@opindex fno-finite-loops
+Assume that a loop with an exit will eventually take the exit and not loop
+indefinitely.  This allows the compiler to remove loops that otherwise have
+no side-effects, not considering eventual endless looping as such.
+
+This option is enabled by default at @option{-O2}.
+
 @item -ftree-dominator-opts
 @opindex ftree-dominator-opts
 Perform a variety of simple scalar cleanups (constant/copy
index 97ae47b313589fd89ac2ece1e66c3c5de208a3d5..c8a281c6d28c9aac3e0ad33554d2c02877d7495a 100644 (file)
@@ -300,7 +300,7 @@ oacc_xform_loop (gcall *call)
   tree chunk_size = NULL_TREE;
   unsigned mask = (unsigned) TREE_INT_CST_LOW (gimple_call_arg (call, 5));
   tree lhs = gimple_call_lhs (call);
-  tree type = TREE_TYPE (lhs);
+  tree type = NULL_TREE;
   tree diff_type = TREE_TYPE (range);
   tree r = NULL_TREE;
   gimple_seq seq = NULL;
@@ -308,6 +308,15 @@ oacc_xform_loop (gcall *call)
   unsigned outer_mask = mask & (~mask + 1); // Outermost partitioning
   unsigned inner_mask = mask & ~outer_mask; // Inner partitioning (if any)
 
+  /* Skip lowering if return value of IFN_GOACC_LOOP call is not used.  */
+  if (!lhs)
+    {
+      gsi_replace_with_seq (&gsi, seq, true);
+      return;
+    }
+
+  type = TREE_TYPE (lhs);
+
 #ifdef ACCEL_COMPILER
   chunk_size = gimple_call_arg (call, 4);
   if (integer_minus_onep (chunk_size)  /* Force static allocation.  */
index 64f94ac8ffd4da68eeaa7778a7be9b529914e608..b38bfb15a56b82cd63d2b378582fb527ed4df7bd 100644 (file)
@@ -494,6 +494,7 @@ static const struct default_options default_options_table[] =
     { OPT_LEVELS_2_PLUS, OPT_fdevirtualize, NULL, 1 },
     { OPT_LEVELS_2_PLUS, OPT_fdevirtualize_speculatively, NULL, 1 },
     { OPT_LEVELS_2_PLUS, OPT_fexpensive_optimizations, NULL, 1 },
+    { OPT_LEVELS_2_PLUS, OPT_ffinite_loops, NULL, 1 },
     { OPT_LEVELS_2_PLUS, OPT_fgcse, NULL, 1 },
     { OPT_LEVELS_2_PLUS, OPT_fhoist_adjacent_loads, NULL, 1 },
     { OPT_LEVELS_2_PLUS, OPT_findirect_inlining, NULL, 1 },
index 7212c6be52577e3f916e5e9f0d8f3634d61798c3..f82dc9e09edc8911b91039a6a8cf255faa8141a2 100644 (file)
@@ -1,3 +1,18 @@
+2019-06-13  Feng Xue  <fxue@os.amperecomputing.com>
+
+       PR tree-optimization/89713
+       * g++.dg/tree-ssa/empty-loop.C: New test.
+       * gcc.dg/tree-ssa/dce-2.c: New test.
+       * gcc.dg/const-1.c: Add -fno-finite-loops option.
+       * gcc.dg/graphite/graphite.exp: Likewise.
+       * gcc.dg/loop-unswitch-1.c: Likewise.
+       * gcc.dg/predict-9.c: Likewise.
+       * gcc.dg/pure-2.c: Likewise.
+       * gcc.dg/tree-ssa/20040211-1.c: Likewise.
+       * gcc.dg/tree-ssa/loop-10.c: Likewise.
+       * gcc.dg/tree-ssa/split-path-6.c: Likewise.
+       * gcc.dg/tree-ssa/ssa-thread-12.c: Likewise.
+
 2019-06-13  Kugan Vivekanandarajah  <kugan.vivekanandarajah@linaro.org>
 
        PR target/88838
diff --git a/gcc/testsuite/g++.dg/tree-ssa/empty-loop.C b/gcc/testsuite/g++.dg/tree-ssa/empty-loop.C
new file mode 100644 (file)
index 0000000..6b1e879
--- /dev/null
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-cddce2 -ffinite-loops" } */
+
+#include <string>
+#include <vector>
+#include <list>
+#include <set>
+#include <map>
+
+using namespace std;
+
+int foo (vector<string> &v, list<string> &l, set<string> &s, map<int, string> &m)
+{
+  for (vector<string>::iterator it = v.begin (); it != v.end (); ++it)
+    it->length();
+
+  for (list<string>::iterator it = l.begin (); it != l.end (); ++it)
+    it->length();
+
+  for (map<int, string>::iterator it = m.begin (); it != m.end (); ++it)
+    it->first + it->second.length();
+
+  for (set<string>::iterator it0 = s.begin (); it0 != s.end(); ++it0)
+    for (vector<string>::reverse_iterator it1 = v.rbegin(); it1 != v.rend(); ++it1)
+      {
+        it0->length();
+        it1->length();
+      }  
+
+  return 0;
+}
+/* { dg-final { scan-tree-dump-not "if" "cddce2"} } */
+
index a5b2b1677283bdddf0c44b167f3c5469e9e653f7..2e95bd8e2eac640bba0c424e38cbc730215f15d8 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile { target nonpic } } */
-/* { dg-options "-O2 -Wsuggest-attribute=const" } */
+/* { dg-options "-O2 -Wsuggest-attribute=const -fno-finite-loops" } */
 
 extern int extern_const(int a) __attribute__ ((const));
 
index ea6144607e2e1c44e4ffc98f6aae4442c3281771..523a955e82d779d95da43009e53f5717afa9988d 100644 (file)
@@ -56,7 +56,7 @@ set vect_files        [lsort [glob -nocomplain $srcdir/$subdir/vect-*.c ] ]
 
 # Tests to be compiled.
 set dg-do-what-default compile
-dg-runtest $scop_files        "" "-O2 -fgraphite -fdump-tree-graphite-all"
+dg-runtest $scop_files        "" "-O2 -fgraphite -fdump-tree-graphite-all -fno-finite-loops"
 dg-runtest $id_files          "" "-O2 -fgraphite-identity -ffast-math -fdump-tree-graphite-details"
 
 # Tests to be run.
index f6fc41d6bcccb6d4c12969eb861f7e255ab8ab1a..de2fb2c0e4bcf9aca5a184d8bd04a383d1097079 100644 (file)
@@ -1,6 +1,6 @@
 /* For PR rtl-optimization/27735  */
 /* { dg-do compile } */
-/* { dg-options "-O2 -funswitch-loops -fdump-tree-unswitch-details" } */
+/* { dg-options "-O2 -funswitch-loops -fdump-tree-unswitch-details -fno-finite-loops" } */
 
 void set_color(void);
 void xml_colorize_line(unsigned int *p, int state)
index 7e5ba085ecea2c5014c1a5977ac269e31a8b71b8..f491c511bd9d6ee558fdc581ec9b517e6596d41e 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -fdisable-tree-evrp -fdump-tree-profile_estimate" } */
+/* { dg-options "-O2 -fdisable-tree-evrp -fdump-tree-profile_estimate -fno-finite-loops" } */
 
 extern int global;
 extern int global2;
index fe6e2bce6954900a78774567af79c39c59f9289a..318cfd186302962253a1457359b775e6be233e83 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -Wsuggest-attribute=pure" } */
+/* { dg-options "-O2 -Wsuggest-attribute=pure -fno-finite-loops" } */
 /* { dg-add-options bind_pic_locally } */
 
 extern int extern_const(int a) __attribute__ ((pure));
index d289e5d0f55e1decdb1cf98b5d0cb1d5f54aa53e..a9bdf26931aa758d85aee6174af37af5156eba7d 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -fdump-tree-cddce2" } */
+/* { dg-options "-O2 -fdump-tree-cddce2 -fno-finite-loops" } */
 
 struct rtx_def;
 typedef struct rtx_def *rtx;
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/dce-2.c b/gcc/testsuite/gcc.dg/tree-ssa/dce-2.c
new file mode 100644 (file)
index 0000000..18c1ddb
--- /dev/null
@@ -0,0 +1,37 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-cddce1 -ffinite-loops" } */
+
+typedef struct list {
+    char pad[15];
+    struct list *next;
+} list;
+
+int data;
+
+list *head, *tail;
+
+int __attribute__((pure)) pfn (int);
+
+int foo (unsigned u, int s)
+{
+  unsigned i;
+  list *p;
+  int j;
+
+  for (i = 0; i < u; i += 2)
+    ;
+
+  for (p = head; p; p = p->next)
+    ;
+
+  for (j = data; j & s; j = pfn (j + 3))
+    ;
+
+  for (p = head; p != tail; p = p->next)
+    for (j = data + 1; j > s; j = pfn (j + 2))
+      ;
+
+  return 0;
+}
+/* { dg-final { scan-tree-dump-not "if" "cddce1"} } */
+
index a29c9fb2501dd6b0d27fa7cebf7709df9fb8d859..3d05ad2d0737cf51be6a879e17220a1fa62244ec 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-options "-O2 -fdump-tree-optimized -fno-finite-loops" } */
 /* { dg-require-effective-target int32plus } */
 
 int bar (void);
index e9b4f2628d55112c2c417d032e39d55aec5aea35..187c08407d5583699f6bd2c43d3c9211cab25b75 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -fsplit-paths -fno-tree-cselim -fdump-tree-split-paths-details -w" } */
+/* { dg-options "-O2 -fsplit-paths -fno-tree-cselim -fdump-tree-split-paths-details -w -fno-finite-loops" } */
 
 struct __sFILE
 {
index d829b04d177da7b12a6e5d7ffeb27f1bf7fbfdd2..67526762f2c1aa98f333e1a5fbd9fc346d3e66f4 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -fdump-tree-thread2-details -fdump-tree-thread3-details -fdump-tree-thread4-details" } */
+/* { dg-options "-O2 -fdump-tree-thread2-details -fdump-tree-thread3-details -fdump-tree-thread4-details -fno-finite-loops" } */
 /* { dg-final { scan-tree-dump "FSM" "thread2" } } */
 /* { dg-final { scan-tree-dump "FSM" "thread3" } } */
 /* { dg-final { scan-tree-dump "FSM" "thread4" { xfail *-*-* } } } */
index 2478219d8737d9e95fbc7eab47a56e8b6a86aa4d..a38899edd6cfd62b18faee8f96183b1283c3b0f2 100644 (file)
@@ -245,6 +245,17 @@ mark_stmt_if_obviously_necessary (gimple *stmt, bool aggressive)
            mark_stmt_necessary (stmt, true);
            return;
          }
+       /* IFN_GOACC_LOOP calls are necessary in that they are used to
+          represent parameter (i.e. step, bound) of a lowered OpenACC
+          partitioned loop.  But this kind of partitioned loop might not
+          survive from aggressive loop removal for it has loop exit and
+          is assumed to be finite.  Therefore, we need to explicitly mark
+          these calls. (An example is libgomp.oacc-c-c++-common/pr84955.c) */
+       if (gimple_call_internal_p (stmt, IFN_GOACC_LOOP))
+         {
+           mark_stmt_necessary (stmt, true);
+           return;
+         }
        if (!gimple_call_lhs (stmt))
          return;
        break;
index 84e6e313c85d3d70e471f53e6dada8d1624b998c..f51385900ed491c62aab69e747289e92571e2a2c 100644 (file)
@@ -2830,6 +2830,27 @@ finite_loop_p (struct loop *loop)
                 loop->num);
       return true;
     }
+
+  if (flag_finite_loops)
+    {
+      unsigned i;
+      vec<edge> exits = get_loop_exit_edges (loop);
+      edge ex;
+
+      /* If the loop has a normal exit, we can assume it will terminate.  */
+      FOR_EACH_VEC_ELT (exits, i, ex)
+       if (!(ex->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_FAKE)))
+         {
+           exits.release ();
+           if (dump_file)
+             fprintf (dump_file, "Assume loop %i to be finite: it has an exit "
+                      "and -ffinite-loops is on.\n", loop->num);
+           return true;
+         }
+
+      exits.release ();
+    }
+
   return false;
 }
 
index 348788067862f9ba3f5dc4283ea4c1d3bae7ceee..35fe54851f930f9b9c24eee7e5ad8bd2ab7a3535 100644 (file)
@@ -1,3 +1,8 @@
+2019-06-13  Feng Xue  <fxue@os.amperecomputing.com>
+
+       PR tree-optimization/89713
+       * testsuite/libgomp.oacc-c-c++-common/pr84955-1.c: New test.
+
 2019-06-11  Jakub Jelinek  <jakub@redhat.com>
 
        PR target/90811
diff --git a/libgomp/testsuite/libgomp.oacc-c-c++-common/pr84955-1.c b/libgomp/testsuite/libgomp.oacc-c-c++-common/pr84955-1.c
new file mode 100644 (file)
index 0000000..44767cd
--- /dev/null
@@ -0,0 +1,31 @@
+/* { dg-do compile }  */
+/* { dg-options "-O2 -fdump-tree-cddce2 -ffinite-loops" } */
+
+int
+f1 (void)
+{
+  int i, j;
+
+#pragma acc parallel loop tile(2,3)
+  for (i = 1; i < 10; i++)
+    for (j = 1; j < 10; j++)
+      for (;;)
+       ;
+
+  return i + j;
+}
+
+int
+f2 (void)
+{
+  int i, j, k;
+
+#pragma acc parallel loop tile(2,3)
+  for (i = 1; i < 10; i++)
+    for (j = 1; j < 10; j++)
+      for (k = 1; k < 10; k++)
+       ;
+
+  return i + j;
+}
+/* { dg-final { scan-tree-dump-not "if" "cddce2"} } */