Bug 71452 - [4.9/5 Regression] Wrong optimization of stores to _Bool via char*
Summary: [4.9/5 Regression] Wrong optimization of stores to _Bool via char*
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: 4.9.4
Assignee: Richard Biener
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2016-06-08 07:53 UTC by Alexander Cherepanov
Modified: 2016-07-07 11:49 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work: 4.5.4, 5.4.1, 6.2.0, 7.0
Known to fail: 4.6.0, 4.9.3, 5.4.0, 6.1.0
Last reconfirmed: 2016-06-08 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Cherepanov 2016-06-08 07:53:52 UTC
Source code:

----------------------------------------------------------------------
#include <stdio.h>

int main()
{
  _Bool b;
  *(char *)&b = 123;
  printf("%d\n", *(char *)&b);
}
----------------------------------------------------------------------

Results:

----------------------------------------------------------------------
$ gcc -std=c11 -pedantic -Wall -Wextra test.c && ./a.out
123

$ gcc -std=c11 -pedantic -Wall -Wextra -O3 test.c && ./a.out
1
----------------------------------------------------------------------

gcc version: gcc (GCC) 7.0.0 20160604 (experimental)

This is somewhat counterintuitive behavior and it's a bug according to https://gcc.gnu.org/ml/gcc/2016-06/msg00046.html .

According to https://gcc.godbolt.org/ such optimization is observed with gcc 4.6.4 but not in gcc 4.5.3.
Comment 1 Richard Biener 2016-06-08 08:10:50 UTC
I have a patch.
Comment 2 Richard Biener 2016-06-08 13:11:58 UTC
Fixed on trunk sofar.
Comment 3 Richard Biener 2016-06-08 13:12:15 UTC
Author: rguenth
Date: Wed Jun  8 13:11:43 2016
New Revision: 237214

URL: https://gcc.gnu.org/viewcvs?rev=237214&root=gcc&view=rev
Log:
2016-06-08  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/71452
	* tree-ssa.c (non_rewritable_lvalue_p): Make sure that the
	type used for the SSA rewrite has enough precision to cover
	the dynamic type of the location.

	* gcc.dg/torture/pr71452.c: New testcase.

Added:
    trunk/gcc/testsuite/gcc.dg/torture/pr71452.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa.c
Comment 4 Richard Biener 2016-07-07 07:31:04 UTC
Author: rguenth
Date: Thu Jul  7 07:30:04 2016
New Revision: 238077

URL: https://gcc.gnu.org/viewcvs?rev=238077&root=gcc&view=rev
Log:
2016-07-07  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2016-05-25  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/71264
	* tree-vect-stmts.c (vect_init_vector): Properly deal with
	vector type val.

	* gcc.dg/vect/pr71264.c: New testcase.

	2016-06-07  Richard Biener  <rguenther@suse.de>

	PR middle-end/71423
	* match.pd ((X | ~Y) -> Y <= X): Properly invert the comparison
	for signed ops.

	* gcc.dg/torture/pr71423.c: New testcase.

	2016-06-14  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/71521
	* tree-vrp.c (extract_range_from_binary_expr_1): Guard
	division int_const_binop against zero divisor.

	* gcc.dg/tree-ssa/vrp101.c: New testcase.

	2016-06-08  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/71452
	* tree-ssa.c (non_rewritable_lvalue_p): Make sure that the
	type used for the SSA rewrite has enough precision to cover
	the dynamic type of the location.

	* gcc.dg/torture/pr71452.c: New testcase.

	2016-06-14  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/71522
	* tree-ssa.c (non_rewritable_lvalue_p): Do not rewrite non-float
	copying into float copying.

	* gcc.dg/torture/pr71522.c: New testcase.

Added:
    branches/gcc-6-branch/gcc/testsuite/g++.dg/torture/pr71452.C
    branches/gcc-6-branch/gcc/testsuite/gcc.dg/torture/pr71423.c
    branches/gcc-6-branch/gcc/testsuite/gcc.dg/torture/pr71452.c
    branches/gcc-6-branch/gcc/testsuite/gcc.dg/torture/pr71522.c
    branches/gcc-6-branch/gcc/testsuite/gcc.dg/tree-ssa/vrp101.c
    branches/gcc-6-branch/gcc/testsuite/gcc.dg/vect/pr71264.c
