[llvm] [InstCombine] Fix a cycle when folding fneg(select) with scalable vector types (PR #112465)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 17 00:10:02 PDT 2024


================
@@ -2878,13 +2878,22 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {
 
     // -(Cond ? X : C) --> Cond ? -X : -C
     // -(Cond ? C : Y) --> Cond ? -C : -Y
-    if ((match(X, m_ImmConstant()) && !isa<ScalableVectorType>(X->getType())) ||
-        (match(Y, m_ImmConstant()) && !isa<ScalableVectorType>(Y->getType()))) {
-      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;
+    Constant *XC = nullptr, *YC = nullptr;
+    if (match(X, m_ImmConstant(XC)) || match(Y, m_ImmConstant(YC))) {
+      Value *NegX = nullptr, *NegY = nullptr;
+      if (XC)
+        NegX = ConstantFoldUnaryOpOperand(Instruction::FNeg, XC, DL);
+      if (YC)
+        NegY = ConstantFoldUnaryOpOperand(Instruction::FNeg, YC, DL);
----------------
nikic wrote:

Right, just the changes to ConstantFold should be sufficient, after that there should not be a need to touch this InstCombine code anymore...

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


More information about the llvm-commits mailing list