Bug 78720 - [7 Regression] Illegal instruction in generated code
Summary: [7 Regression] Illegal instruction in generated code
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 7.0
: P1 normal
Target Milestone: 7.0
Assignee: Jakub Jelinek
URL:
Keywords: wrong-code
: 78769 (view as bug list)
Depends on:
Blocks: yarpgen
  Show dependency treegraph
 
Reported: 2016-12-07 19:35 UTC by Dmitry Babokin
Modified: 2021-11-01 23:07 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-12-07 00:00:00


Attachments
reproducer (348 bytes, application/x-gzip)
2016-12-07 19:35 UTC, Dmitry Babokin
Details
gcc7-pr78720.patch (968 bytes, patch)
2016-12-09 09:18 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dmitry Babokin 2016-12-07 19:35:01 UTC
Created attachment 40278 [details]
reproducer

> g++ -O2 -march=nehalem -o opt illegal_inst.cpp illegal_inst_const.cpp
> ./opt
Illegal instruction

This is regression, which was introduced in trunk couple days ago.

> gcc --version
gcc (GCC) 7.0.0 20161207 (experimental)
Comment 1 Markus Trippelsdorf 2016-12-07 20:14:02 UTC
 g++ -O2 -S illegal_inst.cpp -o - | c++filt

foo():
.LFB12:
        .cfi_startproc
        ud2
        .cfi_endproc
Comment 2 Markus Trippelsdorf 2016-12-07 20:18:19 UTC
 % cat illegal_inst.cpp
extern signed char var_13;
long int rrr;
void foo() {
  rrr = !0 % ((var_13 < 0) << 21);
}
Comment 3 Markus Trippelsdorf 2016-12-07 20:37:41 UTC
Started with r243255:

commit 458de25d72182951e17489052becf952f6b63592
Author: bonzini <bonzini@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Mon Dec 5 13:19:34 2016 +0000

    gcc:
    * match.pd: Simplify X ? C : 0 where C is a power of 2 and
    X tests a single bit.
Comment 4 Markus Trippelsdorf 2016-12-08 13:39:17 UTC
From:

void foo() ()                                                                                                                                                                      
{                                                                                                                                                                                  
  signed char var_13.0_1;                                                                                                                                                          
  signed char _2;                                                                                                                                                                  
  int _3;                                                                                                                                                                          
  int _4;                                                                                                                                                                          
  int _5;                                                                                                                                                                          
  long int _6;                                                                                                                                                                     
                                                                                                                                                                                   
  <bb 2> [0.0%]:                                                                                                                                                                   
  var_13.0_1 = var_13;                                                                                                                                                             
  _2 = var_13.0_1 >> -14;                                                                                                                                                          
  _3 = (int) _2;                                                                                                                                                                   
  _4 = _3 & 2097152;                                                                                                                                                               
  _5 = 1 % _4;                                                                                                                                                                     
  _6 = (long int) _5;                                                                                                                                                              
  rrr = _6;                                                                                                                                                                        
  return;                                                                                                                                                                          
                                                                                                                                                                                   
}

To (ccp1):

void foo() ()                                                                                                                                                                      
{                                                                                                                                                                                  
  signed char var_13.0_1;                                                                                                                                                          
  int _5;                                                                                                                                                                          
  long int _6;                                                                                                                                                                     
                                                                                                                                                                                   
  <bb 2> [0.0%]:                                                                                                                                                                   
  var_13.0_1 = var_13;                                                                                                                                                             
  _5 = 1 % 0;                                                                                                                                                                      
  _6 = (long int) _5;                                                                                                                                                              
  rrr = _6;                                                                                                                                                                        
  return;                                                                                                                                                                          
                                                                                                                                                                                   
}
Comment 5 Jakub Jelinek 2016-12-08 13:55:50 UTC
Already the >> -14 looks wrong.
Comment 6 Jakub Jelinek 2016-12-09 09:18:22 UTC
Created attachment 40284 [details]
gcc7-pr78720.patch

Untested fix.
Comment 7 Jakub Jelinek 2016-12-10 12:06:44 UTC
Author: jakub
Date: Sat Dec 10 12:06:12 2016
New Revision: 243516

URL: https://gcc.gnu.org/viewcvs?rev=243516&root=gcc&view=rev
Log:
	PR tree-optimization/78720
	* match.pd (A < 0 ? C : 0): Only optimize for signed A.  If shift
	is negative, sign extend to @1's type and than AND with C.

	* gcc.c-torture/execute/pr78720.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr78720.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/match.pd
    trunk/gcc/testsuite/ChangeLog
Comment 8 Jakub Jelinek 2016-12-10 12:08:00 UTC
Fixed.
Comment 9 Markus Trippelsdorf 2016-12-11 07:36:47 UTC
*** Bug 78769 has been marked as a duplicate of this bug. ***