Skip to content

Commit 4efa097

Browse files
committed
[LV] Allow tryToCreateWidenRecipe to return a VPValue, use for blends.
Generalize the return value of tryToCreateWidenRecipe to return either a newly create recipe or an existing VPValue. Use this to avoid creating unnecessary VPBlendRecipes. Fixes PR44800.
1 parent 52bc2e7 commit 4efa097

File tree

3 files changed

+96
-21
lines changed

3 files changed

+96
-21
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8249,15 +8249,24 @@ VPRecipeBuilder::tryToOptimizeInductionTruncate(TruncInst *I, VFRange &Range,
82498249
return nullptr;
82508250
}
82518251

8252-
VPBlendRecipe *VPRecipeBuilder::tryToBlend(PHINode *Phi, VPlanPtr &Plan) {
8252+
VPRecipeOrVPValueTy VPRecipeBuilder::tryToBlend(PHINode *Phi, VPlanPtr &Plan) {
8253+
// If all incoming values are equal, the incoming VPValue can be used directly
8254+
// instead of creating a new VPBlendRecipe.
8255+
Value *FirstIncoming = Phi->getIncomingValue(0);
8256+
if (all_of(Phi->incoming_values(), [FirstIncoming](const Value *Inc) {
8257+
return FirstIncoming == Inc;
8258+
})) {
8259+
return Plan->getOrAddVPValue(Phi->getIncomingValue(0));
8260+
}
8261+
82538262
// We know that all PHIs in non-header blocks are converted into selects, so
82548263
// we don't have to worry about the insertion order and we can just use the
82558264
// builder. At this point we generate the predication tree. There may be
82568265
// duplications since this is a simple recursive scan, but future
82578266
// optimizations will clean it up.
8258-
82598267
SmallVector<VPValue *, 2> Operands;
82608268
unsigned NumIncoming = Phi->getNumIncomingValues();
8269+
82618270
for (unsigned In = 0; In < NumIncoming; In++) {
82628271
VPValue *EdgeMask =
82638272
createEdgeMask(Phi->getIncomingBlock(In), Phi->getParent(), Plan);
@@ -8449,9 +8458,9 @@ VPRegionBlock *VPRecipeBuilder::createReplicateRegion(Instruction *Instr,
84498458
return Region;
84508459
}
84518460

8452-
VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(Instruction *Instr,
8453-
VFRange &Range,
8454-
VPlanPtr &Plan) {
8461+
VPRecipeOrVPValueTy VPRecipeBuilder::tryToCreateWidenRecipe(Instruction *Instr,
8462+
VFRange &Range,
8463+
VPlanPtr &Plan) {
84558464
// First, check for specific widening recipes that deal with calls, memory
84568465
// operations, inductions and Phi nodes.
84578466
if (auto *CI = dyn_cast<CallInst>(Instr))
@@ -8623,17 +8632,15 @@ VPlanPtr LoopVectorizationPlanner::buildVPlanWithVPRecipes(
86238632
if (isa<BranchInst>(Instr) || DeadInstructions.count(Instr))
86248633
continue;
86258634

8626-
if (auto Recipe =
8635+
if (auto RecipeOrValue =
86278636
RecipeBuilder.tryToCreateWidenRecipe(Instr, Range, Plan)) {
8628-
8629-
// VPBlendRecipes with a single incoming (value, mask) pair are no-ops.
8630-
// Use the incoming value directly.
8631-
if (isa<VPBlendRecipe>(Recipe) && Recipe->getNumOperands() <= 2) {
8632-
Plan->removeVPValueFor(Instr);
8633-
Plan->addVPValue(Instr, Recipe->getOperand(0));
8634-
delete Recipe;
8637+
// If Instr can be simplified to an existing VPValue, use it.
8638+
if (RecipeOrValue.is<VPValue *>()) {
8639+
Plan->addVPValue(Instr, RecipeOrValue.get<VPValue *>());
86358640
continue;
86368641
}
8642+
// Otherwise, add the new recipe.
8643+
VPRecipeBase *Recipe = RecipeOrValue.get<VPRecipeBase *>();
86378644
for (auto *Def : Recipe->definedValues()) {
86388645
auto *UV = Def->getUnderlyingValue();
86398646
Plan->addVPValue(UV, Def);

llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "LoopVectorizationPlanner.h"
1313
#include "VPlan.h"
1414
#include "llvm/ADT/DenseMap.h"
15+
#include "llvm/ADT/PointerUnion.h"
1516
#include "llvm/IR/IRBuilder.h"
1617

1718
namespace llvm {
@@ -20,6 +21,8 @@ class LoopVectorizationLegality;
2021
class LoopVectorizationCostModel;
2122
class TargetLibraryInfo;
2223

24+
using VPRecipeOrVPValueTy = PointerUnion<VPRecipeBase *, VPValue *>;
25+
2326
/// Helper class to create VPRecipies from IR instructions.
2427
class VPRecipeBuilder {
2528
/// The loop that we evaluate.
@@ -75,10 +78,11 @@ class VPRecipeBuilder {
7578
tryToOptimizeInductionTruncate(TruncInst *I, VFRange &Range,
7679
VPlan &Plan) const;
7780

78-
/// Handle non-loop phi nodes. Currently all such phi nodes are turned into
79-
/// a sequence of select instructions as the vectorizer currently performs
80-
/// full if-conversion.
81-
VPBlendRecipe *tryToBlend(PHINode *Phi, VPlanPtr &Plan);
81+
/// Handle non-loop phi nodes. Return a VPValue, if all incoming values match
82+
/// or a new VPBlendRecipe otherwise. Currently all such phi nodes are turned
83+
/// into a sequence of select instructions as the vectorizer currently
84+
/// performs full if-conversion.
85+
VPRecipeOrVPValueTy tryToBlend(PHINode *Phi, VPlanPtr &Plan);
8286

8387
/// Handle call instructions. If \p CI can be widened for \p Range.Start,
8488
/// return a new VPWidenCallRecipe. Range.End may be decreased to ensure same
@@ -99,10 +103,12 @@ class VPRecipeBuilder {
99103
: OrigLoop(OrigLoop), TLI(TLI), Legal(Legal), CM(CM), PSE(PSE),
100104
Builder(Builder) {}
101105

102-
/// Check if a recipe can be create for \p I withing the given VF \p Range.
103-
/// If a recipe can be created, return it. Otherwise return nullptr.
104-
VPRecipeBase *tryToCreateWidenRecipe(Instruction *Instr, VFRange &Range,
105-
VPlanPtr &Plan);
106+
/// Check if an existing VPValue can be used for \p Instr or a recipe can be
107+
/// create for \p I withing the given VF \p Range. If an existing VPValue can
108+
/// be used or if a recipe can be created, return it. Otherwise return a
109+
/// VPRecipeOrVPValueTy with nullptr.
110+
VPRecipeOrVPValueTy tryToCreateWidenRecipe(Instruction *Instr, VFRange &Range,
111+
VPlanPtr &Plan);
106112

107113
/// Set the recipe created for given ingredient. This operation is a no-op for
108114
/// ingredients that were not marked using a nullptr entry in the map.

llvm/test/Transforms/LoopVectorize/single-value-blend-phis.ll

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,65 @@ loop.latch:
382382
exit:
383383
ret void
384384
}
385+
386+
; Test case for PR44800.
387+
define void @duplicated_incoming_blocks_blend(i32 %x, i32* %ptr) {
388+
; CHECK-LABEL: @duplicated_incoming_blocks_blend(
389+
; CHECK-NEXT: entry:
390+
; CHECK-NEXT: br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
391+
; CHECK: vector.ph:
392+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <2 x i32> poison, i32 [[X:%.*]], i32 0
393+
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <2 x i32> [[BROADCAST_SPLATINSERT]], <2 x i32> poison, <2 x i32> zeroinitializer
394+
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
395+
; CHECK: vector.body:
396+
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
397+
; CHECK-NEXT: [[VEC_IND:%.*]] = phi <2 x i32> [ <i32 0, i32 1>, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[VECTOR_BODY]] ]
398+
; CHECK-NEXT: [[TMP0:%.*]] = icmp ugt <2 x i32> [[VEC_IND]], [[BROADCAST_SPLAT]]
399+
; CHECK-NEXT: [[TMP1:%.*]] = extractelement <2 x i32> [[VEC_IND]], i32 0
400+
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i32, i32* [[PTR:%.*]], i32 [[TMP1]]
401+
; CHECK-NEXT: [[TMP3:%.*]] = getelementptr i32, i32* [[TMP2]], i32 0
402+
; CHECK-NEXT: [[TMP4:%.*]] = bitcast i32* [[TMP3]] to <2 x i32>*
403+
; CHECK-NEXT: store <2 x i32> [[VEC_IND]], <2 x i32>* [[TMP4]], align 4
404+
; CHECK-NEXT: [[INDEX_NEXT]] = add i32 [[INDEX]], 2
405+
; CHECK-NEXT: [[VEC_IND_NEXT]] = add <2 x i32> [[VEC_IND]], <i32 2, i32 2>
406+
; CHECK-NEXT: [[TMP5:%.*]] = icmp eq i32 [[INDEX_NEXT]], 1000
407+
; CHECK-NEXT: br i1 [[TMP5]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], [[LOOP10:!llvm.loop !.*]]
408+
; CHECK: middle.block:
409+
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i32 1000, 1000
410+
; CHECK-NEXT: br i1 [[CMP_N]], label [[EXIT:%.*]], label [[SCALAR_PH]]
411+
; CHECK: scalar.ph:
412+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ 1000, [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
413+
; CHECK-NEXT: br label [[LOOP_HEADER:%.*]]
414+
; CHECK: loop.header:
415+
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[ADD_I:%.*]], [[LOOP_LATCH:%.*]] ]
416+
; CHECK-NEXT: [[C_0:%.*]] = icmp ugt i32 [[IV]], [[X]]
417+
; CHECK-NEXT: br i1 [[C_0]], label [[LOOP_LATCH]], label [[LOOP_LATCH]]
418+
; CHECK: loop.latch:
419+
; CHECK-NEXT: [[P:%.*]] = phi i32 [ [[IV]], [[LOOP_HEADER]] ], [ [[IV]], [[LOOP_HEADER]] ]
420+
; CHECK-NEXT: [[GEP_PTR:%.*]] = getelementptr i32, i32* [[PTR]], i32 [[P]]
421+
; CHECK-NEXT: store i32 [[P]], i32* [[GEP_PTR]], align 4
422+
; CHECK-NEXT: [[ADD_I]] = add nsw i32 [[P]], 1
423+
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[ADD_I]], 1000
424+
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_HEADER]], label [[EXIT]], [[LOOP11:!llvm.loop !.*]]
425+
; CHECK: exit:
426+
; CHECK-NEXT: ret void
427+
;
428+
entry:
429+
br label %loop.header
430+
431+
loop.header:
432+
%iv = phi i32 [ 0 , %entry ], [ %add.i, %loop.latch ]
433+
%c.0 = icmp ugt i32 %iv, %x
434+
br i1 %c.0, label %loop.latch, label %loop.latch
435+
436+
loop.latch:
437+
%p = phi i32 [ %iv, %loop.header ], [ %iv, %loop.header ]
438+
%gep.ptr = getelementptr i32, i32* %ptr, i32 %p
439+
store i32 %p, i32* %gep.ptr
440+
%add.i = add nsw i32 %p, 1
441+
%cmp = icmp slt i32 %add.i, 1000
442+
br i1 %cmp, label %loop.header, label %exit
443+
444+
exit:
445+
ret void
446+
}

0 commit comments

Comments
 (0)