1 //===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
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 file implements a simple pass that applies a variety of small
11 // optimizations for calls to specific well-known function calls (e.g. runtime
12 // library functions). Any optimization that takes the very simple form
13 // "replace call to library function with simpler code that provides the same
14 // result" belongs in this file.
16 //===----------------------------------------------------------------------===//
18 #define DEBUG_TYPE "simplify-libcalls"
19 #include "llvm/Transforms/Scalar.h"
20 #include "llvm/Transforms/Utils/BuildLibCalls.h"
21 #include "llvm/IRBuilder.h"
22 #include "llvm/Intrinsics.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/Module.h"
25 #include "llvm/Pass.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/DataLayout.h"
35 #include "llvm/Target/TargetLibraryInfo.h"
36 #include "llvm/Config/config.h" // FIXME: Shouldn't depend on host!
39 STATISTIC(NumSimplified, "Number of library calls simplified");
40 STATISTIC(NumAnnotated, "Number of attributes added to library functions");
42 static cl::opt<bool> UnsafeFPShrink("enable-double-float-shrink", cl::Hidden,
44 cl::desc("Enable unsafe double to float "
45 "shrinking for math lib calls"));
46 //===----------------------------------------------------------------------===//
47 // Optimizer Base Class
48 //===----------------------------------------------------------------------===//
50 /// This class is the abstract base class for the set of optimizations that
51 /// corresponds to one library call.
53 class LibCallOptimization {
57 const TargetLibraryInfo *TLI;
60 LibCallOptimization() { }
61 virtual ~LibCallOptimization() {}
63 /// CallOptimizer - This pure virtual method is implemented by base classes to
64 /// do various optimizations. If this returns null then no transformation was
65 /// performed. If it returns CI, then it transformed the call and CI is to be
66 /// deleted. If it returns something else, replace CI with the new value and
68 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
71 Value *OptimizeCall(CallInst *CI, const DataLayout *TD,
72 const TargetLibraryInfo *TLI, IRBuilder<> &B) {
73 Caller = CI->getParent()->getParent();
76 if (CI->getCalledFunction())
77 Context = &CI->getCalledFunction()->getContext();
79 // We never change the calling convention.
80 if (CI->getCallingConv() != llvm::CallingConv::C)
83 return CallOptimizer(CI->getCalledFunction(), CI, B);
86 } // End anonymous namespace.
89 //===----------------------------------------------------------------------===//
91 //===----------------------------------------------------------------------===//
93 static bool CallHasFloatingPointArgument(const CallInst *CI) {
94 for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
96 if ((*it)->getType()->isFloatingPointTy())
102 /// IsOnlyUsedInEqualityComparison - Return true if it is only used in equality
103 /// comparisons with With.
104 static bool IsOnlyUsedInEqualityComparison(Value *V, Value *With) {
105 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
107 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
108 if (IC->isEquality() && IC->getOperand(1) == With)
110 // Unknown instruction.
116 //===----------------------------------------------------------------------===//
117 // String and Memory LibCall Optimizations
118 //===----------------------------------------------------------------------===//
121 //===---------------------------------------===//
122 // 'strcspn' Optimizations
124 struct StrCSpnOpt : public LibCallOptimization {
125 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
126 FunctionType *FT = Callee->getFunctionType();
127 if (FT->getNumParams() != 2 ||
128 FT->getParamType(0) != B.getInt8PtrTy() ||
129 FT->getParamType(1) != FT->getParamType(0) ||
130 !FT->getReturnType()->isIntegerTy())
134 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
135 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
137 // strcspn("", s) -> 0
138 if (HasS1 && S1.empty())
139 return Constant::getNullValue(CI->getType());
142 if (HasS1 && HasS2) {
143 size_t Pos = S1.find_first_of(S2);
144 if (Pos == StringRef::npos) Pos = S1.size();
145 return ConstantInt::get(CI->getType(), Pos);
148 // strcspn(s, "") -> strlen(s)
149 if (TD && HasS2 && S2.empty())
150 return EmitStrLen(CI->getArgOperand(0), B, TD, TLI);
156 //===---------------------------------------===//
157 // 'strstr' Optimizations
159 struct StrStrOpt : public LibCallOptimization {
160 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
161 FunctionType *FT = Callee->getFunctionType();
162 if (FT->getNumParams() != 2 ||
163 !FT->getParamType(0)->isPointerTy() ||
164 !FT->getParamType(1)->isPointerTy() ||
165 !FT->getReturnType()->isPointerTy())
168 // fold strstr(x, x) -> x.
169 if (CI->getArgOperand(0) == CI->getArgOperand(1))
170 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
172 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
173 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
174 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD, TLI);
177 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
181 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
183 ICmpInst *Old = cast<ICmpInst>(*UI++);
184 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
185 ConstantInt::getNullValue(StrNCmp->getType()),
187 Old->replaceAllUsesWith(Cmp);
188 Old->eraseFromParent();
193 // See if either input string is a constant string.
194 StringRef SearchStr, ToFindStr;
195 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
196 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
198 // fold strstr(x, "") -> x.
199 if (HasStr2 && ToFindStr.empty())
200 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
202 // If both strings are known, constant fold it.
203 if (HasStr1 && HasStr2) {
204 std::string::size_type Offset = SearchStr.find(ToFindStr);
206 if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
207 return Constant::getNullValue(CI->getType());
209 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
210 Value *Result = CastToCStr(CI->getArgOperand(0), B);
211 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
212 return B.CreateBitCast(Result, CI->getType());
215 // fold strstr(x, "y") -> strchr(x, 'y').
216 if (HasStr2 && ToFindStr.size() == 1) {
217 Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TD, TLI);
218 return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : 0;
225 //===---------------------------------------===//
226 // 'memcmp' Optimizations
228 struct MemCmpOpt : public LibCallOptimization {
229 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
230 FunctionType *FT = Callee->getFunctionType();
231 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
232 !FT->getParamType(1)->isPointerTy() ||
233 !FT->getReturnType()->isIntegerTy(32))
236 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
238 if (LHS == RHS) // memcmp(s,s,x) -> 0
239 return Constant::getNullValue(CI->getType());
241 // Make sure we have a constant length.
242 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
244 uint64_t Len = LenC->getZExtValue();
246 if (Len == 0) // memcmp(s1,s2,0) -> 0
247 return Constant::getNullValue(CI->getType());
249 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
251 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
252 CI->getType(), "lhsv");
253 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
254 CI->getType(), "rhsv");
255 return B.CreateSub(LHSV, RHSV, "chardiff");
258 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
259 StringRef LHSStr, RHSStr;
260 if (getConstantStringInfo(LHS, LHSStr) &&
261 getConstantStringInfo(RHS, RHSStr)) {
262 // Make sure we're not reading out-of-bounds memory.
263 if (Len > LHSStr.size() || Len > RHSStr.size())
265 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
266 return ConstantInt::get(CI->getType(), Ret);
273 //===---------------------------------------===//
274 // 'memcpy' Optimizations
276 struct MemCpyOpt : public LibCallOptimization {
277 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
278 // These optimizations require DataLayout.
281 FunctionType *FT = Callee->getFunctionType();
282 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
283 !FT->getParamType(0)->isPointerTy() ||
284 !FT->getParamType(1)->isPointerTy() ||
285 FT->getParamType(2) != TD->getIntPtrType(*Context))
288 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
289 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
290 CI->getArgOperand(2), 1);
291 return CI->getArgOperand(0);
295 //===---------------------------------------===//
296 // 'memmove' Optimizations
298 struct MemMoveOpt : public LibCallOptimization {
299 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
300 // These optimizations require DataLayout.
303 FunctionType *FT = Callee->getFunctionType();
304 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
305 !FT->getParamType(0)->isPointerTy() ||
306 !FT->getParamType(1)->isPointerTy() ||
307 FT->getParamType(2) != TD->getIntPtrType(*Context))
310 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
311 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
312 CI->getArgOperand(2), 1);
313 return CI->getArgOperand(0);
317 //===---------------------------------------===//
318 // 'memset' Optimizations
320 struct MemSetOpt : public LibCallOptimization {
321 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
322 // These optimizations require DataLayout.
325 FunctionType *FT = Callee->getFunctionType();
326 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
327 !FT->getParamType(0)->isPointerTy() ||
328 !FT->getParamType(1)->isIntegerTy() ||
329 FT->getParamType(2) != TD->getIntPtrType(*Context))
332 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
333 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
334 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
335 return CI->getArgOperand(0);
339 //===----------------------------------------------------------------------===//
340 // Math Library Optimizations
341 //===----------------------------------------------------------------------===//
343 //===---------------------------------------===//
344 // Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
346 struct UnaryDoubleFPOpt : public LibCallOptimization {
348 UnaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {}
349 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
350 FunctionType *FT = Callee->getFunctionType();
351 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
352 !FT->getParamType(0)->isDoubleTy())
356 // Check if all the uses for function like 'sin' are converted to float.
357 for (Value::use_iterator UseI = CI->use_begin(); UseI != CI->use_end();
359 FPTruncInst *Cast = dyn_cast<FPTruncInst>(*UseI);
360 if (Cast == 0 || !Cast->getType()->isFloatTy())
365 // If this is something like 'floor((double)floatval)', convert to floorf.
366 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
367 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
370 // floor((double)floatval) -> (double)floorf(floatval)
371 Value *V = Cast->getOperand(0);
372 V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
373 return B.CreateFPExt(V, B.getDoubleTy());
377 //===---------------------------------------===//
378 // 'cos*' Optimizations
379 struct CosOpt : public LibCallOptimization {
380 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
382 if (UnsafeFPShrink && Callee->getName() == "cos" &&
383 TLI->has(LibFunc::cosf)) {
384 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
385 Ret = UnsafeUnaryDoubleFP.CallOptimizer(Callee, CI, B);
388 FunctionType *FT = Callee->getFunctionType();
389 // Just make sure this has 1 argument of FP type, which matches the
391 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
392 !FT->getParamType(0)->isFloatingPointTy())
396 Value *Op1 = CI->getArgOperand(0);
397 if (BinaryOperator::isFNeg(Op1)) {
398 BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);
399 return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");
405 //===---------------------------------------===//
406 // 'pow*' Optimizations
408 struct PowOpt : public LibCallOptimization {
409 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
411 if (UnsafeFPShrink && Callee->getName() == "pow" &&
412 TLI->has(LibFunc::powf)) {
413 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
414 Ret = UnsafeUnaryDoubleFP.CallOptimizer(Callee, CI, B);
417 FunctionType *FT = Callee->getFunctionType();
418 // Just make sure this has 2 arguments of the same FP type, which match the
420 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
421 FT->getParamType(0) != FT->getParamType(1) ||
422 !FT->getParamType(0)->isFloatingPointTy())
425 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
426 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
427 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
429 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
430 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
433 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
434 if (Op2C == 0) return Ret;
436 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
437 return ConstantFP::get(CI->getType(), 1.0);
439 if (Op2C->isExactlyValue(0.5)) {
440 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
441 // This is faster than calling pow, and still handles negative zero
442 // and negative infinity correctly.
443 // TODO: In fast-math mode, this could be just sqrt(x).
444 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
445 Value *Inf = ConstantFP::getInfinity(CI->getType());
446 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
447 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
448 Callee->getAttributes());
449 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
450 Callee->getAttributes());
451 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
452 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
456 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
458 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
459 return B.CreateFMul(Op1, Op1, "pow2");
460 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
461 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
467 //===---------------------------------------===//
468 // 'exp2' Optimizations
470 struct Exp2Opt : public LibCallOptimization {
471 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
473 if (UnsafeFPShrink && Callee->getName() == "exp2" &&
474 TLI->has(LibFunc::exp2)) {
475 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
476 Ret = UnsafeUnaryDoubleFP.CallOptimizer(Callee, CI, B);
479 FunctionType *FT = Callee->getFunctionType();
480 // Just make sure this has 1 argument of FP type, which matches the
482 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
483 !FT->getParamType(0)->isFloatingPointTy())
486 Value *Op = CI->getArgOperand(0);
487 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
488 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
490 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
491 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
492 LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
493 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
494 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
495 LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
500 if (Op->getType()->isFloatTy())
502 else if (Op->getType()->isDoubleTy())
507 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
508 if (!Op->getType()->isFloatTy())
509 One = ConstantExpr::getFPExtend(One, Op->getType());
511 Module *M = Caller->getParent();
512 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
514 B.getInt32Ty(), NULL);
515 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
516 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
517 CI->setCallingConv(F->getCallingConv());
525 //===----------------------------------------------------------------------===//
526 // Integer Optimizations
527 //===----------------------------------------------------------------------===//
529 //===---------------------------------------===//
530 // 'ffs*' Optimizations
532 struct FFSOpt : public LibCallOptimization {
533 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
534 FunctionType *FT = Callee->getFunctionType();
535 // Just make sure this has 2 arguments of the same FP type, which match the
537 if (FT->getNumParams() != 1 ||
538 !FT->getReturnType()->isIntegerTy(32) ||
539 !FT->getParamType(0)->isIntegerTy())
542 Value *Op = CI->getArgOperand(0);
545 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
546 if (CI->isZero()) // ffs(0) -> 0.
547 return B.getInt32(0);
548 // ffs(c) -> cttz(c)+1
549 return B.getInt32(CI->getValue().countTrailingZeros() + 1);
552 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
553 Type *ArgType = Op->getType();
554 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
555 Intrinsic::cttz, ArgType);
556 Value *V = B.CreateCall2(F, Op, B.getFalse(), "cttz");
557 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
558 V = B.CreateIntCast(V, B.getInt32Ty(), false);
560 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
561 return B.CreateSelect(Cond, V, B.getInt32(0));
565 //===---------------------------------------===//
566 // 'isdigit' Optimizations
568 struct IsDigitOpt : public LibCallOptimization {
569 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
570 FunctionType *FT = Callee->getFunctionType();
571 // We require integer(i32)
572 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
573 !FT->getParamType(0)->isIntegerTy(32))
576 // isdigit(c) -> (c-'0') <u 10
577 Value *Op = CI->getArgOperand(0);
578 Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
579 Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
580 return B.CreateZExt(Op, CI->getType());
584 //===---------------------------------------===//
585 // 'isascii' Optimizations
587 struct IsAsciiOpt : public LibCallOptimization {
588 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
589 FunctionType *FT = Callee->getFunctionType();
590 // We require integer(i32)
591 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
592 !FT->getParamType(0)->isIntegerTy(32))
595 // isascii(c) -> c <u 128
596 Value *Op = CI->getArgOperand(0);
597 Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
598 return B.CreateZExt(Op, CI->getType());
602 //===---------------------------------------===//
603 // 'abs', 'labs', 'llabs' Optimizations
605 struct AbsOpt : public LibCallOptimization {
606 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
607 FunctionType *FT = Callee->getFunctionType();
608 // We require integer(integer) where the types agree.
609 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
610 FT->getParamType(0) != FT->getReturnType())
613 // abs(x) -> x >s -1 ? x : -x
614 Value *Op = CI->getArgOperand(0);
615 Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),
617 Value *Neg = B.CreateNeg(Op, "neg");
618 return B.CreateSelect(Pos, Op, Neg);
623 //===---------------------------------------===//
624 // 'toascii' Optimizations
626 struct ToAsciiOpt : public LibCallOptimization {
627 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
628 FunctionType *FT = Callee->getFunctionType();
629 // We require i32(i32)
630 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
631 !FT->getParamType(0)->isIntegerTy(32))
634 // isascii(c) -> c & 0x7f
635 return B.CreateAnd(CI->getArgOperand(0),
636 ConstantInt::get(CI->getType(),0x7F));
640 //===----------------------------------------------------------------------===//
641 // Formatting and IO Optimizations
642 //===----------------------------------------------------------------------===//
644 //===---------------------------------------===//
645 // 'printf' Optimizations
647 struct PrintFOpt : public LibCallOptimization {
648 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
650 // Check for a fixed format string.
652 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
655 // Empty format string -> noop.
656 if (FormatStr.empty()) // Tolerate printf's declared void.
657 return CI->use_empty() ? (Value*)CI :
658 ConstantInt::get(CI->getType(), 0);
660 // Do not do any of the following transformations if the printf return value
661 // is used, in general the printf return value is not compatible with either
662 // putchar() or puts().
663 if (!CI->use_empty())
666 // printf("x") -> putchar('x'), even for '%'.
667 if (FormatStr.size() == 1) {
668 Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI);
669 if (CI->use_empty() || !Res) return Res;
670 return B.CreateIntCast(Res, CI->getType(), true);
673 // printf("foo\n") --> puts("foo")
674 if (FormatStr[FormatStr.size()-1] == '\n' &&
675 FormatStr.find('%') == std::string::npos) { // no format characters.
676 // Create a string literal with no \n on it. We expect the constant merge
677 // pass to be run after this pass, to merge duplicate strings.
678 FormatStr = FormatStr.drop_back();
679 Value *GV = B.CreateGlobalString(FormatStr, "str");
680 Value *NewCI = EmitPutS(GV, B, TD, TLI);
681 return (CI->use_empty() || !NewCI) ?
683 ConstantInt::get(CI->getType(), FormatStr.size()+1);
686 // Optimize specific format strings.
687 // printf("%c", chr) --> putchar(chr)
688 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
689 CI->getArgOperand(1)->getType()->isIntegerTy()) {
690 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI);
692 if (CI->use_empty() || !Res) return Res;
693 return B.CreateIntCast(Res, CI->getType(), true);
696 // printf("%s\n", str) --> puts(str)
697 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
698 CI->getArgOperand(1)->getType()->isPointerTy()) {
699 return EmitPutS(CI->getArgOperand(1), B, TD, TLI);
704 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
705 // Require one fixed pointer argument and an integer/void result.
706 FunctionType *FT = Callee->getFunctionType();
707 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
708 !(FT->getReturnType()->isIntegerTy() ||
709 FT->getReturnType()->isVoidTy()))
712 if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
716 // printf(format, ...) -> iprintf(format, ...) if no floating point
718 if (TLI->has(LibFunc::iprintf) && !CallHasFloatingPointArgument(CI)) {
719 Module *M = B.GetInsertBlock()->getParent()->getParent();
720 Constant *IPrintFFn =
721 M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
722 CallInst *New = cast<CallInst>(CI->clone());
723 New->setCalledFunction(IPrintFFn);
731 //===---------------------------------------===//
732 // 'sprintf' Optimizations
734 struct SPrintFOpt : public LibCallOptimization {
735 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
737 // Check for a fixed format string.
739 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
742 // If we just have a format string (nothing else crazy) transform it.
743 if (CI->getNumArgOperands() == 2) {
744 // Make sure there's no % in the constant array. We could try to handle
745 // %% -> % in the future if we cared.
746 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
747 if (FormatStr[i] == '%')
748 return 0; // we found a format specifier, bail out.
750 // These optimizations require DataLayout.
753 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
754 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
755 ConstantInt::get(TD->getIntPtrType(*Context), // Copy the
756 FormatStr.size() + 1), 1); // nul byte.
757 return ConstantInt::get(CI->getType(), FormatStr.size());
760 // The remaining optimizations require the format string to be "%s" or "%c"
761 // and have an extra operand.
762 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
763 CI->getNumArgOperands() < 3)
766 // Decode the second character of the format string.
767 if (FormatStr[1] == 'c') {
768 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
769 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
770 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
771 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
772 B.CreateStore(V, Ptr);
773 Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
774 B.CreateStore(B.getInt8(0), Ptr);
776 return ConstantInt::get(CI->getType(), 1);
779 if (FormatStr[1] == 's') {
780 // These optimizations require DataLayout.
783 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
784 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
786 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD, TLI);
789 Value *IncLen = B.CreateAdd(Len,
790 ConstantInt::get(Len->getType(), 1),
792 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
794 // The sprintf result is the unincremented number of bytes in the string.
795 return B.CreateIntCast(Len, CI->getType(), false);
800 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
801 // Require two fixed pointer arguments and an integer result.
802 FunctionType *FT = Callee->getFunctionType();
803 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
804 !FT->getParamType(1)->isPointerTy() ||
805 !FT->getReturnType()->isIntegerTy())
808 if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
812 // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
814 if (TLI->has(LibFunc::siprintf) && !CallHasFloatingPointArgument(CI)) {
815 Module *M = B.GetInsertBlock()->getParent()->getParent();
816 Constant *SIPrintFFn =
817 M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
818 CallInst *New = cast<CallInst>(CI->clone());
819 New->setCalledFunction(SIPrintFFn);
827 //===---------------------------------------===//
828 // 'fwrite' Optimizations
830 struct FWriteOpt : public LibCallOptimization {
831 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
832 // Require a pointer, an integer, an integer, a pointer, returning integer.
833 FunctionType *FT = Callee->getFunctionType();
834 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
835 !FT->getParamType(1)->isIntegerTy() ||
836 !FT->getParamType(2)->isIntegerTy() ||
837 !FT->getParamType(3)->isPointerTy() ||
838 !FT->getReturnType()->isIntegerTy())
841 // Get the element size and count.
842 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
843 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
844 if (!SizeC || !CountC) return 0;
845 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
847 // If this is writing zero records, remove the call (it's a noop).
849 return ConstantInt::get(CI->getType(), 0);
851 // If this is writing one byte, turn it into fputc.
852 // This optimisation is only valid, if the return value is unused.
853 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
854 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
855 Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, TD, TLI);
856 return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
863 //===---------------------------------------===//
864 // 'fputs' Optimizations
866 struct FPutsOpt : public LibCallOptimization {
867 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
868 // These optimizations require DataLayout.
871 // Require two pointers. Also, we can't optimize if return value is used.
872 FunctionType *FT = Callee->getFunctionType();
873 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
874 !FT->getParamType(1)->isPointerTy() ||
878 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
879 uint64_t Len = GetStringLength(CI->getArgOperand(0));
881 // Known to have no uses (see above).
882 return EmitFWrite(CI->getArgOperand(0),
883 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
884 CI->getArgOperand(1), B, TD, TLI);
888 //===---------------------------------------===//
889 // 'fprintf' Optimizations
891 struct FPrintFOpt : public LibCallOptimization {
892 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
894 // All the optimizations depend on the format string.
896 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
899 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
900 if (CI->getNumArgOperands() == 2) {
901 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
902 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
903 return 0; // We found a format specifier.
905 // These optimizations require DataLayout.
908 Value *NewCI = EmitFWrite(CI->getArgOperand(1),
909 ConstantInt::get(TD->getIntPtrType(*Context),
911 CI->getArgOperand(0), B, TD, TLI);
912 return NewCI ? ConstantInt::get(CI->getType(), FormatStr.size()) : 0;
915 // The remaining optimizations require the format string to be "%s" or "%c"
916 // and have an extra operand.
917 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
918 CI->getNumArgOperands() < 3)
921 // Decode the second character of the format string.
922 if (FormatStr[1] == 'c') {
923 // fprintf(F, "%c", chr) --> fputc(chr, F)
924 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
925 Value *NewCI = EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B,
927 return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
930 if (FormatStr[1] == 's') {
931 // fprintf(F, "%s", str) --> fputs(str, F)
932 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
934 return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD, TLI);
939 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
940 // Require two fixed paramters as pointers and integer result.
941 FunctionType *FT = Callee->getFunctionType();
942 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
943 !FT->getParamType(1)->isPointerTy() ||
944 !FT->getReturnType()->isIntegerTy())
947 if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
951 // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
952 // floating point arguments.
953 if (TLI->has(LibFunc::fiprintf) && !CallHasFloatingPointArgument(CI)) {
954 Module *M = B.GetInsertBlock()->getParent()->getParent();
955 Constant *FIPrintFFn =
956 M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
957 CallInst *New = cast<CallInst>(CI->clone());
958 New->setCalledFunction(FIPrintFFn);
966 //===---------------------------------------===//
967 // 'puts' Optimizations
969 struct PutsOpt : public LibCallOptimization {
970 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
971 // Require one fixed pointer argument and an integer/void result.
972 FunctionType *FT = Callee->getFunctionType();
973 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
974 !(FT->getReturnType()->isIntegerTy() ||
975 FT->getReturnType()->isVoidTy()))
978 // Check for a constant string.
980 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
983 if (Str.empty() && CI->use_empty()) {
984 // puts("") -> putchar('\n')
985 Value *Res = EmitPutChar(B.getInt32('\n'), B, TD, TLI);
986 if (CI->use_empty() || !Res) return Res;
987 return B.CreateIntCast(Res, CI->getType(), true);
994 } // end anonymous namespace.
996 //===----------------------------------------------------------------------===//
997 // SimplifyLibCalls Pass Implementation
998 //===----------------------------------------------------------------------===//
1001 /// This pass optimizes well known library functions from libc and libm.
1003 class SimplifyLibCalls : public FunctionPass {
1004 TargetLibraryInfo *TLI;
1006 StringMap<LibCallOptimization*> Optimizations;
1007 // String and Memory LibCall Optimizations
1008 StrCSpnOpt StrCSpn; StrStrOpt StrStr;
1009 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
1010 // Math Library Optimizations
1011 CosOpt Cos; PowOpt Pow; Exp2Opt Exp2;
1012 UnaryDoubleFPOpt UnaryDoubleFP, UnsafeUnaryDoubleFP;
1013 // Integer Optimizations
1014 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1016 // Formatting and IO Optimizations
1017 SPrintFOpt SPrintF; PrintFOpt PrintF;
1018 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
1021 bool Modified; // This is only used by doInitialization.
1023 static char ID; // Pass identification
1024 SimplifyLibCalls() : FunctionPass(ID), UnaryDoubleFP(false),
1025 UnsafeUnaryDoubleFP(true) {
1026 initializeSimplifyLibCallsPass(*PassRegistry::getPassRegistry());
1028 void AddOpt(LibFunc::Func F, LibCallOptimization* Opt);
1029 void AddOpt(LibFunc::Func F1, LibFunc::Func F2, LibCallOptimization* Opt);
1031 void InitOptimizations();
1032 bool runOnFunction(Function &F);
1034 void setDoesNotAccessMemory(Function &F);
1035 void setOnlyReadsMemory(Function &F);
1036 void setDoesNotThrow(Function &F);
1037 void setDoesNotCapture(Function &F, unsigned n);
1038 void setDoesNotAlias(Function &F, unsigned n);
1039 bool doInitialization(Module &M);
1041 void inferPrototypeAttributes(Function &F);
1042 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1043 AU.addRequired<TargetLibraryInfo>();
1046 } // end anonymous namespace.
1048 char SimplifyLibCalls::ID = 0;
1050 INITIALIZE_PASS_BEGIN(SimplifyLibCalls, "simplify-libcalls",
1051 "Simplify well-known library calls", false, false)
1052 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
1053 INITIALIZE_PASS_END(SimplifyLibCalls, "simplify-libcalls",
1054 "Simplify well-known library calls", false, false)
1056 // Public interface to the Simplify LibCalls pass.
1057 FunctionPass *llvm::createSimplifyLibCallsPass() {
1058 return new SimplifyLibCalls();
1061 void SimplifyLibCalls::AddOpt(LibFunc::Func F, LibCallOptimization* Opt) {
1063 Optimizations[TLI->getName(F)] = Opt;
1066 void SimplifyLibCalls::AddOpt(LibFunc::Func F1, LibFunc::Func F2,
1067 LibCallOptimization* Opt) {
1068 if (TLI->has(F1) && TLI->has(F2))
1069 Optimizations[TLI->getName(F1)] = Opt;
1072 /// Optimizations - Populate the Optimizations map with all the optimizations
1074 void SimplifyLibCalls::InitOptimizations() {
1075 // String and Memory LibCall Optimizations
1076 Optimizations["strcspn"] = &StrCSpn;
1077 Optimizations["strstr"] = &StrStr;
1078 Optimizations["memcmp"] = &MemCmp;
1079 AddOpt(LibFunc::memcpy, &MemCpy);
1080 Optimizations["memmove"] = &MemMove;
1081 AddOpt(LibFunc::memset, &MemSet);
1083 // Math Library Optimizations
1084 Optimizations["cosf"] = &Cos;
1085 Optimizations["cos"] = &Cos;
1086 Optimizations["cosl"] = &Cos;
1087 Optimizations["powf"] = &Pow;
1088 Optimizations["pow"] = &Pow;
1089 Optimizations["powl"] = &Pow;
1090 Optimizations["llvm.pow.f32"] = &Pow;
1091 Optimizations["llvm.pow.f64"] = &Pow;
1092 Optimizations["llvm.pow.f80"] = &Pow;
1093 Optimizations["llvm.pow.f128"] = &Pow;
1094 Optimizations["llvm.pow.ppcf128"] = &Pow;
1095 Optimizations["exp2l"] = &Exp2;
1096 Optimizations["exp2"] = &Exp2;
1097 Optimizations["exp2f"] = &Exp2;
1098 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1099 Optimizations["llvm.exp2.f128"] = &Exp2;
1100 Optimizations["llvm.exp2.f80"] = &Exp2;
1101 Optimizations["llvm.exp2.f64"] = &Exp2;
1102 Optimizations["llvm.exp2.f32"] = &Exp2;
1104 AddOpt(LibFunc::ceil, LibFunc::ceilf, &UnaryDoubleFP);
1105 AddOpt(LibFunc::fabs, LibFunc::fabsf, &UnaryDoubleFP);
1106 AddOpt(LibFunc::floor, LibFunc::floorf, &UnaryDoubleFP);
1107 AddOpt(LibFunc::rint, LibFunc::rintf, &UnaryDoubleFP);
1108 AddOpt(LibFunc::round, LibFunc::roundf, &UnaryDoubleFP);
1109 AddOpt(LibFunc::nearbyint, LibFunc::nearbyintf, &UnaryDoubleFP);
1110 AddOpt(LibFunc::trunc, LibFunc::truncf, &UnaryDoubleFP);
1112 if(UnsafeFPShrink) {
1113 AddOpt(LibFunc::acos, LibFunc::acosf, &UnsafeUnaryDoubleFP);
1114 AddOpt(LibFunc::acosh, LibFunc::acoshf, &UnsafeUnaryDoubleFP);
1115 AddOpt(LibFunc::asin, LibFunc::asinf, &UnsafeUnaryDoubleFP);
1116 AddOpt(LibFunc::asinh, LibFunc::asinhf, &UnsafeUnaryDoubleFP);
1117 AddOpt(LibFunc::atan, LibFunc::atanf, &UnsafeUnaryDoubleFP);
1118 AddOpt(LibFunc::atanh, LibFunc::atanhf, &UnsafeUnaryDoubleFP);
1119 AddOpt(LibFunc::cbrt, LibFunc::cbrtf, &UnsafeUnaryDoubleFP);
1120 AddOpt(LibFunc::cosh, LibFunc::coshf, &UnsafeUnaryDoubleFP);
1121 AddOpt(LibFunc::exp, LibFunc::expf, &UnsafeUnaryDoubleFP);
1122 AddOpt(LibFunc::exp10, LibFunc::exp10f, &UnsafeUnaryDoubleFP);
1123 AddOpt(LibFunc::expm1, LibFunc::expm1f, &UnsafeUnaryDoubleFP);
1124 AddOpt(LibFunc::log, LibFunc::logf, &UnsafeUnaryDoubleFP);
1125 AddOpt(LibFunc::log10, LibFunc::log10f, &UnsafeUnaryDoubleFP);
1126 AddOpt(LibFunc::log1p, LibFunc::log1pf, &UnsafeUnaryDoubleFP);
1127 AddOpt(LibFunc::log2, LibFunc::log2f, &UnsafeUnaryDoubleFP);
1128 AddOpt(LibFunc::logb, LibFunc::logbf, &UnsafeUnaryDoubleFP);
1129 AddOpt(LibFunc::sin, LibFunc::sinf, &UnsafeUnaryDoubleFP);
1130 AddOpt(LibFunc::sinh, LibFunc::sinhf, &UnsafeUnaryDoubleFP);
1131 AddOpt(LibFunc::sqrt, LibFunc::sqrtf, &UnsafeUnaryDoubleFP);
1132 AddOpt(LibFunc::tan, LibFunc::tanf, &UnsafeUnaryDoubleFP);
1133 AddOpt(LibFunc::tanh, LibFunc::tanhf, &UnsafeUnaryDoubleFP);
1136 // Integer Optimizations
1137 Optimizations["ffs"] = &FFS;
1138 Optimizations["ffsl"] = &FFS;
1139 Optimizations["ffsll"] = &FFS;
1140 Optimizations["abs"] = &Abs;
1141 Optimizations["labs"] = &Abs;
1142 Optimizations["llabs"] = &Abs;
1143 Optimizations["isdigit"] = &IsDigit;
1144 Optimizations["isascii"] = &IsAscii;
1145 Optimizations["toascii"] = &ToAscii;
1147 // Formatting and IO Optimizations
1148 Optimizations["sprintf"] = &SPrintF;
1149 Optimizations["printf"] = &PrintF;
1150 AddOpt(LibFunc::fwrite, &FWrite);
1151 AddOpt(LibFunc::fputs, &FPuts);
1152 Optimizations["fprintf"] = &FPrintF;
1153 Optimizations["puts"] = &Puts;
1157 /// runOnFunction - Top level algorithm.
1159 bool SimplifyLibCalls::runOnFunction(Function &F) {
1160 TLI = &getAnalysis<TargetLibraryInfo>();
1162 if (Optimizations.empty())
1163 InitOptimizations();
1165 const DataLayout *TD = getAnalysisIfAvailable<DataLayout>();
1167 IRBuilder<> Builder(F.getContext());
1169 bool Changed = false;
1170 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1171 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1172 // Ignore non-calls.
1173 CallInst *CI = dyn_cast<CallInst>(I++);
1176 // Ignore indirect calls and calls to non-external functions.
1177 Function *Callee = CI->getCalledFunction();
1178 if (Callee == 0 || !Callee->isDeclaration() ||
1179 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1182 // Ignore unknown calls.
1183 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1186 // Set the builder to the instruction after the call.
1187 Builder.SetInsertPoint(BB, I);
1189 // Use debug location of CI for all new instructions.
1190 Builder.SetCurrentDebugLocation(CI->getDebugLoc());
1192 // Try to optimize this call.
1193 Value *Result = LCO->OptimizeCall(CI, TD, TLI, Builder);
1194 if (Result == 0) continue;
1196 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1197 dbgs() << " into: " << *Result << "\n");
1199 // Something changed!
1203 // Inspect the instruction after the call (which was potentially just
1207 if (CI != Result && !CI->use_empty()) {
1208 CI->replaceAllUsesWith(Result);
1209 if (!Result->hasName())
1210 Result->takeName(CI);
1212 CI->eraseFromParent();
1218 // Utility methods for doInitialization.
1220 void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1221 if (!F.doesNotAccessMemory()) {
1222 F.setDoesNotAccessMemory();
1227 void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1228 if (!F.onlyReadsMemory()) {
1229 F.setOnlyReadsMemory();
1234 void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1235 if (!F.doesNotThrow()) {
1236 F.setDoesNotThrow();
1241 void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1242 if (!F.doesNotCapture(n)) {
1243 F.setDoesNotCapture(n);
1248 void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1249 if (!F.doesNotAlias(n)) {
1250 F.setDoesNotAlias(n);
1257 void SimplifyLibCalls::inferPrototypeAttributes(Function &F) {
1258 FunctionType *FTy = F.getFunctionType();
1260 StringRef Name = F.getName();
1263 if (Name == "strlen") {
1264 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1266 setOnlyReadsMemory(F);
1268 setDoesNotCapture(F, 1);
1269 } else if (Name == "strchr" ||
1270 Name == "strrchr") {
1271 if (FTy->getNumParams() != 2 ||
1272 !FTy->getParamType(0)->isPointerTy() ||
1273 !FTy->getParamType(1)->isIntegerTy())
1275 setOnlyReadsMemory(F);
1277 } else if (Name == "strcpy" ||
1283 Name == "strtoul" ||
1284 Name == "strtoll" ||
1285 Name == "strtold" ||
1286 Name == "strncat" ||
1287 Name == "strncpy" ||
1288 Name == "stpncpy" ||
1289 Name == "strtoull") {
1290 if (FTy->getNumParams() < 2 ||
1291 !FTy->getParamType(1)->isPointerTy())
1294 setDoesNotCapture(F, 2);
1295 } else if (Name == "strxfrm") {
1296 if (FTy->getNumParams() != 3 ||
1297 !FTy->getParamType(0)->isPointerTy() ||
1298 !FTy->getParamType(1)->isPointerTy())
1301 setDoesNotCapture(F, 1);
1302 setDoesNotCapture(F, 2);
1303 } else if (Name == "strcmp" ||
1305 Name == "strncmp" ||
1306 Name == "strcspn" ||
1307 Name == "strcoll" ||
1308 Name == "strcasecmp" ||
1309 Name == "strncasecmp") {
1310 if (FTy->getNumParams() < 2 ||
1311 !FTy->getParamType(0)->isPointerTy() ||
1312 !FTy->getParamType(1)->isPointerTy())
1314 setOnlyReadsMemory(F);
1316 setDoesNotCapture(F, 1);
1317 setDoesNotCapture(F, 2);
1318 } else if (Name == "strstr" ||
1319 Name == "strpbrk") {
1320 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1322 setOnlyReadsMemory(F);
1324 setDoesNotCapture(F, 2);
1325 } else if (Name == "strtok" ||
1326 Name == "strtok_r") {
1327 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1330 setDoesNotCapture(F, 2);
1331 } else if (Name == "scanf" ||
1333 Name == "setvbuf") {
1334 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1337 setDoesNotCapture(F, 1);
1338 } else if (Name == "strdup" ||
1339 Name == "strndup") {
1340 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1341 !FTy->getParamType(0)->isPointerTy())
1344 setDoesNotAlias(F, 0);
1345 setDoesNotCapture(F, 1);
1346 } else if (Name == "stat" ||
1348 Name == "sprintf" ||
1349 Name == "statvfs") {
1350 if (FTy->getNumParams() < 2 ||
1351 !FTy->getParamType(0)->isPointerTy() ||
1352 !FTy->getParamType(1)->isPointerTy())
1355 setDoesNotCapture(F, 1);
1356 setDoesNotCapture(F, 2);
1357 } else if (Name == "snprintf") {
1358 if (FTy->getNumParams() != 3 ||
1359 !FTy->getParamType(0)->isPointerTy() ||
1360 !FTy->getParamType(2)->isPointerTy())
1363 setDoesNotCapture(F, 1);
1364 setDoesNotCapture(F, 3);
1365 } else if (Name == "setitimer") {
1366 if (FTy->getNumParams() != 3 ||
1367 !FTy->getParamType(1)->isPointerTy() ||
1368 !FTy->getParamType(2)->isPointerTy())
1371 setDoesNotCapture(F, 2);
1372 setDoesNotCapture(F, 3);
1373 } else if (Name == "system") {
1374 if (FTy->getNumParams() != 1 ||
1375 !FTy->getParamType(0)->isPointerTy())
1377 // May throw; "system" is a valid pthread cancellation point.
1378 setDoesNotCapture(F, 1);
1382 if (Name == "malloc") {
1383 if (FTy->getNumParams() != 1 ||
1384 !FTy->getReturnType()->isPointerTy())
1387 setDoesNotAlias(F, 0);
1388 } else if (Name == "memcmp") {
1389 if (FTy->getNumParams() != 3 ||
1390 !FTy->getParamType(0)->isPointerTy() ||
1391 !FTy->getParamType(1)->isPointerTy())
1393 setOnlyReadsMemory(F);
1395 setDoesNotCapture(F, 1);
1396 setDoesNotCapture(F, 2);
1397 } else if (Name == "memchr" ||
1398 Name == "memrchr") {
1399 if (FTy->getNumParams() != 3)
1401 setOnlyReadsMemory(F);
1403 } else if (Name == "modf" ||
1407 Name == "memccpy" ||
1408 Name == "memmove") {
1409 if (FTy->getNumParams() < 2 ||
1410 !FTy->getParamType(1)->isPointerTy())
1413 setDoesNotCapture(F, 2);
1414 } else if (Name == "memalign") {
1415 if (!FTy->getReturnType()->isPointerTy())
1417 setDoesNotAlias(F, 0);
1418 } else if (Name == "mkdir" ||
1420 if (FTy->getNumParams() == 0 ||
1421 !FTy->getParamType(0)->isPointerTy())
1424 setDoesNotCapture(F, 1);
1428 if (Name == "realloc") {
1429 if (FTy->getNumParams() != 2 ||
1430 !FTy->getParamType(0)->isPointerTy() ||
1431 !FTy->getReturnType()->isPointerTy())
1434 setDoesNotAlias(F, 0);
1435 setDoesNotCapture(F, 1);
1436 } else if (Name == "read") {
1437 if (FTy->getNumParams() != 3 ||
1438 !FTy->getParamType(1)->isPointerTy())
1440 // May throw; "read" is a valid pthread cancellation point.
1441 setDoesNotCapture(F, 2);
1442 } else if (Name == "rmdir" ||
1445 Name == "realpath") {
1446 if (FTy->getNumParams() < 1 ||
1447 !FTy->getParamType(0)->isPointerTy())
1450 setDoesNotCapture(F, 1);
1451 } else if (Name == "rename" ||
1452 Name == "readlink") {
1453 if (FTy->getNumParams() < 2 ||
1454 !FTy->getParamType(0)->isPointerTy() ||
1455 !FTy->getParamType(1)->isPointerTy())
1458 setDoesNotCapture(F, 1);
1459 setDoesNotCapture(F, 2);
1463 if (Name == "write") {
1464 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1466 // May throw; "write" is a valid pthread cancellation point.
1467 setDoesNotCapture(F, 2);
1471 if (Name == "bcopy") {
1472 if (FTy->getNumParams() != 3 ||
1473 !FTy->getParamType(0)->isPointerTy() ||
1474 !FTy->getParamType(1)->isPointerTy())
1477 setDoesNotCapture(F, 1);
1478 setDoesNotCapture(F, 2);
1479 } else if (Name == "bcmp") {
1480 if (FTy->getNumParams() != 3 ||
1481 !FTy->getParamType(0)->isPointerTy() ||
1482 !FTy->getParamType(1)->isPointerTy())
1485 setOnlyReadsMemory(F);
1486 setDoesNotCapture(F, 1);
1487 setDoesNotCapture(F, 2);
1488 } else if (Name == "bzero") {
1489 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1492 setDoesNotCapture(F, 1);
1496 if (Name == "calloc") {
1497 if (FTy->getNumParams() != 2 ||
1498 !FTy->getReturnType()->isPointerTy())
1501 setDoesNotAlias(F, 0);
1502 } else if (Name == "chmod" ||
1504 Name == "ctermid" ||
1505 Name == "clearerr" ||
1506 Name == "closedir") {
1507 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1510 setDoesNotCapture(F, 1);
1514 if (Name == "atoi" ||
1518 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1521 setOnlyReadsMemory(F);
1522 setDoesNotCapture(F, 1);
1523 } else if (Name == "access") {
1524 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1527 setDoesNotCapture(F, 1);
1531 if (Name == "fopen") {
1532 if (FTy->getNumParams() != 2 ||
1533 !FTy->getReturnType()->isPointerTy() ||
1534 !FTy->getParamType(0)->isPointerTy() ||
1535 !FTy->getParamType(1)->isPointerTy())
1538 setDoesNotAlias(F, 0);
1539 setDoesNotCapture(F, 1);
1540 setDoesNotCapture(F, 2);
1541 } else if (Name == "fdopen") {
1542 if (FTy->getNumParams() != 2 ||
1543 !FTy->getReturnType()->isPointerTy() ||
1544 !FTy->getParamType(1)->isPointerTy())
1547 setDoesNotAlias(F, 0);
1548 setDoesNotCapture(F, 2);
1549 } else if (Name == "feof" ||
1559 Name == "fsetpos" ||
1560 Name == "flockfile" ||
1561 Name == "funlockfile" ||
1562 Name == "ftrylockfile") {
1563 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1566 setDoesNotCapture(F, 1);
1567 } else if (Name == "ferror") {
1568 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1571 setDoesNotCapture(F, 1);
1572 setOnlyReadsMemory(F);
1573 } else if (Name == "fputc" ||
1578 Name == "fstatvfs") {
1579 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1582 setDoesNotCapture(F, 2);
1583 } else if (Name == "fgets") {
1584 if (FTy->getNumParams() != 3 ||
1585 !FTy->getParamType(0)->isPointerTy() ||
1586 !FTy->getParamType(2)->isPointerTy())
1589 setDoesNotCapture(F, 3);
1590 } else if (Name == "fread" ||
1592 if (FTy->getNumParams() != 4 ||
1593 !FTy->getParamType(0)->isPointerTy() ||
1594 !FTy->getParamType(3)->isPointerTy())
1597 setDoesNotCapture(F, 1);
1598 setDoesNotCapture(F, 4);
1599 } else if (Name == "fputs" ||
1601 Name == "fprintf" ||
1602 Name == "fgetpos") {
1603 if (FTy->getNumParams() < 2 ||
1604 !FTy->getParamType(0)->isPointerTy() ||
1605 !FTy->getParamType(1)->isPointerTy())
1608 setDoesNotCapture(F, 1);
1609 setDoesNotCapture(F, 2);
1613 if (Name == "getc" ||
1614 Name == "getlogin_r" ||
1615 Name == "getc_unlocked") {
1616 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1619 setDoesNotCapture(F, 1);
1620 } else if (Name == "getenv") {
1621 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1624 setOnlyReadsMemory(F);
1625 setDoesNotCapture(F, 1);
1626 } else if (Name == "gets" ||
1627 Name == "getchar") {
1629 } else if (Name == "getitimer") {
1630 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1633 setDoesNotCapture(F, 2);
1634 } else if (Name == "getpwnam") {
1635 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1638 setDoesNotCapture(F, 1);
1642 if (Name == "ungetc") {
1643 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1646 setDoesNotCapture(F, 2);
1647 } else if (Name == "uname" ||
1649 Name == "unsetenv") {
1650 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1653 setDoesNotCapture(F, 1);
1654 } else if (Name == "utime" ||
1656 if (FTy->getNumParams() != 2 ||
1657 !FTy->getParamType(0)->isPointerTy() ||
1658 !FTy->getParamType(1)->isPointerTy())
1661 setDoesNotCapture(F, 1);
1662 setDoesNotCapture(F, 2);
1666 if (Name == "putc") {
1667 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1670 setDoesNotCapture(F, 2);
1671 } else if (Name == "puts" ||
1674 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1677 setDoesNotCapture(F, 1);
1678 } else if (Name == "pread" ||
1680 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1682 // May throw; these are valid pthread cancellation points.
1683 setDoesNotCapture(F, 2);
1684 } else if (Name == "putchar") {
1686 } else if (Name == "popen") {
1687 if (FTy->getNumParams() != 2 ||
1688 !FTy->getReturnType()->isPointerTy() ||
1689 !FTy->getParamType(0)->isPointerTy() ||
1690 !FTy->getParamType(1)->isPointerTy())
1693 setDoesNotAlias(F, 0);
1694 setDoesNotCapture(F, 1);
1695 setDoesNotCapture(F, 2);
1696 } else if (Name == "pclose") {
1697 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1700 setDoesNotCapture(F, 1);
1704 if (Name == "vscanf") {
1705 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1708 setDoesNotCapture(F, 1);
1709 } else if (Name == "vsscanf" ||
1710 Name == "vfscanf") {
1711 if (FTy->getNumParams() != 3 ||
1712 !FTy->getParamType(1)->isPointerTy() ||
1713 !FTy->getParamType(2)->isPointerTy())
1716 setDoesNotCapture(F, 1);
1717 setDoesNotCapture(F, 2);
1718 } else if (Name == "valloc") {
1719 if (!FTy->getReturnType()->isPointerTy())
1722 setDoesNotAlias(F, 0);
1723 } else if (Name == "vprintf") {
1724 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1727 setDoesNotCapture(F, 1);
1728 } else if (Name == "vfprintf" ||
1729 Name == "vsprintf") {
1730 if (FTy->getNumParams() != 3 ||
1731 !FTy->getParamType(0)->isPointerTy() ||
1732 !FTy->getParamType(1)->isPointerTy())
1735 setDoesNotCapture(F, 1);
1736 setDoesNotCapture(F, 2);
1737 } else if (Name == "vsnprintf") {
1738 if (FTy->getNumParams() != 4 ||
1739 !FTy->getParamType(0)->isPointerTy() ||
1740 !FTy->getParamType(2)->isPointerTy())
1743 setDoesNotCapture(F, 1);
1744 setDoesNotCapture(F, 3);
1748 if (Name == "open") {
1749 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1751 // May throw; "open" is a valid pthread cancellation point.
1752 setDoesNotCapture(F, 1);
1753 } else if (Name == "opendir") {
1754 if (FTy->getNumParams() != 1 ||
1755 !FTy->getReturnType()->isPointerTy() ||
1756 !FTy->getParamType(0)->isPointerTy())
1759 setDoesNotAlias(F, 0);
1760 setDoesNotCapture(F, 1);
1764 if (Name == "tmpfile") {
1765 if (!FTy->getReturnType()->isPointerTy())
1768 setDoesNotAlias(F, 0);
1769 } else if (Name == "times") {
1770 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1773 setDoesNotCapture(F, 1);
1777 if (Name == "htonl" ||
1780 setDoesNotAccessMemory(F);
1784 if (Name == "ntohl" ||
1787 setDoesNotAccessMemory(F);
1791 if (Name == "lstat") {
1792 if (FTy->getNumParams() != 2 ||
1793 !FTy->getParamType(0)->isPointerTy() ||
1794 !FTy->getParamType(1)->isPointerTy())
1797 setDoesNotCapture(F, 1);
1798 setDoesNotCapture(F, 2);
1799 } else if (Name == "lchown") {
1800 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1803 setDoesNotCapture(F, 1);
1807 if (Name == "qsort") {
1808 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1810 // May throw; places call through function pointer.
1811 setDoesNotCapture(F, 4);
1815 if (Name == "__strdup" ||
1816 Name == "__strndup") {
1817 if (FTy->getNumParams() < 1 ||
1818 !FTy->getReturnType()->isPointerTy() ||
1819 !FTy->getParamType(0)->isPointerTy())
1822 setDoesNotAlias(F, 0);
1823 setDoesNotCapture(F, 1);
1824 } else if (Name == "__strtok_r") {
1825 if (FTy->getNumParams() != 3 ||
1826 !FTy->getParamType(1)->isPointerTy())
1829 setDoesNotCapture(F, 2);
1830 } else if (Name == "_IO_getc") {
1831 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1834 setDoesNotCapture(F, 1);
1835 } else if (Name == "_IO_putc") {
1836 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1839 setDoesNotCapture(F, 2);
1843 if (Name == "\1__isoc99_scanf") {
1844 if (FTy->getNumParams() < 1 ||
1845 !FTy->getParamType(0)->isPointerTy())
1848 setDoesNotCapture(F, 1);
1849 } else if (Name == "\1stat64" ||
1850 Name == "\1lstat64" ||
1851 Name == "\1statvfs64" ||
1852 Name == "\1__isoc99_sscanf") {
1853 if (FTy->getNumParams() < 1 ||
1854 !FTy->getParamType(0)->isPointerTy() ||
1855 !FTy->getParamType(1)->isPointerTy())
1858 setDoesNotCapture(F, 1);
1859 setDoesNotCapture(F, 2);
1860 } else if (Name == "\1fopen64") {
1861 if (FTy->getNumParams() != 2 ||
1862 !FTy->getReturnType()->isPointerTy() ||
1863 !FTy->getParamType(0)->isPointerTy() ||
1864 !FTy->getParamType(1)->isPointerTy())
1867 setDoesNotAlias(F, 0);
1868 setDoesNotCapture(F, 1);
1869 setDoesNotCapture(F, 2);
1870 } else if (Name == "\1fseeko64" ||
1871 Name == "\1ftello64") {
1872 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1875 setDoesNotCapture(F, 1);
1876 } else if (Name == "\1tmpfile64") {
1877 if (!FTy->getReturnType()->isPointerTy())
1880 setDoesNotAlias(F, 0);
1881 } else if (Name == "\1fstat64" ||
1882 Name == "\1fstatvfs64") {
1883 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1886 setDoesNotCapture(F, 2);
1887 } else if (Name == "\1open64") {
1888 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1890 // May throw; "open" is a valid pthread cancellation point.
1891 setDoesNotCapture(F, 1);
1897 /// doInitialization - Add attributes to well-known functions.
1899 bool SimplifyLibCalls::doInitialization(Module &M) {
1901 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1903 if (F.isDeclaration() && F.hasName())
1904 inferPrototypeAttributes(F);
1910 // Additional cases that we need to add to this file:
1913 // * cbrt(expN(X)) -> expN(x/3)
1914 // * cbrt(sqrt(x)) -> pow(x,1/6)
1915 // * cbrt(sqrt(x)) -> pow(x,1/9)
1918 // * exp(log(x)) -> x
1921 // * log(exp(x)) -> x
1922 // * log(x**y) -> y*log(x)
1923 // * log(exp(y)) -> y*log(e)
1924 // * log(exp2(y)) -> y*log(2)
1925 // * log(exp10(y)) -> y*log(10)
1926 // * log(sqrt(x)) -> 0.5*log(x)
1927 // * log(pow(x,y)) -> y*log(x)
1929 // lround, lroundf, lroundl:
1930 // * lround(cnst) -> cnst'
1933 // * pow(exp(x),y) -> exp(x*y)
1934 // * pow(sqrt(x),y) -> pow(x,y*0.5)
1935 // * pow(pow(x,y),z)-> pow(x,y*z)
1937 // round, roundf, roundl:
1938 // * round(cnst) -> cnst'
1941 // * signbit(cnst) -> cnst'
1942 // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
1944 // sqrt, sqrtf, sqrtl:
1945 // * sqrt(expN(x)) -> expN(x*0.5)
1946 // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
1947 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
1950 // * strchr(p, 0) -> strlen(p)
1952 // * tan(atan(x)) -> x
1954 // trunc, truncf, truncl:
1955 // * trunc(cnst) -> cnst'