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

            Bug ID: 126724
           Summary: The check for SIDE EFFECTS on match patterns should be
                    using expr_no_side_effects_p  instead
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: internal-improvement, missed-optimization, wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

So the patterns like:
(simplify
 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
 (if (INTEGRAL_TYPE_P (type)
      && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
  (cond (convert:boolean_type_node @2) @1 @0)))

Checks TREE_SIDE_EFFECTS but those should be expr_no_side_effects_p instead.

The reason is TREE_SIDE_EFFECTS does not handle trapping instructions.
So in this case we start with an unconditional trapping instruction and make it
conditional.



Note there are 3 uses of TREE_SIDE_EFFECTS which is not needed at all:
```
/* (X ^ -(X < 0)) + (X < 0) -> abs (X) */
(simplify
 (plus:c (bit_xor:c @0 (negate (convert@1 (lt @0 integer_zerop)))) @1)
 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
      && !TYPE_UNSIGNED (TREE_TYPE (@0))
      && !TYPE_SATURATING (TREE_TYPE (@0))
      && (GIMPLE || !TREE_SIDE_EFFECTS (@0)))
  (abs @0)))
...
/* popcount(rotate(X Y)) is popcount(X).  */
(for popcount (POPCOUNT)
  (for rot (lrotate rrotate)
    (simplify
      (popcount (convert?@0 (rot:s@1 @2 @3)))
      (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
           && INTEGRAL_TYPE_P (TREE_TYPE (@1))
           && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
        (with { tree type0 = TREE_TYPE (@0);
                tree type1 = TREE_TYPE (@1);
                unsigned int prec0 = TYPE_PRECISION (type0);
                unsigned int prec1 = TYPE_PRECISION (type1); }
          (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
            (popcount (convert:type0 @2))))))))

/* parity(rotate(X Y)) is parity(X).  */
(for parity (PARITY)
  (for rot (lrotate rrotate)
    (simplify
      (parity (convert?@0 (rot:s@1 @2 @3)))
      (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
           && INTEGRAL_TYPE_P (TREE_TYPE (@1))
           && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
           && TYPE_PRECISION (TREE_TYPE (@0))
              >= TYPE_PRECISION (TREE_TYPE (@1)))
        (with { tree type0 = TREE_TYPE (@0); }
          (parity (convert:type0 @2)))))))
```

The last 2 seems like they had them because of the removal of @3 but genmatch
has code already to handle that case correctly.So they can be safely removed.

Reply via email to