[llvm] [VectorCombine] shrinkType - only attempt to copy flags/metadata if we create a new instruction (PR #225776)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 23 06:51:48 PDT 2026


https://github.com/RKSimon created https://github.com/llvm/llvm-project/pull/225776

CreateBinOp may fold to a constant/argument value etc.

Fixes #225272

>From 497db96d3755bd95a55727cbb3bd18b2e81dc71f Mon Sep 17 00:00:00 2001
From: Simon Pilgrim <llvm-dev at redking.me.uk>
Date: Wed, 23 Sep 2026 14:50:50 +0100
Subject: [PATCH] [VectorCombine] shrinkType - only attempt to copy
 flags/metadata if we create a new instruction

CreateBinOp may fold to a constant/argument value etc.

Fixes #225272
---
 llvm/lib/Transforms/Vectorize/VectorCombine.cpp |  6 ++++--
 llvm/test/Transforms/VectorCombine/pr225272.ll  | 12 ++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/Transforms/VectorCombine/pr225272.ll

diff --git a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
index 1767ed66f7ddd1..ddb06610d6572f 100644
--- a/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
+++ b/llvm/lib/Transforms/Vectorize/VectorCombine.cpp
@@ -5930,8 +5930,10 @@ bool VectorCombine::shrinkType(Instruction &I) {
     std::swap(Op0, Op1);
   Value *NewBinOp =
       Builder.CreateBinOp((Instruction::BinaryOps)I.getOpcode(), Op0, Op1);
-  cast<Instruction>(NewBinOp)->copyIRFlags(&I);
-  cast<Instruction>(NewBinOp)->copyMetadata(I);
+  if (auto *NewBinOpI = dyn_cast<Instruction>(NewBinOp)) {
+    NewBinOpI->copyIRFlags(&I);
+    NewBinOpI->copyMetadata(I);
+  }
   Value *NewZExtr = Builder.CreateZExt(NewBinOp, BigTy);
   replaceValue(I, *NewZExtr);
   return true;
diff --git a/llvm/test/Transforms/VectorCombine/pr225272.ll b/llvm/test/Transforms/VectorCombine/pr225272.ll
new file mode 100644
index 00000000000000..f0264aebf21feb
--- /dev/null
+++ b/llvm/test/Transforms/VectorCombine/pr225272.ll
@@ -0,0 +1,12 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=vector-combine -S %s | FileCheck %s
+
+define <4 x i8> @PR225272(<4 x i4> %x) {
+; CHECK-LABEL: @PR225272(
+; CHECK-NEXT:    [[SHIFTED:%.*]] = zext <4 x i4> [[X:%.*]] to <4 x i8>
+; CHECK-NEXT:    ret <4 x i8> [[SHIFTED]]
+;
+  %wide = zext <4 x i4> %x to <4 x i8>
+  %shifted = lshr <4 x i8> %wide, zeroinitializer
+  ret <4 x i8> %shifted
+}



More information about the llvm-commits mailing list