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