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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |marxin at gcc dot gnu.org

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
The two match.pd rules that trigger here are:
/* Transform (@0 < @1 and @0 < @2) to use min,
   (@0 > @1 and @0 > @2) to use max */
(for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
     op    (lt      le      gt      ge      lt      le      gt      ge     )
     ext   (min     min     max     max     max     max     min     min    )
 (simplify
  (logic (op:cs @0 @1) (op:cs @0 @2))
  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
       && TREE_CODE (@0) != INTEGER_CST)
   (op @0 (ext @1 @2)))))

and

/* max (a, a + CST) -> a + CST where CST is positive.  */
/* max (a, a + CST) -> a where CST is negative.  */
(simplify
 (max:c @0 (plus@2 @0 INTEGER_CST@1))
  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
   (if (tree_int_cst_sgn (@1) > 0)
    @2
    @0)))

Those look fine.  What doesn't look fine is that maybe_fold_and_comparisons
and maybe_fold_or_comparisons when calling maybe_fold_comparisons_from_match_pd
pretend they are BIT_AND_EXPR or BIT_IOR_EXPR when they actually are
TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR (unless we can prove both comparisons are
in the same bb, but we don't really tell).
Perhaps the cases that were only handled before Martin's patch,
i.e. when gimple_simplified_result_is_gimple_val, was ok because then we don't
really use a stmt that could trigger UB.
Or perhaps ifcombine should check what maybe_fold_and_comparisons has returned
and if it is a comparison where one of the operands is a SSA_NAMEs defining
stmt  doesn't dominate both original comparisons and that stmt or anything it
uses could trigger UB, punt?  Though I'd be afraid we could regress many cases
that old gimple-fold.c stuff got right (if it did).

Reply via email to