Bug 34130 - [4.1/4.2 Regression] the builtin abs() gives wrong result when used in some expression
[4.1/4.2 Regression] the builtin abs() gives wrong result when used in some e...
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: middle-end
4.2.1
: P3 normal
: 4.2.3
Assigned To: Not yet assigned to anyone
: wrong-code
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2007-11-17 01:21 UTC by Pengcheng Zou
Modified: 2007-12-16 18:08 UTC (History)
4 users (show)

See Also:
Host: i686-pc-linux
Target:
Build:
Known to work: 2.95.4 4.3.0
Known to fail: 3.3.6 3.4.6 4.1.3 4.2.2
Last reconfirmed: 2007-11-17 12:46:22


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Pengcheng Zou 2007-11-17 01:21:21 UTC
tried the following code on both gcc 4.1 and gcc  3.2,:
----------------------------------------------------
        int result, i;

        i = 0;
        result = -2*abs(i-2);
        printf("result = %d\n", result);
----------------------------------------------------

the result is 4, while the correct result should be '-4;'. 

if use '-fno-builtin' flag to use the glibc's abs() implementation, then the result is correct (-4). also, if the expression is changed something like '0-2*abs(i-2)', then the result is also correct.

so, it seems there is some problem with the builtin abs() function. simply look at the assembly result, it seems gcc thinks A*abs(B) is the same as abs(A*B).

'gcc -v' result

Using built-in specs.
Target: i686-pc-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --disable-libmudflap --enable-languages=c,c++,java,fortran --disable-libgcj --with-cpu=generic --host=i686-pc-linux
Thread model: posix
gcc version 4.2.1
Comment 1 Richard Biener 2007-11-17 12:46:11 UTC
Confirmed.

int foo(int i)
{
  return -2*__builtin_abs(i-2);
}

get's folded to

 return ABS_EXPR <(2 - i) * 2>;

doh.
Comment 2 Richard Biener 2007-11-17 12:46:22 UTC
Mine.
Comment 3 Richard Biener 2007-11-17 14:22:53 UTC
Subject: Bug 34130

Author: rguenth
Date: Sat Nov 17 14:22:42 2007
New Revision: 130258

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130258
Log:
2007-11-17  Richard Guenther  <rguenther@suse.de>

	PR middle-end/34130
	* fold-const.c (extract_muldiv_1): Do not move negative
	constants inside ABS_EXPR.

	* gcc.c-torture/execute/pr34130.c: New testcase.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr34130.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/fold-const.c
    trunk/gcc/testsuite/ChangeLog

Comment 4 Richard Biener 2007-11-17 14:23:12 UTC
Fixed on the trunk.
Comment 5 Matthias Klose 2007-11-22 15:34:18 UTC
Subject: Bug 34130

Author: doko
Date: Thu Nov 22 15:34:03 2007
New Revision: 130352

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130352
Log:
2007-11-22  Matthias Klose  <doko@ubuntu.com>

        Backport from mainline:
        2007-11-17  Richard Guenther  <rguenther@suse.de>

        PR middle-end/34130
        * fold-const.c (extract_muldiv_1): Do not move negative
        constants inside ABS_EXPR.

        PR middle-end/34130
        * gcc.c-torture/execute/pr34130.c: New testcase.

Added:
    branches/gcc-4_2-branch/gcc/testsuite/gcc.c-torture/execute/pr34130.c
      - copied unchanged from r130258, trunk/gcc/testsuite/gcc.c-torture/execute/pr34130.c
Modified:
    branches/gcc-4_2-branch/gcc/ChangeLog
    branches/gcc-4_2-branch/gcc/fold-const.c
    branches/gcc-4_2-branch/gcc/testsuite/ChangeLog

Comment 6 Matthias Klose 2007-11-29 00:33:13 UTC
Subject: Bug 34130

Author: doko
Date: Thu Nov 29 00:32:58 2007
New Revision: 130507

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130507
Log:
gcc/
2007-11-29  Matthias Klose  <doko@ubuntu.com>

       Backport from mainline:
       2007-11-17  Richard Guenther  <rguenther@suse.de>

       PR middle-end/34130
       * fold-const.c (extract_muldiv_1): Do not move negative
       constants inside ABS_EXPR.

gcc/testsuite/

2007-11-29  Matthias Klose  <doko@ubuntu.com>

       Backport from mainline:
       2007-11-17  Richard Guenther  <rguenther@suse.de>

       PR middle-end/34130
       * gcc.c-torture/execute/pr34130.c: New testcase.

Added:
    branches/gcc-4_1-branch/gcc/testsuite/gcc.c-torture/execute/pr34130.c
      - copied unchanged from r130352, branches/gcc-4_2-branch/gcc/testsuite/gcc.c-torture/execute/pr34130.c
Modified:
    branches/gcc-4_1-branch/gcc/ChangeLog
    branches/gcc-4_1-branch/gcc/fold-const.c
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog

Comment 7 Serge Belyshev 2007-12-16 18:08:51 UTC
Fixed on active branches.