[llvm] [InstCombine] Fix constant swap case of fcmp + fadd + sel xfrm (PR #119419)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 10 17:52:07 PST 2024


================
@@ -3762,7 +3750,26 @@ static Value *foldSelectIntoAddConstant(SelectInst &SI,
     cast<Instruction>(NewSelect)->setFastMathFlags(NewFMF);
 
     return NewFAdd;
-  }
+  };
+
+  // select((fcmp Pred, X, 0), (fadd X, C), C)
+  //      => fadd((select (fcmp Pred, X, 0), X, 0), C)
+  //
+  // Pred := OGT, OGE, OLT, OLE, UGT, UGE, ULT, and ULE
+  Instruction *FAdd;
+  Constant *C;
+  Value *X, *Z;
+  CmpInst::Predicate Pred;
+
+  // Note: OneUse check for `Cmp` is necessary because it makes sure that other
+  // InstCombine folds don't undo this transformation and cause an infinite
+  // loop. Furthermore, it could also increase the operation count.
+  if (match(&SI, m_Select(m_OneUse(m_FCmp(Pred, m_Value(X), m_Value(Z))),
+                          m_OneUse(m_Instruction(FAdd)), m_Constant(C))))
+    return TryFoldIntoAddConstant(Pred, X, Z, FAdd, C, false);
----------------
dtcxzyw wrote:

```suggestion
    return TryFoldIntoAddConstant(Pred, X, Z, FAdd, C, /*Swapped=*/false);
```

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


More information about the llvm-commits mailing list