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