From 64bcbff574f70beddfd8eccbf856ee29ab7a2a19 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 26 Jan 2026 16:53:21 +0100 Subject: [PATCH] [SimplifyLibCalls] Directly convert fmin/fmax to intrinsics Drop the custom shrinking code, which we'll also do for intrinsics. Having libcall-only optimizations is confusing, as these are typically directly emitted as intrinsics by the frontend. --- .../llvm/Transforms/Utils/SimplifyLibCalls.h | 2 +- .../lib/Transforms/Utils/SimplifyLibCalls.cpp | 19 ++++--------------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h index 64d2512308935..9ba3455254f47 100644 --- a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h +++ b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h @@ -204,7 +204,7 @@ class LibCallSimplifier { Value *replacePowWithExp(CallInst *Pow, IRBuilderBase &B); Value *replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B); Value *optimizeExp2(CallInst *CI, IRBuilderBase &B); - Value *optimizeFMinFMax(CallInst *CI, IRBuilderBase &B); + Value *optimizeFMinFMax(CallInst *CI, IRBuilderBase &B, Intrinsic::ID IID); Value *optimizeFMinimumnumFMaximumnum(CallInst *CI, IRBuilderBase &B); Value *optimizeLog(CallInst *CI, IRBuilderBase &B); Value *optimizeSqrt(CallInst *CI, IRBuilderBase &B); diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp index 14d82b5d8813c..d3ba4018845dd 100644 --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -2521,17 +2521,8 @@ Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) { return Ret; } -Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) { - Module *M = CI->getModule(); - - // If we can shrink the call to a float function rather than a double - // function, do that first. - Function *Callee = CI->getCalledFunction(); - StringRef Name = Callee->getName(); - if ((Name == "fmin" || Name == "fmax") && hasFloatVersion(M, Name)) - if (Value *Ret = optimizeBinaryDoubleFP(CI, B, TLI)) - return Ret; - +Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B, + Intrinsic::ID IID) { // The LLVM intrinsics minnum/maxnum correspond to fmin/fmax. Canonicalize to // the intrinsics for improved optimization (for example, vectorization). // No-signed-zeros is implied by the definitions of fmax/fmin themselves. @@ -2541,9 +2532,6 @@ Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) { // might be impractical." FastMathFlags FMF = CI->getFastMathFlags(); FMF.setNoSignedZeros(); - - Intrinsic::ID IID = Callee->getName().starts_with("fmin") ? Intrinsic::minnum - : Intrinsic::maxnum; return copyFlags(*CI, B.CreateBinaryIntrinsic(IID, CI->getArgOperand(0), CI->getArgOperand(1), FMF)); } @@ -4148,10 +4136,11 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI, case LibFunc_fminf: case LibFunc_fmin: case LibFunc_fminl: + return optimizeFMinFMax(CI, Builder, Intrinsic::minnum); case LibFunc_fmaxf: case LibFunc_fmax: case LibFunc_fmaxl: - return optimizeFMinFMax(CI, Builder); + return optimizeFMinFMax(CI, Builder, Intrinsic::maxnum); case LibFunc_fminimum_numf: case LibFunc_fminimum_num: case LibFunc_fminimum_numl: