diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2132,8 +2132,17 @@ } } + // Guard the gep(gep) fold so we don't create an add inside a loop + // when there wasn't an equivalent instruction there before. + bool DifferentLoops = false; + if (LI) + if (auto *GEPLoop = LI->getLoopFor(GEP.getParent())) + if (auto *SrcOpI = dyn_cast(Src)) + if (LI->getLoopFor(SrcOpI->getParent()) != GEPLoop) + DifferentLoops = true; + // Fold (gep(gep(Ptr,Idx0),Idx1) -> gep(Ptr,add(Idx0,Idx1)) - if (GO1->getType() == SO1->getType()) { + if (!DifferentLoops && GO1->getType() == SO1->getType()) { bool NewInBounds = GEP.isInBounds() && Src->isInBounds(); auto *NewIdx = Builder.CreateAdd(GO1, SO1, GEP.getName() + ".idx", diff --git a/llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll b/llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll --- a/llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll +++ b/llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll @@ -216,6 +216,7 @@ ; CHECK-LABEL: @gep_cross_loop( ; CHECK-NEXT: entry: ; CHECK-NEXT: [[TMP0:%.*]] = load i64, i64* [[_ARG_:%.*]], align 8 +; CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds float, float* [[_ARG_3:%.*]], i64 [[TMP0]] ; CHECK-NEXT: br label [[FOR_COND_I:%.*]] ; CHECK: for.cond.i: ; CHECK-NEXT: [[IDX:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[ADD11_I:%.*]], [[FOR_BODY_I:%.*]] ] @@ -225,8 +226,7 @@ ; CHECK: for.cond.i.i.i.preheader: ; CHECK-NEXT: ret float [[SUM]] ; CHECK: for.body.i: -; CHECK-NEXT: [[ARRAYIDX_I84_I_IDX:%.*]] = add nsw i64 [[IDX]], [[TMP0]] -; CHECK-NEXT: [[ARRAYIDX_I84_I:%.*]] = getelementptr inbounds float, float* [[_ARG_3:%.*]], i64 [[ARRAYIDX_I84_I_IDX]] +; CHECK-NEXT: [[ARRAYIDX_I84_I:%.*]] = getelementptr inbounds float, float* [[ADD_PTR]], i64 [[IDX]] ; CHECK-NEXT: [[TMP1:%.*]] = load float, float* [[ARRAYIDX_I84_I]], align 4 ; CHECK-NEXT: [[ADD_I]] = fadd fast float [[SUM]], [[TMP1]] ; CHECK-NEXT: [[ADD11_I]] = add nuw nsw i64 [[IDX]], 1