Bug 78633 - [12/13/14/15 Regression] [SH] libgcc/fp-bit.c:944:1: error: invalid rtl sharing found in the insn
Summary: [12/13/14/15 Regression] [SH] libgcc/fp-bit.c:944:1: error: invalid rtl shari...
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 7.0
: P4 normal
Target Milestone: 12.5
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-valid-code
: 79063 (view as bug list)
Depends on:
Blocks:
 
Reported: 2016-12-01 14:24 UTC by Kazumoto Kojima
Modified: 2024-07-19 12:59 UTC (History)
2 users (show)

See Also:
Host:
Target: sh*-*-*
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
reduce testcase (462 bytes, text/plain)
2016-12-07 00:52 UTC, Kazumoto Kojima
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kazumoto Kojima 2016-12-01 14:24:34 UTC
Build fails during compiling libgcc.

../../../ORIG/trunk/libgcc/fp-bit.c: In function '__muldf3':
../../../ORIG/trunk/libgcc/fp-bit.c:944:1: error: invalid rtl sharing found in the insn
 }
 ^
(insn 804 311 318 28 (set (reg:SI 147 t)
        (eq:SI (and:SI (subreg:SI (reg/v:DI 205 [ high ]) 0)
                (const_int 1 [0x1]))
            (const_int 0 [0]))) "../../../ORIG/trunk/libgcc/fp-bit.c":881 -1
     (nil))
../../../ORIG/trunk/libgcc/fp-bit.c:944:1: error: shared rtx
(subreg:SI (reg/v:DI 205 [ high ]) 0)
../../../ORIG/trunk/libgcc/fp-bit.c:944:1: internal compiler error: internal consistency failure
0x8386b17 verify_rtx_sharing
        ../../ORIG/trunk/gcc/emit-rtl.c:2743
0x8386a28 verify_rtx_sharing
        ../../ORIG/trunk/gcc/emit-rtl.c:2758
0x8386a28 verify_rtx_sharing
        ../../ORIG/trunk/gcc/emit-rtl.c:2758
0x8386a28 verify_rtx_sharing
        ../../ORIG/trunk/gcc/emit-rtl.c:2758
0x8386feb verify_insn_sharing
        ../../ORIG/trunk/gcc/emit-rtl.c:2829
0x838b256 verify_rtl_sharing()
        ../../ORIG/trunk/gcc/emit-rtl.c:2852
0x8606233 execute_function_todo
        ../../ORIG/trunk/gcc/passes.c:1982
0x8606a9b do_per_function
        ../../ORIG/trunk/gcc/passes.c:1649
0x8606c1d execute_todo
        ../../ORIG/trunk/gcc/passes.c:2015

It looks that re-enabled RTL sharing verification reveals some rtl sharing issue of this target.
Comment 1 Kazumoto Kojima 2016-12-01 23:43:32 UTC
Here is a trial patch

diff --git a/config/sh/sh.md b/config/sh/sh.md
index c6956a0..c83bf08 100644
--- a/config/sh/sh.md
+++ b/config/sh/sh.md
@@ -858,7 +858,8 @@
 	 operands of the tstsi_t insn, which is generally the case.  */
       if (dump_file)
 	fprintf (dump_file, "cmpeqsi_t: replacing with tstsi_t\n");
-      emit_insn (gen_tstsi_t (XEXP (op.set_src, 0), XEXP (op.set_src, 1)));
+      emit_insn (gen_tstsi_t (copy_rtx (XEXP (op.set_src, 0)),
+			      XEXP (op.set_src, 1)));
       DONE;
     }
 
Oleg, could you take a look at the issue?  Although the patch fixes
the ICE, I'm not sure if it's OK and enough.
Comment 2 Kazumoto Kojima 2016-12-02 01:12:17 UTC
Looks not enough.  Even with the patch, I got a similar ICE for lto case
in testsuite:

FAIL: gcc.dg/atomic/stdatomic-op-1.c   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects  (internal compiler error)

gcc.log says

/exp/ldroot/dodes/ORIG/trunk/gcc/testsuite/gcc.dg/atomic/stdatomic-op-1.c: In function 'main':
/exp/ldroot/dodes/ORIG/trunk/gcc/testsuite/gcc.dg/atomic/stdatomic-op-1.c:341:1: error: invalid rtl sharing found in the insn
(insn 1527 292 294 23 (set (reg:SI 147 t)
        (eq:SI (subreg:SI (reg:QI 559) 0)
            (const_int 0 [0]))) "/exp/ldroot/dodes/ORIG/trunk/gcc/testsuite/gcc.dg/atomic/stdatomic-op-1.c":94 -1
     (nil))
/exp/ldroot/dodes/ORIG/trunk/gcc/testsuite/gcc.dg/atomic/stdatomic-op-1.c:341:1: error: shared rtx
(subreg:SI (reg:QI 559) 0)
/exp/ldroot/dodes/ORIG/trunk/gcc/testsuite/gcc.dg/atomic/stdatomic-op-1.c:341:1: internal compiler error: internal consistency failure
0x829cca7 verify_rtx_sharing
	../../ORIG/trunk/gcc/emit-rtl.c:2743

