Go to the documentation of this file.
42 using namespace PatternMatch;
47 cl::desc(
"Enable unsafe double to float "
48 "shrinking for math lib calls"));
55 return Func == LibFunc_abs || Func == LibFunc_labs ||
56 Func == LibFunc_llabs || Func == LibFunc_strlen;
62 if (
ICmpInst *IC = dyn_cast<ICmpInst>(U))
63 if (IC->isEquality() && IC->getOperand(1) == With)
73 return OI->getType()->isFloatingPointTy();
79 return OI->getType()->isFP128Ty();
84 if (Base < 2 || Base > 36)
90 std::string nptr = Str.
str();
92 long long int Result = strtoll(nptr.c_str(), &End, Base);
110 if (
ICmpInst *IC = dyn_cast<ICmpInst>(U))
111 if (
Constant *
C = dyn_cast<Constant>(IC->getOperand(1)))
112 if (
C->isNullValue())
136 uint64_t DereferenceableBytes) {
140 for (
unsigned ArgNo : ArgNos) {
141 uint64_t DerefBytes = DereferenceableBytes;
147 DereferenceableBytes);
167 for (
unsigned ArgNo : ArgNos) {
190 uint64_t DerefMin = 1;
192 DerefMin =
std::min(
X->getZExtValue(),
Y->getZExtValue());
220 return emitStrLenMemCpy(Src, Dst, Len,
B);
223 Value *LibCallSimplifier::emitStrLenMemCpy(
Value *Src,
Value *Dst, uint64_t Len,
234 Value *CpyDst =
B.CreateGEP(
B.getInt8Ty(), Dst, DstLen,
"endptr");
284 return emitStrLenMemCpy(Src, Dst, SrcLen,
B);
316 return B.CreateGEP(
B.getInt8Ty(), SrcStr, StrLen,
"strchr");
329 return B.CreateGEP(
B.getInt8Ty(), SrcStr,
B.getInt64(
I),
"strchr");
357 return B.CreateGEP(
B.getInt8Ty(), SrcStr,
B.getInt64(
I),
"strrchr");
370 if (HasStr1 && HasStr2)
373 if (HasStr1 && Str1.
empty())
374 return B.CreateNeg(
B.CreateZExt(
375 B.CreateLoad(
B.getInt8Ty(), Str2P,
"strcmpload"), CI->
getType()));
377 if (HasStr2 && Str2.
empty())
378 return B.CreateZExt(
B.CreateLoad(
B.getInt8Ty(), Str1P,
"strcmpload"),
397 if (!HasStr1 && HasStr2) {
403 }
else if (HasStr1 && !HasStr2) {
442 if (HasStr1 && HasStr2) {
448 if (HasStr1 && Str1.
empty())
449 return B.CreateNeg(
B.CreateZExt(
450 B.CreateLoad(
B.getInt8Ty(), Str2P,
"strcmpload"), CI->
getType()));
452 if (HasStr2 && Str2.
empty())
453 return B.CreateZExt(
B.CreateLoad(
B.getInt8Ty(), Str1P,
"strcmpload"),
464 if (!HasStr1 && HasStr2) {
471 }
else if (HasStr1 && !HasStr2) {
487 if (SrcLen &&
Size) {
489 if (SrcLen <= Size->getZExtValue() + 1)
525 return StrLen ?
B.CreateInBoundsGEP(
B.getInt8Ty(), Dst, StrLen) :
nullptr;
537 Value *DstEnd =
B.CreateGEP(
B.getInt8Ty(), Dst,
587 if (Len > SrcLen + 1) {
592 std::string SrcStr = Str.
str();
593 SrcStr.resize(Len,
'\0');
594 Src =
B.CreateGlobalString(SrcStr,
"str");
632 uint64_t NullTermIdx;
633 if (Slice.
Array ==
nullptr) {
636 NullTermIdx = ~((uint64_t)0);
637 for (uint64_t
I = 0,
E = Slice.
Length;
I <
E; ++
I) {
645 if (NullTermIdx == ~((uint64_t)0))
653 cast<ArrayType>(
GEP->getSourceElementType())->getNumElements();
666 (
GEP->isInBounds() && isa<GlobalVariable>(
GEP->getOperand(0)) &&
667 NullTermIdx == ArrSize - 1)) {
679 if (LenTrue && LenFalse) {
682 <<
"folded strlen(select) to select of constants";
684 return B.CreateSelect(
SI->getCondition(),
693 return B.CreateZExt(
B.CreateLoad(
B.getIntNTy(CharSize), Src,
"strlenfirst"),
700 if (
Value *V = optimizeStringLength(CI,
B, 8))
708 unsigned WCharSize = TLI->getWCharSize(M) * 8;
713 return optimizeStringLength(CI,
B, WCharSize);
723 if ((HasS1 && S1.
empty()) || (HasS2 && S2.
empty()))
727 if (HasS1 && HasS2) {
737 if (HasS2 && S2.
size() == 1)
745 if (isa<ConstantPointerNull>(EndPtr)) {
761 if ((HasS1 && S1.
empty()) || (HasS2 && S2.
empty()))
765 if (HasS1 && HasS2) {
781 if (HasS1 && S1.
empty())
785 if (HasS1 && HasS2) {
793 if (HasS2 && S2.
empty())
818 replaceAllUsesWith(Old, Cmp);
829 if (HasStr2 && ToFindStr.
empty())
833 if (HasStr1 && HasStr2) {
842 B.CreateConstInBoundsGEP1_64(
B.getInt8Ty(), Result,
Offset,
"strstr");
843 return B.CreateBitCast(Result, CI->
getType());
847 if (HasStr2 && ToFindStr.
size() == 1) {
849 return StrChr ?
B.CreateBitCast(StrChr, CI->
getType()) :
nullptr;
899 *std::max_element(
reinterpret_cast<const unsigned char *
>(Str.
begin()),
900 reinterpret_cast<const unsigned char *
>(Str.
end()));
907 if (!
DL.fitsInLegalInteger(Max + 1))
922 C =
B.CreateAnd(
C,
B.getIntN(
Width, 0xFF));
930 Value *
Bits =
B.CreateIsNotNull(
B.CreateAnd(Shl, BitfieldC),
"memchr.bits");
934 return B.CreateIntToPtr(
B.CreateAnd(Bounds,
Bits,
"memchr"), CI->
getType());
947 return B.CreateGEP(
B.getInt8Ty(), SrcStr,
B.getInt64(
I),
"memchr");
959 B.CreateZExt(
B.CreateLoad(
B.getInt8Ty(),
castToCStr(LHS,
B),
"lhsc"),
962 B.CreateZExt(
B.CreateLoad(
B.getInt8Ty(),
castToCStr(RHS,
B),
"rhsc"),
964 return B.CreateSub(LHSV, RHSV,
"chardiff");
972 unsigned PrefAlignment =
DL.getPrefTypeAlignment(IntType);
975 Value *LHSV =
nullptr;
976 if (
auto *LHSC = dyn_cast<Constant>(LHS)) {
980 Value *RHSV =
nullptr;
981 if (
auto *RHSC = dyn_cast<Constant>(RHS)) {
993 LHSV =
B.CreateLoad(IntType,
B.CreateBitCast(LHS, LHSPtrTy),
"lhsv");
998 RHSV =
B.CreateLoad(IntType,
B.CreateBitCast(RHS, RHSPtrTy),
"rhsv");
1000 return B.CreateZExt(
B.CreateICmpNE(LHSV, RHSV), CI->
getType(),
"memcmp");
1010 if (Len > LHSStr.
size() || Len > RHSStr.
size())
1027 Value *LibCallSimplifier::optimizeMemCmpBCmpCommon(
CallInst *CI,
1042 if (LenC->getZExtValue() == 0)
1052 if (
Value *V = optimizeMemCmpBCmpCommon(CI,
B))
1069 return optimizeMemCmpBCmpCommon(CI,
B);
1075 if (isa<IntrinsicInst>(CI))
1097 if (
N->isNullValue())
1110 if (
N->getZExtValue() <= SrcStr.
size()) {
1120 B.CreateMemCpy(Dst,
Align(1), Src,
Align(1), NewN);
1121 return Pos + 1 <=
N->getZExtValue()
1122 ?
B.CreateInBoundsGEP(
B.getInt8Ty(), Dst, NewN)
1138 return B.CreateInBoundsGEP(
B.getInt8Ty(), Dst,
N);
1144 if (isa<IntrinsicInst>(CI))
1159 auto *FillValue = dyn_cast<ConstantInt>(Memset->
getArgOperand(1));
1160 if (!FillValue || FillValue->getZExtValue() != 0)
1167 auto *Malloc = dyn_cast<CallInst>(Memset->
getArgOperand(0));
1168 if (!Malloc || !Malloc->hasOneUse())
1172 Function *InnerCallee = Malloc->getCalledFunction();
1177 if (!TLI->getLibFunc(*InnerCallee, Func) || !TLI->has(Func) ||
1178 Func != LibFunc_malloc)
1187 B.SetInsertPoint(Malloc->getParent(), ++Malloc->getIterator());
1188 const DataLayout &
DL = Malloc->getModule()->getDataLayout();
1189 IntegerType *SizeType =
DL.getIntPtrType(
B.GetInsertBlock()->getContext());
1191 Malloc->getArgOperand(0),
1192 Malloc->getAttributes(),
B, *TLI)) {
1193 substituteInParent(Malloc, Calloc);
1203 if (isa<IntrinsicInst>(CI))
1206 if (
auto *Calloc = foldMallocMemset(CI,
B))
1250 if (
FPExtInst *Cast = dyn_cast<FPExtInst>(Val)) {
1251 Value *
Op = Cast->getOperand(0);
1252 if (
Op->getType()->isFloatTy())
1255 if (
ConstantFP *Const = dyn_cast<ConstantFP>(Val)) {
1268 bool isBinary,
bool isPrecise =
false) {
1299 if (!CallerName.
empty() && CallerName.
back() ==
'f' &&
1300 CallerName.
size() == (CalleeName.
size() + 1) &&
1315 R =
isBinary ?
B.CreateCall(Fn, V) :
B.CreateCall(Fn, V[0]);
1321 return B.CreateFPExt(R,
B.getDoubleTy());
1326 bool isPrecise =
false) {
1332 bool isPrecise =
false) {
1348 assert(
Op->getType()->isArrayTy() &&
"Unexpected signature for cabs!");
1349 Real =
B.CreateExtractValue(
Op, 0,
"real");
1350 Imag =
B.CreateExtractValue(
Op, 1,
"imag");
1357 Value *RealReal =
B.CreateFMul(Real, Real);
1358 Value *ImagImag =
B.CreateFMul(Imag, Imag);
1362 return B.CreateCall(FSqrt,
B.CreateFAdd(RealReal, ImagImag),
"cabs");
1367 if (!isa<FPMathOperator>(Call))
1371 B.setFastMathFlags(Call->getFastMathFlags());
1385 return B.CreateFNeg(
B.CreateCall(Call->getCalledFunction(),
X));
1392 return B.CreateCall(Call->getCalledFunction(),
X,
"cos");
1404 assert(Exp != 0 &&
"Incorrect exponent 0 not handled");
1406 if (InnerChain[Exp])
1407 return InnerChain[Exp];
1409 static const unsigned AddChain[33][2] = {
1413 {1, 2}, {2, 2}, {2, 3}, {3, 3}, {2, 5}, {4, 4},
1414 {1, 8}, {5, 5}, {1, 10}, {6, 6}, {4, 9}, {7, 7},
1415 {3, 12}, {8, 8}, {8, 9}, {2, 16}, {1, 18}, {10, 10},
1416 {6, 15}, {11, 11}, {3, 20}, {12, 12}, {8, 17}, {13, 13},
1417 {3, 24}, {14, 14}, {4, 25}, {15, 15}, {3, 28}, {16, 16},
1420 InnerChain[Exp] =
B.CreateFMul(
getPow(InnerChain, AddChain[Exp][0],
B),
1421 getPow(InnerChain, AddChain[Exp][1],
B));
1422 return InnerChain[Exp];
1427 if (isa<SIToFPInst>(I2F) || isa<UIToFPInst>(I2F)) {
1428 Value *
Op = cast<Instruction>(I2F)->getOperand(0);
1431 unsigned BitWidth =
Op->getType()->getPrimitiveSizeInBits();
1433 (
BitWidth == 32 && isa<SIToFPInst>(I2F)))
1434 return isa<SIToFPInst>(I2F) ?
B.CreateSExt(
Op,
B.getInt32Ty())
1435 :
B.CreateZExt(
Op,
B.getInt32Ty());
1466 CallInst *BaseFn = dyn_cast<CallInst>(Base);
1472 TLI->getLibFunc(CalleeFn->
getName(), LibFn) && TLI->has(LibFn)) {
1476 LibFunc LibFnFloat, LibFnDouble, LibFnLongDouble;
1481 case LibFunc_expf:
case LibFunc_exp:
case LibFunc_expl:
1482 ExpName = TLI->getName(LibFunc_exp);
1483 ID = Intrinsic::exp;
1484 LibFnFloat = LibFunc_expf;
1485 LibFnDouble = LibFunc_exp;
1486 LibFnLongDouble = LibFunc_expl;
1488 case LibFunc_exp2f:
case LibFunc_exp2:
case LibFunc_exp2l:
1489 ExpName = TLI->getName(LibFunc_exp2);
1490 ID = Intrinsic::exp2;
1491 LibFnFloat = LibFunc_exp2f;
1492 LibFnDouble = LibFunc_exp2;
1493 LibFnLongDouble = LibFunc_exp2l;
1510 substituteInParent(BaseFn, ExpFn);
1523 (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo)) &&
1524 hasFloatFn(TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl)) {
1527 LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl,
1532 if (
hasFloatFn(TLI, Ty, LibFunc_exp2, LibFunc_exp2f, LibFunc_exp2l)) {
1535 BaseR = BaseR / *BaseF;
1537 const APFloat *NF = IsReciprocal ? &BaseR : BaseF;
1539 if ((IsInteger || IsReciprocal) &&
1542 NI > 1 && NI.isPowerOf2()) {
1543 double N = NI.logBase2() * (IsReciprocal ? -1.0 : 1.0);
1550 LibFunc_exp2l,
B,
Attrs);
1557 hasFloatFn(TLI, Ty, LibFunc_exp10, LibFunc_exp10f, LibFunc_exp10l))
1559 LibFunc_exp10l,
B,
Attrs);
1567 "pow(1.0, y) should have been simplified earlier!");
1569 Value *Log =
nullptr;
1576 Value *
FMul =
B.CreateFMul(Log, Expo,
"mul");
1580 else if (
hasFloatFn(TLI, Ty, LibFunc_exp2, LibFunc_exp2f, LibFunc_exp2l))
1582 LibFunc_exp2l,
B,
Attrs);
1596 return B.CreateCall(SqrtFn, V,
"sqrt");
1605 LibFunc_sqrtl,
B,
Attrs);
1642 Sqrt =
B.CreateCall(FAbsFn, Sqrt,
"abs");
1650 Value *FCmp =
B.CreateFCmpOEQ(Base, NegInf,
"isinf");
1651 Sqrt =
B.CreateSelect(FCmp, PosInf, Sqrt);
1665 return B.CreateCall(
F,
Args);
1687 if (
Value *Exp = replacePowWithExp(Pow,
B))
1706 return B.CreateFMul(Base, Base,
"square");
1708 if (
Value *Sqrt = replacePowWithSqrt(Pow,
B))
1726 Value *Sqrt =
nullptr;
1727 if (!ExpoA.isInteger()) {
1745 Value *InnerChain[33] = {
nullptr};
1746 InnerChain[1] = Base;
1747 InnerChain[2] =
B.CreateFMul(Base, Base,
"square");
1756 FMul =
B.CreateFMul(FMul, Sqrt);
1765 APSInt IntExpo(32,
false);
1776 if (AllowApprox && (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo))) {
1783 if (UnsafeFPShrink &&
Name == TLI->getName(LibFunc_pow) &&
1784 hasFloatVersion(
Name)) {
1797 if (UnsafeFPShrink &&
Name == TLI->getName(LibFunc_exp2) &&
1798 hasFloatVersion(
Name))
1806 if ((isa<SIToFPInst>(
Op) || isa<UIToFPInst>(
Op)) &&
1807 hasFloatFn(TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl)) {
1810 LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl,
1822 if ((
Name ==
"fmin" ||
Name ==
"fmax") && hasFloatVersion(
Name))
1836 B.setFastMathFlags(FMF);
1853 if (UnsafeFPShrink && hasFloatVersion(LogNm))
1861 LibFunc LogLb, ExpLb, Exp2Lb, Exp10Lb, PowLb;
1864 if (TLI->getLibFunc(LogNm, LogLb))
1867 LogID = Intrinsic::log;
1868 ExpLb = LibFunc_expf;
1869 Exp2Lb = LibFunc_exp2f;
1870 Exp10Lb = LibFunc_exp10f;
1871 PowLb = LibFunc_powf;
1874 LogID = Intrinsic::log;
1875 ExpLb = LibFunc_exp;
1876 Exp2Lb = LibFunc_exp2;
1877 Exp10Lb = LibFunc_exp10;
1878 PowLb = LibFunc_pow;
1881 LogID = Intrinsic::log;
1882 ExpLb = LibFunc_expl;
1883 Exp2Lb = LibFunc_exp2l;
1884 Exp10Lb = LibFunc_exp10l;
1885 PowLb = LibFunc_powl;
1889 ExpLb = LibFunc_expf;
1890 Exp2Lb = LibFunc_exp2f;
1891 Exp10Lb = LibFunc_exp10f;
1892 PowLb = LibFunc_powf;
1896 ExpLb = LibFunc_exp;
1897 Exp2Lb = LibFunc_exp2;
1898 Exp10Lb = LibFunc_exp10;
1899 PowLb = LibFunc_pow;
1903 ExpLb = LibFunc_expl;
1904 Exp2Lb = LibFunc_exp2l;
1905 Exp10Lb = LibFunc_exp10l;
1906 PowLb = LibFunc_powl;
1908 case LibFunc_log10f:
1909 LogID = Intrinsic::log10;
1910 ExpLb = LibFunc_expf;
1911 Exp2Lb = LibFunc_exp2f;
1912 Exp10Lb = LibFunc_exp10f;
1913 PowLb = LibFunc_powf;
1916 LogID = Intrinsic::log10;
1917 ExpLb = LibFunc_exp;
1918 Exp2Lb = LibFunc_exp2;
1919 Exp10Lb = LibFunc_exp10;
1920 PowLb = LibFunc_pow;
1922 case LibFunc_log10l:
1923 LogID = Intrinsic::log10;
1924 ExpLb = LibFunc_expl;
1925 Exp2Lb = LibFunc_exp2l;
1926 Exp10Lb = LibFunc_exp10l;
1927 PowLb = LibFunc_powl;
1933 LogID == Intrinsic::log10) {
1935 ExpLb = LibFunc_expf;
1936 Exp2Lb = LibFunc_exp2f;
1937 Exp10Lb = LibFunc_exp10f;
1938 PowLb = LibFunc_powf;
1940 ExpLb = LibFunc_exp;
1941 Exp2Lb = LibFunc_exp2;
1942 Exp10Lb = LibFunc_exp10;
1943 PowLb = LibFunc_pow;
1954 TLI->getLibFunc(*
Arg, ArgLb);
1957 if (ArgLb == PowLb || ArgID == Intrinsic::pow) {
1961 Arg->getOperand(0),
"log")
1963 Value *MulY =
B.CreateFMul(
Arg->getArgOperand(1), LogX,
"mul");
1966 substituteInParent(
Arg, MulY);
1972 if (ArgLb == ExpLb || ArgLb == Exp2Lb || ArgLb == Exp10Lb ||
1973 ArgID == Intrinsic::exp || ArgID == Intrinsic::exp2) {
1975 if (ArgLb == ExpLb || ArgID == Intrinsic::exp)
1978 else if (ArgLb == Exp2Lb || ArgID == Intrinsic::exp2)
1986 Value *MulY =
B.CreateFMul(
Arg->getArgOperand(0), LogE,
"mul");
1989 substituteInParent(
Arg, MulY);
2002 if (TLI->has(LibFunc_sqrtf) && (
Callee->getName() ==
"sqrt" ||
2003 Callee->getIntrinsicID() == Intrinsic::sqrt))
2010 if (!
I ||
I->getOpcode() != Instruction::FMul || !
I->isFast())
2016 Value *Op0 =
I->getOperand(0);
2017 Value *Op1 =
I->getOperand(1);
2018 Value *RepeatOp =
nullptr;
2019 Value *OtherOp =
nullptr;
2029 Value *OtherMul0, *OtherMul1;
2032 if (OtherMul0 == OtherMul1 && cast<Instruction>(Op0)->isFast()) {
2034 RepeatOp = OtherMul0;
2045 B.setFastMathFlags(
I->getFastMathFlags());
2050 Type *ArgType =
I->getType();
2052 Value *FabsCall =
B.CreateCall(Fabs, RepeatOp,
"fabs");
2058 Value *SqrtCall =
B.CreateCall(Sqrt, OtherOp,
"sqrt");
2059 return B.CreateFMul(FabsCall, SqrtCall);
2069 if (UnsafeFPShrink &&
Name ==
"tan" && hasFloatVersion(
Name))
2073 auto *OpC = dyn_cast<CallInst>(Op1);
2078 if (!CI->
isFast() || !OpC->isFast())
2086 if (
F && TLI->getLibFunc(
F->getName(), Func) && TLI->has(Func) &&
2087 ((Func == LibFunc_atan &&
Callee->getName() ==
"tan") ||
2088 (Func == LibFunc_atanf &&
Callee->getName() ==
"tanf") ||
2089 (Func == LibFunc_atanl &&
Callee->getName() ==
"tanl")))
2090 Ret = OpC->getArgOperand(0);
2098 return CI->
hasFnAttr(Attribute::NoUnwind) &&
2111 Name =
"__sincospif_stret";
2120 Name =
"__sincospi_stret";
2131 B.SetInsertPoint(ArgInst->getParent(), ++ArgInst->getIterator());
2135 BasicBlock &EntryBB =
B.GetInsertBlock()->getParent()->getEntryBlock();
2136 B.SetInsertPoint(&EntryBB, EntryBB.
begin());
2139 SinCos =
B.CreateCall(
Callee,
Arg,
"sincospi");
2142 Sin =
B.CreateExtractValue(SinCos, 0,
"sinpi");
2143 Cos =
B.CreateExtractValue(SinCos, 1,
"cospi");
2163 bool IsFloat =
Arg->getType()->isFloatTy();
2170 classifyArgUse(U,
F, IsFloat, SinCalls, CosCalls, SinCosCalls);
2173 if (SinCalls.empty() || CosCalls.empty())
2176 Value *Sin, *Cos, *SinCos;
2182 replaceAllUsesWith(
C, Res);
2185 replaceTrigInsts(SinCalls, Sin);
2186 replaceTrigInsts(CosCalls, Cos);
2187 replaceTrigInsts(SinCosCalls, SinCos);
2192 void LibCallSimplifier::classifyArgUse(
2197 CallInst *CI = dyn_cast<CallInst>(Val);
2208 if (!Callee || !TLI->getLibFunc(*Callee, Func) || !TLI->has(Func) ||
2213 if (Func == LibFunc_sinpif)
2214 SinCalls.push_back(CI);
2215 else if (Func == LibFunc_cospif)
2216 CosCalls.push_back(CI);
2217 else if (Func == LibFunc_sincospif_stret)
2218 SinCosCalls.push_back(CI);
2220 if (Func == LibFunc_sinpi)
2221 SinCalls.push_back(CI);
2222 else if (Func == LibFunc_cospi)
2223 CosCalls.push_back(CI);
2224 else if (Func == LibFunc_sincospi_stret)
2225 SinCosCalls.push_back(CI);
2236 Type *ArgType =
Op->getType();
2238 Intrinsic::cttz, ArgType);
2239 Value *V =
B.CreateCall(
F, {
Op,
B.getTrue()},
"cttz");
2241 V =
B.CreateIntCast(V,
B.getInt32Ty(),
false);
2244 return B.CreateSelect(
Cond, V,
B.getInt32(0));
2250 Type *ArgType =
Op->getType();
2252 Intrinsic::ctlz, ArgType);
2253 Value *V =
B.CreateCall(
F, {
Op,
B.getFalse()},
"ctlz");
2256 return B.CreateIntCast(V, CI->
getType(),
false);
2264 Value *NegX =
B.CreateNSWNeg(
X,
"neg");
2265 return B.CreateSelect(IsNeg, NegX,
X);
2271 Op =
B.CreateSub(
Op,
B.getInt32(
'0'),
"isdigittmp");
2272 Op =
B.CreateICmpULT(
Op,
B.getInt32(10),
"isdigit");
2279 Op =
B.CreateICmpULT(
Op,
B.getInt32(128),
"isascii");
2355 return GV->
getName() ==
"stderr";
2365 if (FormatStr.
empty())
2375 if (FormatStr.
size() == 1 || FormatStr ==
"%%")
2383 if (ChrStr.
size() != 1)
2389 if (FormatStr[FormatStr.
size() - 1] ==
'\n' &&
2394 Value *GV =
B.CreateGlobalString(FormatStr,
"str");
2415 if (
Value *V = optimizePrintFString(CI,
B)) {
2422 Module *
M =
B.GetInsertBlock()->getParent()->getParent();
2424 M->getOrInsertFunction(
"iprintf", FT,
Callee->getAttributes());
2426 New->setCalledFunction(IPrintFFn);
2434 Module *
M =
B.GetInsertBlock()->getParent()->getParent();
2435 auto SmallPrintFFn =
2436 M->getOrInsertFunction(TLI->getName(LibFunc_small_printf),
2437 FT,
Callee->getAttributes());
2439 New->setCalledFunction(SmallPrintFFn);
2448 Value *LibCallSimplifier::optimizeSPrintFString(
CallInst *CI,
2466 FormatStr.
size() + 1));
2472 if (FormatStr.
size() != 2 || FormatStr[0] !=
'%' ||
2477 if (FormatStr[1] ==
'c') {
2483 B.CreateStore(V, Ptr);
2484 Ptr =
B.CreateGEP(
B.getInt8Ty(), Ptr,
B.getInt32(1),
"nul");
2485 B.CreateStore(
B.getInt8(0), Ptr);
2490 if (FormatStr[1] ==
's') {
2511 return B.CreateIntCast(PtrDiff, CI->
getType(),
false);
2529 return B.CreateIntCast(Len, CI->
getType(),
false);
2537 if (
Value *V = optimizeSPrintFString(CI,
B)) {
2544 Module *
M =
B.GetInsertBlock()->getParent()->getParent();
2546 M->getOrInsertFunction(
"siprintf", FT,
Callee->getAttributes());
2548 New->setCalledFunction(SIPrintFFn);
2556 Module *
M =
B.GetInsertBlock()->getParent()->getParent();
2557 auto SmallSPrintFFn =
2558 M->getOrInsertFunction(TLI->getName(LibFunc_small_sprintf),
2559 FT,
Callee->getAttributes());
2561 New->setCalledFunction(SmallSPrintFFn);
2570 Value *LibCallSimplifier::optimizeSnPrintFString(
CallInst *CI,
2577 uint64_t
N =
Size->getZExtValue();
2592 else if (
N < FormatStr.
size() + 1)
2600 FormatStr.
size() + 1));
2606 if (FormatStr.
size() == 2 && FormatStr[0] ==
'%' &&
2610 if (FormatStr[1] ==
'c') {
2621 B.CreateStore(V, Ptr);
2622 Ptr =
B.CreateGEP(
B.getInt8Ty(), Ptr,
B.getInt32(1),
"nul");
2623 B.CreateStore(
B.getInt8(0), Ptr);
2628 if (FormatStr[1] ==
's') {
2636 else if (
N < Str.
size() + 1)
2650 if (
Value *V = optimizeSnPrintFString(CI,
B)) {
2659 Value *LibCallSimplifier::optimizeFPrintFString(
CallInst *CI,
2661 optimizeErrorReporting(CI,
B, 0);
2688 if (FormatStr.
size() != 2 || FormatStr[0] !=
'%' ||
2693 if (FormatStr[1] ==
'c') {
2700 if (FormatStr[1] ==
's') {
2712 if (
Value *V = optimizeFPrintFString(CI,
B)) {
2719 Module *
M =
B.GetInsertBlock()->getParent()->getParent();
2721 M->getOrInsertFunction(
"fiprintf", FT,
Callee->getAttributes());
2723 New->setCalledFunction(FIPrintFFn);
2731 Module *
M =
B.GetInsertBlock()->getParent()->getParent();
2732 auto SmallFPrintFFn =
2733 M->getOrInsertFunction(TLI->getName(LibFunc_small_fprintf),
2734 FT,
Callee->getAttributes());
2736 New->setCalledFunction(SmallFPrintFFn);
2745 optimizeErrorReporting(CI,
B, 3);
2750 if (SizeC && CountC) {
2771 optimizeErrorReporting(CI,
B, 1);
2817 bool LibCallSimplifier::hasFloatVersion(
StringRef FuncName) {
2820 FloatFuncName +=
'f';
2821 if (TLI->getLibFunc(FloatFuncName, Func))
2822 return TLI->has(Func);
2826 Value *LibCallSimplifier::optimizeStringMemoryLibCall(
CallInst *CI,
2831 if (TLI->getLibFunc(*Callee, Func) && TLI->has(Func)) {
2836 "Optimizing string/memory libcall would change the calling convention");
2838 case LibFunc_strcat:
2839 return optimizeStrCat(CI,
Builder);
2840 case LibFunc_strncat:
2841 return optimizeStrNCat(CI,
Builder);
2842 case LibFunc_strchr:
2843 return optimizeStrChr(CI,
Builder);
2844 case LibFunc_strrchr:
2845 return optimizeStrRChr(CI,
Builder);
2846 case LibFunc_strcmp:
2847 return optimizeStrCmp(CI,
Builder);
2848 case LibFunc_strncmp:
2849 return optimizeStrNCmp(CI,
Builder);
2850 case LibFunc_strcpy:
2851 return optimizeStrCpy(CI,
Builder);
2852 case LibFunc_stpcpy:
2853 return optimizeStpCpy(CI,
Builder);
2854 case LibFunc_strncpy:
2855 return optimizeStrNCpy(CI,
Builder);
2856 case LibFunc_strlen:
2857 return optimizeStrLen(CI,
Builder);
2858 case LibFunc_strpbrk:
2859 return optimizeStrPBrk(CI,
Builder);
2860 case LibFunc_strndup:
2861 return optimizeStrNDup(CI,
Builder);
2862 case LibFunc_strtol:
2863 case LibFunc_strtod:
2864 case LibFunc_strtof:
2865 case LibFunc_strtoul:
2866 case LibFunc_strtoll:
2867 case LibFunc_strtold:
2868 case LibFunc_strtoull:
2869 return optimizeStrTo(CI,
Builder);
2870 case LibFunc_strspn:
2871 return optimizeStrSpn(CI,
Builder);
2872 case LibFunc_strcspn:
2873 return optimizeStrCSpn(CI,
Builder);
2874 case LibFunc_strstr:
2875 return optimizeStrStr(CI,
Builder);
2876 case LibFunc_memchr:
2877 return optimizeMemChr(CI,
Builder);
2878 case LibFunc_memrchr:
2879 return optimizeMemRChr(CI,
Builder);
2881 return optimizeBCmp(CI,
Builder);
2882 case LibFunc_memcmp:
2883 return optimizeMemCmp(CI,
Builder);
2884 case LibFunc_memcpy:
2885 return optimizeMemCpy(CI,
Builder);
2886 case LibFunc_memccpy:
2887 return optimizeMemCCpy(CI,
Builder);
2888 case LibFunc_mempcpy:
2889 return optimizeMemPCpy(CI,
Builder);
2890 case LibFunc_memmove:
2891 return optimizeMemMove(CI,
Builder);
2892 case LibFunc_memset:
2893 return optimizeMemSet(CI,
Builder);
2894 case LibFunc_realloc:
2895 return optimizeRealloc(CI,
Builder);
2896 case LibFunc_wcslen:
2897 return optimizeWcslen(CI,
Builder);
2899 return optimizeBCopy(CI,
Builder);
2907 Value *LibCallSimplifier::optimizeFloatingPointLibCall(
CallInst *CI,
2918 case LibFunc_sinpif:
2920 case LibFunc_cospif:
2922 return optimizeSinCosPi(CI,
Builder);
2926 return optimizePow(CI,
Builder);
2930 return optimizeExp2(CI,
Builder);
2938 return optimizeSqrt(CI,
Builder);
2942 case LibFunc_log10f:
2944 case LibFunc_log10l:
2945 case LibFunc_log1pf:
2947 case LibFunc_log1pl:
2954 return optimizeLog(CI,
Builder);
2958 return optimizeTan(CI,
Builder);
2965 case LibFunc_roundeven:
2967 case LibFunc_nearbyint:
2991 case LibFunc_copysign:
3001 return optimizeFMinFMax(CI,
Builder);
3005 return optimizeCAbs(CI,
Builder);
3026 Builder.setDefaultOperandBundles(OpBundles);
3033 else if (isa<FPMathOperator>(CI) && CI->
isFast())
3034 UnsafeFPShrink =
true;
3038 if (!IsCallingConvC)
3042 switch (II->getIntrinsicID()) {
3043 case Intrinsic::pow:
3044 return optimizePow(CI,
Builder);
3045 case Intrinsic::exp2:
3046 return optimizeExp2(CI,
Builder);
3047 case Intrinsic::log:
3049 case Intrinsic::log10:
3050 return optimizeLog(CI,
Builder);
3051 case Intrinsic::sqrt:
3052 return optimizeSqrt(CI,
Builder);
3054 case Intrinsic::memset:
3055 return optimizeMemSet(CI,
Builder);
3057 return optimizeMemCpy(CI,
Builder);
3058 case Intrinsic::memmove:
3059 return optimizeMemMove(CI,
Builder);
3066 if (
Value *SimplifiedFortifiedCI =
3067 FortifiedSimplifier.optimizeCall(CI,
Builder)) {
3069 CallInst *SimplifiedCI = dyn_cast<CallInst>(SimplifiedFortifiedCI);
3073 replaceAllUsesWith(CI, SimplifiedCI);
3078 Builder.SetInsertPoint(SimplifiedCI);
3079 if (
Value *V = optimizeStringMemoryLibCall(SimplifiedCI,
Builder)) {
3081 substituteInParent(SimplifiedCI, V);
3085 return SimplifiedFortifiedCI;
3089 if (TLI->getLibFunc(*
Callee, Func) && TLI->has(Func)) {
3093 if (
Value *V = optimizeStringMemoryLibCall(CI,
Builder))
3095 if (
Value *V = optimizeFloatingPointLibCall(CI, Func,
Builder))
3101 return optimizeFFS(CI,
Builder);
3105 return optimizeFls(CI,
Builder);
3109 return optimizeAbs(CI,
Builder);
3110 case LibFunc_isdigit:
3111 return optimizeIsDigit(CI,
Builder);
3112 case LibFunc_isascii:
3113 return optimizeIsAscii(CI,
Builder);
3114 case LibFunc_toascii:
3115 return optimizeToAscii(CI,
Builder);
3119 return optimizeAtoi(CI,
Builder);
3120 case LibFunc_strtol:
3121 case LibFunc_strtoll:
3122 return optimizeStrtol(CI,
Builder);
3123 case LibFunc_printf:
3124 return optimizePrintF(CI,
Builder);
3125 case LibFunc_sprintf:
3126 return optimizeSPrintF(CI,
Builder);
3127 case LibFunc_snprintf:
3128 return optimizeSnPrintF(CI,
Builder);
3129 case LibFunc_fprintf:
3130 return optimizeFPrintF(CI,
Builder);
3131 case LibFunc_fwrite:
3132 return optimizeFWrite(CI,
Builder);
3134 return optimizeFPuts(CI,
Builder);
3136 return optimizePuts(CI,
Builder);
3137 case LibFunc_perror:
3138 return optimizeErrorReporting(CI,
Builder);
3139 case LibFunc_vfprintf:
3140 case LibFunc_fiprintf:
3141 return optimizeErrorReporting(CI,
Builder, 0);
3155 : FortifiedSimplifier(TLI),
DL(
DL), TLI(TLI), ORE(ORE),
BFI(
BFI), PSI(PSI),
3156 UnsafeFPShrink(
false), Replacer(Replacer), Eraser(Eraser) {}
3163 void LibCallSimplifier::eraseFromParent(
Instruction *
I) {
3203 FortifiedLibCallSimplifier::isFortifiedCallFoldable(
CallInst *CI,
3221 if (ObjSizeCI->isMinusOne())
3224 if (OnlyLowerUnknownSize)
3234 return ObjSizeCI->getZExtValue() >= Len;
3240 return ObjSizeCI->getZExtValue() >= SizeCI->getZExtValue();
3246 Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(
CallInst *CI,
3248 if (isFortifiedCallFoldable(CI, 3, 2)) {
3260 Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(
CallInst *CI,
3262 if (isFortifiedCallFoldable(CI, 3, 2)) {
3274 Value *FortifiedLibCallSimplifier::optimizeMemSetChk(
CallInst *CI,
3278 if (isFortifiedCallFoldable(CI, 3, 2)) {
3290 Value *FortifiedLibCallSimplifier::optimizeMemPCpyChk(
CallInst *CI,
3293 if (isFortifiedCallFoldable(CI, 3, 2))
3296 CallInst *NewCI = cast<CallInst>(Call);
3306 Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(
CallInst *CI,
3314 if (Func == LibFunc_stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) {
3316 return StrLen ?
B.CreateInBoundsGEP(
B.getInt8Ty(), Dst, StrLen) :
nullptr;
3324 if (isFortifiedCallFoldable(CI, 2,
None, 1)) {
3325 if (Func == LibFunc_strcpy_chk)
3331 if (OnlyLowerUnknownSize)
3346 if (
Ret && Func == LibFunc_stpcpy_chk)
3351 Value *FortifiedLibCallSimplifier::optimizeStrLenChk(
CallInst *CI,
3353 if (isFortifiedCallFoldable(CI, 1,
None, 0))
3359 Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(
CallInst *CI,
3362 if (isFortifiedCallFoldable(CI, 3, 2)) {
3363 if (Func == LibFunc_strncpy_chk)
3374 Value *FortifiedLibCallSimplifier::optimizeMemCCpyChk(
CallInst *CI,
3376 if (isFortifiedCallFoldable(CI, 4, 3))
3383 Value *FortifiedLibCallSimplifier::optimizeSNPrintfChk(
CallInst *CI,
3385 if (isFortifiedCallFoldable(CI, 3, 1,
None, 2)) {
3394 Value *FortifiedLibCallSimplifier::optimizeSPrintfChk(
CallInst *CI,
3396 if (isFortifiedCallFoldable(CI, 2,
None,
None, 1)) {
3405 Value *FortifiedLibCallSimplifier::optimizeStrCatChk(
CallInst *CI,
3407 if (isFortifiedCallFoldable(CI, 2))
3413 Value *FortifiedLibCallSimplifier::optimizeStrLCat(
CallInst *CI,
3415 if (isFortifiedCallFoldable(CI, 3))
3422 Value *FortifiedLibCallSimplifier::optimizeStrNCatChk(
CallInst *CI,
3424 if (isFortifiedCallFoldable(CI, 3))
3431 Value *FortifiedLibCallSimplifier::optimizeStrLCpyChk(
CallInst *CI,
3433 if (isFortifiedCallFoldable(CI, 3))
3440 Value *FortifiedLibCallSimplifier::optimizeVSNPrintfChk(
CallInst *CI,
3442 if (isFortifiedCallFoldable(CI, 3, 1,
None, 2))
3449 Value *FortifiedLibCallSimplifier::optimizeVSPrintfChk(
CallInst *CI,
3451 if (isFortifiedCallFoldable(CI, 2,
None,
None, 1))
3481 Builder.setDefaultOperandBundles(OpBundles);
3493 case LibFunc_memcpy_chk:
3494 return optimizeMemCpyChk(CI,
Builder);
3495 case LibFunc_mempcpy_chk:
3496 return optimizeMemPCpyChk(CI,
Builder);
3497 case LibFunc_memmove_chk:
3498 return optimizeMemMoveChk(CI,
Builder);
3499 case LibFunc_memset_chk:
3500 return optimizeMemSetChk(CI,
Builder);
3501 case LibFunc_stpcpy_chk:
3502 case LibFunc_strcpy_chk:
3503 return optimizeStrpCpyChk(CI,
Builder, Func);
3504 case LibFunc_strlen_chk:
3505 return optimizeStrLenChk(CI,
Builder);
3506 case LibFunc_stpncpy_chk:
3507 case LibFunc_strncpy_chk:
3508 return optimizeStrpNCpyChk(CI,
Builder, Func);
3509 case LibFunc_memccpy_chk:
3510 return optimizeMemCCpyChk(CI,
Builder);
3511 case LibFunc_snprintf_chk:
3512 return optimizeSNPrintfChk(CI,
Builder);
3513 case LibFunc_sprintf_chk:
3514 return optimizeSPrintfChk(CI,
Builder);
3515 case LibFunc_strcat_chk:
3516 return optimizeStrCatChk(CI,
Builder);
3517 case LibFunc_strlcat_chk:
3518 return optimizeStrLCat(CI,
Builder);
3519 case LibFunc_strncat_chk:
3520 return optimizeStrNCatChk(CI,
Builder);
3521 case LibFunc_strlcpy_chk:
3522 return optimizeStrLCpyChk(CI,
Builder);
3523 case LibFunc_vsnprintf_chk:
3524 return optimizeVSNPrintfChk(CI,
Builder);
3525 case LibFunc_vsprintf_chk:
3526 return optimizeVSPrintfChk(CI,
Builder);
3535 : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}
uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
LLVM_NODISCARD char back() const
back - Get the last character in the string.
float convertToFloat() const
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
static FastMathFlags getFast()
void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
A parsed version of the target data layout string in and methods for querying it.
const ConstantDataArray * Array
ConstantDataArray pointer.
bool hasOneUse() const
Return true if there is exactly one use of this value.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
bool isPointerTy() const
True if this is an instance of PointerType.
We have fiadd patterns now but the followings have the same cost and complexity We need a way to specify the later is more profitable def def The FP stackifier should handle simple permutates to reduce number of shuffle e g ceil
opStatus add(const APFloat &RHS, roundingMode RM)
static const fltSemantics & IEEEsingle() LLVM_READNONE
static Value * optimizeMemCmpConstantSize(CallInst *CI, Value *LHS, Value *RHS, uint64_t Len, IRBuilderBase &B, const DataLayout &DL)
void setNoSignedZeros(bool B=true)
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg)
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
static constexpr size_t npos
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Value * emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memcmp function.
Value * emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the fwrite function.
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, MaybeAlign Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
uint64_t getElementAsInteger(unsigned i) const
If this is a sequential container of integers (of any size), return the specified element in the low ...
uint64_t getDereferenceableOrNullBytes(unsigned i) const
Extract the number of dereferenceable_or_null bytes for a call or parameter (0=unknown).
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static Value * createPowWithIntegerExponent(Value *Base, Value *Expo, Module *M, IRBuilderBase &B)
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Value * emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the memccpy function.
Merge contiguous icmps into a memcmp
static Constant * getInfinity(Type *Ty, bool Negative=false)
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Triple - Helper class for working with autoconf configuration names.
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
uint64_t Offset
Slice starts at this Offset.
AttrBuilder typeIncompatible(Type *Ty)
Which attributes cannot be applied to a type.
bool isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to be non-zero when defined.
bool isStrictFP() const
Determine if the call requires strict floating point semantics.
bool isFiniteNonZero() const
The instances of the Type class are immutable: once they are created, they are never changed.
static bool callHasFP128Argument(const CallInst *CI)
void removeAttributes(unsigned i, const AttrBuilder &Attrs)
AttributeList getAttributes() const
Return the parameter attributes for this call.
bool hasApproxFunc() const
Determine whether the approximate-math-functions flag is set.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
static bool callHasFloatingPointArgument(const CallInst *CI)
uint64_t getDereferenceableBytes(unsigned i) const
Extract the number of dereferenceable bytes for a call or parameter (0=unknown).
uint64_t Length
Length of the slice.
unsigned getNumArgOperands() const
Value * optimizeCall(CallInst *CI, IRBuilderBase &B)
Take the given call instruction and return a more optimal value to replace the instruction with or 0 ...
Value * getPointerOperand()
Convenience struct for specifying and reasoning about fast-math flags.
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
Adds the attribute to the indicated argument.
const fltSemantics & getSemantics() const
Value * emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs, IRBuilderBase &B, const TargetLibraryInfo &TLI)
Emit a call to the calloc function.
Holds functions to get, set or test bitfields.
LLVM Basic Block Representation.
We have fiadd patterns now but the followings have the same cost and complexity We need a way to specify the later is more profitable def def The FP stackifier should handle simple permutates to reduce number of shuffle e g floor
OneUse_match< T > m_OneUse(const T &SubPattern)
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
This is the shared class of boolean and integer constants.
LibCallSimplifier(const DataLayout &DL, const TargetLibraryInfo *TLI, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, function_ref< void(Instruction *, Value *)> Replacer=&replaceAllUsesWithDefault, function_ref< void(Instruction *)> Eraser=&eraseFromParentDefault)
Value * emitStrDup(Value *Ptr, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strdup function to the builder, for the specified pointer.
Value * emitFPutC(Value *Char, Value *File, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the fputc function.
void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
Removes the attribute from the given argument.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
bool match(Val *V, const Pattern &P)
Value * optimizeCall(CallInst *CI, IRBuilderBase &B)
optimizeCall - Take the given call instruction and return a more optimal value to replace the instruc...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Align getKnownAlignment(Value *V, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
Try to infer an alignment for the specified pointer.
(vector float) vec_cmpeq(*A, *B) C
static uint64_t round(uint64_t Acc, uint64_t Input)
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
iterator begin()
Instruction iterator methods.
Value * emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strcat function.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
Value * emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcpy function.
AttributeSet getParamAttributes(unsigned ArgNo) const
The attributes for the argument or parameter at the given index are returned.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Class to represent integer types.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
double convertToDouble() const
Flag
These should be considered private to the implementation of the MCInstrDesc class.
An arbitrary precision integer that knows its signedness.
ConstantFP - Floating Point Values [float, double].
Value * emitPutChar(Value *Char, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the putchar function. This assumes that Char is an integer.
static bool isOnlyUsedInComparisonWithZero(Value *V)
int getNumOccurrences() const
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static Value * getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno, Module *M, IRBuilderBase &B, const TargetLibraryInfo *TLI)
FunctionType * getFunctionType()
bool isGEPBasedOnPointerToString(const GEPOperator *GEP, unsigned CharSize=8)
Returns true if the GEP is based on a pointer to a string (array of.
Value * emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, IRBuilderBase &B, const AttributeList &Attrs)
Emit a call to the binary function named 'Name' (e.g.
static Value * valueHasFloatPrecision(Value *Val)
Return a variant of Val with float type.
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Value * emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strncmp function to the builder.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
unsigned getIntegerBitWidth() const
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Function * getCaller()
Helper to get the caller (the parent function).
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
Analysis providing profile information.
FortifiedLibCallSimplifier(const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize=false)
AttributeList getAttributes() const
Return the attribute list for this Function.
bool isIntegerTy() const
True if this is an instance of IntegerType.
An efficient, type-erasing, non-owning reference to a callable.
bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
static void annotateDereferenceableBytes(CallInst *CI, ArrayRef< unsigned > ArgNos, uint64_t DereferenceableBytes)
This is an important base class in LLVM.
Value * emitSPrintf(Value *Dest, Value *Fmt, ArrayRef< Value * > VariadicArgs, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the sprintf function.
bool doesNotAccessMemory(unsigned OpNo) const
This instruction compares its operands according to the predicate given to the constructor.
Module * getParent()
Get the module that this global value is contained inside of...
static void insertSinCosCall(IRBuilderBase &B, Function *OrigCallee, Value *Arg, bool UseFloat, Value *&Sin, Value *&Cos, Value *&SinCos)
bool getConstantStringInfo(const Value *V, StringRef &Str, uint64_t Offset=0, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
Value * emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the __memcpy_chk function to the builder.
LLVM_NODISCARD AttributeList addParamAttributes(LLVMContext &C, unsigned ArgNo, const AttrBuilder &B) const
Add an argument attribute to the list.
static bool isTrigLibCall(CallInst *CI)
Value * emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcat function.
Type * getParamType(unsigned i) const
Parameter type accessors.
initializer< Ty > init(const Ty &Val)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
static Attribute getWithDereferenceableBytes(LLVMContext &Context, uint64_t Bytes)
Value * emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the bcmp function.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, OptimizationRemarkEmitter *ORE=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
static bool ignoreCallingConv(LibFunc Func)
Value * emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the mempcpy function.
bool isFast() const
Determine whether all fast-math-flags are set.
Value * emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the stpncpy function to the builder, for the specified pointer arguments and length.
This class represents the LLVM 'select' instruction.
LLVM_NODISCARD size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Value * emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strncat function.
A Module instance is used to store all the information related to an LLVM module.
static bool isOnlyUsedInEqualityComparison(Value *V, Value *With)
Return true if it is only used in equality comparisons with With.
void flipAllBits()
Toggle every bit to its opposite value.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Class for arbitrary precision integers.
LLVM_NODISCARD StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
static bool isBinary(MachineInstr &MI)
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
static Value * optimizeUnaryDoubleFP(CallInst *CI, IRBuilderBase &B, bool isPrecise=false)
Shrink double -> float for unary functions.
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
SmallVector< MachineOperand, 4 > Cond
static const fltSemantics & IEEEdouble() LLVM_READNONE
StringRef - Represent a constant reference to a string, i.e.
bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
static Value * optimizeBinaryDoubleFP(CallInst *CI, IRBuilderBase &B, bool isPrecise=false)
Shrink double -> float for binary functions.
@ ICMP_ULT
unsigned less than
static void annotateNonNullNoUndefBasedOnAccess(CallInst *CI, ArrayRef< unsigned > ArgNos)
static void annotateNonNullAndDereferenceable(CallInst *CI, ArrayRef< unsigned > ArgNos, Value *Size, const DataLayout &DL)
Type * getType() const
All values are typed, get the type of this value.
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
const Function * getFunction() const
Return the function this instruction belongs to.
LLVMContext & getContext() const
All values hold a context through their type.
@ Mod
The access may modify the value stored in memory.
Common base class shared among various IRBuilders.
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Value * emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the stpcpy function to the builder, for the specified pointer arguments.
This class represents an extension of floating point types.
We have fiadd patterns now but the followings have the same cost and complexity We need a way to specify the later is more profitable def def The FP stackifier should handle simple permutates to reduce number of shuffle e g trunc
bool isKnownNeverInfinity(const Value *V, const TargetLibraryInfo *TLI, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
Value * emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strlen function to the builder, for the specified pointer.
Value * emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strcpy function to the builder, for the specified pointer arguments.
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
StringRef getName() const
Return a constant reference to the value's name.
An instruction for reading from memory.
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
Value * emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilderBase &B, const AttributeList &Attrs)
Emit a call to the unary function named 'Name' (e.g.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
amdgpu Simplify well known AMD library false FunctionCallee Callee
uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
bool hasFloatFn(const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Check whether the overloaded floating point function corresponding to Ty is available.
static constexpr roundingMode rmTowardZero
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE minNum semantics.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
static Value * getIntToFPVal(Value *I2F, IRBuilderBase &B)
static Value * replaceUnaryCall(CallInst *CI, IRBuilderBase &B, Intrinsic::ID IID)
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
constexpr unsigned BitWidth
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
Provides information about what library functions are available for the current target.
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
This is blocked on not handling X *X *X powi(X, 3)(see note above). The issue is that we end up getting t
Value * emitFPutS(Value *Str, Value *File, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the fputs function.
static double log2(double V)
static Value * optimizeDoubleFP(CallInst *CI, IRBuilderBase &B, bool isBinary, bool isPrecise=false)
Shrink double -> float functions.
bool hasAllowReassoc() const
Determine whether the allow-reassociation flag is set.
bool hasNoNaNs() const
Determine whether the no-NaNs flag is set.
Value * emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the malloc function.
A wrapper class for inspecting calls to intrinsic functions.
Value * castToCStr(Value *V, IRBuilderBase &B)
Return V if it is an i8*, otherwise cast it to i8*.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
static constexpr roundingMode rmNearestTiesToEven
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
bool isStructTy() const
True if this is an instance of StructType.
Value * emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strncpy function to the builder, for the specified pointer arguments and length.
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Value * getArgOperand(unsigned i) const
This class represents a truncation of floating point types.
const BasicBlock * getParent() const
Predicate getPredicate() const
Return the predicate for this instruction.
Align max(MaybeAlign Lhs, Align Rhs)
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Value * emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, ArrayRef< Value * > Args, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the snprintf function.
Value * emitPutS(Value *Str, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the puts function. This assumes that Str is some pointer.
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
void addAttribute(unsigned i, Attribute::AttrKind Kind)
adds the attribute to the list of attributes.
Value * emitStrChr(Value *Ptr, char C, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strchr function to the builder, for the specified pointer and character.
bool hasNoInfs() const
Determine whether the no-infs flag is set.
bool hasNoSignedZeros() const
Determine whether the no-signed-zeros flag is set.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
static bool isCallingConvCCompatible(CallBase *CI)
Returns true if call site / callee has cdecl-compatible calling conventions.
This class represents a function call, abstracting a target machine's calling convention.
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Represents offset+length into a ConstantDataArray.
void takeName(Value *V)
Transfer the name from V to this value.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Value * getOperand(unsigned i) const
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
static bool canTransformToMemCmp(CallInst *CI, Value *Str, uint64_t Len, const DataLayout &DL)
LLVM_NODISCARD size_t size() const
size - Get the string size.
static Value * getPow(Value *InnerChain[33], unsigned Exp, IRBuilderBase &B)
static cl::opt< bool > EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden, cl::init(false), cl::desc("Enable unsafe double to float " "shrinking for math lib calls"))
BinaryOp_match< LHS, RHS, Instruction::FMul > m_FMul(const LHS &L, const RHS &R)
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Value * emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the vsprintf function.
LLVM Value Representation.
LLVM_NODISCARD int compare(StringRef RHS) const
compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less tha...
Value * emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the vsnprintf function.
iterator_range< user_iterator > users()
static Value * optimizeTrigReflections(CallInst *Call, LibFunc Func, IRBuilderBase &B)
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
static Value * convertStrToNumber(CallInst *CI, StringRef &Str, int64_t Base)
Value * emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memchr function.
Class to represent function types.
A Use represents the edge between a Value definition and its users.
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL)
ConstantFoldLoadFromConstPtr - Return the value that a load from C would produce if it is constant an...