[llvm] [SimplifyLibCalls] Simplify cabs libcall if real or imaginary part of input is zero (PR #97976)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 10 01:22:02 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()) {
+ IRBuilderBase::FastMathFlagGuard Guard(B);
+ B.setFastMathFlags(CI->getFastMathFlags());
+ return copyFlags(
+ *CI, B.CreateUnaryIntrinsic(Intrinsic::fabs, Imag, nullptr, "cabs"));
----------------
arsenm wrote:
There's no reason to ever use a fabs libcall. Just always use the fabs intrinsic
https://github.com/llvm/llvm-project/pull/97976
More information about the llvm-commits
mailing list