https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111875
--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> --- CCP propagates the alignment here.(In reply to Filip Kastl from comment #1) > I found out that this is caused by the copy_prop pass. With -Og, an instance > of copy_prop runs after the fold_builtins pass but before the sanopt pass. > The fold_builtins pass changes the statement p_2 = > __builtin_assume_aligned(p_1, 4) to p_2 = p_1; and changes the alignment of > p_2 to 32 bits. However the alignment of p_1 remains 8 bits so when > copy_prop propagates all occurences of p_2 to instead be occurences of p_1, > the information about alignment is lost. When the sanopt pass runs, it > decides that casting p to (int *) possibly creates UB. > > I see a few possible solutions: > - Stop copy prop from propagating through assignments where the alignments > differ > - Modify copy prop to use the alignment information of the lhs ssa name when > propagating through similar assignment statements > - Modify fold_builtins to copy propagate in similar cases > - Modify fold_builtins to also set alignment of the rhs ssa name when > removing __builtin_assume_aligned in similar cases I think in general none of those work. IIRC the copyprop pass was put there specifically as a "cheap" way to propagate constants exposed by pass_fold_builtins. git blame might tell - there was the alternative to perform this propagation in fold_builtins but it's difficult to be "complete" there. The alternative would be to turn that into a proper simple constant propagation pass. Not sure if all worth for -Og just because of sanopt though.