diff --git a/llvm/lib/Analysis/IVDescriptors.cpp b/llvm/lib/Analysis/IVDescriptors.cpp --- a/llvm/lib/Analysis/IVDescriptors.cpp +++ b/llvm/lib/Analysis/IVDescriptors.cpp @@ -1025,6 +1025,16 @@ // Previous. Nothing left to do. if (DT->dominates(Previous, OtherPrev) || Previous == OtherPrev) return true; + + // If there are other instructions to be sunk after SinkCandidate, remove + // and re-insert SinkCandidate can break those instructions. Bail out for + // simplicity. + if (any_of(SinkAfter, + [SinkCandidate](const std::pair &P) { + return P.second == SinkCandidate; + })) + return false; + // Otherwise, Previous comes after OtherPrev and SinkCandidate needs to be // re-sunk to Previous, instead of sinking to OtherPrev. Remove // SinkCandidate from SinkAfter to ensure it's insert position is updated. diff --git a/llvm/test/Transforms/LoopVectorize/first-order-recurrence-chains.ll b/llvm/test/Transforms/LoopVectorize/first-order-recurrence-chains.ll --- a/llvm/test/Transforms/LoopVectorize/first-order-recurrence-chains.ll +++ b/llvm/test/Transforms/LoopVectorize/first-order-recurrence-chains.ll @@ -637,3 +637,30 @@ exit: ret void } + +; Make sure LLVM doesn't generate wrong data in SinkAfter, and causes crash in +; loop vectorizer. +define void @test_crash(ptr %p) { +; CHECK-LABEL: @test_crash +; CHECK-NOT: vector.body: +; CHECK: ret +Entry: + br label %Loop + +Loop: + %for.1 = phi double [ %iv1, %Loop ], [ 0.000000e+00, %Entry ] + %for.2 = phi double [ %iv2, %Loop ], [ 0.000000e+00, %Entry ] + %for.3 = phi double [ %for.2, %Loop ], [ 0.000000e+00, %Entry ] + %for.4 = phi i64 [ %count, %Loop ], [ 0, %Entry ] + %USE_2_INDVARS = fdiv double %for.3, %for.1 + %div = fdiv double 0.000000e+00, %for.1 + %iv1 = load double, ptr null, align 8 + %count = add nuw nsw i64 %for.4, 1 + %iv2 = load double, ptr null, align 8 + store double %div, ptr %p, align 8 + %cond = icmp eq i64 %count, 0 + br i1 %cond, label %End, label %Loop + +End: + ret void +}