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