1 //===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
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 file implements a simple pass that applies a variety of small
11 // optimizations for calls to specific well-known function calls (e.g. runtime
12 // library functions). Any optimization that takes the very simple form
13 // "replace call to library function with simpler code that provides the same
14 // result" belongs in this file.
16 //===----------------------------------------------------------------------===//
18 #define DEBUG_TYPE "simplify-libcalls"
19 #include "llvm/Transforms/Scalar.h"
20 #include "llvm/Transforms/Utils/BuildLibCalls.h"
21 #include "llvm/Intrinsics.h"
22 #include "llvm/LLVMContext.h"
23 #include "llvm/Module.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/IRBuilder.h"
26 #include "llvm/Analysis/ValueTracking.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/ADT/SmallPtrSet.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Config/config.h"
37 STATISTIC(NumSimplified, "Number of library calls simplified");
38 STATISTIC(NumAnnotated, "Number of attributes added to library functions");
40 //===----------------------------------------------------------------------===//
41 // Optimizer Base Class
42 //===----------------------------------------------------------------------===//
44 /// This class is the abstract base class for the set of optimizations that
45 /// corresponds to one library call.
47 class LibCallOptimization {
53 LibCallOptimization() { }
54 virtual ~LibCallOptimization() {}
56 /// CallOptimizer - This pure virtual method is implemented by base classes to
57 /// do various optimizations. If this returns null then no transformation was
58 /// performed. If it returns CI, then it transformed the call and CI is to be
59 /// deleted. If it returns something else, replace CI with the new value and
61 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
64 Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
65 Caller = CI->getParent()->getParent();
67 if (CI->getCalledFunction())
68 Context = &CI->getCalledFunction()->getContext();
69 return CallOptimizer(CI->getCalledFunction(), CI, B);
72 } // End anonymous namespace.
75 //===----------------------------------------------------------------------===//
77 //===----------------------------------------------------------------------===//
79 /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
80 /// value is equal or not-equal to zero.
81 static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
82 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
84 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
86 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
89 // Unknown instruction.
95 //===----------------------------------------------------------------------===//
96 // String and Memory LibCall Optimizations
97 //===----------------------------------------------------------------------===//
99 //===---------------------------------------===//
100 // 'strcat' Optimizations
102 struct StrCatOpt : public LibCallOptimization {
103 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
104 // Verify the "strcat" function prototype.
105 const FunctionType *FT = Callee->getFunctionType();
106 if (FT->getNumParams() != 2 ||
107 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
108 FT->getParamType(0) != FT->getReturnType() ||
109 FT->getParamType(1) != FT->getReturnType())
112 // Extract some information from the instruction
113 Value *Dst = CI->getOperand(1);
114 Value *Src = CI->getOperand(2);
116 // See if we can get the length of the input string.
117 uint64_t Len = GetStringLength(Src);
118 if (Len == 0) return 0;
119 --Len; // Unbias length.
121 // Handle the simple, do-nothing case: strcat(x, "") -> x
125 // These optimizations require TargetData.
128 EmitStrLenMemCpy(Src, Dst, Len, B);
132 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
133 // We need to find the end of the destination string. That's where the
134 // memory is to be moved to. We just generate a call to strlen.
135 Value *DstLen = EmitStrLen(Dst, B, TD);
137 // Now that we have the destination's length, we must index into the
138 // destination's pointer to get the actual memcpy destination (end of
139 // the string .. we're concatenating).
140 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
142 // We have enough information to now generate the memcpy call to do the
143 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
144 EmitMemCpy(CpyDst, Src,
145 ConstantInt::get(TD->getIntPtrType(*Context), Len+1),
150 //===---------------------------------------===//
151 // 'strncat' Optimizations
153 struct StrNCatOpt : public StrCatOpt {
154 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
155 // Verify the "strncat" function prototype.
156 const FunctionType *FT = Callee->getFunctionType();
157 if (FT->getNumParams() != 3 ||
158 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
159 FT->getParamType(0) != FT->getReturnType() ||
160 FT->getParamType(1) != FT->getReturnType() ||
161 !FT->getParamType(2)->isIntegerTy())
164 // Extract some information from the instruction
165 Value *Dst = CI->getOperand(1);
166 Value *Src = CI->getOperand(2);
169 // We don't do anything if length is not constant
170 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
171 Len = LengthArg->getZExtValue();
175 // See if we can get the length of the input string.
176 uint64_t SrcLen = GetStringLength(Src);
177 if (SrcLen == 0) return 0;
178 --SrcLen; // Unbias length.
180 // Handle the simple, do-nothing cases:
181 // strncat(x, "", c) -> x
182 // strncat(x, c, 0) -> x
183 if (SrcLen == 0 || Len == 0) return Dst;
185 // These optimizations require TargetData.
188 // We don't optimize this case
189 if (Len < SrcLen) return 0;
191 // strncat(x, s, c) -> strcat(x, s)
192 // s is constant so the strcat can be optimized further
193 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
198 //===---------------------------------------===//
199 // 'strchr' Optimizations
201 struct StrChrOpt : public LibCallOptimization {
202 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
203 // Verify the "strchr" function prototype.
204 const FunctionType *FT = Callee->getFunctionType();
205 if (FT->getNumParams() != 2 ||
206 FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
207 FT->getParamType(0) != FT->getReturnType())
210 Value *SrcStr = CI->getOperand(1);
212 // If the second operand is non-constant, see if we can compute the length
213 // of the input string and turn this into memchr.
214 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
216 // These optimizations require TargetData.
219 uint64_t Len = GetStringLength(SrcStr);
220 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
223 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
224 ConstantInt::get(TD->getIntPtrType(*Context), Len),
228 // Otherwise, the character is a constant, see if the first argument is
229 // a string literal. If so, we can constant fold.
231 if (!GetConstantStringInfo(SrcStr, Str))
234 // strchr can find the nul character.
236 char CharValue = CharC->getSExtValue();
238 // Compute the offset.
241 if (i == Str.size()) // Didn't find the char. strchr returns null.
242 return Constant::getNullValue(CI->getType());
243 // Did we find our match?
244 if (Str[i] == CharValue)
249 // strchr(s+n,c) -> gep(s+n+i,c)
250 Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), i);
251 return B.CreateGEP(SrcStr, Idx, "strchr");
255 //===---------------------------------------===//
256 // 'strcmp' Optimizations
258 struct StrCmpOpt : public LibCallOptimization {
259 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
260 // Verify the "strcmp" function prototype.
261 const FunctionType *FT = Callee->getFunctionType();
262 if (FT->getNumParams() != 2 ||
263 !FT->getReturnType()->isIntegerTy(32) ||
264 FT->getParamType(0) != FT->getParamType(1) ||
265 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
268 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
269 if (Str1P == Str2P) // strcmp(x,x) -> 0
270 return ConstantInt::get(CI->getType(), 0);
272 std::string Str1, Str2;
273 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
274 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
276 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
277 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
279 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
280 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
282 // strcmp(x, y) -> cnst (if both x and y are constant strings)
283 if (HasStr1 && HasStr2)
284 return ConstantInt::get(CI->getType(),
285 strcmp(Str1.c_str(),Str2.c_str()));
287 // strcmp(P, "x") -> memcmp(P, "x", 2)
288 uint64_t Len1 = GetStringLength(Str1P);
289 uint64_t Len2 = GetStringLength(Str2P);
291 // These optimizations require TargetData.
294 return EmitMemCmp(Str1P, Str2P,
295 ConstantInt::get(TD->getIntPtrType(*Context),
296 std::min(Len1, Len2)), B, TD);
303 //===---------------------------------------===//
304 // 'strncmp' Optimizations
306 struct StrNCmpOpt : public LibCallOptimization {
307 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
308 // Verify the "strncmp" function prototype.
309 const FunctionType *FT = Callee->getFunctionType();
310 if (FT->getNumParams() != 3 ||
311 !FT->getReturnType()->isIntegerTy(32) ||
312 FT->getParamType(0) != FT->getParamType(1) ||
313 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
314 !FT->getParamType(2)->isIntegerTy())
317 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
318 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
319 return ConstantInt::get(CI->getType(), 0);
321 // Get the length argument if it is constant.
323 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
324 Length = LengthArg->getZExtValue();
328 if (Length == 0) // strncmp(x,y,0) -> 0
329 return ConstantInt::get(CI->getType(), 0);
331 std::string Str1, Str2;
332 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
333 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
335 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
336 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
338 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
339 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
341 // strncmp(x, y) -> cnst (if both x and y are constant strings)
342 if (HasStr1 && HasStr2)
343 return ConstantInt::get(CI->getType(),
344 strncmp(Str1.c_str(), Str2.c_str(), Length));
350 //===---------------------------------------===//
351 // 'strcpy' Optimizations
353 struct StrCpyOpt : public LibCallOptimization {
354 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
356 StrCpyOpt(bool c) : OptChkCall(c) {}
358 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
359 // Verify the "strcpy" function prototype.
360 unsigned NumParams = OptChkCall ? 3 : 2;
361 const FunctionType *FT = Callee->getFunctionType();
362 if (FT->getNumParams() != NumParams ||
363 FT->getReturnType() != FT->getParamType(0) ||
364 FT->getParamType(0) != FT->getParamType(1) ||
365 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
368 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
369 if (Dst == Src) // strcpy(x,x) -> x
372 // These optimizations require TargetData.
375 // See if we can get the length of the input string.
376 uint64_t Len = GetStringLength(Src);
377 if (Len == 0) return 0;
379 // We have enough information to now generate the memcpy call to do the
380 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
382 EmitMemCpyChk(Dst, Src,
383 ConstantInt::get(TD->getIntPtrType(*Context), Len),
384 CI->getOperand(3), B, TD);
387 ConstantInt::get(TD->getIntPtrType(*Context), Len),
393 //===---------------------------------------===//
394 // 'strncpy' Optimizations
396 struct StrNCpyOpt : public LibCallOptimization {
397 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
398 const FunctionType *FT = Callee->getFunctionType();
399 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
400 FT->getParamType(0) != FT->getParamType(1) ||
401 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
402 !FT->getParamType(2)->isIntegerTy())
405 Value *Dst = CI->getOperand(1);
406 Value *Src = CI->getOperand(2);
407 Value *LenOp = CI->getOperand(3);
409 // See if we can get the length of the input string.
410 uint64_t SrcLen = GetStringLength(Src);
411 if (SrcLen == 0) return 0;
415 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
416 EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
417 LenOp, false, B, TD);
422 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
423 Len = LengthArg->getZExtValue();
427 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
429 // These optimizations require TargetData.
432 // Let strncpy handle the zero padding
433 if (Len > SrcLen+1) return 0;
435 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
437 ConstantInt::get(TD->getIntPtrType(*Context), Len),
444 //===---------------------------------------===//
445 // 'strlen' Optimizations
447 struct StrLenOpt : public LibCallOptimization {
448 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
449 const FunctionType *FT = Callee->getFunctionType();
450 if (FT->getNumParams() != 1 ||
451 FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
452 !FT->getReturnType()->isIntegerTy())
455 Value *Src = CI->getOperand(1);
457 // Constant folding: strlen("xyz") -> 3
458 if (uint64_t Len = GetStringLength(Src))
459 return ConstantInt::get(CI->getType(), Len-1);
461 // strlen(x) != 0 --> *x != 0
462 // strlen(x) == 0 --> *x == 0
463 if (IsOnlyUsedInZeroEqualityComparison(CI))
464 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
469 //===---------------------------------------===//
470 // 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
472 struct StrToOpt : public LibCallOptimization {
473 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
474 const FunctionType *FT = Callee->getFunctionType();
475 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
476 !FT->getParamType(0)->isPointerTy() ||
477 !FT->getParamType(1)->isPointerTy())
480 Value *EndPtr = CI->getOperand(2);
481 if (isa<ConstantPointerNull>(EndPtr)) {
482 CI->setOnlyReadsMemory();
483 CI->addAttribute(1, Attribute::NoCapture);
490 //===---------------------------------------===//
491 // 'strstr' Optimizations
493 struct StrStrOpt : public LibCallOptimization {
494 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
495 const FunctionType *FT = Callee->getFunctionType();
496 if (FT->getNumParams() != 2 ||
497 !FT->getParamType(0)->isPointerTy() ||
498 !FT->getParamType(1)->isPointerTy() ||
499 !FT->getReturnType()->isPointerTy())
502 // fold strstr(x, x) -> x.
503 if (CI->getOperand(1) == CI->getOperand(2))
504 return B.CreateBitCast(CI->getOperand(1), CI->getType());
506 // See if either input string is a constant string.
507 std::string SearchStr, ToFindStr;
508 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
509 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
511 // fold strstr(x, "") -> x.
512 if (HasStr2 && ToFindStr.empty())
513 return B.CreateBitCast(CI->getOperand(1), CI->getType());
515 // If both strings are known, constant fold it.
516 if (HasStr1 && HasStr2) {
517 std::string::size_type Offset = SearchStr.find(ToFindStr);
519 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
520 return Constant::getNullValue(CI->getType());
522 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
523 Value *Result = CastToCStr(CI->getOperand(1), B);
524 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
525 return B.CreateBitCast(Result, CI->getType());
528 // fold strstr(x, "y") -> strchr(x, 'y').
529 if (HasStr2 && ToFindStr.size() == 1)
530 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B, TD),
537 //===---------------------------------------===//
538 // 'memcmp' Optimizations
540 struct MemCmpOpt : public LibCallOptimization {
541 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
542 const FunctionType *FT = Callee->getFunctionType();
543 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
544 !FT->getParamType(1)->isPointerTy() ||
545 !FT->getReturnType()->isIntegerTy(32))
548 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
550 if (LHS == RHS) // memcmp(s,s,x) -> 0
551 return Constant::getNullValue(CI->getType());
553 // Make sure we have a constant length.
554 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
556 uint64_t Len = LenC->getZExtValue();
558 if (Len == 0) // memcmp(s1,s2,0) -> 0
559 return Constant::getNullValue(CI->getType());
561 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
563 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
564 CI->getType(), "lhsv");
565 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
566 CI->getType(), "rhsv");
567 return B.CreateSub(LHSV, RHSV, "chardiff");
570 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
571 std::string LHSStr, RHSStr;
572 if (GetConstantStringInfo(LHS, LHSStr) &&
573 GetConstantStringInfo(RHS, RHSStr)) {
574 // Make sure we're not reading out-of-bounds memory.
575 if (Len > LHSStr.length() || Len > RHSStr.length())
577 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
578 return ConstantInt::get(CI->getType(), Ret);
585 //===---------------------------------------===//
586 // 'memcpy' Optimizations
588 struct MemCpyOpt : public LibCallOptimization {
589 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
590 // These optimizations require TargetData.
593 const FunctionType *FT = Callee->getFunctionType();
594 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
595 !FT->getParamType(0)->isPointerTy() ||
596 !FT->getParamType(1)->isPointerTy() ||
597 FT->getParamType(2) != TD->getIntPtrType(*Context))
600 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
601 EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
602 CI->getOperand(3), 1, false, B, TD);
603 return CI->getOperand(1);
607 //===---------------------------------------===//
608 // 'memmove' Optimizations
610 struct MemMoveOpt : public LibCallOptimization {
611 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
612 // These optimizations require TargetData.
615 const FunctionType *FT = Callee->getFunctionType();
616 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
617 !FT->getParamType(0)->isPointerTy() ||
618 !FT->getParamType(1)->isPointerTy() ||
619 FT->getParamType(2) != TD->getIntPtrType(*Context))
622 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
623 EmitMemMove(CI->getOperand(1), CI->getOperand(2),
624 CI->getOperand(3), 1, false, B, TD);
625 return CI->getOperand(1);
629 //===---------------------------------------===//
630 // 'memset' Optimizations
632 struct MemSetOpt : public LibCallOptimization {
633 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
634 // These optimizations require TargetData.
637 const FunctionType *FT = Callee->getFunctionType();
638 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
639 !FT->getParamType(0)->isPointerTy() ||
640 !FT->getParamType(1)->isIntegerTy() ||
641 FT->getParamType(2) != TD->getIntPtrType(*Context))
644 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
645 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
647 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), false, B, TD);
648 return CI->getOperand(1);
652 //===----------------------------------------------------------------------===//
653 // Math Library Optimizations
654 //===----------------------------------------------------------------------===//
656 //===---------------------------------------===//
657 // 'pow*' Optimizations
659 struct PowOpt : public LibCallOptimization {
660 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
661 const FunctionType *FT = Callee->getFunctionType();
662 // Just make sure this has 2 arguments of the same FP type, which match the
664 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
665 FT->getParamType(0) != FT->getParamType(1) ||
666 !FT->getParamType(0)->isFloatingPointTy())
669 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
670 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
671 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
673 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
674 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
677 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
678 if (Op2C == 0) return 0;
680 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
681 return ConstantFP::get(CI->getType(), 1.0);
683 if (Op2C->isExactlyValue(0.5)) {
684 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
685 // This is faster than calling pow, and still handles negative zero
686 // and negative infinite correctly.
687 // TODO: In fast-math mode, this could be just sqrt(x).
688 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
689 Value *Inf = ConstantFP::getInfinity(CI->getType());
690 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
691 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
692 Callee->getAttributes());
693 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
694 Callee->getAttributes());
695 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
696 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
700 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
702 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
703 return B.CreateFMul(Op1, Op1, "pow2");
704 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
705 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
711 //===---------------------------------------===//
712 // 'exp2' Optimizations
714 struct Exp2Opt : public LibCallOptimization {
715 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
716 const FunctionType *FT = Callee->getFunctionType();
717 // Just make sure this has 1 argument of FP type, which matches the
719 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
720 !FT->getParamType(0)->isFloatingPointTy())
723 Value *Op = CI->getOperand(1);
724 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
725 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
727 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
728 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
729 LdExpArg = B.CreateSExt(OpC->getOperand(0),
730 Type::getInt32Ty(*Context), "tmp");
731 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
732 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
733 LdExpArg = B.CreateZExt(OpC->getOperand(0),
734 Type::getInt32Ty(*Context), "tmp");
739 if (Op->getType()->isFloatTy())
741 else if (Op->getType()->isDoubleTy())
746 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
747 if (!Op->getType()->isFloatTy())
748 One = ConstantExpr::getFPExtend(One, Op->getType());
750 Module *M = Caller->getParent();
751 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
753 Type::getInt32Ty(*Context),NULL);
754 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
755 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
756 CI->setCallingConv(F->getCallingConv());
764 //===---------------------------------------===//
765 // Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
767 struct UnaryDoubleFPOpt : public LibCallOptimization {
768 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
769 const FunctionType *FT = Callee->getFunctionType();
770 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
771 !FT->getParamType(0)->isDoubleTy())
774 // If this is something like 'floor((double)floatval)', convert to floorf.
775 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
776 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
779 // floor((double)floatval) -> (double)floorf(floatval)
780 Value *V = Cast->getOperand(0);
781 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
782 Callee->getAttributes());
783 return B.CreateFPExt(V, Type::getDoubleTy(*Context));
787 //===----------------------------------------------------------------------===//
788 // Integer Optimizations
789 //===----------------------------------------------------------------------===//
791 //===---------------------------------------===//
792 // 'ffs*' Optimizations
794 struct FFSOpt : public LibCallOptimization {
795 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
796 const FunctionType *FT = Callee->getFunctionType();
797 // Just make sure this has 2 arguments of the same FP type, which match the
799 if (FT->getNumParams() != 1 ||
800 !FT->getReturnType()->isIntegerTy(32) ||
801 !FT->getParamType(0)->isIntegerTy())
804 Value *Op = CI->getOperand(1);
807 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
808 if (CI->getValue() == 0) // ffs(0) -> 0.
809 return Constant::getNullValue(CI->getType());
810 return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
811 CI->getValue().countTrailingZeros()+1);
814 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
815 const Type *ArgType = Op->getType();
816 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
817 Intrinsic::cttz, &ArgType, 1);
818 Value *V = B.CreateCall(F, Op, "cttz");
819 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
820 V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
822 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
823 return B.CreateSelect(Cond, V,
824 ConstantInt::get(Type::getInt32Ty(*Context), 0));
828 //===---------------------------------------===//
829 // 'isdigit' Optimizations
831 struct IsDigitOpt : public LibCallOptimization {
832 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
833 const FunctionType *FT = Callee->getFunctionType();
834 // We require integer(i32)
835 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
836 !FT->getParamType(0)->isIntegerTy(32))
839 // isdigit(c) -> (c-'0') <u 10
840 Value *Op = CI->getOperand(1);
841 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
843 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
845 return B.CreateZExt(Op, CI->getType());
849 //===---------------------------------------===//
850 // 'isascii' Optimizations
852 struct IsAsciiOpt : public LibCallOptimization {
853 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
854 const FunctionType *FT = Callee->getFunctionType();
855 // We require integer(i32)
856 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
857 !FT->getParamType(0)->isIntegerTy(32))
860 // isascii(c) -> c <u 128
861 Value *Op = CI->getOperand(1);
862 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
864 return B.CreateZExt(Op, CI->getType());
868 //===---------------------------------------===//
869 // 'abs', 'labs', 'llabs' Optimizations
871 struct AbsOpt : public LibCallOptimization {
872 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
873 const FunctionType *FT = Callee->getFunctionType();
874 // We require integer(integer) where the types agree.
875 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
876 FT->getParamType(0) != FT->getReturnType())
879 // abs(x) -> x >s -1 ? x : -x
880 Value *Op = CI->getOperand(1);
881 Value *Pos = B.CreateICmpSGT(Op,
882 Constant::getAllOnesValue(Op->getType()),
884 Value *Neg = B.CreateNeg(Op, "neg");
885 return B.CreateSelect(Pos, Op, Neg);
890 //===---------------------------------------===//
891 // 'toascii' Optimizations
893 struct ToAsciiOpt : public LibCallOptimization {
894 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
895 const FunctionType *FT = Callee->getFunctionType();
896 // We require i32(i32)
897 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
898 !FT->getParamType(0)->isIntegerTy(32))
901 // isascii(c) -> c & 0x7f
902 return B.CreateAnd(CI->getOperand(1),
903 ConstantInt::get(CI->getType(),0x7F));
907 //===----------------------------------------------------------------------===//
908 // Formatting and IO Optimizations
909 //===----------------------------------------------------------------------===//
911 //===---------------------------------------===//
912 // 'printf' Optimizations
914 struct PrintFOpt : public LibCallOptimization {
915 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
916 // Require one fixed pointer argument and an integer/void result.
917 const FunctionType *FT = Callee->getFunctionType();
918 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
919 !(FT->getReturnType()->isIntegerTy() ||
920 FT->getReturnType()->isVoidTy()))
923 // Check for a fixed format string.
924 std::string FormatStr;
925 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
928 // Empty format string -> noop.
929 if (FormatStr.empty()) // Tolerate printf's declared void.
930 return CI->use_empty() ? (Value*)CI :
931 ConstantInt::get(CI->getType(), 0);
933 // printf("x") -> putchar('x'), even for '%'. Return the result of putchar
934 // in case there is an error writing to stdout.
935 if (FormatStr.size() == 1) {
936 Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
937 FormatStr[0]), B, TD);
938 if (CI->use_empty()) return CI;
939 return B.CreateIntCast(Res, CI->getType(), true);
942 // printf("foo\n") --> puts("foo")
943 if (FormatStr[FormatStr.size()-1] == '\n' &&
944 FormatStr.find('%') == std::string::npos) { // no format characters.
945 // Create a string literal with no \n on it. We expect the constant merge
946 // pass to be run after this pass, to merge duplicate strings.
947 FormatStr.erase(FormatStr.end()-1);
948 Constant *C = ConstantArray::get(*Context, FormatStr, true);
949 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
950 GlobalVariable::InternalLinkage, C, "str");
952 return CI->use_empty() ? (Value*)CI :
953 ConstantInt::get(CI->getType(), FormatStr.size()+1);
956 // Optimize specific format strings.
957 // printf("%c", chr) --> putchar(*(i8*)dst)
958 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
959 CI->getOperand(2)->getType()->isIntegerTy()) {
960 Value *Res = EmitPutChar(CI->getOperand(2), B, TD);
962 if (CI->use_empty()) return CI;
963 return B.CreateIntCast(Res, CI->getType(), true);
966 // printf("%s\n", str) --> puts(str)
967 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
968 CI->getOperand(2)->getType()->isPointerTy() &&
970 EmitPutS(CI->getOperand(2), B, TD);
977 //===---------------------------------------===//
978 // 'sprintf' Optimizations
980 struct SPrintFOpt : public LibCallOptimization {
981 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
982 // Require two fixed pointer arguments and an integer result.
983 const FunctionType *FT = Callee->getFunctionType();
984 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
985 !FT->getParamType(1)->isPointerTy() ||
986 !FT->getReturnType()->isIntegerTy())
989 // Check for a fixed format string.
990 std::string FormatStr;
991 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
994 // If we just have a format string (nothing else crazy) transform it.
995 if (CI->getNumOperands() == 3) {
996 // Make sure there's no % in the constant array. We could try to handle
997 // %% -> % in the future if we cared.
998 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
999 if (FormatStr[i] == '%')
1000 return 0; // we found a format specifier, bail out.
1002 // These optimizations require TargetData.
1005 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1006 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
1007 ConstantInt::get(TD->getIntPtrType(*Context),
1008 FormatStr.size()+1), 1, false, B, TD);
1009 return ConstantInt::get(CI->getType(), FormatStr.size());
1012 // The remaining optimizations require the format string to be "%s" or "%c"
1013 // and have an extra operand.
1014 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1017 // Decode the second character of the format string.
1018 if (FormatStr[1] == 'c') {
1019 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1020 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
1021 Value *V = B.CreateTrunc(CI->getOperand(3),
1022 Type::getInt8Ty(*Context), "char");
1023 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1024 B.CreateStore(V, Ptr);
1025 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
1027 B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
1029 return ConstantInt::get(CI->getType(), 1);
1032 if (FormatStr[1] == 's') {
1033 // These optimizations require TargetData.
1036 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1037 if (!CI->getOperand(3)->getType()->isPointerTy()) return 0;
1039 Value *Len = EmitStrLen(CI->getOperand(3), B, TD);
1040 Value *IncLen = B.CreateAdd(Len,
1041 ConstantInt::get(Len->getType(), 1),
1043 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, false, B, TD);
1045 // The sprintf result is the unincremented number of bytes in the string.
1046 return B.CreateIntCast(Len, CI->getType(), false);
1052 //===---------------------------------------===//
1053 // 'fwrite' Optimizations
1055 struct FWriteOpt : public LibCallOptimization {
1056 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1057 // Require a pointer, an integer, an integer, a pointer, returning integer.
1058 const FunctionType *FT = Callee->getFunctionType();
1059 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1060 !FT->getParamType(1)->isIntegerTy() ||
1061 !FT->getParamType(2)->isIntegerTy() ||
1062 !FT->getParamType(3)->isPointerTy() ||
1063 !FT->getReturnType()->isIntegerTy())
1066 // Get the element size and count.
1067 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1068 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1069 if (!SizeC || !CountC) return 0;
1070 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
1072 // If this is writing zero records, remove the call (it's a noop).
1074 return ConstantInt::get(CI->getType(), 0);
1076 // If this is writing one byte, turn it into fputc.
1077 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1078 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1079 EmitFPutC(Char, CI->getOperand(4), B, TD);
1080 return ConstantInt::get(CI->getType(), 1);
1087 //===---------------------------------------===//
1088 // 'fputs' Optimizations
1090 struct FPutsOpt : public LibCallOptimization {
1091 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1092 // These optimizations require TargetData.
1095 // Require two pointers. Also, we can't optimize if return value is used.
1096 const FunctionType *FT = Callee->getFunctionType();
1097 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1098 !FT->getParamType(1)->isPointerTy() ||
1102 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1103 uint64_t Len = GetStringLength(CI->getOperand(1));
1105 EmitFWrite(CI->getOperand(1),
1106 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
1107 CI->getOperand(2), B, TD);
1108 return CI; // Known to have no uses (see above).
1112 //===---------------------------------------===//
1113 // 'fprintf' Optimizations
1115 struct FPrintFOpt : public LibCallOptimization {
1116 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1117 // Require two fixed paramters as pointers and integer result.
1118 const FunctionType *FT = Callee->getFunctionType();
1119 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1120 !FT->getParamType(1)->isPointerTy() ||
1121 !FT->getReturnType()->isIntegerTy())
1124 // All the optimizations depend on the format string.
1125 std::string FormatStr;
1126 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1129 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1130 if (CI->getNumOperands() == 3) {
1131 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1132 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
1133 return 0; // We found a format specifier.
1135 // These optimizations require TargetData.
1138 EmitFWrite(CI->getOperand(2),
1139 ConstantInt::get(TD->getIntPtrType(*Context),
1141 CI->getOperand(1), B, TD);
1142 return ConstantInt::get(CI->getType(), FormatStr.size());
1145 // The remaining optimizations require the format string to be "%s" or "%c"
1146 // and have an extra operand.
1147 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1150 // Decode the second character of the format string.
1151 if (FormatStr[1] == 'c') {
1152 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1153 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
1154 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B, TD);
1155 return ConstantInt::get(CI->getType(), 1);
1158 if (FormatStr[1] == 's') {
1159 // fprintf(F, "%s", str) -> fputs(str, F)
1160 if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty())
1162 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B, TD);
1169 } // end anonymous namespace.
1171 //===----------------------------------------------------------------------===//
1172 // SimplifyLibCalls Pass Implementation
1173 //===----------------------------------------------------------------------===//
1176 /// This pass optimizes well known library functions from libc and libm.
1178 class SimplifyLibCalls : public FunctionPass {
1179 StringMap<LibCallOptimization*> Optimizations;
1180 // String and Memory LibCall Optimizations
1181 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
1182 StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
1183 StrNCpyOpt StrNCpy; StrLenOpt StrLen;
1184 StrToOpt StrTo; StrStrOpt StrStr;
1185 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
1186 // Math Library Optimizations
1187 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
1188 // Integer Optimizations
1189 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1191 // Formatting and IO Optimizations
1192 SPrintFOpt SPrintF; PrintFOpt PrintF;
1193 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
1195 bool Modified; // This is only used by doInitialization.
1197 static char ID; // Pass identification
1198 SimplifyLibCalls() : FunctionPass(&ID), StrCpy(false), StrCpyChk(true) {}
1199 void InitOptimizations();
1200 bool runOnFunction(Function &F);
1202 void setDoesNotAccessMemory(Function &F);
1203 void setOnlyReadsMemory(Function &F);
1204 void setDoesNotThrow(Function &F);
1205 void setDoesNotCapture(Function &F, unsigned n);
1206 void setDoesNotAlias(Function &F, unsigned n);
1207 bool doInitialization(Module &M);
1209 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1212 char SimplifyLibCalls::ID = 0;
1213 } // end anonymous namespace.
1215 static RegisterPass<SimplifyLibCalls>
1216 X("simplify-libcalls", "Simplify well-known library calls");
1218 // Public interface to the Simplify LibCalls pass.
1219 FunctionPass *llvm::createSimplifyLibCallsPass() {
1220 return new SimplifyLibCalls();
1223 /// Optimizations - Populate the Optimizations map with all the optimizations
1225 void SimplifyLibCalls::InitOptimizations() {
1226 // String and Memory LibCall Optimizations
1227 Optimizations["strcat"] = &StrCat;
1228 Optimizations["strncat"] = &StrNCat;
1229 Optimizations["strchr"] = &StrChr;
1230 Optimizations["strcmp"] = &StrCmp;
1231 Optimizations["strncmp"] = &StrNCmp;
1232 Optimizations["strcpy"] = &StrCpy;
1233 Optimizations["strncpy"] = &StrNCpy;
1234 Optimizations["strlen"] = &StrLen;
1235 Optimizations["strtol"] = &StrTo;
1236 Optimizations["strtod"] = &StrTo;
1237 Optimizations["strtof"] = &StrTo;
1238 Optimizations["strtoul"] = &StrTo;
1239 Optimizations["strtoll"] = &StrTo;
1240 Optimizations["strtold"] = &StrTo;
1241 Optimizations["strtoull"] = &StrTo;
1242 Optimizations["strstr"] = &StrStr;
1243 Optimizations["memcmp"] = &MemCmp;
1244 Optimizations["memcpy"] = &MemCpy;
1245 Optimizations["memmove"] = &MemMove;
1246 Optimizations["memset"] = &MemSet;
1248 // _chk variants of String and Memory LibCall Optimizations.
1249 Optimizations["__strcpy_chk"] = &StrCpyChk;
1251 // Math Library Optimizations
1252 Optimizations["powf"] = &Pow;
1253 Optimizations["pow"] = &Pow;
1254 Optimizations["powl"] = &Pow;
1255 Optimizations["llvm.pow.f32"] = &Pow;
1256 Optimizations["llvm.pow.f64"] = &Pow;
1257 Optimizations["llvm.pow.f80"] = &Pow;
1258 Optimizations["llvm.pow.f128"] = &Pow;
1259 Optimizations["llvm.pow.ppcf128"] = &Pow;
1260 Optimizations["exp2l"] = &Exp2;
1261 Optimizations["exp2"] = &Exp2;
1262 Optimizations["exp2f"] = &Exp2;
1263 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1264 Optimizations["llvm.exp2.f128"] = &Exp2;
1265 Optimizations["llvm.exp2.f80"] = &Exp2;
1266 Optimizations["llvm.exp2.f64"] = &Exp2;
1267 Optimizations["llvm.exp2.f32"] = &Exp2;
1270 Optimizations["floor"] = &UnaryDoubleFP;
1273 Optimizations["ceil"] = &UnaryDoubleFP;
1276 Optimizations["round"] = &UnaryDoubleFP;
1279 Optimizations["rint"] = &UnaryDoubleFP;
1281 #ifdef HAVE_NEARBYINTF
1282 Optimizations["nearbyint"] = &UnaryDoubleFP;
1285 // Integer Optimizations
1286 Optimizations["ffs"] = &FFS;
1287 Optimizations["ffsl"] = &FFS;
1288 Optimizations["ffsll"] = &FFS;
1289 Optimizations["abs"] = &Abs;
1290 Optimizations["labs"] = &Abs;
1291 Optimizations["llabs"] = &Abs;
1292 Optimizations["isdigit"] = &IsDigit;
1293 Optimizations["isascii"] = &IsAscii;
1294 Optimizations["toascii"] = &ToAscii;
1296 // Formatting and IO Optimizations
1297 Optimizations["sprintf"] = &SPrintF;
1298 Optimizations["printf"] = &PrintF;
1299 Optimizations["fwrite"] = &FWrite;
1300 Optimizations["fputs"] = &FPuts;
1301 Optimizations["fprintf"] = &FPrintF;
1305 /// runOnFunction - Top level algorithm.
1307 bool SimplifyLibCalls::runOnFunction(Function &F) {
1308 if (Optimizations.empty())
1309 InitOptimizations();
1311 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
1313 IRBuilder<> Builder(F.getContext());
1315 bool Changed = false;
1316 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1317 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1318 // Ignore non-calls.
1319 CallInst *CI = dyn_cast<CallInst>(I++);
1322 // Ignore indirect calls and calls to non-external functions.
1323 Function *Callee = CI->getCalledFunction();
1324 if (Callee == 0 || !Callee->isDeclaration() ||
1325 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1328 // Ignore unknown calls.
1329 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1332 // Set the builder to the instruction after the call.
1333 Builder.SetInsertPoint(BB, I);
1335 // Try to optimize this call.
1336 Value *Result = LCO->OptimizeCall(CI, TD, Builder);
1337 if (Result == 0) continue;
1339 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1340 dbgs() << " into: " << *Result << "\n");
1342 // Something changed!
1346 // Inspect the instruction after the call (which was potentially just
1350 if (CI != Result && !CI->use_empty()) {
1351 CI->replaceAllUsesWith(Result);
1352 if (!Result->hasName())
1353 Result->takeName(CI);
1355 CI->eraseFromParent();
1361 // Utility methods for doInitialization.
1363 void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1364 if (!F.doesNotAccessMemory()) {
1365 F.setDoesNotAccessMemory();
1370 void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1371 if (!F.onlyReadsMemory()) {
1372 F.setOnlyReadsMemory();
1377 void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1378 if (!F.doesNotThrow()) {
1379 F.setDoesNotThrow();
1384 void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1385 if (!F.doesNotCapture(n)) {
1386 F.setDoesNotCapture(n);
1391 void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1392 if (!F.doesNotAlias(n)) {
1393 F.setDoesNotAlias(n);
1399 /// doInitialization - Add attributes to well-known functions.
1401 bool SimplifyLibCalls::doInitialization(Module &M) {
1403 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1405 if (!F.isDeclaration())
1411 const FunctionType *FTy = F.getFunctionType();
1413 StringRef Name = F.getName();
1416 if (Name == "strlen") {
1417 if (FTy->getNumParams() != 1 ||
1418 !FTy->getParamType(0)->isPointerTy())
1420 setOnlyReadsMemory(F);
1422 setDoesNotCapture(F, 1);
1423 } else if (Name == "strchr" ||
1424 Name == "strrchr") {
1425 if (FTy->getNumParams() != 2 ||
1426 !FTy->getParamType(0)->isPointerTy() ||
1427 !FTy->getParamType(1)->isIntegerTy())
1429 setOnlyReadsMemory(F);
1431 } else if (Name == "strcpy" ||
1437 Name == "strtoul" ||
1438 Name == "strtoll" ||
1439 Name == "strtold" ||
1440 Name == "strncat" ||
1441 Name == "strncpy" ||
1442 Name == "strtoull") {
1443 if (FTy->getNumParams() < 2 ||
1444 !FTy->getParamType(1)->isPointerTy())
1447 setDoesNotCapture(F, 2);
1448 } else if (Name == "strxfrm") {
1449 if (FTy->getNumParams() != 3 ||
1450 !FTy->getParamType(0)->isPointerTy() ||
1451 !FTy->getParamType(1)->isPointerTy())
1454 setDoesNotCapture(F, 1);
1455 setDoesNotCapture(F, 2);
1456 } else if (Name == "strcmp" ||
1458 Name == "strncmp" ||
1459 Name == "strcspn" ||
1460 Name == "strcoll" ||
1461 Name == "strcasecmp" ||
1462 Name == "strncasecmp") {
1463 if (FTy->getNumParams() < 2 ||
1464 !FTy->getParamType(0)->isPointerTy() ||
1465 !FTy->getParamType(1)->isPointerTy())
1467 setOnlyReadsMemory(F);
1469 setDoesNotCapture(F, 1);
1470 setDoesNotCapture(F, 2);
1471 } else if (Name == "strstr" ||
1472 Name == "strpbrk") {
1473 if (FTy->getNumParams() != 2 ||
1474 !FTy->getParamType(1)->isPointerTy())
1476 setOnlyReadsMemory(F);
1478 setDoesNotCapture(F, 2);
1479 } else if (Name == "strtok" ||
1480 Name == "strtok_r") {
1481 if (FTy->getNumParams() < 2 ||
1482 !FTy->getParamType(1)->isPointerTy())
1485 setDoesNotCapture(F, 2);
1486 } else if (Name == "scanf" ||
1488 Name == "setvbuf") {
1489 if (FTy->getNumParams() < 1 ||
1490 !FTy->getParamType(0)->isPointerTy())
1493 setDoesNotCapture(F, 1);
1494 } else if (Name == "strdup" ||
1495 Name == "strndup") {
1496 if (FTy->getNumParams() < 1 ||
1497 !FTy->getReturnType()->isPointerTy() ||
1498 !FTy->getParamType(0)->isPointerTy())
1501 setDoesNotAlias(F, 0);
1502 setDoesNotCapture(F, 1);
1503 } else if (Name == "stat" ||
1505 Name == "sprintf" ||
1506 Name == "statvfs") {
1507 if (FTy->getNumParams() < 2 ||
1508 !FTy->getParamType(0)->isPointerTy() ||
1509 !FTy->getParamType(1)->isPointerTy())
1512 setDoesNotCapture(F, 1);
1513 setDoesNotCapture(F, 2);
1514 } else if (Name == "snprintf") {
1515 if (FTy->getNumParams() != 3 ||
1516 !FTy->getParamType(0)->isPointerTy() ||
1517 !FTy->getParamType(2)->isPointerTy())
1520 setDoesNotCapture(F, 1);
1521 setDoesNotCapture(F, 3);
1522 } else if (Name == "setitimer") {
1523 if (FTy->getNumParams() != 3 ||
1524 !FTy->getParamType(1)->isPointerTy() ||
1525 !FTy->getParamType(2)->isPointerTy())
1528 setDoesNotCapture(F, 2);
1529 setDoesNotCapture(F, 3);
1530 } else if (Name == "system") {
1531 if (FTy->getNumParams() != 1 ||
1532 !FTy->getParamType(0)->isPointerTy())
1534 // May throw; "system" is a valid pthread cancellation point.
1535 setDoesNotCapture(F, 1);
1539 if (Name == "malloc") {
1540 if (FTy->getNumParams() != 1 ||
1541 !FTy->getReturnType()->isPointerTy())
1544 setDoesNotAlias(F, 0);
1545 } else if (Name == "memcmp") {
1546 if (FTy->getNumParams() != 3 ||
1547 !FTy->getParamType(0)->isPointerTy() ||
1548 !FTy->getParamType(1)->isPointerTy())
1550 setOnlyReadsMemory(F);
1552 setDoesNotCapture(F, 1);
1553 setDoesNotCapture(F, 2);
1554 } else if (Name == "memchr" ||
1555 Name == "memrchr") {
1556 if (FTy->getNumParams() != 3)
1558 setOnlyReadsMemory(F);
1560 } else if (Name == "modf" ||
1564 Name == "memccpy" ||
1565 Name == "memmove") {
1566 if (FTy->getNumParams() < 2 ||
1567 !FTy->getParamType(1)->isPointerTy())
1570 setDoesNotCapture(F, 2);
1571 } else if (Name == "memalign") {
1572 if (!FTy->getReturnType()->isPointerTy())
1574 setDoesNotAlias(F, 0);
1575 } else if (Name == "mkdir" ||
1577 if (FTy->getNumParams() == 0 ||
1578 !FTy->getParamType(0)->isPointerTy())
1581 setDoesNotCapture(F, 1);
1585 if (Name == "realloc") {
1586 if (FTy->getNumParams() != 2 ||
1587 !FTy->getParamType(0)->isPointerTy() ||
1588 !FTy->getReturnType()->isPointerTy())
1591 setDoesNotAlias(F, 0);
1592 setDoesNotCapture(F, 1);
1593 } else if (Name == "read") {
1594 if (FTy->getNumParams() != 3 ||
1595 !FTy->getParamType(1)->isPointerTy())
1597 // May throw; "read" is a valid pthread cancellation point.
1598 setDoesNotCapture(F, 2);
1599 } else if (Name == "rmdir" ||
1602 Name == "realpath") {
1603 if (FTy->getNumParams() < 1 ||
1604 !FTy->getParamType(0)->isPointerTy())
1607 setDoesNotCapture(F, 1);
1608 } else if (Name == "rename" ||
1609 Name == "readlink") {
1610 if (FTy->getNumParams() < 2 ||
1611 !FTy->getParamType(0)->isPointerTy() ||
1612 !FTy->getParamType(1)->isPointerTy())
1615 setDoesNotCapture(F, 1);
1616 setDoesNotCapture(F, 2);
1620 if (Name == "write") {
1621 if (FTy->getNumParams() != 3 ||
1622 !FTy->getParamType(1)->isPointerTy())
1624 // May throw; "write" is a valid pthread cancellation point.
1625 setDoesNotCapture(F, 2);
1629 if (Name == "bcopy") {
1630 if (FTy->getNumParams() != 3 ||
1631 !FTy->getParamType(0)->isPointerTy() ||
1632 !FTy->getParamType(1)->isPointerTy())
1635 setDoesNotCapture(F, 1);
1636 setDoesNotCapture(F, 2);
1637 } else if (Name == "bcmp") {
1638 if (FTy->getNumParams() != 3 ||
1639 !FTy->getParamType(0)->isPointerTy() ||
1640 !FTy->getParamType(1)->isPointerTy())
1643 setOnlyReadsMemory(F);
1644 setDoesNotCapture(F, 1);
1645 setDoesNotCapture(F, 2);
1646 } else if (Name == "bzero") {
1647 if (FTy->getNumParams() != 2 ||
1648 !FTy->getParamType(0)->isPointerTy())
1651 setDoesNotCapture(F, 1);
1655 if (Name == "calloc") {
1656 if (FTy->getNumParams() != 2 ||
1657 !FTy->getReturnType()->isPointerTy())
1660 setDoesNotAlias(F, 0);
1661 } else if (Name == "chmod" ||
1663 Name == "ctermid" ||
1664 Name == "clearerr" ||
1665 Name == "closedir") {
1666 if (FTy->getNumParams() == 0 ||
1667 !FTy->getParamType(0)->isPointerTy())
1670 setDoesNotCapture(F, 1);
1674 if (Name == "atoi" ||
1678 if (FTy->getNumParams() != 1 ||
1679 !FTy->getParamType(0)->isPointerTy())
1682 setOnlyReadsMemory(F);
1683 setDoesNotCapture(F, 1);
1684 } else if (Name == "access") {
1685 if (FTy->getNumParams() != 2 ||
1686 !FTy->getParamType(0)->isPointerTy())
1689 setDoesNotCapture(F, 1);
1693 if (Name == "fopen") {
1694 if (FTy->getNumParams() != 2 ||
1695 !FTy->getReturnType()->isPointerTy() ||
1696 !FTy->getParamType(0)->isPointerTy() ||
1697 !FTy->getParamType(1)->isPointerTy())
1700 setDoesNotAlias(F, 0);
1701 setDoesNotCapture(F, 1);
1702 setDoesNotCapture(F, 2);
1703 } else if (Name == "fdopen") {
1704 if (FTy->getNumParams() != 2 ||
1705 !FTy->getReturnType()->isPointerTy() ||
1706 !FTy->getParamType(1)->isPointerTy())
1709 setDoesNotAlias(F, 0);
1710 setDoesNotCapture(F, 2);
1711 } else if (Name == "feof" ||
1721 Name == "fsetpos" ||
1722 Name == "flockfile" ||
1723 Name == "funlockfile" ||
1724 Name == "ftrylockfile") {
1725 if (FTy->getNumParams() == 0 ||
1726 !FTy->getParamType(0)->isPointerTy())
1729 setDoesNotCapture(F, 1);
1730 } else if (Name == "ferror") {
1731 if (FTy->getNumParams() != 1 ||
1732 !FTy->getParamType(0)->isPointerTy())
1735 setDoesNotCapture(F, 1);
1736 setOnlyReadsMemory(F);
1737 } else if (Name == "fputc" ||
1742 Name == "fstatvfs") {
1743 if (FTy->getNumParams() != 2 ||
1744 !FTy->getParamType(1)->isPointerTy())
1747 setDoesNotCapture(F, 2);
1748 } else if (Name == "fgets") {
1749 if (FTy->getNumParams() != 3 ||
1750 !FTy->getParamType(0)->isPointerTy() ||
1751 !FTy->getParamType(2)->isPointerTy())
1754 setDoesNotCapture(F, 3);
1755 } else if (Name == "fread" ||
1757 if (FTy->getNumParams() != 4 ||
1758 !FTy->getParamType(0)->isPointerTy() ||
1759 !FTy->getParamType(3)->isPointerTy())
1762 setDoesNotCapture(F, 1);
1763 setDoesNotCapture(F, 4);
1764 } else if (Name == "fputs" ||
1766 Name == "fprintf" ||
1767 Name == "fgetpos") {
1768 if (FTy->getNumParams() < 2 ||
1769 !FTy->getParamType(0)->isPointerTy() ||
1770 !FTy->getParamType(1)->isPointerTy())
1773 setDoesNotCapture(F, 1);
1774 setDoesNotCapture(F, 2);
1778 if (Name == "getc" ||
1779 Name == "getlogin_r" ||
1780 Name == "getc_unlocked") {
1781 if (FTy->getNumParams() == 0 ||
1782 !FTy->getParamType(0)->isPointerTy())
1785 setDoesNotCapture(F, 1);
1786 } else if (Name == "getenv") {
1787 if (FTy->getNumParams() != 1 ||
1788 !FTy->getParamType(0)->isPointerTy())
1791 setOnlyReadsMemory(F);
1792 setDoesNotCapture(F, 1);
1793 } else if (Name == "gets" ||
1794 Name == "getchar") {
1796 } else if (Name == "getitimer") {
1797 if (FTy->getNumParams() != 2 ||
1798 !FTy->getParamType(1)->isPointerTy())
1801 setDoesNotCapture(F, 2);
1802 } else if (Name == "getpwnam") {
1803 if (FTy->getNumParams() != 1 ||
1804 !FTy->getParamType(0)->isPointerTy())
1807 setDoesNotCapture(F, 1);
1811 if (Name == "ungetc") {
1812 if (FTy->getNumParams() != 2 ||
1813 !FTy->getParamType(1)->isPointerTy())
1816 setDoesNotCapture(F, 2);
1817 } else if (Name == "uname" ||
1819 Name == "unsetenv") {
1820 if (FTy->getNumParams() != 1 ||
1821 !FTy->getParamType(0)->isPointerTy())
1824 setDoesNotCapture(F, 1);
1825 } else if (Name == "utime" ||
1827 if (FTy->getNumParams() != 2 ||
1828 !FTy->getParamType(0)->isPointerTy() ||
1829 !FTy->getParamType(1)->isPointerTy())
1832 setDoesNotCapture(F, 1);
1833 setDoesNotCapture(F, 2);
1837 if (Name == "putc") {
1838 if (FTy->getNumParams() != 2 ||
1839 !FTy->getParamType(1)->isPointerTy())
1842 setDoesNotCapture(F, 2);
1843 } else if (Name == "puts" ||
1846 if (FTy->getNumParams() != 1 ||
1847 !FTy->getParamType(0)->isPointerTy())
1850 setDoesNotCapture(F, 1);
1851 } else if (Name == "pread" ||
1853 if (FTy->getNumParams() != 4 ||
1854 !FTy->getParamType(1)->isPointerTy())
1856 // May throw; these are valid pthread cancellation points.
1857 setDoesNotCapture(F, 2);
1858 } else if (Name == "putchar") {
1860 } else if (Name == "popen") {
1861 if (FTy->getNumParams() != 2 ||
1862 !FTy->getReturnType()->isPointerTy() ||
1863 !FTy->getParamType(0)->isPointerTy() ||
1864 !FTy->getParamType(1)->isPointerTy())
1867 setDoesNotAlias(F, 0);
1868 setDoesNotCapture(F, 1);
1869 setDoesNotCapture(F, 2);
1870 } else if (Name == "pclose") {
1871 if (FTy->getNumParams() != 1 ||
1872 !FTy->getParamType(0)->isPointerTy())
1875 setDoesNotCapture(F, 1);
1879 if (Name == "vscanf") {
1880 if (FTy->getNumParams() != 2 ||
1881 !FTy->getParamType(1)->isPointerTy())
1884 setDoesNotCapture(F, 1);
1885 } else if (Name == "vsscanf" ||
1886 Name == "vfscanf") {
1887 if (FTy->getNumParams() != 3 ||
1888 !FTy->getParamType(1)->isPointerTy() ||
1889 !FTy->getParamType(2)->isPointerTy())
1892 setDoesNotCapture(F, 1);
1893 setDoesNotCapture(F, 2);
1894 } else if (Name == "valloc") {
1895 if (!FTy->getReturnType()->isPointerTy())
1898 setDoesNotAlias(F, 0);
1899 } else if (Name == "vprintf") {
1900 if (FTy->getNumParams() != 2 ||
1901 !FTy->getParamType(0)->isPointerTy())
1904 setDoesNotCapture(F, 1);
1905 } else if (Name == "vfprintf" ||
1906 Name == "vsprintf") {
1907 if (FTy->getNumParams() != 3 ||
1908 !FTy->getParamType(0)->isPointerTy() ||
1909 !FTy->getParamType(1)->isPointerTy())
1912 setDoesNotCapture(F, 1);
1913 setDoesNotCapture(F, 2);
1914 } else if (Name == "vsnprintf") {
1915 if (FTy->getNumParams() != 4 ||
1916 !FTy->getParamType(0)->isPointerTy() ||
1917 !FTy->getParamType(2)->isPointerTy())
1920 setDoesNotCapture(F, 1);
1921 setDoesNotCapture(F, 3);
1925 if (Name == "open") {
1926 if (FTy->getNumParams() < 2 ||
1927 !FTy->getParamType(0)->isPointerTy())
1929 // May throw; "open" is a valid pthread cancellation point.
1930 setDoesNotCapture(F, 1);
1931 } else if (Name == "opendir") {
1932 if (FTy->getNumParams() != 1 ||
1933 !FTy->getReturnType()->isPointerTy() ||
1934 !FTy->getParamType(0)->isPointerTy())
1937 setDoesNotAlias(F, 0);
1938 setDoesNotCapture(F, 1);
1942 if (Name == "tmpfile") {
1943 if (!FTy->getReturnType()->isPointerTy())
1946 setDoesNotAlias(F, 0);
1947 } else if (Name == "times") {
1948 if (FTy->getNumParams() != 1 ||
1949 !FTy->getParamType(0)->isPointerTy())
1952 setDoesNotCapture(F, 1);
1956 if (Name == "htonl" ||
1959 setDoesNotAccessMemory(F);
1963 if (Name == "ntohl" ||
1966 setDoesNotAccessMemory(F);
1970 if (Name == "lstat") {
1971 if (FTy->getNumParams() != 2 ||
1972 !FTy->getParamType(0)->isPointerTy() ||
1973 !FTy->getParamType(1)->isPointerTy())
1976 setDoesNotCapture(F, 1);
1977 setDoesNotCapture(F, 2);
1978 } else if (Name == "lchown") {
1979 if (FTy->getNumParams() != 3 ||
1980 !FTy->getParamType(0)->isPointerTy())
1983 setDoesNotCapture(F, 1);
1987 if (Name == "qsort") {
1988 if (FTy->getNumParams() != 4 ||
1989 !FTy->getParamType(3)->isPointerTy())
1991 // May throw; places call through function pointer.
1992 setDoesNotCapture(F, 4);
1996 if (Name == "__strdup" ||
1997 Name == "__strndup") {
1998 if (FTy->getNumParams() < 1 ||
1999 !FTy->getReturnType()->isPointerTy() ||
2000 !FTy->getParamType(0)->isPointerTy())
2003 setDoesNotAlias(F, 0);
2004 setDoesNotCapture(F, 1);
2005 } else if (Name == "__strtok_r") {
2006 if (FTy->getNumParams() != 3 ||
2007 !FTy->getParamType(1)->isPointerTy())
2010 setDoesNotCapture(F, 2);
2011 } else if (Name == "_IO_getc") {
2012 if (FTy->getNumParams() != 1 ||
2013 !FTy->getParamType(0)->isPointerTy())
2016 setDoesNotCapture(F, 1);
2017 } else if (Name == "_IO_putc") {
2018 if (FTy->getNumParams() != 2 ||
2019 !FTy->getParamType(1)->isPointerTy())
2022 setDoesNotCapture(F, 2);
2026 if (Name == "\1__isoc99_scanf") {
2027 if (FTy->getNumParams() < 1 ||
2028 !FTy->getParamType(0)->isPointerTy())
2031 setDoesNotCapture(F, 1);
2032 } else if (Name == "\1stat64" ||
2033 Name == "\1lstat64" ||
2034 Name == "\1statvfs64" ||
2035 Name == "\1__isoc99_sscanf") {
2036 if (FTy->getNumParams() < 1 ||
2037 !FTy->getParamType(0)->isPointerTy() ||
2038 !FTy->getParamType(1)->isPointerTy())
2041 setDoesNotCapture(F, 1);
2042 setDoesNotCapture(F, 2);
2043 } else if (Name == "\1fopen64") {
2044 if (FTy->getNumParams() != 2 ||
2045 !FTy->getReturnType()->isPointerTy() ||
2046 !FTy->getParamType(0)->isPointerTy() ||
2047 !FTy->getParamType(1)->isPointerTy())
2050 setDoesNotAlias(F, 0);
2051 setDoesNotCapture(F, 1);
2052 setDoesNotCapture(F, 2);
2053 } else if (Name == "\1fseeko64" ||
2054 Name == "\1ftello64") {
2055 if (FTy->getNumParams() == 0 ||
2056 !FTy->getParamType(0)->isPointerTy())
2059 setDoesNotCapture(F, 1);
2060 } else if (Name == "\1tmpfile64") {
2061 if (!FTy->getReturnType()->isPointerTy())
2064 setDoesNotAlias(F, 0);
2065 } else if (Name == "\1fstat64" ||
2066 Name == "\1fstatvfs64") {
2067 if (FTy->getNumParams() != 2 ||
2068 !FTy->getParamType(1)->isPointerTy())
2071 setDoesNotCapture(F, 2);
2072 } else if (Name == "\1open64") {
2073 if (FTy->getNumParams() < 2 ||
2074 !FTy->getParamType(0)->isPointerTy())
2076 // May throw; "open" is a valid pthread cancellation point.
2077 setDoesNotCapture(F, 1);
2086 // Additional cases that we need to add to this file:
2089 // * cbrt(expN(X)) -> expN(x/3)
2090 // * cbrt(sqrt(x)) -> pow(x,1/6)
2091 // * cbrt(sqrt(x)) -> pow(x,1/9)
2094 // * cos(-x) -> cos(x)
2097 // * exp(log(x)) -> x
2100 // * log(exp(x)) -> x
2101 // * log(x**y) -> y*log(x)
2102 // * log(exp(y)) -> y*log(e)
2103 // * log(exp2(y)) -> y*log(2)
2104 // * log(exp10(y)) -> y*log(10)
2105 // * log(sqrt(x)) -> 0.5*log(x)
2106 // * log(pow(x,y)) -> y*log(x)
2108 // lround, lroundf, lroundl:
2109 // * lround(cnst) -> cnst'
2112 // * pow(exp(x),y) -> exp(x*y)
2113 // * pow(sqrt(x),y) -> pow(x,y*0.5)
2114 // * pow(pow(x,y),z)-> pow(x,y*z)
2117 // * puts("") -> putchar("\n")
2119 // round, roundf, roundl:
2120 // * round(cnst) -> cnst'
2123 // * signbit(cnst) -> cnst'
2124 // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2126 // sqrt, sqrtf, sqrtl:
2127 // * sqrt(expN(x)) -> expN(x*0.5)
2128 // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2129 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2132 // * stpcpy(str, "literal") ->
2133 // llvm.memcpy(str,"literal",strlen("literal")+1,1)
2135 // * strrchr(s,c) -> reverse_offset_of_in(c,s)
2136 // (if c is a constant integer and s is a constant string)
2137 // * strrchr(s1,0) -> strchr(s1,0)
2140 // * strpbrk(s,a) -> offset_in_for(s,a)
2141 // (if s and a are both constant strings)
2142 // * strpbrk(s,"") -> 0
2143 // * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2146 // * strspn(s,a) -> const_int (if both args are constant)
2147 // * strspn("",a) -> 0
2148 // * strspn(s,"") -> 0
2149 // * strcspn(s,a) -> const_int (if both args are constant)
2150 // * strcspn("",a) -> 0
2151 // * strcspn(s,"") -> strlen(a)
2154 // * tan(atan(x)) -> x
2156 // trunc, truncf, truncl:
2157 // * trunc(cnst) -> cnst'