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