Lines Matching defs:CI

118 static bool callHasFloatingPointArgument(const CallInst *CI) {
119 return any_of(CI->operands(), [](const Use &OI) {
124 static bool callHasFP128Argument(const CallInst *CI) {
125 return any_of(CI->operands(), [](const Use &OI) {
137 static Value *convertStrToInt(CallInst *CI, StringRef &Str, Value *EndPtr,
170 Type *RetTy = CI->getType();
227 Value *StrBeg = CI->getArgOperand(0);
251 static bool canTransformToMemCmp(CallInst *CI, Value *Str, uint64_t Len,
253 if (!isOnlyUsedInComparisonWithZero(CI))
259 if (CI->getFunction()->hasFnAttribute(Attribute::SanitizeMemory))
265 static void annotateDereferenceableBytes(CallInst *CI,
268 const Function *F = CI->getCaller();
273 unsigned AS = CI->getArgOperand(ArgNo)->getType()->getPointerAddressSpace();
275 CI->paramHasAttr(ArgNo, Attribute::NonNull))
276 DerefBytes = std::max(CI->getParamDereferenceableOrNullBytes(ArgNo),
279 if (CI->getParamDereferenceableBytes(ArgNo) < DerefBytes) {
280 CI->removeParamAttr(ArgNo, Attribute::Dereferenceable);
282 CI->paramHasAttr(ArgNo, Attribute::NonNull))
283 CI->removeParamAttr(ArgNo, Attribute::DereferenceableOrNull);
284 CI->addParamAttr(ArgNo, Attribute::getWithDereferenceableBytes(
285 CI->getContext(), DerefBytes));
290 static void annotateNonNullNoUndefBasedOnAccess(CallInst *CI,
292 Function *F = CI->getCaller();
297 if (!CI->paramHasAttr(ArgNo, Attribute::NoUndef))
298 CI->addParamAttr(ArgNo, Attribute::NoUndef);
300 if (!CI->paramHasAttr(ArgNo, Attribute::NonNull)) {
302 CI->getArgOperand(ArgNo)->getType()->getPointerAddressSpace();
305 CI->addParamAttr(ArgNo, Attribute::NonNull);
308 annotateDereferenceableBytes(CI, ArgNo, 1);
312 static void annotateNonNullAndDereferenceable(CallInst *CI, ArrayRef<unsigned> ArgNos,
315 annotateNonNullNoUndefBasedOnAccess(CI, ArgNos);
316 annotateDereferenceableBytes(CI, ArgNos, LenC->getZExtValue());
318 annotateNonNullNoUndefBasedOnAccess(CI, ArgNos);
323 annotateDereferenceableBytes(CI, ArgNos, DerefMin);
357 Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilderBase &B) {
359 Value *Dst = CI->getArgOperand(0);
360 Value *Src = CI->getArgOperand(1);
361 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
366 annotateDereferenceableBytes(CI, 1, Len);
375 return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, Len, B));
399 Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilderBase &B) {
401 Value *Dst = CI->getArgOperand(0);
402 Value *Src = CI->getArgOperand(1);
403 Value *Size = CI->getArgOperand(2);
405 annotateNonNullNoUndefBasedOnAccess(CI, 0);
407 annotateNonNullNoUndefBasedOnAccess(CI, 1);
423 annotateDereferenceableBytes(CI, 1, SrcLen);
439 return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, SrcLen, B));
445 static Value* memChrToCharCompare(CallInst *CI, Value *NBytes,
448 Value *Src = CI->getArgOperand(0);
449 Value *CharVal = CI->getArgOperand(1);
463 Value *NullPtr = Constant::getNullValue(CI->getType());
467 Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilderBase &B) {
468 Value *SrcStr = CI->getArgOperand(0);
469 Value *CharVal = CI->getArgOperand(1);
470 annotateNonNullNoUndefBasedOnAccess(CI, 0);
472 if (isOnlyUsedInEqualityComparison(CI, SrcStr))
473 return memChrToCharCompare(CI, nullptr, B, DL);
481 annotateDereferenceableBytes(CI, 0, Len);
485 Function *Callee = CI->getCalledFunction();
491 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
492 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
493 return copyFlags(*CI,
500 Value *NullPtr = Constant::getNullValue(CI->getType());
501 if (isOnlyUsedInEqualityComparison(CI, NullPtr))
504 return B.CreateIntToPtr(B.getTrue(), CI->getType());
523 return Constant::getNullValue(CI->getType());
529 Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilderBase &B) {
530 Value *SrcStr = CI->getArgOperand(0);
531 Value *CharVal = CI->getArgOperand(1);
533 annotateNonNullNoUndefBasedOnAccess(CI, 0);
539 return copyFlags(*CI, emitStrChr(SrcStr, '\0', B, TLI));
543 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
544 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
550 return copyFlags(*CI, emitMemRChr(SrcStr, CharVal, Size, B, DL, TLI));
553 Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilderBase &B) {
554 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
556 return ConstantInt::get(CI->getType(), 0);
564 return ConstantInt::get(CI->getType(),
569 B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
573 CI->getType());
578 annotateDereferenceableBytes(CI, 0, Len1);
581 annotateDereferenceableBytes(CI, 1, Len2);
585 *CI, emitMemCmp(Str1P, Str2P,
586 ConstantInt::get(DL.getIntPtrType(CI->getContext()),
593 if (canTransformToMemCmp(CI, Str1P, Len2, DL))
595 *CI,
597 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len2),
600 if (canTransformToMemCmp(CI, Str2P, Len1, DL))
602 *CI,
604 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len1),
608 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
612 // Optimize a memcmp or, when StrNCmp is true, strncmp call CI with constant
614 static Value *optimizeMemCmpVarSize(CallInst *CI, Value *LHS, Value *RHS,
618 Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilderBase &B) {
619 Value *Str1P = CI->getArgOperand(0);
620 Value *Str2P = CI->getArgOperand(1);
621 Value *Size = CI->getArgOperand(2);
623 return ConstantInt::get(CI->getType(), 0);
626 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
632 return optimizeMemCmpVarSize(CI, Str1P, Str2P, Size, true, B, DL);
635 return ConstantInt::get(CI->getType(), 0);
638 return copyFlags(*CI, emitMemCmp(Str1P, Str2P, Size, B, DL, TLI));
649 return ConstantInt::get(CI->getType(),
655 B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
659 CI->getType());
663 annotateDereferenceableBytes(CI, 0, Len1);
666 annotateDereferenceableBytes(CI, 1, Len2);
671 if (canTransformToMemCmp(CI, Str1P, Len2, DL))
673 *CI,
675 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len2),
679 if (canTransformToMemCmp(CI, Str2P, Len1, DL))
681 *CI,
683 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len1),
690 Value *LibCallSimplifier::optimizeStrNDup(CallInst *CI, IRBuilderBase &B) {
691 Value *Src = CI->getArgOperand(0);
692 ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
695 annotateDereferenceableBytes(CI, 0, SrcLen);
697 return copyFlags(*CI, emitStrDup(Src, B, TLI));
703 Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilderBase &B) {
704 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
708 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
712 annotateDereferenceableBytes(CI, 1, Len);
720 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len));
721 mergeAttributesAndFlags(NewCI, *CI);
725 Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilderBase &B) {
726 Function *Callee = CI->getCalledFunction();
727 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
730 if (CI->use_empty())
731 return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
741 annotateDereferenceableBytes(CI, 1, Len);
753 mergeAttributesAndFlags(NewCI, *CI);
759 Value *LibCallSimplifier::optimizeStrLCpy(CallInst *CI, IRBuilderBase &B) {
760 Value *Size = CI->getArgOperand(2);
764 annotateNonNullNoUndefBasedOnAccess(CI, 0);
767 annotateNonNullNoUndefBasedOnAccess(CI, 1);
775 Value *Dst = CI->getArgOperand(0);
776 Value *Src = CI->getArgOperand(1);
783 return copyFlags(*CI, emitStrLen(Src, B, DL, TLI));
812 return ConstantInt::get(CI->getType(), 0);
815 Function *Callee = CI->getCalledFunction();
822 mergeAttributesAndFlags(NewCI, *CI);
825 Value *EndOff = ConstantInt::get(CI->getType(), NBytes);
833 return ConstantInt::get(CI->getType(), SrcLen);
836 // Optimize a call CI to either stpncpy when RetEnd is true, or to strncpy
838 Value *LibCallSimplifier::optimizeStringNCpy(CallInst *CI, bool RetEnd,
840 Function *Callee = CI->getCalledFunction();
841 Value *Dst = CI->getArgOperand(0);
842 Value *Src = CI->getArgOperand(1);
843 Value *Size = CI->getArgOperand(2);
848 annotateNonNullNoUndefBasedOnAccess(CI, 0);
849 annotateNonNullNoUndefBasedOnAccess(CI, 1);
882 annotateDereferenceableBytes(CI, 1, SrcLen);
891 CI->getAttributes().getParamAttrs(0).getAlignment().valueOrOne();
893 AttrBuilder ArgAttrs(CI->getContext(), CI->getAttributes().getParamAttrs(0));
895 CI->getContext(), 0, ArgAttrs));
896 copyFlags(*CI, NewCI);
921 mergeAttributesAndFlags(NewCI, *CI);
931 Value *LibCallSimplifier::optimizeStringLength(CallInst *CI, IRBuilderBase &B,
934 Value *Src = CI->getArgOperand(0);
937 if (isOnlyUsedInZeroEqualityComparison(CI) &&
946 CI->getType());
953 return ConstantInt::get(CI->getType(), 0);
960 return B.CreateZExt(Cmp, CI->getType());
966 Value *LenC = ConstantInt::get(CI->getType(), Len - 1);
1011 KnownBits Known = computeKnownBits(Offset, DL, 0, nullptr, CI, nullptr);
1022 Offset = B.CreateSExtOrTrunc(Offset, CI->getType());
1023 return B.CreateSub(ConstantInt::get(CI->getType(), NullTermIdx),
1035 return OptimizationRemark("instcombine", "simplify-libcalls", CI)
1039 ConstantInt::get(CI->getType(), LenTrue - 1),
1040 ConstantInt::get(CI->getType(), LenFalse - 1));
1047 Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilderBase &B) {
1048 if (Value *V = optimizeStringLength(CI, B, 8))
1050 annotateNonNullNoUndefBasedOnAccess(CI, 0);
1054 Value *LibCallSimplifier::optimizeStrNLen(CallInst *CI, IRBuilderBase &B) {
1055 Value *Bound = CI->getArgOperand(1);
1056 if (Value *V = optimizeStringLength(CI, B, 8, Bound))
1060 annotateNonNullNoUndefBasedOnAccess(CI, 0);
1064 Value *LibCallSimplifier::optimizeWcslen(CallInst *CI, IRBuilderBase &B) {
1065 Module &M = *CI->getModule();
1071 return optimizeStringLength(CI, B, WCharSize);
1074 Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilderBase &B) {
1076 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
1077 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
1082 return Constant::getNullValue(CI->getType());
1088 return Constant::getNullValue(CI->getType());
1090 return B.CreateInBoundsGEP(B.getInt8Ty(), CI->getArgOperand(0),
1096 return copyFlags(*CI, emitStrChr(CI->getArgOperand(0), S2[0], B, TLI));
1101 Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilderBase &B) {
1102 Value *EndPtr = CI->getArgOperand(1);
1106 CI->addParamAttr(0, Attribute::NoCapture);
1112 Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilderBase &B) {
1114 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
1115 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
1120 return Constant::getNullValue(CI->getType());
1127 return ConstantInt::get(CI->getType(), Pos);
1133 Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilderBase &B) {
1135 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
1136 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
1140 return Constant::getNullValue(CI->getType());
1147 return ConstantInt::get(CI->getType(), Pos);
1152 return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B, DL, TLI));
1157 Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilderBase &B) {
1159 if (CI->getArgOperand(0) == CI->getArgOperand(1))
1160 return CI->getArgOperand(0);
1163 if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
1164 Value *StrLen = emitStrLen(CI->getArgOperand(1), B, DL, TLI);
1167 Value *StrNCmp = emitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
1171 for (User *U : llvm::make_early_inc_range(CI->users())) {
1178 return CI;
1183 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
1184 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
1188 return CI->getArgOperand(0);
1195 return Constant::getNullValue(CI->getType());
1198 return B.CreateConstInBoundsGEP1_64(B.getInt8Ty(), CI->getArgOperand(0),
1204 return emitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI);
1207 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
1211 Value *LibCallSimplifier::optimizeMemRChr(CallInst *CI, IRBuilderBase &B) {
1212 Value *SrcStr = CI->getArgOperand(0);
1213 Value *Size = CI->getArgOperand(2);
1214 annotateNonNullAndDereferenceable(CI, 0, Size, DL);
1215 Value *CharVal = CI->getArgOperand(1);
1217 Value *NullPtr = Constant::getNullValue(CI->getType());
1299 Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) {
1300 Value *SrcStr = CI->getArgOperand(0);
1301 Value *Size = CI->getArgOperand(2);
1304 annotateNonNullNoUndefBasedOnAccess(CI, 0);
1305 if (isOnlyUsedInEqualityComparison(CI, SrcStr))
1306 return memChrToCharCompare(CI, Size, B, DL);
1309 Value *CharVal = CI->getArgOperand(1);
1312 Value *NullPtr = Constant::getNullValue(CI->getType());
1398 if (isOnlyUsedInEqualityComparison(CI, SrcStr))
1402 return memChrToCharCompare(CI, Size, B, DL);
1408 bool OptForSize = CI->getFunction()->hasOptSize() ||
1409 llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
1422 if (OptForSize || Str.empty() || !isOnlyUsedInZeroEqualityComparison(CI))
1460 return B.CreateIntToPtr(B.CreateOr(CharCompares), CI->getType());
1488 CI->getType());
1491 // Optimize a memcmp or, when StrNCmp is true, strncmp call CI with constant
1493 static Value *optimizeMemCmpVarSize(CallInst *CI, Value *LHS, Value *RHS,
1497 return Constant::getNullValue(CI->getType());
1510 Value *Zero = ConstantInt::get(CI->getType(), 0);
1530 Value *Res = ConstantInt::get(CI->getType(), IRes);
1534 // Optimize a memcmp call CI with constant size Len.
1535 static Value *optimizeMemCmpConstantSize(CallInst *CI, Value *LHS, Value *RHS,
1539 return Constant::getNullValue(CI->getType());
1544 CI->getType(), "lhsv");
1546 CI->getType(), "rhsv");
1553 if (DL.isLegalInteger(Len * 8) && isOnlyUsedInZeroEqualityComparison(CI)) {
1554 IntegerType *IntType = IntegerType::get(CI->getContext(), Len * 8);
1568 if ((LHSV || getKnownAlignment(LHS, DL, CI) >= PrefAlignment) &&
1569 (RHSV || getKnownAlignment(RHS, DL, CI) >= PrefAlignment)) {
1574 return B.CreateZExt(B.CreateICmpNE(LHSV, RHSV), CI->getType(), "memcmp");
1582 Value *LibCallSimplifier::optimizeMemCmpBCmpCommon(CallInst *CI,
1584 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
1585 Value *Size = CI->getArgOperand(2);
1587 annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
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) {
1601 Module *M = CI->getModule();
1602 if (Value *V = optimizeMemCmpBCmpCommon(CI, B))
1609 isOnlyUsedInZeroEqualityComparison(CI)) {
1610 Value *LHS = CI->getArgOperand(0);
1611 Value *RHS = CI->getArgOperand(1);
1612 Value *Size = CI->getArgOperand(2);
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) {
1624 Value *Size = CI->getArgOperand(2);
1625 annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
1626 if (isa<IntrinsicInst>(CI))
1630 CallInst *NewCI = B.CreateMemCpy(CI->getArgOperand(0), Align(1),
1631 CI->getArgOperand(1), Align(1), Size);
1632 mergeAttributesAndFlags(NewCI, *CI);
1633 return CI->getArgOperand(0);
1636 Value *LibCallSimplifier::optimizeMemCCpy(CallInst *CI, IRBuilderBase &B) {
1637 Value *Dst = CI->getArgOperand(0);
1638 Value *Src = CI->getArgOperand(1);
1639 ConstantInt *StopChar = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1640 ConstantInt *N = dyn_cast<ConstantInt>(CI->getArgOperand(3));
1642 if (CI->use_empty() && Dst == Src)
1647 return Constant::getNullValue(CI->getType());
1660 copyFlags(*CI, B.CreateMemCpy(Dst, Align(1), Src, Align(1),
1661 CI->getArgOperand(3)));
1662 return Constant::getNullValue(CI->getType());
1670 copyFlags(*CI, B.CreateMemCpy(Dst, Align(1), Src, Align(1), NewN));
1673 : Constant::getNullValue(CI->getType());
1676 Value *LibCallSimplifier::optimizeMemPCpy(CallInst *CI, IRBuilderBase &B) {
1677 Value *Dst = CI->getArgOperand(0);
1678 Value *N = CI->getArgOperand(2);
1681 B.CreateMemCpy(Dst, Align(1), CI->getArgOperand(1), Align(1), N);
1685 mergeAttributesAndFlags(NewCI, *CI);
1689 Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilderBase &B) {
1690 Value *Size = CI->getArgOperand(2);
1691 annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
1692 if (isa<IntrinsicInst>(CI))
1696 CallInst *NewCI = B.CreateMemMove(CI->getArgOperand(0), Align(1),
1697 CI->getArgOperand(1), Align(1), Size);
1698 mergeAttributesAndFlags(NewCI, *CI);
1699 return CI->getArgOperand(0);
1702 Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilderBase &B) {
1703 Value *Size = CI->getArgOperand(2);
1704 annotateNonNullAndDereferenceable(CI, 0, Size, DL);
1705 if (isa<IntrinsicInst>(CI))
1709 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
1710 CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val, Size, Align(1));
1711 mergeAttributesAndFlags(NewCI, *CI);
1712 return CI->getArgOperand(0);
1715 Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilderBase &B) {
1716 if (isa<ConstantPointerNull>(CI->getArgOperand(0)))
1717 return copyFlags(*CI, emitMalloc(CI->getArgOperand(1), B, DL, TLI));
1726 Value *LibCallSimplifier::optimizeNew(CallInst *CI, IRBuilderBase &B,
1732 if (CI->getAttributes().getFnAttr("memprof").getValueAsString() == "cold")
1734 else if (CI->getAttributes().getFnAttr("memprof").getValueAsString() ==
1737 else if (CI->getAttributes().getFnAttr("memprof").getValueAsString() == "hot")
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,
1856 // Replace a libcall \p CI with a call to intrinsic \p IID
1857 static Value *replaceUnaryCall(CallInst *CI, IRBuilderBase &B,
1859 CallInst *NewCall = B.CreateUnaryIntrinsic(IID, CI->getArgOperand(0), CI);
1860 NewCall->takeName(CI);
1861 return copyFlags(*CI, NewCall);
1887 static Value *optimizeDoubleFP(CallInst *CI, IRBuilderBase &B,
1890 Function *CalleeFn = CI->getCalledFunction();
1891 if (!CI->getType()->isDoubleTy() || !CalleeFn)
1898 for (User *U : CI->users()) {
1906 V[0] = valueHasFloatPrecision(CI->getArgOperand(0));
1907 V[1] = isBinary ? valueHasFloatPrecision(CI->getArgOperand(1)) : nullptr;
1919 StringRef CallerName = CI->getFunction()->getName();
1928 B.setFastMathFlags(CI->getFastMathFlags());
1933 Module *M = CI->getModule();
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) {
1964 if (CI->arg_size() == 1) {
1966 if (!CI->isFast())
1969 Value *Op = CI->getArgOperand(0);
1976 assert(CI->arg_size() == 2 && "Unexpected signature for cabs!");
1978 Real = CI->getArgOperand(0);
1979 Imag = CI->getArgOperand(1);
1995 B.setFastMathFlags(CI->getFastMathFlags());
1998 *CI, B.CreateUnaryIntrinsic(Intrinsic::fabs, AbsOp, nullptr, "cabs"));
2001 if (!CI->isFast())
2007 B.setFastMathFlags(CI->getFastMathFlags());
2012 return copyFlags(*CI, B.CreateUnaryIntrinsic(Intrinsic::sqrt,
2403 Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
2404 Module *M = CI->getModule();
2405 Function *Callee = CI->getCalledFunction();
2410 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2420 Type *Ty = CI->getType();
2426 Value *Op = CI->getArgOperand(0);
2434 return copyFlags(*CI, B.CreateIntrinsic(Intrinsic::ldexp,
2436 {One, Exp}, CI));
2440 B.setFastMathFlags(CI->getFastMathFlags());
2441 return copyFlags(*CI, emitBinaryFloatFnCall(
2450 Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) {
2451 Module *M = CI->getModule();
2455 Function *Callee = CI->getCalledFunction();
2458 if (Value *Ret = optimizeBinaryDoubleFP(CI, B, TLI))
2469 FastMathFlags FMF = CI->getFastMathFlags();
2475 return copyFlags(*CI, B.CreateBinaryIntrinsic(IID, CI->getArgOperand(0),
2476 CI->getArgOperand(1)));
2634 Value *LibCallSimplifier::mergeSqrtToExp(CallInst *CI, IRBuilderBase &B) {
2635 if (!CI->hasAllowReassoc())
2638 Function *SqrtFn = CI->getCalledFunction();
2639 CallInst *Arg = dyn_cast<CallInst>(CI->getArgOperand(0));
2669 if (CI->getType()->getScalarType()->isFloatTy()) {
2673 } else if (CI->getType()->getScalarType()->isDoubleTy()) {
2691 CI, "merged.sqrt");
2697 Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
2698 Module *M = CI->getModule();
2699 Function *Callee = CI->getCalledFunction();
2707 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2709 if (Value *Opt = mergeSqrtToExp(CI, B))
2712 if (!CI->isFast())
2715 Instruction *I = dyn_cast<Instruction>(CI->getArgOperand(0));
2763 return copyFlags(*CI, B.CreateFMul(FabsCall, SqrtCall));
2765 return copyFlags(*CI, FabsCall);
2768 Value *LibCallSimplifier::optimizeTrigInversionPairs(CallInst *CI,
2770 Module *M = CI->getModule();
2771 Function *Callee = CI->getCalledFunction();
2778 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2780 Value *Op1 = CI->getArgOperand(0);
2786 if (!CI->isFast() || !OpC->isFast())
2821 static bool isTrigLibCall(CallInst *CI) {
2825 return CI->doesNotThrow() && CI->doesNotAccessMemory();
2884 static Value *optimizeSymmetricCall(CallInst *CI, bool IsEven,
2887 Value *Src = CI->getArgOperand(0);
2891 B.setFastMathFlags(CI->getFastMathFlags());
2893 auto *CallInst = copyFlags(*CI, B.CreateCall(CI->getCalledFunction(), {X}));
2906 B.setFastMathFlags(CI->getFastMathFlags());
2908 auto *CallInst = copyFlags(*CI, B.CreateCall(CI->getCalledFunction(), {X}));
2915 Value *LibCallSimplifier::optimizeSymmetric(CallInst *CI, LibFunc Func,
2921 return optimizeSymmetricCall(CI, /*IsEven*/ true, B);
2934 return optimizeSymmetricCall(CI, /*IsEven*/ false, B);
2941 Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, bool IsSin, IRBuilderBase &B) {
2944 if (!isTrigLibCall(CI))
2947 Value *Arg = CI->getArgOperand(0);
2957 Function *F = CI->getFunction();
2966 if (!insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos,
2988 auto *CI = dyn_cast<CallInst>(Val);
2989 if (!CI || CI->use_empty())
2993 if (CI->getFunction() != F)
2996 Module *M = CI->getModule();
2997 Function *Callee = CI->getCalledFunction();
3001 !isTrigLibCall(CI))
3006 SinCalls.push_back(CI);
3008 CosCalls.push_back(CI);
3010 SinCosCalls.push_back(CI);
3013 SinCalls.push_back(CI);
3015 CosCalls.push_back(CI);
3017 SinCosCalls.push_back(CI);
3025 Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilderBase &B) {
3028 Type *RetType = CI->getType();
3029 Value *Op = CI->getArgOperand(0);
3040 Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilderBase &B) {
3043 Value *Op = CI->getArgOperand(0);
3049 return B.CreateIntCast(V, CI->getType(), false);
3052 Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilderBase &B) {
3055 Value *X = CI->getArgOperand(0);
3061 Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilderBase &B) {
3063 Value *Op = CI->getArgOperand(0);
3067 return B.CreateZExt(Op, CI->getType());
3070 Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilderBase &B) {
3072 Value *Op = CI->getArgOperand(0);
3075 return B.CreateZExt(Op, CI->getType());
3078 Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilderBase &B) {
3080 return B.CreateAnd(CI->getArgOperand(0),
3081 ConstantInt::get(CI->getType(), 0x7F));
3085 Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilderBase &B) {
3086 CI->addParamAttr(0, Attribute::NoCapture);
3089 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
3092 return convertStrToInt(CI, Str, nullptr, 10, /*AsSigned=*/true, B);
3096 Value *LibCallSimplifier::optimizeStrToInt(CallInst *CI, IRBuilderBase &B,
3098 Value *EndPtr = CI->getArgOperand(1);
3102 CI->addParamAttr(0, Attribute::NoCapture);
3108 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
3111 if (ConstantInt *CInt = dyn_cast<ConstantInt>(CI->getArgOperand(2))) {
3112 return convertStrToInt(CI, Str, EndPtr, CInt->getSExtValue(), AsSigned, B);
3122 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
3124 Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilderBase &B,
3126 Function *Callee = CI->getCalledFunction();
3135 if (!CI->hasFnAttr(Attribute::Cold) &&
3136 isReportingError(Callee, CI, StreamArg)) {
3137 CI->addFnAttr(Attribute::Cold);
3143 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {
3153 if (StreamArg >= (int)CI->arg_size())
3155 LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
3164 Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
3167 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
3172 return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);
3177 if (!CI->use_empty())
3180 Type *IntTy = CI->getType();
3187 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3191 if (FormatStr == "%s" && CI->arg_size() > 1) {
3193 if (!getConstantStringInfo(CI->getOperand(1), OperandStr))
3197 return (Value *)CI;
3204 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3210 return copyFlags(*CI, emitPutS(GV, B, TLI));
3222 return copyFlags(*CI, emitPutS(GV, B, TLI));
3227 if (FormatStr == "%c" && CI->arg_size() > 1 &&
3228 CI->getArgOperand(1)->getType()->isIntegerTy()) {
3231 Value *IntChar = B.CreateIntCast(CI->getArgOperand(1), IntTy, false);
3232 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3236 if (FormatStr == "%s\n" && CI->arg_size() > 1 &&
3237 CI->getArgOperand(1)->getType()->isPointerTy())
3238 return copyFlags(*CI, emitPutS(CI->getArgOperand(1), B, TLI));
3242 Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilderBase &B) {
3244 Module *M = CI->getModule();
3245 Function *Callee = CI->getCalledFunction();
3247 if (Value *V = optimizePrintFString(CI, B)) {
3251 annotateNonNullNoUndefBasedOnAccess(CI, 0);
3256 !callHasFloatingPointArgument(CI)) {
3259 CallInst *New = cast<CallInst>(CI->clone());
3268 !callHasFP128Argument(CI)) {
3271 CallInst *New = cast<CallInst>(CI->clone());
3280 Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI,
3284 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
3288 Value *Dest = CI->getArgOperand(0);
3289 if (CI->arg_size() == 2) {
3297 Dest, Align(1), CI->getArgOperand(1), Align(1),
3298 ConstantInt::get(DL.getIntPtrType(CI->getContext()),
3300 return ConstantInt::get(CI->getType(), FormatStr.size());
3305 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
3311 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
3313 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
3319 return ConstantInt::get(CI->getType(), 1);
3325 if (!CI->getArgOperand(2)->getType()->isPointerTy())
3328 if (CI->use_empty())
3330 return copyFlags(*CI, emitStrCpy(Dest, CI->getArgOperand(2), B, TLI));
3332 uint64_t SrcLen = GetStringLength(CI->getArgOperand(2));
3335 Dest, Align(1), CI->getArgOperand(2), Align(1),
3336 ConstantInt::get(DL.getIntPtrType(CI->getContext()), SrcLen));
3338 return ConstantInt::get(CI->getType(), SrcLen - 1);
3339 } else if (Value *V = emitStpCpy(Dest, CI->getArgOperand(2), B, TLI)) {
3342 return B.CreateIntCast(PtrDiff, CI->getType(), false);
3345 bool OptForSize = CI->getFunction()->hasOptSize() ||
3346 llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3351 Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
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) {
3365 Module *M = CI->getModule();
3366 Function *Callee = CI->getCalledFunction();
3368 if (Value *V = optimizeSPrintFString(CI, B)) {
3372 annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
3377 !callHasFloatingPointArgument(CI)) {
3380 CallInst *New = cast<CallInst>(CI->clone());
3389 !callHasFP128Argument(CI)) {
3392 CallInst *New = cast<CallInst>(CI->clone());
3401 // Transform an snprintf call CI with the bound N to format the string Str
3406 Value *LibCallSimplifier::emitSnPrintfMemCpy(CallInst *CI, Value *StrArg,
3419 Value *StrLen = ConstantInt::get(CI->getType(), Str.size());
3433 Value *DstArg = CI->getArgOperand(0);
3437 *CI,
3440 ConstantInt::get(DL.getIntPtrType(CI->getContext()), NCopy)));
3455 Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI,
3458 ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
3469 Value *DstArg = CI->getArgOperand(0);
3470 Value *FmtArg = CI->getArgOperand(2);
3478 if (CI->arg_size() == 3) {
3484 return emitSnPrintfMemCpy(CI, FmtArg, FormatStr, N, B);
3489 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() != 4)
3499 return emitSnPrintfMemCpy(CI, nullptr, CharStr, N, B);
3503 if (!CI->getArgOperand(3)->getType()->isIntegerTy())
3505 Value *V = B.CreateTrunc(CI->getArgOperand(3), B.getInt8Ty(), "char");
3510 return ConstantInt::get(CI->getType(), 1);
3516 Value *StrArg = CI->getArgOperand(3);
3522 return emitSnPrintfMemCpy(CI, StrArg, Str, N, B);
3525 Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilderBase &B) {
3526 if (Value *V = optimizeSnPrintFString(CI, B)) {
3530 if (isKnownNonZero(CI->getOperand(1), DL))
3531 annotateNonNullNoUndefBasedOnAccess(CI, 0);
3535 Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI,
3537 optimizeErrorReporting(CI, B, 0);
3541 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
3547 if (!CI->use_empty())
3551 if (CI->arg_size() == 2) {
3556 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
3557 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
3559 *CI, emitFWrite(CI->getArgOperand(1),
3561 CI->getArgOperand(0), B, DL, TLI));
3566 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
3572 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
3575 Value *V = B.CreateIntCast(CI->getArgOperand(2), IntTy, /*isSigned*/ true,
3577 return copyFlags(*CI, emitFPutC(V, CI->getArgOperand(0), B, TLI));
3582 if (!CI->getArgOperand(2)->getType()->isPointerTy())
3585 *CI, emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI));
3590 Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilderBase &B) {
3591 Module *M = CI->getModule();
3592 Function *Callee = CI->getCalledFunction();
3594 if (Value *V = optimizeFPrintFString(CI, B)) {
3601 !callHasFloatingPointArgument(CI)) {
3604 CallInst *New = cast<CallInst>(CI->clone());
3613 !callHasFP128Argument(CI)) {
3617 CallInst *New = cast<CallInst>(CI->clone());
3626 Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilderBase &B) {
3627 optimizeErrorReporting(CI, B, 3);
3630 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
3631 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
3637 return ConstantInt::get(CI->getType(), 0);
3641 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
3642 Value *Char = B.CreateLoad(B.getInt8Ty(), CI->getArgOperand(0), "char");
3645 Value *NewCI = emitFPutC(Cast, CI->getArgOperand(3), B, TLI);
3646 return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
3653 Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) {
3654 optimizeErrorReporting(CI, B, 1);
3658 bool OptForSize = CI->getFunction()->hasOptSize() ||
3659 llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3665 if (!CI->use_empty())
3669 uint64_t Len = GetStringLength(CI->getArgOperand(0));
3674 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
3675 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
3677 *CI,
3678 emitFWrite(CI->getArgOperand(0),
3680 CI->getArgOperand(1), B, DL, TLI));
3683 Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilderBase &B) {
3684 annotateNonNullNoUndefBasedOnAccess(CI, 0);
3685 if (!CI->use_empty())
3691 if (getConstantStringInfo(CI->getArgOperand(0), Str) && Str.empty()) {
3694 Type *IntTy = CI->getType();
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),
3704 CI->getArgOperand(0), Align(1),
3705 CI->getArgOperand(2)));
3714 Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI,
3716 Module *M = CI->getModule();
3718 Function *Callee = CI->getCalledFunction();
3724 TargetLibraryInfoImpl::isCallingConvCCompatible(CI)) &&
3728 return optimizeStrCat(CI, Builder);
3730 return optimizeStrNCat(CI, Builder);
3732 return optimizeStrChr(CI, Builder);
3734 return optimizeStrRChr(CI, Builder);
3736 return optimizeStrCmp(CI, Builder);
3738 return optimizeStrNCmp(CI, Builder);
3740 return optimizeStrCpy(CI, Builder);
3742 return optimizeStpCpy(CI, Builder);
3744 return optimizeStrLCpy(CI, Builder);
3746 return optimizeStringNCpy(CI, /*RetEnd=*/true, Builder);
3748 return optimizeStringNCpy(CI, /*RetEnd=*/false, Builder);
3750 return optimizeStrLen(CI, Builder);
3752 return optimizeStrNLen(CI, Builder);
3754 return optimizeStrPBrk(CI, Builder);
3756 return optimizeStrNDup(CI, Builder);
3764 return optimizeStrTo(CI, Builder);
3766 return optimizeStrSpn(CI, Builder);
3768 return optimizeStrCSpn(CI, Builder);
3770 return optimizeStrStr(CI, Builder);
3772 return optimizeMemChr(CI, Builder);
3774 return optimizeMemRChr(CI, Builder);
3776 return optimizeBCmp(CI, Builder);
3778 return optimizeMemCmp(CI, Builder);
3780 return optimizeMemCpy(CI, Builder);
3782 return optimizeMemCCpy(CI, Builder);
3784 return optimizeMemPCpy(CI, Builder);
3786 return optimizeMemMove(CI, Builder);
3788 return optimizeMemSet(CI, Builder);
3790 return optimizeRealloc(CI, Builder);
3792 return optimizeWcslen(CI, Builder);
3794 return optimizeBCopy(CI, Builder);
3811 return optimizeNew(CI, Builder, Func);
3819 Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
3822 const Module *M = CI->getModule();
3825 if (CI->isStrictFP())
3828 if (Value *V = optimizeSymmetric(CI, Func, Builder))
3834 return optimizeSinCosPi(CI, /*IsSin*/true, Builder);
3837 return optimizeSinCosPi(CI, /*IsSin*/false, Builder);
3841 return optimizePow(CI, Builder);
3845 return optimizeExp2(CI, Builder);
3849 return replaceUnaryCall(CI, Builder, Intrinsic::fabs);
3853 return optimizeSqrt(CI, Builder);
3869 return optimizeLog(CI, Builder);
3885 return optimizeTrigInversionPairs(CI, Builder);
3887 return replaceUnaryCall(CI, Builder, Intrinsic::ceil);
3889 return replaceUnaryCall(CI, Builder, Intrinsic::floor);
3891 return replaceUnaryCall(CI, Builder, Intrinsic::round);
3893 return replaceUnaryCall(CI, Builder, Intrinsic::roundeven);
3895 return replaceUnaryCall(CI, Builder, Intrinsic::nearbyint);
3897 return replaceUnaryCall(CI, Builder, Intrinsic::rint);
3899 return replaceUnaryCall(CI, Builder, Intrinsic::trunc);
3911 if (UnsafeFPShrink && hasFloatVersion(M, CI->getCalledFunction()->getName()))
3912 return optimizeUnaryDoubleFP(CI, Builder, TLI, true);
3915 if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
3916 return optimizeBinaryDoubleFP(CI, Builder, TLI);
3924 return optimizeFMinFMax(CI, Builder);
3928 return optimizeCAbs(CI, Builder);
3934 Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) {
3935 Module *M = CI->getModule();
3936 assert(!CI->isMustTailCall() && "These transforms aren't musttail safe.");
3941 if (CI->isNoBuiltin())
3945 Function *Callee = CI->getCalledFunction();
3946 bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
3949 CI->getOperandBundlesAsDefs(OpBundles);
3959 else if (isa<FPMathOperator>(CI) && CI->isFast())
3963 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
3970 return optimizePow(CI, Builder);
3972 return optimizeExp2(CI, Builder);
3976 return optimizeLog(CI, Builder);
3978 return optimizeSqrt(CI, Builder);
3980 return optimizeMemSet(CI, Builder);
3982 return optimizeMemCpy(CI, Builder);
3984 return optimizeMemMove(CI, Builder);
3992 FortifiedSimplifier.optimizeCall(CI, Builder))
4000 if (Value *V = optimizeStringMemoryLibCall(CI, Builder))
4002 if (Value *V = optimizeFloatingPointLibCall(CI, Func, Builder))
4008 return optimizeFFS(CI, Builder);
4012 return optimizeFls(CI, Builder);
4016 return optimizeAbs(CI, Builder);
4018 return optimizeIsDigit(CI, Builder);
4020 return optimizeIsAscii(CI, Builder);
4022 return optimizeToAscii(CI, Builder);
4026 return optimizeAtoi(CI, Builder);
4029 return optimizeStrToInt(CI, Builder, /*AsSigned=*/true);
4032 return optimizeStrToInt(CI, Builder, /*AsSigned=*/false);
4034 return optimizePrintF(CI, Builder);
4036 return optimizeSPrintF(CI, Builder);
4038 return optimizeSnPrintF(CI, Builder);
4040 return optimizeFPrintF(CI, Builder);
4042 return optimizeFWrite(CI, Builder);
4044 return optimizeFPuts(CI, Builder);
4046 return optimizePuts(CI, Builder);
4048 return optimizeErrorReporting(CI, Builder);
4051 return optimizeErrorReporting(CI, Builder, 0);
4113 CallInst *CI, unsigned ObjSizeOp, std::optional<unsigned> SizeOp,
4118 ConstantInt *Flag = dyn_cast<ConstantInt>(CI->getArgOperand(*FlagOp));
4123 if (SizeOp && CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(*SizeOp))
4127 dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) {
4134 uint64_t Len = GetStringLength(CI->getArgOperand(*StrOp));
4138 annotateDereferenceableBytes(CI, *StrOp, Len);
4146 dyn_cast<ConstantInt>(CI->getArgOperand(*SizeOp)))
4153 Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI,
4155 if (isFortifiedCallFoldable(CI, 3, 2)) {
4157 B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
4158 Align(1), CI->getArgOperand(2));
4159 mergeAttributesAndFlags(NewCI, *CI);
4160 return CI->getArgOperand(0);
4165 Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI,
4167 if (isFortifiedCallFoldable(CI, 3, 2)) {
4169 B.CreateMemMove(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
4170 Align(1), CI->getArgOperand(2));
4171 mergeAttributesAndFlags(NewCI, *CI);
4172 return CI->getArgOperand(0);
4177 Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI,
4179 if (isFortifiedCallFoldable(CI, 3, 2)) {
4180 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
4181 CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val,
4182 CI->getArgOperand(2), Align(1));
4183 mergeAttributesAndFlags(NewCI, *CI);
4184 return CI->getArgOperand(0);
4189 Value *FortifiedLibCallSimplifier::optimizeMemPCpyChk(CallInst *CI,
4191 const DataLayout &DL = CI->getDataLayout();
4192 if (isFortifiedCallFoldable(CI, 3, 2))
4193 if (Value *Call = emitMemPCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4194 CI->getArgOperand(2), B, DL, TLI)) {
4195 return mergeAttributesAndFlags(cast<CallInst>(Call), *CI);
4200 Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
4203 const DataLayout &DL = CI->getDataLayout();
4204 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
4205 *ObjSize = CI->getArgOperand(2);
4218 if (isFortifiedCallFoldable(CI, 2, std::nullopt, 1)) {
4220 return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
4222 return copyFlags(*CI, emitStpCpy(Dst, Src, B, TLI));
4231 annotateDereferenceableBytes(CI, 1, Len);
4235 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
4236 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
4244 return copyFlags(*CI, cast<CallInst>(Ret));
4247 Value *FortifiedLibCallSimplifier::optimizeStrLenChk(CallInst *CI,
4249 if (isFortifiedCallFoldable(CI, 1, std::nullopt, 0))
4250 return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B,
4251 CI->getDataLayout(), TLI));
4255 Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
4258 if (isFortifiedCallFoldable(CI, 3, 2)) {
4260 return copyFlags(*CI,
4261 emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4262 CI->getArgOperand(2), B, TLI));
4264 return copyFlags(*CI,
4265 emitStpNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4266 CI->getArgOperand(2), B, TLI));
4272 Value *FortifiedLibCallSimplifier::optimizeMemCCpyChk(CallInst *CI,
4274 if (isFortifiedCallFoldable(CI, 4, 3))
4276 *CI, emitMemCCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4277 CI->getArgOperand(2), CI->getArgOperand(3), B, TLI));
4282 Value *FortifiedLibCallSimplifier::optimizeSNPrintfChk(CallInst *CI,
4284 if (isFortifiedCallFoldable(CI, 3, 1, std::nullopt, 2)) {
4285 SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 5));
4286 return copyFlags(*CI,
4287 emitSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1),
4288 CI->getArgOperand(4), VariadicArgs, B, TLI));
4294 Value *FortifiedLibCallSimplifier::optimizeSPrintfChk(CallInst *CI,
4296 if (isFortifiedCallFoldable(CI, 2, std::nullopt, std::nullopt, 1)) {
4297 SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 4));
4298 return copyFlags(*CI,
4299 emitSPrintf(CI->getArgOperand(0), CI->getArgOperand(3),
4306 Value *FortifiedLibCallSimplifier::optimizeStrCatChk(CallInst *CI,
4308 if (isFortifiedCallFoldable(CI, 2))
4310 *CI, emitStrCat(CI->getArgOperand(0), CI->getArgOperand(1), B, TLI));
4315 Value *FortifiedLibCallSimplifier::optimizeStrLCat(CallInst *CI,
4317 if (isFortifiedCallFoldable(CI, 3))
4318 return copyFlags(*CI,
4319 emitStrLCat(CI->getArgOperand(0), CI->getArgOperand(1),
4320 CI->getArgOperand(2), B, TLI));
4325 Value *FortifiedLibCallSimplifier::optimizeStrNCatChk(CallInst *CI,
4327 if (isFortifiedCallFoldable(CI, 3))
4328 return copyFlags(*CI,
4329 emitStrNCat(CI->getArgOperand(0), CI->getArgOperand(1),
4330 CI->getArgOperand(2), B, TLI));
4335 Value *FortifiedLibCallSimplifier::optimizeStrLCpyChk(CallInst *CI,
4337 if (isFortifiedCallFoldable(CI, 3))
4338 return copyFlags(*CI,
4339 emitStrLCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4340 CI->getArgOperand(2), B, TLI));
4345 Value *FortifiedLibCallSimplifier::optimizeVSNPrintfChk(CallInst *CI,
4347 if (isFortifiedCallFoldable(CI, 3, 1, std::nullopt, 2))
4349 *CI, emitVSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1),
4350 CI->getArgOperand(4), CI->getArgOperand(5), B, TLI));
4355 Value *FortifiedLibCallSimplifier::optimizeVSPrintfChk(CallInst *CI,
4357 if (isFortifiedCallFoldable(CI, 2, std::nullopt, std::nullopt, 1))
4358 return copyFlags(*CI,
4359 emitVSPrintf(CI->getArgOperand(0), CI->getArgOperand(3),
4360 CI->getArgOperand(4), B, TLI));
4365 Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI,
4381 Function *Callee = CI->getCalledFunction();
4382 bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
4385 CI->getOperandBundlesAsDefs(OpBundles);
4401 return optimizeMemCpyChk(CI, Builder);
4403 return optimizeMemPCpyChk(CI, Builder);
4405 return optimizeMemMoveChk(CI, Builder);
4407 return optimizeMemSetChk(CI, Builder);
4410 return optimizeStrpCpyChk(CI, Builder, Func);
4412 return optimizeStrLenChk(CI, Builder);
4415 return optimizeStrpNCpyChk(CI, Builder, Func);
4417 return optimizeMemCCpyChk(CI, Builder);
4419 return optimizeSNPrintfChk(CI, Builder);
4421 return optimizeSPrintfChk(CI, Builder);
4423 return optimizeStrCatChk(CI, Builder);
4425 return optimizeStrLCat(CI, Builder);
4427 return optimizeStrNCatChk(CI, Builder);
4429 return optimizeStrLCpyChk(CI, Builder);
4431 return optimizeVSNPrintfChk(CI, Builder);
4433 return optimizeVSPrintfChk(CI, Builder);