Index: lib/Transforms/Vectorize/LoopVectorize.cpp =================================================================== --- lib/Transforms/Vectorize/LoopVectorize.cpp +++ lib/Transforms/Vectorize/LoopVectorize.cpp @@ -4921,6 +4921,7 @@ // We now know that the loop is vectorizable! // Collect variables that will remain uniform after vectorization. std::vector Worklist; + SmallPtrSet VisitedPHIs; BasicBlock *Latch = TheLoop->getLoopLatch(); // Start with the conditional branch and walk up the block. @@ -4941,13 +4942,17 @@ Worklist.pop_back(); // Look at instructions inside this loop. - // Stop when reaching PHI nodes. // TODO: we need to follow values all over the loop, not only in this block. - if (!I || !TheLoop->contains(I) || isa(I)) + if (!I || !TheLoop->contains(I)) + continue; + + // Look through each PHI node only once, to avoid infinite loops. + if (isa(I) && !VisitedPHIs.insert(I).second) continue; // This is a known uniform. Uniforms.insert(I); + DEBUG(dbgs() << "LV: Found uniform instruction: " << *I << "\n"); // Insert all operands. Worklist.insert(Worklist.end(), I->op_begin(), I->op_end()); Index: test/Transforms/LoopVectorize/X86/uniform-phi.ll =================================================================== --- test/Transforms/LoopVectorize/X86/uniform-phi.ll +++ test/Transforms/LoopVectorize/X86/uniform-phi.ll @@ -0,0 +1,28 @@ +; RUN: opt < %s -loop-vectorize -mtriple=x86_64-apple-macosx10.8.0 -mcpu=corei7 -debug-only=loop-vectorize -S 2>&1 | FileCheck %s +; REQUIRES: asserts +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; CHECK-LABEL: test +; CHECK: LV: Found uniform instruction: %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ] +; CHECK: LV: Found uniform instruction: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 +; CHECK: LV: Found uniform instruction: %exitcond = icmp eq i64 %indvars.iv, 1599 + +define void @test(float* noalias nocapture %a, float* noalias nocapture readonly %b) #0 { +entry: + br label %for.body + +for.body: ; preds = %for.body, %entry + %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ] + %arrayidx = getelementptr inbounds float, float* %b, i64 %indvars.iv + %0 = load float, float* %arrayidx, align 4 + %add = fadd float %0, 1.000000e+00 + %arrayidx5 = getelementptr inbounds float, float* %a, i64 %indvars.iv + store float %add, float* %arrayidx5, align 4 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %exitcond = icmp eq i64 %indvars.iv, 1599 + br i1 %exitcond, label %for.end, label %for.body + +for.end: ; preds = %for.body + ret void +}