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