[llvm] [InstCombine] Handle more even/odd math functions (PR #81324)

Artem Tyurin via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 13 16:17:44 PST 2024


================
@@ -2797,6 +2784,50 @@ static bool insertSinCosCall(IRBuilderBase &B, Function *OrigCallee, Value *Arg,
   return true;
 }
 
+static Value *optimizeSymmetricCall(CallInst *CI, bool IsEven,
+                                    IRBuilderBase &B) {
+  Value *X;
+  if (match(CI->getArgOperand(0), m_OneUse(m_FNeg(m_Value(X))))) {
+    IRBuilderBase::FastMathFlagGuard Guard(B);
+    B.setFastMathFlags(CI->getFastMathFlags());
+
+    auto *CallInst = copyFlags(*CI, B.CreateCall(CI->getCalledFunction(), {X}));
+    if (IsEven) {
+      // Even function: f(-x) = f(x)
+      return CallInst;
+    }
+    // Odd function: f(-x) = -f(x)
+    return B.CreateFNeg(CallInst);
----------------
agentcooper wrote:

I guess one option is to look only for cases like `-f(-x)` where `f` is odd, so it can be replaced with just `f(x)`.

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


More information about the llvm-commits mailing list