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