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