1 //===------ SimplifyLibCalls.cpp - Library calls simplifier ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is a utility pass used for testing the InstructionSimplify analysis.
11 // The analysis is applied to every instruction, and if it simplifies then the
12 // instruction is replaced by the simplification. If you are looking for a pass
13 // that performs serious instruction folding, use the instcombine pass instead.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Utils/SimplifyLibCalls.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/DiagnosticInfo.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/IRBuilder.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Intrinsics.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/PatternMatch.h"
31 #include "llvm/Support/Allocator.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Analysis/TargetLibraryInfo.h"
34 #include "llvm/Transforms/Utils/BuildLibCalls.h"
37 using namespace PatternMatch;
40 ColdErrorCalls("error-reporting-is-cold", cl::init(true), cl::Hidden,
41 cl::desc("Treat error-reporting calls as cold"));
44 EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden,
46 cl::desc("Enable unsafe double to float "
47 "shrinking for math lib calls"));
50 //===----------------------------------------------------------------------===//
52 //===----------------------------------------------------------------------===//
54 static bool ignoreCallingConv(LibFunc::Func Func) {
64 llvm_unreachable("All cases should be covered in the switch.");
67 /// isOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
68 /// value is equal or not-equal to zero.
69 static bool isOnlyUsedInZeroEqualityComparison(Value *V) {
70 for (User *U : V->users()) {
71 if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
73 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
76 // Unknown instruction.
82 /// isOnlyUsedInEqualityComparison - Return true if it is only used in equality
83 /// comparisons with With.
84 static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
85 for (User *U : V->users()) {
86 if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
87 if (IC->isEquality() && IC->getOperand(1) == With)
89 // Unknown instruction.
95 static bool callHasFloatingPointArgument(const CallInst *CI) {
96 for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
98 if ((*it)->getType()->isFloatingPointTy())
104 /// \brief Check whether the overloaded unary floating point function
105 /// corresponding to \a Ty is available.
106 static bool hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
107 LibFunc::Func DoubleFn, LibFunc::Func FloatFn,
108 LibFunc::Func LongDoubleFn) {
109 switch (Ty->getTypeID()) {
110 case Type::FloatTyID:
111 return TLI->has(FloatFn);
112 case Type::DoubleTyID:
113 return TLI->has(DoubleFn);
115 return TLI->has(LongDoubleFn);
119 /// \brief Returns whether \p F matches the signature expected for the
120 /// string/memory copying library function \p Func.
121 /// Acceptable functions are st[rp][n]?cpy, memove, memcpy, and memset.
122 /// Their fortified (_chk) counterparts are also accepted.
123 static bool checkStringCopyLibFuncSignature(Function *F, LibFunc::Func Func) {
124 const DataLayout &DL = F->getParent()->getDataLayout();
125 FunctionType *FT = F->getFunctionType();
126 LLVMContext &Context = F->getContext();
127 Type *PCharTy = Type::getInt8PtrTy(Context);
128 Type *SizeTTy = DL.getIntPtrType(Context);
129 unsigned NumParams = FT->getNumParams();
131 // All string libfuncs return the same type as the first parameter.
132 if (FT->getReturnType() != FT->getParamType(0))
137 llvm_unreachable("Can't check signature for non-string-copy libfunc.");
138 case LibFunc::stpncpy_chk:
139 case LibFunc::strncpy_chk:
140 --NumParams; // fallthrough
141 case LibFunc::stpncpy:
142 case LibFunc::strncpy: {
143 if (NumParams != 3 || FT->getParamType(0) != FT->getParamType(1) ||
144 FT->getParamType(0) != PCharTy || !FT->getParamType(2)->isIntegerTy())
148 case LibFunc::strcpy_chk:
149 case LibFunc::stpcpy_chk:
150 --NumParams; // fallthrough
151 case LibFunc::stpcpy:
152 case LibFunc::strcpy: {
153 if (NumParams != 2 || FT->getParamType(0) != FT->getParamType(1) ||
154 FT->getParamType(0) != PCharTy)
158 case LibFunc::memmove_chk:
159 case LibFunc::memcpy_chk:
160 --NumParams; // fallthrough
161 case LibFunc::memmove:
162 case LibFunc::memcpy: {
163 if (NumParams != 3 || !FT->getParamType(0)->isPointerTy() ||
164 !FT->getParamType(1)->isPointerTy() || FT->getParamType(2) != SizeTTy)
168 case LibFunc::memset_chk:
169 --NumParams; // fallthrough
170 case LibFunc::memset: {
171 if (NumParams != 3 || !FT->getParamType(0)->isPointerTy() ||
172 !FT->getParamType(1)->isIntegerTy() || FT->getParamType(2) != SizeTTy)
177 // If this is a fortified libcall, the last parameter is a size_t.
178 if (NumParams == FT->getNumParams() - 1)
179 return FT->getParamType(FT->getNumParams() - 1) == SizeTTy;
183 //===----------------------------------------------------------------------===//
184 // String and Memory Library Call Optimizations
185 //===----------------------------------------------------------------------===//
187 Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilder<> &B) {
188 Function *Callee = CI->getCalledFunction();
189 // Verify the "strcat" function prototype.
190 FunctionType *FT = Callee->getFunctionType();
191 if (FT->getNumParams() != 2||
192 FT->getReturnType() != B.getInt8PtrTy() ||
193 FT->getParamType(0) != FT->getReturnType() ||
194 FT->getParamType(1) != FT->getReturnType())
197 // Extract some information from the instruction
198 Value *Dst = CI->getArgOperand(0);
199 Value *Src = CI->getArgOperand(1);
201 // See if we can get the length of the input string.
202 uint64_t Len = GetStringLength(Src);
205 --Len; // Unbias length.
207 // Handle the simple, do-nothing case: strcat(x, "") -> x
211 return emitStrLenMemCpy(Src, Dst, Len, B);
214 Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
216 // We need to find the end of the destination string. That's where the
217 // memory is to be moved to. We just generate a call to strlen.
218 Value *DstLen = EmitStrLen(Dst, B, DL, TLI);
222 // Now that we have the destination's length, we must index into the
223 // destination's pointer to get the actual memcpy destination (end of
224 // the string .. we're concatenating).
225 Value *CpyDst = B.CreateGEP(B.getInt8Ty(), Dst, DstLen, "endptr");
227 // We have enough information to now generate the memcpy call to do the
228 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
229 B.CreateMemCpy(CpyDst, Src,
230 ConstantInt::get(DL.getIntPtrType(Src->getContext()), Len + 1),
235 Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilder<> &B) {
236 Function *Callee = CI->getCalledFunction();
237 // Verify the "strncat" function prototype.
238 FunctionType *FT = Callee->getFunctionType();
239 if (FT->getNumParams() != 3 || FT->getReturnType() != B.getInt8PtrTy() ||
240 FT->getParamType(0) != FT->getReturnType() ||
241 FT->getParamType(1) != FT->getReturnType() ||
242 !FT->getParamType(2)->isIntegerTy())
245 // Extract some information from the instruction
246 Value *Dst = CI->getArgOperand(0);
247 Value *Src = CI->getArgOperand(1);
250 // We don't do anything if length is not constant
251 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
252 Len = LengthArg->getZExtValue();
256 // See if we can get the length of the input string.
257 uint64_t SrcLen = GetStringLength(Src);
260 --SrcLen; // Unbias length.
262 // Handle the simple, do-nothing cases:
263 // strncat(x, "", c) -> x
264 // strncat(x, c, 0) -> x
265 if (SrcLen == 0 || Len == 0)
268 // We don't optimize this case
272 // strncat(x, s, c) -> strcat(x, s)
273 // s is constant so the strcat can be optimized further
274 return emitStrLenMemCpy(Src, Dst, SrcLen, B);
277 Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilder<> &B) {
278 Function *Callee = CI->getCalledFunction();
279 // Verify the "strchr" function prototype.
280 FunctionType *FT = Callee->getFunctionType();
281 if (FT->getNumParams() != 2 || FT->getReturnType() != B.getInt8PtrTy() ||
282 FT->getParamType(0) != FT->getReturnType() ||
283 !FT->getParamType(1)->isIntegerTy(32))
286 Value *SrcStr = CI->getArgOperand(0);
288 // If the second operand is non-constant, see if we can compute the length
289 // of the input string and turn this into memchr.
290 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
292 uint64_t Len = GetStringLength(SrcStr);
293 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32)) // memchr needs i32.
296 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
297 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len),
301 // Otherwise, the character is a constant, see if the first argument is
302 // a string literal. If so, we can constant fold.
304 if (!getConstantStringInfo(SrcStr, Str)) {
305 if (CharC->isZero()) // strchr(p, 0) -> p + strlen(p)
306 return B.CreateGEP(B.getInt8Ty(), SrcStr, EmitStrLen(SrcStr, B, DL, TLI), "strchr");
310 // Compute the offset, make sure to handle the case when we're searching for
311 // zero (a weird way to spell strlen).
312 size_t I = (0xFF & CharC->getSExtValue()) == 0
314 : Str.find(CharC->getSExtValue());
315 if (I == StringRef::npos) // Didn't find the char. strchr returns null.
316 return Constant::getNullValue(CI->getType());
318 // strchr(s+n,c) -> gep(s+n+i,c)
319 return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strchr");
322 Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilder<> &B) {
323 Function *Callee = CI->getCalledFunction();
324 // Verify the "strrchr" function prototype.
325 FunctionType *FT = Callee->getFunctionType();
326 if (FT->getNumParams() != 2 || FT->getReturnType() != B.getInt8PtrTy() ||
327 FT->getParamType(0) != FT->getReturnType() ||
328 !FT->getParamType(1)->isIntegerTy(32))
331 Value *SrcStr = CI->getArgOperand(0);
332 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
334 // Cannot fold anything if we're not looking for a constant.
339 if (!getConstantStringInfo(SrcStr, Str)) {
340 // strrchr(s, 0) -> strchr(s, 0)
342 return EmitStrChr(SrcStr, '\0', B, TLI);
346 // Compute the offset.
347 size_t I = (0xFF & CharC->getSExtValue()) == 0
349 : Str.rfind(CharC->getSExtValue());
350 if (I == StringRef::npos) // Didn't find the char. Return null.
351 return Constant::getNullValue(CI->getType());
353 // strrchr(s+n,c) -> gep(s+n+i,c)
354 return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strrchr");
357 Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilder<> &B) {
358 Function *Callee = CI->getCalledFunction();
359 // Verify the "strcmp" function prototype.
360 FunctionType *FT = Callee->getFunctionType();
361 if (FT->getNumParams() != 2 || !FT->getReturnType()->isIntegerTy(32) ||
362 FT->getParamType(0) != FT->getParamType(1) ||
363 FT->getParamType(0) != B.getInt8PtrTy())
366 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
367 if (Str1P == Str2P) // strcmp(x,x) -> 0
368 return ConstantInt::get(CI->getType(), 0);
370 StringRef Str1, Str2;
371 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
372 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
374 // strcmp(x, y) -> cnst (if both x and y are constant strings)
375 if (HasStr1 && HasStr2)
376 return ConstantInt::get(CI->getType(), Str1.compare(Str2));
378 if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
380 B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType()));
382 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
383 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
385 // strcmp(P, "x") -> memcmp(P, "x", 2)
386 uint64_t Len1 = GetStringLength(Str1P);
387 uint64_t Len2 = GetStringLength(Str2P);
389 return EmitMemCmp(Str1P, Str2P,
390 ConstantInt::get(DL.getIntPtrType(CI->getContext()),
391 std::min(Len1, Len2)),
398 Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilder<> &B) {
399 Function *Callee = CI->getCalledFunction();
400 // Verify the "strncmp" function prototype.
401 FunctionType *FT = Callee->getFunctionType();
402 if (FT->getNumParams() != 3 || !FT->getReturnType()->isIntegerTy(32) ||
403 FT->getParamType(0) != FT->getParamType(1) ||
404 FT->getParamType(0) != B.getInt8PtrTy() ||
405 !FT->getParamType(2)->isIntegerTy())
408 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
409 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
410 return ConstantInt::get(CI->getType(), 0);
412 // Get the length argument if it is constant.
414 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
415 Length = LengthArg->getZExtValue();
419 if (Length == 0) // strncmp(x,y,0) -> 0
420 return ConstantInt::get(CI->getType(), 0);
422 if (Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
423 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI);
425 StringRef Str1, Str2;
426 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
427 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
429 // strncmp(x, y) -> cnst (if both x and y are constant strings)
430 if (HasStr1 && HasStr2) {
431 StringRef SubStr1 = Str1.substr(0, Length);
432 StringRef SubStr2 = Str2.substr(0, Length);
433 return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
436 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
438 B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType()));
440 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
441 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
446 Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilder<> &B) {
447 Function *Callee = CI->getCalledFunction();
449 if (!checkStringCopyLibFuncSignature(Callee, LibFunc::strcpy))
452 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
453 if (Dst == Src) // strcpy(x,x) -> x
456 // See if we can get the length of the input string.
457 uint64_t Len = GetStringLength(Src);
461 // We have enough information to now generate the memcpy call to do the
462 // copy for us. Make a memcpy to copy the nul byte with align = 1.
463 B.CreateMemCpy(Dst, Src,
464 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len), 1);
468 Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilder<> &B) {
469 Function *Callee = CI->getCalledFunction();
470 // Verify the "stpcpy" function prototype.
471 FunctionType *FT = Callee->getFunctionType();
473 if (!checkStringCopyLibFuncSignature(Callee, LibFunc::stpcpy))
476 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
477 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
478 Value *StrLen = EmitStrLen(Src, B, DL, TLI);
479 return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
482 // See if we can get the length of the input string.
483 uint64_t Len = GetStringLength(Src);
487 Type *PT = FT->getParamType(0);
488 Value *LenV = ConstantInt::get(DL.getIntPtrType(PT), Len);
490 B.CreateGEP(B.getInt8Ty(), Dst, ConstantInt::get(DL.getIntPtrType(PT), Len - 1));
492 // We have enough information to now generate the memcpy call to do the
493 // copy for us. Make a memcpy to copy the nul byte with align = 1.
494 B.CreateMemCpy(Dst, Src, LenV, 1);
498 Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilder<> &B) {
499 Function *Callee = CI->getCalledFunction();
500 FunctionType *FT = Callee->getFunctionType();
502 if (!checkStringCopyLibFuncSignature(Callee, LibFunc::strncpy))
505 Value *Dst = CI->getArgOperand(0);
506 Value *Src = CI->getArgOperand(1);
507 Value *LenOp = CI->getArgOperand(2);
509 // See if we can get the length of the input string.
510 uint64_t SrcLen = GetStringLength(Src);
516 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
517 B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
522 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
523 Len = LengthArg->getZExtValue();
528 return Dst; // strncpy(x, y, 0) -> x
530 // Let strncpy handle the zero padding
531 if (Len > SrcLen + 1)
534 Type *PT = FT->getParamType(0);
535 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
536 B.CreateMemCpy(Dst, Src, ConstantInt::get(DL.getIntPtrType(PT), Len), 1);
541 Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilder<> &B) {
542 Function *Callee = CI->getCalledFunction();
543 FunctionType *FT = Callee->getFunctionType();
544 if (FT->getNumParams() != 1 || FT->getParamType(0) != B.getInt8PtrTy() ||
545 !FT->getReturnType()->isIntegerTy())
548 Value *Src = CI->getArgOperand(0);
550 // Constant folding: strlen("xyz") -> 3
551 if (uint64_t Len = GetStringLength(Src))
552 return ConstantInt::get(CI->getType(), Len - 1);
554 // strlen(x?"foo":"bars") --> x ? 3 : 4
555 if (SelectInst *SI = dyn_cast<SelectInst>(Src)) {
556 uint64_t LenTrue = GetStringLength(SI->getTrueValue());
557 uint64_t LenFalse = GetStringLength(SI->getFalseValue());
558 if (LenTrue && LenFalse) {
559 Function *Caller = CI->getParent()->getParent();
560 emitOptimizationRemark(CI->getContext(), "simplify-libcalls", *Caller,
562 "folded strlen(select) to select of constants");
563 return B.CreateSelect(SI->getCondition(),
564 ConstantInt::get(CI->getType(), LenTrue - 1),
565 ConstantInt::get(CI->getType(), LenFalse - 1));
569 // strlen(x) != 0 --> *x != 0
570 // strlen(x) == 0 --> *x == 0
571 if (isOnlyUsedInZeroEqualityComparison(CI))
572 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
577 Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilder<> &B) {
578 Function *Callee = CI->getCalledFunction();
579 FunctionType *FT = Callee->getFunctionType();
580 if (FT->getNumParams() != 2 || FT->getParamType(0) != B.getInt8PtrTy() ||
581 FT->getParamType(1) != FT->getParamType(0) ||
582 FT->getReturnType() != FT->getParamType(0))
586 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
587 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
589 // strpbrk(s, "") -> nullptr
590 // strpbrk("", s) -> nullptr
591 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
592 return Constant::getNullValue(CI->getType());
595 if (HasS1 && HasS2) {
596 size_t I = S1.find_first_of(S2);
597 if (I == StringRef::npos) // No match.
598 return Constant::getNullValue(CI->getType());
600 return B.CreateGEP(B.getInt8Ty(), CI->getArgOperand(0), B.getInt64(I), "strpbrk");
603 // strpbrk(s, "a") -> strchr(s, 'a')
604 if (HasS2 && S2.size() == 1)
605 return EmitStrChr(CI->getArgOperand(0), S2[0], B, TLI);
610 Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilder<> &B) {
611 Function *Callee = CI->getCalledFunction();
612 FunctionType *FT = Callee->getFunctionType();
613 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
614 !FT->getParamType(0)->isPointerTy() ||
615 !FT->getParamType(1)->isPointerTy())
618 Value *EndPtr = CI->getArgOperand(1);
619 if (isa<ConstantPointerNull>(EndPtr)) {
620 // With a null EndPtr, this function won't capture the main argument.
621 // It would be readonly too, except that it still may write to errno.
622 CI->addAttribute(1, Attribute::NoCapture);
628 Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilder<> &B) {
629 Function *Callee = CI->getCalledFunction();
630 FunctionType *FT = Callee->getFunctionType();
631 if (FT->getNumParams() != 2 || FT->getParamType(0) != B.getInt8PtrTy() ||
632 FT->getParamType(1) != FT->getParamType(0) ||
633 !FT->getReturnType()->isIntegerTy())
637 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
638 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
640 // strspn(s, "") -> 0
641 // strspn("", s) -> 0
642 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
643 return Constant::getNullValue(CI->getType());
646 if (HasS1 && HasS2) {
647 size_t Pos = S1.find_first_not_of(S2);
648 if (Pos == StringRef::npos)
650 return ConstantInt::get(CI->getType(), Pos);
656 Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilder<> &B) {
657 Function *Callee = CI->getCalledFunction();
658 FunctionType *FT = Callee->getFunctionType();
659 if (FT->getNumParams() != 2 || FT->getParamType(0) != B.getInt8PtrTy() ||
660 FT->getParamType(1) != FT->getParamType(0) ||
661 !FT->getReturnType()->isIntegerTy())
665 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
666 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
668 // strcspn("", s) -> 0
669 if (HasS1 && S1.empty())
670 return Constant::getNullValue(CI->getType());
673 if (HasS1 && HasS2) {
674 size_t Pos = S1.find_first_of(S2);
675 if (Pos == StringRef::npos)
677 return ConstantInt::get(CI->getType(), Pos);
680 // strcspn(s, "") -> strlen(s)
681 if (HasS2 && S2.empty())
682 return EmitStrLen(CI->getArgOperand(0), B, DL, TLI);
687 Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilder<> &B) {
688 Function *Callee = CI->getCalledFunction();
689 FunctionType *FT = Callee->getFunctionType();
690 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
691 !FT->getParamType(1)->isPointerTy() ||
692 !FT->getReturnType()->isPointerTy())
695 // fold strstr(x, x) -> x.
696 if (CI->getArgOperand(0) == CI->getArgOperand(1))
697 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
699 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
700 if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
701 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, DL, TLI);
704 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
708 for (auto UI = CI->user_begin(), UE = CI->user_end(); UI != UE;) {
709 ICmpInst *Old = cast<ICmpInst>(*UI++);
711 B.CreateICmp(Old->getPredicate(), StrNCmp,
712 ConstantInt::getNullValue(StrNCmp->getType()), "cmp");
713 replaceAllUsesWith(Old, Cmp);
718 // See if either input string is a constant string.
719 StringRef SearchStr, ToFindStr;
720 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
721 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
723 // fold strstr(x, "") -> x.
724 if (HasStr2 && ToFindStr.empty())
725 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
727 // If both strings are known, constant fold it.
728 if (HasStr1 && HasStr2) {
729 size_t Offset = SearchStr.find(ToFindStr);
731 if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
732 return Constant::getNullValue(CI->getType());
734 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
735 Value *Result = CastToCStr(CI->getArgOperand(0), B);
736 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
737 return B.CreateBitCast(Result, CI->getType());
740 // fold strstr(x, "y") -> strchr(x, 'y').
741 if (HasStr2 && ToFindStr.size() == 1) {
742 Value *StrChr = EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI);
743 return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr;
748 Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilder<> &B) {
749 Function *Callee = CI->getCalledFunction();
750 FunctionType *FT = Callee->getFunctionType();
751 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
752 !FT->getParamType(1)->isIntegerTy(32) ||
753 !FT->getParamType(2)->isIntegerTy() ||
754 !FT->getReturnType()->isPointerTy())
757 Value *SrcStr = CI->getArgOperand(0);
758 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
759 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
761 // memchr(x, y, 0) -> null
762 if (LenC && LenC->isNullValue())
763 return Constant::getNullValue(CI->getType());
765 // From now on we need at least constant length and string.
767 if (!LenC || !getConstantStringInfo(SrcStr, Str, 0, /*TrimAtNul=*/false))
770 // Truncate the string to LenC. If Str is smaller than LenC we will still only
771 // scan the string, as reading past the end of it is undefined and we can just
772 // return null if we don't find the char.
773 Str = Str.substr(0, LenC->getZExtValue());
775 // If the char is variable but the input str and length are not we can turn
776 // this memchr call into a simple bit field test. Of course this only works
777 // when the return value is only checked against null.
779 // It would be really nice to reuse switch lowering here but we can't change
780 // the CFG at this point.
782 // memchr("\r\n", C, 2) != nullptr -> (C & ((1 << '\r') | (1 << '\n'))) != 0
783 // after bounds check.
784 if (!CharC && !Str.empty() && isOnlyUsedInZeroEqualityComparison(CI)) {
786 *std::max_element(reinterpret_cast<const unsigned char *>(Str.begin()),
787 reinterpret_cast<const unsigned char *>(Str.end()));
789 // Make sure the bit field we're about to create fits in a register on the
791 // FIXME: On a 64 bit architecture this prevents us from using the
792 // interesting range of alpha ascii chars. We could do better by emitting
793 // two bitfields or shifting the range by 64 if no lower chars are used.
794 if (!DL.fitsInLegalInteger(Max + 1))
797 // For the bit field use a power-of-2 type with at least 8 bits to avoid
798 // creating unnecessary illegal types.
799 unsigned char Width = NextPowerOf2(std::max((unsigned char)7, Max));
801 // Now build the bit field.
802 APInt Bitfield(Width, 0);
804 Bitfield.setBit((unsigned char)C);
805 Value *BitfieldC = B.getInt(Bitfield);
807 // First check that the bit field access is within bounds.
808 Value *C = B.CreateZExtOrTrunc(CI->getArgOperand(1), BitfieldC->getType());
809 Value *Bounds = B.CreateICmp(ICmpInst::ICMP_ULT, C, B.getIntN(Width, Width),
812 // Create code that checks if the given bit is set in the field.
813 Value *Shl = B.CreateShl(B.getIntN(Width, 1ULL), C);
814 Value *Bits = B.CreateIsNotNull(B.CreateAnd(Shl, BitfieldC), "memchr.bits");
816 // Finally merge both checks and cast to pointer type. The inttoptr
817 // implicitly zexts the i1 to intptr type.
818 return B.CreateIntToPtr(B.CreateAnd(Bounds, Bits, "memchr"), CI->getType());
821 // Check if all arguments are constants. If so, we can constant fold.
825 // Compute the offset.
826 size_t I = Str.find(CharC->getSExtValue() & 0xFF);
827 if (I == StringRef::npos) // Didn't find the char. memchr returns null.
828 return Constant::getNullValue(CI->getType());
830 // memchr(s+n,c,l) -> gep(s+n+i,c)
831 return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "memchr");
834 Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilder<> &B) {
835 Function *Callee = CI->getCalledFunction();
836 FunctionType *FT = Callee->getFunctionType();
837 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
838 !FT->getParamType(1)->isPointerTy() ||
839 !FT->getReturnType()->isIntegerTy(32))
842 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
844 if (LHS == RHS) // memcmp(s,s,x) -> 0
845 return Constant::getNullValue(CI->getType());
847 // Make sure we have a constant length.
848 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
851 uint64_t Len = LenC->getZExtValue();
853 if (Len == 0) // memcmp(s1,s2,0) -> 0
854 return Constant::getNullValue(CI->getType());
856 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
858 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
859 CI->getType(), "lhsv");
860 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
861 CI->getType(), "rhsv");
862 return B.CreateSub(LHSV, RHSV, "chardiff");
865 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
866 StringRef LHSStr, RHSStr;
867 if (getConstantStringInfo(LHS, LHSStr) &&
868 getConstantStringInfo(RHS, RHSStr)) {
869 // Make sure we're not reading out-of-bounds memory.
870 if (Len > LHSStr.size() || Len > RHSStr.size())
872 // Fold the memcmp and normalize the result. This way we get consistent
873 // results across multiple platforms.
875 int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);
880 return ConstantInt::get(CI->getType(), Ret);
886 Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilder<> &B) {
887 Function *Callee = CI->getCalledFunction();
889 if (!checkStringCopyLibFuncSignature(Callee, LibFunc::memcpy))
892 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
893 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
894 CI->getArgOperand(2), 1);
895 return CI->getArgOperand(0);
898 Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilder<> &B) {
899 Function *Callee = CI->getCalledFunction();
901 if (!checkStringCopyLibFuncSignature(Callee, LibFunc::memmove))
904 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
905 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
906 CI->getArgOperand(2), 1);
907 return CI->getArgOperand(0);
910 Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B) {
911 Function *Callee = CI->getCalledFunction();
913 if (!checkStringCopyLibFuncSignature(Callee, LibFunc::memset))
916 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
917 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
918 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
919 return CI->getArgOperand(0);
922 //===----------------------------------------------------------------------===//
923 // Math Library Optimizations
924 //===----------------------------------------------------------------------===//
926 /// Return a variant of Val with float type.
927 /// Currently this works in two cases: If Val is an FPExtension of a float
928 /// value to something bigger, simply return the operand.
929 /// If Val is a ConstantFP but can be converted to a float ConstantFP without
930 /// loss of precision do so.
931 static Value *valueHasFloatPrecision(Value *Val) {
932 if (FPExtInst *Cast = dyn_cast<FPExtInst>(Val)) {
933 Value *Op = Cast->getOperand(0);
934 if (Op->getType()->isFloatTy())
937 if (ConstantFP *Const = dyn_cast<ConstantFP>(Val)) {
938 APFloat F = Const->getValueAPF();
940 (void)F.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
943 return ConstantFP::get(Const->getContext(), F);
948 //===----------------------------------------------------------------------===//
949 // Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
951 Value *LibCallSimplifier::optimizeUnaryDoubleFP(CallInst *CI, IRBuilder<> &B,
953 Function *Callee = CI->getCalledFunction();
954 FunctionType *FT = Callee->getFunctionType();
955 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
956 !FT->getParamType(0)->isDoubleTy())
960 // Check if all the uses for function like 'sin' are converted to float.
961 for (User *U : CI->users()) {
962 FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);
963 if (!Cast || !Cast->getType()->isFloatTy())
968 // If this is something like 'floor((double)floatval)', convert to floorf.
969 Value *V = valueHasFloatPrecision(CI->getArgOperand(0));
973 // floor((double)floatval) -> (double)floorf(floatval)
974 if (Callee->isIntrinsic()) {
975 Module *M = CI->getParent()->getParent()->getParent();
976 Intrinsic::ID IID = Callee->getIntrinsicID();
977 Function *F = Intrinsic::getDeclaration(M, IID, B.getFloatTy());
978 V = B.CreateCall(F, V);
980 // The call is a library call rather than an intrinsic.
981 V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
984 return B.CreateFPExt(V, B.getDoubleTy());
987 // Double -> Float Shrinking Optimizations for Binary Functions like 'fmin/fmax'
988 Value *LibCallSimplifier::optimizeBinaryDoubleFP(CallInst *CI, IRBuilder<> &B) {
989 Function *Callee = CI->getCalledFunction();
990 FunctionType *FT = Callee->getFunctionType();
991 // Just make sure this has 2 arguments of the same FP type, which match the
993 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
994 FT->getParamType(0) != FT->getParamType(1) ||
995 !FT->getParamType(0)->isFloatingPointTy())
998 // If this is something like 'fmin((double)floatval1, (double)floatval2)',
999 // or fmin(1.0, (double)floatval), then we convert it to fminf.
1000 Value *V1 = valueHasFloatPrecision(CI->getArgOperand(0));
1003 Value *V2 = valueHasFloatPrecision(CI->getArgOperand(1));
1007 // fmin((double)floatval1, (double)floatval2)
1008 // -> (double)fminf(floatval1, floatval2)
1009 // TODO: Handle intrinsics in the same way as in optimizeUnaryDoubleFP().
1010 Value *V = EmitBinaryFloatFnCall(V1, V2, Callee->getName(), B,
1011 Callee->getAttributes());
1012 return B.CreateFPExt(V, B.getDoubleTy());
1015 Value *LibCallSimplifier::optimizeCos(CallInst *CI, IRBuilder<> &B) {
1016 Function *Callee = CI->getCalledFunction();
1017 Value *Ret = nullptr;
1018 if (UnsafeFPShrink && Callee->getName() == "cos" && TLI->has(LibFunc::cosf)) {
1019 Ret = optimizeUnaryDoubleFP(CI, B, true);
1022 FunctionType *FT = Callee->getFunctionType();
1023 // Just make sure this has 1 argument of FP type, which matches the
1025 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1026 !FT->getParamType(0)->isFloatingPointTy())
1029 // cos(-x) -> cos(x)
1030 Value *Op1 = CI->getArgOperand(0);
1031 if (BinaryOperator::isFNeg(Op1)) {
1032 BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);
1033 return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");
1038 Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) {
1039 Function *Callee = CI->getCalledFunction();
1041 Value *Ret = nullptr;
1042 if (UnsafeFPShrink && Callee->getName() == "pow" && TLI->has(LibFunc::powf)) {
1043 Ret = optimizeUnaryDoubleFP(CI, B, true);
1046 FunctionType *FT = Callee->getFunctionType();
1047 // Just make sure this has 2 arguments of the same FP type, which match the
1049 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1050 FT->getParamType(0) != FT->getParamType(1) ||
1051 !FT->getParamType(0)->isFloatingPointTy())
1054 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
1055 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1056 // pow(1.0, x) -> 1.0
1057 if (Op1C->isExactlyValue(1.0))
1059 // pow(2.0, x) -> exp2(x)
1060 if (Op1C->isExactlyValue(2.0) &&
1061 hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp2, LibFunc::exp2f,
1063 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
1064 // pow(10.0, x) -> exp10(x)
1065 if (Op1C->isExactlyValue(10.0) &&
1066 hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp10, LibFunc::exp10f,
1068 return EmitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp10), B,
1069 Callee->getAttributes());
1072 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1076 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
1077 return ConstantFP::get(CI->getType(), 1.0);
1079 if (Op2C->isExactlyValue(0.5) &&
1080 hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::sqrt, LibFunc::sqrtf,
1082 hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::fabs, LibFunc::fabsf,
1084 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1085 // This is faster than calling pow, and still handles negative zero
1086 // and negative infinity correctly.
1087 // TODO: In fast-math mode, this could be just sqrt(x).
1088 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
1089 Value *Inf = ConstantFP::getInfinity(CI->getType());
1090 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
1091 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B, Callee->getAttributes());
1093 EmitUnaryFloatFnCall(Sqrt, "fabs", B, Callee->getAttributes());
1094 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
1095 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
1099 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1101 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
1102 return B.CreateFMul(Op1, Op1, "pow2");
1103 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
1104 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), Op1, "powrecip");
1108 Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilder<> &B) {
1109 Function *Callee = CI->getCalledFunction();
1110 Function *Caller = CI->getParent()->getParent();
1112 Value *Ret = nullptr;
1113 if (UnsafeFPShrink && Callee->getName() == "exp2" &&
1114 TLI->has(LibFunc::exp2f)) {
1115 Ret = optimizeUnaryDoubleFP(CI, B, true);
1118 FunctionType *FT = Callee->getFunctionType();
1119 // Just make sure this has 1 argument of FP type, which matches the
1121 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1122 !FT->getParamType(0)->isFloatingPointTy())
1125 Value *Op = CI->getArgOperand(0);
1126 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1127 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1128 LibFunc::Func LdExp = LibFunc::ldexpl;
1129 if (Op->getType()->isFloatTy())
1130 LdExp = LibFunc::ldexpf;
1131 else if (Op->getType()->isDoubleTy())
1132 LdExp = LibFunc::ldexp;
1134 if (TLI->has(LdExp)) {
1135 Value *LdExpArg = nullptr;
1136 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1137 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
1138 LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
1139 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1140 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
1141 LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
1145 Constant *One = ConstantFP::get(CI->getContext(), APFloat(1.0f));
1146 if (!Op->getType()->isFloatTy())
1147 One = ConstantExpr::getFPExtend(One, Op->getType());
1149 Module *M = Caller->getParent();
1151 M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(),
1152 Op->getType(), B.getInt32Ty(), nullptr);
1153 CallInst *CI = B.CreateCall(Callee, {One, LdExpArg});
1154 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1155 CI->setCallingConv(F->getCallingConv());
1163 Value *LibCallSimplifier::optimizeFabs(CallInst *CI, IRBuilder<> &B) {
1164 Function *Callee = CI->getCalledFunction();
1166 Value *Ret = nullptr;
1167 if (Callee->getName() == "fabs" && TLI->has(LibFunc::fabsf)) {
1168 Ret = optimizeUnaryDoubleFP(CI, B, false);
1171 FunctionType *FT = Callee->getFunctionType();
1172 // Make sure this has 1 argument of FP type which matches the result type.
1173 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1174 !FT->getParamType(0)->isFloatingPointTy())
1177 Value *Op = CI->getArgOperand(0);
1178 if (Instruction *I = dyn_cast<Instruction>(Op)) {
1179 // Fold fabs(x * x) -> x * x; any squared FP value must already be positive.
1180 if (I->getOpcode() == Instruction::FMul)
1181 if (I->getOperand(0) == I->getOperand(1))
1187 Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilder<> &B) {
1188 Function *Callee = CI->getCalledFunction();
1190 Value *Ret = nullptr;
1191 if (TLI->has(LibFunc::sqrtf) && (Callee->getName() == "sqrt" ||
1192 Callee->getIntrinsicID() == Intrinsic::sqrt))
1193 Ret = optimizeUnaryDoubleFP(CI, B, true);
1195 // FIXME: For finer-grain optimization, we need intrinsics to have the same
1196 // fast-math flag decorations that are applied to FP instructions. For now,
1197 // we have to rely on the function-level unsafe-fp-math attribute to do this
1198 // optimization because there's no other way to express that the sqrt can be
1200 Function *F = CI->getParent()->getParent();
1201 if (F->hasFnAttribute("unsafe-fp-math")) {
1202 // Check for unsafe-fp-math = true.
1203 Attribute Attr = F->getFnAttribute("unsafe-fp-math");
1204 if (Attr.getValueAsString() != "true")
1207 Value *Op = CI->getArgOperand(0);
1208 if (Instruction *I = dyn_cast<Instruction>(Op)) {
1209 if (I->getOpcode() == Instruction::FMul && I->hasUnsafeAlgebra()) {
1210 // We're looking for a repeated factor in a multiplication tree,
1211 // so we can do this fold: sqrt(x * x) -> fabs(x);
1212 // or this fold: sqrt(x * x * y) -> fabs(x) * sqrt(y).
1213 Value *Op0 = I->getOperand(0);
1214 Value *Op1 = I->getOperand(1);
1215 Value *RepeatOp = nullptr;
1216 Value *OtherOp = nullptr;
1218 // Simple match: the operands of the multiply are identical.
1221 // Look for a more complicated pattern: one of the operands is itself
1222 // a multiply, so search for a common factor in that multiply.
1223 // Note: We don't bother looking any deeper than this first level or for
1224 // variations of this pattern because instcombine's visitFMUL and/or the
1225 // reassociation pass should give us this form.
1226 Value *OtherMul0, *OtherMul1;
1227 if (match(Op0, m_FMul(m_Value(OtherMul0), m_Value(OtherMul1)))) {
1228 // Pattern: sqrt((x * y) * z)
1229 if (OtherMul0 == OtherMul1) {
1230 // Matched: sqrt((x * x) * z)
1231 RepeatOp = OtherMul0;
1237 // Fast math flags for any created instructions should match the sqrt
1239 // FIXME: We're not checking the sqrt because it doesn't have
1240 // fast-math-flags (see earlier comment).
1241 IRBuilder<true, ConstantFolder,
1242 IRBuilderDefaultInserter<true> >::FastMathFlagGuard Guard(B);
1243 B.SetFastMathFlags(I->getFastMathFlags());
1244 // If we found a repeated factor, hoist it out of the square root and
1245 // replace it with the fabs of that factor.
1246 Module *M = Callee->getParent();
1247 Type *ArgType = Op->getType();
1248 Value *Fabs = Intrinsic::getDeclaration(M, Intrinsic::fabs, ArgType);
1249 Value *FabsCall = B.CreateCall(Fabs, RepeatOp, "fabs");
1251 // If we found a non-repeated factor, we still need to get its square
1252 // root. We then multiply that by the value that was simplified out
1253 // of the square root calculation.
1254 Value *Sqrt = Intrinsic::getDeclaration(M, Intrinsic::sqrt, ArgType);
1255 Value *SqrtCall = B.CreateCall(Sqrt, OtherOp, "sqrt");
1256 return B.CreateFMul(FabsCall, SqrtCall);
1265 static bool isTrigLibCall(CallInst *CI);
1266 static void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,
1267 bool UseFloat, Value *&Sin, Value *&Cos,
1270 Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, IRBuilder<> &B) {
1272 // Make sure the prototype is as expected, otherwise the rest of the
1273 // function is probably invalid and likely to abort.
1274 if (!isTrigLibCall(CI))
1277 Value *Arg = CI->getArgOperand(0);
1278 SmallVector<CallInst *, 1> SinCalls;
1279 SmallVector<CallInst *, 1> CosCalls;
1280 SmallVector<CallInst *, 1> SinCosCalls;
1282 bool IsFloat = Arg->getType()->isFloatTy();
1284 // Look for all compatible sinpi, cospi and sincospi calls with the same
1285 // argument. If there are enough (in some sense) we can make the
1287 for (User *U : Arg->users())
1288 classifyArgUse(U, CI->getParent(), IsFloat, SinCalls, CosCalls,
1291 // It's only worthwhile if both sinpi and cospi are actually used.
1292 if (SinCosCalls.empty() && (SinCalls.empty() || CosCalls.empty()))
1295 Value *Sin, *Cos, *SinCos;
1296 insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos, SinCos);
1298 replaceTrigInsts(SinCalls, Sin);
1299 replaceTrigInsts(CosCalls, Cos);
1300 replaceTrigInsts(SinCosCalls, SinCos);
1305 static bool isTrigLibCall(CallInst *CI) {
1306 Function *Callee = CI->getCalledFunction();
1307 FunctionType *FT = Callee->getFunctionType();
1309 // We can only hope to do anything useful if we can ignore things like errno
1310 // and floating-point exceptions.
1311 bool AttributesSafe =
1312 CI->hasFnAttr(Attribute::NoUnwind) && CI->hasFnAttr(Attribute::ReadNone);
1314 // Other than that we need float(float) or double(double)
1315 return AttributesSafe && FT->getNumParams() == 1 &&
1316 FT->getReturnType() == FT->getParamType(0) &&
1317 (FT->getParamType(0)->isFloatTy() ||
1318 FT->getParamType(0)->isDoubleTy());
1322 LibCallSimplifier::classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,
1323 SmallVectorImpl<CallInst *> &SinCalls,
1324 SmallVectorImpl<CallInst *> &CosCalls,
1325 SmallVectorImpl<CallInst *> &SinCosCalls) {
1326 CallInst *CI = dyn_cast<CallInst>(Val);
1331 Function *Callee = CI->getCalledFunction();
1332 StringRef FuncName = Callee->getName();
1334 if (!TLI->getLibFunc(FuncName, Func) || !TLI->has(Func) || !isTrigLibCall(CI))
1338 if (Func == LibFunc::sinpif)
1339 SinCalls.push_back(CI);
1340 else if (Func == LibFunc::cospif)
1341 CosCalls.push_back(CI);
1342 else if (Func == LibFunc::sincospif_stret)
1343 SinCosCalls.push_back(CI);
1345 if (Func == LibFunc::sinpi)
1346 SinCalls.push_back(CI);
1347 else if (Func == LibFunc::cospi)
1348 CosCalls.push_back(CI);
1349 else if (Func == LibFunc::sincospi_stret)
1350 SinCosCalls.push_back(CI);
1354 void LibCallSimplifier::replaceTrigInsts(SmallVectorImpl<CallInst *> &Calls,
1356 for (SmallVectorImpl<CallInst *>::iterator I = Calls.begin(), E = Calls.end();
1358 replaceAllUsesWith(*I, Res);
1362 void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,
1363 bool UseFloat, Value *&Sin, Value *&Cos, Value *&SinCos) {
1364 Type *ArgTy = Arg->getType();
1368 Triple T(OrigCallee->getParent()->getTargetTriple());
1370 Name = "__sincospif_stret";
1372 assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");
1373 // x86_64 can't use {float, float} since that would be returned in both
1374 // xmm0 and xmm1, which isn't what a real struct would do.
1375 ResTy = T.getArch() == Triple::x86_64
1376 ? static_cast<Type *>(VectorType::get(ArgTy, 2))
1377 : static_cast<Type *>(StructType::get(ArgTy, ArgTy, nullptr));
1379 Name = "__sincospi_stret";
1380 ResTy = StructType::get(ArgTy, ArgTy, nullptr);
1383 Module *M = OrigCallee->getParent();
1384 Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(),
1385 ResTy, ArgTy, nullptr);
1387 if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
1388 // If the argument is an instruction, it must dominate all uses so put our
1389 // sincos call there.
1390 BasicBlock::iterator Loc = ArgInst;
1391 B.SetInsertPoint(ArgInst->getParent(), ++Loc);
1393 // Otherwise (e.g. for a constant) the beginning of the function is as
1394 // good a place as any.
1395 BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
1396 B.SetInsertPoint(&EntryBB, EntryBB.begin());
1399 SinCos = B.CreateCall(Callee, Arg, "sincospi");
1401 if (SinCos->getType()->isStructTy()) {
1402 Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
1403 Cos = B.CreateExtractValue(SinCos, 1, "cospi");
1405 Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
1407 Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
1412 //===----------------------------------------------------------------------===//
1413 // Integer Library Call Optimizations
1414 //===----------------------------------------------------------------------===//
1416 Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilder<> &B) {
1417 Function *Callee = CI->getCalledFunction();
1418 FunctionType *FT = Callee->getFunctionType();
1419 // Just make sure this has 2 arguments of the same FP type, which match the
1421 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy(32) ||
1422 !FT->getParamType(0)->isIntegerTy())
1425 Value *Op = CI->getArgOperand(0);
1428 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1429 if (CI->isZero()) // ffs(0) -> 0.
1430 return B.getInt32(0);
1431 // ffs(c) -> cttz(c)+1
1432 return B.getInt32(CI->getValue().countTrailingZeros() + 1);
1435 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1436 Type *ArgType = Op->getType();
1438 Intrinsic::getDeclaration(Callee->getParent(), Intrinsic::cttz, ArgType);
1439 Value *V = B.CreateCall(F, {Op, B.getFalse()}, "cttz");
1440 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
1441 V = B.CreateIntCast(V, B.getInt32Ty(), false);
1443 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
1444 return B.CreateSelect(Cond, V, B.getInt32(0));
1447 Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilder<> &B) {
1448 Function *Callee = CI->getCalledFunction();
1449 FunctionType *FT = Callee->getFunctionType();
1450 // We require integer(integer) where the types agree.
1451 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1452 FT->getParamType(0) != FT->getReturnType())
1455 // abs(x) -> x >s -1 ? x : -x
1456 Value *Op = CI->getArgOperand(0);
1458 B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()), "ispos");
1459 Value *Neg = B.CreateNeg(Op, "neg");
1460 return B.CreateSelect(Pos, Op, Neg);
1463 Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilder<> &B) {
1464 Function *Callee = CI->getCalledFunction();
1465 FunctionType *FT = Callee->getFunctionType();
1466 // We require integer(i32)
1467 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1468 !FT->getParamType(0)->isIntegerTy(32))
1471 // isdigit(c) -> (c-'0') <u 10
1472 Value *Op = CI->getArgOperand(0);
1473 Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
1474 Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
1475 return B.CreateZExt(Op, CI->getType());
1478 Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilder<> &B) {
1479 Function *Callee = CI->getCalledFunction();
1480 FunctionType *FT = Callee->getFunctionType();
1481 // We require integer(i32)
1482 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1483 !FT->getParamType(0)->isIntegerTy(32))
1486 // isascii(c) -> c <u 128
1487 Value *Op = CI->getArgOperand(0);
1488 Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
1489 return B.CreateZExt(Op, CI->getType());
1492 Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilder<> &B) {
1493 Function *Callee = CI->getCalledFunction();
1494 FunctionType *FT = Callee->getFunctionType();
1495 // We require i32(i32)
1496 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1497 !FT->getParamType(0)->isIntegerTy(32))
1500 // toascii(c) -> c & 0x7f
1501 return B.CreateAnd(CI->getArgOperand(0),
1502 ConstantInt::get(CI->getType(), 0x7F));
1505 //===----------------------------------------------------------------------===//
1506 // Formatting and IO Library Call Optimizations
1507 //===----------------------------------------------------------------------===//
1509 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
1511 Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilder<> &B,
1513 // Error reporting calls should be cold, mark them as such.
1514 // This applies even to non-builtin calls: it is only a hint and applies to
1515 // functions that the frontend might not understand as builtins.
1517 // This heuristic was suggested in:
1518 // Improving Static Branch Prediction in a Compiler
1519 // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu
1520 // Proceedings of PACT'98, Oct. 1998, IEEE
1521 Function *Callee = CI->getCalledFunction();
1523 if (!CI->hasFnAttr(Attribute::Cold) &&
1524 isReportingError(Callee, CI, StreamArg)) {
1525 CI->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold);
1531 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {
1532 if (!ColdErrorCalls)
1535 if (!Callee || !Callee->isDeclaration())
1541 // These functions might be considered cold, but only if their stream
1542 // argument is stderr.
1544 if (StreamArg >= (int)CI->getNumArgOperands())
1546 LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
1549 GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
1550 if (!GV || !GV->isDeclaration())
1552 return GV->getName() == "stderr";
1555 Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {
1556 // Check for a fixed format string.
1557 StringRef FormatStr;
1558 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
1561 // Empty format string -> noop.
1562 if (FormatStr.empty()) // Tolerate printf's declared void.
1563 return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);
1565 // Do not do any of the following transformations if the printf return value
1566 // is used, in general the printf return value is not compatible with either
1567 // putchar() or puts().
1568 if (!CI->use_empty())
1571 // printf("x") -> putchar('x'), even for '%'.
1572 if (FormatStr.size() == 1) {
1573 Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TLI);
1574 if (CI->use_empty() || !Res)
1576 return B.CreateIntCast(Res, CI->getType(), true);
1579 // printf("foo\n") --> puts("foo")
1580 if (FormatStr[FormatStr.size() - 1] == '\n' &&
1581 FormatStr.find('%') == StringRef::npos) { // No format characters.
1582 // Create a string literal with no \n on it. We expect the constant merge
1583 // pass to be run after this pass, to merge duplicate strings.
1584 FormatStr = FormatStr.drop_back();
1585 Value *GV = B.CreateGlobalString(FormatStr, "str");
1586 Value *NewCI = EmitPutS(GV, B, TLI);
1587 return (CI->use_empty() || !NewCI)
1589 : ConstantInt::get(CI->getType(), FormatStr.size() + 1);
1592 // Optimize specific format strings.
1593 // printf("%c", chr) --> putchar(chr)
1594 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
1595 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1596 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TLI);
1598 if (CI->use_empty() || !Res)
1600 return B.CreateIntCast(Res, CI->getType(), true);
1603 // printf("%s\n", str) --> puts(str)
1604 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
1605 CI->getArgOperand(1)->getType()->isPointerTy()) {
1606 return EmitPutS(CI->getArgOperand(1), B, TLI);
1611 Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilder<> &B) {
1613 Function *Callee = CI->getCalledFunction();
1614 // Require one fixed pointer argument and an integer/void result.
1615 FunctionType *FT = Callee->getFunctionType();
1616 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1617 !(FT->getReturnType()->isIntegerTy() || FT->getReturnType()->isVoidTy()))
1620 if (Value *V = optimizePrintFString(CI, B)) {
1624 // printf(format, ...) -> iprintf(format, ...) if no floating point
1626 if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) {
1627 Module *M = B.GetInsertBlock()->getParent()->getParent();
1628 Constant *IPrintFFn =
1629 M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
1630 CallInst *New = cast<CallInst>(CI->clone());
1631 New->setCalledFunction(IPrintFFn);
1638 Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI, IRBuilder<> &B) {
1639 // Check for a fixed format string.
1640 StringRef FormatStr;
1641 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1644 // If we just have a format string (nothing else crazy) transform it.
1645 if (CI->getNumArgOperands() == 2) {
1646 // Make sure there's no % in the constant array. We could try to handle
1647 // %% -> % in the future if we cared.
1648 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1649 if (FormatStr[i] == '%')
1650 return nullptr; // we found a format specifier, bail out.
1652 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1653 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1654 ConstantInt::get(DL.getIntPtrType(CI->getContext()),
1655 FormatStr.size() + 1),
1656 1); // Copy the null byte.
1657 return ConstantInt::get(CI->getType(), FormatStr.size());
1660 // The remaining optimizations require the format string to be "%s" or "%c"
1661 // and have an extra operand.
1662 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1663 CI->getNumArgOperands() < 3)
1666 // Decode the second character of the format string.
1667 if (FormatStr[1] == 'c') {
1668 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1669 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
1671 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
1672 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
1673 B.CreateStore(V, Ptr);
1674 Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
1675 B.CreateStore(B.getInt8(0), Ptr);
1677 return ConstantInt::get(CI->getType(), 1);
1680 if (FormatStr[1] == 's') {
1681 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1682 if (!CI->getArgOperand(2)->getType()->isPointerTy())
1685 Value *Len = EmitStrLen(CI->getArgOperand(2), B, DL, TLI);
1689 B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
1690 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
1692 // The sprintf result is the unincremented number of bytes in the string.
1693 return B.CreateIntCast(Len, CI->getType(), false);
1698 Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilder<> &B) {
1699 Function *Callee = CI->getCalledFunction();
1700 // Require two fixed pointer arguments and an integer result.
1701 FunctionType *FT = Callee->getFunctionType();
1702 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1703 !FT->getParamType(1)->isPointerTy() ||
1704 !FT->getReturnType()->isIntegerTy())
1707 if (Value *V = optimizeSPrintFString(CI, B)) {
1711 // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
1713 if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) {
1714 Module *M = B.GetInsertBlock()->getParent()->getParent();
1715 Constant *SIPrintFFn =
1716 M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
1717 CallInst *New = cast<CallInst>(CI->clone());
1718 New->setCalledFunction(SIPrintFFn);
1725 Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI, IRBuilder<> &B) {
1726 optimizeErrorReporting(CI, B, 0);
1728 // All the optimizations depend on the format string.
1729 StringRef FormatStr;
1730 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1733 // Do not do any of the following transformations if the fprintf return
1734 // value is used, in general the fprintf return value is not compatible
1735 // with fwrite(), fputc() or fputs().
1736 if (!CI->use_empty())
1739 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1740 if (CI->getNumArgOperands() == 2) {
1741 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1742 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
1743 return nullptr; // We found a format specifier.
1746 CI->getArgOperand(1),
1747 ConstantInt::get(DL.getIntPtrType(CI->getContext()), FormatStr.size()),
1748 CI->getArgOperand(0), B, DL, TLI);
1751 // The remaining optimizations require the format string to be "%s" or "%c"
1752 // and have an extra operand.
1753 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1754 CI->getNumArgOperands() < 3)
1757 // Decode the second character of the format string.
1758 if (FormatStr[1] == 'c') {
1759 // fprintf(F, "%c", chr) --> fputc(chr, F)
1760 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
1762 return EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI);
1765 if (FormatStr[1] == 's') {
1766 // fprintf(F, "%s", str) --> fputs(str, F)
1767 if (!CI->getArgOperand(2)->getType()->isPointerTy())
1769 return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI);
1774 Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilder<> &B) {
1775 Function *Callee = CI->getCalledFunction();
1776 // Require two fixed paramters as pointers and integer result.
1777 FunctionType *FT = Callee->getFunctionType();
1778 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1779 !FT->getParamType(1)->isPointerTy() ||
1780 !FT->getReturnType()->isIntegerTy())
1783 if (Value *V = optimizeFPrintFString(CI, B)) {
1787 // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
1788 // floating point arguments.
1789 if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) {
1790 Module *M = B.GetInsertBlock()->getParent()->getParent();
1791 Constant *FIPrintFFn =
1792 M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
1793 CallInst *New = cast<CallInst>(CI->clone());
1794 New->setCalledFunction(FIPrintFFn);
1801 Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilder<> &B) {
1802 optimizeErrorReporting(CI, B, 3);
1804 Function *Callee = CI->getCalledFunction();
1805 // Require a pointer, an integer, an integer, a pointer, returning integer.
1806 FunctionType *FT = Callee->getFunctionType();
1807 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1808 !FT->getParamType(1)->isIntegerTy() ||
1809 !FT->getParamType(2)->isIntegerTy() ||
1810 !FT->getParamType(3)->isPointerTy() ||
1811 !FT->getReturnType()->isIntegerTy())
1814 // Get the element size and count.
1815 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1816 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1817 if (!SizeC || !CountC)
1819 uint64_t Bytes = SizeC->getZExtValue() * CountC->getZExtValue();
1821 // If this is writing zero records, remove the call (it's a noop).
1823 return ConstantInt::get(CI->getType(), 0);
1825 // If this is writing one byte, turn it into fputc.
1826 // This optimisation is only valid, if the return value is unused.
1827 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1828 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1829 Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, TLI);
1830 return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
1836 Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilder<> &B) {
1837 optimizeErrorReporting(CI, B, 1);
1839 Function *Callee = CI->getCalledFunction();
1841 // Require two pointers. Also, we can't optimize if return value is used.
1842 FunctionType *FT = Callee->getFunctionType();
1843 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1844 !FT->getParamType(1)->isPointerTy() || !CI->use_empty())
1847 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1848 uint64_t Len = GetStringLength(CI->getArgOperand(0));
1852 // Known to have no uses (see above).
1854 CI->getArgOperand(0),
1855 ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len - 1),
1856 CI->getArgOperand(1), B, DL, TLI);
1859 Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilder<> &B) {
1860 Function *Callee = CI->getCalledFunction();
1861 // Require one fixed pointer argument and an integer/void result.
1862 FunctionType *FT = Callee->getFunctionType();
1863 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1864 !(FT->getReturnType()->isIntegerTy() || FT->getReturnType()->isVoidTy()))
1867 // Check for a constant string.
1869 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
1872 if (Str.empty() && CI->use_empty()) {
1873 // puts("") -> putchar('\n')
1874 Value *Res = EmitPutChar(B.getInt32('\n'), B, TLI);
1875 if (CI->use_empty() || !Res)
1877 return B.CreateIntCast(Res, CI->getType(), true);
1883 bool LibCallSimplifier::hasFloatVersion(StringRef FuncName) {
1885 SmallString<20> FloatFuncName = FuncName;
1886 FloatFuncName += 'f';
1887 if (TLI->getLibFunc(FloatFuncName, Func))
1888 return TLI->has(Func);
1892 Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI,
1893 IRBuilder<> &Builder) {
1895 Function *Callee = CI->getCalledFunction();
1896 StringRef FuncName = Callee->getName();
1898 // Check for string/memory library functions.
1899 if (TLI->getLibFunc(FuncName, Func) && TLI->has(Func)) {
1900 // Make sure we never change the calling convention.
1901 assert((ignoreCallingConv(Func) ||
1902 CI->getCallingConv() == llvm::CallingConv::C) &&
1903 "Optimizing string/memory libcall would change the calling convention");
1905 case LibFunc::strcat:
1906 return optimizeStrCat(CI, Builder);
1907 case LibFunc::strncat:
1908 return optimizeStrNCat(CI, Builder);
1909 case LibFunc::strchr:
1910 return optimizeStrChr(CI, Builder);
1911 case LibFunc::strrchr:
1912 return optimizeStrRChr(CI, Builder);
1913 case LibFunc::strcmp:
1914 return optimizeStrCmp(CI, Builder);
1915 case LibFunc::strncmp:
1916 return optimizeStrNCmp(CI, Builder);
1917 case LibFunc::strcpy:
1918 return optimizeStrCpy(CI, Builder);
1919 case LibFunc::stpcpy:
1920 return optimizeStpCpy(CI, Builder);
1921 case LibFunc::strncpy:
1922 return optimizeStrNCpy(CI, Builder);
1923 case LibFunc::strlen:
1924 return optimizeStrLen(CI, Builder);
1925 case LibFunc::strpbrk:
1926 return optimizeStrPBrk(CI, Builder);
1927 case LibFunc::strtol:
1928 case LibFunc::strtod:
1929 case LibFunc::strtof:
1930 case LibFunc::strtoul:
1931 case LibFunc::strtoll:
1932 case LibFunc::strtold:
1933 case LibFunc::strtoull:
1934 return optimizeStrTo(CI, Builder);
1935 case LibFunc::strspn:
1936 return optimizeStrSpn(CI, Builder);
1937 case LibFunc::strcspn:
1938 return optimizeStrCSpn(CI, Builder);
1939 case LibFunc::strstr:
1940 return optimizeStrStr(CI, Builder);
1941 case LibFunc::memchr:
1942 return optimizeMemChr(CI, Builder);
1943 case LibFunc::memcmp:
1944 return optimizeMemCmp(CI, Builder);
1945 case LibFunc::memcpy:
1946 return optimizeMemCpy(CI, Builder);
1947 case LibFunc::memmove:
1948 return optimizeMemMove(CI, Builder);
1949 case LibFunc::memset:
1950 return optimizeMemSet(CI, Builder);
1958 Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
1959 if (CI->isNoBuiltin())
1963 Function *Callee = CI->getCalledFunction();
1964 StringRef FuncName = Callee->getName();
1965 IRBuilder<> Builder(CI);
1966 bool isCallingConvC = CI->getCallingConv() == llvm::CallingConv::C;
1968 // Command-line parameter overrides function attribute.
1969 if (EnableUnsafeFPShrink.getNumOccurrences() > 0)
1970 UnsafeFPShrink = EnableUnsafeFPShrink;
1971 else if (Callee->hasFnAttribute("unsafe-fp-math")) {
1972 // FIXME: This is the same problem as described in optimizeSqrt().
1973 // If calls gain access to IR-level FMF, then use that instead of a
1974 // function attribute.
1976 // Check for unsafe-fp-math = true.
1977 Attribute Attr = Callee->getFnAttribute("unsafe-fp-math");
1978 if (Attr.getValueAsString() == "true")
1979 UnsafeFPShrink = true;
1982 // First, check for intrinsics.
1983 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
1984 if (!isCallingConvC)
1986 switch (II->getIntrinsicID()) {
1987 case Intrinsic::pow:
1988 return optimizePow(CI, Builder);
1989 case Intrinsic::exp2:
1990 return optimizeExp2(CI, Builder);
1991 case Intrinsic::fabs:
1992 return optimizeFabs(CI, Builder);
1993 case Intrinsic::sqrt:
1994 return optimizeSqrt(CI, Builder);
2000 // Also try to simplify calls to fortified library functions.
2001 if (Value *SimplifiedFortifiedCI = FortifiedSimplifier.optimizeCall(CI)) {
2002 // Try to further simplify the result.
2003 CallInst *SimplifiedCI = dyn_cast<CallInst>(SimplifiedFortifiedCI);
2004 if (SimplifiedCI && SimplifiedCI->getCalledFunction())
2005 if (Value *V = optimizeStringMemoryLibCall(SimplifiedCI, Builder)) {
2006 // If we were able to further simplify, remove the now redundant call.
2007 SimplifiedCI->replaceAllUsesWith(V);
2008 SimplifiedCI->eraseFromParent();
2011 return SimplifiedFortifiedCI;
2014 // Then check for known library functions.
2015 if (TLI->getLibFunc(FuncName, Func) && TLI->has(Func)) {
2016 // We never change the calling convention.
2017 if (!ignoreCallingConv(Func) && !isCallingConvC)
2019 if (Value *V = optimizeStringMemoryLibCall(CI, Builder))
2025 return optimizeCos(CI, Builder);
2026 case LibFunc::sinpif:
2027 case LibFunc::sinpi:
2028 case LibFunc::cospif:
2029 case LibFunc::cospi:
2030 return optimizeSinCosPi(CI, Builder);
2034 return optimizePow(CI, Builder);
2035 case LibFunc::exp2l:
2037 case LibFunc::exp2f:
2038 return optimizeExp2(CI, Builder);
2039 case LibFunc::fabsf:
2041 case LibFunc::fabsl:
2042 return optimizeFabs(CI, Builder);
2043 case LibFunc::sqrtf:
2045 case LibFunc::sqrtl:
2046 return optimizeSqrt(CI, Builder);
2049 case LibFunc::ffsll:
2050 return optimizeFFS(CI, Builder);
2053 case LibFunc::llabs:
2054 return optimizeAbs(CI, Builder);
2055 case LibFunc::isdigit:
2056 return optimizeIsDigit(CI, Builder);
2057 case LibFunc::isascii:
2058 return optimizeIsAscii(CI, Builder);
2059 case LibFunc::toascii:
2060 return optimizeToAscii(CI, Builder);
2061 case LibFunc::printf:
2062 return optimizePrintF(CI, Builder);
2063 case LibFunc::sprintf:
2064 return optimizeSPrintF(CI, Builder);
2065 case LibFunc::fprintf:
2066 return optimizeFPrintF(CI, Builder);
2067 case LibFunc::fwrite:
2068 return optimizeFWrite(CI, Builder);
2069 case LibFunc::fputs:
2070 return optimizeFPuts(CI, Builder);
2072 return optimizePuts(CI, Builder);
2073 case LibFunc::perror:
2074 return optimizeErrorReporting(CI, Builder);
2075 case LibFunc::vfprintf:
2076 case LibFunc::fiprintf:
2077 return optimizeErrorReporting(CI, Builder, 0);
2078 case LibFunc::fputc:
2079 return optimizeErrorReporting(CI, Builder, 1);
2081 case LibFunc::floor:
2083 case LibFunc::round:
2084 case LibFunc::nearbyint:
2085 case LibFunc::trunc:
2086 if (hasFloatVersion(FuncName))
2087 return optimizeUnaryDoubleFP(CI, Builder, false);
2090 case LibFunc::acosh:
2092 case LibFunc::asinh:
2094 case LibFunc::atanh:
2098 case LibFunc::exp10:
2099 case LibFunc::expm1:
2101 case LibFunc::log10:
2102 case LibFunc::log1p:
2109 if (UnsafeFPShrink && hasFloatVersion(FuncName))
2110 return optimizeUnaryDoubleFP(CI, Builder, true);
2112 case LibFunc::copysign:
2115 if (hasFloatVersion(FuncName))
2116 return optimizeBinaryDoubleFP(CI, Builder);
2125 LibCallSimplifier::LibCallSimplifier(
2126 const DataLayout &DL, const TargetLibraryInfo *TLI,
2127 function_ref<void(Instruction *, Value *)> Replacer)
2128 : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), UnsafeFPShrink(false),
2129 Replacer(Replacer) {}
2131 void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) {
2132 // Indirect through the replacer used in this instance.
2136 /*static*/ void LibCallSimplifier::replaceAllUsesWithDefault(Instruction *I,
2138 I->replaceAllUsesWith(With);
2139 I->eraseFromParent();
2143 // Additional cases that we need to add to this file:
2146 // * cbrt(expN(X)) -> expN(x/3)
2147 // * cbrt(sqrt(x)) -> pow(x,1/6)
2148 // * cbrt(sqrt(x)) -> pow(x,1/9)
2151 // * exp(log(x)) -> x
2154 // * log(exp(x)) -> x
2155 // * log(x**y) -> y*log(x)
2156 // * log(exp(y)) -> y*log(e)
2157 // * log(exp2(y)) -> y*log(2)
2158 // * log(exp10(y)) -> y*log(10)
2159 // * log(sqrt(x)) -> 0.5*log(x)
2160 // * log(pow(x,y)) -> y*log(x)
2162 // lround, lroundf, lroundl:
2163 // * lround(cnst) -> cnst'
2166 // * pow(exp(x),y) -> exp(x*y)
2167 // * pow(sqrt(x),y) -> pow(x,y*0.5)
2168 // * pow(pow(x,y),z)-> pow(x,y*z)
2170 // round, roundf, roundl:
2171 // * round(cnst) -> cnst'
2174 // * signbit(cnst) -> cnst'
2175 // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2177 // sqrt, sqrtf, sqrtl:
2178 // * sqrt(expN(x)) -> expN(x*0.5)
2179 // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2180 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2183 // * tan(atan(x)) -> x
2185 // trunc, truncf, truncl:
2186 // * trunc(cnst) -> cnst'
2190 //===----------------------------------------------------------------------===//
2191 // Fortified Library Call Optimizations
2192 //===----------------------------------------------------------------------===//
2194 bool FortifiedLibCallSimplifier::isFortifiedCallFoldable(CallInst *CI,
2198 if (CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(SizeOp))
2200 if (ConstantInt *ObjSizeCI =
2201 dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) {
2202 if (ObjSizeCI->isAllOnesValue())
2204 // If the object size wasn't -1 (unknown), bail out if we were asked to.
2205 if (OnlyLowerUnknownSize)
2208 uint64_t Len = GetStringLength(CI->getArgOperand(SizeOp));
2209 // If the length is 0 we don't know how long it is and so we can't
2210 // remove the check.
2213 return ObjSizeCI->getZExtValue() >= Len;
2215 if (ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getArgOperand(SizeOp)))
2216 return ObjSizeCI->getZExtValue() >= SizeCI->getZExtValue();
2221 Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI, IRBuilder<> &B) {
2222 Function *Callee = CI->getCalledFunction();
2224 if (!checkStringCopyLibFuncSignature(Callee, LibFunc::memcpy_chk))
2227 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
2228 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
2229 CI->getArgOperand(2), 1);
2230 return CI->getArgOperand(0);
2235 Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI, IRBuilder<> &B) {
2236 Function *Callee = CI->getCalledFunction();
2238 if (!checkStringCopyLibFuncSignature(Callee, LibFunc::memmove_chk))
2241 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
2242 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
2243 CI->getArgOperand(2), 1);
2244 return CI->getArgOperand(0);
2249 Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI, IRBuilder<> &B) {
2250 Function *Callee = CI->getCalledFunction();
2252 if (!checkStringCopyLibFuncSignature(Callee, LibFunc::memset_chk))
2255 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
2256 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
2257 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
2258 return CI->getArgOperand(0);
2263 Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
2265 LibFunc::Func Func) {
2266 Function *Callee = CI->getCalledFunction();
2267 StringRef Name = Callee->getName();
2268 const DataLayout &DL = CI->getModule()->getDataLayout();
2270 if (!checkStringCopyLibFuncSignature(Callee, Func))
2273 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
2274 *ObjSize = CI->getArgOperand(2);
2276 // __stpcpy_chk(x,x,...) -> x+strlen(x)
2277 if (Func == LibFunc::stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) {
2278 Value *StrLen = EmitStrLen(Src, B, DL, TLI);
2279 return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
2282 // If a) we don't have any length information, or b) we know this will
2283 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
2284 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
2285 // TODO: It might be nice to get a maximum length out of the possible
2286 // string lengths for varying.
2287 if (isFortifiedCallFoldable(CI, 2, 1, true))
2288 return EmitStrCpy(Dst, Src, B, TLI, Name.substr(2, 6));
2290 if (OnlyLowerUnknownSize)
2293 // Maybe we can stil fold __st[rp]cpy_chk to __memcpy_chk.
2294 uint64_t Len = GetStringLength(Src);
2298 Type *SizeTTy = DL.getIntPtrType(CI->getContext());
2299 Value *LenV = ConstantInt::get(SizeTTy, Len);
2300 Value *Ret = EmitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI);
2301 // If the function was an __stpcpy_chk, and we were able to fold it into
2302 // a __memcpy_chk, we still need to return the correct end pointer.
2303 if (Ret && Func == LibFunc::stpcpy_chk)
2304 return B.CreateGEP(B.getInt8Ty(), Dst, ConstantInt::get(SizeTTy, Len - 1));
2308 Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
2310 LibFunc::Func Func) {
2311 Function *Callee = CI->getCalledFunction();
2312 StringRef Name = Callee->getName();
2314 if (!checkStringCopyLibFuncSignature(Callee, Func))
2316 if (isFortifiedCallFoldable(CI, 3, 2, false)) {
2317 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
2318 CI->getArgOperand(2), B, TLI, Name.substr(2, 7));
2324 Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI) {
2325 // FIXME: We shouldn't be changing "nobuiltin" or TLI unavailable calls here.
2326 // Some clang users checked for _chk libcall availability using:
2327 // __has_builtin(__builtin___memcpy_chk)
2328 // When compiling with -fno-builtin, this is always true.
2329 // When passing -ffreestanding/-mkernel, which both imply -fno-builtin, we
2330 // end up with fortified libcalls, which isn't acceptable in a freestanding
2331 // environment which only provides their non-fortified counterparts.
2333 // Until we change clang and/or teach external users to check for availability
2334 // differently, disregard the "nobuiltin" attribute and TLI::has.
2339 Function *Callee = CI->getCalledFunction();
2340 StringRef FuncName = Callee->getName();
2341 IRBuilder<> Builder(CI);
2342 bool isCallingConvC = CI->getCallingConv() == llvm::CallingConv::C;
2344 // First, check that this is a known library functions.
2345 if (!TLI->getLibFunc(FuncName, Func))
2348 // We never change the calling convention.
2349 if (!ignoreCallingConv(Func) && !isCallingConvC)
2353 case LibFunc::memcpy_chk:
2354 return optimizeMemCpyChk(CI, Builder);
2355 case LibFunc::memmove_chk:
2356 return optimizeMemMoveChk(CI, Builder);
2357 case LibFunc::memset_chk:
2358 return optimizeMemSetChk(CI, Builder);
2359 case LibFunc::stpcpy_chk:
2360 case LibFunc::strcpy_chk:
2361 return optimizeStrpCpyChk(CI, Builder, Func);
2362 case LibFunc::stpncpy_chk:
2363 case LibFunc::strncpy_chk:
2364 return optimizeStrpNCpyChk(CI, Builder, Func);
2371 FortifiedLibCallSimplifier::FortifiedLibCallSimplifier(
2372 const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
2373 : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}