Lines Matching defs:CI
119 static bool callHasFloatingPointArgument(const CallInst *CI) {
120 return any_of(CI->operands(), [](const Use &OI) {
125 static bool callHasFP128Argument(const CallInst *CI) {
126 return any_of(CI->operands(), [](const Use &OI) {
138 static Value *convertStrToInt(CallInst *CI, StringRef &Str, Value *EndPtr,
171 Type *RetTy = CI->getType();
228 Value *StrBeg = CI->getArgOperand(0);
252 static bool canTransformToMemCmp(CallInst *CI, Value *Str, uint64_t Len,
254 if (!isOnlyUsedInComparisonWithZero(CI))
260 if (CI->getFunction()->hasFnAttribute(Attribute::SanitizeMemory))
266 static void annotateDereferenceableBytes(CallInst *CI,
269 const Function *F = CI->getCaller();
274 unsigned AS = CI->getArgOperand(ArgNo)->getType()->getPointerAddressSpace();
276 CI->paramHasAttr(ArgNo, Attribute::NonNull))
277 DerefBytes = std::max(CI->getParamDereferenceableOrNullBytes(ArgNo),
280 if (CI->getParamDereferenceableBytes(ArgNo) < DerefBytes) {
281 CI->removeParamAttr(ArgNo, Attribute::Dereferenceable);
283 CI->paramHasAttr(ArgNo, Attribute::NonNull))
284 CI->removeParamAttr(ArgNo, Attribute::DereferenceableOrNull);
285 CI->addParamAttr(ArgNo, Attribute::getWithDereferenceableBytes(
286 CI->getContext(), DerefBytes));
291 static void annotateNonNullNoUndefBasedOnAccess(CallInst *CI,
293 Function *F = CI->getCaller();
298 if (!CI->paramHasAttr(ArgNo, Attribute::NoUndef))
299 CI->addParamAttr(ArgNo, Attribute::NoUndef);
301 if (!CI->paramHasAttr(ArgNo, Attribute::NonNull)) {
303 CI->getArgOperand(ArgNo)->getType()->getPointerAddressSpace();
306 CI->addParamAttr(ArgNo, Attribute::NonNull);
309 annotateDereferenceableBytes(CI, ArgNo, 1);
313 static void annotateNonNullAndDereferenceable(CallInst *CI, ArrayRef<unsigned> ArgNos,
316 annotateNonNullNoUndefBasedOnAccess(CI, ArgNos);
317 annotateDereferenceableBytes(CI, ArgNos, LenC->getZExtValue());
319 annotateNonNullNoUndefBasedOnAccess(CI, ArgNos);
324 annotateDereferenceableBytes(CI, ArgNos, DerefMin);
364 Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilderBase &B) {
366 Value *Dst = CI->getArgOperand(0);
367 Value *Src = CI->getArgOperand(1);
368 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
373 annotateDereferenceableBytes(CI, 1, Len);
382 return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, Len, B));
405 Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilderBase &B) {
407 Value *Dst = CI->getArgOperand(0);
408 Value *Src = CI->getArgOperand(1);
409 Value *Size = CI->getArgOperand(2);
411 annotateNonNullNoUndefBasedOnAccess(CI, 0);
413 annotateNonNullNoUndefBasedOnAccess(CI, 1);
429 annotateDereferenceableBytes(CI, 1, SrcLen);
445 return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, SrcLen, B));
451 static Value* memChrToCharCompare(CallInst *CI, Value *NBytes,
454 Value *Src = CI->getArgOperand(0);
455 Value *CharVal = CI->getArgOperand(1);
469 Value *NullPtr = Constant::getNullValue(CI->getType());
473 Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilderBase &B) {
474 Value *SrcStr = CI->getArgOperand(0);
475 Value *CharVal = CI->getArgOperand(1);
476 annotateNonNullNoUndefBasedOnAccess(CI, 0);
478 if (isOnlyUsedInEqualityComparison(CI, SrcStr))
479 return memChrToCharCompare(CI, nullptr, B, DL);
487 annotateDereferenceableBytes(CI, 0, Len);
491 Function *Callee = CI->getCalledFunction();
497 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
498 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
499 return copyFlags(*CI,
506 Value *NullPtr = Constant::getNullValue(CI->getType());
507 if (isOnlyUsedInEqualityComparison(CI, NullPtr))
510 return B.CreateIntToPtr(B.getTrue(), CI->getType());
529 return Constant::getNullValue(CI->getType());
535 Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilderBase &B) {
536 Value *SrcStr = CI->getArgOperand(0);
537 Value *CharVal = CI->getArgOperand(1);
539 annotateNonNullNoUndefBasedOnAccess(CI, 0);
545 return copyFlags(*CI, emitStrChr(SrcStr, '\0', B, TLI));
549 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
550 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
556 return copyFlags(*CI, emitMemRChr(SrcStr, CharVal, Size, B, DL, TLI));
559 Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilderBase &B) {
560 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
562 return ConstantInt::get(CI->getType(), 0);
570 return ConstantInt::get(CI->getType(),
575 B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
579 CI->getType());
584 annotateDereferenceableBytes(CI, 0, Len1);
587 annotateDereferenceableBytes(CI, 1, Len2);
591 *CI, emitMemCmp(Str1P, Str2P,
592 TLI->getAsSizeT(std::min(Len1, Len2), *CI->getModule()),
598 if (canTransformToMemCmp(CI, Str1P, Len2, DL))
599 return copyFlags(*CI, emitMemCmp(Str1P, Str2P,
600 TLI->getAsSizeT(Len2, *CI->getModule()),
603 if (canTransformToMemCmp(CI, Str2P, Len1, DL))
604 return copyFlags(*CI, emitMemCmp(Str1P, Str2P,
605 TLI->getAsSizeT(Len1, *CI->getModule()),
609 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
613 // Optimize a memcmp or, when StrNCmp is true, strncmp call CI with constant
615 static Value *optimizeMemCmpVarSize(CallInst *CI, Value *LHS, Value *RHS,
619 Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilderBase &B) {
620 Value *Str1P = CI->getArgOperand(0);
621 Value *Str2P = CI->getArgOperand(1);
622 Value *Size = CI->getArgOperand(2);
624 return ConstantInt::get(CI->getType(), 0);
627 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
633 return optimizeMemCmpVarSize(CI, Str1P, Str2P, Size, true, B, DL);
636 return ConstantInt::get(CI->getType(), 0);
639 return copyFlags(*CI, emitMemCmp(Str1P, Str2P, Size, B, DL, TLI));
650 return ConstantInt::get(CI->getType(),
656 B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
660 CI->getType());
664 annotateDereferenceableBytes(CI, 0, Len1);
667 annotateDereferenceableBytes(CI, 1, Len2);
672 if (canTransformToMemCmp(CI, Str1P, Len2, DL))
673 return copyFlags(*CI, emitMemCmp(Str1P, Str2P,
674 TLI->getAsSizeT(Len2, *CI->getModule()),
678 if (canTransformToMemCmp(CI, Str2P, Len1, DL))
679 return copyFlags(*CI, emitMemCmp(Str1P, Str2P,
680 TLI->getAsSizeT(Len1, *CI->getModule()),
687 Value *LibCallSimplifier::optimizeStrNDup(CallInst *CI, IRBuilderBase &B) {
688 Value *Src = CI->getArgOperand(0);
689 ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
692 annotateDereferenceableBytes(CI, 0, SrcLen);
694 return copyFlags(*CI, emitStrDup(Src, B, TLI));
700 Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilderBase &B) {
701 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
705 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
709 annotateDereferenceableBytes(CI, 1, Len);
716 TLI->getAsSizeT(Len, *CI->getModule()));
717 mergeAttributesAndFlags(NewCI, *CI);
721 Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilderBase &B) {
722 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
725 if (CI->use_empty())
726 return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
736 annotateDereferenceableBytes(CI, 1, Len);
740 Value *LenV = TLI->getAsSizeT(Len, *CI->getModule());
742 B.getInt8Ty(), Dst, TLI->getAsSizeT(Len - 1, *CI->getModule()));
747 mergeAttributesAndFlags(NewCI, *CI);
753 Value *LibCallSimplifier::optimizeStrLCpy(CallInst *CI, IRBuilderBase &B) {
754 Value *Size = CI->getArgOperand(2);
758 annotateNonNullNoUndefBasedOnAccess(CI, 0);
761 annotateNonNullNoUndefBasedOnAccess(CI, 1);
769 Value *Dst = CI->getArgOperand(0);
770 Value *Src = CI->getArgOperand(1);
777 return copyFlags(*CI, emitStrLen(Src, B, DL, TLI));
806 return ConstantInt::get(CI->getType(), 0);
813 TLI->getAsSizeT(NBytes, *CI->getModule()));
814 mergeAttributesAndFlags(NewCI, *CI);
817 Value *EndOff = ConstantInt::get(CI->getType(), NBytes);
825 return ConstantInt::get(CI->getType(), SrcLen);
828 // Optimize a call CI to either stpncpy when RetEnd is true, or to strncpy
830 Value *LibCallSimplifier::optimizeStringNCpy(CallInst *CI, bool RetEnd,
832 Value *Dst = CI->getArgOperand(0);
833 Value *Src = CI->getArgOperand(1);
834 Value *Size = CI->getArgOperand(2);
839 annotateNonNullNoUndefBasedOnAccess(CI, 0);
840 annotateNonNullNoUndefBasedOnAccess(CI, 1);
873 annotateDereferenceableBytes(CI, 1, SrcLen);
882 CI->getAttributes().getParamAttrs(0).getAlignment().valueOrOne();
884 AttrBuilder ArgAttrs(CI->getContext(), CI->getAttributes().getParamAttrs(0));
886 CI->getContext(), 0, ArgAttrs));
887 copyFlags(*CI, NewCI);
911 TLI->getAsSizeT(N, *CI->getModule()));
912 mergeAttributesAndFlags(NewCI, *CI);
922 Value *LibCallSimplifier::optimizeStringLength(CallInst *CI, IRBuilderBase &B,
925 Value *Src = CI->getArgOperand(0);
928 if (isOnlyUsedInZeroEqualityComparison(CI) &&
937 CI->getType());
944 return ConstantInt::get(CI->getType(), 0);
951 return B.CreateZExt(Cmp, CI->getType());
957 Value *LenC = ConstantInt::get(CI->getType(), Len - 1);
1002 KnownBits Known = computeKnownBits(Offset, DL, 0, nullptr, CI, nullptr);
1013 Offset = B.CreateSExtOrTrunc(Offset, CI->getType());
1014 return B.CreateSub(ConstantInt::get(CI->getType(), NullTermIdx),
1026 return OptimizationRemark("instcombine", "simplify-libcalls", CI)
1030 ConstantInt::get(CI->getType(), LenTrue - 1),
1031 ConstantInt::get(CI->getType(), LenFalse - 1));
1038 Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilderBase &B) {
1039 if (Value *V = optimizeStringLength(CI, B, 8))
1041 annotateNonNullNoUndefBasedOnAccess(CI, 0);
1045 Value *LibCallSimplifier::optimizeStrNLen(CallInst *CI, IRBuilderBase &B) {
1046 Value *Bound = CI->getArgOperand(1);
1047 if (Value *V = optimizeStringLength(CI, B, 8, Bound))
1051 annotateNonNullNoUndefBasedOnAccess(CI, 0);
1055 Value *LibCallSimplifier::optimizeWcslen(CallInst *CI, IRBuilderBase &B) {
1056 Module &M = *CI->getModule();
1062 return optimizeStringLength(CI, B, WCharSize);
1065 Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilderBase &B) {
1067 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
1068 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
1073 return Constant::getNullValue(CI->getType());
1079 return Constant::getNullValue(CI->getType());
1081 return B.CreateInBoundsGEP(B.getInt8Ty(), CI->getArgOperand(0),
1087 return copyFlags(*CI, emitStrChr(CI->getArgOperand(0), S2[0], B, TLI));
1092 Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilderBase &B) {
1093 Value *EndPtr = CI->getArgOperand(1);
1097 CI->addParamAttr(0, Attribute::getWithCaptureInfo(CI->getContext(),
1104 Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilderBase &B) {
1106 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
1107 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
1112 return Constant::getNullValue(CI->getType());
1119 return ConstantInt::get(CI->getType(), Pos);
1125 Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilderBase &B) {
1127 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
1128 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
1132 return Constant::getNullValue(CI->getType());
1139 return ConstantInt::get(CI->getType(), Pos);
1144 return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B, DL, TLI));
1149 Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilderBase &B) {
1151 if (CI->getArgOperand(0) == CI->getArgOperand(1))
1152 return CI->getArgOperand(0);
1155 if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
1156 Value *StrLen = emitStrLen(CI->getArgOperand(1), B, DL, TLI);
1159 Value *StrNCmp = emitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
1163 for (User *U : llvm::make_early_inc_range(CI->users())) {
1170 return CI;
1175 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
1176 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
1180 return CI->getArgOperand(0);
1187 return Constant::getNullValue(CI->getType());
1190 return B.CreateConstInBoundsGEP1_64(B.getInt8Ty(), CI->getArgOperand(0),
1196 return emitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI);
1199 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
1203 Value *LibCallSimplifier::optimizeMemRChr(CallInst *CI, IRBuilderBase &B) {
1204 Value *SrcStr = CI->getArgOperand(0);
1205 Value *Size = CI->getArgOperand(2);
1206 annotateNonNullAndDereferenceable(CI, 0, Size, DL);
1207 Value *CharVal = CI->getArgOperand(1);
1209 Value *NullPtr = Constant::getNullValue(CI->getType());
1291 Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) {
1292 Value *SrcStr = CI->getArgOperand(0);
1293 Value *Size = CI->getArgOperand(2);
1296 annotateNonNullNoUndefBasedOnAccess(CI, 0);
1297 if (isOnlyUsedInEqualityComparison(CI, SrcStr))
1298 return memChrToCharCompare(CI, Size, B, DL);
1301 Value *CharVal = CI->getArgOperand(1);
1304 Value *NullPtr = Constant::getNullValue(CI->getType());
1390 if (isOnlyUsedInEqualityComparison(CI, SrcStr))
1394 return memChrToCharCompare(CI, Size, B, DL);
1400 bool OptForSize = llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
1413 if (OptForSize || Str.empty() || !isOnlyUsedInZeroEqualityComparison(CI))
1453 return B.CreateIntToPtr(B.CreateOr(CharCompares), CI->getType());
1481 CI->getType());
1484 // Optimize a memcmp or, when StrNCmp is true, strncmp call CI with constant
1486 static Value *optimizeMemCmpVarSize(CallInst *CI, Value *LHS, Value *RHS,
1490 return Constant::getNullValue(CI->getType());
1503 Value *Zero = ConstantInt::get(CI->getType(), 0);
1523 Value *Res = ConstantInt::get(CI->getType(), IRes);
1527 // Optimize a memcmp call CI with constant size Len.
1528 static Value *optimizeMemCmpConstantSize(CallInst *CI, Value *LHS, Value *RHS,
1532 return Constant::getNullValue(CI->getType());
1537 CI->getType(), "lhsv");
1539 CI->getType(), "rhsv");
1546 if (DL.isLegalInteger(Len * 8) && isOnlyUsedInZeroEqualityComparison(CI)) {
1547 IntegerType *IntType = IntegerType::get(CI->getContext(), Len * 8);
1561 if ((LHSV || getKnownAlignment(LHS, DL, CI) >= PrefAlignment) &&
1562 (RHSV || getKnownAlignment(RHS, DL, CI) >= PrefAlignment)) {
1567 return B.CreateZExt(B.CreateICmpNE(LHSV, RHSV), CI->getType(), "memcmp");
1575 Value *LibCallSimplifier::optimizeMemCmpBCmpCommon(CallInst *CI,
1577 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
1578 Value *Size = CI->getArgOperand(2);
1580 annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
1582 if (Value *Res = optimizeMemCmpVarSize(CI, LHS, RHS, Size, false, B, DL))
1590 return optimizeMemCmpConstantSize(CI, LHS, RHS, LenC->getZExtValue(), B, DL);
1593 Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilderBase &B) {
1594 Module *M = CI->getModule();
1595 if (Value *V = optimizeMemCmpBCmpCommon(CI, B))
1602 isOnlyUsedInZeroEqualityComparison(CI)) {
1603 Value *LHS = CI->getArgOperand(0);
1604 Value *RHS = CI->getArgOperand(1);
1605 Value *Size = CI->getArgOperand(2);
1606 return copyFlags(*CI, emitBCmp(LHS, RHS, Size, B, DL, TLI));
1612 Value *LibCallSimplifier::optimizeBCmp(CallInst *CI, IRBuilderBase &B) {
1613 return optimizeMemCmpBCmpCommon(CI, B);
1616 Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilderBase &B) {
1617 Value *Size = CI->getArgOperand(2);
1618 annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
1619 if (isa<IntrinsicInst>(CI))
1623 CallInst *NewCI = B.CreateMemCpy(CI->getArgOperand(0), Align(1),
1624 CI->getArgOperand(1), Align(1), Size);
1625 mergeAttributesAndFlags(NewCI, *CI);
1626 return CI->getArgOperand(0);
1629 Value *LibCallSimplifier::optimizeMemCCpy(CallInst *CI, IRBuilderBase &B) {
1630 Value *Dst = CI->getArgOperand(0);
1631 Value *Src = CI->getArgOperand(1);
1632 ConstantInt *StopChar = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1633 ConstantInt *N = dyn_cast<ConstantInt>(CI->getArgOperand(3));
1635 if (CI->use_empty() && Dst == Src)
1640 return Constant::getNullValue(CI->getType());
1653 copyFlags(*CI, B.CreateMemCpy(Dst, Align(1), Src, Align(1),
1654 CI->getArgOperand(3)));
1655 return Constant::getNullValue(CI->getType());
1663 copyFlags(*CI, B.CreateMemCpy(Dst, Align(1), Src, Align(1), NewN));
1666 : Constant::getNullValue(CI->getType());
1669 Value *LibCallSimplifier::optimizeMemPCpy(CallInst *CI, IRBuilderBase &B) {
1670 Value *Dst = CI->getArgOperand(0);
1671 Value *N = CI->getArgOperand(2);
1674 B.CreateMemCpy(Dst, Align(1), CI->getArgOperand(1), Align(1), N);
1678 mergeAttributesAndFlags(NewCI, *CI);
1682 Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilderBase &B) {
1683 Value *Size = CI->getArgOperand(2);
1684 annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
1685 if (isa<IntrinsicInst>(CI))
1689 CallInst *NewCI = B.CreateMemMove(CI->getArgOperand(0), Align(1),
1690 CI->getArgOperand(1), Align(1), Size);
1691 mergeAttributesAndFlags(NewCI, *CI);
1692 return CI->getArgOperand(0);
1695 Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilderBase &B) {
1696 Value *Size = CI->getArgOperand(2);
1697 annotateNonNullAndDereferenceable(CI, 0, Size, DL);
1698 if (isa<IntrinsicInst>(CI))
1702 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
1703 CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val, Size, Align(1));
1704 mergeAttributesAndFlags(NewCI, *CI);
1705 return CI->getArgOperand(0);
1708 Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilderBase &B) {
1709 if (isa<ConstantPointerNull>(CI->getArgOperand(0)))
1710 return copyFlags(*CI, emitMalloc(CI->getArgOperand(1), B, DL, TLI));
1719 Value *LibCallSimplifier::optimizeNew(CallInst *CI, IRBuilderBase &B,
1725 if (CI->getAttributes().getFnAttr("memprof").getValueAsString() == "cold")
1727 else if (CI->getAttributes().getFnAttr("memprof").getValueAsString() ==
1730 else if (CI->getAttributes().getFnAttr("memprof").getValueAsString() == "hot")
1745 return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1750 return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1755 return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1760 return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1766 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1772 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1778 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1784 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1790 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1796 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1802 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1808 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1814 CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
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,
1841 return emitHotColdSizeReturningNew(CI->getArgOperand(0), B, TLI,
1847 return emitHotColdSizeReturningNew(CI->getArgOperand(0), B, TLI,
1854 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1860 CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1873 // Replace a libcall \p CI with a call to intrinsic \p IID
1874 static Value *replaceUnaryCall(CallInst *CI, IRBuilderBase &B,
1876 CallInst *NewCall = B.CreateUnaryIntrinsic(IID, CI->getArgOperand(0), CI);
1877 NewCall->takeName(CI);
1878 return copyFlags(*CI, NewCall);
1904 static Value *optimizeDoubleFP(CallInst *CI, IRBuilderBase &B,
1907 Function *CalleeFn = CI->getCalledFunction();
1908 if (!CI->getType()->isDoubleTy() || !CalleeFn)
1915 for (User *U : CI->users()) {
1923 V[0] = valueHasFloatPrecision(CI->getArgOperand(0));
1924 V[1] = isBinary ? valueHasFloatPrecision(CI->getArgOperand(1)) : nullptr;
1936 StringRef CallerName = CI->getFunction()->getName();
1945 B.setFastMathFlags(CI->getFastMathFlags());
1963 static Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilderBase &B,
1966 return optimizeDoubleFP(CI, B, false, TLI, isPrecise);
1970 static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilderBase &B,
1973 return optimizeDoubleFP(CI, B, true, TLI, isPrecise);
1977 Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilderBase &B) {
1980 if (CI->arg_size() == 1) {
1982 if (!CI->isFast())
1985 Value *Op = CI->getArgOperand(0);
1992 assert(CI->arg_size() == 2 && "Unexpected signature for cabs!");
1994 Real = CI->getArgOperand(0);
1995 Imag = CI->getArgOperand(1);
2011 *CI, B.CreateUnaryIntrinsic(Intrinsic::fabs, AbsOp, CI, "cabs"));
2013 if (!CI->isFast())
2018 Value *RealReal = B.CreateFMulFMF(Real, Real, CI);
2019 Value *ImagImag = B.CreateFMulFMF(Imag, Imag, CI);
2021 *CI, B.CreateUnaryIntrinsic(Intrinsic::sqrt,
2022 B.CreateFAddFMF(RealReal, ImagImag, CI), CI,
2412 Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
2413 Module *M = CI->getModule();
2414 Function *Callee = CI->getCalledFunction();
2419 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2429 Type *Ty = CI->getType();
2435 Value *Op = CI->getArgOperand(0);
2443 return copyFlags(*CI, B.CreateIntrinsic(Intrinsic::ldexp,
2445 {One, Exp}, CI));
2449 B.setFastMathFlags(CI->getFastMathFlags());
2450 return copyFlags(*CI, emitBinaryFloatFnCall(
2459 Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) {
2460 Module *M = CI->getModule();
2464 Function *Callee = CI->getCalledFunction();
2467 if (Value *Ret = optimizeBinaryDoubleFP(CI, B, TLI))
2477 FastMathFlags FMF = CI->getFastMathFlags();
2482 return copyFlags(*CI, B.CreateBinaryIntrinsic(IID, CI->getArgOperand(0),
2483 CI->getArgOperand(1), FMF));
2659 Value *LibCallSimplifier::mergeSqrtToExp(CallInst *CI, IRBuilderBase &B) {
2660 if (!CI->hasAllowReassoc())
2663 Function *SqrtFn = CI->getCalledFunction();
2664 CallInst *Arg = dyn_cast<CallInst>(CI->getArgOperand(0));
2694 if (CI->getType()->getScalarType()->isFloatTy()) {
2698 } else if (CI->getType()->getScalarType()->isDoubleTy()) {
2716 CI, "merged.sqrt");
2722 Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
2723 Module *M = CI->getModule();
2724 Function *Callee = CI->getCalledFunction();
2732 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2734 if (Value *Opt = mergeSqrtToExp(CI, B))
2737 if (!CI->isFast())
2740 Instruction *I = dyn_cast<Instruction>(CI->getArgOperand(0));
2789 return copyFlags(*CI, B.CreateFMulFMF(FabsCall, SqrtCall, I));
2791 return copyFlags(*CI, FabsCall);
2794 Value *LibCallSimplifier::optimizeFMod(CallInst *CI, IRBuilderBase &B) {
2799 bool IsNoNan = CI->hasNoNaNs();
2801 SimplifyQuery SQ(DL, TLI, DT, AC, CI, true, true, DC);
2802 KnownFPClass Known0 = computeKnownFPClass(CI->getOperand(0), fcInf,
2806 computeKnownFPClass(CI->getOperand(1), fcZero | fcSubnormal,
2808 Function *F = CI->getParent()->getParent();
2809 IsNoNan = Known1.isKnownNeverLogicalZero(*F, CI->getType());
2814 Value *FRem = B.CreateFRemFMF(CI->getOperand(0), CI->getOperand(1), CI);
2822 Value *LibCallSimplifier::optimizeTrigInversionPairs(CallInst *CI,
2824 Module *M = CI->getModule();
2825 Function *Callee = CI->getCalledFunction();
2832 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2834 Value *Op1 = CI->getArgOperand(0);
2840 if (!CI->isFast() || !OpC->isFast())
2875 static bool isTrigLibCall(CallInst *CI) {
2879 return CI->doesNotThrow() && CI->doesNotAccessMemory();
2938 static Value *optimizeSymmetricCall(CallInst *CI, bool IsEven,
2941 Value *Src = CI->getArgOperand(0);
2944 auto *Call = B.CreateCall(CI->getCalledFunction(), {X});
2945 Call->copyFastMathFlags(CI);
2946 auto *CallInst = copyFlags(*CI, Call);
2952 return B.CreateFNegFMF(CallInst, CI);
2958 auto *Call = B.CreateCall(CI->getCalledFunction(), {X});
2959 Call->copyFastMathFlags(CI);
2960 return copyFlags(*CI, Call);
2966 Value *LibCallSimplifier::optimizeSymmetric(CallInst *CI, LibFunc Func,
2972 return optimizeSymmetricCall(CI, /*IsEven*/ true, B);
2985 return optimizeSymmetricCall(CI, /*IsEven*/ false, B);
2992 Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, bool IsSin, IRBuilderBase &B) {
2995 if (!isTrigLibCall(CI))
2998 Value *Arg = CI->getArgOperand(0);
3008 Function *F = CI->getFunction();
3017 if (!insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos,
3039 auto *CI = dyn_cast<CallInst>(Val);
3040 if (!CI || CI->use_empty())
3044 if (CI->getFunction() != F)
3047 Module *M = CI->getModule();
3048 Function *Callee = CI->getCalledFunction();
3052 !isTrigLibCall(CI))
3057 SinCalls.push_back(CI);
3059 CosCalls.push_back(CI);
3061 SinCosCalls.push_back(CI);
3064 SinCalls.push_back(CI);
3066 CosCalls.push_back(CI);
3068 SinCosCalls.push_back(CI);
3073 Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
3075 if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
3076 !match(CI->getArgOperand(1), m_APFloat(Y)))
3099 CI->getArgOperand(2), CI->getParamAlign(2));
3100 return ConstantFP::get(CI->getType(), Rem);
3104 Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
3106 if (!CI->doesNotAccessMemory())
3111 if (isa<PoisonValue>(CI->getArgOperand(0)))
3112 return CI->getArgOperand(0);
3113 if (isa<PoisonValue>(CI->getArgOperand(1)))
3114 return CI->getArgOperand(1);
3118 if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
3119 !match(CI->getArgOperand(1), m_APFloat(Y)))
3126 maximum(Difference, APFloat::getZero(CI->getType()->getFltSemantics()));
3127 return ConstantFP::get(CI->getType(), MaxVal);
3134 Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilderBase &B) {
3137 Type *RetType = CI->getType();
3138 Value *Op = CI->getArgOperand(0);
3149 Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilderBase &B) {
3152 Value *Op = CI->getArgOperand(0);
3158 return B.CreateIntCast(V, CI->getType(), false);
3161 Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilderBase &B) {
3164 Value *X = CI->getArgOperand(0);
3170 Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilderBase &B) {
3172 Value *Op = CI->getArgOperand(0);
3176 return B.CreateZExt(Op, CI->getType());
3179 Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilderBase &B) {
3181 Value *Op = CI->getArgOperand(0);
3184 return B.CreateZExt(Op, CI->getType());
3187 Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilderBase &B) {
3189 return B.CreateAnd(CI->getArgOperand(0),
3190 ConstantInt::get(CI->getType(), 0x7F));
3194 Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilderBase &B) {
3196 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
3199 return convertStrToInt(CI, Str, nullptr, 10, /*AsSigned=*/true, B);
3203 Value *LibCallSimplifier::optimizeStrToInt(CallInst *CI, IRBuilderBase &B,
3205 Value *EndPtr = CI->getArgOperand(1);
3209 CI->addParamAttr(0, Attribute::getWithCaptureInfo(CI->getContext(),
3216 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
3219 if (ConstantInt *CInt = dyn_cast<ConstantInt>(CI->getArgOperand(2))) {
3220 return convertStrToInt(CI, Str, EndPtr, CInt->getSExtValue(), AsSigned, B);
3230 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
3232 Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilderBase &B,
3234 Function *Callee = CI->getCalledFunction();
3243 if (!CI->hasFnAttr(Attribute::Cold) &&
3244 isReportingError(Callee, CI, StreamArg)) {
3245 CI->addFnAttr(Attribute::Cold);
3251 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {
3261 if (StreamArg >= (int)CI->arg_size())
3263 LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
3272 Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
3275 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
3280 return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);
3285 if (!CI->use_empty())
3288 Type *IntTy = CI->getType();
3295 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3299 if (FormatStr == "%s" && CI->arg_size() > 1) {
3301 if (!getConstantStringInfo(CI->getOperand(1), OperandStr))
3305 return (Value *)CI;
3312 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3318 return copyFlags(*CI, emitPutS(GV, B, TLI));
3330 return copyFlags(*CI, emitPutS(GV, B, TLI));
3335 if (FormatStr == "%c" && CI->arg_size() > 1 &&
3336 CI->getArgOperand(1)->getType()->isIntegerTy()) {
3339 Value *IntChar = B.CreateIntCast(CI->getArgOperand(1), IntTy, false);
3340 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3344 if (FormatStr == "%s\n" && CI->arg_size() > 1 &&
3345 CI->getArgOperand(1)->getType()->isPointerTy())
3346 return copyFlags(*CI, emitPutS(CI->getArgOperand(1), B, TLI));
3350 Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilderBase &B) {
3352 Module *M = CI->getModule();
3353 Function *Callee = CI->getCalledFunction();
3355 if (Value *V = optimizePrintFString(CI, B)) {
3359 annotateNonNullNoUndefBasedOnAccess(CI, 0);
3364 !callHasFloatingPointArgument(CI)) {
3367 CallInst *New = cast<CallInst>(CI->clone());
3376 !callHasFP128Argument(CI)) {
3379 CallInst *New = cast<CallInst>(CI->clone());
3388 Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI,
3392 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
3396 Value *Dest = CI->getArgOperand(0);
3397 if (CI->arg_size() == 2) {
3404 B.CreateMemCpy(Dest, Align(1), CI->getArgOperand(1), Align(1),
3406 TLI->getAsSizeT(FormatStr.size() + 1, *CI->getModule()));
3407 return ConstantInt::get(CI->getType(), FormatStr.size());
3412 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
3418 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
3420 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
3426 return ConstantInt::get(CI->getType(), 1);
3432 if (!CI->getArgOperand(2)->getType()->isPointerTy())
3435 if (CI->use_empty())
3437 return copyFlags(*CI, emitStrCpy(Dest, CI->getArgOperand(2), B, TLI));
3439 uint64_t SrcLen = GetStringLength(CI->getArgOperand(2));
3441 B.CreateMemCpy(Dest, Align(1), CI->getArgOperand(2), Align(1),
3442 TLI->getAsSizeT(SrcLen, *CI->getModule()));
3444 return ConstantInt::get(CI->getType(), SrcLen - 1);
3445 } else if (Value *V = emitStpCpy(Dest, CI->getArgOperand(2), B, TLI)) {
3448 return B.CreateIntCast(PtrDiff, CI->getType(), false);
3451 if (llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3455 Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
3460 B.CreateMemCpy(Dest, Align(1), CI->getArgOperand(2), Align(1), IncLen);
3463 return B.CreateIntCast(Len, CI->getType(), false);
3468 Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilderBase &B) {
3469 Module *M = CI->getModule();
3470 Function *Callee = CI->getCalledFunction();
3472 if (Value *V = optimizeSPrintFString(CI, B)) {
3476 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
3481 !callHasFloatingPointArgument(CI)) {
3484 CallInst *New = cast<CallInst>(CI->clone());
3493 !callHasFP128Argument(CI)) {
3496 CallInst *New = cast<CallInst>(CI->clone());
3505 // Transform an snprintf call CI with the bound N to format the string Str
3510 Value *LibCallSimplifier::emitSnPrintfMemCpy(CallInst *CI, Value *StrArg,
3523 Value *StrLen = ConstantInt::get(CI->getType(), Str.size());
3537 Value *DstArg = CI->getArgOperand(0);
3540 copyFlags(*CI, B.CreateMemCpy(DstArg, Align(1), StrArg, Align(1),
3541 TLI->getAsSizeT(NCopy, *CI->getModule())));
3556 Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI,
3559 ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
3570 Value *DstArg = CI->getArgOperand(0);
3571 Value *FmtArg = CI->getArgOperand(2);
3579 if (CI->arg_size() == 3) {
3585 return emitSnPrintfMemCpy(CI, FmtArg, FormatStr, N, B);
3590 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() != 4)
3600 return emitSnPrintfMemCpy(CI, nullptr, CharStr, N, B);
3604 if (!CI->getArgOperand(3)->getType()->isIntegerTy())
3606 Value *V = B.CreateTrunc(CI->getArgOperand(3), B.getInt8Ty(), "char");
3611 return ConstantInt::get(CI->getType(), 1);
3617 Value *StrArg = CI->getArgOperand(3);
3623 return emitSnPrintfMemCpy(CI, StrArg, Str, N, B);
3626 Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilderBase &B) {
3627 if (Value *V = optimizeSnPrintFString(CI, B)) {
3631 if (isKnownNonZero(CI->getOperand(1), DL))
3632 annotateNonNullNoUndefBasedOnAccess(CI, 0);
3636 Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI,
3638 optimizeErrorReporting(CI, B, 0);
3642 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
3648 if (!CI->use_empty())
3652 if (CI->arg_size() == 2) {
3658 *CI, emitFWrite(CI->getArgOperand(1),
3659 TLI->getAsSizeT(FormatStr.size(), *CI->getModule()),
3660 CI->getArgOperand(0), B, DL, TLI));
3665 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
3671 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
3674 Value *V = B.CreateIntCast(CI->getArgOperand(2), IntTy, /*isSigned*/ true,
3676 return copyFlags(*CI, emitFPutC(V, CI->getArgOperand(0), B, TLI));
3681 if (!CI->getArgOperand(2)->getType()->isPointerTy())
3684 *CI, emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI));
3689 Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilderBase &B) {
3690 Module *M = CI->getModule();
3691 Function *Callee = CI->getCalledFunction();
3693 if (Value *V = optimizeFPrintFString(CI, B)) {
3700 !callHasFloatingPointArgument(CI)) {
3703 CallInst *New = cast<CallInst>(CI->clone());
3712 !callHasFP128Argument(CI)) {
3716 CallInst *New = cast<CallInst>(CI->clone());
3725 Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilderBase &B) {
3726 optimizeErrorReporting(CI, B, 3);
3729 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
3730 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
3736 return ConstantInt::get(CI->getType(), 0);
3740 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
3741 Value *Char = B.CreateLoad(B.getInt8Ty(), CI->getArgOperand(0), "char");
3744 Value *NewCI = emitFPutC(Cast, CI->getArgOperand(3), B, TLI);
3745 return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
3752 Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) {
3753 optimizeErrorReporting(CI, B, 1);
3757 if (llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3762 if (!CI->use_empty())
3766 uint64_t Len = GetStringLength(CI->getArgOperand(0));
3771 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
3772 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
3774 *CI,
3775 emitFWrite(CI->getArgOperand(0),
3777 CI->getArgOperand(1), B, DL, TLI));
3780 Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilderBase &B) {
3781 annotateNonNullNoUndefBasedOnAccess(CI, 0);
3782 if (!CI->use_empty())
3788 if (getConstantStringInfo(CI->getArgOperand(0), Str) && Str.empty()) {
3791 Type *IntTy = CI->getType();
3792 return copyFlags(*CI, emitPutChar(ConstantInt::get(IntTy, '\n'), B, TLI));
3798 Value *LibCallSimplifier::optimizeExit(CallInst *CI) {
3802 if (!CI->hasFnAttr(Attribute::Cold) &&
3803 match(CI->getArgOperand(0), m_APInt(C)) && !C->isZero()) {
3804 CI->addFnAttr(Attribute::Cold);
3809 Value *LibCallSimplifier::optimizeBCopy(CallInst *CI, IRBuilderBase &B) {
3811 return copyFlags(*CI, B.CreateMemMove(CI->getArgOperand(1), Align(1),
3812 CI->getArgOperand(0), Align(1),
3813 CI->getArgOperand(2)));
3822 Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI,
3824 Module *M = CI->getModule();
3826 Function *Callee = CI->getCalledFunction();
3833 TargetLibraryInfoImpl::isCallingConvCCompatible(CI)) &&
3837 return optimizeStrCat(CI, Builder);
3839 return optimizeStrNCat(CI, Builder);
3841 return optimizeStrChr(CI, Builder);
3843 return optimizeStrRChr(CI, Builder);
3845 return optimizeStrCmp(CI, Builder);
3847 return optimizeStrNCmp(CI, Builder);
3849 return optimizeStrCpy(CI, Builder);
3851 return optimizeStpCpy(CI, Builder);
3853 return optimizeStrLCpy(CI, Builder);
3855 return optimizeStringNCpy(CI, /*RetEnd=*/true, Builder);
3857 return optimizeStringNCpy(CI, /*RetEnd=*/false, Builder);
3859 return optimizeStrLen(CI, Builder);
3861 return optimizeStrNLen(CI, Builder);
3863 return optimizeStrPBrk(CI, Builder);
3865 return optimizeStrNDup(CI, Builder);
3873 return optimizeStrTo(CI, Builder);
3875 return optimizeStrSpn(CI, Builder);
3877 return optimizeStrCSpn(CI, Builder);
3879 return optimizeStrStr(CI, Builder);
3881 return optimizeMemChr(CI, Builder);
3883 return optimizeMemRChr(CI, Builder);
3885 return optimizeBCmp(CI, Builder);
3887 return optimizeMemCmp(CI, Builder);
3889 return optimizeMemCpy(CI, Builder);
3891 return optimizeMemCCpy(CI, Builder);
3893 return optimizeMemPCpy(CI, Builder);
3895 return optimizeMemMove(CI, Builder);
3897 return optimizeMemSet(CI, Builder);
3899 return optimizeRealloc(CI, Builder);
3901 return optimizeWcslen(CI, Builder);
3903 return optimizeBCopy(CI, Builder);
3924 return optimizeNew(CI, Builder, Func);
3933 static Value *optimizeNaN(CallInst *CI) {
3935 if (!getConstantStringInfo(CI->getArgOperand(0), CharSeq))
3945 return ConstantFP::getQNaN(CI->getType(), /*Negative=*/false, &Fill);
3948 Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
3951 const Module *M = CI->getModule();
3954 if (CI->isStrictFP())
3957 if (Value *V = optimizeSymmetric(CI, Func, Builder))
3963 return optimizeSinCosPi(CI, /*IsSin*/true, Builder);
3966 return optimizeSinCosPi(CI, /*IsSin*/false, Builder);
3970 return optimizePow(CI, Builder);
3974 return optimizeExp2(CI, Builder);
3978 return replaceUnaryCall(CI, Builder, Intrinsic::fabs);
3982 return optimizeSqrt(CI, Builder);
3986 return optimizeFMod(CI, Builder);
4002 return optimizeLog(CI, Builder);
4018 return optimizeTrigInversionPairs(CI, Builder);
4020 return replaceUnaryCall(CI, Builder, Intrinsic::ceil);
4022 return replaceUnaryCall(CI, Builder, Intrinsic::floor);
4024 return replaceUnaryCall(CI, Builder, Intrinsic::round);
4026 return replaceUnaryCall(CI, Builder, Intrinsic::roundeven);
4028 return replaceUnaryCall(CI, Builder, Intrinsic::nearbyint);
4030 return replaceUnaryCall(CI, Builder, Intrinsic::rint);
4032 return replaceUnaryCall(CI, Builder, Intrinsic::trunc);
4044 if (UnsafeFPShrink && hasFloatVersion(M, CI->getCalledFunction()->getName()))
4045 return optimizeUnaryDoubleFP(CI, Builder, TLI, true);
4048 if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
4049 return optimizeBinaryDoubleFP(CI, Builder, TLI);
4054 return optimizeFdim(CI, Builder);
4061 return optimizeFMinFMax(CI, Builder);
4065 return optimizeCAbs(CI, Builder);
4069 return optimizeRemquo(CI, Builder);
4073 return optimizeNaN(CI);
4079 Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) {
4080 Module *M = CI->getModule();
4081 assert(!CI->isMustTailCall() && "These transforms aren't musttail safe.");
4086 if (CI->isNoBuiltin())
4090 Function *Callee = CI->getCalledFunction();
4091 bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
4094 CI->getOperandBundlesAsDefs(OpBundles);
4104 else if (isa<FPMathOperator>(CI) && CI->isFast())
4108 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
4115 return optimizePow(CI, Builder);
4117 return optimizeExp2(CI, Builder);
4121 return optimizeLog(CI, Builder);
4123 return optimizeSqrt(CI, Builder);
4125 return optimizeMemSet(CI, Builder);
4127 return optimizeMemCpy(CI, Builder);
4129 return optimizeMemMove(CI, Builder);
4137 FortifiedSimplifier.optimizeCall(CI, Builder))
4145 if (Value *V = optimizeStringMemoryLibCall(CI, Builder))
4147 if (Value *V = optimizeFloatingPointLibCall(CI, Func, Builder))
4153 return optimizeFFS(CI, Builder);
4157 return optimizeFls(CI, Builder);
4161 return optimizeAbs(CI, Builder);
4163 return optimizeIsDigit(CI, Builder);
4165 return optimizeIsAscii(CI, Builder);
4167 return optimizeToAscii(CI, Builder);
4171 return optimizeAtoi(CI, Builder);
4174 return optimizeStrToInt(CI, Builder, /*AsSigned=*/true);
4177 return optimizeStrToInt(CI, Builder, /*AsSigned=*/false);
4179 return optimizePrintF(CI, Builder);
4181 return optimizeSPrintF(CI, Builder);
4183 return optimizeSnPrintF(CI, Builder);
4185 return optimizeFPrintF(CI, Builder);
4187 return optimizeFWrite(CI, Builder);
4189 return optimizeFPuts(CI, Builder);
4191 return optimizePuts(CI, Builder);
4193 return optimizeErrorReporting(CI, Builder);
4196 return optimizeErrorReporting(CI, Builder, 0);
4199 return optimizeExit(CI);
4261 CallInst *CI, unsigned ObjSizeOp, std::optional<unsigned> SizeOp,
4266 ConstantInt *Flag = dyn_cast<ConstantInt>(CI->getArgOperand(*FlagOp));
4271 if (SizeOp && CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(*SizeOp))
4275 dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) {
4282 uint64_t Len = GetStringLength(CI->getArgOperand(*StrOp));
4286 annotateDereferenceableBytes(CI, *StrOp, Len);
4294 dyn_cast<ConstantInt>(CI->getArgOperand(*SizeOp)))
4301 Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI,
4303 if (isFortifiedCallFoldable(CI, 3, 2)) {
4305 B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
4306 Align(1), CI->getArgOperand(2));
4307 mergeAttributesAndFlags(NewCI, *CI);
4308 return CI->getArgOperand(0);
4313 Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI,
4315 if (isFortifiedCallFoldable(CI, 3, 2)) {
4317 B.CreateMemMove(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
4318 Align(1), CI->getArgOperand(2));
4319 mergeAttributesAndFlags(NewCI, *CI);
4320 return CI->getArgOperand(0);
4325 Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI,
4327 if (isFortifiedCallFoldable(CI, 3, 2)) {
4328 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
4329 CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val,
4330 CI->getArgOperand(2), Align(1));
4331 mergeAttributesAndFlags(NewCI, *CI);
4332 return CI->getArgOperand(0);
4337 Value *FortifiedLibCallSimplifier::optimizeMemPCpyChk(CallInst *CI,
4339 const DataLayout &DL = CI->getDataLayout();
4340 if (isFortifiedCallFoldable(CI, 3, 2))
4341 if (Value *Call = emitMemPCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4342 CI->getArgOperand(2), B, DL, TLI)) {
4343 return mergeAttributesAndFlags(cast<CallInst>(Call), *CI);
4348 Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
4351 const DataLayout &DL = CI->getDataLayout();
4352 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
4353 *ObjSize = CI->getArgOperand(2);
4366 if (isFortifiedCallFoldable(CI, 2, std::nullopt, 1)) {
4368 return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
4370 return copyFlags(*CI, emitStpCpy(Dst, Src, B, TLI));
4379 annotateDereferenceableBytes(CI, 1, Len);
4383 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
4384 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
4392 return copyFlags(*CI, cast<CallInst>(Ret));
4395 Value *FortifiedLibCallSimplifier::optimizeStrLenChk(CallInst *CI,
4397 if (isFortifiedCallFoldable(CI, 1, std::nullopt, 0))
4398 return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B,
4399 CI->getDataLayout(), TLI));
4403 Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
4406 if (isFortifiedCallFoldable(CI, 3, 2)) {
4408 return copyFlags(*CI,
4409 emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4410 CI->getArgOperand(2), B, TLI));
4412 return copyFlags(*CI,
4413 emitStpNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4414 CI->getArgOperand(2), B, TLI));
4420 Value *FortifiedLibCallSimplifier::optimizeMemCCpyChk(CallInst *CI,
4422 if (isFortifiedCallFoldable(CI, 4, 3))
4424 *CI, emitMemCCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4425 CI->getArgOperand(2), CI->getArgOperand(3), B, TLI));
4430 Value *FortifiedLibCallSimplifier::optimizeSNPrintfChk(CallInst *CI,
4432 if (isFortifiedCallFoldable(CI, 3, 1, std::nullopt, 2)) {
4433 SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 5));
4434 return copyFlags(*CI,
4435 emitSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1),
4436 CI->getArgOperand(4), VariadicArgs, B, TLI));
4442 Value *FortifiedLibCallSimplifier::optimizeSPrintfChk(CallInst *CI,
4444 if (isFortifiedCallFoldable(CI, 2, std::nullopt, std::nullopt, 1)) {
4445 SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 4));
4446 return copyFlags(*CI,
4447 emitSPrintf(CI->getArgOperand(0), CI->getArgOperand(3),
4454 Value *FortifiedLibCallSimplifier::optimizeStrCatChk(CallInst *CI,
4456 if (isFortifiedCallFoldable(CI, 2))
4458 *CI, emitStrCat(CI->getArgOperand(0), CI->getArgOperand(1), B, TLI));
4463 Value *FortifiedLibCallSimplifier::optimizeStrLCat(CallInst *CI,
4465 if (isFortifiedCallFoldable(CI, 3))
4466 return copyFlags(*CI,
4467 emitStrLCat(CI->getArgOperand(0), CI->getArgOperand(1),
4468 CI->getArgOperand(2), B, TLI));
4473 Value *FortifiedLibCallSimplifier::optimizeStrNCatChk(CallInst *CI,
4475 if (isFortifiedCallFoldable(CI, 3))
4476 return copyFlags(*CI,
4477 emitStrNCat(CI->getArgOperand(0), CI->getArgOperand(1),
4478 CI->getArgOperand(2), B, TLI));
4483 Value *FortifiedLibCallSimplifier::optimizeStrLCpyChk(CallInst *CI,
4485 if (isFortifiedCallFoldable(CI, 3))
4486 return copyFlags(*CI,
4487 emitStrLCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4488 CI->getArgOperand(2), B, TLI));
4493 Value *FortifiedLibCallSimplifier::optimizeVSNPrintfChk(CallInst *CI,
4495 if (isFortifiedCallFoldable(CI, 3, 1, std::nullopt, 2))
4497 *CI, emitVSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1),
4498 CI->getArgOperand(4), CI->getArgOperand(5), B, TLI));
4503 Value *FortifiedLibCallSimplifier::optimizeVSPrintfChk(CallInst *CI,
4505 if (isFortifiedCallFoldable(CI, 2, std::nullopt, std::nullopt, 1))
4506 return copyFlags(*CI,
4507 emitVSPrintf(CI->getArgOperand(0), CI->getArgOperand(3),
4508 CI->getArgOperand(4), B, TLI));
4513 Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI,
4529 Function *Callee = CI->getCalledFunction();
4530 bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
4533 CI->getOperandBundlesAsDefs(OpBundles);
4549 return optimizeMemCpyChk(CI, Builder);
4551 return optimizeMemPCpyChk(CI, Builder);
4553 return optimizeMemMoveChk(CI, Builder);
4555 return optimizeMemSetChk(CI, Builder);
4558 return optimizeStrpCpyChk(CI, Builder, Func);
4560 return optimizeStrLenChk(CI, Builder);
4563 return optimizeStrpNCpyChk(CI, Builder, Func);
4565 return optimizeMemCCpyChk(CI, Builder);
4567 return optimizeSNPrintfChk(CI, Builder);
4569 return optimizeSPrintfChk(CI, Builder);
4571 return optimizeStrCatChk(CI, Builder);
4573 return optimizeStrLCat(CI, Builder);
4575 return optimizeStrNCatChk(CI, Builder);
4577 return optimizeStrLCpyChk(CI, Builder);
4579 return optimizeVSNPrintfChk(CI, Builder);
4581 return optimizeVSPrintfChk(CI, Builder);