Skip to content

Commit df73ee6

Browse files
alexey-bataevgiordano
authored andcommitted
[SLP]Fix PR101213: Reuse extractelement, only if its vector operand comes before new vector value.
When trying to reuse extractelement instruction, need to check that it is inserted into proper position. Its original vector operand should come before new vector value, otherwise new extractelement instruction must be generated. Fixes llvm#101213 (cherry picked from commit f70f122)
1 parent dae0320 commit df73ee6

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -13889,11 +13889,16 @@ Value *BoUpSLP::vectorizeTree(
1388913889
}
1389013890
if (!Ex) {
1389113891
// "Reuse" the existing extract to improve final codegen.
13892-
if (auto *ES = dyn_cast<ExtractElementInst>(Scalar)) {
13892+
if (auto *ES = dyn_cast<ExtractElementInst>(Scalar);
13893+
ES && isa<Instruction>(Vec)) {
1389313894
Value *V = ES->getVectorOperand();
1389413895
if (const TreeEntry *ETE = getTreeEntry(V))
1389513896
V = ETE->VectorizedValue;
13896-
Ex = Builder.CreateExtractElement(V, ES->getIndexOperand());
13897+
if (auto *IV = dyn_cast<Instruction>(V);
13898+
!IV || IV == Vec || (IV->getParent() == cast<Instruction>(Vec)->getParent() && IV->comesBefore(cast<Instruction>(Vec))))
13899+
Ex = Builder.CreateExtractElement(V, ES->getIndexOperand());
13900+
else
13901+
Ex = Builder.CreateExtractElement(Vec, Lane);
1389713902
} else if (ReplaceGEP) {
1389813903
// Leave the GEPs as is, they are free in most cases and better to
1389913904
// keep them as GEPs.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S --passes=slp-vectorizer -slp-threshold=-99999 < %s -mtriple=x86_64-unknown-linux-gnu | FileCheck %s
3+
4+
define void @test() {
5+
; CHECK-LABEL: define void @test() {
6+
; CHECK-NEXT: [[BB:.*]]:
7+
; CHECK-NEXT: br label %[[BB43:.*]]
8+
; CHECK: [[BB20:.*]]:
9+
; CHECK-NEXT: br label %[[BB105:.*]]
10+
; CHECK: [[BB43]]:
11+
; CHECK-NEXT: [[TMP0:%.*]] = phi <2 x ptr addrspace(1)> [ [[TMP1:%.*]], %[[BB51:.*]] ], [ zeroinitializer, %[[BB]] ]
12+
; CHECK-NEXT: br i1 false, label %[[BB105]], label %[[BB51]]
13+
; CHECK: [[BB51]]:
14+
; CHECK-NEXT: [[TMP1]] = phi <2 x ptr addrspace(1)> [ poison, %[[BB54:.*]] ], [ zeroinitializer, %[[BB43]] ]
15+
; CHECK-NEXT: br label %[[BB43]]
16+
; CHECK: [[BB54]]:
17+
; CHECK-NEXT: br label %[[BB51]]
18+
; CHECK: [[BB105]]:
19+
; CHECK-NEXT: [[PHI106:%.*]] = phi ptr addrspace(1) [ null, %[[BB20]] ], [ null, %[[BB43]] ]
20+
; CHECK-NEXT: ret void
21+
;
22+
bb:
23+
%0 = shufflevector <2 x ptr addrspace(1)> zeroinitializer, <2 x ptr addrspace(1)> zeroinitializer, <2 x i32> <i32 1, i32 0>
24+
%1 = extractelement <2 x ptr addrspace(1)> %0, i32 0
25+
%2 = extractelement <2 x ptr addrspace(1)> %0, i32 1
26+
br label %bb43
27+
28+
bb20:
29+
br label %bb105
30+
31+
bb43:
32+
%phi441 = phi ptr addrspace(1) [ %4, %bb51 ], [ %2, %bb ]
33+
%phi452 = phi ptr addrspace(1) [ %5, %bb51 ], [ %1, %bb ]
34+
br i1 false, label %bb105, label %bb51
35+
36+
bb51:
37+
%3 = phi <2 x ptr addrspace(1)> [ poison, %bb54 ], [ zeroinitializer, %bb43 ]
38+
%4 = extractelement <2 x ptr addrspace(1)> %3, i32 0
39+
%5 = extractelement <2 x ptr addrspace(1)> %3, i32 1
40+
br label %bb43
41+
42+
bb54:
43+
br label %bb51
44+
45+
bb105:
46+
%phi106 = phi ptr addrspace(1) [ %1, %bb20 ], [ null, %bb43 ]
47+
ret void
48+
}
49+

0 commit comments

Comments
 (0)