Lines Matching refs:CI
85 static bool callHasFloatingPointArgument(const CallInst *CI) { in callHasFloatingPointArgument() argument
86 return std::any_of(CI->op_begin(), CI->op_end(), [](const Use &OI) { in callHasFloatingPointArgument()
110 Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilder<> &B) { in optimizeStrCat() argument
112 Value *Dst = CI->getArgOperand(0); in optimizeStrCat()
113 Value *Src = CI->getArgOperand(1); in optimizeStrCat()
149 Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilder<> &B) { in optimizeStrNCat() argument
151 Value *Dst = CI->getArgOperand(0); in optimizeStrNCat()
152 Value *Src = CI->getArgOperand(1); in optimizeStrNCat()
156 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2))) in optimizeStrNCat()
182 Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilder<> &B) { in optimizeStrChr() argument
183 Function *Callee = CI->getCalledFunction(); in optimizeStrChr()
185 Value *SrcStr = CI->getArgOperand(0); in optimizeStrChr()
189 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1)); in optimizeStrChr()
195 return emitMemChr(SrcStr, CI->getArgOperand(1), // include nul. in optimizeStrChr()
196 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len), in optimizeStrChr()
216 return Constant::getNullValue(CI->getType()); in optimizeStrChr()
222 Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilder<> &B) { in optimizeStrRChr() argument
223 Value *SrcStr = CI->getArgOperand(0); in optimizeStrRChr()
224 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1)); in optimizeStrRChr()
243 return Constant::getNullValue(CI->getType()); in optimizeStrRChr()
249 Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilder<> &B) { in optimizeStrCmp() argument
250 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1); in optimizeStrCmp()
252 return ConstantInt::get(CI->getType(), 0); in optimizeStrCmp()
260 return ConstantInt::get(CI->getType(), Str1.compare(Str2)); in optimizeStrCmp()
264 B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType())); in optimizeStrCmp()
267 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType()); in optimizeStrCmp()
274 ConstantInt::get(DL.getIntPtrType(CI->getContext()), in optimizeStrCmp()
282 Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilder<> &B) { in optimizeStrNCmp() argument
283 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1); in optimizeStrNCmp()
285 return ConstantInt::get(CI->getType(), 0); in optimizeStrNCmp()
289 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2))) in optimizeStrNCmp()
295 return ConstantInt::get(CI->getType(), 0); in optimizeStrNCmp()
298 return emitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI); in optimizeStrNCmp()
308 return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2)); in optimizeStrNCmp()
313 B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType())); in optimizeStrNCmp()
316 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType()); in optimizeStrNCmp()
321 Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilder<> &B) { in optimizeStrCpy() argument
322 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); in optimizeStrCpy()
334 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len), 1); in optimizeStrCpy()
338 Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilder<> &B) { in optimizeStpCpy() argument
339 Function *Callee = CI->getCalledFunction(); in optimizeStpCpy()
340 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1); in optimizeStpCpy()
362 Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilder<> &B) { in optimizeStrNCpy() argument
363 Function *Callee = CI->getCalledFunction(); in optimizeStrNCpy()
364 Value *Dst = CI->getArgOperand(0); in optimizeStrNCpy()
365 Value *Src = CI->getArgOperand(1); in optimizeStrNCpy()
366 Value *LenOp = CI->getArgOperand(2); in optimizeStrNCpy()
400 Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilder<> &B) { in optimizeStrLen() argument
401 Value *Src = CI->getArgOperand(0); in optimizeStrLen()
405 return ConstantInt::get(CI->getType(), Len - 1); in optimizeStrLen()
432 computeKnownBits(Offset, KnownZero, KnownOne, DL, 0, nullptr, CI, in optimizeStrLen()
451 return B.CreateSub(ConstantInt::get(CI->getType(), NullTermIdx), in optimizeStrLen()
463 Function *Caller = CI->getParent()->getParent(); in optimizeStrLen()
464 emitOptimizationRemark(CI->getContext(), "simplify-libcalls", *Caller, in optimizeStrLen()
468 ConstantInt::get(CI->getType(), LenTrue - 1), in optimizeStrLen()
469 ConstantInt::get(CI->getType(), LenFalse - 1)); in optimizeStrLen()
475 if (isOnlyUsedInZeroEqualityComparison(CI)) in optimizeStrLen()
476 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType()); in optimizeStrLen()
481 Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilder<> &B) { in optimizeStrPBrk() argument
483 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); in optimizeStrPBrk()
484 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); in optimizeStrPBrk()
489 return Constant::getNullValue(CI->getType()); in optimizeStrPBrk()
495 return Constant::getNullValue(CI->getType()); in optimizeStrPBrk()
497 return B.CreateGEP(B.getInt8Ty(), CI->getArgOperand(0), B.getInt64(I), in optimizeStrPBrk()
503 return emitStrChr(CI->getArgOperand(0), S2[0], B, TLI); in optimizeStrPBrk()
508 Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilder<> &B) { in optimizeStrTo() argument
509 Value *EndPtr = CI->getArgOperand(1); in optimizeStrTo()
513 CI->addAttribute(1, Attribute::NoCapture); in optimizeStrTo()
519 Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilder<> &B) { in optimizeStrSpn() argument
521 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); in optimizeStrSpn()
522 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); in optimizeStrSpn()
527 return Constant::getNullValue(CI->getType()); in optimizeStrSpn()
534 return ConstantInt::get(CI->getType(), Pos); in optimizeStrSpn()
540 Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilder<> &B) { in optimizeStrCSpn() argument
542 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1); in optimizeStrCSpn()
543 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2); in optimizeStrCSpn()
547 return Constant::getNullValue(CI->getType()); in optimizeStrCSpn()
554 return ConstantInt::get(CI->getType(), Pos); in optimizeStrCSpn()
559 return emitStrLen(CI->getArgOperand(0), B, DL, TLI); in optimizeStrCSpn()
564 Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilder<> &B) { in optimizeStrStr() argument
566 if (CI->getArgOperand(0) == CI->getArgOperand(1)) in optimizeStrStr()
567 return B.CreateBitCast(CI->getArgOperand(0), CI->getType()); in optimizeStrStr()
570 if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) { in optimizeStrStr()
571 Value *StrLen = emitStrLen(CI->getArgOperand(1), B, DL, TLI); in optimizeStrStr()
574 Value *StrNCmp = emitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1), in optimizeStrStr()
578 for (auto UI = CI->user_begin(), UE = CI->user_end(); UI != UE;) { in optimizeStrStr()
585 return CI; in optimizeStrStr()
590 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr); in optimizeStrStr()
591 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr); in optimizeStrStr()
595 return B.CreateBitCast(CI->getArgOperand(0), CI->getType()); in optimizeStrStr()
602 return Constant::getNullValue(CI->getType()); in optimizeStrStr()
605 Value *Result = castToCStr(CI->getArgOperand(0), B); in optimizeStrStr()
607 return B.CreateBitCast(Result, CI->getType()); in optimizeStrStr()
612 Value *StrChr = emitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI); in optimizeStrStr()
613 return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr; in optimizeStrStr()
618 Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilder<> &B) { in optimizeMemChr() argument
619 Value *SrcStr = CI->getArgOperand(0); in optimizeMemChr()
620 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1)); in optimizeMemChr()
621 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2)); in optimizeMemChr()
625 return Constant::getNullValue(CI->getType()); in optimizeMemChr()
646 if (!CharC && !Str.empty() && isOnlyUsedInZeroEqualityComparison(CI)) { in optimizeMemChr()
670 Value *C = B.CreateZExtOrTrunc(CI->getArgOperand(1), BitfieldC->getType()); in optimizeMemChr()
680 return B.CreateIntToPtr(B.CreateAnd(Bounds, Bits, "memchr"), CI->getType()); in optimizeMemChr()
690 return Constant::getNullValue(CI->getType()); in optimizeMemChr()
696 Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilder<> &B) { in optimizeMemCmp() argument
697 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1); in optimizeMemCmp()
700 return Constant::getNullValue(CI->getType()); in optimizeMemCmp()
703 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2)); in optimizeMemCmp()
709 return Constant::getNullValue(CI->getType()); in optimizeMemCmp()
714 CI->getType(), "lhsv"); in optimizeMemCmp()
716 CI->getType(), "rhsv"); in optimizeMemCmp()
721 if (DL.isLegalInteger(Len * 8) && isOnlyUsedInZeroEqualityComparison(CI)) { in optimizeMemCmp()
723 IntegerType *IntType = IntegerType::get(CI->getContext(), Len * 8); in optimizeMemCmp()
726 if (getKnownAlignment(LHS, DL, CI) >= PrefAlignment && in optimizeMemCmp()
727 getKnownAlignment(RHS, DL, CI) >= PrefAlignment) { in optimizeMemCmp()
739 return B.CreateZExt(B.CreateICmpNE(LHSV, RHSV), CI->getType(), "memcmp"); in optimizeMemCmp()
758 return ConstantInt::get(CI->getType(), Ret); in optimizeMemCmp()
764 Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilder<> &B) { in optimizeMemCpy() argument
766 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), in optimizeMemCpy()
767 CI->getArgOperand(2), 1); in optimizeMemCpy()
768 return CI->getArgOperand(0); in optimizeMemCpy()
771 Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilder<> &B) { in optimizeMemMove() argument
773 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1), in optimizeMemMove()
774 CI->getArgOperand(2), 1); in optimizeMemMove()
775 return CI->getArgOperand(0); in optimizeMemMove()
791 CallInst *CI = B.CreateCall(Calloc, { Num, Size }, "calloc"); in emitCalloc() local
794 CI->setCallingConv(F->getCallingConv()); in emitCalloc()
796 return CI; in emitCalloc()
843 Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B) { in optimizeMemSet() argument
844 if (auto *Calloc = foldMallocMemset(CI, B, *TLI)) in optimizeMemSet()
848 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false); in optimizeMemSet()
849 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1); in optimizeMemSet()
850 return CI->getArgOperand(0); in optimizeMemSet()
880 static Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B, in optimizeUnaryDoubleFP() argument
882 Function *Callee = CI->getCalledFunction(); in optimizeUnaryDoubleFP()
884 if (!CI->getType()->isDoubleTy()) in optimizeUnaryDoubleFP()
889 for (User *U : CI->users()) { in optimizeUnaryDoubleFP()
897 Value *V = valueHasFloatPrecision(CI->getArgOperand(0)); in optimizeUnaryDoubleFP()
903 B.setFastMathFlags(CI->getFastMathFlags()); in optimizeUnaryDoubleFP()
907 Module *M = CI->getModule(); in optimizeUnaryDoubleFP()
920 static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B) { in optimizeBinaryDoubleFP() argument
921 Function *Callee = CI->getCalledFunction(); in optimizeBinaryDoubleFP()
923 if (!CI->getType()->isDoubleTy()) in optimizeBinaryDoubleFP()
928 Value *V1 = valueHasFloatPrecision(CI->getArgOperand(0)); in optimizeBinaryDoubleFP()
931 Value *V2 = valueHasFloatPrecision(CI->getArgOperand(1)); in optimizeBinaryDoubleFP()
937 B.setFastMathFlags(CI->getFastMathFlags()); in optimizeBinaryDoubleFP()
947 Value *LibCallSimplifier::optimizeCos(CallInst *CI, IRBuilder<> &B) { in optimizeCos() argument
948 Function *Callee = CI->getCalledFunction(); in optimizeCos()
952 Ret = optimizeUnaryDoubleFP(CI, B, true); in optimizeCos()
955 Value *Op1 = CI->getArgOperand(0); in optimizeCos()
988 Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) { in optimizePow() argument
989 Function *Callee = CI->getCalledFunction(); in optimizePow()
993 Ret = optimizeUnaryDoubleFP(CI, B, true); in optimizePow()
995 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1); in optimizePow()
1021 if (OpC && OpC->hasUnsafeAlgebra() && CI->hasUnsafeAlgebra()) { in optimizePow()
1027 B.setFastMathFlags(CI->getFastMathFlags()); in optimizePow()
1039 return ConstantFP::get(CI->getType(), 1.0); in optimizePow()
1048 if (CI->hasUnsafeAlgebra()) { in optimizePow()
1050 B.setFastMathFlags(CI->getFastMathFlags()); in optimizePow()
1059 Value *Inf = ConstantFP::getInfinity(CI->getType()); in optimizePow()
1060 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true); in optimizePow()
1074 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), Op1, "powrecip"); in optimizePow()
1077 if (CI->hasUnsafeAlgebra()) { in optimizePow()
1099 FMul = B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), FMul); in optimizePow()
1106 Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilder<> &B) { in optimizeExp2() argument
1107 Function *Callee = CI->getCalledFunction(); in optimizeExp2()
1111 Ret = optimizeUnaryDoubleFP(CI, B, true); in optimizeExp2()
1113 Value *Op = CI->getArgOperand(0); in optimizeExp2()
1133 Constant *One = ConstantFP::get(CI->getContext(), APFloat(1.0f)); in optimizeExp2()
1137 Module *M = CI->getModule(); in optimizeExp2()
1141 CallInst *CI = B.CreateCall(NewCallee, {One, LdExpArg}); in optimizeExp2() local
1143 CI->setCallingConv(F->getCallingConv()); in optimizeExp2()
1145 return CI; in optimizeExp2()
1151 Value *LibCallSimplifier::optimizeFabs(CallInst *CI, IRBuilder<> &B) { in optimizeFabs() argument
1152 Function *Callee = CI->getCalledFunction(); in optimizeFabs()
1156 Ret = optimizeUnaryDoubleFP(CI, B, false); in optimizeFabs()
1158 Value *Op = CI->getArgOperand(0); in optimizeFabs()
1168 Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilder<> &B) { in optimizeFMinFMax() argument
1169 Function *Callee = CI->getCalledFunction(); in optimizeFMinFMax()
1174 if (Value *Ret = optimizeBinaryDoubleFP(CI, B)) in optimizeFMinFMax()
1179 if (CI->hasUnsafeAlgebra()) { in optimizeFMinFMax()
1184 if (!CI->hasNoNaNs()) in optimizeFMinFMax()
1198 Value *Op0 = CI->getArgOperand(0); in optimizeFMinFMax()
1199 Value *Op1 = CI->getArgOperand(1); in optimizeFMinFMax()
1205 Value *LibCallSimplifier::optimizeLog(CallInst *CI, IRBuilder<> &B) { in optimizeLog() argument
1206 Function *Callee = CI->getCalledFunction(); in optimizeLog()
1210 Ret = optimizeUnaryDoubleFP(CI, B, true); in optimizeLog()
1212 if (!CI->hasUnsafeAlgebra()) in optimizeLog()
1214 Value *Op1 = CI->getArgOperand(0); in optimizeLog()
1244 emitUnaryFloatFnCall(ConstantFP::get(CI->getType(), 2.0), in optimizeLog()
1250 Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilder<> &B) { in optimizeSqrt() argument
1251 Function *Callee = CI->getCalledFunction(); in optimizeSqrt()
1255 Ret = optimizeUnaryDoubleFP(CI, B, true); in optimizeSqrt()
1257 if (!CI->hasUnsafeAlgebra()) in optimizeSqrt()
1260 Instruction *I = dyn_cast<Instruction>(CI->getArgOperand(0)); in optimizeSqrt()
1317 Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilder<> &B) { in optimizeTan() argument
1318 Function *Callee = CI->getCalledFunction(); in optimizeTan()
1322 Ret = optimizeUnaryDoubleFP(CI, B, true); in optimizeTan()
1324 Value *Op1 = CI->getArgOperand(0); in optimizeTan()
1330 if (!CI->hasUnsafeAlgebra() || !OpC->hasUnsafeAlgebra()) in optimizeTan()
1346 static bool isTrigLibCall(CallInst *CI) { in isTrigLibCall() argument
1350 return CI->hasFnAttr(Attribute::NoUnwind) && in isTrigLibCall()
1351 CI->hasFnAttr(Attribute::ReadNone); in isTrigLibCall()
1404 Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, IRBuilder<> &B) { in optimizeSinCosPi() argument
1407 if (!isTrigLibCall(CI)) in optimizeSinCosPi()
1410 Value *Arg = CI->getArgOperand(0); in optimizeSinCosPi()
1420 Function *F = CI->getFunction(); in optimizeSinCosPi()
1429 insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos, SinCos); in optimizeSinCosPi()
1443 CallInst *CI = dyn_cast<CallInst>(Val); in classifyArgUse() local
1445 if (!CI) in classifyArgUse()
1449 if (CI->getFunction() != F) in classifyArgUse()
1452 Function *Callee = CI->getCalledFunction(); in classifyArgUse()
1455 !isTrigLibCall(CI)) in classifyArgUse()
1460 SinCalls.push_back(CI); in classifyArgUse()
1462 CosCalls.push_back(CI); in classifyArgUse()
1464 SinCosCalls.push_back(CI); in classifyArgUse()
1467 SinCalls.push_back(CI); in classifyArgUse()
1469 CosCalls.push_back(CI); in classifyArgUse()
1471 SinCosCalls.push_back(CI); in classifyArgUse()
1485 Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilder<> &B) { in optimizeFFS() argument
1486 Function *Callee = CI->getCalledFunction(); in optimizeFFS()
1487 Value *Op = CI->getArgOperand(0); in optimizeFFS()
1490 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { in optimizeFFS() local
1491 if (CI->isZero()) // ffs(0) -> 0. in optimizeFFS()
1494 return B.getInt32(CI->getValue().countTrailingZeros() + 1); in optimizeFFS()
1509 Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilder<> &B) { in optimizeAbs() argument
1511 Value *Op = CI->getArgOperand(0); in optimizeAbs()
1518 Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilder<> &B) { in optimizeIsDigit() argument
1520 Value *Op = CI->getArgOperand(0); in optimizeIsDigit()
1523 return B.CreateZExt(Op, CI->getType()); in optimizeIsDigit()
1526 Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilder<> &B) { in optimizeIsAscii() argument
1528 Value *Op = CI->getArgOperand(0); in optimizeIsAscii()
1530 return B.CreateZExt(Op, CI->getType()); in optimizeIsAscii()
1533 Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilder<> &B) { in optimizeToAscii() argument
1535 return B.CreateAnd(CI->getArgOperand(0), in optimizeToAscii()
1536 ConstantInt::get(CI->getType(), 0x7F)); in optimizeToAscii()
1543 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
1545 Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilder<> &B, in optimizeErrorReporting() argument
1547 Function *Callee = CI->getCalledFunction(); in optimizeErrorReporting()
1556 if (!CI->hasFnAttr(Attribute::Cold) && in optimizeErrorReporting()
1557 isReportingError(Callee, CI, StreamArg)) { in optimizeErrorReporting()
1558 CI->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold); in optimizeErrorReporting()
1564 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) { in isReportingError() argument
1574 if (StreamArg >= (int)CI->getNumArgOperands()) in isReportingError()
1576 LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg)); in isReportingError()
1585 Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) { in optimizePrintFString() argument
1588 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr)) in optimizePrintFString()
1593 return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0); in optimizePrintFString()
1598 if (!CI->use_empty()) in optimizePrintFString()
1606 if (FormatStr == "%s" && CI->getNumArgOperands() > 1) { in optimizePrintFString()
1608 if (!getConstantStringInfo(CI->getOperand(1), ChrStr)) in optimizePrintFString()
1627 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 && in optimizePrintFString()
1628 CI->getArgOperand(1)->getType()->isIntegerTy()) in optimizePrintFString()
1629 return emitPutChar(CI->getArgOperand(1), B, TLI); in optimizePrintFString()
1632 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 && in optimizePrintFString()
1633 CI->getArgOperand(1)->getType()->isPointerTy()) in optimizePrintFString()
1634 return emitPutS(CI->getArgOperand(1), B, TLI); in optimizePrintFString()
1638 Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilder<> &B) { in optimizePrintF() argument
1640 Function *Callee = CI->getCalledFunction(); in optimizePrintF()
1642 if (Value *V = optimizePrintFString(CI, B)) { in optimizePrintF()
1648 if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) { in optimizePrintF()
1652 CallInst *New = cast<CallInst>(CI->clone()); in optimizePrintF()
1660 Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) { in optimizeSPrintFString() argument
1663 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr)) in optimizeSPrintFString()
1667 if (CI->getNumArgOperands() == 2) { in optimizeSPrintFString()
1675 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), in optimizeSPrintFString()
1676 ConstantInt::get(DL.getIntPtrType(CI->getContext()), in optimizeSPrintFString()
1679 return ConstantInt::get(CI->getType(), FormatStr.size()); in optimizeSPrintFString()
1685 CI->getNumArgOperands() < 3) in optimizeSPrintFString()
1691 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) in optimizeSPrintFString()
1693 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char"); in optimizeSPrintFString()
1694 Value *Ptr = castToCStr(CI->getArgOperand(0), B); in optimizeSPrintFString()
1699 return ConstantInt::get(CI->getType(), 1); in optimizeSPrintFString()
1704 if (!CI->getArgOperand(2)->getType()->isPointerTy()) in optimizeSPrintFString()
1707 Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI); in optimizeSPrintFString()
1712 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1); in optimizeSPrintFString()
1715 return B.CreateIntCast(Len, CI->getType(), false); in optimizeSPrintFString()
1720 Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilder<> &B) { in optimizeSPrintF() argument
1721 Function *Callee = CI->getCalledFunction(); in optimizeSPrintF()
1723 if (Value *V = optimizeSPrintFString(CI, B)) { in optimizeSPrintF()
1729 if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) { in optimizeSPrintF()
1733 CallInst *New = cast<CallInst>(CI->clone()); in optimizeSPrintF()
1741 Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, IRBuilder<> &B) { in optimizeFPrintFString() argument
1742 optimizeErrorReporting(CI, B, 0); in optimizeFPrintFString()
1746 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr)) in optimizeFPrintFString()
1752 if (!CI->use_empty()) in optimizeFPrintFString()
1756 if (CI->getNumArgOperands() == 2) { in optimizeFPrintFString()
1762 CI->getArgOperand(1), in optimizeFPrintFString()
1763 ConstantInt::get(DL.getIntPtrType(CI->getContext()), FormatStr.size()), in optimizeFPrintFString()
1764 CI->getArgOperand(0), B, DL, TLI); in optimizeFPrintFString()
1770 CI->getNumArgOperands() < 3) in optimizeFPrintFString()
1776 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) in optimizeFPrintFString()
1778 return emitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI); in optimizeFPrintFString()
1783 if (!CI->getArgOperand(2)->getType()->isPointerTy()) in optimizeFPrintFString()
1785 return emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI); in optimizeFPrintFString()
1790 Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilder<> &B) { in optimizeFPrintF() argument
1791 Function *Callee = CI->getCalledFunction(); in optimizeFPrintF()
1793 if (Value *V = optimizeFPrintFString(CI, B)) { in optimizeFPrintF()
1799 if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) { in optimizeFPrintF()
1803 CallInst *New = cast<CallInst>(CI->clone()); in optimizeFPrintF()
1811 Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilder<> &B) { in optimizeFWrite() argument
1812 optimizeErrorReporting(CI, B, 3); in optimizeFWrite()
1815 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1)); in optimizeFWrite()
1816 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2)); in optimizeFWrite()
1823 return ConstantInt::get(CI->getType(), 0); in optimizeFWrite()
1827 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F) in optimizeFWrite()
1828 Value *Char = B.CreateLoad(castToCStr(CI->getArgOperand(0), B), "char"); in optimizeFWrite()
1829 Value *NewCI = emitFPutC(Char, CI->getArgOperand(3), B, TLI); in optimizeFWrite()
1830 return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr; in optimizeFWrite()
1836 Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilder<> &B) { in optimizeFPuts() argument
1837 optimizeErrorReporting(CI, B, 1); in optimizeFPuts()
1841 if (CI->getParent()->getParent()->optForSize()) in optimizeFPuts()
1845 if (!CI->use_empty()) in optimizeFPuts()
1849 uint64_t Len = GetStringLength(CI->getArgOperand(0)); in optimizeFPuts()
1855 CI->getArgOperand(0), in optimizeFPuts()
1856 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len - 1), in optimizeFPuts()
1857 CI->getArgOperand(1), B, DL, TLI); in optimizeFPuts()
1860 Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilder<> &B) { in optimizePuts() argument
1863 if (!getConstantStringInfo(CI->getArgOperand(0), Str)) in optimizePuts()
1866 if (Str.empty() && CI->use_empty()) { in optimizePuts()
1869 if (CI->use_empty() || !Res) in optimizePuts()
1871 return B.CreateIntCast(Res, CI->getType(), true); in optimizePuts()
1886 Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI, in optimizeStringMemoryLibCall() argument
1889 Function *Callee = CI->getCalledFunction(); in optimizeStringMemoryLibCall()
1894 CI->getCallingConv() == llvm::CallingConv::C) && in optimizeStringMemoryLibCall()
1898 return optimizeStrCat(CI, Builder); in optimizeStringMemoryLibCall()
1900 return optimizeStrNCat(CI, Builder); in optimizeStringMemoryLibCall()
1902 return optimizeStrChr(CI, Builder); in optimizeStringMemoryLibCall()
1904 return optimizeStrRChr(CI, Builder); in optimizeStringMemoryLibCall()
1906 return optimizeStrCmp(CI, Builder); in optimizeStringMemoryLibCall()
1908 return optimizeStrNCmp(CI, Builder); in optimizeStringMemoryLibCall()
1910 return optimizeStrCpy(CI, Builder); in optimizeStringMemoryLibCall()
1912 return optimizeStpCpy(CI, Builder); in optimizeStringMemoryLibCall()
1914 return optimizeStrNCpy(CI, Builder); in optimizeStringMemoryLibCall()
1916 return optimizeStrLen(CI, Builder); in optimizeStringMemoryLibCall()
1918 return optimizeStrPBrk(CI, Builder); in optimizeStringMemoryLibCall()
1926 return optimizeStrTo(CI, Builder); in optimizeStringMemoryLibCall()
1928 return optimizeStrSpn(CI, Builder); in optimizeStringMemoryLibCall()
1930 return optimizeStrCSpn(CI, Builder); in optimizeStringMemoryLibCall()
1932 return optimizeStrStr(CI, Builder); in optimizeStringMemoryLibCall()
1934 return optimizeMemChr(CI, Builder); in optimizeStringMemoryLibCall()
1936 return optimizeMemCmp(CI, Builder); in optimizeStringMemoryLibCall()
1938 return optimizeMemCpy(CI, Builder); in optimizeStringMemoryLibCall()
1940 return optimizeMemMove(CI, Builder); in optimizeStringMemoryLibCall()
1942 return optimizeMemSet(CI, Builder); in optimizeStringMemoryLibCall()
1950 Value *LibCallSimplifier::optimizeCall(CallInst *CI) { in optimizeCall() argument
1951 if (CI->isNoBuiltin()) in optimizeCall()
1955 Function *Callee = CI->getCalledFunction(); in optimizeCall()
1959 CI->getOperandBundlesAsDefs(OpBundles); in optimizeCall()
1960 IRBuilder<> Builder(CI, /*FPMathTag=*/nullptr, OpBundles); in optimizeCall()
1961 bool isCallingConvC = CI->getCallingConv() == llvm::CallingConv::C; in optimizeCall()
1966 else if (isa<FPMathOperator>(CI) && CI->hasUnsafeAlgebra()) in optimizeCall()
1970 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) { in optimizeCall()
1975 return optimizePow(CI, Builder); in optimizeCall()
1977 return optimizeExp2(CI, Builder); in optimizeCall()
1979 return optimizeFabs(CI, Builder); in optimizeCall()
1981 return optimizeLog(CI, Builder); in optimizeCall()
1983 return optimizeSqrt(CI, Builder); in optimizeCall()
1991 if (Value *SimplifiedFortifiedCI = FortifiedSimplifier.optimizeCall(CI)) { in optimizeCall()
2013 if (Value *V = optimizeStringMemoryLibCall(CI, Builder)) in optimizeCall()
2019 return optimizeCos(CI, Builder); in optimizeCall()
2024 return optimizeSinCosPi(CI, Builder); in optimizeCall()
2028 return optimizePow(CI, Builder); in optimizeCall()
2032 return optimizeExp2(CI, Builder); in optimizeCall()
2036 return optimizeFabs(CI, Builder); in optimizeCall()
2040 return optimizeSqrt(CI, Builder); in optimizeCall()
2044 return optimizeFFS(CI, Builder); in optimizeCall()
2048 return optimizeAbs(CI, Builder); in optimizeCall()
2050 return optimizeIsDigit(CI, Builder); in optimizeCall()
2052 return optimizeIsAscii(CI, Builder); in optimizeCall()
2054 return optimizeToAscii(CI, Builder); in optimizeCall()
2056 return optimizePrintF(CI, Builder); in optimizeCall()
2058 return optimizeSPrintF(CI, Builder); in optimizeCall()
2060 return optimizeFPrintF(CI, Builder); in optimizeCall()
2062 return optimizeFWrite(CI, Builder); in optimizeCall()
2064 return optimizeFPuts(CI, Builder); in optimizeCall()
2070 return optimizeLog(CI, Builder); in optimizeCall()
2072 return optimizePuts(CI, Builder); in optimizeCall()
2076 return optimizeTan(CI, Builder); in optimizeCall()
2078 return optimizeErrorReporting(CI, Builder); in optimizeCall()
2081 return optimizeErrorReporting(CI, Builder, 0); in optimizeCall()
2083 return optimizeErrorReporting(CI, Builder, 1); in optimizeCall()
2091 return optimizeUnaryDoubleFP(CI, Builder, false); in optimizeCall()
2108 return optimizeUnaryDoubleFP(CI, Builder, true); in optimizeCall()
2112 return optimizeBinaryDoubleFP(CI, Builder); in optimizeCall()
2120 return optimizeFMinFMax(CI, Builder); in optimizeCall()
2184 bool FortifiedLibCallSimplifier::isFortifiedCallFoldable(CallInst *CI, in isFortifiedCallFoldable() argument
2188 if (CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(SizeOp)) in isFortifiedCallFoldable()
2191 dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) { in isFortifiedCallFoldable()
2198 uint64_t Len = GetStringLength(CI->getArgOperand(SizeOp)); in isFortifiedCallFoldable()
2205 if (ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getArgOperand(SizeOp))) in isFortifiedCallFoldable()
2211 Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI, in optimizeMemCpyChk() argument
2213 if (isFortifiedCallFoldable(CI, 3, 2, false)) { in optimizeMemCpyChk()
2214 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), in optimizeMemCpyChk()
2215 CI->getArgOperand(2), 1); in optimizeMemCpyChk()
2216 return CI->getArgOperand(0); in optimizeMemCpyChk()
2221 Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI, in optimizeMemMoveChk() argument
2223 if (isFortifiedCallFoldable(CI, 3, 2, false)) { in optimizeMemMoveChk()
2224 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1), in optimizeMemMoveChk()
2225 CI->getArgOperand(2), 1); in optimizeMemMoveChk()
2226 return CI->getArgOperand(0); in optimizeMemMoveChk()
2231 Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI, in optimizeMemSetChk() argument
2235 if (isFortifiedCallFoldable(CI, 3, 2, false)) { in optimizeMemSetChk()
2236 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false); in optimizeMemSetChk()
2237 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1); in optimizeMemSetChk()
2238 return CI->getArgOperand(0); in optimizeMemSetChk()
2243 Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI, in optimizeStrpCpyChk() argument
2246 Function *Callee = CI->getCalledFunction(); in optimizeStrpCpyChk()
2248 const DataLayout &DL = CI->getModule()->getDataLayout(); in optimizeStrpCpyChk()
2249 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1), in optimizeStrpCpyChk()
2250 *ObjSize = CI->getArgOperand(2); in optimizeStrpCpyChk()
2263 if (isFortifiedCallFoldable(CI, 2, 1, true)) in optimizeStrpCpyChk()
2274 Type *SizeTTy = DL.getIntPtrType(CI->getContext()); in optimizeStrpCpyChk()
2284 Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI, in optimizeStrpNCpyChk() argument
2287 Function *Callee = CI->getCalledFunction(); in optimizeStrpNCpyChk()
2289 if (isFortifiedCallFoldable(CI, 3, 2, false)) { in optimizeStrpNCpyChk()
2290 Value *Ret = emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1), in optimizeStrpNCpyChk()
2291 CI->getArgOperand(2), B, TLI, Name.substr(2, 7)); in optimizeStrpNCpyChk()
2297 Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI) { in optimizeCall() argument
2312 Function *Callee = CI->getCalledFunction(); in optimizeCall()
2315 CI->getOperandBundlesAsDefs(OpBundles); in optimizeCall()
2316 IRBuilder<> Builder(CI, /*FPMathTag=*/nullptr, OpBundles); in optimizeCall()
2317 bool isCallingConvC = CI->getCallingConv() == llvm::CallingConv::C; in optimizeCall()
2330 return optimizeMemCpyChk(CI, Builder); in optimizeCall()
2332 return optimizeMemMoveChk(CI, Builder); in optimizeCall()
2334 return optimizeMemSetChk(CI, Builder); in optimizeCall()
2337 return optimizeStrpCpyChk(CI, Builder, Func); in optimizeCall()
2340 return optimizeStrpNCpyChk(CI, Builder, Func); in optimizeCall()