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