Bug 70263 - [5 Regression] ICE at -O1 and above in both 32-bit and 64-bit modes on x86_64-linux-gnu (segmentation fault)
Summary: [5 Regression] ICE at -O1 and above in both 32-bit and 64-bit modes on x86_64...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 6.0
: P2 normal
Target Milestone: 6.0
Assignee: Jeffrey A. Law
URL:
Keywords: ice-on-valid-code, ra
Depends on:
Blocks:
 
Reported: 2016-03-17 00:19 UTC by Chengnian Sun
Modified: 2017-11-03 16:38 UTC (History)
4 users (show)

See Also:
Host:
Target: x86_64-*-*
Build:
Known to work: 4.8.5
Known to fail:
Last reconfirmed: 2016-03-17 00:00:00


Attachments
possible fix (458 bytes, patch)
2016-03-18 13:03 UTC, Alan Modra
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chengnian Sun 2016-03-17 00:19:27 UTC
The following code crashes the trunk of gcc at -O1 and above in both 32-bit and 64-bit modes on x86_64-linux-gnu. 

This bug also affects gcc-4.9 and later versions. 


$: gcc-trunk -v
Using built-in specs.
COLLECT_GCC=gcc-trunk
COLLECT_LTO_WRAPPER=/usr/local/gcc-trunk/libexec/gcc/x86_64-pc-linux-gnu/6.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-source-trunk/configure --enable-languages=c,c++,lto --prefix=/usr/local/gcc-trunk --disable-bootstrap
Thread model: posix
gcc version 6.0.0 20160316 (experimental) [trunk revision 234258] (GCC) 
$: 
$: gcc-trunk -O1 small.c
small.c: In function ‘fn1’:
small.c:10:1: internal compiler error: Segmentation fault
 }
 ^
0xb520ff crash_signal
        ../../gcc-source-trunk/gcc/toplev.c:335
0x996b1d memref_used_between_p
        ../../gcc-source-trunk/gcc/ira.c:3237
0x996b1d update_equiv_regs
        ../../gcc-source-trunk/gcc/ira.c:3654
0x998293 ira
        ../../gcc-source-trunk/gcc/ira.c:5219
0x998293 execute
        ../../gcc-source-trunk/gcc/ira.c:5549
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <http://gcc.gnu.org/bugs.html> for instructions.
$: 
$: cat small.c
int a[91];
int b, c;
void fn1() {
  int n, m;
  do {
    a[c--];
    a[--c] = m;
    a[--m] = b;
  } while (n);
}

int main() {}
$:
Comment 1 Richard Biener 2016-03-17 09:18:42 UTC
Confirmed.
Comment 2 Marek Polacek 2016-03-17 10:21:13 UTC
Started with r203895:

commit 4bc0f16ebd21b15d624ebbf8efa88c9c2f1df367
Author: law <law@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Mon Oct 21 15:25:09 2013 +0000

           * tree-ssa-threadedge.c (thread_through_normal_block): New argument VISITED.
            Remove VISISTED as a local variable.  When we have a threadable jump, verify
            the destination of the jump has not been visised.
            (thread_across_edge): Allocate VISITED bitmap once at function scope and
            use it throughout.  Make sure to set appropriate bits in VISITED for E (start
            of jump thread path).
    
            * tree-ssa-threadupdate.c (mark_threaded_blocks): Reject threading through
            a joiner if any edge on the path has a recorded jump thread.
Comment 3 Jeffrey A. Law 2016-03-17 14:53:10 UTC
Had to exposed some kind of latent bug in IRA.  I'll try to take a look shortly.
Comment 4 Jeffrey A. Law 2016-03-17 15:36:52 UTC
So the testcase has undefined behaviour, but obviously we shouldn't be crashing.

The key insns are:

(insn 17 16 18 2 (set (mem:SI (plus:DI (mult:DI (reg:DI 98 [ c_lsm.5 ])
                    (const_int 4 [0x4]))
                (symbol_ref:DI ("a") [flags 0x2] <var_decl 0x7ffff7ff6cf0 a>)) [1 a S4 A32])
        (reg/v:SI 93 [ m ])) j.c:7 86 {*movsi_internal}
     (expr_list:REG_DEAD (reg:DI 98 [ c_lsm.5 ])
        (nil)))
