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