Skip to content

Commit a1c5347

Browse files
committedJan 5, 2016
[InstCombine] insert a new shuffle before its uses (PR26015)
Although this solves the test case in PR26015: https://llvm.org/bugs/show_bug.cgi?id=26015 And may solve PR25999: https://llvm.org/bugs/show_bug.cgi?id=25999 ...I suspect this is not the best solution. I think we want to insert the new shuffle just ahead of the earliest ExtractElementInst that we're replacing, but I don't know how that should be implemented. Differential Revision: http://reviews.llvm.org/D15878 llvm-svn: 256857
1 parent 0aa9f7f commit a1c5347

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed
 

‎llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

+21-8
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,28 @@ static void replaceExtractElements(InsertElementInst *InsElt,
383383
auto *WideVec = new ShuffleVectorInst(ExtVecOp, UndefValue::get(ExtVecType),
384384
ConstantVector::get(ExtendMask));
385385

386-
// Replace all extracts from the original narrow vector with extracts from
387-
// the new wide vector.
388-
WideVec->insertBefore(ExtElt);
386+
// Insert the new shuffle after the vector operand of the extract is defined
387+
// or at the start of the basic block, so any subsequent extracts can use it.
388+
bool ReplaceAllExtUsers;
389+
if (auto *ExtVecOpInst = dyn_cast<Instruction>(ExtVecOp)) {
390+
WideVec->insertAfter(ExtVecOpInst);
391+
ReplaceAllExtUsers = true;
392+
} else {
393+
// TODO: Insert at start of function, so it's always safe to replace all?
394+
IC.InsertNewInstWith(WideVec, *ExtElt->getParent()->getFirstInsertionPt());
395+
ReplaceAllExtUsers = false;
396+
}
397+
398+
// Replace extracts from the original narrow vector with extracts from the new
399+
// wide vector.
389400
for (User *U : ExtVecOp->users()) {
390-
if (ExtractElementInst *OldExt = dyn_cast<ExtractElementInst>(U)) {
391-
auto *NewExt = ExtractElementInst::Create(WideVec, OldExt->getOperand(1));
392-
NewExt->insertAfter(WideVec);
393-
IC.ReplaceInstUsesWith(*OldExt, NewExt);
394-
}
401+
ExtractElementInst *OldExt = dyn_cast<ExtractElementInst>(U);
402+
if (!OldExt ||
403+
(!ReplaceAllExtUsers && OldExt->getParent() != WideVec->getParent()))
404+
continue;
405+
auto *NewExt = ExtractElementInst::Create(WideVec, OldExt->getOperand(1));
406+
NewExt->insertAfter(WideVec);
407+
IC.ReplaceInstUsesWith(*OldExt, NewExt);
395408
}
396409
}
397410

‎llvm/test/Transforms/InstCombine/insert-extract-shuffle.ll

+53
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,56 @@ define <8 x float> @widen_extract4(<8 x float> %ins, <2 x float> %ext) {
7272
ret <8 x float> %i1
7373
}
7474

75+
; PR26015: https://llvm.org/bugs/show_bug.cgi?id=26015
76+
; The widening shuffle must be inserted before any uses.
77+
78+
define <8 x i16> @pr26015(<4 x i16> %t0) {
79+
; CHECK-LABEL: @pr26015(
80+
; CHECK-NEXT: %[[WIDEVEC:.*]] = shufflevector <4 x i16> %t0, <4 x i16> undef, <8 x i32> <i32 undef, i32 undef, i32 undef, i32 3, i32 undef, i32 undef, i32 undef, i32 undef>
81+
; CHECK-NEXT: %[[EXT:.*]] = extractelement <4 x i16> %t0, i32 2
82+
; CHECK-NEXT: %t2 = insertelement <8 x i16> <i16 0, i16 0, i16 0, i16 undef, i16 0, i16 0, i16 undef, i16 undef>, i16 %[[EXT]], i32 3
83+
; CHECK-NEXT: %t3 = insertelement <8 x i16> %t2, i16 0, i32 6
84+
; CHECK-NEXT: %t5 = shufflevector <8 x i16> %t3, <8 x i16> %[[WIDEVEC]], <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 11>
85+
; CHECK-NEXT: ret <8 x i16> %t5
86+
%t1 = extractelement <4 x i16> %t0, i32 2
87+
%t2 = insertelement <8 x i16> zeroinitializer, i16 %t1, i32 3
88+
%t3 = insertelement <8 x i16> %t2, i16 0, i32 6
89+
%t4 = extractelement <4 x i16> %t0, i32 3
90+
%t5 = insertelement <8 x i16> %t3, i16 %t4, i32 7
91+
ret <8 x i16> %t5
92+
}
93+
94+
; PR25999: https://llvm.org/bugs/show_bug.cgi?id=25999
95+
; TODO: The widening shuffle could be inserted at the start of the function to allow the first extract to use it.
96+
97+
define <8 x i16> @pr25999(<4 x i16> %t0, i1 %b) {
98+
; CHECK-LABEL: @pr25999(
99+
; CHECK-NEXT: %t1 = extractelement <4 x i16> %t0, i32 2
100+
; CHECK-NEXT: br i1 %b, label %if, label %end
101+
; CHECK: if:
102+
; CHECK-NEXT: %[[WIDEVEC:.*]] = shufflevector <4 x i16> %t0, <4 x i16> undef, <8 x i32> <i32 undef, i32 undef, i32 undef, i32 3, i32 undef, i32 undef, i32 undef, i32 undef>
103+
; CHECK-NEXT: %t2 = insertelement <8 x i16> <i16 0, i16 0, i16 0, i16 undef, i16 0, i16 0, i16 undef, i16 undef>, i16 %t1, i32 3
104+
; CHECK-NEXT: %t3 = insertelement <8 x i16> %t2, i16 0, i32 6
105+
; CHECK-NEXT: %t5 = shufflevector <8 x i16> %t3, <8 x i16> %[[WIDEVEC]], <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 11>
106+
; CHECK-NEXT: ret <8 x i16> %t5
107+
; CHECK: end:
108+
; CHECK-NEXT: %a1 = add i16 %t1, 4
109+
; CHECK-NEXT: %t6 = insertelement <8 x i16> <i16 undef, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0, i16 0>, i16 %a1, i32 0
110+
; CHECK-NEXT: ret <8 x i16> %t6
111+
112+
%t1 = extractelement <4 x i16> %t0, i32 2
113+
br i1 %b, label %if, label %end
114+
115+
if:
116+
%t2 = insertelement <8 x i16> zeroinitializer, i16 %t1, i32 3
117+
%t3 = insertelement <8 x i16> %t2, i16 0, i32 6
118+
%t4 = extractelement <4 x i16> %t0, i32 3
119+
%t5 = insertelement <8 x i16> %t3, i16 %t4, i32 7
120+
ret <8 x i16> %t5
121+
122+
end:
123+
%a1 = add i16 %t1, 4
124+
%t6 = insertelement <8 x i16> zeroinitializer, i16 %a1, i32 0
125+
ret <8 x i16> %t6
126+
}
127+

0 commit comments

Comments
 (0)
Please sign in to comment.