Skip to content

Commit 6958fc1

Browse files
authored
[InstCombine] Only fold extract element to trunc if vector hasOneUse (llvm#115627) (llvm#999)
2 parents 8c64804 + 8346df5 commit 6958fc1

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ Instruction *InstCombinerImpl::foldBitcastExtElt(ExtractElementInst &Ext) {
205205
if (IsBigEndian)
206206
ExtIndexC = NumElts.getKnownMinValue() - 1 - ExtIndexC;
207207
unsigned ShiftAmountC = ExtIndexC * DestWidth;
208-
if (!ShiftAmountC ||
209-
(isDesirableIntType(X->getType()->getPrimitiveSizeInBits()) &&
210-
Ext.getVectorOperand()->hasOneUse())) {
208+
if ((!ShiftAmountC ||
209+
isDesirableIntType(X->getType()->getPrimitiveSizeInBits())) &&
210+
Ext.getVectorOperand()->hasOneUse()) {
211211
if (ShiftAmountC)
212212
X = Builder.CreateLShr(X, ShiftAmountC, "extelt.offset");
213213
if (DestTy->isFloatingPointTy()) {

llvm/test/Transforms/InstCombine/extractelement.ll

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -722,20 +722,14 @@ define i8 @bitcast_scalar_index_variable(i32 %x, i64 %y) {
722722
ret i8 %r
723723
}
724724

725-
; extra use is ok if we don't need a shift
725+
; extra use is not ok, even if we don't need a shift
726726

727727
define i8 @bitcast_scalar_index0_use(i64 %x) {
728-
; ANYLE-LABEL: @bitcast_scalar_index0_use(
729-
; ANYLE-NEXT: [[V:%.*]] = bitcast i64 [[X:%.*]] to <8 x i8>
730-
; ANYLE-NEXT: call void @use(<8 x i8> [[V]])
731-
; ANYLE-NEXT: [[R:%.*]] = trunc i64 [[X]] to i8
732-
; ANYLE-NEXT: ret i8 [[R]]
733-
;
734-
; ANYBE-LABEL: @bitcast_scalar_index0_use(
735-
; ANYBE-NEXT: [[V:%.*]] = bitcast i64 [[X:%.*]] to <8 x i8>
736-
; ANYBE-NEXT: call void @use(<8 x i8> [[V]])
737-
; ANYBE-NEXT: [[R:%.*]] = extractelement <8 x i8> [[V]], i64 0
738-
; ANYBE-NEXT: ret i8 [[R]]
728+
; ANY-LABEL: @bitcast_scalar_index0_use(
729+
; ANY-NEXT: [[V:%.*]] = bitcast i64 [[X:%.*]] to <8 x i8>
730+
; ANY-NEXT: call void @use(<8 x i8> [[V]])
731+
; ANY-NEXT: [[R:%.*]] = extractelement <8 x i8> [[V]], i64 0
732+
; ANY-NEXT: ret i8 [[R]]
739733
;
740734

741735
%v = bitcast i64 %x to <8 x i8>

llvm/test/Transforms/InstCombine/vector_insertelt_shuffle.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,26 @@ define <4 x float> @bazzzzzz(<4 x float> %x, i32 %a) {
9090
ret <4 x float> %ins1
9191
}
9292

93+
; test that foldBitcastExtElt doesn't interfere with shuffle folding
94+
95+
define <4 x half> @bitcast_extract_insert_to_shuffle(i32 %a, i32 %b) {
96+
; CHECK-LABEL: @bitcast_extract_insert_to_shuffle(
97+
; CHECK-NEXT: [[AVEC:%.*]] = bitcast i32 [[A:%.*]] to <2 x half>
98+
; CHECK-NEXT: [[BVEC:%.*]] = bitcast i32 [[B:%.*]] to <2 x half>
99+
; CHECK-NEXT: [[INS3:%.*]] = shufflevector <2 x half> [[AVEC]], <2 x half> [[BVEC]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
100+
; CHECK-NEXT: ret <4 x half> [[INS3]]
101+
;
102+
%avec = bitcast i32 %a to <2 x half>
103+
%a0 = extractelement <2 x half> %avec, i32 0
104+
%a1 = extractelement <2 x half> %avec, i32 1
105+
%bvec = bitcast i32 %b to <2 x half>
106+
%b0 = extractelement <2 x half> %bvec, i32 0
107+
%b1 = extractelement <2 x half> %bvec, i32 1
108+
%ins0 = insertelement <4 x half> undef, half %a0, i32 0
109+
%ins1 = insertelement <4 x half> %ins0, half %a1, i32 1
110+
%ins2 = insertelement <4 x half> %ins1, half %b0, i32 2
111+
%ins3 = insertelement <4 x half> %ins2, half %b1, i32 3
112+
ret <4 x half> %ins3
113+
}
114+
93115

0 commit comments

Comments
 (0)