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