Modified:
    branches/gcc-6-branch/gcc/ChangeLog
    branches/gcc-6-branch/gcc/match.pd
    branches/gcc-6-branch/gcc/testsuite/ChangeLog
    branches/gcc-6-branch/gcc/tree-ssa.c
    branches/gcc-6-branch/gcc/tree-vect-stmts.c
    branches/gcc-6-branch/gcc/tree-vrp.c
Comment 5 Richard Biener 2016-07-07 10:33:33 UTC
Author: rguenth
Date: Thu Jul  7 10:33:01 2016
New Revision: 238084

URL: https://gcc.gnu.org/viewcvs?rev=238084&root=gcc&view=rev
Log:
2016-07-07  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2016-06-07  Richard Biener  <rguenther@suse.de>

	PR middle-end/71423
	* match.pd ((X | ~Y) -> Y <= X): Properly invert the comparison
	for signed ops.

	* gcc.dg/torture/pr71423.c: New testcase.

	2016-06-08  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/71452
	* tree-ssa.c (non_rewritable_lvalue_p): Make sure that the
	type used for the SSA rewrite has enough precision to cover
	the dynamic type of the location.

	* gcc.dg/torture/pr71452.c: New testcase.

	2016-06-14  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/71522
	* tree-ssa.c (non_rewritable_lvalue_p): Do not rewrite non-float
	copying into float copying.

	* gcc.dg/torture/pr71522.c: New testcase.

Added:
    branches/gcc-5-branch/gcc/testsuite/g++.dg/torture/pr71452.C
    branches/gcc-5-branch/gcc/testsuite/gcc.dg/torture/pr71423.c
    branches/gcc-5-branch/gcc/testsuite/gcc.dg/torture/pr71452.c
    branches/gcc-5-branch/gcc/testsuite/gcc.dg/torture/pr71522.c
Modified:
    branches/gcc-5-branch/gcc/ChangeLog
    branches/gcc-5-branch/gcc/match.pd
    branches/gcc-5-branch/gcc/testsuite/ChangeLog
    branches/gcc-5-branch/gcc/tree-ssa.c
Comment 6 Richard Biener 2016-07-07 11:46:40 UTC
Author: rguenth
Date: Thu Jul  7 11:46:08 2016
New Revision: 238087

URL: https://gcc.gnu.org/viewcvs?rev=238087&root=gcc&view=rev
Log:
2016-07-07  Richard Biener  <rguenther@suse.de>

	Backport from mainline
	2016-04-04  Richard Biener  <rguenther@suse.de>

	PR rtl-optimization/70484
	* rtl.h (canon_output_dependence): Declare.
	* alias.c (canon_output_dependence): New function.
	* dse.c (record_store): Use canon_output_dependence rather
	than canon_true_dependence.

	* gcc.dg/torture/pr70484.c: New testcase.

	2016-06-08  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/71452
	* tree-ssa.c (non_rewritable_lvalue_p): Make sure that the
	type used for the SSA rewrite has enough precision to cover
	the dynamic type of the location.

	* gcc.dg/torture/pr71452.c: New testcase.

	2016-05-06  Richard Biener  <rguenther@suse.de>

	PR middle-end/70931
	* dwarf2out.c (native_encode_initializer): Skip zero-sized fields.

	* gfortran.dg/pr70931.f90: New testcase.

	2016-03-01  Richard Biener  <rguenther@suse.de>

	PR middle-end/70022
	* fold-const.c (fold_indirect_ref_1): Fix range checking for
	vector BIT_FIELD_REF extract.

	* gcc.dg/pr70022.c: New testcase.

Added:
    branches/gcc-4_9-branch/gcc/testsuite/g++.dg/torture/pr71452.C
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/pr70022.c
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/torture/pr70484.c
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/torture/pr71452.c
    branches/gcc-4_9-branch/gcc/testsuite/gfortran.dg/pr70931.f90
Modified:
    branches/gcc-4_9-branch/gcc/ChangeLog
    branches/gcc-4_9-branch/gcc/alias.c
    branches/gcc-4_9-branch/gcc/dse.c
    branches/gcc-4_9-branch/gcc/dwarf2out.c
    branches/gcc-4_9-branch/gcc/fold-const.c
    branches/gcc-4_9-branch/gcc/rtl.h
    branches/gcc-4_9-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_9-branch/gcc/tree-ssa.c
Comment 7 Richard Biener 2016-07-07 11:49:08 UTC
Fixed.