[llvm] [InstCombine] Fold fneg over select (PR #89947)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 24 23:30:44 PDT 2024


================
@@ -2781,6 +2781,16 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {
       propagateSelectFMF(NewSel, P == X);
       return NewSel;
     }
+
+    // -(Cond ? X : C) --> Cond ? -X : -C
+    // -(Cond ? C : Y) --> Cond ? -C : -Y
+    if (match(X, m_ImmConstant()) || match(Y, m_ImmConstant())) {
+      Value *NegX = Builder.CreateFNegFMF(X, &I, X->getName() + ".neg");
+      Value *NegY = Builder.CreateFNegFMF(Y, &I, Y->getName() + ".neg");
+      SelectInst *NewSel = SelectInst::Create(Cond, NegX, NegY);
+      propagateSelectFMF(NewSel, /*CommonOperand=*/true);
+      return NewSel;
+    }
----------------
dtcxzyw wrote:

No. `FoldOpIntoSelect` doesn't propagate FMF flags from fneg to select.
See https://github.com/llvm/llvm-project/pull/86390. `nsz` flag is not always safe to be propagated. So they would be better to be handled separately.


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


More information about the llvm-commits mailing list