On Tue, Dec 6, 2011 at 11:24 PM, Eric Botcazou <ebotca...@adacore.com> wrote: > Hi, > > this is a regression present on mainline and 4.6 branch at -O for the SPARC. > The compiler generates an unaligned access for the memcpy call in: > > char *s > > union > { > int32_t i; > char a[sizeof (int32_t)]; > } > v; > > memcpy (v.a, s, sizeof (int32_t)); > > The memcpy call is folded to an assignment between a couple of MEM_REFs: > > MEM[(char * {ref-all})&v] = MEM[(char * {ref-all})s_1]; > > with type 'char a[4]'. The problem is that SRA turns this into: > > v$i_27 = MEM[(char * {ref-all})s_1].i; > > but s isn't aligned enough for an int32_t access. > > > I don't think we can scalarize in this case on strict-alignment targets. The > SRA pass already contains an alignment check, but it is purely internal to the > RHS and this is clearly not sufficient anymore with MEM_REFs. > > The proposed fix is to enhance this alignment check to take into account both > the RHS and the LHS (much like the memcpy folder). I think it subsumes the > previous check, which could be viewed as a check involving the RHS and the > type of the LHS. But there is a hitch: get_object_alignment is conservative > for MEM_REF (unlike for INDIRECT_REF) so a trick is needed in order not to > pessimize in some cases. > > Tested on SPARC/Solaris. OK for mainline and 4.6 branch?
You are basically (but non-optimally) locally re-implementing what expr.c:get_object_or_type_alignment does, for the bare MEM_REF case (you don't consider offsets that do not change the alignment, nor alignment information present on the SSA name). Note that in expr.c gete_object_or_type_alignment is only called on [TARGET_]MEM_REFs (I didn't export that function exactly because of its restrictions, if we want to export it we should assert it is only called for [TARGET_]MEM_REFs). Thus, apart from the exp_align computation the patch looks ok (it's only non-optimal, not incorrect). Thanks, Richard. > > 2011-12-06 Eric Botcazou <ebotca...@adacore.com> > > PR tree-optimization/51315 > * tree-sra.c (tree_non_mode_aligned_mem_p): Rename to... > (tree_non_aligned_mem_p): ...this. Add ALIGN parameter. Look into > MEM_REFs and deal with simple dereferences specially. > (build_accesses_from_assign): Adjust for above change. > (access_precludes_ipa_sra_p): Likewise. > > > 2011-12-06 Eric Botcazou <ebotca...@adacore.com> > > * gcc.c-torture/execute/20111206-1.c: New test. > > > -- > Eric Botcazou