Bug 35136 - [4.3 Regression] ICE caused by address calculation with loop variable when optimization is on
Summary: [4.3 Regression] ICE caused by address calculation with loop variable when op...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: 4.3.0
Assignee: Eric Botcazou
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: ice-on-valid-code
Depends on:
Blocks:
 
Reported: 2008-02-08 10:29 UTC by Markus Heichel
Modified: 2008-02-14 19:10 UTC (History)
3 users (show)

See Also:
Host: i686-pc-linux
Target: i686-pc-linux
Build: i686-pc-linux
Known to work: 4.2.3
Known to fail:
Last reconfirmed: 2008-02-12 08:25:04


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Heichel 2008-02-08 10:29:00 UTC
The example below raises an UNRECOVERABLE_ERROR when compiled with optimization:

gcc -c -O adr.adb

This seems to be a regression in snapshot 4.3.0-20080201. It is working in 4.3.0-20080111 and older releases like 4.2.3.

adr.adb
================================================
pragma Extend_System(AUX_DEC);
with SYSTEM;

procedure ADR is

   function Y(E : in INTEGER) return STRING is
   begin
      return "";
   end Y;

   function X(C : in SYSTEM.ADDRESS) return STRING is
      D : INTEGER;
      for D use at C;
   begin
      return Y(D);
   end X;

   A : SYSTEM.ADDRESS;
   B : STRING := "";

begin
   for I in 0..1 loop
      B := X(SYSTEM."+"(A, I));
   end loop;
end ADR;
================================================
Comment 1 Richard Biener 2008-02-08 11:11:40 UTC
What UNRECOVERABLE_ERROR?
Comment 2 Markus Heichel 2008-02-08 11:16:40 UTC
The exception is:

TYPES.UNRECOVERABLE_ERROR : comperr.adb:398
Comment 3 Eric Botcazou 2008-02-08 11:23:10 UTC
> The exception is:
> 
> TYPES.UNRECOVERABLE_ERROR : comperr.adb:398

Please post full the error message.
Comment 4 Markus Heichel 2008-02-08 11:27:34 UTC
The complete output is:

> gcc -c -O adr.adb
adr.adb:19:04: warning: variable "A" is read but never assigned
adr.adb: In function 'ADR':
adr.adb:5: error: expected an SSA_NAME object
adr.adb:5: error: in statement
# A.36 = VDEF <A.36> { A.36 }
A.36 = D.375_38;
+===========================GNAT BUG DETECTED==============================+
| 4.3.0 20080201 (experimental) (i686-pc-linux-gnu) verify_ssa failed      |
| Error detected around adr.adb:5                                          |
| 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).                   |
+==========================================================================+

Please include these source files with error report
Note that list may not be accurate in some cases,
so please double check that the problem can still
be reproduced with the set of files listed.

adr.adb

raised TYPES.UNRECOVERABLE_ERROR : comperr.adb:398
Exit 1
Comment 5 Richard Biener 2008-02-08 22:50:29 UTC
4.2 works for me.  This is a middle-end/tree-optimization problem.
Comment 6 Steven Bosscher 2008-02-08 23:05:14 UTC
Is it possible to create an equivalent C test case (e.g. from the initial GIMPLE dumps before the ICE)?
Comment 7 Eric Botcazou 2008-02-12 08:25:04 UTC
Investigating.
Comment 8 Eric Botcazou 2008-02-12 08:45:36 UTC
The failure mode is as follows: IVOPTS decices to turn the iteration on the
value of A (variable in the code) to an iteration on its address.  But there
is a cast in the middle

  a.0_4 = (system__aux_dec__TsaB) a_3(D);
  D.249_5 = i_131 + a.0_4;

so create_iv invokes force_gimple_operand on ADDR_EXPR<NOP_EXPR<SSA_NAME>>,
which is passed to gnat_gimplify_expr.  The latter creates the temporary
A.36 and it is never marked for renaming.

I think that the problem is in create_iv: it should make sure that the SSA
form is preserved, for example by invoking force_gimple_operand_bsi instead
of force_gimple_operand like some functions in tree-ssa-loop-ivopts.c.

But maybe the problem is the discrepancy between force_gimple_operand and
force_gimple_operand_bsi: why does the latter preserve the SSA form and not
the former?
Comment 9 Eric Botcazou 2008-02-12 09:08:50 UTC
> But maybe the problem is the discrepancy between force_gimple_operand and
> force_gimple_operand_bsi: why does the latter preserve the SSA form and not
> the former?

This would fix the problem:

