diff --git a/llvm/include/llvm/Transforms/Utils/LoopUtils.h b/llvm/include/llvm/Transforms/Utils/LoopUtils.h --- a/llvm/include/llvm/Transforms/Utils/LoopUtils.h +++ b/llvm/include/llvm/Transforms/Utils/LoopUtils.h @@ -73,7 +73,8 @@ /// /// Returns true if any modifications are made. bool formLCSSAForInstructions(SmallVectorImpl &Worklist, - DominatorTree &DT, LoopInfo &LI); + DominatorTree &DT, LoopInfo &LI, + ScalarEvolution *SE); /// Put loop into LCSSA form. /// diff --git a/llvm/lib/Transforms/Utils/LCSSA.cpp b/llvm/lib/Transforms/Utils/LCSSA.cpp --- a/llvm/lib/Transforms/Utils/LCSSA.cpp +++ b/llvm/lib/Transforms/Utils/LCSSA.cpp @@ -74,7 +74,8 @@ /// that are outside the current loop. If so, insert LCSSA PHI nodes and /// rewrite the uses. bool llvm::formLCSSAForInstructions(SmallVectorImpl &Worklist, - DominatorTree &DT, LoopInfo &LI) { + DominatorTree &DT, LoopInfo &LI, + ScalarEvolution *SE) { SmallVector UsesToRewrite; SmallSetVector PHIsToRemove; PredIteratorCache PredCache; @@ -134,6 +135,11 @@ SSAUpdater SSAUpdate(&InsertedPHIs); SSAUpdate.Initialize(I->getType(), I->getName()); + // Force re-computation of I, as some users now need to use the new PHI + // node. + if (SE) + SE->forgetValue(I); + // Insert the LCSSA phi's into all of the exit blocks dominated by the // value, and add them to the Phi's map. for (BasicBlock *ExitBB : ExitBlocks) { @@ -368,7 +374,7 @@ Worklist.push_back(&I); } } - Changed = formLCSSAForInstructions(Worklist, DT, *LI); + Changed = formLCSSAForInstructions(Worklist, DT, *LI, SE); // If we modified the code, remove any caches about the loop from SCEV to // avoid dangling entries. diff --git a/llvm/test/Transforms/LoopUnroll/unroll-preserve-scev-lcssa.ll b/llvm/test/Transforms/LoopUnroll/unroll-preserve-scev-lcssa.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/LoopUnroll/unroll-preserve-scev-lcssa.ll @@ -0,0 +1,71 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -loop-reduce -loop-unroll -unroll-runtime -verify-scev -verify-loop-lcssa < %s -S | FileCheck %s + +; Test case for PR43458. We check that we do properly invalidate LCSSA phi +; users in SCEV. Run -loop-reduce first, so SCEV is already populated. + +define void @spam() { +; CHECK-LABEL: @spam( +; CHECK-NEXT: bb: +; CHECK-NEXT: br label [[BB3:%.*]] +; CHECK: bb3: +; CHECK-NEXT: br i1 false, label [[BB9:%.*]], label [[BB5_PREHEADER:%.*]] +; CHECK: bb5.preheader: +; CHECK-NEXT: br label [[BB5:%.*]] +; CHECK: bb5: +; CHECK-NEXT: br label [[BB9]] +; CHECK: bb9: +; CHECK-NEXT: [[TMP10:%.*]] = phi i64 [ 0, [[BB3]] ], [ 5, [[BB5]] ] +; CHECK-NEXT: [[TMP11:%.*]] = trunc i64 [[TMP10]] to i32 +; CHECK-NEXT: [[TMP21:%.*]] = sext i32 [[TMP11]] to i64 +; CHECK-NEXT: [[TMP22:%.*]] = icmp slt i64 0, [[TMP21]] +; CHECK-NEXT: br i1 [[TMP22]], label [[BB24_PREHEADER:%.*]], label [[BB29:%.*]] +; CHECK: bb24.preheader: +; CHECK-NEXT: br label [[BB24:%.*]] +; CHECK: bb24: +; CHECK-NEXT: [[TMP25:%.*]] = phi i64 [ [[TMP26:%.*]], [[BB24]] ], [ 0, [[BB24_PREHEADER]] ] +; CHECK-NEXT: [[TMP26]] = add nuw nsw i64 [[TMP25]], 1 +; CHECK-NEXT: [[TMP27:%.*]] = icmp slt i64 [[TMP26]], [[TMP21]] +; CHECK-NEXT: br i1 [[TMP27]], label [[BB24]], label [[BB29_LOOPEXIT:%.*]] +; CHECK: bb29.loopexit: +; CHECK-NEXT: br label [[BB29]] +; CHECK: bb29: +; CHECK-NEXT: ret void +; +bb: + br label %bb3 + +bb3: ; preds = %bb9, %bb + %pv1 = phi i64 [ 0, %bb ], [ %iv1, %bb9] + %c1 = icmp eq i64 %pv1, 5 + br i1 %c1, label %bb9, label %bb5 + +bb5: ; preds = %bb5, %bb3 + %pv = phi i64 [ 0, %bb3], [ %tmp6, %bb5] + %tmp6 = add nsw i64 %pv, 1 + %cond = icmp eq i64 %tmp6, 5 + br i1 %cond, label %bb8, label %bb5 + +bb8: ; preds = %bb5 + br label %bb9 + +bb9: ; preds = %bb8, %bb3 + %tmp10 = phi i64 [ 0, %bb3 ], [ %tmp6, %bb8 ] + %tmp11 = trunc i64 %tmp10 to i32 + %iv1 = add nsw i64 %pv1, 1 + br i1 false, label %bb3, label %bb20 + +bb20: ; preds = %bb9 + %tmp21 = sext i32 %tmp11 to i64 + %tmp22 = icmp slt i64 0, %tmp21 + br i1 %tmp22, label %bb24, label %bb29 + +bb24: ; preds = %bb24, %bb20 + %tmp25 = phi i64 [ %tmp26, %bb24 ], [ 0, %bb20 ] + %tmp26 = add nuw nsw i64 %tmp25, 1 + %tmp27 = icmp slt i64 %tmp26, %tmp21 + br i1 %tmp27, label %bb24, label %bb29 + +bb29: ; preds = %bb24, %bb20 + ret void +}