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