[llvm] [SimplifyLibCalls] Simplify cabs libcall if real or imaginary part of input is zero (PR #97976)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sun Jul 7 20:21:43 PDT 2024
================
@@ -1880,25 +1881,45 @@ static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilderBase &B,
// cabs(z) -> sqrt((creal(z)*creal(z)) + (cimag(z)*cimag(z)))
Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilderBase &B) {
- if (!CI->isFast())
- return nullptr;
-
- // Propagate fast-math flags from the existing call to new instructions.
- IRBuilderBase::FastMathFlagGuard Guard(B);
- B.setFastMathFlags(CI->getFastMathFlags());
-
Value *Real, *Imag;
+
if (CI->arg_size() == 1) {
Value *Op = CI->getArgOperand(0);
assert(Op->getType()->isArrayTy() && "Unexpected signature for cabs!");
Real = B.CreateExtractValue(Op, 0, "real");
Imag = B.CreateExtractValue(Op, 1, "imag");
+
} else {
assert(CI->arg_size() == 2 && "Unexpected signature for cabs!");
Real = CI->getArgOperand(0);
Imag = CI->getArgOperand(1);
}
+ // if real or imaginary part is zero, simplify to abs(cimag(z))
+ // or abs(creal(z))
+ if (ConstantFP *ConstReal = dyn_cast<ConstantFP>(Real)) {
+ if (ConstReal->isZeroValue()) {
----------------
dtcxzyw wrote:
It also holds for `-0`. Please add some tests for `cabs(-0, x)/cabs(x, -0) -> fabs(x)`.
```suggestion
if (ConstReal->isZero()) {
```
https://github.com/llvm/llvm-project/pull/97976
More information about the llvm-commits
mailing list