[llvm] [InstCombine] Don't fold `sin(-x)` to `-sin(x)` if `denormals` may flush to `+0.0` (PR #227039)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 1 05:06:53 PDT 2026


suogesi wrote:

I know this has been closed but, I want to know that if we split `optimizeSymmetricCall`, will it be more readable?

```cpp
static Value *optimizeEvenCall(CallInst *CI, IRBuilderBase &B) {
  Value *X;
  Value *Src = CI->getArgOperand(0);
  if (match(Src, m_OneUse(m_FNeg(m_Value(X)))) ||
      match(Src, m_FAbs(m_Value(X))) ||
      match(Src, m_CopySign(m_Value(X), m_Value()))) {
    // Even function: f(-x) = f(x), f(abs(x)) = f(x), f(copysign(x, y)) = f(x)
    auto *Call = B.CreateCall(CI->getCalledFunction(), {X}, /*FMFSource=*/CI);
    auto *CallInst = copyFlags(*CI, Call);
    return CallInst;
  }

  return nullptr;
}

static Value *optimizeOddCall(CallInst *CI, IRBuilderBase &B) {
  if (mayFlushDenormalsToPositiveZero(CI)) {
    return nullptr;
  }

  Value *X;
  Value *Src = CI->getArgOperand(0);
  if (match(Src, m_OneUse(m_FNeg(m_Value(X))))) {
    // Odd function: f(-x) = -f(x)
    auto *Call = B.CreateCall(CI->getCalledFunction(), {X}, /*FMFSource=*/CI);
    auto *CallInst = copyFlags(*CI, Call);
    return B.CreateFNegFMF(CallInst, CI);
  }

  return nullptr;
}
```

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


More information about the llvm-commits mailing list