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 CallInst *NewCall = B.CreateUnaryIntrinsic(IID, CI->getArgOperand(0), CI);
1859 NewCall->takeName(CI);
1860 return copyFlags(*CI, NewCall);
1861}
1862
1863/// Return a variant of Val with float type.
1864/// Currently this works in two cases: If Val is an FPExtension of a float
1865/// value to something bigger, simply return the operand.
1866/// If Val is a ConstantFP but can be converted to a float ConstantFP without
1867/// loss of precision do so.
1869 if (FPExtInst *Cast = dyn_cast<FPExtInst>(Val)) {
1870 Value *Op = Cast->getOperand(0);
1871 if (Op->getType()->isFloatTy())
1872 return Op;
1873 }
1874 if (ConstantFP *Const = dyn_cast<ConstantFP>(Val)) {
1875 APFloat F = Const->getValueAPF();
1876 bool losesInfo;
1878 &losesInfo);
1879 if (!losesInfo)
1880 return ConstantFP::get(Const->getContext(), F);
1881 }
1882 return nullptr;
1883}
1884
1885/// Shrink double -> float functions.
1887 bool isBinary, const TargetLibraryInfo *TLI,
1888 bool isPrecise = false) {
1889 Function *CalleeFn = CI->getCalledFunction();
1890 if (!CI->getType()->isDoubleTy() || !CalleeFn)
1891 return nullptr;
1892
1893 // If not all the uses of the function are converted to float, then bail out.
1894 // This matters if the precision of the result is more important than the
1895 // precision of the arguments.
1896 if (isPrecise)
1897 for (User *U : CI->users()) {
1898 FPTruncInst *Cast = dyn_cast<FPTruncInst>(U);
1899 if (!Cast || !Cast->getType()->isFloatTy())
1900 return nullptr;
1901 }
1902
1903 // If this is something like 'g((double) float)', convert to 'gf(float)'.
1904 Value *V[2];
1906 V[1] = isBinary ? valueHasFloatPrecision(CI->getArgOperand(1)) : nullptr;
1907 if (!V[0] || (isBinary && !V[1]))
1908 return nullptr;
1909
1910 // If call isn't an intrinsic, check that it isn't within a function with the
1911 // same name as the float version of this call, otherwise the result is an
1912 // infinite loop. For example, from MinGW-w64:
1913 //
1914 // float expf(float val) { return (float) exp((double) val); }
1915 StringRef CalleeName = CalleeFn->getName();
1916 bool IsIntrinsic = CalleeFn->isIntrinsic();
1917 if (!IsIntrinsic) {
1918 StringRef CallerName = CI->getFunction()->getName();
1919 if (!CallerName.empty() && CallerName.back() == 'f' &&
1920 CallerName.size() == (CalleeName.size() + 1) &&
1921 CallerName.starts_with(CalleeName))
1922 return nullptr;
1923 }
1924
1925 // Propagate the math semantics from the current function to the new function.
1927 B.setFastMathFlags(CI->getFastMathFlags());
1928
1929 // g((double) float) -> (double) gf(float)
1930 Value *R;
1931 if (IsIntrinsic) {
1932 Module *M = CI->getModule();
1933 Intrinsic::ID IID = CalleeFn->getIntrinsicID();
1934 Function *Fn = Intrinsic::getDeclaration(M, IID, B.getFloatTy());
1935 R = isBinary ? B.CreateCall(Fn, V) : B.CreateCall(Fn, V[0]);
1936 } else {
1937 AttributeList CalleeAttrs = CalleeFn->getAttributes();
1938 R = isBinary ? emitBinaryFloatFnCall(V[0], V[1], TLI, CalleeName, B,
1939 CalleeAttrs)
1940 : emitUnaryFloatFnCall(V[0], TLI, CalleeName, B, CalleeAttrs);
1941 }
1942 return B.CreateFPExt(R, B.getDoubleTy());
1943}
1944
1945/// Shrink double -> float for unary functions.
1947 const TargetLibraryInfo *TLI,
1948 bool isPrecise = false) {
1949 return optimizeDoubleFP(CI, B, false, TLI, isPrecise);
1950}
1951
1952/// Shrink double -> float for binary functions.
1954 const TargetLibraryInfo *TLI,
1955 bool isPrecise = false) {
1956 return optimizeDoubleFP(CI, B, true, TLI, isPrecise);
1957}
1958
1959// cabs(z) -> sqrt((creal(z)*creal(z)) + (cimag(z)*cimag(z)))
1960Value *LibCallSimplifier::optimizeCAbs(CallInst *CI, IRBuilderBase &B) {
1961 if (!CI->isFast())
1962 return nullptr;
1963
1964 // Propagate fast-math flags from the existing call to new instructions.
1966 B.setFastMathFlags(CI->getFastMathFlags());
1967
1968 Value *Real, *Imag;
1969 if (CI->arg_size() == 1) {
1970 Value *Op = CI->getArgOperand(0);
1971 assert(Op->getType()->isArrayTy() && "Unexpected signature for cabs!");
1972 Real = B.CreateExtractValue(Op, 0, "real");
1973 Imag = B.CreateExtractValue(Op, 1, "imag");
1974 } else {
1975 assert(CI->arg_size() == 2 && "Unexpected signature for cabs!");
1976 Real = CI->getArgOperand(0);
1977 Imag = CI->getArgOperand(1);
1978 }
1979
1980 Value *RealReal = B.CreateFMul(Real, Real);
1981 Value *ImagImag = B.CreateFMul(Imag, Imag);
1982
1983 return copyFlags(*CI, B.CreateUnaryIntrinsic(Intrinsic::sqrt,
1984 B.CreateFAdd(RealReal, ImagImag),
1985 nullptr, "cabs"));
1986}
1987
1988// Return a properly extended integer (DstWidth bits wide) if the operation is
1989// an itofp.
1990static Value *getIntToFPVal(Value *I2F, IRBuilderBase &B, unsigned DstWidth) {
1991 if (isa<SIToFPInst>(I2F) || isa<UIToFPInst>(I2F)) {
1992 Value *Op = cast<Instruction>(I2F)->getOperand(0);
1993 // Make sure that the exponent fits inside an "int" of size DstWidth,
1994 // thus avoiding any range issues that FP has not.
1995 unsigned BitWidth = Op->getType()->getScalarSizeInBits();
1996 if (BitWidth < DstWidth || (BitWidth == DstWidth && isa<SIToFPInst>(I2F))) {
1997 Type *IntTy = Op->getType()->getWithNewBitWidth(DstWidth);
1998 return isa<SIToFPInst>(I2F) ? B.CreateSExt(Op, IntTy)
1999 : B.CreateZExt(Op, IntTy);
2000 }
2001 }
2002
2003 return nullptr;
2004}
2005
2006/// Use exp{,2}(x * y) for pow(exp{,2}(x), y);
2007/// ldexp(1.0, x) for pow(2.0, itofp(x)); exp2(n * x) for pow(2.0 ** n, x);
2008/// exp10(x) for pow(10.0, x); exp2(log2(n) * x) for pow(n, x).
2009Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilderBase &B) {
2010 Module *M = Pow->getModule();
2011 Value *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
2012 Type *Ty = Pow->getType();
2013 bool Ignored;
2014
2015 // Evaluate special cases related to a nested function as the base.
2016
2017 // pow(exp(x), y) -> exp(x * y)
2018 // pow(exp2(x), y) -> exp2(x * y)
2019 // If exp{,2}() is used only once, it is better to fold two transcendental
2020 // math functions into one. If used again, exp{,2}() would still have to be
2021 // called with the original argument, then keep both original transcendental
2022 // functions. However, this transformation is only safe with fully relaxed
2023 // math semantics, since, besides rounding differences, it changes overflow
2024 // and underflow behavior quite dramatically. For example:
2025 // pow(exp(1000), 0.001) = pow(inf, 0.001) = inf
2026 // Whereas:
2027 // exp(1000 * 0.001) = exp(1)
2028 // TODO: Loosen the requirement for fully relaxed math semantics.
2029 // TODO: Handle exp10() when more targets have it available.
2030 CallInst *BaseFn = dyn_cast<CallInst>(Base);
2031 if (BaseFn && BaseFn->hasOneUse() && BaseFn->isFast() && Pow->isFast()) {
2032 LibFunc LibFn;
2033
2034 Function *CalleeFn = BaseFn->getCalledFunction();
2035 if (CalleeFn && TLI->getLibFunc(CalleeFn->getName(), LibFn) &&
2036 isLibFuncEmittable(M, TLI, LibFn)) {
2037 StringRef ExpName;
2039 Value *ExpFn;
2040 LibFunc LibFnFloat, LibFnDouble, LibFnLongDouble;
2041
2042 switch (LibFn) {
2043 default:
2044 return nullptr;
2045 case LibFunc_expf:
2046 case LibFunc_exp:
2047 case LibFunc_expl:
2048 ExpName = TLI->getName(LibFunc_exp);
2049 ID = Intrinsic::exp;
2050 LibFnFloat = LibFunc_expf;
2051 LibFnDouble = LibFunc_exp;
2052 LibFnLongDouble = LibFunc_expl;
2053 break;
2054 case LibFunc_exp2f:
2055 case LibFunc_exp2:
2056 case LibFunc_exp2l:
2057 ExpName = TLI->getName(LibFunc_exp2);
2058 ID = Intrinsic::exp2;
2059 LibFnFloat = LibFunc_exp2f;
2060 LibFnDouble = LibFunc_exp2;
2061 LibFnLongDouble = LibFunc_exp2l;
2062 break;
2063 }
2064
2065 // Create new exp{,2}() with the product as its argument.
2066 Value *FMul = B.CreateFMul(BaseFn->getArgOperand(0), Expo, "mul");
2067 ExpFn = BaseFn->doesNotAccessMemory()
2068 ? B.CreateUnaryIntrinsic(ID, FMul, nullptr, ExpName)
2069 : emitUnaryFloatFnCall(FMul, TLI, LibFnDouble, LibFnFloat,
2070 LibFnLongDouble, B,
2071 BaseFn->getAttributes());
2072
2073 // Since the new exp{,2}() is different from the original one, dead code
2074 // elimination cannot be trusted to remove it, since it may have side
2075 // effects (e.g., errno). When the only consumer for the original
2076 // exp{,2}() is pow(), then it has to be explicitly erased.
2077 substituteInParent(BaseFn, ExpFn);
2078 return ExpFn;
2079 }
2080 }
2081
2082 // Evaluate special cases related to a constant base.
2083
2084 const APFloat *BaseF;
2085 if (!match(Base, m_APFloat(BaseF)))
2086 return nullptr;
2087
2088 AttributeList NoAttrs; // Attributes are only meaningful on the original call
2089
2090 const bool UseIntrinsic = Pow->doesNotAccessMemory();
2091
2092 // pow(2.0, itofp(x)) -> ldexp(1.0, x)
2093 if ((UseIntrinsic || !Ty->isVectorTy()) && BaseF->isExactlyValue(2.0) &&
2094 (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo)) &&
2095 (UseIntrinsic ||
2096 hasFloatFn(M, TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl))) {
2097
2098 // TODO: Shouldn't really need to depend on getIntToFPVal for intrinsic. Can
2099 // just directly use the original integer type.
2100 if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize())) {
2101 Constant *One = ConstantFP::get(Ty, 1.0);
2102
2103 if (UseIntrinsic) {
2104 return copyFlags(*Pow, B.CreateIntrinsic(Intrinsic::ldexp,
2105 {Ty, ExpoI->getType()},
2106 {One, ExpoI}, Pow, "exp2"));
2107 }
2108
2109 return copyFlags(*Pow, emitBinaryFloatFnCall(
2110 One, ExpoI, TLI, LibFunc_ldexp, LibFunc_ldexpf,
2111 LibFunc_ldexpl, B, NoAttrs));
2112 }
2113 }
2114
2115 // pow(2.0 ** n, x) -> exp2(n * x)
2116 if (hasFloatFn(M, TLI, Ty, LibFunc_exp2, LibFunc_exp2f, LibFunc_exp2l)) {
2117 APFloat BaseR = APFloat(1.0);
2118 BaseR.convert(BaseF->getSemantics(), APFloat::rmTowardZero, &Ignored);
2119 BaseR = BaseR / *BaseF;
2120 bool IsInteger = BaseF->isInteger(), IsReciprocal = BaseR.isInteger();
2121 const APFloat *NF = IsReciprocal ? &BaseR : BaseF;
2122 APSInt NI(64, false);
2123 if ((IsInteger || IsReciprocal) &&
2124 NF->convertToInteger(NI, APFloat::rmTowardZero, &Ignored) ==
2125 APFloat::opOK &&
2126 NI > 1 && NI.isPowerOf2()) {
2127 double N = NI.logBase2() * (IsReciprocal ? -1.0 : 1.0);
2128 Value *FMul = B.CreateFMul(Expo, ConstantFP::get(Ty, N), "mul");
2129 if (Pow->doesNotAccessMemory())
2130 return copyFlags(*Pow, B.CreateUnaryIntrinsic(Intrinsic::exp2, FMul,
2131 nullptr, "exp2"));
2132 else
2133 return copyFlags(*Pow, emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2,
2134 LibFunc_exp2f,
2135 LibFunc_exp2l, B, NoAttrs));
2136 }
2137 }
2138
2139 // pow(10.0, x) -> exp10(x)
2140 if (BaseF->isExactlyValue(10.0) &&
2141 hasFloatFn(M, TLI, Ty, LibFunc_exp10, LibFunc_exp10f, LibFunc_exp10l)) {
2142
2143 if (Pow->doesNotAccessMemory()) {
2144 CallInst *NewExp10 =
2145 B.CreateIntrinsic(Intrinsic::exp10, {Ty}, {Expo}, Pow, "exp10");
2146 return copyFlags(*Pow, NewExp10);
2147 }
2148
2149 return copyFlags(*Pow, emitUnaryFloatFnCall(Expo, TLI, LibFunc_exp10,
2150 LibFunc_exp10f, LibFunc_exp10l,
2151 B, NoAttrs));
2152 }
2153
2154 // pow(x, y) -> exp2(log2(x) * y)
2155 if (Pow->hasApproxFunc() && Pow->hasNoNaNs() && BaseF->isFiniteNonZero() &&
2156 !BaseF->isNegative()) {
2157 // pow(1, inf) is defined to be 1 but exp2(log2(1) * inf) evaluates to NaN.
2158 // Luckily optimizePow has already handled the x == 1 case.
2159 assert(!match(Base, m_FPOne()) &&
2160 "pow(1.0, y) should have been simplified earlier!");
2161
2162 Value *Log = nullptr;
2163 if (Ty->isFloatTy())
2164 Log = ConstantFP::get(Ty, std::log2(BaseF->convertToFloat()));
2165 else if (Ty->isDoubleTy())
2166 Log = ConstantFP::get(Ty, std::log2(BaseF->convertToDouble()));
2167
2168 if (Log) {
2169 Value *FMul = B.CreateFMul(Log, Expo, "mul");
2170 if (Pow->doesNotAccessMemory())
2171 return copyFlags(*Pow, B.CreateUnaryIntrinsic(Intrinsic::exp2, FMul,
2172 nullptr, "exp2"));
2173 else if (hasFloatFn(M, TLI, Ty, LibFunc_exp2, LibFunc_exp2f,
2174 LibFunc_exp2l))
2175 return copyFlags(*Pow, emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2,
2176 LibFunc_exp2f,
2177 LibFunc_exp2l, B, NoAttrs));
2178 }
2179 }
2180
2181 return nullptr;
2182}
2183
2184static Value *getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno,
2185 Module *M, IRBuilderBase &B,
2186 const TargetLibraryInfo *TLI) {
2187 // If errno is never set, then use the intrinsic for sqrt().
2188 if (NoErrno)
2189 return B.CreateUnaryIntrinsic(Intrinsic::sqrt, V, nullptr, "sqrt");
2190
2191 // Otherwise, use the libcall for sqrt().
2192 if (hasFloatFn(M, TLI, V->getType(), LibFunc_sqrt, LibFunc_sqrtf,
2193 LibFunc_sqrtl))
2194 // TODO: We also should check that the target can in fact lower the sqrt()
2195 // libcall. We currently have no way to ask this question, so we ask if
2196 // the target has a sqrt() libcall, which is not exactly the same.
2197 return emitUnaryFloatFnCall(V, TLI, LibFunc_sqrt, LibFunc_sqrtf,
2198 LibFunc_sqrtl, B, Attrs);
2199
2200 return nullptr;
2201}
2202
2203/// Use square root in place of pow(x, +/-0.5).
2204Value *LibCallSimplifier::replacePowWithSqrt(CallInst *Pow, IRBuilderBase &B) {
2205 Value *Sqrt, *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
2206 Module *Mod = Pow->getModule();
2207 Type *Ty = Pow->getType();
2208
2209 const APFloat *ExpoF;
2210 if (!match(Expo, m_APFloat(ExpoF)) ||
2211 (!ExpoF->isExactlyValue(0.5) && !ExpoF->isExactlyValue(-0.5)))
2212 return nullptr;
2213
2214 // Converting pow(X, -0.5) to 1/sqrt(X) may introduce an extra rounding step,
2215 // so that requires fast-math-flags (afn or reassoc).
2216 if (ExpoF->isNegative() && (!Pow->hasApproxFunc() && !Pow->hasAllowReassoc()))
2217 return nullptr;
2218
2219 // If we have a pow() library call (accesses memory) and we can't guarantee
2220 // that the base is not an infinity, give up:
2221 // pow(-Inf, 0.5) is optionally required to have a result of +Inf (not setting
2222 // errno), but sqrt(-Inf) is required by various standards to set errno.
2223 if (!Pow->doesNotAccessMemory() && !Pow->hasNoInfs() &&
2225 SimplifyQuery(DL, TLI, /*DT=*/nullptr, AC, Pow)))
2226 return nullptr;
2227
2229 TLI);
2230 if (!Sqrt)
2231 return nullptr;
2232
2233 // Handle signed zero base by expanding to fabs(sqrt(x)).
2234 if (!Pow->hasNoSignedZeros())
2235 Sqrt = B.CreateUnaryIntrinsic(Intrinsic::fabs, Sqrt, nullptr, "abs");
2236
2237 Sqrt = copyFlags(*Pow, Sqrt);
2238
2239 // Handle non finite base by expanding to
2240 // (x == -infinity ? +infinity : sqrt(x)).
2241 if (!Pow->hasNoInfs()) {
2242 Value *PosInf = ConstantFP::getInfinity(Ty),
2243 *NegInf = ConstantFP::getInfinity(Ty, true);
2244 Value *FCmp = B.CreateFCmpOEQ(Base, NegInf, "isinf");
2245 Sqrt = B.CreateSelect(FCmp, PosInf, Sqrt);
2246 }
2247
2248 // If the exponent is negative, then get the reciprocal.
2249 if (ExpoF->isNegative())
2250 Sqrt = B.CreateFDiv(ConstantFP::get(Ty, 1.0), Sqrt, "reciprocal");
2251
2252 return Sqrt;
2253}
2254
2256 IRBuilderBase &B) {
2257 Value *Args[] = {Base, Expo};
2258 Type *Types[] = {Base->getType(), Expo->getType()};
2259 return B.CreateIntrinsic(Intrinsic::powi, Types, Args);
2260}
2261
2262Value *LibCallSimplifier::optimizePow(CallInst *Pow, IRBuilderBase &B) {
2263 Value *Base = Pow->getArgOperand(0);
2264 Value *Expo = Pow->getArgOperand(1);
2266 StringRef Name = Callee->getName();
2267 Type *Ty = Pow->getType();
2268 Module *M = Pow->getModule();
2269 bool AllowApprox = Pow->hasApproxFunc();
2270 bool Ignored;
2271
2272 // Propagate the math semantics from the call to any created instructions.
2274 B.setFastMathFlags(Pow->getFastMathFlags());
2275 // Evaluate special cases related to the base.
2276
2277 // pow(1.0, x) -> 1.0
2278 if (match(Base, m_FPOne()))
2279 return Base;
2280
2281 if (Value *Exp = replacePowWithExp(Pow, B))
2282 return Exp;
2283
2284 // Evaluate special cases related to the exponent.
2285
2286 // pow(x, -1.0) -> 1.0 / x
2287 if (match(Expo, m_SpecificFP(-1.0)))
2288 return B.CreateFDiv(ConstantFP::get(Ty, 1.0), Base, "reciprocal");
2289
2290 // pow(x, +/-0.0) -> 1.0
2291 if (match(Expo, m_AnyZeroFP()))
2292 return ConstantFP::get(Ty, 1.0);
2293
2294 // pow(x, 1.0) -> x
2295 if (match(Expo, m_FPOne()))
2296 return Base;
2297
2298 // pow(x, 2.0) -> x * x
2299 if (match(Expo, m_SpecificFP(2.0)))
2300 return B.CreateFMul(Base, Base, "square");
2301
2302 if (Value *Sqrt = replacePowWithSqrt(Pow, B))
2303 return Sqrt;
2304
2305 // If we can approximate pow:
2306 // pow(x, n) -> powi(x, n) * sqrt(x) if n has exactly a 0.5 fraction
2307 // pow(x, n) -> powi(x, n) if n is a constant signed integer value
2308 const APFloat *ExpoF;
2309 if (AllowApprox && match(Expo, m_APFloat(ExpoF)) &&
2310 !ExpoF->isExactlyValue(0.5) && !ExpoF->isExactlyValue(-0.5)) {
2311 APFloat ExpoA(abs(*ExpoF));
2312 APFloat ExpoI(*ExpoF);
2313 Value *Sqrt = nullptr;
2314 if (!ExpoA.isInteger()) {
2315 APFloat Expo2 = ExpoA;
2316 // To check if ExpoA is an integer + 0.5, we add it to itself. If there
2317 // is no floating point exception and the result is an integer, then
2318 // ExpoA == integer + 0.5
2319 if (Expo2.add(ExpoA, APFloat::rmNearestTiesToEven) != APFloat::opOK)
2320 return nullptr;
2321
2322 if (!Expo2.isInteger())
2323 return nullptr;
2324
2325 if (ExpoI.roundToIntegral(APFloat::rmTowardNegative) !=
2327 return nullptr;
2328 if (!ExpoI.isInteger())
2329 return nullptr;
2330 ExpoF = &ExpoI;
2331
2333 B, TLI);
2334 if (!Sqrt)
2335 return nullptr;
2336 }
2337
2338 // 0.5 fraction is now optionally handled.
2339 // Do pow -> powi for remaining integer exponent
2340 APSInt IntExpo(TLI->getIntSize(), /*isUnsigned=*/false);
2341 if (ExpoF->isInteger() &&
2342 ExpoF->convertToInteger(IntExpo, APFloat::rmTowardZero, &Ignored) ==
2343 APFloat::opOK) {
2344 Value *PowI = copyFlags(
2345 *Pow,
2347 Base, ConstantInt::get(B.getIntNTy(TLI->getIntSize()), IntExpo),
2348 M, B));
2349
2350 if (PowI && Sqrt)
2351 return B.CreateFMul(PowI, Sqrt);
2352
2353 return PowI;
2354 }
2355 }
2356
2357 // powf(x, itofp(y)) -> powi(x, y)
2358 if (AllowApprox && (isa<SIToFPInst>(Expo) || isa<UIToFPInst>(Expo))) {
2359 if (Value *ExpoI = getIntToFPVal(Expo, B, TLI->getIntSize()))
2360 return copyFlags(*Pow, createPowWithIntegerExponent(Base, ExpoI, M, B));
2361 }
2362
2363 // Shrink pow() to powf() if the arguments are single precision,
2364 // unless the result is expected to be double precision.
2365 if (UnsafeFPShrink && Name == TLI->getName(LibFunc_pow) &&
2366 hasFloatVersion(M, Name)) {
2367 if (Value *Shrunk = optimizeBinaryDoubleFP(Pow, B, TLI, true))
2368 return Shrunk;
2369 }
2370
2371 return nullptr;
2372}
2373
2374Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilderBase &B) {
2375 Module *M = CI->getModule();
2377 StringRef Name = Callee->getName();
2378 Value *Ret = nullptr;
2379 if (UnsafeFPShrink && Name == TLI->getName(LibFunc_exp2) &&
2380 hasFloatVersion(M, Name))
2381 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2382
2383 // If we have an llvm.exp2 intrinsic, emit the llvm.ldexp intrinsic. If we
2384 // have the libcall, emit the libcall.
2385 //
2386 // TODO: In principle we should be able to just always use the intrinsic for
2387 // any doesNotAccessMemory callsite.
2388
2389 const bool UseIntrinsic = Callee->isIntrinsic();
2390 // Bail out for vectors because the code below only expects scalars.
2391 Type *Ty = CI->getType();
2392 if (!UseIntrinsic && Ty->isVectorTy())
2393 return Ret;
2394
2395 // exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= IntSize
2396 // exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < IntSize
2397 Value *Op = CI->getArgOperand(0);
2398 if ((isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op)) &&
2399 (UseIntrinsic ||
2400 hasFloatFn(M, TLI, Ty, LibFunc_ldexp, LibFunc_ldexpf, LibFunc_ldexpl))) {
2401 if (Value *Exp = getIntToFPVal(Op, B, TLI->getIntSize())) {
2402 Constant *One = ConstantFP::get(Ty, 1.0);
2403
2404 if (UseIntrinsic) {
2405 return copyFlags(*CI, B.CreateIntrinsic(Intrinsic::ldexp,
2406 {Ty, Exp->getType()},
2407 {One, Exp}, CI));
2408 }
2409
2411 B.setFastMathFlags(CI->getFastMathFlags());
2412 return copyFlags(*CI, emitBinaryFloatFnCall(
2413 One, Exp, TLI, LibFunc_ldexp, LibFunc_ldexpf,
2414 LibFunc_ldexpl, B, AttributeList()));
2415 }
2416 }
2417
2418 return Ret;
2419}
2420
2421Value *LibCallSimplifier::optimizeFMinFMax(CallInst *CI, IRBuilderBase &B) {
2422 Module *M = CI->getModule();
2423
2424 // If we can shrink the call to a float function rather than a double
2425 // function, do that first.
2427 StringRef Name = Callee->getName();
2428 if ((Name == "fmin" || Name == "fmax") && hasFloatVersion(M, Name))
2429 if (Value *Ret = optimizeBinaryDoubleFP(CI, B, TLI))
2430 return Ret;
2431
2432 // The LLVM intrinsics minnum/maxnum correspond to fmin/fmax. Canonicalize to
2433 // the intrinsics for improved optimization (for example, vectorization).
2434 // No-signed-zeros is implied by the definitions of fmax/fmin themselves.
2435 // From the C standard draft WG14/N1256:
2436 // "Ideally, fmax would be sensitive to the sign of zero, for example
2437 // fmax(-0.0, +0.0) would return +0; however, implementation in software
2438 // might be impractical."
2440 FastMathFlags FMF = CI->getFastMathFlags();
2441 FMF.setNoSignedZeros();
2442 B.setFastMathFlags(FMF);
2443
2444 Intrinsic::ID IID = Callee->getName().starts_with("fmin") ? Intrinsic::minnum
2445 : Intrinsic::maxnum;
2446 return copyFlags(*CI, B.CreateBinaryIntrinsic(IID, CI->getArgOperand(0),
2447 CI->getArgOperand(1)));
2448}
2449
2450Value *LibCallSimplifier::optimizeLog(CallInst *Log, IRBuilderBase &B) {
2451 Function *LogFn = Log->getCalledFunction();
2452 StringRef LogNm = LogFn->getName();
2453 Intrinsic::ID LogID = LogFn->getIntrinsicID();
2454 Module *Mod = Log->getModule();
2455 Type *Ty = Log->getType();
2456 Value *Ret = nullptr;
2457
2458 if (UnsafeFPShrink && hasFloatVersion(Mod, LogNm))
2459 Ret = optimizeUnaryDoubleFP(Log, B, TLI, true);
2460
2461 // The earlier call must also be 'fast' in order to do these transforms.
2462 CallInst *Arg = dyn_cast<CallInst>(Log->getArgOperand(0));
2463 if (!Log->isFast() || !Arg || !Arg->isFast() || !Arg->hasOneUse())
2464 return Ret;
2465
2466 LibFunc LogLb, ExpLb, Exp2Lb, Exp10Lb, PowLb;
2467
2468 // This is only applicable to log(), log2(), log10().
2469 if (TLI->getLibFunc(LogNm, LogLb))
2470 switch (LogLb) {
2471 case LibFunc_logf:
2472 LogID = Intrinsic::log;
2473 ExpLb = LibFunc_expf;
2474 Exp2Lb = LibFunc_exp2f;
2475 Exp10Lb = LibFunc_exp10f;
2476 PowLb = LibFunc_powf;
2477 break;
2478 case LibFunc_log:
2479 LogID = Intrinsic::log;
2480 ExpLb = LibFunc_exp;
2481 Exp2Lb = LibFunc_exp2;
2482 Exp10Lb = LibFunc_exp10;
2483 PowLb = LibFunc_pow;
2484 break;
2485 case LibFunc_logl:
2486 LogID = Intrinsic::log;
2487 ExpLb = LibFunc_expl;
2488 Exp2Lb = LibFunc_exp2l;
2489 Exp10Lb = LibFunc_exp10l;
2490 PowLb = LibFunc_powl;
2491 break;
2492 case LibFunc_log2f:
2493 LogID = Intrinsic::log2;
2494 ExpLb = LibFunc_expf;
2495 Exp2Lb = LibFunc_exp2f;
2496 Exp10Lb = LibFunc_exp10f;
2497 PowLb = LibFunc_powf;
2498 break;
2499 case LibFunc_log2:
2500 LogID = Intrinsic::log2;
2501 ExpLb = LibFunc_exp;
2502 Exp2Lb = LibFunc_exp2;
2503 Exp10Lb = LibFunc_exp10;
2504 PowLb = LibFunc_pow;
2505 break;
2506 case LibFunc_log2l:
2507 LogID = Intrinsic::log2;
2508 ExpLb = LibFunc_expl;
2509 Exp2Lb = LibFunc_exp2l;
2510 Exp10Lb = LibFunc_exp10l;
2511 PowLb = LibFunc_powl;
2512 break;
2513 case LibFunc_log10f:
2514 LogID = Intrinsic::log10;
2515 ExpLb = LibFunc_expf;
2516 Exp2Lb = LibFunc_exp2f;
2517 Exp10Lb = LibFunc_exp10f;
2518 PowLb = LibFunc_powf;
2519 break;
2520 case LibFunc_log10:
2521 LogID = Intrinsic::log10;
2522 ExpLb = LibFunc_exp;
2523 Exp2Lb = LibFunc_exp2;
2524 Exp10Lb = LibFunc_exp10;
2525 PowLb = LibFunc_pow;
2526 break;
2527 case LibFunc_log10l:
2528 LogID = Intrinsic::log10;
2529 ExpLb = LibFunc_expl;
2530 Exp2Lb = LibFunc_exp2l;
2531 Exp10Lb = LibFunc_exp10l;
2532 PowLb = LibFunc_powl;
2533 break;
2534 default:
2535 return Ret;
2536 }
2537 else if (LogID == Intrinsic::log || LogID == Intrinsic::log2 ||
2538 LogID == Intrinsic::log10) {
2539 if (Ty->getScalarType()->isFloatTy()) {
2540 ExpLb = LibFunc_expf;
2541 Exp2Lb = LibFunc_exp2f;
2542 Exp10Lb = LibFunc_exp10f;
2543 PowLb = LibFunc_powf;
2544 } else if (Ty->getScalarType()->isDoubleTy()) {
2545 ExpLb = LibFunc_exp;
2546 Exp2Lb = LibFunc_exp2;
2547 Exp10Lb = LibFunc_exp10;
2548 PowLb = LibFunc_pow;
2549 } else
2550 return Ret;
2551 } else
2552 return Ret;
2553
2555 B.setFastMathFlags(FastMathFlags::getFast());
2556
2557 Intrinsic::ID ArgID = Arg->getIntrinsicID();
2558 LibFunc ArgLb = NotLibFunc;
2559 TLI->getLibFunc(*Arg, ArgLb);
2560
2561 // log(pow(x,y)) -> y*log(x)
2562 AttributeList NoAttrs;
2563 if (ArgLb == PowLb || ArgID == Intrinsic::pow || ArgID == Intrinsic::powi) {
2564 Value *LogX =
2565 Log->doesNotAccessMemory()
2566 ? B.CreateUnaryIntrinsic(LogID, Arg->getOperand(0), nullptr, "log")
2567 : emitUnaryFloatFnCall(Arg->getOperand(0), TLI, LogNm, B, NoAttrs);
2568 Value *Y = Arg->getArgOperand(1);
2569 // Cast exponent to FP if integer.
2570 if (ArgID == Intrinsic::powi)
2571 Y = B.CreateSIToFP(Y, Ty, "cast");
2572 Value *MulY = B.CreateFMul(Y, LogX, "mul");
2573 // Since pow() may have side effects, e.g. errno,
2574 // dead code elimination may not be trusted to remove it.
2575 substituteInParent(Arg, MulY);
2576 return MulY;
2577 }
2578
2579 // log(exp{,2,10}(y)) -> y*log({e,2,10})
2580 // TODO: There is no exp10() intrinsic yet.
2581 if (ArgLb == ExpLb || ArgLb == Exp2Lb || ArgLb == Exp10Lb ||
2582 ArgID == Intrinsic::exp || ArgID == Intrinsic::exp2) {
2583 Constant *Eul;
2584 if (ArgLb == ExpLb || ArgID == Intrinsic::exp)
2585 // FIXME: Add more precise value of e for long double.
2586 Eul = ConstantFP::get(Log->getType(), numbers::e);
2587 else if (ArgLb == Exp2Lb || ArgID == Intrinsic::exp2)
2588 Eul = ConstantFP::get(Log->getType(), 2.0);
2589 else
2590 Eul = ConstantFP::get(Log->getType(), 10.0);
2591 Value *LogE = Log->doesNotAccessMemory()
2592 ? B.CreateUnaryIntrinsic(LogID, Eul, nullptr, "log")
2593 : emitUnaryFloatFnCall(Eul, TLI, LogNm, B, NoAttrs);
2594 Value *MulY = B.CreateFMul(Arg->getArgOperand(0), LogE, "mul");
2595 // Since exp() may have side effects, e.g. errno,
2596 // dead code elimination may not be trusted to remove it.
2597 substituteInParent(Arg, MulY);
2598 return MulY;
2599 }
2600
2601 return Ret;
2602}
2603
2604// sqrt(exp(X)) -> exp(X * 0.5)
2605Value *LibCallSimplifier::mergeSqrtToExp(CallInst *CI, IRBuilderBase &B) {
2606 if (!CI->hasAllowReassoc())
2607 return nullptr;
2608
2609 Function *SqrtFn = CI->getCalledFunction();
2610 CallInst *Arg = dyn_cast<CallInst>(CI->getArgOperand(0));
2611 if (!Arg || !Arg->hasAllowReassoc() || !Arg->hasOneUse())
2612 return nullptr;
2613 Intrinsic::ID ArgID = Arg->getIntrinsicID();
2614 LibFunc ArgLb = NotLibFunc;
2615 TLI->getLibFunc(*Arg, ArgLb);
2616
2617 LibFunc SqrtLb, ExpLb, Exp2Lb, Exp10Lb;
2618
2619 if (TLI->getLibFunc(SqrtFn->getName(), SqrtLb))
2620 switch (SqrtLb) {
2621 case LibFunc_sqrtf:
2622 ExpLb = LibFunc_expf;
2623 Exp2Lb = LibFunc_exp2f;
2624 Exp10Lb = LibFunc_exp10f;
2625 break;
2626 case LibFunc_sqrt:
2627 ExpLb = LibFunc_exp;
2628 Exp2Lb = LibFunc_exp2;
2629 Exp10Lb = LibFunc_exp10;
2630 break;
2631 case LibFunc_sqrtl:
2632 ExpLb = LibFunc_expl;
2633 Exp2Lb = LibFunc_exp2l;
2634 Exp10Lb = LibFunc_exp10l;
2635 break;
2636 default:
2637 return nullptr;
2638 }
2639 else if (SqrtFn->getIntrinsicID() == Intrinsic::sqrt) {
2640 if (CI->getType()->getScalarType()->isFloatTy()) {
2641 ExpLb = LibFunc_expf;
2642 Exp2Lb = LibFunc_exp2f;
2643 Exp10Lb = LibFunc_exp10f;
2644 } else if (CI->getType()->getScalarType()->isDoubleTy()) {
2645 ExpLb = LibFunc_exp;
2646 Exp2Lb = LibFunc_exp2;
2647 Exp10Lb = LibFunc_exp10;
2648 } else
2649 return nullptr;
2650 } else
2651 return nullptr;
2652
2653 if (ArgLb != ExpLb && ArgLb != Exp2Lb && ArgLb != Exp10Lb &&
2654 ArgID != Intrinsic::exp && ArgID != Intrinsic::exp2)
2655 return nullptr;
2656
2658 B.SetInsertPoint(Arg);
2659 auto *ExpOperand = Arg->getOperand(0);
2660 auto *FMul =
2661 B.CreateFMulFMF(ExpOperand, ConstantFP::get(ExpOperand->getType(), 0.5),
2662 CI, "merged.sqrt");
2663
2664 Arg->setOperand(0, FMul);
2665 return Arg;
2666}
2667
2668Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
2669 Module *M = CI->getModule();
2671 Value *Ret = nullptr;
2672 // TODO: Once we have a way (other than checking for the existince of the
2673 // libcall) to tell whether our target can lower @llvm.sqrt, relax the
2674 // condition below.
2675 if (isLibFuncEmittable(M, TLI, LibFunc_sqrtf) &&
2676 (Callee->getName() == "sqrt" ||
2677 Callee->getIntrinsicID() == Intrinsic::sqrt))
2678 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2679
2680 if (Value *Opt = mergeSqrtToExp(CI, B))
2681 return Opt;
2682
2683 if (!CI->isFast())
2684 return Ret;
2685
2686 Instruction *I = dyn_cast<Instruction>(CI->getArgOperand(0));
2687 if (!I || I->getOpcode() != Instruction::FMul || !I->isFast())
2688 return Ret;
2689
2690 // We're looking for a repeated factor in a multiplication tree,
2691 // so we can do this fold: sqrt(x * x) -> fabs(x);
2692 // or this fold: sqrt((x * x) * y) -> fabs(x) * sqrt(y).
2693 Value *Op0 = I->getOperand(0);
2694 Value *Op1 = I->getOperand(1);
2695 Value *RepeatOp = nullptr;
2696 Value *OtherOp = nullptr;
2697 if (Op0 == Op1) {
2698 // Simple match: the operands of the multiply are identical.
2699 RepeatOp = Op0;
2700 } else {
2701 // Look for a more complicated pattern: one of the operands is itself
2702 // a multiply, so search for a common factor in that multiply.
2703 // Note: We don't bother looking any deeper than this first level or for
2704 // variations of this pattern because instcombine's visitFMUL and/or the
2705 // reassociation pass should give us this form.
2706 Value *OtherMul0, *OtherMul1;
2707 if (match(Op0, m_FMul(m_Value(OtherMul0), m_Value(OtherMul1)))) {
2708 // Pattern: sqrt((x * y) * z)
2709 if (OtherMul0 == OtherMul1 && cast<Instruction>(Op0)->isFast()) {
2710 // Matched: sqrt((x * x) * z)
2711 RepeatOp = OtherMul0;
2712 OtherOp = Op1;
2713 }
2714 }
2715 }
2716 if (!RepeatOp)
2717 return Ret;
2718
2719 // Fast math flags for any created instructions should match the sqrt
2720 // and multiply.
2722 B.setFastMathFlags(I->getFastMathFlags());
2723
2724 // If we found a repeated factor, hoist it out of the square root and
2725 // replace it with the fabs of that factor.
2726 Value *FabsCall =
2727 B.CreateUnaryIntrinsic(Intrinsic::fabs, RepeatOp, nullptr, "fabs");
2728 if (OtherOp) {
2729 // If we found a non-repeated factor, we still need to get its square
2730 // root. We then multiply that by the value that was simplified out
2731 // of the square root calculation.
2732 Value *SqrtCall =
2733 B.CreateUnaryIntrinsic(Intrinsic::sqrt, OtherOp, nullptr, "sqrt");
2734 return copyFlags(*CI, B.CreateFMul(FabsCall, SqrtCall));
2735 }
2736 return copyFlags(*CI, FabsCall);
2737}
2738
2739Value *LibCallSimplifier::optimizeTrigInversionPairs(CallInst *CI,
2740 IRBuilderBase &B) {
2741 Module *M = CI->getModule();
2743 Value *Ret = nullptr;
2744 StringRef Name = Callee->getName();
2745 if (UnsafeFPShrink &&
2746 (Name == "tan" || Name == "atanh" || Name == "sinh" || Name == "cosh" ||
2747 Name == "asinh") &&
2748 hasFloatVersion(M, Name))
2749 Ret = optimizeUnaryDoubleFP(CI, B, TLI, true);
2750
2751 Value *Op1 = CI->getArgOperand(0);
2752 auto *OpC = dyn_cast<CallInst>(Op1);
2753 if (!OpC)
2754 return Ret;
2755
2756 // Both calls must be 'fast' in order to remove them.
2757 if (!CI->isFast() || !OpC->isFast())
2758 return Ret;
2759
2760 // tan(atan(x)) -> x
2761 // atanh(tanh(x)) -> x
2762 // sinh(asinh(x)) -> x
2763 // asinh(sinh(x)) -> x
2764 // cosh(acosh(x)) -> x
2765 LibFunc Func;
2766 Function *F = OpC->getCalledFunction();
2767 if (F && TLI->getLibFunc(F->getName(), Func) &&
2768 isLibFuncEmittable(M, TLI, Func)) {
2769 LibFunc inverseFunc = llvm::StringSwitch<LibFunc>(Callee->getName())
2770 .Case("tan", LibFunc_atan)
2771 .Case("atanh", LibFunc_tanh)
2772 .Case("sinh", LibFunc_asinh)
2773 .Case("cosh", LibFunc_acosh)
2774 .Case("tanf", LibFunc_atanf)
2775 .Case("atanhf", LibFunc_tanhf)
2776 .Case("sinhf", LibFunc_asinhf)
2777 .Case("coshf", LibFunc_acoshf)
2778 .Case("tanl", LibFunc_atanl)
2779 .Case("atanhl", LibFunc_tanhl)
2780 .Case("sinhl", LibFunc_asinhl)
2781 .Case("coshl", LibFunc_acoshl)
2782 .Case("asinh", LibFunc_sinh)
2783 .Case("asinhf", LibFunc_sinhf)
2784 .Case("asinhl", LibFunc_sinhl)
2785 .Default(NumLibFuncs); // Used as error value
2786 if (Func == inverseFunc)
2787 Ret = OpC->getArgOperand(0);
2788 }
2789 return Ret;
2790}
2791
2792static bool isTrigLibCall(CallInst *CI) {
2793 // We can only hope to do anything useful if we can ignore things like errno
2794 // and floating-point exceptions.
2795 // We already checked the prototype.
2796 return CI->doesNotThrow() && CI->doesNotAccessMemory();
2797}
2798
2799static bool insertSinCosCall(IRBuilderBase &B, Function *OrigCallee, Value *Arg,
2800 bool UseFloat, Value *&Sin, Value *&Cos,
2801 Value *&SinCos, const TargetLibraryInfo *TLI) {
2802 Module *M = OrigCallee->getParent();
2803 Type *ArgTy = Arg->getType();
2804 Type *ResTy;
2806
2807 Triple T(OrigCallee->getParent()->getTargetTriple());
2808 if (UseFloat) {
2809 Name = "__sincospif_stret";
2810
2811 assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");
2812 // x86_64 can't use {float, float} since that would be returned in both
2813 // xmm0 and xmm1, which isn't what a real struct would do.
2814 ResTy = T.getArch() == Triple::x86_64
2815 ? static_cast<Type *>(FixedVectorType::get(ArgTy, 2))
2816 : static_cast<Type *>(StructType::get(ArgTy, ArgTy));
2817 } else {
2818 Name = "__sincospi_stret";
2819 ResTy = StructType::get(ArgTy, ArgTy);
2820 }
2821
2822 if (!isLibFuncEmittable(M, TLI, Name))
2823 return false;
2824 LibFunc TheLibFunc;
2825 TLI->getLibFunc(Name, TheLibFunc);
2827 M, *TLI, TheLibFunc, OrigCallee->getAttributes(), ResTy, ArgTy);
2828
2829 if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
2830 // If the argument is an instruction, it must dominate all uses so put our
2831 // sincos call there.
2832 B.SetInsertPoint(ArgInst->getParent(), ++ArgInst->getIterator());
2833 } else {
2834 // Otherwise (e.g. for a constant) the beginning of the function is as
2835 // good a place as any.
2836 BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
2837 B.SetInsertPoint(&EntryBB, EntryBB.begin());
2838 }
2839
2840 SinCos = B.CreateCall(Callee, Arg, "sincospi");
2841
2842 if (SinCos->getType()->isStructTy()) {
2843 Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
2844 Cos = B.CreateExtractValue(SinCos, 1, "cospi");
2845 } else {
2846 Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
2847 "sinpi");
2848 Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
2849 "cospi");
2850 }
2851
2852 return true;
2853}
2854
2855static Value *optimizeSymmetricCall(CallInst *CI, bool IsEven,
2856 IRBuilderBase &B) {
2857 Value *X;
2858 Value *Src = CI->getArgOperand(0);
2859
2860 if (match(Src, m_OneUse(m_FNeg(m_Value(X))))) {
2862 B.setFastMathFlags(CI->getFastMathFlags());
2863
2864 auto *CallInst = copyFlags(*CI, B.CreateCall(CI->getCalledFunction(), {X}));
2865 if (IsEven) {
2866 // Even function: f(-x) = f(x)
2867 return CallInst;
2868 }
2869 // Odd function: f(-x) = -f(x)
2870 return B.CreateFNeg(CallInst);
2871 }
2872
2873 // Even function: f(abs(x)) = f(x), f(copysign(x, y)) = f(x)
2874 if (IsEven && (match(Src, m_FAbs(m_Value(X))) ||
2875 match(Src, m_CopySign(m_Value(X), m_Value())))) {
2877 B.setFastMathFlags(CI->getFastMathFlags());
2878
2879 auto *CallInst = copyFlags(*CI, B.CreateCall(CI->getCalledFunction(), {X}));
2880 return CallInst;
2881 }
2882
2883 return nullptr;
2884}
2885
2886Value *LibCallSimplifier::optimizeSymmetric(CallInst *CI, LibFunc Func,
2887 IRBuilderBase &B) {
2888 switch (Func) {
2889 case LibFunc_cos:
2890 case LibFunc_cosf:
2891 case LibFunc_cosl:
2892 return optimizeSymmetricCall(CI, /*IsEven*/ true, B);
2893
2894 case LibFunc_sin:
2895 case LibFunc_sinf:
2896 case LibFunc_sinl:
2897
2898 case LibFunc_tan:
2899 case LibFunc_tanf:
2900 case LibFunc_tanl:
2901
2902 case LibFunc_erf:
2903 case LibFunc_erff:
2904 case LibFunc_erfl:
2905 return optimizeSymmetricCall(CI, /*IsEven*/ false, B);
2906
2907 default:
2908 return nullptr;
2909 }
2910}
2911
2912Value *LibCallSimplifier::optimizeSinCosPi(CallInst *CI, bool IsSin, IRBuilderBase &B) {
2913 // Make sure the prototype is as expected, otherwise the rest of the
2914 // function is probably invalid and likely to abort.
2915 if (!isTrigLibCall(CI))
2916 return nullptr;
2917
2918 Value *Arg = CI->getArgOperand(0);
2921 SmallVector<CallInst *, 1> SinCosCalls;
2922
2923 bool IsFloat = Arg->getType()->isFloatTy();
2924
2925 // Look for all compatible sinpi, cospi and sincospi calls with the same
2926 // argument. If there are enough (in some sense) we can make the
2927 // substitution.
2928 Function *F = CI->getFunction();
2929 for (User *U : Arg->users())
2930 classifyArgUse(U, F, IsFloat, SinCalls, CosCalls, SinCosCalls);
2931
2932 // It's only worthwhile if both sinpi and cospi are actually used.
2933 if (SinCalls.empty() || CosCalls.empty())
2934 return nullptr;
2935
2936 Value *Sin, *Cos, *SinCos;
2937 if (!insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos,
2938 SinCos, TLI))
2939 return nullptr;
2940
2941 auto replaceTrigInsts = [this](SmallVectorImpl<CallInst *> &Calls,
2942 Value *Res) {
2943 for (CallInst *C : Calls)
2944 replaceAllUsesWith(C, Res);
2945 };
2946
2947 replaceTrigInsts(SinCalls, Sin);
2948 replaceTrigInsts(CosCalls, Cos);
2949 replaceTrigInsts(SinCosCalls, SinCos);
2950
2951 return IsSin ? Sin : Cos;
2952}
2953
2954void LibCallSimplifier::classifyArgUse(
2955 Value *Val, Function *F, bool IsFloat,
2958 SmallVectorImpl<CallInst *> &SinCosCalls) {
2959 auto *CI = dyn_cast<CallInst>(Val);
2960 if (!CI || CI->use_empty())
2961 return;
2962
2963 // Don't consider calls in other functions.
2964 if (CI->getFunction() != F)
2965 return;
2966
2967 Module *M = CI->getModule();
2969 LibFunc Func;
2970 if (!Callee || !TLI->getLibFunc(*Callee, Func) ||
2971 !isLibFuncEmittable(M, TLI, Func) ||
2972 !isTrigLibCall(CI))
2973 return;
2974
2975 if (IsFloat) {
2976 if (Func == LibFunc_sinpif)
2977 SinCalls.push_back(CI);
2978 else if (Func == LibFunc_cospif)
2979 CosCalls.push_back(CI);
2980 else if (Func == LibFunc_sincospif_stret)
2981 SinCosCalls.push_back(CI);
2982 } else {
2983 if (Func == LibFunc_sinpi)
2984 SinCalls.push_back(CI);
2985 else if (Func == LibFunc_cospi)
2986 CosCalls.push_back(CI);
2987 else if (Func == LibFunc_sincospi_stret)
2988 SinCosCalls.push_back(CI);
2989 }
2990}
2991
2992//===----------------------------------------------------------------------===//
2993// Integer Library Call Optimizations
2994//===----------------------------------------------------------------------===//
2995
2996Value *LibCallSimplifier::optimizeFFS(CallInst *CI, IRBuilderBase &B) {
2997 // All variants of ffs return int which need not be 32 bits wide.
2998 // ffs{,l,ll}(x) -> x != 0 ? (int)llvm.cttz(x)+1 : 0
2999 Type *RetType = CI->getType();
3000 Value *Op = CI->getArgOperand(0);
3001 Type *ArgType = Op->getType();
3002 Value *V = B.CreateIntrinsic(Intrinsic::cttz, {ArgType}, {Op, B.getTrue()},
3003 nullptr, "cttz");
3004 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
3005 V = B.CreateIntCast(V, RetType, false);
3006
3007 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
3008 return B.CreateSelect(Cond, V, ConstantInt::get(RetType, 0));
3009}
3010
3011Value *LibCallSimplifier::optimizeFls(CallInst *CI, IRBuilderBase &B) {
3012 // All variants of fls return int which need not be 32 bits wide.
3013 // fls{,l,ll}(x) -> (int)(sizeInBits(x) - llvm.ctlz(x, false))
3014 Value *Op = CI->getArgOperand(0);
3015 Type *ArgType = Op->getType();
3016 Value *V = B.CreateIntrinsic(Intrinsic::ctlz, {ArgType}, {Op, B.getFalse()},
3017 nullptr, "ctlz");
3018 V = B.CreateSub(ConstantInt::get(V->getType(), ArgType->getIntegerBitWidth()),
3019 V);
3020 return B.CreateIntCast(V, CI->getType(), false);
3021}
3022
3023Value *LibCallSimplifier::optimizeAbs(CallInst *CI, IRBuilderBase &B) {
3024 // abs(x) -> x <s 0 ? -x : x
3025 // The negation has 'nsw' because abs of INT_MIN is undefined.
3026 Value *X = CI->getArgOperand(0);
3027 Value *IsNeg = B.CreateIsNeg(X);
3028 Value *NegX = B.CreateNSWNeg(X, "neg");
3029 return B.CreateSelect(IsNeg, NegX, X);
3030}
3031
3032Value *LibCallSimplifier::optimizeIsDigit(CallInst *CI, IRBuilderBase &B) {
3033 // isdigit(c) -> (c-'0') <u 10
3034 Value *Op = CI->getArgOperand(0);
3035 Type *ArgType = Op->getType();
3036 Op = B.CreateSub(Op, ConstantInt::get(ArgType, '0'), "isdigittmp");
3037 Op = B.CreateICmpULT(Op, ConstantInt::get(ArgType, 10), "isdigit");
3038 return B.CreateZExt(Op, CI->getType());
3039}
3040
3041Value *LibCallSimplifier::optimizeIsAscii(CallInst *CI, IRBuilderBase &B) {
3042 // isascii(c) -> c <u 128
3043 Value *Op = CI->getArgOperand(0);
3044 Type *ArgType = Op->getType();
3045 Op = B.CreateICmpULT(Op, ConstantInt::get(ArgType, 128), "isascii");
3046 return B.CreateZExt(Op, CI->getType());
3047}
3048
3049Value *LibCallSimplifier::optimizeToAscii(CallInst *CI, IRBuilderBase &B) {
3050 // toascii(c) -> c & 0x7f
3051 return B.CreateAnd(CI->getArgOperand(0),
3052 ConstantInt::get(CI->getType(), 0x7F));
3053}
3054
3055// Fold calls to atoi, atol, and atoll.
3056Value *LibCallSimplifier::optimizeAtoi(CallInst *CI, IRBuilderBase &B) {
3057 CI->addParamAttr(0, Attribute::NoCapture);
3058
3059 StringRef Str;
3060 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
3061 return nullptr;
3062
3063 return convertStrToInt(CI, Str, nullptr, 10, /*AsSigned=*/true, B);
3064}
3065
3066// Fold calls to strtol, strtoll, strtoul, and strtoull.
3067Value *LibCallSimplifier::optimizeStrToInt(CallInst *CI, IRBuilderBase &B,
3068 bool AsSigned) {
3069 Value *EndPtr = CI->getArgOperand(1);
3070 if (isa<ConstantPointerNull>(EndPtr)) {
3071 // With a null EndPtr, this function won't capture the main argument.
3072 // It would be readonly too, except that it still may write to errno.
3073 CI->addParamAttr(0, Attribute::NoCapture);
3074 EndPtr = nullptr;
3075 } else if (!isKnownNonZero(EndPtr, DL))
3076 return nullptr;
3077
3078 StringRef Str;
3079 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
3080 return nullptr;
3081
3082 if (ConstantInt *CInt = dyn_cast<ConstantInt>(CI->getArgOperand(2))) {
3083 return convertStrToInt(CI, Str, EndPtr, CInt->getSExtValue(), AsSigned, B);
3084 }
3085
3086 return nullptr;
3087}
3088
3089//===----------------------------------------------------------------------===//
3090// Formatting and IO Library Call Optimizations
3091//===----------------------------------------------------------------------===//
3092
3093static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg);
3094
3095Value *LibCallSimplifier::optimizeErrorReporting(CallInst *CI, IRBuilderBase &B,
3096 int StreamArg) {
3098 // Error reporting calls should be cold, mark them as such.
3099 // This applies even to non-builtin calls: it is only a hint and applies to
3100 // functions that the frontend might not understand as builtins.
3101
3102 // This heuristic was suggested in:
3103 // Improving Static Branch Prediction in a Compiler
3104 // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu
3105 // Proceedings of PACT'98, Oct. 1998, IEEE
3106 if (!CI->hasFnAttr(Attribute::Cold) &&
3107 isReportingError(Callee, CI, StreamArg)) {
3108 CI->addFnAttr(Attribute::Cold);
3109 }
3110
3111 return nullptr;
3112}
3113
3114static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg) {
3115 if (!Callee || !Callee->isDeclaration())
3116 return false;
3117
3118 if (StreamArg < 0)
3119 return true;
3120
3121 // These functions might be considered cold, but only if their stream
3122 // argument is stderr.
3123
3124 if (StreamArg >= (int)CI->arg_size())
3125 return false;
3126 LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
3127 if (!LI)
3128 return false;
3129 GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
3130 if (!GV || !GV->isDeclaration())
3131 return false;
3132 return GV->getName() == "stderr";
3133}
3134
3135Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
3136 // Check for a fixed format string.
3137 StringRef FormatStr;
3138 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
3139 return nullptr;
3140
3141 // Empty format string -> noop.
3142 if (FormatStr.empty()) // Tolerate printf's declared void.
3143 return CI->use_empty() ? (Value *)CI : ConstantInt::get(CI->getType(), 0);
3144
3145 // Do not do any of the following transformations if the printf return value
3146 // is used, in general the printf return value is not compatible with either
3147 // putchar() or puts().
3148 if (!CI->use_empty())
3149 return nullptr;
3150
3151 Type *IntTy = CI->getType();
3152 // printf("x") -> putchar('x'), even for "%" and "%%".
3153 if (FormatStr.size() == 1 || FormatStr == "%%") {
3154 // Convert the character to unsigned char before passing it to putchar
3155 // to avoid host-specific sign extension in the IR. Putchar converts
3156 // it to unsigned char regardless.
3157 Value *IntChar = ConstantInt::get(IntTy, (unsigned char)FormatStr[0]);
3158 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3159 }
3160
3161 // Try to remove call or emit putchar/puts.
3162 if (FormatStr == "%s" && CI->arg_size() > 1) {
3163 StringRef OperandStr;
3164 if (!getConstantStringInfo(CI->getOperand(1), OperandStr))
3165 return nullptr;
3166 // printf("%s", "") --> NOP
3167 if (OperandStr.empty())
3168 return (Value *)CI;
3169 // printf("%s", "a") --> putchar('a')
3170 if (OperandStr.size() == 1) {
3171 // Convert the character to unsigned char before passing it to putchar
3172 // to avoid host-specific sign extension in the IR. Putchar converts
3173 // it to unsigned char regardless.
3174 Value *IntChar = ConstantInt::get(IntTy, (unsigned char)OperandStr[0]);
3175 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3176 }
3177 // printf("%s", str"\n") --> puts(str)
3178 if (OperandStr.back() == '\n') {
3179 OperandStr = OperandStr.drop_back();
3180 Value *GV = B.CreateGlobalString(OperandStr, "str");
3181 return copyFlags(*CI, emitPutS(GV, B, TLI));
3182 }
3183 return nullptr;
3184 }
3185
3186 // printf("foo\n") --> puts("foo")
3187 if (FormatStr.back() == '\n' &&
3188 !FormatStr.contains('%')) { // No format characters.
3189 // Create a string literal with no \n on it. We expect the constant merge
3190 // pass to be run after this pass, to merge duplicate strings.
3191 FormatStr = FormatStr.drop_back();
3192 Value *GV = B.CreateGlobalString(FormatStr, "str");
3193 return copyFlags(*CI, emitPutS(GV, B, TLI));
3194 }
3195
3196 // Optimize specific format strings.
3197 // printf("%c", chr) --> putchar(chr)
3198 if (FormatStr == "%c" && CI->arg_size() > 1 &&
3199 CI->getArgOperand(1)->getType()->isIntegerTy()) {
3200 // Convert the argument to the type expected by putchar, i.e., int, which
3201 // need not be 32 bits wide but which is the same as printf's return type.
3202 Value *IntChar = B.CreateIntCast(CI->getArgOperand(1), IntTy, false);
3203 return copyFlags(*CI, emitPutChar(IntChar, B, TLI));
3204 }
3205
3206 // printf("%s\n", str) --> puts(str)
3207 if (FormatStr == "%s\n" && CI->arg_size() > 1 &&
3208 CI->getArgOperand(1)->getType()->isPointerTy())
3209 return copyFlags(*CI, emitPutS(CI->getArgOperand(1), B, TLI));
3210 return nullptr;
3211}
3212
3213Value *LibCallSimplifier::optimizePrintF(CallInst *CI, IRBuilderBase &B) {
3214
3215 Module *M = CI->getModule();
3217 FunctionType *FT = Callee->getFunctionType();
3218 if (Value *V = optimizePrintFString(CI, B)) {
3219 return V;
3220 }
3221
3223
3224 // printf(format, ...) -> iprintf(format, ...) if no floating point
3225 // arguments.
3226 if (isLibFuncEmittable(M, TLI, LibFunc_iprintf) &&
3228 FunctionCallee IPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_iprintf, FT,
3229 Callee->getAttributes());
3230 CallInst *New = cast<CallInst>(CI->clone());
3231 New->setCalledFunction(IPrintFFn);
3232 B.Insert(New);
3233 return New;
3234 }
3235
3236 // printf(format, ...) -> __small_printf(format, ...) if no 128-bit floating point
3237 // arguments.
3238 if (isLibFuncEmittable(M, TLI, LibFunc_small_printf) &&
3239 !callHasFP128Argument(CI)) {
3240 auto SmallPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_small_printf, FT,
3241 Callee->getAttributes());
3242 CallInst *New = cast<CallInst>(CI->clone());
3243 New->setCalledFunction(SmallPrintFFn);
3244 B.Insert(New);
3245 return New;
3246 }
3247
3248 return nullptr;
3249}
3250
3251Value *LibCallSimplifier::optimizeSPrintFString(CallInst *CI,
3252 IRBuilderBase &B) {
3253 // Check for a fixed format string.
3254 StringRef FormatStr;
3255 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
3256 return nullptr;
3257
3258 // If we just have a format string (nothing else crazy) transform it.
3259 Value *Dest = CI->getArgOperand(0);
3260 if (CI->arg_size() == 2) {
3261 // Make sure there's no % in the constant array. We could try to handle
3262 // %% -> % in the future if we cared.
3263 if (FormatStr.contains('%'))
3264 return nullptr; // we found a format specifier, bail out.
3265
3266 // sprintf(str, fmt) -> llvm.memcpy(align 1 str, align 1 fmt, strlen(fmt)+1)
3267 B.CreateMemCpy(
3268 Dest, Align(1), CI->getArgOperand(1), Align(1),
3269 ConstantInt::get(DL.getIntPtrType(CI->getContext()),
3270 FormatStr.size() + 1)); // Copy the null byte.
3271 return ConstantInt::get(CI->getType(), FormatStr.size());
3272 }
3273
3274 // The remaining optimizations require the format string to be "%s" or "%c"
3275 // and have an extra operand.
3276 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
3277 return nullptr;
3278
3279 // Decode the second character of the format string.
3280 if (FormatStr[1] == 'c') {
3281 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
3282 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
3283 return nullptr;
3284 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
3285 Value *Ptr = Dest;
3286 B.CreateStore(V, Ptr);
3287 Ptr = B.CreateInBoundsGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
3288 B.CreateStore(B.getInt8(0), Ptr);
3289
3290 return ConstantInt::get(CI->getType(), 1);
3291 }
3292
3293 if (FormatStr[1] == 's') {
3294 // sprintf(dest, "%s", str) -> llvm.memcpy(align 1 dest, align 1 str,
3295 // strlen(str)+1)
3296 if (!CI->getArgOperand(2)->getType()->isPointerTy())
3297 return nullptr;
3298
3299 if (CI->use_empty())
3300 // sprintf(dest, "%s", str) -> strcpy(dest, str)
3301 return copyFlags(*CI, emitStrCpy(Dest, CI->getArgOperand(2), B, TLI));
3302
3303 uint64_t SrcLen = GetStringLength(CI->getArgOperand(2));
3304 if (SrcLen) {
3305 B.CreateMemCpy(
3306 Dest, Align(1), CI->getArgOperand(2), Align(1),
3307 ConstantInt::get(DL.getIntPtrType(CI->getContext()), SrcLen));
3308 // Returns total number of characters written without null-character.
3309 return ConstantInt::get(CI->getType(), SrcLen - 1);
3310 } else if (Value *V = emitStpCpy(Dest, CI->getArgOperand(2), B, TLI)) {
3311 // sprintf(dest, "%s", str) -> stpcpy(dest, str) - dest
3312 Value *PtrDiff = B.CreatePtrDiff(B.getInt8Ty(), V, Dest);
3313 return B.CreateIntCast(PtrDiff, CI->getType(), false);
3314 }
3315
3316 bool OptForSize = CI->getFunction()->hasOptSize() ||
3317 llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3319 if (OptForSize)
3320 return nullptr;
3321
3322 Value *Len = emitStrLen(CI->getArgOperand(2), B, DL, TLI);
3323 if (!Len)
3324 return nullptr;
3325 Value *IncLen =
3326 B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1), "leninc");
3327 B.CreateMemCpy(Dest, Align(1), CI->getArgOperand(2), Align(1), IncLen);
3328
3329 // The sprintf result is the unincremented number of bytes in the string.
3330 return B.CreateIntCast(Len, CI->getType(), false);
3331 }
3332 return nullptr;
3333}
3334
3335Value *LibCallSimplifier::optimizeSPrintF(CallInst *CI, IRBuilderBase &B) {
3336 Module *M = CI->getModule();
3338 FunctionType *FT = Callee->getFunctionType();
3339 if (Value *V = optimizeSPrintFString(CI, B)) {
3340 return V;
3341 }
3342
3344
3345 // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
3346 // point arguments.
3347 if (isLibFuncEmittable(M, TLI, LibFunc_siprintf) &&
3349 FunctionCallee SIPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_siprintf,
3350 FT, Callee->getAttributes());
3351 CallInst *New = cast<CallInst>(CI->clone());
3352 New->setCalledFunction(SIPrintFFn);
3353 B.Insert(New);
3354 return New;
3355 }
3356
3357 // sprintf(str, format, ...) -> __small_sprintf(str, format, ...) if no 128-bit
3358 // floating point arguments.
3359 if (isLibFuncEmittable(M, TLI, LibFunc_small_sprintf) &&
3360 !callHasFP128Argument(CI)) {
3361 auto SmallSPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_small_sprintf, FT,
3362 Callee->getAttributes());
3363 CallInst *New = cast<CallInst>(CI->clone());
3364 New->setCalledFunction(SmallSPrintFFn);
3365 B.Insert(New);
3366 return New;
3367 }
3368
3369 return nullptr;
3370}
3371
3372// Transform an snprintf call CI with the bound N to format the string Str
3373// either to a call to memcpy, or to single character a store, or to nothing,
3374// and fold the result to a constant. A nonnull StrArg refers to the string
3375// argument being formatted. Otherwise the call is one with N < 2 and
3376// the "%c" directive to format a single character.
3377Value *LibCallSimplifier::emitSnPrintfMemCpy(CallInst *CI, Value *StrArg,
3378 StringRef Str, uint64_t N,
3379 IRBuilderBase &B) {
3380 assert(StrArg || (N < 2 && Str.size() == 1));
3381
3382 unsigned IntBits = TLI->getIntSize();
3383 uint64_t IntMax = maxIntN(IntBits);
3384 if (Str.size() > IntMax)
3385 // Bail if the string is longer than INT_MAX. POSIX requires
3386 // implementations to set errno to EOVERFLOW in this case, in
3387 // addition to when N is larger than that (checked by the caller).
3388 return nullptr;
3389
3390 Value *StrLen = ConstantInt::get(CI->getType(), Str.size());
3391 if (N == 0)
3392 return StrLen;
3393
3394 // Set to the number of bytes to copy fron StrArg which is also
3395 // the offset of the terinating nul.
3396 uint64_t NCopy;
3397 if (N > Str.size())
3398 // Copy the full string, including the terminating nul (which must
3399 // be present regardless of the bound).
3400 NCopy = Str.size() + 1;
3401 else
3402 NCopy = N - 1;
3403
3404 Value *DstArg = CI->getArgOperand(0);
3405 if (NCopy && StrArg)
3406 // Transform the call to lvm.memcpy(dst, fmt, N).
3407 copyFlags(
3408 *CI,
3409 B.CreateMemCpy(
3410 DstArg, Align(1), StrArg, Align(1),
3411 ConstantInt::get(DL.getIntPtrType(CI->getContext()), NCopy)));
3412
3413 if (N > Str.size())
3414 // Return early when the whole format string, including the final nul,
3415 // has been copied.
3416 return StrLen;
3417
3418 // Otherwise, when truncating the string append a terminating nul.
3419 Type *Int8Ty = B.getInt8Ty();
3420 Value *NulOff = B.getIntN(IntBits, NCopy);
3421 Value *DstEnd = B.CreateInBoundsGEP(Int8Ty, DstArg, NulOff, "endptr");
3422 B.CreateStore(ConstantInt::get(Int8Ty, 0), DstEnd);
3423 return StrLen;
3424}
3425
3426Value *LibCallSimplifier::optimizeSnPrintFString(CallInst *CI,
3427 IRBuilderBase &B) {
3428 // Check for size
3429 ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(1));
3430 if (!Size)
3431 return nullptr;
3432
3433 uint64_t N = Size->getZExtValue();
3434 uint64_t IntMax = maxIntN(TLI->getIntSize());
3435 if (N > IntMax)
3436 // Bail if the bound exceeds INT_MAX. POSIX requires implementations
3437 // to set errno to EOVERFLOW in this case.
3438 return nullptr;
3439
3440 Value *DstArg = CI->getArgOperand(0);
3441 Value *FmtArg = CI->getArgOperand(2);
3442
3443 // Check for a fixed format string.
3444 StringRef FormatStr;
3445 if (!getConstantStringInfo(FmtArg, FormatStr))
3446 return nullptr;
3447
3448 // If we just have a format string (nothing else crazy) transform it.
3449 if (CI->arg_size() == 3) {
3450 if (FormatStr.contains('%'))
3451 // Bail if the format string contains a directive and there are
3452 // no arguments. We could handle "%%" in the future.
3453 return nullptr;
3454
3455 return emitSnPrintfMemCpy(CI, FmtArg, FormatStr, N, B);
3456 }
3457
3458 // The remaining optimizations require the format string to be "%s" or "%c"
3459 // and have an extra operand.
3460 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() != 4)
3461 return nullptr;
3462
3463 // Decode the second character of the format string.
3464 if (FormatStr[1] == 'c') {
3465 if (N <= 1) {
3466 // Use an arbitary string of length 1 to transform the call into
3467 // either a nul store (N == 1) or a no-op (N == 0) and fold it
3468 // to one.
3469 StringRef CharStr("*");
3470 return emitSnPrintfMemCpy(CI, nullptr, CharStr, N, B);
3471 }
3472
3473 // snprintf(dst, size, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
3474 if (!CI->getArgOperand(3)->getType()->isIntegerTy())
3475 return nullptr;
3476 Value *V = B.CreateTrunc(CI->getArgOperand(3), B.getInt8Ty(), "char");
3477 Value *Ptr = DstArg;
3478 B.CreateStore(V, Ptr);
3479 Ptr = B.CreateInBoundsGEP(B.getInt8Ty(), Ptr, B.getInt32(1), "nul");
3480 B.CreateStore(B.getInt8(0), Ptr);
3481 return ConstantInt::get(CI->getType(), 1);
3482 }
3483
3484 if (FormatStr[1] != 's')
3485 return nullptr;
3486
3487 Value *StrArg = CI->getArgOperand(3);
3488 // snprintf(dest, size, "%s", str) to llvm.memcpy(dest, str, len+1, 1)
3489 StringRef Str;
3490 if (!getConstantStringInfo(StrArg, Str))
3491 return nullptr;
3492
3493 return emitSnPrintfMemCpy(CI, StrArg, Str, N, B);
3494}
3495
3496Value *LibCallSimplifier::optimizeSnPrintF(CallInst *CI, IRBuilderBase &B) {
3497 if (Value *V = optimizeSnPrintFString(CI, B)) {
3498 return V;
3499 }
3500
3501 if (isKnownNonZero(CI->getOperand(1), DL))
3503 return nullptr;
3504}
3505
3506Value *LibCallSimplifier::optimizeFPrintFString(CallInst *CI,
3507 IRBuilderBase &B) {
3508 optimizeErrorReporting(CI, B, 0);
3509
3510 // All the optimizations depend on the format string.
3511 StringRef FormatStr;
3512 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
3513 return nullptr;
3514
3515 // Do not do any of the following transformations if the fprintf return
3516 // value is used, in general the fprintf return value is not compatible
3517 // with fwrite(), fputc() or fputs().
3518 if (!CI->use_empty())
3519 return nullptr;
3520
3521 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
3522 if (CI->arg_size() == 2) {
3523 // Could handle %% -> % if we cared.
3524 if (FormatStr.contains('%'))
3525 return nullptr; // We found a format specifier.
3526
3527 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
3528 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
3529 return copyFlags(
3530 *CI, emitFWrite(CI->getArgOperand(1),
3531 ConstantInt::get(SizeTTy, FormatStr.size()),
3532 CI->getArgOperand(0), B, DL, TLI));
3533 }
3534
3535 // The remaining optimizations require the format string to be "%s" or "%c"
3536 // and have an extra operand.
3537 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->arg_size() < 3)
3538 return nullptr;
3539
3540 // Decode the second character of the format string.
3541 if (FormatStr[1] == 'c') {
3542 // fprintf(F, "%c", chr) --> fputc((int)chr, F)
3543 if (!CI->getArgOperand(2)->getType()->isIntegerTy())
3544 return nullptr;
3545 Type *IntTy = B.getIntNTy(TLI->getIntSize());
3546 Value *V = B.CreateIntCast(CI->getArgOperand(2), IntTy, /*isSigned*/ true,
3547 "chari");
3548 return copyFlags(*CI, emitFPutC(V, CI->getArgOperand(0), B, TLI));
3549 }
3550
3551 if (FormatStr[1] == 's') {
3552 // fprintf(F, "%s", str) --> fputs(str, F)
3553 if (!CI->getArgOperand(2)->getType()->isPointerTy())
3554 return nullptr;
3555 return copyFlags(
3556 *CI, emitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TLI));
3557 }
3558 return nullptr;
3559}
3560
3561Value *LibCallSimplifier::optimizeFPrintF(CallInst *CI, IRBuilderBase &B) {
3562 Module *M = CI->getModule();
3564 FunctionType *FT = Callee->getFunctionType();
3565 if (Value *V = optimizeFPrintFString(CI, B)) {
3566 return V;
3567 }
3568
3569 // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
3570 // floating point arguments.
3571 if (isLibFuncEmittable(M, TLI, LibFunc_fiprintf) &&
3573 FunctionCallee FIPrintFFn = getOrInsertLibFunc(M, *TLI, LibFunc_fiprintf,
3574 FT, Callee->getAttributes());
3575 CallInst *New = cast<CallInst>(CI->clone());
3576 New->setCalledFunction(FIPrintFFn);
3577 B.Insert(New);
3578 return New;
3579 }
3580
3581 // fprintf(stream, format, ...) -> __small_fprintf(stream, format, ...) if no
3582 // 128-bit floating point arguments.
3583 if (isLibFuncEmittable(M, TLI, LibFunc_small_fprintf) &&
3584 !callHasFP128Argument(CI)) {
3585 auto SmallFPrintFFn =
3586 getOrInsertLibFunc(M, *TLI, LibFunc_small_fprintf, FT,
3587 Callee->getAttributes());
3588 CallInst *New = cast<CallInst>(CI->clone());
3589 New->setCalledFunction(SmallFPrintFFn);
3590 B.Insert(New);
3591 return New;
3592 }
3593
3594 return nullptr;
3595}
3596
3597Value *LibCallSimplifier::optimizeFWrite(CallInst *CI, IRBuilderBase &B) {
3598 optimizeErrorReporting(CI, B, 3);
3599
3600 // Get the element size and count.
3601 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
3602 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
3603 if (SizeC && CountC) {
3604 uint64_t Bytes = SizeC->getZExtValue() * CountC->getZExtValue();
3605
3606 // If this is writing zero records, remove the call (it's a noop).
3607 if (Bytes == 0)
3608 return ConstantInt::get(CI->getType(), 0);
3609
3610 // If this is writing one byte, turn it into fputc.
3611 // This optimisation is only valid, if the return value is unused.
3612 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
3613 Value *Char = B.CreateLoad(B.getInt8Ty(), CI->getArgOperand(0), "char");
3614 Type *IntTy = B.getIntNTy(TLI->getIntSize());
3615 Value *Cast = B.CreateIntCast(Char, IntTy, /*isSigned*/ true, "chari");
3616 Value *NewCI = emitFPutC(Cast, CI->getArgOperand(3), B, TLI);
3617 return NewCI ? ConstantInt::get(CI->getType(), 1) : nullptr;
3618 }
3619 }
3620
3621 return nullptr;
3622}
3623
3624Value *LibCallSimplifier::optimizeFPuts(CallInst *CI, IRBuilderBase &B) {
3625 optimizeErrorReporting(CI, B, 1);
3626
3627 // Don't rewrite fputs to fwrite when optimising for size because fwrite
3628 // requires more arguments and thus extra MOVs are required.
3629 bool OptForSize = CI->getFunction()->hasOptSize() ||
3630 llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI,
3632 if (OptForSize)
3633 return nullptr;
3634
3635 // We can't optimize if return value is used.
3636 if (!CI->use_empty())
3637 return nullptr;
3638
3639 // fputs(s,F) --> fwrite(s,strlen(s),1,F)
3641 if (!Len)
3642 return nullptr;
3643
3644 // Known to have no uses (see above).
3645 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
3646 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
3647 return copyFlags(
3648 *CI,
3650 ConstantInt::get(SizeTTy, Len - 1),
3651 CI->getArgOperand(1), B, DL, TLI));
3652}
3653
3654Value *LibCallSimplifier::optimizePuts(CallInst *CI, IRBuilderBase &B) {
3656 if (!CI->use_empty())
3657 return nullptr;
3658
3659 // Check for a constant string.
3660 // puts("") -> putchar('\n')
3661 StringRef Str;
3662 if (getConstantStringInfo(CI->getArgOperand(0), Str) && Str.empty()) {
3663 // putchar takes an argument of the same type as puts returns, i.e.,
3664 // int, which need not be 32 bits wide.
3665 Type *IntTy = CI->getType();
3666 return copyFlags(*CI, emitPutChar(ConstantInt::get(IntTy, '\n'), B, TLI));
3667 }
3668
3669 return nullptr;
3670}
3671
3672Value *LibCallSimplifier::optimizeBCopy(CallInst *CI, IRBuilderBase &B) {
3673 // bcopy(src, dst, n) -> llvm.memmove(dst, src, n)
3674 return copyFlags(*CI, B.CreateMemMove(CI->getArgOperand(1), Align(1),
3675 CI->getArgOperand(0), Align(1),
3676 CI->getArgOperand(2)));
3677}
3678
3679bool LibCallSimplifier::hasFloatVersion(const Module *M, StringRef FuncName) {
3680 SmallString<20> FloatFuncName = FuncName;
3681 FloatFuncName += 'f';
3682 return isLibFuncEmittable(M, TLI, FloatFuncName);
3683}
3684
3685Value *LibCallSimplifier::optimizeStringMemoryLibCall(CallInst *CI,
3686 IRBuilderBase &Builder) {
3687 Module *M = CI->getModule();
3688 LibFunc Func;
3690 // Check for string/memory library functions.
3691 if (TLI->getLibFunc(*Callee, Func) && isLibFuncEmittable(M, TLI, Func)) {
3692 // Make sure we never change the calling convention.
3693 assert(
3694 (ignoreCallingConv(Func) ||
3696 "Optimizing string/memory libcall would change the calling convention");
3697 switch (Func) {
3698 case LibFunc_strcat:
3699 return optimizeStrCat(CI, Builder);
3700 case LibFunc_strncat:
3701 return optimizeStrNCat(CI, Builder);
3702 case LibFunc_strchr:
3703 return optimizeStrChr(CI, Builder);
3704 case LibFunc_strrchr:
3705 return optimizeStrRChr(CI, Builder);
3706 case LibFunc_strcmp:
3707 return optimizeStrCmp(CI, Builder);
3708 case LibFunc_strncmp:
3709 return optimizeStrNCmp(CI, Builder);
3710 case LibFunc_strcpy:
3711 return optimizeStrCpy(CI, Builder);
3712 case LibFunc_stpcpy:
3713 return optimizeStpCpy(CI, Builder);
3714 case LibFunc_strlcpy:
3715 return optimizeStrLCpy(CI, Builder);
3716 case LibFunc_stpncpy:
3717 return optimizeStringNCpy(CI, /*RetEnd=*/true, Builder);
3718 case LibFunc_strncpy:
3719 return optimizeStringNCpy(CI, /*RetEnd=*/false, Builder);
3720 case LibFunc_strlen:
3721 return optimizeStrLen(CI, Builder);
3722 case LibFunc_strnlen:
3723 return optimizeStrNLen(CI, Builder);
3724 case LibFunc_strpbrk:
3725 return optimizeStrPBrk(CI, Builder);
3726 case LibFunc_strndup:
3727 return optimizeStrNDup(CI, Builder);
3728 case LibFunc_strtol:
3729 case LibFunc_strtod:
3730 case LibFunc_strtof:
3731 case LibFunc_strtoul:
3732 case LibFunc_strtoll:
3733 case LibFunc_strtold:
3734 case LibFunc_strtoull:
3735 return optimizeStrTo(CI, Builder);
3736 case LibFunc_strspn:
3737 return optimizeStrSpn(CI, Builder);
3738 case LibFunc_strcspn:
3739 return optimizeStrCSpn(CI, Builder);
3740 case LibFunc_strstr:
3741 return optimizeStrStr(CI, Builder);
3742 case LibFunc_memchr:
3743 return optimizeMemChr(CI, Builder);
3744 case LibFunc_memrchr:
3745 return optimizeMemRChr(CI, Builder);
3746 case LibFunc_bcmp:
3747 return optimizeBCmp(CI, Builder);
3748 case LibFunc_memcmp:
3749 return optimizeMemCmp(CI, Builder);
3750 case LibFunc_memcpy:
3751 return optimizeMemCpy(CI, Builder);
3752 case LibFunc_memccpy:
3753 return optimizeMemCCpy(CI, Builder);
3754 case LibFunc_mempcpy:
3755 return optimizeMemPCpy(CI, Builder);
3756 case LibFunc_memmove:
3757 return optimizeMemMove(CI, Builder);
3758 case LibFunc_memset:
3759 return optimizeMemSet(CI, Builder);
3760 case LibFunc_realloc:
3761 return optimizeRealloc(CI, Builder);
3762 case LibFunc_wcslen:
3763 return optimizeWcslen(CI, Builder);
3764 case LibFunc_bcopy:
3765 return optimizeBCopy(CI, Builder);
3766 case LibFunc_Znwm:
3767 case LibFunc_ZnwmRKSt9nothrow_t:
3768 case LibFunc_ZnwmSt11align_val_t:
3769 case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t:
3770 case LibFunc_Znam:
3771 case LibFunc_ZnamRKSt9nothrow_t:
3772 case LibFunc_ZnamSt11align_val_t:
3773 case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t:
3774 case LibFunc_Znwm12__hot_cold_t:
3775 case LibFunc_ZnwmRKSt9nothrow_t12__hot_cold_t:
3776 case LibFunc_ZnwmSt11align_val_t12__hot_cold_t:
3777 case LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t12__hot_cold_t:
3778 case LibFunc_Znam12__hot_cold_t:
3779 case LibFunc_ZnamRKSt9nothrow_t12__hot_cold_t:
3780 case LibFunc_ZnamSt11align_val_t12__hot_cold_t:
3781 case LibFunc_ZnamSt11align_val_tRKSt9nothrow_t12__hot_cold_t:
3782 return optimizeNew(CI, Builder, Func);
3783 default:
3784 break;
3785 }
3786 }
3787 return nullptr;
3788}
3789
3790Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
3791 LibFunc Func,
3792 IRBuilderBase &Builder) {
3793 const Module *M = CI->getModule();
3794
3795 // Don't optimize calls that require strict floating point semantics.
3796 if (CI->isStrictFP())
3797 return nullptr;
3798
3799 if (Value *V = optimizeSymmetric(CI, Func, Builder))
3800 return V;
3801
3802 switch (Func) {
3803 case LibFunc_sinpif:
3804 case LibFunc_sinpi:
3805 return optimizeSinCosPi(CI, /*IsSin*/true, Builder);
3806 case LibFunc_cospif:
3807 case LibFunc_cospi:
3808 return optimizeSinCosPi(CI, /*IsSin*/false, Builder);
3809 case LibFunc_powf:
3810 case LibFunc_pow:
3811 case LibFunc_powl:
3812 return optimizePow(CI, Builder);
3813 case LibFunc_exp2l:
3814 case LibFunc_exp2:
3815 case LibFunc_exp2f:
3816 return optimizeExp2(CI, Builder);
3817 case LibFunc_fabsf:
3818 case LibFunc_fabs:
3819 case LibFunc_fabsl:
3820 return replaceUnaryCall(CI, Builder, Intrinsic::fabs);
3821 case LibFunc_sqrtf:
3822 case LibFunc_sqrt:
3823 case LibFunc_sqrtl:
3824 return optimizeSqrt(CI, Builder);
3825 case LibFunc_logf:
3826 case LibFunc_log:
3827 case LibFunc_logl:
3828 case LibFunc_log10f:
3829 case LibFunc_log10:
3830 case LibFunc_log10l:
3831 case LibFunc_log1pf:
3832 case LibFunc_log1p:
3833 case LibFunc_log1pl:
3834 case LibFunc_log2f:
3835 case LibFunc_log2:
3836 case LibFunc_log2l:
3837 case LibFunc_logbf:
3838 case LibFunc_logb:
3839 case LibFunc_logbl:
3840 return optimizeLog(CI, Builder);
3841 case LibFunc_tan:
3842 case LibFunc_tanf:
3843 case LibFunc_tanl:
3844 case LibFunc_sinh:
3845 case LibFunc_sinhf:
3846 case LibFunc_sinhl:
3847 case LibFunc_asinh:
3848 case LibFunc_asinhf:
3849 case LibFunc_asinhl:
3850 case LibFunc_cosh:
3851 case LibFunc_coshf:
3852 case LibFunc_coshl:
3853 case LibFunc_atanh:
3854 case LibFunc_atanhf:
3855 case LibFunc_atanhl:
3856 return optimizeTrigInversionPairs(CI, Builder);
3857 case LibFunc_ceil:
3858 return replaceUnaryCall(CI, Builder, Intrinsic::ceil);
3859 case LibFunc_floor:
3860 return replaceUnaryCall(CI, Builder, Intrinsic::floor);
3861 case LibFunc_round:
3862 return replaceUnaryCall(CI, Builder, Intrinsic::round);
3863 case LibFunc_roundeven:
3864 return replaceUnaryCall(CI, Builder, Intrinsic::roundeven);
3865 case LibFunc_nearbyint:
3866 return replaceUnaryCall(CI, Builder, Intrinsic::nearbyint);
3867 case LibFunc_rint:
3868 return replaceUnaryCall(CI, Builder, Intrinsic::rint);
3869 case LibFunc_trunc:
3870 return replaceUnaryCall(CI, Builder, Intrinsic::trunc);
3871 case LibFunc_acos:
3872 case LibFunc_acosh:
3873 case LibFunc_asin:
3874 case LibFunc_atan:
3875 case LibFunc_cbrt:
3876 case LibFunc_exp:
3877 case LibFunc_exp10:
3878 case LibFunc_expm1:
3879 case LibFunc_cos:
3880 case LibFunc_sin:
3881 case LibFunc_tanh:
3882 if (UnsafeFPShrink && hasFloatVersion(M, CI->getCalledFunction()->getName()))
3883 return optimizeUnaryDoubleFP(CI, Builder, TLI, true);
3884 return nullptr;
3885 case LibFunc_copysign:
3886 if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
3887 return optimizeBinaryDoubleFP(CI, Builder, TLI);
3888 return nullptr;
3889 case LibFunc_fminf:
3890 case LibFunc_fmin:
3891 case LibFunc_fminl:
3892 case LibFunc_fmaxf:
3893 case LibFunc_fmax:
3894 case LibFunc_fmaxl:
3895 return optimizeFMinFMax(CI, Builder);
3896 case LibFunc_cabs:
3897 case LibFunc_cabsf:
3898 case LibFunc_cabsl:
3899 return optimizeCAbs(CI, Builder);
3900 default:
3901 return nullptr;
3902 }
3903}
3904
3906 Module *M = CI->getModule();
3907 assert(!CI->isMustTailCall() && "These transforms aren't musttail safe.");
3908
3909 // TODO: Split out the code below that operates on FP calls so that
3910 // we can all non-FP calls with the StrictFP attribute to be
3911 // optimized.
3912 if (CI->isNoBuiltin())
3913 return nullptr;
3914
3915 LibFunc Func;
3916 Function *Callee = CI->getCalledFunction();
3917 bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
3918
3920 CI->getOperandBundlesAsDefs(OpBundles);
3921
3923 Builder.setDefaultOperandBundles(OpBundles);
3924
3925 // Command-line parameter overrides instruction attribute.
3926 // This can't be moved to optimizeFloatingPointLibCall() because it may be
3927 // used by the intrinsic optimizations.
3928 if (EnableUnsafeFPShrink.getNumOccurrences() > 0)
3929 UnsafeFPShrink = EnableUnsafeFPShrink;
3930 else if (isa<FPMathOperator>(CI) && CI->isFast())
3931 UnsafeFPShrink = true;
3932
3933 // First, check for intrinsics.
3934 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
3935 if (!IsCallingConvC)
3936 return nullptr;
3937 // The FP intrinsics have corresponding constrained versions so we don't
3938 // need to check for the StrictFP attribute here.
3939 switch (II->getIntrinsicID()) {
3940 case Intrinsic::pow:
3941 return optimizePow(CI, Builder);
3942 case Intrinsic::exp2:
3943 return optimizeExp2(CI, Builder);
3944 case Intrinsic::log:
3945 case Intrinsic::log2:
3946 case Intrinsic::log10:
3947 return optimizeLog(CI, Builder);
3948 case Intrinsic::sqrt:
3949 return optimizeSqrt(CI, Builder);
3950 case Intrinsic::memset:
3951 return optimizeMemSet(CI, Builder);
3952 case Intrinsic::memcpy:
3953 return optimizeMemCpy(CI, Builder);
3954 case Intrinsic::memmove:
3955 return optimizeMemMove(CI, Builder);
3956 default:
3957 return nullptr;
3958 }
3959 }
3960
3961 // Also try to simplify calls to fortified library functions.
3962 if (Value *SimplifiedFortifiedCI =
3963 FortifiedSimplifier.optimizeCall(CI, Builder))
3964 return SimplifiedFortifiedCI;
3965
3966 // Then check for known library functions.
3967 if (TLI->getLibFunc(*Callee, Func) && isLibFuncEmittable(M, TLI, Func)) {
3968 // We never change the calling convention.
3969 if (!ignoreCallingConv(Func) && !IsCallingConvC)
3970 return nullptr;
3971 if (Value *V = optimizeStringMemoryLibCall(CI, Builder))
3972 return V;
3973 if (Value *V = optimizeFloatingPointLibCall(CI, Func, Builder))
3974 return V;
3975 switch (Func) {
3976 case LibFunc_ffs:
3977 case LibFunc_ffsl:
3978 case LibFunc_ffsll:
3979 return optimizeFFS(CI, Builder);
3980 case LibFunc_fls:
3981 case LibFunc_flsl:
3982 case LibFunc_flsll:
3983 return optimizeFls(CI, Builder);
3984 case LibFunc_abs:
3985 case LibFunc_labs:
3986 case LibFunc_llabs:
3987 return optimizeAbs(CI, Builder);
3988 case LibFunc_isdigit:
3989 return optimizeIsDigit(CI, Builder);
3990 case LibFunc_isascii:
3991 return optimizeIsAscii(CI, Builder);
3992 case LibFunc_toascii:
3993 return optimizeToAscii(CI, Builder);
3994 case LibFunc_atoi:
3995 case LibFunc_atol:
3996 case LibFunc_atoll:
3997 return optimizeAtoi(CI, Builder);
3998 case LibFunc_strtol:
3999 case LibFunc_strtoll:
4000 return optimizeStrToInt(CI, Builder, /*AsSigned=*/true);
4001 case LibFunc_strtoul:
4002 case LibFunc_strtoull:
4003 return optimizeStrToInt(CI, Builder, /*AsSigned=*/false);
4004 case LibFunc_printf:
4005 return optimizePrintF(CI, Builder);
4006 case LibFunc_sprintf:
4007 return optimizeSPrintF(CI, Builder);
4008 case LibFunc_snprintf:
4009 return optimizeSnPrintF(CI, Builder);
4010 case LibFunc_fprintf:
4011 return optimizeFPrintF(CI, Builder);
4012 case LibFunc_fwrite:
4013 return optimizeFWrite(CI, Builder);
4014 case LibFunc_fputs:
4015 return optimizeFPuts(CI, Builder);
4016 case LibFunc_puts:
4017 return optimizePuts(CI, Builder);
4018 case LibFunc_perror:
4019 return optimizeErrorReporting(CI, Builder);
4020 case LibFunc_vfprintf:
4021 case LibFunc_fiprintf:
4022 return optimizeErrorReporting(CI, Builder, 0);
4023 default:
4024 return nullptr;
4025 }
4026 }
4027 return nullptr;
4028}
4029
4031 const DataLayout &DL, const TargetLibraryInfo *TLI, AssumptionCache *AC,
4033 ProfileSummaryInfo *PSI,
4034 function_ref<void(Instruction *, Value *)> Replacer,
4035 function_ref<void(Instruction *)> Eraser)
4036 : FortifiedSimplifier(TLI), DL(DL), TLI(TLI), AC(AC), ORE(ORE), BFI(BFI),
4037 PSI(PSI), Replacer(Replacer), Eraser(Eraser) {}
4038
4039void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) {
4040 // Indirect through the replacer used in this instance.
4041 Replacer(I, With);
4042}
4043
4044void LibCallSimplifier::eraseFromParent(Instruction *I) {
4045 Eraser(I);
4046}
4047
4048// TODO:
4049// Additional cases that we need to add to this file:
4050//
4051// cbrt:
4052// * cbrt(expN(X)) -> expN(x/3)
4053// * cbrt(sqrt(x)) -> pow(x,1/6)
4054// * cbrt(cbrt(x)) -> pow(x,1/9)
4055//
4056// exp, expf, expl:
4057// * exp(log(x)) -> x
4058//
4059// log, logf, logl:
4060// * log(exp(x)) -> x
4061// * log(exp(y)) -> y*log(e)
4062// * log(exp10(y)) -> y*log(10)
4063// * log(sqrt(x)) -> 0.5*log(x)
4064//
4065// pow, powf, powl:
4066// * pow(sqrt(x),y) -> pow(x,y*0.5)
4067// * pow(pow(x,y),z)-> pow(x,y*z)
4068//
4069// signbit:
4070// * signbit(cnst) -> cnst'
4071// * signbit(nncst) -> 0 (if pstv is a non-negative constant)
4072//
4073// sqrt, sqrtf, sqrtl:
4074// * sqrt(expN(x)) -> expN(x*0.5)
4075// * sqrt(Nroot(x)) -> pow(x,1/(2*N))
4076// * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
4077//
4078
4079//===----------------------------------------------------------------------===//
4080// Fortified Library Call Optimizations
4081//===----------------------------------------------------------------------===//
4082
4083bool FortifiedLibCallSimplifier::isFortifiedCallFoldable(
4084 CallInst *CI, unsigned ObjSizeOp, std::optional<unsigned> SizeOp,
4085 std::optional<unsigned> StrOp, std::optional<unsigned> FlagOp) {
4086 // If this function takes a flag argument, the implementation may use it to
4087 // perform extra checks. Don't fold into the non-checking variant.
4088 if (FlagOp) {
4089 ConstantInt *Flag = dyn_cast<ConstantInt>(CI->getArgOperand(*FlagOp));
4090 if (!Flag || !Flag->isZero())
4091 return false;
4092 }
4093
4094 if (SizeOp && CI->getArgOperand(ObjSizeOp) == CI->getArgOperand(*SizeOp))
4095 return true;
4096
4097 if (ConstantInt *ObjSizeCI =
4098 dyn_cast<ConstantInt>(CI->getArgOperand(ObjSizeOp))) {
4099 if (ObjSizeCI->isMinusOne())
4100 return true;
4101 // If the object size wasn't -1 (unknown), bail out if we were asked to.
4102 if (OnlyLowerUnknownSize)
4103 return false;
4104 if (StrOp) {
4106 // If the length is 0 we don't know how long it is and so we can't
4107 // remove the check.
4108 if (Len)
4109 annotateDereferenceableBytes(CI, *StrOp, Len);
4110 else
4111 return false;
4112 return ObjSizeCI->getZExtValue() >= Len;
4113 }
4114
4115 if (SizeOp) {
4116 if (ConstantInt *SizeCI =
4117 dyn_cast<ConstantInt>(CI->getArgOperand(*SizeOp)))
4118 return ObjSizeCI->getZExtValue() >= SizeCI->getZExtValue();
4119 }
4120 }
4121 return false;
4122}
4123
4124Value *FortifiedLibCallSimplifier::optimizeMemCpyChk(CallInst *CI,
4125 IRBuilderBase &B) {
4126 if (isFortifiedCallFoldable(CI, 3, 2)) {
4127 CallInst *NewCI =
4128 B.CreateMemCpy(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
4129 Align(1), CI->getArgOperand(2));
4130 mergeAttributesAndFlags(NewCI, *CI);
4131 return CI->getArgOperand(0);
4132 }
4133 return nullptr;
4134}
4135
4136Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI,
4137 IRBuilderBase &B) {
4138 if (isFortifiedCallFoldable(CI, 3, 2)) {
4139 CallInst *NewCI =
4140 B.CreateMemMove(CI->getArgOperand(0), Align(1), CI->getArgOperand(1),
4141 Align(1), CI->getArgOperand(2));
4142 mergeAttributesAndFlags(NewCI, *CI);
4143 return CI->getArgOperand(0);
4144 }
4145 return nullptr;
4146}
4147
4148Value *FortifiedLibCallSimplifier::optimizeMemSetChk(CallInst *CI,
4149 IRBuilderBase &B) {
4150 if (isFortifiedCallFoldable(CI, 3, 2)) {
4151 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
4152 CallInst *NewCI = B.CreateMemSet(CI->getArgOperand(0), Val,
4153 CI->getArgOperand(2), Align(1));
4154 mergeAttributesAndFlags(NewCI, *CI);
4155 return CI->getArgOperand(0);
4156 }
4157 return nullptr;
4158}
4159
4160Value *FortifiedLibCallSimplifier::optimizeMemPCpyChk(CallInst *CI,
4161 IRBuilderBase &B) {
4162 const DataLayout &DL = CI->getDataLayout();
4163 if (isFortifiedCallFoldable(CI, 3, 2))
4164 if (Value *Call = emitMemPCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4165 CI->getArgOperand(2), B, DL, TLI)) {
4166 return mergeAttributesAndFlags(cast<CallInst>(Call), *CI);
4167 }
4168 return nullptr;
4169}
4170
4171Value *FortifiedLibCallSimplifier::optimizeStrpCpyChk(CallInst *CI,
4173 LibFunc Func) {
4174 const DataLayout &DL = CI->getDataLayout();
4175 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1),
4176 *ObjSize = CI->getArgOperand(2);
4177
4178 // __stpcpy_chk(x,x,...) -> x+strlen(x)
4179 if (Func == LibFunc_stpcpy_chk && !OnlyLowerUnknownSize && Dst == Src) {
4180 Value *StrLen = emitStrLen(Src, B, DL, TLI);
4181 return StrLen ? B.CreateInBoundsGEP(B.getInt8Ty(), Dst, StrLen) : nullptr;
4182 }
4183
4184 // If a) we don't have any length information, or b) we know this will
4185 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
4186 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
4187 // TODO: It might be nice to get a maximum length out of the possible
4188 // string lengths for varying.
4189 if (isFortifiedCallFoldable(CI, 2, std::nullopt, 1)) {
4190 if (Func == LibFunc_strcpy_chk)
4191 return copyFlags(*CI, emitStrCpy(Dst, Src, B, TLI));
4192 else
4193 return copyFlags(*CI, emitStpCpy(Dst, Src, B, TLI));
4194 }
4195
4196 if (OnlyLowerUnknownSize)
4197 return nullptr;
4198
4199 // Maybe we can stil fold __st[rp]cpy_chk to __memcpy_chk.
4201 if (Len)
4202 annotateDereferenceableBytes(CI, 1, Len);
4203 else
4204 return nullptr;
4205
4206 unsigned SizeTBits = TLI->getSizeTSize(*CI->getModule());
4207 Type *SizeTTy = IntegerType::get(CI->getContext(), SizeTBits);
4208 Value *LenV = ConstantInt::get(SizeTTy, Len);
4209 Value *Ret = emitMemCpyChk(Dst, Src, LenV, ObjSize, B, DL, TLI);
4210 // If the function was an __stpcpy_chk, and we were able to fold it into
4211 // a __memcpy_chk, we still need to return the correct end pointer.
4212 if (Ret && Func == LibFunc_stpcpy_chk)
4213 return B.CreateInBoundsGEP(B.getInt8Ty(), Dst,
4214 ConstantInt::get(SizeTTy, Len - 1));
4215 return copyFlags(*CI, cast<CallInst>(Ret));
4216}
4217
4218Value *FortifiedLibCallSimplifier::optimizeStrLenChk(CallInst *CI,
4219 IRBuilderBase &B) {
4220 if (isFortifiedCallFoldable(CI, 1, std::nullopt, 0))
4221 return copyFlags(*CI, emitStrLen(CI->getArgOperand(0), B,
4222 CI->getDataLayout(), TLI));
4223 return nullptr;
4224}
4225
4226Value *FortifiedLibCallSimplifier::optimizeStrpNCpyChk(CallInst *CI,
4228 LibFunc Func) {
4229 if (isFortifiedCallFoldable(CI, 3, 2)) {
4230 if (Func == LibFunc_strncpy_chk)
4231 return copyFlags(*CI,
4233 CI->getArgOperand(2), B, TLI));
4234 else
4235 return copyFlags(*CI,
4237 CI->getArgOperand(2), B, TLI));
4238 }
4239
4240 return nullptr;
4241}
4242
4243Value *FortifiedLibCallSimplifier::optimizeMemCCpyChk(CallInst *CI,
4244 IRBuilderBase &B) {
4245 if (isFortifiedCallFoldable(CI, 4, 3))
4246 return copyFlags(
4247 *CI, emitMemCCpy(CI->getArgOperand(0), CI->getArgOperand(1),
4248 CI->getArgOperand(2), CI->getArgOperand(3), B, TLI));
4249
4250 return nullptr;
4251}
4252
4253Value *FortifiedLibCallSimplifier::optimizeSNPrintfChk(CallInst *CI,
4254 IRBuilderBase &B) {
4255 if (isFortifiedCallFoldable(CI, 3, 1, std::nullopt, 2)) {
4256 SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 5));
4257 return copyFlags(*CI,
4259 CI->getArgOperand(4), VariadicArgs, B, TLI));
4260 }
4261
4262 return nullptr;
4263}
4264
4265Value *FortifiedLibCallSimplifier::optimizeSPrintfChk(CallInst *CI,
4266 IRBuilderBase &B) {
4267 if (isFortifiedCallFoldable(CI, 2, std::nullopt, std::nullopt, 1)) {
4268 SmallVector<Value *, 8> VariadicArgs(drop_begin(CI->args(), 4));
4269 return copyFlags(*CI,
4271 VariadicArgs, B, TLI));
4272 }
4273
4274 return nullptr;
4275}
4276
4277Value *FortifiedLibCallSimplifier::optimizeStrCatChk(CallInst *CI,
4278 IRBuilderBase &B) {
4279 if (isFortifiedCallFoldable(CI, 2))
4280 return copyFlags(
4281 *CI, emitStrCat(CI->getArgOperand(0), CI->getArgOperand(1), B, TLI));
4282
4283 return nullptr;
4284}
4285
4286Value *FortifiedLibCallSimplifier::optimizeStrLCat(CallInst *CI,
4287 IRBuilderBase &B) {
4288 if (isFortifiedCallFoldable(CI, 3))
4289 return copyFlags(*CI,
4291 CI->getArgOperand(2), B, TLI));
4292
4293 return nullptr;
4294}
4295
4296Value *FortifiedLibCallSimplifier::optimizeStrNCatChk(CallInst *CI,
4297 IRBuilderBase &B) {
4298 if (isFortifiedCallFoldable(CI, 3))
4299 return copyFlags(*CI,
4301 CI->getArgOperand(2), B, TLI));
4302
4303 return nullptr;
4304}
4305
4306Value *FortifiedLibCallSimplifier::optimizeStrLCpyChk(CallInst *CI,
4307 IRBuilderBase &B) {
4308 if (isFortifiedCallFoldable(CI, 3))
4309 return copyFlags(*CI,
4311 CI->getArgOperand(2), B, TLI));
4312
4313 return nullptr;
4314}
4315
4316Value *FortifiedLibCallSimplifier::optimizeVSNPrintfChk(CallInst *CI,
4317 IRBuilderBase &B) {
4318 if (isFortifiedCallFoldable(CI, 3, 1, std::nullopt, 2))
4319 return copyFlags(
4320 *CI, emitVSNPrintf(CI->getArgOperand(0), CI->getArgOperand(1),
4321 CI->getArgOperand(4), CI->getArgOperand(5), B, TLI));
4322
4323 return nullptr;
4324}
4325
4326Value *FortifiedLibCallSimplifier::optimizeVSPrintfChk(CallInst *CI,
4327 IRBuilderBase &B) {
4328 if (isFortifiedCallFoldable(CI, 2, std::nullopt, std::nullopt, 1))
4329 return copyFlags(*CI,
4331 CI->getArgOperand(4), B, TLI));
4332
4333 return nullptr;
4334}
4335
4337 IRBuilderBase &Builder) {
4338 // FIXME: We shouldn't be changing "nobuiltin" or TLI unavailable calls here.
4339 // Some clang users checked for _chk libcall availability using:
4340 // __has_builtin(__builtin___memcpy_chk)
4341 // When compiling with -fno-builtin, this is always true.
4342 // When passing -ffreestanding/-mkernel, which both imply -fno-builtin, we
4343 // end up with fortified libcalls, which isn't acceptable in a freestanding
4344 // environment which only provides their non-fortified counterparts.
4345 //
4346 // Until we change clang and/or teach external users to check for availability
4347 // differently, disregard the "nobuiltin" attribute and TLI::has.
4348 //
4349 // PR23093.
4350
4351 LibFunc Func;
4352 Function *Callee = CI->getCalledFunction();
4353 bool IsCallingConvC = TargetLibraryInfoImpl::isCallingConvCCompatible(CI);
4354
4356 CI->getOperandBundlesAsDefs(OpBundles);
4357
4359 Builder.setDefaultOperandBundles(OpBundles);
4360
4361 // First, check that this is a known library functions and that the prototype
4362 // is correct.
4363 if (!TLI->getLibFunc(*Callee, Func))
4364 return nullptr;
4365
4366 // We never change the calling convention.
4367 if (!ignoreCallingConv(Func) && !IsCallingConvC)
4368 return nullptr;
4369
4370 switch (Func) {
4371 case LibFunc_memcpy_chk:
4372 return optimizeMemCpyChk(CI, Builder);
4373 case LibFunc_mempcpy_chk:
4374 return optimizeMemPCpyChk(CI, Builder);
4375 case LibFunc_memmove_chk:
4376 return optimizeMemMoveChk(CI, Builder);
4377 case LibFunc_memset_chk:
4378 return optimizeMemSetChk(CI, Builder);
4379 case LibFunc_stpcpy_chk:
4380 case LibFunc_strcpy_chk:
4381 return optimizeStrpCpyChk(CI, Builder, Func);
4382 case LibFunc_strlen_chk:
4383 return optimizeStrLenChk(CI, Builder);
4384 case LibFunc_stpncpy_chk:
4385 case LibFunc_strncpy_chk:
4386 return optimizeStrpNCpyChk(CI, Builder, Func);
4387 case LibFunc_memccpy_chk:
4388 return optimizeMemCCpyChk(CI, Builder);
4389 case LibFunc_snprintf_chk:
4390 return optimizeSNPrintfChk(CI, Builder);
4391 case LibFunc_sprintf_chk:
4392 return optimizeSPrintfChk(CI, Builder);
4393 case LibFunc_strcat_chk:
4394 return optimizeStrCatChk(CI, Builder);
4395 case LibFunc_strlcat_chk:
4396 return optimizeStrLCat(CI, Builder);
4397 case LibFunc_strncat_chk:
4398 return optimizeStrNCatChk(CI, Builder);
4399 case LibFunc_strlcpy_chk:
4400 return optimizeStrLCpyChk(CI, Builder);
4401 case LibFunc_vsnprintf_chk:
4402 return optimizeVSNPrintfChk(CI, Builder);
4403 case LibFunc_vsprintf_chk:
4404 return optimizeVSPrintfChk(CI, Builder);
4405 default:
4406 break;
4407 }
4408 return nullptr;
4409}
4410
4412 const TargetLibraryInfo *TLI, bool OnlyLowerUnknownSize)
4413 : 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.
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
static bool isBinary(MachineInstr &MI)
const SmallVectorImpl< MachineOperand > & Cond
static bool isDigit(const char C)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool isOnlyUsedInEqualityComparison(Value *V, Value *With)
Return true if it is only used in equality comparisons with With.
static void annotateNonNullAndDereferenceable(CallInst *CI, ArrayRef< unsigned > ArgNos, Value *Size, const DataLayout &DL)
static cl::opt< unsigned, false, HotColdHintParser > ColdNewHintValue("cold-new-hint-value", cl::Hidden, cl::init(1), cl::desc("Value to pass to hot/cold operator new for cold allocation"))
static bool insertSinCosCall(IRBuilderBase &B, Function *OrigCallee, Value *Arg, bool UseFloat, Value *&Sin, Value *&Cos, Value *&SinCos, const TargetLibraryInfo *TLI)
static bool canTransformToMemCmp(CallInst *CI, Value *Str, uint64_t Len, const DataLayout &DL)
static Value * mergeAttributesAndFlags(CallInst *NewCI, const CallInst &Old)
static cl::opt< bool > OptimizeHotColdNew("optimize-hot-cold-new", cl::Hidden, cl::init(false), cl::desc("Enable hot/cold operator new library calls"))
static Value * optimizeBinaryDoubleFP(CallInst *CI, IRBuilderBase &B, const TargetLibraryInfo *TLI, bool isPrecise=false)
Shrink double -> float for binary functions.
static bool ignoreCallingConv(LibFunc Func)
static cl::opt< bool > OptimizeExistingHotColdNew("optimize-existing-hot-cold-new", cl::Hidden, cl::init(false), cl::desc("Enable optimization of existing hot/cold operator new library calls"))
static void annotateDereferenceableBytes(CallInst *CI, ArrayRef< unsigned > ArgNos, uint64_t DereferenceableBytes)
static bool isReportingError(Function *Callee, CallInst *CI, int StreamArg)
static Value * optimizeDoubleFP(CallInst *CI, IRBuilderBase &B, bool isBinary, const TargetLibraryInfo *TLI, bool isPrecise=false)
Shrink double -> float functions.
static Value * optimizeSymmetricCall(CallInst *CI, bool IsEven, IRBuilderBase &B)
static Value * getSqrtCall(Value *V, AttributeList Attrs, bool NoErrno, Module *M, IRBuilderBase &B, const TargetLibraryInfo *TLI)
static Value * valueHasFloatPrecision(Value *Val)
Return a variant of Val with float type.
static Value * optimizeMemCmpConstantSize(CallInst *CI, Value *LHS, Value *RHS, uint64_t Len, IRBuilderBase &B, const DataLayout &DL)
static Value * createPowWithIntegerExponent(Value *Base, Value *Expo, Module *M, IRBuilderBase &B)
static Value * convertStrToInt(CallInst *CI, StringRef &Str, Value *EndPtr, uint64_t Base, bool AsSigned, IRBuilderBase &B)
static Value * memChrToCharCompare(CallInst *CI, Value *NBytes, IRBuilderBase &B, const DataLayout &DL)
static Value * copyFlags(const CallInst &Old, Value *New)
static StringRef substr(StringRef Str, uint64_t Len)
static cl::opt< unsigned, false, HotColdHintParser > HotNewHintValue("hot-new-hint-value", cl::Hidden, cl::init(254), cl::desc("Value to pass to hot/cold operator new for hot allocation"))
static bool isTrigLibCall(CallInst *CI)
static 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:1358
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5282
bool isNegative() const
Definition: APFloat.h:1348
double convertToDouble() const
Converts this APFloat to host double value.
Definition: APFloat.cpp:5341
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:1331
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1086
const fltSemantics & getSemantics() const
Definition: APFloat.h:1356
float convertToFloat() const
Converts this APFloat to host float value.
Definition: APFloat.cpp:5369
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1229
bool isInteger() const
Definition: APFloat.h:1365
Class for arbitrary precision integers.
Definition: APInt.h:77
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1129
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
A cache of @llvm.assume calls within a function.
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute > > Attrs)
Create an AttributeList with the specified parameters in it.
Attribute getFnAttr(Attribute::AttrKind Kind) const
Return the attribute object that exists for the function.
Definition: Attributes.h:864
AttributeSet getParamAttrs(unsigned ArgNo) const
The attributes for the argument or parameter at the given index are returned.
AttributeList addParamAttributes(LLVMContext &C, unsigned ArgNo, const AttrBuilder &B) const
Add an argument attribute to the list.
Definition: Attributes.h:627
MaybeAlign getAlignment() const
Definition: Attributes.cpp:925
static Attribute getWithDereferenceableBytes(LLVMContext &Context, uint64_t Bytes)
Definition: Attributes.cpp:242
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:391
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:438
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
void addFnAttr(Attribute::AttrKind Kind)
Adds the attribute to the function.
Definition: InstrTypes.h:1574
void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
Definition: InstrTypes.h:1965
void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
Removes the attribute from the given argument.
Definition: InstrTypes.h:1641
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1465
bool doesNotAccessMemory(unsigned OpNo) const
Definition: InstrTypes.h:1810
void removeRetAttrs(const AttributeMask &AttrsToRemove)
Removes the attributes from the return value.
Definition: InstrTypes.h:1636
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
Definition: InstrTypes.h:1551
bool isStrictFP() const
Determine if the call requires strict floating point semantics.
Definition: InstrTypes.h:1971
uint64_t getParamDereferenceableBytes(unsigned i) const
Extract the number of dereferenceable bytes for a call or parameter (0=unknown).
Definition: InstrTypes.h:1909
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1546
bool doesNotThrow() const
Determine if the call cannot unwind.
Definition: InstrTypes.h:2015
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1410
uint64_t getParamDereferenceableOrNullBytes(unsigned i) const
Extract the number of dereferenceable_or_null bytes for a parameter (0=unknown).
Definition: InstrTypes.h:1927
Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Definition: InstrTypes.h:1401
unsigned arg_size() const
Definition: InstrTypes.h:1408
AttributeList getAttributes() const
Return the parameter attributes for this call.
Definition: InstrTypes.h:1542
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
Adds the attribute to the indicated argument.
Definition: InstrTypes.h:1594
Function * getCaller()
Helper to get the caller (the parent function).
This class represents a function call, abstracting a target machine's calling convention.
bool isNoTailCall() const
TailCallKind getTailCallKind() const
bool isMustTailCall() const
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:780
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:782
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:783
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:847
uint64_t getElementAsInteger(unsigned i) const
If this is a sequential container of integers (of any size), return the specified element in the low ...
Definition: Constants.cpp:3024
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:269
static Constant * getInfinity(Type *Ty, bool Negative=false)
Definition: Constants.cpp:1084
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Definition: Constants.h:212
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:206
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition: Constants.h:161
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:155
This is an important base class in LLVM.
Definition: Constant.h: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:698
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:242
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:350
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition: Function.h:247
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:690
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:290
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
This instruction compares its operands according to the predicate given to the constructor.
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:92
void setDefaultOperandBundles(ArrayRef< OperandBundleDef > OpBundles)
Definition: IRBuilder.h:363
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
bool hasNoInfs() const LLVM_READONLY
Determine whether the no-infs flag is set.
bool hasNoSignedZeros() const LLVM_READONLY
Determine whether the no-signed-zeros flag is set.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:66
bool isFast() const LLVM_READONLY
Determine whether all fast-math-flags are set.
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:70
FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
bool hasApproxFunc() const LLVM_READONLY
Determine whether the approximate-math-functions flag is set.
bool hasAllowReassoc() const LLVM_READONLY
Determine whether the allow-reassociation flag is set.
const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Definition: Instruction.cpp:74
Class to represent integer types.
Definition: DerivedTypes.h:40
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:278
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
Value * optimizeCall(CallInst *CI, IRBuilderBase &B)
optimizeCall - Take the given call instruction and return a more optimal value to replace the instruc...
LibCallSimplifier(const DataLayout &DL, const TargetLibraryInfo *TLI, AssumptionCache *AC, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, function_ref< void(Instruction *, Value *)> Replacer=&replaceAllUsesWithDefault, function_ref< void(Instruction *)> Eraser=&eraseFromParentDefault)
An instruction for reading from memory.
Definition: Instructions.h:173
Value * getPointerOperand()
Definition: Instructions.h:252
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
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:463
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:258
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:417
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:290
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:178
StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
Definition: StringRef.h:609
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.
const ParentTy * getParent() const
Definition: ilist_node.h:32
#define UINT64_MAX
Definition: DataTypes.h:77
AttributeMask typeIncompatible(Type *Ty, AttributeSafetyKind ASK=ASK_ALL)
Which attributes cannot be applied to a type.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1484
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:443
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:480
@ Length
Definition: DWP.cpp:480
int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Definition: MathExtras.h: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:1434
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:2073
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:612
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:271
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:249
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:246
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:250
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Holds functions to get, set or test bitfields.
Definition: Bitfields.h:212
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
const ConstantDataArray * Array
ConstantDataArray pointer.
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:97
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:134
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:141
Definition: regcomp.c:192