Lines Matching defs:B
138 uint64_t Base, bool AsSigned, IRBuilderBase &B) {
226 Value *Off = B.getInt64(Offset + Str.size());
228 Value *StrEnd = B.CreateInBoundsGEP(B.getInt8Ty(), StrBeg, Off, "endptr");
229 B.CreateStore(StrEnd, EndPtr);
329 // easier chaining. Calls to emit* and B.createCall should probably be wrapped
357 Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilderBase &B) {
375 return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, Len, B));
379 IRBuilderBase &B) {
382 Value *DstLen = emitStrLen(Dst, B, DL, TLI);
389 Value *CpyDst = B.CreateInBoundsGEP(B.getInt8Ty(), Dst, DstLen, "endptr");
393 B.CreateMemCpy(
399 Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilderBase &B) {
439 return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, SrcLen, B));
446 IRBuilderBase &B, const DataLayout &DL)
452 Type *CharTy = B.getInt8Ty();
453 Value *Char0 = B.CreateLoad(CharTy, Src);
454 CharVal = B.CreateTrunc(CharVal, CharTy);
455 Value *Cmp = B.CreateICmpEQ(Char0, CharVal, "char0cmp");
459 Value *And = B.CreateICmpNE(NBytes, Zero);
460 Cmp = B.CreateLogicalAnd(And, Cmp);
464 return B.CreateSelect(Cmp, Src, NullPtr);
467 Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilderBase &B) {
473 return memChrToCharCompare(CI, nullptr, B, DL);
495 ConstantInt::get(SizeTTy, Len), B,
504 return B.CreateIntToPtr(B.getTrue(), CI->getType());
512 if (Value *StrLen = emitStrLen(SrcStr, B, DL, TLI))
513 return B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, StrLen, "strchr");
526 return B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strchr");
529 Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilderBase &B) {
539 return copyFlags(*CI, emitStrChr(SrcStr, '\0', B, TLI));
550 return copyFlags(*CI, emitMemRChr(SrcStr, CharVal, Size, B, DL, TLI));
553 Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilderBase &B) {
568 return B.CreateNeg(B.CreateZExt(
569 B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
572 return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"),
588 B, DL, TLI));
598 B, DL, TLI));
605 B, DL, TLI));
616 IRBuilderBase &B, const DataLayout &DL);
618 Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilderBase &B) {
632 return optimizeMemCmpVarSize(CI, Str1P, Str2P, Size, true, B, DL);
638 return copyFlags(*CI, emitMemCmp(Str1P, Str2P, Size, B, DL, TLI));
654 return B.CreateNeg(B.CreateZExt(
655 B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
658 return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"),
676 B, DL, TLI));
684 B, DL, TLI));
690 Value *LibCallSimplifier::optimizeStrNDup(CallInst *CI, IRBuilderBase &B) {
697 return copyFlags(*CI, emitStrDup(Src, B, TLI));
703 Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilderBase &B) {
719 B.CreateMemCpy(Dst, Align(1), Src, Align(1),
725 Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilderBase &B) {
731 return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
734 Value *StrLen = emitStrLen(Src, B, DL, TLI);
735 return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
747 Value *DstEnd = B.CreateInBoundsGEP(
748 B.getInt8Ty(), Dst, ConstantInt::get(DL.getIntPtrType(PT), Len - 1));
752 CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1), LenV);
759 Value *LibCallSimplifier::optimizeStrLCpy(CallInst *CI, IRBuilderBase &B) {
780 B.CreateStore(B.getInt8(0), Dst);
783 return copyFlags(*CI, emitStrLen(Src, B, DL, TLI));
811 B.CreateStore(B.getInt8(0), Dst);
820 CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1),
826 Value *EndPtr = B.CreateInBoundsGEP(B.getInt8Ty(), Dst, EndOff);
827 B.CreateStore(B.getInt8(0), EndPtr);
839 IRBuilderBase &B) {
863 Type *CharTy = B.getInt8Ty();
864 Value *CharVal = B.CreateLoad(CharTy, Src, "stxncpy.char0");
865 B.CreateStore(CharVal, Dst);
872 Value *Cmp = B.CreateICmpEQ(CharVal, ZeroChar, "stpncpy.char0cmp");
874 Value *Off1 = B.getInt32(1);
875 Value *EndPtr = B.CreateInBoundsGEP(CharTy, Dst, Off1, "stpncpy.end");
876 return B.CreateSelect(Cmp, Dst, EndPtr, "stpncpy.sel");
892 CallInst *NewCI = B.CreateMemSet(Dst, B.getInt8('\0'), Size, MemSetAlign);
913 Src = B.CreateGlobalString(SrcStr, "str");
919 CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1),
927 Value *Off = B.getInt64(std::min(SrcLen, N));
928 return B.CreateInBoundsGEP(B.getInt8Ty(), Dst, Off, "endptr");
931 Value *LibCallSimplifier::optimizeStringLength(CallInst *CI, IRBuilderBase &B,
935 Type *CharTy = B.getIntNTy(CharSize);
945 return B.CreateZExt(B.CreateLoad(CharTy, Src, "char0"),
957 Value *CharVal = B.CreateLoad(CharTy, Src, "strnlen.char0");
959 Value *Cmp = B.CreateICmpNE(CharVal, ZeroChar, "strnlen.char0cmp");
960 return B.CreateZExt(Cmp, CI->getType());
970 return B.CreateBinaryIntrinsic(Intrinsic::umin, LenC, Bound);
1022 Offset = B.CreateSExtOrTrunc(Offset, CI->getType());
1023 return B.CreateSub(ConstantInt::get(CI->getType(), NullTermIdx),
1038 return B.CreateSelect(SI->getCondition(),
1047 Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilderBase &B) {
1048 if (Value *V = optimizeStringLength(CI, B, 8))
1054 Value *LibCallSimplifier::optimizeStrNLen(CallInst *CI, IRBuilderBase &B) {
1056 if (Value *V = optimizeStringLength(CI, B, 8, Bound))
1064 Value *LibCallSimplifier::optimizeWcslen(CallInst *CI, IRBuilderBase &B) {
1071 return optimizeStringLength(CI, B, WCharSize);
1074 Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilderBase &B) {
1090 return B.CreateInBoundsGEP(B.getInt8Ty(), CI->getArgOperand(0),
1091 B.getInt64(I), "strpbrk");
1096 return copyFlags(*CI, emitStrChr(CI->getArgOperand(0), S2[0], B, TLI));
1101 Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilderBase &B) {
1112 Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilderBase &B) {
1133 Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilderBase &B) {
1152 return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B, DL, TLI));
1157 Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilderBase &B) {
1164 Value *StrLen = emitStrLen(CI->getArgOperand(1), B, DL, TLI);
1168 StrLen, B, DL, TLI);
1174 B.CreateICmp(Old->getPredicate(), StrNCmp,
1198 return B.CreateConstInBoundsGEP1_64(B.getInt8Ty(), CI->getArgOperand(0),
1204 return emitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI);
1211 Value *LibCallSimplifier::optimizeMemRChr(CallInst *CI, IRBuilderBase &B) {
1227 Value *Val = B.CreateLoad(B.getInt8Ty(), SrcStr, "memrchr.char0");
1229 CharVal = B.CreateTrunc(CharVal, B.getInt8Ty());
1230 Value *Cmp = B.CreateICmpEQ(Val, CharVal, "memrchr.char0cmp");
1231 return B.CreateSelect(Cmp, SrcStr, NullPtr, "memrchr.sel");
1263 return B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, B.getInt64(Pos));
1270 Value *Cmp = B.CreateICmpULE(Size, ConstantInt::get(Size->getType(), Pos),
1272 Value *SrcPlus = B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr,
1273 B.getInt64(Pos), "memrchr.ptr_plus");
1274 return B.CreateSelect(Cmp, NullPtr, SrcPlus, "memrchr.sel");
1287 Type *Int8Ty = B.getInt8Ty();
1288 Value *NNeZ = B.CreateICmpNE(Size, ConstantInt::get(SizeTy, 0));
1290 CharVal = B.CreateTrunc(CharVal, Int8Ty);
1291 Value *CEqS0 = B.CreateICmpEQ(ConstantInt::get(Int8Ty, Str[0]), CharVal);
1292 Value *And = B.CreateLogicalAnd(NNeZ, CEqS0);
1293 Value *SizeM1 = B.CreateSub(Size, ConstantInt::get(SizeTy, 1));
1295 B.CreateInBoundsGEP(Int8Ty, SrcStr, SizeM1, "memrchr.ptr_plus");
1296 return B.CreateSelect(And, SrcPlus, NullPtr, "memrchr.sel");
1299 Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) {
1306 return memChrToCharCompare(CI, Size, B, DL);
1322 Value *Val = B.CreateLoad(B.getInt8Ty(), SrcStr, "memchr.char0");
1324 CharVal = B.CreateTrunc(CharVal, B.getInt8Ty());
1325 Value *Cmp = B.CreateICmpEQ(Val, CharVal, "memchr.char0cmp");
1326 return B.CreateSelect(Cmp, SrcStr, NullPtr, "memchr.sel");
1344 Value *Cmp = B.CreateICmpULE(Size, ConstantInt::get(Size->getType(), Pos),
1346 Value *SrcPlus = B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, B.getInt64(Pos),
1348 return B.CreateSelect(Cmp, NullPtr, SrcPlus);
1373 Type *Int8Ty = B.getInt8Ty();
1376 CharVal = B.CreateTrunc(CharVal, Int8Ty);
1383 Value *CEqSPos = B.CreateICmpEQ(CharVal, StrPos);
1384 Value *NGtPos = B.CreateICmp(ICmpInst::ICMP_UGT, Size, PosVal);
1385 Value *And = B.CreateAnd(CEqSPos, NGtPos);
1386 Value *SrcPlus = B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, PosVal);
1387 Sel1 = B.CreateSelect(And, SrcPlus, NullPtr, "memchr.sel1");
1391 Value *CEqS0 = B.CreateICmpEQ(Str0, CharVal);
1392 Value *NNeZ = B.CreateICmpNE(Size, ConstantInt::get(SizeTy, 0));
1393 Value *And = B.CreateAnd(NNeZ, CEqS0);
1394 return B.CreateSelect(And, SrcStr, Sel1, "memchr.sel2");
1402 return memChrToCharCompare(CI, Size, B, DL);
1458 B.CreateICmpEQ(CharVal, ConstantInt::get(CharVal->getType(), C)));
1460 return B.CreateIntToPtr(B.CreateOr(CharCompares), CI->getType());
1471 Value *BitfieldC = B.getInt(Bitfield);
1474 Value *C = B.CreateZExtOrTrunc(CharVal, BitfieldC->getType());
1475 C = B.CreateAnd(C, B.getIntN(Width, 0xFF));
1478 Value *Bounds = B.CreateICmp(ICmpInst::ICMP_ULT, C, B.getIntN(Width, Width),
1482 Value *Shl = B.CreateShl(B.getIntN(Width, 1ULL), C);
1483 Value *Bits = B.CreateIsNotNull(B.CreateAnd(Shl, BitfieldC), "memchr.bits");
1487 return B.CreateIntToPtr(B.CreateLogicalAnd(Bounds, Bits, "memchr"),
1495 IRBuilderBase &B, const DataLayout &DL) {
1505 // memcmp(A, B, N) to
1506 // N <= Pos ? 0 : (A < B ? -1 : B < A ? +1 : 0)
1507 // where Pos is the first mismatch between A and B, determined below.
1529 Value *Cmp = B.CreateICmp(ICmpInst::ICMP_ULE, Size, MaxSize);
1531 return B.CreateSelect(Cmp, Zero, Res);
1536 uint64_t Len, IRBuilderBase &B,
1543 Value *LHSV = B.CreateZExt(B.CreateLoad(B.getInt8Ty(), LHS, "lhsc"),
1545 Value *RHSV = B.CreateZExt(B.CreateLoad(B.getInt8Ty(), RHS, "rhsc"),
1547 return B.CreateSub(LHSV, RHSV, "chardiff");
1571 LHSV = B.CreateLoad(IntType, LHS, "lhsv");
1573 RHSV = B.CreateLoad(IntType, RHS, "rhsv");
1574 return B.CreateZExt(B.CreateICmpNE(LHSV, RHSV), CI->getType(), "memcmp");
1583 IRBuilderBase &B) {
1589 if (Value *Res = optimizeMemCmpVarSize(CI, LHS, RHS, Size, false, B, DL))
1597 return optimizeMemCmpConstantSize(CI, LHS, RHS, LenC->getZExtValue(), B, DL);
1600 Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilderBase &B) {
1602 if (Value *V = optimizeMemCmpBCmpCommon(CI, B))
1613 return copyFlags(*CI, emitBCmp(LHS, RHS, Size, B, DL, TLI));
1619 Value *LibCallSimplifier::optimizeBCmp(CallInst *CI, IRBuilderBase &B) {
1620 return optimizeMemCmpBCmpCommon(CI, B);
1623 Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilderBase &B) {
1630 CallInst *NewCI = B.CreateMemCpy(CI->getArgOperand(0), Align(1),
1636 Value *LibCallSimplifier::optimizeMemCCpy(CallInst *CI, IRBuilderBase &B) {
1660 copyFlags(*CI, B.CreateMemCpy(Dst, Align(1), Src, Align(1),
1670 copyFlags(*CI, B.CreateMemCpy(Dst, Align(1), Src, Align(1), NewN));
1672 ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, NewN)
1676 Value *LibCallSimplifier::optimizeMemPCpy(CallInst *CI, IRBuilderBase &B) {
1681 B.CreateMemCpy(Dst, Align(1), CI->getArgOperand(1), Align(1), N);
1686 return B.CreateInBoundsGEP(B.getInt8Ty(), Dst, N);
1689 Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilderBase &B) {
1696 CallInst *NewCI = B.CreateMemMove(CI->getArgOperand(0), Align(1),
1702 Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilderBase &B) {
1709 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
1710 CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val, Size, Align(1));
1715 Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilderBase &B) {
1717 return copyFlags(*CI, emitMalloc(CI->getArgOperand(1), B, DL, TLI));
1726 Value *LibCallSimplifier::optimizeNew(CallInst *CI, IRBuilderBase &B,
1752 return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1757 return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1762 return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1767 return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1773 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1779 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1785 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1791 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1797 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1803 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1809 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1815 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1821 CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
1828 CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
1835 CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
1842 CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
1857 static Value *replaceUnaryCall(CallInst *CI, IRBuilderBase &B,
1859 CallInst *NewCall = B.CreateUnaryIntrinsic(IID, CI->getArgOperand(0), CI);
1887 static Value *optimizeDoubleFP(CallInst *CI, IRBuilderBase &B,
1927 IRBuilderBase::FastMathFlagGuard Guard(B);
1928 B.setFastMathFlags(CI->getFastMathFlags());
1935 Function *Fn = Intrinsic::getDeclaration(M, IID, B.getFloatTy());
1936 R = isBinary ? B.CreateCall(Fn, V) : B.CreateCall(Fn, V[0]);
1939 R = isBinary ? emitBinaryFloatFnCall(V[0], V[1], TLI, CalleeName, B,
1941 : emitUnaryFloatFnCall(V[0], TLI, CalleeName, B, CalleeAttrs);
1943 return B.CreateFPExt(R, B.getDoubleTy());
1947 static Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilderBase &B,
1950 return optimizeDoubleFP(CI, B, false, TLI, isPrecise);
1954 static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilderBase &B,
1957 return optimizeDoubleFP(CI, B, true, TLI, isPrecise);
1961 Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilderBase &B) {
1972 Real = B.CreateExtractValue(Op, 0, "real");
1973 Imag = B.CreateExtractValue(Op, 1, "imag");
1994 IRBuilderBase::FastMathFlagGuard Guard(B);
1995 B.setFastMathFlags(CI->getFastMathFlags());
1998 *CI, B.CreateUnaryIntrinsic(Intrinsic::fabs, AbsOp, nullptr, "cabs"));
2006 IRBuilderBase::FastMathFlagGuard Guard(B);
2007 B.setFastMathFlags(CI->getFastMathFlags());
2009 Value *RealReal = B.CreateFMul(Real, Real);
2010 Value *ImagImag = B.CreateFMul(Imag, Imag);
2012 return copyFlags(*CI, B.CreateUnaryIntrinsic(Intrinsic::sqrt,
2013 B.CreateFAdd(RealReal, ImagImag),
2019 static Value *getIntToFPVal(Value *I2F, IRBuilderBase &B, unsigned DstWidth) {
2027 return isa<SIToFPInst>(I2F) ? B.CreateSExt(Op, IntTy)
2028 : B.CreateZExt(Op, IntTy);
2038 Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
2095 Value *FMul = B.CreateFMul(BaseFn->getArgOperand(0), Expo, "mul");
2097 ? B.CreateUnaryIntrinsic(ID, FMul, nullptr, ExpName)
2099 LibFnLongDouble, B,
2129 if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize())) {
2133 return copyFlags(*Pow, B.CreateIntrinsic(Intrinsic::ldexp,
2140 LibFunc_ldexpl, B, NoAttrs));
2157 Value *FMul = B.CreateFMul(Expo, ConstantFP::get(Ty, N), "mul");
2159 return copyFlags(*Pow, B.CreateUnaryIntrinsic(Intrinsic::exp2, FMul,
2164 LibFunc_exp2l, B, NoAttrs));
2174 B.CreateIntrinsic(Intrinsic::exp10, {Ty}, {Expo}, Pow, "exp10");
2180 B, NoAttrs));
2198 Value *FMul = B.CreateFMul(Log, Expo, "mul");
2200 return copyFlags(*Pow, B.CreateUnaryIntrinsic(Intrinsic::exp2, FMul,
2206 LibFunc_exp2l, B, NoAttrs));
2214 Module *M, IRBuilderBase &B,
2218 return B.CreateUnaryIntrinsic(Intrinsic::sqrt, V, nullptr, "sqrt");
2227 LibFunc_sqrtl, B, Attrs);
2233 Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) {
2257 Sqrt = getSqrtCall(Base, AttributeList(), Pow->doesNotAccessMemory(), Mod, B,
2264 Sqrt = B.CreateUnaryIntrinsic(Intrinsic::fabs, Sqrt, nullptr, "abs");
2273 Value *FCmp = B.CreateFCmpOEQ(Base, NegInf, "isinf");
2274 Sqrt = B.CreateSelect(FCmp, PosInf, Sqrt);
2279 Sqrt = B.CreateFDiv(ConstantFP::get(Ty, 1.0), Sqrt, "reciprocal");
2285 IRBuilderBase &B) {
2288 return B.CreateIntrinsic(Intrinsic::powi, Types, Args);
2291 Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) {
2302 IRBuilderBase::FastMathFlagGuard Guard(B);
2303 B.setFastMathFlags(Pow->getFastMathFlags());
2310 if (Value *Exp = replacePowWithExp(Pow, B))
2317 return B.CreateFDiv(ConstantFP::get(Ty, 1.0), Base, "reciprocal");
2329 return B.CreateFMul(Base, Base, "square");
2331 if (Value *Sqrt = replacePowWithSqrt(Pow, B))
2362 B, TLI);
2376 Base, ConstantInt::get(B.getIntNTy(TLI->getIntSize()), IntExpo),
2377 M, B));
2380 return B.CreateFMul(PowI, Sqrt);
2388 if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize()))
2389 return copyFlags(*Pow, createPowWithIntegerExponent(Base, ExpoI, M, B));
2396 if (Value *Shrunk = optimizeBinaryDoubleFP(Pow, B, TLI, true))
2403 Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
2410 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2430 if (Value *Exp = getIntToFPVal(Op, B, TLI->getIntSize())) {
2434 return copyFlags(*CI, B.CreateIntrinsic(Intrinsic::ldexp,
2439 IRBuilderBase::FastMathFlagGuard Guard(B);
2440 B.setFastMathFlags(CI->getFastMathFlags());
2443 LibFunc_ldexpl, B, AttributeList()));
2450 Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) {
2458 if (Value *Ret = optimizeBinaryDoubleFP(CI, B, TLI))
2468 IRBuilderBase::FastMathFlagGuard Guard(B);
2471 B.setFastMathFlags(FMF);
2475 return copyFlags(*CI, B.CreateBinaryIntrinsic(IID, CI->getArgOperand(0),
2479 Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
2488 Ret = optimizeUnaryDoubleFP(Log, B, TLI, true);
2583 IRBuilderBase::FastMathFlagGuard Guard(B);
2584 B.setFastMathFlags(FastMathFlags::getFast());
2595 ? B.CreateUnaryIntrinsic(LogID, Arg->getOperand(0), nullptr, "log")
2596 : emitUnaryFloatFnCall(Arg->getOperand(0), TLI, LogNm, B, NoAttrs);
2600 Y = B.CreateSIToFP(Y, Ty, "cast");
2601 Value *MulY = B.CreateFMul(Y, LogX, "mul");
2621 ? B.CreateUnaryIntrinsic(LogID, Eul, nullptr, "log")
2622 : emitUnaryFloatFnCall(Eul, TLI, LogNm, B, NoAttrs);
2623 Value *MulY = B.CreateFMul(Arg->getArgOperand(0), LogE, "mul");
2634 Value *LibCallSimplifier::mergeSqrtToExp(CallInst *CI, IRBuilderBase &B) {
2686 IRBuilderBase::InsertPointGuard Guard(B);
2687 B.SetInsertPoint(Arg);
2690 B.CreateFMulFMF(ExpOperand, ConstantFP::get(ExpOperand->getType(), 0.5),
2697 Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
2707 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2709 if (Value *Opt = mergeSqrtToExp(CI, B))
2750 IRBuilderBase::FastMathFlagGuard Guard(B);
2751 B.setFastMathFlags(I->getFastMathFlags());
2756 B.CreateUnaryIntrinsic(Intrinsic::fabs, RepeatOp, nullptr, "fabs");
2762 B.CreateUnaryIntrinsic(Intrinsic::sqrt, OtherOp, nullptr, "sqrt");
2763 return copyFlags(*CI, B.CreateFMul(FabsCall, SqrtCall));
2769 IRBuilderBase &B) {
2778 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2828 static bool insertSinCosCall(IRBuilderBase &B, Function *OrigCallee, Value *Arg,
2861 B.SetInsertPoint(ArgInst->getParent(), ++ArgInst->getIterator());
2865 BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
2866 B.SetInsertPoint(&EntryBB, EntryBB.begin());
2869 SinCos = B.CreateCall(Callee, Arg, "sincospi");
2872 Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
2873 Cos = B.CreateExtractValue(SinCos, 1, "cospi");
2875 Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
2877 Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
2885 IRBuilderBase &B) {
2890 IRBuilderBase::FastMathFlagGuard Guard(B);
2891 B.setFastMathFlags(CI->getFastMathFlags());
2893 auto *CallInst = copyFlags(*CI, B.CreateCall(CI->getCalledFunction(), {X}));
2899 return B.CreateFNeg(CallInst);
2905 IRBuilderBase::FastMathFlagGuard Guard(B);
2906 B.setFastMathFlags(CI->getFastMathFlags());
2908 auto *CallInst = copyFlags(*CI, B.CreateCall(CI->getCalledFunction(), {X}));
2916 IRBuilderBase &B) {
2921 return optimizeSymmetricCall(CI, /*IsEven*/ true, B);
2934 return optimizeSymmetricCall(CI, /*IsEven*/ false, B);
2941 Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, bool IsSin, IRBuilderBase &B) {
2966 if (!insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos,
3025 Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilderBase &B) {
3031 Value *V = B.CreateIntrinsic(Intrinsic::cttz, {ArgType}, {Op, B.getTrue()},
3033 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
3034 V = B.CreateIntCast(V, RetType, false);
3036 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
3037 return B.CreateSelect(Cond, V, ConstantInt::get(RetType, 0));
3040 Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilderBase &B) {
3045 Value *V = B.CreateIntrinsic(Intrinsic::ctlz, {ArgType}, {Op, B.getFalse()},
3047 V = B.CreateSub(ConstantInt::get(V->getType(), ArgType->getIntegerBitWidth()),
3049 return B.CreateIntCast(V, CI->getType(), false);
3052 Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilderBase &B) {
3056 Value *IsNeg = B.CreateIsNeg(X);
3057 Value *NegX = B.CreateNSWNeg(X, "neg");
3058 return B.CreateSelect(IsNeg, NegX, X);
3061 Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilderBase &B) {
3065 Op = B.CreateSub(Op, ConstantInt::get(ArgType, '0'), "isdigittmp");
3066 Op = B.CreateICmpULT(Op, ConstantInt::get(ArgType, 10), "isdigit");
3067 return B.CreateZExt(Op, CI->getType());
3070 Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilderBase &B) {
3074 Op = B.CreateICmpULT(Op, ConstantInt::get(ArgType, 128), "isascii");
3075 return B.CreateZExt(Op, CI->getType());
3078 Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilderBase &B) {
3080 return B.CreateAnd(CI->getArgOperand(0),
3085 Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilderBase &B) {
3092 return convertStrToInt(CI, Str, nullptr, 10, /*AsSigned=*/true, B);
3096 Value *LibCallSimplifier::optimizeStrToInt(CallInst *CI, IRBuilderBase &B,
3112 return convertStrToInt(CI, Str, EndPtr, CInt->getSExtValue(), AsSigned, B);
3124 Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilderBase &B,
3164 Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
3187 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3204 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3209 Value *GV = B.CreateGlobalString(OperandStr, "str");
3210 return copyFlags(*CI, emitPutS(GV, B, TLI));
3221 Value *GV = B.CreateGlobalString(FormatStr, "str");
3222 return copyFlags(*CI, emitPutS(GV, B, TLI));
3231 Value *IntChar = B.CreateIntCast(CI->getArgOperand(1), IntTy, false);
3232 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3238 return copyFlags(*CI, emitPutS(CI->getArgOperand(1), B, TLI));
3242 Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilderBase &B) {
3247 if (Value *V = optimizePrintFString(CI, B)) {
3261 B.Insert(New);
3273 B.Insert(New);
3281 IRBuilderBase &B) {
3296 B.CreateMemCpy(
3313 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
3315 B.CreateStore(V, Ptr);
3316 Ptr = B.CreateInBoundsGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
3317 B.CreateStore(B.getInt8(0), Ptr);
3330 return copyFlags(*CI, emitStrCpy(Dest, CI->getArgOperand(2), B, TLI));
3334 B.CreateMemCpy(
3339 } else if (Value *V = emitStpCpy(Dest, CI->getArgOperand(2), B, TLI)) {
3341 Value *PtrDiff = B.CreatePtrDiff(B.getInt8Ty(), V, Dest);
3342 return B.CreateIntCast(PtrDiff, CI->getType(), false);
3351 Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
3355 B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
3356 B.CreateMemCpy(Dest, Align(1), CI->getArgOperand(2), Align(1), IncLen);
3359 return B.CreateIntCast(Len, CI->getType(), false);
3364 Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilderBase &B) {
3368 if (Value *V = optimizeSPrintFString(CI, B)) {
3382 B.Insert(New);
3394 B.Insert(New);
3408 IRBuilderBase &B) {
3438 B.CreateMemCpy(
3448 Type *Int8Ty = B.getInt8Ty();
3449 Value *NulOff = B.getIntN(IntBits, NCopy);
3450 Value *DstEnd = B.CreateInBoundsGEP(Int8Ty, DstArg, NulOff, "endptr");
3451 B.CreateStore(ConstantInt::get(Int8Ty, 0), DstEnd);
3456 IRBuilderBase &B) {
3484 return emitSnPrintfMemCpy(CI, FmtArg, FormatStr, N, B);
3499 return emitSnPrintfMemCpy(CI, nullptr, CharStr, N, B);
3505 Value *V = B.CreateTrunc(CI->getArgOperand(3), B.getInt8Ty(), "char");
3507 B.CreateStore(V, Ptr);
3508 Ptr = B.CreateInBoundsGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
3509 B.CreateStore(B.getInt8(0), Ptr);
3522 return emitSnPrintfMemCpy(CI, StrArg, Str, N, B);
3525 Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilderBase &B) {
3526 if (Value *V = optimizeSnPrintFString(CI, B)) {
3536 IRBuilderBase &B) {
3537 optimizeErrorReporting(CI, B, 0);
3561 CI->getArgOperand(0), B, DL, TLI));
3574 Type *IntTy = B.getIntNTy(TLI->getIntSize());
3575 Value *V = B.CreateIntCast(CI->getArgOperand(2), IntTy, /*isSigned*/ true,
3577 return copyFlags(*CI, emitFPutC(V, CI->getArgOperand(0), B, TLI));
3585 *CI, emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI));
3590 Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilderBase &B) {
3594 if (Value *V = optimizeFPrintFString(CI, B)) {
3606 B.Insert(New);
3619 B.Insert(New);
3626 Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilderBase &B) {
3627 optimizeErrorReporting(CI, B, 3);
3642 Value *Char = B.CreateLoad(B.getInt8Ty(), CI->getArgOperand(0), "char");
3643 Type *IntTy = B.getIntNTy(TLI->getIntSize());
3644 Value *Cast = B.CreateIntCast(Char, IntTy, /*isSigned*/ true, "chari");
3645 Value *NewCI = emitFPutC(Cast, CI->getArgOperand(3), B, TLI);
3653 Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) {
3654 optimizeErrorReporting(CI, B, 1);
3680 CI->getArgOperand(1), B, DL, TLI));
3683 Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilderBase &B) {
3695 return copyFlags(*CI, emitPutChar(ConstantInt::get(IntTy, '\n'), B, TLI));
3701 Value *LibCallSimplifier::optimizeBCopy(CallInst *CI, IRBuilderBase &B) {
3703 return copyFlags(*CI, B.CreateMemMove(CI->getArgOperand(1), Align(1),
4154 IRBuilderBase &B) {
4157 B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
4166 IRBuilderBase &B) {
4169 B.CreateMemMove(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
4178 IRBuilderBase &B) {
4180 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
4181 CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val,
4190 IRBuilderBase &B) {
4194 CI->getArgOperand(2), B, DL, TLI)) {
4201 IRBuilderBase &B,
4209 Value *StrLen = emitStrLen(Src, B, DL, TLI);
4210 return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
4220 return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
4222 return copyFlags(*CI, emitStpCpy(Dst, Src, B, TLI));
4238 Value *Ret = emitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI);
4242 return B.CreateInBoundsGEP(B.getInt8Ty(), Dst,
4248 IRBuilderBase &B) {
4250 return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B,
4256 IRBuilderBase &B,
4262 CI->getArgOperand(2), B, TLI));
4266 CI->getArgOperand(2), B, TLI));
4273 IRBuilderBase &B) {
4277 CI->getArgOperand(2), CI->getArgOperand(3), B, TLI));
4283 IRBuilderBase &B) {
4288 CI->getArgOperand(4), VariadicArgs, B, TLI));
4295 IRBuilderBase &B) {
4300 VariadicArgs, B, TLI));
4307 IRBuilderBase &B) {
4310 *CI, emitStrCat(CI->getArgOperand(0), CI->getArgOperand(1), B, TLI));
4316 IRBuilderBase &B) {
4320 CI->getArgOperand(2), B, TLI));
4326 IRBuilderBase &B) {
4330 CI->getArgOperand(2), B, TLI));
4336 IRBuilderBase &B) {
4340 CI->getArgOperand(2), B, TLI));
4346 IRBuilderBase &B) {
4350 CI->getArgOperand(4), CI->getArgOperand(5), B, TLI));
4356 IRBuilderBase &B) {
4360 CI->getArgOperand(4), B, TLI));