SimplifyLibCalls.cpp revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
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/SmallString.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/Triple.h"
21#include "llvm/Analysis/ValueTracking.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Intrinsics.h"
27#include "llvm/IR/LLVMContext.h"
28#include "llvm/IR/Module.h"
29#include "llvm/Support/Allocator.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Target/TargetLibraryInfo.h"
32#include "llvm/Transforms/Utils/BuildLibCalls.h"
33
34using namespace llvm;
35
36static cl::opt<bool>
37ColdErrorCalls("error-reporting-is-cold",  cl::init(true),
38  cl::Hidden, cl::desc("Treat error-reporting calls as cold"));
39
40/// This class is the abstract base class for the set of optimizations that
41/// corresponds to one library call.
42namespace {
43class LibCallOptimization {
44protected:
45  Function *Caller;
46  const DataLayout *DL;
47  const TargetLibraryInfo *TLI;
48  const LibCallSimplifier *LCS;
49  LLVMContext* Context;
50public:
51  LibCallOptimization() { }
52  virtual ~LibCallOptimization() {}
53
54  /// callOptimizer - This pure virtual method is implemented by base classes to
55  /// do various optimizations.  If this returns null then no transformation was
56  /// performed.  If it returns CI, then it transformed the call and CI is to be
57  /// deleted.  If it returns something else, replace CI with the new value and
58  /// delete CI.
59  virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
60    =0;
61
62  /// ignoreCallingConv - Returns false if this transformation could possibly
63  /// change the calling convention.
64  virtual bool ignoreCallingConv() { return false; }
65
66  Value *optimizeCall(CallInst *CI, const DataLayout *DL,
67                      const TargetLibraryInfo *TLI,
68                      const LibCallSimplifier *LCS, IRBuilder<> &B) {
69    Caller = CI->getParent()->getParent();
70    this->DL = DL;
71    this->TLI = TLI;
72    this->LCS = LCS;
73    if (CI->getCalledFunction())
74      Context = &CI->getCalledFunction()->getContext();
75
76    // We never change the calling convention.
77    if (!ignoreCallingConv() && CI->getCallingConv() != llvm::CallingConv::C)
78      return NULL;
79
80    return callOptimizer(CI->getCalledFunction(), CI, B);
81  }
82};
83
84//===----------------------------------------------------------------------===//
85// Helper Functions
86//===----------------------------------------------------------------------===//
87
88/// isOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
89/// value is equal or not-equal to zero.
90static bool isOnlyUsedInZeroEqualityComparison(Value *V) {
91  for (User *U : V->users()) {
92    if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
93      if (IC->isEquality())
94        if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
95          if (C->isNullValue())
96            continue;
97    // Unknown instruction.
98    return false;
99  }
100  return true;
101}
102
103/// isOnlyUsedInEqualityComparison - Return true if it is only used in equality
104/// comparisons with With.
105static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
106  for (User *U : V->users()) {
107    if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
108      if (IC->isEquality() && IC->getOperand(1) == With)
109        continue;
110    // Unknown instruction.
111    return false;
112  }
113  return true;
114}
115
116static bool callHasFloatingPointArgument(const CallInst *CI) {
117  for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
118       it != e; ++it) {
119    if ((*it)->getType()->isFloatingPointTy())
120      return true;
121  }
122  return false;
123}
124
125/// \brief Check whether the overloaded unary floating point function
126/// corresponing to \a Ty is available.
127static bool hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
128                            LibFunc::Func DoubleFn, LibFunc::Func FloatFn,
129                            LibFunc::Func LongDoubleFn) {
130  switch (Ty->getTypeID()) {
131  case Type::FloatTyID:
132    return TLI->has(FloatFn);
133  case Type::DoubleTyID:
134    return TLI->has(DoubleFn);
135  default:
136    return TLI->has(LongDoubleFn);
137  }
138}
139
140//===----------------------------------------------------------------------===//
141// Fortified Library Call Optimizations
142//===----------------------------------------------------------------------===//
143
144struct FortifiedLibCallOptimization : public LibCallOptimization {
145protected:
146  virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
147			  bool isString) const = 0;
148};
149
150struct InstFortifiedLibCallOptimization : public FortifiedLibCallOptimization {
151  CallInst *CI;
152
153  bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
154                  bool isString) const override {
155    if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))
156      return true;
157    if (ConstantInt *SizeCI =
158                           dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {
159      if (SizeCI->isAllOnesValue())
160        return true;
161      if (isString) {
162        uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));
163        // If the length is 0 we don't know how long it is and so we can't
164        // remove the check.
165        if (Len == 0) return false;
166        return SizeCI->getZExtValue() >= Len;
167      }
168      if (ConstantInt *Arg = dyn_cast<ConstantInt>(
169                                                  CI->getArgOperand(SizeArgOp)))
170        return SizeCI->getZExtValue() >= Arg->getZExtValue();
171    }
172    return false;
173  }
174};
175
176struct MemCpyChkOpt : public InstFortifiedLibCallOptimization {
177  Value *callOptimizer(Function *Callee, CallInst *CI,
178                       IRBuilder<> &B) override {
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) != DL->getIntPtrType(Context) ||
188        FT->getParamType(3) != DL->getIntPtrType(Context))
189      return 0;
190
191    if (isFoldable(3, 2, false)) {
192      B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
193                     CI->getArgOperand(2), 1);
194      return CI->getArgOperand(0);
195    }
196    return 0;
197  }
198};
199
200struct MemMoveChkOpt : public InstFortifiedLibCallOptimization {
201  Value *callOptimizer(Function *Callee, CallInst *CI,
202                       IRBuilder<> &B) override {
203    this->CI = CI;
204    FunctionType *FT = Callee->getFunctionType();
205    LLVMContext &Context = CI->getParent()->getContext();
206
207    // Check if this has the right signature.
208    if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
209        !FT->getParamType(0)->isPointerTy() ||
210        !FT->getParamType(1)->isPointerTy() ||
211        FT->getParamType(2) != DL->getIntPtrType(Context) ||
212        FT->getParamType(3) != DL->getIntPtrType(Context))
213      return 0;
214
215    if (isFoldable(3, 2, false)) {
216      B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
217                      CI->getArgOperand(2), 1);
218      return CI->getArgOperand(0);
219    }
220    return 0;
221  }
222};
223
224struct MemSetChkOpt : public InstFortifiedLibCallOptimization {
225  Value *callOptimizer(Function *Callee, CallInst *CI,
226                       IRBuilder<> &B) override {
227    this->CI = CI;
228    FunctionType *FT = Callee->getFunctionType();
229    LLVMContext &Context = CI->getParent()->getContext();
230
231    // Check if this has the right signature.
232    if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
233        !FT->getParamType(0)->isPointerTy() ||
234        !FT->getParamType(1)->isIntegerTy() ||
235        FT->getParamType(2) != DL->getIntPtrType(Context) ||
236        FT->getParamType(3) != DL->getIntPtrType(Context))
237      return 0;
238
239    if (isFoldable(3, 2, false)) {
240      Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
241                                   false);
242      B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
243      return CI->getArgOperand(0);
244    }
245    return 0;
246  }
247};
248
249struct StrCpyChkOpt : public InstFortifiedLibCallOptimization {
250  Value *callOptimizer(Function *Callee, CallInst *CI,
251                       IRBuilder<> &B) override {
252    this->CI = CI;
253    StringRef Name = Callee->getName();
254    FunctionType *FT = Callee->getFunctionType();
255    LLVMContext &Context = CI->getParent()->getContext();
256
257    // Check if this has the right signature.
258    if (FT->getNumParams() != 3 ||
259        FT->getReturnType() != FT->getParamType(0) ||
260        FT->getParamType(0) != FT->getParamType(1) ||
261        FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
262        FT->getParamType(2) != DL->getIntPtrType(Context))
263      return 0;
264
265    Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
266    if (Dst == Src)      // __strcpy_chk(x,x)  -> x
267      return Src;
268
269    // If a) we don't have any length information, or b) we know this will
270    // fit then just lower to a plain strcpy. Otherwise we'll keep our
271    // strcpy_chk call which may fail at runtime if the size is too long.
272    // TODO: It might be nice to get a maximum length out of the possible
273    // string lengths for varying.
274    if (isFoldable(2, 1, true)) {
275      Value *Ret = EmitStrCpy(Dst, Src, B, DL, TLI, Name.substr(2, 6));
276      return Ret;
277    } else {
278      // Maybe we can stil fold __strcpy_chk to __memcpy_chk.
279      uint64_t Len = GetStringLength(Src);
280      if (Len == 0) return 0;
281
282      // This optimization require DataLayout.
283      if (!DL) return 0;
284
285      Value *Ret =
286	EmitMemCpyChk(Dst, Src,
287                      ConstantInt::get(DL->getIntPtrType(Context), Len),
288                      CI->getArgOperand(2), B, DL, TLI);
289      return Ret;
290    }
291    return 0;
292  }
293};
294
295struct StpCpyChkOpt : public InstFortifiedLibCallOptimization {
296  Value *callOptimizer(Function *Callee, CallInst *CI,
297                       IRBuilder<> &B) override {
298    this->CI = CI;
299    StringRef Name = Callee->getName();
300    FunctionType *FT = Callee->getFunctionType();
301    LLVMContext &Context = CI->getParent()->getContext();
302
303    // Check if this has the right signature.
304    if (FT->getNumParams() != 3 ||
305        FT->getReturnType() != FT->getParamType(0) ||
306        FT->getParamType(0) != FT->getParamType(1) ||
307        FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
308        FT->getParamType(2) != DL->getIntPtrType(FT->getParamType(0)))
309      return 0;
310
311    Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
312    if (Dst == Src) {  // stpcpy(x,x)  -> x+strlen(x)
313      Value *StrLen = EmitStrLen(Src, B, DL, TLI);
314      return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
315    }
316
317    // If a) we don't have any length information, or b) we know this will
318    // fit then just lower to a plain stpcpy. Otherwise we'll keep our
319    // stpcpy_chk call which may fail at runtime if the size is too long.
320    // TODO: It might be nice to get a maximum length out of the possible
321    // string lengths for varying.
322    if (isFoldable(2, 1, true)) {
323      Value *Ret = EmitStrCpy(Dst, Src, B, DL, TLI, Name.substr(2, 6));
324      return Ret;
325    } else {
326      // Maybe we can stil fold __stpcpy_chk to __memcpy_chk.
327      uint64_t Len = GetStringLength(Src);
328      if (Len == 0) return 0;
329
330      // This optimization require DataLayout.
331      if (!DL) return 0;
332
333      Type *PT = FT->getParamType(0);
334      Value *LenV = ConstantInt::get(DL->getIntPtrType(PT), Len);
335      Value *DstEnd = B.CreateGEP(Dst,
336                                  ConstantInt::get(DL->getIntPtrType(PT),
337                                                   Len - 1));
338      if (!EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, DL, TLI))
339        return 0;
340      return DstEnd;
341    }
342    return 0;
343  }
344};
345
346struct StrNCpyChkOpt : public InstFortifiedLibCallOptimization {
347  Value *callOptimizer(Function *Callee, CallInst *CI,
348                       IRBuilder<> &B) override {
349    this->CI = CI;
350    StringRef Name = Callee->getName();
351    FunctionType *FT = Callee->getFunctionType();
352    LLVMContext &Context = CI->getParent()->getContext();
353
354    // Check if this has the right signature.
355    if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
356        FT->getParamType(0) != FT->getParamType(1) ||
357        FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
358        !FT->getParamType(2)->isIntegerTy() ||
359        FT->getParamType(3) != DL->getIntPtrType(Context))
360      return 0;
361
362    if (isFoldable(3, 2, false)) {
363      Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
364                               CI->getArgOperand(2), B, DL, TLI,
365                               Name.substr(2, 7));
366      return Ret;
367    }
368    return 0;
369  }
370};
371
372//===----------------------------------------------------------------------===//
373// String and Memory Library Call Optimizations
374//===----------------------------------------------------------------------===//
375
376struct StrCatOpt : public LibCallOptimization {
377  Value *callOptimizer(Function *Callee, CallInst *CI,
378                       IRBuilder<> &B) override {
379    // Verify the "strcat" function prototype.
380    FunctionType *FT = Callee->getFunctionType();
381    if (FT->getNumParams() != 2 ||
382        FT->getReturnType() != B.getInt8PtrTy() ||
383        FT->getParamType(0) != FT->getReturnType() ||
384        FT->getParamType(1) != FT->getReturnType())
385      return 0;
386
387    // Extract some information from the instruction
388    Value *Dst = CI->getArgOperand(0);
389    Value *Src = CI->getArgOperand(1);
390
391    // See if we can get the length of the input string.
392    uint64_t Len = GetStringLength(Src);
393    if (Len == 0) return 0;
394    --Len;  // Unbias length.
395
396    // Handle the simple, do-nothing case: strcat(x, "") -> x
397    if (Len == 0)
398      return Dst;
399
400    // These optimizations require DataLayout.
401    if (!DL) return 0;
402
403    return emitStrLenMemCpy(Src, Dst, Len, B);
404  }
405
406  Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
407                          IRBuilder<> &B) {
408    // We need to find the end of the destination string.  That's where the
409    // memory is to be moved to. We just generate a call to strlen.
410    Value *DstLen = EmitStrLen(Dst, B, DL, TLI);
411    if (!DstLen)
412      return 0;
413
414    // Now that we have the destination's length, we must index into the
415    // destination's pointer to get the actual memcpy destination (end of
416    // the string .. we're concatenating).
417    Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
418
419    // We have enough information to now generate the memcpy call to do the
420    // concatenation for us.  Make a memcpy to copy the nul byte with align = 1.
421    B.CreateMemCpy(CpyDst, Src,
422                   ConstantInt::get(DL->getIntPtrType(*Context), Len + 1), 1);
423    return Dst;
424  }
425};
426
427struct StrNCatOpt : public StrCatOpt {
428  Value *callOptimizer(Function *Callee, CallInst *CI,
429                       IRBuilder<> &B) override {
430    // Verify the "strncat" function prototype.
431    FunctionType *FT = Callee->getFunctionType();
432    if (FT->getNumParams() != 3 ||
433        FT->getReturnType() != B.getInt8PtrTy() ||
434        FT->getParamType(0) != FT->getReturnType() ||
435        FT->getParamType(1) != FT->getReturnType() ||
436        !FT->getParamType(2)->isIntegerTy())
437      return 0;
438
439    // Extract some information from the instruction
440    Value *Dst = CI->getArgOperand(0);
441    Value *Src = CI->getArgOperand(1);
442    uint64_t Len;
443
444    // We don't do anything if length is not constant
445    if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
446      Len = LengthArg->getZExtValue();
447    else
448      return 0;
449
450    // See if we can get the length of the input string.
451    uint64_t SrcLen = GetStringLength(Src);
452    if (SrcLen == 0) return 0;
453    --SrcLen;  // Unbias length.
454
455    // Handle the simple, do-nothing cases:
456    // strncat(x, "", c) -> x
457    // strncat(x,  c, 0) -> x
458    if (SrcLen == 0 || Len == 0) return Dst;
459
460    // These optimizations require DataLayout.
461    if (!DL) return 0;
462
463    // We don't optimize this case
464    if (Len < SrcLen) return 0;
465
466    // strncat(x, s, c) -> strcat(x, s)
467    // s is constant so the strcat can be optimized further
468    return emitStrLenMemCpy(Src, Dst, SrcLen, B);
469  }
470};
471
472struct StrChrOpt : public LibCallOptimization {
473  Value *callOptimizer(Function *Callee, CallInst *CI,
474                       IRBuilder<> &B) override {
475    // Verify the "strchr" function prototype.
476    FunctionType *FT = Callee->getFunctionType();
477    if (FT->getNumParams() != 2 ||
478        FT->getReturnType() != B.getInt8PtrTy() ||
479        FT->getParamType(0) != FT->getReturnType() ||
480        !FT->getParamType(1)->isIntegerTy(32))
481      return 0;
482
483    Value *SrcStr = CI->getArgOperand(0);
484
485    // If the second operand is non-constant, see if we can compute the length
486    // of the input string and turn this into memchr.
487    ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
488    if (CharC == 0) {
489      // These optimizations require DataLayout.
490      if (!DL) return 0;
491
492      uint64_t Len = GetStringLength(SrcStr);
493      if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
494        return 0;
495
496      return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
497                        ConstantInt::get(DL->getIntPtrType(*Context), Len),
498                        B, DL, TLI);
499    }
500
501    // Otherwise, the character is a constant, see if the first argument is
502    // a string literal.  If so, we can constant fold.
503    StringRef Str;
504    if (!getConstantStringInfo(SrcStr, Str)) {
505      if (DL && CharC->isZero()) // strchr(p, 0) -> p + strlen(p)
506        return B.CreateGEP(SrcStr, EmitStrLen(SrcStr, B, DL, TLI), "strchr");
507      return 0;
508    }
509
510    // Compute the offset, make sure to handle the case when we're searching for
511    // zero (a weird way to spell strlen).
512    size_t I = (0xFF & CharC->getSExtValue()) == 0 ?
513        Str.size() : Str.find(CharC->getSExtValue());
514    if (I == StringRef::npos) // Didn't find the char.  strchr returns null.
515      return Constant::getNullValue(CI->getType());
516
517    // strchr(s+n,c)  -> gep(s+n+i,c)
518    return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");
519  }
520};
521
522struct StrRChrOpt : public LibCallOptimization {
523  Value *callOptimizer(Function *Callee, CallInst *CI,
524                       IRBuilder<> &B) override {
525    // Verify the "strrchr" function prototype.
526    FunctionType *FT = Callee->getFunctionType();
527    if (FT->getNumParams() != 2 ||
528        FT->getReturnType() != B.getInt8PtrTy() ||
529        FT->getParamType(0) != FT->getReturnType() ||
530        !FT->getParamType(1)->isIntegerTy(32))
531      return 0;
532
533    Value *SrcStr = CI->getArgOperand(0);
534    ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
535
536    // Cannot fold anything if we're not looking for a constant.
537    if (!CharC)
538      return 0;
539
540    StringRef Str;
541    if (!getConstantStringInfo(SrcStr, Str)) {
542      // strrchr(s, 0) -> strchr(s, 0)
543      if (DL && CharC->isZero())
544        return EmitStrChr(SrcStr, '\0', B, DL, TLI);
545      return 0;
546    }
547
548    // Compute the offset.
549    size_t I = (0xFF & CharC->getSExtValue()) == 0 ?
550        Str.size() : Str.rfind(CharC->getSExtValue());
551    if (I == StringRef::npos) // Didn't find the char. Return null.
552      return Constant::getNullValue(CI->getType());
553
554    // strrchr(s+n,c) -> gep(s+n+i,c)
555    return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");
556  }
557};
558
559struct StrCmpOpt : public LibCallOptimization {
560  Value *callOptimizer(Function *Callee, CallInst *CI,
561                       IRBuilder<> &B) override {
562    // Verify the "strcmp" function prototype.
563    FunctionType *FT = Callee->getFunctionType();
564    if (FT->getNumParams() != 2 ||
565        !FT->getReturnType()->isIntegerTy(32) ||
566        FT->getParamType(0) != FT->getParamType(1) ||
567        FT->getParamType(0) != B.getInt8PtrTy())
568      return 0;
569
570    Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
571    if (Str1P == Str2P)      // strcmp(x,x)  -> 0
572      return ConstantInt::get(CI->getType(), 0);
573
574    StringRef Str1, Str2;
575    bool HasStr1 = getConstantStringInfo(Str1P, Str1);
576    bool HasStr2 = getConstantStringInfo(Str2P, Str2);
577
578    // strcmp(x, y)  -> cnst  (if both x and y are constant strings)
579    if (HasStr1 && HasStr2)
580      return ConstantInt::get(CI->getType(), Str1.compare(Str2));
581
582    if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
583      return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
584                                      CI->getType()));
585
586    if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
587      return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
588
589    // strcmp(P, "x") -> memcmp(P, "x", 2)
590    uint64_t Len1 = GetStringLength(Str1P);
591    uint64_t Len2 = GetStringLength(Str2P);
592    if (Len1 && Len2) {
593      // These optimizations require DataLayout.
594      if (!DL) return 0;
595
596      return EmitMemCmp(Str1P, Str2P,
597                        ConstantInt::get(DL->getIntPtrType(*Context),
598                        std::min(Len1, Len2)), B, DL, TLI);
599    }
600
601    return 0;
602  }
603};
604
605struct StrNCmpOpt : public LibCallOptimization {
606  Value *callOptimizer(Function *Callee, CallInst *CI,
607                       IRBuilder<> &B) override {
608    // Verify the "strncmp" function prototype.
609    FunctionType *FT = Callee->getFunctionType();
610    if (FT->getNumParams() != 3 ||
611        !FT->getReturnType()->isIntegerTy(32) ||
612        FT->getParamType(0) != FT->getParamType(1) ||
613        FT->getParamType(0) != B.getInt8PtrTy() ||
614        !FT->getParamType(2)->isIntegerTy())
615      return 0;
616
617    Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
618    if (Str1P == Str2P)      // strncmp(x,x,n)  -> 0
619      return ConstantInt::get(CI->getType(), 0);
620
621    // Get the length argument if it is constant.
622    uint64_t Length;
623    if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
624      Length = LengthArg->getZExtValue();
625    else
626      return 0;
627
628    if (Length == 0) // strncmp(x,y,0)   -> 0
629      return ConstantInt::get(CI->getType(), 0);
630
631    if (DL && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
632      return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI);
633
634    StringRef Str1, Str2;
635    bool HasStr1 = getConstantStringInfo(Str1P, Str1);
636    bool HasStr2 = getConstantStringInfo(Str2P, Str2);
637
638    // strncmp(x, y)  -> cnst  (if both x and y are constant strings)
639    if (HasStr1 && HasStr2) {
640      StringRef SubStr1 = Str1.substr(0, Length);
641      StringRef SubStr2 = Str2.substr(0, Length);
642      return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
643    }
644
645    if (HasStr1 && Str1.empty())  // strncmp("", x, n) -> -*x
646      return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
647                                      CI->getType()));
648
649    if (HasStr2 && Str2.empty())  // strncmp(x, "", n) -> *x
650      return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
651
652    return 0;
653  }
654};
655
656struct StrCpyOpt : public LibCallOptimization {
657  Value *callOptimizer(Function *Callee, CallInst *CI,
658                       IRBuilder<> &B) override {
659    // Verify the "strcpy" function prototype.
660    FunctionType *FT = Callee->getFunctionType();
661    if (FT->getNumParams() != 2 ||
662        FT->getReturnType() != FT->getParamType(0) ||
663        FT->getParamType(0) != FT->getParamType(1) ||
664        FT->getParamType(0) != B.getInt8PtrTy())
665      return 0;
666
667    Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
668    if (Dst == Src)      // strcpy(x,x)  -> x
669      return Src;
670
671    // These optimizations require DataLayout.
672    if (!DL) return 0;
673
674    // See if we can get the length of the input string.
675    uint64_t Len = GetStringLength(Src);
676    if (Len == 0) return 0;
677
678    // We have enough information to now generate the memcpy call to do the
679    // copy for us.  Make a memcpy to copy the nul byte with align = 1.
680    B.CreateMemCpy(Dst, Src,
681		   ConstantInt::get(DL->getIntPtrType(*Context), Len), 1);
682    return Dst;
683  }
684};
685
686struct StpCpyOpt: public LibCallOptimization {
687  Value *callOptimizer(Function *Callee, CallInst *CI,
688                       IRBuilder<> &B) override {
689    // Verify the "stpcpy" function prototype.
690    FunctionType *FT = Callee->getFunctionType();
691    if (FT->getNumParams() != 2 ||
692        FT->getReturnType() != FT->getParamType(0) ||
693        FT->getParamType(0) != FT->getParamType(1) ||
694        FT->getParamType(0) != B.getInt8PtrTy())
695      return 0;
696
697    // These optimizations require DataLayout.
698    if (!DL) return 0;
699
700    Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
701    if (Dst == Src) {  // stpcpy(x,x)  -> x+strlen(x)
702      Value *StrLen = EmitStrLen(Src, B, DL, TLI);
703      return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
704    }
705
706    // See if we can get the length of the input string.
707    uint64_t Len = GetStringLength(Src);
708    if (Len == 0) return 0;
709
710    Type *PT = FT->getParamType(0);
711    Value *LenV = ConstantInt::get(DL->getIntPtrType(PT), Len);
712    Value *DstEnd = B.CreateGEP(Dst,
713                                ConstantInt::get(DL->getIntPtrType(PT),
714                                                 Len - 1));
715
716    // We have enough information to now generate the memcpy call to do the
717    // copy for us.  Make a memcpy to copy the nul byte with align = 1.
718    B.CreateMemCpy(Dst, Src, LenV, 1);
719    return DstEnd;
720  }
721};
722
723struct StrNCpyOpt : public LibCallOptimization {
724  Value *callOptimizer(Function *Callee, CallInst *CI,
725                       IRBuilder<> &B) override {
726    FunctionType *FT = Callee->getFunctionType();
727    if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
728        FT->getParamType(0) != FT->getParamType(1) ||
729        FT->getParamType(0) != B.getInt8PtrTy() ||
730        !FT->getParamType(2)->isIntegerTy())
731      return 0;
732
733    Value *Dst = CI->getArgOperand(0);
734    Value *Src = CI->getArgOperand(1);
735    Value *LenOp = CI->getArgOperand(2);
736
737    // See if we can get the length of the input string.
738    uint64_t SrcLen = GetStringLength(Src);
739    if (SrcLen == 0) return 0;
740    --SrcLen;
741
742    if (SrcLen == 0) {
743      // strncpy(x, "", y) -> memset(x, '\0', y, 1)
744      B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
745      return Dst;
746    }
747
748    uint64_t Len;
749    if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
750      Len = LengthArg->getZExtValue();
751    else
752      return 0;
753
754    if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
755
756    // These optimizations require DataLayout.
757    if (!DL) return 0;
758
759    // Let strncpy handle the zero padding
760    if (Len > SrcLen+1) return 0;
761
762    Type *PT = FT->getParamType(0);
763    // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
764    B.CreateMemCpy(Dst, Src,
765                   ConstantInt::get(DL->getIntPtrType(PT), Len), 1);
766
767    return Dst;
768  }
769};
770
771struct StrLenOpt : public LibCallOptimization {
772  bool ignoreCallingConv() override { return true; }
773  Value *callOptimizer(Function *Callee, CallInst *CI,
774                       IRBuilder<> &B) override {
775    FunctionType *FT = Callee->getFunctionType();
776    if (FT->getNumParams() != 1 ||
777        FT->getParamType(0) != B.getInt8PtrTy() ||
778        !FT->getReturnType()->isIntegerTy())
779      return 0;
780
781    Value *Src = CI->getArgOperand(0);
782
783    // Constant folding: strlen("xyz") -> 3
784    if (uint64_t Len = GetStringLength(Src))
785      return ConstantInt::get(CI->getType(), Len-1);
786
787    // strlen(x) != 0 --> *x != 0
788    // strlen(x) == 0 --> *x == 0
789    if (isOnlyUsedInZeroEqualityComparison(CI))
790      return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
791    return 0;
792  }
793};
794
795struct StrPBrkOpt : public LibCallOptimization {
796  Value *callOptimizer(Function *Callee, CallInst *CI,
797                       IRBuilder<> &B) override {
798    FunctionType *FT = Callee->getFunctionType();
799    if (FT->getNumParams() != 2 ||
800        FT->getParamType(0) != B.getInt8PtrTy() ||
801        FT->getParamType(1) != FT->getParamType(0) ||
802        FT->getReturnType() != FT->getParamType(0))
803      return 0;
804
805    StringRef S1, S2;
806    bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
807    bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
808
809    // strpbrk(s, "") -> NULL
810    // strpbrk("", s) -> NULL
811    if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
812      return Constant::getNullValue(CI->getType());
813
814    // Constant folding.
815    if (HasS1 && HasS2) {
816      size_t I = S1.find_first_of(S2);
817      if (I == StringRef::npos) // No match.
818        return Constant::getNullValue(CI->getType());
819
820      return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");
821    }
822
823    // strpbrk(s, "a") -> strchr(s, 'a')
824    if (DL && HasS2 && S2.size() == 1)
825      return EmitStrChr(CI->getArgOperand(0), S2[0], B, DL, TLI);
826
827    return 0;
828  }
829};
830
831struct StrToOpt : public LibCallOptimization {
832  Value *callOptimizer(Function *Callee, CallInst *CI,
833                       IRBuilder<> &B) override {
834    FunctionType *FT = Callee->getFunctionType();
835    if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
836        !FT->getParamType(0)->isPointerTy() ||
837        !FT->getParamType(1)->isPointerTy())
838      return 0;
839
840    Value *EndPtr = CI->getArgOperand(1);
841    if (isa<ConstantPointerNull>(EndPtr)) {
842      // With a null EndPtr, this function won't capture the main argument.
843      // It would be readonly too, except that it still may write to errno.
844      CI->addAttribute(1, Attribute::NoCapture);
845    }
846
847    return 0;
848  }
849};
850
851struct StrSpnOpt : public LibCallOptimization {
852  Value *callOptimizer(Function *Callee, CallInst *CI,
853                       IRBuilder<> &B) override {
854    FunctionType *FT = Callee->getFunctionType();
855    if (FT->getNumParams() != 2 ||
856        FT->getParamType(0) != B.getInt8PtrTy() ||
857        FT->getParamType(1) != FT->getParamType(0) ||
858        !FT->getReturnType()->isIntegerTy())
859      return 0;
860
861    StringRef S1, S2;
862    bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
863    bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
864
865    // strspn(s, "") -> 0
866    // strspn("", s) -> 0
867    if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
868      return Constant::getNullValue(CI->getType());
869
870    // Constant folding.
871    if (HasS1 && HasS2) {
872      size_t Pos = S1.find_first_not_of(S2);
873      if (Pos == StringRef::npos) Pos = S1.size();
874      return ConstantInt::get(CI->getType(), Pos);
875    }
876
877    return 0;
878  }
879};
880
881struct StrCSpnOpt : public LibCallOptimization {
882  Value *callOptimizer(Function *Callee, CallInst *CI,
883                       IRBuilder<> &B) override {
884    FunctionType *FT = Callee->getFunctionType();
885    if (FT->getNumParams() != 2 ||
886        FT->getParamType(0) != B.getInt8PtrTy() ||
887        FT->getParamType(1) != FT->getParamType(0) ||
888        !FT->getReturnType()->isIntegerTy())
889      return 0;
890
891    StringRef S1, S2;
892    bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
893    bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
894
895    // strcspn("", s) -> 0
896    if (HasS1 && S1.empty())
897      return Constant::getNullValue(CI->getType());
898
899    // Constant folding.
900    if (HasS1 && HasS2) {
901      size_t Pos = S1.find_first_of(S2);
902      if (Pos == StringRef::npos) Pos = S1.size();
903      return ConstantInt::get(CI->getType(), Pos);
904    }
905
906    // strcspn(s, "") -> strlen(s)
907    if (DL && HasS2 && S2.empty())
908      return EmitStrLen(CI->getArgOperand(0), B, DL, TLI);
909
910    return 0;
911  }
912};
913
914struct StrStrOpt : public LibCallOptimization {
915  Value *callOptimizer(Function *Callee, CallInst *CI,
916                       IRBuilder<> &B) override {
917    FunctionType *FT = Callee->getFunctionType();
918    if (FT->getNumParams() != 2 ||
919        !FT->getParamType(0)->isPointerTy() ||
920        !FT->getParamType(1)->isPointerTy() ||
921        !FT->getReturnType()->isPointerTy())
922      return 0;
923
924    // fold strstr(x, x) -> x.
925    if (CI->getArgOperand(0) == CI->getArgOperand(1))
926      return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
927
928    // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
929    if (DL && isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
930      Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, DL, TLI);
931      if (!StrLen)
932        return 0;
933      Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
934                                   StrLen, B, DL, TLI);
935      if (!StrNCmp)
936        return 0;
937      for (auto UI = CI->user_begin(), UE = CI->user_end(); UI != UE;) {
938        ICmpInst *Old = cast<ICmpInst>(*UI++);
939        Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
940                                  ConstantInt::getNullValue(StrNCmp->getType()),
941                                  "cmp");
942        LCS->replaceAllUsesWith(Old, Cmp);
943      }
944      return CI;
945    }
946
947    // See if either input string is a constant string.
948    StringRef SearchStr, ToFindStr;
949    bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
950    bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
951
952    // fold strstr(x, "") -> x.
953    if (HasStr2 && ToFindStr.empty())
954      return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
955
956    // If both strings are known, constant fold it.
957    if (HasStr1 && HasStr2) {
958      size_t Offset = SearchStr.find(ToFindStr);
959
960      if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
961        return Constant::getNullValue(CI->getType());
962
963      // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
964      Value *Result = CastToCStr(CI->getArgOperand(0), B);
965      Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
966      return B.CreateBitCast(Result, CI->getType());
967    }
968
969    // fold strstr(x, "y") -> strchr(x, 'y').
970    if (HasStr2 && ToFindStr.size() == 1) {
971      Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, DL, TLI);
972      return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : 0;
973    }
974    return 0;
975  }
976};
977
978struct MemCmpOpt : public LibCallOptimization {
979  Value *callOptimizer(Function *Callee, CallInst *CI,
980                       IRBuilder<> &B) override {
981    FunctionType *FT = Callee->getFunctionType();
982    if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
983        !FT->getParamType(1)->isPointerTy() ||
984        !FT->getReturnType()->isIntegerTy(32))
985      return 0;
986
987    Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
988
989    if (LHS == RHS)  // memcmp(s,s,x) -> 0
990      return Constant::getNullValue(CI->getType());
991
992    // Make sure we have a constant length.
993    ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
994    if (!LenC) return 0;
995    uint64_t Len = LenC->getZExtValue();
996
997    if (Len == 0) // memcmp(s1,s2,0) -> 0
998      return Constant::getNullValue(CI->getType());
999
1000    // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
1001    if (Len == 1) {
1002      Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
1003                                 CI->getType(), "lhsv");
1004      Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
1005                                 CI->getType(), "rhsv");
1006      return B.CreateSub(LHSV, RHSV, "chardiff");
1007    }
1008
1009    // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
1010    StringRef LHSStr, RHSStr;
1011    if (getConstantStringInfo(LHS, LHSStr) &&
1012        getConstantStringInfo(RHS, RHSStr)) {
1013      // Make sure we're not reading out-of-bounds memory.
1014      if (Len > LHSStr.size() || Len > RHSStr.size())
1015        return 0;
1016      // Fold the memcmp and normalize the result.  This way we get consistent
1017      // results across multiple platforms.
1018      uint64_t Ret = 0;
1019      int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);
1020      if (Cmp < 0)
1021        Ret = -1;
1022      else if (Cmp > 0)
1023        Ret = 1;
1024      return ConstantInt::get(CI->getType(), Ret);
1025    }
1026
1027    return 0;
1028  }
1029};
1030
1031struct MemCpyOpt : public LibCallOptimization {
1032  Value *callOptimizer(Function *Callee, CallInst *CI,
1033                       IRBuilder<> &B) override {
1034    // These optimizations require DataLayout.
1035    if (!DL) return 0;
1036
1037    FunctionType *FT = Callee->getFunctionType();
1038    if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1039        !FT->getParamType(0)->isPointerTy() ||
1040        !FT->getParamType(1)->isPointerTy() ||
1041        FT->getParamType(2) != DL->getIntPtrType(*Context))
1042      return 0;
1043
1044    // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
1045    B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1046                   CI->getArgOperand(2), 1);
1047    return CI->getArgOperand(0);
1048  }
1049};
1050
1051struct MemMoveOpt : public LibCallOptimization {
1052  Value *callOptimizer(Function *Callee, CallInst *CI,
1053                       IRBuilder<> &B) override {
1054    // These optimizations require DataLayout.
1055    if (!DL) return 0;
1056
1057    FunctionType *FT = Callee->getFunctionType();
1058    if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1059        !FT->getParamType(0)->isPointerTy() ||
1060        !FT->getParamType(1)->isPointerTy() ||
1061        FT->getParamType(2) != DL->getIntPtrType(*Context))
1062      return 0;
1063
1064    // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
1065    B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
1066                    CI->getArgOperand(2), 1);
1067    return CI->getArgOperand(0);
1068  }
1069};
1070
1071struct MemSetOpt : public LibCallOptimization {
1072  Value *callOptimizer(Function *Callee, CallInst *CI,
1073                       IRBuilder<> &B) override {
1074    // These optimizations require DataLayout.
1075    if (!DL) return 0;
1076
1077    FunctionType *FT = Callee->getFunctionType();
1078    if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1079        !FT->getParamType(0)->isPointerTy() ||
1080        !FT->getParamType(1)->isIntegerTy() ||
1081        FT->getParamType(2) != DL->getIntPtrType(FT->getParamType(0)))
1082      return 0;
1083
1084    // memset(p, v, n) -> llvm.memset(p, v, n, 1)
1085    Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
1086    B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
1087    return CI->getArgOperand(0);
1088  }
1089};
1090
1091//===----------------------------------------------------------------------===//
1092// Math Library Optimizations
1093//===----------------------------------------------------------------------===//
1094
1095//===----------------------------------------------------------------------===//
1096// Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1097
1098struct UnaryDoubleFPOpt : public LibCallOptimization {
1099  bool CheckRetType;
1100  UnaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {}
1101  Value *callOptimizer(Function *Callee, CallInst *CI,
1102                       IRBuilder<> &B) override {
1103    FunctionType *FT = Callee->getFunctionType();
1104    if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1105        !FT->getParamType(0)->isDoubleTy())
1106      return 0;
1107
1108    if (CheckRetType) {
1109      // Check if all the uses for function like 'sin' are converted to float.
1110      for (User *U : CI->users()) {
1111        FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);
1112        if (Cast == 0 || !Cast->getType()->isFloatTy())
1113          return 0;
1114      }
1115    }
1116
1117    // If this is something like 'floor((double)floatval)', convert to floorf.
1118    FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
1119    if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
1120      return 0;
1121
1122    // floor((double)floatval) -> (double)floorf(floatval)
1123    Value *V = Cast->getOperand(0);
1124    V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
1125    return B.CreateFPExt(V, B.getDoubleTy());
1126  }
1127};
1128
1129// Double -> Float Shrinking Optimizations for Binary Functions like 'fmin/fmax'
1130struct BinaryDoubleFPOpt : public LibCallOptimization {
1131  bool CheckRetType;
1132  BinaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {}
1133  Value *callOptimizer(Function *Callee, CallInst *CI,
1134                       IRBuilder<> &B) override {
1135    FunctionType *FT = Callee->getFunctionType();
1136    // Just make sure this has 2 arguments of the same FP type, which match the
1137    // result type.
1138    if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1139        FT->getParamType(0) != FT->getParamType(1) ||
1140        !FT->getParamType(0)->isFloatingPointTy())
1141      return 0;
1142
1143    if (CheckRetType) {
1144      // Check if all the uses for function like 'fmin/fmax' are converted to
1145      // float.
1146      for (User *U : CI->users()) {
1147        FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);
1148        if (Cast == 0 || !Cast->getType()->isFloatTy())
1149          return 0;
1150      }
1151    }
1152
1153    // If this is something like 'fmin((double)floatval1, (double)floatval2)',
1154    // we convert it to fminf.
1155    FPExtInst *Cast1 = dyn_cast<FPExtInst>(CI->getArgOperand(0));
1156    FPExtInst *Cast2 = dyn_cast<FPExtInst>(CI->getArgOperand(1));
1157    if (Cast1 == 0 || !Cast1->getOperand(0)->getType()->isFloatTy() ||
1158        Cast2 == 0 || !Cast2->getOperand(0)->getType()->isFloatTy())
1159      return 0;
1160
1161    // fmin((double)floatval1, (double)floatval2)
1162    //                      -> (double)fmin(floatval1, floatval2)
1163    Value *V = NULL;
1164    Value *V1 = Cast1->getOperand(0);
1165    Value *V2 = Cast2->getOperand(0);
1166    V = EmitBinaryFloatFnCall(V1, V2, Callee->getName(), B,
1167                              Callee->getAttributes());
1168    return B.CreateFPExt(V, B.getDoubleTy());
1169  }
1170};
1171
1172struct UnsafeFPLibCallOptimization : public LibCallOptimization {
1173  bool UnsafeFPShrink;
1174  UnsafeFPLibCallOptimization(bool UnsafeFPShrink) {
1175    this->UnsafeFPShrink = UnsafeFPShrink;
1176  }
1177};
1178
1179struct CosOpt : public UnsafeFPLibCallOptimization {
1180  CosOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
1181  Value *callOptimizer(Function *Callee, CallInst *CI,
1182                       IRBuilder<> &B) override {
1183    Value *Ret = NULL;
1184    if (UnsafeFPShrink && Callee->getName() == "cos" &&
1185        TLI->has(LibFunc::cosf)) {
1186      UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
1187      Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
1188    }
1189
1190    FunctionType *FT = Callee->getFunctionType();
1191    // Just make sure this has 1 argument of FP type, which matches the
1192    // result type.
1193    if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1194        !FT->getParamType(0)->isFloatingPointTy())
1195      return Ret;
1196
1197    // cos(-x) -> cos(x)
1198    Value *Op1 = CI->getArgOperand(0);
1199    if (BinaryOperator::isFNeg(Op1)) {
1200      BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);
1201      return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");
1202    }
1203    return Ret;
1204  }
1205};
1206
1207struct PowOpt : public UnsafeFPLibCallOptimization {
1208  PowOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
1209  Value *callOptimizer(Function *Callee, CallInst *CI,
1210                       IRBuilder<> &B) override {
1211    Value *Ret = NULL;
1212    if (UnsafeFPShrink && Callee->getName() == "pow" &&
1213        TLI->has(LibFunc::powf)) {
1214      UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
1215      Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
1216    }
1217
1218    FunctionType *FT = Callee->getFunctionType();
1219    // Just make sure this has 2 arguments of the same FP type, which match the
1220    // result type.
1221    if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1222        FT->getParamType(0) != FT->getParamType(1) ||
1223        !FT->getParamType(0)->isFloatingPointTy())
1224      return Ret;
1225
1226    Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
1227    if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1228      // pow(1.0, x) -> 1.0
1229      if (Op1C->isExactlyValue(1.0))
1230        return Op1C;
1231      // pow(2.0, x) -> exp2(x)
1232      if (Op1C->isExactlyValue(2.0) &&
1233          hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp2, LibFunc::exp2f,
1234                          LibFunc::exp2l))
1235        return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
1236      // pow(10.0, x) -> exp10(x)
1237      if (Op1C->isExactlyValue(10.0) &&
1238          hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp10, LibFunc::exp10f,
1239                          LibFunc::exp10l))
1240        return EmitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp10), B,
1241                                    Callee->getAttributes());
1242    }
1243
1244    ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1245    if (Op2C == 0) return Ret;
1246
1247    if (Op2C->getValueAPF().isZero())  // pow(x, 0.0) -> 1.0
1248      return ConstantFP::get(CI->getType(), 1.0);
1249
1250    if (Op2C->isExactlyValue(0.5) &&
1251        hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::sqrt, LibFunc::sqrtf,
1252                        LibFunc::sqrtl) &&
1253        hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::fabs, LibFunc::fabsf,
1254                        LibFunc::fabsl)) {
1255      // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1256      // This is faster than calling pow, and still handles negative zero
1257      // and negative infinity correctly.
1258      // TODO: In fast-math mode, this could be just sqrt(x).
1259      // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
1260      Value *Inf = ConstantFP::getInfinity(CI->getType());
1261      Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
1262      Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1263                                         Callee->getAttributes());
1264      Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1265                                         Callee->getAttributes());
1266      Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
1267      Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
1268      return Sel;
1269    }
1270
1271    if (Op2C->isExactlyValue(1.0))  // pow(x, 1.0) -> x
1272      return Op1;
1273    if (Op2C->isExactlyValue(2.0))  // pow(x, 2.0) -> x*x
1274      return B.CreateFMul(Op1, Op1, "pow2");
1275    if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
1276      return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
1277                          Op1, "powrecip");
1278    return 0;
1279  }
1280};
1281
1282struct Exp2Opt : public UnsafeFPLibCallOptimization {
1283  Exp2Opt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
1284  Value *callOptimizer(Function *Callee, CallInst *CI,
1285                       IRBuilder<> &B) override {
1286    Value *Ret = NULL;
1287    if (UnsafeFPShrink && Callee->getName() == "exp2" &&
1288        TLI->has(LibFunc::exp2f)) {
1289      UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
1290      Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
1291    }
1292
1293    FunctionType *FT = Callee->getFunctionType();
1294    // Just make sure this has 1 argument of FP type, which matches the
1295    // result type.
1296    if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1297        !FT->getParamType(0)->isFloatingPointTy())
1298      return Ret;
1299
1300    Value *Op = CI->getArgOperand(0);
1301    // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x))  if sizeof(x) <= 32
1302    // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x))  if sizeof(x) < 32
1303    LibFunc::Func LdExp = LibFunc::ldexpl;
1304    if (Op->getType()->isFloatTy())
1305      LdExp = LibFunc::ldexpf;
1306    else if (Op->getType()->isDoubleTy())
1307      LdExp = LibFunc::ldexp;
1308
1309    if (TLI->has(LdExp)) {
1310      Value *LdExpArg = 0;
1311      if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1312        if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
1313          LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
1314      } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1315        if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
1316          LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
1317      }
1318
1319      if (LdExpArg) {
1320        Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
1321        if (!Op->getType()->isFloatTy())
1322          One = ConstantExpr::getFPExtend(One, Op->getType());
1323
1324        Module *M = Caller->getParent();
1325        Value *Callee =
1326            M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(),
1327                                   Op->getType(), B.getInt32Ty(), NULL);
1328        CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1329        if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1330          CI->setCallingConv(F->getCallingConv());
1331
1332        return CI;
1333      }
1334    }
1335    return Ret;
1336  }
1337};
1338
1339struct SinCosPiOpt : public LibCallOptimization {
1340  SinCosPiOpt() {}
1341
1342  Value *callOptimizer(Function *Callee, CallInst *CI,
1343                       IRBuilder<> &B) override {
1344    // Make sure the prototype is as expected, otherwise the rest of the
1345    // function is probably invalid and likely to abort.
1346    if (!isTrigLibCall(CI))
1347      return 0;
1348
1349    Value *Arg = CI->getArgOperand(0);
1350    SmallVector<CallInst *, 1> SinCalls;
1351    SmallVector<CallInst *, 1> CosCalls;
1352    SmallVector<CallInst *, 1> SinCosCalls;
1353
1354    bool IsFloat = Arg->getType()->isFloatTy();
1355
1356    // Look for all compatible sinpi, cospi and sincospi calls with the same
1357    // argument. If there are enough (in some sense) we can make the
1358    // substitution.
1359    for (User *U : Arg->users())
1360      classifyArgUse(U, CI->getParent(), IsFloat, SinCalls, CosCalls,
1361                     SinCosCalls);
1362
1363    // It's only worthwhile if both sinpi and cospi are actually used.
1364    if (SinCosCalls.empty() && (SinCalls.empty() || CosCalls.empty()))
1365      return 0;
1366
1367    Value *Sin, *Cos, *SinCos;
1368    insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos,
1369                     SinCos);
1370
1371    replaceTrigInsts(SinCalls, Sin);
1372    replaceTrigInsts(CosCalls, Cos);
1373    replaceTrigInsts(SinCosCalls, SinCos);
1374
1375    return 0;
1376  }
1377
1378  bool isTrigLibCall(CallInst *CI) {
1379    Function *Callee = CI->getCalledFunction();
1380    FunctionType *FT = Callee->getFunctionType();
1381
1382    // We can only hope to do anything useful if we can ignore things like errno
1383    // and floating-point exceptions.
1384    bool AttributesSafe = CI->hasFnAttr(Attribute::NoUnwind) &&
1385                          CI->hasFnAttr(Attribute::ReadNone);
1386
1387    // Other than that we need float(float) or double(double)
1388    return AttributesSafe && FT->getNumParams() == 1 &&
1389           FT->getReturnType() == FT->getParamType(0) &&
1390           (FT->getParamType(0)->isFloatTy() ||
1391            FT->getParamType(0)->isDoubleTy());
1392  }
1393
1394  void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,
1395                      SmallVectorImpl<CallInst *> &SinCalls,
1396                      SmallVectorImpl<CallInst *> &CosCalls,
1397                      SmallVectorImpl<CallInst *> &SinCosCalls) {
1398    CallInst *CI = dyn_cast<CallInst>(Val);
1399
1400    if (!CI)
1401      return;
1402
1403    Function *Callee = CI->getCalledFunction();
1404    StringRef FuncName = Callee->getName();
1405    LibFunc::Func Func;
1406    if (!TLI->getLibFunc(FuncName, Func) || !TLI->has(Func) ||
1407        !isTrigLibCall(CI))
1408      return;
1409
1410    if (IsFloat) {
1411      if (Func == LibFunc::sinpif)
1412        SinCalls.push_back(CI);
1413      else if (Func == LibFunc::cospif)
1414        CosCalls.push_back(CI);
1415      else if (Func == LibFunc::sincospif_stret)
1416        SinCosCalls.push_back(CI);
1417    } else {
1418      if (Func == LibFunc::sinpi)
1419        SinCalls.push_back(CI);
1420      else if (Func == LibFunc::cospi)
1421        CosCalls.push_back(CI);
1422      else if (Func == LibFunc::sincospi_stret)
1423        SinCosCalls.push_back(CI);
1424    }
1425  }
1426
1427  void replaceTrigInsts(SmallVectorImpl<CallInst*> &Calls, Value *Res) {
1428    for (SmallVectorImpl<CallInst*>::iterator I = Calls.begin(),
1429           E = Calls.end();
1430         I != E; ++I) {
1431      LCS->replaceAllUsesWith(*I, Res);
1432    }
1433  }
1434
1435  void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,
1436                        bool UseFloat, Value *&Sin, Value *&Cos,
1437                        Value *&SinCos) {
1438    Type *ArgTy = Arg->getType();
1439    Type *ResTy;
1440    StringRef Name;
1441
1442    Triple T(OrigCallee->getParent()->getTargetTriple());
1443    if (UseFloat) {
1444      Name = "__sincospif_stret";
1445
1446      assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");
1447      // x86_64 can't use {float, float} since that would be returned in both
1448      // xmm0 and xmm1, which isn't what a real struct would do.
1449      ResTy = T.getArch() == Triple::x86_64
1450                  ? static_cast<Type *>(VectorType::get(ArgTy, 2))
1451                  : static_cast<Type *>(StructType::get(ArgTy, ArgTy, NULL));
1452    } else {
1453      Name = "__sincospi_stret";
1454      ResTy = StructType::get(ArgTy, ArgTy, NULL);
1455    }
1456
1457    Module *M = OrigCallee->getParent();
1458    Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(),
1459                                           ResTy, ArgTy, NULL);
1460
1461    if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
1462      // If the argument is an instruction, it must dominate all uses so put our
1463      // sincos call there.
1464      BasicBlock::iterator Loc = ArgInst;
1465      B.SetInsertPoint(ArgInst->getParent(), ++Loc);
1466    } else {
1467      // Otherwise (e.g. for a constant) the beginning of the function is as
1468      // good a place as any.
1469      BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
1470      B.SetInsertPoint(&EntryBB, EntryBB.begin());
1471    }
1472
1473    SinCos = B.CreateCall(Callee, Arg, "sincospi");
1474
1475    if (SinCos->getType()->isStructTy()) {
1476      Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
1477      Cos = B.CreateExtractValue(SinCos, 1, "cospi");
1478    } else {
1479      Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
1480                                   "sinpi");
1481      Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
1482                                   "cospi");
1483    }
1484  }
1485
1486};
1487
1488//===----------------------------------------------------------------------===//
1489// Integer Library Call Optimizations
1490//===----------------------------------------------------------------------===//
1491
1492struct FFSOpt : public LibCallOptimization {
1493  Value *callOptimizer(Function *Callee, CallInst *CI,
1494                       IRBuilder<> &B) override {
1495    FunctionType *FT = Callee->getFunctionType();
1496    // Just make sure this has 2 arguments of the same FP type, which match the
1497    // result type.
1498    if (FT->getNumParams() != 1 ||
1499        !FT->getReturnType()->isIntegerTy(32) ||
1500        !FT->getParamType(0)->isIntegerTy())
1501      return 0;
1502
1503    Value *Op = CI->getArgOperand(0);
1504
1505    // Constant fold.
1506    if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1507      if (CI->isZero()) // ffs(0) -> 0.
1508        return B.getInt32(0);
1509      // ffs(c) -> cttz(c)+1
1510      return B.getInt32(CI->getValue().countTrailingZeros() + 1);
1511    }
1512
1513    // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1514    Type *ArgType = Op->getType();
1515    Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1516                                         Intrinsic::cttz, ArgType);
1517    Value *V = B.CreateCall2(F, Op, B.getFalse(), "cttz");
1518    V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
1519    V = B.CreateIntCast(V, B.getInt32Ty(), false);
1520
1521    Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
1522    return B.CreateSelect(Cond, V, B.getInt32(0));
1523  }
1524};
1525
1526struct AbsOpt : public LibCallOptimization {
1527  bool ignoreCallingConv() override { return true; }
1528  Value *callOptimizer(Function *Callee, CallInst *CI,
1529                       IRBuilder<> &B) override {
1530    FunctionType *FT = Callee->getFunctionType();
1531    // We require integer(integer) where the types agree.
1532    if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1533        FT->getParamType(0) != FT->getReturnType())
1534      return 0;
1535
1536    // abs(x) -> x >s -1 ? x : -x
1537    Value *Op = CI->getArgOperand(0);
1538    Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),
1539                                 "ispos");
1540    Value *Neg = B.CreateNeg(Op, "neg");
1541    return B.CreateSelect(Pos, Op, Neg);
1542  }
1543};
1544
1545struct IsDigitOpt : public LibCallOptimization {
1546  Value *callOptimizer(Function *Callee, CallInst *CI,
1547                       IRBuilder<> &B) override {
1548    FunctionType *FT = Callee->getFunctionType();
1549    // We require integer(i32)
1550    if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1551        !FT->getParamType(0)->isIntegerTy(32))
1552      return 0;
1553
1554    // isdigit(c) -> (c-'0') <u 10
1555    Value *Op = CI->getArgOperand(0);
1556    Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
1557    Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
1558    return B.CreateZExt(Op, CI->getType());
1559  }
1560};
1561
1562struct IsAsciiOpt : public LibCallOptimization {
1563  Value *callOptimizer(Function *Callee, CallInst *CI,
1564                       IRBuilder<> &B) override {
1565    FunctionType *FT = Callee->getFunctionType();
1566    // We require integer(i32)
1567    if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1568        !FT->getParamType(0)->isIntegerTy(32))
1569      return 0;
1570
1571    // isascii(c) -> c <u 128
1572    Value *Op = CI->getArgOperand(0);
1573    Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
1574    return B.CreateZExt(Op, CI->getType());
1575  }
1576};
1577
1578struct ToAsciiOpt : public LibCallOptimization {
1579  Value *callOptimizer(Function *Callee, CallInst *CI,
1580                       IRBuilder<> &B) override {
1581    FunctionType *FT = Callee->getFunctionType();
1582    // We require i32(i32)
1583    if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1584        !FT->getParamType(0)->isIntegerTy(32))
1585      return 0;
1586
1587    // toascii(c) -> c & 0x7f
1588    return B.CreateAnd(CI->getArgOperand(0),
1589                       ConstantInt::get(CI->getType(),0x7F));
1590  }
1591};
1592
1593//===----------------------------------------------------------------------===//
1594// Formatting and IO Library Call Optimizations
1595//===----------------------------------------------------------------------===//
1596
1597struct ErrorReportingOpt : public LibCallOptimization {
1598  ErrorReportingOpt(int S = -1) : StreamArg(S) {}
1599
1600  Value *callOptimizer(Function *Callee, CallInst *CI,
1601                       IRBuilder<> &) override {
1602    // Error reporting calls should be cold, mark them as such.
1603    // This applies even to non-builtin calls: it is only a hint and applies to
1604    // functions that the frontend might not understand as builtins.
1605
1606    // This heuristic was suggested in:
1607    // Improving Static Branch Prediction in a Compiler
1608    // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu
1609    // Proceedings of PACT'98, Oct. 1998, IEEE
1610
1611    if (!CI->hasFnAttr(Attribute::Cold) && isReportingError(Callee, CI)) {
1612      CI->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold);
1613    }
1614
1615    return 0;
1616  }
1617
1618protected:
1619  bool isReportingError(Function *Callee, CallInst *CI) {
1620    if (!ColdErrorCalls)
1621      return false;
1622
1623    if (!Callee || !Callee->isDeclaration())
1624      return false;
1625
1626    if (StreamArg < 0)
1627      return true;
1628
1629    // These functions might be considered cold, but only if their stream
1630    // argument is stderr.
1631
1632    if (StreamArg >= (int) CI->getNumArgOperands())
1633      return false;
1634    LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
1635    if (!LI)
1636      return false;
1637    GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
1638    if (!GV || !GV->isDeclaration())
1639      return false;
1640    return GV->getName() == "stderr";
1641  }
1642
1643  int StreamArg;
1644};
1645
1646struct PrintFOpt : public LibCallOptimization {
1647  Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,
1648                                   IRBuilder<> &B) {
1649    // Check for a fixed format string.
1650    StringRef FormatStr;
1651    if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
1652      return 0;
1653
1654    // Empty format string -> noop.
1655    if (FormatStr.empty())  // Tolerate printf's declared void.
1656      return CI->use_empty() ? (Value*)CI :
1657                               ConstantInt::get(CI->getType(), 0);
1658
1659    // Do not do any of the following transformations if the printf return value
1660    // is used, in general the printf return value is not compatible with either
1661    // putchar() or puts().
1662    if (!CI->use_empty())
1663      return 0;
1664
1665    // printf("x") -> putchar('x'), even for '%'.
1666    if (FormatStr.size() == 1) {
1667      Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, DL, TLI);
1668      if (CI->use_empty() || !Res) return Res;
1669      return B.CreateIntCast(Res, CI->getType(), true);
1670    }
1671
1672    // printf("foo\n") --> puts("foo")
1673    if (FormatStr[FormatStr.size()-1] == '\n' &&
1674        FormatStr.find('%') == StringRef::npos) { // No format characters.
1675      // Create a string literal with no \n on it.  We expect the constant merge
1676      // pass to be run after this pass, to merge duplicate strings.
1677      FormatStr = FormatStr.drop_back();
1678      Value *GV = B.CreateGlobalString(FormatStr, "str");
1679      Value *NewCI = EmitPutS(GV, B, DL, TLI);
1680      return (CI->use_empty() || !NewCI) ?
1681              NewCI :
1682              ConstantInt::get(CI->getType(), FormatStr.size()+1);
1683    }
1684
1685    // Optimize specific format strings.
1686    // printf("%c", chr) --> putchar(chr)
1687    if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
1688        CI->getArgOperand(1)->getType()->isIntegerTy()) {
1689      Value *Res = EmitPutChar(CI->getArgOperand(1), B, DL, TLI);
1690
1691      if (CI->use_empty() || !Res) return Res;
1692      return B.CreateIntCast(Res, CI->getType(), true);
1693    }
1694
1695    // printf("%s\n", str) --> puts(str)
1696    if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
1697        CI->getArgOperand(1)->getType()->isPointerTy()) {
1698      return EmitPutS(CI->getArgOperand(1), B, DL, TLI);
1699    }
1700    return 0;
1701  }
1702
1703  Value *callOptimizer(Function *Callee, CallInst *CI,
1704                       IRBuilder<> &B) override {
1705    // Require one fixed pointer argument and an integer/void result.
1706    FunctionType *FT = Callee->getFunctionType();
1707    if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1708        !(FT->getReturnType()->isIntegerTy() ||
1709          FT->getReturnType()->isVoidTy()))
1710      return 0;
1711
1712    if (Value *V = optimizeFixedFormatString(Callee, CI, B)) {
1713      return V;
1714    }
1715
1716    // printf(format, ...) -> iprintf(format, ...) if no floating point
1717    // arguments.
1718    if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) {
1719      Module *M = B.GetInsertBlock()->getParent()->getParent();
1720      Constant *IPrintFFn =
1721        M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
1722      CallInst *New = cast<CallInst>(CI->clone());
1723      New->setCalledFunction(IPrintFFn);
1724      B.Insert(New);
1725      return New;
1726    }
1727    return 0;
1728  }
1729};
1730
1731struct SPrintFOpt : public LibCallOptimization {
1732  Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
1733                                   IRBuilder<> &B) {
1734    // Check for a fixed format string.
1735    StringRef FormatStr;
1736    if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1737      return 0;
1738
1739    // If we just have a format string (nothing else crazy) transform it.
1740    if (CI->getNumArgOperands() == 2) {
1741      // Make sure there's no % in the constant array.  We could try to handle
1742      // %% -> % in the future if we cared.
1743      for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1744        if (FormatStr[i] == '%')
1745          return 0; // we found a format specifier, bail out.
1746
1747      // These optimizations require DataLayout.
1748      if (!DL) return 0;
1749
1750      // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1751      B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1752                     ConstantInt::get(DL->getIntPtrType(*Context), // Copy the
1753                                      FormatStr.size() + 1), 1);   // nul byte.
1754      return ConstantInt::get(CI->getType(), FormatStr.size());
1755    }
1756
1757    // The remaining optimizations require the format string to be "%s" or "%c"
1758    // and have an extra operand.
1759    if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1760        CI->getNumArgOperands() < 3)
1761      return 0;
1762
1763    // Decode the second character of the format string.
1764    if (FormatStr[1] == 'c') {
1765      // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1766      if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1767      Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
1768      Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
1769      B.CreateStore(V, Ptr);
1770      Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
1771      B.CreateStore(B.getInt8(0), Ptr);
1772
1773      return ConstantInt::get(CI->getType(), 1);
1774    }
1775
1776    if (FormatStr[1] == 's') {
1777      // These optimizations require DataLayout.
1778      if (!DL) return 0;
1779
1780      // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1781      if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
1782
1783      Value *Len = EmitStrLen(CI->getArgOperand(2), B, DL, TLI);
1784      if (!Len)
1785        return 0;
1786      Value *IncLen = B.CreateAdd(Len,
1787                                  ConstantInt::get(Len->getType(), 1),
1788                                  "leninc");
1789      B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
1790
1791      // The sprintf result is the unincremented number of bytes in the string.
1792      return B.CreateIntCast(Len, CI->getType(), false);
1793    }
1794    return 0;
1795  }
1796
1797  Value *callOptimizer(Function *Callee, CallInst *CI,
1798                       IRBuilder<> &B) override {
1799    // Require two fixed pointer arguments and an integer result.
1800    FunctionType *FT = Callee->getFunctionType();
1801    if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1802        !FT->getParamType(1)->isPointerTy() ||
1803        !FT->getReturnType()->isIntegerTy())
1804      return 0;
1805
1806    if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
1807      return V;
1808    }
1809
1810    // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
1811    // point arguments.
1812    if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) {
1813      Module *M = B.GetInsertBlock()->getParent()->getParent();
1814      Constant *SIPrintFFn =
1815        M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
1816      CallInst *New = cast<CallInst>(CI->clone());
1817      New->setCalledFunction(SIPrintFFn);
1818      B.Insert(New);
1819      return New;
1820    }
1821    return 0;
1822  }
1823};
1824
1825struct FPrintFOpt : public LibCallOptimization {
1826  Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,
1827                                   IRBuilder<> &B) {
1828    ErrorReportingOpt ER(/* StreamArg = */ 0);
1829    (void) ER.callOptimizer(Callee, CI, B);
1830
1831    // All the optimizations depend on the format string.
1832    StringRef FormatStr;
1833    if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1834      return 0;
1835
1836    // Do not do any of the following transformations if the fprintf return
1837    // value is used, in general the fprintf return value is not compatible
1838    // with fwrite(), fputc() or fputs().
1839    if (!CI->use_empty())
1840      return 0;
1841
1842    // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1843    if (CI->getNumArgOperands() == 2) {
1844      for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1845        if (FormatStr[i] == '%')  // Could handle %% -> % if we cared.
1846          return 0; // We found a format specifier.
1847
1848      // These optimizations require DataLayout.
1849      if (!DL) return 0;
1850
1851      return EmitFWrite(CI->getArgOperand(1),
1852                        ConstantInt::get(DL->getIntPtrType(*Context),
1853                                         FormatStr.size()),
1854                        CI->getArgOperand(0), B, DL, TLI);
1855    }
1856
1857    // The remaining optimizations require the format string to be "%s" or "%c"
1858    // and have an extra operand.
1859    if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1860        CI->getNumArgOperands() < 3)
1861      return 0;
1862
1863    // Decode the second character of the format string.
1864    if (FormatStr[1] == 'c') {
1865      // fprintf(F, "%c", chr) --> fputc(chr, F)
1866      if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1867      return EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, DL, TLI);
1868    }
1869
1870    if (FormatStr[1] == 's') {
1871      // fprintf(F, "%s", str) --> fputs(str, F)
1872      if (!CI->getArgOperand(2)->getType()->isPointerTy())
1873        return 0;
1874      return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, DL, TLI);
1875    }
1876    return 0;
1877  }
1878
1879  Value *callOptimizer(Function *Callee, CallInst *CI,
1880                       IRBuilder<> &B) override {
1881    // Require two fixed paramters as pointers and integer result.
1882    FunctionType *FT = Callee->getFunctionType();
1883    if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1884        !FT->getParamType(1)->isPointerTy() ||
1885        !FT->getReturnType()->isIntegerTy())
1886      return 0;
1887
1888    if (Value *V = optimizeFixedFormatString(Callee, CI, B)) {
1889      return V;
1890    }
1891
1892    // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
1893    // floating point arguments.
1894    if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) {
1895      Module *M = B.GetInsertBlock()->getParent()->getParent();
1896      Constant *FIPrintFFn =
1897        M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
1898      CallInst *New = cast<CallInst>(CI->clone());
1899      New->setCalledFunction(FIPrintFFn);
1900      B.Insert(New);
1901      return New;
1902    }
1903    return 0;
1904  }
1905};
1906
1907struct FWriteOpt : public LibCallOptimization {
1908  Value *callOptimizer(Function *Callee, CallInst *CI,
1909                       IRBuilder<> &B) override {
1910    ErrorReportingOpt ER(/* StreamArg = */ 3);
1911    (void) ER.callOptimizer(Callee, CI, B);
1912
1913    // Require a pointer, an integer, an integer, a pointer, returning integer.
1914    FunctionType *FT = Callee->getFunctionType();
1915    if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1916        !FT->getParamType(1)->isIntegerTy() ||
1917        !FT->getParamType(2)->isIntegerTy() ||
1918        !FT->getParamType(3)->isPointerTy() ||
1919        !FT->getReturnType()->isIntegerTy())
1920      return 0;
1921
1922    // Get the element size and count.
1923    ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1924    ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1925    if (!SizeC || !CountC) return 0;
1926    uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
1927
1928    // If this is writing zero records, remove the call (it's a noop).
1929    if (Bytes == 0)
1930      return ConstantInt::get(CI->getType(), 0);
1931
1932    // If this is writing one byte, turn it into fputc.
1933    // This optimisation is only valid, if the return value is unused.
1934    if (Bytes == 1 && CI->use_empty()) {  // fwrite(S,1,1,F) -> fputc(S[0],F)
1935      Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1936      Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, DL, TLI);
1937      return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
1938    }
1939
1940    return 0;
1941  }
1942};
1943
1944struct FPutsOpt : public LibCallOptimization {
1945  Value *callOptimizer(Function *Callee, CallInst *CI,
1946                       IRBuilder<> &B) override {
1947    ErrorReportingOpt ER(/* StreamArg = */ 1);
1948    (void) ER.callOptimizer(Callee, CI, B);
1949
1950    // These optimizations require DataLayout.
1951    if (!DL) return 0;
1952
1953    // Require two pointers.  Also, we can't optimize if return value is used.
1954    FunctionType *FT = Callee->getFunctionType();
1955    if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1956        !FT->getParamType(1)->isPointerTy() ||
1957        !CI->use_empty())
1958      return 0;
1959
1960    // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1961    uint64_t Len = GetStringLength(CI->getArgOperand(0));
1962    if (!Len) return 0;
1963    // Known to have no uses (see above).
1964    return EmitFWrite(CI->getArgOperand(0),
1965                      ConstantInt::get(DL->getIntPtrType(*Context), Len-1),
1966                      CI->getArgOperand(1), B, DL, TLI);
1967  }
1968};
1969
1970struct PutsOpt : public LibCallOptimization {
1971  Value *callOptimizer(Function *Callee, CallInst *CI,
1972                       IRBuilder<> &B) override {
1973    // Require one fixed pointer argument and an integer/void result.
1974    FunctionType *FT = Callee->getFunctionType();
1975    if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1976        !(FT->getReturnType()->isIntegerTy() ||
1977          FT->getReturnType()->isVoidTy()))
1978      return 0;
1979
1980    // Check for a constant string.
1981    StringRef Str;
1982    if (!getConstantStringInfo(CI->getArgOperand(0), Str))
1983      return 0;
1984
1985    if (Str.empty() && CI->use_empty()) {
1986      // puts("") -> putchar('\n')
1987      Value *Res = EmitPutChar(B.getInt32('\n'), B, DL, TLI);
1988      if (CI->use_empty() || !Res) return Res;
1989      return B.CreateIntCast(Res, CI->getType(), true);
1990    }
1991
1992    return 0;
1993  }
1994};
1995
1996} // End anonymous namespace.
1997
1998namespace llvm {
1999
2000class LibCallSimplifierImpl {
2001  const DataLayout *DL;
2002  const TargetLibraryInfo *TLI;
2003  const LibCallSimplifier *LCS;
2004  bool UnsafeFPShrink;
2005
2006  // Math library call optimizations.
2007  CosOpt Cos;
2008  PowOpt Pow;
2009  Exp2Opt Exp2;
2010public:
2011  LibCallSimplifierImpl(const DataLayout *DL, const TargetLibraryInfo *TLI,
2012                        const LibCallSimplifier *LCS,
2013                        bool UnsafeFPShrink = false)
2014    : Cos(UnsafeFPShrink), Pow(UnsafeFPShrink), Exp2(UnsafeFPShrink) {
2015    this->DL = DL;
2016    this->TLI = TLI;
2017    this->LCS = LCS;
2018    this->UnsafeFPShrink = UnsafeFPShrink;
2019  }
2020
2021  Value *optimizeCall(CallInst *CI);
2022  LibCallOptimization *lookupOptimization(CallInst *CI);
2023  bool hasFloatVersion(StringRef FuncName);
2024};
2025
2026bool LibCallSimplifierImpl::hasFloatVersion(StringRef FuncName) {
2027  LibFunc::Func Func;
2028  SmallString<20> FloatFuncName = FuncName;
2029  FloatFuncName += 'f';
2030  if (TLI->getLibFunc(FloatFuncName, Func))
2031    return TLI->has(Func);
2032  return false;
2033}
2034
2035// Fortified library call optimizations.
2036static MemCpyChkOpt MemCpyChk;
2037static MemMoveChkOpt MemMoveChk;
2038static MemSetChkOpt MemSetChk;
2039static StrCpyChkOpt StrCpyChk;
2040static StpCpyChkOpt StpCpyChk;
2041static StrNCpyChkOpt StrNCpyChk;
2042
2043// String library call optimizations.
2044static StrCatOpt StrCat;
2045static StrNCatOpt StrNCat;
2046static StrChrOpt StrChr;
2047static StrRChrOpt StrRChr;
2048static StrCmpOpt StrCmp;
2049static StrNCmpOpt StrNCmp;
2050static StrCpyOpt StrCpy;
2051static StpCpyOpt StpCpy;
2052static StrNCpyOpt StrNCpy;
2053static StrLenOpt StrLen;
2054static StrPBrkOpt StrPBrk;
2055static StrToOpt StrTo;
2056static StrSpnOpt StrSpn;
2057static StrCSpnOpt StrCSpn;
2058static StrStrOpt StrStr;
2059
2060// Memory library call optimizations.
2061static MemCmpOpt MemCmp;
2062static MemCpyOpt MemCpy;
2063static MemMoveOpt MemMove;
2064static MemSetOpt MemSet;
2065
2066// Math library call optimizations.
2067static UnaryDoubleFPOpt UnaryDoubleFP(false);
2068static BinaryDoubleFPOpt BinaryDoubleFP(false);
2069static UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
2070static SinCosPiOpt SinCosPi;
2071
2072  // Integer library call optimizations.
2073static FFSOpt FFS;
2074static AbsOpt Abs;
2075static IsDigitOpt IsDigit;
2076static IsAsciiOpt IsAscii;
2077static ToAsciiOpt ToAscii;
2078
2079// Formatting and IO library call optimizations.
2080static ErrorReportingOpt ErrorReporting;
2081static ErrorReportingOpt ErrorReporting0(0);
2082static ErrorReportingOpt ErrorReporting1(1);
2083static PrintFOpt PrintF;
2084static SPrintFOpt SPrintF;
2085static FPrintFOpt FPrintF;
2086static FWriteOpt FWrite;
2087static FPutsOpt FPuts;
2088static PutsOpt Puts;
2089
2090LibCallOptimization *LibCallSimplifierImpl::lookupOptimization(CallInst *CI) {
2091  LibFunc::Func Func;
2092  Function *Callee = CI->getCalledFunction();
2093  StringRef FuncName = Callee->getName();
2094
2095  // Next check for intrinsics.
2096  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
2097    switch (II->getIntrinsicID()) {
2098    case Intrinsic::pow:
2099       return &Pow;
2100    case Intrinsic::exp2:
2101       return &Exp2;
2102    default:
2103       return 0;
2104    }
2105  }
2106
2107  // Then check for known library functions.
2108  if (TLI->getLibFunc(FuncName, Func) && TLI->has(Func)) {
2109    switch (Func) {
2110      case LibFunc::strcat:
2111        return &StrCat;
2112      case LibFunc::strncat:
2113        return &StrNCat;
2114      case LibFunc::strchr:
2115        return &StrChr;
2116      case LibFunc::strrchr:
2117        return &StrRChr;
2118      case LibFunc::strcmp:
2119        return &StrCmp;
2120      case LibFunc::strncmp:
2121        return &StrNCmp;
2122      case LibFunc::strcpy:
2123        return &StrCpy;
2124      case LibFunc::stpcpy:
2125        return &StpCpy;
2126      case LibFunc::strncpy:
2127        return &StrNCpy;
2128      case LibFunc::strlen:
2129        return &StrLen;
2130      case LibFunc::strpbrk:
2131        return &StrPBrk;
2132      case LibFunc::strtol:
2133      case LibFunc::strtod:
2134      case LibFunc::strtof:
2135      case LibFunc::strtoul:
2136      case LibFunc::strtoll:
2137      case LibFunc::strtold:
2138      case LibFunc::strtoull:
2139        return &StrTo;
2140      case LibFunc::strspn:
2141        return &StrSpn;
2142      case LibFunc::strcspn:
2143        return &StrCSpn;
2144      case LibFunc::strstr:
2145        return &StrStr;
2146      case LibFunc::memcmp:
2147        return &MemCmp;
2148      case LibFunc::memcpy:
2149        return &MemCpy;
2150      case LibFunc::memmove:
2151        return &MemMove;
2152      case LibFunc::memset:
2153        return &MemSet;
2154      case LibFunc::cosf:
2155      case LibFunc::cos:
2156      case LibFunc::cosl:
2157        return &Cos;
2158      case LibFunc::sinpif:
2159      case LibFunc::sinpi:
2160      case LibFunc::cospif:
2161      case LibFunc::cospi:
2162        return &SinCosPi;
2163      case LibFunc::powf:
2164      case LibFunc::pow:
2165      case LibFunc::powl:
2166        return &Pow;
2167      case LibFunc::exp2l:
2168      case LibFunc::exp2:
2169      case LibFunc::exp2f:
2170        return &Exp2;
2171      case LibFunc::ffs:
2172      case LibFunc::ffsl:
2173      case LibFunc::ffsll:
2174        return &FFS;
2175      case LibFunc::abs:
2176      case LibFunc::labs:
2177      case LibFunc::llabs:
2178        return &Abs;
2179      case LibFunc::isdigit:
2180        return &IsDigit;
2181      case LibFunc::isascii:
2182        return &IsAscii;
2183      case LibFunc::toascii:
2184        return &ToAscii;
2185      case LibFunc::printf:
2186        return &PrintF;
2187      case LibFunc::sprintf:
2188        return &SPrintF;
2189      case LibFunc::fprintf:
2190        return &FPrintF;
2191      case LibFunc::fwrite:
2192        return &FWrite;
2193      case LibFunc::fputs:
2194        return &FPuts;
2195      case LibFunc::puts:
2196        return &Puts;
2197      case LibFunc::perror:
2198        return &ErrorReporting;
2199      case LibFunc::vfprintf:
2200      case LibFunc::fiprintf:
2201        return &ErrorReporting0;
2202      case LibFunc::fputc:
2203        return &ErrorReporting1;
2204      case LibFunc::ceil:
2205      case LibFunc::fabs:
2206      case LibFunc::floor:
2207      case LibFunc::rint:
2208      case LibFunc::round:
2209      case LibFunc::nearbyint:
2210      case LibFunc::trunc:
2211        if (hasFloatVersion(FuncName))
2212          return &UnaryDoubleFP;
2213        return 0;
2214      case LibFunc::acos:
2215      case LibFunc::acosh:
2216      case LibFunc::asin:
2217      case LibFunc::asinh:
2218      case LibFunc::atan:
2219      case LibFunc::atanh:
2220      case LibFunc::cbrt:
2221      case LibFunc::cosh:
2222      case LibFunc::exp:
2223      case LibFunc::exp10:
2224      case LibFunc::expm1:
2225      case LibFunc::log:
2226      case LibFunc::log10:
2227      case LibFunc::log1p:
2228      case LibFunc::log2:
2229      case LibFunc::logb:
2230      case LibFunc::sin:
2231      case LibFunc::sinh:
2232      case LibFunc::sqrt:
2233      case LibFunc::tan:
2234      case LibFunc::tanh:
2235        if (UnsafeFPShrink && hasFloatVersion(FuncName))
2236         return &UnsafeUnaryDoubleFP;
2237        return 0;
2238      case LibFunc::fmin:
2239      case LibFunc::fmax:
2240        if (hasFloatVersion(FuncName))
2241          return &BinaryDoubleFP;
2242        return 0;
2243      case LibFunc::memcpy_chk:
2244        return &MemCpyChk;
2245      default:
2246        return 0;
2247      }
2248  }
2249
2250  // Finally check for fortified library calls.
2251  if (FuncName.endswith("_chk")) {
2252    if (FuncName == "__memmove_chk")
2253      return &MemMoveChk;
2254    else if (FuncName == "__memset_chk")
2255      return &MemSetChk;
2256    else if (FuncName == "__strcpy_chk")
2257      return &StrCpyChk;
2258    else if (FuncName == "__stpcpy_chk")
2259      return &StpCpyChk;
2260    else if (FuncName == "__strncpy_chk")
2261      return &StrNCpyChk;
2262    else if (FuncName == "__stpncpy_chk")
2263      return &StrNCpyChk;
2264  }
2265
2266  return 0;
2267
2268}
2269
2270Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) {
2271  LibCallOptimization *LCO = lookupOptimization(CI);
2272  if (LCO) {
2273    IRBuilder<> Builder(CI);
2274    return LCO->optimizeCall(CI, DL, TLI, LCS, Builder);
2275  }
2276  return 0;
2277}
2278
2279LibCallSimplifier::LibCallSimplifier(const DataLayout *DL,
2280                                     const TargetLibraryInfo *TLI,
2281                                     bool UnsafeFPShrink) {
2282  Impl = new LibCallSimplifierImpl(DL, TLI, this, UnsafeFPShrink);
2283}
2284
2285LibCallSimplifier::~LibCallSimplifier() {
2286  delete Impl;
2287}
2288
2289Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
2290  if (CI->isNoBuiltin()) return 0;
2291  return Impl->optimizeCall(CI);
2292}
2293
2294void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) const {
2295  I->replaceAllUsesWith(With);
2296  I->eraseFromParent();
2297}
2298
2299}
2300
2301// TODO:
2302//   Additional cases that we need to add to this file:
2303//
2304// cbrt:
2305//   * cbrt(expN(X))  -> expN(x/3)
2306//   * cbrt(sqrt(x))  -> pow(x,1/6)
2307//   * cbrt(sqrt(x))  -> pow(x,1/9)
2308//
2309// exp, expf, expl:
2310//   * exp(log(x))  -> x
2311//
2312// log, logf, logl:
2313//   * log(exp(x))   -> x
2314//   * log(x**y)     -> y*log(x)
2315//   * log(exp(y))   -> y*log(e)
2316//   * log(exp2(y))  -> y*log(2)
2317//   * log(exp10(y)) -> y*log(10)
2318//   * log(sqrt(x))  -> 0.5*log(x)
2319//   * log(pow(x,y)) -> y*log(x)
2320//
2321// lround, lroundf, lroundl:
2322//   * lround(cnst) -> cnst'
2323//
2324// pow, powf, powl:
2325//   * pow(exp(x),y)  -> exp(x*y)
2326//   * pow(sqrt(x),y) -> pow(x,y*0.5)
2327//   * pow(pow(x,y),z)-> pow(x,y*z)
2328//
2329// round, roundf, roundl:
2330//   * round(cnst) -> cnst'
2331//
2332// signbit:
2333//   * signbit(cnst) -> cnst'
2334//   * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2335//
2336// sqrt, sqrtf, sqrtl:
2337//   * sqrt(expN(x))  -> expN(x*0.5)
2338//   * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2339//   * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2340//
2341// tan, tanf, tanl:
2342//   * tan(atan(x)) -> x
2343//
2344// trunc, truncf, truncl:
2345//   * trunc(cnst) -> cnst'
2346//
2347//
2348