[PATCH] D128890: [InstCombine] remove useless insertelement
Chenbing.Zheng via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 4 19:02:32 PDT 2022
Chenbing.Zheng updated this revision to Diff 442161.
Chenbing.Zheng added a comment.
add fixme comment
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D128890/new/
https://reviews.llvm.org/D128890
Files:
llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
llvm/test/Transforms/InstCombine/vscale_extractelement-inseltpoison.ll
llvm/test/Transforms/InstCombine/vscale_extractelement.ll
Index: llvm/test/Transforms/InstCombine/vscale_extractelement.ll
===================================================================
--- llvm/test/Transforms/InstCombine/vscale_extractelement.ll
+++ llvm/test/Transforms/InstCombine/vscale_extractelement.ll
@@ -41,12 +41,10 @@
ret i8 %r
}
-; TODO: Instcombine could remove the insert.
define i8 @extractelement_bitcast_wrong_insert(<vscale x 2 x i32> %a, i32 %x) {
; CHECK-LABEL: @extractelement_bitcast_wrong_insert(
-; CHECK-NEXT: [[VEC:%.*]] = insertelement <vscale x 2 x i32> [[A:%.*]], i32 [[X:%.*]], i64 1
-; CHECK-NEXT: [[VEC_CAST:%.*]] = bitcast <vscale x 2 x i32> [[VEC]] to <vscale x 8 x i8>
-; CHECK-NEXT: [[R:%.*]] = extractelement <vscale x 8 x i8> [[VEC_CAST]], i64 2
+; CHECK-NEXT: [[TMP1:%.*]] = bitcast <vscale x 2 x i32> [[A:%.*]] to <vscale x 8 x i8>
+; CHECK-NEXT: [[R:%.*]] = extractelement <vscale x 8 x i8> [[TMP1]], i64 2
; CHECK-NEXT: ret i8 [[R]]
;
%vec = insertelement <vscale x 2 x i32> %a, i32 %x, i32 1 ; <- This insert could be removed.
Index: llvm/test/Transforms/InstCombine/vscale_extractelement-inseltpoison.ll
===================================================================
--- llvm/test/Transforms/InstCombine/vscale_extractelement-inseltpoison.ll
+++ llvm/test/Transforms/InstCombine/vscale_extractelement-inseltpoison.ll
@@ -41,12 +41,10 @@
ret i8 %r
}
-; TODO: Instcombine could remove the insert.
define i8 @extractelement_bitcast_wrong_insert(<vscale x 2 x i32> %a, i32 %x) {
; CHECK-LABEL: @extractelement_bitcast_wrong_insert(
-; CHECK-NEXT: [[VEC:%.*]] = insertelement <vscale x 2 x i32> [[A:%.*]], i32 [[X:%.*]], i64 1
-; CHECK-NEXT: [[VEC_CAST:%.*]] = bitcast <vscale x 2 x i32> [[VEC]] to <vscale x 8 x i8>
-; CHECK-NEXT: [[R:%.*]] = extractelement <vscale x 8 x i8> [[VEC_CAST]], i64 2
+; CHECK-NEXT: [[TMP1:%.*]] = bitcast <vscale x 2 x i32> [[A:%.*]] to <vscale x 8 x i8>
+; CHECK-NEXT: [[R:%.*]] = extractelement <vscale x 8 x i8> [[TMP1]], i64 2
; CHECK-NEXT: ret i8 [[R]]
;
%vec = insertelement <vscale x 2 x i32> %a, i32 %x, i32 1 ; <- This insert could be removed.
Index: llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -228,8 +228,9 @@
// truncate a subset of scalar bits of an insert op.
if (NumSrcElts.getKnownMinValue() < NumElts.getKnownMinValue()) {
Value *Scalar;
+ Value *Vec;
uint64_t InsIndexC;
- if (!match(X, m_InsertElt(m_Value(), m_Value(Scalar),
+ if (!match(X, m_InsertElt(m_Value(Vec), m_Value(Scalar),
m_ConstantInt(InsIndexC))))
return nullptr;
@@ -239,8 +240,19 @@
// of elements 4-7 of the bitcasted vector.
unsigned NarrowingRatio =
NumElts.getKnownMinValue() / NumSrcElts.getKnownMinValue();
- if (ExtIndexC / NarrowingRatio != InsIndexC)
+
+ if (ExtIndexC / NarrowingRatio != InsIndexC) {
+ // Remove insertelement, if we don't use the inserted element.
+ // extractelement (bitcast (insertelement (Vec, b)), a) ->
+ // extractelement (bitcast (Vec), a)
+ // FIXME: this should be removed to SimplifyDemandedVectorElts,
+ // once scale vectors are supported
+ if (X->hasOneUse() && Ext.getVectorOperand()->hasOneUse()) {
+ Value *NewBC = Builder.CreateBitCast(Vec, Ext.getVectorOperandType());
+ return ExtractElementInst::Create(NewBC, Ext.getIndexOperand());
+ }
return nullptr;
+ }
// We are extracting part of the original scalar. How that scalar is
// inserted into the vector depends on the endian-ness. Example:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128890.442161.patch
Type: text/x-patch
Size: 3778 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220705/bafb1a80/attachment.bin>
More information about the llvm-commits
mailing list