xref: /llvm-project/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp (revision 29441e4f5fa5f5c7709f7cf180815ba97f611297)
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/StringExtras.h"
18 #include "llvm/Analysis/ConstantFolding.h"
19 #include "llvm/Analysis/Loads.h"
20 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/IR/AttributeMask.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/IRBuilder.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/Intrinsics.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/PatternMatch.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/KnownBits.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/TargetParser/Triple.h"
36 #include "llvm/Transforms/Utils/BuildLibCalls.h"
37 #include "llvm/Transforms/Utils/Local.h"
38 #include "llvm/Transforms/Utils/SizeOpts.h"
39 
40 #include <cmath>
41 
42 using namespace llvm;
43 using namespace PatternMatch;
44 
45 static cl::opt<bool>
46     EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden,
47                          cl::init(false),
48                          cl::desc("Enable unsafe double to float "
49                                   "shrinking for math lib calls"));
50 
51 // Enable conversion of operator new calls with a MemProf hot or cold hint
52 // to an operator new call that takes a hot/cold hint. Off by default since
53 // not all allocators currently support this extension.
54 static cl::opt<bool>
55     OptimizeHotColdNew("optimize-hot-cold-new", cl::Hidden, cl::init(false),
56                        cl::desc("Enable hot/cold operator new library calls"));
57 static cl::opt<bool> OptimizeExistingHotColdNew(
58     "optimize-existing-hot-cold-new", cl::Hidden, cl::init(false),
59     cl::desc(
60         "Enable optimization of existing hot/cold operator new library calls"));
61 
62 namespace {
63 
64 // Specialized parser to ensure the hint is an 8 bit value (we can't specify
65 // uint8_t to opt<> as that is interpreted to mean that we are passing a char
66 // option with a specific set of values.
67 struct HotColdHintParser : public cl::parser<unsigned> {
68   HotColdHintParser(cl::Option &O) : cl::parser<unsigned>(O) {}
69 
70   bool parse(cl::Option &O, StringRef ArgName, StringRef Arg, unsigned &Value) {
71     if (Arg.getAsInteger(0, Value))
72       return O.error("'" + Arg + "' value invalid for uint argument!");
73 
74     if (Value > 255)
75       return O.error("'" + Arg + "' value must be in the range [0, 255]!");
76 
77     return false;
78   }
79 };
80 
81 } // end anonymous namespace
82 
83 // Hot/cold operator new takes an 8 bit hotness hint, where 0 is the coldest
84 // and 255 is the hottest. Default to 1 value away from the coldest and hottest
85 // hints, so that the compiler hinted allocations are slightly less strong than
86 // manually inserted hints at the two extremes.
87 static cl::opt<unsigned, false, HotColdHintParser> ColdNewHintValue(
88     "cold-new-hint-value", cl::Hidden, cl::init(1),
89     cl::desc("Value to pass to hot/cold operator new for cold allocation"));
90 static cl::opt<unsigned, false, HotColdHintParser>
91     NotColdNewHintValue("notcold-new-hint-value", cl::Hidden, cl::init(128),
92                         cl::desc("Value to pass to hot/cold operator new for "
93                                  "notcold (warm) allocation"));
94 static cl::opt<unsigned, false, HotColdHintParser> HotNewHintValue(
95     "hot-new-hint-value", cl::Hidden, cl::init(254),
96     cl::desc("Value to pass to hot/cold operator new for hot allocation"));
97 
98 //===----------------------------------------------------------------------===//
99 // Helper Functions
100 //===----------------------------------------------------------------------===//
101 
102 static bool ignoreCallingConv(LibFunc Func) {
103   return Func == LibFunc_abs || Func == LibFunc_labs ||
104          Func == LibFunc_llabs || Func == LibFunc_strlen;
105 }
106 
107 /// Return true if it is only used in equality comparisons with With.
108 static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
109   for (User *U : V->users()) {
110     if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
111       if (IC->isEquality() && IC->getOperand(1) == With)
112         continue;
113     // Unknown instruction.
114     return false;
115   }
116   return true;
117 }
118 
119 static bool callHasFloatingPointArgument(const CallInst *CI) {
120   return any_of(CI->operands(), [](const Use &OI) {
121     return OI->getType()->isFloatingPointTy();
122   });
123 }
124 
125 static bool callHasFP128Argument(const CallInst *CI) {
126   return any_of(CI->operands(), [](const Use &OI) {
127     return OI->getType()->isFP128Ty();
128   });
129 }
130 
131 // Convert the entire string Str representing an integer in Base, up to
132 // the terminating nul if present, to a constant according to the rules
133 // of strtoul[l] or, when AsSigned is set, of strtol[l].  On success
134 // return the result, otherwise null.
135 // The function assumes the string is encoded in ASCII and carefully
136 // avoids converting sequences (including "") that the corresponding
137 // library call might fail and set errno for.
138 static Value *convertStrToInt(CallInst *CI, StringRef &Str, Value *EndPtr,
139                               uint64_t Base, bool AsSigned, IRBuilderBase &B) {
140   if (Base < 2 || Base > 36)
141     if (Base != 0)
142       // Fail for an invalid base (required by POSIX).
143       return nullptr;
144 
145   // Current offset into the original string to reflect in EndPtr.
146   size_t Offset = 0;
147   // Strip leading whitespace.
148   for ( ; Offset != Str.size(); ++Offset)
149     if (!isSpace((unsigned char)Str[Offset])) {
150       Str = Str.substr(Offset);
151       break;
152     }
153 
154   if (Str.empty())
155     // Fail for empty subject sequences (POSIX allows but doesn't require
156     // strtol[l]/strtoul[l] to fail with EINVAL).
157     return nullptr;
158 
159   // Strip but remember the sign.
160   bool Negate = Str[0] == '-';
161   if (Str[0] == '-' || Str[0] == '+') {
162     Str = Str.drop_front();
163     if (Str.empty())
164       // Fail for a sign with nothing after it.
165       return nullptr;
166     ++Offset;
167   }
168 
169   // Set Max to the absolute value of the minimum (for signed), or
170   // to the maximum (for unsigned) value representable in the type.
171   Type *RetTy = CI->getType();
172   unsigned NBits = RetTy->getPrimitiveSizeInBits();
173   uint64_t Max = AsSigned && Negate ? 1 : 0;
174   Max += AsSigned ? maxIntN(NBits) : maxUIntN(NBits);
175 
176   // Autodetect Base if it's zero and consume the "0x" prefix.
177   if (Str.size() > 1) {
178     if (Str[0] == '0') {
179       if (toUpper((unsigned char)Str[1]) == 'X') {
180         if (Str.size() == 2 || (Base && Base != 16))
181           // Fail if Base doesn't allow the "0x" prefix or for the prefix
182           // alone that implementations like BSD set errno to EINVAL for.
183           return nullptr;
184 
185         Str = Str.drop_front(2);
186         Offset += 2;
187         Base = 16;
188       }
189       else if (Base == 0)
190         Base = 8;
191     } else if (Base == 0)
192       Base = 10;
193   }
194   else if (Base == 0)
195     Base = 10;
196 
197   // Convert the rest of the subject sequence, not including the sign,
198   // to its uint64_t representation (this assumes the source character
199   // set is ASCII).
200   uint64_t Result = 0;
201   for (unsigned i = 0; i != Str.size(); ++i) {
202     unsigned char DigVal = Str[i];
203     if (isDigit(DigVal))
204       DigVal = DigVal - '0';
205     else {
206       DigVal = toUpper(DigVal);
207       if (isAlpha(DigVal))
208         DigVal = DigVal - 'A' + 10;
209       else
210         return nullptr;
211     }
212 
213     if (DigVal >= Base)
214       // Fail if the digit is not valid in the Base.
215       return nullptr;
216 
217     // Add the digit and fail if the result is not representable in
218     // the (unsigned form of the) destination type.
219     bool VFlow;
220     Result = SaturatingMultiplyAdd(Result, Base, (uint64_t)DigVal, &VFlow);
221     if (VFlow || Result > Max)
222       return nullptr;
223   }
224 
225   if (EndPtr) {
226     // Store the pointer to the end.
227     Value *Off = B.getInt64(Offset + Str.size());
228     Value *StrBeg = CI->getArgOperand(0);
229     Value *StrEnd = B.CreateInBoundsGEP(B.getInt8Ty(), StrBeg, Off, "endptr");
230     B.CreateStore(StrEnd, EndPtr);
231   }
232 
233   if (Negate)
234     // Unsigned negation doesn't overflow.
235     Result = -Result;
236 
237   return ConstantInt::get(RetTy, Result);
238 }
239 
240 static bool isOnlyUsedInComparisonWithZero(Value *V) {
241   for (User *U : V->users()) {
242     if (ICmpInst *IC = dyn_cast<ICmpInst>(U))
243       if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
244         if (C->isNullValue())
245           continue;
246     // Unknown instruction.
247     return false;
248   }
249   return true;
250 }
251 
252 static bool canTransformToMemCmp(CallInst *CI, Value *Str, uint64_t Len,
253                                  const DataLayout &DL) {
254   if (!isOnlyUsedInComparisonWithZero(CI))
255     return false;
256 
257   if (!isDereferenceableAndAlignedPointer(Str, Align(1), APInt(64, Len), DL))
258     return false;
259 
260   if (CI->getFunction()->hasFnAttribute(Attribute::SanitizeMemory))
261     return false;
262 
263   return true;
264 }
265 
266 static void annotateDereferenceableBytes(CallInst *CI,
267                                          ArrayRef<unsigned> ArgNos,
268                                          uint64_t DereferenceableBytes) {
269   const Function *F = CI->getCaller();
270   if (!F)
271     return;
272   for (unsigned ArgNo : ArgNos) {
273     uint64_t DerefBytes = DereferenceableBytes;
274     unsigned AS = CI->getArgOperand(ArgNo)->getType()->getPointerAddressSpace();
275     if (!llvm::NullPointerIsDefined(F, AS) ||
276         CI->paramHasAttr(ArgNo, Attribute::NonNull))
277       DerefBytes = std::max(CI->getParamDereferenceableOrNullBytes(ArgNo),
278                             DereferenceableBytes);
279 
280     if (CI->getParamDereferenceableBytes(ArgNo) < DerefBytes) {
281       CI->removeParamAttr(ArgNo, Attribute::Dereferenceable);
282       if (!llvm::NullPointerIsDefined(F, AS) ||
283           CI->paramHasAttr(ArgNo, Attribute::NonNull))
284         CI->removeParamAttr(ArgNo, Attribute::DereferenceableOrNull);
285       CI->addParamAttr(ArgNo, Attribute::getWithDereferenceableBytes(
286                                   CI->getContext(), DerefBytes));
287     }
288   }
289 }
290 
291 static void annotateNonNullNoUndefBasedOnAccess(CallInst *CI,
292                                          ArrayRef<unsigned> ArgNos) {
293   Function *F = CI->getCaller();
294   if (!F)
295     return;
296 
297   for (unsigned ArgNo : ArgNos) {
298     if (!CI->paramHasAttr(ArgNo, Attribute::NoUndef))
299       CI->addParamAttr(ArgNo, Attribute::NoUndef);
300 
301     if (!CI->paramHasAttr(ArgNo, Attribute::NonNull)) {
302       unsigned AS =
303           CI->getArgOperand(ArgNo)->getType()->getPointerAddressSpace();
304       if (llvm::NullPointerIsDefined(F, AS))
305         continue;
306       CI->addParamAttr(ArgNo, Attribute::NonNull);
307     }
308 
309     annotateDereferenceableBytes(CI, ArgNo, 1);
310   }
311 }
312 
313 static void annotateNonNullAndDereferenceable(CallInst *CI, ArrayRef<unsigned> ArgNos,
314                                Value *Size, const DataLayout &DL) {
315   if (ConstantInt *LenC = dyn_cast<ConstantInt>(Size)) {
316     annotateNonNullNoUndefBasedOnAccess(CI, ArgNos);
317     annotateDereferenceableBytes(CI, ArgNos, LenC->getZExtValue());
318   } else if (isKnownNonZero(Size, DL)) {
319     annotateNonNullNoUndefBasedOnAccess(CI, ArgNos);
320     const APInt *X, *Y;
321     uint64_t DerefMin = 1;
322     if (match(Size, m_Select(m_Value(), m_APInt(X), m_APInt(Y)))) {
323       DerefMin = std::min(X->getZExtValue(), Y->getZExtValue());
324       annotateDereferenceableBytes(CI, ArgNos, DerefMin);
325     }
326   }
327 }
328 
329 // Copy CallInst "flags" like musttail, notail, and tail. Return New param for
330 // easier chaining. Calls to emit* and B.createCall should probably be wrapped
331 // in this function when New is created to replace Old. Callers should take
332 // care to check Old.isMustTailCall() if they aren't replacing Old directly
333 // with New.
334 static Value *copyFlags(const CallInst &Old, Value *New) {
335   assert(!Old.isMustTailCall() && "do not copy musttail call flags");
336   assert(!Old.isNoTailCall() && "do not copy notail call flags");
337   if (auto *NewCI = dyn_cast_or_null<CallInst>(New))
338     NewCI->setTailCallKind(Old.getTailCallKind());
339   return New;
340 }
341 
342 static Value *mergeAttributesAndFlags(CallInst *NewCI, const CallInst &Old) {
343   NewCI->setAttributes(AttributeList::get(
344       NewCI->getContext(), {NewCI->getAttributes(), Old.getAttributes()}));
345   NewCI->removeRetAttrs(AttributeFuncs::typeIncompatible(
346       NewCI->getType(), NewCI->getRetAttributes()));
347   for (unsigned I = 0; I < NewCI->arg_size(); ++I)
348     NewCI->removeParamAttrs(
349         I, AttributeFuncs::typeIncompatible(NewCI->getArgOperand(I)->getType(),
350                                             NewCI->getParamAttributes(I)));
351 
352   return copyFlags(Old, NewCI);
353 }
354 
355 // Helper to avoid truncating the length if size_t is 32-bits.
356 static StringRef substr(StringRef Str, uint64_t Len) {
357   return Len >= Str.size() ? Str : Str.substr(0, Len);
358 }
359 
360 //===----------------------------------------------------------------------===//
361 // String and Memory Library Call Optimizations
362 //===----------------------------------------------------------------------===//
363 
364 Value *LibCallSimplifier::optimizeStrCat(CallInst *CI, IRBuilderBase &B) {
365   // Extract some information from the instruction
366   Value *Dst = CI->getArgOperand(0);
367   Value *Src = CI->getArgOperand(1);
368   annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
369 
370   // See if we can get the length of the input string.
371   uint64_t Len = GetStringLength(Src);
372   if (Len)
373     annotateDereferenceableBytes(CI, 1, Len);
374   else
375     return nullptr;
376   --Len; // Unbias length.
377 
378   // Handle the simple, do-nothing case: strcat(x, "") -> x
379   if (Len == 0)
380     return Dst;
381 
382   return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, Len, B));
383 }
384 
385 Value *LibCallSimplifier::emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
386                                            IRBuilderBase &B) {
387   // We need to find the end of the destination string.  That's where the
388   // memory is to be moved to. We just generate a call to strlen.
389   Value *DstLen = emitStrLen(Dst, B, DL, TLI);
390   if (!DstLen)
391     return nullptr;
392 
393   // Now that we have the destination's length, we must index into the
394   // destination's pointer to get the actual memcpy destination (end of
395   // the string .. we're concatenating).
396   Value *CpyDst = B.CreateInBoundsGEP(B.getInt8Ty(), Dst, DstLen, "endptr");
397 
398   // We have enough information to now generate the memcpy call to do the
399   // concatenation for us.  Make a memcpy to copy the nul byte with align = 1.
400   B.CreateMemCpy(CpyDst, Align(1), Src, Align(1),
401                  TLI->getAsSizeT(Len + 1, *B.GetInsertBlock()->getModule()));
402   return Dst;
403 }
404 
405 Value *LibCallSimplifier::optimizeStrNCat(CallInst *CI, IRBuilderBase &B) {
406   // Extract some information from the instruction.
407   Value *Dst = CI->getArgOperand(0);
408   Value *Src = CI->getArgOperand(1);
409   Value *Size = CI->getArgOperand(2);
410   uint64_t Len;
411   annotateNonNullNoUndefBasedOnAccess(CI, 0);
412   if (isKnownNonZero(Size, DL))
413     annotateNonNullNoUndefBasedOnAccess(CI, 1);
414 
415   // We don't do anything if length is not constant.
416   ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size);
417   if (LengthArg) {
418     Len = LengthArg->getZExtValue();
419     // strncat(x, c, 0) -> x
420     if (!Len)
421       return Dst;
422   } else {
423     return nullptr;
424   }
425 
426   // See if we can get the length of the input string.
427   uint64_t SrcLen = GetStringLength(Src);
428   if (SrcLen) {
429     annotateDereferenceableBytes(CI, 1, SrcLen);
430     --SrcLen; // Unbias length.
431   } else {
432     return nullptr;
433   }
434 
435   // strncat(x, "", c) -> x
436   if (SrcLen == 0)
437     return Dst;
438 
439   // We don't optimize this case.
440   if (Len < SrcLen)
441     return nullptr;
442 
443   // strncat(x, s, c) -> strcat(x, s)
444   // s is constant so the strcat can be optimized further.
445   return copyFlags(*CI, emitStrLenMemCpy(Src, Dst, SrcLen, B));
446 }
447 
448 // Helper to transform memchr(S, C, N) == S to N && *S == C and, when
449 // NBytes is null, strchr(S, C) to *S == C.  A precondition of the function
450 // is that either S is dereferenceable or the value of N is nonzero.
451 static Value* memChrToCharCompare(CallInst *CI, Value *NBytes,
452                                   IRBuilderBase &B, const DataLayout &DL)
453 {
454   Value *Src = CI->getArgOperand(0);
455   Value *CharVal = CI->getArgOperand(1);
456 
457   // Fold memchr(A, C, N) == A to N && *A == C.
458   Type *CharTy = B.getInt8Ty();
459   Value *Char0 = B.CreateLoad(CharTy, Src);
460   CharVal = B.CreateTrunc(CharVal, CharTy);
461   Value *Cmp = B.CreateICmpEQ(Char0, CharVal, "char0cmp");
462 
463   if (NBytes) {
464     Value *Zero = ConstantInt::get(NBytes->getType(), 0);
465     Value *And = B.CreateICmpNE(NBytes, Zero);
466     Cmp = B.CreateLogicalAnd(And, Cmp);
467   }
468 
469   Value *NullPtr = Constant::getNullValue(CI->getType());
470   return B.CreateSelect(Cmp, Src, NullPtr);
471 }
472 
473 Value *LibCallSimplifier::optimizeStrChr(CallInst *CI, IRBuilderBase &B) {
474   Value *SrcStr = CI->getArgOperand(0);
475   Value *CharVal = CI->getArgOperand(1);
476   annotateNonNullNoUndefBasedOnAccess(CI, 0);
477 
478   if (isOnlyUsedInEqualityComparison(CI, SrcStr))
479     return memChrToCharCompare(CI, nullptr, B, DL);
480 
481   // If the second operand is non-constant, see if we can compute the length
482   // of the input string and turn this into memchr.
483   ConstantInt *CharC = dyn_cast<ConstantInt>(CharVal);
484   if (!CharC) {
485     uint64_t Len = GetStringLength(SrcStr);
486     if (Len)
487       annotateDereferenceableBytes(CI, 0, Len);
488     else
489       return nullptr;
490 
491     Function *Callee = CI->getCalledFunction();
492     FunctionType *FT = Callee->getFunctionType();
493     unsigned IntBits = TLI->getIntSize();
494     if (!FT->getParamType(1)->isIntegerTy(IntBits)) // memchr needs 'int'.
495       return nullptr;
496 
497     unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
498     Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
499     return copyFlags(*CI,
500                      emitMemChr(SrcStr, CharVal, // include nul.
501                                 ConstantInt::get(SizeTTy, Len), B,
502                                 DL, TLI));
503   }
504 
505   if (CharC->isZero()) {
506     Value *NullPtr = Constant::getNullValue(CI->getType());
507     if (isOnlyUsedInEqualityComparison(CI, NullPtr))
508       // Pre-empt the transformation to strlen below and fold
509       // strchr(A, '\0') == null to false.
510       return B.CreateIntToPtr(B.getTrue(), CI->getType());
511   }
512 
513   // Otherwise, the character is a constant, see if the first argument is
514   // a string literal.  If so, we can constant fold.
515   StringRef Str;
516   if (!getConstantStringInfo(SrcStr, Str)) {
517     if (CharC->isZero()) // strchr(p, 0) -> p + strlen(p)
518       if (Value *StrLen = emitStrLen(SrcStr, B, DL, TLI))
519         return B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, StrLen, "strchr");
520     return nullptr;
521   }
522 
523   // Compute the offset, make sure to handle the case when we're searching for
524   // zero (a weird way to spell strlen).
525   size_t I = (0xFF & CharC->getSExtValue()) == 0
526                  ? Str.size()
527                  : Str.find(CharC->getSExtValue());
528   if (I == StringRef::npos) // Didn't find the char.  strchr returns null.
529     return Constant::getNullValue(CI->getType());
530 
531   // strchr(s+n,c)  -> gep(s+n+i,c)
532   return B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, B.getInt64(I), "strchr");
533 }
534 
535 Value *LibCallSimplifier::optimizeStrRChr(CallInst *CI, IRBuilderBase &B) {
536   Value *SrcStr = CI->getArgOperand(0);
537   Value *CharVal = CI->getArgOperand(1);
538   ConstantInt *CharC = dyn_cast<ConstantInt>(CharVal);
539   annotateNonNullNoUndefBasedOnAccess(CI, 0);
540 
541   StringRef Str;
542   if (!getConstantStringInfo(SrcStr, Str)) {
543     // strrchr(s, 0) -> strchr(s, 0)
544     if (CharC && CharC->isZero())
545       return copyFlags(*CI, emitStrChr(SrcStr, '\0', B, TLI));
546     return nullptr;
547   }
548 
549   unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
550   Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
551 
552   // Try to expand strrchr to the memrchr nonstandard extension if it's
553   // available, or simply fail otherwise.
554   uint64_t NBytes = Str.size() + 1;   // Include the terminating nul.
555   Value *Size = ConstantInt::get(SizeTTy, NBytes);
556   return copyFlags(*CI, emitMemRChr(SrcStr, CharVal, Size, B, DL, TLI));
557 }
558 
559 Value *LibCallSimplifier::optimizeStrCmp(CallInst *CI, IRBuilderBase &B) {
560   Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
561   if (Str1P == Str2P) // strcmp(x,x)  -> 0
562     return ConstantInt::get(CI->getType(), 0);
563 
564   StringRef Str1, Str2;
565   bool HasStr1 = getConstantStringInfo(Str1P, Str1);
566   bool HasStr2 = getConstantStringInfo(Str2P, Str2);
567 
568   // strcmp(x, y)  -> cnst  (if both x and y are constant strings)
569   if (HasStr1 && HasStr2)
570     return ConstantInt::get(CI->getType(),
571                             std::clamp(Str1.compare(Str2), -1, 1));
572 
573   if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
574     return B.CreateNeg(B.CreateZExt(
575         B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
576 
577   if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
578     return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"),
579                         CI->getType());
580 
581   // strcmp(P, "x") -> memcmp(P, "x", 2)
582   uint64_t Len1 = GetStringLength(Str1P);
583   if (Len1)
584     annotateDereferenceableBytes(CI, 0, Len1);
585   uint64_t Len2 = GetStringLength(Str2P);
586   if (Len2)
587     annotateDereferenceableBytes(CI, 1, Len2);
588 
589   if (Len1 && Len2) {
590     return copyFlags(
591         *CI, emitMemCmp(Str1P, Str2P,
592                         TLI->getAsSizeT(std::min(Len1, Len2), *CI->getModule()),
593                         B, DL, TLI));
594   }
595 
596   // strcmp to memcmp
597   if (!HasStr1 && HasStr2) {
598     if (canTransformToMemCmp(CI, Str1P, Len2, DL))
599       return copyFlags(*CI, emitMemCmp(Str1P, Str2P,
600                                        TLI->getAsSizeT(Len2, *CI->getModule()),
601                                        B, DL, TLI));
602   } else if (HasStr1 && !HasStr2) {
603     if (canTransformToMemCmp(CI, Str2P, Len1, DL))
604       return copyFlags(*CI, emitMemCmp(Str1P, Str2P,
605                                        TLI->getAsSizeT(Len1, *CI->getModule()),
606                                        B, DL, TLI));
607   }
608 
609   annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
610   return nullptr;
611 }
612 
613 // Optimize a memcmp or, when StrNCmp is true, strncmp call CI with constant
614 // arrays LHS and RHS and nonconstant Size.
615 static Value *optimizeMemCmpVarSize(CallInst *CI, Value *LHS, Value *RHS,
616                                     Value *Size, bool StrNCmp,
617                                     IRBuilderBase &B, const DataLayout &DL);
618 
619 Value *LibCallSimplifier::optimizeStrNCmp(CallInst *CI, IRBuilderBase &B) {
620   Value *Str1P = CI->getArgOperand(0);
621   Value *Str2P = CI->getArgOperand(1);
622   Value *Size = CI->getArgOperand(2);
623   if (Str1P == Str2P) // strncmp(x,x,n)  -> 0
624     return ConstantInt::get(CI->getType(), 0);
625 
626   if (isKnownNonZero(Size, DL))
627     annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
628   // Get the length argument if it is constant.
629   uint64_t Length;
630   if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(Size))
631     Length = LengthArg->getZExtValue();
632   else
633     return optimizeMemCmpVarSize(CI, Str1P, Str2P, Size, true, B, DL);
634 
635   if (Length == 0) // strncmp(x,y,0)   -> 0
636     return ConstantInt::get(CI->getType(), 0);
637 
638   if (Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
639     return copyFlags(*CI, emitMemCmp(Str1P, Str2P, Size, B, DL, TLI));
640 
641   StringRef Str1, Str2;
642   bool HasStr1 = getConstantStringInfo(Str1P, Str1);
643   bool HasStr2 = getConstantStringInfo(Str2P, Str2);
644 
645   // strncmp(x, y)  -> cnst  (if both x and y are constant strings)
646   if (HasStr1 && HasStr2) {
647     // Avoid truncating the 64-bit Length to 32 bits in ILP32.
648     StringRef SubStr1 = substr(Str1, Length);
649     StringRef SubStr2 = substr(Str2, Length);
650     return ConstantInt::get(CI->getType(),
651                             std::clamp(SubStr1.compare(SubStr2), -1, 1));
652   }
653 
654   if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
655     return B.CreateNeg(B.CreateZExt(
656         B.CreateLoad(B.getInt8Ty(), Str2P, "strcmpload"), CI->getType()));
657 
658   if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
659     return B.CreateZExt(B.CreateLoad(B.getInt8Ty(), Str1P, "strcmpload"),
660                         CI->getType());
661 
662   uint64_t Len1 = GetStringLength(Str1P);
663   if (Len1)
664     annotateDereferenceableBytes(CI, 0, Len1);
665   uint64_t Len2 = GetStringLength(Str2P);
666   if (Len2)
667     annotateDereferenceableBytes(CI, 1, Len2);
668 
669   // strncmp to memcmp
670   if (!HasStr1 && HasStr2) {
671     Len2 = std::min(Len2, Length);
672     if (canTransformToMemCmp(CI, Str1P, Len2, DL))
673       return copyFlags(*CI, emitMemCmp(Str1P, Str2P,
674                                        TLI->getAsSizeT(Len2, *CI->getModule()),
675                                        B, DL, TLI));
676   } else if (HasStr1 && !HasStr2) {
677     Len1 = std::min(Len1, Length);
678     if (canTransformToMemCmp(CI, Str2P, Len1, DL))
679       return copyFlags(*CI, emitMemCmp(Str1P, Str2P,
680                                        TLI->getAsSizeT(Len1, *CI->getModule()),
681                                        B, DL, TLI));
682   }
683 
684   return nullptr;
685 }
686 
687 Value *LibCallSimplifier::optimizeStrNDup(CallInst *CI, IRBuilderBase &B) {
688   Value *Src = CI->getArgOperand(0);
689   ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
690   uint64_t SrcLen = GetStringLength(Src);
691   if (SrcLen && Size) {
692     annotateDereferenceableBytes(CI, 0, SrcLen);
693     if (SrcLen <= Size->getZExtValue() + 1)
694       return copyFlags(*CI, emitStrDup(Src, B, TLI));
695   }
696 
697   return nullptr;
698 }
699 
700 Value *LibCallSimplifier::optimizeStrCpy(CallInst *CI, IRBuilderBase &B) {
701   Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
702   if (Dst == Src) // strcpy(x,x)  -> x
703     return Src;
704 
705   annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
706   // See if we can get the length of the input string.
707   uint64_t Len = GetStringLength(Src);
708   if (Len)
709     annotateDereferenceableBytes(CI, 1, Len);
710   else
711     return nullptr;
712 
713   // We have enough information to now generate the memcpy call to do the
714   // copy for us.  Make a memcpy to copy the nul byte with align = 1.
715   CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1),
716                                    TLI->getAsSizeT(Len, *CI->getModule()));
717   mergeAttributesAndFlags(NewCI, *CI);
718   return Dst;
719 }
720 
721 Value *LibCallSimplifier::optimizeStpCpy(CallInst *CI, IRBuilderBase &B) {
722   Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
723 
724   // stpcpy(d,s) -> strcpy(d,s) if the result is not used.
725   if (CI->use_empty())
726     return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
727 
728   if (Dst == Src) { // stpcpy(x,x)  -> x+strlen(x)
729     Value *StrLen = emitStrLen(Src, B, DL, TLI);
730     return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
731   }
732 
733   // See if we can get the length of the input string.
734   uint64_t Len = GetStringLength(Src);
735   if (Len)
736     annotateDereferenceableBytes(CI, 1, Len);
737   else
738     return nullptr;
739 
740   Value *LenV = TLI->getAsSizeT(Len, *CI->getModule());
741   Value *DstEnd = B.CreateInBoundsGEP(
742       B.getInt8Ty(), Dst, TLI->getAsSizeT(Len - 1, *CI->getModule()));
743 
744   // We have enough information to now generate the memcpy call to do the
745   // copy for us.  Make a memcpy to copy the nul byte with align = 1.
746   CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1), LenV);
747   mergeAttributesAndFlags(NewCI, *CI);
748   return DstEnd;
749 }
750 
751 // Optimize a call to size_t strlcpy(char*, const char*, size_t).
752 
753 Value *LibCallSimplifier::optimizeStrLCpy(CallInst *CI, IRBuilderBase &B) {
754   Value *Size = CI->getArgOperand(2);
755   if (isKnownNonZero(Size, DL))
756     // Like snprintf, the function stores into the destination only when
757     // the size argument is nonzero.
758     annotateNonNullNoUndefBasedOnAccess(CI, 0);
759   // The function reads the source argument regardless of Size (it returns
760   // its length).
761   annotateNonNullNoUndefBasedOnAccess(CI, 1);
762 
763   uint64_t NBytes;
764   if (ConstantInt *SizeC = dyn_cast<ConstantInt>(Size))
765     NBytes = SizeC->getZExtValue();
766   else
767     return nullptr;
768 
769   Value *Dst = CI->getArgOperand(0);
770   Value *Src = CI->getArgOperand(1);
771   if (NBytes <= 1) {
772     if (NBytes == 1)
773       // For a call to strlcpy(D, S, 1) first store a nul in *D.
774       B.CreateStore(B.getInt8(0), Dst);
775 
776     // Transform strlcpy(D, S, 0) to a call to strlen(S).
777     return copyFlags(*CI, emitStrLen(Src, B, DL, TLI));
778   }
779 
780   // Try to determine the length of the source, substituting its size
781   // when it's not nul-terminated (as it's required to be) to avoid
782   // reading past its end.
783   StringRef Str;
784   if (!getConstantStringInfo(Src, Str, /*TrimAtNul=*/false))
785     return nullptr;
786 
787   uint64_t SrcLen = Str.find('\0');
788   // Set if the terminating nul should be copied by the call to memcpy
789   // below.
790   bool NulTerm = SrcLen < NBytes;
791 
792   if (NulTerm)
793     // Overwrite NBytes with the number of bytes to copy, including
794     // the terminating nul.
795     NBytes = SrcLen + 1;
796   else {
797     // Set the length of the source for the function to return to its
798     // size, and cap NBytes at the same.
799     SrcLen = std::min(SrcLen, uint64_t(Str.size()));
800     NBytes = std::min(NBytes - 1, SrcLen);
801   }
802 
803   if (SrcLen == 0) {
804     // Transform strlcpy(D, "", N) to (*D = '\0, 0).
805     B.CreateStore(B.getInt8(0), Dst);
806     return ConstantInt::get(CI->getType(), 0);
807   }
808 
809   // Transform strlcpy(D, S, N) to memcpy(D, S, N') where N' is the lower
810   // bound on strlen(S) + 1 and N, optionally followed by a nul store to
811   // D[N' - 1] if necessary.
812   CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1),
813                                    TLI->getAsSizeT(NBytes, *CI->getModule()));
814   mergeAttributesAndFlags(NewCI, *CI);
815 
816   if (!NulTerm) {
817     Value *EndOff = ConstantInt::get(CI->getType(), NBytes);
818     Value *EndPtr = B.CreateInBoundsGEP(B.getInt8Ty(), Dst, EndOff);
819     B.CreateStore(B.getInt8(0), EndPtr);
820   }
821 
822   // Like snprintf, strlcpy returns the number of nonzero bytes that would
823   // have been copied if the bound had been sufficiently big (which in this
824   // case is strlen(Src)).
825   return ConstantInt::get(CI->getType(), SrcLen);
826 }
827 
828 // Optimize a call CI to either stpncpy when RetEnd is true, or to strncpy
829 // otherwise.
830 Value *LibCallSimplifier::optimizeStringNCpy(CallInst *CI, bool RetEnd,
831                                              IRBuilderBase &B) {
832   Value *Dst = CI->getArgOperand(0);
833   Value *Src = CI->getArgOperand(1);
834   Value *Size = CI->getArgOperand(2);
835 
836   if (isKnownNonZero(Size, DL)) {
837     // Both st{p,r}ncpy(D, S, N) access the source and destination arrays
838     // only when N is nonzero.
839     annotateNonNullNoUndefBasedOnAccess(CI, 0);
840     annotateNonNullNoUndefBasedOnAccess(CI, 1);
841   }
842 
843   // If the "bound" argument is known set N to it.  Otherwise set it to
844   // UINT64_MAX and handle it later.
845   uint64_t N = UINT64_MAX;
846   if (ConstantInt *SizeC = dyn_cast<ConstantInt>(Size))
847     N = SizeC->getZExtValue();
848 
849   if (N == 0)
850     // Fold st{p,r}ncpy(D, S, 0) to D.
851     return Dst;
852 
853   if (N == 1) {
854     Type *CharTy = B.getInt8Ty();
855     Value *CharVal = B.CreateLoad(CharTy, Src, "stxncpy.char0");
856     B.CreateStore(CharVal, Dst);
857     if (!RetEnd)
858       // Transform strncpy(D, S, 1) to return (*D = *S), D.
859       return Dst;
860 
861     // Transform stpncpy(D, S, 1) to return (*D = *S) ? D + 1 : D.
862     Value *ZeroChar = ConstantInt::get(CharTy, 0);
863     Value *Cmp = B.CreateICmpEQ(CharVal, ZeroChar, "stpncpy.char0cmp");
864 
865     Value *Off1 = B.getInt32(1);
866     Value *EndPtr = B.CreateInBoundsGEP(CharTy, Dst, Off1, "stpncpy.end");
867     return B.CreateSelect(Cmp, Dst, EndPtr, "stpncpy.sel");
868   }
869 
870   // If the length of the input string is known set SrcLen to it.
871   uint64_t SrcLen = GetStringLength(Src);
872   if (SrcLen)
873     annotateDereferenceableBytes(CI, 1, SrcLen);
874   else
875     return nullptr;
876 
877   --SrcLen; // Unbias length.
878 
879   if (SrcLen == 0) {
880     // Transform st{p,r}ncpy(D, "", N) to memset(D, '\0', N) for any N.
881     Align MemSetAlign =
882       CI->getAttributes().getParamAttrs(0).getAlignment().valueOrOne();
883     CallInst *NewCI = B.CreateMemSet(Dst, B.getInt8('\0'), Size, MemSetAlign);
884     AttrBuilder ArgAttrs(CI->getContext(), CI->getAttributes().getParamAttrs(0));
885     NewCI->setAttributes(NewCI->getAttributes().addParamAttributes(
886         CI->getContext(), 0, ArgAttrs));
887     copyFlags(*CI, NewCI);
888     return Dst;
889   }
890 
891   if (N > SrcLen + 1) {
892     if (N > 128)
893       // Bail if N is large or unknown.
894       return nullptr;
895 
896     // st{p,r}ncpy(D, "a", N) -> memcpy(D, "a\0\0\0", N) for N <= 128.
897     StringRef Str;
898     if (!getConstantStringInfo(Src, Str))
899       return nullptr;
900     std::string SrcStr = Str.str();
901     // Create a bigger, nul-padded array with the same length, SrcLen,
902     // as the original string.
903     SrcStr.resize(N, '\0');
904     Src = B.CreateGlobalString(SrcStr, "str", /*AddressSpace=*/0,
905                                /*M=*/nullptr, /*AddNull=*/false);
906   }
907 
908   // st{p,r}ncpy(D, S, N) -> memcpy(align 1 D, align 1 S, N) when both
909   // S and N are constant.
910   CallInst *NewCI = B.CreateMemCpy(Dst, Align(1), Src, Align(1),
911                                    TLI->getAsSizeT(N, *CI->getModule()));
912   mergeAttributesAndFlags(NewCI, *CI);
913   if (!RetEnd)
914     return Dst;
915 
916   // stpncpy(D, S, N) returns the address of the first null in D if it writes
917   // one, otherwise D + N.
918   Value *Off = B.getInt64(std::min(SrcLen, N));
919   return B.CreateInBoundsGEP(B.getInt8Ty(), Dst, Off, "endptr");
920 }
921 
922 Value *LibCallSimplifier::optimizeStringLength(CallInst *CI, IRBuilderBase &B,
923                                                unsigned CharSize,
924                                                Value *Bound) {
925   Value *Src = CI->getArgOperand(0);
926   Type *CharTy = B.getIntNTy(CharSize);
927 
928   if (isOnlyUsedInZeroEqualityComparison(CI) &&
929       (!Bound || isKnownNonZero(Bound, DL))) {
930     // Fold strlen:
931     //   strlen(x) != 0 --> *x != 0
932     //   strlen(x) == 0 --> *x == 0
933     // and likewise strnlen with constant N > 0:
934     //   strnlen(x, N) != 0 --> *x != 0
935     //   strnlen(x, N) == 0 --> *x == 0
936     return B.CreateZExt(B.CreateLoad(CharTy, Src, "char0"),
937                         CI->getType());
938   }
939 
940   if (Bound) {
941     if (ConstantInt *BoundCst = dyn_cast<ConstantInt>(Bound)) {
942       if (BoundCst->isZero())
943         // Fold strnlen(s, 0) -> 0 for any s, constant or otherwise.
944         return ConstantInt::get(CI->getType(), 0);
945 
946       if (BoundCst->isOne()) {
947         // Fold strnlen(s, 1) -> *s ? 1 : 0 for any s.
948         Value *CharVal = B.CreateLoad(CharTy, Src, "strnlen.char0");
949         Value *ZeroChar = ConstantInt::get(CharTy, 0);
950         Value *Cmp = B.CreateICmpNE(CharVal, ZeroChar, "strnlen.char0cmp");
951         return B.CreateZExt(Cmp, CI->getType());
952       }
953     }
954   }
955 
956   if (uint64_t Len = GetStringLength(Src, CharSize)) {
957     Value *LenC = ConstantInt::get(CI->getType(), Len - 1);
958     // Fold strlen("xyz") -> 3 and strnlen("xyz", 2) -> 2
959     // and strnlen("xyz", Bound) -> min(3, Bound) for nonconstant Bound.
960     if (Bound)
961       return B.CreateBinaryIntrinsic(Intrinsic::umin, LenC, Bound);
962     return LenC;
963   }
964 
965   if (Bound)
966     // Punt for strnlen for now.
967     return nullptr;
968 
969   // If s is a constant pointer pointing to a string literal, we can fold
970   // strlen(s + x) to strlen(s) - x, when x is known to be in the range
971   // [0, strlen(s)] or the string has a single null terminator '\0' at the end.
972   // We only try to simplify strlen when the pointer s points to an array
973   // of CharSize elements. Otherwise, we would need to scale the offset x before
974   // doing the subtraction. This will make the optimization more complex, and
975   // it's not very useful because calling strlen for a pointer of other types is
976   // very uncommon.
977   if (GEPOperator *GEP = dyn_cast<GEPOperator>(Src)) {
978     // TODO: Handle subobjects.
979     if (!isGEPBasedOnPointerToString(GEP, CharSize))
980       return nullptr;
981 
982     ConstantDataArraySlice Slice;
983     if (getConstantDataArrayInfo(GEP->getOperand(0), Slice, CharSize)) {
984       uint64_t NullTermIdx;
985       if (Slice.Array == nullptr) {
986         NullTermIdx = 0;
987       } else {
988         NullTermIdx = ~((uint64_t)0);
989         for (uint64_t I = 0, E = Slice.Length; I < E; ++I) {
990           if (Slice.Array->getElementAsInteger(I + Slice.Offset) == 0) {
991             NullTermIdx = I;
992             break;
993           }
994         }
995         // If the string does not have '\0', leave it to strlen to compute
996         // its length.
997         if (NullTermIdx == ~((uint64_t)0))
998           return nullptr;
999       }
1000 
1001       Value *Offset = GEP->getOperand(2);
1002       KnownBits Known = computeKnownBits(Offset, DL, 0, nullptr, CI, nullptr);
1003       uint64_t ArrSize =
1004              cast<ArrayType>(GEP->getSourceElementType())->getNumElements();
1005 
1006       // If Offset is not provably in the range [0, NullTermIdx], we can still
1007       // optimize if we can prove that the program has undefined behavior when
1008       // Offset is outside that range. That is the case when GEP->getOperand(0)
1009       // is a pointer to an object whose memory extent is NullTermIdx+1.
1010       if ((Known.isNonNegative() && Known.getMaxValue().ule(NullTermIdx)) ||
1011           (isa<GlobalVariable>(GEP->getOperand(0)) &&
1012            NullTermIdx == ArrSize - 1)) {
1013         Offset = B.CreateSExtOrTrunc(Offset, CI->getType());
1014         return B.CreateSub(ConstantInt::get(CI->getType(), NullTermIdx),
1015                            Offset);
1016       }
1017     }
1018   }
1019 
1020   // strlen(x?"foo":"bars") --> x ? 3 : 4
1021   if (SelectInst *SI = dyn_cast<SelectInst>(Src)) {
1022     uint64_t LenTrue = GetStringLength(SI->getTrueValue(), CharSize);
1023     uint64_t LenFalse = GetStringLength(SI->getFalseValue(), CharSize);
1024     if (LenTrue && LenFalse) {
1025       ORE.emit([&]() {
1026         return OptimizationRemark("instcombine", "simplify-libcalls", CI)
1027                << "folded strlen(select) to select of constants";
1028       });
1029       return B.CreateSelect(SI->getCondition(),
1030                             ConstantInt::get(CI->getType(), LenTrue - 1),
1031                             ConstantInt::get(CI->getType(), LenFalse - 1));
1032     }
1033   }
1034 
1035   return nullptr;
1036 }
1037 
1038 Value *LibCallSimplifier::optimizeStrLen(CallInst *CI, IRBuilderBase &B) {
1039   if (Value *V = optimizeStringLength(CI, B, 8))
1040     return V;
1041   annotateNonNullNoUndefBasedOnAccess(CI, 0);
1042   return nullptr;
1043 }
1044 
1045 Value *LibCallSimplifier::optimizeStrNLen(CallInst *CI, IRBuilderBase &B) {
1046   Value *Bound = CI->getArgOperand(1);
1047   if (Value *V = optimizeStringLength(CI, B, 8, Bound))
1048     return V;
1049 
1050   if (isKnownNonZero(Bound, DL))
1051     annotateNonNullNoUndefBasedOnAccess(CI, 0);
1052   return nullptr;
1053 }
1054 
1055 Value *LibCallSimplifier::optimizeWcslen(CallInst *CI, IRBuilderBase &B) {
1056   Module &M = *CI->getModule();
1057   unsigned WCharSize = TLI->getWCharSize(M) * 8;
1058   // We cannot perform this optimization without wchar_size metadata.
1059   if (WCharSize == 0)
1060     return nullptr;
1061 
1062   return optimizeStringLength(CI, B, WCharSize);
1063 }
1064 
1065 Value *LibCallSimplifier::optimizeStrPBrk(CallInst *CI, IRBuilderBase &B) {
1066   StringRef S1, S2;
1067   bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
1068   bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
1069 
1070   // strpbrk(s, "") -> nullptr
1071   // strpbrk("", s) -> nullptr
1072   if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
1073     return Constant::getNullValue(CI->getType());
1074 
1075   // Constant folding.
1076   if (HasS1 && HasS2) {
1077     size_t I = S1.find_first_of(S2);
1078     if (I == StringRef::npos) // No match.
1079       return Constant::getNullValue(CI->getType());
1080 
1081     return B.CreateInBoundsGEP(B.getInt8Ty(), CI->getArgOperand(0),
1082                                B.getInt64(I), "strpbrk");
1083   }
1084 
1085   // strpbrk(s, "a") -> strchr(s, 'a')
1086   if (HasS2 && S2.size() == 1)
1087     return copyFlags(*CI, emitStrChr(CI->getArgOperand(0), S2[0], B, TLI));
1088 
1089   return nullptr;
1090 }
1091 
1092 Value *LibCallSimplifier::optimizeStrTo(CallInst *CI, IRBuilderBase &B) {
1093   Value *EndPtr = CI->getArgOperand(1);
1094   if (isa<ConstantPointerNull>(EndPtr)) {
1095     // With a null EndPtr, this function won't capture the main argument.
1096     // It would be readonly too, except that it still may write to errno.
1097     CI->addParamAttr(0, Attribute::getWithCaptureInfo(CI->getContext(),
1098                                                       CaptureInfo::none()));
1099   }
1100 
1101   return nullptr;
1102 }
1103 
1104 Value *LibCallSimplifier::optimizeStrSpn(CallInst *CI, IRBuilderBase &B) {
1105   StringRef S1, S2;
1106   bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
1107   bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
1108 
1109   // strspn(s, "") -> 0
1110   // strspn("", s) -> 0
1111   if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
1112     return Constant::getNullValue(CI->getType());
1113 
1114   // Constant folding.
1115   if (HasS1 && HasS2) {
1116     size_t Pos = S1.find_first_not_of(S2);
1117     if (Pos == StringRef::npos)
1118       Pos = S1.size();
1119     return ConstantInt::get(CI->getType(), Pos);
1120   }
1121 
1122   return nullptr;
1123 }
1124 
1125 Value *LibCallSimplifier::optimizeStrCSpn(CallInst *CI, IRBuilderBase &B) {
1126   StringRef S1, S2;
1127   bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
1128   bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
1129 
1130   // strcspn("", s) -> 0
1131   if (HasS1 && S1.empty())
1132     return Constant::getNullValue(CI->getType());
1133 
1134   // Constant folding.
1135   if (HasS1 && HasS2) {
1136     size_t Pos = S1.find_first_of(S2);
1137     if (Pos == StringRef::npos)
1138       Pos = S1.size();
1139     return ConstantInt::get(CI->getType(), Pos);
1140   }
1141 
1142   // strcspn(s, "") -> strlen(s)
1143   if (HasS2 && S2.empty())
1144     return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B, DL, TLI));
1145 
1146   return nullptr;
1147 }
1148 
1149 Value *LibCallSimplifier::optimizeStrStr(CallInst *CI, IRBuilderBase &B) {
1150   // fold strstr(x, x) -> x.
1151   if (CI->getArgOperand(0) == CI->getArgOperand(1))
1152     return CI->getArgOperand(0);
1153 
1154   // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
1155   if (isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
1156     Value *StrLen = emitStrLen(CI->getArgOperand(1), B, DL, TLI);
1157     if (!StrLen)
1158       return nullptr;
1159     Value *StrNCmp = emitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
1160                                  StrLen, B, DL, TLI);
1161     if (!StrNCmp)
1162       return nullptr;
1163     for (User *U : llvm::make_early_inc_range(CI->users())) {
1164       ICmpInst *Old = cast<ICmpInst>(U);
1165       Value *Cmp =
1166           B.CreateICmp(Old->getPredicate(), StrNCmp,
1167                        ConstantInt::getNullValue(StrNCmp->getType()), "cmp");
1168       replaceAllUsesWith(Old, Cmp);
1169     }
1170     return CI;
1171   }
1172 
1173   // See if either input string is a constant string.
1174   StringRef SearchStr, ToFindStr;
1175   bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
1176   bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
1177 
1178   // fold strstr(x, "") -> x.
1179   if (HasStr2 && ToFindStr.empty())
1180     return CI->getArgOperand(0);
1181 
1182   // If both strings are known, constant fold it.
1183   if (HasStr1 && HasStr2) {
1184     size_t Offset = SearchStr.find(ToFindStr);
1185 
1186     if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
1187       return Constant::getNullValue(CI->getType());
1188 
1189     // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
1190     return B.CreateConstInBoundsGEP1_64(B.getInt8Ty(), CI->getArgOperand(0),
1191                                         Offset, "strstr");
1192   }
1193 
1194   // fold strstr(x, "y") -> strchr(x, 'y').
1195   if (HasStr2 && ToFindStr.size() == 1) {
1196     return emitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TLI);
1197   }
1198 
1199   annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
1200   return nullptr;
1201 }
1202 
1203 Value *LibCallSimplifier::optimizeMemRChr(CallInst *CI, IRBuilderBase &B) {
1204   Value *SrcStr = CI->getArgOperand(0);
1205   Value *Size = CI->getArgOperand(2);
1206   annotateNonNullAndDereferenceable(CI, 0, Size, DL);
1207   Value *CharVal = CI->getArgOperand(1);
1208   ConstantInt *LenC = dyn_cast<ConstantInt>(Size);
1209   Value *NullPtr = Constant::getNullValue(CI->getType());
1210 
1211   if (LenC) {
1212     if (LenC->isZero())
1213       // Fold memrchr(x, y, 0) --> null.
1214       return NullPtr;
1215 
1216     if (LenC->isOne()) {
1217       // Fold memrchr(x, y, 1) --> *x == y ? x : null for any x and y,
1218       // constant or otherwise.
1219       Value *Val = B.CreateLoad(B.getInt8Ty(), SrcStr, "memrchr.char0");
1220       // Slice off the character's high end bits.
1221       CharVal = B.CreateTrunc(CharVal, B.getInt8Ty());
1222       Value *Cmp = B.CreateICmpEQ(Val, CharVal, "memrchr.char0cmp");
1223       return B.CreateSelect(Cmp, SrcStr, NullPtr, "memrchr.sel");
1224     }
1225   }
1226 
1227   StringRef Str;
1228   if (!getConstantStringInfo(SrcStr, Str, /*TrimAtNul=*/false))
1229     return nullptr;
1230 
1231   if (Str.size() == 0)
1232     // If the array is empty fold memrchr(A, C, N) to null for any value
1233     // of C and N on the basis that the only valid value of N is zero
1234     // (otherwise the call is undefined).
1235     return NullPtr;
1236 
1237   uint64_t EndOff = UINT64_MAX;
1238   if (LenC) {
1239     EndOff = LenC->getZExtValue();
1240     if (Str.size() < EndOff)
1241       // Punt out-of-bounds accesses to sanitizers and/or libc.
1242       return nullptr;
1243   }
1244 
1245   if (ConstantInt *CharC = dyn_cast<ConstantInt>(CharVal)) {
1246     // Fold memrchr(S, C, N) for a constant C.
1247     size_t Pos = Str.rfind(CharC->getZExtValue(), EndOff);
1248     if (Pos == StringRef::npos)
1249       // When the character is not in the source array fold the result
1250       // to null regardless of Size.
1251       return NullPtr;
1252 
1253     if (LenC)
1254       // Fold memrchr(s, c, N) --> s + Pos for constant N > Pos.
1255       return B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, B.getInt64(Pos));
1256 
1257     if (Str.find(Str[Pos]) == Pos) {
1258       // When there is just a single occurrence of C in S, i.e., the one
1259       // in Str[Pos], fold
1260       //   memrchr(s, c, N) --> N <= Pos ? null : s + Pos
1261       // for nonconstant N.
1262       Value *Cmp = B.CreateICmpULE(Size, ConstantInt::get(Size->getType(), Pos),
1263                                    "memrchr.cmp");
1264       Value *SrcPlus = B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr,
1265                                            B.getInt64(Pos), "memrchr.ptr_plus");
1266       return B.CreateSelect(Cmp, NullPtr, SrcPlus, "memrchr.sel");
1267     }
1268   }
1269 
1270   // Truncate the string to search at most EndOff characters.
1271   Str = Str.substr(0, EndOff);
1272   if (Str.find_first_not_of(Str[0]) != StringRef::npos)
1273     return nullptr;
1274 
1275   // If the source array consists of all equal characters, then for any
1276   // C and N (whether in bounds or not), fold memrchr(S, C, N) to
1277   //   N != 0 && *S == C ? S + N - 1 : null
1278   Type *SizeTy = Size->getType();
1279   Type *Int8Ty = B.getInt8Ty();
1280   Value *NNeZ = B.CreateICmpNE(Size, ConstantInt::get(SizeTy, 0));
1281   // Slice off the sought character's high end bits.
1282   CharVal = B.CreateTrunc(CharVal, Int8Ty);
1283   Value *CEqS0 = B.CreateICmpEQ(ConstantInt::get(Int8Ty, Str[0]), CharVal);
1284   Value *And = B.CreateLogicalAnd(NNeZ, CEqS0);
1285   Value *SizeM1 = B.CreateSub(Size, ConstantInt::get(SizeTy, 1));
1286   Value *SrcPlus =
1287       B.CreateInBoundsGEP(Int8Ty, SrcStr, SizeM1, "memrchr.ptr_plus");
1288   return B.CreateSelect(And, SrcPlus, NullPtr, "memrchr.sel");
1289 }
1290 
1291 Value *LibCallSimplifier::optimizeMemChr(CallInst *CI, IRBuilderBase &B) {
1292   Value *SrcStr = CI->getArgOperand(0);
1293   Value *Size = CI->getArgOperand(2);
1294 
1295   if (isKnownNonZero(Size, DL)) {
1296     annotateNonNullNoUndefBasedOnAccess(CI, 0);
1297     if (isOnlyUsedInEqualityComparison(CI, SrcStr))
1298       return memChrToCharCompare(CI, Size, B, DL);
1299   }
1300 
1301   Value *CharVal = CI->getArgOperand(1);
1302   ConstantInt *CharC = dyn_cast<ConstantInt>(CharVal);
1303   ConstantInt *LenC = dyn_cast<ConstantInt>(Size);
1304   Value *NullPtr = Constant::getNullValue(CI->getType());
1305 
1306   // memchr(x, y, 0) -> null
1307   if (LenC) {
1308     if (LenC->isZero())
1309       return NullPtr;
1310 
1311     if (LenC->isOne()) {
1312       // Fold memchr(x, y, 1) --> *x == y ? x : null for any x and y,
1313       // constant or otherwise.
1314       Value *Val = B.CreateLoad(B.getInt8Ty(), SrcStr, "memchr.char0");
1315       // Slice off the character's high end bits.
1316       CharVal = B.CreateTrunc(CharVal, B.getInt8Ty());
1317       Value *Cmp = B.CreateICmpEQ(Val, CharVal, "memchr.char0cmp");
1318       return B.CreateSelect(Cmp, SrcStr, NullPtr, "memchr.sel");
1319     }
1320   }
1321 
1322   StringRef Str;
1323   if (!getConstantStringInfo(SrcStr, Str, /*TrimAtNul=*/false))
1324     return nullptr;
1325 
1326   if (CharC) {
1327     size_t Pos = Str.find(CharC->getZExtValue());
1328     if (Pos == StringRef::npos)
1329       // When the character is not in the source array fold the result
1330       // to null regardless of Size.
1331       return NullPtr;
1332 
1333     // Fold memchr(s, c, n) -> n <= Pos ? null : s + Pos
1334     // When the constant Size is less than or equal to the character
1335     // position also fold the result to null.
1336     Value *Cmp = B.CreateICmpULE(Size, ConstantInt::get(Size->getType(), Pos),
1337                                  "memchr.cmp");
1338     Value *SrcPlus = B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, B.getInt64(Pos),
1339                                          "memchr.ptr");
1340     return B.CreateSelect(Cmp, NullPtr, SrcPlus);
1341   }
1342 
1343   if (Str.size() == 0)
1344     // If the array is empty fold memchr(A, C, N) to null for any value
1345     // of C and N on the basis that the only valid value of N is zero
1346     // (otherwise the call is undefined).
1347     return NullPtr;
1348 
1349   if (LenC)
1350     Str = substr(Str, LenC->getZExtValue());
1351 
1352   size_t Pos = Str.find_first_not_of(Str[0]);
1353   if (Pos == StringRef::npos
1354       || Str.find_first_not_of(Str[Pos], Pos) == StringRef::npos) {
1355     // If the source array consists of at most two consecutive sequences
1356     // of the same characters, then for any C and N (whether in bounds or
1357     // not), fold memchr(S, C, N) to
1358     //   N != 0 && *S == C ? S : null
1359     // or for the two sequences to:
1360     //   N != 0 && *S == C ? S : (N > Pos && S[Pos] == C ? S + Pos : null)
1361     //   ^Sel2                   ^Sel1 are denoted above.
1362     // The latter makes it also possible to fold strchr() calls with strings
1363     // of the same characters.
1364     Type *SizeTy = Size->getType();
1365     Type *Int8Ty = B.getInt8Ty();
1366 
1367     // Slice off the sought character's high end bits.
1368     CharVal = B.CreateTrunc(CharVal, Int8Ty);
1369 
1370     Value *Sel1 = NullPtr;
1371     if (Pos != StringRef::npos) {
1372       // Handle two consecutive sequences of the same characters.
1373       Value *PosVal = ConstantInt::get(SizeTy, Pos);
1374       Value *StrPos = ConstantInt::get(Int8Ty, Str[Pos]);
1375       Value *CEqSPos = B.CreateICmpEQ(CharVal, StrPos);
1376       Value *NGtPos = B.CreateICmp(ICmpInst::ICMP_UGT, Size, PosVal);
1377       Value *And = B.CreateAnd(CEqSPos, NGtPos);
1378       Value *SrcPlus = B.CreateInBoundsGEP(B.getInt8Ty(), SrcStr, PosVal);
1379       Sel1 = B.CreateSelect(And, SrcPlus, NullPtr, "memchr.sel1");
1380     }
1381 
1382     Value *Str0 = ConstantInt::get(Int8Ty, Str[0]);
1383     Value *CEqS0 = B.CreateICmpEQ(Str0, CharVal);
1384     Value *NNeZ = B.CreateICmpNE(Size, ConstantInt::get(SizeTy, 0));
1385     Value *And = B.CreateAnd(NNeZ, CEqS0);
1386     return B.CreateSelect(And, SrcStr, Sel1, "memchr.sel2");
1387   }
1388 
1389   if (!LenC) {
1390     if (isOnlyUsedInEqualityComparison(CI, SrcStr))
1391       // S is dereferenceable so it's safe to load from it and fold
1392       //   memchr(S, C, N) == S to N && *S == C for any C and N.
1393       // TODO: This is safe even for nonconstant S.
1394       return memChrToCharCompare(CI, Size, B, DL);
1395 
1396     // From now on we need a constant length and constant array.
1397     return nullptr;
1398   }
1399 
1400   bool OptForSize = llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
1401                                                 PGSOQueryType::IRPass);
1402 
1403   // If the char is variable but the input str and length are not we can turn
1404   // this memchr call into a simple bit field test. Of course this only works
1405   // when the return value is only checked against null.
1406   //
1407   // It would be really nice to reuse switch lowering here but we can't change
1408   // the CFG at this point.
1409   //
1410   // memchr("\r\n", C, 2) != nullptr -> (1 << C & ((1 << '\r') | (1 << '\n')))
1411   // != 0
1412   //   after bounds check.
1413   if (OptForSize || Str.empty() || !isOnlyUsedInZeroEqualityComparison(CI))
1414     return nullptr;
1415 
1416   unsigned char Max =
1417       *std::max_element(reinterpret_cast<const unsigned char *>(Str.begin()),
1418                         reinterpret_cast<const unsigned char *>(Str.end()));
1419 
1420   // Make sure the bit field we're about to create fits in a register on the
1421   // target.
1422   // FIXME: On a 64 bit architecture this prevents us from using the
1423   // interesting range of alpha ascii chars. We could do better by emitting
1424   // two bitfields or shifting the range by 64 if no lower chars are used.
1425   if (!DL.fitsInLegalInteger(Max + 1)) {
1426     // Build chain of ORs
1427     // Transform:
1428     //    memchr("abcd", C, 4) != nullptr
1429     // to:
1430     //    (C == 'a' || C == 'b' || C == 'c' || C == 'd') != 0
1431     std::string SortedStr = Str.str();
1432     llvm::sort(SortedStr);
1433     // Compute the number of of non-contiguous ranges.
1434     unsigned NonContRanges = 1;
1435     for (size_t i = 1; i < SortedStr.size(); ++i) {
1436       if (SortedStr[i] > SortedStr[i - 1] + 1) {
1437         NonContRanges++;
1438       }
1439     }
1440 
1441     // Restrict this optimization to profitable cases with one or two range
1442     // checks.
1443     if (NonContRanges > 2)
1444       return nullptr;
1445 
1446     // Slice off the character's high end bits.
1447     CharVal = B.CreateTrunc(CharVal, B.getInt8Ty());
1448 
1449     SmallVector<Value *> CharCompares;
1450     for (unsigned char C : SortedStr)
1451       CharCompares.push_back(B.CreateICmpEQ(CharVal, B.getInt8(C)));
1452 
1453     return B.CreateIntToPtr(B.CreateOr(CharCompares), CI->getType());
1454   }
1455 
1456   // For the bit field use a power-of-2 type with at least 8 bits to avoid
1457   // creating unnecessary illegal types.
1458   unsigned char Width = NextPowerOf2(std::max((unsigned char)7, Max));
1459 
1460   // Now build the bit field.
1461   APInt Bitfield(Width, 0);
1462   for (char C : Str)
1463     Bitfield.setBit((unsigned char)C);
1464   Value *BitfieldC = B.getInt(Bitfield);
1465 
1466   // Adjust width of "C" to the bitfield width, then mask off the high bits.
1467   Value *C = B.CreateZExtOrTrunc(CharVal, BitfieldC->getType());
1468   C = B.CreateAnd(C, B.getIntN(Width, 0xFF));
1469 
1470   // First check that the bit field access is within bounds.
1471   Value *Bounds = B.CreateICmp(ICmpInst::ICMP_ULT, C, B.getIntN(Width, Width),
1472                                "memchr.bounds");
1473 
1474   // Create code that checks if the given bit is set in the field.
1475   Value *Shl = B.CreateShl(B.getIntN(Width, 1ULL), C);
1476   Value *Bits = B.CreateIsNotNull(B.CreateAnd(Shl, BitfieldC), "memchr.bits");
1477 
1478   // Finally merge both checks and cast to pointer type. The inttoptr
1479   // implicitly zexts the i1 to intptr type.
1480   return B.CreateIntToPtr(B.CreateLogicalAnd(Bounds, Bits, "memchr"),
1481                           CI->getType());
1482 }
1483 
1484 // Optimize a memcmp or, when StrNCmp is true, strncmp call CI with constant
1485 // arrays LHS and RHS and nonconstant Size.
1486 static Value *optimizeMemCmpVarSize(CallInst *CI, Value *LHS, Value *RHS,
1487                                     Value *Size, bool StrNCmp,
1488                                     IRBuilderBase &B, const DataLayout &DL) {
1489   if (LHS == RHS) // memcmp(s,s,x) -> 0
1490     return Constant::getNullValue(CI->getType());
1491 
1492   StringRef LStr, RStr;
1493   if (!getConstantStringInfo(LHS, LStr, /*TrimAtNul=*/false) ||
1494       !getConstantStringInfo(RHS, RStr, /*TrimAtNul=*/false))
1495     return nullptr;
1496 
1497   // If the contents of both constant arrays are known, fold a call to
1498   // memcmp(A, B, N) to
1499   //   N <= Pos ? 0 : (A < B ? -1 : B < A ? +1 : 0)
1500   // where Pos is the first mismatch between A and B, determined below.
1501 
1502   uint64_t Pos = 0;
1503   Value *Zero = ConstantInt::get(CI->getType(), 0);
1504   for (uint64_t MinSize = std::min(LStr.size(), RStr.size()); ; ++Pos) {
1505     if (Pos == MinSize ||
1506         (StrNCmp && (LStr[Pos] == '\0' && RStr[Pos] == '\0'))) {
1507       // One array is a leading part of the other of equal or greater
1508       // size, or for strncmp, the arrays are equal strings.
1509       // Fold the result to zero.  Size is assumed to be in bounds, since
1510       // otherwise the call would be undefined.
1511       return Zero;
1512     }
1513 
1514     if (LStr[Pos] != RStr[Pos])
1515       break;
1516   }
1517 
1518   // Normalize the result.
1519   typedef unsigned char UChar;
1520   int IRes = UChar(LStr[Pos]) < UChar(RStr[Pos]) ? -1 : 1;
1521   Value *MaxSize = ConstantInt::get(Size->getType(), Pos);
1522   Value *Cmp = B.CreateICmp(ICmpInst::ICMP_ULE, Size, MaxSize);
1523   Value *Res = ConstantInt::get(CI->getType(), IRes);
1524   return B.CreateSelect(Cmp, Zero, Res);
1525 }
1526 
1527 // Optimize a memcmp call CI with constant size Len.
1528 static Value *optimizeMemCmpConstantSize(CallInst *CI, Value *LHS, Value *RHS,
1529                                          uint64_t Len, IRBuilderBase &B,
1530                                          const DataLayout &DL) {
1531   if (Len == 0) // memcmp(s1,s2,0) -> 0
1532     return Constant::getNullValue(CI->getType());
1533 
1534   // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
1535   if (Len == 1) {
1536     Value *LHSV = B.CreateZExt(B.CreateLoad(B.getInt8Ty(), LHS, "lhsc"),
1537                                CI->getType(), "lhsv");
1538     Value *RHSV = B.CreateZExt(B.CreateLoad(B.getInt8Ty(), RHS, "rhsc"),
1539                                CI->getType(), "rhsv");
1540     return B.CreateSub(LHSV, RHSV, "chardiff");
1541   }
1542 
1543   // memcmp(S1,S2,N/8)==0 -> (*(intN_t*)S1 != *(intN_t*)S2)==0
1544   // TODO: The case where both inputs are constants does not need to be limited
1545   // to legal integers or equality comparison. See block below this.
1546   if (DL.isLegalInteger(Len * 8) && isOnlyUsedInZeroEqualityComparison(CI)) {
1547     IntegerType *IntType = IntegerType::get(CI->getContext(), Len * 8);
1548     Align PrefAlignment = DL.getPrefTypeAlign(IntType);
1549 
1550     // First, see if we can fold either argument to a constant.
1551     Value *LHSV = nullptr;
1552     if (auto *LHSC = dyn_cast<Constant>(LHS))
1553       LHSV = ConstantFoldLoadFromConstPtr(LHSC, IntType, DL);
1554 
1555     Value *RHSV = nullptr;
1556     if (auto *RHSC = dyn_cast<Constant>(RHS))
1557       RHSV = ConstantFoldLoadFromConstPtr(RHSC, IntType, DL);
1558 
1559     // Don't generate unaligned loads. If either source is constant data,
1560     // alignment doesn't matter for that source because there is no load.
1561     if ((LHSV || getKnownAlignment(LHS, DL, CI) >= PrefAlignment) &&
1562         (RHSV || getKnownAlignment(RHS, DL, CI) >= PrefAlignment)) {
1563       if (!LHSV)
1564         LHSV = B.CreateLoad(IntType, LHS, "lhsv");
1565       if (!RHSV)
1566         RHSV = B.CreateLoad(IntType, RHS, "rhsv");
1567       return B.CreateZExt(B.CreateICmpNE(LHSV, RHSV), CI->getType(), "memcmp");
1568     }
1569   }
1570 
1571   return nullptr;
1572 }
1573 
1574 // Most simplifications for memcmp also apply to bcmp.
1575 Value *LibCallSimplifier::optimizeMemCmpBCmpCommon(CallInst *CI,
1576                                                    IRBuilderBase &B) {
1577   Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
1578   Value *Size = CI->getArgOperand(2);
1579 
1580   annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
1581 
1582   if (Value *Res = optimizeMemCmpVarSize(CI, LHS, RHS, Size, false, B, DL))
1583     return Res;
1584 
1585   // Handle constant Size.
1586   ConstantInt *LenC = dyn_cast<ConstantInt>(Size);
1587   if (!LenC)
1588     return nullptr;
1589 
1590   return optimizeMemCmpConstantSize(CI, LHS, RHS, LenC->getZExtValue(), B, DL);
1591 }
1592 
1593 Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilderBase &B) {
1594   Module *M = CI->getModule();
1595   if (Value *V = optimizeMemCmpBCmpCommon(CI, B))
1596     return V;
1597 
1598   // memcmp(x, y, Len) == 0 -> bcmp(x, y, Len) == 0
1599   // bcmp can be more efficient than memcmp because it only has to know that
1600   // there is a difference, not how different one is to the other.
1601   if (isLibFuncEmittable(M, TLI, LibFunc_bcmp) &&
1602       isOnlyUsedInZeroEqualityComparison(CI)) {
1603     Value *LHS = CI->getArgOperand(0);
1604     Value *RHS = CI->getArgOperand(1);
1605     Value *Size = CI->getArgOperand(2);
1606     return copyFlags(*CI, emitBCmp(LHS, RHS, Size, B, DL, TLI));
1607   }
1608 
1609   return nullptr;
1610 }
1611 
1612 Value *LibCallSimplifier::optimizeBCmp(CallInst *CI, IRBuilderBase &B) {
1613   return optimizeMemCmpBCmpCommon(CI, B);
1614 }
1615 
1616 Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilderBase &B) {
1617   Value *Size = CI->getArgOperand(2);
1618   annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
1619   if (isa<IntrinsicInst>(CI))
1620     return nullptr;
1621 
1622   // memcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n)
1623   CallInst *NewCI = B.CreateMemCpy(CI->getArgOperand(0), Align(1),
1624                                    CI->getArgOperand(1), Align(1), Size);
1625   mergeAttributesAndFlags(NewCI, *CI);
1626   return CI->getArgOperand(0);
1627 }
1628 
1629 Value *LibCallSimplifier::optimizeMemCCpy(CallInst *CI, IRBuilderBase &B) {
1630   Value *Dst = CI->getArgOperand(0);
1631   Value *Src = CI->getArgOperand(1);
1632   ConstantInt *StopChar = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1633   ConstantInt *N = dyn_cast<ConstantInt>(CI->getArgOperand(3));
1634   StringRef SrcStr;
1635   if (CI->use_empty() && Dst == Src)
1636     return Dst;
1637   // memccpy(d, s, c, 0) -> nullptr
1638   if (N) {
1639     if (N->isNullValue())
1640       return Constant::getNullValue(CI->getType());
1641     if (!getConstantStringInfo(Src, SrcStr, /*TrimAtNul=*/false) ||
1642         // TODO: Handle zeroinitializer.
1643         !StopChar)
1644       return nullptr;
1645   } else {
1646     return nullptr;
1647   }
1648 
1649   // Wrap arg 'c' of type int to char
1650   size_t Pos = SrcStr.find(StopChar->getSExtValue() & 0xFF);
1651   if (Pos == StringRef::npos) {
1652     if (N->getZExtValue() <= SrcStr.size()) {
1653       copyFlags(*CI, B.CreateMemCpy(Dst, Align(1), Src, Align(1),
1654                                     CI->getArgOperand(3)));
1655       return Constant::getNullValue(CI->getType());
1656     }
1657     return nullptr;
1658   }
1659 
1660   Value *NewN =
1661       ConstantInt::get(N->getType(), std::min(uint64_t(Pos + 1), N->getZExtValue()));
1662   // memccpy -> llvm.memcpy
1663   copyFlags(*CI, B.CreateMemCpy(Dst, Align(1), Src, Align(1), NewN));
1664   return Pos + 1 <= N->getZExtValue()
1665              ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, NewN)
1666              : Constant::getNullValue(CI->getType());
1667 }
1668 
1669 Value *LibCallSimplifier::optimizeMemPCpy(CallInst *CI, IRBuilderBase &B) {
1670   Value *Dst = CI->getArgOperand(0);
1671   Value *N = CI->getArgOperand(2);
1672   // mempcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n), x + n
1673   CallInst *NewCI =
1674       B.CreateMemCpy(Dst, Align(1), CI->getArgOperand(1), Align(1), N);
1675   // Propagate attributes, but memcpy has no return value, so make sure that
1676   // any return attributes are compliant.
1677   // TODO: Attach return value attributes to the 1st operand to preserve them?
1678   mergeAttributesAndFlags(NewCI, *CI);
1679   return B.CreateInBoundsGEP(B.getInt8Ty(), Dst, N);
1680 }
1681 
1682 Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilderBase &B) {
1683   Value *Size = CI->getArgOperand(2);
1684   annotateNonNullAndDereferenceable(CI, {0, 1}, Size, DL);
1685   if (isa<IntrinsicInst>(CI))
1686     return nullptr;
1687 
1688   // memmove(x, y, n) -> llvm.memmove(align 1 x, align 1 y, n)
1689   CallInst *NewCI = B.CreateMemMove(CI->getArgOperand(0), Align(1),
1690                                     CI->getArgOperand(1), Align(1), Size);
1691   mergeAttributesAndFlags(NewCI, *CI);
1692   return CI->getArgOperand(0);
1693 }
1694 
1695 Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilderBase &B) {
1696   Value *Size = CI->getArgOperand(2);
1697   annotateNonNullAndDereferenceable(CI, 0, Size, DL);
1698   if (isa<IntrinsicInst>(CI))
1699     return nullptr;
1700 
1701   // memset(p, v, n) -> llvm.memset(align 1 p, v, n)
1702   Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
1703   CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val, Size, Align(1));
1704   mergeAttributesAndFlags(NewCI, *CI);
1705   return CI->getArgOperand(0);
1706 }
1707 
1708 Value *LibCallSimplifier::optimizeRealloc(CallInst *CI, IRBuilderBase &B) {
1709   if (isa<ConstantPointerNull>(CI->getArgOperand(0)))
1710     return copyFlags(*CI, emitMalloc(CI->getArgOperand(1), B, DL, TLI));
1711 
1712   return nullptr;
1713 }
1714 
1715 // When enabled, replace operator new() calls marked with a hot or cold memprof
1716 // attribute with an operator new() call that takes a __hot_cold_t parameter.
1717 // Currently this is supported by the open source version of tcmalloc, see:
1718 // https://github.com/google/tcmalloc/blob/master/tcmalloc/new_extension.h
1719 Value *LibCallSimplifier::optimizeNew(CallInst *CI, IRBuilderBase &B,
1720                                       LibFunc &Func) {
1721   if (!OptimizeHotColdNew)
1722     return nullptr;
1723 
1724   uint8_t HotCold;
1725   if (CI->getAttributes().getFnAttr("memprof").getValueAsString() == "cold")
1726     HotCold = ColdNewHintValue;
1727   else if (CI->getAttributes().getFnAttr("memprof").getValueAsString() ==
1728            "notcold")
1729     HotCold = NotColdNewHintValue;
1730   else if (CI->getAttributes().getFnAttr("memprof").getValueAsString() == "hot")
1731     HotCold = HotNewHintValue;
1732   else
1733     return nullptr;
1734 
1735   // For calls that already pass a hot/cold hint, only update the hint if
1736   // directed by OptimizeExistingHotColdNew. For other calls to new, add a hint
1737   // if cold or hot, and leave as-is for default handling if "notcold" aka warm.
1738   // Note that in cases where we decide it is "notcold", it might be slightly
1739   // better to replace the hinted call with a non hinted call, to avoid the
1740   // extra parameter and the if condition check of the hint value in the
1741   // allocator. This can be considered in the future.
1742   switch (Func) {
1743   case LibFunc_Znwm12__hot_cold_t:
1744     if (OptimizeExistingHotColdNew)
1745       return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1746                             LibFunc_Znwm12__hot_cold_t, HotCold);
1747     break;
1748   case LibFunc_Znwm:
1749     if (HotCold != NotColdNewHintValue)
1750       return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1751                             LibFunc_Znwm12__hot_cold_t, HotCold);
1752     break;
1753   case LibFunc_Znam12__hot_cold_t:
1754     if (OptimizeExistingHotColdNew)
1755       return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1756                             LibFunc_Znam12__hot_cold_t, HotCold);
1757     break;
1758   case LibFunc_Znam:
1759     if (HotCold != NotColdNewHintValue)
1760       return emitHotColdNew(CI->getArgOperand(0), B, TLI,
1761                             LibFunc_Znam12__hot_cold_t, HotCold);
1762     break;
1763   case LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t:
1764     if (OptimizeExistingHotColdNew)
1765       return emitHotColdNewNoThrow(
1766           CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1767           LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t, HotCold);
1768     break;
1769   case LibFunc_ZnwmRKSt9nothrow_t:
1770     if (HotCold != NotColdNewHintValue)
1771       return emitHotColdNewNoThrow(
1772           CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1773           LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t, HotCold);
1774     break;
1775   case LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t:
1776     if (OptimizeExistingHotColdNew)
1777       return emitHotColdNewNoThrow(
1778           CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1779           LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t, HotCold);
1780     break;
1781   case LibFunc_ZnamRKSt9nothrow_t:
1782     if (HotCold != NotColdNewHintValue)
1783       return emitHotColdNewNoThrow(
1784           CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1785           LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t, HotCold);
1786     break;
1787   case LibFunc_ZnwmSt11align_val_t12__hot_cold_t:
1788     if (OptimizeExistingHotColdNew)
1789       return emitHotColdNewAligned(
1790           CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1791           LibFunc_ZnwmSt11align_val_t12__hot_cold_t, HotCold);
1792     break;
1793   case LibFunc_ZnwmSt11align_val_t:
1794     if (HotCold != NotColdNewHintValue)
1795       return emitHotColdNewAligned(
1796           CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1797           LibFunc_ZnwmSt11align_val_t12__hot_cold_t, HotCold);
1798     break;
1799   case LibFunc_ZnamSt11align_val_t12__hot_cold_t:
1800     if (OptimizeExistingHotColdNew)
1801       return emitHotColdNewAligned(
1802           CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1803           LibFunc_ZnamSt11align_val_t12__hot_cold_t, HotCold);
1804     break;
1805   case LibFunc_ZnamSt11align_val_t:
1806     if (HotCold != NotColdNewHintValue)
1807       return emitHotColdNewAligned(
1808           CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1809           LibFunc_ZnamSt11align_val_t12__hot_cold_t, HotCold);
1810     break;
1811   case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t:
1812     if (OptimizeExistingHotColdNew)
1813       return emitHotColdNewAlignedNoThrow(
1814           CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
1815           TLI, LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t,
1816           HotCold);
1817     break;
1818   case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t:
1819     if (HotCold != NotColdNewHintValue)
1820       return emitHotColdNewAlignedNoThrow(
1821           CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
1822           TLI, LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t,
1823           HotCold);
1824     break;
1825   case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t:
1826     if (OptimizeExistingHotColdNew)
1827       return emitHotColdNewAlignedNoThrow(
1828           CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
1829           TLI, LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t,
1830           HotCold);
1831     break;
1832   case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t:
1833     if (HotCold != NotColdNewHintValue)
1834       return emitHotColdNewAlignedNoThrow(
1835           CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2), B,
1836           TLI, LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t,
1837           HotCold);
1838     break;
1839   case LibFunc_size_returning_new:
1840     if (HotCold != NotColdNewHintValue)
1841       return emitHotColdSizeReturningNew(CI->getArgOperand(0), B, TLI,
1842                                          LibFunc_size_returning_new_hot_cold,
1843                                          HotCold);
1844     break;
1845   case LibFunc_size_returning_new_hot_cold:
1846     if (OptimizeExistingHotColdNew)
1847       return emitHotColdSizeReturningNew(CI->getArgOperand(0), B, TLI,
1848                                          LibFunc_size_returning_new_hot_cold,
1849                                          HotCold);
1850     break;
1851   case LibFunc_size_returning_new_aligned:
1852     if (HotCold != NotColdNewHintValue)
1853       return emitHotColdSizeReturningNewAligned(
1854           CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1855           LibFunc_size_returning_new_aligned_hot_cold, HotCold);
1856     break;
1857   case LibFunc_size_returning_new_aligned_hot_cold:
1858     if (OptimizeExistingHotColdNew)
1859       return emitHotColdSizeReturningNewAligned(
1860           CI->getArgOperand(0), CI->getArgOperand(1), B, TLI,
1861           LibFunc_size_returning_new_aligned_hot_cold, HotCold);
1862     break;
1863   default:
1864     return nullptr;
1865   }
1866   return nullptr;
1867 }
1868 
1869 //===----------------------------------------------------------------------===//
1870 // Math Library Optimizations
1871 //===----------------------------------------------------------------------===//
1872 
1873 // Replace a libcall \p CI with a call to intrinsic \p IID
1874 static Value *replaceUnaryCall(CallInst *CI, IRBuilderBase &B,
1875                                Intrinsic::ID IID) {
1876   CallInst *NewCall = B.CreateUnaryIntrinsic(IID, CI->getArgOperand(0), CI);
1877   NewCall->takeName(CI);
1878   return copyFlags(*CI, NewCall);
1879 }
1880 
1881 /// Return a variant of Val with float type.
1882 /// Currently this works in two cases: If Val is an FPExtension of a float
1883 /// value to something bigger, simply return the operand.
1884 /// If Val is a ConstantFP but can be converted to a float ConstantFP without
1885 /// loss of precision do so.
1886 static Value *valueHasFloatPrecision(Value *Val) {
1887   if (FPExtInst *Cast = dyn_cast<FPExtInst>(Val)) {
1888     Value *Op = Cast->getOperand(0);
1889     if (Op->getType()->isFloatTy())
1890       return Op;
1891   }
1892   if (ConstantFP *Const = dyn_cast<ConstantFP>(Val)) {
1893     APFloat F = Const->getValueAPF();
1894     bool losesInfo;
1895     (void)F.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
1896                     &losesInfo);
1897     if (!losesInfo)
1898       return ConstantFP::get(Const->getContext(), F);
1899   }
1900   return nullptr;
1901 }
1902 
1903 /// Shrink double -> float functions.
1904 static Value *optimizeDoubleFP(CallInst *CI, IRBuilderBase &B,
1905                                bool isBinary, const TargetLibraryInfo *TLI,
1906                                bool isPrecise = false) {
1907   Function *CalleeFn = CI->getCalledFunction();
1908   if (!CI->getType()->isDoubleTy() || !CalleeFn)
1909     return nullptr;
1910 
1911   // If not all the uses of the function are converted to float, then bail out.
1912   // This matters if the precision of the result is more important than the
1913   // precision of the arguments.
1914   if (isPrecise)
1915     for (User *U : CI->users()) {
1916       FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);
1917       if (!Cast || !Cast->getType()->isFloatTy())
1918         return nullptr;
1919     }
1920 
1921   // If this is something like 'g((double) float)', convert to 'gf(float)'.
1922   Value *V[2];
1923   V[0] = valueHasFloatPrecision(CI->getArgOperand(0));
1924   V[1] = isBinary ? valueHasFloatPrecision(CI->getArgOperand(1)) : nullptr;
1925   if (!V[0] || (isBinary && !V[1]))
1926     return nullptr;
1927 
1928   // If call isn't an intrinsic, check that it isn't within a function with the
1929   // same name as the float version of this call, otherwise the result is an
1930   // infinite loop.  For example, from MinGW-w64:
1931   //
1932   // float expf(float val) { return (float) exp((double) val); }
1933   StringRef CalleeName = CalleeFn->getName();
1934   bool IsIntrinsic = CalleeFn->isIntrinsic();
1935   if (!IsIntrinsic) {
1936     StringRef CallerName = CI->getFunction()->getName();
1937     if (!CallerName.empty() && CallerName.back() == 'f' &&
1938         CallerName.size() == (CalleeName.size() + 1) &&
1939         CallerName.starts_with(CalleeName))
1940       return nullptr;
1941   }
1942 
1943   // Propagate the math semantics from the current function to the new function.
1944   IRBuilderBase::FastMathFlagGuard Guard(B);
1945   B.setFastMathFlags(CI->getFastMathFlags());
1946 
1947   // g((double) float) -> (double) gf(float)
1948   Value *R;
1949   if (IsIntrinsic) {
1950     Intrinsic::ID IID = CalleeFn->getIntrinsicID();
1951     R = isBinary ? B.CreateIntrinsic(IID, B.getFloatTy(), V)
1952                  : B.CreateIntrinsic(IID, B.getFloatTy(), V[0]);
1953   } else {
1954     AttributeList CalleeAttrs = CalleeFn->getAttributes();
1955     R = isBinary ? emitBinaryFloatFnCall(V[0], V[1], TLI, CalleeName, B,
1956                                          CalleeAttrs)
1957                  : emitUnaryFloatFnCall(V[0], TLI, CalleeName, B, CalleeAttrs);
1958   }
1959   return B.CreateFPExt(R, B.getDoubleTy());
1960 }
1961 
1962 /// Shrink double -> float for unary functions.
1963 static Value *optimizeUnaryDoubleFP(CallInst *CI, IRBuilderBase &B,
1964                                     const TargetLibraryInfo *TLI,
1965                                     bool isPrecise = false) {
1966   return optimizeDoubleFP(CI, B, false, TLI, isPrecise);
1967 }
1968 
1969 /// Shrink double -> float for binary functions.
1970 static Value *optimizeBinaryDoubleFP(CallInst *CI, IRBuilderBase &B,
1971                                      const TargetLibraryInfo *TLI,
1972                                      bool isPrecise = false) {
1973   return optimizeDoubleFP(CI, B, true, TLI, isPrecise);
1974 }
1975 
1976 // cabs(z) -> sqrt((creal(z)*creal(z)) + (cimag(z)*cimag(z)))
1977 Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilderBase &B) {
1978   Value *Real, *Imag;
1979 
1980   if (CI->arg_size() == 1) {
1981 
1982     if (!CI->isFast())
1983       return nullptr;
1984 
1985     Value *Op = CI->getArgOperand(0);
1986     assert(Op->getType()->isArrayTy() && "Unexpected signature for cabs!");
1987 
1988     Real = B.CreateExtractValue(Op, 0, "real");
1989     Imag = B.CreateExtractValue(Op, 1, "imag");
1990 
1991   } else {
1992     assert(CI->arg_size() == 2 && "Unexpected signature for cabs!");
1993 
1994     Real = CI->getArgOperand(0);
1995     Imag = CI->getArgOperand(1);
1996 
1997     // if real or imaginary part is zero, simplify to abs(cimag(z))
1998     // or abs(creal(z))
1999     Value *AbsOp = nullptr;
2000     if (ConstantFP *ConstReal = dyn_cast<ConstantFP>(Real)) {
2001       if (ConstReal->isZero())
2002         AbsOp = Imag;
2003 
2004     } else if (ConstantFP *ConstImag = dyn_cast<ConstantFP>(Imag)) {
2005       if (ConstImag->isZero())
2006         AbsOp = Real;
2007     }
2008 
2009     if (AbsOp)
2010       return copyFlags(
2011           *CI, B.CreateUnaryIntrinsic(Intrinsic::fabs, AbsOp, CI, "cabs"));
2012 
2013     if (!CI->isFast())
2014       return nullptr;
2015   }
2016 
2017   // Propagate fast-math flags from the existing call to new instructions.
2018   Value *RealReal = B.CreateFMulFMF(Real, Real, CI);
2019   Value *ImagImag = B.CreateFMulFMF(Imag, Imag, CI);
2020   return copyFlags(
2021       *CI, B.CreateUnaryIntrinsic(Intrinsic::sqrt,
2022                                   B.CreateFAddFMF(RealReal, ImagImag, CI), CI,
2023                                   "cabs"));
2024 }
2025 
2026 // Return a properly extended integer (DstWidth bits wide) if the operation is
2027 // an itofp.
2028 static Value *getIntToFPVal(Value *I2F, IRBuilderBase &B, unsigned DstWidth) {
2029   if (isa<SIToFPInst>(I2F) || isa<UIToFPInst>(I2F)) {
2030     Value *Op = cast<Instruction>(I2F)->getOperand(0);
2031     // Make sure that the exponent fits inside an "int" of size DstWidth,
2032     // thus avoiding any range issues that FP has not.
2033     unsigned BitWidth = Op->getType()->getScalarSizeInBits();
2034     if (BitWidth < DstWidth || (BitWidth == DstWidth && isa<SIToFPInst>(I2F))) {
2035       Type *IntTy = Op->getType()->getWithNewBitWidth(DstWidth);
2036       return isa<SIToFPInst>(I2F) ? B.CreateSExt(Op, IntTy)
2037                                   : B.CreateZExt(Op, IntTy);
2038     }
2039   }
2040 
2041   return nullptr;
2042 }
2043 
2044 /// Use exp{,2}(x * y) for pow(exp{,2}(x), y);
2045 /// ldexp(1.0, x) for pow(2.0, itofp(x)); exp2(n * x) for pow(2.0 ** n, x);
2046 /// exp10(x) for pow(10.0, x); exp2(log2(n) * x) for pow(n, x).
2047 Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
2048   Module *M = Pow->getModule();
2049   Value *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
2050   Type *Ty = Pow->getType();
2051   bool Ignored;
2052 
2053   // Evaluate special cases related to a nested function as the base.
2054 
2055   // pow(exp(x), y) -> exp(x * y)
2056   // pow(exp2(x), y) -> exp2(x * y)
2057   // If exp{,2}() is used only once, it is better to fold two transcendental
2058   // math functions into one.  If used again, exp{,2}() would still have to be
2059   // called with the original argument, then keep both original transcendental
2060   // functions.  However, this transformation is only safe with fully relaxed
2061   // math semantics, since, besides rounding differences, it changes overflow
2062   // and underflow behavior quite dramatically.  For example:
2063   //   pow(exp(1000), 0.001) = pow(inf, 0.001) = inf
2064   // Whereas:
2065   //   exp(1000 * 0.001) = exp(1)
2066   // TODO: Loosen the requirement for fully relaxed math semantics.
2067   // TODO: Handle exp10() when more targets have it available.
2068   CallInst *BaseFn = dyn_cast<CallInst>(Base);
2069   if (BaseFn && BaseFn->hasOneUse() && BaseFn->isFast() && Pow->isFast()) {
2070     LibFunc LibFn;
2071 
2072     Function *CalleeFn = BaseFn->getCalledFunction();
2073     if (CalleeFn && TLI->getLibFunc(CalleeFn->getName(), LibFn) &&
2074         isLibFuncEmittable(M, TLI, LibFn)) {
2075       StringRef ExpName;
2076       Intrinsic::ID ID;
2077       Value *ExpFn;
2078       LibFunc LibFnFloat, LibFnDouble, LibFnLongDouble;
2079 
2080       switch (LibFn) {
2081       default:
2082         return nullptr;
2083       case LibFunc_expf:
2084       case LibFunc_exp:
2085       case LibFunc_expl:
2086         ExpName = TLI->getName(LibFunc_exp);
2087         ID = Intrinsic::exp;
2088         LibFnFloat = LibFunc_expf;
2089         LibFnDouble = LibFunc_exp;
2090         LibFnLongDouble = LibFunc_expl;
2091         break;
2092       case LibFunc_exp2f:
2093       case LibFunc_exp2:
2094       case LibFunc_exp2l:
2095         ExpName = TLI->getName(LibFunc_exp2);
2096         ID = Intrinsic::exp2;
2097         LibFnFloat = LibFunc_exp2f;
2098         LibFnDouble = LibFunc_exp2;
2099         LibFnLongDouble = LibFunc_exp2l;
2100         break;
2101       }
2102 
2103       // Create new exp{,2}() with the product as its argument.
2104       Value *FMul = B.CreateFMul(BaseFn->getArgOperand(0), Expo, "mul");
2105       ExpFn = BaseFn->doesNotAccessMemory()
2106                   ? B.CreateUnaryIntrinsic(ID, FMul, nullptr, ExpName)
2107                   : emitUnaryFloatFnCall(FMul, TLI, LibFnDouble, LibFnFloat,
2108                                          LibFnLongDouble, B,
2109                                          BaseFn->getAttributes());
2110 
2111       // Since the new exp{,2}() is different from the original one, dead code
2112       // elimination cannot be trusted to remove it, since it may have side
2113       // effects (e.g., errno).  When the only consumer for the original
2114       // exp{,2}() is pow(), then it has to be explicitly erased.
2115       substituteInParent(BaseFn, ExpFn);
2116       return ExpFn;
2117     }
2118   }
2119 
2120   // Evaluate special cases related to a constant base.
2121 
2122   const APFloat *BaseF;
2123   if (!match(Base, m_APFloat(BaseF)))
2124     return nullptr;
2125 
2126   AttributeList NoAttrs; // Attributes are only meaningful on the original call
2127 
2128   const bool UseIntrinsic = Pow->doesNotAccessMemory();
2129 
2130   // pow(2.0, itofp(x)) -> ldexp(1.0, x)
2131   if ((UseIntrinsic || !Ty->isVectorTy()) && BaseF->isExactlyValue(2.0) &&
2132       (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo)) &&
2133       (UseIntrinsic ||
2134        hasFloatFn(M, TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl))) {
2135 
2136     // TODO: Shouldn't really need to depend on getIntToFPVal for intrinsic. Can
2137     // just directly use the original integer type.
2138     if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize())) {
2139       Constant *One = ConstantFP::get(Ty, 1.0);
2140 
2141       if (UseIntrinsic) {
2142         return copyFlags(*Pow, B.CreateIntrinsic(Intrinsic::ldexp,
2143                                                  {Ty, ExpoI->getType()},
2144                                                  {One, ExpoI}, Pow, "exp2"));
2145       }
2146 
2147       return copyFlags(*Pow, emitBinaryFloatFnCall(
2148                                  One, ExpoI, TLI, LibFunc_ldexp, LibFunc_ldexpf,
2149                                  LibFunc_ldexpl, B, NoAttrs));
2150     }
2151   }
2152 
2153   // pow(2.0 ** n, x) -> exp2(n * x)
2154   if (hasFloatFn(M, TLI, Ty, LibFunc_exp2, LibFunc_exp2f, LibFunc_exp2l)) {
2155     APFloat BaseR = APFloat(1.0);
2156     BaseR.convert(BaseF->getSemantics(), APFloat::rmTowardZero, &Ignored);
2157     BaseR = BaseR / *BaseF;
2158     bool IsInteger = BaseF->isInteger(), IsReciprocal = BaseR.isInteger();
2159     const APFloat *NF = IsReciprocal ? &BaseR : BaseF;
2160     APSInt NI(64, false);
2161     if ((IsInteger || IsReciprocal) &&
2162         NF->convertToInteger(NI, APFloat::rmTowardZero, &Ignored) ==
2163             APFloat::opOK &&
2164         NI > 1 && NI.isPowerOf2()) {
2165       double N = NI.logBase2() * (IsReciprocal ? -1.0 : 1.0);
2166       Value *FMul = B.CreateFMul(Expo, ConstantFP::get(Ty, N), "mul");
2167       if (Pow->doesNotAccessMemory())
2168         return copyFlags(*Pow, B.CreateUnaryIntrinsic(Intrinsic::exp2, FMul,
2169                                                       nullptr, "exp2"));
2170       else
2171         return copyFlags(*Pow, emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2,
2172                                                     LibFunc_exp2f,
2173                                                     LibFunc_exp2l, B, NoAttrs));
2174     }
2175   }
2176 
2177   // pow(10.0, x) -> exp10(x)
2178   if (BaseF->isExactlyValue(10.0) &&
2179       hasFloatFn(M, TLI, Ty, LibFunc_exp10, LibFunc_exp10f, LibFunc_exp10l)) {
2180 
2181     if (Pow->doesNotAccessMemory()) {
2182       CallInst *NewExp10 =
2183           B.CreateIntrinsic(Intrinsic::exp10, {Ty}, {Expo}, Pow, "exp10");
2184       return copyFlags(*Pow, NewExp10);
2185     }
2186 
2187     return copyFlags(*Pow, emitUnaryFloatFnCall(Expo, TLI, LibFunc_exp10,
2188                                                 LibFunc_exp10f, LibFunc_exp10l,
2189                                                 B, NoAttrs));
2190   }
2191 
2192   // pow(x, y) -> exp2(log2(x) * y)
2193   if (Pow->hasApproxFunc() && Pow->hasNoNaNs() && BaseF->isFiniteNonZero() &&
2194       !BaseF->isNegative()) {
2195     // pow(1, inf) is defined to be 1 but exp2(log2(1) * inf) evaluates to NaN.
2196     // Luckily optimizePow has already handled the x == 1 case.
2197     assert(!match(Base, m_FPOne()) &&
2198            "pow(1.0, y) should have been simplified earlier!");
2199 
2200     Value *Log = nullptr;
2201     if (Ty->isFloatTy())
2202       Log = ConstantFP::get(Ty, std::log2(BaseF->convertToFloat()));
2203     else if (Ty->isDoubleTy())
2204       Log = ConstantFP::get(Ty, std::log2(BaseF->convertToDouble()));
2205 
2206     if (Log) {
2207       Value *FMul = B.CreateFMul(Log, Expo, "mul");
2208       if (Pow->doesNotAccessMemory())
2209         return copyFlags(*Pow, B.CreateUnaryIntrinsic(Intrinsic::exp2, FMul,
2210                                                       nullptr, "exp2"));
2211       else if (hasFloatFn(M, TLI, Ty, LibFunc_exp2, LibFunc_exp2f,
2212                           LibFunc_exp2l))
2213         return copyFlags(*Pow, emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2,
2214                                                     LibFunc_exp2f,
2215                                                     LibFunc_exp2l, B, NoAttrs));
2216     }
2217   }
2218 
2219   return nullptr;
2220 }
2221 
2222 static Value *getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno,
2223                           Module *M, IRBuilderBase &B,
2224                           const TargetLibraryInfo *TLI) {
2225   // If errno is never set, then use the intrinsic for sqrt().
2226   if (NoErrno)
2227     return B.CreateUnaryIntrinsic(Intrinsic::sqrt, V, nullptr, "sqrt");
2228 
2229   // Otherwise, use the libcall for sqrt().
2230   if (hasFloatFn(M, TLI, V->getType(), LibFunc_sqrt, LibFunc_sqrtf,
2231                  LibFunc_sqrtl))
2232     // TODO: We also should check that the target can in fact lower the sqrt()
2233     // libcall. We currently have no way to ask this question, so we ask if
2234     // the target has a sqrt() libcall, which is not exactly the same.
2235     return emitUnaryFloatFnCall(V, TLI, LibFunc_sqrt, LibFunc_sqrtf,
2236                                 LibFunc_sqrtl, B, Attrs);
2237 
2238   return nullptr;
2239 }
2240 
2241 /// Use square root in place of pow(x, +/-0.5).
2242 Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) {
2243   Value *Sqrt, *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
2244   Module *Mod = Pow->getModule();
2245   Type *Ty = Pow->getType();
2246 
2247   const APFloat *ExpoF;
2248   if (!match(Expo, m_APFloat(ExpoF)) ||
2249       (!ExpoF->isExactlyValue(0.5) && !ExpoF->isExactlyValue(-0.5)))
2250     return nullptr;
2251 
2252   // Converting pow(X, -0.5) to 1/sqrt(X) may introduce an extra rounding step,
2253   // so that requires fast-math-flags (afn or reassoc).
2254   if (ExpoF->isNegative() && (!Pow->hasApproxFunc() && !Pow->hasAllowReassoc()))
2255     return nullptr;
2256 
2257   // If we have a pow() library call (accesses memory) and we can't guarantee
2258   // that the base is not an infinity, give up:
2259   // pow(-Inf, 0.5) is optionally required to have a result of +Inf (not setting
2260   // errno), but sqrt(-Inf) is required by various standards to set errno.
2261   if (!Pow->doesNotAccessMemory() && !Pow->hasNoInfs() &&
2262       !isKnownNeverInfinity(
2263           Base, 0, SimplifyQuery(DL, TLI, DT, AC, Pow, true, true, DC)))
2264     return nullptr;
2265 
2266   Sqrt = getSqrtCall(Base, AttributeList(), Pow->doesNotAccessMemory(), Mod, B,
2267                      TLI);
2268   if (!Sqrt)
2269     return nullptr;
2270 
2271   // Handle signed zero base by expanding to fabs(sqrt(x)).
2272   if (!Pow->hasNoSignedZeros())
2273     Sqrt = B.CreateUnaryIntrinsic(Intrinsic::fabs, Sqrt, nullptr, "abs");
2274 
2275   Sqrt = copyFlags(*Pow, Sqrt);
2276 
2277   // Handle non finite base by expanding to
2278   // (x == -infinity ? +infinity : sqrt(x)).
2279   if (!Pow->hasNoInfs()) {
2280     Value *PosInf = ConstantFP::getInfinity(Ty),
2281           *NegInf = ConstantFP::getInfinity(Ty, true);
2282     Value *FCmp = B.CreateFCmpOEQ(Base, NegInf, "isinf");
2283     Sqrt = B.CreateSelect(FCmp, PosInf, Sqrt);
2284   }
2285 
2286   // If the exponent is negative, then get the reciprocal.
2287   if (ExpoF->isNegative())
2288     Sqrt = B.CreateFDiv(ConstantFP::get(Ty, 1.0), Sqrt, "reciprocal");
2289 
2290   return Sqrt;
2291 }
2292 
2293 static Value *createPowWithIntegerExponent(Value *Base, Value *Expo, Module *M,
2294                                            IRBuilderBase &B) {
2295   Value *Args[] = {Base, Expo};
2296   Type *Types[] = {Base->getType(), Expo->getType()};
2297   return B.CreateIntrinsic(Intrinsic::powi, Types, Args);
2298 }
2299 
2300 Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) {
2301   Value *Base = Pow->getArgOperand(0);
2302   Value *Expo = Pow->getArgOperand(1);
2303   Function *Callee = Pow->getCalledFunction();
2304   StringRef Name = Callee->getName();
2305   Type *Ty = Pow->getType();
2306   Module *M = Pow->getModule();
2307   bool AllowApprox = Pow->hasApproxFunc();
2308   bool Ignored;
2309 
2310   // Propagate the math semantics from the call to any created instructions.
2311   IRBuilderBase::FastMathFlagGuard Guard(B);
2312   B.setFastMathFlags(Pow->getFastMathFlags());
2313   // Evaluate special cases related to the base.
2314 
2315   // pow(1.0, x) -> 1.0
2316   if (match(Base, m_FPOne()))
2317     return Base;
2318 
2319   if (Value *Exp = replacePowWithExp(Pow, B))
2320     return Exp;
2321 
2322   // Evaluate special cases related to the exponent.
2323 
2324   // pow(x, -1.0) -> 1.0 / x
2325   if (match(Expo, m_SpecificFP(-1.0)))
2326     return B.CreateFDiv(ConstantFP::get(Ty, 1.0), Base, "reciprocal");
2327 
2328   // pow(x, +/-0.0) -> 1.0
2329   if (match(Expo, m_AnyZeroFP()))
2330     return ConstantFP::get(Ty, 1.0);
2331 
2332   // pow(x, 1.0) -> x
2333   if (match(Expo, m_FPOne()))
2334     return Base;
2335 
2336   // pow(x, 2.0) -> x * x
2337   if (match(Expo, m_SpecificFP(2.0)))
2338     return B.CreateFMul(Base, Base, "square");
2339 
2340   if (Value *Sqrt = replacePowWithSqrt(Pow, B))
2341     return Sqrt;
2342 
2343   // If we can approximate pow:
2344   // pow(x, n) -> powi(x, n) * sqrt(x) if n has exactly a 0.5 fraction
2345   // pow(x, n) -> powi(x, n) if n is a constant signed integer value
2346   const APFloat *ExpoF;
2347   if (AllowApprox && match(Expo, m_APFloat(ExpoF)) &&
2348       !ExpoF->isExactlyValue(0.5) && !ExpoF->isExactlyValue(-0.5)) {
2349     APFloat ExpoA(abs(*ExpoF));
2350     APFloat ExpoI(*ExpoF);
2351     Value *Sqrt = nullptr;
2352     if (!ExpoA.isInteger()) {
2353       APFloat Expo2 = ExpoA;
2354       // To check if ExpoA is an integer + 0.5, we add it to itself. If there
2355       // is no floating point exception and the result is an integer, then
2356       // ExpoA == integer + 0.5
2357       if (Expo2.add(ExpoA, APFloat::rmNearestTiesToEven) != APFloat::opOK)
2358         return nullptr;
2359 
2360       if (!Expo2.isInteger())
2361         return nullptr;
2362 
2363       if (ExpoI.roundToIntegral(APFloat::rmTowardNegative) !=
2364           APFloat::opInexact)
2365         return nullptr;
2366       if (!ExpoI.isInteger())
2367         return nullptr;
2368       ExpoF = &ExpoI;
2369 
2370       Sqrt = getSqrtCall(Base, AttributeList(), Pow->doesNotAccessMemory(), M,
2371                          B, TLI);
2372       if (!Sqrt)
2373         return nullptr;
2374     }
2375 
2376     // 0.5 fraction is now optionally handled.
2377     // Do pow -> powi for remaining integer exponent
2378     APSInt IntExpo(TLI->getIntSize(), /*isUnsigned=*/false);
2379     if (ExpoF->isInteger() &&
2380         ExpoF->convertToInteger(IntExpo, APFloat::rmTowardZero, &Ignored) ==
2381             APFloat::opOK) {
2382       Value *PowI = copyFlags(
2383           *Pow,
2384           createPowWithIntegerExponent(
2385               Base, ConstantInt::get(B.getIntNTy(TLI->getIntSize()), IntExpo),
2386               M, B));
2387 
2388       if (PowI && Sqrt)
2389         return B.CreateFMul(PowI, Sqrt);
2390 
2391       return PowI;
2392     }
2393   }
2394 
2395   // powf(x, itofp(y)) -> powi(x, y)
2396   if (AllowApprox && (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo))) {
2397     if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize()))
2398       return copyFlags(*Pow, createPowWithIntegerExponent(Base, ExpoI, M, B));
2399   }
2400 
2401   // Shrink pow() to powf() if the arguments are single precision,
2402   // unless the result is expected to be double precision.
2403   if (UnsafeFPShrink && Name == TLI->getName(LibFunc_pow) &&
2404       hasFloatVersion(M, Name)) {
2405     if (Value *Shrunk = optimizeBinaryDoubleFP(Pow, B, TLI, true))
2406       return Shrunk;
2407   }
2408 
2409   return nullptr;
2410 }
2411 
2412 Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
2413   Module *M = CI->getModule();
2414   Function *Callee = CI->getCalledFunction();
2415   StringRef Name = Callee->getName();
2416   Value *Ret = nullptr;
2417   if (UnsafeFPShrink && Name == TLI->getName(LibFunc_exp2) &&
2418       hasFloatVersion(M, Name))
2419     Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2420 
2421   // If we have an llvm.exp2 intrinsic, emit the llvm.ldexp intrinsic. If we
2422   // have the libcall, emit the libcall.
2423   //
2424   // TODO: In principle we should be able to just always use the intrinsic for
2425   // any doesNotAccessMemory callsite.
2426 
2427   const bool UseIntrinsic = Callee->isIntrinsic();
2428   // Bail out for vectors because the code below only expects scalars.
2429   Type *Ty = CI->getType();
2430   if (!UseIntrinsic && Ty->isVectorTy())
2431     return Ret;
2432 
2433   // exp2(sitofp(x)) -> ldexp(1.0, sext(x))  if sizeof(x) <= IntSize
2434   // exp2(uitofp(x)) -> ldexp(1.0, zext(x))  if sizeof(x) < IntSize
2435   Value *Op = CI->getArgOperand(0);
2436   if ((isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op)) &&
2437       (UseIntrinsic ||
2438        hasFloatFn(M, TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl))) {
2439     if (Value *Exp = getIntToFPVal(Op, B, TLI->getIntSize())) {
2440       Constant *One = ConstantFP::get(Ty, 1.0);
2441 
2442       if (UseIntrinsic) {
2443         return copyFlags(*CI, B.CreateIntrinsic(Intrinsic::ldexp,
2444                                                 {Ty, Exp->getType()},
2445                                                 {One, Exp}, CI));
2446       }
2447 
2448       IRBuilderBase::FastMathFlagGuard Guard(B);
2449       B.setFastMathFlags(CI->getFastMathFlags());
2450       return copyFlags(*CI, emitBinaryFloatFnCall(
2451                                 One, Exp, TLI, LibFunc_ldexp, LibFunc_ldexpf,
2452                                 LibFunc_ldexpl, B, AttributeList()));
2453     }
2454   }
2455 
2456   return Ret;
2457 }
2458 
2459 Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) {
2460   Module *M = CI->getModule();
2461 
2462   // If we can shrink the call to a float function rather than a double
2463   // function, do that first.
2464   Function *Callee = CI->getCalledFunction();
2465   StringRef Name = Callee->getName();
2466   if ((Name == "fmin" || Name == "fmax") && hasFloatVersion(M, Name))
2467     if (Value *Ret = optimizeBinaryDoubleFP(CI, B, TLI))
2468       return Ret;
2469 
2470   // The LLVM intrinsics minnum/maxnum correspond to fmin/fmax. Canonicalize to
2471   // the intrinsics for improved optimization (for example, vectorization).
2472   // No-signed-zeros is implied by the definitions of fmax/fmin themselves.
2473   // From the C standard draft WG14/N1256:
2474   // "Ideally, fmax would be sensitive to the sign of zero, for example
2475   // fmax(-0.0, +0.0) would return +0; however, implementation in software
2476   // might be impractical."
2477   FastMathFlags FMF = CI->getFastMathFlags();
2478   FMF.setNoSignedZeros();
2479 
2480   Intrinsic::ID IID = Callee->getName().starts_with("fmin") ? Intrinsic::minnum
2481                                                             : Intrinsic::maxnum;
2482   return copyFlags(*CI, B.CreateBinaryIntrinsic(IID, CI->getArgOperand(0),
2483                                                 CI->getArgOperand(1), FMF));
2484 }
2485 
2486 Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
2487   Function *LogFn = Log->getCalledFunction();
2488   StringRef LogNm = LogFn->getName();
2489   Intrinsic::ID LogID = LogFn->getIntrinsicID();
2490   Module *Mod = Log->getModule();
2491   Type *Ty = Log->getType();
2492 
2493   if (UnsafeFPShrink && hasFloatVersion(Mod, LogNm))
2494     if (Value *Ret = optimizeUnaryDoubleFP(Log, B, TLI, true))
2495       return Ret;
2496 
2497   LibFunc LogLb, ExpLb, Exp2Lb, Exp10Lb, PowLb;
2498 
2499   // This is only applicable to log(), log2(), log10().
2500   if (TLI->getLibFunc(LogNm, LogLb)) {
2501     switch (LogLb) {
2502     case LibFunc_logf:
2503       LogID = Intrinsic::log;
2504       ExpLb = LibFunc_expf;
2505       Exp2Lb = LibFunc_exp2f;
2506       Exp10Lb = LibFunc_exp10f;
2507       PowLb = LibFunc_powf;
2508       break;
2509     case LibFunc_log:
2510       LogID = Intrinsic::log;
2511       ExpLb = LibFunc_exp;
2512       Exp2Lb = LibFunc_exp2;
2513       Exp10Lb = LibFunc_exp10;
2514       PowLb = LibFunc_pow;
2515       break;
2516     case LibFunc_logl:
2517       LogID = Intrinsic::log;
2518       ExpLb = LibFunc_expl;
2519       Exp2Lb = LibFunc_exp2l;
2520       Exp10Lb = LibFunc_exp10l;
2521       PowLb = LibFunc_powl;
2522       break;
2523     case LibFunc_log2f:
2524       LogID = Intrinsic::log2;
2525       ExpLb = LibFunc_expf;
2526       Exp2Lb = LibFunc_exp2f;
2527       Exp10Lb = LibFunc_exp10f;
2528       PowLb = LibFunc_powf;
2529       break;
2530     case LibFunc_log2:
2531       LogID = Intrinsic::log2;
2532       ExpLb = LibFunc_exp;
2533       Exp2Lb = LibFunc_exp2;
2534       Exp10Lb = LibFunc_exp10;
2535       PowLb = LibFunc_pow;
2536       break;
2537     case LibFunc_log2l:
2538       LogID = Intrinsic::log2;
2539       ExpLb = LibFunc_expl;
2540       Exp2Lb = LibFunc_exp2l;
2541       Exp10Lb = LibFunc_exp10l;
2542       PowLb = LibFunc_powl;
2543       break;
2544     case LibFunc_log10f:
2545       LogID = Intrinsic::log10;
2546       ExpLb = LibFunc_expf;
2547       Exp2Lb = LibFunc_exp2f;
2548       Exp10Lb = LibFunc_exp10f;
2549       PowLb = LibFunc_powf;
2550       break;
2551     case LibFunc_log10:
2552       LogID = Intrinsic::log10;
2553       ExpLb = LibFunc_exp;
2554       Exp2Lb = LibFunc_exp2;
2555       Exp10Lb = LibFunc_exp10;
2556       PowLb = LibFunc_pow;
2557       break;
2558     case LibFunc_log10l:
2559       LogID = Intrinsic::log10;
2560       ExpLb = LibFunc_expl;
2561       Exp2Lb = LibFunc_exp2l;
2562       Exp10Lb = LibFunc_exp10l;
2563       PowLb = LibFunc_powl;
2564       break;
2565     default:
2566       return nullptr;
2567     }
2568 
2569     // Convert libcall to intrinsic if the value is known > 0.
2570     bool IsKnownNoErrno = Log->hasNoNaNs() && Log->hasNoInfs();
2571     if (!IsKnownNoErrno) {
2572       SimplifyQuery SQ(DL, TLI, DT, AC, Log, true, true, DC);
2573       KnownFPClass Known = computeKnownFPClass(
2574           Log->getOperand(0),
2575           KnownFPClass::OrderedLessThanZeroMask | fcSubnormal,
2576           /*Depth=*/0, SQ);
2577       Function *F = Log->getParent()->getParent();
2578       IsKnownNoErrno = Known.cannotBeOrderedLessThanZero() &&
2579                        Known.isKnownNeverLogicalZero(*F, Ty);
2580     }
2581     if (IsKnownNoErrno) {
2582       auto *NewLog = B.CreateUnaryIntrinsic(LogID, Log->getArgOperand(0), Log);
2583       NewLog->copyMetadata(*Log);
2584       return copyFlags(*Log, NewLog);
2585     }
2586   } else if (LogID == Intrinsic::log || LogID == Intrinsic::log2 ||
2587              LogID == Intrinsic::log10) {
2588     if (Ty->getScalarType()->isFloatTy()) {
2589       ExpLb = LibFunc_expf;
2590       Exp2Lb = LibFunc_exp2f;
2591       Exp10Lb = LibFunc_exp10f;
2592       PowLb = LibFunc_powf;
2593     } else if (Ty->getScalarType()->isDoubleTy()) {
2594       ExpLb = LibFunc_exp;
2595       Exp2Lb = LibFunc_exp2;
2596       Exp10Lb = LibFunc_exp10;
2597       PowLb = LibFunc_pow;
2598     } else
2599       return nullptr;
2600   } else
2601     return nullptr;
2602 
2603   // The earlier call must also be 'fast' in order to do these transforms.
2604   CallInst *Arg = dyn_cast<CallInst>(Log->getArgOperand(0));
2605   if (!Log->isFast() || !Arg || !Arg->isFast() || !Arg->hasOneUse())
2606     return nullptr;
2607 
2608   IRBuilderBase::FastMathFlagGuard Guard(B);
2609   B.setFastMathFlags(FastMathFlags::getFast());
2610 
2611   Intrinsic::ID ArgID = Arg->getIntrinsicID();
2612   LibFunc ArgLb = NotLibFunc;
2613   TLI->getLibFunc(*Arg, ArgLb);
2614 
2615   // log(pow(x,y)) -> y*log(x)
2616   AttributeList NoAttrs;
2617   if (ArgLb == PowLb || ArgID == Intrinsic::pow || ArgID == Intrinsic::powi) {
2618     Value *LogX =
2619         Log->doesNotAccessMemory()
2620             ? B.CreateUnaryIntrinsic(LogID, Arg->getOperand(0), nullptr, "log")
2621             : emitUnaryFloatFnCall(Arg->getOperand(0), TLI, LogNm, B, NoAttrs);
2622     Value *Y = Arg->getArgOperand(1);
2623     // Cast exponent to FP if integer.
2624     if (ArgID == Intrinsic::powi)
2625       Y = B.CreateSIToFP(Y, Ty, "cast");
2626     Value *MulY = B.CreateFMul(Y, LogX, "mul");
2627     // Since pow() may have side effects, e.g. errno,
2628     // dead code elimination may not be trusted to remove it.
2629     substituteInParent(Arg, MulY);
2630     return MulY;
2631   }
2632 
2633   // log(exp{,2,10}(y)) -> y*log({e,2,10})
2634   // TODO: There is no exp10() intrinsic yet.
2635   if (ArgLb == ExpLb || ArgLb == Exp2Lb || ArgLb == Exp10Lb ||
2636            ArgID == Intrinsic::exp || ArgID == Intrinsic::exp2) {
2637     Constant *Eul;
2638     if (ArgLb == ExpLb || ArgID == Intrinsic::exp)
2639       // FIXME: Add more precise value of e for long double.
2640       Eul = ConstantFP::get(Log->getType(), numbers::e);
2641     else if (ArgLb == Exp2Lb || ArgID == Intrinsic::exp2)
2642       Eul = ConstantFP::get(Log->getType(), 2.0);
2643     else
2644       Eul = ConstantFP::get(Log->getType(), 10.0);
2645     Value *LogE = Log->doesNotAccessMemory()
2646                       ? B.CreateUnaryIntrinsic(LogID, Eul, nullptr, "log")
2647                       : emitUnaryFloatFnCall(Eul, TLI, LogNm, B, NoAttrs);
2648     Value *MulY = B.CreateFMul(Arg->getArgOperand(0), LogE, "mul");
2649     // Since exp() may have side effects, e.g. errno,
2650     // dead code elimination may not be trusted to remove it.
2651     substituteInParent(Arg, MulY);
2652     return MulY;
2653   }
2654 
2655   return nullptr;
2656 }
2657 
2658 // sqrt(exp(X)) -> exp(X * 0.5)
2659 Value *LibCallSimplifier::mergeSqrtToExp(CallInst *CI, IRBuilderBase &B) {
2660   if (!CI->hasAllowReassoc())
2661     return nullptr;
2662 
2663   Function *SqrtFn = CI->getCalledFunction();
2664   CallInst *Arg = dyn_cast<CallInst>(CI->getArgOperand(0));
2665   if (!Arg || !Arg->hasAllowReassoc() || !Arg->hasOneUse())
2666     return nullptr;
2667   Intrinsic::ID ArgID = Arg->getIntrinsicID();
2668   LibFunc ArgLb = NotLibFunc;
2669   TLI->getLibFunc(*Arg, ArgLb);
2670 
2671   LibFunc SqrtLb, ExpLb, Exp2Lb, Exp10Lb;
2672 
2673   if (TLI->getLibFunc(SqrtFn->getName(), SqrtLb))
2674     switch (SqrtLb) {
2675     case LibFunc_sqrtf:
2676       ExpLb = LibFunc_expf;
2677       Exp2Lb = LibFunc_exp2f;
2678       Exp10Lb = LibFunc_exp10f;
2679       break;
2680     case LibFunc_sqrt:
2681       ExpLb = LibFunc_exp;
2682       Exp2Lb = LibFunc_exp2;
2683       Exp10Lb = LibFunc_exp10;
2684       break;
2685     case LibFunc_sqrtl:
2686       ExpLb = LibFunc_expl;
2687       Exp2Lb = LibFunc_exp2l;
2688       Exp10Lb = LibFunc_exp10l;
2689       break;
2690     default:
2691       return nullptr;
2692     }
2693   else if (SqrtFn->getIntrinsicID() == Intrinsic::sqrt) {
2694     if (CI->getType()->getScalarType()->isFloatTy()) {
2695       ExpLb = LibFunc_expf;
2696       Exp2Lb = LibFunc_exp2f;
2697       Exp10Lb = LibFunc_exp10f;
2698     } else if (CI->getType()->getScalarType()->isDoubleTy()) {
2699       ExpLb = LibFunc_exp;
2700       Exp2Lb = LibFunc_exp2;
2701       Exp10Lb = LibFunc_exp10;
2702     } else
2703       return nullptr;
2704   } else
2705     return nullptr;
2706 
2707   if (ArgLb != ExpLb && ArgLb != Exp2Lb && ArgLb != Exp10Lb &&
2708       ArgID != Intrinsic::exp && ArgID != Intrinsic::exp2)
2709     return nullptr;
2710 
2711   IRBuilderBase::InsertPointGuard Guard(B);
2712   B.SetInsertPoint(Arg);
2713   auto *ExpOperand = Arg->getOperand(0);
2714   auto *FMul =
2715       B.CreateFMulFMF(ExpOperand, ConstantFP::get(ExpOperand->getType(), 0.5),
2716                       CI, "merged.sqrt");
2717 
2718   Arg->setOperand(0, FMul);
2719   return Arg;
2720 }
2721 
2722 Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
2723   Module *M = CI->getModule();
2724   Function *Callee = CI->getCalledFunction();
2725   Value *Ret = nullptr;
2726   // TODO: Once we have a way (other than checking for the existince of the
2727   // libcall) to tell whether our target can lower @llvm.sqrt, relax the
2728   // condition below.
2729   if (isLibFuncEmittable(M, TLI, LibFunc_sqrtf) &&
2730       (Callee->getName() == "sqrt" ||
2731        Callee->getIntrinsicID() == Intrinsic::sqrt))
2732     Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2733 
2734   if (Value *Opt = mergeSqrtToExp(CI, B))
2735     return Opt;
2736 
2737   if (!CI->isFast())
2738     return Ret;
2739 
2740   Instruction *I = dyn_cast<Instruction>(CI->getArgOperand(0));
2741   if (!I || I->getOpcode() != Instruction::FMul || !I->isFast())
2742     return Ret;
2743 
2744   // We're looking for a repeated factor in a multiplication tree,
2745   // so we can do this fold: sqrt(x * x) -> fabs(x);
2746   // or this fold: sqrt((x * x) * y) -> fabs(x) * sqrt(y).
2747   Value *Op0 = I->getOperand(0);
2748   Value *Op1 = I->getOperand(1);
2749   Value *RepeatOp = nullptr;
2750   Value *OtherOp = nullptr;
2751   if (Op0 == Op1) {
2752     // Simple match: the operands of the multiply are identical.
2753     RepeatOp = Op0;
2754   } else {
2755     // Look for a more complicated pattern: one of the operands is itself
2756     // a multiply, so search for a common factor in that multiply.
2757     // Note: We don't bother looking any deeper than this first level or for
2758     // variations of this pattern because instcombine's visitFMUL and/or the
2759     // reassociation pass should give us this form.
2760     Value *MulOp;
2761     if (match(Op0, m_FMul(m_Value(MulOp), m_Deferred(MulOp))) &&
2762         cast<Instruction>(Op0)->isFast()) {
2763       // Pattern: sqrt((x * x) * z)
2764       RepeatOp = MulOp;
2765       OtherOp = Op1;
2766     } else if (match(Op1, m_FMul(m_Value(MulOp), m_Deferred(MulOp))) &&
2767                cast<Instruction>(Op1)->isFast()) {
2768       // Pattern: sqrt(z * (x * x))
2769       RepeatOp = MulOp;
2770       OtherOp = Op0;
2771     }
2772   }
2773   if (!RepeatOp)
2774     return Ret;
2775 
2776   // Fast math flags for any created instructions should match the sqrt
2777   // and multiply.
2778 
2779   // If we found a repeated factor, hoist it out of the square root and
2780   // replace it with the fabs of that factor.
2781   Value *FabsCall =
2782       B.CreateUnaryIntrinsic(Intrinsic::fabs, RepeatOp, I, "fabs");
2783   if (OtherOp) {
2784     // If we found a non-repeated factor, we still need to get its square
2785     // root. We then multiply that by the value that was simplified out
2786     // of the square root calculation.
2787     Value *SqrtCall =
2788         B.CreateUnaryIntrinsic(Intrinsic::sqrt, OtherOp, I, "sqrt");
2789     return copyFlags(*CI, B.CreateFMulFMF(FabsCall, SqrtCall, I));
2790   }
2791   return copyFlags(*CI, FabsCall);
2792 }
2793 
2794 Value *LibCallSimplifier::optimizeFMod(CallInst *CI, IRBuilderBase &B) {
2795 
2796   // fmod(x,y) can set errno if y == 0 or x == +/-inf, and returns Nan in those
2797   // case. If we know those do not happen, then we can convert the fmod into
2798   // frem.
2799   bool IsNoNan = CI->hasNoNaNs();
2800   if (!IsNoNan) {
2801     SimplifyQuery SQ(DL, TLI, DT, AC, CI, true, true, DC);
2802     KnownFPClass Known0 = computeKnownFPClass(CI->getOperand(0), fcInf,
2803                                               /*Depth=*/0, SQ);
2804     if (Known0.isKnownNeverInfinity()) {
2805       KnownFPClass Known1 =
2806           computeKnownFPClass(CI->getOperand(1), fcZero | fcSubnormal,
2807                               /*Depth=*/0, SQ);
2808       Function *F = CI->getParent()->getParent();
2809       IsNoNan = Known1.isKnownNeverLogicalZero(*F, CI->getType());
2810     }
2811   }
2812 
2813   if (IsNoNan) {
2814     Value *FRem = B.CreateFRemFMF(CI->getOperand(0), CI->getOperand(1), CI);
2815     if (auto *FRemI = dyn_cast<Instruction>(FRem))
2816       FRemI->setHasNoNaNs(true);
2817     return FRem;
2818   }
2819   return nullptr;
2820 }
2821 
2822 Value *LibCallSimplifier::optimizeTrigInversionPairs(CallInst *CI,
2823                                                      IRBuilderBase &B) {
2824   Module *M = CI->getModule();
2825   Function *Callee = CI->getCalledFunction();
2826   Value *Ret = nullptr;
2827   StringRef Name = Callee->getName();
2828   if (UnsafeFPShrink &&
2829       (Name == "tan" || Name == "atanh" || Name == "sinh" || Name == "cosh" ||
2830        Name == "asinh") &&
2831       hasFloatVersion(M, Name))
2832     Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2833 
2834   Value *Op1 = CI->getArgOperand(0);
2835   auto *OpC = dyn_cast<CallInst>(Op1);
2836   if (!OpC)
2837     return Ret;
2838 
2839   // Both calls must be 'fast' in order to remove them.
2840   if (!CI->isFast() || !OpC->isFast())
2841     return Ret;
2842 
2843   // tan(atan(x)) -> x
2844   // atanh(tanh(x)) -> x
2845   // sinh(asinh(x)) -> x
2846   // asinh(sinh(x)) -> x
2847   // cosh(acosh(x)) -> x
2848   LibFunc Func;
2849   Function *F = OpC->getCalledFunction();
2850   if (F && TLI->getLibFunc(F->getName(), Func) &&
2851       isLibFuncEmittable(M, TLI, Func)) {
2852     LibFunc inverseFunc = llvm::StringSwitch<LibFunc>(Callee->getName())
2853                               .Case("tan", LibFunc_atan)
2854                               .Case("atanh", LibFunc_tanh)
2855                               .Case("sinh", LibFunc_asinh)
2856                               .Case("cosh", LibFunc_acosh)
2857                               .Case("tanf", LibFunc_atanf)
2858                               .Case("atanhf", LibFunc_tanhf)
2859                               .Case("sinhf", LibFunc_asinhf)
2860                               .Case("coshf", LibFunc_acoshf)
2861                               .Case("tanl", LibFunc_atanl)
2862                               .Case("atanhl", LibFunc_tanhl)
2863                               .Case("sinhl", LibFunc_asinhl)
2864                               .Case("coshl", LibFunc_acoshl)
2865                               .Case("asinh", LibFunc_sinh)
2866                               .Case("asinhf", LibFunc_sinhf)
2867                               .Case("asinhl", LibFunc_sinhl)
2868                               .Default(NumLibFuncs); // Used as error value
2869     if (Func == inverseFunc)
2870       Ret = OpC->getArgOperand(0);
2871   }
2872   return Ret;
2873 }
2874 
2875 static bool isTrigLibCall(CallInst *CI) {
2876   // We can only hope to do anything useful if we can ignore things like errno
2877   // and floating-point exceptions.
2878   // We already checked the prototype.
2879   return CI->doesNotThrow() && CI->doesNotAccessMemory();
2880 }
2881 
2882 static bool insertSinCosCall(IRBuilderBase &B, Function *OrigCallee, Value *Arg,
2883                              bool UseFloat, Value *&Sin, Value *&Cos,
2884                              Value *&SinCos, const TargetLibraryInfo *TLI) {
2885   Module *M = OrigCallee->getParent();
2886   Type *ArgTy = Arg->getType();
2887   Type *ResTy;
2888   StringRef Name;
2889 
2890   Triple T(OrigCallee->getParent()->getTargetTriple());
2891   if (UseFloat) {
2892     Name = "__sincospif_stret";
2893 
2894     assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");
2895     // x86_64 can't use {float, float} since that would be returned in both
2896     // xmm0 and xmm1, which isn't what a real struct would do.
2897     ResTy = T.getArch() == Triple::x86_64
2898                 ? static_cast<Type *>(FixedVectorType::get(ArgTy, 2))
2899                 : static_cast<Type *>(StructType::get(ArgTy, ArgTy));
2900   } else {
2901     Name = "__sincospi_stret";
2902     ResTy = StructType::get(ArgTy, ArgTy);
2903   }
2904 
2905   if (!isLibFuncEmittable(M, TLI, Name))
2906     return false;
2907   LibFunc TheLibFunc;
2908   TLI->getLibFunc(Name, TheLibFunc);
2909   FunctionCallee Callee = getOrInsertLibFunc(
2910       M, *TLI, TheLibFunc, OrigCallee->getAttributes(), ResTy, ArgTy);
2911 
2912   if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
2913     // If the argument is an instruction, it must dominate all uses so put our
2914     // sincos call there.
2915     B.SetInsertPoint(ArgInst->getParent(), ++ArgInst->getIterator());
2916   } else {
2917     // Otherwise (e.g. for a constant) the beginning of the function is as
2918     // good a place as any.
2919     BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
2920     B.SetInsertPoint(&EntryBB, EntryBB.begin());
2921   }
2922 
2923   SinCos = B.CreateCall(Callee, Arg, "sincospi");
2924 
2925   if (SinCos->getType()->isStructTy()) {
2926     Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
2927     Cos = B.CreateExtractValue(SinCos, 1, "cospi");
2928   } else {
2929     Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
2930                                  "sinpi");
2931     Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
2932                                  "cospi");
2933   }
2934 
2935   return true;
2936 }
2937 
2938 static Value *optimizeSymmetricCall(CallInst *CI, bool IsEven,
2939                                     IRBuilderBase &B) {
2940   Value *X;
2941   Value *Src = CI->getArgOperand(0);
2942 
2943   if (match(Src, m_OneUse(m_FNeg(m_Value(X))))) {
2944     auto *Call = B.CreateCall(CI->getCalledFunction(), {X});
2945     Call->copyFastMathFlags(CI);
2946     auto *CallInst = copyFlags(*CI, Call);
2947     if (IsEven) {
2948       // Even function: f(-x) = f(x)
2949       return CallInst;
2950     }
2951     // Odd function: f(-x) = -f(x)
2952     return B.CreateFNegFMF(CallInst, CI);
2953   }
2954 
2955   // Even function: f(abs(x)) = f(x), f(copysign(x, y)) = f(x)
2956   if (IsEven && (match(Src, m_FAbs(m_Value(X))) ||
2957                  match(Src, m_CopySign(m_Value(X), m_Value())))) {
2958     auto *Call = B.CreateCall(CI->getCalledFunction(), {X});
2959     Call->copyFastMathFlags(CI);
2960     return copyFlags(*CI, Call);
2961   }
2962 
2963   return nullptr;
2964 }
2965 
2966 Value *LibCallSimplifier::optimizeSymmetric(CallInst *CI, LibFunc Func,
2967                                             IRBuilderBase &B) {
2968   switch (Func) {
2969   case LibFunc_cos:
2970   case LibFunc_cosf:
2971   case LibFunc_cosl:
2972     return optimizeSymmetricCall(CI, /*IsEven*/ true, B);
2973 
2974   case LibFunc_sin:
2975   case LibFunc_sinf:
2976   case LibFunc_sinl:
2977 
2978   case LibFunc_tan:
2979   case LibFunc_tanf:
2980   case LibFunc_tanl:
2981 
2982   case LibFunc_erf:
2983   case LibFunc_erff:
2984   case LibFunc_erfl:
2985     return optimizeSymmetricCall(CI, /*IsEven*/ false, B);
2986 
2987   default:
2988     return nullptr;
2989   }
2990 }
2991 
2992 Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, bool IsSin, IRBuilderBase &B) {
2993   // Make sure the prototype is as expected, otherwise the rest of the
2994   // function is probably invalid and likely to abort.
2995   if (!isTrigLibCall(CI))
2996     return nullptr;
2997 
2998   Value *Arg = CI->getArgOperand(0);
2999   SmallVector<CallInst *, 1> SinCalls;
3000   SmallVector<CallInst *, 1> CosCalls;
3001   SmallVector<CallInst *, 1> SinCosCalls;
3002 
3003   bool IsFloat = Arg->getType()->isFloatTy();
3004 
3005   // Look for all compatible sinpi, cospi and sincospi calls with the same
3006   // argument. If there are enough (in some sense) we can make the
3007   // substitution.
3008   Function *F = CI->getFunction();
3009   for (User *U : Arg->users())
3010     classifyArgUse(U, F, IsFloat, SinCalls, CosCalls, SinCosCalls);
3011 
3012   // It's only worthwhile if both sinpi and cospi are actually used.
3013   if (SinCalls.empty() || CosCalls.empty())
3014     return nullptr;
3015 
3016   Value *Sin, *Cos, *SinCos;
3017   if (!insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos,
3018                         SinCos, TLI))
3019     return nullptr;
3020 
3021   auto replaceTrigInsts = [this](SmallVectorImpl<CallInst *> &Calls,
3022                                  Value *Res) {
3023     for (CallInst *C : Calls)
3024       replaceAllUsesWith(C, Res);
3025   };
3026 
3027   replaceTrigInsts(SinCalls, Sin);
3028   replaceTrigInsts(CosCalls, Cos);
3029   replaceTrigInsts(SinCosCalls, SinCos);
3030 
3031   return IsSin ? Sin : Cos;
3032 }
3033 
3034 void LibCallSimplifier::classifyArgUse(
3035     Value *Val, Function *F, bool IsFloat,
3036     SmallVectorImpl<CallInst *> &SinCalls,
3037     SmallVectorImpl<CallInst *> &CosCalls,
3038     SmallVectorImpl<CallInst *> &SinCosCalls) {
3039   auto *CI = dyn_cast<CallInst>(Val);
3040   if (!CI || CI->use_empty())
3041     return;
3042 
3043   // Don't consider calls in other functions.
3044   if (CI->getFunction() != F)
3045     return;
3046 
3047   Module *M = CI->getModule();
3048   Function *Callee = CI->getCalledFunction();
3049   LibFunc Func;
3050   if (!Callee || !TLI->getLibFunc(*Callee, Func) ||
3051       !isLibFuncEmittable(M, TLI, Func) ||
3052       !isTrigLibCall(CI))
3053     return;
3054 
3055   if (IsFloat) {
3056     if (Func == LibFunc_sinpif)
3057       SinCalls.push_back(CI);
3058     else if (Func == LibFunc_cospif)
3059       CosCalls.push_back(CI);
3060     else if (Func == LibFunc_sincospif_stret)
3061       SinCosCalls.push_back(CI);
3062   } else {
3063     if (Func == LibFunc_sinpi)
3064       SinCalls.push_back(CI);
3065     else if (Func == LibFunc_cospi)
3066       CosCalls.push_back(CI);
3067     else if (Func == LibFunc_sincospi_stret)
3068       SinCosCalls.push_back(CI);
3069   }
3070 }
3071 
3072 /// Constant folds remquo
3073 Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
3074   const APFloat *X, *Y;
3075   if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
3076       !match(CI->getArgOperand(1), m_APFloat(Y)))
3077     return nullptr;
3078 
3079   APFloat::opStatus Status;
3080   APFloat Quot = *X;
3081   Status = Quot.divide(*Y, APFloat::rmNearestTiesToEven);
3082   if (Status != APFloat::opOK && Status != APFloat::opInexact)
3083     return nullptr;
3084   APFloat Rem = *X;
3085   if (Rem.remainder(*Y) != APFloat::opOK)
3086     return nullptr;
3087 
3088   // TODO: We can only keep at least the three of the last bits of x/y
3089   unsigned IntBW = TLI->getIntSize();
3090   APSInt QuotInt(IntBW, /*isUnsigned=*/false);
3091   bool IsExact;
3092   Status =
3093       Quot.convertToInteger(QuotInt, APFloat::rmNearestTiesToEven, &IsExact);
3094   if (Status != APFloat::opOK && Status != APFloat::opInexact)
3095     return nullptr;
3096 
3097   B.CreateAlignedStore(
3098       ConstantInt::get(B.getIntNTy(IntBW), QuotInt.getExtValue()),
3099       CI->getArgOperand(2), CI->getParamAlign(2));
3100   return ConstantFP::get(CI->getType(), Rem);
3101 }
3102 
3103 /// Constant folds fdim
3104 Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
3105   // Cannot perform the fold unless the call has attribute memory(none)
3106   if (!CI->doesNotAccessMemory())
3107     return nullptr;
3108 
3109   // TODO : Handle undef values
3110   // Propagate poison if any
3111   if (isa<PoisonValue>(CI->getArgOperand(0)))
3112     return CI->getArgOperand(0);
3113   if (isa<PoisonValue>(CI->getArgOperand(1)))
3114     return CI->getArgOperand(1);
3115 
3116   const APFloat *X, *Y;
3117   // Check if both values are constants
3118   if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
3119       !match(CI->getArgOperand(1), m_APFloat(Y)))
3120     return nullptr;
3121 
3122   APFloat Difference = *X;
3123   Difference.subtract(*Y, RoundingMode::NearestTiesToEven);
3124 
3125   APFloat MaxVal =
3126       maximum(Difference, APFloat::getZero(CI->getType()->getFltSemantics()));
3127   return ConstantFP::get(CI->getType(), MaxVal);
3128 }
3129 
3130 //===----------------------------------------------------------------------===//
3131 // Integer Library Call Optimizations
3132 //===----------------------------------------------------------------------===//
3133 
3134 Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilderBase &B) {
3135   // All variants of ffs return int which need not be 32 bits wide.
3136   // ffs{,l,ll}(x) -> x != 0 ? (int)llvm.cttz(x)+1 : 0
3137   Type *RetType = CI->getType();
3138   Value *Op = CI->getArgOperand(0);
3139   Type *ArgType = Op->getType();
3140   Value *V = B.CreateIntrinsic(Intrinsic::cttz, {ArgType}, {Op, B.getTrue()},
3141                                nullptr, "cttz");
3142   V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
3143   V = B.CreateIntCast(V, RetType, false);
3144 
3145   Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
3146   return B.CreateSelect(Cond, V, ConstantInt::get(RetType, 0));
3147 }
3148 
3149 Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilderBase &B) {
3150   // All variants of fls return int which need not be 32 bits wide.
3151   // fls{,l,ll}(x) -> (int)(sizeInBits(x) - llvm.ctlz(x, false))
3152   Value *Op = CI->getArgOperand(0);
3153   Type *ArgType = Op->getType();
3154   Value *V = B.CreateIntrinsic(Intrinsic::ctlz, {ArgType}, {Op, B.getFalse()},
3155                                nullptr, "ctlz");
3156   V = B.CreateSub(ConstantInt::get(V->getType(), ArgType->getIntegerBitWidth()),
3157                   V);
3158   return B.CreateIntCast(V, CI->getType(), false);
3159 }
3160 
3161 Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilderBase &B) {
3162   // abs(x) -> x <s 0 ? -x : x
3163   // The negation has 'nsw' because abs of INT_MIN is undefined.
3164   Value *X = CI->getArgOperand(0);
3165   Value *IsNeg = B.CreateIsNeg(X);
3166   Value *NegX = B.CreateNSWNeg(X, "neg");
3167   return B.CreateSelect(IsNeg, NegX, X);
3168 }
3169 
3170 Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilderBase &B) {
3171   // isdigit(c) -> (c-'0') <u 10
3172   Value *Op = CI->getArgOperand(0);
3173   Type *ArgType = Op->getType();
3174   Op = B.CreateSub(Op, ConstantInt::get(ArgType, '0'), "isdigittmp");
3175   Op = B.CreateICmpULT(Op, ConstantInt::get(ArgType, 10), "isdigit");
3176   return B.CreateZExt(Op, CI->getType());
3177 }
3178 
3179 Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilderBase &B) {
3180   // isascii(c) -> c <u 128
3181   Value *Op = CI->getArgOperand(0);
3182   Type *ArgType = Op->getType();
3183   Op = B.CreateICmpULT(Op, ConstantInt::get(ArgType, 128), "isascii");
3184   return B.CreateZExt(Op, CI->getType());
3185 }
3186 
3187 Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilderBase &B) {
3188   // toascii(c) -> c & 0x7f
3189   return B.CreateAnd(CI->getArgOperand(0),
3190                      ConstantInt::get(CI->getType(), 0x7F));
3191 }
3192 
3193 // Fold calls to atoi, atol, and atoll.
3194 Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilderBase &B) {
3195   StringRef Str;
3196   if (!getConstantStringInfo(CI->getArgOperand(0), Str))
3197     return nullptr;
3198 
3199   return convertStrToInt(CI, Str, nullptr, 10, /*AsSigned=*/true, B);
3200 }
3201 
3202 // Fold calls to strtol, strtoll, strtoul, and strtoull.
3203 Value *LibCallSimplifier::optimizeStrToInt(CallInst *CI, IRBuilderBase &B,
3204                                            bool AsSigned) {
3205   Value *EndPtr = CI->getArgOperand(1);
3206   if (isa<ConstantPointerNull>(EndPtr)) {
3207     // With a null EndPtr, this function won't capture the main argument.
3208     // It would be readonly too, except that it still may write to errno.
3209     CI->addParamAttr(0, Attribute::getWithCaptureInfo(CI->getContext(),
3210                                                       CaptureInfo::none()));
3211     EndPtr = nullptr;
3212   } else if (!isKnownNonZero(EndPtr, DL))
3213     return nullptr;
3214 
3215   StringRef Str;
3216   if (!getConstantStringInfo(CI->getArgOperand(0), Str))
3217     return nullptr;
3218 
3219   if (ConstantInt *CInt = dyn_cast<ConstantInt>(CI->getArgOperand(2))) {
3220     return convertStrToInt(CI, Str, EndPtr, CInt->getSExtValue(), AsSigned, B);
3221   }
3222 
3223   return nullptr;
3224 }
3225 
3226 //===----------------------------------------------------------------------===//
3227 // Formatting and IO Library Call Optimizations
3228 //===----------------------------------------------------------------------===//
3229 
3230 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
3231 
3232 Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilderBase &B,
3233                                                  int StreamArg) {
3234   Function *Callee = CI->getCalledFunction();
3235   // Error reporting calls should be cold, mark them as such.
3236   // This applies even to non-builtin calls: it is only a hint and applies to
3237   // functions that the frontend might not understand as builtins.
3238 
3239   // This heuristic was suggested in:
3240   // Improving Static Branch Prediction in a Compiler
3241   // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu
3242   // Proceedings of PACT'98, Oct. 1998, IEEE
3243   if (!CI->hasFnAttr(Attribute::Cold) &&
3244       isReportingError(Callee, CI, StreamArg)) {
3245     CI->addFnAttr(Attribute::Cold);
3246   }
3247 
3248   return nullptr;
3249 }
3250 
3251 static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {
3252   if (!Callee || !Callee->isDeclaration())
3253     return false;
3254 
3255   if (StreamArg < 0)
3256     return true;
3257 
3258   // These functions might be considered cold, but only if their stream
3259   // argument is stderr.
3260 
3261   if (StreamArg >= (int)CI->arg_size())
3262     return false;
3263   LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
3264   if (!LI)
3265     return false;
3266   GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
3267   if (!GV || !GV->isDeclaration())
3268     return false;
3269   return GV->getName() == "stderr";
3270 }
3271 
3272 Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
3273   // Check for a fixed format string.
3274   StringRef FormatStr;
3275   if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
3276     return nullptr;
3277 
3278   // Empty format string -> noop.
3279   if (FormatStr.empty()) // Tolerate printf's declared void.
3280     return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);
3281 
3282   // Do not do any of the following transformations if the printf return value
3283   // is used, in general the printf return value is not compatible with either
3284   // putchar() or puts().
3285   if (!CI->use_empty())
3286     return nullptr;
3287 
3288   Type *IntTy = CI->getType();
3289   // printf("x") -> putchar('x'), even for "%" and "%%".
3290   if (FormatStr.size() == 1 || FormatStr == "%%") {
3291     // Convert the character to unsigned char before passing it to putchar
3292     // to avoid host-specific sign extension in the IR.  Putchar converts
3293     // it to unsigned char regardless.
3294     Value *IntChar = ConstantInt::get(IntTy, (unsigned char)FormatStr[0]);
3295     return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3296   }
3297 
3298   // Try to remove call or emit putchar/puts.
3299   if (FormatStr == "%s" && CI->arg_size() > 1) {
3300     StringRef OperandStr;
3301     if (!getConstantStringInfo(CI->getOperand(1), OperandStr))
3302       return nullptr;
3303     // printf("%s", "") --> NOP
3304     if (OperandStr.empty())
3305       return (Value *)CI;
3306     // printf("%s", "a") --> putchar('a')
3307     if (OperandStr.size() == 1) {
3308       // Convert the character to unsigned char before passing it to putchar
3309       // to avoid host-specific sign extension in the IR.  Putchar converts
3310       // it to unsigned char regardless.
3311       Value *IntChar = ConstantInt::get(IntTy, (unsigned char)OperandStr[0]);
3312       return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3313     }
3314     // printf("%s", str"\n") --> puts(str)
3315     if (OperandStr.back() == '\n') {
3316       OperandStr = OperandStr.drop_back();
3317       Value *GV = B.CreateGlobalString(OperandStr, "str");
3318       return copyFlags(*CI, emitPutS(GV, B, TLI));
3319     }
3320     return nullptr;
3321   }
3322 
3323   // printf("foo\n") --> puts("foo")
3324   if (FormatStr.back() == '\n' &&
3325       !FormatStr.contains('%')) { // No format characters.
3326     // Create a string literal with no \n on it.  We expect the constant merge
3327     // pass to be run after this pass, to merge duplicate strings.
3328     FormatStr = FormatStr.drop_back();
3329     Value *GV = B.CreateGlobalString(FormatStr, "str");
3330     return copyFlags(*CI, emitPutS(GV, B, TLI));
3331   }
3332 
3333   // Optimize specific format strings.
3334   // printf("%c", chr) --> putchar(chr)
3335   if (FormatStr == "%c" && CI->arg_size() > 1 &&
3336       CI->getArgOperand(1)->getType()->isIntegerTy()) {
3337     // Convert the argument to the type expected by putchar, i.e., int, which
3338     // need not be 32 bits wide but which is the same as printf's return type.
3339     Value *IntChar = B.CreateIntCast(CI->getArgOperand(1), IntTy, false);
3340     return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3341   }
3342 
3343   // printf("%s\n", str) --> puts(str)
3344   if (FormatStr == "%s\n" && CI->arg_size() > 1 &&
3345       CI->getArgOperand(1)->getType()->isPointerTy())
3346     return copyFlags(*CI, emitPutS(CI->getArgOperand(1), B, TLI));
3347   return nullptr;
3348 }
3349 
3350 Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilderBase &B) {
3351 
3352   Module *M = CI->getModule();
3353   Function *Callee = CI->getCalledFunction();
3354   FunctionType *FT = Callee->getFunctionType();
3355   if (Value *V = optimizePrintFString(CI, B)) {
3356     return V;
3357   }
3358 
3359   annotateNonNullNoUndefBasedOnAccess(CI, 0);
3360 
3361   // printf(format, ...) -> iprintf(format, ...) if no floating point
3362   // arguments.
3363   if (isLibFuncEmittable(M, TLI, LibFunc_iprintf) &&
3364       !callHasFloatingPointArgument(CI)) {
3365     FunctionCallee IPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_iprintf, FT,
3366                                                   Callee->getAttributes());
3367     CallInst *New = cast<CallInst>(CI->clone());
3368     New->setCalledFunction(IPrintFFn);
3369     B.Insert(New);
3370     return New;
3371   }
3372 
3373   // printf(format, ...) -> __small_printf(format, ...) if no 128-bit floating point
3374   // arguments.
3375   if (isLibFuncEmittable(M, TLI, LibFunc_small_printf) &&
3376       !callHasFP128Argument(CI)) {
3377     auto SmallPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_small_printf, FT,
3378                                             Callee->getAttributes());
3379     CallInst *New = cast<CallInst>(CI->clone());
3380     New->setCalledFunction(SmallPrintFFn);
3381     B.Insert(New);
3382     return New;
3383   }
3384 
3385   return nullptr;
3386 }
3387 
3388 Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI,
3389                                                 IRBuilderBase &B) {
3390   // Check for a fixed format string.
3391   StringRef FormatStr;
3392   if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
3393     return nullptr;
3394 
3395   // If we just have a format string (nothing else crazy) transform it.
3396   Value *Dest = CI->getArgOperand(0);
3397   if (CI->arg_size() == 2) {
3398     // Make sure there's no % in the constant array.  We could try to handle
3399     // %% -> % in the future if we cared.
3400     if (FormatStr.contains('%'))
3401       return nullptr; // we found a format specifier, bail out.
3402 
3403     // sprintf(str, fmt) -> llvm.memcpy(align 1 str, align 1 fmt, strlen(fmt)+1)
3404     B.CreateMemCpy(Dest, Align(1), CI->getArgOperand(1), Align(1),
3405                    // Copy the null byte.
3406                    TLI->getAsSizeT(FormatStr.size() + 1, *CI->getModule()));
3407     return ConstantInt::get(CI->getType(), FormatStr.size());
3408   }
3409 
3410   // The remaining optimizations require the format string to be "%s" or "%c"
3411   // and have an extra operand.
3412   if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
3413     return nullptr;
3414 
3415   // Decode the second character of the format string.
3416   if (FormatStr[1] == 'c') {
3417     // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
3418     if (!CI->getArgOperand(2)->getType()->isIntegerTy())
3419       return nullptr;
3420     Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
3421     Value *Ptr = Dest;
3422     B.CreateStore(V, Ptr);
3423     Ptr = B.CreateInBoundsGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
3424     B.CreateStore(B.getInt8(0), Ptr);
3425 
3426     return ConstantInt::get(CI->getType(), 1);
3427   }
3428 
3429   if (FormatStr[1] == 's') {
3430     // sprintf(dest, "%s", str) -> llvm.memcpy(align 1 dest, align 1 str,
3431     // strlen(str)+1)
3432     if (!CI->getArgOperand(2)->getType()->isPointerTy())
3433       return nullptr;
3434 
3435     if (CI->use_empty())
3436       // sprintf(dest, "%s", str) -> strcpy(dest, str)
3437       return copyFlags(*CI, emitStrCpy(Dest, CI->getArgOperand(2), B, TLI));
3438 
3439     uint64_t SrcLen = GetStringLength(CI->getArgOperand(2));
3440     if (SrcLen) {
3441       B.CreateMemCpy(Dest, Align(1), CI->getArgOperand(2), Align(1),
3442                      TLI->getAsSizeT(SrcLen, *CI->getModule()));
3443       // Returns total number of characters written without null-character.
3444       return ConstantInt::get(CI->getType(), SrcLen - 1);
3445     } else if (Value *V = emitStpCpy(Dest, CI->getArgOperand(2), B, TLI)) {
3446       // sprintf(dest, "%s", str) -> stpcpy(dest, str) - dest
3447       Value *PtrDiff = B.CreatePtrDiff(B.getInt8Ty(), V, Dest);
3448       return B.CreateIntCast(PtrDiff, CI->getType(), false);
3449     }
3450 
3451     if (llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3452                                     PGSOQueryType::IRPass))
3453       return nullptr;
3454 
3455     Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
3456     if (!Len)
3457       return nullptr;
3458     Value *IncLen =
3459         B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
3460     B.CreateMemCpy(Dest, Align(1), CI->getArgOperand(2), Align(1), IncLen);
3461 
3462     // The sprintf result is the unincremented number of bytes in the string.
3463     return B.CreateIntCast(Len, CI->getType(), false);
3464   }
3465   return nullptr;
3466 }
3467 
3468 Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilderBase &B) {
3469   Module *M = CI->getModule();
3470   Function *Callee = CI->getCalledFunction();
3471   FunctionType *FT = Callee->getFunctionType();
3472   if (Value *V = optimizeSPrintFString(CI, B)) {
3473     return V;
3474   }
3475 
3476   annotateNonNullNoUndefBasedOnAccess(CI, {0, 1});
3477 
3478   // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
3479   // point arguments.
3480   if (isLibFuncEmittable(M, TLI, LibFunc_siprintf) &&
3481       !callHasFloatingPointArgument(CI)) {
3482     FunctionCallee SIPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_siprintf,
3483                                                    FT, Callee->getAttributes());
3484     CallInst *New = cast<CallInst>(CI->clone());
3485     New->setCalledFunction(SIPrintFFn);
3486     B.Insert(New);
3487     return New;
3488   }
3489 
3490   // sprintf(str, format, ...) -> __small_sprintf(str, format, ...) if no 128-bit
3491   // floating point arguments.
3492   if (isLibFuncEmittable(M, TLI, LibFunc_small_sprintf) &&
3493       !callHasFP128Argument(CI)) {
3494     auto SmallSPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_small_sprintf, FT,
3495                                              Callee->getAttributes());
3496     CallInst *New = cast<CallInst>(CI->clone());
3497     New->setCalledFunction(SmallSPrintFFn);
3498     B.Insert(New);
3499     return New;
3500   }
3501 
3502   return nullptr;
3503 }
3504 
3505 // Transform an snprintf call CI with the bound N to format the string Str
3506 // either to a call to memcpy, or to single character a store, or to nothing,
3507 // and fold the result to a constant.  A nonnull StrArg refers to the string
3508 // argument being formatted.  Otherwise the call is one with N < 2 and
3509 // the "%c" directive to format a single character.
3510 Value *LibCallSimplifier::emitSnPrintfMemCpy(CallInst *CI, Value *StrArg,
3511                                              StringRef Str, uint64_t N,
3512                                              IRBuilderBase &B) {
3513   assert(StrArg || (N < 2 && Str.size() == 1));
3514 
3515   unsigned IntBits = TLI->getIntSize();
3516   uint64_t IntMax = maxIntN(IntBits);
3517   if (Str.size() > IntMax)
3518     // Bail if the string is longer than INT_MAX.  POSIX requires
3519     // implementations to set errno to EOVERFLOW in this case, in
3520     // addition to when N is larger than that (checked by the caller).
3521     return nullptr;
3522 
3523   Value *StrLen = ConstantInt::get(CI->getType(), Str.size());
3524   if (N == 0)
3525     return StrLen;
3526 
3527   // Set to the number of bytes to copy fron StrArg which is also
3528   // the offset of the terinating nul.
3529   uint64_t NCopy;
3530   if (N > Str.size())
3531     // Copy the full string, including the terminating nul (which must
3532     // be present regardless of the bound).
3533     NCopy = Str.size() + 1;
3534   else
3535     NCopy = N - 1;
3536 
3537   Value *DstArg = CI->getArgOperand(0);
3538   if (NCopy && StrArg)
3539     // Transform the call to lvm.memcpy(dst, fmt, N).
3540     copyFlags(*CI, B.CreateMemCpy(DstArg, Align(1), StrArg, Align(1),
3541                                   TLI->getAsSizeT(NCopy, *CI->getModule())));
3542 
3543   if (N > Str.size())
3544     // Return early when the whole format string, including the final nul,
3545     // has been copied.
3546     return StrLen;
3547 
3548   // Otherwise, when truncating the string append a terminating nul.
3549   Type *Int8Ty = B.getInt8Ty();
3550   Value *NulOff = B.getIntN(IntBits, NCopy);
3551   Value *DstEnd = B.CreateInBoundsGEP(Int8Ty, DstArg, NulOff, "endptr");
3552   B.CreateStore(ConstantInt::get(Int8Ty, 0), DstEnd);
3553   return StrLen;
3554 }
3555 
3556 Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI,
3557                                                  IRBuilderBase &B) {
3558   // Check for size
3559   ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
3560   if (!Size)
3561     return nullptr;
3562 
3563   uint64_t N = Size->getZExtValue();
3564   uint64_t IntMax = maxIntN(TLI->getIntSize());
3565   if (N > IntMax)
3566     // Bail if the bound exceeds INT_MAX.  POSIX requires implementations
3567     // to set errno to EOVERFLOW in this case.
3568     return nullptr;
3569 
3570   Value *DstArg = CI->getArgOperand(0);
3571   Value *FmtArg = CI->getArgOperand(2);
3572 
3573   // Check for a fixed format string.
3574   StringRef FormatStr;
3575   if (!getConstantStringInfo(FmtArg, FormatStr))
3576     return nullptr;
3577 
3578   // If we just have a format string (nothing else crazy) transform it.
3579   if (CI->arg_size() == 3) {
3580     if (FormatStr.contains('%'))
3581       // Bail if the format string contains a directive and there are
3582       // no arguments.  We could handle "%%" in the future.
3583       return nullptr;
3584 
3585     return emitSnPrintfMemCpy(CI, FmtArg, FormatStr, N, B);
3586   }
3587 
3588   // The remaining optimizations require the format string to be "%s" or "%c"
3589   // and have an extra operand.
3590   if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() != 4)
3591     return nullptr;
3592 
3593   // Decode the second character of the format string.
3594   if (FormatStr[1] == 'c') {
3595     if (N <= 1) {
3596       // Use an arbitary string of length 1 to transform the call into
3597       // either a nul store (N == 1) or a no-op (N == 0) and fold it
3598       // to one.
3599       StringRef CharStr("*");
3600       return emitSnPrintfMemCpy(CI, nullptr, CharStr, N, B);
3601     }
3602 
3603     // snprintf(dst, size, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
3604     if (!CI->getArgOperand(3)->getType()->isIntegerTy())
3605       return nullptr;
3606     Value *V = B.CreateTrunc(CI->getArgOperand(3), B.getInt8Ty(), "char");
3607     Value *Ptr = DstArg;
3608     B.CreateStore(V, Ptr);
3609     Ptr = B.CreateInBoundsGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
3610     B.CreateStore(B.getInt8(0), Ptr);
3611     return ConstantInt::get(CI->getType(), 1);
3612   }
3613 
3614   if (FormatStr[1] != 's')
3615     return nullptr;
3616 
3617   Value *StrArg = CI->getArgOperand(3);
3618   // snprintf(dest, size, "%s", str) to llvm.memcpy(dest, str, len+1, 1)
3619   StringRef Str;
3620   if (!getConstantStringInfo(StrArg, Str))
3621     return nullptr;
3622 
3623   return emitSnPrintfMemCpy(CI, StrArg, Str, N, B);
3624 }
3625 
3626 Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilderBase &B) {
3627   if (Value *V = optimizeSnPrintFString(CI, B)) {
3628     return V;
3629   }
3630 
3631   if (isKnownNonZero(CI->getOperand(1), DL))
3632     annotateNonNullNoUndefBasedOnAccess(CI, 0);
3633   return nullptr;
3634 }
3635 
3636 Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI,
3637                                                 IRBuilderBase &B) {
3638   optimizeErrorReporting(CI, B, 0);
3639 
3640   // All the optimizations depend on the format string.
3641   StringRef FormatStr;
3642   if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
3643     return nullptr;
3644 
3645   // Do not do any of the following transformations if the fprintf return
3646   // value is used, in general the fprintf return value is not compatible
3647   // with fwrite(), fputc() or fputs().
3648   if (!CI->use_empty())
3649     return nullptr;
3650 
3651   // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
3652   if (CI->arg_size() == 2) {
3653     // Could handle %% -> % if we cared.
3654     if (FormatStr.contains('%'))
3655       return nullptr; // We found a format specifier.
3656 
3657     return copyFlags(
3658         *CI, emitFWrite(CI->getArgOperand(1),
3659                         TLI->getAsSizeT(FormatStr.size(), *CI->getModule()),
3660                         CI->getArgOperand(0), B, DL, TLI));
3661   }
3662 
3663   // The remaining optimizations require the format string to be "%s" or "%c"
3664   // and have an extra operand.
3665   if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
3666     return nullptr;
3667 
3668   // Decode the second character of the format string.
3669   if (FormatStr[1] == 'c') {
3670     // fprintf(F, "%c", chr) --> fputc((int)chr, F)
3671     if (!CI->getArgOperand(2)->getType()->isIntegerTy())
3672       return nullptr;
3673     Type *IntTy = B.getIntNTy(TLI->getIntSize());
3674     Value *V = B.CreateIntCast(CI->getArgOperand(2), IntTy, /*isSigned*/ true,
3675                                "chari");
3676     return copyFlags(*CI, emitFPutC(V, CI->getArgOperand(0), B, TLI));
3677   }
3678 
3679   if (FormatStr[1] == 's') {
3680     // fprintf(F, "%s", str) --> fputs(str, F)
3681     if (!CI->getArgOperand(2)->getType()->isPointerTy())
3682       return nullptr;
3683     return copyFlags(
3684         *CI, emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI));
3685   }
3686   return nullptr;
3687 }
3688 
3689 Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilderBase &B) {
3690   Module *M = CI->getModule();
3691   Function *Callee = CI->getCalledFunction();
3692   FunctionType *FT = Callee->getFunctionType();
3693   if (Value *V = optimizeFPrintFString(CI, B)) {
3694     return V;
3695   }
3696 
3697   // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
3698   // floating point arguments.
3699   if (isLibFuncEmittable(M, TLI, LibFunc_fiprintf) &&
3700       !callHasFloatingPointArgument(CI)) {
3701     FunctionCallee FIPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_fiprintf,
3702                                                    FT, Callee->getAttributes());
3703     CallInst *New = cast<CallInst>(CI->clone());
3704     New->setCalledFunction(FIPrintFFn);
3705     B.Insert(New);
3706     return New;
3707   }
3708 
3709   // fprintf(stream, format, ...) -> __small_fprintf(stream, format, ...) if no
3710   // 128-bit floating point arguments.
3711   if (isLibFuncEmittable(M, TLI, LibFunc_small_fprintf) &&
3712       !callHasFP128Argument(CI)) {
3713     auto SmallFPrintFFn =
3714         getOrInsertLibFunc(M, *TLI, LibFunc_small_fprintf, FT,
3715                            Callee->getAttributes());
3716     CallInst *New = cast<CallInst>(CI->clone());
3717     New->setCalledFunction(SmallFPrintFFn);
3718     B.Insert(New);
3719     return New;
3720   }
3721 
3722   return nullptr;
3723 }
3724 
3725 Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilderBase &B) {
3726   optimizeErrorReporting(CI, B, 3);
3727 
3728   // Get the element size and count.
3729   ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
3730   ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
3731   if (SizeC && CountC) {
3732     uint64_t Bytes = SizeC->getZExtValue() * CountC->getZExtValue();
3733 
3734     // If this is writing zero records, remove the call (it's a noop).
3735     if (Bytes == 0)
3736       return ConstantInt::get(CI->getType(), 0);
3737 
3738     // If this is writing one byte, turn it into fputc.
3739     // This optimisation is only valid, if the return value is unused.
3740     if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
3741       Value *Char = B.CreateLoad(B.getInt8Ty(), CI->getArgOperand(0), "char");
3742       Type *IntTy = B.getIntNTy(TLI->getIntSize());
3743       Value *Cast = B.CreateIntCast(Char, IntTy, /*isSigned*/ true, "chari");
3744       Value *NewCI = emitFPutC(Cast, CI->getArgOperand(3), B, TLI);
3745       return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
3746     }
3747   }
3748 
3749   return nullptr;
3750 }
3751 
3752 Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) {
3753   optimizeErrorReporting(CI, B, 1);
3754 
3755   // Don't rewrite fputs to fwrite when optimising for size because fwrite
3756   // requires more arguments and thus extra MOVs are required.
3757   if (llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3758                                   PGSOQueryType::IRPass))
3759     return nullptr;
3760 
3761   // We can't optimize if return value is used.
3762   if (!CI->use_empty())
3763     return nullptr;
3764 
3765   // fputs(s,F) --> fwrite(s,strlen(s),1,F)
3766   uint64_t Len = GetStringLength(CI->getArgOperand(0));
3767   if (!Len)
3768     return nullptr;
3769 
3770   // Known to have no uses (see above).
3771   unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
3772   Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
3773   return copyFlags(
3774       *CI,
3775       emitFWrite(CI->getArgOperand(0),
3776                  ConstantInt::get(SizeTTy, Len - 1),
3777                  CI->getArgOperand(1), B, DL, TLI));
3778 }
3779 
3780 Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilderBase &B) {
3781   annotateNonNullNoUndefBasedOnAccess(CI, 0);
3782   if (!CI->use_empty())
3783     return nullptr;
3784 
3785   // Check for a constant string.
3786   // puts("") -> putchar('\n')
3787   StringRef Str;
3788   if (getConstantStringInfo(CI->getArgOperand(0), Str) && Str.empty()) {
3789     // putchar takes an argument of the same type as puts returns, i.e.,
3790     // int, which need not be 32 bits wide.
3791     Type *IntTy = CI->getType();
3792     return copyFlags(*CI, emitPutChar(ConstantInt::get(IntTy, '\n'), B, TLI));
3793   }
3794 
3795   return nullptr;
3796 }
3797 
3798 Value *LibCallSimplifier::optimizeExit(CallInst *CI) {
3799 
3800   // Mark 'exit' as cold if its not exit(0) (success).
3801   const APInt *C;
3802   if (!CI->hasFnAttr(Attribute::Cold) &&
3803       match(CI->getArgOperand(0), m_APInt(C)) && !C->isZero()) {
3804     CI->addFnAttr(Attribute::Cold);
3805   }
3806   return nullptr;
3807 }
3808 
3809 Value *LibCallSimplifier::optimizeBCopy(CallInst *CI, IRBuilderBase &B) {
3810   // bcopy(src, dst, n) -> llvm.memmove(dst, src, n)
3811   return copyFlags(*CI, B.CreateMemMove(CI->getArgOperand(1), Align(1),
3812                                         CI->getArgOperand(0), Align(1),
3813                                         CI->getArgOperand(2)));
3814 }
3815 
3816 bool LibCallSimplifier::hasFloatVersion(const Module *M, StringRef FuncName) {
3817   SmallString<20> FloatFuncName = FuncName;
3818   FloatFuncName += 'f';
3819   return isLibFuncEmittable(M, TLI, FloatFuncName);
3820 }
3821 
3822 Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI,
3823                                                       IRBuilderBase &Builder) {
3824   Module *M = CI->getModule();
3825   LibFunc Func;
3826   Function *Callee = CI->getCalledFunction();
3827 
3828   // Check for string/memory library functions.
3829   if (TLI->getLibFunc(*Callee, Func) && isLibFuncEmittable(M, TLI, Func)) {
3830     // Make sure we never change the calling convention.
3831     assert(
3832         (ignoreCallingConv(Func) ||
3833          TargetLibraryInfoImpl::isCallingConvCCompatible(CI)) &&
3834         "Optimizing string/memory libcall would change the calling convention");
3835     switch (Func) {
3836     case LibFunc_strcat:
3837       return optimizeStrCat(CI, Builder);
3838     case LibFunc_strncat:
3839       return optimizeStrNCat(CI, Builder);
3840     case LibFunc_strchr:
3841       return optimizeStrChr(CI, Builder);
3842     case LibFunc_strrchr:
3843       return optimizeStrRChr(CI, Builder);
3844     case LibFunc_strcmp:
3845       return optimizeStrCmp(CI, Builder);
3846     case LibFunc_strncmp:
3847       return optimizeStrNCmp(CI, Builder);
3848     case LibFunc_strcpy:
3849       return optimizeStrCpy(CI, Builder);
3850     case LibFunc_stpcpy:
3851       return optimizeStpCpy(CI, Builder);
3852     case LibFunc_strlcpy:
3853       return optimizeStrLCpy(CI, Builder);
3854     case LibFunc_stpncpy:
3855       return optimizeStringNCpy(CI, /*RetEnd=*/true, Builder);
3856     case LibFunc_strncpy:
3857       return optimizeStringNCpy(CI, /*RetEnd=*/false, Builder);
3858     case LibFunc_strlen:
3859       return optimizeStrLen(CI, Builder);
3860     case LibFunc_strnlen:
3861       return optimizeStrNLen(CI, Builder);
3862     case LibFunc_strpbrk:
3863       return optimizeStrPBrk(CI, Builder);
3864     case LibFunc_strndup:
3865       return optimizeStrNDup(CI, Builder);
3866     case LibFunc_strtol:
3867     case LibFunc_strtod:
3868     case LibFunc_strtof:
3869     case LibFunc_strtoul:
3870     case LibFunc_strtoll:
3871     case LibFunc_strtold:
3872     case LibFunc_strtoull:
3873       return optimizeStrTo(CI, Builder);
3874     case LibFunc_strspn:
3875       return optimizeStrSpn(CI, Builder);
3876     case LibFunc_strcspn:
3877       return optimizeStrCSpn(CI, Builder);
3878     case LibFunc_strstr:
3879       return optimizeStrStr(CI, Builder);
3880     case LibFunc_memchr:
3881       return optimizeMemChr(CI, Builder);
3882     case LibFunc_memrchr:
3883       return optimizeMemRChr(CI, Builder);
3884     case LibFunc_bcmp:
3885       return optimizeBCmp(CI, Builder);
3886     case LibFunc_memcmp:
3887       return optimizeMemCmp(CI, Builder);
3888     case LibFunc_memcpy:
3889       return optimizeMemCpy(CI, Builder);
3890     case LibFunc_memccpy:
3891       return optimizeMemCCpy(CI, Builder);
3892     case LibFunc_mempcpy:
3893       return optimizeMemPCpy(CI, Builder);
3894     case LibFunc_memmove:
3895       return optimizeMemMove(CI, Builder);
3896     case LibFunc_memset:
3897       return optimizeMemSet(CI, Builder);
3898     case LibFunc_realloc:
3899       return optimizeRealloc(CI, Builder);
3900     case LibFunc_wcslen:
3901       return optimizeWcslen(CI, Builder);
3902     case LibFunc_bcopy:
3903       return optimizeBCopy(CI, Builder);
3904     case LibFunc_Znwm:
3905     case LibFunc_ZnwmRKSt9nothrow_t:
3906     case LibFunc_ZnwmSt11align_val_t:
3907     case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t:
3908     case LibFunc_Znam:
3909     case LibFunc_ZnamRKSt9nothrow_t:
3910     case LibFunc_ZnamSt11align_val_t:
3911     case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t:
3912     case LibFunc_Znwm12__hot_cold_t:
3913     case LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t:
3914     case LibFunc_ZnwmSt11align_val_t12__hot_cold_t:
3915     case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t:
3916     case LibFunc_Znam12__hot_cold_t:
3917     case LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t:
3918     case LibFunc_ZnamSt11align_val_t12__hot_cold_t:
3919     case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t:
3920     case LibFunc_size_returning_new:
3921     case LibFunc_size_returning_new_hot_cold:
3922     case LibFunc_size_returning_new_aligned:
3923     case LibFunc_size_returning_new_aligned_hot_cold:
3924       return optimizeNew(CI, Builder, Func);
3925     default:
3926       break;
3927     }
3928   }
3929   return nullptr;
3930 }
3931 
3932 /// Constant folding nan/nanf/nanl.
3933 static Value *optimizeNaN(CallInst *CI) {
3934   StringRef CharSeq;
3935   if (!getConstantStringInfo(CI->getArgOperand(0), CharSeq))
3936     return nullptr;
3937 
3938   APInt Fill;
3939   // Treat empty strings as if they were zero.
3940   if (CharSeq.empty())
3941     Fill = APInt(32, 0);
3942   else if (CharSeq.getAsInteger(0, Fill))
3943     return nullptr;
3944 
3945   return ConstantFP::getQNaN(CI->getType(), /*Negative=*/false, &Fill);
3946 }
3947 
3948 Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
3949                                                        LibFunc Func,
3950                                                        IRBuilderBase &Builder) {
3951   const Module *M = CI->getModule();
3952 
3953   // Don't optimize calls that require strict floating point semantics.
3954   if (CI->isStrictFP())
3955     return nullptr;
3956 
3957   if (Value *V = optimizeSymmetric(CI, Func, Builder))
3958     return V;
3959 
3960   switch (Func) {
3961   case LibFunc_sinpif:
3962   case LibFunc_sinpi:
3963     return optimizeSinCosPi(CI, /*IsSin*/true, Builder);
3964   case LibFunc_cospif:
3965   case LibFunc_cospi:
3966     return optimizeSinCosPi(CI, /*IsSin*/false, Builder);
3967   case LibFunc_powf:
3968   case LibFunc_pow:
3969   case LibFunc_powl:
3970     return optimizePow(CI, Builder);
3971   case LibFunc_exp2l:
3972   case LibFunc_exp2:
3973   case LibFunc_exp2f:
3974     return optimizeExp2(CI, Builder);
3975   case LibFunc_fabsf:
3976   case LibFunc_fabs:
3977   case LibFunc_fabsl:
3978     return replaceUnaryCall(CI, Builder, Intrinsic::fabs);
3979   case LibFunc_sqrtf:
3980   case LibFunc_sqrt:
3981   case LibFunc_sqrtl:
3982     return optimizeSqrt(CI, Builder);
3983   case LibFunc_fmod:
3984   case LibFunc_fmodf:
3985   case LibFunc_fmodl:
3986     return optimizeFMod(CI, Builder);
3987   case LibFunc_logf:
3988   case LibFunc_log:
3989   case LibFunc_logl:
3990   case LibFunc_log10f:
3991   case LibFunc_log10:
3992   case LibFunc_log10l:
3993   case LibFunc_log1pf:
3994   case LibFunc_log1p:
3995   case LibFunc_log1pl:
3996   case LibFunc_log2f:
3997   case LibFunc_log2:
3998   case LibFunc_log2l:
3999   case LibFunc_logbf:
4000   case LibFunc_logb:
4001   case LibFunc_logbl:
4002     return optimizeLog(CI, Builder);
4003   case LibFunc_tan:
4004   case LibFunc_tanf:
4005   case LibFunc_tanl:
4006   case LibFunc_sinh:
4007   case LibFunc_sinhf:
4008   case LibFunc_sinhl:
4009   case LibFunc_asinh:
4010   case LibFunc_asinhf:
4011   case LibFunc_asinhl:
4012   case LibFunc_cosh:
4013   case LibFunc_coshf:
4014   case LibFunc_coshl:
4015   case LibFunc_atanh:
4016   case LibFunc_atanhf:
4017   case LibFunc_atanhl:
4018     return optimizeTrigInversionPairs(CI, Builder);
4019   case LibFunc_ceil:
4020     return replaceUnaryCall(CI, Builder, Intrinsic::ceil);
4021   case LibFunc_floor:
4022     return replaceUnaryCall(CI, Builder, Intrinsic::floor);
4023   case LibFunc_round:
4024     return replaceUnaryCall(CI, Builder, Intrinsic::round);
4025   case LibFunc_roundeven:
4026     return replaceUnaryCall(CI, Builder, Intrinsic::roundeven);
4027   case LibFunc_nearbyint:
4028     return replaceUnaryCall(CI, Builder, Intrinsic::nearbyint);
4029   case LibFunc_rint:
4030     return replaceUnaryCall(CI, Builder, Intrinsic::rint);
4031   case LibFunc_trunc:
4032     return replaceUnaryCall(CI, Builder, Intrinsic::trunc);
4033   case LibFunc_acos:
4034   case LibFunc_acosh:
4035   case LibFunc_asin:
4036   case LibFunc_atan:
4037   case LibFunc_cbrt:
4038   case LibFunc_exp:
4039   case LibFunc_exp10:
4040   case LibFunc_expm1:
4041   case LibFunc_cos:
4042   case LibFunc_sin:
4043   case LibFunc_tanh:
4044     if (UnsafeFPShrink && hasFloatVersion(M, CI->getCalledFunction()->getName()))
4045       return optimizeUnaryDoubleFP(CI, Builder, TLI, true);
4046     return nullptr;
4047   case LibFunc_copysign:
4048     if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
4049       return optimizeBinaryDoubleFP(CI, Builder, TLI);
4050     return nullptr;
4051   case LibFunc_fdim:
4052   case LibFunc_fdimf:
4053   case LibFunc_fdiml:
4054     return optimizeFdim(CI, Builder);
4055   case LibFunc_fminf:
4056   case LibFunc_fmin:
4057   case LibFunc_fminl:
4058   case LibFunc_fmaxf:
4059   case LibFunc_fmax:
4060   case LibFunc_fmaxl:
4061     return optimizeFMinFMax(CI, Builder);
4062   case LibFunc_cabs:
4063   case LibFunc_cabsf:
4064   case LibFunc_cabsl:
4065     return optimizeCAbs(CI, Builder);
4066   case LibFunc_remquo:
4067   case LibFunc_remquof:
4068   case LibFunc_remquol:
4069     return optimizeRemquo(CI, Builder);
4070   case LibFunc_nan:
4071   case LibFunc_nanf:
4072   case LibFunc_nanl:
4073     return optimizeNaN(CI);
4074   default:
4075     return nullptr;
4076   }
4077 }
4078 
4079 Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) {
4080   Module *M = CI->getModule();
4081   assert(!CI->isMustTailCall() && "These transforms aren't musttail safe.");
4082 
4083   // TODO: Split out the code below that operates on FP calls so that
4084   //       we can all non-FP calls with the StrictFP attribute to be
4085   //       optimized.
4086   if (CI->isNoBuiltin())
4087     return nullptr;
4088 
4089   LibFunc Func;
4090   Function *Callee = CI->getCalledFunction();
4091   bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
4092 
4093   SmallVector<OperandBundleDef, 2> OpBundles;
4094   CI->getOperandBundlesAsDefs(OpBundles);
4095 
4096   IRBuilderBase::OperandBundlesGuard Guard(Builder);
4097   Builder.setDefaultOperandBundles(OpBundles);
4098 
4099   // Command-line parameter overrides instruction attribute.
4100   // This can't be moved to optimizeFloatingPointLibCall() because it may be
4101   // used by the intrinsic optimizations.
4102   if (EnableUnsafeFPShrink.getNumOccurrences() > 0)
4103     UnsafeFPShrink = EnableUnsafeFPShrink;
4104   else if (isa<FPMathOperator>(CI) && CI->isFast())
4105     UnsafeFPShrink = true;
4106 
4107   // First, check for intrinsics.
4108   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
4109     if (!IsCallingConvC)
4110       return nullptr;
4111     // The FP intrinsics have corresponding constrained versions so we don't
4112     // need to check for the StrictFP attribute here.
4113     switch (II->getIntrinsicID()) {
4114     case Intrinsic::pow:
4115       return optimizePow(CI, Builder);
4116     case Intrinsic::exp2:
4117       return optimizeExp2(CI, Builder);
4118     case Intrinsic::log:
4119     case Intrinsic::log2:
4120     case Intrinsic::log10:
4121       return optimizeLog(CI, Builder);
4122     case Intrinsic::sqrt:
4123       return optimizeSqrt(CI, Builder);
4124     case Intrinsic::memset:
4125       return optimizeMemSet(CI, Builder);
4126     case Intrinsic::memcpy:
4127       return optimizeMemCpy(CI, Builder);
4128     case Intrinsic::memmove:
4129       return optimizeMemMove(CI, Builder);
4130     default:
4131       return nullptr;
4132     }
4133   }
4134 
4135   // Also try to simplify calls to fortified library functions.
4136   if (Value *SimplifiedFortifiedCI =
4137           FortifiedSimplifier.optimizeCall(CI, Builder))
4138     return SimplifiedFortifiedCI;
4139 
4140   // Then check for known library functions.
4141   if (TLI->getLibFunc(*Callee, Func) && isLibFuncEmittable(M, TLI, Func)) {
4142     // We never change the calling convention.
4143     if (!ignoreCallingConv(Func) && !IsCallingConvC)
4144       return nullptr;
4145     if (Value *V = optimizeStringMemoryLibCall(CI, Builder))
4146       return V;
4147     if (Value *V = optimizeFloatingPointLibCall(CI, Func, Builder))
4148       return V;
4149     switch (Func) {
4150     case LibFunc_ffs:
4151     case LibFunc_ffsl:
4152     case LibFunc_ffsll:
4153       return optimizeFFS(CI, Builder);
4154     case LibFunc_fls:
4155     case LibFunc_flsl:
4156     case LibFunc_flsll:
4157       return optimizeFls(CI, Builder);
4158     case LibFunc_abs:
4159     case LibFunc_labs:
4160     case LibFunc_llabs:
4161       return optimizeAbs(CI, Builder);
4162     case LibFunc_isdigit:
4163       return optimizeIsDigit(CI, Builder);
4164     case LibFunc_isascii:
4165       return optimizeIsAscii(CI, Builder);
4166     case LibFunc_toascii:
4167       return optimizeToAscii(CI, Builder);
4168     case LibFunc_atoi:
4169     case LibFunc_atol:
4170     case LibFunc_atoll:
4171       return optimizeAtoi(CI, Builder);
4172     case LibFunc_strtol:
4173     case LibFunc_strtoll:
4174       return optimizeStrToInt(CI, Builder, /*AsSigned=*/true);
4175     case LibFunc_strtoul:
4176     case LibFunc_strtoull:
4177       return optimizeStrToInt(CI, Builder, /*AsSigned=*/false);
4178     case LibFunc_printf:
4179       return optimizePrintF(CI, Builder);
4180     case LibFunc_sprintf:
4181       return optimizeSPrintF(CI, Builder);
4182     case LibFunc_snprintf:
4183       return optimizeSnPrintF(CI, Builder);
4184     case LibFunc_fprintf:
4185       return optimizeFPrintF(CI, Builder);
4186     case LibFunc_fwrite:
4187       return optimizeFWrite(CI, Builder);
4188     case LibFunc_fputs:
4189       return optimizeFPuts(CI, Builder);
4190     case LibFunc_puts:
4191       return optimizePuts(CI, Builder);
4192     case LibFunc_perror:
4193       return optimizeErrorReporting(CI, Builder);
4194     case LibFunc_vfprintf:
4195     case LibFunc_fiprintf:
4196       return optimizeErrorReporting(CI, Builder, 0);
4197     case LibFunc_exit:
4198     case LibFunc_Exit:
4199       return optimizeExit(CI);
4200     default:
4201       return nullptr;
4202     }
4203   }
4204   return nullptr;
4205 }
4206 
4207 LibCallSimplifier::LibCallSimplifier(
4208     const DataLayout &DL, const TargetLibraryInfo *TLI, DominatorTree *DT,
4209     DomConditionCache *DC, AssumptionCache *AC, OptimizationRemarkEmitter &ORE,
4210     BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
4211     function_ref<void(Instruction *, Value *)> Replacer,
4212     function_ref<void(Instruction *)> Eraser)
4213     : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), DT(DT), DC(DC), AC(AC),
4214       ORE(ORE), BFI(BFI), PSI(PSI), Replacer(Replacer), Eraser(Eraser) {}
4215 
4216 void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) {
4217   // Indirect through the replacer used in this instance.
4218   Replacer(I, With);
4219 }
4220 
4221 void LibCallSimplifier::eraseFromParent(Instruction *I) {
4222   Eraser(I);
4223 }
4224 
4225 // TODO:
4226 //   Additional cases that we need to add to this file:
4227 //
4228 // cbrt:
4229 //   * cbrt(expN(X))  -> expN(x/3)
4230 //   * cbrt(sqrt(x))  -> pow(x,1/6)
4231 //   * cbrt(cbrt(x))  -> pow(x,1/9)
4232 //
4233 // exp, expf, expl:
4234 //   * exp(log(x))  -> x
4235 //
4236 // log, logf, logl:
4237 //   * log(exp(x))   -> x
4238 //   * log(exp(y))   -> y*log(e)
4239 //   * log(exp10(y)) -> y*log(10)
4240 //   * log(sqrt(x))  -> 0.5*log(x)
4241 //
4242 // pow, powf, powl:
4243 //   * pow(sqrt(x),y) -> pow(x,y*0.5)
4244 //   * pow(pow(x,y),z)-> pow(x,y*z)
4245 //
4246 // signbit:
4247 //   * signbit(cnst) -> cnst'
4248 //   * signbit(nncst) -> 0 (if pstv is a non-negative constant)
4249 //
4250 // sqrt, sqrtf, sqrtl:
4251 //   * sqrt(expN(x))  -> expN(x*0.5)
4252 //   * sqrt(Nroot(x)) -> pow(x,1/(2*N))
4253 //   * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
4254 //
4255 
4256 //===----------------------------------------------------------------------===//
4257 // Fortified Library Call Optimizations
4258 //===----------------------------------------------------------------------===//
4259 
4260 bool FortifiedLibCallSimplifier::isFortifiedCallFoldable(
4261     CallInst *CI, unsigned ObjSizeOp, std::optional<unsigned> SizeOp,
4262     std::optional<unsigned> StrOp, std::optional<unsigned> FlagOp) {
4263   // If this function takes a flag argument, the implementation may use it to
4264   // perform extra checks. Don't fold into the non-checking variant.
4265   if (FlagOp) {
4266     ConstantInt *Flag = dyn_cast<ConstantInt>(CI->getArgOperand(*FlagOp));
4267     if (!Flag || !Flag->isZero())
4268       return false;
4269   }
4270 
4271   if (SizeOp && CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(*SizeOp))
4272     return true;
4273 
4274   if (ConstantInt *ObjSizeCI =
4275           dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) {
4276     if (ObjSizeCI->isMinusOne())
4277       return true;
4278     // If the object size wasn't -1 (unknown), bail out if we were asked to.
4279     if (OnlyLowerUnknownSize)
4280       return false;
4281     if (StrOp) {
4282       uint64_t Len = GetStringLength(CI->getArgOperand(*StrOp));
4283       // If the length is 0 we don't know how long it is and so we can't
4284       // remove the check.
4285       if (Len)
4286         annotateDereferenceableBytes(CI, *StrOp, Len);
4287       else
4288         return false;
4289       return ObjSizeCI->getZExtValue() >= Len;
4290     }
4291 
4292     if (SizeOp) {
4293       if (ConstantInt *SizeCI =
4294               dyn_cast<ConstantInt>(CI->getArgOperand(*SizeOp)))
4295         return ObjSizeCI->getZExtValue() >= SizeCI->getZExtValue();
4296     }
4297   }
4298   return false;
4299 }
4300 
4301 Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI,
4302                                                      IRBuilderBase &B) {
4303   if (isFortifiedCallFoldable(CI, 3, 2)) {
4304     CallInst *NewCI =
4305         B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
4306                        Align(1), CI->getArgOperand(2));
4307     mergeAttributesAndFlags(NewCI, *CI);
4308     return CI->getArgOperand(0);
4309   }
4310   return nullptr;
4311 }
4312 
4313 Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI,
4314                                                       IRBuilderBase &B) {
4315   if (isFortifiedCallFoldable(CI, 3, 2)) {
4316     CallInst *NewCI =
4317         B.CreateMemMove(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
4318                         Align(1), CI->getArgOperand(2));
4319     mergeAttributesAndFlags(NewCI, *CI);
4320     return CI->getArgOperand(0);
4321   }
4322   return nullptr;
4323 }
4324 
4325 Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI,
4326                                                      IRBuilderBase &B) {
4327   if (isFortifiedCallFoldable(CI, 3, 2)) {
4328     Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
4329     CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val,
4330                                      CI->getArgOperand(2), Align(1));
4331     mergeAttributesAndFlags(NewCI, *CI);
4332     return CI->getArgOperand(0);
4333   }
4334   return nullptr;
4335 }
4336 
4337 Value *FortifiedLibCallSimplifier::optimizeMemPCpyChk(CallInst *CI,
4338                                                       IRBuilderBase &B) {
4339   const DataLayout &DL = CI->getDataLayout();
4340   if (isFortifiedCallFoldable(CI, 3, 2))
4341     if (Value *Call = emitMemPCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4342                                   CI->getArgOperand(2), B, DL, TLI)) {
4343       return mergeAttributesAndFlags(cast<CallInst>(Call), *CI);
4344     }
4345   return nullptr;
4346 }
4347 
4348 Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
4349                                                       IRBuilderBase &B,
4350                                                       LibFunc Func) {
4351   const DataLayout &DL = CI->getDataLayout();
4352   Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
4353         *ObjSize = CI->getArgOperand(2);
4354 
4355   // __stpcpy_chk(x,x,...)  -> x+strlen(x)
4356   if (Func == LibFunc_stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) {
4357     Value *StrLen = emitStrLen(Src, B, DL, TLI);
4358     return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
4359   }
4360 
4361   // If a) we don't have any length information, or b) we know this will
4362   // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
4363   // st[rp]cpy_chk call which may fail at runtime if the size is too long.
4364   // TODO: It might be nice to get a maximum length out of the possible
4365   // string lengths for varying.
4366   if (isFortifiedCallFoldable(CI, 2, std::nullopt, 1)) {
4367     if (Func == LibFunc_strcpy_chk)
4368       return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
4369     else
4370       return copyFlags(*CI, emitStpCpy(Dst, Src, B, TLI));
4371   }
4372 
4373   if (OnlyLowerUnknownSize)
4374     return nullptr;
4375 
4376   // Maybe we can stil fold __st[rp]cpy_chk to __memcpy_chk.
4377   uint64_t Len = GetStringLength(Src);
4378   if (Len)
4379     annotateDereferenceableBytes(CI, 1, Len);
4380   else
4381     return nullptr;
4382 
4383   unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
4384   Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
4385   Value *LenV = ConstantInt::get(SizeTTy, Len);
4386   Value *Ret = emitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI);
4387   // If the function was an __stpcpy_chk, and we were able to fold it into
4388   // a __memcpy_chk, we still need to return the correct end pointer.
4389   if (Ret && Func == LibFunc_stpcpy_chk)
4390     return B.CreateInBoundsGEP(B.getInt8Ty(), Dst,
4391                                ConstantInt::get(SizeTTy, Len - 1));
4392   return copyFlags(*CI, cast<CallInst>(Ret));
4393 }
4394 
4395 Value *FortifiedLibCallSimplifier::optimizeStrLenChk(CallInst *CI,
4396                                                      IRBuilderBase &B) {
4397   if (isFortifiedCallFoldable(CI, 1, std::nullopt, 0))
4398     return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B,
4399                                      CI->getDataLayout(), TLI));
4400   return nullptr;
4401 }
4402 
4403 Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
4404                                                        IRBuilderBase &B,
4405                                                        LibFunc Func) {
4406   if (isFortifiedCallFoldable(CI, 3, 2)) {
4407     if (Func == LibFunc_strncpy_chk)
4408       return copyFlags(*CI,
4409                        emitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4410                                    CI->getArgOperand(2), B, TLI));
4411     else
4412       return copyFlags(*CI,
4413                        emitStpNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4414                                    CI->getArgOperand(2), B, TLI));
4415   }
4416 
4417   return nullptr;
4418 }
4419 
4420 Value *FortifiedLibCallSimplifier::optimizeMemCCpyChk(CallInst *CI,
4421                                                       IRBuilderBase &B) {
4422   if (isFortifiedCallFoldable(CI, 4, 3))
4423     return copyFlags(
4424         *CI, emitMemCCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4425                          CI->getArgOperand(2), CI->getArgOperand(3), B, TLI));
4426 
4427   return nullptr;
4428 }
4429 
4430 Value *FortifiedLibCallSimplifier::optimizeSNPrintfChk(CallInst *CI,
4431                                                        IRBuilderBase &B) {
4432   if (isFortifiedCallFoldable(CI, 3, 1, std::nullopt, 2)) {
4433     SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 5));
4434     return copyFlags(*CI,
4435                      emitSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1),
4436                                   CI->getArgOperand(4), VariadicArgs, B, TLI));
4437   }
4438 
4439   return nullptr;
4440 }
4441 
4442 Value *FortifiedLibCallSimplifier::optimizeSPrintfChk(CallInst *CI,
4443                                                       IRBuilderBase &B) {
4444   if (isFortifiedCallFoldable(CI, 2, std::nullopt, std::nullopt, 1)) {
4445     SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 4));
4446     return copyFlags(*CI,
4447                      emitSPrintf(CI->getArgOperand(0), CI->getArgOperand(3),
4448                                  VariadicArgs, B, TLI));
4449   }
4450 
4451   return nullptr;
4452 }
4453 
4454 Value *FortifiedLibCallSimplifier::optimizeStrCatChk(CallInst *CI,
4455                                                      IRBuilderBase &B) {
4456   if (isFortifiedCallFoldable(CI, 2))
4457     return copyFlags(
4458         *CI, emitStrCat(CI->getArgOperand(0), CI->getArgOperand(1), B, TLI));
4459 
4460   return nullptr;
4461 }
4462 
4463 Value *FortifiedLibCallSimplifier::optimizeStrLCat(CallInst *CI,
4464                                                    IRBuilderBase &B) {
4465   if (isFortifiedCallFoldable(CI, 3))
4466     return copyFlags(*CI,
4467                      emitStrLCat(CI->getArgOperand(0), CI->getArgOperand(1),
4468                                  CI->getArgOperand(2), B, TLI));
4469 
4470   return nullptr;
4471 }
4472 
4473 Value *FortifiedLibCallSimplifier::optimizeStrNCatChk(CallInst *CI,
4474                                                       IRBuilderBase &B) {
4475   if (isFortifiedCallFoldable(CI, 3))
4476     return copyFlags(*CI,
4477                      emitStrNCat(CI->getArgOperand(0), CI->getArgOperand(1),
4478                                  CI->getArgOperand(2), B, TLI));
4479 
4480   return nullptr;
4481 }
4482 
4483 Value *FortifiedLibCallSimplifier::optimizeStrLCpyChk(CallInst *CI,
4484                                                       IRBuilderBase &B) {
4485   if (isFortifiedCallFoldable(CI, 3))
4486     return copyFlags(*CI,
4487                      emitStrLCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4488                                  CI->getArgOperand(2), B, TLI));
4489 
4490   return nullptr;
4491 }
4492 
4493 Value *FortifiedLibCallSimplifier::optimizeVSNPrintfChk(CallInst *CI,
4494                                                         IRBuilderBase &B) {
4495   if (isFortifiedCallFoldable(CI, 3, 1, std::nullopt, 2))
4496     return copyFlags(
4497         *CI, emitVSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1),
4498                            CI->getArgOperand(4), CI->getArgOperand(5), B, TLI));
4499 
4500   return nullptr;
4501 }
4502 
4503 Value *FortifiedLibCallSimplifier::optimizeVSPrintfChk(CallInst *CI,
4504                                                        IRBuilderBase &B) {
4505   if (isFortifiedCallFoldable(CI, 2, std::nullopt, std::nullopt, 1))
4506     return copyFlags(*CI,
4507                      emitVSPrintf(CI->getArgOperand(0), CI->getArgOperand(3),
4508                                   CI->getArgOperand(4), B, TLI));
4509 
4510   return nullptr;
4511 }
4512 
4513 Value *FortifiedLibCallSimplifier::optimizeCall(CallInst *CI,
4514                                                 IRBuilderBase &Builder) {
4515   // FIXME: We shouldn't be changing "nobuiltin" or TLI unavailable calls here.
4516   // Some clang users checked for _chk libcall availability using:
4517   //   __has_builtin(__builtin___memcpy_chk)
4518   // When compiling with -fno-builtin, this is always true.
4519   // When passing -ffreestanding/-mkernel, which both imply -fno-builtin, we
4520   // end up with fortified libcalls, which isn't acceptable in a freestanding
4521   // environment which only provides their non-fortified counterparts.
4522   //
4523   // Until we change clang and/or teach external users to check for availability
4524   // differently, disregard the "nobuiltin" attribute and TLI::has.
4525   //
4526   // PR23093.
4527 
4528   LibFunc Func;
4529   Function *Callee = CI->getCalledFunction();
4530   bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
4531 
4532   SmallVector<OperandBundleDef, 2> OpBundles;
4533   CI->getOperandBundlesAsDefs(OpBundles);
4534 
4535   IRBuilderBase::OperandBundlesGuard Guard(Builder);
4536   Builder.setDefaultOperandBundles(OpBundles);
4537 
4538   // First, check that this is a known library functions and that the prototype
4539   // is correct.
4540   if (!TLI->getLibFunc(*Callee, Func))
4541     return nullptr;
4542 
4543   // We never change the calling convention.
4544   if (!ignoreCallingConv(Func) && !IsCallingConvC)
4545     return nullptr;
4546 
4547   switch (Func) {
4548   case LibFunc_memcpy_chk:
4549     return optimizeMemCpyChk(CI, Builder);
4550   case LibFunc_mempcpy_chk:
4551     return optimizeMemPCpyChk(CI, Builder);
4552   case LibFunc_memmove_chk:
4553     return optimizeMemMoveChk(CI, Builder);
4554   case LibFunc_memset_chk:
4555     return optimizeMemSetChk(CI, Builder);
4556   case LibFunc_stpcpy_chk:
4557   case LibFunc_strcpy_chk:
4558     return optimizeStrpCpyChk(CI, Builder, Func);
4559   case LibFunc_strlen_chk:
4560     return optimizeStrLenChk(CI, Builder);
4561   case LibFunc_stpncpy_chk:
4562   case LibFunc_strncpy_chk:
4563     return optimizeStrpNCpyChk(CI, Builder, Func);
4564   case LibFunc_memccpy_chk:
4565     return optimizeMemCCpyChk(CI, Builder);
4566   case LibFunc_snprintf_chk:
4567     return optimizeSNPrintfChk(CI, Builder);
4568   case LibFunc_sprintf_chk:
4569     return optimizeSPrintfChk(CI, Builder);
4570   case LibFunc_strcat_chk:
4571     return optimizeStrCatChk(CI, Builder);
4572   case LibFunc_strlcat_chk:
4573     return optimizeStrLCat(CI, Builder);
4574   case LibFunc_strncat_chk:
4575     return optimizeStrNCatChk(CI, Builder);
4576   case LibFunc_strlcpy_chk:
4577     return optimizeStrLCpyChk(CI, Builder);
4578   case LibFunc_vsnprintf_chk:
4579     return optimizeVSNPrintfChk(CI, Builder);
4580   case LibFunc_vsprintf_chk:
4581     return optimizeVSPrintfChk(CI, Builder);
4582   default:
4583     break;
4584   }
4585   return nullptr;
4586 }
4587 
4588 FortifiedLibCallSimplifier::FortifiedLibCallSimplifier(
4589     const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
4590     : TLI(TLI), OnlyLowerUnknownSize(OnlyLowerUnknownSize) {}
4591