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

            Bug ID: 123109
           Summary: `a >> (bits-1) != 0` is not optimized to `a < 0` by
                    match
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: internal-improvement
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
            Blocks: 120206
  Target Milestone: ---

This was kinda of noticed on accident via PR 123107.
But currently fold simplifies `(signed >> (sizeof(signed)*8-1)) != 0` into
`signed < 0` but match does not do it.

```
      /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
         of X.  Similarly fold (X >> C) == 0 into X >= 0.  */
      if (TREE_CODE (arg0) == RSHIFT_EXPR
          && integer_zerop (arg1)
          && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
        {
          tree arg00 = TREE_OPERAND (arg0, 0);
          tree arg01 = TREE_OPERAND (arg0, 1);
          tree itype = TREE_TYPE (arg00);
          if (wi::to_wide (arg01) == element_precision (itype) - 1)
            {
              if (TYPE_UNSIGNED (itype))
                {
                  itype = signed_type_for (itype);
                  arg00 = fold_convert_loc (loc, itype, arg00);
                }
              return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
                                  type, arg00, build_zero_cst (itype));
            }
        }
```

Note for match, we need to expand_vec_cmp_expr_p (I think that is the correct
thing to check).


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120206
[Bug 120206] Removal of
forward_propagate_into_gimple_cond/forward_propagate_into_comparison

Reply via email to