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