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