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())) {
2392 Constant *One = ConstantFP::get(Ty, 1.0);
2393
2394 // TODO: Emitting the intrinsic should not depend on whether the libcall
2395 // is available.
2396 if (CI->doesNotAccessMemory()) {
2397 return copyFlags(*CI, B.CreateIntrinsic(Intrinsic::ldexp,
2398 {Ty, Exp->getType()},
2399 {One, Exp}, CI));
2400 }
2401
2403 B.setFastMathFlags(CI->getFastMathFlags());
2404 return copyFlags(*CI, emitBinaryFloatFnCall(
2405 One, Exp, TLI, LibFunc_ldexp, LibFunc_ldexpf,
2406 LibFunc_ldexpl, B, AttributeList()));
2407 }
2408 }
2409
2410 return Ret;
2411}
2412
2413Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) {
2414 Module *M = CI->getModule();
2415
2416 // If we can shrink the call to a float function rather than a double
2417 // function, do that first.
2419 StringRef Name = Callee->getName();
2420 if ((Name == "fmin" || Name == "fmax") && hasFloatVersion(M, Name))
2421 if (Value *Ret = optimizeBinaryDoubleFP(CI, B, TLI))
2422 return Ret;
2423
2424 // The LLVM intrinsics minnum/maxnum correspond to fmin/fmax. Canonicalize to
2425 // the intrinsics for improved optimization (for example, vectorization).
2426 // No-signed-zeros is implied by the definitions of fmax/fmin themselves.
2427 // From the C standard draft WG14/N1256:
2428 // "Ideally, fmax would be sensitive to the sign of zero, for example
2429 // fmax(-0.0, +0.0) would return +0; however, implementation in software
2430 // might be impractical."
2432 FastMathFlags FMF = CI->getFastMathFlags();
2433 FMF.setNoSignedZeros();
2434 B.setFastMathFlags(FMF);
2435
2436 Intrinsic::ID IID = Callee->getName().starts_with("fmin") ? Intrinsic::minnum
2437 : Intrinsic::maxnum;
2439 return copyFlags(
2440 *CI, B.CreateCall(F, {CI->getArgOperand(0), CI->getArgOperand(1)}));
2441}
2442
2443Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
2444 Function *LogFn = Log->getCalledFunction();
2445 StringRef LogNm = LogFn->getName();
2446 Intrinsic::ID LogID = LogFn->getIntrinsicID();
2447 Module *Mod = Log->getModule();
2448 Type *Ty = Log->getType();
2449 Value *Ret = nullptr;
2450
2451 if (UnsafeFPShrink && hasFloatVersion(Mod, LogNm))
2452 Ret = optimizeUnaryDoubleFP(Log, B, TLI, true);
2453
2454 // The earlier call must also be 'fast' in order to do these transforms.
2455 CallInst *Arg = dyn_cast<CallInst>(Log->getArgOperand(0));
2456 if (!Log->isFast() || !Arg || !Arg->isFast() || !Arg->hasOneUse())
2457 return Ret;
2458
2459 LibFunc LogLb, ExpLb, Exp2Lb, Exp10Lb, PowLb;
2460
2461 // This is only applicable to log(), log2(), log10().
2462 if (TLI->getLibFunc(LogNm, LogLb))
2463 switch (LogLb) {
2464 case LibFunc_logf:
2465 LogID = Intrinsic::log;
2466 ExpLb = LibFunc_expf;
2467 Exp2Lb = LibFunc_exp2f;
2468 Exp10Lb = LibFunc_exp10f;
2469 PowLb = LibFunc_powf;
2470 break;
2471 case LibFunc_log:
2472 LogID = Intrinsic::log;
2473 ExpLb = LibFunc_exp;
2474 Exp2Lb = LibFunc_exp2;
2475 Exp10Lb = LibFunc_exp10;
2476 PowLb = LibFunc_pow;
2477 break;
2478 case LibFunc_logl:
2479 LogID = Intrinsic::log;
2480 ExpLb = LibFunc_expl;
2481 Exp2Lb = LibFunc_exp2l;
2482 Exp10Lb = LibFunc_exp10l;
2483 PowLb = LibFunc_powl;
2484 break;
2485 case LibFunc_log2f:
2486 LogID = Intrinsic::log2;
2487 ExpLb = LibFunc_expf;
2488 Exp2Lb = LibFunc_exp2f;
2489 Exp10Lb = LibFunc_exp10f;
2490 PowLb = LibFunc_powf;
2491 break;
2492 case LibFunc_log2:
2493 LogID = Intrinsic::log2;
2494 ExpLb = LibFunc_exp;
2495 Exp2Lb = LibFunc_exp2;
2496 Exp10Lb = LibFunc_exp10;
2497 PowLb = LibFunc_pow;
2498 break;
2499 case LibFunc_log2l:
2500 LogID = Intrinsic::log2;
2501 ExpLb = LibFunc_expl;
2502 Exp2Lb = LibFunc_exp2l;
2503 Exp10Lb = LibFunc_exp10l;
2504 PowLb = LibFunc_powl;
2505 break;
2506 case LibFunc_log10f:
2507 LogID = Intrinsic::log10;
2508 ExpLb = LibFunc_expf;
2509 Exp2Lb = LibFunc_exp2f;
2510 Exp10Lb = LibFunc_exp10f;
2511 PowLb = LibFunc_powf;
2512 break;
2513 case LibFunc_log10:
2514 LogID = Intrinsic::log10;
2515 ExpLb = LibFunc_exp;
2516 Exp2Lb = LibFunc_exp2;
2517 Exp10Lb = LibFunc_exp10;
2518 PowLb = LibFunc_pow;
2519 break;
2520 case LibFunc_log10l:
2521 LogID = Intrinsic::log10;
2522 ExpLb = LibFunc_expl;
2523 Exp2Lb = LibFunc_exp2l;
2524 Exp10Lb = LibFunc_exp10l;
2525 PowLb = LibFunc_powl;
2526 break;
2527 default:
2528 return Ret;
2529 }
2530 else if (LogID == Intrinsic::log || LogID == Intrinsic::log2 ||
2531 LogID == Intrinsic::log10) {
2532 if (Ty->getScalarType()->isFloatTy()) {
2533 ExpLb = LibFunc_expf;
2534 Exp2Lb = LibFunc_exp2f;
2535 Exp10Lb = LibFunc_exp10f;
2536 PowLb = LibFunc_powf;
2537 } else if (Ty->getScalarType()->isDoubleTy()) {
2538 ExpLb = LibFunc_exp;
2539 Exp2Lb = LibFunc_exp2;
2540 Exp10Lb = LibFunc_exp10;
2541 PowLb = LibFunc_pow;
2542 } else
2543 return Ret;
2544 } else
2545 return Ret;
2546
2548 B.setFastMathFlags(FastMathFlags::getFast());
2549
2550 Intrinsic::ID ArgID = Arg->getIntrinsicID();
2551 LibFunc ArgLb = NotLibFunc;
2552 TLI->getLibFunc(*Arg, ArgLb);
2553
2554 // log(pow(x,y)) -> y*log(x)
2555 AttributeList NoAttrs;
2556 if (ArgLb == PowLb || ArgID == Intrinsic::pow || ArgID == Intrinsic::powi) {
2557 Value *LogX =
2558 Log->doesNotAccessMemory()
2559 ? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty),
2560 Arg->getOperand(0), "log")
2561 : emitUnaryFloatFnCall(Arg->getOperand(0), TLI, LogNm, B, NoAttrs);
2562 Value *Y = Arg->getArgOperand(1);
2563 // Cast exponent to FP if integer.
2564 if (ArgID == Intrinsic::powi)
2565 Y = B.CreateSIToFP(Y, Ty, "cast");
2566 Value *MulY = B.CreateFMul(Y, LogX, "mul");
2567 // Since pow() may have side effects, e.g. errno,
2568 // dead code elimination may not be trusted to remove it.
2569 substituteInParent(Arg, MulY);
2570 return MulY;
2571 }
2572
2573 // log(exp{,2,10}(y)) -> y*log({e,2,10})
2574 // TODO: There is no exp10() intrinsic yet.
2575 if (ArgLb == ExpLb || ArgLb == Exp2Lb || ArgLb == Exp10Lb ||
2576 ArgID == Intrinsic::exp || ArgID == Intrinsic::exp2) {
2577 Constant *Eul;
2578 if (ArgLb == ExpLb || ArgID == Intrinsic::exp)
2579 // FIXME: Add more precise value of e for long double.
2580 Eul = ConstantFP::get(Log->getType(), numbers::e);
2581 else if (ArgLb == Exp2Lb || ArgID == Intrinsic::exp2)
2582 Eul = ConstantFP::get(Log->getType(), 2.0);
2583 else
2584 Eul = ConstantFP::get(Log->getType(), 10.0);
2585 Value *LogE = Log->doesNotAccessMemory()
2586 ? B.CreateCall(Intrinsic::getDeclaration(Mod, LogID, Ty),
2587 Eul, "log")
2588 : emitUnaryFloatFnCall(Eul, TLI, LogNm, B, NoAttrs);
2589 Value *MulY = B.CreateFMul(Arg->getArgOperand(0), LogE, "mul");
2590 // Since exp() may have side effects, e.g. errno,
2591 // dead code elimination may not be trusted to remove it.
2592 substituteInParent(Arg, MulY);
2593 return MulY;
2594 }
2595
2596 return Ret;
2597}
2598
2599// sqrt(exp(X)) -> exp(X * 0.5)
2600Value *LibCallSimplifier::mergeSqrtToExp(CallInst *CI, IRBuilderBase &B) {
2601 if (!CI->hasAllowReassoc())
2602 return nullptr;
2603
2604 Function *SqrtFn = CI->getCalledFunction();
2605 CallInst *Arg = dyn_cast<CallInst>(CI->getArgOperand(0));
2606 if (!Arg || !Arg->hasAllowReassoc() || !Arg->hasOneUse())
2607 return nullptr;
2608 Intrinsic::ID ArgID = Arg->getIntrinsicID();
2609 LibFunc ArgLb = NotLibFunc;
2610 TLI->getLibFunc(*Arg, ArgLb);
2611
2612 LibFunc SqrtLb, ExpLb, Exp2Lb, Exp10Lb;
2613
2614 if (TLI->getLibFunc(SqrtFn->getName(), SqrtLb))
2615 switch (SqrtLb) {
2616 case LibFunc_sqrtf:
2617 ExpLb = LibFunc_expf;
2618 Exp2Lb = LibFunc_exp2f;
2619 Exp10Lb = LibFunc_exp10f;
2620 break;
2621 case LibFunc_sqrt:
2622 ExpLb = LibFunc_exp;
2623 Exp2Lb = LibFunc_exp2;
2624 Exp10Lb = LibFunc_exp10;
2625 break;
2626 case LibFunc_sqrtl:
2627 ExpLb = LibFunc_expl;
2628 Exp2Lb = LibFunc_exp2l;
2629 Exp10Lb = LibFunc_exp10l;
2630 break;
2631 default:
2632 return nullptr;
2633 }
2634 else if (SqrtFn->getIntrinsicID() == Intrinsic::sqrt) {
2635 if (CI->getType()->getScalarType()->isFloatTy()) {
2636 ExpLb = LibFunc_expf;
2637 Exp2Lb = LibFunc_exp2f;
2638 Exp10Lb = LibFunc_exp10f;
2639 } else if (CI->getType()->getScalarType()->isDoubleTy()) {
2640 ExpLb = LibFunc_exp;
2641 Exp2Lb = LibFunc_exp2;
2642 Exp10Lb = LibFunc_exp10;
2643 } else
2644 return nullptr;
2645 } else
2646 return nullptr;
2647
2648 if (ArgLb != ExpLb && ArgLb != Exp2Lb && ArgLb != Exp10Lb &&
2649 ArgID != Intrinsic::exp && ArgID != Intrinsic::exp2)
2650 return nullptr;
2651
2653 B.SetInsertPoint(Arg);
2654 auto *ExpOperand = Arg->getOperand(0);
2655 auto *FMul =
2656 B.CreateFMulFMF(ExpOperand, ConstantFP::get(ExpOperand->getType(), 0.5),
2657 CI, "merged.sqrt");
2658
2659 Arg->setOperand(0, FMul);
2660 return Arg;
2661}
2662
2663Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
2664 Module *M = CI->getModule();
2666 Value *Ret = nullptr;
2667 // TODO: Once we have a way (other than checking for the existince of the
2668 // libcall) to tell whether our target can lower @llvm.sqrt, relax the
2669 // condition below.
2670 if (isLibFuncEmittable(M, TLI, LibFunc_sqrtf) &&
2671 (Callee->getName() == "sqrt" ||
2672 Callee->getIntrinsicID() == Intrinsic::sqrt))
2673 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2674
2675 if (Value *Opt = mergeSqrtToExp(CI, B))
2676 return Opt;
2677
2678 if (!CI->isFast())
2679 return Ret;
2680
2681 Instruction *I = dyn_cast<Instruction>(CI->getArgOperand(0));
2682 if (!I || I->getOpcode() != Instruction::FMul || !I->isFast())
2683 return Ret;
2684
2685 // We're looking for a repeated factor in a multiplication tree,
2686 // so we can do this fold: sqrt(x * x) -> fabs(x);
2687 // or this fold: sqrt((x * x) * y) -> fabs(x) * sqrt(y).
2688 Value *Op0 = I->getOperand(0);
2689 Value *Op1 = I->getOperand(1);
2690 Value *RepeatOp = nullptr;
2691 Value *OtherOp = nullptr;
2692 if (Op0 == Op1) {
2693 // Simple match: the operands of the multiply are identical.
2694 RepeatOp = Op0;
2695 } else {
2696 // Look for a more complicated pattern: one of the operands is itself
2697 // a multiply, so search for a common factor in that multiply.
2698 // Note: We don't bother looking any deeper than this first level or for
2699 // variations of this pattern because instcombine's visitFMUL and/or the
2700 // reassociation pass should give us this form.
2701 Value *OtherMul0, *OtherMul1;
2702 if (match(Op0, m_FMul(m_Value(OtherMul0), m_Value(OtherMul1)))) {
2703 // Pattern: sqrt((x * y) * z)
2704 if (OtherMul0 == OtherMul1 && cast<Instruction>(Op0)->isFast()) {
2705 // Matched: sqrt((x * x) * z)
2706 RepeatOp = OtherMul0;
2707 OtherOp = Op1;
2708 }
2709 }
2710 }
2711 if (!RepeatOp)
2712 return Ret;
2713
2714 // Fast math flags for any created instructions should match the sqrt
2715 // and multiply.
2717 B.setFastMathFlags(I->getFastMathFlags());
2718
2719 // If we found a repeated factor, hoist it out of the square root and
2720 // replace it with the fabs of that factor.
2721 Type *ArgType = I->getType();
2722 Function *Fabs = Intrinsic::getDeclaration(M, Intrinsic::fabs, ArgType);
2723 Value *FabsCall = B.CreateCall(Fabs, RepeatOp, "fabs");
2724 if (OtherOp) {
2725 // If we found a non-repeated factor, we still need to get its square
2726 // root. We then multiply that by the value that was simplified out
2727 // of the square root calculation.
2728 Function *Sqrt = Intrinsic::getDeclaration(M, Intrinsic::sqrt, ArgType);
2729 Value *SqrtCall = B.CreateCall(Sqrt, OtherOp, "sqrt");
2730 return copyFlags(*CI, B.CreateFMul(FabsCall, SqrtCall));
2731 }
2732 return copyFlags(*CI, FabsCall);
2733}
2734
2735Value *LibCallSimplifier::optimizeTrigInversionPairs(CallInst *CI,
2736 IRBuilderBase &B) {
2737 Module *M = CI->getModule();
2739 Value *Ret = nullptr;
2740 StringRef Name = Callee->getName();
2741 if (UnsafeFPShrink &&
2742 (Name == "tan" || Name == "atanh" || Name == "sinh" || Name == "cosh" ||
2743 Name == "asinh") &&
2744 hasFloatVersion(M, Name))
2745 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2746
2747 Value *Op1 = CI->getArgOperand(0);
2748 auto *OpC = dyn_cast<CallInst>(Op1);
2749 if (!OpC)
2750 return Ret;
2751
2752 // Both calls must be 'fast' in order to remove them.
2753 if (!CI->isFast() || !OpC->isFast())
2754 return Ret;
2755
2756 // tan(atan(x)) -> x
2757 // atanh(tanh(x)) -> x
2758 // sinh(asinh(x)) -> x
2759 // asinh(sinh(x)) -> x
2760 // cosh(acosh(x)) -> x
2761 LibFunc Func;
2762 Function *F = OpC->getCalledFunction();
2763 if (F && TLI->getLibFunc(F->getName(), Func) &&
2764 isLibFuncEmittable(M, TLI, Func)) {
2765 LibFunc inverseFunc = llvm::StringSwitch<LibFunc>(Callee->getName())
2766 .Case("tan", LibFunc_atan)
2767 .Case("atanh", LibFunc_tanh)
2768 .Case("sinh", LibFunc_asinh)
2769 .Case("cosh", LibFunc_acosh)
2770 .Case("tanf", LibFunc_atanf)
2771 .Case("atanhf", LibFunc_tanhf)
2772 .Case("sinhf", LibFunc_asinhf)
2773 .Case("coshf", LibFunc_acoshf)
2774 .Case("tanl", LibFunc_atanl)
2775 .Case("atanhl", LibFunc_tanhl)
2776 .Case("sinhl", LibFunc_asinhl)
2777 .Case("coshl", LibFunc_acoshl)
2778 .Case("asinh", LibFunc_sinh)
2779 .Case("asinhf", LibFunc_sinhf)
2780 .Case("asinhl", LibFunc_sinhl)
2781 .Default(NumLibFuncs); // Used as error value
2782 if (Func == inverseFunc)
2783 Ret = OpC->getArgOperand(0);
2784 }
2785 return Ret;
2786}
2787
2788static bool isTrigLibCall(CallInst *CI) {
2789 // We can only hope to do anything useful if we can ignore things like errno
2790 // and floating-point exceptions.
2791 // We already checked the prototype.
2792 return CI->doesNotThrow() && CI->doesNotAccessMemory();
2793}
2794
2795static bool insertSinCosCall(IRBuilderBase &B, Function *OrigCallee, Value *Arg,
2796 bool UseFloat, Value *&Sin, Value *&Cos,
2797 Value *&SinCos, const TargetLibraryInfo *TLI) {
2798 Module *M = OrigCallee->getParent();
2799 Type *ArgTy = Arg->getType();
2800 Type *ResTy;
2802
2803 Triple T(OrigCallee->getParent()->getTargetTriple());
2804 if (UseFloat) {
2805 Name = "__sincospif_stret";
2806
2807 assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");
2808 // x86_64 can't use {float, float} since that would be returned in both
2809 // xmm0 and xmm1, which isn't what a real struct would do.
2810 ResTy = T.getArch() == Triple::x86_64
2811 ? static_cast<Type *>(FixedVectorType::get(ArgTy, 2))
2812 : static_cast<Type *>(StructType::get(ArgTy, ArgTy));
2813 } else {
2814 Name = "__sincospi_stret";
2815 ResTy = StructType::get(ArgTy, ArgTy);
2816 }
2817
2818 if (!isLibFuncEmittable(M, TLI, Name))
2819 return false;
2820 LibFunc TheLibFunc;
2821 TLI->getLibFunc(Name, TheLibFunc);
2823 M, *TLI, TheLibFunc, OrigCallee->getAttributes(), ResTy, ArgTy);
2824
2825 if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
2826 // If the argument is an instruction, it must dominate all uses so put our
2827 // sincos call there.
2828 B.SetInsertPoint(ArgInst->getParent(), ++ArgInst->getIterator());
2829 } else {
2830 // Otherwise (e.g. for a constant) the beginning of the function is as
2831 // good a place as any.
2832 BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
2833 B.SetInsertPoint(&EntryBB, EntryBB.begin());
2834 }
2835
2836 SinCos = B.CreateCall(Callee, Arg, "sincospi");
2837
2838 if (SinCos->getType()->isStructTy()) {
2839 Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
2840 Cos = B.CreateExtractValue(SinCos, 1, "cospi");
2841 } else {
2842 Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
2843 "sinpi");
2844 Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
2845 "cospi");
2846 }
2847
2848 return true;
2849}
2850
2851static Value *optimizeSymmetricCall(CallInst *CI, bool IsEven,
2852 IRBuilderBase &B) {
2853 Value *X;
2854 Value *Src = CI->getArgOperand(0);
2855
2856 if (match(Src, m_OneUse(m_FNeg(m_Value(X))))) {
2858 B.setFastMathFlags(CI->getFastMathFlags());
2859
2860 auto *CallInst = copyFlags(*CI, B.CreateCall(CI->getCalledFunction(), {X}));
2861 if (IsEven) {
2862 // Even function: f(-x) = f(x)
2863 return CallInst;
2864 }
2865 // Odd function: f(-x) = -f(x)
2866 return B.CreateFNeg(CallInst);
2867 }
2868
2869 // Even function: f(abs(x)) = f(x), f(copysign(x, y)) = f(x)
2870 if (IsEven && (match(Src, m_FAbs(m_Value(X))) ||
2871 match(Src, m_CopySign(m_Value(X), m_Value())))) {
2873 B.setFastMathFlags(CI->getFastMathFlags());
2874
2875 auto *CallInst = copyFlags(*CI, B.CreateCall(CI->getCalledFunction(), {X}));
2876 return CallInst;
2877 }
2878
2879 return nullptr;
2880}
2881
2882Value *LibCallSimplifier::optimizeSymmetric(CallInst *CI, LibFunc Func,
2883 IRBuilderBase &B) {
2884 switch (Func) {
2885 case LibFunc_cos:
2886 case LibFunc_cosf:
2887 case LibFunc_cosl:
2888 return optimizeSymmetricCall(CI, /*IsEven*/ true, B);
2889
2890 case LibFunc_sin:
2891 case LibFunc_sinf:
2892 case LibFunc_sinl:
2893
2894 case LibFunc_tan:
2895 case LibFunc_tanf:
2896 case LibFunc_tanl:
2897
2898 case LibFunc_erf:
2899 case LibFunc_erff:
2900 case LibFunc_erfl:
2901 return optimizeSymmetricCall(CI, /*IsEven*/ false, B);
2902
2903 default:
2904 return nullptr;
2905 }
2906}
2907
2908Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, bool IsSin, IRBuilderBase &B) {
2909 // Make sure the prototype is as expected, otherwise the rest of the
2910 // function is probably invalid and likely to abort.
2911 if (!isTrigLibCall(CI))
2912 return nullptr;
2913
2914 Value *Arg = CI->getArgOperand(0);
2917 SmallVector<CallInst *, 1> SinCosCalls;
2918
2919 bool IsFloat = Arg->getType()->isFloatTy();
2920
2921 // Look for all compatible sinpi, cospi and sincospi calls with the same
2922 // argument. If there are enough (in some sense) we can make the
2923 // substitution.
2924 Function *F = CI->getFunction();
2925 for (User *U : Arg->users())
2926 classifyArgUse(U, F, IsFloat, SinCalls, CosCalls, SinCosCalls);
2927
2928 // It's only worthwhile if both sinpi and cospi are actually used.
2929 if (SinCalls.empty() || CosCalls.empty())
2930 return nullptr;
2931
2932 Value *Sin, *Cos, *SinCos;
2933 if (!insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos,
2934 SinCos, TLI))
2935 return nullptr;
2936
2937 auto replaceTrigInsts = [this](SmallVectorImpl<CallInst *> &Calls,
2938 Value *Res) {
2939 for (CallInst *C : Calls)
2940 replaceAllUsesWith(C, Res);
2941 };
2942
2943 replaceTrigInsts(SinCalls, Sin);
2944 replaceTrigInsts(CosCalls, Cos);
2945 replaceTrigInsts(SinCosCalls, SinCos);
2946
2947 return IsSin ? Sin : Cos;
2948}
2949
2950void LibCallSimplifier::classifyArgUse(
2951 Value *Val, Function *F, bool IsFloat,
2954 SmallVectorImpl<CallInst *> &SinCosCalls) {
2955 auto *CI = dyn_cast<CallInst>(Val);
2956 if (!CI || CI->use_empty())
2957 return;
2958
2959 // Don't consider calls in other functions.
2960 if (CI->getFunction() != F)
2961 return;
2962
2963 Module *M = CI->getModule();
2965 LibFunc Func;
2966 if (!Callee || !TLI->getLibFunc(*Callee, Func) ||
2967 !isLibFuncEmittable(M, TLI, Func) ||
2968 !isTrigLibCall(CI))
2969 return;
2970
2971 if (IsFloat) {
2972 if (Func == LibFunc_sinpif)
2973 SinCalls.push_back(CI);
2974 else if (Func == LibFunc_cospif)
2975 CosCalls.push_back(CI);
2976 else if (Func == LibFunc_sincospif_stret)
2977 SinCosCalls.push_back(CI);
2978 } else {
2979 if (Func == LibFunc_sinpi)
2980 SinCalls.push_back(CI);
2981 else if (Func == LibFunc_cospi)
2982 CosCalls.push_back(CI);
2983 else if (Func == LibFunc_sincospi_stret)
2984 SinCosCalls.push_back(CI);
2985 }
2986}
2987
2988//===----------------------------------------------------------------------===//
2989// Integer Library Call Optimizations
2990//===----------------------------------------------------------------------===//
2991
2992Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilderBase &B) {
2993 // All variants of ffs return int which need not be 32 bits wide.
2994 // ffs{,l,ll}(x) -> x != 0 ? (int)llvm.cttz(x)+1 : 0
2995 Type *RetType = CI->getType();
2996 Value *Op = CI->getArgOperand(0);
2997 Type *ArgType = Op->getType();
2999 Intrinsic::cttz, ArgType);
3000 Value *V = B.CreateCall(F, {Op, B.getTrue()}, "cttz");
3001 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
3002 V = B.CreateIntCast(V, RetType, false);
3003
3004 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
3005 return B.CreateSelect(Cond, V, ConstantInt::get(RetType, 0));
3006}
3007
3008Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilderBase &B) {
3009 // All variants of fls return int which need not be 32 bits wide.
3010 // fls{,l,ll}(x) -> (int)(sizeInBits(x) - llvm.ctlz(x, false))
3011 Value *Op = CI->getArgOperand(0);
3012 Type *ArgType = Op->getType();
3014 Intrinsic::ctlz, ArgType);
3015 Value *V = B.CreateCall(F, {Op, B.getFalse()}, "ctlz");
3016 V = B.CreateSub(ConstantInt::get(V->getType(), ArgType->getIntegerBitWidth()),
3017 V);
3018 return B.CreateIntCast(V, CI->getType(), false);
3019}
3020
3021Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilderBase &B) {
3022 // abs(x) -> x <s 0 ? -x : x
3023 // The negation has 'nsw' because abs of INT_MIN is undefined.
3024 Value *X = CI->getArgOperand(0);
3025 Value *IsNeg = B.CreateIsNeg(X);
3026 Value *NegX = B.CreateNSWNeg(X, "neg");
3027 return B.CreateSelect(IsNeg, NegX, X);
3028}
3029
3030Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilderBase &B) {
3031 // isdigit(c) -> (c-'0') <u 10
3032 Value *Op = CI->getArgOperand(0);
3033 Type *ArgType = Op->getType();
3034 Op = B.CreateSub(Op, ConstantInt::get(ArgType, '0'), "isdigittmp");
3035 Op = B.CreateICmpULT(Op, ConstantInt::get(ArgType, 10), "isdigit");
3036 return B.CreateZExt(Op, CI->getType());
3037}
3038
3039Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilderBase &B) {
3040 // isascii(c) -> c <u 128
3041 Value *Op = CI->getArgOperand(0);
3042 Type *ArgType = Op->getType();
3043 Op = B.CreateICmpULT(Op, ConstantInt::get(ArgType, 128), "isascii");
3044 return B.CreateZExt(Op, CI->getType());
3045}
3046
3047Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilderBase &B) {
3048 // toascii(c) -> c & 0x7f
3049 return B.CreateAnd(CI->getArgOperand(0),
3050 ConstantInt::get(CI->getType(), 0x7F));
3051}
3052
3053// Fold calls to atoi, atol, and atoll.
3054Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilderBase &B) {
3055 CI->addParamAttr(0, Attribute::NoCapture);
3056
3057 StringRef Str;
3058 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
3059 return nullptr;
3060
3061 return convertStrToInt(CI, Str, nullptr, 10, /*AsSigned=*/true, B);
3062}
3063
3064// Fold calls to strtol, strtoll, strtoul, and strtoull.
3065Value *LibCallSimplifier::optimizeStrToInt(CallInst *CI, IRBuilderBase &B,
3066 bool AsSigned) {
3067 Value *EndPtr = CI->getArgOperand(1);
3068 if (isa<ConstantPointerNull>(EndPtr)) {
3069 // With a null EndPtr, this function won't capture the main argument.
3070 // It would be readonly too, except that it still may write to errno.
3071 CI->addParamAttr(0, Attribute::NoCapture);
3072 EndPtr = nullptr;
3073 } else if (!isKnownNonZero(EndPtr, DL))
3074 return nullptr;
3075
3076 StringRef Str;
3077 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
3078 return nullptr;
3079
3080 if (ConstantInt *CInt = dyn_cast<ConstantInt>(CI->getArgOperand(2))) {
3081 return convertStrToInt(CI, Str, EndPtr, CInt->getSExtValue(), AsSigned, B);
3082 }
3083
3084 return nullptr;
3085}
3086
3087//===----------------------------------------------------------------------===//
3088// Formatting and IO Library Call Optimizations
3089//===----------------------------------------------------------------------===//
3090
3091static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
3092
3093Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilderBase &B,
3094 int StreamArg) {
3096 // Error reporting calls should be cold, mark them as such.
3097 // This applies even to non-builtin calls: it is only a hint and applies to
3098 // functions that the frontend might not understand as builtins.
3099
3100 // This heuristic was suggested in:
3101 // Improving Static Branch Prediction in a Compiler
3102 // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu
3103 // Proceedings of PACT'98, Oct. 1998, IEEE
3104 if (!CI->hasFnAttr(Attribute::Cold) &&
3105 isReportingError(Callee, CI, StreamArg)) {
3106 CI->addFnAttr(Attribute::Cold);
3107 }
3108
3109 return nullptr;
3110}
3111
3112static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {
3113 if (!Callee || !Callee->isDeclaration())
3114 return false;
3115
3116 if (StreamArg < 0)
3117 return true;
3118
3119 // These functions might be considered cold, but only if their stream
3120 // argument is stderr.
3121
3122 if (StreamArg >= (int)CI->arg_size())
3123 return false;
3124 LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
3125 if (!LI)
3126 return false;
3127 GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
3128 if (!GV || !GV->isDeclaration())
3129 return false;
3130 return GV->getName() == "stderr";
3131}
3132
3133Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
3134 // Check for a fixed format string.
3135 StringRef FormatStr;
3136 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
3137 return nullptr;
3138
3139 // Empty format string -> noop.
3140 if (FormatStr.empty()) // Tolerate printf's declared void.
3141 return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);
3142
3143 // Do not do any of the following transformations if the printf return value
3144 // is used, in general the printf return value is not compatible with either
3145 // putchar() or puts().
3146 if (!CI->use_empty())
3147 return nullptr;
3148
3149 Type *IntTy = CI->getType();
3150 // printf("x") -> putchar('x'), even for "%" and "%%".
3151 if (FormatStr.size() == 1 || FormatStr == "%%") {
3152 // Convert the character to unsigned char before passing it to putchar
3153 // to avoid host-specific sign extension in the IR. Putchar converts
3154 // it to unsigned char regardless.
3155 Value *IntChar = ConstantInt::get(IntTy, (unsigned char)FormatStr[0]);
3156 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3157 }
3158
3159 // Try to remove call or emit putchar/puts.
3160 if (FormatStr == "%s" && CI->arg_size() > 1) {
3161 StringRef OperandStr;
3162 if (!getConstantStringInfo(CI->getOperand(1), OperandStr))
3163 return nullptr;
3164 // printf("%s", "") --> NOP
3165 if (OperandStr.empty())
3166 return (Value *)CI;
3167 // printf("%s", "a") --> putchar('a')
3168 if (OperandStr.size() == 1) {
3169 // Convert the character to unsigned char before passing it to putchar
3170 // to avoid host-specific sign extension in the IR. Putchar converts
3171 // it to unsigned char regardless.
3172 Value *IntChar = ConstantInt::get(IntTy, (unsigned char)OperandStr[0]);
3173 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3174 }
3175 // printf("%s", str"\n") --> puts(str)
3176 if (OperandStr.back() == '\n') {
3177 OperandStr = OperandStr.drop_back();
3178 Value *GV = B.CreateGlobalString(OperandStr, "str");
3179 return copyFlags(*CI, emitPutS(GV, B, TLI));
3180 }
3181 return nullptr;
3182 }
3183
3184 // printf("foo\n") --> puts("foo")
3185 if (FormatStr.back() == '\n' &&
3186 !FormatStr.contains('%')) { // No format characters.
3187 // Create a string literal with no \n on it. We expect the constant merge
3188 // pass to be run after this pass, to merge duplicate strings.
3189 FormatStr = FormatStr.drop_back();
3190 Value *GV = B.CreateGlobalString(FormatStr, "str");
3191 return copyFlags(*CI, emitPutS(GV, B, TLI));
3192 }
3193
3194 // Optimize specific format strings.
3195 // printf("%c", chr) --> putchar(chr)
3196 if (FormatStr == "%c" && CI->arg_size() > 1 &&
3197 CI->getArgOperand(1)->getType()->isIntegerTy()) {
3198 // Convert the argument to the type expected by putchar, i.e., int, which
3199 // need not be 32 bits wide but which is the same as printf's return type.
3200 Value *IntChar = B.CreateIntCast(CI->getArgOperand(1), IntTy, false);
3201 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3202 }
3203
3204 // printf("%s\n", str) --> puts(str)
3205 if (FormatStr == "%s\n" && CI->arg_size() > 1 &&
3206 CI->getArgOperand(1)->getType()->isPointerTy())
3207 return copyFlags(*CI, emitPutS(CI->getArgOperand(1), B, TLI));
3208 return nullptr;
3209}
3210
3211Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilderBase &B) {
3212
3213 Module *M = CI->getModule();
3215 FunctionType *FT = Callee->getFunctionType();
3216 if (Value *V = optimizePrintFString(CI, B)) {
3217 return V;
3218 }
3219
3221
3222 // printf(format, ...) -> iprintf(format, ...) if no floating point
3223 // arguments.
3224 if (isLibFuncEmittable(M, TLI, LibFunc_iprintf) &&
3226 FunctionCallee IPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_iprintf, FT,
3227 Callee->getAttributes());
3228 CallInst *New = cast<CallInst>(CI->clone());
3229 New->setCalledFunction(IPrintFFn);
3230 B.Insert(New);
3231 return New;
3232 }
3233
3234 // printf(format, ...) -> __small_printf(format, ...) if no 128-bit floating point
3235 // arguments.
3236 if (isLibFuncEmittable(M, TLI, LibFunc_small_printf) &&
3237 !callHasFP128Argument(CI)) {
3238 auto SmallPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_small_printf, FT,
3239 Callee->getAttributes());
3240 CallInst *New = cast<CallInst>(CI->clone());
3241 New->setCalledFunction(SmallPrintFFn);
3242 B.Insert(New);
3243 return New;
3244 }
3245
3246 return nullptr;
3247}
3248
3249Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI,
3250 IRBuilderBase &B) {
3251 // Check for a fixed format string.
3252 StringRef FormatStr;
3253 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
3254 return nullptr;
3255
3256 // If we just have a format string (nothing else crazy) transform it.
3257 Value *Dest = CI->getArgOperand(0);
3258 if (CI->arg_size() == 2) {
3259 // Make sure there's no % in the constant array. We could try to handle
3260 // %% -> % in the future if we cared.
3261 if (FormatStr.contains('%'))
3262 return nullptr; // we found a format specifier, bail out.
3263
3264 // sprintf(str, fmt) -> llvm.memcpy(align 1 str, align 1 fmt, strlen(fmt)+1)
3265 B.CreateMemCpy(
3266 Dest, Align(1), CI->getArgOperand(1), Align(1),
3267 ConstantInt::get(DL.getIntPtrType(CI->getContext()),
3268 FormatStr.size() + 1)); // Copy the null byte.
3269 return ConstantInt::get(CI->getType(), FormatStr.size());
3270 }
3271
3272 // The remaining optimizations require the format string to be "%s" or "%c"
3273 // and have an extra operand.
3274 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
3275 return nullptr;
3276
3277 // Decode the second character of the format string.
3278 if (FormatStr[1] == 'c') {
3279 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
3280 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
3281 return nullptr;
3282 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
3283 Value *Ptr = Dest;
3284 B.CreateStore(V, Ptr);
3285 Ptr = B.CreateInBoundsGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
3286 B.CreateStore(B.getInt8(0), Ptr);
3287
3288 return ConstantInt::get(CI->getType(), 1);
3289 }
3290
3291 if (FormatStr[1] == 's') {
3292 // sprintf(dest, "%s", str) -> llvm.memcpy(align 1 dest, align 1 str,
3293 // strlen(str)+1)
3294 if (!CI->getArgOperand(2)->getType()->isPointerTy())
3295 return nullptr;
3296
3297 if (CI->use_empty())
3298 // sprintf(dest, "%s", str) -> strcpy(dest, str)
3299 return copyFlags(*CI, emitStrCpy(Dest, CI->getArgOperand(2), B, TLI));
3300
3301 uint64_t SrcLen = GetStringLength(CI->getArgOperand(2));
3302 if (SrcLen) {
3303 B.CreateMemCpy(
3304 Dest, Align(1), CI->getArgOperand(2), Align(1),
3305 ConstantInt::get(DL.getIntPtrType(CI->getContext()), SrcLen));
3306 // Returns total number of characters written without null-character.
3307 return ConstantInt::get(CI->getType(), SrcLen - 1);
3308 } else if (Value *V = emitStpCpy(Dest, CI->getArgOperand(2), B, TLI)) {
3309 // sprintf(dest, "%s", str) -> stpcpy(dest, str) - dest
3310 Value *PtrDiff = B.CreatePtrDiff(B.getInt8Ty(), V, Dest);
3311 return B.CreateIntCast(PtrDiff, CI->getType(), false);
3312 }
3313
3314 bool OptForSize = CI->getFunction()->hasOptSize() ||
3315 llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3317 if (OptForSize)
3318 return nullptr;
3319
3320 Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
3321 if (!Len)
3322 return nullptr;
3323 Value *IncLen =
3324 B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
3325 B.CreateMemCpy(Dest, Align(1), CI->getArgOperand(2), Align(1), IncLen);
3326
3327 // The sprintf result is the unincremented number of bytes in the string.
3328 return B.CreateIntCast(Len, CI->getType(), false);
3329 }
3330 return nullptr;
3331}
3332
3333Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilderBase &B) {
3334 Module *M = CI->getModule();
3336 FunctionType *FT = Callee->getFunctionType();
3337 if (Value *V = optimizeSPrintFString(CI, B)) {
3338 return V;
3339 }
3340
3342
3343 // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
3344 // point arguments.
3345 if (isLibFuncEmittable(M, TLI, LibFunc_siprintf) &&
3347 FunctionCallee SIPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_siprintf,
3348 FT, Callee->getAttributes());
3349 CallInst *New = cast<CallInst>(CI->clone());
3350 New->setCalledFunction(SIPrintFFn);
3351 B.Insert(New);
3352 return New;
3353 }
3354
3355 // sprintf(str, format, ...) -> __small_sprintf(str, format, ...) if no 128-bit
3356 // floating point arguments.
3357 if (isLibFuncEmittable(M, TLI, LibFunc_small_sprintf) &&
3358 !callHasFP128Argument(CI)) {
3359 auto SmallSPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_small_sprintf, FT,
3360 Callee->getAttributes());
3361 CallInst *New = cast<CallInst>(CI->clone());
3362 New->setCalledFunction(SmallSPrintFFn);
3363 B.Insert(New);
3364 return New;
3365 }
3366
3367 return nullptr;
3368}
3369
3370// Transform an snprintf call CI with the bound N to format the string Str
3371// either to a call to memcpy, or to single character a store, or to nothing,
3372// and fold the result to a constant. A nonnull StrArg refers to the string
3373// argument being formatted. Otherwise the call is one with N < 2 and
3374// the "%c" directive to format a single character.
3375Value *LibCallSimplifier::emitSnPrintfMemCpy(CallInst *CI, Value *StrArg,
3376 StringRef Str, uint64_t N,
3377 IRBuilderBase &B) {
3378 assert(StrArg || (N < 2 && Str.size() == 1));
3379
3380 unsigned IntBits = TLI->getIntSize();
3381 uint64_t IntMax = maxIntN(IntBits);
3382 if (Str.size() > IntMax)
3383 // Bail if the string is longer than INT_MAX. POSIX requires
3384 // implementations to set errno to EOVERFLOW in this case, in
3385 // addition to when N is larger than that (checked by the caller).
3386 return nullptr;
3387
3388 Value *StrLen = ConstantInt::get(CI->getType(), Str.size());
3389 if (N == 0)
3390 return StrLen;
3391
3392 // Set to the number of bytes to copy fron StrArg which is also
3393 // the offset of the terinating nul.
3394 uint64_t NCopy;
3395 if (N > Str.size())
3396 // Copy the full string, including the terminating nul (which must
3397 // be present regardless of the bound).
3398 NCopy = Str.size() + 1;
3399 else
3400 NCopy = N - 1;
3401
3402 Value *DstArg = CI->getArgOperand(0);
3403 if (NCopy && StrArg)
3404 // Transform the call to lvm.memcpy(dst, fmt, N).
3405 copyFlags(
3406 *CI,
3407 B.CreateMemCpy(
3408 DstArg, Align(1), StrArg, Align(1),
3409 ConstantInt::get(DL.getIntPtrType(CI->getContext()), NCopy)));
3410
3411 if (N > Str.size())
3412 // Return early when the whole format string, including the final nul,
3413 // has been copied.
3414 return StrLen;
3415
3416 // Otherwise, when truncating the string append a terminating nul.
3417 Type *Int8Ty = B.getInt8Ty();
3418 Value *NulOff = B.getIntN(IntBits, NCopy);
3419 Value *DstEnd = B.CreateInBoundsGEP(Int8Ty, DstArg, NulOff, "endptr");
3420 B.CreateStore(ConstantInt::get(Int8Ty, 0), DstEnd);
3421 return StrLen;
3422}
3423
3424Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI,
3425 IRBuilderBase &B) {
3426 // Check for size
3427 ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
3428 if (!Size)
3429 return nullptr;
3430
3431 uint64_t N = Size->getZExtValue();
3432 uint64_t IntMax = maxIntN(TLI->getIntSize());
3433 if (N > IntMax)
3434 // Bail if the bound exceeds INT_MAX. POSIX requires implementations
3435 // to set errno to EOVERFLOW in this case.
3436 return nullptr;
3437
3438 Value *DstArg = CI->getArgOperand(0);
3439 Value *FmtArg = CI->getArgOperand(2);
3440
3441 // Check for a fixed format string.
3442 StringRef FormatStr;
3443 if (!getConstantStringInfo(FmtArg, FormatStr))
3444 return nullptr;
3445
3446 // If we just have a format string (nothing else crazy) transform it.
3447 if (CI->arg_size() == 3) {
3448 if (FormatStr.contains('%'))
3449 // Bail if the format string contains a directive and there are
3450 // no arguments. We could handle "%%" in the future.
3451 return nullptr;
3452
3453 return emitSnPrintfMemCpy(CI, FmtArg, FormatStr, N, B);
3454 }
3455
3456 // The remaining optimizations require the format string to be "%s" or "%c"
3457 // and have an extra operand.
3458 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() != 4)
3459 return nullptr;
3460
3461 // Decode the second character of the format string.
3462 if (FormatStr[1] == 'c') {
3463 if (N <= 1) {
3464 // Use an arbitary string of length 1 to transform the call into
3465 // either a nul store (N == 1) or a no-op (N == 0) and fold it
3466 // to one.
3467 StringRef CharStr("*");
3468 return emitSnPrintfMemCpy(CI, nullptr, CharStr, N, B);
3469 }
3470
3471 // snprintf(dst, size, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
3472 if (!CI->getArgOperand(3)->getType()->isIntegerTy())
3473 return nullptr;
3474 Value *V = B.CreateTrunc(CI->getArgOperand(3), B.getInt8Ty(), "char");
3475 Value *Ptr = DstArg;
3476 B.CreateStore(V, Ptr);
3477 Ptr = B.CreateInBoundsGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
3478 B.CreateStore(B.getInt8(0), Ptr);
3479 return ConstantInt::get(CI->getType(), 1);
3480 }
3481
3482 if (FormatStr[1] != 's')
3483 return nullptr;
3484
3485 Value *StrArg = CI->getArgOperand(3);
3486 // snprintf(dest, size, "%s", str) to llvm.memcpy(dest, str, len+1, 1)
3487 StringRef Str;
3488 if (!getConstantStringInfo(StrArg, Str))
3489 return nullptr;
3490
3491 return emitSnPrintfMemCpy(CI, StrArg, Str, N, B);
3492}
3493
3494Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilderBase &B) {
3495 if (Value *V = optimizeSnPrintFString(CI, B)) {
3496 return V;
3497 }
3498
3499 if (isKnownNonZero(CI->getOperand(1), DL))
3501 return nullptr;
3502}
3503
3504Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI,
3505 IRBuilderBase &B) {
3506 optimizeErrorReporting(CI, B, 0);
3507
3508 // All the optimizations depend on the format string.
3509 StringRef FormatStr;
3510 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
3511 return nullptr;
3512
3513 // Do not do any of the following transformations if the fprintf return
3514 // value is used, in general the fprintf return value is not compatible
3515 // with fwrite(), fputc() or fputs().
3516 if (!CI->use_empty())
3517 return nullptr;
3518
3519 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
3520 if (CI->arg_size() == 2) {
3521 // Could handle %% -> % if we cared.
3522 if (FormatStr.contains('%'))
3523 return nullptr; // We found a format specifier.
3524
3525 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
3526 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
3527 return copyFlags(
3528 *CI, emitFWrite(CI->getArgOperand(1),
3529 ConstantInt::get(SizeTTy, FormatStr.size()),
3530 CI->getArgOperand(0), B, DL, TLI));
3531 }
3532
3533 // The remaining optimizations require the format string to be "%s" or "%c"
3534 // and have an extra operand.
3535 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
3536 return nullptr;
3537
3538 // Decode the second character of the format string.
3539 if (FormatStr[1] == 'c') {
3540 // fprintf(F, "%c", chr) --> fputc((int)chr, F)
3541 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
3542 return nullptr;
3543 Type *IntTy = B.getIntNTy(TLI->getIntSize());
3544 Value *V = B.CreateIntCast(CI->getArgOperand(2), IntTy, /*isSigned*/ true,
3545 "chari");
3546 return copyFlags(*CI, emitFPutC(V, CI->getArgOperand(0), B, TLI));
3547 }
3548
3549 if (FormatStr[1] == 's') {
3550 // fprintf(F, "%s", str) --> fputs(str, F)
3551 if (!CI->getArgOperand(2)->getType()->isPointerTy())
3552 return nullptr;
3553 return copyFlags(
3554 *CI, emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI));
3555 }
3556 return nullptr;
3557}
3558
3559Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilderBase &B) {
3560 Module *M = CI->getModule();
3562 FunctionType *FT = Callee->getFunctionType();
3563 if (Value *V = optimizeFPrintFString(CI, B)) {
3564 return V;
3565 }
3566
3567 // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
3568 // floating point arguments.
3569 if (isLibFuncEmittable(M, TLI, LibFunc_fiprintf) &&
3571 FunctionCallee FIPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_fiprintf,
3572 FT, Callee->getAttributes());
3573 CallInst *New = cast<CallInst>(CI->clone());
3574 New->setCalledFunction(FIPrintFFn);
3575 B.Insert(New);
3576 return New;
3577 }
3578
3579 // fprintf(stream, format, ...) -> __small_fprintf(stream, format, ...) if no
3580 // 128-bit floating point arguments.
3581 if (isLibFuncEmittable(M, TLI, LibFunc_small_fprintf) &&
3582 !callHasFP128Argument(CI)) {
3583 auto SmallFPrintFFn =
3584 getOrInsertLibFunc(M, *TLI, LibFunc_small_fprintf, FT,
3585 Callee->getAttributes());
3586 CallInst *New = cast<CallInst>(CI->clone());
3587 New->setCalledFunction(SmallFPrintFFn);
3588 B.Insert(New);
3589 return New;
3590 }
3591
3592 return nullptr;
3593}
3594
3595Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilderBase &B) {
3596 optimizeErrorReporting(CI, B, 3);
3597
3598 // Get the element size and count.
3599 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
3600 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
3601 if (SizeC && CountC) {
3602 uint64_t Bytes = SizeC->getZExtValue() * CountC->getZExtValue();
3603
3604 // If this is writing zero records, remove the call (it's a noop).
3605 if (Bytes == 0)
3606 return ConstantInt::get(CI->getType(), 0);
3607
3608 // If this is writing one byte, turn it into fputc.
3609 // This optimisation is only valid, if the return value is unused.
3610 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
3611 Value *Char = B.CreateLoad(B.getInt8Ty(), CI->getArgOperand(0), "char");
3612 Type *IntTy = B.getIntNTy(TLI->getIntSize());
3613 Value *Cast = B.CreateIntCast(Char, IntTy, /*isSigned*/ true, "chari");
3614 Value *NewCI = emitFPutC(Cast, CI->getArgOperand(3), B, TLI);
3615 return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
3616 }
3617 }
3618
3619 return nullptr;
3620}
3621
3622Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) {
3623 optimizeErrorReporting(CI, B, 1);
3624
3625 // Don't rewrite fputs to fwrite when optimising for size because fwrite
3626 // requires more arguments and thus extra MOVs are required.
3627 bool OptForSize = CI->getFunction()->hasOptSize() ||
3628 llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3630 if (OptForSize)
3631 return nullptr;
3632
3633 // We can't optimize if return value is used.
3634 if (!CI->use_empty())
3635 return nullptr;
3636
3637 // fputs(s,F) --> fwrite(s,strlen(s),1,F)
3639 if (!Len)
3640 return nullptr;
3641
3642 // Known to have no uses (see above).
3643 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
3644 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
3645 return copyFlags(
3646 *CI,
3648 ConstantInt::get(SizeTTy, Len - 1),
3649 CI->getArgOperand(1), B, DL, TLI));
3650}
3651
3652Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilderBase &B) {
3654 if (!CI->use_empty())
3655 return nullptr;
3656
3657 // Check for a constant string.
3658 // puts("") -> putchar('\n')
3659 StringRef Str;
3660 if (getConstantStringInfo(CI->getArgOperand(0), Str) && Str.empty()) {
3661 // putchar takes an argument of the same type as puts returns, i.e.,
3662 // int, which need not be 32 bits wide.
3663 Type *IntTy = CI->getType();
3664 return copyFlags(*CI, emitPutChar(ConstantInt::get(IntTy, '\n'), B, TLI));
3665 }
3666
3667 return nullptr;
3668}
3669
3670Value *LibCallSimplifier::optimizeBCopy(CallInst *CI, IRBuilderBase &B) {
3671 // bcopy(src, dst, n) -> llvm.memmove(dst, src, n)
3672 return copyFlags(*CI, B.CreateMemMove(CI->getArgOperand(1), Align(1),
3673 CI->getArgOperand(0), Align(1),
3674 CI->getArgOperand(2)));
3675}
3676
3677bool LibCallSimplifier::hasFloatVersion(const Module *M, StringRef FuncName) {
3678 SmallString<20> FloatFuncName = FuncName;
3679 FloatFuncName += 'f';
3680 return isLibFuncEmittable(M, TLI, FloatFuncName);
3681}
3682
3683Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI,
3684 IRBuilderBase &Builder) {
3685 Module *M = CI->getModule();
3686 LibFunc Func;
3688 // Check for string/memory library functions.
3689 if (TLI->getLibFunc(*Callee, Func) && isLibFuncEmittable(M, TLI, Func)) {
3690 // Make sure we never change the calling convention.
3691 assert(
3692 (ignoreCallingConv(Func) ||
3694 "Optimizing string/memory libcall would change the calling convention");
3695 switch (Func) {
3696 case LibFunc_strcat:
3697 return optimizeStrCat(CI, Builder);
3698 case LibFunc_strncat:
3699 return optimizeStrNCat(CI, Builder);
3700 case LibFunc_strchr:
3701 return optimizeStrChr(CI, Builder);
3702 case LibFunc_strrchr:
3703 return optimizeStrRChr(CI, Builder);
3704 case LibFunc_strcmp:
3705 return optimizeStrCmp(CI, Builder);
3706 case LibFunc_strncmp:
3707 return optimizeStrNCmp(CI, Builder);
3708 case LibFunc_strcpy:
3709 return optimizeStrCpy(CI, Builder);
3710 case LibFunc_stpcpy:
3711 return optimizeStpCpy(CI, Builder);
3712 case LibFunc_strlcpy:
3713 return optimizeStrLCpy(CI, Builder);
3714 case LibFunc_stpncpy:
3715 return optimizeStringNCpy(CI, /*RetEnd=*/true, Builder);
3716 case LibFunc_strncpy:
3717 return optimizeStringNCpy(CI, /*RetEnd=*/false, Builder);
3718 case LibFunc_strlen:
3719 return optimizeStrLen(CI, Builder);
3720 case LibFunc_strnlen:
3721 return optimizeStrNLen(CI, Builder);
3722 case LibFunc_strpbrk:
3723 return optimizeStrPBrk(CI, Builder);
3724 case LibFunc_strndup:
3725 return optimizeStrNDup(CI, Builder);
3726 case LibFunc_strtol:
3727 case LibFunc_strtod:
3728 case LibFunc_strtof:
3729 case LibFunc_strtoul:
3730 case LibFunc_strtoll:
3731 case LibFunc_strtold:
3732 case LibFunc_strtoull:
3733 return optimizeStrTo(CI, Builder);
3734 case LibFunc_strspn:
3735 return optimizeStrSpn(CI, Builder);
3736 case LibFunc_strcspn:
3737 return optimizeStrCSpn(CI, Builder);
3738 case LibFunc_strstr:
3739 return optimizeStrStr(CI, Builder);
3740 case LibFunc_memchr:
3741 return optimizeMemChr(CI, Builder);
3742 case LibFunc_memrchr:
3743 return optimizeMemRChr(CI, Builder);
3744 case LibFunc_bcmp:
3745 return optimizeBCmp(CI, Builder);
3746 case LibFunc_memcmp:
3747 return optimizeMemCmp(CI, Builder);
3748 case LibFunc_memcpy:
3749 return optimizeMemCpy(CI, Builder);
3750 case LibFunc_memccpy:
3751 return optimizeMemCCpy(CI, Builder);
3752 case LibFunc_mempcpy:
3753 return optimizeMemPCpy(CI, Builder);
3754 case LibFunc_memmove:
3755 return optimizeMemMove(CI, Builder);
3756 case LibFunc_memset:
3757 return optimizeMemSet(CI, Builder);
3758 case LibFunc_realloc:
3759 return optimizeRealloc(CI, Builder);
3760 case LibFunc_wcslen:
3761 return optimizeWcslen(CI, Builder);
3762 case LibFunc_bcopy:
3763 return optimizeBCopy(CI, Builder);
3764 case LibFunc_Znwm:
3765 case LibFunc_ZnwmRKSt9nothrow_t:
3766 case LibFunc_ZnwmSt11align_val_t:
3767 case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t:
3768 case LibFunc_Znam:
3769 case LibFunc_ZnamRKSt9nothrow_t:
3770 case LibFunc_ZnamSt11align_val_t:
3771 case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t:
3772 case LibFunc_Znwm12__hot_cold_t:
3773 case LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t:
3774 case LibFunc_ZnwmSt11align_val_t12__hot_cold_t:
3775 case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t:
3776 case LibFunc_Znam12__hot_cold_t:
3777 case LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t:
3778 case LibFunc_ZnamSt11align_val_t12__hot_cold_t:
3779 case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t:
3780 return optimizeNew(CI, Builder, Func);
3781 default:
3782 break;
3783 }
3784 }
3785 return nullptr;
3786}
3787
3788Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
3789 LibFunc Func,
3790 IRBuilderBase &Builder) {
3791 const Module *M = CI->getModule();
3792
3793 // Don't optimize calls that require strict floating point semantics.
3794 if (CI->isStrictFP())
3795 return nullptr;
3796
3797 if (Value *V = optimizeSymmetric(CI, Func, Builder))
3798 return V;
3799
3800 switch (Func) {
3801 case LibFunc_sinpif:
3802 case LibFunc_sinpi:
3803 return optimizeSinCosPi(CI, /*IsSin*/true, Builder);
3804 case LibFunc_cospif:
3805 case LibFunc_cospi:
3806 return optimizeSinCosPi(CI, /*IsSin*/false, Builder);
3807 case LibFunc_powf:
3808 case LibFunc_pow:
3809 case LibFunc_powl:
3810 return optimizePow(CI, Builder);
3811 case LibFunc_exp2l:
3812 case LibFunc_exp2:
3813 case LibFunc_exp2f:
3814 return optimizeExp2(CI, Builder);
3815 case LibFunc_fabsf:
3816 case LibFunc_fabs:
3817 case LibFunc_fabsl:
3818 return replaceUnaryCall(CI, Builder, Intrinsic::fabs);
3819 case LibFunc_sqrtf:
3820 case LibFunc_sqrt:
3821 case LibFunc_sqrtl:
3822 return optimizeSqrt(CI, Builder);
3823 case LibFunc_logf:
3824 case LibFunc_log:
3825 case LibFunc_logl:
3826 case LibFunc_log10f:
3827 case LibFunc_log10:
3828 case LibFunc_log10l:
3829 case LibFunc_log1pf:
3830 case LibFunc_log1p:
3831 case LibFunc_log1pl:
3832 case LibFunc_log2f:
3833 case LibFunc_log2:
3834 case LibFunc_log2l:
3835 case LibFunc_logbf:
3836 case LibFunc_logb:
3837 case LibFunc_logbl:
3838 return optimizeLog(CI, Builder);
3839 case LibFunc_tan:
3840 case LibFunc_tanf:
3841 case LibFunc_tanl:
3842 case LibFunc_sinh:
3843 case LibFunc_sinhf:
3844 case LibFunc_sinhl:
3845 case LibFunc_asinh:
3846 case LibFunc_asinhf:
3847 case LibFunc_asinhl:
3848 case LibFunc_cosh:
3849 case LibFunc_coshf:
3850 case LibFunc_coshl:
3851 case LibFunc_atanh:
3852 case LibFunc_atanhf:
3853 case LibFunc_atanhl:
3854 return optimizeTrigInversionPairs(CI, Builder);
3855 case LibFunc_ceil:
3856 return replaceUnaryCall(CI, Builder, Intrinsic::ceil);
3857 case LibFunc_floor:
3858 return replaceUnaryCall(CI, Builder, Intrinsic::floor);
3859 case LibFunc_round:
3860 return replaceUnaryCall(CI, Builder, Intrinsic::round);
3861 case LibFunc_roundeven:
3862 return replaceUnaryCall(CI, Builder, Intrinsic::roundeven);
3863 case LibFunc_nearbyint:
3864 return replaceUnaryCall(CI, Builder, Intrinsic::nearbyint);
3865 case LibFunc_rint:
3866 return replaceUnaryCall(CI, Builder, Intrinsic::rint);
3867 case LibFunc_trunc:
3868 return replaceUnaryCall(CI, Builder, Intrinsic::trunc);
3869 case LibFunc_acos:
3870 case LibFunc_acosh:
3871 case LibFunc_asin:
3872 case LibFunc_atan:
3873 case LibFunc_cbrt:
3874 case LibFunc_exp:
3875 case LibFunc_exp10:
3876 case LibFunc_expm1:
3877 case LibFunc_cos:
3878 case LibFunc_sin:
3879 case LibFunc_tanh:
3880 if (UnsafeFPShrink && hasFloatVersion(M, CI->getCalledFunction()->getName()))
3881 return optimizeUnaryDoubleFP(CI, Builder, TLI, true);
3882 return nullptr;
3883 case LibFunc_copysign:
3884 if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
3885 return optimizeBinaryDoubleFP(CI, Builder, TLI);
3886 return nullptr;
3887 case LibFunc_fminf:
3888 case LibFunc_fmin:
3889 case LibFunc_fminl:
3890 case LibFunc_fmaxf:
3891 case LibFunc_fmax:
3892 case LibFunc_fmaxl:
3893 return optimizeFMinFMax(CI, Builder);
3894 case LibFunc_cabs:
3895 case LibFunc_cabsf:
3896 case LibFunc_cabsl:
3897 return optimizeCAbs(CI, Builder);
3898 default:
3899 return nullptr;
3900 }
3901}
3902
3904 Module *M = CI->getModule();
3905 assert(!CI->isMustTailCall() && "These transforms aren't musttail safe.");
3906
3907 // TODO: Split out the code below that operates on FP calls so that
3908 // we can all non-FP calls with the StrictFP attribute to be
3909 // optimized.
3910 if (CI->isNoBuiltin())
3911 return nullptr;
3912
3913 LibFunc Func;
3914 Function *Callee = CI->getCalledFunction();
3915 bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
3916
3918 CI->getOperandBundlesAsDefs(OpBundles);
3919
3921 Builder.setDefaultOperandBundles(OpBundles);
3922
3923 // Command-line parameter overrides instruction attribute.
3924 // This can't be moved to optimizeFloatingPointLibCall() because it may be
3925 // used by the intrinsic optimizations.
3926 if (EnableUnsafeFPShrink.getNumOccurrences() > 0)
3927 UnsafeFPShrink = EnableUnsafeFPShrink;
3928 else if (isa<FPMathOperator>(CI) && CI->isFast())
3929 UnsafeFPShrink = true;
3930
3931 // First, check for intrinsics.
3932 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
3933 if (!IsCallingConvC)
3934 return nullptr;
3935 // The FP intrinsics have corresponding constrained versions so we don't
3936 // need to check for the StrictFP attribute here.
3937 switch (II->getIntrinsicID()) {
3938 case Intrinsic::pow:
3939 return optimizePow(CI, Builder);
3940 case Intrinsic::exp2:
3941 return optimizeExp2(CI, Builder);
3942 case Intrinsic::log:
3943 case Intrinsic::log2:
3944 case Intrinsic::log10:
3945 return optimizeLog(CI, Builder);
3946 case Intrinsic::sqrt:
3947 return optimizeSqrt(CI, Builder);
3948 case Intrinsic::memset:
3949 return optimizeMemSet(CI, Builder);
3950 case Intrinsic::memcpy:
3951 return optimizeMemCpy(CI, Builder);
3952 case Intrinsic::memmove:
3953 return optimizeMemMove(CI, Builder);
3954 default:
3955 return nullptr;
3956 }
3957 }
3958
3959 // Also try to simplify calls to fortified library functions.
3960 if (Value *SimplifiedFortifiedCI =
3961 FortifiedSimplifier.optimizeCall(CI, Builder))
3962 return SimplifiedFortifiedCI;
3963
3964 // Then check for known library functions.
3965 if (TLI->getLibFunc(*Callee, Func) && isLibFuncEmittable(M, TLI, Func)) {
3966 // We never change the calling convention.
3967 if (!ignoreCallingConv(Func) && !IsCallingConvC)
3968 return nullptr;
3969 if (Value *V = optimizeStringMemoryLibCall(CI, Builder))
3970 return V;
3971 if (Value *V = optimizeFloatingPointLibCall(CI, Func, Builder))
3972 return V;
3973 switch (Func) {
3974 case LibFunc_ffs:
3975 case LibFunc_ffsl:
3976 case LibFunc_ffsll:
3977 return optimizeFFS(CI, Builder);
3978 case LibFunc_fls:
3979 case LibFunc_flsl:
3980 case LibFunc_flsll:
3981 return optimizeFls(CI, Builder);
3982 case LibFunc_abs:
3983 case LibFunc_labs:
3984 case LibFunc_llabs:
3985 return optimizeAbs(CI, Builder);
3986 case LibFunc_isdigit:
3987 return optimizeIsDigit(CI, Builder);
3988 case LibFunc_isascii:
3989 return optimizeIsAscii(CI, Builder);
3990 case LibFunc_toascii:
3991 return optimizeToAscii(CI, Builder);
3992 case LibFunc_atoi:
3993 case LibFunc_atol:
3994 case LibFunc_atoll:
3995 return optimizeAtoi(CI, Builder);
3996 case LibFunc_strtol:
3997 case LibFunc_strtoll:
3998 return optimizeStrToInt(CI, Builder, /*AsSigned=*/true);
3999 case LibFunc_strtoul:
4000 case LibFunc_strtoull:
4001 return optimizeStrToInt(CI, Builder, /*AsSigned=*/false);
4002 case LibFunc_printf:
4003 return optimizePrintF(CI, Builder);
4004 case LibFunc_sprintf:
4005 return optimizeSPrintF(CI, Builder);
4006 case LibFunc_snprintf:
4007 return optimizeSnPrintF(CI, Builder);
4008 case LibFunc_fprintf:
4009 return optimizeFPrintF(CI, Builder);
4010 case LibFunc_fwrite:
4011 return optimizeFWrite(CI, Builder);
4012 case LibFunc_fputs:
4013 return optimizeFPuts(CI, Builder);
4014 case LibFunc_puts:
4015 return optimizePuts(CI, Builder);
4016 case LibFunc_perror:
4017 return optimizeErrorReporting(CI, Builder);
4018 case LibFunc_vfprintf:
4019 case LibFunc_fiprintf:
4020 return optimizeErrorReporting(CI, Builder, 0);
4021 default:
4022 return nullptr;
4023 }
4024 }
4025 return nullptr;
4026}
4027
4029 const DataLayout &DL, const TargetLibraryInfo *TLI, AssumptionCache *AC,
4031 ProfileSummaryInfo *PSI,
4032 function_ref<void(Instruction *, Value *)> Replacer,
4033 function_ref<void(Instruction *)> Eraser)
4034 : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), AC(AC), ORE(ORE), BFI(BFI),
4035 PSI(PSI), Replacer(Replacer), Eraser(Eraser) {}
4036
4037void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) {
4038 // Indirect through the replacer used in this instance.
4039 Replacer(I, With);
4040}
4041
4042void LibCallSimplifier::eraseFromParent(Instruction *I) {
4043 Eraser(I);
4044}
4045
4046// TODO:
4047// Additional cases that we need to add to this file:
4048//
4049// cbrt:
4050// * cbrt(expN(X)) -> expN(x/3)
4051// * cbrt(sqrt(x)) -> pow(x,1/6)
4052// * cbrt(cbrt(x)) -> pow(x,1/9)
4053//
4054// exp, expf, expl:
4055// * exp(log(x)) -> x
4056//
4057// log, logf, logl:
4058// * log(exp(x)) -> x
4059// * log(exp(y)) -> y*log(e)
4060// * log(exp10(y)) -> y*log(10)
4061// * log(sqrt(x)) -> 0.5*log(x)
4062//
4063// pow, powf, powl:
4064// * pow(sqrt(x),y) -> pow(x,y*0.5)
4065// * pow(pow(x,y),z)-> pow(x,y*z)
4066//
4067// signbit:
4068// * signbit(cnst) -> cnst'
4069// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
4070//
4071// sqrt, sqrtf, sqrtl:
4072// * sqrt(expN(x)) -> expN(x*0.5)
4073// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
4074// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
4075//
4076
4077//===----------------------------------------------------------------------===//
4078// Fortified Library Call Optimizations
4079//===----------------------------------------------------------------------===//
4080
4081bool FortifiedLibCallSimplifier::isFortifiedCallFoldable(
4082 CallInst *CI, unsigned ObjSizeOp, std::optional<unsigned> SizeOp,
4083 std::optional<unsigned> StrOp, std::optional<unsigned> FlagOp) {
4084 // If this function takes a flag argument, the implementation may use it to
4085 // perform extra checks. Don't fold into the non-checking variant.
4086 if (FlagOp) {
4087 ConstantInt *Flag = dyn_cast<ConstantInt>(CI->getArgOperand(*FlagOp));
4088 if (!Flag || !Flag->isZero())
4089 return false;
4090 }
4091
4092 if (SizeOp && CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(*SizeOp))
4093 return true;
4094
4095 if (ConstantInt *ObjSizeCI =
4096 dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) {
4097 if (ObjSizeCI->isMinusOne())
4098 return true;
4099 // If the object size wasn't -1 (unknown), bail out if we were asked to.
4100 if (OnlyLowerUnknownSize)
4101 return false;
4102 if (StrOp) {
4104 // If the length is 0 we don't know how long it is and so we can't
4105 // remove the check.
4106 if (Len)
4107 annotateDereferenceableBytes(CI, *StrOp, Len);
4108 else
4109 return false;
4110 return ObjSizeCI->getZExtValue() >= Len;
4111 }
4112
4113 if (SizeOp) {
4114 if (ConstantInt *SizeCI =
4115 dyn_cast<ConstantInt>(CI->getArgOperand(*SizeOp)))
4116 return ObjSizeCI->getZExtValue() >= SizeCI->getZExtValue();
4117 }
4118 }
4119 return false;
4120}
4121
4122Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI,
4123 IRBuilderBase &B) {
4124 if (isFortifiedCallFoldable(CI, 3, 2)) {
4125 CallInst *NewCI =
4126 B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
4127 Align(1), CI->getArgOperand(2));
4128 mergeAttributesAndFlags(NewCI, *CI);
4129 return CI->getArgOperand(0);
4130 }
4131 return nullptr;
4132}
4133
4134Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI,
4135 IRBuilderBase &B) {
4136 if (isFortifiedCallFoldable(CI, 3, 2)) {
4137 CallInst *NewCI =
4138 B.CreateMemMove(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
4139 Align(1), CI->getArgOperand(2));
4140 mergeAttributesAndFlags(NewCI, *CI);
4141 return CI->getArgOperand(0);
4142 }
4143 return nullptr;
4144}
4145
4146Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI,
4147 IRBuilderBase &B) {
4148 if (isFortifiedCallFoldable(CI, 3, 2)) {
4149 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
4150 CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val,
4151 CI->getArgOperand(2), Align(1));
4152 mergeAttributesAndFlags(NewCI, *CI);
4153 return CI->getArgOperand(0);
4154 }
4155 return nullptr;
4156}
4157
4158Value *FortifiedLibCallSimplifier::optimizeMemPCpyChk(CallInst *CI,
4159 IRBuilderBase &B) {
4160 const DataLayout &DL = CI->getModule()->getDataLayout();
4161 if (isFortifiedCallFoldable(CI, 3, 2))
4162 if (Value *Call = emitMemPCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4163 CI->getArgOperand(2), B, DL, TLI)) {
4164 return mergeAttributesAndFlags(cast<CallInst>(Call), *CI);
4165 }
4166 return nullptr;
4167}
4168
4169Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
4171 LibFunc Func) {
4172 const DataLayout &DL = CI->getModule()->getDataLayout();
4173 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
4174 *ObjSize = CI->getArgOperand(2);
4175
4176 // __stpcpy_chk(x,x,...) -> x+strlen(x)
4177 if (Func == LibFunc_stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) {
4178 Value *StrLen = emitStrLen(Src, B, DL, TLI);
4179 return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
4180 }
4181
4182 // If a) we don't have any length information, or b) we know this will
4183 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
4184 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
4185 // TODO: It might be nice to get a maximum length out of the possible
4186 // string lengths for varying.
4187 if (isFortifiedCallFoldable(CI, 2, std::nullopt, 1)) {
4188 if (Func == LibFunc_strcpy_chk)
4189 return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
4190 else
4191 return copyFlags(*CI, emitStpCpy(Dst, Src, B, TLI));
4192 }
4193
4194 if (OnlyLowerUnknownSize)
4195 return nullptr;
4196
4197 // Maybe we can stil fold __st[rp]cpy_chk to __memcpy_chk.
4199 if (Len)
4200 annotateDereferenceableBytes(CI, 1, Len);
4201 else
4202 return nullptr;
4203
4204 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
4205 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
4206 Value *LenV = ConstantInt::get(SizeTTy, Len);
4207 Value *Ret = emitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI);
4208 // If the function was an __stpcpy_chk, and we were able to fold it into
4209 // a __memcpy_chk, we still need to return the correct end pointer.
4210 if (Ret && Func == LibFunc_stpcpy_chk)
4211 return B.CreateInBoundsGEP(B.getInt8Ty(), Dst,
4212 ConstantInt::get(SizeTTy, Len - 1));
4213 return copyFlags(*CI, cast<CallInst>(Ret));
4214}
4215
4216Value *FortifiedLibCallSimplifier::optimizeStrLenChk(CallInst *CI,
4217 IRBuilderBase &B) {
4218 if (isFortifiedCallFoldable(CI, 1, std::nullopt, 0))
4219 return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B,
4220 CI->getModule()->getDataLayout(), TLI));
4221 return nullptr;
4222}
4223
4224Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
4226 LibFunc Func) {
4227 if (isFortifiedCallFoldable(CI, 3, 2)) {
4228 if (Func == LibFunc_strncpy_chk)
4229 return copyFlags(*CI,
4231 CI->getArgOperand(2), B, TLI));
4232 else
4233 return copyFlags(*CI,
4235 CI->getArgOperand(2), B, TLI));
4236 }
4237
4238 return nullptr;
4239}
4240
4241Value *FortifiedLibCallSimplifier::optimizeMemCCpyChk(CallInst *CI,
4242 IRBuilderBase &B) {
4243 if (isFortifiedCallFoldable(CI, 4, 3))
4244 return copyFlags(
4245 *CI, emitMemCCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4246 CI->getArgOperand(2), CI->getArgOperand(3), B, TLI));
4247
4248 return nullptr;
4249}
4250
4251Value *FortifiedLibCallSimplifier::optimizeSNPrintfChk(CallInst *CI,
4252 IRBuilderBase &B) {
4253 if (isFortifiedCallFoldable(CI, 3, 1, std::nullopt, 2)) {
4254 SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 5));
4255 return copyFlags(*CI,
4257 CI->getArgOperand(4), VariadicArgs, B, TLI));
4258 }
4259
4260 return nullptr;
4261}
4262
4263Value *FortifiedLibCallSimplifier::optimizeSPrintfChk(CallInst *CI,
4264 IRBuilderBase &B) {
4265 if (isFortifiedCallFoldable(CI, 2, std::nullopt, std::nullopt, 1)) {
4266 SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 4));
4267 return copyFlags(*CI,
4269 VariadicArgs, B, TLI));
4270 }
4271
4272 return nullptr;
4273}
4274
4275Value *FortifiedLibCallSimplifier::optimizeStrCatChk(CallInst *CI,
4276 IRBuilderBase &B) {
4277 if (isFortifiedCallFoldable(CI, 2))
4278 return copyFlags(
4279 *CI, emitStrCat(CI->getArgOperand(0), CI->getArgOperand(1), B, TLI));
4280
4281 return nullptr;
4282}
4283
4284Value *FortifiedLibCallSimplifier::optimizeStrLCat(CallInst *CI,
4285 IRBuilderBase &B) {
4286 if (isFortifiedCallFoldable(CI, 3))
4287 return copyFlags(*CI,
4289 CI->getArgOperand(2), B, TLI));
4290
4291 return nullptr;
4292}
4293
4294Value *FortifiedLibCallSimplifier::optimizeStrNCatChk(CallInst *CI,
4295 IRBuilderBase &B) {
4296 if (isFortifiedCallFoldable(CI, 3))
4297 return copyFlags(*CI,
4299 CI->getArgOperand(2), B, TLI));
4300
4301 return nullptr;
4302}
4303
4304Value *FortifiedLibCallSimplifier::optimizeStrLCpyChk(CallInst *CI,
4305 IRBuilderBase &B) {
4306 if (isFortifiedCallFoldable(CI, 3))
4307 return copyFlags(*CI,
4309 CI->getArgOperand(2), B, TLI));
4310
4311 return nullptr;
4312}
4313
4314Value *FortifiedLibCallSimplifier::optimizeVSNPrintfChk(CallInst *CI,
4315 IRBuilderBase &B) {
4316 if (isFortifiedCallFoldable(CI, 3, 1, std::nullopt, 2))
4317 return copyFlags(
4318 *CI, emitVSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1),
4319 CI->getArgOperand(4), CI->getArgOperand(5), B, TLI));
4320
4321 return nullptr;
4322}
4323
4324Value *FortifiedLibCallSimplifier::optimizeVSPrintfChk(CallInst *CI,
4325 IRBuilderBase &B) {
4326 if (isFortifiedCallFoldable(CI, 2, std::nullopt, std::nullopt, 1))
4327 return copyFlags(*CI,
4329 CI->getArgOperand(4), B, TLI));
4330
4331 return nullptr;
4332}
4333
4335 IRBuilderBase &Builder) {
4336 // FIXME: We shouldn't be changing "nobuiltin" or TLI unavailable calls here.
4337 // Some clang users checked for _chk libcall availability using:
4338 // __has_builtin(__builtin___memcpy_chk)
4339 // When compiling with -fno-builtin, this is always true.
4340 // When passing -ffreestanding/-mkernel, which both imply -fno-builtin, we
4341 // end up with fortified libcalls, which isn't acceptable in a freestanding
4342 // environment which only provides their non-fortified counterparts.
4343 //
4344 // Until we change clang and/or teach external users to check for availability
4345 // differently, disregard the "nobuiltin" attribute and TLI::has.
4346 //
4347 // PR23093.
4348
4349 LibFunc Func;
4350 Function *Callee = CI->getCalledFunction();
4351 bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
4352
4354 CI->getOperandBundlesAsDefs(OpBundles);
4355
4357 Builder.setDefaultOperandBundles(OpBundles);
4358
4359 // First, check that this is a known library functions and that the prototype
4360 // is correct.
4361 if (!TLI->getLibFunc(*Callee, Func))
4362 return nullptr;
4363
4364 // We never change the calling convention.
4365 if (!ignoreCallingConv(Func) && !IsCallingConvC)
4366 return nullptr;
4367
4368 switch (Func) {
4369 case LibFunc_memcpy_chk:
4370 return optimizeMemCpyChk(CI, Builder);
4371 case LibFunc_mempcpy_chk:
4372 return optimizeMemPCpyChk(CI, Builder);
4373 case LibFunc_memmove_chk:
4374 return optimizeMemMoveChk(CI, Builder);
4375 case LibFunc_memset_chk:
4376 return optimizeMemSetChk(CI, Builder);
4377 case LibFunc_stpcpy_chk:
4378 case LibFunc_strcpy_chk:
4379 return optimizeStrpCpyChk(CI, Builder, Func);
4380 case LibFunc_strlen_chk:
4381 return optimizeStrLenChk(CI, Builder);
4382 case LibFunc_stpncpy_chk:
4383 case LibFunc_strncpy_chk:
4384 return optimizeStrpNCpyChk(CI, Builder, Func);
4385 case LibFunc_memccpy_chk:
4386 return optimizeMemCCpyChk(CI, Builder);
4387 case LibFunc_snprintf_chk:
4388 return optimizeSNPrintfChk(CI, Builder);
4389 case LibFunc_sprintf_chk:
4390 return optimizeSPrintfChk(CI, Builder);
4391 case LibFunc_strcat_chk:
4392 return optimizeStrCatChk(CI, Builder);
4393 case LibFunc_strlcat_chk:
4394 return optimizeStrLCat(CI, Builder);
4395 case LibFunc_strncat_chk:
4396 return optimizeStrNCatChk(CI, Builder);
4397 case LibFunc_strlcpy_chk:
4398 return optimizeStrLCpyChk(CI, Builder);
4399 case LibFunc_vsnprintf_chk:
4400 return optimizeVSNPrintfChk(CI, Builder);
4401 case LibFunc_vsprintf_chk:
4402 return optimizeVSPrintfChk(CI, Builder);
4403 default:
4404 break;
4405 }
4406 return nullptr;
4407}
4408
4410 const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
4411 : 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:5191
bool isNegative() const
Definition: APFloat.h:1295
double convertToDouble() const
Converts this APFloat to host double value.
Definition: APFloat.cpp:5250
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:5263
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:675
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:286
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:655
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:1469
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:2058
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