1 //===------ SimplifyLibCalls.cpp - Library calls simplifier ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is a utility pass used for testing the InstructionSimplify analysis.
11 // The analysis is applied to every instruction, and if it simplifies then the
12 // instruction is replaced by the simplification. If you are looking for a pass
13 // that performs serious instruction folding, use the instcombine pass instead.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Utils/SimplifyLibCalls.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Intrinsics.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/Support/Allocator.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Target/TargetLibraryInfo.h"
32 #include "llvm/Transforms/Utils/BuildLibCalls.h"
37 ColdErrorCalls("error-reporting-is-cold", cl::init(true),
38 cl::Hidden, cl::desc("Treat error-reporting calls as cold"));
40 /// This class is the abstract base class for the set of optimizations that
41 /// corresponds to one library call.
43 class LibCallOptimization {
47 const TargetLibraryInfo *TLI;
48 const LibCallSimplifier *LCS;
51 LibCallOptimization() { }
52 virtual ~LibCallOptimization() {}
54 /// callOptimizer - This pure virtual method is implemented by base classes to
55 /// do various optimizations. If this returns null then no transformation was
56 /// performed. If it returns CI, then it transformed the call and CI is to be
57 /// deleted. If it returns something else, replace CI with the new value and
59 virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
62 /// ignoreCallingConv - Returns false if this transformation could possibly
63 /// change the calling convention.
64 virtual bool ignoreCallingConv() { return false; }
66 Value *optimizeCall(CallInst *CI, const DataLayout *DL,
67 const TargetLibraryInfo *TLI,
68 const LibCallSimplifier *LCS, IRBuilder<> &B) {
69 Caller = CI->getParent()->getParent();
73 if (CI->getCalledFunction())
74 Context = &CI->getCalledFunction()->getContext();
76 // We never change the calling convention.
77 if (!ignoreCallingConv() && CI->getCallingConv() != llvm::CallingConv::C)
80 return callOptimizer(CI->getCalledFunction(), CI, B);
84 //===----------------------------------------------------------------------===//
86 //===----------------------------------------------------------------------===//
88 /// isOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
89 /// value is equal or not-equal to zero.
90 static bool isOnlyUsedInZeroEqualityComparison(Value *V) {
91 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
93 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
95 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
98 // Unknown instruction.
104 /// isOnlyUsedInEqualityComparison - Return true if it is only used in equality
105 /// comparisons with With.
106 static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
107 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
109 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
110 if (IC->isEquality() && IC->getOperand(1) == With)
112 // Unknown instruction.
118 static bool callHasFloatingPointArgument(const CallInst *CI) {
119 for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
121 if ((*it)->getType()->isFloatingPointTy())
127 /// \brief Check whether the overloaded unary floating point function
128 /// corresponing to \a Ty is available.
129 static bool hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
130 LibFunc::Func DoubleFn, LibFunc::Func FloatFn,
131 LibFunc::Func LongDoubleFn) {
132 switch (Ty->getTypeID()) {
133 case Type::FloatTyID:
134 return TLI->has(FloatFn);
135 case Type::DoubleTyID:
136 return TLI->has(DoubleFn);
138 return TLI->has(LongDoubleFn);
142 //===----------------------------------------------------------------------===//
143 // Fortified Library Call Optimizations
144 //===----------------------------------------------------------------------===//
146 struct FortifiedLibCallOptimization : public LibCallOptimization {
148 virtual bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
149 bool isString) const = 0;
152 struct InstFortifiedLibCallOptimization : public FortifiedLibCallOptimization {
155 bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp,
156 bool isString) const override {
157 if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))
159 if (ConstantInt *SizeCI =
160 dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {
161 if (SizeCI->isAllOnesValue())
164 uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));
165 // If the length is 0 we don't know how long it is and so we can't
167 if (Len == 0) return false;
168 return SizeCI->getZExtValue() >= Len;
170 if (ConstantInt *Arg = dyn_cast<ConstantInt>(
171 CI->getArgOperand(SizeArgOp)))
172 return SizeCI->getZExtValue() >= Arg->getZExtValue();
178 struct MemCpyChkOpt : public InstFortifiedLibCallOptimization {
179 Value *callOptimizer(Function *Callee, CallInst *CI,
180 IRBuilder<> &B) override {
182 FunctionType *FT = Callee->getFunctionType();
183 LLVMContext &Context = CI->getParent()->getContext();
185 // Check if this has the right signature.
186 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
187 !FT->getParamType(0)->isPointerTy() ||
188 !FT->getParamType(1)->isPointerTy() ||
189 FT->getParamType(2) != DL->getIntPtrType(Context) ||
190 FT->getParamType(3) != DL->getIntPtrType(Context))
193 if (isFoldable(3, 2, false)) {
194 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
195 CI->getArgOperand(2), 1);
196 return CI->getArgOperand(0);
202 struct MemMoveChkOpt : public InstFortifiedLibCallOptimization {
203 Value *callOptimizer(Function *Callee, CallInst *CI,
204 IRBuilder<> &B) override {
206 FunctionType *FT = Callee->getFunctionType();
207 LLVMContext &Context = CI->getParent()->getContext();
209 // Check if this has the right signature.
210 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
211 !FT->getParamType(0)->isPointerTy() ||
212 !FT->getParamType(1)->isPointerTy() ||
213 FT->getParamType(2) != DL->getIntPtrType(Context) ||
214 FT->getParamType(3) != DL->getIntPtrType(Context))
217 if (isFoldable(3, 2, false)) {
218 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
219 CI->getArgOperand(2), 1);
220 return CI->getArgOperand(0);
226 struct MemSetChkOpt : public InstFortifiedLibCallOptimization {
227 Value *callOptimizer(Function *Callee, CallInst *CI,
228 IRBuilder<> &B) override {
230 FunctionType *FT = Callee->getFunctionType();
231 LLVMContext &Context = CI->getParent()->getContext();
233 // Check if this has the right signature.
234 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
235 !FT->getParamType(0)->isPointerTy() ||
236 !FT->getParamType(1)->isIntegerTy() ||
237 FT->getParamType(2) != DL->getIntPtrType(Context) ||
238 FT->getParamType(3) != DL->getIntPtrType(Context))
241 if (isFoldable(3, 2, false)) {
242 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
244 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
245 return CI->getArgOperand(0);
251 struct StrCpyChkOpt : public InstFortifiedLibCallOptimization {
252 Value *callOptimizer(Function *Callee, CallInst *CI,
253 IRBuilder<> &B) override {
255 StringRef Name = Callee->getName();
256 FunctionType *FT = Callee->getFunctionType();
257 LLVMContext &Context = CI->getParent()->getContext();
259 // Check if this has the right signature.
260 if (FT->getNumParams() != 3 ||
261 FT->getReturnType() != FT->getParamType(0) ||
262 FT->getParamType(0) != FT->getParamType(1) ||
263 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
264 FT->getParamType(2) != DL->getIntPtrType(Context))
267 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
268 if (Dst == Src) // __strcpy_chk(x,x) -> x
271 // If a) we don't have any length information, or b) we know this will
272 // fit then just lower to a plain strcpy. Otherwise we'll keep our
273 // strcpy_chk call which may fail at runtime if the size is too long.
274 // TODO: It might be nice to get a maximum length out of the possible
275 // string lengths for varying.
276 if (isFoldable(2, 1, true)) {
277 Value *Ret = EmitStrCpy(Dst, Src, B, DL, TLI, Name.substr(2, 6));
280 // Maybe we can stil fold __strcpy_chk to __memcpy_chk.
281 uint64_t Len = GetStringLength(Src);
282 if (Len == 0) return 0;
284 // This optimization require DataLayout.
288 EmitMemCpyChk(Dst, Src,
289 ConstantInt::get(DL->getIntPtrType(Context), Len),
290 CI->getArgOperand(2), B, DL, TLI);
297 struct StpCpyChkOpt : public InstFortifiedLibCallOptimization {
298 Value *callOptimizer(Function *Callee, CallInst *CI,
299 IRBuilder<> &B) override {
301 StringRef Name = Callee->getName();
302 FunctionType *FT = Callee->getFunctionType();
303 LLVMContext &Context = CI->getParent()->getContext();
305 // Check if this has the right signature.
306 if (FT->getNumParams() != 3 ||
307 FT->getReturnType() != FT->getParamType(0) ||
308 FT->getParamType(0) != FT->getParamType(1) ||
309 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
310 FT->getParamType(2) != DL->getIntPtrType(FT->getParamType(0)))
313 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
314 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
315 Value *StrLen = EmitStrLen(Src, B, DL, TLI);
316 return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
319 // If a) we don't have any length information, or b) we know this will
320 // fit then just lower to a plain stpcpy. Otherwise we'll keep our
321 // stpcpy_chk call which may fail at runtime if the size is too long.
322 // TODO: It might be nice to get a maximum length out of the possible
323 // string lengths for varying.
324 if (isFoldable(2, 1, true)) {
325 Value *Ret = EmitStrCpy(Dst, Src, B, DL, TLI, Name.substr(2, 6));
328 // Maybe we can stil fold __stpcpy_chk to __memcpy_chk.
329 uint64_t Len = GetStringLength(Src);
330 if (Len == 0) return 0;
332 // This optimization require DataLayout.
335 Type *PT = FT->getParamType(0);
336 Value *LenV = ConstantInt::get(DL->getIntPtrType(PT), Len);
337 Value *DstEnd = B.CreateGEP(Dst,
338 ConstantInt::get(DL->getIntPtrType(PT),
340 if (!EmitMemCpyChk(Dst, Src, LenV, CI->getArgOperand(2), B, DL, TLI))
348 struct StrNCpyChkOpt : public InstFortifiedLibCallOptimization {
349 Value *callOptimizer(Function *Callee, CallInst *CI,
350 IRBuilder<> &B) override {
352 StringRef Name = Callee->getName();
353 FunctionType *FT = Callee->getFunctionType();
354 LLVMContext &Context = CI->getParent()->getContext();
356 // Check if this has the right signature.
357 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
358 FT->getParamType(0) != FT->getParamType(1) ||
359 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
360 !FT->getParamType(2)->isIntegerTy() ||
361 FT->getParamType(3) != DL->getIntPtrType(Context))
364 if (isFoldable(3, 2, false)) {
365 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
366 CI->getArgOperand(2), B, DL, TLI,
374 //===----------------------------------------------------------------------===//
375 // String and Memory Library Call Optimizations
376 //===----------------------------------------------------------------------===//
378 struct StrCatOpt : public LibCallOptimization {
379 Value *callOptimizer(Function *Callee, CallInst *CI,
380 IRBuilder<> &B) override {
381 // Verify the "strcat" function prototype.
382 FunctionType *FT = Callee->getFunctionType();
383 if (FT->getNumParams() != 2 ||
384 FT->getReturnType() != B.getInt8PtrTy() ||
385 FT->getParamType(0) != FT->getReturnType() ||
386 FT->getParamType(1) != FT->getReturnType())
389 // Extract some information from the instruction
390 Value *Dst = CI->getArgOperand(0);
391 Value *Src = CI->getArgOperand(1);
393 // See if we can get the length of the input string.
394 uint64_t Len = GetStringLength(Src);
395 if (Len == 0) return 0;
396 --Len; // Unbias length.
398 // Handle the simple, do-nothing case: strcat(x, "") -> x
402 // These optimizations require DataLayout.
405 return emitStrLenMemCpy(Src, Dst, Len, B);
408 Value *emitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len,
410 // We need to find the end of the destination string. That's where the
411 // memory is to be moved to. We just generate a call to strlen.
412 Value *DstLen = EmitStrLen(Dst, B, DL, TLI);
416 // Now that we have the destination's length, we must index into the
417 // destination's pointer to get the actual memcpy destination (end of
418 // the string .. we're concatenating).
419 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
421 // We have enough information to now generate the memcpy call to do the
422 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
423 B.CreateMemCpy(CpyDst, Src,
424 ConstantInt::get(DL->getIntPtrType(*Context), Len + 1), 1);
429 struct StrNCatOpt : public StrCatOpt {
430 Value *callOptimizer(Function *Callee, CallInst *CI,
431 IRBuilder<> &B) override {
432 // Verify the "strncat" function prototype.
433 FunctionType *FT = Callee->getFunctionType();
434 if (FT->getNumParams() != 3 ||
435 FT->getReturnType() != B.getInt8PtrTy() ||
436 FT->getParamType(0) != FT->getReturnType() ||
437 FT->getParamType(1) != FT->getReturnType() ||
438 !FT->getParamType(2)->isIntegerTy())
441 // Extract some information from the instruction
442 Value *Dst = CI->getArgOperand(0);
443 Value *Src = CI->getArgOperand(1);
446 // We don't do anything if length is not constant
447 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
448 Len = LengthArg->getZExtValue();
452 // See if we can get the length of the input string.
453 uint64_t SrcLen = GetStringLength(Src);
454 if (SrcLen == 0) return 0;
455 --SrcLen; // Unbias length.
457 // Handle the simple, do-nothing cases:
458 // strncat(x, "", c) -> x
459 // strncat(x, c, 0) -> x
460 if (SrcLen == 0 || Len == 0) return Dst;
462 // These optimizations require DataLayout.
465 // We don't optimize this case
466 if (Len < SrcLen) return 0;
468 // strncat(x, s, c) -> strcat(x, s)
469 // s is constant so the strcat can be optimized further
470 return emitStrLenMemCpy(Src, Dst, SrcLen, B);
474 struct StrChrOpt : public LibCallOptimization {
475 Value *callOptimizer(Function *Callee, CallInst *CI,
476 IRBuilder<> &B) override {
477 // Verify the "strchr" function prototype.
478 FunctionType *FT = Callee->getFunctionType();
479 if (FT->getNumParams() != 2 ||
480 FT->getReturnType() != B.getInt8PtrTy() ||
481 FT->getParamType(0) != FT->getReturnType() ||
482 !FT->getParamType(1)->isIntegerTy(32))
485 Value *SrcStr = CI->getArgOperand(0);
487 // If the second operand is non-constant, see if we can compute the length
488 // of the input string and turn this into memchr.
489 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
491 // These optimizations require DataLayout.
494 uint64_t Len = GetStringLength(SrcStr);
495 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
498 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
499 ConstantInt::get(DL->getIntPtrType(*Context), Len),
503 // Otherwise, the character is a constant, see if the first argument is
504 // a string literal. If so, we can constant fold.
506 if (!getConstantStringInfo(SrcStr, Str)) {
507 if (DL && CharC->isZero()) // strchr(p, 0) -> p + strlen(p)
508 return B.CreateGEP(SrcStr, EmitStrLen(SrcStr, B, DL, TLI), "strchr");
512 // Compute the offset, make sure to handle the case when we're searching for
513 // zero (a weird way to spell strlen).
514 size_t I = (0xFF & CharC->getSExtValue()) == 0 ?
515 Str.size() : Str.find(CharC->getSExtValue());
516 if (I == StringRef::npos) // Didn't find the char. strchr returns null.
517 return Constant::getNullValue(CI->getType());
519 // strchr(s+n,c) -> gep(s+n+i,c)
520 return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");
524 struct StrRChrOpt : public LibCallOptimization {
525 Value *callOptimizer(Function *Callee, CallInst *CI,
526 IRBuilder<> &B) override {
527 // Verify the "strrchr" function prototype.
528 FunctionType *FT = Callee->getFunctionType();
529 if (FT->getNumParams() != 2 ||
530 FT->getReturnType() != B.getInt8PtrTy() ||
531 FT->getParamType(0) != FT->getReturnType() ||
532 !FT->getParamType(1)->isIntegerTy(32))
535 Value *SrcStr = CI->getArgOperand(0);
536 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
538 // Cannot fold anything if we're not looking for a constant.
543 if (!getConstantStringInfo(SrcStr, Str)) {
544 // strrchr(s, 0) -> strchr(s, 0)
545 if (DL && CharC->isZero())
546 return EmitStrChr(SrcStr, '\0', B, DL, TLI);
550 // Compute the offset.
551 size_t I = (0xFF & CharC->getSExtValue()) == 0 ?
552 Str.size() : Str.rfind(CharC->getSExtValue());
553 if (I == StringRef::npos) // Didn't find the char. Return null.
554 return Constant::getNullValue(CI->getType());
556 // strrchr(s+n,c) -> gep(s+n+i,c)
557 return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");
561 struct StrCmpOpt : public LibCallOptimization {
562 Value *callOptimizer(Function *Callee, CallInst *CI,
563 IRBuilder<> &B) override {
564 // Verify the "strcmp" function prototype.
565 FunctionType *FT = Callee->getFunctionType();
566 if (FT->getNumParams() != 2 ||
567 !FT->getReturnType()->isIntegerTy(32) ||
568 FT->getParamType(0) != FT->getParamType(1) ||
569 FT->getParamType(0) != B.getInt8PtrTy())
572 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
573 if (Str1P == Str2P) // strcmp(x,x) -> 0
574 return ConstantInt::get(CI->getType(), 0);
576 StringRef Str1, Str2;
577 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
578 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
580 // strcmp(x, y) -> cnst (if both x and y are constant strings)
581 if (HasStr1 && HasStr2)
582 return ConstantInt::get(CI->getType(), Str1.compare(Str2));
584 if (HasStr1 && Str1.empty()) // strcmp("", x) -> -*x
585 return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
588 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
589 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
591 // strcmp(P, "x") -> memcmp(P, "x", 2)
592 uint64_t Len1 = GetStringLength(Str1P);
593 uint64_t Len2 = GetStringLength(Str2P);
595 // These optimizations require DataLayout.
598 return EmitMemCmp(Str1P, Str2P,
599 ConstantInt::get(DL->getIntPtrType(*Context),
600 std::min(Len1, Len2)), B, DL, TLI);
607 struct StrNCmpOpt : public LibCallOptimization {
608 Value *callOptimizer(Function *Callee, CallInst *CI,
609 IRBuilder<> &B) override {
610 // Verify the "strncmp" function prototype.
611 FunctionType *FT = Callee->getFunctionType();
612 if (FT->getNumParams() != 3 ||
613 !FT->getReturnType()->isIntegerTy(32) ||
614 FT->getParamType(0) != FT->getParamType(1) ||
615 FT->getParamType(0) != B.getInt8PtrTy() ||
616 !FT->getParamType(2)->isIntegerTy())
619 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
620 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
621 return ConstantInt::get(CI->getType(), 0);
623 // Get the length argument if it is constant.
625 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
626 Length = LengthArg->getZExtValue();
630 if (Length == 0) // strncmp(x,y,0) -> 0
631 return ConstantInt::get(CI->getType(), 0);
633 if (DL && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
634 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, DL, TLI);
636 StringRef Str1, Str2;
637 bool HasStr1 = getConstantStringInfo(Str1P, Str1);
638 bool HasStr2 = getConstantStringInfo(Str2P, Str2);
640 // strncmp(x, y) -> cnst (if both x and y are constant strings)
641 if (HasStr1 && HasStr2) {
642 StringRef SubStr1 = Str1.substr(0, Length);
643 StringRef SubStr2 = Str2.substr(0, Length);
644 return ConstantInt::get(CI->getType(), SubStr1.compare(SubStr2));
647 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> -*x
648 return B.CreateNeg(B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"),
651 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
652 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
658 struct StrCpyOpt : public LibCallOptimization {
659 Value *callOptimizer(Function *Callee, CallInst *CI,
660 IRBuilder<> &B) override {
661 // Verify the "strcpy" function prototype.
662 FunctionType *FT = Callee->getFunctionType();
663 if (FT->getNumParams() != 2 ||
664 FT->getReturnType() != FT->getParamType(0) ||
665 FT->getParamType(0) != FT->getParamType(1) ||
666 FT->getParamType(0) != B.getInt8PtrTy())
669 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
670 if (Dst == Src) // strcpy(x,x) -> x
673 // These optimizations require DataLayout.
676 // See if we can get the length of the input string.
677 uint64_t Len = GetStringLength(Src);
678 if (Len == 0) return 0;
680 // We have enough information to now generate the memcpy call to do the
681 // copy for us. Make a memcpy to copy the nul byte with align = 1.
682 B.CreateMemCpy(Dst, Src,
683 ConstantInt::get(DL->getIntPtrType(*Context), Len), 1);
688 struct StpCpyOpt: public LibCallOptimization {
689 Value *callOptimizer(Function *Callee, CallInst *CI,
690 IRBuilder<> &B) override {
691 // Verify the "stpcpy" function prototype.
692 FunctionType *FT = Callee->getFunctionType();
693 if (FT->getNumParams() != 2 ||
694 FT->getReturnType() != FT->getParamType(0) ||
695 FT->getParamType(0) != FT->getParamType(1) ||
696 FT->getParamType(0) != B.getInt8PtrTy())
699 // These optimizations require DataLayout.
702 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
703 if (Dst == Src) { // stpcpy(x,x) -> x+strlen(x)
704 Value *StrLen = EmitStrLen(Src, B, DL, TLI);
705 return StrLen ? B.CreateInBoundsGEP(Dst, StrLen) : 0;
708 // See if we can get the length of the input string.
709 uint64_t Len = GetStringLength(Src);
710 if (Len == 0) return 0;
712 Type *PT = FT->getParamType(0);
713 Value *LenV = ConstantInt::get(DL->getIntPtrType(PT), Len);
714 Value *DstEnd = B.CreateGEP(Dst,
715 ConstantInt::get(DL->getIntPtrType(PT),
718 // We have enough information to now generate the memcpy call to do the
719 // copy for us. Make a memcpy to copy the nul byte with align = 1.
720 B.CreateMemCpy(Dst, Src, LenV, 1);
725 struct StrNCpyOpt : public LibCallOptimization {
726 Value *callOptimizer(Function *Callee, CallInst *CI,
727 IRBuilder<> &B) override {
728 FunctionType *FT = Callee->getFunctionType();
729 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
730 FT->getParamType(0) != FT->getParamType(1) ||
731 FT->getParamType(0) != B.getInt8PtrTy() ||
732 !FT->getParamType(2)->isIntegerTy())
735 Value *Dst = CI->getArgOperand(0);
736 Value *Src = CI->getArgOperand(1);
737 Value *LenOp = CI->getArgOperand(2);
739 // See if we can get the length of the input string.
740 uint64_t SrcLen = GetStringLength(Src);
741 if (SrcLen == 0) return 0;
745 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
746 B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
751 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
752 Len = LengthArg->getZExtValue();
756 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
758 // These optimizations require DataLayout.
761 // Let strncpy handle the zero padding
762 if (Len > SrcLen+1) return 0;
764 Type *PT = FT->getParamType(0);
765 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
766 B.CreateMemCpy(Dst, Src,
767 ConstantInt::get(DL->getIntPtrType(PT), Len), 1);
773 struct StrLenOpt : public LibCallOptimization {
774 bool ignoreCallingConv() override { return true; }
775 Value *callOptimizer(Function *Callee, CallInst *CI,
776 IRBuilder<> &B) override {
777 FunctionType *FT = Callee->getFunctionType();
778 if (FT->getNumParams() != 1 ||
779 FT->getParamType(0) != B.getInt8PtrTy() ||
780 !FT->getReturnType()->isIntegerTy())
783 Value *Src = CI->getArgOperand(0);
785 // Constant folding: strlen("xyz") -> 3
786 if (uint64_t Len = GetStringLength(Src))
787 return ConstantInt::get(CI->getType(), Len-1);
789 // strlen(x) != 0 --> *x != 0
790 // strlen(x) == 0 --> *x == 0
791 if (isOnlyUsedInZeroEqualityComparison(CI))
792 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
797 struct StrPBrkOpt : public LibCallOptimization {
798 Value *callOptimizer(Function *Callee, CallInst *CI,
799 IRBuilder<> &B) override {
800 FunctionType *FT = Callee->getFunctionType();
801 if (FT->getNumParams() != 2 ||
802 FT->getParamType(0) != B.getInt8PtrTy() ||
803 FT->getParamType(1) != FT->getParamType(0) ||
804 FT->getReturnType() != FT->getParamType(0))
808 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
809 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
811 // strpbrk(s, "") -> NULL
812 // strpbrk("", s) -> NULL
813 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
814 return Constant::getNullValue(CI->getType());
817 if (HasS1 && HasS2) {
818 size_t I = S1.find_first_of(S2);
819 if (I == StringRef::npos) // No match.
820 return Constant::getNullValue(CI->getType());
822 return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");
825 // strpbrk(s, "a") -> strchr(s, 'a')
826 if (DL && HasS2 && S2.size() == 1)
827 return EmitStrChr(CI->getArgOperand(0), S2[0], B, DL, TLI);
833 struct StrToOpt : public LibCallOptimization {
834 Value *callOptimizer(Function *Callee, CallInst *CI,
835 IRBuilder<> &B) override {
836 FunctionType *FT = Callee->getFunctionType();
837 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
838 !FT->getParamType(0)->isPointerTy() ||
839 !FT->getParamType(1)->isPointerTy())
842 Value *EndPtr = CI->getArgOperand(1);
843 if (isa<ConstantPointerNull>(EndPtr)) {
844 // With a null EndPtr, this function won't capture the main argument.
845 // It would be readonly too, except that it still may write to errno.
846 CI->addAttribute(1, Attribute::NoCapture);
853 struct StrSpnOpt : public LibCallOptimization {
854 Value *callOptimizer(Function *Callee, CallInst *CI,
855 IRBuilder<> &B) override {
856 FunctionType *FT = Callee->getFunctionType();
857 if (FT->getNumParams() != 2 ||
858 FT->getParamType(0) != B.getInt8PtrTy() ||
859 FT->getParamType(1) != FT->getParamType(0) ||
860 !FT->getReturnType()->isIntegerTy())
864 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
865 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
867 // strspn(s, "") -> 0
868 // strspn("", s) -> 0
869 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
870 return Constant::getNullValue(CI->getType());
873 if (HasS1 && HasS2) {
874 size_t Pos = S1.find_first_not_of(S2);
875 if (Pos == StringRef::npos) Pos = S1.size();
876 return ConstantInt::get(CI->getType(), Pos);
883 struct StrCSpnOpt : public LibCallOptimization {
884 Value *callOptimizer(Function *Callee, CallInst *CI,
885 IRBuilder<> &B) override {
886 FunctionType *FT = Callee->getFunctionType();
887 if (FT->getNumParams() != 2 ||
888 FT->getParamType(0) != B.getInt8PtrTy() ||
889 FT->getParamType(1) != FT->getParamType(0) ||
890 !FT->getReturnType()->isIntegerTy())
894 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
895 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
897 // strcspn("", s) -> 0
898 if (HasS1 && S1.empty())
899 return Constant::getNullValue(CI->getType());
902 if (HasS1 && HasS2) {
903 size_t Pos = S1.find_first_of(S2);
904 if (Pos == StringRef::npos) Pos = S1.size();
905 return ConstantInt::get(CI->getType(), Pos);
908 // strcspn(s, "") -> strlen(s)
909 if (DL && HasS2 && S2.empty())
910 return EmitStrLen(CI->getArgOperand(0), B, DL, TLI);
916 struct StrStrOpt : public LibCallOptimization {
917 Value *callOptimizer(Function *Callee, CallInst *CI,
918 IRBuilder<> &B) override {
919 FunctionType *FT = Callee->getFunctionType();
920 if (FT->getNumParams() != 2 ||
921 !FT->getParamType(0)->isPointerTy() ||
922 !FT->getParamType(1)->isPointerTy() ||
923 !FT->getReturnType()->isPointerTy())
926 // fold strstr(x, x) -> x.
927 if (CI->getArgOperand(0) == CI->getArgOperand(1))
928 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
930 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
931 if (DL && isOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
932 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, DL, TLI);
935 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
939 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
941 ICmpInst *Old = cast<ICmpInst>(*UI++);
942 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
943 ConstantInt::getNullValue(StrNCmp->getType()),
945 LCS->replaceAllUsesWith(Old, Cmp);
950 // See if either input string is a constant string.
951 StringRef SearchStr, ToFindStr;
952 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
953 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
955 // fold strstr(x, "") -> x.
956 if (HasStr2 && ToFindStr.empty())
957 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
959 // If both strings are known, constant fold it.
960 if (HasStr1 && HasStr2) {
961 size_t Offset = SearchStr.find(ToFindStr);
963 if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
964 return Constant::getNullValue(CI->getType());
966 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
967 Value *Result = CastToCStr(CI->getArgOperand(0), B);
968 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
969 return B.CreateBitCast(Result, CI->getType());
972 // fold strstr(x, "y") -> strchr(x, 'y').
973 if (HasStr2 && ToFindStr.size() == 1) {
974 Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, DL, TLI);
975 return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : 0;
981 struct MemCmpOpt : public LibCallOptimization {
982 Value *callOptimizer(Function *Callee, CallInst *CI,
983 IRBuilder<> &B) override {
984 FunctionType *FT = Callee->getFunctionType();
985 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
986 !FT->getParamType(1)->isPointerTy() ||
987 !FT->getReturnType()->isIntegerTy(32))
990 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
992 if (LHS == RHS) // memcmp(s,s,x) -> 0
993 return Constant::getNullValue(CI->getType());
995 // Make sure we have a constant length.
996 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
998 uint64_t Len = LenC->getZExtValue();
1000 if (Len == 0) // memcmp(s1,s2,0) -> 0
1001 return Constant::getNullValue(CI->getType());
1003 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
1005 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
1006 CI->getType(), "lhsv");
1007 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
1008 CI->getType(), "rhsv");
1009 return B.CreateSub(LHSV, RHSV, "chardiff");
1012 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
1013 StringRef LHSStr, RHSStr;
1014 if (getConstantStringInfo(LHS, LHSStr) &&
1015 getConstantStringInfo(RHS, RHSStr)) {
1016 // Make sure we're not reading out-of-bounds memory.
1017 if (Len > LHSStr.size() || Len > RHSStr.size())
1019 // Fold the memcmp and normalize the result. This way we get consistent
1020 // results across multiple platforms.
1022 int Cmp = memcmp(LHSStr.data(), RHSStr.data(), Len);
1027 return ConstantInt::get(CI->getType(), Ret);
1034 struct MemCpyOpt : public LibCallOptimization {
1035 Value *callOptimizer(Function *Callee, CallInst *CI,
1036 IRBuilder<> &B) override {
1037 // These optimizations require DataLayout.
1040 FunctionType *FT = Callee->getFunctionType();
1041 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1042 !FT->getParamType(0)->isPointerTy() ||
1043 !FT->getParamType(1)->isPointerTy() ||
1044 FT->getParamType(2) != DL->getIntPtrType(*Context))
1047 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
1048 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1049 CI->getArgOperand(2), 1);
1050 return CI->getArgOperand(0);
1054 struct MemMoveOpt : public LibCallOptimization {
1055 Value *callOptimizer(Function *Callee, CallInst *CI,
1056 IRBuilder<> &B) override {
1057 // These optimizations require DataLayout.
1060 FunctionType *FT = Callee->getFunctionType();
1061 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1062 !FT->getParamType(0)->isPointerTy() ||
1063 !FT->getParamType(1)->isPointerTy() ||
1064 FT->getParamType(2) != DL->getIntPtrType(*Context))
1067 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
1068 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
1069 CI->getArgOperand(2), 1);
1070 return CI->getArgOperand(0);
1074 struct MemSetOpt : public LibCallOptimization {
1075 Value *callOptimizer(Function *Callee, CallInst *CI,
1076 IRBuilder<> &B) override {
1077 // These optimizations require DataLayout.
1080 FunctionType *FT = Callee->getFunctionType();
1081 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
1082 !FT->getParamType(0)->isPointerTy() ||
1083 !FT->getParamType(1)->isIntegerTy() ||
1084 FT->getParamType(2) != DL->getIntPtrType(FT->getParamType(0)))
1087 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
1088 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
1089 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
1090 return CI->getArgOperand(0);
1094 //===----------------------------------------------------------------------===//
1095 // Math Library Optimizations
1096 //===----------------------------------------------------------------------===//
1098 //===----------------------------------------------------------------------===//
1099 // Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
1101 struct UnaryDoubleFPOpt : public LibCallOptimization {
1103 UnaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {}
1104 Value *callOptimizer(Function *Callee, CallInst *CI,
1105 IRBuilder<> &B) override {
1106 FunctionType *FT = Callee->getFunctionType();
1107 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
1108 !FT->getParamType(0)->isDoubleTy())
1112 // Check if all the uses for function like 'sin' are converted to float.
1113 for (Value::use_iterator UseI = CI->use_begin(); UseI != CI->use_end();
1115 FPTruncInst *Cast = dyn_cast<FPTruncInst>(*UseI);
1116 if (Cast == 0 || !Cast->getType()->isFloatTy())
1121 // If this is something like 'floor((double)floatval)', convert to floorf.
1122 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
1123 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
1126 // floor((double)floatval) -> (double)floorf(floatval)
1127 Value *V = Cast->getOperand(0);
1128 V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
1129 return B.CreateFPExt(V, B.getDoubleTy());
1133 // Double -> Float Shrinking Optimizations for Binary Functions like 'fmin/fmax'
1134 struct BinaryDoubleFPOpt : public LibCallOptimization {
1136 BinaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {}
1137 Value *callOptimizer(Function *Callee, CallInst *CI,
1138 IRBuilder<> &B) override {
1139 FunctionType *FT = Callee->getFunctionType();
1140 // Just make sure this has 2 arguments of the same FP type, which match the
1142 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1143 FT->getParamType(0) != FT->getParamType(1) ||
1144 !FT->getParamType(0)->isFloatingPointTy())
1148 // Check if all the uses for function like 'fmin/fmax' are converted to
1150 for (Value::use_iterator UseI = CI->use_begin(); UseI != CI->use_end();
1152 FPTruncInst *Cast = dyn_cast<FPTruncInst>(*UseI);
1153 if (Cast == 0 || !Cast->getType()->isFloatTy())
1158 // If this is something like 'fmin((double)floatval1, (double)floatval2)',
1159 // we convert it to fminf.
1160 FPExtInst *Cast1 = dyn_cast<FPExtInst>(CI->getArgOperand(0));
1161 FPExtInst *Cast2 = dyn_cast<FPExtInst>(CI->getArgOperand(1));
1162 if (Cast1 == 0 || !Cast1->getOperand(0)->getType()->isFloatTy() ||
1163 Cast2 == 0 || !Cast2->getOperand(0)->getType()->isFloatTy())
1166 // fmin((double)floatval1, (double)floatval2)
1167 // -> (double)fmin(floatval1, floatval2)
1169 Value *V1 = Cast1->getOperand(0);
1170 Value *V2 = Cast2->getOperand(0);
1171 V = EmitBinaryFloatFnCall(V1, V2, Callee->getName(), B,
1172 Callee->getAttributes());
1173 return B.CreateFPExt(V, B.getDoubleTy());
1177 struct UnsafeFPLibCallOptimization : public LibCallOptimization {
1178 bool UnsafeFPShrink;
1179 UnsafeFPLibCallOptimization(bool UnsafeFPShrink) {
1180 this->UnsafeFPShrink = UnsafeFPShrink;
1184 struct CosOpt : public UnsafeFPLibCallOptimization {
1185 CosOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
1186 Value *callOptimizer(Function *Callee, CallInst *CI,
1187 IRBuilder<> &B) override {
1189 if (UnsafeFPShrink && Callee->getName() == "cos" &&
1190 TLI->has(LibFunc::cosf)) {
1191 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
1192 Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
1195 FunctionType *FT = Callee->getFunctionType();
1196 // Just make sure this has 1 argument of FP type, which matches the
1198 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1199 !FT->getParamType(0)->isFloatingPointTy())
1202 // cos(-x) -> cos(x)
1203 Value *Op1 = CI->getArgOperand(0);
1204 if (BinaryOperator::isFNeg(Op1)) {
1205 BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);
1206 return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");
1212 struct PowOpt : public UnsafeFPLibCallOptimization {
1213 PowOpt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
1214 Value *callOptimizer(Function *Callee, CallInst *CI,
1215 IRBuilder<> &B) override {
1217 if (UnsafeFPShrink && Callee->getName() == "pow" &&
1218 TLI->has(LibFunc::powf)) {
1219 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
1220 Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
1223 FunctionType *FT = Callee->getFunctionType();
1224 // Just make sure this has 2 arguments of the same FP type, which match the
1226 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
1227 FT->getParamType(0) != FT->getParamType(1) ||
1228 !FT->getParamType(0)->isFloatingPointTy())
1231 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
1232 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
1233 // pow(1.0, x) -> 1.0
1234 if (Op1C->isExactlyValue(1.0))
1236 // pow(2.0, x) -> exp2(x)
1237 if (Op1C->isExactlyValue(2.0) &&
1238 hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp2, LibFunc::exp2f,
1240 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
1241 // pow(10.0, x) -> exp10(x)
1242 if (Op1C->isExactlyValue(10.0) &&
1243 hasUnaryFloatFn(TLI, Op1->getType(), LibFunc::exp10, LibFunc::exp10f,
1245 return EmitUnaryFloatFnCall(Op2, TLI->getName(LibFunc::exp10), B,
1246 Callee->getAttributes());
1249 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
1250 if (Op2C == 0) return Ret;
1252 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
1253 return ConstantFP::get(CI->getType(), 1.0);
1255 if (Op2C->isExactlyValue(0.5) &&
1256 hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::sqrt, LibFunc::sqrtf,
1258 hasUnaryFloatFn(TLI, Op2->getType(), LibFunc::fabs, LibFunc::fabsf,
1260 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
1261 // This is faster than calling pow, and still handles negative zero
1262 // and negative infinity correctly.
1263 // TODO: In fast-math mode, this could be just sqrt(x).
1264 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
1265 Value *Inf = ConstantFP::getInfinity(CI->getType());
1266 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
1267 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
1268 Callee->getAttributes());
1269 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
1270 Callee->getAttributes());
1271 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
1272 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
1276 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
1278 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
1279 return B.CreateFMul(Op1, Op1, "pow2");
1280 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
1281 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
1287 struct Exp2Opt : public UnsafeFPLibCallOptimization {
1288 Exp2Opt(bool UnsafeFPShrink) : UnsafeFPLibCallOptimization(UnsafeFPShrink) {}
1289 Value *callOptimizer(Function *Callee, CallInst *CI,
1290 IRBuilder<> &B) override {
1292 if (UnsafeFPShrink && Callee->getName() == "exp2" &&
1293 TLI->has(LibFunc::exp2f)) {
1294 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
1295 Ret = UnsafeUnaryDoubleFP.callOptimizer(Callee, CI, B);
1298 FunctionType *FT = Callee->getFunctionType();
1299 // Just make sure this has 1 argument of FP type, which matches the
1301 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1302 !FT->getParamType(0)->isFloatingPointTy())
1305 Value *Op = CI->getArgOperand(0);
1306 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
1307 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
1308 LibFunc::Func LdExp = LibFunc::ldexpl;
1309 if (Op->getType()->isFloatTy())
1310 LdExp = LibFunc::ldexpf;
1311 else if (Op->getType()->isDoubleTy())
1312 LdExp = LibFunc::ldexp;
1314 if (TLI->has(LdExp)) {
1315 Value *LdExpArg = 0;
1316 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
1317 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
1318 LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
1319 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
1320 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
1321 LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
1325 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
1326 if (!Op->getType()->isFloatTy())
1327 One = ConstantExpr::getFPExtend(One, Op->getType());
1329 Module *M = Caller->getParent();
1331 M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(),
1332 Op->getType(), B.getInt32Ty(), NULL);
1333 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
1334 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
1335 CI->setCallingConv(F->getCallingConv());
1344 struct SinCosPiOpt : public LibCallOptimization {
1347 Value *callOptimizer(Function *Callee, CallInst *CI,
1348 IRBuilder<> &B) override {
1349 // Make sure the prototype is as expected, otherwise the rest of the
1350 // function is probably invalid and likely to abort.
1351 if (!isTrigLibCall(CI))
1354 Value *Arg = CI->getArgOperand(0);
1355 SmallVector<CallInst *, 1> SinCalls;
1356 SmallVector<CallInst *, 1> CosCalls;
1357 SmallVector<CallInst *, 1> SinCosCalls;
1359 bool IsFloat = Arg->getType()->isFloatTy();
1361 // Look for all compatible sinpi, cospi and sincospi calls with the same
1362 // argument. If there are enough (in some sense) we can make the
1364 for (Value::use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
1366 classifyArgUse(*UI, CI->getParent(), IsFloat, SinCalls, CosCalls,
1369 // It's only worthwhile if both sinpi and cospi are actually used.
1370 if (SinCosCalls.empty() && (SinCalls.empty() || CosCalls.empty()))
1373 Value *Sin, *Cos, *SinCos;
1374 insertSinCosCall(B, CI->getCalledFunction(), Arg, IsFloat, Sin, Cos,
1377 replaceTrigInsts(SinCalls, Sin);
1378 replaceTrigInsts(CosCalls, Cos);
1379 replaceTrigInsts(SinCosCalls, SinCos);
1384 bool isTrigLibCall(CallInst *CI) {
1385 Function *Callee = CI->getCalledFunction();
1386 FunctionType *FT = Callee->getFunctionType();
1388 // We can only hope to do anything useful if we can ignore things like errno
1389 // and floating-point exceptions.
1390 bool AttributesSafe = CI->hasFnAttr(Attribute::NoUnwind) &&
1391 CI->hasFnAttr(Attribute::ReadNone);
1393 // Other than that we need float(float) or double(double)
1394 return AttributesSafe && FT->getNumParams() == 1 &&
1395 FT->getReturnType() == FT->getParamType(0) &&
1396 (FT->getParamType(0)->isFloatTy() ||
1397 FT->getParamType(0)->isDoubleTy());
1400 void classifyArgUse(Value *Val, BasicBlock *BB, bool IsFloat,
1401 SmallVectorImpl<CallInst *> &SinCalls,
1402 SmallVectorImpl<CallInst *> &CosCalls,
1403 SmallVectorImpl<CallInst *> &SinCosCalls) {
1404 CallInst *CI = dyn_cast<CallInst>(Val);
1409 Function *Callee = CI->getCalledFunction();
1410 StringRef FuncName = Callee->getName();
1412 if (!TLI->getLibFunc(FuncName, Func) || !TLI->has(Func) ||
1417 if (Func == LibFunc::sinpif)
1418 SinCalls.push_back(CI);
1419 else if (Func == LibFunc::cospif)
1420 CosCalls.push_back(CI);
1421 else if (Func == LibFunc::sincospif_stret)
1422 SinCosCalls.push_back(CI);
1424 if (Func == LibFunc::sinpi)
1425 SinCalls.push_back(CI);
1426 else if (Func == LibFunc::cospi)
1427 CosCalls.push_back(CI);
1428 else if (Func == LibFunc::sincospi_stret)
1429 SinCosCalls.push_back(CI);
1433 void replaceTrigInsts(SmallVectorImpl<CallInst*> &Calls, Value *Res) {
1434 for (SmallVectorImpl<CallInst*>::iterator I = Calls.begin(),
1437 LCS->replaceAllUsesWith(*I, Res);
1441 void insertSinCosCall(IRBuilder<> &B, Function *OrigCallee, Value *Arg,
1442 bool UseFloat, Value *&Sin, Value *&Cos,
1444 Type *ArgTy = Arg->getType();
1448 Triple T(OrigCallee->getParent()->getTargetTriple());
1450 Name = "__sincospif_stret";
1452 assert(T.getArch() != Triple::x86 && "x86 messy and unsupported for now");
1453 // x86_64 can't use {float, float} since that would be returned in both
1454 // xmm0 and xmm1, which isn't what a real struct would do.
1455 ResTy = T.getArch() == Triple::x86_64
1456 ? static_cast<Type *>(VectorType::get(ArgTy, 2))
1457 : static_cast<Type *>(StructType::get(ArgTy, ArgTy, NULL));
1459 Name = "__sincospi_stret";
1460 ResTy = StructType::get(ArgTy, ArgTy, NULL);
1463 Module *M = OrigCallee->getParent();
1464 Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(),
1465 ResTy, ArgTy, NULL);
1467 if (Instruction *ArgInst = dyn_cast<Instruction>(Arg)) {
1468 // If the argument is an instruction, it must dominate all uses so put our
1469 // sincos call there.
1470 BasicBlock::iterator Loc = ArgInst;
1471 B.SetInsertPoint(ArgInst->getParent(), ++Loc);
1473 // Otherwise (e.g. for a constant) the beginning of the function is as
1474 // good a place as any.
1475 BasicBlock &EntryBB = B.GetInsertBlock()->getParent()->getEntryBlock();
1476 B.SetInsertPoint(&EntryBB, EntryBB.begin());
1479 SinCos = B.CreateCall(Callee, Arg, "sincospi");
1481 if (SinCos->getType()->isStructTy()) {
1482 Sin = B.CreateExtractValue(SinCos, 0, "sinpi");
1483 Cos = B.CreateExtractValue(SinCos, 1, "cospi");
1485 Sin = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 0),
1487 Cos = B.CreateExtractElement(SinCos, ConstantInt::get(B.getInt32Ty(), 1),
1494 //===----------------------------------------------------------------------===//
1495 // Integer Library Call Optimizations
1496 //===----------------------------------------------------------------------===//
1498 struct FFSOpt : public LibCallOptimization {
1499 Value *callOptimizer(Function *Callee, CallInst *CI,
1500 IRBuilder<> &B) override {
1501 FunctionType *FT = Callee->getFunctionType();
1502 // Just make sure this has 2 arguments of the same FP type, which match the
1504 if (FT->getNumParams() != 1 ||
1505 !FT->getReturnType()->isIntegerTy(32) ||
1506 !FT->getParamType(0)->isIntegerTy())
1509 Value *Op = CI->getArgOperand(0);
1512 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1513 if (CI->isZero()) // ffs(0) -> 0.
1514 return B.getInt32(0);
1515 // ffs(c) -> cttz(c)+1
1516 return B.getInt32(CI->getValue().countTrailingZeros() + 1);
1519 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1520 Type *ArgType = Op->getType();
1521 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1522 Intrinsic::cttz, ArgType);
1523 Value *V = B.CreateCall2(F, Op, B.getFalse(), "cttz");
1524 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
1525 V = B.CreateIntCast(V, B.getInt32Ty(), false);
1527 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
1528 return B.CreateSelect(Cond, V, B.getInt32(0));
1532 struct AbsOpt : public LibCallOptimization {
1533 bool ignoreCallingConv() override { return true; }
1534 Value *callOptimizer(Function *Callee, CallInst *CI,
1535 IRBuilder<> &B) override {
1536 FunctionType *FT = Callee->getFunctionType();
1537 // We require integer(integer) where the types agree.
1538 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1539 FT->getParamType(0) != FT->getReturnType())
1542 // abs(x) -> x >s -1 ? x : -x
1543 Value *Op = CI->getArgOperand(0);
1544 Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),
1546 Value *Neg = B.CreateNeg(Op, "neg");
1547 return B.CreateSelect(Pos, Op, Neg);
1551 struct IsDigitOpt : public LibCallOptimization {
1552 Value *callOptimizer(Function *Callee, CallInst *CI,
1553 IRBuilder<> &B) override {
1554 FunctionType *FT = Callee->getFunctionType();
1555 // We require integer(i32)
1556 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1557 !FT->getParamType(0)->isIntegerTy(32))
1560 // isdigit(c) -> (c-'0') <u 10
1561 Value *Op = CI->getArgOperand(0);
1562 Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
1563 Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
1564 return B.CreateZExt(Op, CI->getType());
1568 struct IsAsciiOpt : public LibCallOptimization {
1569 Value *callOptimizer(Function *Callee, CallInst *CI,
1570 IRBuilder<> &B) override {
1571 FunctionType *FT = Callee->getFunctionType();
1572 // We require integer(i32)
1573 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1574 !FT->getParamType(0)->isIntegerTy(32))
1577 // isascii(c) -> c <u 128
1578 Value *Op = CI->getArgOperand(0);
1579 Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
1580 return B.CreateZExt(Op, CI->getType());
1584 struct ToAsciiOpt : public LibCallOptimization {
1585 Value *callOptimizer(Function *Callee, CallInst *CI,
1586 IRBuilder<> &B) override {
1587 FunctionType *FT = Callee->getFunctionType();
1588 // We require i32(i32)
1589 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1590 !FT->getParamType(0)->isIntegerTy(32))
1593 // toascii(c) -> c & 0x7f
1594 return B.CreateAnd(CI->getArgOperand(0),
1595 ConstantInt::get(CI->getType(),0x7F));
1599 //===----------------------------------------------------------------------===//
1600 // Formatting and IO Library Call Optimizations
1601 //===----------------------------------------------------------------------===//
1603 struct ErrorReportingOpt : public LibCallOptimization {
1604 ErrorReportingOpt(int S = -1) : StreamArg(S) {}
1606 Value *callOptimizer(Function *Callee, CallInst *CI,
1607 IRBuilder<> &) override {
1608 // Error reporting calls should be cold, mark them as such.
1609 // This applies even to non-builtin calls: it is only a hint and applies to
1610 // functions that the frontend might not understand as builtins.
1612 // This heuristic was suggested in:
1613 // Improving Static Branch Prediction in a Compiler
1614 // Brian L. Deitrich, Ben-Chung Cheng, Wen-mei W. Hwu
1615 // Proceedings of PACT'98, Oct. 1998, IEEE
1617 if (!CI->hasFnAttr(Attribute::Cold) && isReportingError(Callee, CI)) {
1618 CI->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold);
1625 bool isReportingError(Function *Callee, CallInst *CI) {
1626 if (!ColdErrorCalls)
1629 if (!Callee || !Callee->isDeclaration())
1635 // These functions might be considered cold, but only if their stream
1636 // argument is stderr.
1638 if (StreamArg >= (int) CI->getNumArgOperands())
1640 LoadInst *LI = dyn_cast<LoadInst>(CI->getArgOperand(StreamArg));
1643 GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getPointerOperand());
1644 if (!GV || !GV->isDeclaration())
1646 return GV->getName() == "stderr";
1652 struct PrintFOpt : public LibCallOptimization {
1653 Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,
1655 // Check for a fixed format string.
1656 StringRef FormatStr;
1657 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
1660 // Empty format string -> noop.
1661 if (FormatStr.empty()) // Tolerate printf's declared void.
1662 return CI->use_empty() ? (Value*)CI :
1663 ConstantInt::get(CI->getType(), 0);
1665 // Do not do any of the following transformations if the printf return value
1666 // is used, in general the printf return value is not compatible with either
1667 // putchar() or puts().
1668 if (!CI->use_empty())
1671 // printf("x") -> putchar('x'), even for '%'.
1672 if (FormatStr.size() == 1) {
1673 Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, DL, TLI);
1674 if (CI->use_empty() || !Res) return Res;
1675 return B.CreateIntCast(Res, CI->getType(), true);
1678 // printf("foo\n") --> puts("foo")
1679 if (FormatStr[FormatStr.size()-1] == '\n' &&
1680 FormatStr.find('%') == StringRef::npos) { // No format characters.
1681 // Create a string literal with no \n on it. We expect the constant merge
1682 // pass to be run after this pass, to merge duplicate strings.
1683 FormatStr = FormatStr.drop_back();
1684 Value *GV = B.CreateGlobalString(FormatStr, "str");
1685 Value *NewCI = EmitPutS(GV, B, DL, TLI);
1686 return (CI->use_empty() || !NewCI) ?
1688 ConstantInt::get(CI->getType(), FormatStr.size()+1);
1691 // Optimize specific format strings.
1692 // printf("%c", chr) --> putchar(chr)
1693 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
1694 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1695 Value *Res = EmitPutChar(CI->getArgOperand(1), B, DL, TLI);
1697 if (CI->use_empty() || !Res) return Res;
1698 return B.CreateIntCast(Res, CI->getType(), true);
1701 // printf("%s\n", str) --> puts(str)
1702 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
1703 CI->getArgOperand(1)->getType()->isPointerTy()) {
1704 return EmitPutS(CI->getArgOperand(1), B, DL, TLI);
1709 Value *callOptimizer(Function *Callee, CallInst *CI,
1710 IRBuilder<> &B) override {
1711 // Require one fixed pointer argument and an integer/void result.
1712 FunctionType *FT = Callee->getFunctionType();
1713 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1714 !(FT->getReturnType()->isIntegerTy() ||
1715 FT->getReturnType()->isVoidTy()))
1718 if (Value *V = optimizeFixedFormatString(Callee, CI, B)) {
1722 // printf(format, ...) -> iprintf(format, ...) if no floating point
1724 if (TLI->has(LibFunc::iprintf) && !callHasFloatingPointArgument(CI)) {
1725 Module *M = B.GetInsertBlock()->getParent()->getParent();
1726 Constant *IPrintFFn =
1727 M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
1728 CallInst *New = cast<CallInst>(CI->clone());
1729 New->setCalledFunction(IPrintFFn);
1737 struct SPrintFOpt : public LibCallOptimization {
1738 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
1740 // Check for a fixed format string.
1741 StringRef FormatStr;
1742 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1745 // If we just have a format string (nothing else crazy) transform it.
1746 if (CI->getNumArgOperands() == 2) {
1747 // Make sure there's no % in the constant array. We could try to handle
1748 // %% -> % in the future if we cared.
1749 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1750 if (FormatStr[i] == '%')
1751 return 0; // we found a format specifier, bail out.
1753 // These optimizations require DataLayout.
1756 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1757 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1758 ConstantInt::get(DL->getIntPtrType(*Context), // Copy the
1759 FormatStr.size() + 1), 1); // nul byte.
1760 return ConstantInt::get(CI->getType(), FormatStr.size());
1763 // The remaining optimizations require the format string to be "%s" or "%c"
1764 // and have an extra operand.
1765 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1766 CI->getNumArgOperands() < 3)
1769 // Decode the second character of the format string.
1770 if (FormatStr[1] == 'c') {
1771 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1772 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1773 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
1774 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
1775 B.CreateStore(V, Ptr);
1776 Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
1777 B.CreateStore(B.getInt8(0), Ptr);
1779 return ConstantInt::get(CI->getType(), 1);
1782 if (FormatStr[1] == 's') {
1783 // These optimizations require DataLayout.
1786 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1787 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
1789 Value *Len = EmitStrLen(CI->getArgOperand(2), B, DL, TLI);
1792 Value *IncLen = B.CreateAdd(Len,
1793 ConstantInt::get(Len->getType(), 1),
1795 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
1797 // The sprintf result is the unincremented number of bytes in the string.
1798 return B.CreateIntCast(Len, CI->getType(), false);
1803 Value *callOptimizer(Function *Callee, CallInst *CI,
1804 IRBuilder<> &B) override {
1805 // Require two fixed pointer arguments and an integer result.
1806 FunctionType *FT = Callee->getFunctionType();
1807 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1808 !FT->getParamType(1)->isPointerTy() ||
1809 !FT->getReturnType()->isIntegerTy())
1812 if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
1816 // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
1818 if (TLI->has(LibFunc::siprintf) && !callHasFloatingPointArgument(CI)) {
1819 Module *M = B.GetInsertBlock()->getParent()->getParent();
1820 Constant *SIPrintFFn =
1821 M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
1822 CallInst *New = cast<CallInst>(CI->clone());
1823 New->setCalledFunction(SIPrintFFn);
1831 struct FPrintFOpt : public LibCallOptimization {
1832 Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,
1834 ErrorReportingOpt ER(/* StreamArg = */ 0);
1835 (void) ER.callOptimizer(Callee, CI, B);
1837 // All the optimizations depend on the format string.
1838 StringRef FormatStr;
1839 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
1842 // Do not do any of the following transformations if the fprintf return
1843 // value is used, in general the fprintf return value is not compatible
1844 // with fwrite(), fputc() or fputs().
1845 if (!CI->use_empty())
1848 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1849 if (CI->getNumArgOperands() == 2) {
1850 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1851 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
1852 return 0; // We found a format specifier.
1854 // These optimizations require DataLayout.
1857 return EmitFWrite(CI->getArgOperand(1),
1858 ConstantInt::get(DL->getIntPtrType(*Context),
1860 CI->getArgOperand(0), B, DL, TLI);
1863 // The remaining optimizations require the format string to be "%s" or "%c"
1864 // and have an extra operand.
1865 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1866 CI->getNumArgOperands() < 3)
1869 // Decode the second character of the format string.
1870 if (FormatStr[1] == 'c') {
1871 // fprintf(F, "%c", chr) --> fputc(chr, F)
1872 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1873 return EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, DL, TLI);
1876 if (FormatStr[1] == 's') {
1877 // fprintf(F, "%s", str) --> fputs(str, F)
1878 if (!CI->getArgOperand(2)->getType()->isPointerTy())
1880 return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, DL, TLI);
1885 Value *callOptimizer(Function *Callee, CallInst *CI,
1886 IRBuilder<> &B) override {
1887 // Require two fixed paramters as pointers and integer result.
1888 FunctionType *FT = Callee->getFunctionType();
1889 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1890 !FT->getParamType(1)->isPointerTy() ||
1891 !FT->getReturnType()->isIntegerTy())
1894 if (Value *V = optimizeFixedFormatString(Callee, CI, B)) {
1898 // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
1899 // floating point arguments.
1900 if (TLI->has(LibFunc::fiprintf) && !callHasFloatingPointArgument(CI)) {
1901 Module *M = B.GetInsertBlock()->getParent()->getParent();
1902 Constant *FIPrintFFn =
1903 M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
1904 CallInst *New = cast<CallInst>(CI->clone());
1905 New->setCalledFunction(FIPrintFFn);
1913 struct FWriteOpt : public LibCallOptimization {
1914 Value *callOptimizer(Function *Callee, CallInst *CI,
1915 IRBuilder<> &B) override {
1916 ErrorReportingOpt ER(/* StreamArg = */ 3);
1917 (void) ER.callOptimizer(Callee, CI, B);
1919 // Require a pointer, an integer, an integer, a pointer, returning integer.
1920 FunctionType *FT = Callee->getFunctionType();
1921 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1922 !FT->getParamType(1)->isIntegerTy() ||
1923 !FT->getParamType(2)->isIntegerTy() ||
1924 !FT->getParamType(3)->isPointerTy() ||
1925 !FT->getReturnType()->isIntegerTy())
1928 // Get the element size and count.
1929 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1930 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1931 if (!SizeC || !CountC) return 0;
1932 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
1934 // If this is writing zero records, remove the call (it's a noop).
1936 return ConstantInt::get(CI->getType(), 0);
1938 // If this is writing one byte, turn it into fputc.
1939 // This optimisation is only valid, if the return value is unused.
1940 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1941 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1942 Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, DL, TLI);
1943 return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
1950 struct FPutsOpt : public LibCallOptimization {
1951 Value *callOptimizer(Function *Callee, CallInst *CI,
1952 IRBuilder<> &B) override {
1953 ErrorReportingOpt ER(/* StreamArg = */ 1);
1954 (void) ER.callOptimizer(Callee, CI, B);
1956 // These optimizations require DataLayout.
1959 // Require two pointers. Also, we can't optimize if return value is used.
1960 FunctionType *FT = Callee->getFunctionType();
1961 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1962 !FT->getParamType(1)->isPointerTy() ||
1966 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1967 uint64_t Len = GetStringLength(CI->getArgOperand(0));
1969 // Known to have no uses (see above).
1970 return EmitFWrite(CI->getArgOperand(0),
1971 ConstantInt::get(DL->getIntPtrType(*Context), Len-1),
1972 CI->getArgOperand(1), B, DL, TLI);
1976 struct PutsOpt : public LibCallOptimization {
1977 Value *callOptimizer(Function *Callee, CallInst *CI,
1978 IRBuilder<> &B) override {
1979 // Require one fixed pointer argument and an integer/void result.
1980 FunctionType *FT = Callee->getFunctionType();
1981 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1982 !(FT->getReturnType()->isIntegerTy() ||
1983 FT->getReturnType()->isVoidTy()))
1986 // Check for a constant string.
1988 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
1991 if (Str.empty() && CI->use_empty()) {
1992 // puts("") -> putchar('\n')
1993 Value *Res = EmitPutChar(B.getInt32('\n'), B, DL, TLI);
1994 if (CI->use_empty() || !Res) return Res;
1995 return B.CreateIntCast(Res, CI->getType(), true);
2002 } // End anonymous namespace.
2006 class LibCallSimplifierImpl {
2007 const DataLayout *DL;
2008 const TargetLibraryInfo *TLI;
2009 const LibCallSimplifier *LCS;
2010 bool UnsafeFPShrink;
2012 // Math library call optimizations.
2017 LibCallSimplifierImpl(const DataLayout *DL, const TargetLibraryInfo *TLI,
2018 const LibCallSimplifier *LCS,
2019 bool UnsafeFPShrink = false)
2020 : Cos(UnsafeFPShrink), Pow(UnsafeFPShrink), Exp2(UnsafeFPShrink) {
2024 this->UnsafeFPShrink = UnsafeFPShrink;
2027 Value *optimizeCall(CallInst *CI);
2028 LibCallOptimization *lookupOptimization(CallInst *CI);
2029 bool hasFloatVersion(StringRef FuncName);
2032 bool LibCallSimplifierImpl::hasFloatVersion(StringRef FuncName) {
2034 SmallString<20> FloatFuncName = FuncName;
2035 FloatFuncName += 'f';
2036 if (TLI->getLibFunc(FloatFuncName, Func))
2037 return TLI->has(Func);
2041 // Fortified library call optimizations.
2042 static MemCpyChkOpt MemCpyChk;
2043 static MemMoveChkOpt MemMoveChk;
2044 static MemSetChkOpt MemSetChk;
2045 static StrCpyChkOpt StrCpyChk;
2046 static StpCpyChkOpt StpCpyChk;
2047 static StrNCpyChkOpt StrNCpyChk;
2049 // String library call optimizations.
2050 static StrCatOpt StrCat;
2051 static StrNCatOpt StrNCat;
2052 static StrChrOpt StrChr;
2053 static StrRChrOpt StrRChr;
2054 static StrCmpOpt StrCmp;
2055 static StrNCmpOpt StrNCmp;
2056 static StrCpyOpt StrCpy;
2057 static StpCpyOpt StpCpy;
2058 static StrNCpyOpt StrNCpy;
2059 static StrLenOpt StrLen;
2060 static StrPBrkOpt StrPBrk;
2061 static StrToOpt StrTo;
2062 static StrSpnOpt StrSpn;
2063 static StrCSpnOpt StrCSpn;
2064 static StrStrOpt StrStr;
2066 // Memory library call optimizations.
2067 static MemCmpOpt MemCmp;
2068 static MemCpyOpt MemCpy;
2069 static MemMoveOpt MemMove;
2070 static MemSetOpt MemSet;
2072 // Math library call optimizations.
2073 static UnaryDoubleFPOpt UnaryDoubleFP(false);
2074 static BinaryDoubleFPOpt BinaryDoubleFP(false);
2075 static UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
2076 static SinCosPiOpt SinCosPi;
2078 // Integer library call optimizations.
2081 static IsDigitOpt IsDigit;
2082 static IsAsciiOpt IsAscii;
2083 static ToAsciiOpt ToAscii;
2085 // Formatting and IO library call optimizations.
2086 static ErrorReportingOpt ErrorReporting;
2087 static ErrorReportingOpt ErrorReporting0(0);
2088 static ErrorReportingOpt ErrorReporting1(1);
2089 static PrintFOpt PrintF;
2090 static SPrintFOpt SPrintF;
2091 static FPrintFOpt FPrintF;
2092 static FWriteOpt FWrite;
2093 static FPutsOpt FPuts;
2094 static PutsOpt Puts;
2096 LibCallOptimization *LibCallSimplifierImpl::lookupOptimization(CallInst *CI) {
2098 Function *Callee = CI->getCalledFunction();
2099 StringRef FuncName = Callee->getName();
2101 // Next check for intrinsics.
2102 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) {
2103 switch (II->getIntrinsicID()) {
2104 case Intrinsic::pow:
2106 case Intrinsic::exp2:
2113 // Then check for known library functions.
2114 if (TLI->getLibFunc(FuncName, Func) && TLI->has(Func)) {
2116 case LibFunc::strcat:
2118 case LibFunc::strncat:
2120 case LibFunc::strchr:
2122 case LibFunc::strrchr:
2124 case LibFunc::strcmp:
2126 case LibFunc::strncmp:
2128 case LibFunc::strcpy:
2130 case LibFunc::stpcpy:
2132 case LibFunc::strncpy:
2134 case LibFunc::strlen:
2136 case LibFunc::strpbrk:
2138 case LibFunc::strtol:
2139 case LibFunc::strtod:
2140 case LibFunc::strtof:
2141 case LibFunc::strtoul:
2142 case LibFunc::strtoll:
2143 case LibFunc::strtold:
2144 case LibFunc::strtoull:
2146 case LibFunc::strspn:
2148 case LibFunc::strcspn:
2150 case LibFunc::strstr:
2152 case LibFunc::memcmp:
2154 case LibFunc::memcpy:
2156 case LibFunc::memmove:
2158 case LibFunc::memset:
2164 case LibFunc::sinpif:
2165 case LibFunc::sinpi:
2166 case LibFunc::cospif:
2167 case LibFunc::cospi:
2173 case LibFunc::exp2l:
2175 case LibFunc::exp2f:
2179 case LibFunc::ffsll:
2183 case LibFunc::llabs:
2185 case LibFunc::isdigit:
2187 case LibFunc::isascii:
2189 case LibFunc::toascii:
2191 case LibFunc::printf:
2193 case LibFunc::sprintf:
2195 case LibFunc::fprintf:
2197 case LibFunc::fwrite:
2199 case LibFunc::fputs:
2203 case LibFunc::perror:
2204 return &ErrorReporting;
2205 case LibFunc::vfprintf:
2206 case LibFunc::fiprintf:
2207 return &ErrorReporting0;
2208 case LibFunc::fputc:
2209 return &ErrorReporting1;
2212 case LibFunc::floor:
2214 case LibFunc::round:
2215 case LibFunc::nearbyint:
2216 case LibFunc::trunc:
2217 if (hasFloatVersion(FuncName))
2218 return &UnaryDoubleFP;
2221 case LibFunc::acosh:
2223 case LibFunc::asinh:
2225 case LibFunc::atanh:
2229 case LibFunc::exp10:
2230 case LibFunc::expm1:
2232 case LibFunc::log10:
2233 case LibFunc::log1p:
2241 if (UnsafeFPShrink && hasFloatVersion(FuncName))
2242 return &UnsafeUnaryDoubleFP;
2246 if (hasFloatVersion(FuncName))
2247 return &BinaryDoubleFP;
2249 case LibFunc::memcpy_chk:
2256 // Finally check for fortified library calls.
2257 if (FuncName.endswith("_chk")) {
2258 if (FuncName == "__memmove_chk")
2260 else if (FuncName == "__memset_chk")
2262 else if (FuncName == "__strcpy_chk")
2264 else if (FuncName == "__stpcpy_chk")
2266 else if (FuncName == "__strncpy_chk")
2268 else if (FuncName == "__stpncpy_chk")
2276 Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) {
2277 LibCallOptimization *LCO = lookupOptimization(CI);
2279 IRBuilder<> Builder(CI);
2280 return LCO->optimizeCall(CI, DL, TLI, LCS, Builder);
2285 LibCallSimplifier::LibCallSimplifier(const DataLayout *DL,
2286 const TargetLibraryInfo *TLI,
2287 bool UnsafeFPShrink) {
2288 Impl = new LibCallSimplifierImpl(DL, TLI, this, UnsafeFPShrink);
2291 LibCallSimplifier::~LibCallSimplifier() {
2295 Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
2296 if (CI->isNoBuiltin()) return 0;
2297 return Impl->optimizeCall(CI);
2300 void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) const {
2301 I->replaceAllUsesWith(With);
2302 I->eraseFromParent();
2308 // Additional cases that we need to add to this file:
2311 // * cbrt(expN(X)) -> expN(x/3)
2312 // * cbrt(sqrt(x)) -> pow(x,1/6)
2313 // * cbrt(sqrt(x)) -> pow(x,1/9)
2316 // * exp(log(x)) -> x
2319 // * log(exp(x)) -> x
2320 // * log(x**y) -> y*log(x)
2321 // * log(exp(y)) -> y*log(e)
2322 // * log(exp2(y)) -> y*log(2)
2323 // * log(exp10(y)) -> y*log(10)
2324 // * log(sqrt(x)) -> 0.5*log(x)
2325 // * log(pow(x,y)) -> y*log(x)
2327 // lround, lroundf, lroundl:
2328 // * lround(cnst) -> cnst'
2331 // * pow(exp(x),y) -> exp(x*y)
2332 // * pow(sqrt(x),y) -> pow(x,y*0.5)
2333 // * pow(pow(x,y),z)-> pow(x,y*z)
2335 // round, roundf, roundl:
2336 // * round(cnst) -> cnst'
2339 // * signbit(cnst) -> cnst'
2340 // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2342 // sqrt, sqrtf, sqrtl:
2343 // * sqrt(expN(x)) -> expN(x*0.5)
2344 // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2345 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2348 // * tan(atan(x)) -> x
2350 // trunc, truncf, truncl:
2351 // * trunc(cnst) -> cnst'