Skip to content

Commit

Permalink
[LV] Allow tryToCreateWidenRecipe to return a VPValue, use for blends.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fhahn committed Feb 23, 2021
1 parent 52bc2e7 commit 4efa097
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 21 deletions.
33 changes: 20 additions & 13 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Expand Up @@ -8249,15 +8249,24 @@ VPRecipeBuilder::tryToOptimizeInductionTruncate(TruncInst *I, VFRange &Range,
return nullptr;
}

VPBlendRecipe *VPRecipeBuilder::tryToBlend(PHINode *Phi, VPlanPtr &Plan) {
VPRecipeOrVPValueTy VPRecipeBuilder::tryToBlend(PHINode *Phi, VPlanPtr &Plan) {
// If all incoming values are equal, the incoming VPValue can be used directly
// instead of creating a new VPBlendRecipe.
Value *FirstIncoming = Phi->getIncomingValue(0);
if (all_of(Phi->incoming_values(), [FirstIncoming](const Value *Inc) {
return FirstIncoming == Inc;
})) {
return Plan->getOrAddVPValue(Phi->getIncomingValue(0));
}

// We know that all PHIs in non-header blocks are converted into selects, so
// we don't have to worry about the insertion order and we can just use the
// builder. At this point we generate the predication tree. There may be
// duplications since this is a simple recursive scan, but future
// optimizations will clean it up.

SmallVector<VPValue *, 2> Operands;
unsigned NumIncoming = Phi->getNumIncomingValues();

for (unsigned In = 0; In < NumIncoming; In++) {
VPValue *EdgeMask =
createEdgeMask(Phi->getIncomingBlock(In), Phi->getParent(), Plan);
Expand Down Expand Up @@ -8449,9 +8458,9 @@ VPRegionBlock *VPRecipeBuilder::createReplicateRegion(Instruction *Instr,
return Region;
}

VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(Instruction *Instr,
VFRange &Range,
VPlanPtr &Plan) {
VPRecipeOrVPValueTy VPRecipeBuilder::tryToCreateWidenRecipe(Instruction *Instr,
VFRange &Range,
VPlanPtr &Plan) {
// First, check for specific widening recipes that deal with calls, memory
// operations, inductions and Phi nodes.
if (auto *CI = dyn_cast<CallInst>(Instr))
Expand Down Expand Up @@ -8623,17 +8632,15 @@ VPlanPtr LoopVectorizationPlanner::buildVPlanWithVPRecipes(
if (isa<BranchInst>(Instr) || DeadInstructions.count(Instr))
continue;

if (auto Recipe =
if (auto RecipeOrValue =
RecipeBuilder.tryToCreateWidenRecipe(Instr, Range, Plan)) {

// VPBlendRecipes with a single incoming (value, mask) pair are no-ops.
// Use the incoming value directly.
if (isa<VPBlendRecipe>(Recipe) && Recipe->getNumOperands() <= 2) {
Plan->removeVPValueFor(Instr);
Plan->addVPValue(Instr, Recipe->getOperand(0));
delete Recipe;
// If Instr can be simplified to an existing VPValue, use it.
if (RecipeOrValue.is<VPValue *>()) {
Plan->addVPValue(Instr, RecipeOrValue.get<VPValue *>());
continue;
}
// Otherwise, add the new recipe.
VPRecipeBase *Recipe = RecipeOrValue.get<VPRecipeBase *>();
for (auto *Def : Recipe->definedValues()) {
auto *UV = Def->getUnderlyingValue();
Plan->addVPValue(UV, Def);
Expand Down
22 changes: 14 additions & 8 deletions llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
Expand Up @@ -12,6 +12,7 @@
#include "LoopVectorizationPlanner.h"
#include "VPlan.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/IR/IRBuilder.h"

namespace llvm {
Expand All @@ -20,6 +21,8 @@ class LoopVectorizationLegality;
class LoopVectorizationCostModel;
class TargetLibraryInfo;

using VPRecipeOrVPValueTy = PointerUnion<VPRecipeBase *, VPValue *>;

/// Helper class to create VPRecipies from IR instructions.
class VPRecipeBuilder {
/// The loop that we evaluate.
Expand Down Expand Up @@ -75,10 +78,11 @@ class VPRecipeBuilder {
tryToOptimizeInductionTruncate(TruncInst *I, VFRange &Range,
VPlan &Plan) const;

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

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

/// Check if a recipe can be create for \p I withing the given VF \p Range.
/// If a recipe can be created, return it. Otherwise return nullptr.
VPRecipeBase *tryToCreateWidenRecipe(Instruction *Instr, VFRange &Range,
VPlanPtr &Plan);
/// Check if an existing VPValue can be used for \p Instr or a recipe can be
/// create for \p I withing the given VF \p Range. If an existing VPValue can
/// be used or if a recipe can be created, return it. Otherwise return a
/// VPRecipeOrVPValueTy with nullptr.
VPRecipeOrVPValueTy tryToCreateWidenRecipe(Instruction *Instr, VFRange &Range,
VPlanPtr &Plan);

/// Set the recipe created for given ingredient. This operation is a no-op for
/// ingredients that were not marked using a nullptr entry in the map.
Expand Down
62 changes: 62 additions & 0 deletions llvm/test/Transforms/LoopVectorize/single-value-blend-phis.ll
Expand Up @@ -382,3 +382,65 @@ loop.latch:
exit:
ret void
}

; Test case for PR44800.
define void @duplicated_incoming_blocks_blend(i32 %x, i32* %ptr) {
; CHECK-LABEL: @duplicated_incoming_blocks_blend(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
; CHECK: vector.ph:
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <2 x i32> poison, i32 [[X:%.*]], i32 0
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <2 x i32> [[BROADCAST_SPLATINSERT]], <2 x i32> poison, <2 x i32> zeroinitializer
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
; CHECK: vector.body:
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
; CHECK-NEXT: [[VEC_IND:%.*]] = phi <2 x i32> [ <i32 0, i32 1>, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[VECTOR_BODY]] ]
; CHECK-NEXT: [[TMP0:%.*]] = icmp ugt <2 x i32> [[VEC_IND]], [[BROADCAST_SPLAT]]
; CHECK-NEXT: [[TMP1:%.*]] = extractelement <2 x i32> [[VEC_IND]], i32 0
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i32, i32* [[PTR:%.*]], i32 [[TMP1]]
; CHECK-NEXT: [[TMP3:%.*]] = getelementptr i32, i32* [[TMP2]], i32 0
; CHECK-NEXT: [[TMP4:%.*]] = bitcast i32* [[TMP3]] to <2 x i32>*
; CHECK-NEXT: store <2 x i32> [[VEC_IND]], <2 x i32>* [[TMP4]], align 4
; CHECK-NEXT: [[INDEX_NEXT]] = add i32 [[INDEX]], 2
; CHECK-NEXT: [[VEC_IND_NEXT]] = add <2 x i32> [[VEC_IND]], <i32 2, i32 2>
; CHECK-NEXT: [[TMP5:%.*]] = icmp eq i32 [[INDEX_NEXT]], 1000
; CHECK-NEXT: br i1 [[TMP5]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], [[LOOP10:!llvm.loop !.*]]
; CHECK: middle.block:
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i32 1000, 1000
; CHECK-NEXT: br i1 [[CMP_N]], label [[EXIT:%.*]], label [[SCALAR_PH]]
; CHECK: scalar.ph:
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ 1000, [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
; CHECK-NEXT: br label [[LOOP_HEADER:%.*]]
; CHECK: loop.header:
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[ADD_I:%.*]], [[LOOP_LATCH:%.*]] ]
; CHECK-NEXT: [[C_0:%.*]] = icmp ugt i32 [[IV]], [[X]]
; CHECK-NEXT: br i1 [[C_0]], label [[LOOP_LATCH]], label [[LOOP_LATCH]]
; CHECK: loop.latch:
; CHECK-NEXT: [[P:%.*]] = phi i32 [ [[IV]], [[LOOP_HEADER]] ], [ [[IV]], [[LOOP_HEADER]] ]
; CHECK-NEXT: [[GEP_PTR:%.*]] = getelementptr i32, i32* [[PTR]], i32 [[P]]
; CHECK-NEXT: store i32 [[P]], i32* [[GEP_PTR]], align 4
; CHECK-NEXT: [[ADD_I]] = add nsw i32 [[P]], 1
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[ADD_I]], 1000
; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_HEADER]], label [[EXIT]], [[LOOP11:!llvm.loop !.*]]
; CHECK: exit:
; CHECK-NEXT: ret void
;
entry:
br label %loop.header

loop.header:
%iv = phi i32 [ 0 , %entry ], [ %add.i, %loop.latch ]
%c.0 = icmp ugt i32 %iv, %x
br i1 %c.0, label %loop.latch, label %loop.latch

loop.latch:
%p = phi i32 [ %iv, %loop.header ], [ %iv, %loop.header ]
%gep.ptr = getelementptr i32, i32* %ptr, i32 %p
store i32 %p, i32* %gep.ptr
%add.i = add nsw i32 %p, 1
%cmp = icmp slt i32 %add.i, 1000
br i1 %cmp, label %loop.header, label %exit

exit:
ret void
}

0 comments on commit 4efa097

Please sign in to comment.