[llvm] [VectorCombine] Support nary operands and intrinsics in scalarizeOpOrCmp (PR #138406)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Fri May 23 11:08:21 PDT 2025


================
@@ -1120,15 +1110,37 @@ bool VectorCombine::scalarizeBinopOrCmp(Instruction &I) {
     VectorOpCost = TTI.getIntrinsicInstrCost(VectorICA, CostKind);
   }
 
+  // Fold the vector constants in the original vectors into a new base vector to
+  // get more accurate cost modelling.
+  Value *NewVecC = nullptr;
+  if (auto *CI = dyn_cast<CmpInst>(&I))
+    NewVecC = ConstantFoldCompareInstOperands(CI->getPredicate(), VecCs[0],
+                                              VecCs[1], *DL);
+  else if (isa<UnaryOperator>(I))
+    NewVecC = ConstantFoldUnaryOpOperand((Instruction::UnaryOps)Opcode,
+                                         VecCs[0], *DL);
+  else if (isa<BinaryOperator>(I))
+    NewVecC = ConstantFoldBinaryOpOperands((Instruction::BinaryOps)Opcode,
+                                           VecCs[0], VecCs[1], *DL);
+  else if (isa<IntrinsicInst>(I) && cast<IntrinsicInst>(I).arg_size() == 2)
+    NewVecC =
+        ConstantFoldBinaryIntrinsic(cast<IntrinsicInst>(I).getIntrinsicID(),
----------------
lukel97 wrote:

Not that I could find, there's `ConstantFoldCall` but it looks like it doesn't handle any scalable intrinsics, apart from `aarch64_sve_convert_from_svbool`. For fixed vectors it actually calls ConstantFoldScalarCall on each element, which in turn calls `ConstantFoldIntrinsicCall2` for binary intrinsics. 

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


More information about the llvm-commits mailing list