Bug 43438 - [4.3 Regression] possible wrong code bug
Summary: [4.3 Regression] possible wrong code bug
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: 4.3.5
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2010-03-19 06:16 UTC by John Regehr
Modified: 2010-04-20 13:09 UTC (History)
4 users (show)

See Also:
Host:
Target: i?86-*-* x86_64-*-*
Build:
Known to work: 4.1.2 4.5.0 4.4.4
Known to fail: 4.3.4 4.4.3
Last reconfirmed: 2010-03-19 12:06:17


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description John Regehr 2010-03-19 06:16:05 UTC
The -O0 result looks right.  This behavior observed on x86 using r157445 and x64 using r157542.

regehr@john-home:~$ current-gcc -O0 small.c -o small -Wall 
regehr@john-home:~$ ./small
1
regehr@john-home:~$ current-gcc -O1 small.c -o small -Wall 
regehr@john-home:~$ ./small
0
regehr@john-home:~$ cat small.c
extern int printf (__const char *__restrict __format, ...);

static unsigned char g_2 = 1;
static int g_9;
static int *l_8 = &g_9;

static void func_12(int p_13)
{
  int * l_17 = &g_9;
  *l_17 &= 0 < p_13;
}

int main(void)
{
  unsigned char l_11 = 254;
  *l_8 |= g_2;
  l_11 |= *l_8;
  func_12(l_11);
  printf("%d\n", g_9);
  return 0;
} 
regehr@john-home:~$ current-gcc -v
Using built-in specs.
COLLECT_GCC=current-gcc
COLLECT_LTO_WRAPPER=/home/regehr/z/compiler-install/gcc-r157445-install/libexec/gcc/i686-pc-linux-gnu/4.5.0/lto-wrapper
Target: i686-pc-linux-gnu
Configured with: ../configure --with-libelf=/usr/local --enable-lto --prefix=/home/regehr/z/compiler-install/gcc-r157445-install --program-prefix=r157445- --enable-languages=c,c++
Thread model: posix
gcc version 4.5.0 20100314 (experimental) (GCC)
Comment 1 Ozkan Sezer 2010-03-19 06:51:25 UTC
Happened on x86_64-pc-linux and my gcc-4.4 was affected too, gcc-3.4.6 seemed fine.
Comment 2 Mikael Pettersson 2010-03-19 09:46:55 UTC
4.2.4/4.3.4/4.4.3 are affected, 4.1.2 and older seem to be Ok.
Comment 3 Richard Biener 2010-03-19 11:02:13 UTC
Confirmed.
Comment 4 Richard Biener 2010-03-19 11:29:41 UTC
Combine breaks this.
Comment 5 Richard Biener 2010-03-19 11:35:05 UTC
It combines

(insn 6 5 7 2 t.c:16 (parallel [
            (set (reg:SI 59 [ D.2732 ])
                (ior:SI (reg:SI 67 [ g_9 ])
                    (const_int 1 [0x1])))
            (clobber (reg:CC 17 flags))
        ]) 394 {*iorsi_1} (expr_list:REG_DEAD (reg:SI 67 [ g_9 ])
        (expr_list:REG_UNUSED (reg:CC 17 flags)
            (expr_list:REG_EQUAL (ior:SI (mem/c/i:SI (symbol_ref:DI ("g_9") [flags 0x2]  <var_decl 0x7ffff5ae50a0 g_9>) [0 g_9+0 S4 A32])
                    (const_int 1 [0x1]))
                (nil)))))

(insn 7 6 8 2 t.c:10 (parallel [
            (set (reg:QI 68)
                (ior:QI (subreg:QI (reg:SI 59 [ D.2732 ]) 0)
                    (const_int -2 [0xfffffffffffffffe])))
            (clobber (reg:CC 17 flags))
        ]) 398 {*iorqi_1} (expr_list:REG_UNUSED (reg:CC 17 flags)
        (nil)))

(insn 8 7 9 2 t.c:10 (parallel [
            (set (reg:SI 69)
                (zero_extend:SI (reg:QI 68)))
            (clobber (reg:CC 17 flags))
        ]) 119 {*zero_extendqisi2_movzbl_and} (expr_list:REG_DEAD (reg:QI 68)
        (expr_list:REG_UNUSED (reg:CC 17 flags)
            (nil))))

and somewhere forgets to apply the zero-extension:

Successfully matched this instruction:
(set (reg:SI 59 [ D.2732 ])
    (ior:SI (reg:SI 67 [ g_9 ])
        (const_int 1 [0x1])))
