This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug tree-optimization/71289] New: Fails to optimize unsigned mul overflow check 'A > -1 / B'


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

            Bug ID: 71289
           Summary: Fails to optimize unsigned mul overflow check 'A > -1
                    / B'
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: amonakov at gcc dot gnu.org
  Target Milestone: ---

If A and B are both unsigned, then 'A > -1 / B' is a nice predicate for
checking whether A*B would overflow. The compiler should be able to optimize
the otherwise-unused division to a multiplication followed by branch on
overflow.

I've tried to add the corresponding transform to match.pd, but it seems
something else needs to be wired up as well, because it doesn't trigger. What
am I missing? TIA.

diff --git a/gcc/match.pd b/gcc/match.pd
index e511e9a..83c02a8 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2582,6 +2582,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))

+/* Simplify unsigned multiplication overflow check A > -1 / B to a builtin. 
*/
+(for cmp (gt le)
+     out (ne eq)
+ (simplify
+  (cmp @0 (trunc_div:s integer_all_onesp @1))
+  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
+       && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
+   (out
+    (imagpart (IFN_MUL_OVERFLOW @0 @1))
+    { build_zero_cst (TREE_TYPE (@0)); }))))

 /* Simplification of math builtins.  These rules must all be optimizations
    as well as IL simplifications.  If there is a possibility that the new

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]