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