Bug 84739

Summary: [6/7 Regression] ICE in get_value_for_expr, at tree-ssa-ccp.c:649
Product: gcc Reporter: G. Steinmetz <gscfq>
Component: tree-optimizationAssignee: Jakub Jelinek <jakub>
Status: RESOLVED FIXED    
Severity: normal CC: dmalcolm, jakub
Priority: P2 Keywords: ice-on-valid-code
Version: 8.0   
Target Milestone: 6.5   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2018-03-06 00:00:00
Attachments: gcc8-pr84739.patch

Description G. Steinmetz 2018-03-06 18:22:33 UTC
Affects versions 6/7/8 at -O[s23] :


$ cat z1.c
static void baz (void) __attribute__((weakref("bar")));
int
foo (int x, int y)
{
  if (x)
    y = 0;
  if (y)
    goto L1;
  y = 0;
L1:
  return y;
}
void
bar (int x, int y)
{
  y = foo (x, y);
  if (y != 0)
    baz ();
}


$ gcc-5 -O2 -c z1.c
$
$ gcc-8-20180304 -O2 -c z1.c
z1.c:1:13: warning: 'baz' alias between functions of incompatible types 'void(void)' and 'void(int,  int)' [-Wattribute-alias]
 static void baz (void) __attribute__((weakref("bar")));
             ^~~
z1.c:14:1: note: aliased declaration here
 bar (int x, int y)
 ^~~
during GIMPLE pass: ccp
z1.c: In function 'bar':
z1.c:14:1: internal compiler error: Segmentation fault
0xaed10f crash_signal
        ../../gcc/toplev.c:325
0xbc5401 get_value_for_expr
        ../../gcc/tree-ssa-ccp.c:649
0xbc7ea8 ccp_propagate::visit_phi(gphi*)
        ../../gcc/tree-ssa-ccp.c:1133
0xc42778 ssa_propagation_engine::simulate_stmt(gimple*)
        ../../gcc/tree-ssa-propagate.c:233
0xc42992 ssa_propagation_engine::simulate_block(basic_block_def*)
        ../../gcc/tree-ssa-propagate.c:359
0xc43669 ssa_propagation_engine::ssa_propagate()
        ../../gcc/tree-ssa-propagate.c:800
0xbc4697 do_ssa_ccp
        ../../gcc/tree-ssa-ccp.c:2474
0xbc4697 execute
        ../../gcc/tree-ssa-ccp.c:2518
Comment 1 G. Steinmetz 2018-03-06 18:23:00 UTC
Configured with --enable-checking=yes :


$ gcc-8-20180304 -O2 -c z1.c
z1.c:1:13: warning: 'baz' alias between functions of incompatible types 'void(void)' and 'void(int,  int)' [-Wattribute-alias]
 static void baz (void) __attribute__((weakref("bar")));
             ^~~
z1.c:14:1: note: aliased declaration here
 bar (int x, int y)
 ^~~
during GIMPLE pass: tailr
z1.c: In function 'bar':
z1.c:19:1: internal compiler error: in gimple_call_arg, at gimple.h:3179
 }
 ^
0xecae86 gimple_call_arg
        ../../gcc/gimple.h:3179
0xecae86 gimple_call_arg
        ../../gcc/gimple.h:3187
0xecae86 eliminate_tail_call
        ../../gcc/tree-tailcall.c:921
0xecae86 optimize_tail_call
        ../../gcc/tree-tailcall.c:953
0xecae86 tree_optimize_tail_calls_1
        ../../gcc/tree-tailcall.c:1082
Comment 2 David Malcolm 2018-03-06 21:45:32 UTC
Confirmed.

The ICE in comment #1 started with r231428.
Comment 3 Jakub Jelinek 2018-03-06 22:09:43 UTC
Untested fix:
--- gcc/tree-tailcall.c.jj	2018-01-12 11:35:51.470222838 +0100
+++ gcc/tree-tailcall.c	2018-03-06 23:05:39.779048759 +0100
@@ -481,7 +481,7 @@ find_tail_calls (basic_block bb, struct
     {
       tree arg;
 
-      for (param = DECL_ARGUMENTS (func), idx = 0;
+      for (param = DECL_ARGUMENTS (current_function_decl), idx = 0;
 	   param && idx < gimple_call_num_args (call);
 	   param = DECL_CHAIN (param), idx ++)
 	{

Wonder if the bug is also reproduceable when the gimple call has some different fntype.
Comment 4 Jakub Jelinek 2018-03-07 08:52:34 UTC
Created attachment 43582 [details]
gcc8-pr84739.patch

Untested fix.  With different gimple_call_fntype the ICE doesn't reproduce, because if there is a mismatch between func's DECL_ARGUMENTS and the actual arguments, the current code already rejects it as tail recursion.  In the past,
there used to be if (func == current_function_decl) condition and so what we picked DECL_ARGUMENTS from didn't really matter, but now with the recursive_call_p check it does.
Comment 5 Jakub Jelinek 2018-03-08 06:57:31 UTC
Author: jakub
Date: Thu Mar  8 06:56:59 2018
New Revision: 258351

URL: https://gcc.gnu.org/viewcvs?rev=258351&root=gcc&view=rev
Log:
	PR tree-optimization/84739
	* tree-tailcall.c (find_tail_calls): Check call arguments against
	DECL_ARGUMENTS (current_function_decl) rather than
	DECL_ARGUMENTS (func) when checking for tail recursion.

	* gcc.dg/pr84739.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr84739.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-tailcall.c
Comment 6 Jakub Jelinek 2018-03-08 06:59:31 UTC
Fixed on the trunk so far.
Comment 7 Jakub Jelinek 2018-06-22 20:31:52 UTC
Author: jakub
Date: Fri Jun 22 20:31:10 2018
New Revision: 261914

URL: https://gcc.gnu.org/viewcvs?rev=261914&root=gcc&view=rev
Log:
	Backported from mainline
	2018-03-08  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/84739
	* tree-tailcall.c (find_tail_calls): Check call arguments against
	DECL_ARGUMENTS (current_function_decl) rather than
	DECL_ARGUMENTS (func) when checking for tail recursion.

	* gcc.dg/pr84739.c: New test.

Added:
    branches/gcc-7-branch/gcc/testsuite/gcc.dg/pr84739.c
Modified:
    branches/gcc-7-branch/gcc/ChangeLog
    branches/gcc-7-branch/gcc/testsuite/ChangeLog
    branches/gcc-7-branch/gcc/tree-tailcall.c
Comment 8 Jakub Jelinek 2018-06-25 17:29:04 UTC
Author: jakub
Date: Mon Jun 25 17:28:32 2018
New Revision: 262071

URL: https://gcc.gnu.org/viewcvs?rev=262071&root=gcc&view=rev
Log:
	Backported from mainline
	2018-03-08  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/84739
	* tree-tailcall.c (find_tail_calls): Check call arguments against
	DECL_ARGUMENTS (current_function_decl) rather than
	DECL_ARGUMENTS (func) when checking for tail recursion.

	* gcc.dg/pr84739.c: New test.

Added:
    branches/gcc-6-branch/gcc/testsuite/gcc.dg/pr84739.c
Modified:
    branches/gcc-6-branch/gcc/ChangeLog
    branches/gcc-6-branch/gcc/testsuite/ChangeLog
    branches/gcc-6-branch/gcc/tree-tailcall.c
Comment 9 Jakub Jelinek 2018-06-25 18:20:00 UTC
Fixed for 6.5 and 7.4+ too.