Skip to content

Commit 9402bb0

Browse files
[SLP]Do not count extractelement costs in unreachable/landing pad blocks.
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: #105667
1 parent 4d85285 commit 9402bb0

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10768,17 +10768,21 @@ InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals) {
1076810768
std::optional<DenseMap<Value *, unsigned>> ValueToExtUses;
1076910769
DenseMap<const TreeEntry *, DenseSet<Value *>> ExtractsCount;
1077010770
for (ExternalUser &EU : ExternalUses) {
10771+
// Uses by ephemeral values are free (because the ephemeral value will be
10772+
// removed prior to code generation, and so the extraction will be
10773+
// removed as well) as well as uses in unreachable blocks or in landing pads
10774+
// (rarely executed).
10775+
if (EphValues.count(EU.User) ||
10776+
(EU.User &&
10777+
(!DT->isReachableFromEntry(cast<Instruction>(EU.User)->getParent()) ||
10778+
cast<Instruction>(EU.User)->getParent()->isLandingPad())))
10779+
continue;
10780+
1077110781
// We only add extract cost once for the same scalar.
1077210782
if (!isa_and_nonnull<InsertElementInst>(EU.User) &&
1077310783
!ExtractCostCalculated.insert(EU.Scalar).second)
1077410784
continue;
1077510785

10776-
// Uses by ephemeral values are free (because the ephemeral value will be
10777-
// removed prior to code generation, and so the extraction will be
10778-
// removed as well).
10779-
if (EphValues.count(EU.User))
10780-
continue;
10781-
1078210786
// No extract cost for vector "scalar"
1078310787
if (isa<FixedVectorType>(EU.Scalar->getType()))
1078410788
continue;

llvm/test/Transforms/SLPVectorizer/X86/same-scalar-in-same-phi-extract.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ define void @test(i32 %arg) {
55
; CHECK-LABEL: define void @test(
66
; CHECK-SAME: i32 [[ARG:%.*]]) {
77
; CHECK-NEXT: bb:
8-
; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[ARG]] to i64
98
; CHECK-NEXT: [[TMP0:%.*]] = insertelement <2 x i32> <i32 poison, i32 0>, i32 [[ARG]], i32 0
109
; CHECK-NEXT: br label [[BB2:%.*]]
1110
; CHECK: bb2:
@@ -15,6 +14,8 @@ define void @test(i32 %arg) {
1514
; CHECK-NEXT: i32 1, label [[BB4:%.*]]
1615
; CHECK-NEXT: ]
1716
; CHECK: bb3:
17+
; CHECK-NEXT: [[TMP1:%.*]] = extractelement <2 x i32> [[TMP0]], i32 0
18+
; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[TMP1]] to i64
1819
; CHECK-NEXT: switch i32 0, label [[BB10]] [
1920
; CHECK-NEXT: i32 18, label [[BB7:%.*]]
2021
; CHECK-NEXT: i32 1, label [[BB7]]

0 commit comments

Comments
 (0)