https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64601
--- Comment #8 from Marc Glisse <glisse at gcc dot gnu.org> --- With the modified hack below, the testsuite passes, except: gcc.dg/tree-ssa/scev-[34].c those go from xfail to pass, that sounds good g++.dg/tree-ssa/pr31146.C seems that the scan-tree-dump is too strict c-c++-common/ubsan/object-size-9.c for some reason it prints <memory cannot be printed> instead of a bunch of 0s but I can't reproduce outside the dg framework, doesn't seem very important. I had to add a couple checks compared to the first version. Giving up on volatile operands, sure, there is probably a better way but whatever. Forwprop tends to feed artificial &MEM_REF[(void*)...] to this function in many cases (POINTER_PLUS_EXPR in particular), which causes a number of issues (including, surprisingly, assuming too strong alignment). Testing for ptr_type_node is a hack, but the necessity for it is just an artifact of placing the transformation there, it shouldn't be necessary if I put it elsewhere. The transformation is triggered by dereferences, and compares the types of objects, not pointers, so it isn't obvious that the issues you were describing apply to it. --- tree-ssa-forwprop.c (revision 219694) +++ tree-ssa-forwprop.c (working copy) @@ -777,20 +777,49 @@ forward_propagate_addr_expr_1 (tree name new_def_rhs); else if (is_gimple_min_invariant (new_def_rhs)) gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs); else return false; gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt); update_stmt (use_stmt); return true; } + /* *&X -> X. */ + if (TREE_CODE (lhs) == MEM_REF + && !gimple_has_volatile_ops (use_stmt) + && TREE_OPERAND (lhs, 0) == name + && integer_zerop (TREE_OPERAND (lhs, 1)) + && (TREE_CODE (TREE_OPERAND (def_rhs, 0)) != MEM_REF + || TREE_TYPE (TREE_OPERAND (TREE_OPERAND (def_rhs, 0), 1)) + != ptr_type_node) + && useless_type_conversion_p (TREE_TYPE (lhs), + TREE_TYPE (TREE_OPERAND (def_rhs, 0)))) + { + gimple_assign_set_lhs (use_stmt, unshare_expr (TREE_OPERAND (def_rhs, 0))); + tidy_after_forward_propagate_addr (use_stmt); + return true; + } + else if (TREE_CODE (rhs) == MEM_REF + && !gimple_has_volatile_ops (use_stmt) + && TREE_OPERAND (rhs, 0) == name + && integer_zerop (TREE_OPERAND (rhs, 1)) + && (TREE_CODE (TREE_OPERAND (def_rhs, 0)) != MEM_REF + || TREE_TYPE (TREE_OPERAND (TREE_OPERAND (def_rhs, 0), 1)) + != ptr_type_node) + && useless_type_conversion_p (TREE_TYPE (rhs), + TREE_TYPE (TREE_OPERAND (def_rhs, 0)))) + { + gimple_assign_set_rhs1 (use_stmt, unshare_expr (TREE_OPERAND (def_rhs, 0))); + tidy_after_forward_propagate_addr (use_stmt); + return true; + } /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS. ADDR_EXPR will not appear on the LHS. */ tree *lhsp = gimple_assign_lhs_ptr (use_stmt); while (handled_component_p (*lhsp)) lhsp = &TREE_OPERAND (*lhsp, 0); lhs = *lhsp; /* Now see if the LHS node is a MEM_REF using NAME. If so, propagate the ADDR_EXPR into the use of NAME and fold the result. */ if (TREE_CODE (lhs) == MEM_REF