Successfully matched this instruction:
(set (reg:SI 69)
    (const_int -1 [0xffffffffffffffff]))
Comment 6 Richard Biener 2010-03-19 12:06:17 UTC
Breakpoint 7, combine_simplify_rtx (x=0x7ffff5ae6e58, op0_mode=VOIDmode, 
    in_dest=0) at /space/rguenther/src/svn/trunk/gcc/combine.c:4861
4861      enum rtx_code code = GET_CODE (x);
(set (reg:SI 69)
    (and:SI (subreg:SI (ior:QI (subreg:QI (ior:SI (reg:SI 67 [ g_9 ])
                        (const_int 1 [0x1])) 0)
                (const_int -2 [0xfffffffffffffffe])) 0)
        (const_int 255 [0xff])))
(gdb) finish
Value returned is $19 = (struct rtx_def *) 0x7ffff5ae6e58
(gdb) call debug_rtx ($19)
(set (reg:SI 69)
    (const_int -1 [0xffffffffffffffff]))

but combine_simplify_rtx didn't simplify the subexpressions without the
set.  Hm.
Comment 7 Richard Biener 2010-03-19 12:40:47 UTC
make_extraction (mode=SImode, inner=0x7ffff5b48ac8, pos=0, pos_rtx=0x0, len=8, 
    unsignedp=1, in_dest=0, in_compare=0)
    at /space/rguenther/src/svn/trunk/gcc/combine.c:6648
6648      enum machine_mode is_mode = GET_MODE (inner);
(gdb) call debug_rtx (inner)
(subreg:SI (ior:QI (subreg:QI (ior:SI (reg:SI 67 [ g_9 ])
                (const_int 1 [0x1])) 0)
        (const_int -2 [0xfffffffffffffffe])) 0)


      if (CONST_INT_P (new_rtx))
        return gen_int_mode (INTVAL (new_rtx), mode);

misses the nececssary truncation.  Dropping the special casing works
(but doesn't optimize).  Fixing the appearantly many issues works as well.
Comment 8 Richard Biener 2010-03-20 13:44:32 UTC
Subject: Bug 43438

Author: rguenth
Date: Sat Mar 20 13:44:18 2010
New Revision: 157592

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=157592
Log:
2010-03-20  Richard Guenther  <rguenther@suse.de>

	PR rtl-optimization/43438
	* combine.c (make_extraction): Properly zero-/sign-extend an
	extraction of the low part of a CONST_INT.  Also handle
	CONST_DOUBLE.

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

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr43438.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/combine.c
    trunk/gcc/testsuite/ChangeLog

Comment 9 Richard Biener 2010-03-20 13:44:46 UTC
Fixed for 4.5 sofar.
Comment 10 Jakub Jelinek 2010-03-22 15:06:01 UTC
Subject: Bug 43438

Author: jakub
Date: Mon Mar 22 15:05:46 2010
New Revision: 157634

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=157634
Log:
	Backport from mainline:
	2010-03-20  Richard Guenther  <rguenther@suse.de>

	PR rtl-optimization/43438
	* combine.c (make_extraction): Properly zero-/sign-extend an
	extraction of the low part of a CONST_INT.  Also handle
	CONST_DOUBLE.

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

Added:
    branches/gcc-4_4-branch/gcc/testsuite/gcc.c-torture/execute/pr43438.c
Modified:
    branches/gcc-4_4-branch/gcc/ChangeLog
    branches/gcc-4_4-branch/gcc/combine.c
    branches/gcc-4_4-branch/gcc/testsuite/ChangeLog

Comment 11 Jakub Jelinek 2010-03-22 15:28:25 UTC
Fixed also on 4.4 branch.
Comment 12 Richard Biener 2010-04-20 13:09:49 UTC
Fixed.
Comment 13 Richard Biener 2010-04-20 13:10:04 UTC
Subject: Bug 43438

Author: rguenth
Date: Tue Apr 20 13:09:42 2010
New Revision: 158555

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=158555
Log:
2010-04-20  Richard Guenther  <rguenther@suse.de>

	PR rtl-optimization/43438
	* combine.c (make_extraction): Properly zero-/sign-extend an
	extraction of the low part of a CONST_INT.

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

Added:
    branches/gcc-4_3-branch/gcc/testsuite/gcc.c-torture/execute/pr43438.c
Modified:
    branches/gcc-4_3-branch/gcc/ChangeLog
    branches/gcc-4_3-branch/gcc/combine.c
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog