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