Index: lib/Transforms/Vectorize/LoopVectorize.cpp =================================================================== --- lib/Transforms/Vectorize/LoopVectorize.cpp +++ lib/Transforms/Vectorize/LoopVectorize.cpp @@ -6161,8 +6161,9 @@ return None; } + unsigned TC = PSE.getSE()->getSmallConstantTripCount(TheLoop); if (!OptForSize) // Remaining checks deal with scalar loop when OptForSize. - return computeFeasibleMaxVF(OptForSize); + return computeFeasibleMaxVF(OptForSize, TC); if (Legal->getRuntimePointerChecking()->Need) { ORE->emit(createMissedAnalysis("CantVersionLoopWithOptForSize") @@ -6175,7 +6176,6 @@ } // If we optimize the program for size, avoid creating the tail loop. - unsigned TC = PSE.getSE()->getSmallConstantTripCount(TheLoop); DEBUG(dbgs() << "LV: Found trip count: " << TC << '\n'); // If we don't know the precise trip count, don't try to vectorize. @@ -6236,15 +6236,20 @@ DEBUG(dbgs() << "LV: The Widest register is: " << WidestRegister << " bits.\n"); + assert(MaxVectorSize <= 64 && "Did not expect to pack so many elements" + " into one vector!"); if (MaxVectorSize == 0) { DEBUG(dbgs() << "LV: The target has no vector registers.\n"); MaxVectorSize = 1; } else if (ConstTripCount && ConstTripCount < MaxVectorSize && - isPowerOf2_32(ConstTripCount)) + isPowerOf2_32(ConstTripCount)) { + // We need to clamp the VF to be the ConstTripCount. There is no point in + // choosing a higher viable VF as done in the loop below. + DEBUG(dbgs() << "LV: Clamping the MaxVF to the constant trip count: " + << ConstTripCount << "\n"); MaxVectorSize = ConstTripCount; - - assert(MaxVectorSize <= 64 && "Did not expect to pack so many elements" - " into one vector!"); + return MaxVectorSize; + } unsigned MaxVF = MaxVectorSize; if (MaximizeBandwidth && !OptForSize) { Index: test/Transforms/LoopVectorize/X86/vector_max_bandwidth.ll =================================================================== --- test/Transforms/LoopVectorize/X86/vector_max_bandwidth.ll +++ test/Transforms/LoopVectorize/X86/vector_max_bandwidth.ll @@ -1,5 +1,6 @@ ; RUN: opt -loop-vectorize -vectorizer-maximize-bandwidth -mcpu=corei7-avx -debug-only=loop-vectorize -S < %s 2>&1 | FileCheck %s --check-prefix=CHECK-AVX1 ; RUN: opt -loop-vectorize -vectorizer-maximize-bandwidth -mcpu=core-avx2 -debug-only=loop-vectorize -S < %s 2>&1 | FileCheck %s --check-prefix=CHECK-AVX2 +; RUN: opt -loop-vectorize -vectorizer-maximize-bandwidth -mcpu=core-avx2 -S < %s | FileCheck %s --check-prefix=CHECK-CLAMP ; REQUIRES: asserts target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" @@ -46,3 +47,34 @@ %exitcond = icmp eq i64 %indvars.iv.next, 1000 br i1 %exitcond, label %for.cond.cleanup, label %for.body } + +; We should not choose a VF larger than the constant TC. +; VF chosen should be atmost 16 (not the max possible vector width = 32) +define void @not_too_small_tc(i8* noalias nocapture %A, i8* noalias nocapture readonly %B) { +; CHECK-LABEL: not_too_small_tc +; CHECK-CLAMP: entry: +; CHECK-CLAMP: br i1 false, label %scalar.ph, label %vector.ph +; CHECK-CLAMP: vector.ph: +; CHECK-CLAMP: br label %vector.body +; CHECK-CLAMP: vector.body: +; CHECK-CLAMP: <16 x i8> +entry: + br label %for.body + +for.body: + %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ] + %arrayidx = getelementptr inbounds i8, i8* %B, i64 %indvars.iv + %l1 = load i8, i8* %arrayidx, align 4, !llvm.mem.parallel_loop_access !3 + %arrayidx2 = getelementptr inbounds i8, i8* %A, i64 %indvars.iv + %l2 = load i8, i8* %arrayidx2, align 4, !llvm.mem.parallel_loop_access !3 + %add = add i8 %l1, %l2 + store i8 %add, i8* %arrayidx2, align 4, !llvm.mem.parallel_loop_access !3 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %exitcond = icmp eq i64 %indvars.iv.next, 16 + br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !4 + +for.end: + ret void +} +!3 = !{!3} +!4 = !{!4}