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.
I have a patch.
Fixed on trunk sofar.
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
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
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
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
Fixed.