Skip to content

Commit

Permalink
[SLP]Do not count extractelement costs in unreachable/landing pad blo…
Browse files Browse the repository at this point in the history
…cks.

If the external user of the scalar to be extract is in
unreachable/landing pad block, we can skip counting their cost.

Reviewers: RKSimon

Reviewed By: RKSimon

Pull Request: llvm#105667
  • Loading branch information
alexey-bataev authored Aug 22, 2024
1 parent 4d85285 commit 9402bb0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 10 additions & 6 deletions llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10768,17 +10768,21 @@ InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals) {
std::optional<DenseMap<Value *, unsigned>> ValueToExtUses;
DenseMap<const TreeEntry *, DenseSet<Value *>> ExtractsCount;
for (ExternalUser &EU : ExternalUses) {
// Uses by ephemeral values are free (because the ephemeral value will be
// removed prior to code generation, and so the extraction will be
// removed as well) as well as uses in unreachable blocks or in landing pads
// (rarely executed).
if (EphValues.count(EU.User) ||
(EU.User &&
(!DT->isReachableFromEntry(cast<Instruction>(EU.User)->getParent()) ||
cast<Instruction>(EU.User)->getParent()->isLandingPad())))
continue;

// We only add extract cost once for the same scalar.
if (!isa_and_nonnull<InsertElementInst>(EU.User) &&
!ExtractCostCalculated.insert(EU.Scalar).second)
continue;

// Uses by ephemeral values are free (because the ephemeral value will be
// removed prior to code generation, and so the extraction will be
// removed as well).
if (EphValues.count(EU.User))
continue;

// No extract cost for vector "scalar"
if (isa<FixedVectorType>(EU.Scalar->getType()))
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ define void @test(i32 %arg) {
; CHECK-LABEL: define void @test(
; CHECK-SAME: i32 [[ARG:%.*]]) {
; CHECK-NEXT: bb:
; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[ARG]] to i64
; CHECK-NEXT: [[TMP0:%.*]] = insertelement <2 x i32> <i32 poison, i32 0>, i32 [[ARG]], i32 0
; CHECK-NEXT: br label [[BB2:%.*]]
; CHECK: bb2:
Expand All @@ -15,6 +14,8 @@ define void @test(i32 %arg) {
; CHECK-NEXT: i32 1, label [[BB4:%.*]]
; CHECK-NEXT: ]
; CHECK: bb3:
; CHECK-NEXT: [[TMP1:%.*]] = extractelement <2 x i32> [[TMP0]], i32 0
; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[TMP1]] to i64
; CHECK-NEXT: switch i32 0, label [[BB10]] [
; CHECK-NEXT: i32 18, label [[BB7:%.*]]
; CHECK-NEXT: i32 1, label [[BB7]]
Expand Down

0 comments on commit 9402bb0

Please sign in to comment.