diff options
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 422 |
1 files changed, 184 insertions, 238 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp index fa3a9d21f3dfb..cfcc3454a2102 100644 --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -138,28 +138,6 @@ static Value *convertStrToNumber(CallInst *CI, StringRef &Str, int64_t Base) { return ConstantInt::get(CI->getType(), Result); } -static bool isLocallyOpenedFile(Value *File, CallInst *CI, IRBuilder<> &B, - const TargetLibraryInfo *TLI) { - CallInst *FOpen = dyn_cast<CallInst>(File); - if (!FOpen) - return false; - - Function *InnerCallee = FOpen->getCalledFunction(); - if (!InnerCallee) - return false; - - LibFunc Func; - if (!TLI->getLibFunc(*InnerCallee, Func) || !TLI->has(Func) || - Func != LibFunc_fopen) - return false; - - inferLibFuncAttributes(*CI->getCalledFunction(), *TLI); - if (PointerMayBeCaptured(File, true, true)) - return false; - - return true; -} - static bool isOnlyUsedInComparisonWithZero(Value *V) { for (User *U : V->users()) { if (ICmpInst *IC = dyn_cast<ICmpInst>(U)) @@ -177,8 +155,7 @@ static bool canTransformToMemCmp(CallInst *CI, Value *Str, uint64_t Len, if (!isOnlyUsedInComparisonWithZero(CI)) return false; - if (!isDereferenceableAndAlignedPointer(Str, Align::None(), APInt(64, Len), - DL)) + if (!isDereferenceableAndAlignedPointer(Str, Align(1), APInt(64, Len), DL)) return false; if (CI->getFunction()->hasFnAttribute(Attribute::SanitizeMemory)) @@ -252,7 +229,7 @@ static void annotateNonNullAndDereferenceable(CallInst *CI, ArrayRef<unsigned> A // String and Memory Library Call Optimizations //===----------------------------------------------------------------------===// -Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilderBase &B) { // Extract some information from the instruction Value *Dst = CI->getArgOperand(0); Value *Src = CI->getArgOperand(1); @@ -274,7 +251,7 @@ Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilder<> &B) { } Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, - IRBuilder<> &B) { + IRBuilderBase &B) { // We need to find the end of the destination string. That's where the // memory is to be moved to. We just generate a call to strlen. Value *DstLen = emitStrLen(Dst, B, DL, TLI); @@ -289,12 +266,12 @@ Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, // We have enough information to now generate the memcpy call to do the // concatenation for us. Make a memcpy to copy the nul byte with align = 1. B.CreateMemCpy( - CpyDst, Align::None(), Src, Align::None(), + CpyDst, Align(1), Src, Align(1), ConstantInt::get(DL.getIntPtrType(Src->getContext()), Len + 1)); return Dst; } -Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilderBase &B) { // Extract some information from the instruction. Value *Dst = CI->getArgOperand(0); Value *Src = CI->getArgOperand(1); @@ -337,7 +314,7 @@ Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilder<> &B) { return emitStrLenMemCpy(Src, Dst, SrcLen, B); } -Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilderBase &B) { Function *Callee = CI->getCalledFunction(); FunctionType *FT = Callee->getFunctionType(); Value *SrcStr = CI->getArgOperand(0); @@ -382,7 +359,7 @@ Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilder<> &B) { return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strchr"); } -Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilderBase &B) { Value *SrcStr = CI->getArgOperand(0); ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1)); annotateNonNullBasedOnAccess(CI, 0); @@ -410,7 +387,7 @@ Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilder<> &B) { return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strrchr"); } -Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilderBase &B) { Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1); if (Str1P == Str2P) // strcmp(x,x) -> 0 return ConstantInt::get(CI->getType(), 0); @@ -465,7 +442,7 @@ Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilderBase &B) { Value *Str1P = CI->getArgOperand(0); Value *Str2P = CI->getArgOperand(1); Value *Size = CI->getArgOperand(2); @@ -533,7 +510,7 @@ Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeStrNDup(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrNDup(CallInst *CI, IRBuilderBase &B) { Value *Src = CI->getArgOperand(0); ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1)); uint64_t SrcLen = GetStringLength(Src); @@ -546,7 +523,7 @@ Value *LibCallSimplifier::optimizeStrNDup(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilderBase &B) { Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); if (Dst == Src) // strcpy(x,x) -> x return Src; @@ -562,13 +539,13 @@ Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilder<> &B) { // We have enough information to now generate the memcpy call to do the // copy for us. Make a memcpy to copy the nul byte with align = 1. CallInst *NewCI = - B.CreateMemCpy(Dst, Align::None(), Src, Align::None(), + B.CreateMemCpy(Dst, Align(1), Src, Align(1), ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len)); NewCI->setAttributes(CI->getAttributes()); return Dst; } -Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilderBase &B) { Function *Callee = CI->getCalledFunction(); Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x) @@ -590,13 +567,12 @@ Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilder<> &B) { // We have enough information to now generate the memcpy call to do the // copy for us. Make a memcpy to copy the nul byte with align = 1. - CallInst *NewCI = - B.CreateMemCpy(Dst, Align::None(), Src, Align::None(), LenV); + CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1), LenV); NewCI->setAttributes(CI->getAttributes()); return DstEnd; } -Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilderBase &B) { Function *Callee = CI->getCalledFunction(); Value *Dst = CI->getArgOperand(0); Value *Src = CI->getArgOperand(1); @@ -626,7 +602,7 @@ Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilder<> &B) { if (SrcLen == 0) { // strncpy(x, "", y) -> memset(align 1 x, '\0', y) - CallInst *NewCI = B.CreateMemSet(Dst, B.getInt8('\0'), Size, Align::None()); + CallInst *NewCI = B.CreateMemSet(Dst, B.getInt8('\0'), Size, Align(1)); AttrBuilder ArgAttrs(CI->getAttributes().getParamAttributes(0)); NewCI->setAttributes(NewCI->getAttributes().addParamAttributes( CI->getContext(), 0, ArgAttrs)); @@ -639,13 +615,13 @@ Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilder<> &B) { Type *PT = Callee->getFunctionType()->getParamType(0); // strncpy(x, s, c) -> memcpy(align 1 x, align 1 s, c) [s and c are constant] - CallInst *NewCI = B.CreateMemCpy(Dst, Align::None(), Src, Align::None(), + CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1), ConstantInt::get(DL.getIntPtrType(PT), Len)); NewCI->setAttributes(CI->getAttributes()); return Dst; } -Value *LibCallSimplifier::optimizeStringLength(CallInst *CI, IRBuilder<> &B, +Value *LibCallSimplifier::optimizeStringLength(CallInst *CI, IRBuilderBase &B, unsigned CharSize) { Value *Src = CI->getArgOperand(0); @@ -736,14 +712,14 @@ Value *LibCallSimplifier::optimizeStringLength(CallInst *CI, IRBuilder<> &B, return nullptr; } -Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilderBase &B) { if (Value *V = optimizeStringLength(CI, B, 8)) return V; annotateNonNullBasedOnAccess(CI, 0); return nullptr; } -Value *LibCallSimplifier::optimizeWcslen(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeWcslen(CallInst *CI, IRBuilderBase &B) { Module &M = *CI->getModule(); unsigned WCharSize = TLI->getWCharSize(M) * 8; // We cannot perform this optimization without wchar_size metadata. @@ -753,7 +729,7 @@ Value *LibCallSimplifier::optimizeWcslen(CallInst *CI, IRBuilder<> &B) { return optimizeStringLength(CI, B, WCharSize); } -Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilderBase &B) { StringRef S1, S2; bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); @@ -780,7 +756,7 @@ Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilderBase &B) { Value *EndPtr = CI->getArgOperand(1); if (isa<ConstantPointerNull>(EndPtr)) { // With a null EndPtr, this function won't capture the main argument. @@ -791,7 +767,7 @@ Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilderBase &B) { StringRef S1, S2; bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); @@ -812,7 +788,7 @@ Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilderBase &B) { StringRef S1, S2; bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); @@ -836,7 +812,7 @@ Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilderBase &B) { // fold strstr(x, x) -> x. if (CI->getArgOperand(0) == CI->getArgOperand(1)) return B.CreateBitCast(CI->getArgOperand(0), CI->getType()); @@ -893,13 +869,13 @@ Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeMemRChr(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeMemRChr(CallInst *CI, IRBuilderBase &B) { if (isKnownNonZero(CI->getOperand(2), DL)) annotateNonNullBasedOnAccess(CI, 0); return nullptr; } -Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) { Value *SrcStr = CI->getArgOperand(0); Value *Size = CI->getArgOperand(2); annotateNonNullAndDereferenceable(CI, 0, Size, DL); @@ -988,7 +964,7 @@ Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilder<> &B) { } static Value *optimizeMemCmpConstantSize(CallInst *CI, Value *LHS, Value *RHS, - uint64_t Len, IRBuilder<> &B, + uint64_t Len, IRBuilderBase &B, const DataLayout &DL) { if (Len == 0) // memcmp(s1,s2,0) -> 0 return Constant::getNullValue(CI->getType()); @@ -1065,7 +1041,7 @@ static Value *optimizeMemCmpConstantSize(CallInst *CI, Value *LHS, Value *RHS, // Most simplifications for memcmp also apply to bcmp. Value *LibCallSimplifier::optimizeMemCmpBCmpCommon(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1); Value *Size = CI->getArgOperand(2); @@ -1088,7 +1064,7 @@ Value *LibCallSimplifier::optimizeMemCmpBCmpCommon(CallInst *CI, return nullptr; } -Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilderBase &B) { if (Value *V = optimizeMemCmpBCmpCommon(CI, B)) return V; @@ -1105,24 +1081,24 @@ Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeBCmp(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeBCmp(CallInst *CI, IRBuilderBase &B) { return optimizeMemCmpBCmpCommon(CI, B); } -Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilderBase &B) { Value *Size = CI->getArgOperand(2); annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL); if (isa<IntrinsicInst>(CI)) return nullptr; // memcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n) - CallInst *NewCI = B.CreateMemCpy(CI->getArgOperand(0), Align::None(), - CI->getArgOperand(1), Align::None(), Size); + CallInst *NewCI = B.CreateMemCpy(CI->getArgOperand(0), Align(1), + CI->getArgOperand(1), Align(1), Size); NewCI->setAttributes(CI->getAttributes()); return CI->getArgOperand(0); } -Value *LibCallSimplifier::optimizeMemCCpy(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeMemCCpy(CallInst *CI, IRBuilderBase &B) { Value *Dst = CI->getArgOperand(0); Value *Src = CI->getArgOperand(1); ConstantInt *StopChar = dyn_cast<ConstantInt>(CI->getArgOperand(2)); @@ -1146,8 +1122,7 @@ Value *LibCallSimplifier::optimizeMemCCpy(CallInst *CI, IRBuilder<> &B) { size_t Pos = SrcStr.find(StopChar->getSExtValue() & 0xFF); if (Pos == StringRef::npos) { if (N->getZExtValue() <= SrcStr.size()) { - B.CreateMemCpy(Dst, Align::None(), Src, Align::None(), - CI->getArgOperand(3)); + B.CreateMemCpy(Dst, Align(1), Src, Align(1), CI->getArgOperand(3)); return Constant::getNullValue(CI->getType()); } return nullptr; @@ -1156,37 +1131,37 @@ Value *LibCallSimplifier::optimizeMemCCpy(CallInst *CI, IRBuilder<> &B) { Value *NewN = ConstantInt::get(N->getType(), std::min(uint64_t(Pos + 1), N->getZExtValue())); // memccpy -> llvm.memcpy - B.CreateMemCpy(Dst, Align::None(), Src, Align::None(), NewN); + B.CreateMemCpy(Dst, Align(1), Src, Align(1), NewN); return Pos + 1 <= N->getZExtValue() ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, NewN) : Constant::getNullValue(CI->getType()); } -Value *LibCallSimplifier::optimizeMemPCpy(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeMemPCpy(CallInst *CI, IRBuilderBase &B) { Value *Dst = CI->getArgOperand(0); Value *N = CI->getArgOperand(2); // mempcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n), x + n - CallInst *NewCI = B.CreateMemCpy(Dst, Align::None(), CI->getArgOperand(1), - Align::None(), N); + CallInst *NewCI = + B.CreateMemCpy(Dst, Align(1), CI->getArgOperand(1), Align(1), N); NewCI->setAttributes(CI->getAttributes()); return B.CreateInBoundsGEP(B.getInt8Ty(), Dst, N); } -Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilderBase &B) { Value *Size = CI->getArgOperand(2); annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL); if (isa<IntrinsicInst>(CI)) return nullptr; // memmove(x, y, n) -> llvm.memmove(align 1 x, align 1 y, n) - CallInst *NewCI = B.CreateMemMove(CI->getArgOperand(0), Align::None(), - CI->getArgOperand(1), Align::None(), Size); + CallInst *NewCI = B.CreateMemMove(CI->getArgOperand(0), Align(1), + CI->getArgOperand(1), Align(1), Size); NewCI->setAttributes(CI->getAttributes()); return CI->getArgOperand(0); } /// Fold memset[_chk](malloc(n), 0, n) --> calloc(1, n). -Value *LibCallSimplifier::foldMallocMemset(CallInst *Memset, IRBuilder<> &B) { +Value *LibCallSimplifier::foldMallocMemset(CallInst *Memset, IRBuilderBase &B) { // This has to be a memset of zeros (bzero). auto *FillValue = dyn_cast<ConstantInt>(Memset->getArgOperand(1)); if (!FillValue || FillValue->getZExtValue() != 0) @@ -1229,7 +1204,7 @@ Value *LibCallSimplifier::foldMallocMemset(CallInst *Memset, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilderBase &B) { Value *Size = CI->getArgOperand(2); annotateNonNullAndDereferenceable(CI, 0, Size, DL); if (isa<IntrinsicInst>(CI)) @@ -1240,13 +1215,12 @@ Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B) { // memset(p, v, n) -> llvm.memset(align 1 p, v, n) Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false); - CallInst *NewCI = - B.CreateMemSet(CI->getArgOperand(0), Val, Size, Align::None()); + CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val, Size, Align(1)); NewCI->setAttributes(CI->getAttributes()); return CI->getArgOperand(0); } -Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilderBase &B) { if (isa<ConstantPointerNull>(CI->getArgOperand(0))) return emitMalloc(CI->getArgOperand(1), B, DL, TLI); @@ -1258,9 +1232,10 @@ Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilder<> &B) { //===----------------------------------------------------------------------===// // Replace a libcall \p CI with a call to intrinsic \p IID -static Value *replaceUnaryCall(CallInst *CI, IRBuilder<> &B, Intrinsic::ID IID) { +static Value *replaceUnaryCall(CallInst *CI, IRBuilderBase &B, + Intrinsic::ID IID) { // Propagate fast-math flags from the existing call to the new call. - IRBuilder<>::FastMathFlagGuard Guard(B); + IRBuilderBase::FastMathFlagGuard Guard(B); B.setFastMathFlags(CI->getFastMathFlags()); Module *M = CI->getModule(); @@ -1294,7 +1269,7 @@ static Value *valueHasFloatPrecision(Value *Val) { } /// Shrink double -> float functions. -static Value *optimizeDoubleFP(CallInst *CI, IRBuilder<> &B, +static Value *optimizeDoubleFP(CallInst *CI, IRBuilderBase &B, bool isBinary, bool isPrecise = false) { Function *CalleeFn = CI->getCalledFunction(); if (!CI->getType()->isDoubleTy() || !CalleeFn) @@ -1333,7 +1308,7 @@ static Value *optimizeDoubleFP(CallInst *CI, IRBuilder<> &B, } // Propagate the math semantics from the current function to the new function. - IRBuilder<>::FastMathFlagGuard Guard(B); + IRBuilderBase::FastMathFlagGuard Guard(B); B.setFastMathFlags(CI->getFastMathFlags()); // g((double) float) -> (double) gf(float) @@ -1352,24 +1327,24 @@ static Value *optimizeDoubleFP(CallInst *CI, IRBuilder<> &B, } /// Shrink double -> float for unary functions. -static Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B, +static Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilderBase &B, bool isPrecise = false) { return optimizeDoubleFP(CI, B, false, isPrecise); } /// Shrink double -> float for binary functions. -static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B, +static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilderBase &B, bool isPrecise = false) { return optimizeDoubleFP(CI, B, true, isPrecise); } // cabs(z) -> sqrt((creal(z)*creal(z)) + (cimag(z)*cimag(z))) -Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilderBase &B) { if (!CI->isFast()) return nullptr; // Propagate fast-math flags from the existing call to new instructions. - IRBuilder<>::FastMathFlagGuard Guard(B); + IRBuilderBase::FastMathFlagGuard Guard(B); B.setFastMathFlags(CI->getFastMathFlags()); Value *Real, *Imag; @@ -1393,11 +1368,11 @@ Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilder<> &B) { } static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func, - IRBuilder<> &B) { + IRBuilderBase &B) { if (!isa<FPMathOperator>(Call)) return nullptr; - IRBuilder<>::FastMathFlagGuard Guard(B); + IRBuilderBase::FastMathFlagGuard Guard(B); B.setFastMathFlags(Call->getFastMathFlags()); // TODO: Can this be shared to also handle LLVM intrinsics? @@ -1427,7 +1402,7 @@ static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func, return nullptr; } -static Value *getPow(Value *InnerChain[33], unsigned Exp, IRBuilder<> &B) { +static Value *getPow(Value *InnerChain[33], unsigned Exp, IRBuilderBase &B) { // Multiplications calculated using Addition Chains. // Refer: http://wwwhomes.uni-bielefeld.de/achim/addition_chain.html @@ -1453,7 +1428,7 @@ static Value *getPow(Value *InnerChain[33], unsigned Exp, IRBuilder<> &B) { } // Return a properly extended 32-bit integer if the operation is an itofp. -static Value *getIntToFPVal(Value *I2F, IRBuilder<> &B) { +static Value *getIntToFPVal(Value *I2F, IRBuilderBase &B) { if (isa<SIToFPInst>(I2F) || isa<UIToFPInst>(I2F)) { Value *Op = cast<Instruction>(I2F)->getOperand(0); // Make sure that the exponent fits inside an int32_t, @@ -1471,9 +1446,9 @@ static Value *getIntToFPVal(Value *I2F, IRBuilder<> &B) { /// Use exp{,2}(x * y) for pow(exp{,2}(x), y); /// ldexp(1.0, x) for pow(2.0, itofp(x)); exp2(n * x) for pow(2.0 ** n, x); /// exp10(x) for pow(10.0, x); exp2(log2(n) * x) for pow(n, x). -Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilder<> &B) { +Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) { Value *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1); - AttributeList Attrs = Pow->getCalledFunction()->getAttributes(); + AttributeList Attrs; // Attributes are only meaningful on the original call Module *Mod = Pow->getModule(); Type *Ty = Pow->getType(); bool Ignored; @@ -1588,9 +1563,14 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilder<> &B) { return emitUnaryFloatFnCall(Expo, TLI, LibFunc_exp10, LibFunc_exp10f, LibFunc_exp10l, B, Attrs); - // pow(n, x) -> exp2(log2(n) * x) - if (Pow->hasOneUse() && Pow->hasApproxFunc() && Pow->hasNoNaNs() && - Pow->hasNoInfs() && BaseF->isNormal() && !BaseF->isNegative()) { + // pow(x, y) -> exp2(log2(x) * y) + if (Pow->hasApproxFunc() && Pow->hasNoNaNs() && BaseF->isFiniteNonZero() && + !BaseF->isNegative()) { + // pow(1, inf) is defined to be 1 but exp2(log2(1) * inf) evaluates to NaN. + // Luckily optimizePow has already handled the x == 1 case. + assert(!match(Base, m_FPOne()) && + "pow(1.0, y) should have been simplified earlier!"); + Value *Log = nullptr; if (Ty->isFloatTy()) Log = ConstantFP::get(Ty, std::log2(BaseF->convertToFloat())); @@ -1612,7 +1592,7 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilder<> &B) { } static Value *getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno, - Module *M, IRBuilder<> &B, + Module *M, IRBuilderBase &B, const TargetLibraryInfo *TLI) { // If errno is never set, then use the intrinsic for sqrt(). if (NoErrno) { @@ -1633,9 +1613,9 @@ static Value *getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno, } /// Use square root in place of pow(x, +/-0.5). -Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilder<> &B) { +Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) { Value *Sqrt, *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1); - AttributeList Attrs = Pow->getCalledFunction()->getAttributes(); + AttributeList Attrs; // Attributes are only meaningful on the original call Module *Mod = Pow->getModule(); Type *Ty = Pow->getType(); @@ -1676,13 +1656,13 @@ Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilder<> &B) { } static Value *createPowWithIntegerExponent(Value *Base, Value *Expo, Module *M, - IRBuilder<> &B) { + IRBuilderBase &B) { Value *Args[] = {Base, Expo}; Function *F = Intrinsic::getDeclaration(M, Intrinsic::powi, Base->getType()); return B.CreateCall(F, Args); } -Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) { Value *Base = Pow->getArgOperand(0); Value *Expo = Pow->getArgOperand(1); Function *Callee = Pow->getCalledFunction(); @@ -1693,12 +1673,8 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilder<> &B) { bool AllowApprox = Pow->hasApproxFunc(); bool Ignored; - // Bail out if simplifying libcalls to pow() is disabled. - if (!hasFloatFn(TLI, Ty, LibFunc_pow, LibFunc_powf, LibFunc_powl)) - return nullptr; - // Propagate the math semantics from the call to any created instructions. - IRBuilder<>::FastMathFlagGuard Guard(B); + IRBuilderBase::FastMathFlagGuard Guard(B); B.setFastMathFlags(Pow->getFastMathFlags()); // Shrink pow() to powf() if the arguments are single precision, @@ -1748,7 +1724,7 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilder<> &B) { // be different) and it should also consider optimizing for size. APFloat LimF(ExpoF->getSemantics(), 33), ExpoA(abs(*ExpoF)); - if (ExpoA.compare(LimF) == APFloat::cmpLessThan) { + if (ExpoA < LimF) { // This transformation applies to integer or integer+0.5 exponents only. // For integer+0.5, we create a sqrt(Base) call. Value *Sqrt = nullptr; @@ -1807,8 +1783,9 @@ Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilder<> &B) { return Shrunk; } -Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) { Function *Callee = CI->getCalledFunction(); + AttributeList Attrs; // Attributes are only meaningful on the original call StringRef Name = Callee->getName(); Value *Ret = nullptr; if (UnsafeFPShrink && Name == TLI->getName(LibFunc_exp2) && @@ -1825,13 +1802,13 @@ Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilder<> &B) { if (Value *Exp = getIntToFPVal(Op, B)) return emitBinaryFloatFnCall(ConstantFP::get(Ty, 1.0), Exp, TLI, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl, - B, CI->getCalledFunction()->getAttributes()); + B, Attrs); } return Ret; } -Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) { // If we can shrink the call to a float function rather than a double // function, do that first. Function *Callee = CI->getCalledFunction(); @@ -1847,7 +1824,7 @@ Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilder<> &B) { // "Ideally, fmax would be sensitive to the sign of zero, for example // fmax(-0.0, +0.0) would return +0; however, implementation in software // might be impractical." - IRBuilder<>::FastMathFlagGuard Guard(B); + IRBuilderBase::FastMathFlagGuard Guard(B); FastMathFlags FMF = CI->getFastMathFlags(); FMF.setNoSignedZeros(); B.setFastMathFlags(FMF); @@ -1858,9 +1835,9 @@ Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilder<> &B) { return B.CreateCall(F, { CI->getArgOperand(0), CI->getArgOperand(1) }); } -Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) { Function *LogFn = Log->getCalledFunction(); - AttributeList Attrs = LogFn->getAttributes(); + AttributeList Attrs; // Attributes are only meaningful on the original call StringRef LogNm = LogFn->getName(); Intrinsic::ID LogID = LogFn->getIntrinsicID(); Module *Mod = Log->getModule(); @@ -1963,12 +1940,12 @@ Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilder<> &B) { } else return Ret; - IRBuilder<>::FastMathFlagGuard Guard(B); + IRBuilderBase::FastMathFlagGuard Guard(B); B.setFastMathFlags(FastMathFlags::getFast()); Intrinsic::ID ArgID = Arg->getIntrinsicID(); LibFunc ArgLb = NotLibFunc; - TLI->getLibFunc(Arg, ArgLb); + TLI->getLibFunc(*Arg, ArgLb); // log(pow(x,y)) -> y*log(x) if (ArgLb == PowLb || ArgID == Intrinsic::pow) { @@ -2010,7 +1987,7 @@ Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilder<> &B) { return Ret; } -Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) { Function *Callee = CI->getCalledFunction(); Value *Ret = nullptr; // TODO: Once we have a way (other than checking for the existince of the @@ -2058,7 +2035,7 @@ Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilder<> &B) { // Fast math flags for any created instructions should match the sqrt // and multiply. - IRBuilder<>::FastMathFlagGuard Guard(B); + IRBuilderBase::FastMathFlagGuard Guard(B); B.setFastMathFlags(I->getFastMathFlags()); // If we found a repeated factor, hoist it out of the square root and @@ -2079,7 +2056,7 @@ Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilder<> &B) { } // TODO: Generalize to handle any trig function and its inverse. -Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilderBase &B) { Function *Callee = CI->getCalledFunction(); Value *Ret = nullptr; StringRef Name = Callee->getName(); @@ -2116,7 +2093,7 @@ static bool isTrigLibCall(CallInst *CI) { CI->hasFnAttr(Attribute::ReadNone); } -static void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg, +static void insertSinCosCall(IRBuilderBase &B, Function *OrigCallee, Value *Arg, bool UseFloat, Value *&Sin, Value *&Cos, Value *&SinCos) { Type *ArgTy = Arg->getType(); @@ -2131,7 +2108,7 @@ static void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg, // x86_64 can't use {float, float} since that would be returned in both // xmm0 and xmm1, which isn't what a real struct would do. ResTy = T.getArch() == Triple::x86_64 - ? static_cast<Type *>(VectorType::get(ArgTy, 2)) + ? static_cast<Type *>(FixedVectorType::get(ArgTy, 2)) : static_cast<Type *>(StructType::get(ArgTy, ArgTy)); } else { Name = "__sincospi_stret"; @@ -2166,7 +2143,7 @@ static void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg, } } -Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, IRBuilderBase &B) { // Make sure the prototype is as expected, otherwise the rest of the // function is probably invalid and likely to abort. if (!isTrigLibCall(CI)) @@ -2247,7 +2224,7 @@ void LibCallSimplifier::classifyArgUse( // Integer Library Call Optimizations //===----------------------------------------------------------------------===// -Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilderBase &B) { // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0 Value *Op = CI->getArgOperand(0); Type *ArgType = Op->getType(); @@ -2261,7 +2238,7 @@ Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilder<> &B) { return B.CreateSelect(Cond, V, B.getInt32(0)); } -Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilderBase &B) { // fls(x) -> (i32)(sizeInBits(x) - llvm.ctlz(x, false)) Value *Op = CI->getArgOperand(0); Type *ArgType = Op->getType(); @@ -2273,7 +2250,7 @@ Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilder<> &B) { return B.CreateIntCast(V, CI->getType(), false); } -Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilderBase &B) { // abs(x) -> x <s 0 ? -x : x // The negation has 'nsw' because abs of INT_MIN is undefined. Value *X = CI->getArgOperand(0); @@ -2282,7 +2259,7 @@ Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilder<> &B) { return B.CreateSelect(IsNeg, NegX, X); } -Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilderBase &B) { // isdigit(c) -> (c-'0') <u 10 Value *Op = CI->getArgOperand(0); Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp"); @@ -2290,20 +2267,20 @@ Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilder<> &B) { return B.CreateZExt(Op, CI->getType()); } -Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilderBase &B) { // isascii(c) -> c <u 128 Value *Op = CI->getArgOperand(0); Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii"); return B.CreateZExt(Op, CI->getType()); } -Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilderBase &B) { // toascii(c) -> c & 0x7f return B.CreateAnd(CI->getArgOperand(0), ConstantInt::get(CI->getType(), 0x7F)); } -Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilderBase &B) { StringRef Str; if (!getConstantStringInfo(CI->getArgOperand(0), Str)) return nullptr; @@ -2311,7 +2288,7 @@ Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilder<> &B) { return convertStrToNumber(CI, Str, 10); } -Value *LibCallSimplifier::optimizeStrtol(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeStrtol(CallInst *CI, IRBuilderBase &B) { StringRef Str; if (!getConstantStringInfo(CI->getArgOperand(0), Str)) return nullptr; @@ -2332,7 +2309,7 @@ Value *LibCallSimplifier::optimizeStrtol(CallInst *CI, IRBuilder<> &B) { static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg); -Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilder<> &B, +Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilderBase &B, int StreamArg) { Function *Callee = CI->getCalledFunction(); // Error reporting calls should be cold, mark them as such. @@ -2372,7 +2349,7 @@ static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) { return GV->getName() == "stderr"; } -Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) { // Check for a fixed format string. StringRef FormatStr; if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr)) @@ -2425,7 +2402,7 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilderBase &B) { Function *Callee = CI->getCalledFunction(); FunctionType *FT = Callee->getFunctionType(); @@ -2462,7 +2439,8 @@ Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, + IRBuilderBase &B) { // Check for a fixed format string. StringRef FormatStr; if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr)) @@ -2477,8 +2455,7 @@ Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) { // sprintf(str, fmt) -> llvm.memcpy(align 1 str, align 1 fmt, strlen(fmt)+1) B.CreateMemCpy( - CI->getArgOperand(0), Align::None(), CI->getArgOperand(1), - Align::None(), + CI->getArgOperand(0), Align(1), CI->getArgOperand(1), Align(1), ConstantInt::get(DL.getIntPtrType(CI->getContext()), FormatStr.size() + 1)); // Copy the null byte. return ConstantInt::get(CI->getType(), FormatStr.size()); @@ -2515,8 +2492,8 @@ Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) { return nullptr; Value *IncLen = B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc"); - B.CreateMemCpy(CI->getArgOperand(0), Align::None(), CI->getArgOperand(2), - Align::None(), IncLen); + B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(2), + Align(1), IncLen); // The sprintf result is the unincremented number of bytes in the string. return B.CreateIntCast(Len, CI->getType(), false); @@ -2524,7 +2501,7 @@ Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilderBase &B) { Function *Callee = CI->getCalledFunction(); FunctionType *FT = Callee->getFunctionType(); if (Value *V = optimizeSPrintFString(CI, B)) { @@ -2560,7 +2537,8 @@ Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI, + IRBuilderBase &B) { // Check for size ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1)); if (!Size) @@ -2587,8 +2565,7 @@ Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI, IRBuilder<> &B) { // snprintf(dst, size, fmt) -> llvm.memcpy(align 1 dst, align 1 fmt, // strlen(fmt)+1) B.CreateMemCpy( - CI->getArgOperand(0), Align::None(), CI->getArgOperand(2), - Align::None(), + CI->getArgOperand(0), Align(1), CI->getArgOperand(2), Align(1), ConstantInt::get(DL.getIntPtrType(CI->getContext()), FormatStr.size() + 1)); // Copy the null byte. return ConstantInt::get(CI->getType(), FormatStr.size()); @@ -2629,9 +2606,8 @@ Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI, IRBuilder<> &B) { else if (N < Str.size() + 1) return nullptr; - B.CreateMemCpy(CI->getArgOperand(0), Align::None(), CI->getArgOperand(3), - Align::None(), - ConstantInt::get(CI->getType(), Str.size() + 1)); + B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(3), + Align(1), ConstantInt::get(CI->getType(), Str.size() + 1)); // The snprintf result is the unincremented number of bytes in the string. return ConstantInt::get(CI->getType(), Str.size()); @@ -2640,7 +2616,7 @@ Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilderBase &B) { if (Value *V = optimizeSnPrintFString(CI, B)) { return V; } @@ -2650,7 +2626,8 @@ Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, + IRBuilderBase &B) { optimizeErrorReporting(CI, B, 0); // All the optimizations depend on the format string. @@ -2699,7 +2676,7 @@ Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilderBase &B) { Function *Callee = CI->getCalledFunction(); FunctionType *FT = Callee->getFunctionType(); if (Value *V = optimizeFPrintFString(CI, B)) { @@ -2734,7 +2711,7 @@ Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilderBase &B) { optimizeErrorReporting(CI, B, 3); // Get the element size and count. @@ -2757,15 +2734,10 @@ Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilder<> &B) { } } - if (isLocallyOpenedFile(CI->getArgOperand(3), CI, B, TLI)) - return emitFWriteUnlocked(CI->getArgOperand(0), CI->getArgOperand(1), - CI->getArgOperand(2), CI->getArgOperand(3), B, DL, - TLI); - return nullptr; } -Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) { optimizeErrorReporting(CI, B, 1); // Don't rewrite fputs to fwrite when optimising for size because fwrite @@ -2776,15 +2748,9 @@ Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilder<> &B) { if (OptForSize) return nullptr; - // Check if has any use - if (!CI->use_empty()) { - if (isLocallyOpenedFile(CI->getArgOperand(1), CI, B, TLI)) - return emitFPutSUnlocked(CI->getArgOperand(0), CI->getArgOperand(1), B, - TLI); - else - // We can't optimize if return value is used. - return nullptr; - } + // We can't optimize if return value is used. + if (!CI->use_empty()) + return nullptr; // fputs(s,F) --> fwrite(s,strlen(s),1,F) uint64_t Len = GetStringLength(CI->getArgOperand(0)); @@ -2798,41 +2764,7 @@ Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilder<> &B) { CI->getArgOperand(1), B, DL, TLI); } -Value *LibCallSimplifier::optimizeFPutc(CallInst *CI, IRBuilder<> &B) { - optimizeErrorReporting(CI, B, 1); - - if (isLocallyOpenedFile(CI->getArgOperand(1), CI, B, TLI)) - return emitFPutCUnlocked(CI->getArgOperand(0), CI->getArgOperand(1), B, - TLI); - - return nullptr; -} - -Value *LibCallSimplifier::optimizeFGetc(CallInst *CI, IRBuilder<> &B) { - if (isLocallyOpenedFile(CI->getArgOperand(0), CI, B, TLI)) - return emitFGetCUnlocked(CI->getArgOperand(0), B, TLI); - - return nullptr; -} - -Value *LibCallSimplifier::optimizeFGets(CallInst *CI, IRBuilder<> &B) { - if (isLocallyOpenedFile(CI->getArgOperand(2), CI, B, TLI)) - return emitFGetSUnlocked(CI->getArgOperand(0), CI->getArgOperand(1), - CI->getArgOperand(2), B, TLI); - - return nullptr; -} - -Value *LibCallSimplifier::optimizeFRead(CallInst *CI, IRBuilder<> &B) { - if (isLocallyOpenedFile(CI->getArgOperand(3), CI, B, TLI)) - return emitFReadUnlocked(CI->getArgOperand(0), CI->getArgOperand(1), - CI->getArgOperand(2), CI->getArgOperand(3), B, DL, - TLI); - - return nullptr; -} - -Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilderBase &B) { annotateNonNullBasedOnAccess(CI, 0); if (!CI->use_empty()) return nullptr; @@ -2846,11 +2778,10 @@ Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilder<> &B) { return nullptr; } -Value *LibCallSimplifier::optimizeBCopy(CallInst *CI, IRBuilder<> &B) { +Value *LibCallSimplifier::optimizeBCopy(CallInst *CI, IRBuilderBase &B) { // bcopy(src, dst, n) -> llvm.memmove(dst, src, n) - return B.CreateMemMove(CI->getArgOperand(1), Align::None(), - CI->getArgOperand(0), Align::None(), - CI->getArgOperand(2)); + return B.CreateMemMove(CI->getArgOperand(1), Align(1), CI->getArgOperand(0), + Align(1), CI->getArgOperand(2)); } bool LibCallSimplifier::hasFloatVersion(StringRef FuncName) { @@ -2863,7 +2794,7 @@ bool LibCallSimplifier::hasFloatVersion(StringRef FuncName) { } Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI, - IRBuilder<> &Builder) { + IRBuilderBase &Builder) { LibFunc Func; Function *Callee = CI->getCalledFunction(); // Check for string/memory library functions. @@ -2944,7 +2875,7 @@ Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI, Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func, - IRBuilder<> &Builder) { + IRBuilderBase &Builder) { // Don't optimize calls that require strict floating point semantics. if (CI->isStrictFP()) return nullptr; @@ -3000,6 +2931,8 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI, return replaceUnaryCall(CI, Builder, Intrinsic::floor); case LibFunc_round: return replaceUnaryCall(CI, Builder, Intrinsic::round); + case LibFunc_roundeven: + return replaceUnaryCall(CI, Builder, Intrinsic::roundeven); case LibFunc_nearbyint: return replaceUnaryCall(CI, Builder, Intrinsic::nearbyint); case LibFunc_rint: @@ -3044,7 +2977,7 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI, } } -Value *LibCallSimplifier::optimizeCall(CallInst *CI) { +Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) { // TODO: Split out the code below that operates on FP calls so that // we can all non-FP calls with the StrictFP attribute to be // optimized. @@ -3053,11 +2986,13 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) { LibFunc Func; Function *Callee = CI->getCalledFunction(); + bool isCallingConvC = isCallingConvCCompatible(CI); SmallVector<OperandBundleDef, 2> OpBundles; CI->getOperandBundlesAsDefs(OpBundles); - IRBuilder<> Builder(CI, /*FPMathTag=*/nullptr, OpBundles); - bool isCallingConvC = isCallingConvCCompatible(CI); + + IRBuilderBase::OperandBundlesGuard Guard(Builder); + Builder.setDefaultOperandBundles(OpBundles); // Command-line parameter overrides instruction attribute. // This can't be moved to optimizeFloatingPointLibCall() because it may be @@ -3097,14 +3032,20 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) { } // Also try to simplify calls to fortified library functions. - if (Value *SimplifiedFortifiedCI = FortifiedSimplifier.optimizeCall(CI)) { + if (Value *SimplifiedFortifiedCI = + FortifiedSimplifier.optimizeCall(CI, Builder)) { // Try to further simplify the result. CallInst *SimplifiedCI = dyn_cast<CallInst>(SimplifiedFortifiedCI); if (SimplifiedCI && SimplifiedCI->getCalledFunction()) { - // Use an IR Builder from SimplifiedCI if available instead of CI - // to guarantee we reach all uses we might replace later on. - IRBuilder<> TmpBuilder(SimplifiedCI); - if (Value *V = optimizeStringMemoryLibCall(SimplifiedCI, TmpBuilder)) { + // Ensure that SimplifiedCI's uses are complete, since some calls have + // their uses analyzed. + replaceAllUsesWith(CI, SimplifiedCI); + + // Set insertion point to SimplifiedCI to guarantee we reach all uses + // we might replace later on. + IRBuilderBase::InsertPointGuard Guard(Builder); + Builder.SetInsertPoint(SimplifiedCI); + if (Value *V = optimizeStringMemoryLibCall(SimplifiedCI, Builder)) { // If we were able to further simplify, remove the now redundant call. substituteInParent(SimplifiedCI, V); return V; @@ -3158,16 +3099,8 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) { return optimizeFPrintF(CI, Builder); case LibFunc_fwrite: return optimizeFWrite(CI, Builder); - case LibFunc_fread: - return optimizeFRead(CI, Builder); case LibFunc_fputs: return optimizeFPuts(CI, Builder); - case LibFunc_fgets: - return optimizeFGets(CI, Builder); - case LibFunc_fputc: - return optimizeFPutc(CI, Builder); - case LibFunc_fgetc: - return optimizeFGetc(CI, Builder); case LibFunc_puts: return optimizePuts(CI, Builder); case LibFunc_perror: @@ -3280,11 +3213,11 @@ FortifiedLibCallSimplifier::isFortifiedCallFoldable(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { if (isFortifiedCallFoldable(CI, 3, 2)) { - CallInst *NewCI = B.CreateMemCpy(CI->getArgOperand(0), Align::None(), - CI->getArgOperand(1), Align::None(), - CI->getArgOperand(2)); + CallInst *NewCI = + B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(1), + Align(1), CI->getArgOperand(2)); NewCI->setAttributes(CI->getAttributes()); return CI->getArgOperand(0); } @@ -3292,11 +3225,11 @@ Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { if (isFortifiedCallFoldable(CI, 3, 2)) { - CallInst *NewCI = B.CreateMemMove(CI->getArgOperand(0), Align::None(), - CI->getArgOperand(1), Align::None(), - CI->getArgOperand(2)); + CallInst *NewCI = + B.CreateMemMove(CI->getArgOperand(0), Align(1), CI->getArgOperand(1), + Align(1), CI->getArgOperand(2)); NewCI->setAttributes(CI->getAttributes()); return CI->getArgOperand(0); } @@ -3304,13 +3237,13 @@ Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { // TODO: Try foldMallocMemset() here. if (isFortifiedCallFoldable(CI, 3, 2)) { Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false); CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val, - CI->getArgOperand(2), Align::None()); + CI->getArgOperand(2), Align(1)); NewCI->setAttributes(CI->getAttributes()); return CI->getArgOperand(0); } @@ -3318,7 +3251,7 @@ Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI, - IRBuilder<> &B, + IRBuilderBase &B, LibFunc Func) { const DataLayout &DL = CI->getModule()->getDataLayout(); Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1), @@ -3362,8 +3295,16 @@ Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI, return Ret; } +Value *FortifiedLibCallSimplifier::optimizeStrLenChk(CallInst *CI, + IRBuilderBase &B) { + if (isFortifiedCallFoldable(CI, 1, None, 0)) + return emitStrLen(CI->getArgOperand(0), B, CI->getModule()->getDataLayout(), + TLI); + return nullptr; +} + Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI, - IRBuilder<> &B, + IRBuilderBase &B, LibFunc Func) { if (isFortifiedCallFoldable(CI, 3, 2)) { if (Func == LibFunc_strncpy_chk) @@ -3378,7 +3319,7 @@ Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeMemCCpyChk(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { if (isFortifiedCallFoldable(CI, 4, 3)) return emitMemCCpy(CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), CI->getArgOperand(3), B, TLI); @@ -3387,7 +3328,7 @@ Value *FortifiedLibCallSimplifier::optimizeMemCCpyChk(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeSNPrintfChk(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { if (isFortifiedCallFoldable(CI, 3, 1, None, 2)) { SmallVector<Value *, 8> VariadicArgs(CI->arg_begin() + 5, CI->arg_end()); return emitSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1), @@ -3398,7 +3339,7 @@ Value *FortifiedLibCallSimplifier::optimizeSNPrintfChk(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeSPrintfChk(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { if (isFortifiedCallFoldable(CI, 2, None, None, 1)) { SmallVector<Value *, 8> VariadicArgs(CI->arg_begin() + 4, CI->arg_end()); return emitSPrintf(CI->getArgOperand(0), CI->getArgOperand(3), VariadicArgs, @@ -3409,7 +3350,7 @@ Value *FortifiedLibCallSimplifier::optimizeSPrintfChk(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeStrCatChk(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { if (isFortifiedCallFoldable(CI, 2)) return emitStrCat(CI->getArgOperand(0), CI->getArgOperand(1), B, TLI); @@ -3417,7 +3358,7 @@ Value *FortifiedLibCallSimplifier::optimizeStrCatChk(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeStrLCat(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { if (isFortifiedCallFoldable(CI, 3)) return emitStrLCat(CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B, TLI); @@ -3426,7 +3367,7 @@ Value *FortifiedLibCallSimplifier::optimizeStrLCat(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeStrNCatChk(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { if (isFortifiedCallFoldable(CI, 3)) return emitStrNCat(CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B, TLI); @@ -3435,7 +3376,7 @@ Value *FortifiedLibCallSimplifier::optimizeStrNCatChk(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeStrLCpyChk(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { if (isFortifiedCallFoldable(CI, 3)) return emitStrLCpy(CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B, TLI); @@ -3444,7 +3385,7 @@ Value *FortifiedLibCallSimplifier::optimizeStrLCpyChk(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeVSNPrintfChk(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { if (isFortifiedCallFoldable(CI, 3, 1, None, 2)) return emitVSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(4), CI->getArgOperand(5), B, TLI); @@ -3453,7 +3394,7 @@ Value *FortifiedLibCallSimplifier::optimizeVSNPrintfChk(CallInst *CI, } Value *FortifiedLibCallSimplifier::optimizeVSPrintfChk(CallInst *CI, - IRBuilder<> &B) { + IRBuilderBase &B) { if (isFortifiedCallFoldable(CI, 2, None, None, 1)) return emitVSPrintf(CI->getArgOperand(0), CI->getArgOperand(3), CI->getArgOperand(4), B, TLI); @@ -3461,7 +3402,8 @@ Value *FortifiedLibCallSimplifier::optimizeVSPrintfChk(CallInst *CI, return nullptr; } -Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI) { +Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI, + IRBuilderBase &Builder) { // FIXME: We shouldn't be changing "nobuiltin" or TLI unavailable calls here. // Some clang users checked for _chk libcall availability using: // __has_builtin(__builtin___memcpy_chk) @@ -3477,11 +3419,13 @@ Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI) { LibFunc Func; Function *Callee = CI->getCalledFunction(); + bool isCallingConvC = isCallingConvCCompatible(CI); SmallVector<OperandBundleDef, 2> OpBundles; CI->getOperandBundlesAsDefs(OpBundles); - IRBuilder<> Builder(CI, /*FPMathTag=*/nullptr, OpBundles); - bool isCallingConvC = isCallingConvCCompatible(CI); + + IRBuilderBase::OperandBundlesGuard Guard(Builder); + Builder.setDefaultOperandBundles(OpBundles); // First, check that this is a known library functions and that the prototype // is correct. @@ -3502,6 +3446,8 @@ Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI) { case LibFunc_stpcpy_chk: case LibFunc_strcpy_chk: return optimizeStrpCpyChk(CI, Builder, Func); + case LibFunc_strlen_chk: + return optimizeStrLenChk(CI, Builder); case LibFunc_stpncpy_chk: case LibFunc_strncpy_chk: return optimizeStrpNCpyChk(CI, Builder, Func); |