1 //===------ SimplifyLibCalls.cpp - Library calls simplifier ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the library calls simplifier. It does not implement
10 // any pass, but can't be used by other passes to do simplifications.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/Utils/SimplifyLibCalls.h"
15 #include "llvm/ADT/APSInt.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/Analysis/ConstantFolding.h"
19 #include "llvm/Analysis/Loads.h"
20 #include "llvm/Analysis/OptimizationRemarkEmitter.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/Module.h"
28 #include "llvm/IR/PatternMatch.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/KnownBits.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/Transforms/Utils/BuildLibCalls.h"
33 #include "llvm/Transforms/Utils/Local.h"
34 #include "llvm/Transforms/Utils/SizeOpts.h"
35 
36 using namespace llvm;
37 using namespace PatternMatch;
38 
39 static cl::opt<bool>
40     EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden,
41                          cl::init(false),
42                          cl::desc("Enable unsafe double to float "
43                                   "shrinking for math lib calls"));
44 
45 //===----------------------------------------------------------------------===//
46 // Helper Functions
47 //===----------------------------------------------------------------------===//
48 
49 static bool ignoreCallingConv(LibFunc Func) {
50   return Func == LibFunc_abs || Func == LibFunc_labs ||
51          Func == LibFunc_llabs || Func == LibFunc_strlen;
52 }
53 
54 /// Return true if it is only used in equality comparisons with With.
55 static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
56   for (User *U : V->users()) {
57     if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
58       if (IC->isEquality() && IC->getOperand(1) == With)
59         continue;
60     // Unknown instruction.
61     return false;
62   }
63   return true;
64 }
65 
66 static bool callHasFloatingPointArgument(const CallInst *CI) {
67   return any_of(CI->operands(), [](const Use &OI) {
68     return OI->getType()->isFloatingPointTy();
69   });
70 }
71 
72 static bool callHasFP128Argument(const CallInst *CI) {
73   return any_of(CI->operands(), [](const Use &OI) {
74     return OI->getType()->isFP128Ty();
75   });
76 }
77 
78 static Value *convertStrToNumber(CallInst *CI, StringRef &Str, int64_t Base) {
79   if (Base < 2 || Base > 36)
80     // handle special zero base
81     if (Base != 0)
82       return nullptr;
83 
84   char *End;
85   std::string nptr = Str.str();
86   errno = 0;
87   long long int Result = strtoll(nptr.c_str(), &End, Base);
88   if (errno)
89     return nullptr;
90 
91   // if we assume all possible target locales are ASCII supersets,
92   // then if strtoll successfully parses a number on the host,
93   // it will also successfully parse the same way on the target
94   if (*End != '\0')
95     return nullptr;
96 
97   if (!isIntN(CI->getType()->getPrimitiveSizeInBits(), Result))
98     return nullptr;
99 
100   return ConstantInt::get(CI->getType(), Result);
101 }
102 
103 static bool isOnlyUsedInComparisonWithZero(Value *V) {
104   for (User *U : V->users()) {
105     if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
106       if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
107         if (C->isNullValue())
108           continue;
109     // Unknown instruction.
110     return false;
111   }
112   return true;
113 }
114 
115 static bool canTransformToMemCmp(CallInst *CI, Value *Str, uint64_t Len,
116                                  const DataLayout &DL) {
117   if (!isOnlyUsedInComparisonWithZero(CI))
118     return false;
119 
120   if (!isDereferenceableAndAlignedPointer(Str, Align(1), APInt(64, Len), DL))
121     return false;
122 
123   if (CI->getFunction()->hasFnAttribute(Attribute::SanitizeMemory))
124     return false;
125 
126   return true;
127 }
128 
129 static void annotateDereferenceableBytes(CallInst *CI,
130                                          ArrayRef<unsigned> ArgNos,
131                                          uint64_t DereferenceableBytes) {
132   const Function *F = CI->getCaller();
133   if (!F)
134     return;
135   for (unsigned ArgNo : ArgNos) {
136     uint64_t DerefBytes = DereferenceableBytes;
137     unsigned AS = CI->getArgOperand(ArgNo)->getType()->getPointerAddressSpace();
138     if (!llvm::NullPointerIsDefined(F, AS) ||
139         CI->paramHasAttr(ArgNo, Attribute::NonNull))
140       DerefBytes = std::max(CI->getParamDereferenceableOrNullBytes(ArgNo),
141                             DereferenceableBytes);
142 
143     if (CI->getParamDereferenceableBytes(ArgNo) < DerefBytes) {
144       CI->removeParamAttr(ArgNo, Attribute::Dereferenceable);
145       if (!llvm::NullPointerIsDefined(F, AS) ||
146           CI->paramHasAttr(ArgNo, Attribute::NonNull))
147         CI->removeParamAttr(ArgNo, Attribute::DereferenceableOrNull);
148       CI->addParamAttr(ArgNo, Attribute::getWithDereferenceableBytes(
149                                   CI->getContext(), DerefBytes));
150     }
151   }
152 }
153 
154 static void annotateNonNullNoUndefBasedOnAccess(CallInst *CI,
155                                          ArrayRef<unsigned> ArgNos) {
156   Function *F = CI->getCaller();
157   if (!F)
158     return;
159 
160   for (unsigned ArgNo : ArgNos) {
161     if (!CI->paramHasAttr(ArgNo, Attribute::NoUndef))
162       CI->addParamAttr(ArgNo, Attribute::NoUndef);
163 
164     if (CI->paramHasAttr(ArgNo, Attribute::NonNull))
165       continue;
166     unsigned AS = CI->getArgOperand(ArgNo)->getType()->getPointerAddressSpace();
167     if (llvm::NullPointerIsDefined(F, AS))
168       continue;
169 
170     CI->addParamAttr(ArgNo, Attribute::NonNull);
171     annotateDereferenceableBytes(CI, ArgNo, 1);
172   }
173 }
174 
175 static void annotateNonNullAndDereferenceable(CallInst *CI, ArrayRef<unsigned> ArgNos,
176                                Value *Size, const DataLayout &DL) {
177   if (ConstantInt *LenC = dyn_cast<ConstantInt>(Size)) {
178     annotateNonNullNoUndefBasedOnAccess(CI, ArgNos);
179     annotateDereferenceableBytes(CI, ArgNos, LenC->getZExtValue());
180   } else if (isKnownNonZero(Size, DL)) {
181     annotateNonNullNoUndefBasedOnAccess(CI, ArgNos);
182     const APInt *X, *Y;
183     uint64_t DerefMin = 1;
184     if (match(Size, m_Select(m_Value(), m_APInt(X), m_APInt(Y)))) {
185       DerefMin = std::min(X->getZExtValue(), Y->getZExtValue());
186       annotateDereferenceableBytes(CI, ArgNos, DerefMin);
187     }
188   }
189 }
190 
191 // Copy CallInst "flags" like musttail, notail, and tail. Return New param for
192 // easier chaining. Calls to emit* and B.createCall should probably be wrapped
193 // in this function when New is created to replace Old. Callers should take
194 // care to check Old.isMustTailCall() if they aren't replacing Old directly
195 // with New.
196 static Value *copyFlags(const CallInst &Old, Value *New) {
197   assert(!Old.isMustTailCall() && "do not copy musttail call flags");
198   assert(!Old.isNoTailCall() && "do not copy notail call flags");
199   if (auto *NewCI = dyn_cast_or_null<CallInst>(New))
200     NewCI->setTailCallKind(Old.getTailCallKind());
201   return New;
202 }
203 
204 //===----------------------------------------------------------------------===//
205 // String and Memory Library Call Optimizations
206 //===----------------------------------------------------------------------===//
207 
208 Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilderBase &B) {
209   // Extract some information from the instruction
210   Value *Dst = CI->getArgOperand(0);
211   Value *Src = CI->getArgOperand(1);
212   annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
213 
214   // See if we can get the length of the input string.
215   uint64_t Len = GetStringLength(Src);
216   if (Len)
217     annotateDereferenceableBytes(CI, 1, Len);
218   else
219     return nullptr;
220   --Len; // Unbias length.
221 
222   // Handle the simple, do-nothing case: strcat(x, "") -> x
223   if (Len == 0)
224     return Dst;
225 
226   return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, Len, B));
227 }
228 
229 Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
230                                            IRBuilderBase &B) {
231   // We need to find the end of the destination string.  That's where the
232   // memory is to be moved to. We just generate a call to strlen.
233   Value *DstLen = emitStrLen(Dst, B, DL, TLI);
234   if (!DstLen)
235     return nullptr;
236 
237   // Now that we have the destination's length, we must index into the
238   // destination's pointer to get the actual memcpy destination (end of
239   // the string .. we're concatenating).
240   Value *CpyDst = B.CreateGEP(B.getInt8Ty(), Dst, DstLen, "endptr");
241 
242   // We have enough information to now generate the memcpy call to do the
243   // concatenation for us.  Make a memcpy to copy the nul byte with align = 1.
244   B.CreateMemCpy(
245       CpyDst, Align(1), Src, Align(1),
246       ConstantInt::get(DL.getIntPtrType(Src->getContext()), Len + 1));
247   return Dst;
248 }
249 
250 Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilderBase &B) {
251   // Extract some information from the instruction.
252   Value *Dst = CI->getArgOperand(0);
253   Value *Src = CI->getArgOperand(1);
254   Value *Size = CI->getArgOperand(2);
255   uint64_t Len;
256   annotateNonNullNoUndefBasedOnAccess(CI, 0);
257   if (isKnownNonZero(Size, DL))
258     annotateNonNullNoUndefBasedOnAccess(CI, 1);
259 
260   // We don't do anything if length is not constant.
261   ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size);
262   if (LengthArg) {
263     Len = LengthArg->getZExtValue();
264     // strncat(x, c, 0) -> x
265     if (!Len)
266       return Dst;
267   } else {
268     return nullptr;
269   }
270 
271   // See if we can get the length of the input string.
272   uint64_t SrcLen = GetStringLength(Src);
273   if (SrcLen) {
274     annotateDereferenceableBytes(CI, 1, SrcLen);
275     --SrcLen; // Unbias length.
276   } else {
277     return nullptr;
278   }
279 
280   // strncat(x, "", c) -> x
281   if (SrcLen == 0)
282     return Dst;
283 
284   // We don't optimize this case.
285   if (Len < SrcLen)
286     return nullptr;
287 
288   // strncat(x, s, c) -> strcat(x, s)
289   // s is constant so the strcat can be optimized further.
290   return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, SrcLen, B));
291 }
292 
293 Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilderBase &B) {
294   Function *Callee = CI->getCalledFunction();
295   FunctionType *FT = Callee->getFunctionType();
296   Value *SrcStr = CI->getArgOperand(0);
297   annotateNonNullNoUndefBasedOnAccess(CI, 0);
298 
299   // If the second operand is non-constant, see if we can compute the length
300   // of the input string and turn this into memchr.
301   ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
302   if (!CharC) {
303     uint64_t Len = GetStringLength(SrcStr);
304     if (Len)
305       annotateDereferenceableBytes(CI, 0, Len);
306     else
307       return nullptr;
308     if (!FT->getParamType(1)->isIntegerTy(32)) // memchr needs i32.
309       return nullptr;
310 
311     return copyFlags(
312         *CI,
313         emitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
314                    ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len), B,
315                    DL, TLI));
316   }
317 
318   // Otherwise, the character is a constant, see if the first argument is
319   // a string literal.  If so, we can constant fold.
320   StringRef Str;
321   if (!getConstantStringInfo(SrcStr, Str)) {
322     if (CharC->isZero()) // strchr(p, 0) -> p + strlen(p)
323       if (Value *StrLen = emitStrLen(SrcStr, B, DL, TLI))
324         return B.CreateGEP(B.getInt8Ty(), SrcStr, StrLen, "strchr");
325     return nullptr;
326   }
327 
328   // Compute the offset, make sure to handle the case when we're searching for
329   // zero (a weird way to spell strlen).
330   size_t I = (0xFF & CharC->getSExtValue()) == 0
331                  ? Str.size()
332                  : Str.find(CharC->getSExtValue());
333   if (I == StringRef::npos) // Didn't find the char.  strchr returns null.
334     return Constant::getNullValue(CI->getType());
335 
336   // strchr(s+n,c)  -> gep(s+n+i,c)
337   return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strchr");
338 }
339 
340 Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilderBase &B) {
341   Value *SrcStr = CI->getArgOperand(0);
342   ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
343   annotateNonNullNoUndefBasedOnAccess(CI, 0);
344 
345   // Cannot fold anything if we're not looking for a constant.
346   if (!CharC)
347     return nullptr;
348 
349   StringRef Str;
350   if (!getConstantStringInfo(SrcStr, Str)) {
351     // strrchr(s, 0) -> strchr(s, 0)
352     if (CharC->isZero())
353       return copyFlags(*CI, emitStrChr(SrcStr, '\0', B, TLI));
354     return nullptr;
355   }
356 
357   // Compute the offset.
358   size_t I = (0xFF & CharC->getSExtValue()) == 0
359                  ? Str.size()
360                  : Str.rfind(CharC->getSExtValue());
361   if (I == StringRef::npos) // Didn't find the char. Return null.
362     return Constant::getNullValue(CI->getType());
363 
364   // strrchr(s+n,c) -> gep(s+n+i,c)
365   return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strrchr");
366 }
367 
368 Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilderBase &B) {
369   Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
370   if (Str1P == Str2P) // strcmp(x,x)  -> 0
371     return ConstantInt::get(CI->getType(), 0);
372 
373   StringRef Str1, Str2;
374   bool HasStr1 = getConstantStringInfo(Str1P, Str1);
375   bool HasStr2 = getConstantStringInfo(Str2P, Str2);
376 
377   // strcmp(x, y)  -> cnst  (if both x and y are constant strings)
378   if (HasStr1 && HasStr2)
379     return ConstantInt::get(CI->getType(), Str1.compare(Str2));
380 
381   if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
382     return B.CreateNeg(B.CreateZExt(
383         B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
384 
385   if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
386     return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"),
387                         CI->getType());
388 
389   // strcmp(P, "x") -> memcmp(P, "x", 2)
390   uint64_t Len1 = GetStringLength(Str1P);
391   if (Len1)
392     annotateDereferenceableBytes(CI, 0, Len1);
393   uint64_t Len2 = GetStringLength(Str2P);
394   if (Len2)
395     annotateDereferenceableBytes(CI, 1, Len2);
396 
397   if (Len1 && Len2) {
398     return copyFlags(
399         *CI, emitMemCmp(Str1P, Str2P,
400                         ConstantInt::get(DL.getIntPtrType(CI->getContext()),
401                                          std::min(Len1, Len2)),
402                         B, DL, TLI));
403   }
404 
405   // strcmp to memcmp
406   if (!HasStr1 && HasStr2) {
407     if (canTransformToMemCmp(CI, Str1P, Len2, DL))
408       return copyFlags(
409           *CI,
410           emitMemCmp(Str1P, Str2P,
411                      ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len2),
412                      B, DL, TLI));
413   } else if (HasStr1 && !HasStr2) {
414     if (canTransformToMemCmp(CI, Str2P, Len1, DL))
415       return copyFlags(
416           *CI,
417           emitMemCmp(Str1P, Str2P,
418                      ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len1),
419                      B, DL, TLI));
420   }
421 
422   annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
423   return nullptr;
424 }
425 
426 Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilderBase &B) {
427   Value *Str1P = CI->getArgOperand(0);
428   Value *Str2P = CI->getArgOperand(1);
429   Value *Size = CI->getArgOperand(2);
430   if (Str1P == Str2P) // strncmp(x,x,n)  -> 0
431     return ConstantInt::get(CI->getType(), 0);
432 
433   if (isKnownNonZero(Size, DL))
434     annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
435   // Get the length argument if it is constant.
436   uint64_t Length;
437   if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size))
438     Length = LengthArg->getZExtValue();
439   else
440     return nullptr;
441 
442   if (Length == 0) // strncmp(x,y,0)   -> 0
443     return ConstantInt::get(CI->getType(), 0);
444 
445   if (Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
446     return copyFlags(*CI, emitMemCmp(Str1P, Str2P, Size, B, DL, TLI));
447 
448   StringRef Str1, Str2;
449   bool HasStr1 = getConstantStringInfo(Str1P, Str1);
450   bool HasStr2 = getConstantStringInfo(Str2P, Str2);
451 
452   // strncmp(x, y)  -> cnst  (if both x and y are constant strings)
453   if (HasStr1 && HasStr2) {
454     // Avoid truncating the 64-bit Length to 32 bits in ILP32.
455     StringRef::size_type MinLen1 = std::min((uint64_t)Str1.size(), Length);
456     StringRef::size_type MinLen2 = std::min((uint64_t)Str2.size(), Length);
457     StringRef SubStr1 = Str1.substr(0, MinLen1);
458     StringRef SubStr2 = Str2.substr(0, MinLen2);
459     return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
460   }
461 
462   if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
463     return B.CreateNeg(B.CreateZExt(
464         B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
465 
466   if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
467     return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"),
468                         CI->getType());
469 
470   uint64_t Len1 = GetStringLength(Str1P);
471   if (Len1)
472     annotateDereferenceableBytes(CI, 0, Len1);
473   uint64_t Len2 = GetStringLength(Str2P);
474   if (Len2)
475     annotateDereferenceableBytes(CI, 1, Len2);
476 
477   // strncmp to memcmp
478   if (!HasStr1 && HasStr2) {
479     Len2 = std::min(Len2, Length);
480     if (canTransformToMemCmp(CI, Str1P, Len2, DL))
481       return copyFlags(
482           *CI,
483           emitMemCmp(Str1P, Str2P,
484                      ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len2),
485                      B, DL, TLI));
486   } else if (HasStr1 && !HasStr2) {
487     Len1 = std::min(Len1, Length);
488     if (canTransformToMemCmp(CI, Str2P, Len1, DL))
489       return copyFlags(
490           *CI,
491           emitMemCmp(Str1P, Str2P,
492                      ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len1),
493                      B, DL, TLI));
494   }
495 
496   return nullptr;
497 }
498 
499 Value *LibCallSimplifier::optimizeStrNDup(CallInst *CI, IRBuilderBase &B) {
500   Value *Src = CI->getArgOperand(0);
501   ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
502   uint64_t SrcLen = GetStringLength(Src);
503   if (SrcLen && Size) {
504     annotateDereferenceableBytes(CI, 0, SrcLen);
505     if (SrcLen <= Size->getZExtValue() + 1)
506       return copyFlags(*CI, emitStrDup(Src, B, TLI));
507   }
508 
509   return nullptr;
510 }
511 
512 Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilderBase &B) {
513   Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
514   if (Dst == Src) // strcpy(x,x)  -> x
515     return Src;
516 
517   annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
518   // See if we can get the length of the input string.
519   uint64_t Len = GetStringLength(Src);
520   if (Len)
521     annotateDereferenceableBytes(CI, 1, Len);
522   else
523     return nullptr;
524 
525   // We have enough information to now generate the memcpy call to do the
526   // copy for us.  Make a memcpy to copy the nul byte with align = 1.
527   CallInst *NewCI =
528       B.CreateMemCpy(Dst, Align(1), Src, Align(1),
529                      ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len));
530   NewCI->setAttributes(CI->getAttributes());
531   NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
532   copyFlags(*CI, NewCI);
533   return Dst;
534 }
535 
536 Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilderBase &B) {
537   Function *Callee = CI->getCalledFunction();
538   Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
539 
540   // stpcpy(d,s) -> strcpy(d,s) if the result is not used.
541   if (CI->use_empty())
542     return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
543 
544   if (Dst == Src) { // stpcpy(x,x)  -> x+strlen(x)
545     Value *StrLen = emitStrLen(Src, B, DL, TLI);
546     return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
547   }
548 
549   // See if we can get the length of the input string.
550   uint64_t Len = GetStringLength(Src);
551   if (Len)
552     annotateDereferenceableBytes(CI, 1, Len);
553   else
554     return nullptr;
555 
556   Type *PT = Callee->getFunctionType()->getParamType(0);
557   Value *LenV = ConstantInt::get(DL.getIntPtrType(PT), Len);
558   Value *DstEnd = B.CreateGEP(B.getInt8Ty(), Dst,
559                               ConstantInt::get(DL.getIntPtrType(PT), Len - 1));
560 
561   // We have enough information to now generate the memcpy call to do the
562   // copy for us.  Make a memcpy to copy the nul byte with align = 1.
563   CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1), LenV);
564   NewCI->setAttributes(CI->getAttributes());
565   NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
566   copyFlags(*CI, NewCI);
567   return DstEnd;
568 }
569 
570 Value *LibCallSimplifier::optimizeStrNCpy(CallInst *CI, IRBuilderBase &B) {
571   Function *Callee = CI->getCalledFunction();
572   Value *Dst = CI->getArgOperand(0);
573   Value *Src = CI->getArgOperand(1);
574   Value *Size = CI->getArgOperand(2);
575   annotateNonNullNoUndefBasedOnAccess(CI, 0);
576   if (isKnownNonZero(Size, DL))
577     annotateNonNullNoUndefBasedOnAccess(CI, 1);
578 
579   uint64_t Len;
580   if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size))
581     Len = LengthArg->getZExtValue();
582   else
583     return nullptr;
584 
585   // strncpy(x, y, 0) -> x
586   if (Len == 0)
587     return Dst;
588 
589   // See if we can get the length of the input string.
590   uint64_t SrcLen = GetStringLength(Src);
591   if (SrcLen) {
592     annotateDereferenceableBytes(CI, 1, SrcLen);
593     --SrcLen; // Unbias length.
594   } else {
595     return nullptr;
596   }
597 
598   if (SrcLen == 0) {
599     // strncpy(x, "", y) -> memset(x, '\0', y)
600     Align MemSetAlign =
601         CI->getAttributes().getParamAttrs(0).getAlignment().valueOrOne();
602     CallInst *NewCI = B.CreateMemSet(Dst, B.getInt8('\0'), Size, MemSetAlign);
603     AttrBuilder ArgAttrs(CI->getContext(), CI->getAttributes().getParamAttrs(0));
604     NewCI->setAttributes(NewCI->getAttributes().addParamAttributes(
605         CI->getContext(), 0, ArgAttrs));
606     copyFlags(*CI, NewCI);
607     return Dst;
608   }
609 
610   // strncpy(a, "a", 4) - > memcpy(a, "a\0\0\0", 4)
611   if (Len > SrcLen + 1) {
612     if (Len <= 128) {
613       StringRef Str;
614       if (!getConstantStringInfo(Src, Str))
615         return nullptr;
616       std::string SrcStr = Str.str();
617       SrcStr.resize(Len, '\0');
618       Src = B.CreateGlobalString(SrcStr, "str");
619     } else {
620       return nullptr;
621     }
622   }
623 
624   Type *PT = Callee->getFunctionType()->getParamType(0);
625   // strncpy(x, s, c) -> memcpy(align 1 x, align 1 s, c) [s and c are constant]
626   CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1),
627                                    ConstantInt::get(DL.getIntPtrType(PT), Len));
628   NewCI->setAttributes(CI->getAttributes());
629   NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
630   copyFlags(*CI, NewCI);
631   return Dst;
632 }
633 
634 Value *LibCallSimplifier::optimizeStringLength(CallInst *CI, IRBuilderBase &B,
635                                                unsigned CharSize,
636                                                Value *Bound) {
637   Value *Src = CI->getArgOperand(0);
638   Type *CharTy = B.getIntNTy(CharSize);
639 
640   if (isOnlyUsedInZeroEqualityComparison(CI) &&
641       (!Bound || isKnownNonZero(Bound, DL))) {
642     // Fold strlen:
643     //   strlen(x) != 0 --> *x != 0
644     //   strlen(x) == 0 --> *x == 0
645     // and likewise strnlen with constant N > 0:
646     //   strnlen(x, N) != 0 --> *x != 0
647     //   strnlen(x, N) == 0 --> *x == 0
648     return B.CreateZExt(B.CreateLoad(CharTy, Src, "char0"),
649                         CI->getType());
650   }
651 
652   if (Bound) {
653     if (ConstantInt *BoundCst = dyn_cast<ConstantInt>(Bound)) {
654       if (BoundCst->isZero())
655         // Fold strnlen(s, 0) -> 0 for any s, constant or otherwise.
656         return ConstantInt::get(CI->getType(), 0);
657 
658       if (BoundCst->isOne()) {
659         // Fold strnlen(s, 1) -> *s ? 1 : 0 for any s.
660         Value *CharVal = B.CreateLoad(CharTy, Src, "strnlen.char0");
661         Value *ZeroChar = ConstantInt::get(CharTy, 0);
662         Value *Cmp = B.CreateICmpNE(CharVal, ZeroChar, "strnlen.char0cmp");
663         return B.CreateZExt(Cmp, CI->getType());
664       }
665     }
666   }
667 
668   if (uint64_t Len = GetStringLength(Src, CharSize)) {
669     Value *LenC = ConstantInt::get(CI->getType(), Len - 1);
670     // Fold strlen("xyz") -> 3 and strnlen("xyz", 2) -> 2
671     // and strnlen("xyz", Bound) -> min(3, Bound) for nonconstant Bound.
672     if (Bound)
673       return B.CreateBinaryIntrinsic(Intrinsic::umin, LenC, Bound);
674     return LenC;
675   }
676 
677   if (Bound)
678     // Punt for strnlen for now.
679     return nullptr;
680 
681   // If s is a constant pointer pointing to a string literal, we can fold
682   // strlen(s + x) to strlen(s) - x, when x is known to be in the range
683   // [0, strlen(s)] or the string has a single null terminator '\0' at the end.
684   // We only try to simplify strlen when the pointer s points to an array
685   // of i8. Otherwise, we would need to scale the offset x before doing the
686   // subtraction. This will make the optimization more complex, and it's not
687   // very useful because calling strlen for a pointer of other types is
688   // very uncommon.
689   if (GEPOperator *GEP = dyn_cast<GEPOperator>(Src)) {
690     if (!isGEPBasedOnPointerToString(GEP, CharSize))
691       return nullptr;
692 
693     ConstantDataArraySlice Slice;
694     if (getConstantDataArrayInfo(GEP->getOperand(0), Slice, CharSize)) {
695       uint64_t NullTermIdx;
696       if (Slice.Array == nullptr) {
697         NullTermIdx = 0;
698       } else {
699         NullTermIdx = ~((uint64_t)0);
700         for (uint64_t I = 0, E = Slice.Length; I < E; ++I) {
701           if (Slice.Array->getElementAsInteger(I + Slice.Offset) == 0) {
702             NullTermIdx = I;
703             break;
704           }
705         }
706         // If the string does not have '\0', leave it to strlen to compute
707         // its length.
708         if (NullTermIdx == ~((uint64_t)0))
709           return nullptr;
710       }
711 
712       Value *Offset = GEP->getOperand(2);
713       KnownBits Known = computeKnownBits(Offset, DL, 0, nullptr, CI, nullptr);
714       uint64_t ArrSize =
715              cast<ArrayType>(GEP->getSourceElementType())->getNumElements();
716 
717       // If Offset is not provably in the range [0, NullTermIdx], we can still
718       // optimize if we can prove that the program has undefined behavior when
719       // Offset is outside that range. That is the case when GEP->getOperand(0)
720       // is a pointer to an object whose memory extent is NullTermIdx+1.
721       if ((Known.isNonNegative() && Known.getMaxValue().ule(NullTermIdx)) ||
722           (isa<GlobalVariable>(GEP->getOperand(0)) &&
723            NullTermIdx == ArrSize - 1)) {
724         Offset = B.CreateSExtOrTrunc(Offset, CI->getType());
725         return B.CreateSub(ConstantInt::get(CI->getType(), NullTermIdx),
726                            Offset);
727       }
728     }
729   }
730 
731   // strlen(x?"foo":"bars") --> x ? 3 : 4
732   if (SelectInst *SI = dyn_cast<SelectInst>(Src)) {
733     uint64_t LenTrue = GetStringLength(SI->getTrueValue(), CharSize);
734     uint64_t LenFalse = GetStringLength(SI->getFalseValue(), CharSize);
735     if (LenTrue && LenFalse) {
736       ORE.emit([&]() {
737         return OptimizationRemark("instcombine", "simplify-libcalls", CI)
738                << "folded strlen(select) to select of constants";
739       });
740       return B.CreateSelect(SI->getCondition(),
741                             ConstantInt::get(CI->getType(), LenTrue - 1),
742                             ConstantInt::get(CI->getType(), LenFalse - 1));
743     }
744   }
745 
746   return nullptr;
747 }
748 
749 Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilderBase &B) {
750   if (Value *V = optimizeStringLength(CI, B, 8))
751     return V;
752   annotateNonNullNoUndefBasedOnAccess(CI, 0);
753   return nullptr;
754 }
755 
756 Value *LibCallSimplifier::optimizeStrNLen(CallInst *CI, IRBuilderBase &B) {
757   Value *Bound = CI->getArgOperand(1);
758   if (Value *V = optimizeStringLength(CI, B, 8, Bound))
759     return V;
760 
761   if (isKnownNonZero(Bound, DL))
762     annotateNonNullNoUndefBasedOnAccess(CI, 0);
763   return nullptr;
764 }
765 
766 Value *LibCallSimplifier::optimizeWcslen(CallInst *CI, IRBuilderBase &B) {
767   Module &M = *CI->getModule();
768   unsigned WCharSize = TLI->getWCharSize(M) * 8;
769   // We cannot perform this optimization without wchar_size metadata.
770   if (WCharSize == 0)
771     return nullptr;
772 
773   return optimizeStringLength(CI, B, WCharSize);
774 }
775 
776 Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilderBase &B) {
777   StringRef S1, S2;
778   bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
779   bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
780 
781   // strpbrk(s, "") -> nullptr
782   // strpbrk("", s) -> nullptr
783   if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
784     return Constant::getNullValue(CI->getType());
785 
786   // Constant folding.
787   if (HasS1 && HasS2) {
788     size_t I = S1.find_first_of(S2);
789     if (I == StringRef::npos) // No match.
790       return Constant::getNullValue(CI->getType());
791 
792     return B.CreateGEP(B.getInt8Ty(), CI->getArgOperand(0), B.getInt64(I),
793                        "strpbrk");
794   }
795 
796   // strpbrk(s, "a") -> strchr(s, 'a')
797   if (HasS2 && S2.size() == 1)
798     return copyFlags(*CI, emitStrChr(CI->getArgOperand(0), S2[0], B, TLI));
799 
800   return nullptr;
801 }
802 
803 Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilderBase &B) {
804   Value *EndPtr = CI->getArgOperand(1);
805   if (isa<ConstantPointerNull>(EndPtr)) {
806     // With a null EndPtr, this function won't capture the main argument.
807     // It would be readonly too, except that it still may write to errno.
808     CI->addParamAttr(0, Attribute::NoCapture);
809   }
810 
811   return nullptr;
812 }
813 
814 Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilderBase &B) {
815   StringRef S1, S2;
816   bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
817   bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
818 
819   // strspn(s, "") -> 0
820   // strspn("", s) -> 0
821   if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
822     return Constant::getNullValue(CI->getType());
823 
824   // Constant folding.
825   if (HasS1 && HasS2) {
826     size_t Pos = S1.find_first_not_of(S2);
827     if (Pos == StringRef::npos)
828       Pos = S1.size();
829     return ConstantInt::get(CI->getType(), Pos);
830   }
831 
832   return nullptr;
833 }
834 
835 Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilderBase &B) {
836   StringRef S1, S2;
837   bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
838   bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
839 
840   // strcspn("", s) -> 0
841   if (HasS1 && S1.empty())
842     return Constant::getNullValue(CI->getType());
843 
844   // Constant folding.
845   if (HasS1 && HasS2) {
846     size_t Pos = S1.find_first_of(S2);
847     if (Pos == StringRef::npos)
848       Pos = S1.size();
849     return ConstantInt::get(CI->getType(), Pos);
850   }
851 
852   // strcspn(s, "") -> strlen(s)
853   if (HasS2 && S2.empty())
854     return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B, DL, TLI));
855 
856   return nullptr;
857 }
858 
859 Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilderBase &B) {
860   // fold strstr(x, x) -> x.
861   if (CI->getArgOperand(0) == CI->getArgOperand(1))
862     return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
863 
864   // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
865   if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
866     Value *StrLen = emitStrLen(CI->getArgOperand(1), B, DL, TLI);
867     if (!StrLen)
868       return nullptr;
869     Value *StrNCmp = emitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
870                                  StrLen, B, DL, TLI);
871     if (!StrNCmp)
872       return nullptr;
873     for (User *U : llvm::make_early_inc_range(CI->users())) {
874       ICmpInst *Old = cast<ICmpInst>(U);
875       Value *Cmp =
876           B.CreateICmp(Old->getPredicate(), StrNCmp,
877                        ConstantInt::getNullValue(StrNCmp->getType()), "cmp");
878       replaceAllUsesWith(Old, Cmp);
879     }
880     return CI;
881   }
882 
883   // See if either input string is a constant string.
884   StringRef SearchStr, ToFindStr;
885   bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
886   bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
887 
888   // fold strstr(x, "") -> x.
889   if (HasStr2 && ToFindStr.empty())
890     return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
891 
892   // If both strings are known, constant fold it.
893   if (HasStr1 && HasStr2) {
894     size_t Offset = SearchStr.find(ToFindStr);
895 
896     if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
897       return Constant::getNullValue(CI->getType());
898 
899     // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
900     Value *Result = castToCStr(CI->getArgOperand(0), B);
901     Result =
902         B.CreateConstInBoundsGEP1_64(B.getInt8Ty(), Result, Offset, "strstr");
903     return B.CreateBitCast(Result, CI->getType());
904   }
905 
906   // fold strstr(x, "y") -> strchr(x, 'y').
907   if (HasStr2 && ToFindStr.size() == 1) {
908     Value *StrChr = emitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI);
909     return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : nullptr;
910   }
911 
912   annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
913   return nullptr;
914 }
915 
916 Value *LibCallSimplifier::optimizeMemRChr(CallInst *CI, IRBuilderBase &B) {
917   Value *SrcStr = CI->getArgOperand(0);
918   Value *Size = CI->getArgOperand(2);
919   annotateNonNullAndDereferenceable(CI, 0, Size, DL);
920   Value *CharVal = CI->getArgOperand(1);
921   ConstantInt *LenC = dyn_cast<ConstantInt>(Size);
922   Value *NullPtr = Constant::getNullValue(CI->getType());
923 
924   if (LenC) {
925     if (LenC->isZero())
926       // Fold memrchr(x, y, 0) --> null.
927       return NullPtr;
928 
929     if (LenC->isOne()) {
930       // Fold memrchr(x, y, 1) --> *x == y ? x : null for any x and y,
931       // constant or otherwise.
932       Value *Val = B.CreateLoad(B.getInt8Ty(), SrcStr, "memrchr.char0");
933       // Slice off the character's high end bits.
934       CharVal = B.CreateTrunc(CharVal, B.getInt8Ty());
935       Value *Cmp = B.CreateICmpEQ(Val, CharVal, "memrchr.char0cmp");
936       return B.CreateSelect(Cmp, SrcStr, NullPtr, "memrchr.sel");
937     }
938   }
939 
940   StringRef Str;
941   if (!getConstantStringInfo(SrcStr, Str, 0, /*TrimAtNul=*/false))
942     return nullptr;
943 
944   uint64_t EndOff = UINT64_MAX;
945   if (LenC) {
946     EndOff = LenC->getZExtValue();
947     if (Str.size() < EndOff)
948       // Punt out-of-bounds accesses to sanitizers and/or libc.
949       return nullptr;
950   }
951 
952   if (ConstantInt *CharC = dyn_cast<ConstantInt>(CharVal)) {
953     // Fold memrchr(S, C, N) for a constant C.
954     size_t Pos = Str.rfind(CharC->getZExtValue(), EndOff);
955     if (Pos == StringRef::npos)
956       // When the character is not in the source array fold the result
957       // to null regardless of Size.
958       return NullPtr;
959 
960     if (LenC)
961       // Fold memrchr(s, c, N) --> s + Pos for constant N > Pos.
962       return B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(Pos));
963 
964     if (Str.find(CharC->getZExtValue(), Pos) == StringRef::npos) {
965       // When there is just a single occurrence of C in S, fold
966       //   memrchr(s, c, N) --> N <= Pos ? null : s + Pos
967       // for nonconstant N.
968       Value *Cmp = B.CreateICmpULE(Size, ConstantInt::get(Size->getType(),
969 							  Pos),
970 				   "memrchr.cmp");
971       Value *SrcPlus = B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(Pos),
972 				   "memrchr.ptr_plus");
973       return B.CreateSelect(Cmp, NullPtr, SrcPlus, "memrchr.sel");
974     }
975   }
976 
977   return nullptr;
978 }
979 
980 Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) {
981   Value *SrcStr = CI->getArgOperand(0);
982   Value *Size = CI->getArgOperand(2);
983   if (isKnownNonZero(Size, DL))
984     annotateNonNullNoUndefBasedOnAccess(CI, 0);
985 
986   Value *CharVal = CI->getArgOperand(1);
987   ConstantInt *CharC = dyn_cast<ConstantInt>(CharVal);
988   ConstantInt *LenC = dyn_cast<ConstantInt>(Size);
989 
990   // memchr(x, y, 0) -> null
991   if (LenC) {
992     if (LenC->isZero())
993       return Constant::getNullValue(CI->getType());
994 
995     if (LenC->isOne()) {
996       // Fold memchr(x, y, 1) --> *x == y ? x : null for any x and y,
997       // constant or otherwise.
998       Value *Val = B.CreateLoad(B.getInt8Ty(), SrcStr, "memchr.char0");
999       // Slice off the character's high end bits.
1000       CharVal = B.CreateTrunc(CharVal, B.getInt8Ty());
1001       Value *Cmp = B.CreateICmpEQ(Val, CharVal, "memchr.char0cmp");
1002       Value *NullPtr = Constant::getNullValue(CI->getType());
1003       return B.CreateSelect(Cmp, SrcStr, NullPtr, "memchr.sel");
1004     }
1005   }
1006 
1007   StringRef Str;
1008   if (!getConstantStringInfo(SrcStr, Str, 0, /*TrimAtNul=*/false))
1009     return nullptr;
1010 
1011   if (CharC) {
1012     size_t Pos = Str.find(CharC->getZExtValue());
1013     if (Pos == StringRef::npos)
1014       // When the character is not in the source array fold the result
1015       // to null regardless of Size.
1016       return Constant::getNullValue(CI->getType());
1017 
1018     // Fold memchr(s, c, n) -> n <= Pos ? null : s + Pos
1019     // When the constant Size is less than or equal to the character
1020     // position also fold the result to null.
1021     Value *Cmp = B.CreateICmpULE(Size, ConstantInt::get(Size->getType(), Pos),
1022                                  "memchr.cmp");
1023     Value *NullPtr = Constant::getNullValue(CI->getType());
1024     Value *SrcPlus =
1025         B.CreateGEP(B.getInt8Ty(), SrcStr, B.getInt64(Pos), "memchr.ptr");
1026     return B.CreateSelect(Cmp, NullPtr, SrcPlus);
1027   }
1028 
1029   if (!LenC)
1030     // From now on we need a constant length and constant array.
1031     return nullptr;
1032 
1033   // Truncate the string to LenC without slicing when targeting LP64
1034   // on an ILP32 host.
1035   uint64_t EndOff = std::min(LenC->getZExtValue(), (uint64_t)StringRef::npos);
1036   Str = Str.substr(0, EndOff);
1037 
1038   // If the char is variable but the input str and length are not we can turn
1039   // this memchr call into a simple bit field test. Of course this only works
1040   // when the return value is only checked against null.
1041   //
1042   // It would be really nice to reuse switch lowering here but we can't change
1043   // the CFG at this point.
1044   //
1045   // memchr("\r\n", C, 2) != nullptr -> (1 << C & ((1 << '\r') | (1 << '\n')))
1046   // != 0
1047   //   after bounds check.
1048   if (Str.empty() || !isOnlyUsedInZeroEqualityComparison(CI))
1049     return nullptr;
1050 
1051   unsigned char Max =
1052       *std::max_element(reinterpret_cast<const unsigned char *>(Str.begin()),
1053                         reinterpret_cast<const unsigned char *>(Str.end()));
1054 
1055   // Make sure the bit field we're about to create fits in a register on the
1056   // target.
1057   // FIXME: On a 64 bit architecture this prevents us from using the
1058   // interesting range of alpha ascii chars. We could do better by emitting
1059   // two bitfields or shifting the range by 64 if no lower chars are used.
1060   if (!DL.fitsInLegalInteger(Max + 1))
1061     return nullptr;
1062 
1063   // For the bit field use a power-of-2 type with at least 8 bits to avoid
1064   // creating unnecessary illegal types.
1065   unsigned char Width = NextPowerOf2(std::max((unsigned char)7, Max));
1066 
1067   // Now build the bit field.
1068   APInt Bitfield(Width, 0);
1069   for (char C : Str)
1070     Bitfield.setBit((unsigned char)C);
1071   Value *BitfieldC = B.getInt(Bitfield);
1072 
1073   // Adjust width of "C" to the bitfield width, then mask off the high bits.
1074   Value *C = B.CreateZExtOrTrunc(CharVal, BitfieldC->getType());
1075   C = B.CreateAnd(C, B.getIntN(Width, 0xFF));
1076 
1077   // First check that the bit field access is within bounds.
1078   Value *Bounds = B.CreateICmp(ICmpInst::ICMP_ULT, C, B.getIntN(Width, Width),
1079                                "memchr.bounds");
1080 
1081   // Create code that checks if the given bit is set in the field.
1082   Value *Shl = B.CreateShl(B.getIntN(Width, 1ULL), C);
1083   Value *Bits = B.CreateIsNotNull(B.CreateAnd(Shl, BitfieldC), "memchr.bits");
1084 
1085   // Finally merge both checks and cast to pointer type. The inttoptr
1086   // implicitly zexts the i1 to intptr type.
1087   return B.CreateIntToPtr(B.CreateLogicalAnd(Bounds, Bits, "memchr"),
1088                           CI->getType());
1089 }
1090 
1091 static Value *optimizeMemCmpConstantSize(CallInst *CI, Value *LHS, Value *RHS,
1092                                          uint64_t Len, IRBuilderBase &B,
1093                                          const DataLayout &DL) {
1094   if (Len == 0) // memcmp(s1,s2,0) -> 0
1095     return Constant::getNullValue(CI->getType());
1096 
1097   // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
1098   if (Len == 1) {
1099     Value *LHSV =
1100         B.CreateZExt(B.CreateLoad(B.getInt8Ty(), castToCStr(LHS, B), "lhsc"),
1101                      CI->getType(), "lhsv");
1102     Value *RHSV =
1103         B.CreateZExt(B.CreateLoad(B.getInt8Ty(), castToCStr(RHS, B), "rhsc"),
1104                      CI->getType(), "rhsv");
1105     return B.CreateSub(LHSV, RHSV, "chardiff");
1106   }
1107 
1108   // memcmp(S1,S2,N/8)==0 -> (*(intN_t*)S1 != *(intN_t*)S2)==0
1109   // TODO: The case where both inputs are constants does not need to be limited
1110   // to legal integers or equality comparison. See block below this.
1111   if (DL.isLegalInteger(Len * 8) && isOnlyUsedInZeroEqualityComparison(CI)) {
1112     IntegerType *IntType = IntegerType::get(CI->getContext(), Len * 8);
1113     unsigned PrefAlignment = DL.getPrefTypeAlignment(IntType);
1114 
1115     // First, see if we can fold either argument to a constant.
1116     Value *LHSV = nullptr;
1117     if (auto *LHSC = dyn_cast<Constant>(LHS)) {
1118       LHSC = ConstantExpr::getBitCast(LHSC, IntType->getPointerTo());
1119       LHSV = ConstantFoldLoadFromConstPtr(LHSC, IntType, DL);
1120     }
1121     Value *RHSV = nullptr;
1122     if (auto *RHSC = dyn_cast<Constant>(RHS)) {
1123       RHSC = ConstantExpr::getBitCast(RHSC, IntType->getPointerTo());
1124       RHSV = ConstantFoldLoadFromConstPtr(RHSC, IntType, DL);
1125     }
1126 
1127     // Don't generate unaligned loads. If either source is constant data,
1128     // alignment doesn't matter for that source because there is no load.
1129     if ((LHSV || getKnownAlignment(LHS, DL, CI) >= PrefAlignment) &&
1130         (RHSV || getKnownAlignment(RHS, DL, CI) >= PrefAlignment)) {
1131       if (!LHSV) {
1132         Type *LHSPtrTy =
1133             IntType->getPointerTo(LHS->getType()->getPointerAddressSpace());
1134         LHSV = B.CreateLoad(IntType, B.CreateBitCast(LHS, LHSPtrTy), "lhsv");
1135       }
1136       if (!RHSV) {
1137         Type *RHSPtrTy =
1138             IntType->getPointerTo(RHS->getType()->getPointerAddressSpace());
1139         RHSV = B.CreateLoad(IntType, B.CreateBitCast(RHS, RHSPtrTy), "rhsv");
1140       }
1141       return B.CreateZExt(B.CreateICmpNE(LHSV, RHSV), CI->getType(), "memcmp");
1142     }
1143   }
1144 
1145   // Constant folding: memcmp(x, y, Len) -> constant (all arguments are const).
1146   // TODO: This is limited to i8 arrays.
1147   StringRef LHSStr, RHSStr;
1148   if (getConstantStringInfo(LHS, LHSStr) &&
1149       getConstantStringInfo(RHS, RHSStr)) {
1150     // Make sure we're not reading out-of-bounds memory.
1151     if (Len > LHSStr.size() || Len > RHSStr.size())
1152       return nullptr;
1153     // Fold the memcmp and normalize the result.  This way we get consistent
1154     // results across multiple platforms.
1155     uint64_t Ret = 0;
1156     int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);
1157     if (Cmp < 0)
1158       Ret = -1;
1159     else if (Cmp > 0)
1160       Ret = 1;
1161     return ConstantInt::get(CI->getType(), Ret);
1162   }
1163 
1164   return nullptr;
1165 }
1166 
1167 // Most simplifications for memcmp also apply to bcmp.
1168 Value *LibCallSimplifier::optimizeMemCmpBCmpCommon(CallInst *CI,
1169                                                    IRBuilderBase &B) {
1170   Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
1171   Value *Size = CI->getArgOperand(2);
1172 
1173   if (LHS == RHS) // memcmp(s,s,x) -> 0
1174     return Constant::getNullValue(CI->getType());
1175 
1176   annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
1177   // Handle constant lengths.
1178   ConstantInt *LenC = dyn_cast<ConstantInt>(Size);
1179   if (!LenC)
1180     return nullptr;
1181 
1182   // memcmp(d,s,0) -> 0
1183   if (LenC->getZExtValue() == 0)
1184     return Constant::getNullValue(CI->getType());
1185 
1186   if (Value *Res =
1187           optimizeMemCmpConstantSize(CI, LHS, RHS, LenC->getZExtValue(), B, DL))
1188     return Res;
1189   return nullptr;
1190 }
1191 
1192 Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilderBase &B) {
1193   Module *M = CI->getModule();
1194   if (Value *V = optimizeMemCmpBCmpCommon(CI, B))
1195     return V;
1196 
1197   // memcmp(x, y, Len) == 0 -> bcmp(x, y, Len) == 0
1198   // bcmp can be more efficient than memcmp because it only has to know that
1199   // there is a difference, not how different one is to the other.
1200   if (isLibFuncEmittable(M, TLI, LibFunc_bcmp) &&
1201       isOnlyUsedInZeroEqualityComparison(CI)) {
1202     Value *LHS = CI->getArgOperand(0);
1203     Value *RHS = CI->getArgOperand(1);
1204     Value *Size = CI->getArgOperand(2);
1205     return copyFlags(*CI, emitBCmp(LHS, RHS, Size, B, DL, TLI));
1206   }
1207 
1208   return nullptr;
1209 }
1210 
1211 Value *LibCallSimplifier::optimizeBCmp(CallInst *CI, IRBuilderBase &B) {
1212   return optimizeMemCmpBCmpCommon(CI, B);
1213 }
1214 
1215 Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilderBase &B) {
1216   Value *Size = CI->getArgOperand(2);
1217   annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
1218   if (isa<IntrinsicInst>(CI))
1219     return nullptr;
1220 
1221   // memcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n)
1222   CallInst *NewCI = B.CreateMemCpy(CI->getArgOperand(0), Align(1),
1223                                    CI->getArgOperand(1), Align(1), Size);
1224   NewCI->setAttributes(CI->getAttributes());
1225   NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
1226   copyFlags(*CI, NewCI);
1227   return CI->getArgOperand(0);
1228 }
1229 
1230 Value *LibCallSimplifier::optimizeMemCCpy(CallInst *CI, IRBuilderBase &B) {
1231   Value *Dst = CI->getArgOperand(0);
1232   Value *Src = CI->getArgOperand(1);
1233   ConstantInt *StopChar = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1234   ConstantInt *N = dyn_cast<ConstantInt>(CI->getArgOperand(3));
1235   StringRef SrcStr;
1236   if (CI->use_empty() && Dst == Src)
1237     return Dst;
1238   // memccpy(d, s, c, 0) -> nullptr
1239   if (N) {
1240     if (N->isNullValue())
1241       return Constant::getNullValue(CI->getType());
1242     if (!getConstantStringInfo(Src, SrcStr, /*Offset=*/0,
1243                                /*TrimAtNul=*/false) ||
1244         !StopChar)
1245       return nullptr;
1246   } else {
1247     return nullptr;
1248   }
1249 
1250   // Wrap arg 'c' of type int to char
1251   size_t Pos = SrcStr.find(StopChar->getSExtValue() & 0xFF);
1252   if (Pos == StringRef::npos) {
1253     if (N->getZExtValue() <= SrcStr.size()) {
1254       copyFlags(*CI, B.CreateMemCpy(Dst, Align(1), Src, Align(1),
1255                                     CI->getArgOperand(3)));
1256       return Constant::getNullValue(CI->getType());
1257     }
1258     return nullptr;
1259   }
1260 
1261   Value *NewN =
1262       ConstantInt::get(N->getType(), std::min(uint64_t(Pos + 1), N->getZExtValue()));
1263   // memccpy -> llvm.memcpy
1264   copyFlags(*CI, B.CreateMemCpy(Dst, Align(1), Src, Align(1), NewN));
1265   return Pos + 1 <= N->getZExtValue()
1266              ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, NewN)
1267              : Constant::getNullValue(CI->getType());
1268 }
1269 
1270 Value *LibCallSimplifier::optimizeMemPCpy(CallInst *CI, IRBuilderBase &B) {
1271   Value *Dst = CI->getArgOperand(0);
1272   Value *N = CI->getArgOperand(2);
1273   // mempcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n), x + n
1274   CallInst *NewCI =
1275       B.CreateMemCpy(Dst, Align(1), CI->getArgOperand(1), Align(1), N);
1276   // Propagate attributes, but memcpy has no return value, so make sure that
1277   // any return attributes are compliant.
1278   // TODO: Attach return value attributes to the 1st operand to preserve them?
1279   NewCI->setAttributes(CI->getAttributes());
1280   NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
1281   copyFlags(*CI, NewCI);
1282   return B.CreateInBoundsGEP(B.getInt8Ty(), Dst, N);
1283 }
1284 
1285 Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilderBase &B) {
1286   Value *Size = CI->getArgOperand(2);
1287   annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
1288   if (isa<IntrinsicInst>(CI))
1289     return nullptr;
1290 
1291   // memmove(x, y, n) -> llvm.memmove(align 1 x, align 1 y, n)
1292   CallInst *NewCI = B.CreateMemMove(CI->getArgOperand(0), Align(1),
1293                                     CI->getArgOperand(1), Align(1), Size);
1294   NewCI->setAttributes(CI->getAttributes());
1295   NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
1296   copyFlags(*CI, NewCI);
1297   return CI->getArgOperand(0);
1298 }
1299 
1300 Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilderBase &B) {
1301   Value *Size = CI->getArgOperand(2);
1302   annotateNonNullAndDereferenceable(CI, 0, Size, DL);
1303   if (isa<IntrinsicInst>(CI))
1304     return nullptr;
1305 
1306   // memset(p, v, n) -> llvm.memset(align 1 p, v, n)
1307   Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
1308   CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val, Size, Align(1));
1309   NewCI->setAttributes(CI->getAttributes());
1310   NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
1311   copyFlags(*CI, NewCI);
1312   return CI->getArgOperand(0);
1313 }
1314 
1315 Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilderBase &B) {
1316   if (isa<ConstantPointerNull>(CI->getArgOperand(0)))
1317     return copyFlags(*CI, emitMalloc(CI->getArgOperand(1), B, DL, TLI));
1318 
1319   return nullptr;
1320 }
1321 
1322 //===----------------------------------------------------------------------===//
1323 // Math Library Optimizations
1324 //===----------------------------------------------------------------------===//
1325 
1326 // Replace a libcall \p CI with a call to intrinsic \p IID
1327 static Value *replaceUnaryCall(CallInst *CI, IRBuilderBase &B,
1328                                Intrinsic::ID IID) {
1329   // Propagate fast-math flags from the existing call to the new call.
1330   IRBuilderBase::FastMathFlagGuard Guard(B);
1331   B.setFastMathFlags(CI->getFastMathFlags());
1332 
1333   Module *M = CI->getModule();
1334   Value *V = CI->getArgOperand(0);
1335   Function *F = Intrinsic::getDeclaration(M, IID, CI->getType());
1336   CallInst *NewCall = B.CreateCall(F, V);
1337   NewCall->takeName(CI);
1338   return copyFlags(*CI, NewCall);
1339 }
1340 
1341 /// Return a variant of Val with float type.
1342 /// Currently this works in two cases: If Val is an FPExtension of a float
1343 /// value to something bigger, simply return the operand.
1344 /// If Val is a ConstantFP but can be converted to a float ConstantFP without
1345 /// loss of precision do so.
1346 static Value *valueHasFloatPrecision(Value *Val) {
1347   if (FPExtInst *Cast = dyn_cast<FPExtInst>(Val)) {
1348     Value *Op = Cast->getOperand(0);
1349     if (Op->getType()->isFloatTy())
1350       return Op;
1351   }
1352   if (ConstantFP *Const = dyn_cast<ConstantFP>(Val)) {
1353     APFloat F = Const->getValueAPF();
1354     bool losesInfo;
1355     (void)F.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
1356                     &losesInfo);
1357     if (!losesInfo)
1358       return ConstantFP::get(Const->getContext(), F);
1359   }
1360   return nullptr;
1361 }
1362 
1363 /// Shrink double -> float functions.
1364 static Value *optimizeDoubleFP(CallInst *CI, IRBuilderBase &B,
1365                                bool isBinary, const TargetLibraryInfo *TLI,
1366                                bool isPrecise = false) {
1367   Function *CalleeFn = CI->getCalledFunction();
1368   if (!CI->getType()->isDoubleTy() || !CalleeFn)
1369     return nullptr;
1370 
1371   // If not all the uses of the function are converted to float, then bail out.
1372   // This matters if the precision of the result is more important than the
1373   // precision of the arguments.
1374   if (isPrecise)
1375     for (User *U : CI->users()) {
1376       FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);
1377       if (!Cast || !Cast->getType()->isFloatTy())
1378         return nullptr;
1379     }
1380 
1381   // If this is something like 'g((double) float)', convert to 'gf(float)'.
1382   Value *V[2];
1383   V[0] = valueHasFloatPrecision(CI->getArgOperand(0));
1384   V[1] = isBinary ? valueHasFloatPrecision(CI->getArgOperand(1)) : nullptr;
1385   if (!V[0] || (isBinary && !V[1]))
1386     return nullptr;
1387 
1388   // If call isn't an intrinsic, check that it isn't within a function with the
1389   // same name as the float version of this call, otherwise the result is an
1390   // infinite loop.  For example, from MinGW-w64:
1391   //
1392   // float expf(float val) { return (float) exp((double) val); }
1393   StringRef CalleeName = CalleeFn->getName();
1394   bool IsIntrinsic = CalleeFn->isIntrinsic();
1395   if (!IsIntrinsic) {
1396     StringRef CallerName = CI->getFunction()->getName();
1397     if (!CallerName.empty() && CallerName.back() == 'f' &&
1398         CallerName.size() == (CalleeName.size() + 1) &&
1399         CallerName.startswith(CalleeName))
1400       return nullptr;
1401   }
1402 
1403   // Propagate the math semantics from the current function to the new function.
1404   IRBuilderBase::FastMathFlagGuard Guard(B);
1405   B.setFastMathFlags(CI->getFastMathFlags());
1406 
1407   // g((double) float) -> (double) gf(float)
1408   Value *R;
1409   if (IsIntrinsic) {
1410     Module *M = CI->getModule();
1411     Intrinsic::ID IID = CalleeFn->getIntrinsicID();
1412     Function *Fn = Intrinsic::getDeclaration(M, IID, B.getFloatTy());
1413     R = isBinary ? B.CreateCall(Fn, V) : B.CreateCall(Fn, V[0]);
1414   } else {
1415     AttributeList CalleeAttrs = CalleeFn->getAttributes();
1416     R = isBinary ? emitBinaryFloatFnCall(V[0], V[1], TLI, CalleeName, B,
1417                                          CalleeAttrs)
1418                  : emitUnaryFloatFnCall(V[0], TLI, CalleeName, B, CalleeAttrs);
1419   }
1420   return B.CreateFPExt(R, B.getDoubleTy());
1421 }
1422 
1423 /// Shrink double -> float for unary functions.
1424 static Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilderBase &B,
1425                                     const TargetLibraryInfo *TLI,
1426                                     bool isPrecise = false) {
1427   return optimizeDoubleFP(CI, B, false, TLI, isPrecise);
1428 }
1429 
1430 /// Shrink double -> float for binary functions.
1431 static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilderBase &B,
1432                                      const TargetLibraryInfo *TLI,
1433                                      bool isPrecise = false) {
1434   return optimizeDoubleFP(CI, B, true, TLI, isPrecise);
1435 }
1436 
1437 // cabs(z) -> sqrt((creal(z)*creal(z)) + (cimag(z)*cimag(z)))
1438 Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilderBase &B) {
1439   if (!CI->isFast())
1440     return nullptr;
1441 
1442   // Propagate fast-math flags from the existing call to new instructions.
1443   IRBuilderBase::FastMathFlagGuard Guard(B);
1444   B.setFastMathFlags(CI->getFastMathFlags());
1445 
1446   Value *Real, *Imag;
1447   if (CI->arg_size() == 1) {
1448     Value *Op = CI->getArgOperand(0);
1449     assert(Op->getType()->isArrayTy() && "Unexpected signature for cabs!");
1450     Real = B.CreateExtractValue(Op, 0, "real");
1451     Imag = B.CreateExtractValue(Op, 1, "imag");
1452   } else {
1453     assert(CI->arg_size() == 2 && "Unexpected signature for cabs!");
1454     Real = CI->getArgOperand(0);
1455     Imag = CI->getArgOperand(1);
1456   }
1457 
1458   Value *RealReal = B.CreateFMul(Real, Real);
1459   Value *ImagImag = B.CreateFMul(Imag, Imag);
1460 
1461   Function *FSqrt = Intrinsic::getDeclaration(CI->getModule(), Intrinsic::sqrt,
1462                                               CI->getType());
1463   return copyFlags(
1464       *CI, B.CreateCall(FSqrt, B.CreateFAdd(RealReal, ImagImag), "cabs"));
1465 }
1466 
1467 static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func,
1468                                       IRBuilderBase &B) {
1469   if (!isa<FPMathOperator>(Call))
1470     return nullptr;
1471 
1472   IRBuilderBase::FastMathFlagGuard Guard(B);
1473   B.setFastMathFlags(Call->getFastMathFlags());
1474 
1475   // TODO: Can this be shared to also handle LLVM intrinsics?
1476   Value *X;
1477   switch (Func) {
1478   case LibFunc_sin:
1479   case LibFunc_sinf:
1480   case LibFunc_sinl:
1481   case LibFunc_tan:
1482   case LibFunc_tanf:
1483   case LibFunc_tanl:
1484     // sin(-X) --> -sin(X)
1485     // tan(-X) --> -tan(X)
1486     if (match(Call->getArgOperand(0), m_OneUse(m_FNeg(m_Value(X)))))
1487       return B.CreateFNeg(
1488           copyFlags(*Call, B.CreateCall(Call->getCalledFunction(), X)));
1489     break;
1490   case LibFunc_cos:
1491   case LibFunc_cosf:
1492   case LibFunc_cosl:
1493     // cos(-X) --> cos(X)
1494     if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))))
1495       return copyFlags(*Call,
1496                        B.CreateCall(Call->getCalledFunction(), X, "cos"));
1497     break;
1498   default:
1499     break;
1500   }
1501   return nullptr;
1502 }
1503 
1504 static Value *getPow(Value *InnerChain[33], unsigned Exp, IRBuilderBase &B) {
1505   // Multiplications calculated using Addition Chains.
1506   // Refer: http://wwwhomes.uni-bielefeld.de/achim/addition_chain.html
1507 
1508   assert(Exp != 0 && "Incorrect exponent 0 not handled");
1509 
1510   if (InnerChain[Exp])
1511     return InnerChain[Exp];
1512 
1513   static const unsigned AddChain[33][2] = {
1514       {0, 0}, // Unused.
1515       {0, 0}, // Unused (base case = pow1).
1516       {1, 1}, // Unused (pre-computed).
1517       {1, 2},  {2, 2},   {2, 3},  {3, 3},   {2, 5},  {4, 4},
1518       {1, 8},  {5, 5},   {1, 10}, {6, 6},   {4, 9},  {7, 7},
1519       {3, 12}, {8, 8},   {8, 9},  {2, 16},  {1, 18}, {10, 10},
1520       {6, 15}, {11, 11}, {3, 20}, {12, 12}, {8, 17}, {13, 13},
1521       {3, 24}, {14, 14}, {4, 25}, {15, 15}, {3, 28}, {16, 16},
1522   };
1523 
1524   InnerChain[Exp] = B.CreateFMul(getPow(InnerChain, AddChain[Exp][0], B),
1525                                  getPow(InnerChain, AddChain[Exp][1], B));
1526   return InnerChain[Exp];
1527 }
1528 
1529 // Return a properly extended integer (DstWidth bits wide) if the operation is
1530 // an itofp.
1531 static Value *getIntToFPVal(Value *I2F, IRBuilderBase &B, unsigned DstWidth) {
1532   if (isa<SIToFPInst>(I2F) || isa<UIToFPInst>(I2F)) {
1533     Value *Op = cast<Instruction>(I2F)->getOperand(0);
1534     // Make sure that the exponent fits inside an "int" of size DstWidth,
1535     // thus avoiding any range issues that FP has not.
1536     unsigned BitWidth = Op->getType()->getPrimitiveSizeInBits();
1537     if (BitWidth < DstWidth ||
1538         (BitWidth == DstWidth && isa<SIToFPInst>(I2F)))
1539       return isa<SIToFPInst>(I2F) ? B.CreateSExt(Op, B.getIntNTy(DstWidth))
1540                                   : B.CreateZExt(Op, B.getIntNTy(DstWidth));
1541   }
1542 
1543   return nullptr;
1544 }
1545 
1546 /// Use exp{,2}(x * y) for pow(exp{,2}(x), y);
1547 /// ldexp(1.0, x) for pow(2.0, itofp(x)); exp2(n * x) for pow(2.0 ** n, x);
1548 /// exp10(x) for pow(10.0, x); exp2(log2(n) * x) for pow(n, x).
1549 Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
1550   Module *M = Pow->getModule();
1551   Value *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
1552   AttributeList Attrs; // Attributes are only meaningful on the original call
1553   Module *Mod = Pow->getModule();
1554   Type *Ty = Pow->getType();
1555   bool Ignored;
1556 
1557   // Evaluate special cases related to a nested function as the base.
1558 
1559   // pow(exp(x), y) -> exp(x * y)
1560   // pow(exp2(x), y) -> exp2(x * y)
1561   // If exp{,2}() is used only once, it is better to fold two transcendental
1562   // math functions into one.  If used again, exp{,2}() would still have to be
1563   // called with the original argument, then keep both original transcendental
1564   // functions.  However, this transformation is only safe with fully relaxed
1565   // math semantics, since, besides rounding differences, it changes overflow
1566   // and underflow behavior quite dramatically.  For example:
1567   //   pow(exp(1000), 0.001) = pow(inf, 0.001) = inf
1568   // Whereas:
1569   //   exp(1000 * 0.001) = exp(1)
1570   // TODO: Loosen the requirement for fully relaxed math semantics.
1571   // TODO: Handle exp10() when more targets have it available.
1572   CallInst *BaseFn = dyn_cast<CallInst>(Base);
1573   if (BaseFn && BaseFn->hasOneUse() && BaseFn->isFast() && Pow->isFast()) {
1574     LibFunc LibFn;
1575 
1576     Function *CalleeFn = BaseFn->getCalledFunction();
1577     if (CalleeFn &&
1578         TLI->getLibFunc(CalleeFn->getName(), LibFn) &&
1579         isLibFuncEmittable(M, TLI, LibFn)) {
1580       StringRef ExpName;
1581       Intrinsic::ID ID;
1582       Value *ExpFn;
1583       LibFunc LibFnFloat, LibFnDouble, LibFnLongDouble;
1584 
1585       switch (LibFn) {
1586       default:
1587         return nullptr;
1588       case LibFunc_expf:  case LibFunc_exp:  case LibFunc_expl:
1589         ExpName = TLI->getName(LibFunc_exp);
1590         ID = Intrinsic::exp;
1591         LibFnFloat = LibFunc_expf;
1592         LibFnDouble = LibFunc_exp;
1593         LibFnLongDouble = LibFunc_expl;
1594         break;
1595       case LibFunc_exp2f: case LibFunc_exp2: case LibFunc_exp2l:
1596         ExpName = TLI->getName(LibFunc_exp2);
1597         ID = Intrinsic::exp2;
1598         LibFnFloat = LibFunc_exp2f;
1599         LibFnDouble = LibFunc_exp2;
1600         LibFnLongDouble = LibFunc_exp2l;
1601         break;
1602       }
1603 
1604       // Create new exp{,2}() with the product as its argument.
1605       Value *FMul = B.CreateFMul(BaseFn->getArgOperand(0), Expo, "mul");
1606       ExpFn = BaseFn->doesNotAccessMemory()
1607               ? B.CreateCall(Intrinsic::getDeclaration(Mod, ID, Ty),
1608                              FMul, ExpName)
1609               : emitUnaryFloatFnCall(FMul, TLI, LibFnDouble, LibFnFloat,
1610                                      LibFnLongDouble, B,
1611                                      BaseFn->getAttributes());
1612 
1613       // Since the new exp{,2}() is different from the original one, dead code
1614       // elimination cannot be trusted to remove it, since it may have side
1615       // effects (e.g., errno).  When the only consumer for the original
1616       // exp{,2}() is pow(), then it has to be explicitly erased.
1617       substituteInParent(BaseFn, ExpFn);
1618       return ExpFn;
1619     }
1620   }
1621 
1622   // Evaluate special cases related to a constant base.
1623 
1624   const APFloat *BaseF;
1625   if (!match(Pow->getArgOperand(0), m_APFloat(BaseF)))
1626     return nullptr;
1627 
1628   // pow(2.0, itofp(x)) -> ldexp(1.0, x)
1629   if (match(Base, m_SpecificFP(2.0)) &&
1630       (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo)) &&
1631       hasFloatFn(M, TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl)) {
1632     if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize()))
1633       return copyFlags(*Pow,
1634                        emitBinaryFloatFnCall(ConstantFP::get(Ty, 1.0), ExpoI,
1635                                              TLI, LibFunc_ldexp, LibFunc_ldexpf,
1636                                              LibFunc_ldexpl, B, Attrs));
1637   }
1638 
1639   // pow(2.0 ** n, x) -> exp2(n * x)
1640   if (hasFloatFn(M, TLI, Ty, LibFunc_exp2, LibFunc_exp2f, LibFunc_exp2l)) {
1641     APFloat BaseR = APFloat(1.0);
1642     BaseR.convert(BaseF->getSemantics(), APFloat::rmTowardZero, &Ignored);
1643     BaseR = BaseR / *BaseF;
1644     bool IsInteger = BaseF->isInteger(), IsReciprocal = BaseR.isInteger();
1645     const APFloat *NF = IsReciprocal ? &BaseR : BaseF;
1646     APSInt NI(64, false);
1647     if ((IsInteger || IsReciprocal) &&
1648         NF->convertToInteger(NI, APFloat::rmTowardZero, &Ignored) ==
1649             APFloat::opOK &&
1650         NI > 1 && NI.isPowerOf2()) {
1651       double N = NI.logBase2() * (IsReciprocal ? -1.0 : 1.0);
1652       Value *FMul = B.CreateFMul(Expo, ConstantFP::get(Ty, N), "mul");
1653       if (Pow->doesNotAccessMemory())
1654         return copyFlags(*Pow, B.CreateCall(Intrinsic::getDeclaration(
1655                                                 Mod, Intrinsic::exp2, Ty),
1656                                             FMul, "exp2"));
1657       else
1658         return copyFlags(*Pow, emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2,
1659                                                     LibFunc_exp2f,
1660                                                     LibFunc_exp2l, B, Attrs));
1661     }
1662   }
1663 
1664   // pow(10.0, x) -> exp10(x)
1665   // TODO: There is no exp10() intrinsic yet, but some day there shall be one.
1666   if (match(Base, m_SpecificFP(10.0)) &&
1667       hasFloatFn(M, TLI, Ty, LibFunc_exp10, LibFunc_exp10f, LibFunc_exp10l))
1668     return copyFlags(*Pow, emitUnaryFloatFnCall(Expo, TLI, LibFunc_exp10,
1669                                                 LibFunc_exp10f, LibFunc_exp10l,
1670                                                 B, Attrs));
1671 
1672   // pow(x, y) -> exp2(log2(x) * y)
1673   if (Pow->hasApproxFunc() && Pow->hasNoNaNs() && BaseF->isFiniteNonZero() &&
1674       !BaseF->isNegative()) {
1675     // pow(1, inf) is defined to be 1 but exp2(log2(1) * inf) evaluates to NaN.
1676     // Luckily optimizePow has already handled the x == 1 case.
1677     assert(!match(Base, m_FPOne()) &&
1678            "pow(1.0, y) should have been simplified earlier!");
1679 
1680     Value *Log = nullptr;
1681     if (Ty->isFloatTy())
1682       Log = ConstantFP::get(Ty, std::log2(BaseF->convertToFloat()));
1683     else if (Ty->isDoubleTy())
1684       Log = ConstantFP::get(Ty, std::log2(BaseF->convertToDouble()));
1685 
1686     if (Log) {
1687       Value *FMul = B.CreateFMul(Log, Expo, "mul");
1688       if (Pow->doesNotAccessMemory())
1689         return copyFlags(*Pow, B.CreateCall(Intrinsic::getDeclaration(
1690                                                 Mod, Intrinsic::exp2, Ty),
1691                                             FMul, "exp2"));
1692       else if (hasFloatFn(M, TLI, Ty, LibFunc_exp2, LibFunc_exp2f,
1693                           LibFunc_exp2l))
1694         return copyFlags(*Pow, emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2,
1695                                                     LibFunc_exp2f,
1696                                                     LibFunc_exp2l, B, Attrs));
1697     }
1698   }
1699 
1700   return nullptr;
1701 }
1702 
1703 static Value *getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno,
1704                           Module *M, IRBuilderBase &B,
1705                           const TargetLibraryInfo *TLI) {
1706   // If errno is never set, then use the intrinsic for sqrt().
1707   if (NoErrno) {
1708     Function *SqrtFn =
1709         Intrinsic::getDeclaration(M, Intrinsic::sqrt, V->getType());
1710     return B.CreateCall(SqrtFn, V, "sqrt");
1711   }
1712 
1713   // Otherwise, use the libcall for sqrt().
1714   if (hasFloatFn(M, TLI, V->getType(), LibFunc_sqrt, LibFunc_sqrtf,
1715                  LibFunc_sqrtl))
1716     // TODO: We also should check that the target can in fact lower the sqrt()
1717     // libcall. We currently have no way to ask this question, so we ask if
1718     // the target has a sqrt() libcall, which is not exactly the same.
1719     return emitUnaryFloatFnCall(V, TLI, LibFunc_sqrt, LibFunc_sqrtf,
1720                                 LibFunc_sqrtl, B, Attrs);
1721 
1722   return nullptr;
1723 }
1724 
1725 /// Use square root in place of pow(x, +/-0.5).
1726 Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) {
1727   Value *Sqrt, *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
1728   AttributeList Attrs; // Attributes are only meaningful on the original call
1729   Module *Mod = Pow->getModule();
1730   Type *Ty = Pow->getType();
1731 
1732   const APFloat *ExpoF;
1733   if (!match(Expo, m_APFloat(ExpoF)) ||
1734       (!ExpoF->isExactlyValue(0.5) && !ExpoF->isExactlyValue(-0.5)))
1735     return nullptr;
1736 
1737   // Converting pow(X, -0.5) to 1/sqrt(X) may introduce an extra rounding step,
1738   // so that requires fast-math-flags (afn or reassoc).
1739   if (ExpoF->isNegative() && (!Pow->hasApproxFunc() && !Pow->hasAllowReassoc()))
1740     return nullptr;
1741 
1742   // If we have a pow() library call (accesses memory) and we can't guarantee
1743   // that the base is not an infinity, give up:
1744   // pow(-Inf, 0.5) is optionally required to have a result of +Inf (not setting
1745   // errno), but sqrt(-Inf) is required by various standards to set errno.
1746   if (!Pow->doesNotAccessMemory() && !Pow->hasNoInfs() &&
1747       !isKnownNeverInfinity(Base, TLI))
1748     return nullptr;
1749 
1750   Sqrt = getSqrtCall(Base, Attrs, Pow->doesNotAccessMemory(), Mod, B, TLI);
1751   if (!Sqrt)
1752     return nullptr;
1753 
1754   // Handle signed zero base by expanding to fabs(sqrt(x)).
1755   if (!Pow->hasNoSignedZeros()) {
1756     Function *FAbsFn = Intrinsic::getDeclaration(Mod, Intrinsic::fabs, Ty);
1757     Sqrt = B.CreateCall(FAbsFn, Sqrt, "abs");
1758   }
1759 
1760   Sqrt = copyFlags(*Pow, Sqrt);
1761 
1762   // Handle non finite base by expanding to
1763   // (x == -infinity ? +infinity : sqrt(x)).
1764   if (!Pow->hasNoInfs()) {
1765     Value *PosInf = ConstantFP::getInfinity(Ty),
1766           *NegInf = ConstantFP::getInfinity(Ty, true);
1767     Value *FCmp = B.CreateFCmpOEQ(Base, NegInf, "isinf");
1768     Sqrt = B.CreateSelect(FCmp, PosInf, Sqrt);
1769   }
1770 
1771   // If the exponent is negative, then get the reciprocal.
1772   if (ExpoF->isNegative())
1773     Sqrt = B.CreateFDiv(ConstantFP::get(Ty, 1.0), Sqrt, "reciprocal");
1774 
1775   return Sqrt;
1776 }
1777 
1778 static Value *createPowWithIntegerExponent(Value *Base, Value *Expo, Module *M,
1779                                            IRBuilderBase &B) {
1780   Value *Args[] = {Base, Expo};
1781   Type *Types[] = {Base->getType(), Expo->getType()};
1782   Function *F = Intrinsic::getDeclaration(M, Intrinsic::powi, Types);
1783   return B.CreateCall(F, Args);
1784 }
1785 
1786 Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) {
1787   Value *Base = Pow->getArgOperand(0);
1788   Value *Expo = Pow->getArgOperand(1);
1789   Function *Callee = Pow->getCalledFunction();
1790   StringRef Name = Callee->getName();
1791   Type *Ty = Pow->getType();
1792   Module *M = Pow->getModule();
1793   bool AllowApprox = Pow->hasApproxFunc();
1794   bool Ignored;
1795 
1796   // Propagate the math semantics from the call to any created instructions.
1797   IRBuilderBase::FastMathFlagGuard Guard(B);
1798   B.setFastMathFlags(Pow->getFastMathFlags());
1799   // Evaluate special cases related to the base.
1800 
1801   // pow(1.0, x) -> 1.0
1802   if (match(Base, m_FPOne()))
1803     return Base;
1804 
1805   if (Value *Exp = replacePowWithExp(Pow, B))
1806     return Exp;
1807 
1808   // Evaluate special cases related to the exponent.
1809 
1810   // pow(x, -1.0) -> 1.0 / x
1811   if (match(Expo, m_SpecificFP(-1.0)))
1812     return B.CreateFDiv(ConstantFP::get(Ty, 1.0), Base, "reciprocal");
1813 
1814   // pow(x, +/-0.0) -> 1.0
1815   if (match(Expo, m_AnyZeroFP()))
1816     return ConstantFP::get(Ty, 1.0);
1817 
1818   // pow(x, 1.0) -> x
1819   if (match(Expo, m_FPOne()))
1820     return Base;
1821 
1822   // pow(x, 2.0) -> x * x
1823   if (match(Expo, m_SpecificFP(2.0)))
1824     return B.CreateFMul(Base, Base, "square");
1825 
1826   if (Value *Sqrt = replacePowWithSqrt(Pow, B))
1827     return Sqrt;
1828 
1829   // pow(x, n) -> x * x * x * ...
1830   const APFloat *ExpoF;
1831   if (AllowApprox && match(Expo, m_APFloat(ExpoF)) &&
1832       !ExpoF->isExactlyValue(0.5) && !ExpoF->isExactlyValue(-0.5)) {
1833     // We limit to a max of 7 multiplications, thus the maximum exponent is 32.
1834     // If the exponent is an integer+0.5 we generate a call to sqrt and an
1835     // additional fmul.
1836     // TODO: This whole transformation should be backend specific (e.g. some
1837     //       backends might prefer libcalls or the limit for the exponent might
1838     //       be different) and it should also consider optimizing for size.
1839     APFloat LimF(ExpoF->getSemantics(), 33),
1840             ExpoA(abs(*ExpoF));
1841     if (ExpoA < LimF) {
1842       // This transformation applies to integer or integer+0.5 exponents only.
1843       // For integer+0.5, we create a sqrt(Base) call.
1844       Value *Sqrt = nullptr;
1845       if (!ExpoA.isInteger()) {
1846         APFloat Expo2 = ExpoA;
1847         // To check if ExpoA is an integer + 0.5, we add it to itself. If there
1848         // is no floating point exception and the result is an integer, then
1849         // ExpoA == integer + 0.5
1850         if (Expo2.add(ExpoA, APFloat::rmNearestTiesToEven) != APFloat::opOK)
1851           return nullptr;
1852 
1853         if (!Expo2.isInteger())
1854           return nullptr;
1855 
1856         Sqrt = getSqrtCall(Base, Pow->getCalledFunction()->getAttributes(),
1857                            Pow->doesNotAccessMemory(), M, B, TLI);
1858         if (!Sqrt)
1859           return nullptr;
1860       }
1861 
1862       // We will memoize intermediate products of the Addition Chain.
1863       Value *InnerChain[33] = {nullptr};
1864       InnerChain[1] = Base;
1865       InnerChain[2] = B.CreateFMul(Base, Base, "square");
1866 
1867       // We cannot readily convert a non-double type (like float) to a double.
1868       // So we first convert it to something which could be converted to double.
1869       ExpoA.convert(APFloat::IEEEdouble(), APFloat::rmTowardZero, &Ignored);
1870       Value *FMul = getPow(InnerChain, ExpoA.convertToDouble(), B);
1871 
1872       // Expand pow(x, y+0.5) to pow(x, y) * sqrt(x).
1873       if (Sqrt)
1874         FMul = B.CreateFMul(FMul, Sqrt);
1875 
1876       // If the exponent is negative, then get the reciprocal.
1877       if (ExpoF->isNegative())
1878         FMul = B.CreateFDiv(ConstantFP::get(Ty, 1.0), FMul, "reciprocal");
1879 
1880       return FMul;
1881     }
1882 
1883     APSInt IntExpo(TLI->getIntSize(), /*isUnsigned=*/false);
1884     // powf(x, n) -> powi(x, n) if n is a constant signed integer value
1885     if (ExpoF->isInteger() &&
1886         ExpoF->convertToInteger(IntExpo, APFloat::rmTowardZero, &Ignored) ==
1887             APFloat::opOK) {
1888       return copyFlags(
1889           *Pow,
1890           createPowWithIntegerExponent(
1891               Base, ConstantInt::get(B.getIntNTy(TLI->getIntSize()), IntExpo),
1892               M, B));
1893     }
1894   }
1895 
1896   // powf(x, itofp(y)) -> powi(x, y)
1897   if (AllowApprox && (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo))) {
1898     if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize()))
1899       return copyFlags(*Pow, createPowWithIntegerExponent(Base, ExpoI, M, B));
1900   }
1901 
1902   // Shrink pow() to powf() if the arguments are single precision,
1903   // unless the result is expected to be double precision.
1904   if (UnsafeFPShrink && Name == TLI->getName(LibFunc_pow) &&
1905       hasFloatVersion(M, Name)) {
1906     if (Value *Shrunk = optimizeBinaryDoubleFP(Pow, B, TLI, true))
1907       return Shrunk;
1908   }
1909 
1910   return nullptr;
1911 }
1912 
1913 Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
1914   Module *M = CI->getModule();
1915   Function *Callee = CI->getCalledFunction();
1916   AttributeList Attrs; // Attributes are only meaningful on the original call
1917   StringRef Name = Callee->getName();
1918   Value *Ret = nullptr;
1919   if (UnsafeFPShrink && Name == TLI->getName(LibFunc_exp2) &&
1920       hasFloatVersion(M, Name))
1921     Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
1922 
1923   Type *Ty = CI->getType();
1924   Value *Op = CI->getArgOperand(0);
1925 
1926   // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x))  if sizeof(x) <= IntSize
1927   // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x))  if sizeof(x) < IntSize
1928   if ((isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op)) &&
1929       hasFloatFn(M, TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl)) {
1930     if (Value *Exp = getIntToFPVal(Op, B, TLI->getIntSize()))
1931       return emitBinaryFloatFnCall(ConstantFP::get(Ty, 1.0), Exp, TLI,
1932                                    LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl,
1933                                    B, Attrs);
1934   }
1935 
1936   return Ret;
1937 }
1938 
1939 Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) {
1940   Module *M = CI->getModule();
1941 
1942   // If we can shrink the call to a float function rather than a double
1943   // function, do that first.
1944   Function *Callee = CI->getCalledFunction();
1945   StringRef Name = Callee->getName();
1946   if ((Name == "fmin" || Name == "fmax") && hasFloatVersion(M, Name))
1947     if (Value *Ret = optimizeBinaryDoubleFP(CI, B, TLI))
1948       return Ret;
1949 
1950   // The LLVM intrinsics minnum/maxnum correspond to fmin/fmax. Canonicalize to
1951   // the intrinsics for improved optimization (for example, vectorization).
1952   // No-signed-zeros is implied by the definitions of fmax/fmin themselves.
1953   // From the C standard draft WG14/N1256:
1954   // "Ideally, fmax would be sensitive to the sign of zero, for example
1955   // fmax(-0.0, +0.0) would return +0; however, implementation in software
1956   // might be impractical."
1957   IRBuilderBase::FastMathFlagGuard Guard(B);
1958   FastMathFlags FMF = CI->getFastMathFlags();
1959   FMF.setNoSignedZeros();
1960   B.setFastMathFlags(FMF);
1961 
1962   Intrinsic::ID IID = Callee->getName().startswith("fmin") ? Intrinsic::minnum
1963                                                            : Intrinsic::maxnum;
1964   Function *F = Intrinsic::getDeclaration(CI->getModule(), IID, CI->getType());
1965   return copyFlags(
1966       *CI, B.CreateCall(F, {CI->getArgOperand(0), CI->getArgOperand(1)}));
1967 }
1968 
1969 Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
1970   Function *LogFn = Log->getCalledFunction();
1971   AttributeList Attrs; // Attributes are only meaningful on the original call
1972   StringRef LogNm = LogFn->getName();
1973   Intrinsic::ID LogID = LogFn->getIntrinsicID();
1974   Module *Mod = Log->getModule();
1975   Type *Ty = Log->getType();
1976   Value *Ret = nullptr;
1977 
1978   if (UnsafeFPShrink && hasFloatVersion(Mod, LogNm))
1979     Ret = optimizeUnaryDoubleFP(Log, B, TLI, true);
1980 
1981   // The earlier call must also be 'fast' in order to do these transforms.
1982   CallInst *Arg = dyn_cast<CallInst>(Log->getArgOperand(0));
1983   if (!Log->isFast() || !Arg || !Arg->isFast() || !Arg->hasOneUse())
1984     return Ret;
1985 
1986   LibFunc LogLb, ExpLb, Exp2Lb, Exp10Lb, PowLb;
1987 
1988   // This is only applicable to log(), log2(), log10().
1989   if (TLI->getLibFunc(LogNm, LogLb))
1990     switch (LogLb) {
1991     case LibFunc_logf:
1992       LogID = Intrinsic::log;
1993       ExpLb = LibFunc_expf;
1994       Exp2Lb = LibFunc_exp2f;
1995       Exp10Lb = LibFunc_exp10f;
1996       PowLb = LibFunc_powf;
1997       break;
1998     case LibFunc_log:
1999       LogID = Intrinsic::log;
2000       ExpLb = LibFunc_exp;
2001       Exp2Lb = LibFunc_exp2;
2002       Exp10Lb = LibFunc_exp10;
2003       PowLb = LibFunc_pow;
2004       break;
2005     case LibFunc_logl:
2006       LogID = Intrinsic::log;
2007       ExpLb = LibFunc_expl;
2008       Exp2Lb = LibFunc_exp2l;
2009       Exp10Lb = LibFunc_exp10l;
2010       PowLb = LibFunc_powl;
2011       break;
2012     case LibFunc_log2f:
2013       LogID = Intrinsic::log2;
2014       ExpLb = LibFunc_expf;
2015       Exp2Lb = LibFunc_exp2f;
2016       Exp10Lb = LibFunc_exp10f;
2017       PowLb = LibFunc_powf;
2018       break;
2019     case LibFunc_log2:
2020       LogID = Intrinsic::log2;
2021       ExpLb = LibFunc_exp;
2022       Exp2Lb = LibFunc_exp2;
2023       Exp10Lb = LibFunc_exp10;
2024       PowLb = LibFunc_pow;
2025       break;
2026     case LibFunc_log2l:
2027       LogID = Intrinsic::log2;
2028       ExpLb = LibFunc_expl;
2029       Exp2Lb = LibFunc_exp2l;
2030       Exp10Lb = LibFunc_exp10l;
2031       PowLb = LibFunc_powl;
2032       break;
2033     case LibFunc_log10f:
2034       LogID = Intrinsic::log10;
2035       ExpLb = LibFunc_expf;
2036       Exp2Lb = LibFunc_exp2f;
2037       Exp10Lb = LibFunc_exp10f;
2038       PowLb = LibFunc_powf;
2039       break;
2040     case LibFunc_log10:
2041       LogID = Intrinsic::log10;
2042       ExpLb = LibFunc_exp;
2043       Exp2Lb = LibFunc_exp2;
2044       Exp10Lb = LibFunc_exp10;
2045       PowLb = LibFunc_pow;
2046       break;
2047     case LibFunc_log10l:
2048       LogID = Intrinsic::log10;
2049       ExpLb = LibFunc_expl;
2050       Exp2Lb = LibFunc_exp2l;
2051       Exp10Lb = LibFunc_exp10l;
2052       PowLb = LibFunc_powl;
2053       break;
2054     default:
2055       return Ret;
2056     }
2057   else if (LogID == Intrinsic::log || LogID == Intrinsic::log2 ||
2058            LogID == Intrinsic::log10) {
2059     if (Ty->getScalarType()->isFloatTy()) {
2060       ExpLb = LibFunc_expf;
2061       Exp2Lb = LibFunc_exp2f;
2062       Exp10Lb = LibFunc_exp10f;
2063       PowLb = LibFunc_powf;
2064     } else if (Ty->getScalarType()->isDoubleTy()) {
2065       ExpLb = LibFunc_exp;
2066       Exp2Lb = LibFunc_exp2;
2067       Exp10Lb = LibFunc_exp10;
2068       PowLb = LibFunc_pow;
2069     } else
2070       return Ret;
2071   } else
2072     return Ret;
2073 
2074   IRBuilderBase::FastMathFlagGuard Guard(B);
2075   B.setFastMathFlags(FastMathFlags::getFast());
2076 
2077   Intrinsic::ID ArgID = Arg->getIntrinsicID();
2078   LibFunc ArgLb = NotLibFunc;
2079   TLI->getLibFunc(*Arg, ArgLb);
2080 
2081   // log(pow(x,y)) -> y*log(x)
2082   if (ArgLb == PowLb || ArgID == Intrinsic::pow) {
2083     Value *LogX =
2084         Log->doesNotAccessMemory()
2085             ? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty),
2086                            Arg->getOperand(0), "log")
2087             : emitUnaryFloatFnCall(Arg->getOperand(0), TLI, LogNm, B, Attrs);
2088     Value *MulY = B.CreateFMul(Arg->getArgOperand(1), LogX, "mul");
2089     // Since pow() may have side effects, e.g. errno,
2090     // dead code elimination may not be trusted to remove it.
2091     substituteInParent(Arg, MulY);
2092     return MulY;
2093   }
2094 
2095   // log(exp{,2,10}(y)) -> y*log({e,2,10})
2096   // TODO: There is no exp10() intrinsic yet.
2097   if (ArgLb == ExpLb || ArgLb == Exp2Lb || ArgLb == Exp10Lb ||
2098            ArgID == Intrinsic::exp || ArgID == Intrinsic::exp2) {
2099     Constant *Eul;
2100     if (ArgLb == ExpLb || ArgID == Intrinsic::exp)
2101       // FIXME: Add more precise value of e for long double.
2102       Eul = ConstantFP::get(Log->getType(), numbers::e);
2103     else if (ArgLb == Exp2Lb || ArgID == Intrinsic::exp2)
2104       Eul = ConstantFP::get(Log->getType(), 2.0);
2105     else
2106       Eul = ConstantFP::get(Log->getType(), 10.0);
2107     Value *LogE = Log->doesNotAccessMemory()
2108                       ? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty),
2109                                      Eul, "log")
2110                       : emitUnaryFloatFnCall(Eul, TLI, LogNm, B, Attrs);
2111     Value *MulY = B.CreateFMul(Arg->getArgOperand(0), LogE, "mul");
2112     // Since exp() may have side effects, e.g. errno,
2113     // dead code elimination may not be trusted to remove it.
2114     substituteInParent(Arg, MulY);
2115     return MulY;
2116   }
2117 
2118   return Ret;
2119 }
2120 
2121 Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
2122   Module *M = CI->getModule();
2123   Function *Callee = CI->getCalledFunction();
2124   Value *Ret = nullptr;
2125   // TODO: Once we have a way (other than checking for the existince of the
2126   // libcall) to tell whether our target can lower @llvm.sqrt, relax the
2127   // condition below.
2128   if (isLibFuncEmittable(M, TLI, LibFunc_sqrtf) &&
2129       (Callee->getName() == "sqrt" ||
2130        Callee->getIntrinsicID() == Intrinsic::sqrt))
2131     Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2132 
2133   if (!CI->isFast())
2134     return Ret;
2135 
2136   Instruction *I = dyn_cast<Instruction>(CI->getArgOperand(0));
2137   if (!I || I->getOpcode() != Instruction::FMul || !I->isFast())
2138     return Ret;
2139 
2140   // We're looking for a repeated factor in a multiplication tree,
2141   // so we can do this fold: sqrt(x * x) -> fabs(x);
2142   // or this fold: sqrt((x * x) * y) -> fabs(x) * sqrt(y).
2143   Value *Op0 = I->getOperand(0);
2144   Value *Op1 = I->getOperand(1);
2145   Value *RepeatOp = nullptr;
2146   Value *OtherOp = nullptr;
2147   if (Op0 == Op1) {
2148     // Simple match: the operands of the multiply are identical.
2149     RepeatOp = Op0;
2150   } else {
2151     // Look for a more complicated pattern: one of the operands is itself
2152     // a multiply, so search for a common factor in that multiply.
2153     // Note: We don't bother looking any deeper than this first level or for
2154     // variations of this pattern because instcombine's visitFMUL and/or the
2155     // reassociation pass should give us this form.
2156     Value *OtherMul0, *OtherMul1;
2157     if (match(Op0, m_FMul(m_Value(OtherMul0), m_Value(OtherMul1)))) {
2158       // Pattern: sqrt((x * y) * z)
2159       if (OtherMul0 == OtherMul1 && cast<Instruction>(Op0)->isFast()) {
2160         // Matched: sqrt((x * x) * z)
2161         RepeatOp = OtherMul0;
2162         OtherOp = Op1;
2163       }
2164     }
2165   }
2166   if (!RepeatOp)
2167     return Ret;
2168 
2169   // Fast math flags for any created instructions should match the sqrt
2170   // and multiply.
2171   IRBuilderBase::FastMathFlagGuard Guard(B);
2172   B.setFastMathFlags(I->getFastMathFlags());
2173 
2174   // If we found a repeated factor, hoist it out of the square root and
2175   // replace it with the fabs of that factor.
2176   Type *ArgType = I->getType();
2177   Function *Fabs = Intrinsic::getDeclaration(M, Intrinsic::fabs, ArgType);
2178   Value *FabsCall = B.CreateCall(Fabs, RepeatOp, "fabs");
2179   if (OtherOp) {
2180     // If we found a non-repeated factor, we still need to get its square
2181     // root. We then multiply that by the value that was simplified out
2182     // of the square root calculation.
2183     Function *Sqrt = Intrinsic::getDeclaration(M, Intrinsic::sqrt, ArgType);
2184     Value *SqrtCall = B.CreateCall(Sqrt, OtherOp, "sqrt");
2185     return copyFlags(*CI, B.CreateFMul(FabsCall, SqrtCall));
2186   }
2187   return copyFlags(*CI, FabsCall);
2188 }
2189 
2190 // TODO: Generalize to handle any trig function and its inverse.
2191 Value *LibCallSimplifier::optimizeTan(CallInst *CI, IRBuilderBase &B) {
2192   Module *M = CI->getModule();
2193   Function *Callee = CI->getCalledFunction();
2194   Value *Ret = nullptr;
2195   StringRef Name = Callee->getName();
2196   if (UnsafeFPShrink && Name == "tan" && hasFloatVersion(M, Name))
2197     Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2198 
2199   Value *Op1 = CI->getArgOperand(0);
2200   auto *OpC = dyn_cast<CallInst>(Op1);
2201   if (!OpC)
2202     return Ret;
2203 
2204   // Both calls must be 'fast' in order to remove them.
2205   if (!CI->isFast() || !OpC->isFast())
2206     return Ret;
2207 
2208   // tan(atan(x)) -> x
2209   // tanf(atanf(x)) -> x
2210   // tanl(atanl(x)) -> x
2211   LibFunc Func;
2212   Function *F = OpC->getCalledFunction();
2213   if (F && TLI->getLibFunc(F->getName(), Func) &&
2214       isLibFuncEmittable(M, TLI, Func) &&
2215       ((Func == LibFunc_atan && Callee->getName() == "tan") ||
2216        (Func == LibFunc_atanf && Callee->getName() == "tanf") ||
2217        (Func == LibFunc_atanl && Callee->getName() == "tanl")))
2218     Ret = OpC->getArgOperand(0);
2219   return Ret;
2220 }
2221 
2222 static bool isTrigLibCall(CallInst *CI) {
2223   // We can only hope to do anything useful if we can ignore things like errno
2224   // and floating-point exceptions.
2225   // We already checked the prototype.
2226   return CI->hasFnAttr(Attribute::NoUnwind) &&
2227          CI->hasFnAttr(Attribute::ReadNone);
2228 }
2229 
2230 static bool insertSinCosCall(IRBuilderBase &B, Function *OrigCallee, Value *Arg,
2231                              bool UseFloat, Value *&Sin, Value *&Cos,
2232                              Value *&SinCos, const TargetLibraryInfo *TLI) {
2233   Module *M = OrigCallee->getParent();
2234   Type *ArgTy = Arg->getType();
2235   Type *ResTy;
2236   StringRef Name;
2237 
2238   Triple T(OrigCallee->getParent()->getTargetTriple());
2239   if (UseFloat) {
2240     Name = "__sincospif_stret";
2241 
2242     assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");
2243     // x86_64 can't use {float, float} since that would be returned in both
2244     // xmm0 and xmm1, which isn't what a real struct would do.
2245     ResTy = T.getArch() == Triple::x86_64
2246                 ? static_cast<Type *>(FixedVectorType::get(ArgTy, 2))
2247                 : static_cast<Type *>(StructType::get(ArgTy, ArgTy));
2248   } else {
2249     Name = "__sincospi_stret";
2250     ResTy = StructType::get(ArgTy, ArgTy);
2251   }
2252 
2253   if (!isLibFuncEmittable(M, TLI, Name))
2254     return false;
2255   LibFunc TheLibFunc;
2256   TLI->getLibFunc(Name, TheLibFunc);
2257   FunctionCallee Callee = getOrInsertLibFunc(
2258       M, *TLI, TheLibFunc, OrigCallee->getAttributes(), ResTy, ArgTy);
2259 
2260   if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
2261     // If the argument is an instruction, it must dominate all uses so put our
2262     // sincos call there.
2263     B.SetInsertPoint(ArgInst->getParent(), ++ArgInst->getIterator());
2264   } else {
2265     // Otherwise (e.g. for a constant) the beginning of the function is as
2266     // good a place as any.
2267     BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
2268     B.SetInsertPoint(&EntryBB, EntryBB.begin());
2269   }
2270 
2271   SinCos = B.CreateCall(Callee, Arg, "sincospi");
2272 
2273   if (SinCos->getType()->isStructTy()) {
2274     Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
2275     Cos = B.CreateExtractValue(SinCos, 1, "cospi");
2276   } else {
2277     Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
2278                                  "sinpi");
2279     Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
2280                                  "cospi");
2281   }
2282 
2283   return true;
2284 }
2285 
2286 Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, IRBuilderBase &B) {
2287   // Make sure the prototype is as expected, otherwise the rest of the
2288   // function is probably invalid and likely to abort.
2289   if (!isTrigLibCall(CI))
2290     return nullptr;
2291 
2292   Value *Arg = CI->getArgOperand(0);
2293   SmallVector<CallInst *, 1> SinCalls;
2294   SmallVector<CallInst *, 1> CosCalls;
2295   SmallVector<CallInst *, 1> SinCosCalls;
2296 
2297   bool IsFloat = Arg->getType()->isFloatTy();
2298 
2299   // Look for all compatible sinpi, cospi and sincospi calls with the same
2300   // argument. If there are enough (in some sense) we can make the
2301   // substitution.
2302   Function *F = CI->getFunction();
2303   for (User *U : Arg->users())
2304     classifyArgUse(U, F, IsFloat, SinCalls, CosCalls, SinCosCalls);
2305 
2306   // It's only worthwhile if both sinpi and cospi are actually used.
2307   if (SinCalls.empty() || CosCalls.empty())
2308     return nullptr;
2309 
2310   Value *Sin, *Cos, *SinCos;
2311   if (!insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos,
2312                         SinCos, TLI))
2313     return nullptr;
2314 
2315   auto replaceTrigInsts = [this](SmallVectorImpl<CallInst *> &Calls,
2316                                  Value *Res) {
2317     for (CallInst *C : Calls)
2318       replaceAllUsesWith(C, Res);
2319   };
2320 
2321   replaceTrigInsts(SinCalls, Sin);
2322   replaceTrigInsts(CosCalls, Cos);
2323   replaceTrigInsts(SinCosCalls, SinCos);
2324 
2325   return nullptr;
2326 }
2327 
2328 void LibCallSimplifier::classifyArgUse(
2329     Value *Val, Function *F, bool IsFloat,
2330     SmallVectorImpl<CallInst *> &SinCalls,
2331     SmallVectorImpl<CallInst *> &CosCalls,
2332     SmallVectorImpl<CallInst *> &SinCosCalls) {
2333   CallInst *CI = dyn_cast<CallInst>(Val);
2334   Module *M = CI->getModule();
2335 
2336   if (!CI || CI->use_empty())
2337     return;
2338 
2339   // Don't consider calls in other functions.
2340   if (CI->getFunction() != F)
2341     return;
2342 
2343   Function *Callee = CI->getCalledFunction();
2344   LibFunc Func;
2345   if (!Callee || !TLI->getLibFunc(*Callee, Func) ||
2346       !isLibFuncEmittable(M, TLI, Func) ||
2347       !isTrigLibCall(CI))
2348     return;
2349 
2350   if (IsFloat) {
2351     if (Func == LibFunc_sinpif)
2352       SinCalls.push_back(CI);
2353     else if (Func == LibFunc_cospif)
2354       CosCalls.push_back(CI);
2355     else if (Func == LibFunc_sincospif_stret)
2356       SinCosCalls.push_back(CI);
2357   } else {
2358     if (Func == LibFunc_sinpi)
2359       SinCalls.push_back(CI);
2360     else if (Func == LibFunc_cospi)
2361       CosCalls.push_back(CI);
2362     else if (Func == LibFunc_sincospi_stret)
2363       SinCosCalls.push_back(CI);
2364   }
2365 }
2366 
2367 //===----------------------------------------------------------------------===//
2368 // Integer Library Call Optimizations
2369 //===----------------------------------------------------------------------===//
2370 
2371 Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilderBase &B) {
2372   // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
2373   Value *Op = CI->getArgOperand(0);
2374   Type *ArgType = Op->getType();
2375   Function *F = Intrinsic::getDeclaration(CI->getCalledFunction()->getParent(),
2376                                           Intrinsic::cttz, ArgType);
2377   Value *V = B.CreateCall(F, {Op, B.getTrue()}, "cttz");
2378   V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
2379   V = B.CreateIntCast(V, B.getInt32Ty(), false);
2380 
2381   Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
2382   return B.CreateSelect(Cond, V, B.getInt32(0));
2383 }
2384 
2385 Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilderBase &B) {
2386   // fls(x) -> (i32)(sizeInBits(x) - llvm.ctlz(x, false))
2387   Value *Op = CI->getArgOperand(0);
2388   Type *ArgType = Op->getType();
2389   Function *F = Intrinsic::getDeclaration(CI->getCalledFunction()->getParent(),
2390                                           Intrinsic::ctlz, ArgType);
2391   Value *V = B.CreateCall(F, {Op, B.getFalse()}, "ctlz");
2392   V = B.CreateSub(ConstantInt::get(V->getType(), ArgType->getIntegerBitWidth()),
2393                   V);
2394   return B.CreateIntCast(V, CI->getType(), false);
2395 }
2396 
2397 Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilderBase &B) {
2398   // abs(x) -> x <s 0 ? -x : x
2399   // The negation has 'nsw' because abs of INT_MIN is undefined.
2400   Value *X = CI->getArgOperand(0);
2401   Value *IsNeg = B.CreateIsNeg(X);
2402   Value *NegX = B.CreateNSWNeg(X, "neg");
2403   return B.CreateSelect(IsNeg, NegX, X);
2404 }
2405 
2406 Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilderBase &B) {
2407   // isdigit(c) -> (c-'0') <u 10
2408   Value *Op = CI->getArgOperand(0);
2409   Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
2410   Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
2411   return B.CreateZExt(Op, CI->getType());
2412 }
2413 
2414 Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilderBase &B) {
2415   // isascii(c) -> c <u 128
2416   Value *Op = CI->getArgOperand(0);
2417   Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
2418   return B.CreateZExt(Op, CI->getType());
2419 }
2420 
2421 Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilderBase &B) {
2422   // toascii(c) -> c & 0x7f
2423   return B.CreateAnd(CI->getArgOperand(0),
2424                      ConstantInt::get(CI->getType(), 0x7F));
2425 }
2426 
2427 Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilderBase &B) {
2428   StringRef Str;
2429   if (!getConstantStringInfo(CI->getArgOperand(0), Str))
2430     return nullptr;
2431 
2432   return convertStrToNumber(CI, Str, 10);
2433 }
2434 
2435 Value *LibCallSimplifier::optimizeStrtol(CallInst *CI, IRBuilderBase &B) {
2436   StringRef Str;
2437   if (!getConstantStringInfo(CI->getArgOperand(0), Str))
2438     return nullptr;
2439 
2440   if (!isa<ConstantPointerNull>(CI->getArgOperand(1)))
2441     return nullptr;
2442 
2443   if (ConstantInt *CInt = dyn_cast<ConstantInt>(CI->getArgOperand(2))) {
2444     return convertStrToNumber(CI, Str, CInt->getSExtValue());
2445   }
2446 
2447   return nullptr;
2448 }
2449 
2450 //===----------------------------------------------------------------------===//
2451 // Formatting and IO Library Call Optimizations
2452 //===----------------------------------------------------------------------===//
2453 
2454 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
2455 
2456 Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilderBase &B,
2457                                                  int StreamArg) {
2458   Function *Callee = CI->getCalledFunction();
2459   // Error reporting calls should be cold, mark them as such.
2460   // This applies even to non-builtin calls: it is only a hint and applies to
2461   // functions that the frontend might not understand as builtins.
2462 
2463   // This heuristic was suggested in:
2464   // Improving Static Branch Prediction in a Compiler
2465   // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu
2466   // Proceedings of PACT'98, Oct. 1998, IEEE
2467   if (!CI->hasFnAttr(Attribute::Cold) &&
2468       isReportingError(Callee, CI, StreamArg)) {
2469     CI->addFnAttr(Attribute::Cold);
2470   }
2471 
2472   return nullptr;
2473 }
2474 
2475 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {
2476   if (!Callee || !Callee->isDeclaration())
2477     return false;
2478 
2479   if (StreamArg < 0)
2480     return true;
2481 
2482   // These functions might be considered cold, but only if their stream
2483   // argument is stderr.
2484 
2485   if (StreamArg >= (int)CI->arg_size())
2486     return false;
2487   LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
2488   if (!LI)
2489     return false;
2490   GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
2491   if (!GV || !GV->isDeclaration())
2492     return false;
2493   return GV->getName() == "stderr";
2494 }
2495 
2496 Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
2497   // Check for a fixed format string.
2498   StringRef FormatStr;
2499   if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
2500     return nullptr;
2501 
2502   // Empty format string -> noop.
2503   if (FormatStr.empty()) // Tolerate printf's declared void.
2504     return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);
2505 
2506   // Do not do any of the following transformations if the printf return value
2507   // is used, in general the printf return value is not compatible with either
2508   // putchar() or puts().
2509   if (!CI->use_empty())
2510     return nullptr;
2511 
2512   // printf("x") -> putchar('x'), even for "%" and "%%".
2513   if (FormatStr.size() == 1 || FormatStr == "%%")
2514     return copyFlags(*CI, emitPutChar(B.getInt32(FormatStr[0]), B, TLI));
2515 
2516   // Try to remove call or emit putchar/puts.
2517   if (FormatStr == "%s" && CI->arg_size() > 1) {
2518     StringRef OperandStr;
2519     if (!getConstantStringInfo(CI->getOperand(1), OperandStr))
2520       return nullptr;
2521     // printf("%s", "") --> NOP
2522     if (OperandStr.empty())
2523       return (Value *)CI;
2524     // printf("%s", "a") --> putchar('a')
2525     if (OperandStr.size() == 1)
2526       return copyFlags(*CI, emitPutChar(B.getInt32(OperandStr[0]), B, TLI));
2527     // printf("%s", str"\n") --> puts(str)
2528     if (OperandStr.back() == '\n') {
2529       OperandStr = OperandStr.drop_back();
2530       Value *GV = B.CreateGlobalString(OperandStr, "str");
2531       return copyFlags(*CI, emitPutS(GV, B, TLI));
2532     }
2533     return nullptr;
2534   }
2535 
2536   // printf("foo\n") --> puts("foo")
2537   if (FormatStr.back() == '\n' &&
2538       !FormatStr.contains('%')) { // No format characters.
2539     // Create a string literal with no \n on it.  We expect the constant merge
2540     // pass to be run after this pass, to merge duplicate strings.
2541     FormatStr = FormatStr.drop_back();
2542     Value *GV = B.CreateGlobalString(FormatStr, "str");
2543     return copyFlags(*CI, emitPutS(GV, B, TLI));
2544   }
2545 
2546   // Optimize specific format strings.
2547   // printf("%c", chr) --> putchar(chr)
2548   if (FormatStr == "%c" && CI->arg_size() > 1 &&
2549       CI->getArgOperand(1)->getType()->isIntegerTy())
2550     return copyFlags(*CI, emitPutChar(CI->getArgOperand(1), B, TLI));
2551 
2552   // printf("%s\n", str) --> puts(str)
2553   if (FormatStr == "%s\n" && CI->arg_size() > 1 &&
2554       CI->getArgOperand(1)->getType()->isPointerTy())
2555     return copyFlags(*CI, emitPutS(CI->getArgOperand(1), B, TLI));
2556   return nullptr;
2557 }
2558 
2559 Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilderBase &B) {
2560 
2561   Module *M = CI->getModule();
2562   Function *Callee = CI->getCalledFunction();
2563   FunctionType *FT = Callee->getFunctionType();
2564   if (Value *V = optimizePrintFString(CI, B)) {
2565     return V;
2566   }
2567 
2568   // printf(format, ...) -> iprintf(format, ...) if no floating point
2569   // arguments.
2570   if (isLibFuncEmittable(M, TLI, LibFunc_iprintf) &&
2571       !callHasFloatingPointArgument(CI)) {
2572     FunctionCallee IPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_iprintf, FT,
2573                                                   Callee->getAttributes());
2574     CallInst *New = cast<CallInst>(CI->clone());
2575     New->setCalledFunction(IPrintFFn);
2576     B.Insert(New);
2577     return New;
2578   }
2579 
2580   // printf(format, ...) -> __small_printf(format, ...) if no 128-bit floating point
2581   // arguments.
2582   if (isLibFuncEmittable(M, TLI, LibFunc_small_printf) &&
2583       !callHasFP128Argument(CI)) {
2584     auto SmallPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_small_printf, FT,
2585                                             Callee->getAttributes());
2586     CallInst *New = cast<CallInst>(CI->clone());
2587     New->setCalledFunction(SmallPrintFFn);
2588     B.Insert(New);
2589     return New;
2590   }
2591 
2592   annotateNonNullNoUndefBasedOnAccess(CI, 0);
2593   return nullptr;
2594 }
2595 
2596 Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI,
2597                                                 IRBuilderBase &B) {
2598   // Check for a fixed format string.
2599   StringRef FormatStr;
2600   if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
2601     return nullptr;
2602 
2603   // If we just have a format string (nothing else crazy) transform it.
2604   Value *Dest = CI->getArgOperand(0);
2605   if (CI->arg_size() == 2) {
2606     // Make sure there's no % in the constant array.  We could try to handle
2607     // %% -> % in the future if we cared.
2608     if (FormatStr.contains('%'))
2609       return nullptr; // we found a format specifier, bail out.
2610 
2611     // sprintf(str, fmt) -> llvm.memcpy(align 1 str, align 1 fmt, strlen(fmt)+1)
2612     B.CreateMemCpy(
2613         Dest, Align(1), CI->getArgOperand(1), Align(1),
2614         ConstantInt::get(DL.getIntPtrType(CI->getContext()),
2615                          FormatStr.size() + 1)); // Copy the null byte.
2616     return ConstantInt::get(CI->getType(), FormatStr.size());
2617   }
2618 
2619   // The remaining optimizations require the format string to be "%s" or "%c"
2620   // and have an extra operand.
2621   if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
2622     return nullptr;
2623 
2624   // Decode the second character of the format string.
2625   if (FormatStr[1] == 'c') {
2626     // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
2627     if (!CI->getArgOperand(2)->getType()->isIntegerTy())
2628       return nullptr;
2629     Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
2630     Value *Ptr = castToCStr(Dest, B);
2631     B.CreateStore(V, Ptr);
2632     Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
2633     B.CreateStore(B.getInt8(0), Ptr);
2634 
2635     return ConstantInt::get(CI->getType(), 1);
2636   }
2637 
2638   if (FormatStr[1] == 's') {
2639     // sprintf(dest, "%s", str) -> llvm.memcpy(align 1 dest, align 1 str,
2640     // strlen(str)+1)
2641     if (!CI->getArgOperand(2)->getType()->isPointerTy())
2642       return nullptr;
2643 
2644     if (CI->use_empty())
2645       // sprintf(dest, "%s", str) -> strcpy(dest, str)
2646       return copyFlags(*CI, emitStrCpy(Dest, CI->getArgOperand(2), B, TLI));
2647 
2648     uint64_t SrcLen = GetStringLength(CI->getArgOperand(2));
2649     if (SrcLen) {
2650       B.CreateMemCpy(
2651           Dest, Align(1), CI->getArgOperand(2), Align(1),
2652           ConstantInt::get(DL.getIntPtrType(CI->getContext()), SrcLen));
2653       // Returns total number of characters written without null-character.
2654       return ConstantInt::get(CI->getType(), SrcLen - 1);
2655     } else if (Value *V = emitStpCpy(Dest, CI->getArgOperand(2), B, TLI)) {
2656       // sprintf(dest, "%s", str) -> stpcpy(dest, str) - dest
2657       // Handle mismatched pointer types (goes away with typeless pointers?).
2658       V = B.CreatePointerCast(V, B.getInt8PtrTy());
2659       Dest = B.CreatePointerCast(Dest, B.getInt8PtrTy());
2660       Value *PtrDiff = B.CreatePtrDiff(B.getInt8Ty(), V, Dest);
2661       return B.CreateIntCast(PtrDiff, CI->getType(), false);
2662     }
2663 
2664     bool OptForSize = CI->getFunction()->hasOptSize() ||
2665                       llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
2666                                                   PGSOQueryType::IRPass);
2667     if (OptForSize)
2668       return nullptr;
2669 
2670     Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
2671     if (!Len)
2672       return nullptr;
2673     Value *IncLen =
2674         B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
2675     B.CreateMemCpy(Dest, Align(1), CI->getArgOperand(2), Align(1), IncLen);
2676 
2677     // The sprintf result is the unincremented number of bytes in the string.
2678     return B.CreateIntCast(Len, CI->getType(), false);
2679   }
2680   return nullptr;
2681 }
2682 
2683 Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilderBase &B) {
2684   Module *M = CI->getModule();
2685   Function *Callee = CI->getCalledFunction();
2686   FunctionType *FT = Callee->getFunctionType();
2687   if (Value *V = optimizeSPrintFString(CI, B)) {
2688     return V;
2689   }
2690 
2691   // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
2692   // point arguments.
2693   if (isLibFuncEmittable(M, TLI, LibFunc_siprintf) &&
2694       !callHasFloatingPointArgument(CI)) {
2695     FunctionCallee SIPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_siprintf,
2696                                                    FT, Callee->getAttributes());
2697     CallInst *New = cast<CallInst>(CI->clone());
2698     New->setCalledFunction(SIPrintFFn);
2699     B.Insert(New);
2700     return New;
2701   }
2702 
2703   // sprintf(str, format, ...) -> __small_sprintf(str, format, ...) if no 128-bit
2704   // floating point arguments.
2705   if (isLibFuncEmittable(M, TLI, LibFunc_small_sprintf) &&
2706       !callHasFP128Argument(CI)) {
2707     auto SmallSPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_small_sprintf, FT,
2708                                              Callee->getAttributes());
2709     CallInst *New = cast<CallInst>(CI->clone());
2710     New->setCalledFunction(SmallSPrintFFn);
2711     B.Insert(New);
2712     return New;
2713   }
2714 
2715   annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
2716   return nullptr;
2717 }
2718 
2719 Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI,
2720                                                  IRBuilderBase &B) {
2721   // Check for size
2722   ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
2723   if (!Size)
2724     return nullptr;
2725 
2726   uint64_t N = Size->getZExtValue();
2727   // Check for a fixed format string.
2728   StringRef FormatStr;
2729   if (!getConstantStringInfo(CI->getArgOperand(2), FormatStr))
2730     return nullptr;
2731 
2732   // If we just have a format string (nothing else crazy) transform it.
2733   if (CI->arg_size() == 3) {
2734     // Make sure there's no % in the constant array.  We could try to handle
2735     // %% -> % in the future if we cared.
2736     if (FormatStr.contains('%'))
2737       return nullptr; // we found a format specifier, bail out.
2738 
2739     if (N == 0)
2740       return ConstantInt::get(CI->getType(), FormatStr.size());
2741     else if (N < FormatStr.size() + 1)
2742       return nullptr;
2743 
2744     // snprintf(dst, size, fmt) -> llvm.memcpy(align 1 dst, align 1 fmt,
2745     // strlen(fmt)+1)
2746     copyFlags(
2747         *CI,
2748         B.CreateMemCpy(
2749             CI->getArgOperand(0), Align(1), CI->getArgOperand(2), Align(1),
2750             ConstantInt::get(DL.getIntPtrType(CI->getContext()),
2751                              FormatStr.size() + 1))); // Copy the null byte.
2752     return ConstantInt::get(CI->getType(), FormatStr.size());
2753   }
2754 
2755   // The remaining optimizations require the format string to be "%s" or "%c"
2756   // and have an extra operand.
2757   if (FormatStr.size() == 2 && FormatStr[0] == '%' && CI->arg_size() == 4) {
2758 
2759     // Decode the second character of the format string.
2760     if (FormatStr[1] == 'c') {
2761       if (N == 0)
2762         return ConstantInt::get(CI->getType(), 1);
2763       else if (N == 1)
2764         return nullptr;
2765 
2766       // snprintf(dst, size, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
2767       if (!CI->getArgOperand(3)->getType()->isIntegerTy())
2768         return nullptr;
2769       Value *V = B.CreateTrunc(CI->getArgOperand(3), B.getInt8Ty(), "char");
2770       Value *Ptr = castToCStr(CI->getArgOperand(0), B);
2771       B.CreateStore(V, Ptr);
2772       Ptr = B.CreateGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
2773       B.CreateStore(B.getInt8(0), Ptr);
2774 
2775       return ConstantInt::get(CI->getType(), 1);
2776     }
2777 
2778     if (FormatStr[1] == 's') {
2779       // snprintf(dest, size, "%s", str) to llvm.memcpy(dest, str, len+1, 1)
2780       StringRef Str;
2781       if (!getConstantStringInfo(CI->getArgOperand(3), Str))
2782         return nullptr;
2783 
2784       if (N == 0)
2785         return ConstantInt::get(CI->getType(), Str.size());
2786       else if (N < Str.size() + 1)
2787         return nullptr;
2788 
2789       copyFlags(
2790           *CI, B.CreateMemCpy(CI->getArgOperand(0), Align(1),
2791                               CI->getArgOperand(3), Align(1),
2792                               ConstantInt::get(CI->getType(), Str.size() + 1)));
2793 
2794       // The snprintf result is the unincremented number of bytes in the string.
2795       return ConstantInt::get(CI->getType(), Str.size());
2796     }
2797   }
2798   return nullptr;
2799 }
2800 
2801 Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilderBase &B) {
2802   if (Value *V = optimizeSnPrintFString(CI, B)) {
2803     return V;
2804   }
2805 
2806   if (isKnownNonZero(CI->getOperand(1), DL))
2807     annotateNonNullNoUndefBasedOnAccess(CI, 0);
2808   return nullptr;
2809 }
2810 
2811 Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI,
2812                                                 IRBuilderBase &B) {
2813   optimizeErrorReporting(CI, B, 0);
2814 
2815   // All the optimizations depend on the format string.
2816   StringRef FormatStr;
2817   if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
2818     return nullptr;
2819 
2820   // Do not do any of the following transformations if the fprintf return
2821   // value is used, in general the fprintf return value is not compatible
2822   // with fwrite(), fputc() or fputs().
2823   if (!CI->use_empty())
2824     return nullptr;
2825 
2826   // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
2827   if (CI->arg_size() == 2) {
2828     // Could handle %% -> % if we cared.
2829     if (FormatStr.contains('%'))
2830       return nullptr; // We found a format specifier.
2831 
2832     return copyFlags(
2833         *CI, emitFWrite(CI->getArgOperand(1),
2834                         ConstantInt::get(DL.getIntPtrType(CI->getContext()),
2835                                          FormatStr.size()),
2836                         CI->getArgOperand(0), B, DL, TLI));
2837   }
2838 
2839   // The remaining optimizations require the format string to be "%s" or "%c"
2840   // and have an extra operand.
2841   if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
2842     return nullptr;
2843 
2844   // Decode the second character of the format string.
2845   if (FormatStr[1] == 'c') {
2846     // fprintf(F, "%c", chr) --> fputc(chr, F)
2847     if (!CI->getArgOperand(2)->getType()->isIntegerTy())
2848       return nullptr;
2849     return copyFlags(
2850         *CI, emitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI));
2851   }
2852 
2853   if (FormatStr[1] == 's') {
2854     // fprintf(F, "%s", str) --> fputs(str, F)
2855     if (!CI->getArgOperand(2)->getType()->isPointerTy())
2856       return nullptr;
2857     return copyFlags(
2858         *CI, emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI));
2859   }
2860   return nullptr;
2861 }
2862 
2863 Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilderBase &B) {
2864   Module *M = CI->getModule();
2865   Function *Callee = CI->getCalledFunction();
2866   FunctionType *FT = Callee->getFunctionType();
2867   if (Value *V = optimizeFPrintFString(CI, B)) {
2868     return V;
2869   }
2870 
2871   // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
2872   // floating point arguments.
2873   if (isLibFuncEmittable(M, TLI, LibFunc_fiprintf) &&
2874       !callHasFloatingPointArgument(CI)) {
2875     FunctionCallee FIPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_fiprintf,
2876                                                    FT, Callee->getAttributes());
2877     CallInst *New = cast<CallInst>(CI->clone());
2878     New->setCalledFunction(FIPrintFFn);
2879     B.Insert(New);
2880     return New;
2881   }
2882 
2883   // fprintf(stream, format, ...) -> __small_fprintf(stream, format, ...) if no
2884   // 128-bit floating point arguments.
2885   if (isLibFuncEmittable(M, TLI, LibFunc_small_fprintf) &&
2886       !callHasFP128Argument(CI)) {
2887     auto SmallFPrintFFn =
2888         getOrInsertLibFunc(M, *TLI, LibFunc_small_fprintf, FT,
2889                            Callee->getAttributes());
2890     CallInst *New = cast<CallInst>(CI->clone());
2891     New->setCalledFunction(SmallFPrintFFn);
2892     B.Insert(New);
2893     return New;
2894   }
2895 
2896   return nullptr;
2897 }
2898 
2899 Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilderBase &B) {
2900   optimizeErrorReporting(CI, B, 3);
2901 
2902   // Get the element size and count.
2903   ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
2904   ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
2905   if (SizeC && CountC) {
2906     uint64_t Bytes = SizeC->getZExtValue() * CountC->getZExtValue();
2907 
2908     // If this is writing zero records, remove the call (it's a noop).
2909     if (Bytes == 0)
2910       return ConstantInt::get(CI->getType(), 0);
2911 
2912     // If this is writing one byte, turn it into fputc.
2913     // This optimisation is only valid, if the return value is unused.
2914     if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
2915       Value *Char = B.CreateLoad(B.getInt8Ty(),
2916                                  castToCStr(CI->getArgOperand(0), B), "char");
2917       Value *NewCI = emitFPutC(Char, CI->getArgOperand(3), B, TLI);
2918       return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
2919     }
2920   }
2921 
2922   return nullptr;
2923 }
2924 
2925 Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) {
2926   optimizeErrorReporting(CI, B, 1);
2927 
2928   // Don't rewrite fputs to fwrite when optimising for size because fwrite
2929   // requires more arguments and thus extra MOVs are required.
2930   bool OptForSize = CI->getFunction()->hasOptSize() ||
2931                     llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
2932                                                 PGSOQueryType::IRPass);
2933   if (OptForSize)
2934     return nullptr;
2935 
2936   // We can't optimize if return value is used.
2937   if (!CI->use_empty())
2938     return nullptr;
2939 
2940   // fputs(s,F) --> fwrite(s,strlen(s),1,F)
2941   uint64_t Len = GetStringLength(CI->getArgOperand(0));
2942   if (!Len)
2943     return nullptr;
2944 
2945   // Known to have no uses (see above).
2946   return copyFlags(
2947       *CI,
2948       emitFWrite(CI->getArgOperand(0),
2949                  ConstantInt::get(DL.getIntPtrType(CI->getContext()), Len - 1),
2950                  CI->getArgOperand(1), B, DL, TLI));
2951 }
2952 
2953 Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilderBase &B) {
2954   annotateNonNullNoUndefBasedOnAccess(CI, 0);
2955   if (!CI->use_empty())
2956     return nullptr;
2957 
2958   // Check for a constant string.
2959   // puts("") -> putchar('\n')
2960   StringRef Str;
2961   if (getConstantStringInfo(CI->getArgOperand(0), Str) && Str.empty())
2962     return copyFlags(*CI, emitPutChar(B.getInt32('\n'), B, TLI));
2963 
2964   return nullptr;
2965 }
2966 
2967 Value *LibCallSimplifier::optimizeBCopy(CallInst *CI, IRBuilderBase &B) {
2968   // bcopy(src, dst, n) -> llvm.memmove(dst, src, n)
2969   return copyFlags(*CI, B.CreateMemMove(CI->getArgOperand(1), Align(1),
2970                                         CI->getArgOperand(0), Align(1),
2971                                         CI->getArgOperand(2)));
2972 }
2973 
2974 bool LibCallSimplifier::hasFloatVersion(const Module *M, StringRef FuncName) {
2975   SmallString<20> FloatFuncName = FuncName;
2976   FloatFuncName += 'f';
2977   return isLibFuncEmittable(M, TLI, FloatFuncName);
2978 }
2979 
2980 Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI,
2981                                                       IRBuilderBase &Builder) {
2982   Module *M = CI->getModule();
2983   LibFunc Func;
2984   Function *Callee = CI->getCalledFunction();
2985   // Check for string/memory library functions.
2986   if (TLI->getLibFunc(*Callee, Func) && isLibFuncEmittable(M, TLI, Func)) {
2987     // Make sure we never change the calling convention.
2988     assert(
2989         (ignoreCallingConv(Func) ||
2990          TargetLibraryInfoImpl::isCallingConvCCompatible(CI)) &&
2991         "Optimizing string/memory libcall would change the calling convention");
2992     switch (Func) {
2993     case LibFunc_strcat:
2994       return optimizeStrCat(CI, Builder);
2995     case LibFunc_strncat:
2996       return optimizeStrNCat(CI, Builder);
2997     case LibFunc_strchr:
2998       return optimizeStrChr(CI, Builder);
2999     case LibFunc_strrchr:
3000       return optimizeStrRChr(CI, Builder);
3001     case LibFunc_strcmp:
3002       return optimizeStrCmp(CI, Builder);
3003     case LibFunc_strncmp:
3004       return optimizeStrNCmp(CI, Builder);
3005     case LibFunc_strcpy:
3006       return optimizeStrCpy(CI, Builder);
3007     case LibFunc_stpcpy:
3008       return optimizeStpCpy(CI, Builder);
3009     case LibFunc_strncpy:
3010       return optimizeStrNCpy(CI, Builder);
3011     case LibFunc_strlen:
3012       return optimizeStrLen(CI, Builder);
3013     case LibFunc_strnlen:
3014       return optimizeStrNLen(CI, Builder);
3015     case LibFunc_strpbrk:
3016       return optimizeStrPBrk(CI, Builder);
3017     case LibFunc_strndup:
3018       return optimizeStrNDup(CI, Builder);
3019     case LibFunc_strtol:
3020     case LibFunc_strtod:
3021     case LibFunc_strtof:
3022     case LibFunc_strtoul:
3023     case LibFunc_strtoll:
3024     case LibFunc_strtold:
3025     case LibFunc_strtoull:
3026       return optimizeStrTo(CI, Builder);
3027     case LibFunc_strspn:
3028       return optimizeStrSpn(CI, Builder);
3029     case LibFunc_strcspn:
3030       return optimizeStrCSpn(CI, Builder);
3031     case LibFunc_strstr:
3032       return optimizeStrStr(CI, Builder);
3033     case LibFunc_memchr:
3034       return optimizeMemChr(CI, Builder);
3035     case LibFunc_memrchr:
3036       return optimizeMemRChr(CI, Builder);
3037     case LibFunc_bcmp:
3038       return optimizeBCmp(CI, Builder);
3039     case LibFunc_memcmp:
3040       return optimizeMemCmp(CI, Builder);
3041     case LibFunc_memcpy:
3042       return optimizeMemCpy(CI, Builder);
3043     case LibFunc_memccpy:
3044       return optimizeMemCCpy(CI, Builder);
3045     case LibFunc_mempcpy:
3046       return optimizeMemPCpy(CI, Builder);
3047     case LibFunc_memmove:
3048       return optimizeMemMove(CI, Builder);
3049     case LibFunc_memset:
3050       return optimizeMemSet(CI, Builder);
3051     case LibFunc_realloc:
3052       return optimizeRealloc(CI, Builder);
3053     case LibFunc_wcslen:
3054       return optimizeWcslen(CI, Builder);
3055     case LibFunc_bcopy:
3056       return optimizeBCopy(CI, Builder);
3057     default:
3058       break;
3059     }
3060   }
3061   return nullptr;
3062 }
3063 
3064 Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
3065                                                        LibFunc Func,
3066                                                        IRBuilderBase &Builder) {
3067   const Module *M = CI->getModule();
3068 
3069   // Don't optimize calls that require strict floating point semantics.
3070   if (CI->isStrictFP())
3071     return nullptr;
3072 
3073   if (Value *V = optimizeTrigReflections(CI, Func, Builder))
3074     return V;
3075 
3076   switch (Func) {
3077   case LibFunc_sinpif:
3078   case LibFunc_sinpi:
3079   case LibFunc_cospif:
3080   case LibFunc_cospi:
3081     return optimizeSinCosPi(CI, Builder);
3082   case LibFunc_powf:
3083   case LibFunc_pow:
3084   case LibFunc_powl:
3085     return optimizePow(CI, Builder);
3086   case LibFunc_exp2l:
3087   case LibFunc_exp2:
3088   case LibFunc_exp2f:
3089     return optimizeExp2(CI, Builder);
3090   case LibFunc_fabsf:
3091   case LibFunc_fabs:
3092   case LibFunc_fabsl:
3093     return replaceUnaryCall(CI, Builder, Intrinsic::fabs);
3094   case LibFunc_sqrtf:
3095   case LibFunc_sqrt:
3096   case LibFunc_sqrtl:
3097     return optimizeSqrt(CI, Builder);
3098   case LibFunc_logf:
3099   case LibFunc_log:
3100   case LibFunc_logl:
3101   case LibFunc_log10f:
3102   case LibFunc_log10:
3103   case LibFunc_log10l:
3104   case LibFunc_log1pf:
3105   case LibFunc_log1p:
3106   case LibFunc_log1pl:
3107   case LibFunc_log2f:
3108   case LibFunc_log2:
3109   case LibFunc_log2l:
3110   case LibFunc_logbf:
3111   case LibFunc_logb:
3112   case LibFunc_logbl:
3113     return optimizeLog(CI, Builder);
3114   case LibFunc_tan:
3115   case LibFunc_tanf:
3116   case LibFunc_tanl:
3117     return optimizeTan(CI, Builder);
3118   case LibFunc_ceil:
3119     return replaceUnaryCall(CI, Builder, Intrinsic::ceil);
3120   case LibFunc_floor:
3121     return replaceUnaryCall(CI, Builder, Intrinsic::floor);
3122   case LibFunc_round:
3123     return replaceUnaryCall(CI, Builder, Intrinsic::round);
3124   case LibFunc_roundeven:
3125     return replaceUnaryCall(CI, Builder, Intrinsic::roundeven);
3126   case LibFunc_nearbyint:
3127     return replaceUnaryCall(CI, Builder, Intrinsic::nearbyint);
3128   case LibFunc_rint:
3129     return replaceUnaryCall(CI, Builder, Intrinsic::rint);
3130   case LibFunc_trunc:
3131     return replaceUnaryCall(CI, Builder, Intrinsic::trunc);
3132   case LibFunc_acos:
3133   case LibFunc_acosh:
3134   case LibFunc_asin:
3135   case LibFunc_asinh:
3136   case LibFunc_atan:
3137   case LibFunc_atanh:
3138   case LibFunc_cbrt:
3139   case LibFunc_cosh:
3140   case LibFunc_exp:
3141   case LibFunc_exp10:
3142   case LibFunc_expm1:
3143   case LibFunc_cos:
3144   case LibFunc_sin:
3145   case LibFunc_sinh:
3146   case LibFunc_tanh:
3147     if (UnsafeFPShrink && hasFloatVersion(M, CI->getCalledFunction()->getName()))
3148       return optimizeUnaryDoubleFP(CI, Builder, TLI, true);
3149     return nullptr;
3150   case LibFunc_copysign:
3151     if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
3152       return optimizeBinaryDoubleFP(CI, Builder, TLI);
3153     return nullptr;
3154   case LibFunc_fminf:
3155   case LibFunc_fmin:
3156   case LibFunc_fminl:
3157   case LibFunc_fmaxf:
3158   case LibFunc_fmax:
3159   case LibFunc_fmaxl:
3160     return optimizeFMinFMax(CI, Builder);
3161   case LibFunc_cabs:
3162   case LibFunc_cabsf:
3163   case LibFunc_cabsl:
3164     return optimizeCAbs(CI, Builder);
3165   default:
3166     return nullptr;
3167   }
3168 }
3169 
3170 Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) {
3171   Module *M = CI->getModule();
3172   assert(!CI->isMustTailCall() && "These transforms aren't musttail safe.");
3173 
3174   // TODO: Split out the code below that operates on FP calls so that
3175   //       we can all non-FP calls with the StrictFP attribute to be
3176   //       optimized.
3177   if (CI->isNoBuiltin())
3178     return nullptr;
3179 
3180   LibFunc Func;
3181   Function *Callee = CI->getCalledFunction();
3182   bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
3183 
3184   SmallVector<OperandBundleDef, 2> OpBundles;
3185   CI->getOperandBundlesAsDefs(OpBundles);
3186 
3187   IRBuilderBase::OperandBundlesGuard Guard(Builder);
3188   Builder.setDefaultOperandBundles(OpBundles);
3189 
3190   // Command-line parameter overrides instruction attribute.
3191   // This can't be moved to optimizeFloatingPointLibCall() because it may be
3192   // used by the intrinsic optimizations.
3193   if (EnableUnsafeFPShrink.getNumOccurrences() > 0)
3194     UnsafeFPShrink = EnableUnsafeFPShrink;
3195   else if (isa<FPMathOperator>(CI) && CI->isFast())
3196     UnsafeFPShrink = true;
3197 
3198   // First, check for intrinsics.
3199   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
3200     if (!IsCallingConvC)
3201       return nullptr;
3202     // The FP intrinsics have corresponding constrained versions so we don't
3203     // need to check for the StrictFP attribute here.
3204     switch (II->getIntrinsicID()) {
3205     case Intrinsic::pow:
3206       return optimizePow(CI, Builder);
3207     case Intrinsic::exp2:
3208       return optimizeExp2(CI, Builder);
3209     case Intrinsic::log:
3210     case Intrinsic::log2:
3211     case Intrinsic::log10:
3212       return optimizeLog(CI, Builder);
3213     case Intrinsic::sqrt:
3214       return optimizeSqrt(CI, Builder);
3215     case Intrinsic::memset:
3216       return optimizeMemSet(CI, Builder);
3217     case Intrinsic::memcpy:
3218       return optimizeMemCpy(CI, Builder);
3219     case Intrinsic::memmove:
3220       return optimizeMemMove(CI, Builder);
3221     default:
3222       return nullptr;
3223     }
3224   }
3225 
3226   // Also try to simplify calls to fortified library functions.
3227   if (Value *SimplifiedFortifiedCI =
3228           FortifiedSimplifier.optimizeCall(CI, Builder)) {
3229     // Try to further simplify the result.
3230     CallInst *SimplifiedCI = dyn_cast<CallInst>(SimplifiedFortifiedCI);
3231     if (SimplifiedCI && SimplifiedCI->getCalledFunction()) {
3232       // Ensure that SimplifiedCI's uses are complete, since some calls have
3233       // their uses analyzed.
3234       replaceAllUsesWith(CI, SimplifiedCI);
3235 
3236       // Set insertion point to SimplifiedCI to guarantee we reach all uses
3237       // we might replace later on.
3238       IRBuilderBase::InsertPointGuard Guard(Builder);
3239       Builder.SetInsertPoint(SimplifiedCI);
3240       if (Value *V = optimizeStringMemoryLibCall(SimplifiedCI, Builder)) {
3241         // If we were able to further simplify, remove the now redundant call.
3242         substituteInParent(SimplifiedCI, V);
3243         return V;
3244       }
3245     }
3246     return SimplifiedFortifiedCI;
3247   }
3248 
3249   // Then check for known library functions.
3250   if (TLI->getLibFunc(*Callee, Func) && isLibFuncEmittable(M, TLI, Func)) {
3251     // We never change the calling convention.
3252     if (!ignoreCallingConv(Func) && !IsCallingConvC)
3253       return nullptr;
3254     if (Value *V = optimizeStringMemoryLibCall(CI, Builder))
3255       return V;
3256     if (Value *V = optimizeFloatingPointLibCall(CI, Func, Builder))
3257       return V;
3258     switch (Func) {
3259     case LibFunc_ffs:
3260     case LibFunc_ffsl:
3261     case LibFunc_ffsll:
3262       return optimizeFFS(CI, Builder);
3263     case LibFunc_fls:
3264     case LibFunc_flsl:
3265     case LibFunc_flsll:
3266       return optimizeFls(CI, Builder);
3267     case LibFunc_abs:
3268     case LibFunc_labs:
3269     case LibFunc_llabs:
3270       return optimizeAbs(CI, Builder);
3271     case LibFunc_isdigit:
3272       return optimizeIsDigit(CI, Builder);
3273     case LibFunc_isascii:
3274       return optimizeIsAscii(CI, Builder);
3275     case LibFunc_toascii:
3276       return optimizeToAscii(CI, Builder);
3277     case LibFunc_atoi:
3278     case LibFunc_atol:
3279     case LibFunc_atoll:
3280       return optimizeAtoi(CI, Builder);
3281     case LibFunc_strtol:
3282     case LibFunc_strtoll:
3283       return optimizeStrtol(CI, Builder);
3284     case LibFunc_printf:
3285       return optimizePrintF(CI, Builder);
3286     case LibFunc_sprintf:
3287       return optimizeSPrintF(CI, Builder);
3288     case LibFunc_snprintf:
3289       return optimizeSnPrintF(CI, Builder);
3290     case LibFunc_fprintf:
3291       return optimizeFPrintF(CI, Builder);
3292     case LibFunc_fwrite:
3293       return optimizeFWrite(CI, Builder);
3294     case LibFunc_fputs:
3295       return optimizeFPuts(CI, Builder);
3296     case LibFunc_puts:
3297       return optimizePuts(CI, Builder);
3298     case LibFunc_perror:
3299       return optimizeErrorReporting(CI, Builder);
3300     case LibFunc_vfprintf:
3301     case LibFunc_fiprintf:
3302       return optimizeErrorReporting(CI, Builder, 0);
3303     default:
3304       return nullptr;
3305     }
3306   }
3307   return nullptr;
3308 }
3309 
3310 LibCallSimplifier::LibCallSimplifier(
3311     const DataLayout &DL, const TargetLibraryInfo *TLI,
3312     OptimizationRemarkEmitter &ORE,
3313     BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
3314     function_ref<void(Instruction *, Value *)> Replacer,
3315     function_ref<void(Instruction *)> Eraser)
3316     : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), ORE(ORE), BFI(BFI), PSI(PSI),
3317       Replacer(Replacer), Eraser(Eraser) {}
3318 
3319 void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) {
3320   // Indirect through the replacer used in this instance.
3321   Replacer(I, With);
3322 }
3323 
3324 void LibCallSimplifier::eraseFromParent(Instruction *I) {
3325   Eraser(I);
3326 }
3327 
3328 // TODO:
3329 //   Additional cases that we need to add to this file:
3330 //
3331 // cbrt:
3332 //   * cbrt(expN(X))  -> expN(x/3)
3333 //   * cbrt(sqrt(x))  -> pow(x,1/6)
3334 //   * cbrt(cbrt(x))  -> pow(x,1/9)
3335 //
3336 // exp, expf, expl:
3337 //   * exp(log(x))  -> x
3338 //
3339 // log, logf, logl:
3340 //   * log(exp(x))   -> x
3341 //   * log(exp(y))   -> y*log(e)
3342 //   * log(exp10(y)) -> y*log(10)
3343 //   * log(sqrt(x))  -> 0.5*log(x)
3344 //
3345 // pow, powf, powl:
3346 //   * pow(sqrt(x),y) -> pow(x,y*0.5)
3347 //   * pow(pow(x,y),z)-> pow(x,y*z)
3348 //
3349 // signbit:
3350 //   * signbit(cnst) -> cnst'
3351 //   * signbit(nncst) -> 0 (if pstv is a non-negative constant)
3352 //
3353 // sqrt, sqrtf, sqrtl:
3354 //   * sqrt(expN(x))  -> expN(x*0.5)
3355 //   * sqrt(Nroot(x)) -> pow(x,1/(2*N))
3356 //   * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
3357 //
3358 
3359 //===----------------------------------------------------------------------===//
3360 // Fortified Library Call Optimizations
3361 //===----------------------------------------------------------------------===//
3362 
3363 bool
3364 FortifiedLibCallSimplifier::isFortifiedCallFoldable(CallInst *CI,
3365                                                     unsigned ObjSizeOp,
3366                                                     Optional<unsigned> SizeOp,
3367                                                     Optional<unsigned> StrOp,
3368                                                     Optional<unsigned> FlagOp) {
3369   // If this function takes a flag argument, the implementation may use it to
3370   // perform extra checks. Don't fold into the non-checking variant.
3371   if (FlagOp) {
3372     ConstantInt *Flag = dyn_cast<ConstantInt>(CI->getArgOperand(*FlagOp));
3373     if (!Flag || !Flag->isZero())
3374       return false;
3375   }
3376 
3377   if (SizeOp && CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(*SizeOp))
3378     return true;
3379 
3380   if (ConstantInt *ObjSizeCI =
3381           dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) {
3382     if (ObjSizeCI->isMinusOne())
3383       return true;
3384     // If the object size wasn't -1 (unknown), bail out if we were asked to.
3385     if (OnlyLowerUnknownSize)
3386       return false;
3387     if (StrOp) {
3388       uint64_t Len = GetStringLength(CI->getArgOperand(*StrOp));
3389       // If the length is 0 we don't know how long it is and so we can't
3390       // remove the check.
3391       if (Len)
3392         annotateDereferenceableBytes(CI, *StrOp, Len);
3393       else
3394         return false;
3395       return ObjSizeCI->getZExtValue() >= Len;
3396     }
3397 
3398     if (SizeOp) {
3399       if (ConstantInt *SizeCI =
3400               dyn_cast<ConstantInt>(CI->getArgOperand(*SizeOp)))
3401         return ObjSizeCI->getZExtValue() >= SizeCI->getZExtValue();
3402     }
3403   }
3404   return false;
3405 }
3406 
3407 Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI,
3408                                                      IRBuilderBase &B) {
3409   if (isFortifiedCallFoldable(CI, 3, 2)) {
3410     CallInst *NewCI =
3411         B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
3412                        Align(1), CI->getArgOperand(2));
3413     NewCI->setAttributes(CI->getAttributes());
3414     NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
3415     copyFlags(*CI, NewCI);
3416     return CI->getArgOperand(0);
3417   }
3418   return nullptr;
3419 }
3420 
3421 Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI,
3422                                                       IRBuilderBase &B) {
3423   if (isFortifiedCallFoldable(CI, 3, 2)) {
3424     CallInst *NewCI =
3425         B.CreateMemMove(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
3426                         Align(1), CI->getArgOperand(2));
3427     NewCI->setAttributes(CI->getAttributes());
3428     NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
3429     copyFlags(*CI, NewCI);
3430     return CI->getArgOperand(0);
3431   }
3432   return nullptr;
3433 }
3434 
3435 Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI,
3436                                                      IRBuilderBase &B) {
3437   if (isFortifiedCallFoldable(CI, 3, 2)) {
3438     Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
3439     CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val,
3440                                      CI->getArgOperand(2), Align(1));
3441     NewCI->setAttributes(CI->getAttributes());
3442     NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
3443     copyFlags(*CI, NewCI);
3444     return CI->getArgOperand(0);
3445   }
3446   return nullptr;
3447 }
3448 
3449 Value *FortifiedLibCallSimplifier::optimizeMemPCpyChk(CallInst *CI,
3450                                                       IRBuilderBase &B) {
3451   const DataLayout &DL = CI->getModule()->getDataLayout();
3452   if (isFortifiedCallFoldable(CI, 3, 2))
3453     if (Value *Call = emitMemPCpy(CI->getArgOperand(0), CI->getArgOperand(1),
3454                                   CI->getArgOperand(2), B, DL, TLI)) {
3455       CallInst *NewCI = cast<CallInst>(Call);
3456       NewCI->setAttributes(CI->getAttributes());
3457       NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(NewCI->getType()));
3458       return copyFlags(*CI, NewCI);
3459     }
3460   return nullptr;
3461 }
3462 
3463 Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
3464                                                       IRBuilderBase &B,
3465                                                       LibFunc Func) {
3466   const DataLayout &DL = CI->getModule()->getDataLayout();
3467   Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
3468         *ObjSize = CI->getArgOperand(2);
3469 
3470   // __stpcpy_chk(x,x,...)  -> x+strlen(x)
3471   if (Func == LibFunc_stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) {
3472     Value *StrLen = emitStrLen(Src, B, DL, TLI);
3473     return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
3474   }
3475 
3476   // If a) we don't have any length information, or b) we know this will
3477   // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
3478   // st[rp]cpy_chk call which may fail at runtime if the size is too long.
3479   // TODO: It might be nice to get a maximum length out of the possible
3480   // string lengths for varying.
3481   if (isFortifiedCallFoldable(CI, 2, None, 1)) {
3482     if (Func == LibFunc_strcpy_chk)
3483       return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
3484     else
3485       return copyFlags(*CI, emitStpCpy(Dst, Src, B, TLI));
3486   }
3487 
3488   if (OnlyLowerUnknownSize)
3489     return nullptr;
3490 
3491   // Maybe we can stil fold __st[rp]cpy_chk to __memcpy_chk.
3492   uint64_t Len = GetStringLength(Src);
3493   if (Len)
3494     annotateDereferenceableBytes(CI, 1, Len);
3495   else
3496     return nullptr;
3497 
3498   // FIXME: There is really no guarantee that sizeof(size_t) is equal to
3499   // sizeof(int*) for every target. So the assumption used here to derive the
3500   // SizeTBits based on the size of an integer pointer in address space zero
3501   // isn't always valid.
3502   Type *SizeTTy = DL.getIntPtrType(CI->getContext(), /*AddressSpace=*/0);
3503   Value *LenV = ConstantInt::get(SizeTTy, Len);
3504   Value *Ret = emitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI);
3505   // If the function was an __stpcpy_chk, and we were able to fold it into
3506   // a __memcpy_chk, we still need to return the correct end pointer.
3507   if (Ret && Func == LibFunc_stpcpy_chk)
3508     return B.CreateGEP(B.getInt8Ty(), Dst, ConstantInt::get(SizeTTy, Len - 1));
3509   return copyFlags(*CI, cast<CallInst>(Ret));
3510 }
3511 
3512 Value *FortifiedLibCallSimplifier::optimizeStrLenChk(CallInst *CI,
3513                                                      IRBuilderBase &B) {
3514   if (isFortifiedCallFoldable(CI, 1, None, 0))
3515     return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B,
3516                                      CI->getModule()->getDataLayout(), TLI));
3517   return nullptr;
3518 }
3519 
3520 Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
3521                                                        IRBuilderBase &B,
3522                                                        LibFunc Func) {
3523   if (isFortifiedCallFoldable(CI, 3, 2)) {
3524     if (Func == LibFunc_strncpy_chk)
3525       return copyFlags(*CI,
3526                        emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
3527                                    CI->getArgOperand(2), B, TLI));
3528     else
3529       return copyFlags(*CI,
3530                        emitStpNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
3531                                    CI->getArgOperand(2), B, TLI));
3532   }
3533 
3534   return nullptr;
3535 }
3536 
3537 Value *FortifiedLibCallSimplifier::optimizeMemCCpyChk(CallInst *CI,
3538                                                       IRBuilderBase &B) {
3539   if (isFortifiedCallFoldable(CI, 4, 3))
3540     return copyFlags(
3541         *CI, emitMemCCpy(CI->getArgOperand(0), CI->getArgOperand(1),
3542                          CI->getArgOperand(2), CI->getArgOperand(3), B, TLI));
3543 
3544   return nullptr;
3545 }
3546 
3547 Value *FortifiedLibCallSimplifier::optimizeSNPrintfChk(CallInst *CI,
3548                                                        IRBuilderBase &B) {
3549   if (isFortifiedCallFoldable(CI, 3, 1, None, 2)) {
3550     SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 5));
3551     return copyFlags(*CI,
3552                      emitSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1),
3553                                   CI->getArgOperand(4), VariadicArgs, B, TLI));
3554   }
3555 
3556   return nullptr;
3557 }
3558 
3559 Value *FortifiedLibCallSimplifier::optimizeSPrintfChk(CallInst *CI,
3560                                                       IRBuilderBase &B) {
3561   if (isFortifiedCallFoldable(CI, 2, None, None, 1)) {
3562     SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 4));
3563     return copyFlags(*CI,
3564                      emitSPrintf(CI->getArgOperand(0), CI->getArgOperand(3),
3565                                  VariadicArgs, B, TLI));
3566   }
3567 
3568   return nullptr;
3569 }
3570 
3571 Value *FortifiedLibCallSimplifier::optimizeStrCatChk(CallInst *CI,
3572                                                      IRBuilderBase &B) {
3573   if (isFortifiedCallFoldable(CI, 2))
3574     return copyFlags(
3575         *CI, emitStrCat(CI->getArgOperand(0), CI->getArgOperand(1), B, TLI));
3576 
3577   return nullptr;
3578 }
3579 
3580 Value *FortifiedLibCallSimplifier::optimizeStrLCat(CallInst *CI,
3581                                                    IRBuilderBase &B) {
3582   if (isFortifiedCallFoldable(CI, 3))
3583     return copyFlags(*CI,
3584                      emitStrLCat(CI->getArgOperand(0), CI->getArgOperand(1),
3585                                  CI->getArgOperand(2), B, TLI));
3586 
3587   return nullptr;
3588 }
3589 
3590 Value *FortifiedLibCallSimplifier::optimizeStrNCatChk(CallInst *CI,
3591                                                       IRBuilderBase &B) {
3592   if (isFortifiedCallFoldable(CI, 3))
3593     return copyFlags(*CI,
3594                      emitStrNCat(CI->getArgOperand(0), CI->getArgOperand(1),
3595                                  CI->getArgOperand(2), B, TLI));
3596 
3597   return nullptr;
3598 }
3599 
3600 Value *FortifiedLibCallSimplifier::optimizeStrLCpyChk(CallInst *CI,
3601                                                       IRBuilderBase &B) {
3602   if (isFortifiedCallFoldable(CI, 3))
3603     return copyFlags(*CI,
3604                      emitStrLCpy(CI->getArgOperand(0), CI->getArgOperand(1),
3605                                  CI->getArgOperand(2), B, TLI));
3606 
3607   return nullptr;
3608 }
3609 
3610 Value *FortifiedLibCallSimplifier::optimizeVSNPrintfChk(CallInst *CI,
3611                                                         IRBuilderBase &B) {
3612   if (isFortifiedCallFoldable(CI, 3, 1, None, 2))
3613     return copyFlags(
3614         *CI, emitVSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1),
3615                            CI->getArgOperand(4), CI->getArgOperand(5), B, TLI));
3616 
3617   return nullptr;
3618 }
3619 
3620 Value *FortifiedLibCallSimplifier::optimizeVSPrintfChk(CallInst *CI,
3621                                                        IRBuilderBase &B) {
3622   if (isFortifiedCallFoldable(CI, 2, None, None, 1))
3623     return copyFlags(*CI,
3624                      emitVSPrintf(CI->getArgOperand(0), CI->getArgOperand(3),
3625                                   CI->getArgOperand(4), B, TLI));
3626 
3627   return nullptr;
3628 }
3629 
3630 Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI,
3631                                                 IRBuilderBase &Builder) {
3632   // FIXME: We shouldn't be changing "nobuiltin" or TLI unavailable calls here.
3633   // Some clang users checked for _chk libcall availability using:
3634   //   __has_builtin(__builtin___memcpy_chk)
3635   // When compiling with -fno-builtin, this is always true.
3636   // When passing -ffreestanding/-mkernel, which both imply -fno-builtin, we
3637   // end up with fortified libcalls, which isn't acceptable in a freestanding
3638   // environment which only provides their non-fortified counterparts.
3639   //
3640   // Until we change clang and/or teach external users to check for availability
3641   // differently, disregard the "nobuiltin" attribute and TLI::has.
3642   //
3643   // PR23093.
3644 
3645   LibFunc Func;
3646   Function *Callee = CI->getCalledFunction();
3647   bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
3648 
3649   SmallVector<OperandBundleDef, 2> OpBundles;
3650   CI->getOperandBundlesAsDefs(OpBundles);
3651 
3652   IRBuilderBase::OperandBundlesGuard Guard(Builder);
3653   Builder.setDefaultOperandBundles(OpBundles);
3654 
3655   // First, check that this is a known library functions and that the prototype
3656   // is correct.
3657   if (!TLI->getLibFunc(*Callee, Func))
3658     return nullptr;
3659 
3660   // We never change the calling convention.
3661   if (!ignoreCallingConv(Func) && !IsCallingConvC)
3662     return nullptr;
3663 
3664   switch (Func) {
3665   case LibFunc_memcpy_chk:
3666     return optimizeMemCpyChk(CI, Builder);
3667   case LibFunc_mempcpy_chk:
3668     return optimizeMemPCpyChk(CI, Builder);
3669   case LibFunc_memmove_chk:
3670     return optimizeMemMoveChk(CI, Builder);
3671   case LibFunc_memset_chk:
3672     return optimizeMemSetChk(CI, Builder);
3673   case LibFunc_stpcpy_chk:
3674   case LibFunc_strcpy_chk:
3675     return optimizeStrpCpyChk(CI, Builder, Func);
3676   case LibFunc_strlen_chk:
3677     return optimizeStrLenChk(CI, Builder);
3678   case LibFunc_stpncpy_chk:
3679   case LibFunc_strncpy_chk:
3680     return optimizeStrpNCpyChk(CI, Builder, Func);
3681   case LibFunc_memccpy_chk:
3682     return optimizeMemCCpyChk(CI, Builder);
3683   case LibFunc_snprintf_chk:
3684     return optimizeSNPrintfChk(CI, Builder);
3685   case LibFunc_sprintf_chk:
3686     return optimizeSPrintfChk(CI, Builder);
3687   case LibFunc_strcat_chk:
3688     return optimizeStrCatChk(CI, Builder);
3689   case LibFunc_strlcat_chk:
3690     return optimizeStrLCat(CI, Builder);
3691   case LibFunc_strncat_chk:
3692     return optimizeStrNCatChk(CI, Builder);
3693   case LibFunc_strlcpy_chk:
3694     return optimizeStrLCpyChk(CI, Builder);
3695   case LibFunc_vsnprintf_chk:
3696     return optimizeVSNPrintfChk(CI, Builder);
3697   case LibFunc_vsprintf_chk:
3698     return optimizeVSPrintfChk(CI, Builder);
3699   default:
3700     break;
3701   }
3702   return nullptr;
3703 }
3704 
3705 FortifiedLibCallSimplifier::FortifiedLibCallSimplifier(
3706     const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
3707     : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}
3708