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

Joshua Cranmer via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 13 11:56:35 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);
----------------
jcranmer-intel wrote:

On a side note, I wonder if commuting the sign with an odd function is an improvement in practice. For even functions, we're clearly getting rid of an instruction, which is almost always a win. But for odd functions, it's just moving the instruction, and it's not offhand clear to me if peephole opportunities tend to be better with it being before or after the libm function.

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


More information about the llvm-commits mailing list