Skip to content

Commit e170676

Browse files
[Instcombine] Combine extractelement from a vector_extract at index 0 (#151491)
Extracting any element from a subvector starting at index 0 is equivalent to extracting from the original vector, i.e. extract_elt(vector_extract(x, 0), y) -> extract_elt(x, y)
1 parent b0313ad commit e170676

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,13 @@ Instruction *InstCombinerImpl::visitExtractElementInst(ExtractElementInst &EI) {
462462
return ScalarPHI;
463463
}
464464

465+
// If SrcVec is a subvector starting at index 0, extract from the
466+
// wider source vector
467+
Value *V;
468+
if (match(SrcVec,
469+
m_Intrinsic<Intrinsic::vector_extract>(m_Value(V), m_Zero())))
470+
return ExtractElementInst::Create(V, Index);
471+
465472
// TODO come up with a n-ary matcher that subsumes both unary and
466473
// binary matchers.
467474
UnaryOperator *UO;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=instcombine < %s | FileCheck %s
3+
4+
define i1 @extract_const_idx(<vscale x 4 x i1> %a) {
5+
; CHECK-LABEL: define i1 @extract_const_idx(
6+
; CHECK-SAME: <vscale x 4 x i1> [[A:%.*]]) {
7+
; CHECK-NEXT: [[ELT:%.*]] = extractelement <vscale x 4 x i1> [[A]], i64 1
8+
; CHECK-NEXT: ret i1 [[ELT]]
9+
;
10+
%subvec = call <vscale x 2 x i1> @llvm.vector.extract.nxv2i1.nxv4i1.i64(<vscale x 4 x i1> %a, i64 0)
11+
%elt = extractelement <vscale x 2 x i1> %subvec, i32 1
12+
ret i1 %elt
13+
}
14+
15+
define float @extract_variable_idx(<vscale x 4 x float> %a, i32 %idx) {
16+
; CHECK-LABEL: define float @extract_variable_idx(
17+
; CHECK-SAME: <vscale x 4 x float> [[A:%.*]], i32 [[IDX:%.*]]) {
18+
; CHECK-NEXT: [[ELT:%.*]] = extractelement <vscale x 4 x float> [[A]], i32 [[IDX]]
19+
; CHECK-NEXT: ret float [[ELT]]
20+
;
21+
%subvec = call <vscale x 2 x float> @llvm.vector.extract.nxv2f32.nxv4f32.i64(<vscale x 4 x float> %a, i64 0)
22+
%elt = extractelement <vscale x 2 x float> %subvec, i32 %idx
23+
ret float %elt
24+
}
25+
26+
define float @negative_test(<vscale x 4 x float> %a) {
27+
; CHECK-LABEL: define float @negative_test(
28+
; CHECK-SAME: <vscale x 4 x float> [[A:%.*]]) {
29+
; CHECK-NEXT: [[SUBVEC:%.*]] = call <vscale x 2 x float> @llvm.vector.extract.nxv2f32.nxv4f32(<vscale x 4 x float> [[A]], i64 2)
30+
; CHECK-NEXT: [[ELT:%.*]] = extractelement <vscale x 2 x float> [[SUBVEC]], i64 1
31+
; CHECK-NEXT: ret float [[ELT]]
32+
;
33+
%subvec = call <vscale x 2 x float> @llvm.vector.extract.nxv2f32.nxv4f32.i64(<vscale x 4 x float> %a, i64 2)
34+
%elt = extractelement <vscale x 2 x float> %subvec, i32 1
35+
ret float %elt
36+
}

0 commit comments

Comments
 (0)