[llvm] [Instcombine] Combine extractelement from a vector_extract at index 0 (PR #151491)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 31 03:47:47 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Kerry McLaughlin (kmclaughlin-arm)

<details>
<summary>Changes</summary>

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)

---
Full diff: https://github.com/llvm/llvm-project/pull/151491.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp (+5-1) 
- (added) llvm/test/Transforms/InstCombine/scalable-extract-subvec-elt.ll (+13) 


``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
index 00b877b8a07ef..6f2adba9e3f6b 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -444,7 +444,11 @@ Instruction *InstCombinerImpl::visitExtractElementInst(ExtractElementInst &EI) {
         else
           Idx = PoisonValue::get(Ty);
         return replaceInstUsesWith(EI, Idx);
-      }
+      } else if (IID == Intrinsic::vector_extract)
+        // If II is a subvector starting at index 0, extract from the wider
+        // source vector
+        if (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 0)
+          return ExtractElementInst::Create(II->getArgOperand(0), Index);
     }
 
     // InstSimplify should handle cases where the index is invalid.
diff --git a/llvm/test/Transforms/InstCombine/scalable-extract-subvec-elt.ll b/llvm/test/Transforms/InstCombine/scalable-extract-subvec-elt.ll
new file mode 100644
index 0000000000000..38cbeb0df98bd
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/scalable-extract-subvec-elt.ll
@@ -0,0 +1,13 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+define i1 @scalable_test(<vscale x 4 x i1> %a) {
+; CHECK-LABEL: define i1 @scalable_test(
+; CHECK-SAME: <vscale x 4 x i1> [[A:%.*]]) {
+; CHECK-NEXT:    [[ELT:%.*]] = extractelement <vscale x 4 x i1> [[A]], i64 1
+; CHECK-NEXT:    ret i1 [[ELT]]
+;
+  %subvec = call <vscale x 2 x i1> @llvm.vector.extract.nxv2i1.nxv4i1.i64(<vscale x 4 x i1> %a, i64 0)
+  %elt = extractelement <vscale x 2 x i1> %subvec, i32 1
+  ret i1 %elt
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/151491


More information about the llvm-commits mailing list