Index: gimplify.c
===================================================================
--- gimplify.c	(revision 132243)
+++ gimplify.c	(working copy)
@@ -6629,6 +6629,14 @@ force_gimple_operand (tree expr, tree *s
 
   pop_gimplify_context (NULL);
 
+  if (*stmts && gimple_in_ssa_p (cfun))
+    {
+      tree_stmt_iterator tsi;
+
+      for (tsi = tsi_start (*stmts); !tsi_end_p (tsi); tsi_next (&tsi))
+	mark_symbols_for_renaming (tsi_stmt (tsi));
+    }
+
   return expr;
 }
 
@@ -6648,14 +6656,6 @@ force_gimple_operand_bsi (block_stmt_ite
   expr = force_gimple_operand (expr, &stmts, simple_p, var);
   if (stmts)
     {
-      if (gimple_in_ssa_p (cfun))
-	{
-	  tree_stmt_iterator tsi;
-
-	  for (tsi = tsi_start (stmts); !tsi_end_p (tsi); tsi_next (&tsi))
-	    mark_symbols_for_renaming (tsi_stmt (tsi));
-	}
-
       if (before)
 	bsi_insert_before (bsi, stmts, m);
       else
Comment 10 Eric Botcazou 2008-02-12 20:50:11 UTC
Subject: Bug 35136

Author: ebotcazou
Date: Tue Feb 12 20:49:21 2008
New Revision: 132267

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=132267
Log:
	PR middle-end/35136
	* gimplify.c (force_gimple_operand_bsi): Move SSA renaming code from
	here to...
	(force_gimple_operand): ...here.


Added:
    trunk/gcc/testsuite/gnat.dg/loop_address.adb
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimplify.c
    trunk/gcc/testsuite/ChangeLog

Comment 11 Eric Botcazou 2008-02-12 20:53:12 UTC
Thanks for reporting the problem.
Comment 12 Eric Botcazou 2008-02-13 06:43:51 UTC
Something is wrong.
Comment 13 Jakub Jelinek 2008-02-13 08:29:43 UTC
I'm not sure it is a good idea to change semantics of force_gimple_operand
this late in 4.3 cycle.
Comment 14 Eric Botcazou 2008-02-13 08:44:06 UTC
> I'm not sure it is a good idea to change semantics of force_gimple_operand
> this late in 4.3 cycle.

It's not really a change of semantics, it only bridges the gap between two
functions that are supposed to behave the same, but accidentally don't.
Either they both should do SSA renaming, or they both shouldn't, but there
is no justification for the current status in my opinion.

That being said, there is a more annoying problem with IVOPTS which is now
papered over twice, instead of once previously, so I don't mind backing out
the change.
Comment 15 Richard Biener 2008-02-13 14:33:58 UTC
So, what exactly is wrong now?  And what is this IVOPTs problem? ;)
Comment 16 Eric Botcazou 2008-02-13 21:54:09 UTC
> So, what exactly is wrong now?

Oh nothing, just the generated code. :-)

  D.375_38 = (system__aux_dec__TsaB) a_3(D);
  A.36 = D.375_38;
  A.37_35 = (system__address *) &A.36;
  ivtmp.34_32 = (unsigned int) A.37_35;

[...]

  D.379_8 = (system__aux_dec__TsaB) a_3(D);
  A.38 = D.379_8;
  D.380_1 = &A.38 + 2;
  D.381_97 = (unsigned int) D.380_1;
  if (ivtmp.34_58 == D.381_97)

We compare apples (address of A.36) with orange (address of A.38).

> And what is this IVOPTs problem? ;)

It is too optimistic about addressability.
Comment 17 Eric Botcazou 2008-02-13 22:25:24 UTC
> It is too optimistic about addressability.

It takes the address of non-addressable things:

  base &VIEW_CONVERT_EXPR<system__address>((system__aux_dec__TsaB) a_3(D))
Comment 18 Eric Botcazou 2008-02-14 19:08:22 UTC
Subject: Bug 35136

Author: ebotcazou
Date: Thu Feb 14 19:07:38 2008
New Revision: 132320

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=132320
Log:
	PR middle-end/35136
	* gimplify.c (force_gimple_operand_bsi): Revert 2008-02-12 change.
	(force_gimple_operand): Likewise.
	* tree-ssa-loop-ivopts.c (may_be_nonaddressable_p): Add new cases
	for TARGET_MEM_REF and CONVERT_EXPR/NON_LVALUE_EXPR/NOP_EXPR.
	Also recurse on the operand for regular VIEW_CONVERT_EXPRs.
	(find_interesting_uses_address): Check addressability and alignment
	of the base expression only after substituting bases of IVs into it.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimplify.c
    trunk/gcc/tree-ssa-loop-ivopts.c

Comment 19 Eric Botcazou 2008-02-14 19:10:31 UTC
Hopefully for real this time.