The following fixes the oldest bug in my list of assigned PRs, namely
combining A / CST / CST' to A / (CST * CST').  extract_muldiv does
this in fold-const.c (but it fails to optimize to zero when CST * CST'
overflows).

Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk.

Richard.

2014-12-01  Richard Biener  <rguent...@suse.de>

        PR tree-optimization/15346
        * Makefile.in (gimple-match.o-warn): Remove -Wno-unused-parameter,
        add -Wno-unused-but-set-variable.
        * match.pd: Combine two successive divisions.

        * gcc.dg/tree-ssa/forwprop-32.c: New testcase.

Index: gcc/match.pd
===================================================================
--- gcc/match.pd        (revision 218140)
+++ gcc/match.pd        (working copy)
@@ -129,6 +129,19 @@ (define_operator_list inverted_tcc_compa
       && TYPE_UNSIGNED (type))
   (trunc_div @0 @1)))
 
+/* Combine two successive divisions.  */
+(for div (trunc_div ceil_div floor_div round_div exact_div)
+ (simplify
+  (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
+  (with {
+    bool overflow_p;
+    wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
+   }
+   (if (!overflow_p)
+    (div @0 { wide_int_to_tree (type, mul); }))
+   (if (overflow_p)
+    { build_zero_cst (type); }))))
+
 /* Optimize A / A to 1.0 if we don't care about
    NaNs or Infinities.  */
 (simplify
Index: gcc/testsuite/gcc.dg/tree-ssa/forwprop-32.c
===================================================================
--- gcc/testsuite/gcc.dg/tree-ssa/forwprop-32.c (revision 0)
+++ gcc/testsuite/gcc.dg/tree-ssa/forwprop-32.c (working copy)
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-forwprop1 -fdump-tree-ccp1" } */
+
+int foo (int x)
+{
+  int tem = x / 3;
+  return tem / 5;
+}
+int bar (int x)
+{
+  int tem = x / 3;
+  return tem / (__INT_MAX__ / 2);
+}
+
+/* { dg-final { scan-tree-dump "x_.\\(D\\) / 15" "forwprop1" } } */
+/* { dg-final { scan-tree-dump "return 0;" "ccp1" } } */
+/* { dg-final { cleanup-tree-dump "forwprop1" } } */
+/* { dg-final { cleanup-tree-dump "ccp1" } } */
Index: gcc/Makefile.in
===================================================================
--- gcc/Makefile.in     (revision 218142)
+++ gcc/Makefile.in     (working copy)
@@ -209,8 +209,8 @@ gengtype-lex.o-warn = -Wno-error
 libgcov-util.o-warn = -Wno-error
 libgcov-driver-tool.o-warn = -Wno-error
 libgcov-merge-tool.o-warn = -Wno-error
-gimple-match.o-warn = -Wno-unused-variable -Wno-unused-parameter
-generic-match.o-warn = -Wno-unused-variable -Wno-unused-parameter
+gimple-match.o-warn = -Wno-unused-variable -Wno-unused-but-set-variable
+generic-match.o-warn = -Wno-unused-variable -Wno-unused-but-set-variable
 
 # All warnings have to be shut off in stage1 if the compiler used then
 # isn't gcc; configure determines that.  WARN_CFLAGS will be either

Reply via email to