This fixes the performance problem in PR27740. Also this patch includes code for a compile time opportunity in BasicAA.
Problem in 27740:
for the following pair :
%f35.pre-phi.i = phi float* [ %.pre6.i, %entry.if.else_crit_edge.i ], [ %.pre6.i, %land.lhs.true.if.else_crit_edge.i ]
%f11 = getelementptr inbounds %struct.s, %struct.s* %p1, i64 0, i32 1
where:
%.pre6.i = getelementptr inbounds %struct.s, %struct.s* %p1, i64 0, i32 3
We cannot prove that the pair is not aliased. This is because BasicAA first goes to aliasGEP, which recursively calls aliasCheck to look at aliasing for the base pointer of the GEP (with unknown size) and the phi node in the original pair. The result of that is a MayAlias. A number of alternative solutions have been discussed in this comment and also discussed with Hal.
The solution here, focuses on PHIs with a unique incoming value. This is very similar to what we do in stripPointerCasts, for GEP and some other cases, where we go past the current instruction, when all it does is hiding a pointer. I think potentially this functionality can be added to stripPointerCases, to look beyond phi nodes, but at this point I want to make this change limited, given the long conversations and investigations that we had about 27740.
Also, some of the recursive calls to aliasCheck, from alias[GEP|Select|PHI] pass one of the values for which GetUnderlyingObject is already called, again. So if we keep the underlying object that is already calculated around, we won't need to recompute it, given this is an expensive call.
I will add a test, by reducing the test case of 27740 and add to this.
Because GetUnderlyingObject is depth limited, as we walk up the use/def chain, we get closer to the real underlying object (in those somewhat-rare cases where we actually hit the depth cutoff). I'm quite happy to not do redundant work here, but we also don't want to regress on complex code either. I recommend trying something like this: