The following fixes another alignment preserving bug in folding. Bootstrap / regtest running on x86_64-unknown-linux-gnu.
Richard. 2016-06-10 Richard Biener <rguent...@suse.de> PR middle-end/64516 * fold-const.c (fold_unary_loc): Preserve alignment when folding a VIEW_CONVERT_EXPR into a MEM_REF. * gcc.dg/align-3.c: New testcase. Index: gcc/fold-const.c =================================================================== *** gcc/fold-const.c (revision 237286) --- gcc/fold-const.c (working copy) *************** fold_unary_loc (location_t loc, enum tre *** 7964,7969 **** --- 7974,7981 ---- case VIEW_CONVERT_EXPR: if (TREE_CODE (op0) == MEM_REF) { + if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type)) + type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0))); tem = fold_build2_loc (loc, MEM_REF, type, TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1)); REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0); Index: gcc/testsuite/gcc.dg/align-3.c =================================================================== --- gcc/testsuite/gcc.dg/align-3.c (revision 0) +++ gcc/testsuite/gcc.dg/align-3.c (working copy) @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-rtl-expand" } */ + +typedef struct { char a[2]; } __attribute__((__packed__)) TU2; +unsigned short get16_unaligned(const void *p) { + unsigned short v; + *(TU2 *)(void *)(&v) = *(const TU2 *)p; + return v; +} + +/* { dg-final { scan-rtl-dump "MEM\[^\n\r\]*A8\\\]" "expand" } } */