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 // 'strspn' Optimizations
124 struct StrSpnOpt : 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 // strspn(s, "") -> 0
138 // strspn("", s) -> 0
139 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
140 return Constant::getNullValue(CI->getType());
143 if (HasS1 && HasS2) {
144 size_t Pos = S1.find_first_not_of(S2);
145 if (Pos == StringRef::npos) Pos = S1.size();
146 return ConstantInt::get(CI->getType(), Pos);
153 //===---------------------------------------===//
154 // 'strcspn' Optimizations
156 struct StrCSpnOpt : public LibCallOptimization {
157 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
158 FunctionType *FT = Callee->getFunctionType();
159 if (FT->getNumParams() != 2 ||
160 FT->getParamType(0) != B.getInt8PtrTy() ||
161 FT->getParamType(1) != FT->getParamType(0) ||
162 !FT->getReturnType()->isIntegerTy())
166 bool HasS1 = getConstantStringInfo(CI->getArgOperand(0), S1);
167 bool HasS2 = getConstantStringInfo(CI->getArgOperand(1), S2);
169 // strcspn("", s) -> 0
170 if (HasS1 && S1.empty())
171 return Constant::getNullValue(CI->getType());
174 if (HasS1 && HasS2) {
175 size_t Pos = S1.find_first_of(S2);
176 if (Pos == StringRef::npos) Pos = S1.size();
177 return ConstantInt::get(CI->getType(), Pos);
180 // strcspn(s, "") -> strlen(s)
181 if (TD && HasS2 && S2.empty())
182 return EmitStrLen(CI->getArgOperand(0), B, TD, TLI);
188 //===---------------------------------------===//
189 // 'strstr' Optimizations
191 struct StrStrOpt : public LibCallOptimization {
192 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
193 FunctionType *FT = Callee->getFunctionType();
194 if (FT->getNumParams() != 2 ||
195 !FT->getParamType(0)->isPointerTy() ||
196 !FT->getParamType(1)->isPointerTy() ||
197 !FT->getReturnType()->isPointerTy())
200 // fold strstr(x, x) -> x.
201 if (CI->getArgOperand(0) == CI->getArgOperand(1))
202 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
204 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
205 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
206 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD, TLI);
209 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
213 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
215 ICmpInst *Old = cast<ICmpInst>(*UI++);
216 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
217 ConstantInt::getNullValue(StrNCmp->getType()),
219 Old->replaceAllUsesWith(Cmp);
220 Old->eraseFromParent();
225 // See if either input string is a constant string.
226 StringRef SearchStr, ToFindStr;
227 bool HasStr1 = getConstantStringInfo(CI->getArgOperand(0), SearchStr);
228 bool HasStr2 = getConstantStringInfo(CI->getArgOperand(1), ToFindStr);
230 // fold strstr(x, "") -> x.
231 if (HasStr2 && ToFindStr.empty())
232 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
234 // If both strings are known, constant fold it.
235 if (HasStr1 && HasStr2) {
236 std::string::size_type Offset = SearchStr.find(ToFindStr);
238 if (Offset == StringRef::npos) // strstr("foo", "bar") -> null
239 return Constant::getNullValue(CI->getType());
241 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
242 Value *Result = CastToCStr(CI->getArgOperand(0), B);
243 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
244 return B.CreateBitCast(Result, CI->getType());
247 // fold strstr(x, "y") -> strchr(x, 'y').
248 if (HasStr2 && ToFindStr.size() == 1) {
249 Value *StrChr= EmitStrChr(CI->getArgOperand(0), ToFindStr[0], B, TD, TLI);
250 return StrChr ? B.CreateBitCast(StrChr, CI->getType()) : 0;
257 //===---------------------------------------===//
258 // 'memcmp' Optimizations
260 struct MemCmpOpt : public LibCallOptimization {
261 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
262 FunctionType *FT = Callee->getFunctionType();
263 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
264 !FT->getParamType(1)->isPointerTy() ||
265 !FT->getReturnType()->isIntegerTy(32))
268 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
270 if (LHS == RHS) // memcmp(s,s,x) -> 0
271 return Constant::getNullValue(CI->getType());
273 // Make sure we have a constant length.
274 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
276 uint64_t Len = LenC->getZExtValue();
278 if (Len == 0) // memcmp(s1,s2,0) -> 0
279 return Constant::getNullValue(CI->getType());
281 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
283 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
284 CI->getType(), "lhsv");
285 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
286 CI->getType(), "rhsv");
287 return B.CreateSub(LHSV, RHSV, "chardiff");
290 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
291 StringRef LHSStr, RHSStr;
292 if (getConstantStringInfo(LHS, LHSStr) &&
293 getConstantStringInfo(RHS, RHSStr)) {
294 // Make sure we're not reading out-of-bounds memory.
295 if (Len > LHSStr.size() || Len > RHSStr.size())
297 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
298 return ConstantInt::get(CI->getType(), Ret);
305 //===---------------------------------------===//
306 // 'memcpy' Optimizations
308 struct MemCpyOpt : public LibCallOptimization {
309 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
310 // These optimizations require DataLayout.
313 FunctionType *FT = Callee->getFunctionType();
314 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
315 !FT->getParamType(0)->isPointerTy() ||
316 !FT->getParamType(1)->isPointerTy() ||
317 FT->getParamType(2) != TD->getIntPtrType(*Context))
320 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
321 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
322 CI->getArgOperand(2), 1);
323 return CI->getArgOperand(0);
327 //===---------------------------------------===//
328 // 'memmove' Optimizations
330 struct MemMoveOpt : public LibCallOptimization {
331 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
332 // These optimizations require DataLayout.
335 FunctionType *FT = Callee->getFunctionType();
336 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
337 !FT->getParamType(0)->isPointerTy() ||
338 !FT->getParamType(1)->isPointerTy() ||
339 FT->getParamType(2) != TD->getIntPtrType(*Context))
342 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
343 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
344 CI->getArgOperand(2), 1);
345 return CI->getArgOperand(0);
349 //===---------------------------------------===//
350 // 'memset' Optimizations
352 struct MemSetOpt : public LibCallOptimization {
353 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
354 // These optimizations require DataLayout.
357 FunctionType *FT = Callee->getFunctionType();
358 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
359 !FT->getParamType(0)->isPointerTy() ||
360 !FT->getParamType(1)->isIntegerTy() ||
361 FT->getParamType(2) != TD->getIntPtrType(*Context))
364 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
365 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
366 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
367 return CI->getArgOperand(0);
371 //===----------------------------------------------------------------------===//
372 // Math Library Optimizations
373 //===----------------------------------------------------------------------===//
375 //===---------------------------------------===//
376 // Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
378 struct UnaryDoubleFPOpt : public LibCallOptimization {
380 UnaryDoubleFPOpt(bool CheckReturnType): CheckRetType(CheckReturnType) {}
381 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
382 FunctionType *FT = Callee->getFunctionType();
383 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
384 !FT->getParamType(0)->isDoubleTy())
388 // Check if all the uses for function like 'sin' are converted to float.
389 for (Value::use_iterator UseI = CI->use_begin(); UseI != CI->use_end();
391 FPTruncInst *Cast = dyn_cast<FPTruncInst>(*UseI);
392 if (Cast == 0 || !Cast->getType()->isFloatTy())
397 // If this is something like 'floor((double)floatval)', convert to floorf.
398 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
399 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
402 // floor((double)floatval) -> (double)floorf(floatval)
403 Value *V = Cast->getOperand(0);
404 V = EmitUnaryFloatFnCall(V, Callee->getName(), B, Callee->getAttributes());
405 return B.CreateFPExt(V, B.getDoubleTy());
409 //===---------------------------------------===//
410 // 'cos*' Optimizations
411 struct CosOpt : public LibCallOptimization {
412 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
414 if (UnsafeFPShrink && Callee->getName() == "cos" &&
415 TLI->has(LibFunc::cosf)) {
416 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
417 Ret = UnsafeUnaryDoubleFP.CallOptimizer(Callee, CI, B);
420 FunctionType *FT = Callee->getFunctionType();
421 // Just make sure this has 1 argument of FP type, which matches the
423 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
424 !FT->getParamType(0)->isFloatingPointTy())
428 Value *Op1 = CI->getArgOperand(0);
429 if (BinaryOperator::isFNeg(Op1)) {
430 BinaryOperator *BinExpr = cast<BinaryOperator>(Op1);
431 return B.CreateCall(Callee, BinExpr->getOperand(1), "cos");
437 //===---------------------------------------===//
438 // 'pow*' Optimizations
440 struct PowOpt : public LibCallOptimization {
441 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
443 if (UnsafeFPShrink && Callee->getName() == "pow" &&
444 TLI->has(LibFunc::powf)) {
445 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
446 Ret = UnsafeUnaryDoubleFP.CallOptimizer(Callee, CI, B);
449 FunctionType *FT = Callee->getFunctionType();
450 // Just make sure this has 2 arguments of the same FP type, which match the
452 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
453 FT->getParamType(0) != FT->getParamType(1) ||
454 !FT->getParamType(0)->isFloatingPointTy())
457 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
458 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
459 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
461 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
462 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
465 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
466 if (Op2C == 0) return Ret;
468 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
469 return ConstantFP::get(CI->getType(), 1.0);
471 if (Op2C->isExactlyValue(0.5)) {
472 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
473 // This is faster than calling pow, and still handles negative zero
474 // and negative infinity correctly.
475 // TODO: In fast-math mode, this could be just sqrt(x).
476 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
477 Value *Inf = ConstantFP::getInfinity(CI->getType());
478 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
479 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
480 Callee->getAttributes());
481 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
482 Callee->getAttributes());
483 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf);
484 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs);
488 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
490 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
491 return B.CreateFMul(Op1, Op1, "pow2");
492 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
493 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
499 //===---------------------------------------===//
500 // 'exp2' Optimizations
502 struct Exp2Opt : public LibCallOptimization {
503 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
505 if (UnsafeFPShrink && Callee->getName() == "exp2" &&
506 TLI->has(LibFunc::exp2)) {
507 UnaryDoubleFPOpt UnsafeUnaryDoubleFP(true);
508 Ret = UnsafeUnaryDoubleFP.CallOptimizer(Callee, CI, B);
511 FunctionType *FT = Callee->getFunctionType();
512 // Just make sure this has 1 argument of FP type, which matches the
514 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
515 !FT->getParamType(0)->isFloatingPointTy())
518 Value *Op = CI->getArgOperand(0);
519 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
520 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
522 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
523 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
524 LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty());
525 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
526 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
527 LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty());
532 if (Op->getType()->isFloatTy())
534 else if (Op->getType()->isDoubleTy())
539 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
540 if (!Op->getType()->isFloatTy())
541 One = ConstantExpr::getFPExtend(One, Op->getType());
543 Module *M = Caller->getParent();
544 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
546 B.getInt32Ty(), NULL);
547 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
548 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
549 CI->setCallingConv(F->getCallingConv());
557 //===----------------------------------------------------------------------===//
558 // Integer Optimizations
559 //===----------------------------------------------------------------------===//
561 //===---------------------------------------===//
562 // 'ffs*' Optimizations
564 struct FFSOpt : public LibCallOptimization {
565 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
566 FunctionType *FT = Callee->getFunctionType();
567 // Just make sure this has 2 arguments of the same FP type, which match the
569 if (FT->getNumParams() != 1 ||
570 !FT->getReturnType()->isIntegerTy(32) ||
571 !FT->getParamType(0)->isIntegerTy())
574 Value *Op = CI->getArgOperand(0);
577 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
578 if (CI->isZero()) // ffs(0) -> 0.
579 return B.getInt32(0);
580 // ffs(c) -> cttz(c)+1
581 return B.getInt32(CI->getValue().countTrailingZeros() + 1);
584 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
585 Type *ArgType = Op->getType();
586 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
587 Intrinsic::cttz, ArgType);
588 Value *V = B.CreateCall2(F, Op, B.getFalse(), "cttz");
589 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1));
590 V = B.CreateIntCast(V, B.getInt32Ty(), false);
592 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType));
593 return B.CreateSelect(Cond, V, B.getInt32(0));
597 //===---------------------------------------===//
598 // 'isdigit' Optimizations
600 struct IsDigitOpt : public LibCallOptimization {
601 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
602 FunctionType *FT = Callee->getFunctionType();
603 // We require integer(i32)
604 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
605 !FT->getParamType(0)->isIntegerTy(32))
608 // isdigit(c) -> (c-'0') <u 10
609 Value *Op = CI->getArgOperand(0);
610 Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
611 Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
612 return B.CreateZExt(Op, CI->getType());
616 //===---------------------------------------===//
617 // 'isascii' Optimizations
619 struct IsAsciiOpt : public LibCallOptimization {
620 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
621 FunctionType *FT = Callee->getFunctionType();
622 // We require integer(i32)
623 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
624 !FT->getParamType(0)->isIntegerTy(32))
627 // isascii(c) -> c <u 128
628 Value *Op = CI->getArgOperand(0);
629 Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
630 return B.CreateZExt(Op, CI->getType());
634 //===---------------------------------------===//
635 // 'abs', 'labs', 'llabs' Optimizations
637 struct AbsOpt : public LibCallOptimization {
638 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
639 FunctionType *FT = Callee->getFunctionType();
640 // We require integer(integer) where the types agree.
641 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
642 FT->getParamType(0) != FT->getReturnType())
645 // abs(x) -> x >s -1 ? x : -x
646 Value *Op = CI->getArgOperand(0);
647 Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),
649 Value *Neg = B.CreateNeg(Op, "neg");
650 return B.CreateSelect(Pos, Op, Neg);
655 //===---------------------------------------===//
656 // 'toascii' Optimizations
658 struct ToAsciiOpt : public LibCallOptimization {
659 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
660 FunctionType *FT = Callee->getFunctionType();
661 // We require i32(i32)
662 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
663 !FT->getParamType(0)->isIntegerTy(32))
666 // isascii(c) -> c & 0x7f
667 return B.CreateAnd(CI->getArgOperand(0),
668 ConstantInt::get(CI->getType(),0x7F));
672 //===----------------------------------------------------------------------===//
673 // Formatting and IO Optimizations
674 //===----------------------------------------------------------------------===//
676 //===---------------------------------------===//
677 // 'printf' Optimizations
679 struct PrintFOpt : public LibCallOptimization {
680 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
682 // Check for a fixed format string.
684 if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
687 // Empty format string -> noop.
688 if (FormatStr.empty()) // Tolerate printf's declared void.
689 return CI->use_empty() ? (Value*)CI :
690 ConstantInt::get(CI->getType(), 0);
692 // Do not do any of the following transformations if the printf return value
693 // is used, in general the printf return value is not compatible with either
694 // putchar() or puts().
695 if (!CI->use_empty())
698 // printf("x") -> putchar('x'), even for '%'.
699 if (FormatStr.size() == 1) {
700 Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI);
701 if (CI->use_empty() || !Res) return Res;
702 return B.CreateIntCast(Res, CI->getType(), true);
705 // printf("foo\n") --> puts("foo")
706 if (FormatStr[FormatStr.size()-1] == '\n' &&
707 FormatStr.find('%') == std::string::npos) { // no format characters.
708 // Create a string literal with no \n on it. We expect the constant merge
709 // pass to be run after this pass, to merge duplicate strings.
710 FormatStr = FormatStr.drop_back();
711 Value *GV = B.CreateGlobalString(FormatStr, "str");
712 Value *NewCI = EmitPutS(GV, B, TD, TLI);
713 return (CI->use_empty() || !NewCI) ?
715 ConstantInt::get(CI->getType(), FormatStr.size()+1);
718 // Optimize specific format strings.
719 // printf("%c", chr) --> putchar(chr)
720 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
721 CI->getArgOperand(1)->getType()->isIntegerTy()) {
722 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI);
724 if (CI->use_empty() || !Res) return Res;
725 return B.CreateIntCast(Res, CI->getType(), true);
728 // printf("%s\n", str) --> puts(str)
729 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
730 CI->getArgOperand(1)->getType()->isPointerTy()) {
731 return EmitPutS(CI->getArgOperand(1), B, TD, TLI);
736 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
737 // Require one fixed pointer argument and an integer/void result.
738 FunctionType *FT = Callee->getFunctionType();
739 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
740 !(FT->getReturnType()->isIntegerTy() ||
741 FT->getReturnType()->isVoidTy()))
744 if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
748 // printf(format, ...) -> iprintf(format, ...) if no floating point
750 if (TLI->has(LibFunc::iprintf) && !CallHasFloatingPointArgument(CI)) {
751 Module *M = B.GetInsertBlock()->getParent()->getParent();
752 Constant *IPrintFFn =
753 M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
754 CallInst *New = cast<CallInst>(CI->clone());
755 New->setCalledFunction(IPrintFFn);
763 //===---------------------------------------===//
764 // 'sprintf' Optimizations
766 struct SPrintFOpt : public LibCallOptimization {
767 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
769 // Check for a fixed format string.
771 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
774 // If we just have a format string (nothing else crazy) transform it.
775 if (CI->getNumArgOperands() == 2) {
776 // Make sure there's no % in the constant array. We could try to handle
777 // %% -> % in the future if we cared.
778 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
779 if (FormatStr[i] == '%')
780 return 0; // we found a format specifier, bail out.
782 // These optimizations require DataLayout.
785 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
786 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
787 ConstantInt::get(TD->getIntPtrType(*Context), // Copy the
788 FormatStr.size() + 1), 1); // nul byte.
789 return ConstantInt::get(CI->getType(), FormatStr.size());
792 // The remaining optimizations require the format string to be "%s" or "%c"
793 // and have an extra operand.
794 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
795 CI->getNumArgOperands() < 3)
798 // Decode the second character of the format string.
799 if (FormatStr[1] == 'c') {
800 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
801 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
802 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
803 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
804 B.CreateStore(V, Ptr);
805 Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
806 B.CreateStore(B.getInt8(0), Ptr);
808 return ConstantInt::get(CI->getType(), 1);
811 if (FormatStr[1] == 's') {
812 // These optimizations require DataLayout.
815 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
816 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
818 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD, TLI);
821 Value *IncLen = B.CreateAdd(Len,
822 ConstantInt::get(Len->getType(), 1),
824 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
826 // The sprintf result is the unincremented number of bytes in the string.
827 return B.CreateIntCast(Len, CI->getType(), false);
832 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
833 // Require two fixed pointer arguments and an integer result.
834 FunctionType *FT = Callee->getFunctionType();
835 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
836 !FT->getParamType(1)->isPointerTy() ||
837 !FT->getReturnType()->isIntegerTy())
840 if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
844 // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
846 if (TLI->has(LibFunc::siprintf) && !CallHasFloatingPointArgument(CI)) {
847 Module *M = B.GetInsertBlock()->getParent()->getParent();
848 Constant *SIPrintFFn =
849 M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
850 CallInst *New = cast<CallInst>(CI->clone());
851 New->setCalledFunction(SIPrintFFn);
859 //===---------------------------------------===//
860 // 'fwrite' Optimizations
862 struct FWriteOpt : public LibCallOptimization {
863 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
864 // Require a pointer, an integer, an integer, a pointer, returning integer.
865 FunctionType *FT = Callee->getFunctionType();
866 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
867 !FT->getParamType(1)->isIntegerTy() ||
868 !FT->getParamType(2)->isIntegerTy() ||
869 !FT->getParamType(3)->isPointerTy() ||
870 !FT->getReturnType()->isIntegerTy())
873 // Get the element size and count.
874 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
875 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
876 if (!SizeC || !CountC) return 0;
877 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
879 // If this is writing zero records, remove the call (it's a noop).
881 return ConstantInt::get(CI->getType(), 0);
883 // If this is writing one byte, turn it into fputc.
884 // This optimisation is only valid, if the return value is unused.
885 if (Bytes == 1 && CI->use_empty()) { // fwrite(S,1,1,F) -> fputc(S[0],F)
886 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
887 Value *NewCI = EmitFPutC(Char, CI->getArgOperand(3), B, TD, TLI);
888 return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
895 //===---------------------------------------===//
896 // 'fputs' Optimizations
898 struct FPutsOpt : public LibCallOptimization {
899 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
900 // These optimizations require DataLayout.
903 // Require two pointers. Also, we can't optimize if return value is used.
904 FunctionType *FT = Callee->getFunctionType();
905 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
906 !FT->getParamType(1)->isPointerTy() ||
910 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
911 uint64_t Len = GetStringLength(CI->getArgOperand(0));
913 // Known to have no uses (see above).
914 return EmitFWrite(CI->getArgOperand(0),
915 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
916 CI->getArgOperand(1), B, TD, TLI);
920 //===---------------------------------------===//
921 // 'fprintf' Optimizations
923 struct FPrintFOpt : public LibCallOptimization {
924 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
926 // All the optimizations depend on the format string.
928 if (!getConstantStringInfo(CI->getArgOperand(1), FormatStr))
931 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
932 if (CI->getNumArgOperands() == 2) {
933 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
934 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
935 return 0; // We found a format specifier.
937 // These optimizations require DataLayout.
940 Value *NewCI = EmitFWrite(CI->getArgOperand(1),
941 ConstantInt::get(TD->getIntPtrType(*Context),
943 CI->getArgOperand(0), B, TD, TLI);
944 return NewCI ? ConstantInt::get(CI->getType(), FormatStr.size()) : 0;
947 // The remaining optimizations require the format string to be "%s" or "%c"
948 // and have an extra operand.
949 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
950 CI->getNumArgOperands() < 3)
953 // Decode the second character of the format string.
954 if (FormatStr[1] == 'c') {
955 // fprintf(F, "%c", chr) --> fputc(chr, F)
956 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
957 Value *NewCI = EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B,
959 return NewCI ? ConstantInt::get(CI->getType(), 1) : 0;
962 if (FormatStr[1] == 's') {
963 // fprintf(F, "%s", str) --> fputs(str, F)
964 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
966 return EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD, TLI);
971 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
972 // Require two fixed paramters as pointers and integer result.
973 FunctionType *FT = Callee->getFunctionType();
974 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
975 !FT->getParamType(1)->isPointerTy() ||
976 !FT->getReturnType()->isIntegerTy())
979 if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
983 // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
984 // floating point arguments.
985 if (TLI->has(LibFunc::fiprintf) && !CallHasFloatingPointArgument(CI)) {
986 Module *M = B.GetInsertBlock()->getParent()->getParent();
987 Constant *FIPrintFFn =
988 M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
989 CallInst *New = cast<CallInst>(CI->clone());
990 New->setCalledFunction(FIPrintFFn);
998 //===---------------------------------------===//
999 // 'puts' Optimizations
1001 struct PutsOpt : public LibCallOptimization {
1002 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1003 // Require one fixed pointer argument and an integer/void result.
1004 FunctionType *FT = Callee->getFunctionType();
1005 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1006 !(FT->getReturnType()->isIntegerTy() ||
1007 FT->getReturnType()->isVoidTy()))
1010 // Check for a constant string.
1012 if (!getConstantStringInfo(CI->getArgOperand(0), Str))
1015 if (Str.empty() && CI->use_empty()) {
1016 // puts("") -> putchar('\n')
1017 Value *Res = EmitPutChar(B.getInt32('\n'), B, TD, TLI);
1018 if (CI->use_empty() || !Res) return Res;
1019 return B.CreateIntCast(Res, CI->getType(), true);
1026 } // end anonymous namespace.
1028 //===----------------------------------------------------------------------===//
1029 // SimplifyLibCalls Pass Implementation
1030 //===----------------------------------------------------------------------===//
1033 /// This pass optimizes well known library functions from libc and libm.
1035 class SimplifyLibCalls : public FunctionPass {
1036 TargetLibraryInfo *TLI;
1038 StringMap<LibCallOptimization*> Optimizations;
1039 // String and Memory LibCall Optimizations
1040 StrSpnOpt StrSpn; StrCSpnOpt StrCSpn; StrStrOpt StrStr;
1041 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
1042 // Math Library Optimizations
1043 CosOpt Cos; PowOpt Pow; Exp2Opt Exp2;
1044 UnaryDoubleFPOpt UnaryDoubleFP, UnsafeUnaryDoubleFP;
1045 // Integer Optimizations
1046 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1048 // Formatting and IO Optimizations
1049 SPrintFOpt SPrintF; PrintFOpt PrintF;
1050 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
1053 bool Modified; // This is only used by doInitialization.
1055 static char ID; // Pass identification
1056 SimplifyLibCalls() : FunctionPass(ID), UnaryDoubleFP(false),
1057 UnsafeUnaryDoubleFP(true) {
1058 initializeSimplifyLibCallsPass(*PassRegistry::getPassRegistry());
1060 void AddOpt(LibFunc::Func F, LibCallOptimization* Opt);
1061 void AddOpt(LibFunc::Func F1, LibFunc::Func F2, LibCallOptimization* Opt);
1063 void InitOptimizations();
1064 bool runOnFunction(Function &F);
1066 void setDoesNotAccessMemory(Function &F);
1067 void setOnlyReadsMemory(Function &F);
1068 void setDoesNotThrow(Function &F);
1069 void setDoesNotCapture(Function &F, unsigned n);
1070 void setDoesNotAlias(Function &F, unsigned n);
1071 bool doInitialization(Module &M);
1073 void inferPrototypeAttributes(Function &F);
1074 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1075 AU.addRequired<TargetLibraryInfo>();
1078 } // end anonymous namespace.
1080 char SimplifyLibCalls::ID = 0;
1082 INITIALIZE_PASS_BEGIN(SimplifyLibCalls, "simplify-libcalls",
1083 "Simplify well-known library calls", false, false)
1084 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
1085 INITIALIZE_PASS_END(SimplifyLibCalls, "simplify-libcalls",
1086 "Simplify well-known library calls", false, false)
1088 // Public interface to the Simplify LibCalls pass.
1089 FunctionPass *llvm::createSimplifyLibCallsPass() {
1090 return new SimplifyLibCalls();
1093 void SimplifyLibCalls::AddOpt(LibFunc::Func F, LibCallOptimization* Opt) {
1095 Optimizations[TLI->getName(F)] = Opt;
1098 void SimplifyLibCalls::AddOpt(LibFunc::Func F1, LibFunc::Func F2,
1099 LibCallOptimization* Opt) {
1100 if (TLI->has(F1) && TLI->has(F2))
1101 Optimizations[TLI->getName(F1)] = Opt;
1104 /// Optimizations - Populate the Optimizations map with all the optimizations
1106 void SimplifyLibCalls::InitOptimizations() {
1107 // String and Memory LibCall Optimizations
1108 Optimizations["strspn"] = &StrSpn;
1109 Optimizations["strcspn"] = &StrCSpn;
1110 Optimizations["strstr"] = &StrStr;
1111 Optimizations["memcmp"] = &MemCmp;
1112 AddOpt(LibFunc::memcpy, &MemCpy);
1113 Optimizations["memmove"] = &MemMove;
1114 AddOpt(LibFunc::memset, &MemSet);
1116 // Math Library Optimizations
1117 Optimizations["cosf"] = &Cos;
1118 Optimizations["cos"] = &Cos;
1119 Optimizations["cosl"] = &Cos;
1120 Optimizations["powf"] = &Pow;
1121 Optimizations["pow"] = &Pow;
1122 Optimizations["powl"] = &Pow;
1123 Optimizations["llvm.pow.f32"] = &Pow;
1124 Optimizations["llvm.pow.f64"] = &Pow;
1125 Optimizations["llvm.pow.f80"] = &Pow;
1126 Optimizations["llvm.pow.f128"] = &Pow;
1127 Optimizations["llvm.pow.ppcf128"] = &Pow;
1128 Optimizations["exp2l"] = &Exp2;
1129 Optimizations["exp2"] = &Exp2;
1130 Optimizations["exp2f"] = &Exp2;
1131 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1132 Optimizations["llvm.exp2.f128"] = &Exp2;
1133 Optimizations["llvm.exp2.f80"] = &Exp2;
1134 Optimizations["llvm.exp2.f64"] = &Exp2;
1135 Optimizations["llvm.exp2.f32"] = &Exp2;
1137 AddOpt(LibFunc::ceil, LibFunc::ceilf, &UnaryDoubleFP);
1138 AddOpt(LibFunc::fabs, LibFunc::fabsf, &UnaryDoubleFP);
1139 AddOpt(LibFunc::floor, LibFunc::floorf, &UnaryDoubleFP);
1140 AddOpt(LibFunc::rint, LibFunc::rintf, &UnaryDoubleFP);
1141 AddOpt(LibFunc::round, LibFunc::roundf, &UnaryDoubleFP);
1142 AddOpt(LibFunc::nearbyint, LibFunc::nearbyintf, &UnaryDoubleFP);
1143 AddOpt(LibFunc::trunc, LibFunc::truncf, &UnaryDoubleFP);
1145 if(UnsafeFPShrink) {
1146 AddOpt(LibFunc::acos, LibFunc::acosf, &UnsafeUnaryDoubleFP);
1147 AddOpt(LibFunc::acosh, LibFunc::acoshf, &UnsafeUnaryDoubleFP);
1148 AddOpt(LibFunc::asin, LibFunc::asinf, &UnsafeUnaryDoubleFP);
1149 AddOpt(LibFunc::asinh, LibFunc::asinhf, &UnsafeUnaryDoubleFP);
1150 AddOpt(LibFunc::atan, LibFunc::atanf, &UnsafeUnaryDoubleFP);
1151 AddOpt(LibFunc::atanh, LibFunc::atanhf, &UnsafeUnaryDoubleFP);
1152 AddOpt(LibFunc::cbrt, LibFunc::cbrtf, &UnsafeUnaryDoubleFP);
1153 AddOpt(LibFunc::cosh, LibFunc::coshf, &UnsafeUnaryDoubleFP);
1154 AddOpt(LibFunc::exp, LibFunc::expf, &UnsafeUnaryDoubleFP);
1155 AddOpt(LibFunc::exp10, LibFunc::exp10f, &UnsafeUnaryDoubleFP);
1156 AddOpt(LibFunc::expm1, LibFunc::expm1f, &UnsafeUnaryDoubleFP);
1157 AddOpt(LibFunc::log, LibFunc::logf, &UnsafeUnaryDoubleFP);
1158 AddOpt(LibFunc::log10, LibFunc::log10f, &UnsafeUnaryDoubleFP);
1159 AddOpt(LibFunc::log1p, LibFunc::log1pf, &UnsafeUnaryDoubleFP);
1160 AddOpt(LibFunc::log2, LibFunc::log2f, &UnsafeUnaryDoubleFP);
1161 AddOpt(LibFunc::logb, LibFunc::logbf, &UnsafeUnaryDoubleFP);
1162 AddOpt(LibFunc::sin, LibFunc::sinf, &UnsafeUnaryDoubleFP);
1163 AddOpt(LibFunc::sinh, LibFunc::sinhf, &UnsafeUnaryDoubleFP);
1164 AddOpt(LibFunc::sqrt, LibFunc::sqrtf, &UnsafeUnaryDoubleFP);
1165 AddOpt(LibFunc::tan, LibFunc::tanf, &UnsafeUnaryDoubleFP);
1166 AddOpt(LibFunc::tanh, LibFunc::tanhf, &UnsafeUnaryDoubleFP);
1169 // Integer Optimizations
1170 Optimizations["ffs"] = &FFS;
1171 Optimizations["ffsl"] = &FFS;
1172 Optimizations["ffsll"] = &FFS;
1173 Optimizations["abs"] = &Abs;
1174 Optimizations["labs"] = &Abs;
1175 Optimizations["llabs"] = &Abs;
1176 Optimizations["isdigit"] = &IsDigit;
1177 Optimizations["isascii"] = &IsAscii;
1178 Optimizations["toascii"] = &ToAscii;
1180 // Formatting and IO Optimizations
1181 Optimizations["sprintf"] = &SPrintF;
1182 Optimizations["printf"] = &PrintF;
1183 AddOpt(LibFunc::fwrite, &FWrite);
1184 AddOpt(LibFunc::fputs, &FPuts);
1185 Optimizations["fprintf"] = &FPrintF;
1186 Optimizations["puts"] = &Puts;
1190 /// runOnFunction - Top level algorithm.
1192 bool SimplifyLibCalls::runOnFunction(Function &F) {
1193 TLI = &getAnalysis<TargetLibraryInfo>();
1195 if (Optimizations.empty())
1196 InitOptimizations();
1198 const DataLayout *TD = getAnalysisIfAvailable<DataLayout>();
1200 IRBuilder<> Builder(F.getContext());
1202 bool Changed = false;
1203 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1204 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1205 // Ignore non-calls.
1206 CallInst *CI = dyn_cast<CallInst>(I++);
1209 // Ignore indirect calls and calls to non-external functions.
1210 Function *Callee = CI->getCalledFunction();
1211 if (Callee == 0 || !Callee->isDeclaration() ||
1212 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1215 // Ignore unknown calls.
1216 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1219 // Set the builder to the instruction after the call.
1220 Builder.SetInsertPoint(BB, I);
1222 // Use debug location of CI for all new instructions.
1223 Builder.SetCurrentDebugLocation(CI->getDebugLoc());
1225 // Try to optimize this call.
1226 Value *Result = LCO->OptimizeCall(CI, TD, TLI, Builder);
1227 if (Result == 0) continue;
1229 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1230 dbgs() << " into: " << *Result << "\n");
1232 // Something changed!
1236 // Inspect the instruction after the call (which was potentially just
1240 if (CI != Result && !CI->use_empty()) {
1241 CI->replaceAllUsesWith(Result);
1242 if (!Result->hasName())
1243 Result->takeName(CI);
1245 CI->eraseFromParent();
1251 // Utility methods for doInitialization.
1253 void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1254 if (!F.doesNotAccessMemory()) {
1255 F.setDoesNotAccessMemory();
1260 void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1261 if (!F.onlyReadsMemory()) {
1262 F.setOnlyReadsMemory();
1267 void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1268 if (!F.doesNotThrow()) {
1269 F.setDoesNotThrow();
1274 void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1275 if (!F.doesNotCapture(n)) {
1276 F.setDoesNotCapture(n);
1281 void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1282 if (!F.doesNotAlias(n)) {
1283 F.setDoesNotAlias(n);
1290 void SimplifyLibCalls::inferPrototypeAttributes(Function &F) {
1291 FunctionType *FTy = F.getFunctionType();
1293 StringRef Name = F.getName();
1296 if (Name == "strlen") {
1297 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1299 setOnlyReadsMemory(F);
1301 setDoesNotCapture(F, 1);
1302 } else if (Name == "strchr" ||
1303 Name == "strrchr") {
1304 if (FTy->getNumParams() != 2 ||
1305 !FTy->getParamType(0)->isPointerTy() ||
1306 !FTy->getParamType(1)->isIntegerTy())
1308 setOnlyReadsMemory(F);
1310 } else if (Name == "strcpy" ||
1316 Name == "strtoul" ||
1317 Name == "strtoll" ||
1318 Name == "strtold" ||
1319 Name == "strncat" ||
1320 Name == "strncpy" ||
1321 Name == "stpncpy" ||
1322 Name == "strtoull") {
1323 if (FTy->getNumParams() < 2 ||
1324 !FTy->getParamType(1)->isPointerTy())
1327 setDoesNotCapture(F, 2);
1328 } else if (Name == "strxfrm") {
1329 if (FTy->getNumParams() != 3 ||
1330 !FTy->getParamType(0)->isPointerTy() ||
1331 !FTy->getParamType(1)->isPointerTy())
1334 setDoesNotCapture(F, 1);
1335 setDoesNotCapture(F, 2);
1336 } else if (Name == "strcmp" ||
1338 Name == "strncmp" ||
1339 Name == "strcspn" ||
1340 Name == "strcoll" ||
1341 Name == "strcasecmp" ||
1342 Name == "strncasecmp") {
1343 if (FTy->getNumParams() < 2 ||
1344 !FTy->getParamType(0)->isPointerTy() ||
1345 !FTy->getParamType(1)->isPointerTy())
1347 setOnlyReadsMemory(F);
1349 setDoesNotCapture(F, 1);
1350 setDoesNotCapture(F, 2);
1351 } else if (Name == "strstr" ||
1352 Name == "strpbrk") {
1353 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1355 setOnlyReadsMemory(F);
1357 setDoesNotCapture(F, 2);
1358 } else if (Name == "strtok" ||
1359 Name == "strtok_r") {
1360 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1363 setDoesNotCapture(F, 2);
1364 } else if (Name == "scanf" ||
1366 Name == "setvbuf") {
1367 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1370 setDoesNotCapture(F, 1);
1371 } else if (Name == "strdup" ||
1372 Name == "strndup") {
1373 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1374 !FTy->getParamType(0)->isPointerTy())
1377 setDoesNotAlias(F, 0);
1378 setDoesNotCapture(F, 1);
1379 } else if (Name == "stat" ||
1381 Name == "sprintf" ||
1382 Name == "statvfs") {
1383 if (FTy->getNumParams() < 2 ||
1384 !FTy->getParamType(0)->isPointerTy() ||
1385 !FTy->getParamType(1)->isPointerTy())
1388 setDoesNotCapture(F, 1);
1389 setDoesNotCapture(F, 2);
1390 } else if (Name == "snprintf") {
1391 if (FTy->getNumParams() != 3 ||
1392 !FTy->getParamType(0)->isPointerTy() ||
1393 !FTy->getParamType(2)->isPointerTy())
1396 setDoesNotCapture(F, 1);
1397 setDoesNotCapture(F, 3);
1398 } else if (Name == "setitimer") {
1399 if (FTy->getNumParams() != 3 ||
1400 !FTy->getParamType(1)->isPointerTy() ||
1401 !FTy->getParamType(2)->isPointerTy())
1404 setDoesNotCapture(F, 2);
1405 setDoesNotCapture(F, 3);
1406 } else if (Name == "system") {
1407 if (FTy->getNumParams() != 1 ||
1408 !FTy->getParamType(0)->isPointerTy())
1410 // May throw; "system" is a valid pthread cancellation point.
1411 setDoesNotCapture(F, 1);
1415 if (Name == "malloc") {
1416 if (FTy->getNumParams() != 1 ||
1417 !FTy->getReturnType()->isPointerTy())
1420 setDoesNotAlias(F, 0);
1421 } else if (Name == "memcmp") {
1422 if (FTy->getNumParams() != 3 ||
1423 !FTy->getParamType(0)->isPointerTy() ||
1424 !FTy->getParamType(1)->isPointerTy())
1426 setOnlyReadsMemory(F);
1428 setDoesNotCapture(F, 1);
1429 setDoesNotCapture(F, 2);
1430 } else if (Name == "memchr" ||
1431 Name == "memrchr") {
1432 if (FTy->getNumParams() != 3)
1434 setOnlyReadsMemory(F);
1436 } else if (Name == "modf" ||
1440 Name == "memccpy" ||
1441 Name == "memmove") {
1442 if (FTy->getNumParams() < 2 ||
1443 !FTy->getParamType(1)->isPointerTy())
1446 setDoesNotCapture(F, 2);
1447 } else if (Name == "memalign") {
1448 if (!FTy->getReturnType()->isPointerTy())
1450 setDoesNotAlias(F, 0);
1451 } else if (Name == "mkdir" ||
1453 if (FTy->getNumParams() == 0 ||
1454 !FTy->getParamType(0)->isPointerTy())
1457 setDoesNotCapture(F, 1);
1461 if (Name == "realloc") {
1462 if (FTy->getNumParams() != 2 ||
1463 !FTy->getParamType(0)->isPointerTy() ||
1464 !FTy->getReturnType()->isPointerTy())
1467 setDoesNotAlias(F, 0);
1468 setDoesNotCapture(F, 1);
1469 } else if (Name == "read") {
1470 if (FTy->getNumParams() != 3 ||
1471 !FTy->getParamType(1)->isPointerTy())
1473 // May throw; "read" is a valid pthread cancellation point.
1474 setDoesNotCapture(F, 2);
1475 } else if (Name == "rmdir" ||
1478 Name == "realpath") {
1479 if (FTy->getNumParams() < 1 ||
1480 !FTy->getParamType(0)->isPointerTy())
1483 setDoesNotCapture(F, 1);
1484 } else if (Name == "rename" ||
1485 Name == "readlink") {
1486 if (FTy->getNumParams() < 2 ||
1487 !FTy->getParamType(0)->isPointerTy() ||
1488 !FTy->getParamType(1)->isPointerTy())
1491 setDoesNotCapture(F, 1);
1492 setDoesNotCapture(F, 2);
1496 if (Name == "write") {
1497 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1499 // May throw; "write" is a valid pthread cancellation point.
1500 setDoesNotCapture(F, 2);
1504 if (Name == "bcopy") {
1505 if (FTy->getNumParams() != 3 ||
1506 !FTy->getParamType(0)->isPointerTy() ||
1507 !FTy->getParamType(1)->isPointerTy())
1510 setDoesNotCapture(F, 1);
1511 setDoesNotCapture(F, 2);
1512 } else if (Name == "bcmp") {
1513 if (FTy->getNumParams() != 3 ||
1514 !FTy->getParamType(0)->isPointerTy() ||
1515 !FTy->getParamType(1)->isPointerTy())
1518 setOnlyReadsMemory(F);
1519 setDoesNotCapture(F, 1);
1520 setDoesNotCapture(F, 2);
1521 } else if (Name == "bzero") {
1522 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1525 setDoesNotCapture(F, 1);
1529 if (Name == "calloc") {
1530 if (FTy->getNumParams() != 2 ||
1531 !FTy->getReturnType()->isPointerTy())
1534 setDoesNotAlias(F, 0);
1535 } else if (Name == "chmod" ||
1537 Name == "ctermid" ||
1538 Name == "clearerr" ||
1539 Name == "closedir") {
1540 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1543 setDoesNotCapture(F, 1);
1547 if (Name == "atoi" ||
1551 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1554 setOnlyReadsMemory(F);
1555 setDoesNotCapture(F, 1);
1556 } else if (Name == "access") {
1557 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1560 setDoesNotCapture(F, 1);
1564 if (Name == "fopen") {
1565 if (FTy->getNumParams() != 2 ||
1566 !FTy->getReturnType()->isPointerTy() ||
1567 !FTy->getParamType(0)->isPointerTy() ||
1568 !FTy->getParamType(1)->isPointerTy())
1571 setDoesNotAlias(F, 0);
1572 setDoesNotCapture(F, 1);
1573 setDoesNotCapture(F, 2);
1574 } else if (Name == "fdopen") {
1575 if (FTy->getNumParams() != 2 ||
1576 !FTy->getReturnType()->isPointerTy() ||
1577 !FTy->getParamType(1)->isPointerTy())
1580 setDoesNotAlias(F, 0);
1581 setDoesNotCapture(F, 2);
1582 } else if (Name == "feof" ||
1592 Name == "fsetpos" ||
1593 Name == "flockfile" ||
1594 Name == "funlockfile" ||
1595 Name == "ftrylockfile") {
1596 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1599 setDoesNotCapture(F, 1);
1600 } else if (Name == "ferror") {
1601 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1604 setDoesNotCapture(F, 1);
1605 setOnlyReadsMemory(F);
1606 } else if (Name == "fputc" ||
1611 Name == "fstatvfs") {
1612 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1615 setDoesNotCapture(F, 2);
1616 } else if (Name == "fgets") {
1617 if (FTy->getNumParams() != 3 ||
1618 !FTy->getParamType(0)->isPointerTy() ||
1619 !FTy->getParamType(2)->isPointerTy())
1622 setDoesNotCapture(F, 3);
1623 } else if (Name == "fread" ||
1625 if (FTy->getNumParams() != 4 ||
1626 !FTy->getParamType(0)->isPointerTy() ||
1627 !FTy->getParamType(3)->isPointerTy())
1630 setDoesNotCapture(F, 1);
1631 setDoesNotCapture(F, 4);
1632 } else if (Name == "fputs" ||
1634 Name == "fprintf" ||
1635 Name == "fgetpos") {
1636 if (FTy->getNumParams() < 2 ||
1637 !FTy->getParamType(0)->isPointerTy() ||
1638 !FTy->getParamType(1)->isPointerTy())
1641 setDoesNotCapture(F, 1);
1642 setDoesNotCapture(F, 2);
1646 if (Name == "getc" ||
1647 Name == "getlogin_r" ||
1648 Name == "getc_unlocked") {
1649 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1652 setDoesNotCapture(F, 1);
1653 } else if (Name == "getenv") {
1654 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1657 setOnlyReadsMemory(F);
1658 setDoesNotCapture(F, 1);
1659 } else if (Name == "gets" ||
1660 Name == "getchar") {
1662 } else if (Name == "getitimer") {
1663 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1666 setDoesNotCapture(F, 2);
1667 } else if (Name == "getpwnam") {
1668 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1671 setDoesNotCapture(F, 1);
1675 if (Name == "ungetc") {
1676 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1679 setDoesNotCapture(F, 2);
1680 } else if (Name == "uname" ||
1682 Name == "unsetenv") {
1683 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1686 setDoesNotCapture(F, 1);
1687 } else if (Name == "utime" ||
1689 if (FTy->getNumParams() != 2 ||
1690 !FTy->getParamType(0)->isPointerTy() ||
1691 !FTy->getParamType(1)->isPointerTy())
1694 setDoesNotCapture(F, 1);
1695 setDoesNotCapture(F, 2);
1699 if (Name == "putc") {
1700 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1703 setDoesNotCapture(F, 2);
1704 } else if (Name == "puts" ||
1707 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1710 setDoesNotCapture(F, 1);
1711 } else if (Name == "pread" ||
1713 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1715 // May throw; these are valid pthread cancellation points.
1716 setDoesNotCapture(F, 2);
1717 } else if (Name == "putchar") {
1719 } else if (Name == "popen") {
1720 if (FTy->getNumParams() != 2 ||
1721 !FTy->getReturnType()->isPointerTy() ||
1722 !FTy->getParamType(0)->isPointerTy() ||
1723 !FTy->getParamType(1)->isPointerTy())
1726 setDoesNotAlias(F, 0);
1727 setDoesNotCapture(F, 1);
1728 setDoesNotCapture(F, 2);
1729 } else if (Name == "pclose") {
1730 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1733 setDoesNotCapture(F, 1);
1737 if (Name == "vscanf") {
1738 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1741 setDoesNotCapture(F, 1);
1742 } else if (Name == "vsscanf" ||
1743 Name == "vfscanf") {
1744 if (FTy->getNumParams() != 3 ||
1745 !FTy->getParamType(1)->isPointerTy() ||
1746 !FTy->getParamType(2)->isPointerTy())
1749 setDoesNotCapture(F, 1);
1750 setDoesNotCapture(F, 2);
1751 } else if (Name == "valloc") {
1752 if (!FTy->getReturnType()->isPointerTy())
1755 setDoesNotAlias(F, 0);
1756 } else if (Name == "vprintf") {
1757 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1760 setDoesNotCapture(F, 1);
1761 } else if (Name == "vfprintf" ||
1762 Name == "vsprintf") {
1763 if (FTy->getNumParams() != 3 ||
1764 !FTy->getParamType(0)->isPointerTy() ||
1765 !FTy->getParamType(1)->isPointerTy())
1768 setDoesNotCapture(F, 1);
1769 setDoesNotCapture(F, 2);
1770 } else if (Name == "vsnprintf") {
1771 if (FTy->getNumParams() != 4 ||
1772 !FTy->getParamType(0)->isPointerTy() ||
1773 !FTy->getParamType(2)->isPointerTy())
1776 setDoesNotCapture(F, 1);
1777 setDoesNotCapture(F, 3);
1781 if (Name == "open") {
1782 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1784 // May throw; "open" is a valid pthread cancellation point.
1785 setDoesNotCapture(F, 1);
1786 } else if (Name == "opendir") {
1787 if (FTy->getNumParams() != 1 ||
1788 !FTy->getReturnType()->isPointerTy() ||
1789 !FTy->getParamType(0)->isPointerTy())
1792 setDoesNotAlias(F, 0);
1793 setDoesNotCapture(F, 1);
1797 if (Name == "tmpfile") {
1798 if (!FTy->getReturnType()->isPointerTy())
1801 setDoesNotAlias(F, 0);
1802 } else if (Name == "times") {
1803 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1806 setDoesNotCapture(F, 1);
1810 if (Name == "htonl" ||
1813 setDoesNotAccessMemory(F);
1817 if (Name == "ntohl" ||
1820 setDoesNotAccessMemory(F);
1824 if (Name == "lstat") {
1825 if (FTy->getNumParams() != 2 ||
1826 !FTy->getParamType(0)->isPointerTy() ||
1827 !FTy->getParamType(1)->isPointerTy())
1830 setDoesNotCapture(F, 1);
1831 setDoesNotCapture(F, 2);
1832 } else if (Name == "lchown") {
1833 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1836 setDoesNotCapture(F, 1);
1840 if (Name == "qsort") {
1841 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1843 // May throw; places call through function pointer.
1844 setDoesNotCapture(F, 4);
1848 if (Name == "__strdup" ||
1849 Name == "__strndup") {
1850 if (FTy->getNumParams() < 1 ||
1851 !FTy->getReturnType()->isPointerTy() ||
1852 !FTy->getParamType(0)->isPointerTy())
1855 setDoesNotAlias(F, 0);
1856 setDoesNotCapture(F, 1);
1857 } else if (Name == "__strtok_r") {
1858 if (FTy->getNumParams() != 3 ||
1859 !FTy->getParamType(1)->isPointerTy())
1862 setDoesNotCapture(F, 2);
1863 } else if (Name == "_IO_getc") {
1864 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1867 setDoesNotCapture(F, 1);
1868 } else if (Name == "_IO_putc") {
1869 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1872 setDoesNotCapture(F, 2);
1876 if (Name == "\1__isoc99_scanf") {
1877 if (FTy->getNumParams() < 1 ||
1878 !FTy->getParamType(0)->isPointerTy())
1881 setDoesNotCapture(F, 1);
1882 } else if (Name == "\1stat64" ||
1883 Name == "\1lstat64" ||
1884 Name == "\1statvfs64" ||
1885 Name == "\1__isoc99_sscanf") {
1886 if (FTy->getNumParams() < 1 ||
1887 !FTy->getParamType(0)->isPointerTy() ||
1888 !FTy->getParamType(1)->isPointerTy())
1891 setDoesNotCapture(F, 1);
1892 setDoesNotCapture(F, 2);
1893 } else if (Name == "\1fopen64") {
1894 if (FTy->getNumParams() != 2 ||
1895 !FTy->getReturnType()->isPointerTy() ||
1896 !FTy->getParamType(0)->isPointerTy() ||
1897 !FTy->getParamType(1)->isPointerTy())
1900 setDoesNotAlias(F, 0);
1901 setDoesNotCapture(F, 1);
1902 setDoesNotCapture(F, 2);
1903 } else if (Name == "\1fseeko64" ||
1904 Name == "\1ftello64") {
1905 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1908 setDoesNotCapture(F, 1);
1909 } else if (Name == "\1tmpfile64") {
1910 if (!FTy->getReturnType()->isPointerTy())
1913 setDoesNotAlias(F, 0);
1914 } else if (Name == "\1fstat64" ||
1915 Name == "\1fstatvfs64") {
1916 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1919 setDoesNotCapture(F, 2);
1920 } else if (Name == "\1open64") {
1921 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1923 // May throw; "open" is a valid pthread cancellation point.
1924 setDoesNotCapture(F, 1);
1930 /// doInitialization - Add attributes to well-known functions.
1932 bool SimplifyLibCalls::doInitialization(Module &M) {
1934 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1936 if (F.isDeclaration() && F.hasName())
1937 inferPrototypeAttributes(F);
1943 // Additional cases that we need to add to this file:
1946 // * cbrt(expN(X)) -> expN(x/3)
1947 // * cbrt(sqrt(x)) -> pow(x,1/6)
1948 // * cbrt(sqrt(x)) -> pow(x,1/9)
1951 // * exp(log(x)) -> x
1954 // * log(exp(x)) -> x
1955 // * log(x**y) -> y*log(x)
1956 // * log(exp(y)) -> y*log(e)
1957 // * log(exp2(y)) -> y*log(2)
1958 // * log(exp10(y)) -> y*log(10)
1959 // * log(sqrt(x)) -> 0.5*log(x)
1960 // * log(pow(x,y)) -> y*log(x)
1962 // lround, lroundf, lroundl:
1963 // * lround(cnst) -> cnst'
1966 // * pow(exp(x),y) -> exp(x*y)
1967 // * pow(sqrt(x),y) -> pow(x,y*0.5)
1968 // * pow(pow(x,y),z)-> pow(x,y*z)
1970 // round, roundf, roundl:
1971 // * round(cnst) -> cnst'
1974 // * signbit(cnst) -> cnst'
1975 // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
1977 // sqrt, sqrtf, sqrtl:
1978 // * sqrt(expN(x)) -> expN(x*0.5)
1979 // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
1980 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
1983 // * strchr(p, 0) -> strlen(p)
1985 // * tan(atan(x)) -> x
1987 // trunc, truncf, truncl:
1988 // * trunc(cnst) -> cnst'