[llvm] [InstCombine] Fold `select (A &/| B), T, F` if `select B, T, F` is foldable (PR #76621)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 30 11:21:46 PST 2023


================
@@ -3793,5 +3794,51 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
   if (Instruction *I = foldBitCeil(SI, Builder))
     return I;
 
+  // Fold:
+  // (select A && B, T, F) -> (select A, (select B, T, F), F)
+  // (select A || B, T, F) -> (select A, T, (select B, T, F))
+  // if (select B, T, F) is foldable.
+  // TODO: preserve FMF flags
+  auto FoldSelectWithAndOrCond = [&](bool IsAnd, Value *A,
+                                     Value *B) -> Instruction * {
+    if (Value *V = simplifySelectInst(B, TrueVal, FalseVal,
+                                      SQ.getWithInstruction(&SI)))
+      return SelectInst::Create(A, IsAnd ? V : TrueVal, IsAnd ? FalseVal : V);
+
+    // Is (select B, T, F) a SPF?
+    if (CondVal->hasOneUse() && SelType->isIntOrIntVectorTy()) {
----------------
dtcxzyw wrote:

It would be profitable if we can fold more decomposed patterns here (e.g., `foldAndOrOfICmps`).
But I don't think it is a scalable approach.
See also https://github.com/llvm/llvm-project/issues/76623#issuecomment-1872587241.

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


More information about the llvm-commits mailing list