and it went away with the patch below.

diff --git a/config/sh/sh.md b/config/sh/sh.md
index c6956a0..667a9a5 100644
--- a/config/sh/sh.md
+++ b/config/sh/sh.md
@@ -607,7 +607,7 @@
 	    {
 	      if (dump_file)
 		fprintf (dump_file, "tstsi_t: converting to cmpeqsi_t\n");
-	      emit_insn (gen_cmpeqsi_t (eop0.use_as_extended_reg (curr_insn),
+	      emit_insn (gen_cmpeqsi_t (copy_rtx(eop0.use_as_extended_reg (curr_insn)),
 					const0_rtx));
 	      DONE;
 	    }
Comment 3 Richard Biener 2016-12-02 08:41:05 UTC
Likely latent on release branches.
Comment 4 Kazumoto Kojima 2016-12-03 11:48:19 UTC
Now I can't reproduce the ICEs for the revision 243217.  They failed
for the revision 243091.  I'm not sure whether the issue is false
positive or not.  I'd like to keep this PR for a while.
Comment 5 Oleg Endo 2016-12-04 13:27:30 UTC
(In reply to Kazumoto Kojima from comment #1)
> Here is a trial patch
> 
> diff --git a/config/sh/sh.md b/config/sh/sh.md
> index c6956a0..c83bf08 100644
> --- a/config/sh/sh.md
> +++ b/config/sh/sh.md
> @@ -858,7 +858,8 @@
>  	 operands of the tstsi_t insn, which is generally the case.  */
>        if (dump_file)
>  	fprintf (dump_file, "cmpeqsi_t: replacing with tstsi_t\n");
> -      emit_insn (gen_tstsi_t (XEXP (op.set_src, 0), XEXP (op.set_src, 1)));
> +      emit_insn (gen_tstsi_t (copy_rtx (XEXP (op.set_src, 0)),
> +			      XEXP (op.set_src, 1)));
>        DONE;
>      }
>  
> Oleg, could you take a look at the issue?  Although the patch fixes
> the ICE, I'm not sure if it's OK and enough.

Hm ... if this is a latent bug, I would like to understand why it's wrong to reference a reg rtx in the insn input operands.  I've been doing that in several places in similar ways...
Comment 6 Kazumoto Kojima 2016-12-05 00:39:16 UTC
I've tried a bi-sect on git tree.  The ICE went away with

commit c95f3fa2db12f22bbb2158d18c95f6714b4292b8
Author: krebbel <krebbel@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Fri Dec 2 08:32:40 2016 +0000

    Do not simplify "(and (reg) (const bit)" to if_then_else.

which corresponds to

2016-12-02  Dominik Vogt  <vogt@linux.vnet.ibm.com>
    
        * combine.c (combine_simplify_rtx):  Suppress replacement of
        "(and (reg) (const_int bit))" with "if_then_else".

on svn.  The ICE pops up again with reverting it on HAED.
I don't get what was going on without it, though.
Comment 7 Oleg Endo 2016-12-05 13:32:39 UTC
Maybe it's this thread?
https://gcc.gnu.org/ml/gcc-patches/2016-12/msg00334.html
Comment 8 Dominik Vogt 2016-12-05 13:44:09 UTC
There's a typo in the patch.  Should be reverted in a minute.  Sorry for the trouble.
Comment 9 Dominik Vogt 2016-12-05 14:03:00 UTC
The faulty patch has been reverted in r243256.
Comment 10 Kazumoto Kojima 2016-12-05 22:45:46 UTC
Ah, don't mind.  Your patch accidentally hides this PR.  Now
the SH build failure comes back on trunk :-)
Comment 11 Kazumoto Kojima 2016-12-07 00:52:15 UTC
Created attachment 40271 [details]
reduce testcase

With -O1, sh4-linux compiler makes insns

(insn 67 150 165 5 (set (reg:SI 239)
        (and:SI (subreg:SI (reg/v:DI 163 [ high ]) 0)
            (const_int 1 [0x1]))) "test.c":34 88 {*andsi_compact}
     (nil))
(insn 165 67 74 5 (set (reg:SI 147 t)
        (eq:SI (and:SI (subreg:SI (reg/v:DI 163 [ high ]) 0)
                (const_int 1 [0x1]))
            (const_int 0 [0]))) "test.c":34 -1
     (nil))

and two (subreg:SI (reg/v:DI 163 [ high ]) 0) are shared.

emit-rtl.c:verify_rtl_sharing calls verify_insn_sharing for these
insns and verify_insn_sharing marks rtxes with verify_rtx_sharing.
SUBREG rtx is always marked as used with verify_rtx_sharing.  Then
the second subreg is reported as erroneous because it's already
marked as used.  I think that this is a false positive.  It seems
to me that SUBREG should be handled specially.

diff --git a/gcc/emit-rtl.c b/gcc/emit-rtl.c
index 4650540..4fa4773 100644
--- a/gcc/emit-rtl.c
+++ b/gcc/emit-rtl.c
@@ -2714,6 +2714,9 @@ verify_rtx_sharing (rtx orig, rtx insn)
     case SCRATCH:
       /* SCRATCH must be shared because they represent distinct values.  */
       return;
+    case SUBREG:
+      verify_rtx_sharing (SUBREG_REG (x), insn);
+      return;
     case CLOBBER:
       /* Share clobbers of hard registers (like cc0), but do not share pseudo reg
          clobbers or clobbers of hard registers that originated as pseudos.

I could be totally wrong about that, though.
Comment 12 Kazumoto Kojima 2016-12-07 02:13:26 UTC
Hmm...  From
https://gcc.gnu.org/onlinedocs/gccint/Sharing.html#Sharing
the above patch looks wrong.  Perhaps the splitter in problem
might have to take care of subreg case even when referencing
a reg rtx in the input operands.
Comment 13 Oleg Endo 2016-12-07 12:09:17 UTC
(In reply to Kazumoto Kojima from comment #12)
> Perhaps the splitter in problem
> might have to take care of subreg case even when referencing
> a reg rtx in the input operands.

So it looks like a new rtx object should be created when referencing a subreg.  That's ... a problem.  I have been ignoring this for a while, so I guess there are several places which need patching.  I can reproduce the error on sh-elf.  I will  look at it, but I'm very busy at the moment, so it will take some time.
Comment 14 Kazumoto Kojima 2016-12-07 13:03:56 UTC
No problem.  I can continue nightly build&test with a minimal change

diff --git a/gcc/config/sh/sh.md b/gcc/config/sh/sh.md
index c6956a0..edadba7 100644
--- a/gcc/config/sh/sh.md
+++ b/gcc/config/sh/sh.md
@@ -858,7 +858,8 @@
 	 operands of the tstsi_t insn, which is generally the case.  */
       if (dump_file)
 	fprintf (dump_file, "cmpeqsi_t: replacing with tstsi_t\n");
-      emit_insn (gen_tstsi_t (XEXP (op.set_src, 0), XEXP (op.set_src, 1)));
+      emit_insn (gen_tstsi_t (copy_rtx (XEXP (op.set_src, 0)),
+			      copy_rtx (XEXP (op.set_src, 1))));
       DONE;
     }
 