(insn 18 17 20 2 (parallel [
            (set (reg/v:SI 93 [ m ])
                (plus:SI (reg/v:SI 93 [ m ])
                    (const_int -1 [0xffffffffffffffff])))
            (clobber (reg:CC 17 flags))
        ]) j.c:8 218 {*addsi_1}
     (expr_list:REG_UNUSED (reg:CC 17 flags)
        (nil)))

Note that insn 17 comes before insn 18.

We're in this code:

     /* If this sets a MEM to the contents of a REG that is only used
         in a single basic block, see if the register is always equivalent
         to that memory location and if moving the store from INSN to the
         insn that set REG is safe.  If so, put a REG_EQUIV note on the
         initializing insn.

         Don't add a REG_EQUIV note if the insn already has one.  The existing
         REG_EQUIV is likely more useful than the one we are adding.

         If one of the regs in the address has reg_equiv[REGNO].replace set,
         then we can't add this REG_EQUIV note.  The reg_equiv[REGNO].replace
         optimization may move the set of this register immediately before
         insn, which puts it after reg_equiv[REGNO].init_insns, and hence
         the mention in the REG_EQUIV note would be to an uninitialized
         pseudo.  */


We make the following call:   
           && ! memref_used_between_p (dest, init_insn, insn)

Where init_insn 18 and insn is 17.  memref_used_between_p is a forward walk, so not surprisingly bad things happen when the 3rd argument comes before the 2nd argument.  This situation is a direct result of the undefined behaviour in the testcase.

From looking at the code, I think it's easiest to just bulletproof memref_used_between_p.  Another approach would be to track the insns we've seen in the enclosing loop and if INSN is one of them, then assume we can't create the REG_EQUIV note.

Either should solve this problem.
Comment 5 Alan Modra 2016-03-18 12:29:18 UTC
Why is reg_equiv[93].init_insns pointing at insn 18 in the first place?  Surely insn 18, a decrement of reg 93, gives no useful initialization of reg 93?
Comment 6 Alan Modra 2016-03-18 13:03:10 UTC
Created attachment 38017 [details]
possible fix

Maybe something as simple as this?
Comment 7 Jeffrey A. Law 2016-03-18 16:43:16 UTC
In all the cases where we can have the store before the equiv we're in the realm of undefined behavior AFAICT.  But it's not clear to me that the equiv will always be a self-referencing.

So your patch will almost certainly fix this instance, but I'm not sure it'll fix it in the more general case.
Comment 8 Jeffrey A. Law 2016-03-18 19:30:52 UTC
Author: law
Date: Fri Mar 18 19:30:20 2016
New Revision: 234344

URL: https://gcc.gnu.org/viewcvs?rev=234344&root=gcc&view=rev
Log:
	PR rtl-optimization/70263
	* ira.c (memref_used_between_p): Assert we found END in the insn chain.
	(update_equiv_regs): When trying to move a store to after the insn
	that sets the source of the store, make sure the store occurs after
	the insn that sets the source of the store.  When successful note
	the REG_EQUIV note created in the dump file.

	PR rtl-optimization/70263
	* gcc.c-torture/compile/pr70263-1.c: New test.
	* gcc.target/i386/pr70263-2.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/compile/pr70263-1.c
    trunk/gcc/testsuite/gcc.target/i386/pr70263-2.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/ira.c
    trunk/gcc/testsuite/ChangeLog
Comment 9 Richard Biener 2016-08-03 11:56:48 UTC
GCC 4.9 branch is being closed
Comment 10 Jakub Jelinek 2017-10-10 14:51:15 UTC
GCC 5 branch has been closed, should be fixed in GCC 6 and later.
Comment 11 uros 2017-11-03 16:38:10 UTC
Author: uros
Date: Fri Nov  3 16:37:39 2017
New Revision: 254385

URL: https://gcc.gnu.org/viewcvs?rev=254385&root=gcc&view=rev
Log:
	PR testsuite/82828
	PR rtl-optimization/70263
	* gcc.target/i386/pr70263-2.c: Fix invalid testcase.


Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.target/i386/pr70263-2.c