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

--- Comment #2 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Andrea Pinski <[email protected]>:

https://gcc.gnu.org/g:edb9556068ae47e1a3cbad69249424ab7cce5b7c

commit r17-4378-gedb9556068ae47e1a3cbad69249424ab7cce5b7c
Author: Andrea Pinski <[email protected]>
Date:   Fri Sep 11 17:58:42 2026 -0700

    match: `minmax (a - c, b) + c -> minmax (a, b + c)` in some cases
[PR127103]

    This adds a pattern to optimize `minmax (a - c, b) + c` into `minmax (a, b
+ c)` when
    `b + c` is known not to overflow.

    This is a simplified version from previous one where we only handle the `a
- c` case
    rather than the generic

    This shows up in std::vector::push_back :

    ```
      # RANGE [irange] const size_type [0, 2147483647] MASK 0x7fffffff VALUE
0x0
      __room_33 = 2147483647 - _32;
    ...
      # RANGE [irange] long unsigned int [1, 2147483646] MASK 0x7fffffff VALUE
0x0
      _35 = MIN_EXPR <__room_33, _34>;
      # RANGE [irange] long unsigned int [1, 4294967292]
      _36 = _32 + _35;
    ```
    _32 has a range of `[0, 2147483647]`, _34 has a range of `[1, 2147483646]`.

    This is transformed into:
    _t = _32 + _34;
    _36 = MIN_EXPR<_t, 2147483647>

    This then allows to remove a condition that was never could be executed
too.

    This is currently limited to unsigned types as overflow_free_p does not
tell
    me if we are going to introduce new undefined overflow; it will return true
for
    all TYPE_OVERFLOW_UNDEFINED types.  I filed PR 127356 to add a modified
version
    of overflow_free_p.  But since the types in push_back is unsigned this is
enough
    for now.  When that goes in; I will combine r17-4210-g9d734bb468e714 with
this
    pattern and remove the special case of 0.

    Changes since v1:
     * v2: Only handle the case where we have `a - c` as the operand to minmax.

    Bootstrapped and tested on x86_64-linux-gnu.

            PR tree-optimization/127103
    gcc/ChangeLog:

            * match.pd (`minmax (a - c, b) + c`): New pattern.

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/minmax-plus-1.c: New test.

    Signed-off-by: Andrea Pinski <[email protected]>

Reply via email to