diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp --- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -388,14 +388,24 @@ // If the FP induction variable still has uses, this is because something else // in the loop uses its value. In order to canonicalize the induction // variable, we chose to eliminate the IV and rewrite it in terms of an - // int->fp cast. + // int->fp cast. fp->int casts of the original induction can be directly + // simplified to use the integer induction . // // We give preference to sitofp over uitofp because it is faster on most // platforms. if (WeakPH) { - Value *Conv = new SIToFPInst(NewPHI, PN->getType(), "indvar.conv", - &*PN->getParent()->getFirstInsertionPt()); - PN->replaceAllUsesWith(Conv); + Value *Conv = nullptr; + for (Use &U : make_early_inc_range(PN->uses())) { + if (isa(U.getUser())) { + U.getUser()->replaceAllUsesWith(NewPHI); + continue; + } + if (!Conv) + Conv = new SIToFPInst(NewPHI, PN->getType(), "indvar.conv", + &*PN->getParent()->getFirstInsertionPt()); + U.set(Conv); + } + RecursivelyDeleteTriviallyDeadInstructions(PN, TLI, MSSAU.get()); } return true; diff --git a/llvm/test/Transforms/IndVarSimplify/floating-point-iv.ll b/llvm/test/Transforms/IndVarSimplify/floating-point-iv.ll --- a/llvm/test/Transforms/IndVarSimplify/floating-point-iv.ll +++ b/llvm/test/Transforms/IndVarSimplify/floating-point-iv.ll @@ -376,8 +376,8 @@ ; CHECK: loop: ; CHECK-NEXT: [[FLOAT_IV_INT:%.*]] = phi i32 [ 1000, [[ENTRY:%.*]] ], [ [[FLOAT_IV_NEXT_INT:%.*]], [[LOOP]] ] ; CHECK-NEXT: [[INDVAR_CONV:%.*]] = sitofp i32 [[FLOAT_IV_INT]] to float -; CHECK-NEXT: [[CONV:%.*]] = fptosi float [[INDVAR_CONV]] to i32 -; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[CONV]] to i64 +; CHECK-NEXT: [[CONV:%.*]] = fptosi float 1.000000e+03 to i32 +; CHECK-NEXT: [[IDXPROM:%.*]] = sext i32 [[FLOAT_IV_INT]] to i64 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[DST:%.*]], i64 [[IDXPROM]] ; CHECK-NEXT: store float [[INDVAR_CONV]], ptr [[ARRAYIDX]], align 4 ; CHECK-NEXT: [[FLOAT_IV_NEXT_INT]] = add nsw i32 [[FLOAT_IV_INT]], -1