[llvm] [InstCombine] Fold binary op of reductions. (PR #121567)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 3 10:18:11 PST 2025


================
@@ -2296,6 +2296,58 @@ Instruction *InstCombinerImpl::foldVectorBinop(BinaryOperator &Inst) {
   return nullptr;
 }
 
+static Intrinsic::ID getReductionForBinop(Instruction::BinaryOps Opc) {
+  switch (Opc) {
+  default:
+    break;
+  case Instruction::Add:
+    return Intrinsic::vector_reduce_add;
+  case Instruction::Mul:
+    return Intrinsic::vector_reduce_mul;
+  case Instruction::And:
+    return Intrinsic::vector_reduce_and;
+  case Instruction::Or:
+    return Intrinsic::vector_reduce_or;
+  case Instruction::Xor:
+    return Intrinsic::vector_reduce_xor;
+  }
+  return Intrinsic::num_intrinsics;
+}
+
+Instruction *InstCombinerImpl::foldBinopOfReductions(BinaryOperator &Inst) {
+  IntrinsicInst *II0 = dyn_cast<IntrinsicInst>(Inst.getOperand(0));
+  if (!II0)
+    return nullptr;
+  IntrinsicInst *II1 = dyn_cast<IntrinsicInst>(Inst.getOperand(1));
+  if (!II1)
+    return nullptr;
+
+  Instruction::BinaryOps BinOpOpc = Inst.getOpcode();
+  Intrinsic::ID ReductionIID = getReductionForBinop(BinOpOpc);
+  if (BinOpOpc == Instruction::Sub)
+    ReductionIID = Intrinsic::vector_reduce_add;
+
+  if (ReductionIID == Intrinsic::num_intrinsics)
+    return nullptr;
----------------
goldsteinn wrote:

You can drop this check as no intrin will be `Instrinsic::no_intrinsic`

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


More information about the llvm-commits mailing list