[llvm] [VectorCombine] Add type shrinking and zext propagation for fixed-width vector types (PR #104606)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 16 08:39:14 PDT 2024


================
@@ -2493,6 +2494,106 @@ bool VectorCombine::foldSelectShuffle(Instruction &I, bool FromReduction) {
   return true;
 }
 
+/// Check if instruction depends on ZExt and this ZExt can be moved after the
+/// instruction. Move ZExt if it is profitable
+bool VectorCombine::shrinkType(llvm::Instruction &I) {
+  Value *ZExted, *OtherOperand;
+  if (match(&I, m_c_BinOp(m_ZExt(m_Value(ZExted)), m_Value(OtherOperand)))) {
+    if (I.getOpcode() != Instruction::And && I.getOpcode() != Instruction::Or &&
+        I.getOpcode() != Instruction::Xor && I.getOpcode() != Instruction::LShr)
+      return false;
+
+    // In case of LShr extraction, ZExtOperand should be applied to the first
+    // operand
+    if (I.getOpcode() == Instruction::LShr && I.getOperand(1) != OtherOperand)
+      return false;
+
+    Instruction *ZExtOperand = cast<Instruction>(
+        I.getOperand(I.getOperand(0) == OtherOperand ? 1 : 0));
+
+    auto *BigTy = cast<FixedVectorType>(I.getType());
+    auto *SmallTy = cast<FixedVectorType>(ZExted->getType());
+    auto BW = SmallTy->getElementType()->getPrimitiveSizeInBits();
+
+    // Check that the expression overall uses at most the same number of bits as
+    // ZExted
+    auto KB = computeKnownBits(&I, *DL);
+    auto IBW = KB.getBitWidth() - KB.Zero.countLeadingOnes();
----------------
RKSimon wrote:

(style) Don't use auto for non-obvious types

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


More information about the llvm-commits mailing list