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

            Bug ID: 128748
           Summary: wrong code at -O1 -fwrapv: fold_binary rewrites
                    ABS_EXPR <x> <= 0 to x == 0 although abs (INT_MIN)
                    wraps
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jeffchen006 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

#include <cstdio>
int x = -2147483647 - 1;
int main() {
  int a = x < 0 ? -x : x;
  printf("%d\n", a <= 0);
}

$ g++ -O0 -fwrapv t.cpp && ./a.out
1
$ g++ -O1 -fwrapv t.cpp && ./a.out
0

-O1 to -O3 fail; -fno-strict-overflow (Linux kernel flag) gives the same; -O1
-fwrapv -fno-tree-forwprop gives 1.

Expected 1: with -fwrapv, -x wraps to INT_MIN, so a == INT_MIN and a <= 0.

ABS_EXPR <x> already exists before ccp1 in this test. forwprop1 then does (via
combine_cond_expr_cond and fold_binary_loc, fold-const.cc):

  039t.forwprop1: Replaced 'a_5 <= 0' with 'x.0_1 == 0'

-fno-tree-forwprop gives 1; -fdisable-tree-forwprop1 alone does not (a later
forwprop does the same fold). The fold:

      /* If we are comparing an ABS_EXPR with a constant, ...
         But ABS (X) <= C is a range comparison, ...  */
      if (code == LE_EXPR
          && TREE_CODE (arg1) == INTEGER_CST
          && TREE_CODE (arg0) == ABS_EXPR
          && ! TREE_SIDE_EFFECTS (arg0)
          && (tem = negate_expr (arg1)) != 0
          && TREE_CODE (tem) == INTEGER_CST
          && !TREE_OVERFLOW (tem))
        return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
                                build2 (GE_EXPR, type, TREE_OPERAND (arg0, 0),
tem),
                                build2 (LE_EXPR, type, TREE_OPERAND (arg0, 0),
arg1));

There is no TYPE_OVERFLOW_UNDEFINED check. With wrapping overflow ABS_EXPR
<INT_MIN> is INT_MIN, which satisfies <= C, but -C <= X <= C does not hold for
X = INT_MIN. The match.pd rule for ABS_EXPR <X> > C already requires `op ==
ABSU_EXPR || TYPE_OVERFLOW_UNDEFINED (...)`. Fails on all releases tested (7.5
to trunk); the rewrite is from r30299 (1999), older than -fwrapv.

-O0 -fsanitize=undefined,address -fwrapv is clean; Clang -fwrapv gives 1.

Cause: fold_binary_loc (fold-const.cc) rewrites ABS (X) <= C as X >= -C && X <=
C without checking that signed overflow is undefined. Under -fwrapv, ABS
(INT_MIN) is INT_MIN, which is <= C, but INT_MIN is not in [-C, C], so the
rewrite changes the result. forwprop reaches this fold through
combine_cond_expr_cond.

gcc -Wall -Wextra gives no warnings. The test needs -fwrapv: the bug is that a
fold ignores it. -fno-strict-aliasing does not change the output.

Possible fix: One possible fix, which we tested locally (not bootstrapped), is
to require TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)) before the rewrite in
fold_binary_loc, as the match.pd rule for ABS_EXPR <X> > C already does. It
fixes the C and C++ tests at -O1 to -O3 and with -fno-strict-overflow;
pr114090.c still passes.

gcc version 17.0.0 20261114 (experimental), r17-6275-gfceddda853f,
x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release

Reply via email to