Currently we use the phi-translated address when traversing the
definitions upwards. The translated address is then used to check for
clobbers along a path. If the Phi is part of a cycle, this is not
correct I think, as it means we only check for clobbers with the initial
value of the cycle. But it is possible that a definition along the path
clobbers an address other than the initial value of the cycle.
This patch just detects trivial cycles, as I want to make sure I am not
missing anything before generalizing.
Consider the example below. Without this patch, MemorySSA will set the
defining access of %lv = load i16, i16* %arrayidx, align 2, !tbaa !3
to LiveOnEntry, because it phi-translates the location of the load to
getelementptr inbounds [2 x i16], [2 x i16]* @c, i64 0, i64 1 in
%entry, but the loop body is executed twice and it loads from
getelementptr inbounds [2 x i16], [2 x i16]* @c, i64 0, i64 1 andj
getelementptr inbounds [2 x i16], [2 x i16]* @c, i64 0, i64 0. The
latter aliases the store in %entry.
define i32 @main(i32* noalias %ptr) { entry: %s1.ptr = getelementptr inbounds [2 x i16], [2 x i16]* @c, i64 0, i64 0 store i16 1, i16* %s1.ptr, align 2, !tbaa !3 br label %for.body for.body: %storemerge2 = phi i32 [ 1, %entry ], [ %dec, %for.body ] %idxprom1 = zext i32 %storemerge2 to i64 %arrayidx = getelementptr inbounds [2 x i16], [2 x i16]* @c, i64 0, i64 %idxprom1 %lv = load i16, i16* %arrayidx, align 2, !tbaa !3 %conv = sext i16 %lv to i32 store i32 %conv, i32* %ptr, align 4, !tbaa !7 %dec = add nsw i32 %storemerge2, -1 %cmp = icmp sgt i32 %storemerge2, 0 br i1 %cmp, label %for.body, label %for.end for.end: %s2.ptr = getelementptr inbounds [2 x i16], [2 x i16]* @c, i64 0, i64 0 store i16 0, i16* %s2.ptr, align 2, !tbaa !3 ret i32 0 }
Fixes PR46156.