When combining the indices of GEP chains, we currently wait until the source of the chain has been simplified before simplifying its users. For example, when trying to fold %tmp1 into %tmp2 in the code below, we give up so that we can try to fold %tmp0 into %tmp1 first.
%tmp0 = getelementptr %pair, %pair* %p, i64 %i %tmp1 = getelementptr %pair, %pair* %tmp0, i64 1 %tmp2 = getelementptr %pair, %pair* %tmp1, i64 0, i32 1
However if the %tmp0 - %tmp1 combine is unsuccessful, we will never actually return to try and simplify %tmp2, even though we can.
This patch causes us to always perform the zero-index simplification useful for this example. With this patch, we will simplify the above code to:
%tmp0 = getelementptr %pair, %pair* %p, i64 %i %tmp2 = getelementptr %pair, %pair* %tmp0, i64 1, i32 1
Hey Eli,
The single-use check is good enough for my purposes. But if we do that, I think it makes more sense to just guard the bail out check here with !Src->hasOneUse(). This will enable the full combine instead of just the zero simplification. So if GEP is the only user of Src, we go ahead with the combine and Src will be eliminated; otherwise, bail out and wait for Src to be simplified first.
I will test this approach and update the patch with the results.