Bug 21959 - [4.1 Regression] vrp miscompiles Ada front-end, drops loop exit test in well-defined wrap-around circumstances
Summary: [4.1 Regression] vrp miscompiles Ada front-end, drops loop exit test in well-...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.1.0
: P2 critical
Target Milestone: 4.1.0
Assignee: Diego Novillo
URL:
Keywords: build, wrong-code
: 22221 (view as bug list)
Depends on:
Blocks: 18373 21923 21994 22019
  Show dependency treegraph
 
Reported: 2005-06-08 12:31 UTC by Andrew Pinski
Modified: 2005-06-28 18:03 UTC (History)
8 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.0.0
Known to fail: 4.1.0
Last reconfirmed: 2005-06-21 19:49:54


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2005-06-08 12:31:46 UTC
See <http://gcc.gnu.org/ml/gcc/2005-06/msg00059.html> for the full description.
Comment 1 Andrew Pinski 2005-06-08 12:32:26 UTC
Confirmed based on the whole thread.
Comment 2 Pawel Sikora 2005-06-09 11:59:40 UTC
hmm, I can't test the 4.1 bootstrap with -fwrapv due to xgcc error.

make -C obj-amd64-pld-linux \
        bootstrap \
        GCJFLAGS="%{rpmcflags}" \
        BOOT_ADAFLAGS="%{rpmcflags} -fwrapv" \
        GNATLIBCFLAGS="-O1 -fwrapv" \
        BOOT_CFLAGS="%{rpmcflags}" \
        STAGE1_CFLAGS="%{rpmcflags} -O0" \
        LDFLAGS_FOR_TARGET="%{rpmldflags}"

(...)
stage1/xgcc -Bstage1/ -B/usr/amd64-pld-linux/bin/ -c -O1 -fwrapv
-I- -I. -Iada -I../../gcc/ada ../../gcc/ada/ada.ads -o ada/ada.o
ada.ads:16:01: language defined units may not be recompiled
make[2]: *** [ada/ada.o] Error 1
Comment 3 Andreas Schwab 2005-06-10 16:57:27 UTC
> hmm, I can't test the 4.1 bootstrap with -fwrapv due to xgcc error.  Try this to enable -fwrapv by default:  --- misc.c	01 Jun 2005 10:00:47 +0200	1.102 +++ misc.c	08 Jun 2005 14:58:25 +0200	 @@ -339,6 +339,8 @@ gnat_init_options (unsigned int argc, co    /* Uninitialized really means uninitialized in Ada.  */    flag_zero_initialized_in_bss = 0;   +  flag_wrapv = 1; +    return CL_Ada;  }
Comment 4 Andreas Schwab 2005-06-10 16:59:54 UTC
> hmm, I can't test the 4.1 bootstrap with -fwrapv due to xgcc error.

Try this to enable -fwrapv by default:

--- misc.c	01 Jun 2005 10:00:47 +0200	1.102
+++ misc.c	08 Jun 2005 14:58:25 +0200	
@@ -339,6 +339,8 @@ gnat_init_options (unsigned int argc, co
   /* Uninitialized really means uninitialized in Ada.  */
   flag_zero_initialized_in_bss = 0;
 
+  flag_wrapv = 1;
+
   return CL_Ada;
 }
 
Comment 5 Andrew Pinski 2005-06-10 18:52:45 UTC
(In reply to comment #4)
as the thread said this is only a workaround.
Comment 6 Pawel Sikora 2005-06-10 21:23:53 UTC
with this workaround stage3 fails :/ 
 
(...) 
../../xgcc -B../../  -c -g -O2 -fPIC      -W -Wall -gnatpg  a-ncelfu.ads -o 
a-ncelfu.o 
a-ngcefu.adb: In function 'Ada.Numerics.Complex_Elementary_Functions.Sqrt': 
a-ngcefu.adb:578: error: Statement marked for throw in middle of block. 
D.1921_90 = ada__numerics__complex_types__compose_from_cartesian (D.1920_89, 
0.0); 
 
a-ngcefu.adb:578: error: Statement marked for throw in middle of block. 
D.1928_86 = ada__numerics__complex_types__compose_from_cartesian (0.0, 
D.1927_85); 
 
a-ngcefu.adb:578: error: Statement marked for throw in middle of block. 
D.1931_78 = ada__numerics__complex_types__compose_from_cartesian 
(r_xD.1914_73, r_xD.1914_73); 
 
a-ngcefu.adb:578: error: Statement marked for throw in middle of block. 
D.1933_76 = ada__numerics__complex_types__compose_from_cartesian 
(r_xD.1914_73, D.1932_75); 
 
a-ngcefu.adb:578: error: Statement marked for throw in middle of block. 
D.1948_55 = ada__numerics__complex_types__compose_from_cartesian (r_xD.1914_2, 
r_yD.1915_4); 
 
a-ngcefu.adb:578: error: Statement marked for throw in middle of block. 
D.1950_21 = ada__numerics__complex_types__Odivide__3 (xD.370_6, 4.0e+0); 
 
a-ngcefu.adb:578: error: Statement marked for throw in middle of block. 
D.1953_25 = ada__numerics__complex_types__compose_from_cartesian (D.1952_24, 
D.1951_22); 
 
a-ngcefu.adb:578: error: Statement marked for throw in middle of block. 
D.1948_45 = ada__numerics__complex_types__compose_from_cartesian 
(r_xD.1914_34, r_yD.1915_5); 
 
+===========================GNAT BUG DETECTED==============================+ 
| 4.1.0 20050610 (experimental) (i686-pld-linux-gnu) verify_stmts failed.  | 
| Error detected at a-ncelfu.ads:23:1                                      | 
| Please submit a bug report; see http://gcc.gnu.org/bugs.html.            | 
| Use a subject line meaningful to you and us to track the bug.            | 
| Include the entire contents of this bug box in the report.               | 
| Include the exact gcc or gnatmake command that you entered.              | 
| Also include sources listed below in gnatchop format                     | 
| (concatenated together with no headers between files).                   | 
+==========================================================================+ 
(...) 
raised TYPES.UNRECOVERABLE_ERROR : comperr.adb:381 
Comment 7 Andrew Pinski 2005-06-10 21:26:10 UTC
(In reply to comment #6)
> with this workaround stage3 fails :/ 
Yes that is PR 21994 and then we are blocked by 21923.
Comment 8 Eric Botcazou 2005-06-17 19:56:41 UTC
Well, it seems to me that the culprit is fold, no?

	    else if (!in_gimple_form
		     && TREE_INT_CST_HIGH (arg1) == signed_max_hi
		     && TREE_INT_CST_LOW (arg1) == signed_max_lo
		     && TYPE_UNSIGNED (TREE_TYPE (arg1))
		     /* signed_type does not work on pointer types.  */
		     && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
	      {
		/* The following case also applies to X < signed_max+1
		   and X >= signed_max+1 because previous transformations.  */
		if (code == LE_EXPR || code == GT_EXPR)
		  {
		    tree st0, st1;
		    st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
		    st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
		    return fold
		      (build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
			       type, fold_convert (st0, arg0),
			       fold_convert (st1, integer_zero_node)));
		  }
	      }

(gdb) p debug_tree(arg0)
 <var_decl 0x556e1d80 J4b
    type <integer_type 0x556d9144 character sizes-gimplified public visited
unsigned QI
        size <integer_cst 0x556a61f8 constant invariant visited 8>
        unit size <integer_cst 0x556a6210 constant invariant visited 1>
        user align 8 symtab 0 alias set -1 precision 8 min <integer_cst
0x556d71e0 0> max <integer_cst 0x556d71f8 255> RM size <integer_cst 0x556a61f8 8>
        pointer_to_this <pointer_type 0x556d93cc access_character>>
    unsigned ignored QI file ../../asets.adb line 3 size <integer_cst 0x556a61f8
8> unit size <integer_cst 0x556a6210 1>
    align 8 context <function_decl 0x556e0144 asets___elabb> initial
<integer_cst 0x556d71e0 0>>
$13 = void
(gdb) p debug_tree(arg1)
 <integer_cst 0x556e3030 type <integer_type 0x556d9144 character> constant
invariant 127>
$14 = void
(gdb) p code
$15 = LE_EXPR
Comment 9 Andrew Pinski 2005-06-17 20:02:04 UTC
(In reply to comment #8)
> Well, it seems to me that the culprit is fold, no?

No because Ada front-end is creating:
signed_char <= 127 which is always true, removing this from fold would not change the problem as it 
will still be optimized away.
Comment 10 Eric Botcazou 2005-06-17 20:23:59 UTC
> No because Ada front-end is creating:
> signed_char <= 127 which is always true, removing this from fold would not
> change the problem as it will still be optimized away.

Where do you see signed_char?  The type 0x556d9144 is unsigned.

Comment 11 Andrew Pinski 2005-06-17 20:37:49 UTC
(In reply to comment #10)
> > No because Ada front-end is creating:
> > signed_char <= 127 which is always true, removing this from fold would not
> > change the problem as it will still be optimized away.
> 
> Where do you see signed_char?  The type 0x556d9144 is unsigned.

Woops, I must have read something wrong.
This is still a bug in the VRP.
C Testcase:
unsigned char c[0xFF];
void f(void)
{
  unsigned char i;
  c[128] = 128;
  i = 0;
  while (1)
  {
    if (((signed char) i) < 0) break;
    c[i] = ' ';
    i++;
  }
}
Comment 12 Andrew Pinski 2005-06-17 20:54:31 UTC
Why does ivcanon/tree-ssa-loop-niter gets this correct (at 128) but VRP cannot.
Comment 13 Andrew Pinski 2005-06-21 19:49:54 UTC
Still wrong:
i_2: VARYING
i.0_6: [0, +INF]  EQUIVALENCES: { } (0 elements)


  # i_2 = PHI <0(0), i_9(2)>;
<L0>:;
  i.0_6 = (signed char) i_2;
  if (i.0_6 < 0) goto <L2>; else goto <L1>;
Comment 14 Laurent GUERBY 2005-06-21 21:06:00 UTC
Still an infinite loop on bootstrap as of LAST_UPDATED Tue Jun 21 20:10:50 UTC 2005

stage2/xgcc -Bstage2/
-B/home/guerby/work/gcc/install/install-20050621T221553/x86_64-unknown-linux-gnu/bin/
-c -g -O2      -gnatpg -gnata -I- -I. -Iada
-I/home/guerby/work/gcc/version-head/gcc/ada
/home/guerby/work/gcc/version-head/gcc/ada/ada.ads -o ada/ada.o
Comment 15 Diego Novillo 2005-06-27 17:05:05 UTC
(In reply to comment #12)
> Why does ivcanon/tree-ssa-loop-niter gets this correct (at 128) but VRP cannot.
>
Wrong.  scev fails to tell us that this variable may wrap around.
Comment 16 Diego Novillo 2005-06-27 19:41:39 UTC
Testing patch.
Comment 17 GCC Commits 2005-06-28 00:52:46 UTC
Subject: Bug 21959

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	dnovillo@gcc.gnu.org	2005-06-28 00:52:35

Modified files:
	gcc            : ChangeLog tree-ssa-loop-niter.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.dg/tree-ssa: pr21959.c 

Log message:
	PR 21959
	* tree-ssa-loop-niter.c (scev_probably_wraps_p): Handle type
	casts between unsigned and signed types with different size
	or precision.
	
	testsuite/ChangeLog
	
	PR 21959
	* gcc.dg/tree-ssa/pr21959.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.9246&r2=2.9247
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree-ssa-loop-niter.c.diff?cvsroot=gcc&r1=2.31&r2=2.32
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.5692&r2=1.5693
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/tree-ssa/pr21959.c.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 18 Diego Novillo 2005-06-28 01:14:30 UTC
Fixed.
Comment 19 Andrew Pinski 2005-06-28 18:03:57 UTC
*** Bug 22221 has been marked as a duplicate of this bug. ***