added to my local git tree so as not to miss other breakages on trunk.
Comment 15 Joseph S. Myers 2017-01-11 18:41:47 UTC
*** Bug 79063 has been marked as a duplicate of this bug. ***
Comment 16 Jakub Jelinek 2017-01-11 20:27:46 UTC
When a fix exists, why hasn't it been posted to gcc-patches?
Comment 17 Oleg Endo 2017-01-12 13:12:01 UTC
(In reply to Jakub Jelinek from comment #16)
> When a fix exists, why hasn't it been posted to gcc-patches?

Because, like I wrote in comment #13, I would like to check if there might be a better fix for the problem.  If not, we will commit the patch in comment #11 for GCC 7.
Comment 18 Jakub Jelinek 2017-01-12 15:19:46 UTC
If the original insn that has the op.set_src rtx in it is not removed by the splitter (which it seems it is not), and it is just expected to be removed during DCE later, then doing copy_rtx is the right thing to do there, sharing non-shareable RTXes between multiple instructions, even when it is just for a couple of passes, is invalid.
Comment 19 Kazumoto Kojima 2017-01-17 04:08:24 UTC
Author: kkojima
Date: Tue Jan 17 04:07:51 2017
New Revision: 244516

URL: https://gcc.gnu.org/viewcvs?rev=244516&root=gcc&view=rev
Log:
	PR target/78633
	* config/sh/sh.md (cmpeqsi_t+1): Call copy_rtx to avoid invalid
	RTL sharing.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/config/sh/sh.md
Comment 20 Kazumoto Kojima 2017-01-17 04:16:38 UTC
I've applied a quick fix.  I'd like to keep this open for further
checks.
Comment 21 Jakub Jelinek 2017-05-02 15:56:28 UTC
GCC 7.1 has been released.
Comment 22 Richard Biener 2018-01-25 08:26:39 UTC
GCC 7.3 is being released, adjusting target milestone.
Comment 23 Richard Biener 2019-11-14 07:57:39 UTC
The GCC 7 branch is being closed, re-targeting to GCC 8.4.
Comment 24 Jakub Jelinek 2020-03-04 09:38:46 UTC
GCC 8.4.0 has been released, adjusting target milestone.
Comment 25 Jakub Jelinek 2021-05-14 09:48:37 UTC
GCC 8 branch is being closed.
Comment 26 Richard Biener 2021-06-01 08:08:30 UTC
GCC 9.4 is being released, retargeting bugs to GCC 9.5.
Comment 27 Richard Biener 2022-05-27 09:36:50 UTC
GCC 9 branch is being closed
Comment 28 Jakub Jelinek 2022-06-28 10:32:47 UTC
GCC 10.4 is being released, retargeting bugs to GCC 10.5.
Comment 29 Richard Biener 2023-07-07 10:31:50 UTC
GCC 10 branch is being closed.
Comment 30 Richard Biener 2024-07-19 12:59:36 UTC
GCC 11 branch is being closed.