<div dir="ltr">Coincidentally, I just ran into this same issue on some of our benchmarks for the NVPTX backend.  You have something like this before instcombine:<div><br></div><div>  %tmp = getelementptr inbounds i32, i32* %input, i64 %offset<br></div><div>loop:<br></div><div>  %loop_variant = ...</div><div>  %ptr = getelementptr inbounds i32, i32* %tmp, i64 %loop_variant</div><div><br></div><div>Which gets transformed to:</div><div><br></div><div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">loop:<br></div><!--
--><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">  %loop_variant = ...</div><div style="text-align:start;text-indent:0px">  %sum = add nsw i64 %loop_variant, %offset<br></div><div style="text-align:start;text-indent:0px">  %ptr = getelementptr inbounds i32, i32* %input, i64 %sum<br></div><div style="text-align:start;text-indent:0px"><br></div><div style="text-align:start;text-indent:0px">The merge essentially reassociates the loop-variant term (%loop_variant) and loop-invariant terms (%input and %offset) in such a way that LICM can't remove it.<br></div></div><div><div><br></div><div>One idea is to only perform this style of gep merge if at least one of the following conditions is true:</div><div>(1) both index terms in the GEP  are constant.  In this case<!--
--> no new add instruction is created, instead the constants are folded.</div><div>(2) the GEPs are in the same BB.</div><div>(3) LoopInfo is available, and we know we're not creating a new instruction in a (deeper) loop.<br></div><div><br></div><div>What do you think?</div><div><br></div><div>Mark</div><div><br></div></div></div>