[PATCH] D146349: [InstCombine] Make `FoldOpIntoSelect` handle non-constants and use condition to deduce constants.

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 9 17:15:43 PDT 2023


goldstein.w.n updated this revision to Diff 512055.
goldstein.w.n added a comment.

Rebase


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146349/new/

https://reviews.llvm.org/D146349

Files:
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp


Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1026,22 +1026,38 @@
   return SelectInst::Create(X, TVal, FVal);
 }
 
-static Constant *constantFoldOperationIntoSelectOperand(
-    Instruction &I, SelectInst *SI, Value *SO) {
-  auto *ConstSO = dyn_cast<Constant>(SO);
-  if (!ConstSO)
-    return nullptr;
-
-  SmallVector<Constant *> ConstOps;
+static SmallVector<Value *>
+getInstSelectArmOperands(Instruction &I, SelectInst *SI, bool IsTrueArm) {
+  SmallVector<Value *> Ops;
   for (Value *Op : I.operands()) {
+    CmpInst::Predicate Pred;
+    Constant *CondC;
     if (Op == SI)
-      ConstOps.push_back(ConstSO);
-    else if (auto *C = dyn_cast<Constant>(Op))
+      Ops.push_back(IsTrueArm ? SI->getTrueValue() : SI->getFalseValue());
+    else if (match(SI->getCondition(),
+                   m_ICmp(Pred, m_Specific(Op), m_Constant(CondC))) &&
+             Pred == (IsTrueArm ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE))
+      Ops.push_back(CondC);
+    else
+      Ops.push_back(Op);
+  }
+
+  return Ops;
+}
+
+static Constant *constantFoldOperationIntoSelectOperand(Instruction &I,
+                                                        SelectInst *SI,
+                                                        bool IsTrueArm) {
+  SmallVector<Constant *> ConstOps;
+  SmallVector<Value *> Ops = getInstSelectArmOperands(I, SI, IsTrueArm);
+  for (Value *Op : Ops) {
+    if (auto *C = dyn_cast<Constant>(Op))
       ConstOps.push_back(C);
     else
       return nullptr;
   }
-  return ConstantFoldInstOperands(&I, ConstOps, I.getModule()->getDataLayout());
+  return ConstantFoldInstOperands(&I, ConstOps,
+                                  I.getModule()->getDataLayout());
 }
 
 static Value *foldOperationIntoSelectOperand(Instruction &I, SelectInst *SI,
@@ -1083,8 +1099,8 @@
   }
 
   // Make sure that one of the select arms constant folds successfully.
-  Value *NewTV = constantFoldOperationIntoSelectOperand(Op, SI, TV);
-  Value *NewFV = constantFoldOperationIntoSelectOperand(Op, SI, FV);
+  Value *NewTV = constantFoldOperationIntoSelectOperand(Op, SI, /*IsTrueArm*/ true);
+  Value *NewFV = constantFoldOperationIntoSelectOperand(Op, SI, /*IsTrueArm*/ false);
   if (!NewTV && !NewFV)
     return nullptr;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146349.512055.patch
Type: text/x-patch
Size: 2451 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230410/26264875/attachment.bin>


More information about the llvm-commits mailing list