Skip to content

Commit 8389177

Browse files
authored
SimplifyLibCalls: Use IRBuilder helpers for creating intrinsics (llvm#92288)
1 parent b5107bd commit 8389177

File tree

1 file changed

+29
-53
lines changed

1 file changed

+29
-53
lines changed

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,14 +1855,7 @@ Value *LibCallSimplifier::optimizeNew(CallInst *CI, IRBuilderBase &B,
18551855
// Replace a libcall \p CI with a call to intrinsic \p IID
18561856
static Value *replaceUnaryCall(CallInst *CI, IRBuilderBase &B,
18571857
Intrinsic::ID IID) {
1858-
// Propagate fast-math flags from the existing call to the new call.
1859-
IRBuilderBase::FastMathFlagGuard Guard(B);
1860-
B.setFastMathFlags(CI->getFastMathFlags());
1861-
1862-
Module *M = CI->getModule();
1863-
Value *V = CI->getArgOperand(0);
1864-
Function *F = Intrinsic::getDeclaration(M, IID, CI->getType());
1865-
CallInst *NewCall = B.CreateCall(F, V);
1858+
CallInst *NewCall = B.CreateUnaryIntrinsic(IID, CI->getArgOperand(0), CI);
18661859
NewCall->takeName(CI);
18671860
return copyFlags(*CI, NewCall);
18681861
}
@@ -1987,10 +1980,9 @@ Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilderBase &B) {
19871980
Value *RealReal = B.CreateFMul(Real, Real);
19881981
Value *ImagImag = B.CreateFMul(Imag, Imag);
19891982

1990-
Function *FSqrt = Intrinsic::getDeclaration(CI->getModule(), Intrinsic::sqrt,
1991-
CI->getType());
1992-
return copyFlags(
1993-
*CI, B.CreateCall(FSqrt, B.CreateFAdd(RealReal, ImagImag), "cabs"));
1983+
return copyFlags(*CI, B.CreateUnaryIntrinsic(Intrinsic::sqrt,
1984+
B.CreateFAdd(RealReal, ImagImag),
1985+
nullptr, "cabs"));
19941986
}
19951987

19961988
// Return a properly extended integer (DstWidth bits wide) if the operation is
@@ -2016,7 +2008,6 @@ static Value *getIntToFPVal(Value *I2F, IRBuilderBase &B, unsigned DstWidth) {
20162008
Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
20172009
Module *M = Pow->getModule();
20182010
Value *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
2019-
Module *Mod = Pow->getModule();
20202011
Type *Ty = Pow->getType();
20212012
bool Ignored;
20222013

@@ -2073,11 +2064,10 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
20732064
// Create new exp{,2}() with the product as its argument.
20742065
Value *FMul = B.CreateFMul(BaseFn->getArgOperand(0), Expo, "mul");
20752066
ExpFn = BaseFn->doesNotAccessMemory()
2076-
? B.CreateCall(Intrinsic::getDeclaration(Mod, ID, Ty),
2077-
FMul, ExpName)
2078-
: emitUnaryFloatFnCall(FMul, TLI, LibFnDouble, LibFnFloat,
2079-
LibFnLongDouble, B,
2080-
BaseFn->getAttributes());
2067+
? B.CreateUnaryIntrinsic(ID, FMul, nullptr, ExpName)
2068+
: emitUnaryFloatFnCall(FMul, TLI, LibFnDouble, LibFnFloat,
2069+
LibFnLongDouble, B,
2070+
BaseFn->getAttributes());
20812071

20822072
// Since the new exp{,2}() is different from the original one, dead code
20832073
// elimination cannot be trusted to remove it, since it may have side
@@ -2123,9 +2113,8 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
21232113
double N = NI.logBase2() * (IsReciprocal ? -1.0 : 1.0);
21242114
Value *FMul = B.CreateFMul(Expo, ConstantFP::get(Ty, N), "mul");
21252115
if (Pow->doesNotAccessMemory())
2126-
return copyFlags(*Pow, B.CreateCall(Intrinsic::getDeclaration(
2127-
Mod, Intrinsic::exp2, Ty),
2128-
FMul, "exp2"));
2116+
return copyFlags(*Pow, B.CreateUnaryIntrinsic(Intrinsic::exp2, FMul,
2117+
nullptr, "exp2"));
21292118
else
21302119
return copyFlags(*Pow, emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2,
21312120
LibFunc_exp2f,
@@ -2165,9 +2154,8 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
21652154
if (Log) {
21662155
Value *FMul = B.CreateFMul(Log, Expo, "mul");
21672156
if (Pow->doesNotAccessMemory())
2168-
return copyFlags(*Pow, B.CreateCall(Intrinsic::getDeclaration(
2169-
Mod, Intrinsic::exp2, Ty),
2170-
FMul, "exp2"));
2157+
return copyFlags(*Pow, B.CreateUnaryIntrinsic(Intrinsic::exp2, FMul,
2158+
nullptr, "exp2"));
21712159
else if (hasFloatFn(M, TLI, Ty, LibFunc_exp2, LibFunc_exp2f,
21722160
LibFunc_exp2l))
21732161
return copyFlags(*Pow, emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2,
@@ -2183,11 +2171,8 @@ static Value *getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno,
21832171
Module *M, IRBuilderBase &B,
21842172
const TargetLibraryInfo *TLI) {
21852173
// If errno is never set, then use the intrinsic for sqrt().
2186-
if (NoErrno) {
2187-
Function *SqrtFn =
2188-
Intrinsic::getDeclaration(M, Intrinsic::sqrt, V->getType());
2189-
return B.CreateCall(SqrtFn, V, "sqrt");
2190-
}
2174+
if (NoErrno)
2175+
return B.CreateUnaryIntrinsic(Intrinsic::sqrt, V, nullptr, "sqrt");
21912176

21922177
// Otherwise, use the libcall for sqrt().
21932178
if (hasFloatFn(M, TLI, V->getType(), LibFunc_sqrt, LibFunc_sqrtf,
@@ -2232,10 +2217,8 @@ Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) {
22322217
return nullptr;
22332218

22342219
// Handle signed zero base by expanding to fabs(sqrt(x)).
2235-
if (!Pow->hasNoSignedZeros()) {
2236-
Function *FAbsFn = Intrinsic::getDeclaration(Mod, Intrinsic::fabs, Ty);
2237-
Sqrt = B.CreateCall(FAbsFn, Sqrt, "abs");
2238-
}
2220+
if (!Pow->hasNoSignedZeros())
2221+
Sqrt = B.CreateUnaryIntrinsic(Intrinsic::fabs, Sqrt, nullptr, "abs");
22392222

22402223
Sqrt = copyFlags(*Pow, Sqrt);
22412224

@@ -2259,8 +2242,7 @@ static Value *createPowWithIntegerExponent(Value *Base, Value *Expo, Module *M,
22592242
IRBuilderBase &B) {
22602243
Value *Args[] = {Base, Expo};
22612244
Type *Types[] = {Base->getType(), Expo->getType()};
2262-
Function *F = Intrinsic::getDeclaration(M, Intrinsic::powi, Types);
2263-
return B.CreateCall(F, Args);
2245+
return B.CreateIntrinsic(Intrinsic::powi, Types, Args);
22642246
}
22652247

22662248
Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) {
@@ -2442,9 +2424,8 @@ Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) {
24422424

24432425
Intrinsic::ID IID = Callee->getName().starts_with("fmin") ? Intrinsic::minnum
24442426
: Intrinsic::maxnum;
2445-
Function *F = Intrinsic::getDeclaration(CI->getModule(), IID, CI->getType());
2446-
return copyFlags(
2447-
*CI, B.CreateCall(F, {CI->getArgOperand(0), CI->getArgOperand(1)}));
2427+
return copyFlags(*CI, B.CreateBinaryIntrinsic(IID, CI->getArgOperand(0),
2428+
CI->getArgOperand(1)));
24482429
}
24492430

24502431
Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
@@ -2563,8 +2544,7 @@ Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
25632544
if (ArgLb == PowLb || ArgID == Intrinsic::pow || ArgID == Intrinsic::powi) {
25642545
Value *LogX =
25652546
Log->doesNotAccessMemory()
2566-
? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty),
2567-
Arg->getOperand(0), "log")
2547+
? B.CreateUnaryIntrinsic(LogID, Arg->getOperand(0), nullptr, "log")
25682548
: emitUnaryFloatFnCall(Arg->getOperand(0), TLI, LogNm, B, NoAttrs);
25692549
Value *Y = Arg->getArgOperand(1);
25702550
// Cast exponent to FP if integer.
@@ -2590,8 +2570,7 @@ Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
25902570
else
25912571
Eul = ConstantFP::get(Log->getType(), 10.0);
25922572
Value *LogE = Log->doesNotAccessMemory()
2593-
? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty),
2594-
Eul, "log")
2573+
? B.CreateUnaryIntrinsic(LogID, Eul, nullptr, "log")
25952574
: emitUnaryFloatFnCall(Eul, TLI, LogNm, B, NoAttrs);
25962575
Value *MulY = B.CreateFMul(Arg->getArgOperand(0), LogE, "mul");
25972576
// Since exp() may have side effects, e.g. errno,
@@ -2725,15 +2704,14 @@ Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
27252704

27262705
// If we found a repeated factor, hoist it out of the square root and
27272706
// replace it with the fabs of that factor.
2728-
Type *ArgType = I->getType();
2729-
Function *Fabs = Intrinsic::getDeclaration(M, Intrinsic::fabs, ArgType);
2730-
Value *FabsCall = B.CreateCall(Fabs, RepeatOp, "fabs");
2707+
Value *FabsCall =
2708+
B.CreateUnaryIntrinsic(Intrinsic::fabs, RepeatOp, nullptr, "fabs");
27312709
if (OtherOp) {
27322710
// If we found a non-repeated factor, we still need to get its square
27332711
// root. We then multiply that by the value that was simplified out
27342712
// of the square root calculation.
2735-
Function *Sqrt = Intrinsic::getDeclaration(M, Intrinsic::sqrt, ArgType);
2736-
Value *SqrtCall = B.CreateCall(Sqrt, OtherOp, "sqrt");
2713+
Value *SqrtCall =
2714+
B.CreateUnaryIntrinsic(Intrinsic::sqrt, OtherOp, nullptr, "sqrt");
27372715
return copyFlags(*CI, B.CreateFMul(FabsCall, SqrtCall));
27382716
}
27392717
return copyFlags(*CI, FabsCall);
@@ -3002,9 +2980,8 @@ Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilderBase &B) {
30022980
Type *RetType = CI->getType();
30032981
Value *Op = CI->getArgOperand(0);
30042982
Type *ArgType = Op->getType();
3005-
Function *F = Intrinsic::getDeclaration(CI->getCalledFunction()->getParent(),
3006-
Intrinsic::cttz, ArgType);
3007-
Value *V = B.CreateCall(F, {Op, B.getTrue()}, "cttz");
2983+
Value *V = B.CreateIntrinsic(Intrinsic::cttz, {ArgType}, {Op, B.getTrue()},
2984+
nullptr, "cttz");
30082985
V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
30092986
V = B.CreateIntCast(V, RetType, false);
30102987

@@ -3017,9 +2994,8 @@ Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilderBase &B) {
30172994
// fls{,l,ll}(x) -> (int)(sizeInBits(x) - llvm.ctlz(x, false))
30182995
Value *Op = CI->getArgOperand(0);
30192996
Type *ArgType = Op->getType();
3020-
Function *F = Intrinsic::getDeclaration(CI->getCalledFunction()->getParent(),
3021-
Intrinsic::ctlz, ArgType);
3022-
Value *V = B.CreateCall(F, {Op, B.getFalse()}, "ctlz");
2997+
Value *V = B.CreateIntrinsic(Intrinsic::ctlz, {ArgType}, {Op, B.getFalse()},
2998+
nullptr, "ctlz");
30232999
V = B.CreateSub(ConstantInt::get(V->getType(), ArgType->getIntegerBitWidth()),
30243000
V);
30253001
return B.CreateIntCast(V, CI->getType(), false);

0 commit comments

Comments
 (0)