Go to the documentation of this file.
37 using namespace PatternMatch;
42 cl::desc(
"Enable unsafe double to float "
43 "shrinking for math lib calls"));
50 return Func == LibFunc_abs || Func == LibFunc_labs ||
51 Func == LibFunc_llabs || Func == LibFunc_strlen;
57 if (
ICmpInst *IC = dyn_cast<ICmpInst>(U))
58 if (IC->isEquality() && IC->getOperand(1) == With)
68 return OI->getType()->isFloatingPointTy();
74 return OI->getType()->isFP128Ty();
79 if (Base < 2 || Base > 36)
85 std::string nptr = Str.str();
87 long long int Result = strtoll(nptr.c_str(), &End,
Base);
105 if (
ICmpInst *IC = dyn_cast<ICmpInst>(U))
106 if (
Constant *
C = dyn_cast<Constant>(IC->getOperand(1)))
107 if (
C->isNullValue())
135 for (
unsigned ArgNo : ArgNos) {
136 uint64_t DerefBytes = DereferenceableBytes;
141 DereferenceableBytes);
160 for (
unsigned ArgNo : ArgNos) {
177 if (
ConstantInt *LenC = dyn_cast<ConstantInt>(Size)) {
185 DerefMin =
std::min(
X->getZExtValue(),
Y->getZExtValue());
199 if (
auto *NewCI = dyn_cast_or_null<CallInst>(New))
226 return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, Len,
B));
240 Value *CpyDst =
B.CreateGEP(
B.getInt8Ty(), Dst, DstLen,
"endptr");
261 ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size);
290 return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, SrcLen,
B));
324 return B.CreateGEP(
B.getInt8Ty(), SrcStr, StrLen,
"strchr");
337 return B.CreateGEP(
B.getInt8Ty(), SrcStr,
B.getInt64(
I),
"strchr");
365 return B.CreateGEP(
B.getInt8Ty(), SrcStr,
B.getInt64(
I),
"strrchr");
378 if (HasStr1 && HasStr2)
381 if (HasStr1 && Str1.
empty())
382 return B.CreateNeg(
B.CreateZExt(
383 B.CreateLoad(
B.getInt8Ty(), Str2P,
"strcmpload"), CI->
getType()));
385 if (HasStr2 && Str2.
empty())
386 return B.CreateZExt(
B.CreateLoad(
B.getInt8Ty(), Str1P,
"strcmpload"),
406 if (!HasStr1 && HasStr2) {
413 }
else if (HasStr1 && !HasStr2) {
437 if (
ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size))
453 if (HasStr1 && HasStr2) {
462 if (HasStr1 && Str1.
empty())
463 return B.CreateNeg(
B.CreateZExt(
464 B.CreateLoad(
B.getInt8Ty(), Str2P,
"strcmpload"), CI->
getType()));
466 if (HasStr2 && Str2.
empty())
467 return B.CreateZExt(
B.CreateLoad(
B.getInt8Ty(), Str1P,
"strcmpload"),
478 if (!HasStr1 && HasStr2) {
486 }
else if (HasStr1 && !HasStr2) {
503 if (SrcLen && Size) {
505 if (SrcLen <= Size->getZExtValue() + 1)
546 return StrLen ?
B.CreateInBoundsGEP(
B.getInt8Ty(), Dst, StrLen) :
nullptr;
558 Value *DstEnd =
B.CreateGEP(
B.getInt8Ty(), Dst,
580 if (
ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size))
602 CallInst *NewCI =
B.CreateMemSet(Dst,
B.getInt8(
'\0'), Size, MemSetAlign);
611 if (Len > SrcLen + 1) {
616 std::string SrcStr = Str.str();
617 SrcStr.resize(Len,
'\0');
618 Src =
B.CreateGlobalString(SrcStr,
"str");
638 Type *CharTy =
B.getIntNTy(CharSize);
648 return B.CreateZExt(
B.CreateLoad(CharTy, Src,
"char0"),
653 if (
ConstantInt *BoundCst = dyn_cast<ConstantInt>(Bound)) {
654 if (BoundCst->isZero())
658 if (BoundCst->isOne()) {
660 Value *CharVal =
B.CreateLoad(CharTy, Src,
"strnlen.char0");
662 Value *
Cmp =
B.CreateICmpNE(CharVal, ZeroChar,
"strnlen.char0cmp");
663 return B.CreateZExt(Cmp, CI->
getType());
696 if (Slice.
Array ==
nullptr) {
715 cast<ArrayType>(
GEP->getSourceElementType())->getNumElements();
722 (isa<GlobalVariable>(
GEP->getOperand(0)) &&
723 NullTermIdx == ArrSize - 1)) {
735 if (LenTrue && LenFalse) {
738 <<
"folded strlen(select) to select of constants";
740 return B.CreateSelect(
SI->getCondition(),
750 if (
Value *V = optimizeStringLength(CI,
B, 8))
758 if (
Value *V = optimizeStringLength(CI,
B, 8, Bound))
768 unsigned WCharSize = TLI->getWCharSize(M) * 8;
773 return optimizeStringLength(CI,
B, WCharSize);
783 if ((HasS1 && S1.
empty()) || (HasS2 && S2.
empty()))
787 if (HasS1 && HasS2) {
797 if (HasS2 && S2.
size() == 1)
805 if (isa<ConstantPointerNull>(EndPtr)) {
821 if ((HasS1 && S1.
empty()) || (HasS2 && S2.
empty()))
825 if (HasS1 && HasS2) {
841 if (HasS1 && S1.
empty())
845 if (HasS1 && HasS2) {
853 if (HasS2 && S2.
empty())
878 replaceAllUsesWith(Old, Cmp);
889 if (HasStr2 && ToFindStr.
empty())
893 if (HasStr1 && HasStr2) {
902 B.CreateConstInBoundsGEP1_64(
B.getInt8Ty(), Result, Offset,
"strstr");
903 return B.CreateBitCast(Result, CI->
getType());
907 if (HasStr2 && ToFindStr.
size() == 1) {
909 return StrChr ?
B.CreateBitCast(StrChr, CI->
getType()) :
nullptr;
932 Value *Val =
B.CreateLoad(
B.getInt8Ty(), SrcStr,
"memrchr.char0");
934 CharVal =
B.CreateTrunc(CharVal,
B.getInt8Ty());
935 Value *
Cmp =
B.CreateICmpEQ(Val, CharVal,
"memrchr.char0cmp");
936 return B.CreateSelect(Cmp, SrcStr, NullPtr,
"memrchr.sel");
947 if (Str.size() < EndOff)
952 if (
ConstantInt *CharC = dyn_cast<ConstantInt>(CharVal)) {
962 return B.CreateGEP(
B.getInt8Ty(), SrcStr,
B.getInt64(Pos));
971 Value *SrcPlus =
B.CreateGEP(
B.getInt8Ty(), SrcStr,
B.getInt64(Pos),
973 return B.CreateSelect(Cmp, NullPtr, SrcPlus,
"memrchr.sel");
987 ConstantInt *CharC = dyn_cast<ConstantInt>(CharVal);
998 Value *Val =
B.CreateLoad(
B.getInt8Ty(), SrcStr,
"memchr.char0");
1000 CharVal =
B.CreateTrunc(CharVal,
B.getInt8Ty());
1001 Value *
Cmp =
B.CreateICmpEQ(Val, CharVal,
"memchr.char0cmp");
1003 return B.CreateSelect(Cmp, SrcStr, NullPtr,
"memchr.sel");
1025 B.CreateGEP(
B.getInt8Ty(), SrcStr,
B.getInt64(Pos),
"memchr.ptr");
1026 return B.CreateSelect(Cmp, NullPtr, SrcPlus);
1036 Str = Str.substr(0, EndOff);
1052 *std::max_element(
reinterpret_cast<const unsigned char *
>(Str.begin()),
1053 reinterpret_cast<const unsigned char *
>(Str.end()));
1060 if (!
DL.fitsInLegalInteger(Max + 1))
1075 C =
B.CreateAnd(
C,
B.getIntN(
Width, 0xFF));
1083 Value *
Bits =
B.CreateIsNotNull(
B.CreateAnd(Shl, BitfieldC),
"memchr.bits");
1087 return B.CreateIntToPtr(
B.CreateLogicalAnd(Bounds,
Bits,
"memchr"),
1105 return B.CreateSub(LHSV, RHSV,
"chardiff");
1113 unsigned PrefAlignment =
DL.getPrefTypeAlignment(IntType);
1116 Value *LHSV =
nullptr;
1117 if (
auto *LHSC = dyn_cast<Constant>(
LHS)) {
1121 Value *RHSV =
nullptr;
1122 if (
auto *RHSC = dyn_cast<Constant>(
RHS)) {
1134 LHSV =
B.CreateLoad(IntType,
B.CreateBitCast(
LHS, LHSPtrTy),
"lhsv");
1139 RHSV =
B.CreateLoad(IntType,
B.CreateBitCast(
RHS, RHSPtrTy),
"rhsv");
1141 return B.CreateZExt(
B.CreateICmpNE(LHSV, RHSV), CI->
getType(),
"memcmp");
1151 if (Len > LHSStr.
size() || Len > RHSStr.
size())
1168 Value *LibCallSimplifier::optimizeMemCmpBCmpCommon(
CallInst *CI,
1183 if (LenC->getZExtValue() == 0)
1194 if (
Value *V = optimizeMemCmpBCmpCommon(CI,
B))
1212 return optimizeMemCmpBCmpCommon(CI,
B);
1218 if (isa<IntrinsicInst>(CI))
1240 if (
N->isNullValue())
1253 if (
N->getZExtValue() <= SrcStr.
size()) {
1265 return Pos + 1 <=
N->getZExtValue()
1266 ?
B.CreateInBoundsGEP(
B.getInt8Ty(), Dst, NewN)
1282 return B.CreateInBoundsGEP(
B.getInt8Ty(), Dst,
N);
1288 if (isa<IntrinsicInst>(CI))
1303 if (isa<IntrinsicInst>(CI))
1347 if (
FPExtInst *Cast = dyn_cast<FPExtInst>(Val)) {
1348 Value *
Op = Cast->getOperand(0);
1349 if (
Op->getType()->isFloatTy())
1352 if (
ConstantFP *Const = dyn_cast<ConstantFP>(Val)) {
1366 bool isPrecise =
false) {
1397 if (!CallerName.
empty() && CallerName.
back() ==
'f' &&
1398 CallerName.
size() == (CalleeName.
size() + 1) &&
1413 R =
isBinary ?
B.CreateCall(Fn, V) :
B.CreateCall(Fn, V[0]);
1420 return B.CreateFPExt(R,
B.getDoubleTy());
1426 bool isPrecise =
false) {
1433 bool isPrecise =
false) {
1449 assert(
Op->getType()->isArrayTy() &&
"Unexpected signature for cabs!");
1450 Real =
B.CreateExtractValue(
Op, 0,
"real");
1451 Imag =
B.CreateExtractValue(
Op, 1,
"imag");
1458 Value *RealReal =
B.CreateFMul(Real, Real);
1459 Value *ImagImag =
B.CreateFMul(Imag, Imag);
1464 *CI,
B.CreateCall(FSqrt,
B.CreateFAdd(RealReal, ImagImag),
"cabs"));
1469 if (!isa<FPMathOperator>(Call))
1473 B.setFastMathFlags(Call->getFastMathFlags());
1487 return B.CreateFNeg(
1488 copyFlags(*Call,
B.CreateCall(Call->getCalledFunction(),
X)));
1496 B.CreateCall(Call->getCalledFunction(),
X,
"cos"));
1508 assert(Exp != 0 &&
"Incorrect exponent 0 not handled");
1510 if (InnerChain[Exp])
1511 return InnerChain[Exp];
1513 static const unsigned AddChain[33][2] = {
1517 {1, 2}, {2, 2}, {2, 3}, {3, 3}, {2, 5}, {4, 4},
1518 {1, 8}, {5, 5}, {1, 10}, {6, 6}, {4, 9}, {7, 7},
1519 {3, 12}, {8, 8}, {8, 9}, {2, 16}, {1, 18}, {10, 10},
1520 {6, 15}, {11, 11}, {3, 20}, {12, 12}, {8, 17}, {13, 13},
1521 {3, 24}, {14, 14}, {4, 25}, {15, 15}, {3, 28}, {16, 16},
1524 InnerChain[Exp] =
B.CreateFMul(
getPow(InnerChain, AddChain[Exp][0],
B),
1525 getPow(InnerChain, AddChain[Exp][1],
B));
1526 return InnerChain[Exp];
1532 if (isa<SIToFPInst>(I2F) || isa<UIToFPInst>(I2F)) {
1533 Value *
Op = cast<Instruction>(I2F)->getOperand(0);
1536 unsigned BitWidth =
Op->getType()->getPrimitiveSizeInBits();
1538 (
BitWidth == DstWidth && isa<SIToFPInst>(I2F)))
1539 return isa<SIToFPInst>(I2F) ?
B.CreateSExt(
Op,
B.getIntNTy(DstWidth))
1540 :
B.CreateZExt(
Op,
B.getIntNTy(DstWidth));
1578 TLI->getLibFunc(CalleeFn->
getName(), LibFn) &&
1583 LibFunc LibFnFloat, LibFnDouble, LibFnLongDouble;
1588 case LibFunc_expf:
case LibFunc_exp:
case LibFunc_expl:
1589 ExpName = TLI->getName(LibFunc_exp);
1590 ID = Intrinsic::exp;
1591 LibFnFloat = LibFunc_expf;
1592 LibFnDouble = LibFunc_exp;
1593 LibFnLongDouble = LibFunc_expl;
1595 case LibFunc_exp2f:
case LibFunc_exp2:
case LibFunc_exp2l:
1596 ExpName = TLI->getName(LibFunc_exp2);
1597 ID = Intrinsic::exp2;
1598 LibFnFloat = LibFunc_exp2f;
1599 LibFnDouble = LibFunc_exp2;
1600 LibFnLongDouble = LibFunc_exp2l;
1617 substituteInParent(BaseFn, ExpFn);
1630 (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo)) &&
1631 hasFloatFn(M, TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl)) {
1635 TLI, LibFunc_ldexp, LibFunc_ldexpf,
1636 LibFunc_ldexpl,
B,
Attrs));
1640 if (
hasFloatFn(M, TLI, Ty, LibFunc_exp2, LibFunc_exp2f, LibFunc_exp2l)) {
1643 BaseR = BaseR / *BaseF;
1645 const APFloat *NF = IsReciprocal ? &BaseR : BaseF;
1647 if ((IsInteger || IsReciprocal) &&
1650 NI > 1 && NI.isPowerOf2()) {
1651 double N = NI.logBase2() * (IsReciprocal ? -1.0 : 1.0);
1655 Mod, Intrinsic::exp2, Ty),
1660 LibFunc_exp2l,
B,
Attrs));
1667 hasFloatFn(M, TLI, Ty, LibFunc_exp10, LibFunc_exp10f, LibFunc_exp10l))
1669 LibFunc_exp10f, LibFunc_exp10l,
1678 "pow(1.0, y) should have been simplified earlier!");
1680 Value *Log =
nullptr;
1687 Value *
FMul =
B.CreateFMul(Log, Expo,
"mul");
1690 Mod, Intrinsic::exp2, Ty),
1692 else if (
hasFloatFn(M, TLI, Ty, LibFunc_exp2, LibFunc_exp2f,
1696 LibFunc_exp2l,
B,
Attrs));
1710 return B.CreateCall(SqrtFn, V,
"sqrt");
1720 LibFunc_sqrtl,
B,
Attrs);
1757 Sqrt =
B.CreateCall(FAbsFn, Sqrt,
"abs");
1767 Value *FCmp =
B.CreateFCmpOEQ(
Base, NegInf,
"isinf");
1768 Sqrt =
B.CreateSelect(FCmp, PosInf, Sqrt);
1783 return B.CreateCall(
F,
Args);
1805 if (
Value *Exp = replacePowWithExp(Pow,
B))
1824 return B.CreateFMul(
Base,
Base,
"square");
1826 if (
Value *Sqrt = replacePowWithSqrt(Pow,
B))
1844 Value *Sqrt =
nullptr;
1845 if (!ExpoA.isInteger()) {
1863 Value *InnerChain[33] = {
nullptr};
1864 InnerChain[1] =
Base;
1865 InnerChain[2] =
B.CreateFMul(
Base,
Base,
"square");
1874 FMul =
B.CreateFMul(FMul, Sqrt);
1883 APSInt IntExpo(TLI->getIntSize(),
false);
1897 if (AllowApprox && (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo))) {
1904 if (UnsafeFPShrink &&
Name == TLI->getName(LibFunc_pow) &&
1905 hasFloatVersion(M,
Name)) {
1919 if (UnsafeFPShrink &&
Name == TLI->getName(LibFunc_exp2) &&
1920 hasFloatVersion(M,
Name))
1928 if ((isa<SIToFPInst>(
Op) || isa<UIToFPInst>(
Op)) &&
1929 hasFloatFn(M, TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl)) {
1932 LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl,
1946 if ((
Name ==
"fmin" ||
Name ==
"fmax") && hasFloatVersion(M,
Name))
1960 B.setFastMathFlags(FMF);
1966 *CI,
B.CreateCall(
F, {CI->getArgOperand(0), CI->getArgOperand(1)}));
1978 if (UnsafeFPShrink && hasFloatVersion(Mod, LogNm))
1986 LibFunc LogLb, ExpLb, Exp2Lb, Exp10Lb, PowLb;
1989 if (TLI->getLibFunc(LogNm, LogLb))
1992 LogID = Intrinsic::log;
1993 ExpLb = LibFunc_expf;
1994 Exp2Lb = LibFunc_exp2f;
1995 Exp10Lb = LibFunc_exp10f;
1996 PowLb = LibFunc_powf;
1999 LogID = Intrinsic::log;
2000 ExpLb = LibFunc_exp;
2001 Exp2Lb = LibFunc_exp2;
2002 Exp10Lb = LibFunc_exp10;
2003 PowLb = LibFunc_pow;
2006 LogID = Intrinsic::log;
2007 ExpLb = LibFunc_expl;
2008 Exp2Lb = LibFunc_exp2l;
2009 Exp10Lb = LibFunc_exp10l;
2010 PowLb = LibFunc_powl;
2014 ExpLb = LibFunc_expf;
2015 Exp2Lb = LibFunc_exp2f;
2016 Exp10Lb = LibFunc_exp10f;
2017 PowLb = LibFunc_powf;
2021 ExpLb = LibFunc_exp;
2022 Exp2Lb = LibFunc_exp2;
2023 Exp10Lb = LibFunc_exp10;
2024 PowLb = LibFunc_pow;
2028 ExpLb = LibFunc_expl;
2029 Exp2Lb = LibFunc_exp2l;
2030 Exp10Lb = LibFunc_exp10l;
2031 PowLb = LibFunc_powl;
2033 case LibFunc_log10f:
2034 LogID = Intrinsic::log10;
2035 ExpLb = LibFunc_expf;
2036 Exp2Lb = LibFunc_exp2f;
2037 Exp10Lb = LibFunc_exp10f;
2038 PowLb = LibFunc_powf;
2041 LogID = Intrinsic::log10;
2042 ExpLb = LibFunc_exp;
2043 Exp2Lb = LibFunc_exp2;
2044 Exp10Lb = LibFunc_exp10;
2045 PowLb = LibFunc_pow;
2047 case LibFunc_log10l:
2048 LogID = Intrinsic::log10;
2049 ExpLb = LibFunc_expl;
2050 Exp2Lb = LibFunc_exp2l;
2051 Exp10Lb = LibFunc_exp10l;
2052 PowLb = LibFunc_powl;
2058 LogID == Intrinsic::log10) {
2060 ExpLb = LibFunc_expf;
2061 Exp2Lb = LibFunc_exp2f;
2062 Exp10Lb = LibFunc_exp10f;
2063 PowLb = LibFunc_powf;
2065 ExpLb = LibFunc_exp;
2066 Exp2Lb = LibFunc_exp2;
2067 Exp10Lb = LibFunc_exp10;
2068 PowLb = LibFunc_pow;
2079 TLI->getLibFunc(*
Arg, ArgLb);
2082 if (ArgLb == PowLb || ArgID == Intrinsic::pow) {
2086 Arg->getOperand(0),
"log")
2088 Value *MulY =
B.CreateFMul(
Arg->getArgOperand(1), LogX,
"mul");
2091 substituteInParent(
Arg, MulY);
2097 if (ArgLb == ExpLb || ArgLb == Exp2Lb || ArgLb == Exp10Lb ||
2098 ArgID == Intrinsic::exp || ArgID == Intrinsic::exp2) {
2100 if (ArgLb == ExpLb || ArgID == Intrinsic::exp)
2103 else if (ArgLb == Exp2Lb || ArgID == Intrinsic::exp2)
2111 Value *MulY =
B.CreateFMul(
Arg->getArgOperand(0), LogE,
"mul");
2114 substituteInParent(
Arg, MulY);
2129 (
Callee->getName() ==
"sqrt" ||
2130 Callee->getIntrinsicID() == Intrinsic::sqrt))
2137 if (!
I ||
I->getOpcode() != Instruction::FMul || !
I->isFast())
2143 Value *Op0 =
I->getOperand(0);
2144 Value *Op1 =
I->getOperand(1);
2145 Value *RepeatOp =
nullptr;
2146 Value *OtherOp =
nullptr;
2156 Value *OtherMul0, *OtherMul1;
2159 if (OtherMul0 == OtherMul1 && cast<Instruction>(Op0)->isFast()) {
2161 RepeatOp = OtherMul0;
2172 B.setFastMathFlags(
I->getFastMathFlags());
2176 Type *ArgType =
I->getType();
2178 Value *FabsCall =
B.CreateCall(Fabs, RepeatOp,
"fabs");
2184 Value *SqrtCall =
B.CreateCall(Sqrt, OtherOp,
"sqrt");
2185 return copyFlags(*CI,
B.CreateFMul(FabsCall, SqrtCall));
2196 if (UnsafeFPShrink &&
Name ==
"tan" && hasFloatVersion(M,
Name))
2200 auto *OpC = dyn_cast<CallInst>(Op1);
2205 if (!CI->
isFast() || !OpC->isFast())
2213 if (
F && TLI->getLibFunc(
F->getName(), Func) &&
2215 ((Func == LibFunc_atan &&
Callee->getName() ==
"tan") ||
2216 (Func == LibFunc_atanf &&
Callee->getName() ==
"tanf") ||
2217 (Func == LibFunc_atanl &&
Callee->getName() ==
"tanl")))
2218 Ret = OpC->getArgOperand(0);
2226 return CI->
hasFnAttr(Attribute::NoUnwind) &&
2240 Name =
"__sincospif_stret";
2249 Name =
"__sincospi_stret";
2263 B.SetInsertPoint(ArgInst->getParent(), ++ArgInst->getIterator());
2267 BasicBlock &EntryBB =
B.GetInsertBlock()->getParent()->getEntryBlock();
2268 B.SetInsertPoint(&EntryBB, EntryBB.
begin());
2271 SinCos =
B.CreateCall(
Callee,
Arg,
"sincospi");
2274 Sin =
B.CreateExtractValue(SinCos, 0,
"sinpi");
2275 Cos =
B.CreateExtractValue(SinCos, 1,
"cospi");
2297 bool IsFloat =
Arg->getType()->isFloatTy();
2304 classifyArgUse(U,
F, IsFloat, SinCalls, CosCalls, SinCosCalls);
2307 if (SinCalls.empty() || CosCalls.empty())
2318 replaceAllUsesWith(
C, Res);
2321 replaceTrigInsts(SinCalls, Sin);
2322 replaceTrigInsts(CosCalls, Cos);
2323 replaceTrigInsts(SinCosCalls, SinCos);
2328 void LibCallSimplifier::classifyArgUse(
2333 CallInst *CI = dyn_cast<CallInst>(Val);
2345 if (!Callee || !TLI->getLibFunc(*Callee, Func) ||
2351 if (Func == LibFunc_sinpif)
2352 SinCalls.push_back(CI);
2353 else if (Func == LibFunc_cospif)
2354 CosCalls.push_back(CI);
2355 else if (Func == LibFunc_sincospif_stret)
2356 SinCosCalls.push_back(CI);
2358 if (Func == LibFunc_sinpi)
2359 SinCalls.push_back(CI);
2360 else if (Func == LibFunc_cospi)
2361 CosCalls.push_back(CI);
2362 else if (Func == LibFunc_sincospi_stret)
2363 SinCosCalls.push_back(CI);
2374 Type *ArgType =
Op->getType();
2376 Intrinsic::cttz, ArgType);
2377 Value *V =
B.CreateCall(
F, {
Op,
B.getTrue()},
"cttz");
2379 V =
B.CreateIntCast(V,
B.getInt32Ty(),
false);
2382 return B.CreateSelect(
Cond, V,
B.getInt32(0));
2388 Type *ArgType =
Op->getType();
2390 Intrinsic::ctlz, ArgType);
2391 Value *V =
B.CreateCall(
F, {
Op,
B.getFalse()},
"ctlz");
2394 return B.CreateIntCast(V, CI->
getType(),
false);
2401 Value *IsNeg =
B.CreateIsNeg(
X);
2402 Value *NegX =
B.CreateNSWNeg(
X,
"neg");
2403 return B.CreateSelect(IsNeg, NegX,
X);
2409 Op =
B.CreateSub(
Op,
B.getInt32(
'0'),
"isdigittmp");
2410 Op =
B.CreateICmpULT(
Op,
B.getInt32(10),
"isdigit");
2417 Op =
B.CreateICmpULT(
Op,
B.getInt32(128),
"isascii");
2485 if (StreamArg >= (
int)CI->
arg_size())
2493 return GV->
getName() ==
"stderr";
2503 if (FormatStr.
empty())
2513 if (FormatStr.
size() == 1 || FormatStr ==
"%%")
2517 if (FormatStr ==
"%s" && CI->
arg_size() > 1) {
2522 if (OperandStr.
empty())
2525 if (OperandStr.
size() == 1)
2528 if (OperandStr.
back() ==
'\n') {
2530 Value *GV =
B.CreateGlobalString(OperandStr,
"str");
2537 if (FormatStr.
back() ==
'\n' &&
2542 Value *GV =
B.CreateGlobalString(FormatStr,
"str");
2548 if (FormatStr ==
"%c" && CI->
arg_size() > 1 &&
2553 if (FormatStr ==
"%s\n" && CI->
arg_size() > 1 &&
2564 if (
Value *V = optimizePrintFString(CI,
B)) {
2573 Callee->getAttributes());
2575 New->setCalledFunction(IPrintFFn);
2585 Callee->getAttributes());
2587 New->setCalledFunction(SmallPrintFFn);
2596 Value *LibCallSimplifier::optimizeSPrintFString(
CallInst *CI,
2615 FormatStr.
size() + 1));
2621 if (FormatStr.
size() != 2 || FormatStr[0] !=
'%' || CI->
arg_size() < 3)
2625 if (FormatStr[1] ==
'c') {
2631 B.CreateStore(V, Ptr);
2632 Ptr =
B.CreateGEP(
B.getInt8Ty(), Ptr,
B.getInt32(1),
"nul");
2633 B.CreateStore(
B.getInt8(0), Ptr);
2638 if (FormatStr[1] ==
's') {
2658 V =
B.CreatePointerCast(V,
B.getInt8PtrTy());
2659 Dest =
B.CreatePointerCast(Dest,
B.getInt8PtrTy());
2660 Value *PtrDiff =
B.CreatePtrDiff(
B.getInt8Ty(), V, Dest);
2661 return B.CreateIntCast(PtrDiff, CI->
getType(),
false);
2678 return B.CreateIntCast(Len, CI->
getType(),
false);
2687 if (
Value *V = optimizeSPrintFString(CI,
B)) {
2696 FT,
Callee->getAttributes());
2698 New->setCalledFunction(SIPrintFFn);
2708 Callee->getAttributes());
2710 New->setCalledFunction(SmallSPrintFFn);
2719 Value *LibCallSimplifier::optimizeSnPrintFString(
CallInst *CI,
2741 else if (
N < FormatStr.
size() + 1)
2751 FormatStr.
size() + 1)));
2757 if (FormatStr.
size() == 2 && FormatStr[0] ==
'%' && CI->
arg_size() == 4) {
2760 if (FormatStr[1] ==
'c') {
2771 B.CreateStore(V, Ptr);
2772 Ptr =
B.CreateGEP(
B.getInt8Ty(), Ptr,
B.getInt32(1),
"nul");
2773 B.CreateStore(
B.getInt8(0), Ptr);
2778 if (FormatStr[1] ==
's') {
2786 else if (
N < Str.size() + 1)
2802 if (
Value *V = optimizeSnPrintFString(CI,
B)) {
2811 Value *LibCallSimplifier::optimizeFPrintFString(
CallInst *CI,
2813 optimizeErrorReporting(CI,
B, 0);
2841 if (FormatStr.
size() != 2 || FormatStr[0] !=
'%' || CI->
arg_size() < 3)
2845 if (FormatStr[1] ==
'c') {
2853 if (FormatStr[1] ==
's') {
2867 if (
Value *V = optimizeFPrintFString(CI,
B)) {
2876 FT,
Callee->getAttributes());
2878 New->setCalledFunction(FIPrintFFn);
2887 auto SmallFPrintFFn =
2889 Callee->getAttributes());
2891 New->setCalledFunction(SmallFPrintFFn);
2900 optimizeErrorReporting(CI,
B, 3);
2905 if (SizeC && CountC) {
2926 optimizeErrorReporting(CI,
B, 1);
2974 bool LibCallSimplifier::hasFloatVersion(
const Module *M,
StringRef FuncName) {
2976 FloatFuncName +=
'f';
2980 Value *LibCallSimplifier::optimizeStringMemoryLibCall(
CallInst *CI,
2991 "Optimizing string/memory libcall would change the calling convention");
2993 case LibFunc_strcat:
2994 return optimizeStrCat(CI,
Builder);
2995 case LibFunc_strncat:
2996 return optimizeStrNCat(CI,
Builder);
2997 case LibFunc_strchr:
2998 return optimizeStrChr(CI,
Builder);
2999 case LibFunc_strrchr:
3000 return optimizeStrRChr(CI,
Builder);
3001 case LibFunc_strcmp:
3002 return optimizeStrCmp(CI,
Builder);
3003 case LibFunc_strncmp:
3004 return optimizeStrNCmp(CI,
Builder);
3005 case LibFunc_strcpy:
3006 return optimizeStrCpy(CI,
Builder);
3007 case LibFunc_stpcpy:
3008 return optimizeStpCpy(CI,
Builder);
3009 case LibFunc_strncpy:
3010 return optimizeStrNCpy(CI,
Builder);
3011 case LibFunc_strlen:
3012 return optimizeStrLen(CI,
Builder);
3013 case LibFunc_strnlen:
3014 return optimizeStrNLen(CI,
Builder);
3015 case LibFunc_strpbrk:
3016 return optimizeStrPBrk(CI,
Builder);
3017 case LibFunc_strndup:
3018 return optimizeStrNDup(CI,
Builder);
3019 case LibFunc_strtol:
3020 case LibFunc_strtod:
3021 case LibFunc_strtof:
3022 case LibFunc_strtoul:
3023 case LibFunc_strtoll:
3024 case LibFunc_strtold:
3025 case LibFunc_strtoull:
3026 return optimizeStrTo(CI,
Builder);
3027 case LibFunc_strspn:
3028 return optimizeStrSpn(CI,
Builder);
3029 case LibFunc_strcspn:
3030 return optimizeStrCSpn(CI,
Builder);
3031 case LibFunc_strstr:
3032 return optimizeStrStr(CI,
Builder);
3033 case LibFunc_memchr:
3034 return optimizeMemChr(CI,
Builder);
3035 case LibFunc_memrchr:
3036 return optimizeMemRChr(CI,
Builder);
3038 return optimizeBCmp(CI,
Builder);
3039 case LibFunc_memcmp:
3040 return optimizeMemCmp(CI,
Builder);
3041 case LibFunc_memcpy:
3042 return optimizeMemCpy(CI,
Builder);
3043 case LibFunc_memccpy:
3044 return optimizeMemCCpy(CI,
Builder);
3045 case LibFunc_mempcpy:
3046 return optimizeMemPCpy(CI,
Builder);
3047 case LibFunc_memmove:
3048 return optimizeMemMove(CI,
Builder);
3049 case LibFunc_memset:
3050 return optimizeMemSet(CI,
Builder);
3051 case LibFunc_realloc:
3052 return optimizeRealloc(CI,
Builder);
3053 case LibFunc_wcslen:
3054 return optimizeWcslen(CI,
Builder);
3056 return optimizeBCopy(CI,
Builder);
3064 Value *LibCallSimplifier::optimizeFloatingPointLibCall(
CallInst *CI,
3077 case LibFunc_sinpif:
3079 case LibFunc_cospif:
3081 return optimizeSinCosPi(CI,
Builder);
3085 return optimizePow(CI,
Builder);
3089 return optimizeExp2(CI,
Builder);
3097 return optimizeSqrt(CI,
Builder);
3101 case LibFunc_log10f:
3103 case LibFunc_log10l:
3104 case LibFunc_log1pf:
3106 case LibFunc_log1pl:
3113 return optimizeLog(CI,
Builder);
3117 return optimizeTan(CI,
Builder);
3124 case LibFunc_roundeven:
3126 case LibFunc_nearbyint:
3150 case LibFunc_copysign:
3160 return optimizeFMinFMax(CI,
Builder);
3164 return optimizeCAbs(CI,
Builder);
3188 Builder.setDefaultOperandBundles(OpBundles);
3195 else if (isa<FPMathOperator>(CI) && CI->
isFast())
3196 UnsafeFPShrink =
true;
3200 if (!IsCallingConvC)
3204 switch (II->getIntrinsicID()) {
3205 case Intrinsic::pow:
3206 return optimizePow(CI,
Builder);
3207 case Intrinsic::exp2:
3208 return optimizeExp2(CI,
Builder);
3209 case Intrinsic::log:
3211 case Intrinsic::log10:
3212 return optimizeLog(CI,
Builder);
3213 case Intrinsic::sqrt:
3214 return optimizeSqrt(CI,
Builder);
3215 case Intrinsic::memset:
3216 return optimizeMemSet(CI,
Builder);
3218 return optimizeMemCpy(CI,
Builder);
3219 case Intrinsic::memmove:
3220 return optimizeMemMove(CI,
Builder);
3227 if (
Value *SimplifiedFortifiedCI =
3228 FortifiedSimplifier.optimizeCall(CI,
Builder)) {
3230 CallInst *SimplifiedCI = dyn_cast<CallInst>(SimplifiedFortifiedCI);
3234 replaceAllUsesWith(CI, SimplifiedCI);
3239 Builder.SetInsertPoint(SimplifiedCI);
3240 if (
Value *V = optimizeStringMemoryLibCall(SimplifiedCI,
Builder)) {
3242 substituteInParent(SimplifiedCI, V);
3246 return SimplifiedFortifiedCI;
3254 if (
Value *V = optimizeStringMemoryLibCall(CI,
Builder))
3256 if (
Value *V = optimizeFloatingPointLibCall(CI, Func,
Builder))
3262 return optimizeFFS(CI,
Builder);
3266 return optimizeFls(CI,
Builder);
3270 return optimizeAbs(CI,
Builder);
3271 case LibFunc_isdigit:
3272 return optimizeIsDigit(CI,
Builder);
3273 case LibFunc_isascii:
3274 return optimizeIsAscii(CI,
Builder);
3275 case LibFunc_toascii:
3276 return optimizeToAscii(CI,
Builder);
3280 return optimizeAtoi(CI,
Builder);
3281 case LibFunc_strtol:
3282 case LibFunc_strtoll:
3283 return optimizeStrtol(CI,
Builder);
3284 case LibFunc_printf:
3285 return optimizePrintF(CI,
Builder);
3286 case LibFunc_sprintf:
3287 return optimizeSPrintF(CI,
Builder);
3288 case LibFunc_snprintf:
3289 return optimizeSnPrintF(CI,
Builder);
3290 case LibFunc_fprintf:
3291 return optimizeFPrintF(CI,
Builder);
3292 case LibFunc_fwrite:
3293 return optimizeFWrite(CI,
Builder);
3295 return optimizeFPuts(CI,
Builder);
3297 return optimizePuts(CI,
Builder);
3298 case LibFunc_perror:
3299 return optimizeErrorReporting(CI,
Builder);
3300 case LibFunc_vfprintf:
3301 case LibFunc_fiprintf:
3302 return optimizeErrorReporting(CI,
Builder, 0);
3316 : FortifiedSimplifier(TLI),
DL(
DL), TLI(TLI), ORE(ORE),
BFI(
BFI), PSI(PSI),
3317 Replacer(Replacer), Eraser(Eraser) {}
3324 void LibCallSimplifier::eraseFromParent(
Instruction *
I) {
3364 FortifiedLibCallSimplifier::isFortifiedCallFoldable(
CallInst *CI,
3382 if (ObjSizeCI->isMinusOne())
3385 if (OnlyLowerUnknownSize)
3395 return ObjSizeCI->getZExtValue() >= Len;
3401 return ObjSizeCI->getZExtValue() >= SizeCI->getZExtValue();
3407 Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(
CallInst *CI,
3409 if (isFortifiedCallFoldable(CI, 3, 2)) {
3421 Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(
CallInst *CI,
3423 if (isFortifiedCallFoldable(CI, 3, 2)) {
3435 Value *FortifiedLibCallSimplifier::optimizeMemSetChk(
CallInst *CI,
3437 if (isFortifiedCallFoldable(CI, 3, 2)) {
3449 Value *FortifiedLibCallSimplifier::optimizeMemPCpyChk(
CallInst *CI,
3452 if (isFortifiedCallFoldable(CI, 3, 2))
3455 CallInst *NewCI = cast<CallInst>(Call);
3463 Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(
CallInst *CI,
3471 if (Func == LibFunc_stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) {
3473 return StrLen ?
B.CreateInBoundsGEP(
B.getInt8Ty(), Dst, StrLen) :
nullptr;
3481 if (isFortifiedCallFoldable(CI, 2,
None, 1)) {
3482 if (Func == LibFunc_strcpy_chk)
3488 if (OnlyLowerUnknownSize)
3507 if (
Ret && Func == LibFunc_stpcpy_chk)
3512 Value *FortifiedLibCallSimplifier::optimizeStrLenChk(
CallInst *CI,
3514 if (isFortifiedCallFoldable(CI, 1,
None, 0))
3520 Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(
CallInst *CI,
3523 if (isFortifiedCallFoldable(CI, 3, 2)) {
3524 if (Func == LibFunc_strncpy_chk)
3537 Value *FortifiedLibCallSimplifier::optimizeMemCCpyChk(
CallInst *CI,
3539 if (isFortifiedCallFoldable(CI, 4, 3))
3547 Value *FortifiedLibCallSimplifier::optimizeSNPrintfChk(
CallInst *CI,
3549 if (isFortifiedCallFoldable(CI, 3, 1,
None, 2)) {
3559 Value *FortifiedLibCallSimplifier::optimizeSPrintfChk(
CallInst *CI,
3561 if (isFortifiedCallFoldable(CI, 2,
None,
None, 1)) {
3565 VariadicArgs,
B, TLI));
3571 Value *FortifiedLibCallSimplifier::optimizeStrCatChk(
CallInst *CI,
3573 if (isFortifiedCallFoldable(CI, 2))
3580 Value *FortifiedLibCallSimplifier::optimizeStrLCat(
CallInst *CI,
3582 if (isFortifiedCallFoldable(CI, 3))
3590 Value *FortifiedLibCallSimplifier::optimizeStrNCatChk(
CallInst *CI,
3592 if (isFortifiedCallFoldable(CI, 3))
3600 Value *FortifiedLibCallSimplifier::optimizeStrLCpyChk(
CallInst *CI,
3602 if (isFortifiedCallFoldable(CI, 3))
3610 Value *FortifiedLibCallSimplifier::optimizeVSNPrintfChk(
CallInst *CI,
3612 if (isFortifiedCallFoldable(CI, 3, 1,
None, 2))
3620 Value *FortifiedLibCallSimplifier::optimizeVSPrintfChk(
CallInst *CI,
3622 if (isFortifiedCallFoldable(CI, 2,
None,
None, 1))
3653 Builder.setDefaultOperandBundles(OpBundles);
3665 case LibFunc_memcpy_chk:
3666 return optimizeMemCpyChk(CI,
Builder);
3667 case LibFunc_mempcpy_chk:
3668 return optimizeMemPCpyChk(CI,
Builder);
3669 case LibFunc_memmove_chk:
3670 return optimizeMemMoveChk(CI,
Builder);
3671 case LibFunc_memset_chk:
3672 return optimizeMemSetChk(CI,
Builder);
3673 case LibFunc_stpcpy_chk:
3674 case LibFunc_strcpy_chk:
3675 return optimizeStrpCpyChk(CI,
Builder, Func);
3676 case LibFunc_strlen_chk:
3677 return optimizeStrLenChk(CI,
Builder);
3678 case LibFunc_stpncpy_chk:
3679 case LibFunc_strncpy_chk:
3680 return optimizeStrpNCpyChk(CI,
Builder, Func);
3681 case LibFunc_memccpy_chk:
3682 return optimizeMemCCpyChk(CI,
Builder);
3683 case LibFunc_snprintf_chk:
3684 return optimizeSNPrintfChk(CI,
Builder);
3685 case LibFunc_sprintf_chk:
3686 return optimizeSPrintfChk(CI,
Builder);
3687 case LibFunc_strcat_chk:
3688 return optimizeStrCatChk(CI,
Builder);
3689 case LibFunc_strlcat_chk:
3690 return optimizeStrLCat(CI,
Builder);
3691 case LibFunc_strncat_chk:
3692 return optimizeStrNCatChk(CI,
Builder);
3693 case LibFunc_strlcpy_chk:
3694 return optimizeStrLCpyChk(CI,
Builder);
3695 case LibFunc_vsnprintf_chk:
3696 return optimizeVSNPrintfChk(CI,
Builder);
3697 case LibFunc_vsprintf_chk:
3698 return optimizeVSPrintfChk(CI,
Builder);
3707 : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}
LLVM_NODISCARD char back() const
back - Get the last character in the string.
float convertToFloat() const
Converts this APFloat to host float value.
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.".
This is an optimization pass for GlobalISel generic memory operations.
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...
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)
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 * emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, StringRef Name, IRBuilderBase &B, const AttributeList &Attrs)
Emit a call to the unary function named 'Name' (e.g.
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.
uint64_t getElementAsInteger(unsigned i) const
If this is a sequential container of integers (of any size), return the specified element in the low ...
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.
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)
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
AttributeList getAttributes() const
Return the parameter attributes for this call.
bool hasApproxFunc() const
Determine whether the approximate-math-functions flag is set.
static bool callHasFloatingPointArgument(const CallInst *CI)
uint64_t Length
Length of the slice.
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
Holds functions to get, set or test bitfields.
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
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)
bool isNonNegative() const
Returns true if this value is known to be non-negative.
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.
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 or the function signa...
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")
void removeRetAttrs(const AttributeMask &AttrsToRemove)
Removes the attributes from the return value.
Value * emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcpy function.
static Value * getIntToFPVal(Value *I2F, IRBuilderBase &B, unsigned DstWidth)
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
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
Converts this APFloat to host double value.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
LLVM_NODISCARD bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
void addFnAttr(Attribute::AttrKind Kind)
Adds the attribute to the function.
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.
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.
uint64_t getParamDereferenceableBytes(unsigned i) const
Extract the number of dereferenceable bytes for a call or parameter (0=unknown).
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.
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
unsigned getIntegerBitWidth() const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
static Value * optimizeBinaryDoubleFP(CallInst *CI, IRBuilderBase &B, const TargetLibraryInfo *TLI, bool isPrecise=false)
Shrink double -> float for binary functions.
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...
static bool insertSinCosCall(IRBuilderBase &B, Function *OrigCallee, Value *Arg, bool UseFloat, Value *&Sin, Value *&Cos, Value *&SinCos, const TargetLibraryInfo *TLI)
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)
uint64_t getParamDereferenceableOrNullBytes(unsigned i) const
Extract the number of dereferenceable_or_null bytes for a parameter (0=unknown).
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
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.
constexpr LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
bool doesNotAccessMemory(unsigned OpNo) const
This instruction compares its operands according to the predicate given to the constructor.
static Value * optimizeDoubleFP(CallInst *CI, IRBuilderBase &B, bool isBinary, const TargetLibraryInfo *TLI, bool isPrecise=false)
Shrink double -> float functions.
Module * getParent()
Get the module that this global value is contained inside of...
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.
bool hasFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Check whether the overloaded floating point function corresponding to Ty is available.
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)
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
FunctionCallee getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI, LibFunc TheLibFunc, FunctionType *T, AttributeList AttributeList)
Calls getOrInsertFunction() and then makes sure to add mandatory argument attributes.
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(<
StandardInstrumentations SI(Debug, VerifyEach)
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.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Class for arbitrary precision integers.
static Value * optimizeUnaryDoubleFP(CallInst *CI, IRBuilderBase &B, const TargetLibraryInfo *TLI, bool isPrecise=false)
Shrink double -> float for unary functions.
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:
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.
@ 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.
bool isNoTailCall() const
LLVMContext & getContext() const
All values hold a context through their type.
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.
bool isMustTailCall() const
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 ...
static Value * copyFlags(const CallInst &Old, Value *New)
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.
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...
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
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'.
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
bool isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI, LibFunc TheLibFunc)
Check whether the library function is available on target and also that it in the current Module is a...
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.
unsigned arg_size() const
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.
AttributeMask typeIncompatible(Type *Ty, AttributeSafetyKind ASK=ASK_ALL)
Which attributes cannot be applied to a type.
static double log2(double V)
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)
Value * emitBinaryFloatFnCall(Value *Op1, Value *Op2, const TargetLibraryInfo *TLI, StringRef Name, IRBuilderBase &B, const AttributeList &Attrs)
Emit a call to the binary function named 'Name' (e.g.
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).
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.
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
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
MaybeAlign getAlignment() 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)
static Value * getPow(Value *InnerChain[33], unsigned Exp, IRBuilderBase &B)
AttributeSet getParamAttrs(unsigned ArgNo) const
The attributes for the argument or parameter at the given index are returned.
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()
TailCallKind getTailCallKind() const
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.
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align 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.