Silence -Wunused-variable in release builds.
[llvm/stm8.git] / lib / Transforms / Scalar / SimplifyLibCalls.cpp
blob6247b0348f14e1828075ca9b6941a8f6506479e0
1 //===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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/Intrinsics.h"
22 #include "llvm/LLVMContext.h"
23 #include "llvm/Module.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/IRBuilder.h"
26 #include "llvm/Analysis/ValueTracking.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetLibraryInfo.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/StringMap.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Config/config.h" // FIXME: Shouldn't depend on host!
36 using namespace llvm;
38 STATISTIC(NumSimplified, "Number of library calls simplified");
39 STATISTIC(NumAnnotated, "Number of attributes added to library functions");
41 //===----------------------------------------------------------------------===//
42 // Optimizer Base Class
43 //===----------------------------------------------------------------------===//
45 /// This class is the abstract base class for the set of optimizations that
46 /// corresponds to one library call.
47 namespace {
48 class LibCallOptimization {
49 protected:
50 Function *Caller;
51 const TargetData *TD;
52 const TargetLibraryInfo *TLI;
53 LLVMContext* Context;
54 public:
55 LibCallOptimization() { }
56 virtual ~LibCallOptimization() {}
58 /// CallOptimizer - This pure virtual method is implemented by base classes to
59 /// do various optimizations. If this returns null then no transformation was
60 /// performed. If it returns CI, then it transformed the call and CI is to be
61 /// deleted. If it returns something else, replace CI with the new value and
62 /// delete CI.
63 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
64 =0;
66 Value *OptimizeCall(CallInst *CI, const TargetData *TD,
67 const TargetLibraryInfo *TLI, IRBuilder<> &B) {
68 Caller = CI->getParent()->getParent();
69 this->TD = TD;
70 this->TLI = TLI;
71 if (CI->getCalledFunction())
72 Context = &CI->getCalledFunction()->getContext();
74 // We never change the calling convention.
75 if (CI->getCallingConv() != llvm::CallingConv::C)
76 return NULL;
78 return CallOptimizer(CI->getCalledFunction(), CI, B);
81 } // End anonymous namespace.
84 //===----------------------------------------------------------------------===//
85 // Helper Functions
86 //===----------------------------------------------------------------------===//
88 /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
89 /// value is equal or not-equal to zero.
90 static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
91 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
92 UI != E; ++UI) {
93 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
94 if (IC->isEquality())
95 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
96 if (C->isNullValue())
97 continue;
98 // Unknown instruction.
99 return false;
101 return true;
104 static bool CallHasFloatingPointArgument(const CallInst *CI) {
105 for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
106 it != e; ++it) {
107 if ((*it)->getType()->isFloatingPointTy())
108 return true;
110 return false;
113 /// IsOnlyUsedInEqualityComparison - Return true if it is only used in equality
114 /// comparisons with With.
115 static bool IsOnlyUsedInEqualityComparison(Value *V, Value *With) {
116 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
117 UI != E; ++UI) {
118 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
119 if (IC->isEquality() && IC->getOperand(1) == With)
120 continue;
121 // Unknown instruction.
122 return false;
124 return true;
127 //===----------------------------------------------------------------------===//
128 // String and Memory LibCall Optimizations
129 //===----------------------------------------------------------------------===//
131 //===---------------------------------------===//
132 // 'strcat' Optimizations
133 namespace {
134 struct StrCatOpt : public LibCallOptimization {
135 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
136 // Verify the "strcat" function prototype.
137 const FunctionType *FT = Callee->getFunctionType();
138 if (FT->getNumParams() != 2 ||
139 FT->getReturnType() != B.getInt8PtrTy() ||
140 FT->getParamType(0) != FT->getReturnType() ||
141 FT->getParamType(1) != FT->getReturnType())
142 return 0;
144 // Extract some information from the instruction
145 Value *Dst = CI->getArgOperand(0);
146 Value *Src = CI->getArgOperand(1);
148 // See if we can get the length of the input string.
149 uint64_t Len = GetStringLength(Src);
150 if (Len == 0) return 0;
151 --Len; // Unbias length.
153 // Handle the simple, do-nothing case: strcat(x, "") -> x
154 if (Len == 0)
155 return Dst;
157 // These optimizations require TargetData.
158 if (!TD) return 0;
160 EmitStrLenMemCpy(Src, Dst, Len, B);
161 return Dst;
164 void EmitStrLenMemCpy(Value *Src, Value *Dst, uint64_t Len, IRBuilder<> &B) {
165 // We need to find the end of the destination string. That's where the
166 // memory is to be moved to. We just generate a call to strlen.
167 Value *DstLen = EmitStrLen(Dst, B, TD);
169 // Now that we have the destination's length, we must index into the
170 // destination's pointer to get the actual memcpy destination (end of
171 // the string .. we're concatenating).
172 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
174 // We have enough information to now generate the memcpy call to do the
175 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
176 B.CreateMemCpy(CpyDst, Src,
177 ConstantInt::get(TD->getIntPtrType(*Context), Len + 1), 1);
181 //===---------------------------------------===//
182 // 'strncat' Optimizations
184 struct StrNCatOpt : public StrCatOpt {
185 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
186 // Verify the "strncat" function prototype.
187 const FunctionType *FT = Callee->getFunctionType();
188 if (FT->getNumParams() != 3 ||
189 FT->getReturnType() != B.getInt8PtrTy() ||
190 FT->getParamType(0) != FT->getReturnType() ||
191 FT->getParamType(1) != FT->getReturnType() ||
192 !FT->getParamType(2)->isIntegerTy())
193 return 0;
195 // Extract some information from the instruction
196 Value *Dst = CI->getArgOperand(0);
197 Value *Src = CI->getArgOperand(1);
198 uint64_t Len;
200 // We don't do anything if length is not constant
201 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
202 Len = LengthArg->getZExtValue();
203 else
204 return 0;
206 // See if we can get the length of the input string.
207 uint64_t SrcLen = GetStringLength(Src);
208 if (SrcLen == 0) return 0;
209 --SrcLen; // Unbias length.
211 // Handle the simple, do-nothing cases:
212 // strncat(x, "", c) -> x
213 // strncat(x, c, 0) -> x
214 if (SrcLen == 0 || Len == 0) return Dst;
216 // These optimizations require TargetData.
217 if (!TD) return 0;
219 // We don't optimize this case
220 if (Len < SrcLen) return 0;
222 // strncat(x, s, c) -> strcat(x, s)
223 // s is constant so the strcat can be optimized further
224 EmitStrLenMemCpy(Src, Dst, SrcLen, B);
225 return Dst;
229 //===---------------------------------------===//
230 // 'strchr' Optimizations
232 struct StrChrOpt : public LibCallOptimization {
233 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
234 // Verify the "strchr" function prototype.
235 const FunctionType *FT = Callee->getFunctionType();
236 if (FT->getNumParams() != 2 ||
237 FT->getReturnType() != B.getInt8PtrTy() ||
238 FT->getParamType(0) != FT->getReturnType() ||
239 !FT->getParamType(1)->isIntegerTy(32))
240 return 0;
242 Value *SrcStr = CI->getArgOperand(0);
244 // If the second operand is non-constant, see if we can compute the length
245 // of the input string and turn this into memchr.
246 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
247 if (CharC == 0) {
248 // These optimizations require TargetData.
249 if (!TD) return 0;
251 uint64_t Len = GetStringLength(SrcStr);
252 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
253 return 0;
255 return EmitMemChr(SrcStr, CI->getArgOperand(1), // include nul.
256 ConstantInt::get(TD->getIntPtrType(*Context), Len),
257 B, TD);
260 // Otherwise, the character is a constant, see if the first argument is
261 // a string literal. If so, we can constant fold.
262 std::string Str;
263 if (!GetConstantStringInfo(SrcStr, Str))
264 return 0;
266 // strchr can find the nul character.
267 Str += '\0';
269 // Compute the offset.
270 size_t I = Str.find(CharC->getSExtValue());
271 if (I == std::string::npos) // Didn't find the char. strchr returns null.
272 return Constant::getNullValue(CI->getType());
274 // strchr(s+n,c) -> gep(s+n+i,c)
275 return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");
279 //===---------------------------------------===//
280 // 'strrchr' Optimizations
282 struct StrRChrOpt : public LibCallOptimization {
283 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
284 // Verify the "strrchr" function prototype.
285 const FunctionType *FT = Callee->getFunctionType();
286 if (FT->getNumParams() != 2 ||
287 FT->getReturnType() != B.getInt8PtrTy() ||
288 FT->getParamType(0) != FT->getReturnType() ||
289 !FT->getParamType(1)->isIntegerTy(32))
290 return 0;
292 Value *SrcStr = CI->getArgOperand(0);
293 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
295 // Cannot fold anything if we're not looking for a constant.
296 if (!CharC)
297 return 0;
299 std::string Str;
300 if (!GetConstantStringInfo(SrcStr, Str)) {
301 // strrchr(s, 0) -> strchr(s, 0)
302 if (TD && CharC->isZero())
303 return EmitStrChr(SrcStr, '\0', B, TD);
304 return 0;
307 // strrchr can find the nul character.
308 Str += '\0';
310 // Compute the offset.
311 size_t I = Str.rfind(CharC->getSExtValue());
312 if (I == std::string::npos) // Didn't find the char. Return null.
313 return Constant::getNullValue(CI->getType());
315 // strrchr(s+n,c) -> gep(s+n+i,c)
316 return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");
320 //===---------------------------------------===//
321 // 'strcmp' Optimizations
323 struct StrCmpOpt : public LibCallOptimization {
324 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
325 // Verify the "strcmp" function prototype.
326 const FunctionType *FT = Callee->getFunctionType();
327 if (FT->getNumParams() != 2 ||
328 !FT->getReturnType()->isIntegerTy(32) ||
329 FT->getParamType(0) != FT->getParamType(1) ||
330 FT->getParamType(0) != B.getInt8PtrTy())
331 return 0;
333 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
334 if (Str1P == Str2P) // strcmp(x,x) -> 0
335 return ConstantInt::get(CI->getType(), 0);
337 std::string Str1, Str2;
338 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
339 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
341 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
342 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
344 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
345 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
347 // strcmp(x, y) -> cnst (if both x and y are constant strings)
348 if (HasStr1 && HasStr2)
349 return ConstantInt::get(CI->getType(),
350 strcmp(Str1.c_str(),Str2.c_str()));
352 // strcmp(P, "x") -> memcmp(P, "x", 2)
353 uint64_t Len1 = GetStringLength(Str1P);
354 uint64_t Len2 = GetStringLength(Str2P);
355 if (Len1 && Len2) {
356 // These optimizations require TargetData.
357 if (!TD) return 0;
359 return EmitMemCmp(Str1P, Str2P,
360 ConstantInt::get(TD->getIntPtrType(*Context),
361 std::min(Len1, Len2)), B, TD);
364 return 0;
368 //===---------------------------------------===//
369 // 'strncmp' Optimizations
371 struct StrNCmpOpt : public LibCallOptimization {
372 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
373 // Verify the "strncmp" function prototype.
374 const FunctionType *FT = Callee->getFunctionType();
375 if (FT->getNumParams() != 3 ||
376 !FT->getReturnType()->isIntegerTy(32) ||
377 FT->getParamType(0) != FT->getParamType(1) ||
378 FT->getParamType(0) != B.getInt8PtrTy() ||
379 !FT->getParamType(2)->isIntegerTy())
380 return 0;
382 Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
383 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
384 return ConstantInt::get(CI->getType(), 0);
386 // Get the length argument if it is constant.
387 uint64_t Length;
388 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
389 Length = LengthArg->getZExtValue();
390 else
391 return 0;
393 if (Length == 0) // strncmp(x,y,0) -> 0
394 return ConstantInt::get(CI->getType(), 0);
396 if (TD && Length == 1) // strncmp(x,y,1) -> memcmp(x,y,1)
397 return EmitMemCmp(Str1P, Str2P, CI->getArgOperand(2), B, TD);
399 std::string Str1, Str2;
400 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
401 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
403 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
404 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
406 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
407 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
409 // strncmp(x, y) -> cnst (if both x and y are constant strings)
410 if (HasStr1 && HasStr2)
411 return ConstantInt::get(CI->getType(),
412 strncmp(Str1.c_str(), Str2.c_str(), Length));
413 return 0;
418 //===---------------------------------------===//
419 // 'strcpy' Optimizations
421 struct StrCpyOpt : public LibCallOptimization {
422 bool OptChkCall; // True if it's optimizing a __strcpy_chk libcall.
424 StrCpyOpt(bool c) : OptChkCall(c) {}
426 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
427 // Verify the "strcpy" function prototype.
428 unsigned NumParams = OptChkCall ? 3 : 2;
429 const FunctionType *FT = Callee->getFunctionType();
430 if (FT->getNumParams() != NumParams ||
431 FT->getReturnType() != FT->getParamType(0) ||
432 FT->getParamType(0) != FT->getParamType(1) ||
433 FT->getParamType(0) != B.getInt8PtrTy())
434 return 0;
436 Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
437 if (Dst == Src) // strcpy(x,x) -> x
438 return Src;
440 // These optimizations require TargetData.
441 if (!TD) return 0;
443 // See if we can get the length of the input string.
444 uint64_t Len = GetStringLength(Src);
445 if (Len == 0) return 0;
447 // We have enough information to now generate the memcpy call to do the
448 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
449 if (OptChkCall)
450 EmitMemCpyChk(Dst, Src,
451 ConstantInt::get(TD->getIntPtrType(*Context), Len),
452 CI->getArgOperand(2), B, TD);
453 else
454 B.CreateMemCpy(Dst, Src,
455 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
456 return Dst;
460 //===---------------------------------------===//
461 // 'strncpy' Optimizations
463 struct StrNCpyOpt : public LibCallOptimization {
464 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
465 const FunctionType *FT = Callee->getFunctionType();
466 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
467 FT->getParamType(0) != FT->getParamType(1) ||
468 FT->getParamType(0) != B.getInt8PtrTy() ||
469 !FT->getParamType(2)->isIntegerTy())
470 return 0;
472 Value *Dst = CI->getArgOperand(0);
473 Value *Src = CI->getArgOperand(1);
474 Value *LenOp = CI->getArgOperand(2);
476 // See if we can get the length of the input string.
477 uint64_t SrcLen = GetStringLength(Src);
478 if (SrcLen == 0) return 0;
479 --SrcLen;
481 if (SrcLen == 0) {
482 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
483 B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
484 return Dst;
487 uint64_t Len;
488 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(LenOp))
489 Len = LengthArg->getZExtValue();
490 else
491 return 0;
493 if (Len == 0) return Dst; // strncpy(x, y, 0) -> x
495 // These optimizations require TargetData.
496 if (!TD) return 0;
498 // Let strncpy handle the zero padding
499 if (Len > SrcLen+1) return 0;
501 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
502 B.CreateMemCpy(Dst, Src,
503 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
505 return Dst;
509 //===---------------------------------------===//
510 // 'strlen' Optimizations
512 struct StrLenOpt : public LibCallOptimization {
513 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
514 const FunctionType *FT = Callee->getFunctionType();
515 if (FT->getNumParams() != 1 ||
516 FT->getParamType(0) != B.getInt8PtrTy() ||
517 !FT->getReturnType()->isIntegerTy())
518 return 0;
520 Value *Src = CI->getArgOperand(0);
522 // Constant folding: strlen("xyz") -> 3
523 if (uint64_t Len = GetStringLength(Src))
524 return ConstantInt::get(CI->getType(), Len-1);
526 // strlen(x) != 0 --> *x != 0
527 // strlen(x) == 0 --> *x == 0
528 if (IsOnlyUsedInZeroEqualityComparison(CI))
529 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
530 return 0;
535 //===---------------------------------------===//
536 // 'strpbrk' Optimizations
538 struct StrPBrkOpt : public LibCallOptimization {
539 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
540 const FunctionType *FT = Callee->getFunctionType();
541 if (FT->getNumParams() != 2 ||
542 FT->getParamType(0) != B.getInt8PtrTy() ||
543 FT->getParamType(1) != FT->getParamType(0) ||
544 FT->getReturnType() != FT->getParamType(0))
545 return 0;
547 std::string S1, S2;
548 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
549 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
551 // strpbrk(s, "") -> NULL
552 // strpbrk("", s) -> NULL
553 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
554 return Constant::getNullValue(CI->getType());
556 // Constant folding.
557 if (HasS1 && HasS2) {
558 size_t I = S1.find_first_of(S2);
559 if (I == std::string::npos) // No match.
560 return Constant::getNullValue(CI->getType());
562 return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");
565 // strpbrk(s, "a") -> strchr(s, 'a')
566 if (TD && HasS2 && S2.size() == 1)
567 return EmitStrChr(CI->getArgOperand(0), S2[0], B, TD);
569 return 0;
573 //===---------------------------------------===//
574 // 'strto*' Optimizations. This handles strtol, strtod, strtof, strtoul, etc.
576 struct StrToOpt : public LibCallOptimization {
577 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
578 const FunctionType *FT = Callee->getFunctionType();
579 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
580 !FT->getParamType(0)->isPointerTy() ||
581 !FT->getParamType(1)->isPointerTy())
582 return 0;
584 Value *EndPtr = CI->getArgOperand(1);
585 if (isa<ConstantPointerNull>(EndPtr)) {
586 // With a null EndPtr, this function won't capture the main argument.
587 // It would be readonly too, except that it still may write to errno.
588 CI->addAttribute(1, Attribute::NoCapture);
591 return 0;
595 //===---------------------------------------===//
596 // 'strspn' Optimizations
598 struct StrSpnOpt : public LibCallOptimization {
599 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
600 const FunctionType *FT = Callee->getFunctionType();
601 if (FT->getNumParams() != 2 ||
602 FT->getParamType(0) != B.getInt8PtrTy() ||
603 FT->getParamType(1) != FT->getParamType(0) ||
604 !FT->getReturnType()->isIntegerTy())
605 return 0;
607 std::string S1, S2;
608 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
609 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
611 // strspn(s, "") -> 0
612 // strspn("", s) -> 0
613 if ((HasS1 && S1.empty()) || (HasS2 && S2.empty()))
614 return Constant::getNullValue(CI->getType());
616 // Constant folding.
617 if (HasS1 && HasS2)
618 return ConstantInt::get(CI->getType(), strspn(S1.c_str(), S2.c_str()));
620 return 0;
624 //===---------------------------------------===//
625 // 'strcspn' Optimizations
627 struct StrCSpnOpt : public LibCallOptimization {
628 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
629 const FunctionType *FT = Callee->getFunctionType();
630 if (FT->getNumParams() != 2 ||
631 FT->getParamType(0) != B.getInt8PtrTy() ||
632 FT->getParamType(1) != FT->getParamType(0) ||
633 !FT->getReturnType()->isIntegerTy())
634 return 0;
636 std::string S1, S2;
637 bool HasS1 = GetConstantStringInfo(CI->getArgOperand(0), S1);
638 bool HasS2 = GetConstantStringInfo(CI->getArgOperand(1), S2);
640 // strcspn("", s) -> 0
641 if (HasS1 && S1.empty())
642 return Constant::getNullValue(CI->getType());
644 // Constant folding.
645 if (HasS1 && HasS2)
646 return ConstantInt::get(CI->getType(), strcspn(S1.c_str(), S2.c_str()));
648 // strcspn(s, "") -> strlen(s)
649 if (TD && HasS2 && S2.empty())
650 return EmitStrLen(CI->getArgOperand(0), B, TD);
652 return 0;
656 //===---------------------------------------===//
657 // 'strstr' Optimizations
659 struct StrStrOpt : public LibCallOptimization {
660 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
661 const FunctionType *FT = Callee->getFunctionType();
662 if (FT->getNumParams() != 2 ||
663 !FT->getParamType(0)->isPointerTy() ||
664 !FT->getParamType(1)->isPointerTy() ||
665 !FT->getReturnType()->isPointerTy())
666 return 0;
668 // fold strstr(x, x) -> x.
669 if (CI->getArgOperand(0) == CI->getArgOperand(1))
670 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
672 // fold strstr(a, b) == a -> strncmp(a, b, strlen(b)) == 0
673 if (TD && IsOnlyUsedInEqualityComparison(CI, CI->getArgOperand(0))) {
674 Value *StrLen = EmitStrLen(CI->getArgOperand(1), B, TD);
675 Value *StrNCmp = EmitStrNCmp(CI->getArgOperand(0), CI->getArgOperand(1),
676 StrLen, B, TD);
677 for (Value::use_iterator UI = CI->use_begin(), UE = CI->use_end();
678 UI != UE; ) {
679 ICmpInst *Old = cast<ICmpInst>(*UI++);
680 Value *Cmp = B.CreateICmp(Old->getPredicate(), StrNCmp,
681 ConstantInt::getNullValue(StrNCmp->getType()),
682 "cmp");
683 Old->replaceAllUsesWith(Cmp);
684 Old->eraseFromParent();
686 return CI;
689 // See if either input string is a constant string.
690 std::string SearchStr, ToFindStr;
691 bool HasStr1 = GetConstantStringInfo(CI->getArgOperand(0), SearchStr);
692 bool HasStr2 = GetConstantStringInfo(CI->getArgOperand(1), ToFindStr);
694 // fold strstr(x, "") -> x.
695 if (HasStr2 && ToFindStr.empty())
696 return B.CreateBitCast(CI->getArgOperand(0), CI->getType());
698 // If both strings are known, constant fold it.
699 if (HasStr1 && HasStr2) {
700 std::string::size_type Offset = SearchStr.find(ToFindStr);
702 if (Offset == std::string::npos) // strstr("foo", "bar") -> null
703 return Constant::getNullValue(CI->getType());
705 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
706 Value *Result = CastToCStr(CI->getArgOperand(0), B);
707 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
708 return B.CreateBitCast(Result, CI->getType());
711 // fold strstr(x, "y") -> strchr(x, 'y').
712 if (HasStr2 && ToFindStr.size() == 1)
713 return B.CreateBitCast(EmitStrChr(CI->getArgOperand(0),
714 ToFindStr[0], B, TD), CI->getType());
715 return 0;
720 //===---------------------------------------===//
721 // 'memcmp' Optimizations
723 struct MemCmpOpt : public LibCallOptimization {
724 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
725 const FunctionType *FT = Callee->getFunctionType();
726 if (FT->getNumParams() != 3 || !FT->getParamType(0)->isPointerTy() ||
727 !FT->getParamType(1)->isPointerTy() ||
728 !FT->getReturnType()->isIntegerTy(32))
729 return 0;
731 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
733 if (LHS == RHS) // memcmp(s,s,x) -> 0
734 return Constant::getNullValue(CI->getType());
736 // Make sure we have a constant length.
737 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
738 if (!LenC) return 0;
739 uint64_t Len = LenC->getZExtValue();
741 if (Len == 0) // memcmp(s1,s2,0) -> 0
742 return Constant::getNullValue(CI->getType());
744 // memcmp(S1,S2,1) -> *(unsigned char*)LHS - *(unsigned char*)RHS
745 if (Len == 1) {
746 Value *LHSV = B.CreateZExt(B.CreateLoad(CastToCStr(LHS, B), "lhsc"),
747 CI->getType(), "lhsv");
748 Value *RHSV = B.CreateZExt(B.CreateLoad(CastToCStr(RHS, B), "rhsc"),
749 CI->getType(), "rhsv");
750 return B.CreateSub(LHSV, RHSV, "chardiff");
753 // Constant folding: memcmp(x, y, l) -> cnst (all arguments are constant)
754 std::string LHSStr, RHSStr;
755 if (GetConstantStringInfo(LHS, LHSStr) &&
756 GetConstantStringInfo(RHS, RHSStr)) {
757 // Make sure we're not reading out-of-bounds memory.
758 if (Len > LHSStr.length() || Len > RHSStr.length())
759 return 0;
760 uint64_t Ret = memcmp(LHSStr.data(), RHSStr.data(), Len);
761 return ConstantInt::get(CI->getType(), Ret);
764 return 0;
768 //===---------------------------------------===//
769 // 'memcpy' Optimizations
771 struct MemCpyOpt : public LibCallOptimization {
772 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
773 // These optimizations require TargetData.
774 if (!TD) return 0;
776 const FunctionType *FT = Callee->getFunctionType();
777 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
778 !FT->getParamType(0)->isPointerTy() ||
779 !FT->getParamType(1)->isPointerTy() ||
780 FT->getParamType(2) != TD->getIntPtrType(*Context))
781 return 0;
783 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
784 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
785 CI->getArgOperand(2), 1);
786 return CI->getArgOperand(0);
790 //===---------------------------------------===//
791 // 'memmove' Optimizations
793 struct MemMoveOpt : public LibCallOptimization {
794 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
795 // These optimizations require TargetData.
796 if (!TD) return 0;
798 const FunctionType *FT = Callee->getFunctionType();
799 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
800 !FT->getParamType(0)->isPointerTy() ||
801 !FT->getParamType(1)->isPointerTy() ||
802 FT->getParamType(2) != TD->getIntPtrType(*Context))
803 return 0;
805 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
806 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
807 CI->getArgOperand(2), 1);
808 return CI->getArgOperand(0);
812 //===---------------------------------------===//
813 // 'memset' Optimizations
815 struct MemSetOpt : public LibCallOptimization {
816 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
817 // These optimizations require TargetData.
818 if (!TD) return 0;
820 const FunctionType *FT = Callee->getFunctionType();
821 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
822 !FT->getParamType(0)->isPointerTy() ||
823 !FT->getParamType(1)->isIntegerTy() ||
824 FT->getParamType(2) != TD->getIntPtrType(*Context))
825 return 0;
827 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
828 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
829 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
830 return CI->getArgOperand(0);
834 //===----------------------------------------------------------------------===//
835 // Math Library Optimizations
836 //===----------------------------------------------------------------------===//
838 //===---------------------------------------===//
839 // 'pow*' Optimizations
841 struct PowOpt : public LibCallOptimization {
842 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
843 const FunctionType *FT = Callee->getFunctionType();
844 // Just make sure this has 2 arguments of the same FP type, which match the
845 // result type.
846 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
847 FT->getParamType(0) != FT->getParamType(1) ||
848 !FT->getParamType(0)->isFloatingPointTy())
849 return 0;
851 Value *Op1 = CI->getArgOperand(0), *Op2 = CI->getArgOperand(1);
852 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
853 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
854 return Op1C;
855 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
856 return EmitUnaryFloatFnCall(Op2, "exp2", B, Callee->getAttributes());
859 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
860 if (Op2C == 0) return 0;
862 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
863 return ConstantFP::get(CI->getType(), 1.0);
865 if (Op2C->isExactlyValue(0.5)) {
866 // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
867 // This is faster than calling pow, and still handles negative zero
868 // and negative infinite correctly.
869 // TODO: In fast-math mode, this could be just sqrt(x).
870 // TODO: In finite-only mode, this could be just fabs(sqrt(x)).
871 Value *Inf = ConstantFP::getInfinity(CI->getType());
872 Value *NegInf = ConstantFP::getInfinity(CI->getType(), true);
873 Value *Sqrt = EmitUnaryFloatFnCall(Op1, "sqrt", B,
874 Callee->getAttributes());
875 Value *FAbs = EmitUnaryFloatFnCall(Sqrt, "fabs", B,
876 Callee->getAttributes());
877 Value *FCmp = B.CreateFCmpOEQ(Op1, NegInf, "tmp");
878 Value *Sel = B.CreateSelect(FCmp, Inf, FAbs, "tmp");
879 return Sel;
882 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
883 return Op1;
884 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
885 return B.CreateFMul(Op1, Op1, "pow2");
886 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
887 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0),
888 Op1, "powrecip");
889 return 0;
893 //===---------------------------------------===//
894 // 'exp2' Optimizations
896 struct Exp2Opt : public LibCallOptimization {
897 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
898 const FunctionType *FT = Callee->getFunctionType();
899 // Just make sure this has 1 argument of FP type, which matches the
900 // result type.
901 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
902 !FT->getParamType(0)->isFloatingPointTy())
903 return 0;
905 Value *Op = CI->getArgOperand(0);
906 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
907 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
908 Value *LdExpArg = 0;
909 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
910 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
911 LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty(), "tmp");
912 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
913 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
914 LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty(), "tmp");
917 if (LdExpArg) {
918 const char *Name;
919 if (Op->getType()->isFloatTy())
920 Name = "ldexpf";
921 else if (Op->getType()->isDoubleTy())
922 Name = "ldexp";
923 else
924 Name = "ldexpl";
926 Constant *One = ConstantFP::get(*Context, APFloat(1.0f));
927 if (!Op->getType()->isFloatTy())
928 One = ConstantExpr::getFPExtend(One, Op->getType());
930 Module *M = Caller->getParent();
931 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
932 Op->getType(),
933 B.getInt32Ty(), NULL);
934 CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
935 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
936 CI->setCallingConv(F->getCallingConv());
938 return CI;
940 return 0;
944 //===---------------------------------------===//
945 // Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
947 struct UnaryDoubleFPOpt : public LibCallOptimization {
948 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
949 const FunctionType *FT = Callee->getFunctionType();
950 if (FT->getNumParams() != 1 || !FT->getReturnType()->isDoubleTy() ||
951 !FT->getParamType(0)->isDoubleTy())
952 return 0;
954 // If this is something like 'floor((double)floatval)', convert to floorf.
955 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getArgOperand(0));
956 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
957 return 0;
959 // floor((double)floatval) -> (double)floorf(floatval)
960 Value *V = Cast->getOperand(0);
961 V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
962 Callee->getAttributes());
963 return B.CreateFPExt(V, B.getDoubleTy());
967 //===----------------------------------------------------------------------===//
968 // Integer Optimizations
969 //===----------------------------------------------------------------------===//
971 //===---------------------------------------===//
972 // 'ffs*' Optimizations
974 struct FFSOpt : public LibCallOptimization {
975 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
976 const FunctionType *FT = Callee->getFunctionType();
977 // Just make sure this has 2 arguments of the same FP type, which match the
978 // result type.
979 if (FT->getNumParams() != 1 ||
980 !FT->getReturnType()->isIntegerTy(32) ||
981 !FT->getParamType(0)->isIntegerTy())
982 return 0;
984 Value *Op = CI->getArgOperand(0);
986 // Constant fold.
987 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
988 if (CI->getValue() == 0) // ffs(0) -> 0.
989 return Constant::getNullValue(CI->getType());
990 // ffs(c) -> cttz(c)+1
991 return B.getInt32(CI->getValue().countTrailingZeros() + 1);
994 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
995 const Type *ArgType = Op->getType();
996 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
997 Intrinsic::cttz, &ArgType, 1);
998 Value *V = B.CreateCall(F, Op, "cttz");
999 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
1000 V = B.CreateIntCast(V, B.getInt32Ty(), false, "tmp");
1002 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
1003 return B.CreateSelect(Cond, V, B.getInt32(0));
1007 //===---------------------------------------===//
1008 // 'isdigit' Optimizations
1010 struct IsDigitOpt : public LibCallOptimization {
1011 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1012 const FunctionType *FT = Callee->getFunctionType();
1013 // We require integer(i32)
1014 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1015 !FT->getParamType(0)->isIntegerTy(32))
1016 return 0;
1018 // isdigit(c) -> (c-'0') <u 10
1019 Value *Op = CI->getArgOperand(0);
1020 Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
1021 Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
1022 return B.CreateZExt(Op, CI->getType());
1026 //===---------------------------------------===//
1027 // 'isascii' Optimizations
1029 struct IsAsciiOpt : public LibCallOptimization {
1030 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1031 const FunctionType *FT = Callee->getFunctionType();
1032 // We require integer(i32)
1033 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1034 !FT->getParamType(0)->isIntegerTy(32))
1035 return 0;
1037 // isascii(c) -> c <u 128
1038 Value *Op = CI->getArgOperand(0);
1039 Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
1040 return B.CreateZExt(Op, CI->getType());
1044 //===---------------------------------------===//
1045 // 'abs', 'labs', 'llabs' Optimizations
1047 struct AbsOpt : public LibCallOptimization {
1048 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1049 const FunctionType *FT = Callee->getFunctionType();
1050 // We require integer(integer) where the types agree.
1051 if (FT->getNumParams() != 1 || !FT->getReturnType()->isIntegerTy() ||
1052 FT->getParamType(0) != FT->getReturnType())
1053 return 0;
1055 // abs(x) -> x >s -1 ? x : -x
1056 Value *Op = CI->getArgOperand(0);
1057 Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),
1058 "ispos");
1059 Value *Neg = B.CreateNeg(Op, "neg");
1060 return B.CreateSelect(Pos, Op, Neg);
1065 //===---------------------------------------===//
1066 // 'toascii' Optimizations
1068 struct ToAsciiOpt : public LibCallOptimization {
1069 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1070 const FunctionType *FT = Callee->getFunctionType();
1071 // We require i32(i32)
1072 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1073 !FT->getParamType(0)->isIntegerTy(32))
1074 return 0;
1076 // isascii(c) -> c & 0x7f
1077 return B.CreateAnd(CI->getArgOperand(0),
1078 ConstantInt::get(CI->getType(),0x7F));
1082 //===----------------------------------------------------------------------===//
1083 // Formatting and IO Optimizations
1084 //===----------------------------------------------------------------------===//
1086 //===---------------------------------------===//
1087 // 'printf' Optimizations
1089 struct PrintFOpt : public LibCallOptimization {
1090 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
1091 IRBuilder<> &B) {
1092 // Check for a fixed format string.
1093 std::string FormatStr;
1094 if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
1095 return 0;
1097 // Empty format string -> noop.
1098 if (FormatStr.empty()) // Tolerate printf's declared void.
1099 return CI->use_empty() ? (Value*)CI :
1100 ConstantInt::get(CI->getType(), 0);
1102 // Do not do any of the following transformations if the printf return value
1103 // is used, in general the printf return value is not compatible with either
1104 // putchar() or puts().
1105 if (!CI->use_empty())
1106 return 0;
1108 // printf("x") -> putchar('x'), even for '%'.
1109 if (FormatStr.size() == 1) {
1110 Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD);
1111 if (CI->use_empty()) return CI;
1112 return B.CreateIntCast(Res, CI->getType(), true);
1115 // printf("foo\n") --> puts("foo")
1116 if (FormatStr[FormatStr.size()-1] == '\n' &&
1117 FormatStr.find('%') == std::string::npos) { // no format characters.
1118 // Create a string literal with no \n on it. We expect the constant merge
1119 // pass to be run after this pass, to merge duplicate strings.
1120 FormatStr.erase(FormatStr.end()-1);
1121 Constant *C = ConstantArray::get(*Context, FormatStr, true);
1122 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1123 GlobalVariable::InternalLinkage, C, "str");
1124 EmitPutS(C, B, TD);
1125 return CI->use_empty() ? (Value*)CI :
1126 ConstantInt::get(CI->getType(), FormatStr.size()+1);
1129 // Optimize specific format strings.
1130 // printf("%c", chr) --> putchar(chr)
1131 if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
1132 CI->getArgOperand(1)->getType()->isIntegerTy()) {
1133 Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD);
1135 if (CI->use_empty()) return CI;
1136 return B.CreateIntCast(Res, CI->getType(), true);
1139 // printf("%s\n", str) --> puts(str)
1140 if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
1141 CI->getArgOperand(1)->getType()->isPointerTy()) {
1142 EmitPutS(CI->getArgOperand(1), B, TD);
1143 return CI;
1145 return 0;
1148 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1149 // Require one fixed pointer argument and an integer/void result.
1150 const FunctionType *FT = Callee->getFunctionType();
1151 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1152 !(FT->getReturnType()->isIntegerTy() ||
1153 FT->getReturnType()->isVoidTy()))
1154 return 0;
1156 if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
1157 return V;
1160 // printf(format, ...) -> iprintf(format, ...) if no floating point
1161 // arguments.
1162 if (TLI->has(LibFunc::iprintf) && !CallHasFloatingPointArgument(CI)) {
1163 Module *M = B.GetInsertBlock()->getParent()->getParent();
1164 Constant *IPrintFFn =
1165 M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
1166 CallInst *New = cast<CallInst>(CI->clone());
1167 New->setCalledFunction(IPrintFFn);
1168 B.Insert(New);
1169 return New;
1171 return 0;
1175 //===---------------------------------------===//
1176 // 'sprintf' Optimizations
1178 struct SPrintFOpt : public LibCallOptimization {
1179 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
1180 IRBuilder<> &B) {
1181 // Check for a fixed format string.
1182 std::string FormatStr;
1183 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
1184 return 0;
1186 // If we just have a format string (nothing else crazy) transform it.
1187 if (CI->getNumArgOperands() == 2) {
1188 // Make sure there's no % in the constant array. We could try to handle
1189 // %% -> % in the future if we cared.
1190 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1191 if (FormatStr[i] == '%')
1192 return 0; // we found a format specifier, bail out.
1194 // These optimizations require TargetData.
1195 if (!TD) return 0;
1197 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1198 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
1199 ConstantInt::get(TD->getIntPtrType(*Context), // Copy the
1200 FormatStr.size() + 1), 1); // nul byte.
1201 return ConstantInt::get(CI->getType(), FormatStr.size());
1204 // The remaining optimizations require the format string to be "%s" or "%c"
1205 // and have an extra operand.
1206 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1207 CI->getNumArgOperands() < 3)
1208 return 0;
1210 // Decode the second character of the format string.
1211 if (FormatStr[1] == 'c') {
1212 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1213 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1214 Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
1215 Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
1216 B.CreateStore(V, Ptr);
1217 Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
1218 B.CreateStore(B.getInt8(0), Ptr);
1220 return ConstantInt::get(CI->getType(), 1);
1223 if (FormatStr[1] == 's') {
1224 // These optimizations require TargetData.
1225 if (!TD) return 0;
1227 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1228 if (!CI->getArgOperand(2)->getType()->isPointerTy()) return 0;
1230 Value *Len = EmitStrLen(CI->getArgOperand(2), B, TD);
1231 Value *IncLen = B.CreateAdd(Len,
1232 ConstantInt::get(Len->getType(), 1),
1233 "leninc");
1234 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
1236 // The sprintf result is the unincremented number of bytes in the string.
1237 return B.CreateIntCast(Len, CI->getType(), false);
1239 return 0;
1242 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1243 // Require two fixed pointer arguments and an integer result.
1244 const FunctionType *FT = Callee->getFunctionType();
1245 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1246 !FT->getParamType(1)->isPointerTy() ||
1247 !FT->getReturnType()->isIntegerTy())
1248 return 0;
1250 if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
1251 return V;
1254 // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
1255 // point arguments.
1256 if (TLI->has(LibFunc::siprintf) && !CallHasFloatingPointArgument(CI)) {
1257 Module *M = B.GetInsertBlock()->getParent()->getParent();
1258 Constant *SIPrintFFn =
1259 M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
1260 CallInst *New = cast<CallInst>(CI->clone());
1261 New->setCalledFunction(SIPrintFFn);
1262 B.Insert(New);
1263 return New;
1265 return 0;
1269 //===---------------------------------------===//
1270 // 'fwrite' Optimizations
1272 struct FWriteOpt : public LibCallOptimization {
1273 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1274 // Require a pointer, an integer, an integer, a pointer, returning integer.
1275 const FunctionType *FT = Callee->getFunctionType();
1276 if (FT->getNumParams() != 4 || !FT->getParamType(0)->isPointerTy() ||
1277 !FT->getParamType(1)->isIntegerTy() ||
1278 !FT->getParamType(2)->isIntegerTy() ||
1279 !FT->getParamType(3)->isPointerTy() ||
1280 !FT->getReturnType()->isIntegerTy())
1281 return 0;
1283 // Get the element size and count.
1284 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getArgOperand(1));
1285 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getArgOperand(2));
1286 if (!SizeC || !CountC) return 0;
1287 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
1289 // If this is writing zero records, remove the call (it's a noop).
1290 if (Bytes == 0)
1291 return ConstantInt::get(CI->getType(), 0);
1293 // If this is writing one byte, turn it into fputc.
1294 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1295 Value *Char = B.CreateLoad(CastToCStr(CI->getArgOperand(0), B), "char");
1296 EmitFPutC(Char, CI->getArgOperand(3), B, TD);
1297 return ConstantInt::get(CI->getType(), 1);
1300 return 0;
1304 //===---------------------------------------===//
1305 // 'fputs' Optimizations
1307 struct FPutsOpt : public LibCallOptimization {
1308 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1309 // These optimizations require TargetData.
1310 if (!TD) return 0;
1312 // Require two pointers. Also, we can't optimize if return value is used.
1313 const FunctionType *FT = Callee->getFunctionType();
1314 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1315 !FT->getParamType(1)->isPointerTy() ||
1316 !CI->use_empty())
1317 return 0;
1319 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1320 uint64_t Len = GetStringLength(CI->getArgOperand(0));
1321 if (!Len) return 0;
1322 EmitFWrite(CI->getArgOperand(0),
1323 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
1324 CI->getArgOperand(1), B, TD);
1325 return CI; // Known to have no uses (see above).
1329 //===---------------------------------------===//
1330 // 'fprintf' Optimizations
1332 struct FPrintFOpt : public LibCallOptimization {
1333 Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
1334 IRBuilder<> &B) {
1335 // All the optimizations depend on the format string.
1336 std::string FormatStr;
1337 if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
1338 return 0;
1340 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1341 if (CI->getNumArgOperands() == 2) {
1342 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1343 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
1344 return 0; // We found a format specifier.
1346 // These optimizations require TargetData.
1347 if (!TD) return 0;
1349 EmitFWrite(CI->getArgOperand(1),
1350 ConstantInt::get(TD->getIntPtrType(*Context),
1351 FormatStr.size()),
1352 CI->getArgOperand(0), B, TD);
1353 return ConstantInt::get(CI->getType(), FormatStr.size());
1356 // The remaining optimizations require the format string to be "%s" or "%c"
1357 // and have an extra operand.
1358 if (FormatStr.size() != 2 || FormatStr[0] != '%' ||
1359 CI->getNumArgOperands() < 3)
1360 return 0;
1362 // Decode the second character of the format string.
1363 if (FormatStr[1] == 'c') {
1364 // fprintf(F, "%c", chr) --> fputc(chr, F)
1365 if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
1366 EmitFPutC(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
1367 return ConstantInt::get(CI->getType(), 1);
1370 if (FormatStr[1] == 's') {
1371 // fprintf(F, "%s", str) --> fputs(str, F)
1372 if (!CI->getArgOperand(2)->getType()->isPointerTy() || !CI->use_empty())
1373 return 0;
1374 EmitFPutS(CI->getArgOperand(2), CI->getArgOperand(0), B, TD);
1375 return CI;
1377 return 0;
1380 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1381 // Require two fixed paramters as pointers and integer result.
1382 const FunctionType *FT = Callee->getFunctionType();
1383 if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
1384 !FT->getParamType(1)->isPointerTy() ||
1385 !FT->getReturnType()->isIntegerTy())
1386 return 0;
1388 if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
1389 return V;
1392 // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
1393 // floating point arguments.
1394 if (TLI->has(LibFunc::fiprintf) && !CallHasFloatingPointArgument(CI)) {
1395 Module *M = B.GetInsertBlock()->getParent()->getParent();
1396 Constant *FIPrintFFn =
1397 M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
1398 CallInst *New = cast<CallInst>(CI->clone());
1399 New->setCalledFunction(FIPrintFFn);
1400 B.Insert(New);
1401 return New;
1403 return 0;
1407 //===---------------------------------------===//
1408 // 'puts' Optimizations
1410 struct PutsOpt : public LibCallOptimization {
1411 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1412 // Require one fixed pointer argument and an integer/void result.
1413 const FunctionType *FT = Callee->getFunctionType();
1414 if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
1415 !(FT->getReturnType()->isIntegerTy() ||
1416 FT->getReturnType()->isVoidTy()))
1417 return 0;
1419 // Check for a constant string.
1420 std::string Str;
1421 if (!GetConstantStringInfo(CI->getArgOperand(0), Str))
1422 return 0;
1424 if (Str.empty() && CI->use_empty()) {
1425 // puts("") -> putchar('\n')
1426 Value *Res = EmitPutChar(B.getInt32('\n'), B, TD);
1427 if (CI->use_empty()) return CI;
1428 return B.CreateIntCast(Res, CI->getType(), true);
1431 return 0;
1435 } // end anonymous namespace.
1437 //===----------------------------------------------------------------------===//
1438 // SimplifyLibCalls Pass Implementation
1439 //===----------------------------------------------------------------------===//
1441 namespace {
1442 /// This pass optimizes well known library functions from libc and libm.
1444 class SimplifyLibCalls : public FunctionPass {
1445 TargetLibraryInfo *TLI;
1447 StringMap<LibCallOptimization*> Optimizations;
1448 // String and Memory LibCall Optimizations
1449 StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrRChrOpt StrRChr;
1450 StrCmpOpt StrCmp; StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
1451 StrNCpyOpt StrNCpy; StrLenOpt StrLen; StrPBrkOpt StrPBrk;
1452 StrToOpt StrTo; StrSpnOpt StrSpn; StrCSpnOpt StrCSpn; StrStrOpt StrStr;
1453 MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
1454 // Math Library Optimizations
1455 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
1456 // Integer Optimizations
1457 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1458 ToAsciiOpt ToAscii;
1459 // Formatting and IO Optimizations
1460 SPrintFOpt SPrintF; PrintFOpt PrintF;
1461 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
1462 PutsOpt Puts;
1464 bool Modified; // This is only used by doInitialization.
1465 public:
1466 static char ID; // Pass identification
1467 SimplifyLibCalls() : FunctionPass(ID), StrCpy(false), StrCpyChk(true) {
1468 initializeSimplifyLibCallsPass(*PassRegistry::getPassRegistry());
1470 void InitOptimizations();
1471 bool runOnFunction(Function &F);
1473 void setDoesNotAccessMemory(Function &F);
1474 void setOnlyReadsMemory(Function &F);
1475 void setDoesNotThrow(Function &F);
1476 void setDoesNotCapture(Function &F, unsigned n);
1477 void setDoesNotAlias(Function &F, unsigned n);
1478 bool doInitialization(Module &M);
1480 void inferPrototypeAttributes(Function &F);
1481 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1482 AU.addRequired<TargetLibraryInfo>();
1485 } // end anonymous namespace.
1487 char SimplifyLibCalls::ID = 0;
1489 INITIALIZE_PASS_BEGIN(SimplifyLibCalls, "simplify-libcalls",
1490 "Simplify well-known library calls", false, false)
1491 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
1492 INITIALIZE_PASS_END(SimplifyLibCalls, "simplify-libcalls",
1493 "Simplify well-known library calls", false, false)
1495 // Public interface to the Simplify LibCalls pass.
1496 FunctionPass *llvm::createSimplifyLibCallsPass() {
1497 return new SimplifyLibCalls();
1500 /// Optimizations - Populate the Optimizations map with all the optimizations
1501 /// we know.
1502 void SimplifyLibCalls::InitOptimizations() {
1503 // String and Memory LibCall Optimizations
1504 Optimizations["strcat"] = &StrCat;
1505 Optimizations["strncat"] = &StrNCat;
1506 Optimizations["strchr"] = &StrChr;
1507 Optimizations["strrchr"] = &StrRChr;
1508 Optimizations["strcmp"] = &StrCmp;
1509 Optimizations["strncmp"] = &StrNCmp;
1510 Optimizations["strcpy"] = &StrCpy;
1511 Optimizations["strncpy"] = &StrNCpy;
1512 Optimizations["strlen"] = &StrLen;
1513 Optimizations["strpbrk"] = &StrPBrk;
1514 Optimizations["strtol"] = &StrTo;
1515 Optimizations["strtod"] = &StrTo;
1516 Optimizations["strtof"] = &StrTo;
1517 Optimizations["strtoul"] = &StrTo;
1518 Optimizations["strtoll"] = &StrTo;
1519 Optimizations["strtold"] = &StrTo;
1520 Optimizations["strtoull"] = &StrTo;
1521 Optimizations["strspn"] = &StrSpn;
1522 Optimizations["strcspn"] = &StrCSpn;
1523 Optimizations["strstr"] = &StrStr;
1524 Optimizations["memcmp"] = &MemCmp;
1525 if (TLI->has(LibFunc::memcpy)) Optimizations["memcpy"] = &MemCpy;
1526 Optimizations["memmove"] = &MemMove;
1527 if (TLI->has(LibFunc::memset)) Optimizations["memset"] = &MemSet;
1529 // _chk variants of String and Memory LibCall Optimizations.
1530 Optimizations["__strcpy_chk"] = &StrCpyChk;
1532 // Math Library Optimizations
1533 Optimizations["powf"] = &Pow;
1534 Optimizations["pow"] = &Pow;
1535 Optimizations["powl"] = &Pow;
1536 Optimizations["llvm.pow.f32"] = &Pow;
1537 Optimizations["llvm.pow.f64"] = &Pow;
1538 Optimizations["llvm.pow.f80"] = &Pow;
1539 Optimizations["llvm.pow.f128"] = &Pow;
1540 Optimizations["llvm.pow.ppcf128"] = &Pow;
1541 Optimizations["exp2l"] = &Exp2;
1542 Optimizations["exp2"] = &Exp2;
1543 Optimizations["exp2f"] = &Exp2;
1544 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1545 Optimizations["llvm.exp2.f128"] = &Exp2;
1546 Optimizations["llvm.exp2.f80"] = &Exp2;
1547 Optimizations["llvm.exp2.f64"] = &Exp2;
1548 Optimizations["llvm.exp2.f32"] = &Exp2;
1550 #ifdef HAVE_FLOORF
1551 Optimizations["floor"] = &UnaryDoubleFP;
1552 #endif
1553 #ifdef HAVE_CEILF
1554 Optimizations["ceil"] = &UnaryDoubleFP;
1555 #endif
1556 #ifdef HAVE_ROUNDF
1557 Optimizations["round"] = &UnaryDoubleFP;
1558 #endif
1559 #ifdef HAVE_RINTF
1560 Optimizations["rint"] = &UnaryDoubleFP;
1561 #endif
1562 #ifdef HAVE_NEARBYINTF
1563 Optimizations["nearbyint"] = &UnaryDoubleFP;
1564 #endif
1566 // Integer Optimizations
1567 Optimizations["ffs"] = &FFS;
1568 Optimizations["ffsl"] = &FFS;
1569 Optimizations["ffsll"] = &FFS;
1570 Optimizations["abs"] = &Abs;
1571 Optimizations["labs"] = &Abs;
1572 Optimizations["llabs"] = &Abs;
1573 Optimizations["isdigit"] = &IsDigit;
1574 Optimizations["isascii"] = &IsAscii;
1575 Optimizations["toascii"] = &ToAscii;
1577 // Formatting and IO Optimizations
1578 Optimizations["sprintf"] = &SPrintF;
1579 Optimizations["printf"] = &PrintF;
1580 Optimizations["fwrite"] = &FWrite;
1581 Optimizations["fputs"] = &FPuts;
1582 Optimizations["fprintf"] = &FPrintF;
1583 Optimizations["puts"] = &Puts;
1587 /// runOnFunction - Top level algorithm.
1589 bool SimplifyLibCalls::runOnFunction(Function &F) {
1590 TLI = &getAnalysis<TargetLibraryInfo>();
1592 if (Optimizations.empty())
1593 InitOptimizations();
1595 const TargetData *TD = getAnalysisIfAvailable<TargetData>();
1597 IRBuilder<> Builder(F.getContext());
1599 bool Changed = false;
1600 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1601 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1602 // Ignore non-calls.
1603 CallInst *CI = dyn_cast<CallInst>(I++);
1604 if (!CI) continue;
1606 // Ignore indirect calls and calls to non-external functions.
1607 Function *Callee = CI->getCalledFunction();
1608 if (Callee == 0 || !Callee->isDeclaration() ||
1609 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1610 continue;
1612 // Ignore unknown calls.
1613 LibCallOptimization *LCO = Optimizations.lookup(Callee->getName());
1614 if (!LCO) continue;
1616 // Set the builder to the instruction after the call.
1617 Builder.SetInsertPoint(BB, I);
1619 // Use debug location of CI for all new instructions.
1620 Builder.SetCurrentDebugLocation(CI->getDebugLoc());
1622 // Try to optimize this call.
1623 Value *Result = LCO->OptimizeCall(CI, TD, TLI, Builder);
1624 if (Result == 0) continue;
1626 DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
1627 dbgs() << " into: " << *Result << "\n");
1629 // Something changed!
1630 Changed = true;
1631 ++NumSimplified;
1633 // Inspect the instruction after the call (which was potentially just
1634 // added) next.
1635 I = CI; ++I;
1637 if (CI != Result && !CI->use_empty()) {
1638 CI->replaceAllUsesWith(Result);
1639 if (!Result->hasName())
1640 Result->takeName(CI);
1642 CI->eraseFromParent();
1645 return Changed;
1648 // Utility methods for doInitialization.
1650 void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1651 if (!F.doesNotAccessMemory()) {
1652 F.setDoesNotAccessMemory();
1653 ++NumAnnotated;
1654 Modified = true;
1657 void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1658 if (!F.onlyReadsMemory()) {
1659 F.setOnlyReadsMemory();
1660 ++NumAnnotated;
1661 Modified = true;
1664 void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1665 if (!F.doesNotThrow()) {
1666 F.setDoesNotThrow();
1667 ++NumAnnotated;
1668 Modified = true;
1671 void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1672 if (!F.doesNotCapture(n)) {
1673 F.setDoesNotCapture(n);
1674 ++NumAnnotated;
1675 Modified = true;
1678 void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1679 if (!F.doesNotAlias(n)) {
1680 F.setDoesNotAlias(n);
1681 ++NumAnnotated;
1682 Modified = true;
1687 void SimplifyLibCalls::inferPrototypeAttributes(Function &F) {
1688 const FunctionType *FTy = F.getFunctionType();
1690 StringRef Name = F.getName();
1691 switch (Name[0]) {
1692 case 's':
1693 if (Name == "strlen") {
1694 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1695 return;
1696 setOnlyReadsMemory(F);
1697 setDoesNotThrow(F);
1698 setDoesNotCapture(F, 1);
1699 } else if (Name == "strchr" ||
1700 Name == "strrchr") {
1701 if (FTy->getNumParams() != 2 ||
1702 !FTy->getParamType(0)->isPointerTy() ||
1703 !FTy->getParamType(1)->isIntegerTy())
1704 return;
1705 setOnlyReadsMemory(F);
1706 setDoesNotThrow(F);
1707 } else if (Name == "strcpy" ||
1708 Name == "stpcpy" ||
1709 Name == "strcat" ||
1710 Name == "strtol" ||
1711 Name == "strtod" ||
1712 Name == "strtof" ||
1713 Name == "strtoul" ||
1714 Name == "strtoll" ||
1715 Name == "strtold" ||
1716 Name == "strncat" ||
1717 Name == "strncpy" ||
1718 Name == "strtoull") {
1719 if (FTy->getNumParams() < 2 ||
1720 !FTy->getParamType(1)->isPointerTy())
1721 return;
1722 setDoesNotThrow(F);
1723 setDoesNotCapture(F, 2);
1724 } else if (Name == "strxfrm") {
1725 if (FTy->getNumParams() != 3 ||
1726 !FTy->getParamType(0)->isPointerTy() ||
1727 !FTy->getParamType(1)->isPointerTy())
1728 return;
1729 setDoesNotThrow(F);
1730 setDoesNotCapture(F, 1);
1731 setDoesNotCapture(F, 2);
1732 } else if (Name == "strcmp" ||
1733 Name == "strspn" ||
1734 Name == "strncmp" ||
1735 Name == "strcspn" ||
1736 Name == "strcoll" ||
1737 Name == "strcasecmp" ||
1738 Name == "strncasecmp") {
1739 if (FTy->getNumParams() < 2 ||
1740 !FTy->getParamType(0)->isPointerTy() ||
1741 !FTy->getParamType(1)->isPointerTy())
1742 return;
1743 setOnlyReadsMemory(F);
1744 setDoesNotThrow(F);
1745 setDoesNotCapture(F, 1);
1746 setDoesNotCapture(F, 2);
1747 } else if (Name == "strstr" ||
1748 Name == "strpbrk") {
1749 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1750 return;
1751 setOnlyReadsMemory(F);
1752 setDoesNotThrow(F);
1753 setDoesNotCapture(F, 2);
1754 } else if (Name == "strtok" ||
1755 Name == "strtok_r") {
1756 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1757 return;
1758 setDoesNotThrow(F);
1759 setDoesNotCapture(F, 2);
1760 } else if (Name == "scanf" ||
1761 Name == "setbuf" ||
1762 Name == "setvbuf") {
1763 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1764 return;
1765 setDoesNotThrow(F);
1766 setDoesNotCapture(F, 1);
1767 } else if (Name == "strdup" ||
1768 Name == "strndup") {
1769 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1770 !FTy->getParamType(0)->isPointerTy())
1771 return;
1772 setDoesNotThrow(F);
1773 setDoesNotAlias(F, 0);
1774 setDoesNotCapture(F, 1);
1775 } else if (Name == "stat" ||
1776 Name == "sscanf" ||
1777 Name == "sprintf" ||
1778 Name == "statvfs") {
1779 if (FTy->getNumParams() < 2 ||
1780 !FTy->getParamType(0)->isPointerTy() ||
1781 !FTy->getParamType(1)->isPointerTy())
1782 return;
1783 setDoesNotThrow(F);
1784 setDoesNotCapture(F, 1);
1785 setDoesNotCapture(F, 2);
1786 } else if (Name == "snprintf") {
1787 if (FTy->getNumParams() != 3 ||
1788 !FTy->getParamType(0)->isPointerTy() ||
1789 !FTy->getParamType(2)->isPointerTy())
1790 return;
1791 setDoesNotThrow(F);
1792 setDoesNotCapture(F, 1);
1793 setDoesNotCapture(F, 3);
1794 } else if (Name == "setitimer") {
1795 if (FTy->getNumParams() != 3 ||
1796 !FTy->getParamType(1)->isPointerTy() ||
1797 !FTy->getParamType(2)->isPointerTy())
1798 return;
1799 setDoesNotThrow(F);
1800 setDoesNotCapture(F, 2);
1801 setDoesNotCapture(F, 3);
1802 } else if (Name == "system") {
1803 if (FTy->getNumParams() != 1 ||
1804 !FTy->getParamType(0)->isPointerTy())
1805 return;
1806 // May throw; "system" is a valid pthread cancellation point.
1807 setDoesNotCapture(F, 1);
1809 break;
1810 case 'm':
1811 if (Name == "malloc") {
1812 if (FTy->getNumParams() != 1 ||
1813 !FTy->getReturnType()->isPointerTy())
1814 return;
1815 setDoesNotThrow(F);
1816 setDoesNotAlias(F, 0);
1817 } else if (Name == "memcmp") {
1818 if (FTy->getNumParams() != 3 ||
1819 !FTy->getParamType(0)->isPointerTy() ||
1820 !FTy->getParamType(1)->isPointerTy())
1821 return;
1822 setOnlyReadsMemory(F);
1823 setDoesNotThrow(F);
1824 setDoesNotCapture(F, 1);
1825 setDoesNotCapture(F, 2);
1826 } else if (Name == "memchr" ||
1827 Name == "memrchr") {
1828 if (FTy->getNumParams() != 3)
1829 return;
1830 setOnlyReadsMemory(F);
1831 setDoesNotThrow(F);
1832 } else if (Name == "modf" ||
1833 Name == "modff" ||
1834 Name == "modfl" ||
1835 Name == "memcpy" ||
1836 Name == "memccpy" ||
1837 Name == "memmove") {
1838 if (FTy->getNumParams() < 2 ||
1839 !FTy->getParamType(1)->isPointerTy())
1840 return;
1841 setDoesNotThrow(F);
1842 setDoesNotCapture(F, 2);
1843 } else if (Name == "memalign") {
1844 if (!FTy->getReturnType()->isPointerTy())
1845 return;
1846 setDoesNotAlias(F, 0);
1847 } else if (Name == "mkdir" ||
1848 Name == "mktime") {
1849 if (FTy->getNumParams() == 0 ||
1850 !FTy->getParamType(0)->isPointerTy())
1851 return;
1852 setDoesNotThrow(F);
1853 setDoesNotCapture(F, 1);
1855 break;
1856 case 'r':
1857 if (Name == "realloc") {
1858 if (FTy->getNumParams() != 2 ||
1859 !FTy->getParamType(0)->isPointerTy() ||
1860 !FTy->getReturnType()->isPointerTy())
1861 return;
1862 setDoesNotThrow(F);
1863 setDoesNotAlias(F, 0);
1864 setDoesNotCapture(F, 1);
1865 } else if (Name == "read") {
1866 if (FTy->getNumParams() != 3 ||
1867 !FTy->getParamType(1)->isPointerTy())
1868 return;
1869 // May throw; "read" is a valid pthread cancellation point.
1870 setDoesNotCapture(F, 2);
1871 } else if (Name == "rmdir" ||
1872 Name == "rewind" ||
1873 Name == "remove" ||
1874 Name == "realpath") {
1875 if (FTy->getNumParams() < 1 ||
1876 !FTy->getParamType(0)->isPointerTy())
1877 return;
1878 setDoesNotThrow(F);
1879 setDoesNotCapture(F, 1);
1880 } else if (Name == "rename" ||
1881 Name == "readlink") {
1882 if (FTy->getNumParams() < 2 ||
1883 !FTy->getParamType(0)->isPointerTy() ||
1884 !FTy->getParamType(1)->isPointerTy())
1885 return;
1886 setDoesNotThrow(F);
1887 setDoesNotCapture(F, 1);
1888 setDoesNotCapture(F, 2);
1890 break;
1891 case 'w':
1892 if (Name == "write") {
1893 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1894 return;
1895 // May throw; "write" is a valid pthread cancellation point.
1896 setDoesNotCapture(F, 2);
1898 break;
1899 case 'b':
1900 if (Name == "bcopy") {
1901 if (FTy->getNumParams() != 3 ||
1902 !FTy->getParamType(0)->isPointerTy() ||
1903 !FTy->getParamType(1)->isPointerTy())
1904 return;
1905 setDoesNotThrow(F);
1906 setDoesNotCapture(F, 1);
1907 setDoesNotCapture(F, 2);
1908 } else if (Name == "bcmp") {
1909 if (FTy->getNumParams() != 3 ||
1910 !FTy->getParamType(0)->isPointerTy() ||
1911 !FTy->getParamType(1)->isPointerTy())
1912 return;
1913 setDoesNotThrow(F);
1914 setOnlyReadsMemory(F);
1915 setDoesNotCapture(F, 1);
1916 setDoesNotCapture(F, 2);
1917 } else if (Name == "bzero") {
1918 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1919 return;
1920 setDoesNotThrow(F);
1921 setDoesNotCapture(F, 1);
1923 break;
1924 case 'c':
1925 if (Name == "calloc") {
1926 if (FTy->getNumParams() != 2 ||
1927 !FTy->getReturnType()->isPointerTy())
1928 return;
1929 setDoesNotThrow(F);
1930 setDoesNotAlias(F, 0);
1931 } else if (Name == "chmod" ||
1932 Name == "chown" ||
1933 Name == "ctermid" ||
1934 Name == "clearerr" ||
1935 Name == "closedir") {
1936 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1937 return;
1938 setDoesNotThrow(F);
1939 setDoesNotCapture(F, 1);
1941 break;
1942 case 'a':
1943 if (Name == "atoi" ||
1944 Name == "atol" ||
1945 Name == "atof" ||
1946 Name == "atoll") {
1947 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1948 return;
1949 setDoesNotThrow(F);
1950 setOnlyReadsMemory(F);
1951 setDoesNotCapture(F, 1);
1952 } else if (Name == "access") {
1953 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1954 return;
1955 setDoesNotThrow(F);
1956 setDoesNotCapture(F, 1);
1958 break;
1959 case 'f':
1960 if (Name == "fopen") {
1961 if (FTy->getNumParams() != 2 ||
1962 !FTy->getReturnType()->isPointerTy() ||
1963 !FTy->getParamType(0)->isPointerTy() ||
1964 !FTy->getParamType(1)->isPointerTy())
1965 return;
1966 setDoesNotThrow(F);
1967 setDoesNotAlias(F, 0);
1968 setDoesNotCapture(F, 1);
1969 setDoesNotCapture(F, 2);
1970 } else if (Name == "fdopen") {
1971 if (FTy->getNumParams() != 2 ||
1972 !FTy->getReturnType()->isPointerTy() ||
1973 !FTy->getParamType(1)->isPointerTy())
1974 return;
1975 setDoesNotThrow(F);
1976 setDoesNotAlias(F, 0);
1977 setDoesNotCapture(F, 2);
1978 } else if (Name == "feof" ||
1979 Name == "free" ||
1980 Name == "fseek" ||
1981 Name == "ftell" ||
1982 Name == "fgetc" ||
1983 Name == "fseeko" ||
1984 Name == "ftello" ||
1985 Name == "fileno" ||
1986 Name == "fflush" ||
1987 Name == "fclose" ||
1988 Name == "fsetpos" ||
1989 Name == "flockfile" ||
1990 Name == "funlockfile" ||
1991 Name == "ftrylockfile") {
1992 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1993 return;
1994 setDoesNotThrow(F);
1995 setDoesNotCapture(F, 1);
1996 } else if (Name == "ferror") {
1997 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1998 return;
1999 setDoesNotThrow(F);
2000 setDoesNotCapture(F, 1);
2001 setOnlyReadsMemory(F);
2002 } else if (Name == "fputc" ||
2003 Name == "fstat" ||
2004 Name == "frexp" ||
2005 Name == "frexpf" ||
2006 Name == "frexpl" ||
2007 Name == "fstatvfs") {
2008 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
2009 return;
2010 setDoesNotThrow(F);
2011 setDoesNotCapture(F, 2);
2012 } else if (Name == "fgets") {
2013 if (FTy->getNumParams() != 3 ||
2014 !FTy->getParamType(0)->isPointerTy() ||
2015 !FTy->getParamType(2)->isPointerTy())
2016 return;
2017 setDoesNotThrow(F);
2018 setDoesNotCapture(F, 3);
2019 } else if (Name == "fread" ||
2020 Name == "fwrite") {
2021 if (FTy->getNumParams() != 4 ||
2022 !FTy->getParamType(0)->isPointerTy() ||
2023 !FTy->getParamType(3)->isPointerTy())
2024 return;
2025 setDoesNotThrow(F);
2026 setDoesNotCapture(F, 1);
2027 setDoesNotCapture(F, 4);
2028 } else if (Name == "fputs" ||
2029 Name == "fscanf" ||
2030 Name == "fprintf" ||
2031 Name == "fgetpos") {
2032 if (FTy->getNumParams() < 2 ||
2033 !FTy->getParamType(0)->isPointerTy() ||
2034 !FTy->getParamType(1)->isPointerTy())
2035 return;
2036 setDoesNotThrow(F);
2037 setDoesNotCapture(F, 1);
2038 setDoesNotCapture(F, 2);
2040 break;
2041 case 'g':
2042 if (Name == "getc" ||
2043 Name == "getlogin_r" ||
2044 Name == "getc_unlocked") {
2045 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
2046 return;
2047 setDoesNotThrow(F);
2048 setDoesNotCapture(F, 1);
2049 } else if (Name == "getenv") {
2050 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
2051 return;
2052 setDoesNotThrow(F);
2053 setOnlyReadsMemory(F);
2054 setDoesNotCapture(F, 1);
2055 } else if (Name == "gets" ||
2056 Name == "getchar") {
2057 setDoesNotThrow(F);
2058 } else if (Name == "getitimer") {
2059 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
2060 return;
2061 setDoesNotThrow(F);
2062 setDoesNotCapture(F, 2);
2063 } else if (Name == "getpwnam") {
2064 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
2065 return;
2066 setDoesNotThrow(F);
2067 setDoesNotCapture(F, 1);
2069 break;
2070 case 'u':
2071 if (Name == "ungetc") {
2072 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
2073 return;
2074 setDoesNotThrow(F);
2075 setDoesNotCapture(F, 2);
2076 } else if (Name == "uname" ||
2077 Name == "unlink" ||
2078 Name == "unsetenv") {
2079 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
2080 return;
2081 setDoesNotThrow(F);
2082 setDoesNotCapture(F, 1);
2083 } else if (Name == "utime" ||
2084 Name == "utimes") {
2085 if (FTy->getNumParams() != 2 ||
2086 !FTy->getParamType(0)->isPointerTy() ||
2087 !FTy->getParamType(1)->isPointerTy())
2088 return;
2089 setDoesNotThrow(F);
2090 setDoesNotCapture(F, 1);
2091 setDoesNotCapture(F, 2);
2093 break;
2094 case 'p':
2095 if (Name == "putc") {
2096 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
2097 return;
2098 setDoesNotThrow(F);
2099 setDoesNotCapture(F, 2);
2100 } else if (Name == "puts" ||
2101 Name == "printf" ||
2102 Name == "perror") {
2103 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
2104 return;
2105 setDoesNotThrow(F);
2106 setDoesNotCapture(F, 1);
2107 } else if (Name == "pread" ||
2108 Name == "pwrite") {
2109 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
2110 return;
2111 // May throw; these are valid pthread cancellation points.
2112 setDoesNotCapture(F, 2);
2113 } else if (Name == "putchar") {
2114 setDoesNotThrow(F);
2115 } else if (Name == "popen") {
2116 if (FTy->getNumParams() != 2 ||
2117 !FTy->getReturnType()->isPointerTy() ||
2118 !FTy->getParamType(0)->isPointerTy() ||
2119 !FTy->getParamType(1)->isPointerTy())
2120 return;
2121 setDoesNotThrow(F);
2122 setDoesNotAlias(F, 0);
2123 setDoesNotCapture(F, 1);
2124 setDoesNotCapture(F, 2);
2125 } else if (Name == "pclose") {
2126 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
2127 return;
2128 setDoesNotThrow(F);
2129 setDoesNotCapture(F, 1);
2131 break;
2132 case 'v':
2133 if (Name == "vscanf") {
2134 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
2135 return;
2136 setDoesNotThrow(F);
2137 setDoesNotCapture(F, 1);
2138 } else if (Name == "vsscanf" ||
2139 Name == "vfscanf") {
2140 if (FTy->getNumParams() != 3 ||
2141 !FTy->getParamType(1)->isPointerTy() ||
2142 !FTy->getParamType(2)->isPointerTy())
2143 return;
2144 setDoesNotThrow(F);
2145 setDoesNotCapture(F, 1);
2146 setDoesNotCapture(F, 2);
2147 } else if (Name == "valloc") {
2148 if (!FTy->getReturnType()->isPointerTy())
2149 return;
2150 setDoesNotThrow(F);
2151 setDoesNotAlias(F, 0);
2152 } else if (Name == "vprintf") {
2153 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
2154 return;
2155 setDoesNotThrow(F);
2156 setDoesNotCapture(F, 1);
2157 } else if (Name == "vfprintf" ||
2158 Name == "vsprintf") {
2159 if (FTy->getNumParams() != 3 ||
2160 !FTy->getParamType(0)->isPointerTy() ||
2161 !FTy->getParamType(1)->isPointerTy())
2162 return;
2163 setDoesNotThrow(F);
2164 setDoesNotCapture(F, 1);
2165 setDoesNotCapture(F, 2);
2166 } else if (Name == "vsnprintf") {
2167 if (FTy->getNumParams() != 4 ||
2168 !FTy->getParamType(0)->isPointerTy() ||
2169 !FTy->getParamType(2)->isPointerTy())
2170 return;
2171 setDoesNotThrow(F);
2172 setDoesNotCapture(F, 1);
2173 setDoesNotCapture(F, 3);
2175 break;
2176 case 'o':
2177 if (Name == "open") {
2178 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
2179 return;
2180 // May throw; "open" is a valid pthread cancellation point.
2181 setDoesNotCapture(F, 1);
2182 } else if (Name == "opendir") {
2183 if (FTy->getNumParams() != 1 ||
2184 !FTy->getReturnType()->isPointerTy() ||
2185 !FTy->getParamType(0)->isPointerTy())
2186 return;
2187 setDoesNotThrow(F);
2188 setDoesNotAlias(F, 0);
2189 setDoesNotCapture(F, 1);
2191 break;
2192 case 't':
2193 if (Name == "tmpfile") {
2194 if (!FTy->getReturnType()->isPointerTy())
2195 return;
2196 setDoesNotThrow(F);
2197 setDoesNotAlias(F, 0);
2198 } else if (Name == "times") {
2199 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
2200 return;
2201 setDoesNotThrow(F);
2202 setDoesNotCapture(F, 1);
2204 break;
2205 case 'h':
2206 if (Name == "htonl" ||
2207 Name == "htons") {
2208 setDoesNotThrow(F);
2209 setDoesNotAccessMemory(F);
2211 break;
2212 case 'n':
2213 if (Name == "ntohl" ||
2214 Name == "ntohs") {
2215 setDoesNotThrow(F);
2216 setDoesNotAccessMemory(F);
2218 break;
2219 case 'l':
2220 if (Name == "lstat") {
2221 if (FTy->getNumParams() != 2 ||
2222 !FTy->getParamType(0)->isPointerTy() ||
2223 !FTy->getParamType(1)->isPointerTy())
2224 return;
2225 setDoesNotThrow(F);
2226 setDoesNotCapture(F, 1);
2227 setDoesNotCapture(F, 2);
2228 } else if (Name == "lchown") {
2229 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
2230 return;
2231 setDoesNotThrow(F);
2232 setDoesNotCapture(F, 1);
2234 break;
2235 case 'q':
2236 if (Name == "qsort") {
2237 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
2238 return;
2239 // May throw; places call through function pointer.
2240 setDoesNotCapture(F, 4);
2242 break;
2243 case '_':
2244 if (Name == "__strdup" ||
2245 Name == "__strndup") {
2246 if (FTy->getNumParams() < 1 ||
2247 !FTy->getReturnType()->isPointerTy() ||
2248 !FTy->getParamType(0)->isPointerTy())
2249 return;
2250 setDoesNotThrow(F);
2251 setDoesNotAlias(F, 0);
2252 setDoesNotCapture(F, 1);
2253 } else if (Name == "__strtok_r") {
2254 if (FTy->getNumParams() != 3 ||
2255 !FTy->getParamType(1)->isPointerTy())
2256 return;
2257 setDoesNotThrow(F);
2258 setDoesNotCapture(F, 2);
2259 } else if (Name == "_IO_getc") {
2260 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
2261 return;
2262 setDoesNotThrow(F);
2263 setDoesNotCapture(F, 1);
2264 } else if (Name == "_IO_putc") {
2265 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
2266 return;
2267 setDoesNotThrow(F);
2268 setDoesNotCapture(F, 2);
2270 break;
2271 case 1:
2272 if (Name == "\1__isoc99_scanf") {
2273 if (FTy->getNumParams() < 1 ||
2274 !FTy->getParamType(0)->isPointerTy())
2275 return;
2276 setDoesNotThrow(F);
2277 setDoesNotCapture(F, 1);
2278 } else if (Name == "\1stat64" ||
2279 Name == "\1lstat64" ||
2280 Name == "\1statvfs64" ||
2281 Name == "\1__isoc99_sscanf") {
2282 if (FTy->getNumParams() < 1 ||
2283 !FTy->getParamType(0)->isPointerTy() ||
2284 !FTy->getParamType(1)->isPointerTy())
2285 return;
2286 setDoesNotThrow(F);
2287 setDoesNotCapture(F, 1);
2288 setDoesNotCapture(F, 2);
2289 } else if (Name == "\1fopen64") {
2290 if (FTy->getNumParams() != 2 ||
2291 !FTy->getReturnType()->isPointerTy() ||
2292 !FTy->getParamType(0)->isPointerTy() ||
2293 !FTy->getParamType(1)->isPointerTy())
2294 return;
2295 setDoesNotThrow(F);
2296 setDoesNotAlias(F, 0);
2297 setDoesNotCapture(F, 1);
2298 setDoesNotCapture(F, 2);
2299 } else if (Name == "\1fseeko64" ||
2300 Name == "\1ftello64") {
2301 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
2302 return;
2303 setDoesNotThrow(F);
2304 setDoesNotCapture(F, 1);
2305 } else if (Name == "\1tmpfile64") {
2306 if (!FTy->getReturnType()->isPointerTy())
2307 return;
2308 setDoesNotThrow(F);
2309 setDoesNotAlias(F, 0);
2310 } else if (Name == "\1fstat64" ||
2311 Name == "\1fstatvfs64") {
2312 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
2313 return;
2314 setDoesNotThrow(F);
2315 setDoesNotCapture(F, 2);
2316 } else if (Name == "\1open64") {
2317 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
2318 return;
2319 // May throw; "open" is a valid pthread cancellation point.
2320 setDoesNotCapture(F, 1);
2322 break;
2326 /// doInitialization - Add attributes to well-known functions.
2328 bool SimplifyLibCalls::doInitialization(Module &M) {
2329 Modified = false;
2330 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
2331 Function &F = *I;
2332 if (F.isDeclaration() && F.hasName())
2333 inferPrototypeAttributes(F);
2335 return Modified;
2338 // TODO:
2339 // Additional cases that we need to add to this file:
2341 // cbrt:
2342 // * cbrt(expN(X)) -> expN(x/3)
2343 // * cbrt(sqrt(x)) -> pow(x,1/6)
2344 // * cbrt(sqrt(x)) -> pow(x,1/9)
2346 // cos, cosf, cosl:
2347 // * cos(-x) -> cos(x)
2349 // exp, expf, expl:
2350 // * exp(log(x)) -> x
2352 // log, logf, logl:
2353 // * log(exp(x)) -> x
2354 // * log(x**y) -> y*log(x)
2355 // * log(exp(y)) -> y*log(e)
2356 // * log(exp2(y)) -> y*log(2)
2357 // * log(exp10(y)) -> y*log(10)
2358 // * log(sqrt(x)) -> 0.5*log(x)
2359 // * log(pow(x,y)) -> y*log(x)
2361 // lround, lroundf, lroundl:
2362 // * lround(cnst) -> cnst'
2364 // pow, powf, powl:
2365 // * pow(exp(x),y) -> exp(x*y)
2366 // * pow(sqrt(x),y) -> pow(x,y*0.5)
2367 // * pow(pow(x,y),z)-> pow(x,y*z)
2369 // round, roundf, roundl:
2370 // * round(cnst) -> cnst'
2372 // signbit:
2373 // * signbit(cnst) -> cnst'
2374 // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2376 // sqrt, sqrtf, sqrtl:
2377 // * sqrt(expN(x)) -> expN(x*0.5)
2378 // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2379 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2381 // stpcpy:
2382 // * stpcpy(str, "literal") ->
2383 // llvm.memcpy(str,"literal",strlen("literal")+1,1)
2385 // tan, tanf, tanl:
2386 // * tan(atan(x)) -> x
2388 // trunc, truncf, truncl:
2389 // * trunc(cnst) -> cnst'