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/StringMap.h"
19 #include "llvm/Analysis/ValueTracking.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/Intrinsics.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/Support/Allocator.h"
27 #include "llvm/Target/TargetLibraryInfo.h"
28 #include "llvm/Transforms/Utils/BuildLibCalls.h"
32 /// This class is the abstract base class for the set of optimizations that
33 /// corresponds to one library call.
35 class LibCallOptimization {
39 const TargetLibraryInfo *TLI;
40 const LibCallSimplifier *LCS;
43 LibCallOptimization() { }
44 virtual ~LibCallOptimization() {}
46 /// callOptimizer - This pure virtual method is implemented by base classes to
47 /// do various optimizations. If this returns null then no transformation was
48 /// performed. If it returns CI, then it transformed the call and CI is to be
49 /// deleted. If it returns something else, replace CI with the new value and
51 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
54 /// ignoreCallingConv - Returns false if this transformation could possibly
55 /// change the calling convention.
56 virtual bool ignoreCallingConv() { return false; }
58 Value *optimizeCall(CallInst *CI, const DataLayout *TD,
59 const TargetLibraryInfo *TLI,
60 const LibCallSimplifier *LCS, IRBuilder<> &B) {
61 Caller = CI->getParent()->getParent();
65 if (CI->getCalledFunction())
66 Context = &CI->getCalledFunction()->getContext();
68 // We never change the calling convention.
69 if (!ignoreCallingConv() && CI->getCallingConv() != llvm::CallingConv::C)
72 return callOptimizer(CI->getCalledFunction(), CI, B);
76 //===----------------------------------------------------------------------===//
78 //===----------------------------------------------------------------------===//
80 /// isOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
81 /// value is equal or not-equal to zero.
82 static bool isOnlyUsedInZeroEqualityComparison(Value *V) {
83 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
85 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
87 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
90 // Unknown instruction.
96 /// isOnlyUsedInEqualityComparison - Return true if it is only used in equality
97 /// comparisons with With.
98 static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
99 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
101 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
102 if (IC->isEquality() && IC->getOperand(1) == With)
104 // Unknown instruction.
110 static bool callHasFloatingPointArgument(const CallInst *CI) {
111 for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
113 if ((*it)->getType()->isFloatingPointTy())
119 //===----------------------------------------------------------------------===//
120 // Fortified Library Call Optimizations
121 //===----------------------------------------------------------------------===//
123 struct FortifiedLibCallOptimization : public LibCallOptimization {
125 virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
126 bool isString) const = 0;
129 struct InstFortifiedLibCallOptimization : public FortifiedLibCallOptimization {
132 bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, bool isString) const {
133 if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))
135 if (ConstantInt *SizeCI =
136 dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {
137 if (SizeCI->isAllOnesValue())
140 uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));
141 // If the length is 0 we don't know how long it is and so we can't
143 if (Len == 0) return false;
144 return SizeCI->getZExtValue() >= Len;
146 if (ConstantInt *Arg = dyn_cast<ConstantInt>(
147 CI->getArgOperand(SizeArgOp)))
148 return SizeCI->getZExtValue() >= Arg->getZExtValue();
154 struct MemCpyChkOpt : public InstFortifiedLibCallOptimization {
155 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
157 FunctionType *FT = Callee->getFunctionType();
158 LLVMContext &Context = CI->getParent()->getContext();
160 // Check if this has the right signature.
161 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
162 !FT->getParamType(0)->isPointerTy() ||
163 !FT->getParamType(1)->isPointerTy() ||
164 FT->getParamType(2) != TD->getIntPtrType(Context) ||
165 FT->getParamType(3) != TD->getIntPtrType(Context))
168 if (isFoldable(3, 2, false)) {
169 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
170 CI->getArgOperand(2), 1);
171 return CI->getArgOperand(0);
177 struct MemMoveChkOpt : public InstFortifiedLibCallOptimization {
178 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
180 FunctionType *FT = Callee->getFunctionType();
181 LLVMContext &Context = CI->getParent()->getContext();
183 // Check if this has the right signature.
184 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
185 !FT->getParamType(0)->isPointerTy() ||
186 !FT->getParamType(1)->isPointerTy() ||
187 FT->getParamType(2) != TD->getIntPtrType(Context) ||
188 FT->getParamType(3) != TD->getIntPtrType(Context))
191 if (isFoldable(3, 2, false)) {
192 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
193 CI->getArgOperand(2), 1);
194 return CI->getArgOperand(0);
200 struct MemSetChkOpt : public InstFortifiedLibCallOptimization {
201 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
203 FunctionType *FT = Callee->getFunctionType();
204 LLVMContext &Context = CI->getParent()->getContext();
206 // Check if this has the right signature.
207 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
208 !FT->getParamType(0)->isPointerTy() ||
209 !FT->getParamType(1)->isIntegerTy() ||
210 FT->getParamType(2) != TD->getIntPtrType(Context) ||
211 FT->getParamType(3) != TD->getIntPtrType(Context))
214 if (isFoldable(3, 2, false)) {
215 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
217 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
218 return CI->getArgOperand(0);
224 struct StrCpyChkOpt : public InstFortifiedLibCallOptimization {
225 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
227 StringRef Name = Callee->getName();
228 FunctionType *FT = Callee->getFunctionType();
229 LLVMContext &Context = CI->getParent()->getContext();
231 // Check if this has the right signature.
232 if (FT->getNumParams() != 3 ||
233 FT->getReturnType() != FT->getParamType(0) ||
234 FT->getParamType(0) != FT->getParamType(1) ||
235 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
236 FT->getParamType(2) != TD->getIntPtrType(Context))
239 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
240 if (Dst == Src) // __strcpy_chk(x,x) -> x
243 // If a) we don't have any length information, or b) we know this will
244 // fit then just lower to a plain strcpy. Otherwise we'll keep our
245 // strcpy_chk call which may fail at runtime if the size is too long.
246 // TODO: It might be nice to get a maximum length out of the possible
247 // string lengths for varying.
248 if (isFoldable(2, 1, true)) {
249 Value *Ret = EmitStrCpy(Dst, Src, B, TD, TLI, Name.substr(2, 6));
252 // Maybe we can stil fold __strcpy_chk to __memcpy_chk.
253 uint64_t Len = GetStringLength(Src);
254 if (Len == 0) return 0;
256 // This optimization require DataLayout.
260 EmitMemCpyChk(Dst, Src,
261 ConstantInt::get(TD->getIntPtrType(Context), Len),
262 CI->getArgOperand(2), B, TD, TLI);
269 struct StpCpyChkOpt : public InstFortifiedLibCallOptimization {
270 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
272 StringRef Name = Callee->getName();
273 FunctionType *FT = Callee->getFunctionType();
274 LLVMContext &Context = CI->getParent()->getContext();
276 // Check if this has the right signature.
277 if (FT->getNumParams() != 3 ||
278 FT->getReturnType() != FT->getParamType(0) ||
279 FT->getParamType(0) != FT->getParamType(1) ||
280 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
281 FT->getParamType(2) != TD->getIntPtrType(FT->getParamType(0)))
284 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
285 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
286 Value *StrLen = EmitStrLen(Src, B, TD, TLI);
287 return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
290 // If a) we don't have any length information, or b) we know this will
291 // fit then just lower to a plain stpcpy. Otherwise we'll keep our
292 // stpcpy_chk call which may fail at runtime if the size is too long.
293 // TODO: It might be nice to get a maximum length out of the possible
294 // string lengths for varying.
295 if (isFoldable(2, 1, true)) {
296 Value *Ret = EmitStrCpy(Dst, Src, B, TD, TLI, Name.substr(2, 6));
299 // Maybe we can stil fold __stpcpy_chk to __memcpy_chk.
300 uint64_t Len = GetStringLength(Src);
301 if (Len == 0) return 0;
303 // This optimization require DataLayout.
306 Type *PT = FT->getParamType(0);
307 Value *LenV = ConstantInt::get(TD->getIntPtrType(PT), Len);
308 Value *DstEnd = B.CreateGEP(Dst,
309 ConstantInt::get(TD->getIntPtrType(PT),
311 if (!EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, TD, TLI))
319 struct StrNCpyChkOpt : public InstFortifiedLibCallOptimization {
320 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
322 StringRef Name = Callee->getName();
323 FunctionType *FT = Callee->getFunctionType();
324 LLVMContext &Context = CI->getParent()->getContext();
326 // Check if this has the right signature.
327 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
328 FT->getParamType(0) != FT->getParamType(1) ||
329 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
330 !FT->getParamType(2)->isIntegerTy() ||
331 FT->getParamType(3) != TD->getIntPtrType(Context))
334 if (isFoldable(3, 2, false)) {
335 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
336 CI->getArgOperand(2), B, TD, TLI,
344 //===----------------------------------------------------------------------===//
345 // String and Memory Library Call Optimizations
346 //===----------------------------------------------------------------------===//
348 struct StrCatOpt : public LibCallOptimization {
349 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
350 // Verify the "strcat" function prototype.
351 FunctionType *FT = Callee->getFunctionType();
352 if (FT->getNumParams() != 2 ||
353 FT->getReturnType() != B.getInt8PtrTy() ||
354 FT->getParamType(0) != FT->getReturnType() ||
355 FT->getParamType(1) != FT->getReturnType())
358 // Extract some information from the instruction
359 Value *Dst = CI->getArgOperand(0);
360 Value *Src = CI->getArgOperand(1);
362 // See if we can get the length of the input string.
363 uint64_t Len = GetStringLength(Src);
364 if (Len == 0) return 0;
365 --Len; // Unbias length.
367 // Handle the simple, do-nothing case: strcat(x, "") -> x
371 // These optimizations require DataLayout.
374 return emitStrLenMemCpy(Src, Dst, Len, B);
377 Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
379 // We need to find the end of the destination string. That's where the
380 // memory is to be moved to. We just generate a call to strlen.
381 Value *DstLen = EmitStrLen(Dst, B, TD, TLI);
385 // Now that we have the destination's length, we must index into the
386 // destination's pointer to get the actual memcpy destination (end of
387 // the string .. we're concatenating).
388 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
390 // We have enough information to now generate the memcpy call to do the
391 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
392 B.CreateMemCpy(CpyDst, Src,
393 ConstantInt::get(TD->getIntPtrType(*Context), Len + 1), 1);
398 struct StrNCatOpt : public StrCatOpt {
399 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
400 // Verify the "strncat" function prototype.
401 FunctionType *FT = Callee->getFunctionType();
402 if (FT->getNumParams() != 3 ||
403 FT->getReturnType() != B.getInt8PtrTy() ||
404 FT->getParamType(0) != FT->getReturnType() ||
405 FT->getParamType(1) != FT->getReturnType() ||
406 !FT->getParamType(2)->isIntegerTy())
409 // Extract some information from the instruction
410 Value *Dst = CI->getArgOperand(0);
411 Value *Src = CI->getArgOperand(1);
414 // We don't do anything if length is not constant
415 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
416 Len = LengthArg->getZExtValue();
420 // See if we can get the length of the input string.
421 uint64_t SrcLen = GetStringLength(Src);
422 if (SrcLen == 0) return 0;
423 --SrcLen; // Unbias length.
425 // Handle the simple, do-nothing cases:
426 // strncat(x, "", c) -> x
427 // strncat(x, c, 0) -> x
428 if (SrcLen == 0 || Len == 0) return Dst;
430 // These optimizations require DataLayout.
433 // We don't optimize this case
434 if (Len < SrcLen) return 0;
436 // strncat(x, s, c) -> strcat(x, s)
437 // s is constant so the strcat can be optimized further
438 return emitStrLenMemCpy(Src, Dst, SrcLen, B);
442 struct StrChrOpt : public LibCallOptimization {
443 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
444 // Verify the "strchr" function prototype.
445 FunctionType *FT = Callee->getFunctionType();
446 if (FT->getNumParams() != 2 ||
447 FT->getReturnType() != B.getInt8PtrTy() ||
448 FT->getParamType(0) != FT->getReturnType() ||
449 !FT->getParamType(1)->isIntegerTy(32))
452 Value *SrcStr = CI->getArgOperand(0);
454 // If the second operand is non-constant, see if we can compute the length
455 // of the input string and turn this into memchr.
456 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
458 // These optimizations require DataLayout.
461 uint64_t Len = GetStringLength(SrcStr);
462 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
465 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
466 ConstantInt::get(TD->getIntPtrType(*Context), Len),
470 // Otherwise, the character is a constant, see if the first argument is
471 // a string literal. If so, we can constant fold.
473 if (!getConstantStringInfo(SrcStr, Str))
476 // Compute the offset, make sure to handle the case when we're searching for
477 // zero (a weird way to spell strlen).
478 size_t I = CharC->getSExtValue() == 0 ?
479 Str.size() : Str.find(CharC->getSExtValue());
480 if (I == StringRef::npos) // Didn't find the char. strchr returns null.
481 return Constant::getNullValue(CI->getType());
483 // strchr(s+n,c) -> gep(s+n+i,c)
484 return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");
488 struct StrRChrOpt : public LibCallOptimization {
489 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
490 // Verify the "strrchr" function prototype.
491 FunctionType *FT = Callee->getFunctionType();
492 if (FT->getNumParams() != 2 ||
493 FT->getReturnType() != B.getInt8PtrTy() ||
494 FT->getParamType(0) != FT->getReturnType() ||
495 !FT->getParamType(1)->isIntegerTy(32))
498 Value *SrcStr = CI->getArgOperand(0);
499 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
501 // Cannot fold anything if we're not looking for a constant.
506 if (!getConstantStringInfo(SrcStr, Str)) {
507 // strrchr(s, 0) -> strchr(s, 0)
508 if (TD && CharC->isZero())
509 return EmitStrChr(SrcStr, '\0', B, TD, TLI);
513 // Compute the offset.
514 size_t I = CharC->getSExtValue() == 0 ?
515 Str.size() : Str.rfind(CharC->getSExtValue());
516 if (I == StringRef::npos) // Didn't find the char. Return null.
517 return Constant::getNullValue(CI->getType());
519 // strrchr(s+n,c) -> gep(s+n+i,c)
520 return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");
524 struct StrCmpOpt : public LibCallOptimization {
525 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
526 // Verify the "strcmp" function prototype.
527 FunctionType *FT = Callee->getFunctionType();
528 if (FT->getNumParams() != 2 ||
529 !FT->getReturnType()->isIntegerTy(32) ||
530 FT->getParamType(0) != FT->getParamType(1) ||
531 FT->getParamType(0) != B.getInt8PtrTy())
534 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
535 if (Str1P == Str2P) // strcmp(x,x) -> 0
536 return ConstantInt::get(CI->getType(), 0);
538 StringRef Str1, Str2;
539 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
540 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
542 // strcmp(x, y) -> cnst (if both x and y are constant strings)
543 if (HasStr1 && HasStr2)
544 return ConstantInt::get(CI->getType(), Str1.compare(Str2));
546 if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
547 return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
550 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
551 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
553 // strcmp(P, "x") -> memcmp(P, "x", 2)
554 uint64_t Len1 = GetStringLength(Str1P);
555 uint64_t Len2 = GetStringLength(Str2P);
557 // These optimizations require DataLayout.
560 return EmitMemCmp(Str1P, Str2P,
561 ConstantInt::get(TD->getIntPtrType(*Context),
562 std::min(Len1, Len2)), B, TD, TLI);
569 struct StrNCmpOpt : public LibCallOptimization {
570 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
571 // Verify the "strncmp" function prototype.
572 FunctionType *FT = Callee->getFunctionType();
573 if (FT->getNumParams() != 3 ||
574 !FT->getReturnType()->isIntegerTy(32) ||
575 FT->getParamType(0) != FT->getParamType(1) ||
576 FT->getParamType(0) != B.getInt8PtrTy() ||
577 !FT->getParamType(2)->isIntegerTy())
580 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
581 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
582 return ConstantInt::get(CI->getType(), 0);
584 // Get the length argument if it is constant.
586 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
587 Length = LengthArg->getZExtValue();
591 if (Length == 0) // strncmp(x,y,0) -> 0
592 return ConstantInt::get(CI->getType(), 0);
594 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
595 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD, TLI);
597 StringRef Str1, Str2;
598 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
599 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
601 // strncmp(x, y) -> cnst (if both x and y are constant strings)
602 if (HasStr1 && HasStr2) {
603 StringRef SubStr1 = Str1.substr(0, Length);
604 StringRef SubStr2 = Str2.substr(0, Length);
605 return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
608 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
609 return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
612 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
613 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
619 struct StrCpyOpt : public LibCallOptimization {
620 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
621 // Verify the "strcpy" function prototype.
622 FunctionType *FT = Callee->getFunctionType();
623 if (FT->getNumParams() != 2 ||
624 FT->getReturnType() != FT->getParamType(0) ||
625 FT->getParamType(0) != FT->getParamType(1) ||
626 FT->getParamType(0) != B.getInt8PtrTy())
629 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
630 if (Dst == Src) // strcpy(x,x) -> x
633 // These optimizations require DataLayout.
636 // See if we can get the length of the input string.
637 uint64_t Len = GetStringLength(Src);
638 if (Len == 0) return 0;
640 // We have enough information to now generate the memcpy call to do the
641 // copy for us. Make a memcpy to copy the nul byte with align = 1.
642 B.CreateMemCpy(Dst, Src,
643 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
648 struct StpCpyOpt: public LibCallOptimization {
649 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
650 // Verify the "stpcpy" function prototype.
651 FunctionType *FT = Callee->getFunctionType();
652 if (FT->getNumParams() != 2 ||
653 FT->getReturnType() != FT->getParamType(0) ||
654 FT->getParamType(0) != FT->getParamType(1) ||
655 FT->getParamType(0) != B.getInt8PtrTy())
658 // These optimizations require DataLayout.
661 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
662 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
663 Value *StrLen = EmitStrLen(Src, B, TD, TLI);
664 return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
667 // See if we can get the length of the input string.
668 uint64_t Len = GetStringLength(Src);
669 if (Len == 0) return 0;
671 Type *PT = FT->getParamType(0);
672 Value *LenV = ConstantInt::get(TD->getIntPtrType(PT), Len);
673 Value *DstEnd = B.CreateGEP(Dst,
674 ConstantInt::get(TD->getIntPtrType(PT),
677 // We have enough information to now generate the memcpy call to do the
678 // copy for us. Make a memcpy to copy the nul byte with align = 1.
679 B.CreateMemCpy(Dst, Src, LenV, 1);
684 struct StrNCpyOpt : public LibCallOptimization {
685 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
686 FunctionType *FT = Callee->getFunctionType();
687 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
688 FT->getParamType(0) != FT->getParamType(1) ||
689 FT->getParamType(0) != B.getInt8PtrTy() ||
690 !FT->getParamType(2)->isIntegerTy())
693 Value *Dst = CI->getArgOperand(0);
694 Value *Src = CI->getArgOperand(1);
695 Value *LenOp = CI->getArgOperand(2);
697 // See if we can get the length of the input string.
698 uint64_t SrcLen = GetStringLength(Src);
699 if (SrcLen == 0) return 0;
703 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
704 B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
709 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
710 Len = LengthArg->getZExtValue();
714 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
716 // These optimizations require DataLayout.
719 // Let strncpy handle the zero padding
720 if (Len > SrcLen+1) return 0;
722 Type *PT = FT->getParamType(0);
723 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
724 B.CreateMemCpy(Dst, Src,
725 ConstantInt::get(TD->getIntPtrType(PT), Len), 1);
731 struct StrLenOpt : public LibCallOptimization {
732 virtual bool ignoreCallingConv() { return true; }
733 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
734 FunctionType *FT = Callee->getFunctionType();
735 if (FT->getNumParams() != 1 ||
736 FT->getParamType(0) != B.getInt8PtrTy() ||
737 !FT->getReturnType()->isIntegerTy())
740 Value *Src = CI->getArgOperand(0);
742 // Constant folding: strlen("xyz") -> 3
743 if (uint64_t Len = GetStringLength(Src))
744 return ConstantInt::get(CI->getType(), Len-1);
746 // strlen(x) != 0 --> *x != 0
747 // strlen(x) == 0 --> *x == 0
748 if (isOnlyUsedInZeroEqualityComparison(CI))
749 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
754 struct StrPBrkOpt : public LibCallOptimization {
755 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
756 FunctionType *FT = Callee->getFunctionType();
757 if (FT->getNumParams() != 2 ||
758 FT->getParamType(0) != B.getInt8PtrTy() ||
759 FT->getParamType(1) != FT->getParamType(0) ||
760 FT->getReturnType() != FT->getParamType(0))
764 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
765 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
767 // strpbrk(s, "") -> NULL
768 // strpbrk("", s) -> NULL
769 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
770 return Constant::getNullValue(CI->getType());
773 if (HasS1 && HasS2) {
774 size_t I = S1.find_first_of(S2);
775 if (I == std::string::npos) // No match.
776 return Constant::getNullValue(CI->getType());
778 return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");
781 // strpbrk(s, "a") -> strchr(s, 'a')
782 if (TD && HasS2 && S2.size() == 1)
783 return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD, TLI);
789 struct StrToOpt : public LibCallOptimization {
790 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
791 FunctionType *FT = Callee->getFunctionType();
792 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
793 !FT->getParamType(0)->isPointerTy() ||
794 !FT->getParamType(1)->isPointerTy())
797 Value *EndPtr = CI->getArgOperand(1);
798 if (isa<ConstantPointerNull>(EndPtr)) {
799 // With a null EndPtr, this function won't capture the main argument.
800 // It would be readonly too, except that it still may write to errno.
801 CI->addAttribute(1, Attribute::get(Callee->getContext(),
802 Attribute::NoCapture));
809 struct StrSpnOpt : public LibCallOptimization {
810 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
811 FunctionType *FT = Callee->getFunctionType();
812 if (FT->getNumParams() != 2 ||
813 FT->getParamType(0) != B.getInt8PtrTy() ||
814 FT->getParamType(1) != FT->getParamType(0) ||
815 !FT->getReturnType()->isIntegerTy())
819 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
820 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
822 // strspn(s, "") -> 0
823 // strspn("", s) -> 0
824 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
825 return Constant::getNullValue(CI->getType());
828 if (HasS1 && HasS2) {
829 size_t Pos = S1.find_first_not_of(S2);
830 if (Pos == StringRef::npos) Pos = S1.size();
831 return ConstantInt::get(CI->getType(), Pos);
838 struct StrCSpnOpt : public LibCallOptimization {
839 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
840 FunctionType *FT = Callee->getFunctionType();
841 if (FT->getNumParams() != 2 ||
842 FT->getParamType(0) != B.getInt8PtrTy() ||
843 FT->getParamType(1) != FT->getParamType(0) ||
844 !FT->getReturnType()->isIntegerTy())
848 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
849 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
851 // strcspn("", s) -> 0
852 if (HasS1 && S1.empty())
853 return Constant::getNullValue(CI->getType());
856 if (HasS1 && HasS2) {
857 size_t Pos = S1.find_first_of(S2);
858 if (Pos == StringRef::npos) Pos = S1.size();
859 return ConstantInt::get(CI->getType(), Pos);
862 // strcspn(s, "") -> strlen(s)
863 if (TD && HasS2 && S2.empty())
864 return EmitStrLen(CI->getArgOperand(0), B, TD, TLI);
870 struct StrStrOpt : public LibCallOptimization {
871 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
872 FunctionType *FT = Callee->getFunctionType();
873 if (FT->getNumParams() != 2 ||
874 !FT->getParamType(0)->isPointerTy() ||
875 !FT->getParamType(1)->isPointerTy() ||
876 !FT->getReturnType()->isPointerTy())
879 // fold strstr(x, x) -> x.
880 if (CI->getArgOperand(0) == CI->getArgOperand(1))
881 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
883 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
884 if (TD && isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
885 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD, TLI);
888 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
892 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
894 ICmpInst *Old = cast<ICmpInst>(*UI++);
895 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
896 ConstantInt::getNullValue(StrNCmp->getType()),
898 LCS->replaceAllUsesWith(Old, Cmp);
903 // See if either input string is a constant string.
904 StringRef SearchStr, ToFindStr;
905 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
906 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
908 // fold strstr(x, "") -> x.
909 if (HasStr2 && ToFindStr.empty())
910 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
912 // If both strings are known, constant fold it.
913 if (HasStr1 && HasStr2) {
914 std::string::size_type Offset = SearchStr.find(ToFindStr);
916 if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
917 return Constant::getNullValue(CI->getType());
919 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
920 Value *Result = CastToCStr(CI->getArgOperand(0), B);
921 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
922 return B.CreateBitCast(Result, CI->getType());
925 // fold strstr(x, "y") -> strchr(x, 'y').
926 if (HasStr2 && ToFindStr.size() == 1) {
927 Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TD, TLI);
928 return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : 0;
934 struct MemCmpOpt : public LibCallOptimization {
935 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
936 FunctionType *FT = Callee->getFunctionType();
937 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
938 !FT->getParamType(1)->isPointerTy() ||
939 !FT->getReturnType()->isIntegerTy(32))
942 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
944 if (LHS == RHS) // memcmp(s,s,x) -> 0
945 return Constant::getNullValue(CI->getType());
947 // Make sure we have a constant length.
948 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
950 uint64_t Len = LenC->getZExtValue();
952 if (Len == 0) // memcmp(s1,s2,0) -> 0
953 return Constant::getNullValue(CI->getType());
955 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
957 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
958 CI->getType(), "lhsv");
959 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
960 CI->getType(), "rhsv");
961 return B.CreateSub(LHSV, RHSV, "chardiff");
964 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
965 StringRef LHSStr, RHSStr;
966 if (getConstantStringInfo(LHS, LHSStr) &&
967 getConstantStringInfo(RHS, RHSStr)) {
968 // Make sure we're not reading out-of-bounds memory.
969 if (Len > LHSStr.size() || Len > RHSStr.size())
971 // Fold the memcmp and normalize the result. This way we get consistent
972 // results across multiple platforms.
974 int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);
979 return ConstantInt::get(CI->getType(), Ret);
986 struct MemCpyOpt : public LibCallOptimization {
987 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
988 // These optimizations require DataLayout.
991 FunctionType *FT = Callee->getFunctionType();
992 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
993 !FT->getParamType(0)->isPointerTy() ||
994 !FT->getParamType(1)->isPointerTy() ||
995 FT->getParamType(2) != TD->getIntPtrType(*Context))
998 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
999 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1000 CI->getArgOperand(2), 1);
1001 return CI->getArgOperand(0);
1005 struct MemMoveOpt : public LibCallOptimization {
1006 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1007 // These optimizations require DataLayout.
1010 FunctionType *FT = Callee->getFunctionType();
1011 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1012 !FT->getParamType(0)->isPointerTy() ||
1013 !FT->getParamType(1)->isPointerTy() ||
1014 FT->getParamType(2) != TD->getIntPtrType(*Context))
1017 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
1018 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
1019 CI->getArgOperand(2), 1);
1020 return CI->getArgOperand(0);
1024 struct MemSetOpt : public LibCallOptimization {
1025 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1026 // These optimizations require DataLayout.
1029 FunctionType *FT = Callee->getFunctionType();
1030 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1031 !FT->getParamType(0)->isPointerTy() ||
1032 !FT->getParamType(1)->isIntegerTy() ||
1033 FT->getParamType(2) != TD->getIntPtrType(*Context))
1036 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
1037 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
1038 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
1039 return CI->getArgOperand(0);
1043 //===----------------------------------------------------------------------===//
1044 // Math Library Optimizations
1045 //===----------------------------------------------------------------------===//
1047 //===----------------------------------------------------------------------===//
1048 // Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1050 struct UnaryDoubleFPOpt : public LibCallOptimization {
1052 UnaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {}
1053 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1054 FunctionType *FT = Callee->getFunctionType();
1055 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1056 !FT->getParamType(0)->isDoubleTy())
1060 // Check if all the uses for function like 'sin' are converted to float.
1061 for (Value::use_iterator UseI = CI->use_begin(); UseI != CI->use_end();
1063 FPTruncInst *Cast = dyn_cast<FPTruncInst>(*UseI);
1064 if (Cast == 0 || !Cast->getType()->isFloatTy())
1069 // If this is something like 'floor((double)floatval)', convert to floorf.
1070 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
1071 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
1074 // floor((double)floatval) -> (double)floorf(floatval)
1075 Value *V = Cast->getOperand(0);
1076 V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
1077 return B.CreateFPExt(V, B.getDoubleTy());
1081 struct UnsafeFPLibCallOptimization : public LibCallOptimization {
1082 bool UnsafeFPShrink;
1083 UnsafeFPLibCallOptimization(bool UnsafeFPShrink) {
1084 this->UnsafeFPShrink = UnsafeFPShrink;
1088 struct CosOpt : public UnsafeFPLibCallOptimization {
1089 CosOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
1090 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1092 if (UnsafeFPShrink && Callee->getName() == "cos" &&
1093 TLI->has(LibFunc::cosf)) {
1094 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
1095 Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
1098 FunctionType *FT = Callee->getFunctionType();
1099 // Just make sure this has 1 argument of FP type, which matches the
1101 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1102 !FT->getParamType(0)->isFloatingPointTy())
1105 // cos(-x) -> cos(x)
1106 Value *Op1 = CI->getArgOperand(0);
1107 if (BinaryOperator::isFNeg(Op1)) {
1108 BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);
1109 return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");
1115 struct PowOpt : public UnsafeFPLibCallOptimization {
1116 PowOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
1117 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1119 if (UnsafeFPShrink && Callee->getName() == "pow" &&
1120 TLI->has(LibFunc::powf)) {
1121 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
1122 Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
1125 FunctionType *FT = Callee->getFunctionType();
1126 // Just make sure this has 2 arguments of the same FP type, which match the
1128 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1129 FT->getParamType(0) != FT->getParamType(1) ||
1130 !FT->getParamType(0)->isFloatingPointTy())
1133 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
1134 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1135 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
1137 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
1138 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
1141 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1142 if (Op2C == 0) return Ret;
1144 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
1145 return ConstantFP::get(CI->getType(), 1.0);
1147 if (Op2C->isExactlyValue(0.5)) {
1148 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1149 // This is faster than calling pow, and still handles negative zero
1150 // and negative infinity correctly.
1151 // TODO: In fast-math mode, this could be just sqrt(x).
1152 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
1153 Value *Inf = ConstantFP::getInfinity(CI->getType());
1154 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
1155 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1156 Callee->getAttributes());
1157 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1158 Callee->getAttributes());
1159 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
1160 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
1164 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1166 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
1167 return B.CreateFMul(Op1, Op1, "pow2");
1168 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
1169 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
1175 struct Exp2Opt : public UnsafeFPLibCallOptimization {
1176 Exp2Opt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
1177 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1179 if (UnsafeFPShrink && Callee->getName() == "exp2" &&
1180 TLI->has(LibFunc::exp2)) {
1181 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
1182 Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
1185 FunctionType *FT = Callee->getFunctionType();
1186 // Just make sure this has 1 argument of FP type, which matches the
1188 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1189 !FT->getParamType(0)->isFloatingPointTy())
1192 Value *Op = CI->getArgOperand(0);
1193 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1194 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1195 Value *LdExpArg = 0;
1196 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1197 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
1198 LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
1199 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1200 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
1201 LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
1206 if (Op->getType()->isFloatTy())
1208 else if (Op->getType()->isDoubleTy())
1213 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
1214 if (!Op->getType()->isFloatTy())
1215 One = ConstantExpr::getFPExtend(One, Op->getType());
1217 Module *M = Caller->getParent();
1218 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
1220 B.getInt32Ty(), NULL);
1221 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1222 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1223 CI->setCallingConv(F->getCallingConv());
1231 //===----------------------------------------------------------------------===//
1232 // Integer Library Call Optimizations
1233 //===----------------------------------------------------------------------===//
1235 struct FFSOpt : public LibCallOptimization {
1236 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1237 FunctionType *FT = Callee->getFunctionType();
1238 // Just make sure this has 2 arguments of the same FP type, which match the
1240 if (FT->getNumParams() != 1 ||
1241 !FT->getReturnType()->isIntegerTy(32) ||
1242 !FT->getParamType(0)->isIntegerTy())
1245 Value *Op = CI->getArgOperand(0);
1248 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1249 if (CI->isZero()) // ffs(0) -> 0.
1250 return B.getInt32(0);
1251 // ffs(c) -> cttz(c)+1
1252 return B.getInt32(CI->getValue().countTrailingZeros() + 1);
1255 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1256 Type *ArgType = Op->getType();
1257 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1258 Intrinsic::cttz, ArgType);
1259 Value *V = B.CreateCall2(F, Op, B.getFalse(), "cttz");
1260 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
1261 V = B.CreateIntCast(V, B.getInt32Ty(), false);
1263 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
1264 return B.CreateSelect(Cond, V, B.getInt32(0));
1268 struct AbsOpt : public LibCallOptimization {
1269 virtual bool ignoreCallingConv() { return true; }
1270 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1271 FunctionType *FT = Callee->getFunctionType();
1272 // We require integer(integer) where the types agree.
1273 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1274 FT->getParamType(0) != FT->getReturnType())
1277 // abs(x) -> x >s -1 ? x : -x
1278 Value *Op = CI->getArgOperand(0);
1279 Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),
1281 Value *Neg = B.CreateNeg(Op, "neg");
1282 return B.CreateSelect(Pos, Op, Neg);
1286 struct IsDigitOpt : public LibCallOptimization {
1287 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1288 FunctionType *FT = Callee->getFunctionType();
1289 // We require integer(i32)
1290 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1291 !FT->getParamType(0)->isIntegerTy(32))
1294 // isdigit(c) -> (c-'0') <u 10
1295 Value *Op = CI->getArgOperand(0);
1296 Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
1297 Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
1298 return B.CreateZExt(Op, CI->getType());
1302 struct IsAsciiOpt : public LibCallOptimization {
1303 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1304 FunctionType *FT = Callee->getFunctionType();
1305 // We require integer(i32)
1306 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1307 !FT->getParamType(0)->isIntegerTy(32))
1310 // isascii(c) -> c <u 128
1311 Value *Op = CI->getArgOperand(0);
1312 Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
1313 return B.CreateZExt(Op, CI->getType());
1317 struct ToAsciiOpt : public LibCallOptimization {
1318 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1319 FunctionType *FT = Callee->getFunctionType();
1320 // We require i32(i32)
1321 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1322 !FT->getParamType(0)->isIntegerTy(32))
1325 // toascii(c) -> c & 0x7f
1326 return B.CreateAnd(CI->getArgOperand(0),
1327 ConstantInt::get(CI->getType(),0x7F));
1331 //===----------------------------------------------------------------------===//
1332 // Formatting and IO Library Call Optimizations
1333 //===----------------------------------------------------------------------===//
1335 struct PrintFOpt : public LibCallOptimization {
1336 Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,
1338 // Check for a fixed format string.
1339 StringRef FormatStr;
1340 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
1343 // Empty format string -> noop.
1344 if (FormatStr.empty()) // Tolerate printf's declared void.
1345 return CI->use_empty() ? (Value*)CI :
1346 ConstantInt::get(CI->getType(), 0);
1348 // Do not do any of the following transformations if the printf return value
1349 // is used, in general the printf return value is not compatible with either
1350 // putchar() or puts().
1351 if (!CI->use_empty())
1354 // printf("x") -> putchar('x'), even for '%'.
1355 if (FormatStr.size() == 1) {
1356 Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI);
1357 if (CI->use_empty() || !Res) return Res;
1358 return B.CreateIntCast(Res, CI->getType(), true);
1361 // printf("foo\n") --> puts("foo")
1362 if (FormatStr[FormatStr.size()-1] == '\n' &&
1363 FormatStr.find('%') == std::string::npos) { // no format characters.
1364 // Create a string literal with no \n on it. We expect the constant merge
1365 // pass to be run after this pass, to merge duplicate strings.
1366 FormatStr = FormatStr.drop_back();
1367 Value *GV = B.CreateGlobalString(FormatStr, "str");
1368 Value *NewCI = EmitPutS(GV, B, TD, TLI);
1369 return (CI->use_empty() || !NewCI) ?
1371 ConstantInt::get(CI->getType(), FormatStr.size()+1);
1374 // Optimize specific format strings.
1375 // printf("%c", chr) --> putchar(chr)
1376 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
1377 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1378 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI);
1380 if (CI->use_empty() || !Res) return Res;
1381 return B.CreateIntCast(Res, CI->getType(), true);
1384 // printf("%s\n", str) --> puts(str)
1385 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
1386 CI->getArgOperand(1)->getType()->isPointerTy()) {
1387 return EmitPutS(CI->getArgOperand(1), B, TD, TLI);
1392 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1393 // Require one fixed pointer argument and an integer/void result.
1394 FunctionType *FT = Callee->getFunctionType();
1395 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1396 !(FT->getReturnType()->isIntegerTy() ||
1397 FT->getReturnType()->isVoidTy()))
1400 if (Value *V = optimizeFixedFormatString(Callee, CI, B)) {
1404 // printf(format, ...) -> iprintf(format, ...) if no floating point
1406 if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) {
1407 Module *M = B.GetInsertBlock()->getParent()->getParent();
1408 Constant *IPrintFFn =
1409 M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
1410 CallInst *New = cast<CallInst>(CI->clone());
1411 New->setCalledFunction(IPrintFFn);
1419 struct SPrintFOpt : public LibCallOptimization {
1420 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
1422 // Check for a fixed format string.
1423 StringRef FormatStr;
1424 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1427 // If we just have a format string (nothing else crazy) transform it.
1428 if (CI->getNumArgOperands() == 2) {
1429 // Make sure there's no % in the constant array. We could try to handle
1430 // %% -> % in the future if we cared.
1431 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1432 if (FormatStr[i] == '%')
1433 return 0; // we found a format specifier, bail out.
1435 // These optimizations require DataLayout.
1438 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1439 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1440 ConstantInt::get(TD->getIntPtrType(*Context), // Copy the
1441 FormatStr.size() + 1), 1); // nul byte.
1442 return ConstantInt::get(CI->getType(), FormatStr.size());
1445 // The remaining optimizations require the format string to be "%s" or "%c"
1446 // and have an extra operand.
1447 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1448 CI->getNumArgOperands() < 3)
1451 // Decode the second character of the format string.
1452 if (FormatStr[1] == 'c') {
1453 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1454 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1455 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
1456 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
1457 B.CreateStore(V, Ptr);
1458 Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
1459 B.CreateStore(B.getInt8(0), Ptr);
1461 return ConstantInt::get(CI->getType(), 1);
1464 if (FormatStr[1] == 's') {
1465 // These optimizations require DataLayout.
1468 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1469 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
1471 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD, TLI);
1474 Value *IncLen = B.CreateAdd(Len,
1475 ConstantInt::get(Len->getType(), 1),
1477 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
1479 // The sprintf result is the unincremented number of bytes in the string.
1480 return B.CreateIntCast(Len, CI->getType(), false);
1485 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1486 // Require two fixed pointer arguments and an integer result.
1487 FunctionType *FT = Callee->getFunctionType();
1488 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1489 !FT->getParamType(1)->isPointerTy() ||
1490 !FT->getReturnType()->isIntegerTy())
1493 if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
1497 // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
1499 if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) {
1500 Module *M = B.GetInsertBlock()->getParent()->getParent();
1501 Constant *SIPrintFFn =
1502 M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
1503 CallInst *New = cast<CallInst>(CI->clone());
1504 New->setCalledFunction(SIPrintFFn);
1512 struct FPrintFOpt : public LibCallOptimization {
1513 Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,
1515 // All the optimizations depend on the format string.
1516 StringRef FormatStr;
1517 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1520 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1521 if (CI->getNumArgOperands() == 2) {
1522 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1523 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
1524 return 0; // We found a format specifier.
1526 // These optimizations require DataLayout.
1529 Value *NewCI = EmitFWrite(CI->getArgOperand(1),
1530 ConstantInt::get(TD->getIntPtrType(*Context),
1532 CI->getArgOperand(0), B, TD, TLI);
1533 return NewCI ? ConstantInt::get(CI->getType(), FormatStr.size()) : 0;
1536 // The remaining optimizations require the format string to be "%s" or "%c"
1537 // and have an extra operand.
1538 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1539 CI->getNumArgOperands() < 3)
1542 // Decode the second character of the format string.
1543 if (FormatStr[1] == 'c') {
1544 // fprintf(F, "%c", chr) --> fputc(chr, F)
1545 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1546 Value *NewCI = EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B,
1548 return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
1551 if (FormatStr[1] == 's') {
1552 // fprintf(F, "%s", str) --> fputs(str, F)
1553 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
1555 return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD, TLI);
1560 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1561 // Require two fixed paramters as pointers and integer result.
1562 FunctionType *FT = Callee->getFunctionType();
1563 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1564 !FT->getParamType(1)->isPointerTy() ||
1565 !FT->getReturnType()->isIntegerTy())
1568 if (Value *V = optimizeFixedFormatString(Callee, CI, B)) {
1572 // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
1573 // floating point arguments.
1574 if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) {
1575 Module *M = B.GetInsertBlock()->getParent()->getParent();
1576 Constant *FIPrintFFn =
1577 M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
1578 CallInst *New = cast<CallInst>(CI->clone());
1579 New->setCalledFunction(FIPrintFFn);
1587 struct FWriteOpt : public LibCallOptimization {
1588 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1589 // Require a pointer, an integer, an integer, a pointer, returning integer.
1590 FunctionType *FT = Callee->getFunctionType();
1591 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1592 !FT->getParamType(1)->isIntegerTy() ||
1593 !FT->getParamType(2)->isIntegerTy() ||
1594 !FT->getParamType(3)->isPointerTy() ||
1595 !FT->getReturnType()->isIntegerTy())
1598 // Get the element size and count.
1599 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1600 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1601 if (!SizeC || !CountC) return 0;
1602 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
1604 // If this is writing zero records, remove the call (it's a noop).
1606 return ConstantInt::get(CI->getType(), 0);
1608 // If this is writing one byte, turn it into fputc.
1609 // This optimisation is only valid, if the return value is unused.
1610 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1611 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1612 Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, TD, TLI);
1613 return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
1620 struct FPutsOpt : public LibCallOptimization {
1621 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1622 // These optimizations require DataLayout.
1625 // Require two pointers. Also, we can't optimize if return value is used.
1626 FunctionType *FT = Callee->getFunctionType();
1627 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1628 !FT->getParamType(1)->isPointerTy() ||
1632 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1633 uint64_t Len = GetStringLength(CI->getArgOperand(0));
1635 // Known to have no uses (see above).
1636 return EmitFWrite(CI->getArgOperand(0),
1637 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
1638 CI->getArgOperand(1), B, TD, TLI);
1642 struct PutsOpt : public LibCallOptimization {
1643 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1644 // Require one fixed pointer argument and an integer/void result.
1645 FunctionType *FT = Callee->getFunctionType();
1646 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1647 !(FT->getReturnType()->isIntegerTy() ||
1648 FT->getReturnType()->isVoidTy()))
1651 // Check for a constant string.
1653 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
1656 if (Str.empty() && CI->use_empty()) {
1657 // puts("") -> putchar('\n')
1658 Value *Res = EmitPutChar(B.getInt32('\n'), B, TD, TLI);
1659 if (CI->use_empty() || !Res) return Res;
1660 return B.CreateIntCast(Res, CI->getType(), true);
1667 } // End anonymous namespace.
1671 class LibCallSimplifierImpl {
1672 const DataLayout *TD;
1673 const TargetLibraryInfo *TLI;
1674 const LibCallSimplifier *LCS;
1675 bool UnsafeFPShrink;
1676 StringMap<LibCallOptimization*, BumpPtrAllocator> Optimizations;
1678 // Fortified library call optimizations.
1679 MemCpyChkOpt MemCpyChk;
1680 MemMoveChkOpt MemMoveChk;
1681 MemSetChkOpt MemSetChk;
1682 StrCpyChkOpt StrCpyChk;
1683 StpCpyChkOpt StpCpyChk;
1684 StrNCpyChkOpt StrNCpyChk;
1686 // String library call optimizations.
1703 // Memory library call optimizations.
1709 // Math library call optimizations.
1710 UnaryDoubleFPOpt UnaryDoubleFP, UnsafeUnaryDoubleFP;
1711 CosOpt Cos; PowOpt Pow; Exp2Opt Exp2;
1713 // Integer library call optimizations.
1720 // Formatting and IO library call optimizations.
1728 void initOptimizations();
1729 void addOpt(LibFunc::Func F, LibCallOptimization* Opt);
1730 void addOpt(LibFunc::Func F1, LibFunc::Func F2, LibCallOptimization* Opt);
1732 LibCallSimplifierImpl(const DataLayout *TD, const TargetLibraryInfo *TLI,
1733 const LibCallSimplifier *LCS,
1734 bool UnsafeFPShrink = false)
1735 : UnaryDoubleFP(false), UnsafeUnaryDoubleFP(true),
1736 Cos(UnsafeFPShrink), Pow(UnsafeFPShrink), Exp2(UnsafeFPShrink) {
1740 this->UnsafeFPShrink = UnsafeFPShrink;
1743 Value *optimizeCall(CallInst *CI);
1746 void LibCallSimplifierImpl::initOptimizations() {
1747 // Fortified library call optimizations.
1748 Optimizations["__memcpy_chk"] = &MemCpyChk;
1749 Optimizations["__memmove_chk"] = &MemMoveChk;
1750 Optimizations["__memset_chk"] = &MemSetChk;
1751 Optimizations["__strcpy_chk"] = &StrCpyChk;
1752 Optimizations["__stpcpy_chk"] = &StpCpyChk;
1753 Optimizations["__strncpy_chk"] = &StrNCpyChk;
1754 Optimizations["__stpncpy_chk"] = &StrNCpyChk;
1756 // String library call optimizations.
1757 addOpt(LibFunc::strcat, &StrCat);
1758 addOpt(LibFunc::strncat, &StrNCat);
1759 addOpt(LibFunc::strchr, &StrChr);
1760 addOpt(LibFunc::strrchr, &StrRChr);
1761 addOpt(LibFunc::strcmp, &StrCmp);
1762 addOpt(LibFunc::strncmp, &StrNCmp);
1763 addOpt(LibFunc::strcpy, &StrCpy);
1764 addOpt(LibFunc::stpcpy, &StpCpy);
1765 addOpt(LibFunc::strncpy, &StrNCpy);
1766 addOpt(LibFunc::strlen, &StrLen);
1767 addOpt(LibFunc::strpbrk, &StrPBrk);
1768 addOpt(LibFunc::strtol, &StrTo);
1769 addOpt(LibFunc::strtod, &StrTo);
1770 addOpt(LibFunc::strtof, &StrTo);
1771 addOpt(LibFunc::strtoul, &StrTo);
1772 addOpt(LibFunc::strtoll, &StrTo);
1773 addOpt(LibFunc::strtold, &StrTo);
1774 addOpt(LibFunc::strtoull, &StrTo);
1775 addOpt(LibFunc::strspn, &StrSpn);
1776 addOpt(LibFunc::strcspn, &StrCSpn);
1777 addOpt(LibFunc::strstr, &StrStr);
1779 // Memory library call optimizations.
1780 addOpt(LibFunc::memcmp, &MemCmp);
1781 addOpt(LibFunc::memcpy, &MemCpy);
1782 addOpt(LibFunc::memmove, &MemMove);
1783 addOpt(LibFunc::memset, &MemSet);
1785 // Math library call optimizations.
1786 addOpt(LibFunc::ceil, LibFunc::ceilf, &UnaryDoubleFP);
1787 addOpt(LibFunc::fabs, LibFunc::fabsf, &UnaryDoubleFP);
1788 addOpt(LibFunc::floor, LibFunc::floorf, &UnaryDoubleFP);
1789 addOpt(LibFunc::rint, LibFunc::rintf, &UnaryDoubleFP);
1790 addOpt(LibFunc::round, LibFunc::roundf, &UnaryDoubleFP);
1791 addOpt(LibFunc::nearbyint, LibFunc::nearbyintf, &UnaryDoubleFP);
1792 addOpt(LibFunc::trunc, LibFunc::truncf, &UnaryDoubleFP);
1794 if(UnsafeFPShrink) {
1795 addOpt(LibFunc::acos, LibFunc::acosf, &UnsafeUnaryDoubleFP);
1796 addOpt(LibFunc::acosh, LibFunc::acoshf, &UnsafeUnaryDoubleFP);
1797 addOpt(LibFunc::asin, LibFunc::asinf, &UnsafeUnaryDoubleFP);
1798 addOpt(LibFunc::asinh, LibFunc::asinhf, &UnsafeUnaryDoubleFP);
1799 addOpt(LibFunc::atan, LibFunc::atanf, &UnsafeUnaryDoubleFP);
1800 addOpt(LibFunc::atanh, LibFunc::atanhf, &UnsafeUnaryDoubleFP);
1801 addOpt(LibFunc::cbrt, LibFunc::cbrtf, &UnsafeUnaryDoubleFP);
1802 addOpt(LibFunc::cosh, LibFunc::coshf, &UnsafeUnaryDoubleFP);
1803 addOpt(LibFunc::exp, LibFunc::expf, &UnsafeUnaryDoubleFP);
1804 addOpt(LibFunc::exp10, LibFunc::exp10f, &UnsafeUnaryDoubleFP);
1805 addOpt(LibFunc::expm1, LibFunc::expm1f, &UnsafeUnaryDoubleFP);
1806 addOpt(LibFunc::log, LibFunc::logf, &UnsafeUnaryDoubleFP);
1807 addOpt(LibFunc::log10, LibFunc::log10f, &UnsafeUnaryDoubleFP);
1808 addOpt(LibFunc::log1p, LibFunc::log1pf, &UnsafeUnaryDoubleFP);
1809 addOpt(LibFunc::log2, LibFunc::log2f, &UnsafeUnaryDoubleFP);
1810 addOpt(LibFunc::logb, LibFunc::logbf, &UnsafeUnaryDoubleFP);
1811 addOpt(LibFunc::sin, LibFunc::sinf, &UnsafeUnaryDoubleFP);
1812 addOpt(LibFunc::sinh, LibFunc::sinhf, &UnsafeUnaryDoubleFP);
1813 addOpt(LibFunc::sqrt, LibFunc::sqrtf, &UnsafeUnaryDoubleFP);
1814 addOpt(LibFunc::tan, LibFunc::tanf, &UnsafeUnaryDoubleFP);
1815 addOpt(LibFunc::tanh, LibFunc::tanhf, &UnsafeUnaryDoubleFP);
1818 addOpt(LibFunc::cosf, &Cos);
1819 addOpt(LibFunc::cos, &Cos);
1820 addOpt(LibFunc::cosl, &Cos);
1821 addOpt(LibFunc::powf, &Pow);
1822 addOpt(LibFunc::pow, &Pow);
1823 addOpt(LibFunc::powl, &Pow);
1824 Optimizations["llvm.pow.f32"] = &Pow;
1825 Optimizations["llvm.pow.f64"] = &Pow;
1826 Optimizations["llvm.pow.f80"] = &Pow;
1827 Optimizations["llvm.pow.f128"] = &Pow;
1828 Optimizations["llvm.pow.ppcf128"] = &Pow;
1829 addOpt(LibFunc::exp2l, &Exp2);
1830 addOpt(LibFunc::exp2, &Exp2);
1831 addOpt(LibFunc::exp2f, &Exp2);
1832 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1833 Optimizations["llvm.exp2.f128"] = &Exp2;
1834 Optimizations["llvm.exp2.f80"] = &Exp2;
1835 Optimizations["llvm.exp2.f64"] = &Exp2;
1836 Optimizations["llvm.exp2.f32"] = &Exp2;
1838 // Integer library call optimizations.
1839 addOpt(LibFunc::ffs, &FFS);
1840 addOpt(LibFunc::ffsl, &FFS);
1841 addOpt(LibFunc::ffsll, &FFS);
1842 addOpt(LibFunc::abs, &Abs);
1843 addOpt(LibFunc::labs, &Abs);
1844 addOpt(LibFunc::llabs, &Abs);
1845 addOpt(LibFunc::isdigit, &IsDigit);
1846 addOpt(LibFunc::isascii, &IsAscii);
1847 addOpt(LibFunc::toascii, &ToAscii);
1849 // Formatting and IO library call optimizations.
1850 addOpt(LibFunc::printf, &PrintF);
1851 addOpt(LibFunc::sprintf, &SPrintF);
1852 addOpt(LibFunc::fprintf, &FPrintF);
1853 addOpt(LibFunc::fwrite, &FWrite);
1854 addOpt(LibFunc::fputs, &FPuts);
1855 addOpt(LibFunc::puts, &Puts);
1858 Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) {
1859 if (Optimizations.empty())
1860 initOptimizations();
1862 Function *Callee = CI->getCalledFunction();
1863 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1865 IRBuilder<> Builder(CI);
1866 return LCO->optimizeCall(CI, TD, TLI, LCS, Builder);
1871 void LibCallSimplifierImpl::addOpt(LibFunc::Func F, LibCallOptimization* Opt) {
1873 Optimizations[TLI->getName(F)] = Opt;
1876 void LibCallSimplifierImpl::addOpt(LibFunc::Func F1, LibFunc::Func F2,
1877 LibCallOptimization* Opt) {
1878 if (TLI->has(F1) && TLI->has(F2))
1879 Optimizations[TLI->getName(F1)] = Opt;
1882 LibCallSimplifier::LibCallSimplifier(const DataLayout *TD,
1883 const TargetLibraryInfo *TLI,
1884 bool UnsafeFPShrink) {
1885 Impl = new LibCallSimplifierImpl(TD, TLI, this, UnsafeFPShrink);
1888 LibCallSimplifier::~LibCallSimplifier() {
1892 Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
1893 if (CI->hasFnAttr(Attribute::NoBuiltin)) return 0;
1894 return Impl->optimizeCall(CI);
1897 void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) const {
1898 I->replaceAllUsesWith(With);
1899 I->eraseFromParent();