@@ -1855,14 +1855,7 @@ Value *LibCallSimplifier::optimizeNew(CallInst *CI, IRBuilderBase &B,
1855
1855
// Replace a libcall \p CI with a call to intrinsic \p IID
1856
1856
static Value *replaceUnaryCall (CallInst *CI, IRBuilderBase &B,
1857
1857
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);
1866
1859
NewCall->takeName (CI);
1867
1860
return copyFlags (*CI, NewCall);
1868
1861
}
@@ -1987,10 +1980,9 @@ Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilderBase &B) {
1987
1980
Value *RealReal = B.CreateFMul (Real, Real);
1988
1981
Value *ImagImag = B.CreateFMul (Imag, Imag);
1989
1982
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" ));
1994
1986
}
1995
1987
1996
1988
// 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) {
2016
2008
Value *LibCallSimplifier::replacePowWithExp (CallInst *Pow, IRBuilderBase &B) {
2017
2009
Module *M = Pow->getModule ();
2018
2010
Value *Base = Pow->getArgOperand (0 ), *Expo = Pow->getArgOperand (1 );
2019
- Module *Mod = Pow->getModule ();
2020
2011
Type *Ty = Pow->getType ();
2021
2012
bool Ignored;
2022
2013
@@ -2073,11 +2064,10 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
2073
2064
// Create new exp{,2}() with the product as its argument.
2074
2065
Value *FMul = B.CreateFMul (BaseFn->getArgOperand (0 ), Expo, " mul" );
2075
2066
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 ());
2081
2071
2082
2072
// Since the new exp{,2}() is different from the original one, dead code
2083
2073
// elimination cannot be trusted to remove it, since it may have side
@@ -2123,9 +2113,8 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
2123
2113
double N = NI.logBase2 () * (IsReciprocal ? -1.0 : 1.0 );
2124
2114
Value *FMul = B.CreateFMul (Expo, ConstantFP::get (Ty, N), " mul" );
2125
2115
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" ));
2129
2118
else
2130
2119
return copyFlags (*Pow, emitUnaryFloatFnCall (FMul, TLI, LibFunc_exp2,
2131
2120
LibFunc_exp2f,
@@ -2165,9 +2154,8 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
2165
2154
if (Log) {
2166
2155
Value *FMul = B.CreateFMul (Log, Expo, " mul" );
2167
2156
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" ));
2171
2159
else if (hasFloatFn (M, TLI, Ty, LibFunc_exp2, LibFunc_exp2f,
2172
2160
LibFunc_exp2l))
2173
2161
return copyFlags (*Pow, emitUnaryFloatFnCall (FMul, TLI, LibFunc_exp2,
@@ -2183,11 +2171,8 @@ static Value *getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno,
2183
2171
Module *M, IRBuilderBase &B,
2184
2172
const TargetLibraryInfo *TLI) {
2185
2173
// 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" );
2191
2176
2192
2177
// Otherwise, use the libcall for sqrt().
2193
2178
if (hasFloatFn (M, TLI, V->getType (), LibFunc_sqrt, LibFunc_sqrtf,
@@ -2232,10 +2217,8 @@ Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) {
2232
2217
return nullptr ;
2233
2218
2234
2219
// 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" );
2239
2222
2240
2223
Sqrt = copyFlags (*Pow, Sqrt);
2241
2224
@@ -2259,8 +2242,7 @@ static Value *createPowWithIntegerExponent(Value *Base, Value *Expo, Module *M,
2259
2242
IRBuilderBase &B) {
2260
2243
Value *Args[] = {Base, Expo};
2261
2244
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);
2264
2246
}
2265
2247
2266
2248
Value *LibCallSimplifier::optimizePow (CallInst *Pow, IRBuilderBase &B) {
@@ -2442,9 +2424,8 @@ Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) {
2442
2424
2443
2425
Intrinsic::ID IID = Callee->getName ().starts_with (" fmin" ) ? Intrinsic::minnum
2444
2426
: 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 )));
2448
2429
}
2449
2430
2450
2431
Value *LibCallSimplifier::optimizeLog (CallInst *Log, IRBuilderBase &B) {
@@ -2563,8 +2544,7 @@ Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
2563
2544
if (ArgLb == PowLb || ArgID == Intrinsic::pow || ArgID == Intrinsic::powi) {
2564
2545
Value *LogX =
2565
2546
Log->doesNotAccessMemory ()
2566
- ? B.CreateCall (Intrinsic::getDeclaration (Mod, LogID, Ty),
2567
- Arg->getOperand (0 ), " log" )
2547
+ ? B.CreateUnaryIntrinsic (LogID, Arg->getOperand (0 ), nullptr , " log" )
2568
2548
: emitUnaryFloatFnCall (Arg->getOperand (0 ), TLI, LogNm, B, NoAttrs);
2569
2549
Value *Y = Arg->getArgOperand (1 );
2570
2550
// Cast exponent to FP if integer.
@@ -2590,8 +2570,7 @@ Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
2590
2570
else
2591
2571
Eul = ConstantFP::get (Log->getType (), 10.0 );
2592
2572
Value *LogE = Log->doesNotAccessMemory ()
2593
- ? B.CreateCall (Intrinsic::getDeclaration (Mod, LogID, Ty),
2594
- Eul, " log" )
2573
+ ? B.CreateUnaryIntrinsic (LogID, Eul, nullptr , " log" )
2595
2574
: emitUnaryFloatFnCall (Eul, TLI, LogNm, B, NoAttrs);
2596
2575
Value *MulY = B.CreateFMul (Arg->getArgOperand (0 ), LogE, " mul" );
2597
2576
// Since exp() may have side effects, e.g. errno,
@@ -2725,15 +2704,14 @@ Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
2725
2704
2726
2705
// If we found a repeated factor, hoist it out of the square root and
2727
2706
// 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" );
2731
2709
if (OtherOp) {
2732
2710
// If we found a non-repeated factor, we still need to get its square
2733
2711
// root. We then multiply that by the value that was simplified out
2734
2712
// 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" );
2737
2715
return copyFlags (*CI, B.CreateFMul (FabsCall, SqrtCall));
2738
2716
}
2739
2717
return copyFlags (*CI, FabsCall);
@@ -3002,9 +2980,8 @@ Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilderBase &B) {
3002
2980
Type *RetType = CI->getType ();
3003
2981
Value *Op = CI->getArgOperand (0 );
3004
2982
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" );
3008
2985
V = B.CreateAdd (V, ConstantInt::get (V->getType (), 1 ));
3009
2986
V = B.CreateIntCast (V, RetType, false );
3010
2987
@@ -3017,9 +2994,8 @@ Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilderBase &B) {
3017
2994
// fls{,l,ll}(x) -> (int)(sizeInBits(x) - llvm.ctlz(x, false))
3018
2995
Value *Op = CI->getArgOperand (0 );
3019
2996
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" );
3023
2999
V = B.CreateSub (ConstantInt::get (V->getType (), ArgType->getIntegerBitWidth ()),
3024
3000
V);
3025
3001
return B.CreateIntCast (V, CI->getType (), false );
0 commit comments