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