Bug 77937 - [7 Regression] ICE: in replace_one_candidate, at gimple-ssa-strength-reduction.c:3370
Summary: [7 Regression] ICE: in replace_one_candidate, at gimple-ssa-strength-reductio...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: 7.0
Assignee: Bill Schmidt
URL:
Keywords: ice-on-valid-code
Depends on:
Blocks:
 
Reported: 2016-10-11 16:53 UTC by Markus Trippelsdorf
Modified: 2016-10-19 13:35 UTC (History)
3 users (show)

See Also:
Host:
Target: x86_64-pc-linux-gnu
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-10-11 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Trippelsdorf 2016-10-11 16:53:24 UTC
markus@x4 ffmpeg % cat diracdsp.i
int *a;
int b, c, d;
void fn1(char *p1, int p2) {
  int x;
  while (1) {
    x = 0;
    for (; x < 8; x++)
      p1[0] = -a[0] * d + p1[0] * c + 1 >> b >> 1;
    p1 += p2;
  }
}

markus@x4 ffmpeg % gcc -c -O3 -march=amdfam10 diracdsp.i
diracdsp.i: In function ‘fn1’:
diracdsp.i:3:6: internal compiler error: in replace_one_candidate, at gimple-ssa-strength-reduction.c:3370
 void fn1(char *p1, int p2) {
      ^~~
0x1269fe9 replace_one_candidate
        ../../gcc/gcc/gimple-ssa-strength-reduction.c:3370
0x126db67 replace_profitable_candidates
        ../../gcc/gcc/gimple-ssa-strength-reduction.c:3481
0x126dadb replace_profitable_candidates
        ../../gcc/gcc/gimple-ssa-strength-reduction.c:3490
0x1271fc6 analyze_candidates_and_replace
        ../../gcc/gcc/gimple-ssa-strength-reduction.c:3569
0x1271fc6 execute
        ../../gcc/gcc/gimple-ssa-strength-reduction.c:3643
Comment 1 Markus Trippelsdorf 2016-10-11 17:01:00 UTC
Started with r240945:

commit 41b5b58f52ee35f1efe7eeac644db6992dac5499
Author: wschmidt <wschmidt@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Mon Oct 10 18:39:41 2016 +0000

    2016-10-10  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

            PR tree-optimization/77824
            * gimple-ssa-strength-reduction.c (stmt_cost): Explicitly return
            zero cost for copies.
            (find_candidates_dom_walker::before_dom_children): Replace
            MODIFY_EXPR with SSA_NAME.
            (replace_mult_candidate): Likewise.
            (replace_profitable_candidates): Likewise.
Comment 2 Bill Schmidt 2016-10-11 17:45:38 UTC
Mine to investigate -- looks like enabling copies to be handled properly has exposed another "opportunity" here.  Looks like probably another issue with pointer addition.
Comment 3 Bill Schmidt 2016-10-11 19:03:05 UTC
Does not reproduce on powerpc64le-unknown-linux-gnu.  Can you please report the target triple?
Comment 4 Markus Trippelsdorf 2016-10-11 19:04:18 UTC
Well, -march=amdfam10 is of course x86_64-pc-linux-gnu.
Comment 5 Bill Schmidt 2016-10-11 20:39:40 UTC
OK, thanks.  While I get a cross built, could you please try the following patch?

Index: gcc/gimple-ssa-strength-reduction.c                                      
===================================================================
--- gcc/gimple-ssa-strength-reduction.c (revision 240946)                       
+++ gcc/gimple-ssa-strength-reduction.c (working copy)                          
@@ -3367,7 +3367,7 @@ replace_one_candidate (slsr_cand_t c, unsigned i,
     {                                                                          
       tree stride_type = TREE_TYPE (c->stride);                                
       tree orig_type = TREE_TYPE (orig_rhs2);                                  
-      gcc_assert (repl_code != POINTER_PLUS_EXPR);                             
+      tree basis_type = TREE_TYPE (basis_name);                                
                                                                                
       if (types_compatible_p (orig_type, stride_type))                         
        rhs2 = c->stride;                                                       
@@ -3374,7 +3374,16 @@ replace_one_candidate (slsr_cand_t c, unsigned i,
       else                                                                     
        rhs2 = introduce_cast_before_cand (c, orig_type, c->stride);            
                                                                                
-      if (orig_code != MINUS_EXPR                                              
+      if (POINTER_TYPE_P (basis_type))                                         
+       {                                                                       
+         tree neg_stride = fold_unary (NEGATE_EXPR, sizetype, rhs2);           
+         gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);               
+         gimple_assign_set_rhs_with_ops (&gsi, POINTER_PLUS_EXPR,              
+                                         basis_name, neg_stride);              
+         update_stmt (gsi_stmt (gsi));                                         
+         c->cand_stmt = gsi_stmt (gsi);                                        
+       }                                                                       
+      else if (orig_code != MINUS_EXPR                                         
          || !operand_equal_p (basis_name, orig_rhs1, 0)                        
          || !operand_equal_p (rhs2, orig_rhs2, 0))                             
        {
Comment 6 Markus Trippelsdorf 2016-10-12 02:55:07 UTC
Unfortunately it doesn't work:

dirac.i:3:6: internal compiler error: Segmentation fault
 void fn1(char *p1, int p2) {
      ^~~
0xb6d367 crash_signal
        ../../gcc/gcc/toplev.c:337
0x7ff87981f10f ???
        /home/markus/glibc/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0
0xb9bad2 contains_struct_check(tree_node*, tree_node_structure_enum, char const*, int, char const*)
        ../../gcc/gcc/tree.h:3144
0xb9bad2 verify_gimple_assign_binary
        ../../gcc/gcc/tree-cfg.c:3732
0xbb479b verify_gimple_in_cfg(function*, bool)
        ../../gcc/gcc/tree-cfg.c:5135
0xa97737 execute_function_todo
        ../../gcc/gcc/passes.c:1964
0xa987ec execute_todo
        ../../gcc/gcc/passes.c:2014
Comment 7 Bill Schmidt 2016-10-12 19:37:55 UTC
OK, I can reproduce now, and understand the problem better.  This patch fixes the problem.  I'll regstrap it and get it committed shortly.

Index: gcc/gimple-ssa-strength-reduction.c
===================================================================
--- gcc/gimple-ssa-strength-reduction.c (revision 240946)
+++ gcc/gimple-ssa-strength-reduction.c (working copy)
@@ -2816,8 +2816,7 @@ analyze_increments (slsr_cand_t first_dep, machine
       else if (incr == 0
               || incr == 1
               || (incr == -1
-                  && (gimple_assign_rhs_code (first_dep->cand_stmt)
-                      != POINTER_PLUS_EXPR)))
+                  && !POINTER_TYPE_P (first_dep->cand_type)))
        incr_vec[i].cost = COST_NEUTRAL;
       
       /* FORNOW: If we need to add an initializer, give up if a cast from
Comment 8 Bill Schmidt 2016-10-13 01:08:52 UTC
Author: wschmidt
Date: Thu Oct 13 01:08:20 2016
New Revision: 241082

URL: https://gcc.gnu.org/viewcvs?rev=241082&root=gcc&view=rev
Log:
2016-10-12  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	PR tree-optimization/77937
	* gimple-ssa-strength-reduction.c (analyze_increments): Use
	POINTER_TYPE_P on the candidate type to determine whether
	candidates in this chain require pointer arithmetic.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimple-ssa-strength-reduction.c
Comment 9 Bill Schmidt 2016-10-13 01:17:31 UTC
Keeping this open until the fix has some burn-in time, planning to backport to GCC 6 and 5.
Comment 10 Markus Trippelsdorf 2016-10-13 05:16:52 UTC
It still happens, but on another unit this time:

markus@x4 ffmpeg % cat h264dsp.i
extern int fn2(int);
extern int fn3(int);
int a, b, c;
void fn1(long p1) {
  char *d;
  for (;; d += p1) {
    d[0] = fn2(1 >> a);
    fn3(0);
    fn3(c >> a);
    d[1] = fn3(d[1] * b + c >> a);
    d[4] = fn3(d[4] * b + c >> a);
    d[5] = fn3(d[5] * b + c >> a);
  }
}

markus@x4 ffmpeg % gcc -march=amdfam10 -O3 -c -S h264dsp.i
h264dsp.i: In function ‘fn1’:
h264dsp.i:4:6: internal compiler error: in replace_one_candidate, at gimple-ssa-strength-reduction.c:3369
 void fn1(long p1) {
      ^~~

Simply building ffmpeg with -march=amdfam10 finds these bugs.

Please consider reverting your original patch until you come up with solution.
Comment 11 Bill Schmidt 2016-10-13 15:34:53 UTC
Author: wschmidt
Date: Thu Oct 13 15:34:22 2016
New Revision: 241125

URL: https://gcc.gnu.org/viewcvs?rev=241125&root=gcc&view=rev
Log:
2016-10-13  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	PR tree-optimization/77937
	* gimple-ssa-strength-reduction.c (analyze_increments): Set cost
	to infinite when we have a pointer with an increment of -1.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimple-ssa-strength-reduction.c
Comment 12 Bill Schmidt 2016-10-13 15:43:06 UTC
Just saw your request to include the test case.  I will do this but may ask you to review the dejagnu bits for accuracy for your target, as these don't fire on arches I regularly build for.
Comment 13 Bill Schmidt 2016-10-13 19:51:12 UTC
Author: wschmidt
Date: Thu Oct 13 19:50:41 2016
New Revision: 241139

URL: https://gcc.gnu.org/viewcvs?rev=241139&root=gcc&view=rev
Log:
2016-10-13  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	PR tree-optimization/77937
	* gcc.dg/torture/pr77937-1.c: New.
	* gcc.dg/torture/pr77937-2.c: New.


Added:
    trunk/gcc/testsuite/gcc.dg/torture/pr77937-1.c
    trunk/gcc/testsuite/gcc.dg/torture/pr77937-2.c
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 14 Bill Schmidt 2016-10-17 14:22:54 UTC
Author: wschmidt
Date: Mon Oct 17 14:22:22 2016
New Revision: 241243

URL: https://gcc.gnu.org/viewcvs?rev=241243&root=gcc&view=rev
Log:
[gcc]

2016-10-17  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	Backport from mainline
	2016-10-12  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	PR tree-optimization/77937
	* gimple-ssa-strength-reduction.c (analyze_increments): Use
	POINTER_TYPE_P on the candidate type to determine whether
	candidates in this chain require pointer arithmetic.

	2016-10-13  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	PR tree-optimization/77937
	* gimple-ssa-strength-reduction.c (analyze_increments): Set cost
	to infinite when we have a pointer with an increment of -1.

[gcc/testsuite]

2016-10-17  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	Backport from mainline
	2016-10-13  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	PR tree-optimization/77937
	* gcc.dg/torture/pr77937-1.c: New.
	* gcc.dg/torture/pr77937-2.c: New.


Added:
    branches/gcc-6-branch/gcc/testsuite/gcc.dg/torture/pr77937-1.c
    branches/gcc-6-branch/gcc/testsuite/gcc.dg/torture/pr77937-2.c
Modified:
    branches/gcc-6-branch/gcc/ChangeLog
    branches/gcc-6-branch/gcc/gimple-ssa-strength-reduction.c
    branches/gcc-6-branch/gcc/testsuite/ChangeLog
Comment 15 Bill Schmidt 2016-10-17 14:24:53 UTC
Author: wschmidt
Date: Mon Oct 17 14:24:21 2016
New Revision: 241244

URL: https://gcc.gnu.org/viewcvs?rev=241244&root=gcc&view=rev
Log:
[gcc]

2016-10-17  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	Backport from mainline
	2016-10-12  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	PR tree-optimization/77937
	* gimple-ssa-strength-reduction.c (analyze_increments): Use
	POINTER_TYPE_P on the candidate type to determine whether
	candidates in this chain require pointer arithmetic.

	2016-10-13  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	PR tree-optimization/77937
	* gimple-ssa-strength-reduction.c (analyze_increments): Set cost
	to infinite when we have a pointer with an increment of -1.

[gcc/testsuite]

2016-10-17  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	Backport from mainline
	2016-10-13  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	PR tree-optimization/77937
	* gcc.dg/torture/pr77937-1.c: New.
	* gcc.dg/torture/pr77937-2.c: New.


Added:
    branches/gcc-5-branch/gcc/testsuite/gcc.dg/torture/pr77937-1.c
    branches/gcc-5-branch/gcc/testsuite/gcc.dg/torture/pr77937-2.c
Modified:
    branches/gcc-5-branch/gcc/ChangeLog
    branches/gcc-5-branch/gcc/gimple-ssa-strength-reduction.c
    branches/gcc-5-branch/gcc/testsuite/ChangeLog
Comment 16 Bill Schmidt 2016-10-17 14:27:07 UTC
Fixed.  Further work for similar issues is being done under https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77916.
Comment 17 Bill Schmidt 2016-10-19 13:35:46 UTC
Author: wschmidt
Date: Wed Oct 19 13:35:14 2016
New Revision: 241342

URL: https://gcc.gnu.org/viewcvs?rev=241342&root=gcc&view=rev
Log:
2016-10-19  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	PR tree-optimization/77916
	PR tree-optimization/77937
	* gimple-ssa-strength-reduction.c (analyze_increments): Remove
	stopgap fix.
	(insert_initializers): Requirement of initializer for -1 should be
	based on pointer-typedness of the candidate basis.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimple-ssa-strength-reduction.c