[llvm] [InstCombine] Avoid breaking reduction in `FoldOpIntoSelect` (PR #148616)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 14 04:56:18 PDT 2025


https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/148616

None

>From e9a9371d9df7130d7d0004c59bb86757acc89c42 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 14 Jul 2025 19:55:02 +0800
Subject: [PATCH] test

---
 .../InstCombine/InstructionCombining.cpp        | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 91a1b61ddc483..dd03ab8f08f85 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1738,14 +1738,17 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
   if (SI->getType()->isIntOrIntVectorTy(1))
     return nullptr;
 
-  // Avoid breaking min/max reduction pattern,
+  // Avoid breaking reduction pattern,
   // which is necessary for vectorization later.
-  if (isa<MinMaxIntrinsic>(&Op))
-    for (Value *IntrinOp : Op.operands())
-      if (auto *PN = dyn_cast<PHINode>(IntrinOp))
-        for (Value *PhiOp : PN->operands())
-          if (PhiOp == &Op)
-            return nullptr;
+  PHINode *PhiNode;
+  Value *Start, *Step;
+  if (auto *BinOp = dyn_cast<BinaryOperator>(&Op)) {
+    if (matchSimpleRecurrence(BinOp, PhiNode, Start, Step))
+      return nullptr;
+  } else if (auto *II = dyn_cast<IntrinsicInst>(&Op)) {
+    if (matchSimpleBinaryIntrinsicRecurrence(II, PhiNode, Start, Step))
+      return nullptr;
+  }
 
   // Test if a FCmpInst instruction is used exclusively by a select as
   // part of a minimum or maximum operation. If so, refrain from doing



More information about the llvm-commits mailing list