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