Index: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp =================================================================== --- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -8759,8 +8759,9 @@ } SmallPtrSet DeadInstructions; - VPlanTransforms::VPInstructionsToVPRecipes( - OrigLoop, Plan, Legal->getInductionVars(), DeadInstructions); + VPlanTransforms::VPInstructionsToVPRecipes(*PSE.getSE(), OrigLoop, Plan, + Legal->getInductionVars(), + DeadInstructions); return Plan; } Index: llvm/lib/Transforms/Vectorize/VPlanTransforms.h =================================================================== --- llvm/lib/Transforms/Vectorize/VPlanTransforms.h +++ llvm/lib/Transforms/Vectorize/VPlanTransforms.h @@ -23,7 +23,7 @@ /// Replaces the VPInstructions in \p Plan with corresponding /// widen recipes. static void VPInstructionsToVPRecipes( - Loop *OrigLoop, VPlanPtr &Plan, + ScalarEvolution &SE, Loop *OrigLoop, VPlanPtr &Plan, LoopVectorizationLegality::InductionList &Inductions, SmallPtrSetImpl &DeadInstructions); }; Index: llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp =================================================================== --- llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp +++ llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp @@ -17,7 +17,7 @@ using namespace llvm; void VPlanTransforms::VPInstructionsToVPRecipes( - Loop *OrigLoop, VPlanPtr &Plan, + ScalarEvolution &SE, Loop *OrigLoop, VPlanPtr &Plan, LoopVectorizationLegality::InductionList &Inductions, SmallPtrSetImpl &DeadInstructions) { @@ -71,6 +71,11 @@ } else if (GetElementPtrInst *GEP = dyn_cast(Inst)) { NewRecipe = new VPWidenGEPRecipe( GEP, Plan->mapToVPValues(GEP->operands()), OrigLoop); + } else if (SelectInst *SI = dyn_cast(Inst)) { + bool InvariantCond = + SE.isLoopInvariant(SE.getSCEV(SI->getOperand(0)), OrigLoop); + NewRecipe = new VPWidenSelectRecipe( + *SI, Plan->mapToVPValues(SI->operands()), InvariantCond); } else { NewRecipe = new VPWidenRecipe(*Inst, Plan->mapToVPValues(Inst->operands())); Index: llvm/test/Transforms/LoopVectorize/vplan-widen-select-instruction.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/LoopVectorize/vplan-widen-select-instruction.ll @@ -0,0 +1,71 @@ +; RUN: opt -loop-vectorize -force-vector-width=4 -enable-vplan-native-path -S %s | FileCheck %s + +; Test that VPlan native path is able to widen select instruction in the innermost loop. + +define void @inner_loop_select(double* noalias nocapture readonly %a.in, double* noalias nocapture readonly %b.in, double* noalias nocapture %c.out, i1 %select) { +; CHECK-LABEL: @inner_loop_select( + +; CHECK: vector.body: +; CHECK-NEXT: %[[FOR1_INDEX:.*]] = phi i64 [ 0, %[[LABEL_PR:.*]] ], [ %{{.*}}, %[[LABEL_FOR1_LATCH:.*]] ] +; CHECK: %[[VEC_INDEX:.*]] = phi <4 x i64> [ , %[[LABEL_PR]] ], [ %{{.*}}, %[[LABEL_FOR1_LATCH]] ] +; CHECK-NEXT: %[[A_PTR:.*]] = getelementptr inbounds double, double* %a.in, <4 x i64> %[[VEC_INDEX]] +; CHECK-NEXT: %[[MASKED_GATHER1:.*]] = call <4 x double> @llvm.masked.gather.v4f64.v4p0f64(<4 x double*> %[[A_PTR]], i32 8, <4 x i1> , <4 x double> undef) +; CHECK-NEXT: %[[B_PTR:.*]] = getelementptr inbounds double, double* %b.in, <4 x i64> %[[VEC_INDEX]] +; CHECK-NEXT: %[[MASKED_GATHER2:.*]] = call <4 x double> @llvm.masked.gather.v4f64.v4p0f64(<4 x double*> %[[B_PTR]], i32 8, <4 x i1> , <4 x double> undef) +; CHECK-NEXT: br label %[[FOR2_HEADER:.*]] + +; CHECK: [[FOR2_HEADER]]: +; CHECK-NEXT: %[[FOR2_INDEX:.*]] = phi <4 x i32> [ zeroinitializer, %vector.body ], [ %[[FOR2_INDEX_NEXT:.*]], %[[FOR2_HEADER]] ] +; CHECK-NEXT: %[[REDUCTION:.*]] = phi <4 x double> [ %[[MASKED_GATHER1]], %vector.body ], [ %[[REDUCTION_NEXT:.*]], %[[FOR2_HEADER]] ] +; CHECK-NEXT: %[[SELECT:.*]] = select i1 %select, <4 x double> %[[MASKED_GATHER1]], <4 x double> %[[MASKED_GATHER2]] +; CHECK-NEXT: %[[REDUCTION_NEXT]] = fadd <4 x double> %[[SELECT]], %[[REDUCTION]] +; CHECK-NEXT: %[[FOR2_INDEX_NEXT]] = add nuw nsw <4 x i32> %[[FOR2_INDEX]], +; CHECK-NEXT: %[[VEC_PTR:.*]] = icmp eq <4 x i32> %[[FOR2_INDEX_NEXT]], +; CHECK-NEXT: %[[EXIT_COND:.*]] = extractelement <4 x i1> %[[VEC_PTR]], i32 0 +; CHECK-NEXT: br i1 %[[EXIT_COND]], label %[[FOR1_LATCH:.*]], label %{{.*}} + +; CHECK: [[FOR1_LATCH]]: +; CHECK-NEXT: %[[REDUCTION:.*]] = phi <4 x double> [ %[[REDUCTION_NEXT]], %[[FOR2_HEADER]] ] +; CHECK-NEXT: %[[C_PTR:.*]] = getelementptr inbounds double, double* %c.out, <4 x i64> %[[VEC_INDEX]] +; CHECK-NEXT: call void @llvm.masked.scatter.v4f64.v4p0f64(<4 x double> %[[REDUCTION]], <4 x double*> %[[C_PTR]], i32 8, <4 x i1> ) +; CHECK-NEXT: %[[VEC_INDEX_NEXT:.*]] = add nuw nsw <4 x i64> %[[VEC_INDEX]], +; CHECK-NEXT: %[[VEC_PTR:.*]] = icmp eq <4 x i64> %[[VEC_INDEX_NEXT]], +; CHECK-NEXT: %{{.*}} = extractelement <4 x i1> %[[VEC_PTR]], i32 0 +; CHECK-NEXT: %[[FOR1_INDEX_NEXT:.*]] = add i64 %[[FOR1_INDEX]], 4 +; CHECK-NEXT: %{{.*}} = add <4 x i64> %[[VEC_INDEX]], +; CHECK-NEXT: %[[EXIT_COND:.*]] = icmp eq i64 %[[FOR1_INDEX_NEXT]], 1000 +; CHECK-NEXT: br i1 %[[EXIT_COND]], label %{{.*}}, label %vector.body + +entry: + br label %for1.header + +for1.header: + %indvar1 = phi i64 [ 0, %entry ], [ %indvar11, %for1.latch ] + %a.ptr = getelementptr inbounds double, double* %a.in, i64 %indvar1 + %a = load double, double* %a.ptr, align 8 + %b.ptr = getelementptr inbounds double, double* %b.in, i64 %indvar1 + %b = load double, double* %b.ptr, align 8 + br label %for2.header + +for2.header: + %indvar2 = phi i32 [ 0, %for1.header ], [ %indvar21, %for2.header ] + %a.reduction = phi double [ %a, %for1.header ], [ %a.reduction1, %for2.header ] + %select.b = select i1 %select, double %a, double %b + %a.reduction1 = fadd double %select.b, %a.reduction + %indvar21 = add nuw nsw i32 %indvar2, 1 + %for2.cond = icmp eq i32 %indvar21, 10000 + br i1 %for2.cond, label %for1.latch, label %for2.header + +for1.latch: + %c.ptr = getelementptr inbounds double, double* %c.out, i64 %indvar1 + store double %a.reduction1, double* %c.ptr, align 8 + %indvar11 = add nuw nsw i64 %indvar1, 1 + %for1.cond = icmp eq i64 %indvar11, 1000 + br i1 %for1.cond, label %exit, label %for1.header, !llvm.loop !0 + +exit: + ret void +} + +!0 = distinct !{!0, !1} +!1 = !{!"llvm.loop.vectorize.enable", i1 true} Index: llvm/unittests/Transforms/Vectorize/VPlanHCFGTest.cpp =================================================================== --- llvm/unittests/Transforms/Vectorize/VPlanHCFGTest.cpp +++ llvm/unittests/Transforms/Vectorize/VPlanHCFGTest.cpp @@ -130,8 +130,8 @@ LoopVectorizationLegality::InductionList Inductions; SmallPtrSet DeadInstructions; - VPlanTransforms::VPInstructionsToVPRecipes(LI->getLoopFor(LoopHeader), Plan, - Inductions, DeadInstructions); + VPlanTransforms::VPInstructionsToVPRecipes( + *SE, LI->getLoopFor(LoopHeader), Plan, Inductions, DeadInstructions); } TEST_F(VPlanHCFGTest, testVPInstructionToVPRecipesInner) { @@ -160,8 +160,8 @@ LoopVectorizationLegality::InductionList Inductions; SmallPtrSet DeadInstructions; - VPlanTransforms::VPInstructionsToVPRecipes(LI->getLoopFor(LoopHeader), Plan, - Inductions, DeadInstructions); + VPlanTransforms::VPInstructionsToVPRecipes( + *SE, LI->getLoopFor(LoopHeader), Plan, Inductions, DeadInstructions); VPBlockBase *Entry = Plan->getEntry()->getEntryBasicBlock(); EXPECT_NE(nullptr, Entry->getSingleSuccessor()); Index: llvm/unittests/Transforms/Vectorize/VPlanTestBase.h =================================================================== --- llvm/unittests/Transforms/Vectorize/VPlanTestBase.h +++ llvm/unittests/Transforms/Vectorize/VPlanTestBase.h @@ -17,6 +17,7 @@ #include "llvm/Analysis/AssumptionCache.h" #include "llvm/Analysis/BasicAliasAnalysis.h" #include "llvm/Analysis/LoopInfo.h" +#include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/AsmParser/Parser.h" #include "llvm/IR/Dominators.h" #include "llvm/Support/SourceMgr.h" @@ -28,12 +29,23 @@ /// given loop entry block. class VPlanTestBase : public testing::Test { protected: + TargetLibraryInfoImpl TLII; + TargetLibraryInfo TLI; + DataLayout DL; + std::unique_ptr Ctx; std::unique_ptr M; std::unique_ptr LI; std::unique_ptr DT; + std::unique_ptr AC; + std::unique_ptr SE; - VPlanTestBase() : Ctx(new LLVMContext) {} + VPlanTestBase() + : TLII(), TLI(TLII), + DL("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-" + "f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:" + "16:32:64-S128"), + Ctx(new LLVMContext) {} Module &parseModule(const char *ModuleString) { SMDiagnostic Err; @@ -45,6 +57,8 @@ void doAnalysis(Function &F) { DT.reset(new DominatorTree(F)); LI.reset(new LoopInfo(*DT)); + AC.reset(new AssumptionCache(F)); + SE.reset(new ScalarEvolution(F, TLI, *AC, *DT, *LI)); } VPlanPtr buildHCFG(BasicBlock *LoopHeader) {