instcombine: Migrate strspn optimizations
[oota-llvm.git] / lib / Transforms / Scalar / SimplifyLibCalls.cpp
1 //===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #define DEBUG_TYPE "simplify-libcalls"
19 #include "llvm/Transforms/Scalar.h"
20 #include "llvm/Transforms/Utils/BuildLibCalls.h"
21 #include "llvm/IRBuilder.h"
22 #include "llvm/Intrinsics.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/Module.h"
25 #include "llvm/Pass.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/DataLayout.h"
35 #include "llvm/Target/TargetLibraryInfo.h"
36 #include "llvm/Config/config.h"            // FIXME: Shouldn't depend on host!
37 using namespace llvm;
38
39 STATISTIC(NumSimplified, "Number of library calls simplified");
40 STATISTIC(NumAnnotated, "Number of attributes added to library functions");
41
42 static cl::opt<bool> UnsafeFPShrink("enable-double-float-shrink", cl::Hidden,
43                                    cl::init(false),
44                                    cl::desc("Enable unsafe double to float "
45                                             "shrinking for math lib calls"));
46 //===----------------------------------------------------------------------===//
47 // Optimizer Base Class
48 //===----------------------------------------------------------------------===//
49
50 /// This class is the abstract base class for the set of optimizations that
51 /// corresponds to one library call.
52 namespace {
53 class LibCallOptimization {
54 protected:
55   Function *Caller;
56   const DataLayout *TD;
57   const TargetLibraryInfo *TLI;
58   LLVMContext* Context;
59 public:
60   LibCallOptimization() { }
61   virtual ~LibCallOptimization() {}
62
63   /// CallOptimizer - This pure virtual method is implemented by base classes to
64   /// do various optimizations.  If this returns null then no transformation was
65   /// performed.  If it returns CI, then it transformed the call and CI is to be
66   /// deleted.  If it returns something else, replace CI with the new value and
67   /// delete CI.
68   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
69     =0;
70
71   Value *OptimizeCall(CallInst *CI, const DataLayout *TD,
72                       const TargetLibraryInfo *TLI, IRBuilder<> &B) {
73     Caller = CI->getParent()->getParent();
74     this->TD = TD;
75     this->TLI = TLI;
76     if (CI->getCalledFunction())
77       Context = &CI->getCalledFunction()->getContext();
78
79     // We never change the calling convention.
80     if (CI->getCallingConv() != llvm::CallingConv::C)
81       return NULL;
82
83     return CallOptimizer(CI->getCalledFunction(), CI, B);
84   }
85 };
86 } // End anonymous namespace.
87
88
89 //===----------------------------------------------------------------------===//
90 // Helper Functions
91 //===----------------------------------------------------------------------===//
92
93 static bool CallHasFloatingPointArgument(const CallInst *CI) {
94   for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
95        it != e; ++it) {
96     if ((*it)->getType()->isFloatingPointTy())
97       return true;
98   }
99   return false;
100 }
101
102 /// IsOnlyUsedInEqualityComparison - Return true if it is only used in equality
103 /// comparisons with With.
104 static bool IsOnlyUsedInEqualityComparison(Value *V, Value *With) {
105   for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
106        UI != E; ++UI) {
107     if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
108       if (IC->isEquality() && IC->getOperand(1) == With)
109         continue;
110     // Unknown instruction.
111     return false;
112   }
113   return true;
114 }
115
116 //===----------------------------------------------------------------------===//
117 // String and Memory LibCall Optimizations
118 //===----------------------------------------------------------------------===//
119
120 namespace {
121 //===---------------------------------------===//
122 // 'strcspn' Optimizations
123
124 struct StrCSpnOpt : public LibCallOptimization {
125   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
126     FunctionType *FT = Callee->getFunctionType();
127     if (FT->getNumParams() != 2 ||
128         FT->getParamType(0) != B.getInt8PtrTy() ||
129         FT->getParamType(1) != FT->getParamType(0) ||
130         !FT->getReturnType()->isIntegerTy())
131       return 0;
132
133     StringRef S1, S2;
134     bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
135     bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
136
137     // strcspn("", s) -> 0
138     if (HasS1 && S1.empty())
139       return Constant::getNullValue(CI->getType());
140
141     // Constant folding.
142     if (HasS1 && HasS2) {
143       size_t Pos = S1.find_first_of(S2);
144       if (Pos == StringRef::npos) Pos = S1.size();
145       return ConstantInt::get(CI->getType(), Pos);
146     }
147
148     // strcspn(s, "") -> strlen(s)
149     if (TD && HasS2 && S2.empty())
150       return EmitStrLen(CI->getArgOperand(0), B, TD, TLI);
151
152     return 0;
153   }
154 };
155
156 //===---------------------------------------===//
157 // 'strstr' Optimizations
158
159 struct StrStrOpt : public LibCallOptimization {
160   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
161     FunctionType *FT = Callee->getFunctionType();
162     if (FT->getNumParams() != 2 ||
163         !FT->getParamType(0)->isPointerTy() ||
164         !FT->getParamType(1)->isPointerTy() ||
165         !FT->getReturnType()->isPointerTy())
166       return 0;
167
168     // fold strstr(x, x) -> x.
169     if (CI->getArgOperand(0) == CI->getArgOperand(1))
170       return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
171
172     // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
173     if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
174       Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD, TLI);
175       if (!StrLen)
176         return 0;
177       Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
178                                    StrLen, B, TD, TLI);
179       if (!StrNCmp)
180         return 0;
181       for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
182            UI != UE; ) {
183         ICmpInst *Old = cast<ICmpInst>(*UI++);
184         Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
185                                   ConstantInt::getNullValue(StrNCmp->getType()),
186                                   "cmp");
187         Old->replaceAllUsesWith(Cmp);
188         Old->eraseFromParent();
189       }
190       return CI;
191     }
192
193     // See if either input string is a constant string.
194     StringRef SearchStr, ToFindStr;
195     bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
196     bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
197
198     // fold strstr(x, "") -> x.
199     if (HasStr2 && ToFindStr.empty())
200       return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
201
202     // If both strings are known, constant fold it.
203     if (HasStr1 && HasStr2) {
204       std::string::size_type Offset = SearchStr.find(ToFindStr);
205
206       if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
207         return Constant::getNullValue(CI->getType());
208
209       // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
210       Value *Result = CastToCStr(CI->getArgOperand(0), B);
211       Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
212       return B.CreateBitCast(Result, CI->getType());
213     }
214
215     // fold strstr(x, "y") -> strchr(x, 'y').
216     if (HasStr2 && ToFindStr.size() == 1) {
217       Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TD, TLI);
218       return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : 0;
219     }
220     return 0;
221   }
222 };
223
224
225 //===---------------------------------------===//
226 // 'memcmp' Optimizations
227
228 struct MemCmpOpt : public LibCallOptimization {
229   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
230     FunctionType *FT = Callee->getFunctionType();
231     if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
232         !FT->getParamType(1)->isPointerTy() ||
233         !FT->getReturnType()->isIntegerTy(32))
234       return 0;
235
236     Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
237
238     if (LHS == RHS)  // memcmp(s,s,x) -> 0
239       return Constant::getNullValue(CI->getType());
240
241     // Make sure we have a constant length.
242     ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
243     if (!LenC) return 0;
244     uint64_t Len = LenC->getZExtValue();
245
246     if (Len == 0) // memcmp(s1,s2,0) -> 0
247       return Constant::getNullValue(CI->getType());
248
249     // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
250     if (Len == 1) {
251       Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
252                                  CI->getType(), "lhsv");
253       Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
254                                  CI->getType(), "rhsv");
255       return B.CreateSub(LHSV, RHSV, "chardiff");
256     }
257
258     // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
259     StringRef LHSStr, RHSStr;
260     if (getConstantStringInfo(LHS, LHSStr) &&
261         getConstantStringInfo(RHS, RHSStr)) {
262       // Make sure we're not reading out-of-bounds memory.
263       if (Len > LHSStr.size() || Len > RHSStr.size())
264         return 0;
265       uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
266       return ConstantInt::get(CI->getType(), Ret);
267     }
268
269     return 0;
270   }
271 };
272
273 //===---------------------------------------===//
274 // 'memcpy' Optimizations
275
276 struct MemCpyOpt : public LibCallOptimization {
277   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
278     // These optimizations require DataLayout.
279     if (!TD) return 0;
280
281     FunctionType *FT = Callee->getFunctionType();
282     if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
283         !FT->getParamType(0)->isPointerTy() ||
284         !FT->getParamType(1)->isPointerTy() ||
285         FT->getParamType(2) != TD->getIntPtrType(*Context))
286       return 0;
287
288     // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
289     B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
290                    CI->getArgOperand(2), 1);
291     return CI->getArgOperand(0);
292   }
293 };
294
295 //===---------------------------------------===//
296 // 'memmove' Optimizations
297
298 struct MemMoveOpt : public LibCallOptimization {
299   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
300     // These optimizations require DataLayout.
301     if (!TD) return 0;
302
303     FunctionType *FT = Callee->getFunctionType();
304     if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
305         !FT->getParamType(0)->isPointerTy() ||
306         !FT->getParamType(1)->isPointerTy() ||
307         FT->getParamType(2) != TD->getIntPtrType(*Context))
308       return 0;
309
310     // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
311     B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
312                     CI->getArgOperand(2), 1);
313     return CI->getArgOperand(0);
314   }
315 };
316
317 //===---------------------------------------===//
318 // 'memset' Optimizations
319
320 struct MemSetOpt : public LibCallOptimization {
321   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
322     // These optimizations require DataLayout.
323     if (!TD) return 0;
324
325     FunctionType *FT = Callee->getFunctionType();
326     if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
327         !FT->getParamType(0)->isPointerTy() ||
328         !FT->getParamType(1)->isIntegerTy() ||
329         FT->getParamType(2) != TD->getIntPtrType(*Context))
330       return 0;
331
332     // memset(p, v, n) -> llvm.memset(p, v, n, 1)
333     Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
334     B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
335     return CI->getArgOperand(0);
336   }
337 };
338
339 //===----------------------------------------------------------------------===//
340 // Math Library Optimizations
341 //===----------------------------------------------------------------------===//
342
343 //===---------------------------------------===//
344 // Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
345
346 struct UnaryDoubleFPOpt : public LibCallOptimization {
347   bool CheckRetType;
348   UnaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {}
349   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
350     FunctionType *FT = Callee->getFunctionType();
351     if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
352         !FT->getParamType(0)->isDoubleTy())
353       return 0;
354
355     if (CheckRetType) {
356       // Check if all the uses for function like 'sin' are converted to float.
357       for (Value::use_iterator UseI = CI->use_begin(); UseI != CI->use_end();
358           ++UseI) {
359         FPTruncInst *Cast = dyn_cast<FPTruncInst>(*UseI);
360         if (Cast == 0 || !Cast->getType()->isFloatTy())
361           return 0;
362       }
363     }
364
365     // If this is something like 'floor((double)floatval)', convert to floorf.
366     FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
367     if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
368       return 0;
369
370     // floor((double)floatval) -> (double)floorf(floatval)
371     Value *V = Cast->getOperand(0);
372     V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
373     return B.CreateFPExt(V, B.getDoubleTy());
374   }
375 };
376
377 //===---------------------------------------===//
378 // 'cos*' Optimizations
379 struct CosOpt : public LibCallOptimization {
380   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
381     Value *Ret = NULL;
382     if (UnsafeFPShrink && Callee->getName() == "cos" &&
383         TLI->has(LibFunc::cosf)) {
384       UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
385       Ret = UnsafeUnaryDoubleFP.CallOptimizer(Callee, CI, B);
386     }
387
388     FunctionType *FT = Callee->getFunctionType();
389     // Just make sure this has 1 argument of FP type, which matches the
390     // result type.
391     if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
392         !FT->getParamType(0)->isFloatingPointTy())
393       return Ret;
394
395     // cos(-x) -> cos(x)
396     Value *Op1 = CI->getArgOperand(0);
397     if (BinaryOperator::isFNeg(Op1)) {
398       BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);
399       return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");
400     }
401     return Ret;
402   }
403 };
404
405 //===---------------------------------------===//
406 // 'pow*' Optimizations
407
408 struct PowOpt : public LibCallOptimization {
409   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
410     Value *Ret = NULL;
411     if (UnsafeFPShrink && Callee->getName() == "pow" &&
412         TLI->has(LibFunc::powf)) {
413       UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
414       Ret = UnsafeUnaryDoubleFP.CallOptimizer(Callee, CI, B);
415     }
416
417     FunctionType *FT = Callee->getFunctionType();
418     // Just make sure this has 2 arguments of the same FP type, which match the
419     // result type.
420     if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
421         FT->getParamType(0) != FT->getParamType(1) ||
422         !FT->getParamType(0)->isFloatingPointTy())
423       return Ret;
424
425     Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
426     if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
427       if (Op1C->isExactlyValue(1.0))  // pow(1.0, x) -> 1.0
428         return Op1C;
429       if (Op1C->isExactlyValue(2.0))  // pow(2.0, x) -> exp2(x)
430         return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
431     }
432
433     ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
434     if (Op2C == 0) return Ret;
435
436     if (Op2C->getValueAPF().isZero())  // pow(x, 0.0) -> 1.0
437       return ConstantFP::get(CI->getType(), 1.0);
438
439     if (Op2C->isExactlyValue(0.5)) {
440       // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
441       // This is faster than calling pow, and still handles negative zero
442       // and negative infinity correctly.
443       // TODO: In fast-math mode, this could be just sqrt(x).
444       // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
445       Value *Inf = ConstantFP::getInfinity(CI->getType());
446       Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
447       Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
448                                          Callee->getAttributes());
449       Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
450                                          Callee->getAttributes());
451       Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
452       Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
453       return Sel;
454     }
455
456     if (Op2C->isExactlyValue(1.0))  // pow(x, 1.0) -> x
457       return Op1;
458     if (Op2C->isExactlyValue(2.0))  // pow(x, 2.0) -> x*x
459       return B.CreateFMul(Op1, Op1, "pow2");
460     if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
461       return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
462                           Op1, "powrecip");
463     return 0;
464   }
465 };
466
467 //===---------------------------------------===//
468 // 'exp2' Optimizations
469
470 struct Exp2Opt : public LibCallOptimization {
471   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
472     Value *Ret = NULL;
473     if (UnsafeFPShrink && Callee->getName() == "exp2" &&
474         TLI->has(LibFunc::exp2)) {
475       UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
476       Ret = UnsafeUnaryDoubleFP.CallOptimizer(Callee, CI, B);
477     }
478
479     FunctionType *FT = Callee->getFunctionType();
480     // Just make sure this has 1 argument of FP type, which matches the
481     // result type.
482     if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
483         !FT->getParamType(0)->isFloatingPointTy())
484       return Ret;
485
486     Value *Op = CI->getArgOperand(0);
487     // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x))  if sizeof(x) <= 32
488     // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x))  if sizeof(x) < 32
489     Value *LdExpArg = 0;
490     if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
491       if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
492         LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
493     } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
494       if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
495         LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
496     }
497
498     if (LdExpArg) {
499       const char *Name;
500       if (Op->getType()->isFloatTy())
501         Name = "ldexpf";
502       else if (Op->getType()->isDoubleTy())
503         Name = "ldexp";
504       else
505         Name = "ldexpl";
506
507       Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
508       if (!Op->getType()->isFloatTy())
509         One = ConstantExpr::getFPExtend(One, Op->getType());
510
511       Module *M = Caller->getParent();
512       Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
513                                              Op->getType(),
514                                              B.getInt32Ty(), NULL);
515       CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
516       if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
517         CI->setCallingConv(F->getCallingConv());
518
519       return CI;
520     }
521     return Ret;
522   }
523 };
524
525 //===----------------------------------------------------------------------===//
526 // Integer Optimizations
527 //===----------------------------------------------------------------------===//
528
529 //===---------------------------------------===//
530 // 'ffs*' Optimizations
531
532 struct FFSOpt : public LibCallOptimization {
533   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
534     FunctionType *FT = Callee->getFunctionType();
535     // Just make sure this has 2 arguments of the same FP type, which match the
536     // result type.
537     if (FT->getNumParams() != 1 ||
538         !FT->getReturnType()->isIntegerTy(32) ||
539         !FT->getParamType(0)->isIntegerTy())
540       return 0;
541
542     Value *Op = CI->getArgOperand(0);
543
544     // Constant fold.
545     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
546       if (CI->isZero()) // ffs(0) -> 0.
547         return B.getInt32(0);
548       // ffs(c) -> cttz(c)+1
549       return B.getInt32(CI->getValue().countTrailingZeros() + 1);
550     }
551
552     // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
553     Type *ArgType = Op->getType();
554     Value *F = Intrinsic::getDeclaration(Callee->getParent(),
555                                          Intrinsic::cttz, ArgType);
556     Value *V = B.CreateCall2(F, Op, B.getFalse(), "cttz");
557     V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
558     V = B.CreateIntCast(V, B.getInt32Ty(), false);
559
560     Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
561     return B.CreateSelect(Cond, V, B.getInt32(0));
562   }
563 };
564
565 //===---------------------------------------===//
566 // 'isdigit' Optimizations
567
568 struct IsDigitOpt : public LibCallOptimization {
569   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
570     FunctionType *FT = Callee->getFunctionType();
571     // We require integer(i32)
572     if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
573         !FT->getParamType(0)->isIntegerTy(32))
574       return 0;
575
576     // isdigit(c) -> (c-'0') <u 10
577     Value *Op = CI->getArgOperand(0);
578     Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
579     Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
580     return B.CreateZExt(Op, CI->getType());
581   }
582 };
583
584 //===---------------------------------------===//
585 // 'isascii' Optimizations
586
587 struct IsAsciiOpt : public LibCallOptimization {
588   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
589     FunctionType *FT = Callee->getFunctionType();
590     // We require integer(i32)
591     if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
592         !FT->getParamType(0)->isIntegerTy(32))
593       return 0;
594
595     // isascii(c) -> c <u 128
596     Value *Op = CI->getArgOperand(0);
597     Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
598     return B.CreateZExt(Op, CI->getType());
599   }
600 };
601
602 //===---------------------------------------===//
603 // 'abs', 'labs', 'llabs' Optimizations
604
605 struct AbsOpt : public LibCallOptimization {
606   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
607     FunctionType *FT = Callee->getFunctionType();
608     // We require integer(integer) where the types agree.
609     if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
610         FT->getParamType(0) != FT->getReturnType())
611       return 0;
612
613     // abs(x) -> x >s -1 ? x : -x
614     Value *Op = CI->getArgOperand(0);
615     Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),
616                                  "ispos");
617     Value *Neg = B.CreateNeg(Op, "neg");
618     return B.CreateSelect(Pos, Op, Neg);
619   }
620 };
621
622
623 //===---------------------------------------===//
624 // 'toascii' Optimizations
625
626 struct ToAsciiOpt : public LibCallOptimization {
627   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
628     FunctionType *FT = Callee->getFunctionType();
629     // We require i32(i32)
630     if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
631         !FT->getParamType(0)->isIntegerTy(32))
632       return 0;
633
634     // isascii(c) -> c & 0x7f
635     return B.CreateAnd(CI->getArgOperand(0),
636                        ConstantInt::get(CI->getType(),0x7F));
637   }
638 };
639
640 //===----------------------------------------------------------------------===//
641 // Formatting and IO Optimizations
642 //===----------------------------------------------------------------------===//
643
644 //===---------------------------------------===//
645 // 'printf' Optimizations
646
647 struct PrintFOpt : public LibCallOptimization {
648   Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
649                                    IRBuilder<> &B) {
650     // Check for a fixed format string.
651     StringRef FormatStr;
652     if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
653       return 0;
654
655     // Empty format string -> noop.
656     if (FormatStr.empty())  // Tolerate printf's declared void.
657       return CI->use_empty() ? (Value*)CI :
658                                ConstantInt::get(CI->getType(), 0);
659
660     // Do not do any of the following transformations if the printf return value
661     // is used, in general the printf return value is not compatible with either
662     // putchar() or puts().
663     if (!CI->use_empty())
664       return 0;
665
666     // printf("x") -> putchar('x'), even for '%'.
667     if (FormatStr.size() == 1) {
668       Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI);
669       if (CI->use_empty() || !Res) return Res;
670       return B.CreateIntCast(Res, CI->getType(), true);
671     }
672
673     // printf("foo\n") --> puts("foo")
674     if (FormatStr[FormatStr.size()-1] == '\n' &&
675         FormatStr.find('%') == std::string::npos) {  // no format characters.
676       // Create a string literal with no \n on it.  We expect the constant merge
677       // pass to be run after this pass, to merge duplicate strings.
678       FormatStr = FormatStr.drop_back();
679       Value *GV = B.CreateGlobalString(FormatStr, "str");
680       Value *NewCI = EmitPutS(GV, B, TD, TLI);
681       return (CI->use_empty() || !NewCI) ?
682               NewCI :
683               ConstantInt::get(CI->getType(), FormatStr.size()+1);
684     }
685
686     // Optimize specific format strings.
687     // printf("%c", chr) --> putchar(chr)
688     if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
689         CI->getArgOperand(1)->getType()->isIntegerTy()) {
690       Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI);
691
692       if (CI->use_empty() || !Res) return Res;
693       return B.CreateIntCast(Res, CI->getType(), true);
694     }
695
696     // printf("%s\n", str) --> puts(str)
697     if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
698         CI->getArgOperand(1)->getType()->isPointerTy()) {
699       return EmitPutS(CI->getArgOperand(1), B, TD, TLI);
700     }
701     return 0;
702   }
703
704   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
705     // Require one fixed pointer argument and an integer/void result.
706     FunctionType *FT = Callee->getFunctionType();
707     if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
708         !(FT->getReturnType()->isIntegerTy() ||
709           FT->getReturnType()->isVoidTy()))
710       return 0;
711
712     if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
713       return V;
714     }
715
716     // printf(format, ...) -> iprintf(format, ...) if no floating point
717     // arguments.
718     if (TLI->has(LibFunc::iprintf) && !CallHasFloatingPointArgument(CI)) {
719       Module *M = B.GetInsertBlock()->getParent()->getParent();
720       Constant *IPrintFFn =
721         M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
722       CallInst *New = cast<CallInst>(CI->clone());
723       New->setCalledFunction(IPrintFFn);
724       B.Insert(New);
725       return New;
726     }
727     return 0;
728   }
729 };
730
731 //===---------------------------------------===//
732 // 'sprintf' Optimizations
733
734 struct SPrintFOpt : public LibCallOptimization {
735   Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
736                                    IRBuilder<> &B) {
737     // Check for a fixed format string.
738     StringRef FormatStr;
739     if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
740       return 0;
741
742     // If we just have a format string (nothing else crazy) transform it.
743     if (CI->getNumArgOperands() == 2) {
744       // Make sure there's no % in the constant array.  We could try to handle
745       // %% -> % in the future if we cared.
746       for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
747         if (FormatStr[i] == '%')
748           return 0; // we found a format specifier, bail out.
749
750       // These optimizations require DataLayout.
751       if (!TD) return 0;
752
753       // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
754       B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
755                      ConstantInt::get(TD->getIntPtrType(*Context), // Copy the
756                                       FormatStr.size() + 1), 1);   // nul byte.
757       return ConstantInt::get(CI->getType(), FormatStr.size());
758     }
759
760     // The remaining optimizations require the format string to be "%s" or "%c"
761     // and have an extra operand.
762     if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
763         CI->getNumArgOperands() < 3)
764       return 0;
765
766     // Decode the second character of the format string.
767     if (FormatStr[1] == 'c') {
768       // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
769       if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
770       Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
771       Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
772       B.CreateStore(V, Ptr);
773       Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
774       B.CreateStore(B.getInt8(0), Ptr);
775
776       return ConstantInt::get(CI->getType(), 1);
777     }
778
779     if (FormatStr[1] == 's') {
780       // These optimizations require DataLayout.
781       if (!TD) return 0;
782
783       // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
784       if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
785
786       Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD, TLI);
787       if (!Len)
788         return 0;
789       Value *IncLen = B.CreateAdd(Len,
790                                   ConstantInt::get(Len->getType(), 1),
791                                   "leninc");
792       B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
793
794       // The sprintf result is the unincremented number of bytes in the string.
795       return B.CreateIntCast(Len, CI->getType(), false);
796     }
797     return 0;
798   }
799
800   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
801     // Require two fixed pointer arguments and an integer result.
802     FunctionType *FT = Callee->getFunctionType();
803     if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
804         !FT->getParamType(1)->isPointerTy() ||
805         !FT->getReturnType()->isIntegerTy())
806       return 0;
807
808     if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
809       return V;
810     }
811
812     // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
813     // point arguments.
814     if (TLI->has(LibFunc::siprintf) && !CallHasFloatingPointArgument(CI)) {
815       Module *M = B.GetInsertBlock()->getParent()->getParent();
816       Constant *SIPrintFFn =
817         M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
818       CallInst *New = cast<CallInst>(CI->clone());
819       New->setCalledFunction(SIPrintFFn);
820       B.Insert(New);
821       return New;
822     }
823     return 0;
824   }
825 };
826
827 //===---------------------------------------===//
828 // 'fwrite' Optimizations
829
830 struct FWriteOpt : public LibCallOptimization {
831   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
832     // Require a pointer, an integer, an integer, a pointer, returning integer.
833     FunctionType *FT = Callee->getFunctionType();
834     if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
835         !FT->getParamType(1)->isIntegerTy() ||
836         !FT->getParamType(2)->isIntegerTy() ||
837         !FT->getParamType(3)->isPointerTy() ||
838         !FT->getReturnType()->isIntegerTy())
839       return 0;
840
841     // Get the element size and count.
842     ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
843     ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
844     if (!SizeC || !CountC) return 0;
845     uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
846
847     // If this is writing zero records, remove the call (it's a noop).
848     if (Bytes == 0)
849       return ConstantInt::get(CI->getType(), 0);
850
851     // If this is writing one byte, turn it into fputc.
852     // This optimisation is only valid, if the return value is unused.
853     if (Bytes == 1 && CI->use_empty()) {  // fwrite(S,1,1,F) -> fputc(S[0],F)
854       Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
855       Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, TD, TLI);
856       return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
857     }
858
859     return 0;
860   }
861 };
862
863 //===---------------------------------------===//
864 // 'fputs' Optimizations
865
866 struct FPutsOpt : public LibCallOptimization {
867   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
868     // These optimizations require DataLayout.
869     if (!TD) return 0;
870
871     // Require two pointers.  Also, we can't optimize if return value is used.
872     FunctionType *FT = Callee->getFunctionType();
873     if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
874         !FT->getParamType(1)->isPointerTy() ||
875         !CI->use_empty())
876       return 0;
877
878     // fputs(s,F) --> fwrite(s,1,strlen(s),F)
879     uint64_t Len = GetStringLength(CI->getArgOperand(0));
880     if (!Len) return 0;
881     // Known to have no uses (see above).
882     return EmitFWrite(CI->getArgOperand(0),
883                       ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
884                       CI->getArgOperand(1), B, TD, TLI);
885   }
886 };
887
888 //===---------------------------------------===//
889 // 'fprintf' Optimizations
890
891 struct FPrintFOpt : public LibCallOptimization {
892   Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
893                                    IRBuilder<> &B) {
894     // All the optimizations depend on the format string.
895     StringRef FormatStr;
896     if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
897       return 0;
898
899     // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
900     if (CI->getNumArgOperands() == 2) {
901       for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
902         if (FormatStr[i] == '%')  // Could handle %% -> % if we cared.
903           return 0; // We found a format specifier.
904
905       // These optimizations require DataLayout.
906       if (!TD) return 0;
907
908       Value *NewCI = EmitFWrite(CI->getArgOperand(1),
909                                 ConstantInt::get(TD->getIntPtrType(*Context),
910                                                  FormatStr.size()),
911                                 CI->getArgOperand(0), B, TD, TLI);
912       return NewCI ? ConstantInt::get(CI->getType(), FormatStr.size()) : 0;
913     }
914
915     // The remaining optimizations require the format string to be "%s" or "%c"
916     // and have an extra operand.
917     if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
918         CI->getNumArgOperands() < 3)
919       return 0;
920
921     // Decode the second character of the format string.
922     if (FormatStr[1] == 'c') {
923       // fprintf(F, "%c", chr) --> fputc(chr, F)
924       if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
925       Value *NewCI = EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B,
926                                TD, TLI);
927       return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
928     }
929
930     if (FormatStr[1] == 's') {
931       // fprintf(F, "%s", str) --> fputs(str, F)
932       if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
933         return 0;
934       return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD, TLI);
935     }
936     return 0;
937   }
938
939   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
940     // Require two fixed paramters as pointers and integer result.
941     FunctionType *FT = Callee->getFunctionType();
942     if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
943         !FT->getParamType(1)->isPointerTy() ||
944         !FT->getReturnType()->isIntegerTy())
945       return 0;
946
947     if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
948       return V;
949     }
950
951     // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
952     // floating point arguments.
953     if (TLI->has(LibFunc::fiprintf) && !CallHasFloatingPointArgument(CI)) {
954       Module *M = B.GetInsertBlock()->getParent()->getParent();
955       Constant *FIPrintFFn =
956         M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
957       CallInst *New = cast<CallInst>(CI->clone());
958       New->setCalledFunction(FIPrintFFn);
959       B.Insert(New);
960       return New;
961     }
962     return 0;
963   }
964 };
965
966 //===---------------------------------------===//
967 // 'puts' Optimizations
968
969 struct PutsOpt : public LibCallOptimization {
970   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
971     // Require one fixed pointer argument and an integer/void result.
972     FunctionType *FT = Callee->getFunctionType();
973     if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
974         !(FT->getReturnType()->isIntegerTy() ||
975           FT->getReturnType()->isVoidTy()))
976       return 0;
977
978     // Check for a constant string.
979     StringRef Str;
980     if (!getConstantStringInfo(CI->getArgOperand(0), Str))
981       return 0;
982
983     if (Str.empty() && CI->use_empty()) {
984       // puts("") -> putchar('\n')
985       Value *Res = EmitPutChar(B.getInt32('\n'), B, TD, TLI);
986       if (CI->use_empty() || !Res) return Res;
987       return B.CreateIntCast(Res, CI->getType(), true);
988     }
989
990     return 0;
991   }
992 };
993
994 } // end anonymous namespace.
995
996 //===----------------------------------------------------------------------===//
997 // SimplifyLibCalls Pass Implementation
998 //===----------------------------------------------------------------------===//
999
1000 namespace {
1001   /// This pass optimizes well known library functions from libc and libm.
1002   ///
1003   class SimplifyLibCalls : public FunctionPass {
1004     TargetLibraryInfo *TLI;
1005
1006     StringMap<LibCallOptimization*> Optimizations;
1007     // String and Memory LibCall Optimizations
1008     StrCSpnOpt StrCSpn; StrStrOpt StrStr;
1009     MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
1010     // Math Library Optimizations
1011     CosOpt Cos; PowOpt Pow; Exp2Opt Exp2;
1012     UnaryDoubleFPOpt UnaryDoubleFP, UnsafeUnaryDoubleFP;
1013     // Integer Optimizations
1014     FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1015     ToAsciiOpt ToAscii;
1016     // Formatting and IO Optimizations
1017     SPrintFOpt SPrintF; PrintFOpt PrintF;
1018     FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
1019     PutsOpt Puts;
1020
1021     bool Modified;  // This is only used by doInitialization.
1022   public:
1023     static char ID; // Pass identification
1024     SimplifyLibCalls() : FunctionPass(ID), UnaryDoubleFP(false),
1025                          UnsafeUnaryDoubleFP(true) {
1026       initializeSimplifyLibCallsPass(*PassRegistry::getPassRegistry());
1027     }
1028     void AddOpt(LibFunc::Func F, LibCallOptimization* Opt);
1029     void AddOpt(LibFunc::Func F1, LibFunc::Func F2, LibCallOptimization* Opt);
1030
1031     void InitOptimizations();
1032     bool runOnFunction(Function &F);
1033
1034     void setDoesNotAccessMemory(Function &F);
1035     void setOnlyReadsMemory(Function &F);
1036     void setDoesNotThrow(Function &F);
1037     void setDoesNotCapture(Function &F, unsigned n);
1038     void setDoesNotAlias(Function &F, unsigned n);
1039     bool doInitialization(Module &M);
1040
1041     void inferPrototypeAttributes(Function &F);
1042     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1043       AU.addRequired<TargetLibraryInfo>();
1044     }
1045   };
1046 } // end anonymous namespace.
1047
1048 char SimplifyLibCalls::ID = 0;
1049
1050 INITIALIZE_PASS_BEGIN(SimplifyLibCalls, "simplify-libcalls",
1051                       "Simplify well-known library calls", false, false)
1052 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
1053 INITIALIZE_PASS_END(SimplifyLibCalls, "simplify-libcalls",
1054                     "Simplify well-known library calls", false, false)
1055
1056 // Public interface to the Simplify LibCalls pass.
1057 FunctionPass *llvm::createSimplifyLibCallsPass() {
1058   return new SimplifyLibCalls();
1059 }
1060
1061 void SimplifyLibCalls::AddOpt(LibFunc::Func F, LibCallOptimization* Opt) {
1062   if (TLI->has(F))
1063     Optimizations[TLI->getName(F)] = Opt;
1064 }
1065
1066 void SimplifyLibCalls::AddOpt(LibFunc::Func F1, LibFunc::Func F2,
1067                               LibCallOptimization* Opt) {
1068   if (TLI->has(F1) && TLI->has(F2))
1069     Optimizations[TLI->getName(F1)] = Opt;
1070 }
1071
1072 /// Optimizations - Populate the Optimizations map with all the optimizations
1073 /// we know.
1074 void SimplifyLibCalls::InitOptimizations() {
1075   // String and Memory LibCall Optimizations
1076   Optimizations["strcspn"] = &StrCSpn;
1077   Optimizations["strstr"] = &StrStr;
1078   Optimizations["memcmp"] = &MemCmp;
1079   AddOpt(LibFunc::memcpy, &MemCpy);
1080   Optimizations["memmove"] = &MemMove;
1081   AddOpt(LibFunc::memset, &MemSet);
1082
1083   // Math Library Optimizations
1084   Optimizations["cosf"] = &Cos;
1085   Optimizations["cos"] = &Cos;
1086   Optimizations["cosl"] = &Cos;
1087   Optimizations["powf"] = &Pow;
1088   Optimizations["pow"] = &Pow;
1089   Optimizations["powl"] = &Pow;
1090   Optimizations["llvm.pow.f32"] = &Pow;
1091   Optimizations["llvm.pow.f64"] = &Pow;
1092   Optimizations["llvm.pow.f80"] = &Pow;
1093   Optimizations["llvm.pow.f128"] = &Pow;
1094   Optimizations["llvm.pow.ppcf128"] = &Pow;
1095   Optimizations["exp2l"] = &Exp2;
1096   Optimizations["exp2"] = &Exp2;
1097   Optimizations["exp2f"] = &Exp2;
1098   Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1099   Optimizations["llvm.exp2.f128"] = &Exp2;
1100   Optimizations["llvm.exp2.f80"] = &Exp2;
1101   Optimizations["llvm.exp2.f64"] = &Exp2;
1102   Optimizations["llvm.exp2.f32"] = &Exp2;
1103
1104   AddOpt(LibFunc::ceil, LibFunc::ceilf, &UnaryDoubleFP);
1105   AddOpt(LibFunc::fabs, LibFunc::fabsf, &UnaryDoubleFP);
1106   AddOpt(LibFunc::floor, LibFunc::floorf, &UnaryDoubleFP);
1107   AddOpt(LibFunc::rint, LibFunc::rintf, &UnaryDoubleFP);
1108   AddOpt(LibFunc::round, LibFunc::roundf, &UnaryDoubleFP);
1109   AddOpt(LibFunc::nearbyint, LibFunc::nearbyintf, &UnaryDoubleFP);
1110   AddOpt(LibFunc::trunc, LibFunc::truncf, &UnaryDoubleFP);
1111
1112   if(UnsafeFPShrink) {
1113     AddOpt(LibFunc::acos, LibFunc::acosf, &UnsafeUnaryDoubleFP);
1114     AddOpt(LibFunc::acosh, LibFunc::acoshf, &UnsafeUnaryDoubleFP);
1115     AddOpt(LibFunc::asin, LibFunc::asinf, &UnsafeUnaryDoubleFP);
1116     AddOpt(LibFunc::asinh, LibFunc::asinhf, &UnsafeUnaryDoubleFP);
1117     AddOpt(LibFunc::atan, LibFunc::atanf, &UnsafeUnaryDoubleFP);
1118     AddOpt(LibFunc::atanh, LibFunc::atanhf, &UnsafeUnaryDoubleFP);
1119     AddOpt(LibFunc::cbrt, LibFunc::cbrtf, &UnsafeUnaryDoubleFP);
1120     AddOpt(LibFunc::cosh, LibFunc::coshf, &UnsafeUnaryDoubleFP);
1121     AddOpt(LibFunc::exp, LibFunc::expf, &UnsafeUnaryDoubleFP);
1122     AddOpt(LibFunc::exp10, LibFunc::exp10f, &UnsafeUnaryDoubleFP);
1123     AddOpt(LibFunc::expm1, LibFunc::expm1f, &UnsafeUnaryDoubleFP);
1124     AddOpt(LibFunc::log, LibFunc::logf, &UnsafeUnaryDoubleFP);
1125     AddOpt(LibFunc::log10, LibFunc::log10f, &UnsafeUnaryDoubleFP);
1126     AddOpt(LibFunc::log1p, LibFunc::log1pf, &UnsafeUnaryDoubleFP);
1127     AddOpt(LibFunc::log2, LibFunc::log2f, &UnsafeUnaryDoubleFP);
1128     AddOpt(LibFunc::logb, LibFunc::logbf, &UnsafeUnaryDoubleFP);
1129     AddOpt(LibFunc::sin, LibFunc::sinf, &UnsafeUnaryDoubleFP);
1130     AddOpt(LibFunc::sinh, LibFunc::sinhf, &UnsafeUnaryDoubleFP);
1131     AddOpt(LibFunc::sqrt, LibFunc::sqrtf, &UnsafeUnaryDoubleFP);
1132     AddOpt(LibFunc::tan, LibFunc::tanf, &UnsafeUnaryDoubleFP);
1133     AddOpt(LibFunc::tanh, LibFunc::tanhf, &UnsafeUnaryDoubleFP);
1134   }
1135
1136   // Integer Optimizations
1137   Optimizations["ffs"] = &FFS;
1138   Optimizations["ffsl"] = &FFS;
1139   Optimizations["ffsll"] = &FFS;
1140   Optimizations["abs"] = &Abs;
1141   Optimizations["labs"] = &Abs;
1142   Optimizations["llabs"] = &Abs;
1143   Optimizations["isdigit"] = &IsDigit;
1144   Optimizations["isascii"] = &IsAscii;
1145   Optimizations["toascii"] = &ToAscii;
1146
1147   // Formatting and IO Optimizations
1148   Optimizations["sprintf"] = &SPrintF;
1149   Optimizations["printf"] = &PrintF;
1150   AddOpt(LibFunc::fwrite, &FWrite);
1151   AddOpt(LibFunc::fputs, &FPuts);
1152   Optimizations["fprintf"] = &FPrintF;
1153   Optimizations["puts"] = &Puts;
1154 }
1155
1156
1157 /// runOnFunction - Top level algorithm.
1158 ///
1159 bool SimplifyLibCalls::runOnFunction(Function &F) {
1160   TLI = &getAnalysis<TargetLibraryInfo>();
1161
1162   if (Optimizations.empty())
1163     InitOptimizations();
1164
1165   const DataLayout *TD = getAnalysisIfAvailable<DataLayout>();
1166
1167   IRBuilder<> Builder(F.getContext());
1168
1169   bool Changed = false;
1170   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1171     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1172       // Ignore non-calls.
1173       CallInst *CI = dyn_cast<CallInst>(I++);
1174       if (!CI) continue;
1175
1176       // Ignore indirect calls and calls to non-external functions.
1177       Function *Callee = CI->getCalledFunction();
1178       if (Callee == 0 || !Callee->isDeclaration() ||
1179           !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1180         continue;
1181
1182       // Ignore unknown calls.
1183       LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1184       if (!LCO) continue;
1185
1186       // Set the builder to the instruction after the call.
1187       Builder.SetInsertPoint(BB, I);
1188
1189       // Use debug location of CI for all new instructions.
1190       Builder.SetCurrentDebugLocation(CI->getDebugLoc());
1191
1192       // Try to optimize this call.
1193       Value *Result = LCO->OptimizeCall(CI, TD, TLI, Builder);
1194       if (Result == 0) continue;
1195
1196       DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1197             dbgs() << "  into: " << *Result << "\n");
1198
1199       // Something changed!
1200       Changed = true;
1201       ++NumSimplified;
1202
1203       // Inspect the instruction after the call (which was potentially just
1204       // added) next.
1205       I = CI; ++I;
1206
1207       if (CI != Result && !CI->use_empty()) {
1208         CI->replaceAllUsesWith(Result);
1209         if (!Result->hasName())
1210           Result->takeName(CI);
1211       }
1212       CI->eraseFromParent();
1213     }
1214   }
1215   return Changed;
1216 }
1217
1218 // Utility methods for doInitialization.
1219
1220 void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1221   if (!F.doesNotAccessMemory()) {
1222     F.setDoesNotAccessMemory();
1223     ++NumAnnotated;
1224     Modified = true;
1225   }
1226 }
1227 void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1228   if (!F.onlyReadsMemory()) {
1229     F.setOnlyReadsMemory();
1230     ++NumAnnotated;
1231     Modified = true;
1232   }
1233 }
1234 void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1235   if (!F.doesNotThrow()) {
1236     F.setDoesNotThrow();
1237     ++NumAnnotated;
1238     Modified = true;
1239   }
1240 }
1241 void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1242   if (!F.doesNotCapture(n)) {
1243     F.setDoesNotCapture(n);
1244     ++NumAnnotated;
1245     Modified = true;
1246   }
1247 }
1248 void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1249   if (!F.doesNotAlias(n)) {
1250     F.setDoesNotAlias(n);
1251     ++NumAnnotated;
1252     Modified = true;
1253   }
1254 }
1255
1256
1257 void SimplifyLibCalls::inferPrototypeAttributes(Function &F) {
1258   FunctionType *FTy = F.getFunctionType();
1259
1260   StringRef Name = F.getName();
1261   switch (Name[0]) {
1262   case 's':
1263     if (Name == "strlen") {
1264       if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1265         return;
1266       setOnlyReadsMemory(F);
1267       setDoesNotThrow(F);
1268       setDoesNotCapture(F, 1);
1269     } else if (Name == "strchr" ||
1270                Name == "strrchr") {
1271       if (FTy->getNumParams() != 2 ||
1272           !FTy->getParamType(0)->isPointerTy() ||
1273           !FTy->getParamType(1)->isIntegerTy())
1274         return;
1275       setOnlyReadsMemory(F);
1276       setDoesNotThrow(F);
1277     } else if (Name == "strcpy" ||
1278                Name == "stpcpy" ||
1279                Name == "strcat" ||
1280                Name == "strtol" ||
1281                Name == "strtod" ||
1282                Name == "strtof" ||
1283                Name == "strtoul" ||
1284                Name == "strtoll" ||
1285                Name == "strtold" ||
1286                Name == "strncat" ||
1287                Name == "strncpy" ||
1288                Name == "stpncpy" ||
1289                Name == "strtoull") {
1290       if (FTy->getNumParams() < 2 ||
1291           !FTy->getParamType(1)->isPointerTy())
1292         return;
1293       setDoesNotThrow(F);
1294       setDoesNotCapture(F, 2);
1295     } else if (Name == "strxfrm") {
1296       if (FTy->getNumParams() != 3 ||
1297           !FTy->getParamType(0)->isPointerTy() ||
1298           !FTy->getParamType(1)->isPointerTy())
1299         return;
1300       setDoesNotThrow(F);
1301       setDoesNotCapture(F, 1);
1302       setDoesNotCapture(F, 2);
1303     } else if (Name == "strcmp" ||
1304                Name == "strspn" ||
1305                Name == "strncmp" ||
1306                Name == "strcspn" ||
1307                Name == "strcoll" ||
1308                Name == "strcasecmp" ||
1309                Name == "strncasecmp") {
1310       if (FTy->getNumParams() < 2 ||
1311           !FTy->getParamType(0)->isPointerTy() ||
1312           !FTy->getParamType(1)->isPointerTy())
1313         return;
1314       setOnlyReadsMemory(F);
1315       setDoesNotThrow(F);
1316       setDoesNotCapture(F, 1);
1317       setDoesNotCapture(F, 2);
1318     } else if (Name == "strstr" ||
1319                Name == "strpbrk") {
1320       if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1321         return;
1322       setOnlyReadsMemory(F);
1323       setDoesNotThrow(F);
1324       setDoesNotCapture(F, 2);
1325     } else if (Name == "strtok" ||
1326                Name == "strtok_r") {
1327       if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1328         return;
1329       setDoesNotThrow(F);
1330       setDoesNotCapture(F, 2);
1331     } else if (Name == "scanf" ||
1332                Name == "setbuf" ||
1333                Name == "setvbuf") {
1334       if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1335         return;
1336       setDoesNotThrow(F);
1337       setDoesNotCapture(F, 1);
1338     } else if (Name == "strdup" ||
1339                Name == "strndup") {
1340       if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1341           !FTy->getParamType(0)->isPointerTy())
1342         return;
1343       setDoesNotThrow(F);
1344       setDoesNotAlias(F, 0);
1345       setDoesNotCapture(F, 1);
1346     } else if (Name == "stat" ||
1347                Name == "sscanf" ||
1348                Name == "sprintf" ||
1349                Name == "statvfs") {
1350       if (FTy->getNumParams() < 2 ||
1351           !FTy->getParamType(0)->isPointerTy() ||
1352           !FTy->getParamType(1)->isPointerTy())
1353         return;
1354       setDoesNotThrow(F);
1355       setDoesNotCapture(F, 1);
1356       setDoesNotCapture(F, 2);
1357     } else if (Name == "snprintf") {
1358       if (FTy->getNumParams() != 3 ||
1359           !FTy->getParamType(0)->isPointerTy() ||
1360           !FTy->getParamType(2)->isPointerTy())
1361         return;
1362       setDoesNotThrow(F);
1363       setDoesNotCapture(F, 1);
1364       setDoesNotCapture(F, 3);
1365     } else if (Name == "setitimer") {
1366       if (FTy->getNumParams() != 3 ||
1367           !FTy->getParamType(1)->isPointerTy() ||
1368           !FTy->getParamType(2)->isPointerTy())
1369         return;
1370       setDoesNotThrow(F);
1371       setDoesNotCapture(F, 2);
1372       setDoesNotCapture(F, 3);
1373     } else if (Name == "system") {
1374       if (FTy->getNumParams() != 1 ||
1375           !FTy->getParamType(0)->isPointerTy())
1376         return;
1377       // May throw; "system" is a valid pthread cancellation point.
1378       setDoesNotCapture(F, 1);
1379     }
1380     break;
1381   case 'm':
1382     if (Name == "malloc") {
1383       if (FTy->getNumParams() != 1 ||
1384           !FTy->getReturnType()->isPointerTy())
1385         return;
1386       setDoesNotThrow(F);
1387       setDoesNotAlias(F, 0);
1388     } else if (Name == "memcmp") {
1389       if (FTy->getNumParams() != 3 ||
1390           !FTy->getParamType(0)->isPointerTy() ||
1391           !FTy->getParamType(1)->isPointerTy())
1392         return;
1393       setOnlyReadsMemory(F);
1394       setDoesNotThrow(F);
1395       setDoesNotCapture(F, 1);
1396       setDoesNotCapture(F, 2);
1397     } else if (Name == "memchr" ||
1398                Name == "memrchr") {
1399       if (FTy->getNumParams() != 3)
1400         return;
1401       setOnlyReadsMemory(F);
1402       setDoesNotThrow(F);
1403     } else if (Name == "modf" ||
1404                Name == "modff" ||
1405                Name == "modfl" ||
1406                Name == "memcpy" ||
1407                Name == "memccpy" ||
1408                Name == "memmove") {
1409       if (FTy->getNumParams() < 2 ||
1410           !FTy->getParamType(1)->isPointerTy())
1411         return;
1412       setDoesNotThrow(F);
1413       setDoesNotCapture(F, 2);
1414     } else if (Name == "memalign") {
1415       if (!FTy->getReturnType()->isPointerTy())
1416         return;
1417       setDoesNotAlias(F, 0);
1418     } else if (Name == "mkdir" ||
1419                Name == "mktime") {
1420       if (FTy->getNumParams() == 0 ||
1421           !FTy->getParamType(0)->isPointerTy())
1422         return;
1423       setDoesNotThrow(F);
1424       setDoesNotCapture(F, 1);
1425     }
1426     break;
1427   case 'r':
1428     if (Name == "realloc") {
1429       if (FTy->getNumParams() != 2 ||
1430           !FTy->getParamType(0)->isPointerTy() ||
1431           !FTy->getReturnType()->isPointerTy())
1432         return;
1433       setDoesNotThrow(F);
1434       setDoesNotAlias(F, 0);
1435       setDoesNotCapture(F, 1);
1436     } else if (Name == "read") {
1437       if (FTy->getNumParams() != 3 ||
1438           !FTy->getParamType(1)->isPointerTy())
1439         return;
1440       // May throw; "read" is a valid pthread cancellation point.
1441       setDoesNotCapture(F, 2);
1442     } else if (Name == "rmdir" ||
1443                Name == "rewind" ||
1444                Name == "remove" ||
1445                Name == "realpath") {
1446       if (FTy->getNumParams() < 1 ||
1447           !FTy->getParamType(0)->isPointerTy())
1448         return;
1449       setDoesNotThrow(F);
1450       setDoesNotCapture(F, 1);
1451     } else if (Name == "rename" ||
1452                Name == "readlink") {
1453       if (FTy->getNumParams() < 2 ||
1454           !FTy->getParamType(0)->isPointerTy() ||
1455           !FTy->getParamType(1)->isPointerTy())
1456         return;
1457       setDoesNotThrow(F);
1458       setDoesNotCapture(F, 1);
1459       setDoesNotCapture(F, 2);
1460     }
1461     break;
1462   case 'w':
1463     if (Name == "write") {
1464       if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1465         return;
1466       // May throw; "write" is a valid pthread cancellation point.
1467       setDoesNotCapture(F, 2);
1468     }
1469     break;
1470   case 'b':
1471     if (Name == "bcopy") {
1472       if (FTy->getNumParams() != 3 ||
1473           !FTy->getParamType(0)->isPointerTy() ||
1474           !FTy->getParamType(1)->isPointerTy())
1475         return;
1476       setDoesNotThrow(F);
1477       setDoesNotCapture(F, 1);
1478       setDoesNotCapture(F, 2);
1479     } else if (Name == "bcmp") {
1480       if (FTy->getNumParams() != 3 ||
1481           !FTy->getParamType(0)->isPointerTy() ||
1482           !FTy->getParamType(1)->isPointerTy())
1483         return;
1484       setDoesNotThrow(F);
1485       setOnlyReadsMemory(F);
1486       setDoesNotCapture(F, 1);
1487       setDoesNotCapture(F, 2);
1488     } else if (Name == "bzero") {
1489       if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1490         return;
1491       setDoesNotThrow(F);
1492       setDoesNotCapture(F, 1);
1493     }
1494     break;
1495   case 'c':
1496     if (Name == "calloc") {
1497       if (FTy->getNumParams() != 2 ||
1498           !FTy->getReturnType()->isPointerTy())
1499         return;
1500       setDoesNotThrow(F);
1501       setDoesNotAlias(F, 0);
1502     } else if (Name == "chmod" ||
1503                Name == "chown" ||
1504                Name == "ctermid" ||
1505                Name == "clearerr" ||
1506                Name == "closedir") {
1507       if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1508         return;
1509       setDoesNotThrow(F);
1510       setDoesNotCapture(F, 1);
1511     }
1512     break;
1513   case 'a':
1514     if (Name == "atoi" ||
1515         Name == "atol" ||
1516         Name == "atof" ||
1517         Name == "atoll") {
1518       if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1519         return;
1520       setDoesNotThrow(F);
1521       setOnlyReadsMemory(F);
1522       setDoesNotCapture(F, 1);
1523     } else if (Name == "access") {
1524       if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1525         return;
1526       setDoesNotThrow(F);
1527       setDoesNotCapture(F, 1);
1528     }
1529     break;
1530   case 'f':
1531     if (Name == "fopen") {
1532       if (FTy->getNumParams() != 2 ||
1533           !FTy->getReturnType()->isPointerTy() ||
1534           !FTy->getParamType(0)->isPointerTy() ||
1535           !FTy->getParamType(1)->isPointerTy())
1536         return;
1537       setDoesNotThrow(F);
1538       setDoesNotAlias(F, 0);
1539       setDoesNotCapture(F, 1);
1540       setDoesNotCapture(F, 2);
1541     } else if (Name == "fdopen") {
1542       if (FTy->getNumParams() != 2 ||
1543           !FTy->getReturnType()->isPointerTy() ||
1544           !FTy->getParamType(1)->isPointerTy())
1545         return;
1546       setDoesNotThrow(F);
1547       setDoesNotAlias(F, 0);
1548       setDoesNotCapture(F, 2);
1549     } else if (Name == "feof" ||
1550                Name == "free" ||
1551                Name == "fseek" ||
1552                Name == "ftell" ||
1553                Name == "fgetc" ||
1554                Name == "fseeko" ||
1555                Name == "ftello" ||
1556                Name == "fileno" ||
1557                Name == "fflush" ||
1558                Name == "fclose" ||
1559                Name == "fsetpos" ||
1560                Name == "flockfile" ||
1561                Name == "funlockfile" ||
1562                Name == "ftrylockfile") {
1563       if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1564         return;
1565       setDoesNotThrow(F);
1566       setDoesNotCapture(F, 1);
1567     } else if (Name == "ferror") {
1568       if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1569         return;
1570       setDoesNotThrow(F);
1571       setDoesNotCapture(F, 1);
1572       setOnlyReadsMemory(F);
1573     } else if (Name == "fputc" ||
1574                Name == "fstat" ||
1575                Name == "frexp" ||
1576                Name == "frexpf" ||
1577                Name == "frexpl" ||
1578                Name == "fstatvfs") {
1579       if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1580         return;
1581       setDoesNotThrow(F);
1582       setDoesNotCapture(F, 2);
1583     } else if (Name == "fgets") {
1584       if (FTy->getNumParams() != 3 ||
1585           !FTy->getParamType(0)->isPointerTy() ||
1586           !FTy->getParamType(2)->isPointerTy())
1587         return;
1588       setDoesNotThrow(F);
1589       setDoesNotCapture(F, 3);
1590     } else if (Name == "fread" ||
1591                Name == "fwrite") {
1592       if (FTy->getNumParams() != 4 ||
1593           !FTy->getParamType(0)->isPointerTy() ||
1594           !FTy->getParamType(3)->isPointerTy())
1595         return;
1596       setDoesNotThrow(F);
1597       setDoesNotCapture(F, 1);
1598       setDoesNotCapture(F, 4);
1599     } else if (Name == "fputs" ||
1600                Name == "fscanf" ||
1601                Name == "fprintf" ||
1602                Name == "fgetpos") {
1603       if (FTy->getNumParams() < 2 ||
1604           !FTy->getParamType(0)->isPointerTy() ||
1605           !FTy->getParamType(1)->isPointerTy())
1606         return;
1607       setDoesNotThrow(F);
1608       setDoesNotCapture(F, 1);
1609       setDoesNotCapture(F, 2);
1610     }
1611     break;
1612   case 'g':
1613     if (Name == "getc" ||
1614         Name == "getlogin_r" ||
1615         Name == "getc_unlocked") {
1616       if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1617         return;
1618       setDoesNotThrow(F);
1619       setDoesNotCapture(F, 1);
1620     } else if (Name == "getenv") {
1621       if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1622         return;
1623       setDoesNotThrow(F);
1624       setOnlyReadsMemory(F);
1625       setDoesNotCapture(F, 1);
1626     } else if (Name == "gets" ||
1627                Name == "getchar") {
1628       setDoesNotThrow(F);
1629     } else if (Name == "getitimer") {
1630       if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1631         return;
1632       setDoesNotThrow(F);
1633       setDoesNotCapture(F, 2);
1634     } else if (Name == "getpwnam") {
1635       if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1636         return;
1637       setDoesNotThrow(F);
1638       setDoesNotCapture(F, 1);
1639     }
1640     break;
1641   case 'u':
1642     if (Name == "ungetc") {
1643       if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1644         return;
1645       setDoesNotThrow(F);
1646       setDoesNotCapture(F, 2);
1647     } else if (Name == "uname" ||
1648                Name == "unlink" ||
1649                Name == "unsetenv") {
1650       if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1651         return;
1652       setDoesNotThrow(F);
1653       setDoesNotCapture(F, 1);
1654     } else if (Name == "utime" ||
1655                Name == "utimes") {
1656       if (FTy->getNumParams() != 2 ||
1657           !FTy->getParamType(0)->isPointerTy() ||
1658           !FTy->getParamType(1)->isPointerTy())
1659         return;
1660       setDoesNotThrow(F);
1661       setDoesNotCapture(F, 1);
1662       setDoesNotCapture(F, 2);
1663     }
1664     break;
1665   case 'p':
1666     if (Name == "putc") {
1667       if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1668         return;
1669       setDoesNotThrow(F);
1670       setDoesNotCapture(F, 2);
1671     } else if (Name == "puts" ||
1672                Name == "printf" ||
1673                Name == "perror") {
1674       if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1675         return;
1676       setDoesNotThrow(F);
1677       setDoesNotCapture(F, 1);
1678     } else if (Name == "pread" ||
1679                Name == "pwrite") {
1680       if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1681         return;
1682       // May throw; these are valid pthread cancellation points.
1683       setDoesNotCapture(F, 2);
1684     } else if (Name == "putchar") {
1685       setDoesNotThrow(F);
1686     } else if (Name == "popen") {
1687       if (FTy->getNumParams() != 2 ||
1688           !FTy->getReturnType()->isPointerTy() ||
1689           !FTy->getParamType(0)->isPointerTy() ||
1690           !FTy->getParamType(1)->isPointerTy())
1691         return;
1692       setDoesNotThrow(F);
1693       setDoesNotAlias(F, 0);
1694       setDoesNotCapture(F, 1);
1695       setDoesNotCapture(F, 2);
1696     } else if (Name == "pclose") {
1697       if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1698         return;
1699       setDoesNotThrow(F);
1700       setDoesNotCapture(F, 1);
1701     }
1702     break;
1703   case 'v':
1704     if (Name == "vscanf") {
1705       if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1706         return;
1707       setDoesNotThrow(F);
1708       setDoesNotCapture(F, 1);
1709     } else if (Name == "vsscanf" ||
1710                Name == "vfscanf") {
1711       if (FTy->getNumParams() != 3 ||
1712           !FTy->getParamType(1)->isPointerTy() ||
1713           !FTy->getParamType(2)->isPointerTy())
1714         return;
1715       setDoesNotThrow(F);
1716       setDoesNotCapture(F, 1);
1717       setDoesNotCapture(F, 2);
1718     } else if (Name == "valloc") {
1719       if (!FTy->getReturnType()->isPointerTy())
1720         return;
1721       setDoesNotThrow(F);
1722       setDoesNotAlias(F, 0);
1723     } else if (Name == "vprintf") {
1724       if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1725         return;
1726       setDoesNotThrow(F);
1727       setDoesNotCapture(F, 1);
1728     } else if (Name == "vfprintf" ||
1729                Name == "vsprintf") {
1730       if (FTy->getNumParams() != 3 ||
1731           !FTy->getParamType(0)->isPointerTy() ||
1732           !FTy->getParamType(1)->isPointerTy())
1733         return;
1734       setDoesNotThrow(F);
1735       setDoesNotCapture(F, 1);
1736       setDoesNotCapture(F, 2);
1737     } else if (Name == "vsnprintf") {
1738       if (FTy->getNumParams() != 4 ||
1739           !FTy->getParamType(0)->isPointerTy() ||
1740           !FTy->getParamType(2)->isPointerTy())
1741         return;
1742       setDoesNotThrow(F);
1743       setDoesNotCapture(F, 1);
1744       setDoesNotCapture(F, 3);
1745     }
1746     break;
1747   case 'o':
1748     if (Name == "open") {
1749       if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1750         return;
1751       // May throw; "open" is a valid pthread cancellation point.
1752       setDoesNotCapture(F, 1);
1753     } else if (Name == "opendir") {
1754       if (FTy->getNumParams() != 1 ||
1755           !FTy->getReturnType()->isPointerTy() ||
1756           !FTy->getParamType(0)->isPointerTy())
1757         return;
1758       setDoesNotThrow(F);
1759       setDoesNotAlias(F, 0);
1760       setDoesNotCapture(F, 1);
1761     }
1762     break;
1763   case 't':
1764     if (Name == "tmpfile") {
1765       if (!FTy->getReturnType()->isPointerTy())
1766         return;
1767       setDoesNotThrow(F);
1768       setDoesNotAlias(F, 0);
1769     } else if (Name == "times") {
1770       if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1771         return;
1772       setDoesNotThrow(F);
1773       setDoesNotCapture(F, 1);
1774     }
1775     break;
1776   case 'h':
1777     if (Name == "htonl" ||
1778         Name == "htons") {
1779       setDoesNotThrow(F);
1780       setDoesNotAccessMemory(F);
1781     }
1782     break;
1783   case 'n':
1784     if (Name == "ntohl" ||
1785         Name == "ntohs") {
1786       setDoesNotThrow(F);
1787       setDoesNotAccessMemory(F);
1788     }
1789     break;
1790   case 'l':
1791     if (Name == "lstat") {
1792       if (FTy->getNumParams() != 2 ||
1793           !FTy->getParamType(0)->isPointerTy() ||
1794           !FTy->getParamType(1)->isPointerTy())
1795         return;
1796       setDoesNotThrow(F);
1797       setDoesNotCapture(F, 1);
1798       setDoesNotCapture(F, 2);
1799     } else if (Name == "lchown") {
1800       if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1801         return;
1802       setDoesNotThrow(F);
1803       setDoesNotCapture(F, 1);
1804     }
1805     break;
1806   case 'q':
1807     if (Name == "qsort") {
1808       if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1809         return;
1810       // May throw; places call through function pointer.
1811       setDoesNotCapture(F, 4);
1812     }
1813     break;
1814   case '_':
1815     if (Name == "__strdup" ||
1816         Name == "__strndup") {
1817       if (FTy->getNumParams() < 1 ||
1818           !FTy->getReturnType()->isPointerTy() ||
1819           !FTy->getParamType(0)->isPointerTy())
1820         return;
1821       setDoesNotThrow(F);
1822       setDoesNotAlias(F, 0);
1823       setDoesNotCapture(F, 1);
1824     } else if (Name == "__strtok_r") {
1825       if (FTy->getNumParams() != 3 ||
1826           !FTy->getParamType(1)->isPointerTy())
1827         return;
1828       setDoesNotThrow(F);
1829       setDoesNotCapture(F, 2);
1830     } else if (Name == "_IO_getc") {
1831       if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1832         return;
1833       setDoesNotThrow(F);
1834       setDoesNotCapture(F, 1);
1835     } else if (Name == "_IO_putc") {
1836       if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1837         return;
1838       setDoesNotThrow(F);
1839       setDoesNotCapture(F, 2);
1840     }
1841     break;
1842   case 1:
1843     if (Name == "\1__isoc99_scanf") {
1844       if (FTy->getNumParams() < 1 ||
1845           !FTy->getParamType(0)->isPointerTy())
1846         return;
1847       setDoesNotThrow(F);
1848       setDoesNotCapture(F, 1);
1849     } else if (Name == "\1stat64" ||
1850                Name == "\1lstat64" ||
1851                Name == "\1statvfs64" ||
1852                Name == "\1__isoc99_sscanf") {
1853       if (FTy->getNumParams() < 1 ||
1854           !FTy->getParamType(0)->isPointerTy() ||
1855           !FTy->getParamType(1)->isPointerTy())
1856         return;
1857       setDoesNotThrow(F);
1858       setDoesNotCapture(F, 1);
1859       setDoesNotCapture(F, 2);
1860     } else if (Name == "\1fopen64") {
1861       if (FTy->getNumParams() != 2 ||
1862           !FTy->getReturnType()->isPointerTy() ||
1863           !FTy->getParamType(0)->isPointerTy() ||
1864           !FTy->getParamType(1)->isPointerTy())
1865         return;
1866       setDoesNotThrow(F);
1867       setDoesNotAlias(F, 0);
1868       setDoesNotCapture(F, 1);
1869       setDoesNotCapture(F, 2);
1870     } else if (Name == "\1fseeko64" ||
1871                Name == "\1ftello64") {
1872       if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1873         return;
1874       setDoesNotThrow(F);
1875       setDoesNotCapture(F, 1);
1876     } else if (Name == "\1tmpfile64") {
1877       if (!FTy->getReturnType()->isPointerTy())
1878         return;
1879       setDoesNotThrow(F);
1880       setDoesNotAlias(F, 0);
1881     } else if (Name == "\1fstat64" ||
1882                Name == "\1fstatvfs64") {
1883       if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1884         return;
1885       setDoesNotThrow(F);
1886       setDoesNotCapture(F, 2);
1887     } else if (Name == "\1open64") {
1888       if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1889         return;
1890       // May throw; "open" is a valid pthread cancellation point.
1891       setDoesNotCapture(F, 1);
1892     }
1893     break;
1894   }
1895 }
1896
1897 /// doInitialization - Add attributes to well-known functions.
1898 ///
1899 bool SimplifyLibCalls::doInitialization(Module &M) {
1900   Modified = false;
1901   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1902     Function &F = *I;
1903     if (F.isDeclaration() && F.hasName())
1904       inferPrototypeAttributes(F);
1905   }
1906   return Modified;
1907 }
1908
1909 // TODO:
1910 //   Additional cases that we need to add to this file:
1911 //
1912 // cbrt:
1913 //   * cbrt(expN(X))  -> expN(x/3)
1914 //   * cbrt(sqrt(x))  -> pow(x,1/6)
1915 //   * cbrt(sqrt(x))  -> pow(x,1/9)
1916 //
1917 // exp, expf, expl:
1918 //   * exp(log(x))  -> x
1919 //
1920 // log, logf, logl:
1921 //   * log(exp(x))   -> x
1922 //   * log(x**y)     -> y*log(x)
1923 //   * log(exp(y))   -> y*log(e)
1924 //   * log(exp2(y))  -> y*log(2)
1925 //   * log(exp10(y)) -> y*log(10)
1926 //   * log(sqrt(x))  -> 0.5*log(x)
1927 //   * log(pow(x,y)) -> y*log(x)
1928 //
1929 // lround, lroundf, lroundl:
1930 //   * lround(cnst) -> cnst'
1931 //
1932 // pow, powf, powl:
1933 //   * pow(exp(x),y)  -> exp(x*y)
1934 //   * pow(sqrt(x),y) -> pow(x,y*0.5)
1935 //   * pow(pow(x,y),z)-> pow(x,y*z)
1936 //
1937 // round, roundf, roundl:
1938 //   * round(cnst) -> cnst'
1939 //
1940 // signbit:
1941 //   * signbit(cnst) -> cnst'
1942 //   * signbit(nncst) -> 0 (if pstv is a non-negative constant)
1943 //
1944 // sqrt, sqrtf, sqrtl:
1945 //   * sqrt(expN(x))  -> expN(x*0.5)
1946 //   * sqrt(Nroot(x)) -> pow(x,1/(2*N))
1947 //   * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
1948 //
1949 // strchr:
1950 //   * strchr(p, 0) -> strlen(p)
1951 // tan, tanf, tanl:
1952 //   * tan(atan(x)) -> x
1953 //
1954 // trunc, truncf, truncl:
1955 //   * trunc(cnst) -> cnst'
1956 //
1957 //