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