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