diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp --- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -657,7 +657,7 @@ return Hint; } -/// Replace the UseInst with a constant if possible. +/// Replace the UseInst with a loop invariant expression if it is safe. bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) { if (!SE->isSCEVable(I->getType())) return false; @@ -673,6 +673,13 @@ return false; auto *IP = GetLoopInvariantInsertPosition(L, I); + + if (!isSafeToExpandAt(S, IP, *SE)) { + LLVM_DEBUG(dbgs() << "INDVARS: Can not replace IV user: " << *I + << " with non-speculable loop invariant: " << *S << '\n'); + return false; + } + auto *Invariant = Rewriter.expandCodeFor(S, I->getType(), IP); I->replaceAllUsesWith(Invariant); diff --git a/llvm/test/Transforms/IndVarSimplify/X86/pr45360.ll b/llvm/test/Transforms/IndVarSimplify/X86/pr45360.ll --- a/llvm/test/Transforms/IndVarSimplify/X86/pr45360.ll +++ b/llvm/test/Transforms/IndVarSimplify/X86/pr45360.ll @@ -26,7 +26,6 @@ ; CHECK: bb19: ; CHECK-NEXT: [[I8_LCSSA9:%.*]] = phi i32 [ [[D_PROMOTED8]], [[BB:%.*]] ], [ [[I8:%.*]], [[BB27:%.*]] ] ; CHECK-NEXT: [[I8]] = and i32 [[I8_LCSSA9]], [[I6]] -; CHECK-NEXT: [[TMP0:%.*]] = urem i32 [[I24]], [[I8]] ; CHECK-NEXT: [[I21:%.*]] = icmp eq i32 [[I8]], 0 ; CHECK-NEXT: br i1 [[I21]], label [[BB27_THREAD:%.*]], label [[BB27]] ; CHECK: bb27.thread: @@ -35,8 +34,9 @@ ; CHECK-NEXT: store i32 0, i32* @c, align 4 ; CHECK-NEXT: br label [[BB32:%.*]] ; CHECK: bb27: -; CHECK-NEXT: store i32 [[TMP0]], i32* @e, align 4 -; CHECK-NEXT: [[I30:%.*]] = icmp eq i32 [[TMP0]], 0 +; CHECK-NEXT: [[I26:%.*]] = urem i32 [[I24]], [[I8]] +; CHECK-NEXT: store i32 [[I26]], i32* @e, align 4 +; CHECK-NEXT: [[I30:%.*]] = icmp eq i32 [[I26]], 0 ; CHECK-NEXT: br i1 [[I30]], label [[BB32_LOOPEXIT:%.*]], label [[BB19]] ; CHECK: bb32.loopexit: ; CHECK-NEXT: store i32 -1, i32* @f, align 4