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). For example, a call to the function "exit(3)" that
13 // occurs within the main() function can be transformed into a simple "return 3"
14 // instruction. Any optimization that takes this form (replace call to library
15 // function with simpler code that provides the same result) belongs in this
18 //===----------------------------------------------------------------------===//
20 #define DEBUG_TYPE "simplify-libcalls"
21 #include "llvm/Transforms/Scalar.h"
22 #include "llvm/Intrinsics.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/ADT/SmallPtrSet.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Config/config.h"
36 STATISTIC(NumSimplified, "Number of library calls simplified");
37 STATISTIC(NumAnnotated, "Number of attributes added to library functions");
39 //===----------------------------------------------------------------------===//
40 // Optimizer Base Class
41 //===----------------------------------------------------------------------===//
43 /// This class is the abstract base class for the set of optimizations that
44 /// corresponds to one library call.
46 class VISIBILITY_HIDDEN LibCallOptimization {
51 LibCallOptimization() { }
52 virtual ~LibCallOptimization() {}
54 /// CallOptimizer - This pure virtual method is implemented by base classes to
55 /// do various optimizations. If this returns null then no transformation was
56 /// performed. If it returns CI, then it transformed the call and CI is to be
57 /// deleted. If it returns something else, replace CI with the new value and
59 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
62 Value *OptimizeCall(CallInst *CI, const TargetData &TD, IRBuilder<> &B) {
63 Caller = CI->getParent()->getParent();
65 return CallOptimizer(CI->getCalledFunction(), CI, B);
68 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
69 Value *CastToCStr(Value *V, IRBuilder<> &B);
71 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
72 /// specified pointer. Ptr is required to be some pointer type, and the
73 /// return value has 'intptr_t' type.
74 Value *EmitStrLen(Value *Ptr, IRBuilder<> &B);
76 /// EmitMemCpy - Emit a call to the memcpy function to the builder. This
77 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
78 Value *EmitMemCpy(Value *Dst, Value *Src, Value *Len,
79 unsigned Align, IRBuilder<> &B);
81 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
82 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
83 Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B);
85 /// EmitMemCmp - Emit a call to the memcmp function.
86 Value *EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B);
88 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
89 /// 'floor'). This function is known to take a single of type matching 'Op'
90 /// and returns one value with the same type. If 'Op' is a long double, 'l'
91 /// is added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
92 Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder<> &B);
94 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
96 void EmitPutChar(Value *Char, IRBuilder<> &B);
98 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
100 void EmitPutS(Value *Str, IRBuilder<> &B);
102 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
103 /// an i32, and File is a pointer to FILE.
104 void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B);
106 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
107 /// pointer and File is a pointer to FILE.
108 void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B);
110 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
111 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
112 void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B);
115 } // End anonymous namespace.
117 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
118 Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
119 return B.CreateBitCast(V, PointerType::getUnqual(Type::Int8Ty), "cstr");
122 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
123 /// specified pointer. This always returns an integer value of size intptr_t.
124 Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder<> &B) {
125 Module *M = Caller->getParent();
126 AttributeWithIndex AWI[2];
127 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
128 AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
129 Attribute::NoUnwind);
131 Constant *StrLen =M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
133 PointerType::getUnqual(Type::Int8Ty),
135 return B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
138 /// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
139 /// expects that the size has type 'intptr_t' and Dst/Src are pointers.
140 Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
141 unsigned Align, IRBuilder<> &B) {
142 Module *M = Caller->getParent();
143 Intrinsic::ID IID = Intrinsic::memcpy;
145 Tys[0] = Len->getType();
146 Value *MemCpy = Intrinsic::getDeclaration(M, IID, Tys, 1);
147 return B.CreateCall4(MemCpy, CastToCStr(Dst, B), CastToCStr(Src, B), Len,
148 ConstantInt::get(Type::Int32Ty, Align));
151 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
152 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
153 Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
154 Value *Len, IRBuilder<> &B) {
155 Module *M = Caller->getParent();
156 AttributeWithIndex AWI;
157 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
159 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
160 PointerType::getUnqual(Type::Int8Ty),
161 PointerType::getUnqual(Type::Int8Ty),
162 Type::Int32Ty, TD->getIntPtrType(),
164 return B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
167 /// EmitMemCmp - Emit a call to the memcmp function.
168 Value *LibCallOptimization::EmitMemCmp(Value *Ptr1, Value *Ptr2,
169 Value *Len, IRBuilder<> &B) {
170 Module *M = Caller->getParent();
171 AttributeWithIndex AWI[3];
172 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
173 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
174 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
175 Attribute::NoUnwind);
177 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
179 PointerType::getUnqual(Type::Int8Ty),
180 PointerType::getUnqual(Type::Int8Ty),
181 TD->getIntPtrType(), NULL);
182 return B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
186 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
187 /// 'floor'). This function is known to take a single of type matching 'Op' and
188 /// returns one value with the same type. If 'Op' is a long double, 'l' is
189 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
190 Value *LibCallOptimization::EmitUnaryFloatFnCall(Value *Op, const char *Name,
193 if (Op->getType() != Type::DoubleTy) {
194 // If we need to add a suffix, copy into NameBuffer.
195 unsigned NameLen = strlen(Name);
196 assert(NameLen < sizeof(NameBuffer)-2);
197 memcpy(NameBuffer, Name, NameLen);
198 if (Op->getType() == Type::FloatTy)
199 NameBuffer[NameLen] = 'f'; // floorf
201 NameBuffer[NameLen] = 'l'; // floorl
202 NameBuffer[NameLen+1] = 0;
206 Module *M = Caller->getParent();
207 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
208 Op->getType(), NULL);
209 return B.CreateCall(Callee, Op, Name);
212 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
214 void LibCallOptimization::EmitPutChar(Value *Char, IRBuilder<> &B) {
215 Module *M = Caller->getParent();
216 Value *F = M->getOrInsertFunction("putchar", Type::Int32Ty,
217 Type::Int32Ty, NULL);
218 B.CreateCall(F, B.CreateIntCast(Char, Type::Int32Ty, "chari"), "putchar");
221 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
223 void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
224 Module *M = Caller->getParent();
225 AttributeWithIndex AWI[2];
226 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
227 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
229 Value *F = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
231 PointerType::getUnqual(Type::Int8Ty), NULL);
232 B.CreateCall(F, CastToCStr(Str, B), "puts");
235 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
236 /// an integer and File is a pointer to FILE.
237 void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) {
238 Module *M = Caller->getParent();
239 AttributeWithIndex AWI[2];
240 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
241 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
243 if (isa<PointerType>(File->getType()))
244 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2), Type::Int32Ty,
245 Type::Int32Ty, File->getType(), NULL);
248 F = M->getOrInsertFunction("fputc", Type::Int32Ty, Type::Int32Ty,
249 File->getType(), NULL);
250 Char = B.CreateIntCast(Char, Type::Int32Ty, "chari");
251 B.CreateCall2(F, Char, File, "fputc");
254 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
255 /// pointer and File is a pointer to FILE.
256 void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
257 Module *M = Caller->getParent();
258 AttributeWithIndex AWI[3];
259 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
260 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
261 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
263 if (isa<PointerType>(File->getType()))
264 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3), Type::Int32Ty,
265 PointerType::getUnqual(Type::Int8Ty),
266 File->getType(), NULL);
268 F = M->getOrInsertFunction("fputs", Type::Int32Ty,
269 PointerType::getUnqual(Type::Int8Ty),
270 File->getType(), NULL);
271 B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
274 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
275 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
276 void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
278 Module *M = Caller->getParent();
279 AttributeWithIndex AWI[3];
280 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
281 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
282 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
284 if (isa<PointerType>(File->getType()))
285 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
287 PointerType::getUnqual(Type::Int8Ty),
288 TD->getIntPtrType(), TD->getIntPtrType(),
289 File->getType(), NULL);
291 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(),
292 PointerType::getUnqual(Type::Int8Ty),
293 TD->getIntPtrType(), TD->getIntPtrType(),
294 File->getType(), NULL);
295 B.CreateCall4(F, CastToCStr(Ptr, B), Size,
296 ConstantInt::get(TD->getIntPtrType(), 1), File);
299 //===----------------------------------------------------------------------===//
301 //===----------------------------------------------------------------------===//
303 /// GetStringLengthH - If we can compute the length of the string pointed to by
304 /// the specified pointer, return 'len+1'. If we can't, return 0.
305 static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
306 // Look through noop bitcast instructions.
307 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
308 return GetStringLengthH(BCI->getOperand(0), PHIs);
310 // If this is a PHI node, there are two cases: either we have already seen it
312 if (PHINode *PN = dyn_cast<PHINode>(V)) {
313 if (!PHIs.insert(PN))
314 return ~0ULL; // already in the set.
316 // If it was new, see if all the input strings are the same length.
317 uint64_t LenSoFar = ~0ULL;
318 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
319 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
320 if (Len == 0) return 0; // Unknown length -> unknown.
322 if (Len == ~0ULL) continue;
324 if (Len != LenSoFar && LenSoFar != ~0ULL)
325 return 0; // Disagree -> unknown.
329 // Success, all agree.
333 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
334 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
335 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
336 if (Len1 == 0) return 0;
337 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
338 if (Len2 == 0) return 0;
339 if (Len1 == ~0ULL) return Len2;
340 if (Len2 == ~0ULL) return Len1;
341 if (Len1 != Len2) return 0;
345 // If the value is not a GEP instruction nor a constant expression with a
346 // GEP instruction, then return unknown.
348 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
350 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
351 if (CE->getOpcode() != Instruction::GetElementPtr)
358 // Make sure the GEP has exactly three arguments.
359 if (GEP->getNumOperands() != 3)
362 // Check to make sure that the first operand of the GEP is an integer and
363 // has value 0 so that we are sure we're indexing into the initializer.
364 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
370 // If the second index isn't a ConstantInt, then this is a variable index
371 // into the array. If this occurs, we can't say anything meaningful about
373 uint64_t StartIdx = 0;
374 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
375 StartIdx = CI->getZExtValue();
379 // The GEP instruction, constant or instruction, must reference a global
380 // variable that is a constant and is initialized. The referenced constant
381 // initializer is the array that we'll use for optimization.
382 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
383 if (!GV || !GV->isConstant() || !GV->hasInitializer())
385 Constant *GlobalInit = GV->getInitializer();
387 // Handle the ConstantAggregateZero case, which is a degenerate case. The
388 // initializer is constant zero so the length of the string must be zero.
389 if (isa<ConstantAggregateZero>(GlobalInit))
390 return 1; // Len = 0 offset by 1.
392 // Must be a Constant Array
393 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
394 if (!Array || Array->getType()->getElementType() != Type::Int8Ty)
397 // Get the number of elements in the array
398 uint64_t NumElts = Array->getType()->getNumElements();
400 // Traverse the constant array from StartIdx (derived above) which is
401 // the place the GEP refers to in the array.
402 for (unsigned i = StartIdx; i != NumElts; ++i) {
403 Constant *Elt = Array->getOperand(i);
404 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
405 if (!CI) // This array isn't suitable, non-int initializer.
408 return i-StartIdx+1; // We found end of string, success!
411 return 0; // The array isn't null terminated, conservatively return 'unknown'.
414 /// GetStringLength - If we can compute the length of the string pointed to by
415 /// the specified pointer, return 'len+1'. If we can't, return 0.
416 static uint64_t GetStringLength(Value *V) {
417 if (!isa<PointerType>(V->getType())) return 0;
419 SmallPtrSet<PHINode*, 32> PHIs;
420 uint64_t Len = GetStringLengthH(V, PHIs);
421 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
422 // an empty string as a length.
423 return Len == ~0ULL ? 1 : Len;
426 /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
427 /// value is equal or not-equal to zero.
428 static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
429 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
431 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
432 if (IC->isEquality())
433 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
434 if (C->isNullValue())
436 // Unknown instruction.
442 //===----------------------------------------------------------------------===//
443 // Miscellaneous LibCall Optimizations
444 //===----------------------------------------------------------------------===//
447 //===---------------------------------------===//
448 // 'exit' Optimizations
450 /// ExitOpt - int main() { exit(4); } --> int main() { return 4; }
451 struct VISIBILITY_HIDDEN ExitOpt : public LibCallOptimization {
452 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
453 // Verify we have a reasonable prototype for exit.
454 if (Callee->arg_size() == 0 || !CI->use_empty())
457 // Verify the caller is main, and that the result type of main matches the
458 // argument type of exit.
459 if (!Caller->isName("main") || !Caller->hasExternalLinkage() ||
460 Caller->getReturnType() != CI->getOperand(1)->getType())
463 TerminatorInst *OldTI = CI->getParent()->getTerminator();
465 // Create the return after the call.
466 ReturnInst *RI = B.CreateRet(CI->getOperand(1));
468 // Drop all successor phi node entries.
469 for (unsigned i = 0, e = OldTI->getNumSuccessors(); i != e; ++i)
470 OldTI->getSuccessor(i)->removePredecessor(CI->getParent());
472 // Erase all instructions from after our return instruction until the end of
474 BasicBlock::iterator FirstDead = RI; ++FirstDead;
475 CI->getParent()->getInstList().erase(FirstDead, CI->getParent()->end());
480 //===----------------------------------------------------------------------===//
481 // String and Memory LibCall Optimizations
482 //===----------------------------------------------------------------------===//
484 //===---------------------------------------===//
485 // 'strcat' Optimizations
487 struct VISIBILITY_HIDDEN StrCatOpt : public LibCallOptimization {
488 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
489 // Verify the "strcat" function prototype.
490 const FunctionType *FT = Callee->getFunctionType();
491 if (FT->getNumParams() != 2 ||
492 FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
493 FT->getParamType(0) != FT->getReturnType() ||
494 FT->getParamType(1) != FT->getReturnType())
497 // Extract some information from the instruction
498 Value *Dst = CI->getOperand(1);
499 Value *Src = CI->getOperand(2);
501 // See if we can get the length of the input string.
502 uint64_t Len = GetStringLength(Src);
503 if (Len == 0) return 0;
504 --Len; // Unbias length.
506 // Handle the simple, do-nothing case: strcat(x, "") -> x
510 // We need to find the end of the destination string. That's where the
511 // memory is to be moved to. We just generate a call to strlen.
512 Value *DstLen = EmitStrLen(Dst, B);
514 // Now that we have the destination's length, we must index into the
515 // destination's pointer to get the actual memcpy destination (end of
516 // the string .. we're concatenating).
517 Value *CpyDst = B.CreateGEP(Dst, DstLen, "endptr");
519 // We have enough information to now generate the memcpy call to do the
520 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
521 EmitMemCpy(CpyDst, Src, ConstantInt::get(TD->getIntPtrType(), Len+1), 1, B);
526 //===---------------------------------------===//
527 // 'strchr' Optimizations
529 struct VISIBILITY_HIDDEN StrChrOpt : public LibCallOptimization {
530 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
531 // Verify the "strchr" function prototype.
532 const FunctionType *FT = Callee->getFunctionType();
533 if (FT->getNumParams() != 2 ||
534 FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
535 FT->getParamType(0) != FT->getReturnType())
538 Value *SrcStr = CI->getOperand(1);
540 // If the second operand is non-constant, see if we can compute the length
541 // of the input string and turn this into memchr.
542 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
544 uint64_t Len = GetStringLength(SrcStr);
545 if (Len == 0 || FT->getParamType(1) != Type::Int32Ty) // memchr needs i32.
548 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
549 ConstantInt::get(TD->getIntPtrType(), Len), B);
552 // Otherwise, the character is a constant, see if the first argument is
553 // a string literal. If so, we can constant fold.
555 if (!GetConstantStringInfo(SrcStr, Str))
558 // strchr can find the nul character.
560 char CharValue = CharC->getSExtValue();
562 // Compute the offset.
565 if (i == Str.size()) // Didn't find the char. strchr returns null.
566 return Constant::getNullValue(CI->getType());
567 // Did we find our match?
568 if (Str[i] == CharValue)
573 // strchr(s+n,c) -> gep(s+n+i,c)
574 Value *Idx = ConstantInt::get(Type::Int64Ty, i);
575 return B.CreateGEP(SrcStr, Idx, "strchr");
579 //===---------------------------------------===//
580 // 'strcmp' Optimizations
582 struct VISIBILITY_HIDDEN StrCmpOpt : public LibCallOptimization {
583 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
584 // Verify the "strcmp" function prototype.
585 const FunctionType *FT = Callee->getFunctionType();
586 if (FT->getNumParams() != 2 || FT->getReturnType() != Type::Int32Ty ||
587 FT->getParamType(0) != FT->getParamType(1) ||
588 FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty))
591 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
592 if (Str1P == Str2P) // strcmp(x,x) -> 0
593 return ConstantInt::get(CI->getType(), 0);
595 std::string Str1, Str2;
596 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
597 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
599 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
600 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
602 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
603 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
605 // strcmp(x, y) -> cnst (if both x and y are constant strings)
606 if (HasStr1 && HasStr2)
607 return ConstantInt::get(CI->getType(), strcmp(Str1.c_str(),Str2.c_str()));
609 // strcmp(P, "x") -> memcmp(P, "x", 2)
610 uint64_t Len1 = GetStringLength(Str1P);
611 uint64_t Len2 = GetStringLength(Str2P);
613 // Choose the smallest Len excluding 0 which means 'unknown'.
614 if (!Len1 || (Len2 && Len2 < Len1))
616 return EmitMemCmp(Str1P, Str2P,
617 ConstantInt::get(TD->getIntPtrType(), Len1), B);
624 //===---------------------------------------===//
625 // 'strncmp' Optimizations
627 struct VISIBILITY_HIDDEN StrNCmpOpt : public LibCallOptimization {
628 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
629 // Verify the "strncmp" function prototype.
630 const FunctionType *FT = Callee->getFunctionType();
631 if (FT->getNumParams() != 3 || FT->getReturnType() != Type::Int32Ty ||
632 FT->getParamType(0) != FT->getParamType(1) ||
633 FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
634 !isa<IntegerType>(FT->getParamType(2)))
637 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
638 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
639 return ConstantInt::get(CI->getType(), 0);
641 // Get the length argument if it is constant.
643 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
644 Length = LengthArg->getZExtValue();
648 if (Length == 0) // strncmp(x,y,0) -> 0
649 return ConstantInt::get(CI->getType(), 0);
651 std::string Str1, Str2;
652 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
653 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
655 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
656 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
658 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
659 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
661 // strncmp(x, y) -> cnst (if both x and y are constant strings)
662 if (HasStr1 && HasStr2)
663 return ConstantInt::get(CI->getType(),
664 strncmp(Str1.c_str(), Str2.c_str(), Length));
670 //===---------------------------------------===//
671 // 'strcpy' Optimizations
673 struct VISIBILITY_HIDDEN StrCpyOpt : public LibCallOptimization {
674 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
675 // Verify the "strcpy" function prototype.
676 const FunctionType *FT = Callee->getFunctionType();
677 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
678 FT->getParamType(0) != FT->getParamType(1) ||
679 FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty))
682 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
683 if (Dst == Src) // strcpy(x,x) -> x
686 // See if we can get the length of the input string.
687 uint64_t Len = GetStringLength(Src);
688 if (Len == 0) return 0;
690 // We have enough information to now generate the memcpy call to do the
691 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
692 EmitMemCpy(Dst, Src, ConstantInt::get(TD->getIntPtrType(), Len), 1, B);
699 //===---------------------------------------===//
700 // 'strlen' Optimizations
702 struct VISIBILITY_HIDDEN StrLenOpt : public LibCallOptimization {
703 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
704 const FunctionType *FT = Callee->getFunctionType();
705 if (FT->getNumParams() != 1 ||
706 FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
707 !isa<IntegerType>(FT->getReturnType()))
710 Value *Src = CI->getOperand(1);
712 // Constant folding: strlen("xyz") -> 3
713 if (uint64_t Len = GetStringLength(Src))
714 return ConstantInt::get(CI->getType(), Len-1);
716 // Handle strlen(p) != 0.
717 if (!IsOnlyUsedInZeroEqualityComparison(CI)) return 0;
719 // strlen(x) != 0 --> *x != 0
720 // strlen(x) == 0 --> *x == 0
721 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
725 //===---------------------------------------===//
726 // 'strto*' Optimizations
728 struct VISIBILITY_HIDDEN StrToOpt : public LibCallOptimization {
729 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
730 const FunctionType *FT = Callee->getFunctionType();
731 if ((FT->getNumParams() != 2 && FT->getNumParams() != 3) ||
732 !isa<PointerType>(FT->getParamType(0)) ||
733 !isa<PointerType>(FT->getParamType(1)))
736 Value *EndPtr = CI->getOperand(2);
737 if (isa<ConstantPointerNull>(EndPtr)) {
738 CI->setOnlyReadsMemory();
739 CI->addAttribute(1, Attribute::NoCapture);
747 //===---------------------------------------===//
748 // 'memcmp' Optimizations
750 struct VISIBILITY_HIDDEN MemCmpOpt : public LibCallOptimization {
751 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
752 const FunctionType *FT = Callee->getFunctionType();
753 if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
754 !isa<PointerType>(FT->getParamType(1)) ||
755 FT->getReturnType() != Type::Int32Ty)
758 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
760 if (LHS == RHS) // memcmp(s,s,x) -> 0
761 return Constant::getNullValue(CI->getType());
763 // Make sure we have a constant length.
764 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
766 uint64_t Len = LenC->getZExtValue();
768 if (Len == 0) // memcmp(s1,s2,0) -> 0
769 return Constant::getNullValue(CI->getType());
771 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
772 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
773 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
774 return B.CreateZExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
777 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS) != 0
778 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS) != 0
779 if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
780 const Type *PTy = PointerType::getUnqual(Len == 2 ?
781 Type::Int16Ty : Type::Int32Ty);
782 LHS = B.CreateBitCast(LHS, PTy, "tmp");
783 RHS = B.CreateBitCast(RHS, PTy, "tmp");
784 LoadInst *LHSV = B.CreateLoad(LHS, "lhsv");
785 LoadInst *RHSV = B.CreateLoad(RHS, "rhsv");
786 LHSV->setAlignment(1); RHSV->setAlignment(1); // Unaligned loads.
787 return B.CreateZExt(B.CreateXor(LHSV, RHSV, "shortdiff"), CI->getType());
794 //===---------------------------------------===//
795 // 'memcpy' Optimizations
797 struct VISIBILITY_HIDDEN MemCpyOpt : public LibCallOptimization {
798 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
799 const FunctionType *FT = Callee->getFunctionType();
800 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
801 !isa<PointerType>(FT->getParamType(0)) ||
802 !isa<PointerType>(FT->getParamType(1)) ||
803 FT->getParamType(2) != TD->getIntPtrType())
806 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
807 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
808 return CI->getOperand(1);
812 //===---------------------------------------===//
813 // 'memmove' Optimizations
815 struct VISIBILITY_HIDDEN MemMoveOpt : public LibCallOptimization {
816 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
817 const FunctionType *FT = Callee->getFunctionType();
818 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
819 !isa<PointerType>(FT->getParamType(0)) ||
820 !isa<PointerType>(FT->getParamType(1)) ||
821 FT->getParamType(2) != TD->getIntPtrType())
824 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
825 Module *M = Caller->getParent();
826 Intrinsic::ID IID = Intrinsic::memmove;
828 Tys[0] = TD->getIntPtrType();
829 Value *MemMove = Intrinsic::getDeclaration(M, IID, Tys, 1);
830 Value *Dst = CastToCStr(CI->getOperand(1), B);
831 Value *Src = CastToCStr(CI->getOperand(2), B);
832 Value *Size = CI->getOperand(3);
833 Value *Align = ConstantInt::get(Type::Int32Ty, 1);
834 B.CreateCall4(MemMove, Dst, Src, Size, Align);
835 return CI->getOperand(1);
839 //===---------------------------------------===//
840 // 'memset' Optimizations
842 struct VISIBILITY_HIDDEN MemSetOpt : public LibCallOptimization {
843 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
844 const FunctionType *FT = Callee->getFunctionType();
845 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
846 !isa<PointerType>(FT->getParamType(0)) ||
847 FT->getParamType(1) != TD->getIntPtrType() ||
848 FT->getParamType(2) != TD->getIntPtrType())
851 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
852 Module *M = Caller->getParent();
853 Intrinsic::ID IID = Intrinsic::memset;
855 Tys[0] = TD->getIntPtrType();
856 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
857 Value *Dst = CastToCStr(CI->getOperand(1), B);
858 Value *Val = B.CreateTrunc(CI->getOperand(2), Type::Int8Ty);
859 Value *Size = CI->getOperand(3);
860 Value *Align = ConstantInt::get(Type::Int32Ty, 1);
861 B.CreateCall4(MemSet, Dst, Val, Size, Align);
862 return CI->getOperand(1);
866 //===----------------------------------------------------------------------===//
867 // Math Library Optimizations
868 //===----------------------------------------------------------------------===//
870 //===---------------------------------------===//
871 // 'pow*' Optimizations
873 struct VISIBILITY_HIDDEN PowOpt : public LibCallOptimization {
874 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
875 const FunctionType *FT = Callee->getFunctionType();
876 // Just make sure this has 2 arguments of the same FP type, which match the
878 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
879 FT->getParamType(0) != FT->getParamType(1) ||
880 !FT->getParamType(0)->isFloatingPoint())
883 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
884 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
885 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
887 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
888 return EmitUnaryFloatFnCall(Op2, "exp2", B);
891 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
892 if (Op2C == 0) return 0;
894 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
895 return ConstantFP::get(CI->getType(), 1.0);
897 if (Op2C->isExactlyValue(0.5)) {
898 // FIXME: This is not safe for -0.0 and -inf. This can only be done when
899 // 'unsafe' math optimizations are allowed.
900 // x pow(x, 0.5) sqrt(x)
901 // ---------------------------------------------
905 // pow(x, 0.5) -> sqrt(x)
906 return B.CreateCall(get_sqrt(), Op1, "sqrt");
910 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
912 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
913 return B.CreateMul(Op1, Op1, "pow2");
914 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
915 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), Op1, "powrecip");
920 //===---------------------------------------===//
921 // 'exp2' Optimizations
923 struct VISIBILITY_HIDDEN Exp2Opt : public LibCallOptimization {
924 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
925 const FunctionType *FT = Callee->getFunctionType();
926 // Just make sure this has 1 argument of FP type, which matches the
928 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
929 !FT->getParamType(0)->isFloatingPoint())
932 Value *Op = CI->getOperand(1);
933 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
934 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
936 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
937 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
938 LdExpArg = B.CreateSExt(OpC->getOperand(0), Type::Int32Ty, "tmp");
939 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
940 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
941 LdExpArg = B.CreateZExt(OpC->getOperand(0), Type::Int32Ty, "tmp");
946 if (Op->getType() == Type::FloatTy)
948 else if (Op->getType() == Type::DoubleTy)
953 Constant *One = ConstantFP::get(APFloat(1.0f));
954 if (Op->getType() != Type::FloatTy)
955 One = ConstantExpr::getFPExtend(One, Op->getType());
957 Module *M = Caller->getParent();
958 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
959 Op->getType(), Type::Int32Ty,NULL);
960 return B.CreateCall2(Callee, One, LdExpArg);
967 //===---------------------------------------===//
968 // Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
970 struct VISIBILITY_HIDDEN UnaryDoubleFPOpt : public LibCallOptimization {
971 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
972 const FunctionType *FT = Callee->getFunctionType();
973 if (FT->getNumParams() != 1 || FT->getReturnType() != Type::DoubleTy ||
974 FT->getParamType(0) != Type::DoubleTy)
977 // If this is something like 'floor((double)floatval)', convert to floorf.
978 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
979 if (Cast == 0 || Cast->getOperand(0)->getType() != Type::FloatTy)
982 // floor((double)floatval) -> (double)floorf(floatval)
983 Value *V = Cast->getOperand(0);
984 V = EmitUnaryFloatFnCall(V, Callee->getNameStart(), B);
985 return B.CreateFPExt(V, Type::DoubleTy);
989 //===----------------------------------------------------------------------===//
990 // Integer Optimizations
991 //===----------------------------------------------------------------------===//
993 //===---------------------------------------===//
994 // 'ffs*' Optimizations
996 struct VISIBILITY_HIDDEN FFSOpt : public LibCallOptimization {
997 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
998 const FunctionType *FT = Callee->getFunctionType();
999 // Just make sure this has 2 arguments of the same FP type, which match the
1001 if (FT->getNumParams() != 1 || FT->getReturnType() != Type::Int32Ty ||
1002 !isa<IntegerType>(FT->getParamType(0)))
1005 Value *Op = CI->getOperand(1);
1008 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
1009 if (CI->getValue() == 0) // ffs(0) -> 0.
1010 return Constant::getNullValue(CI->getType());
1011 return ConstantInt::get(Type::Int32Ty, // ffs(c) -> cttz(c)+1
1012 CI->getValue().countTrailingZeros()+1);
1015 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
1016 const Type *ArgType = Op->getType();
1017 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
1018 Intrinsic::cttz, &ArgType, 1);
1019 Value *V = B.CreateCall(F, Op, "cttz");
1020 V = B.CreateAdd(V, ConstantInt::get(Type::Int32Ty, 1), "tmp");
1021 V = B.CreateIntCast(V, Type::Int32Ty, false, "tmp");
1023 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
1024 return B.CreateSelect(Cond, V, ConstantInt::get(Type::Int32Ty, 0));
1028 //===---------------------------------------===//
1029 // 'isdigit' Optimizations
1031 struct VISIBILITY_HIDDEN IsDigitOpt : public LibCallOptimization {
1032 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1033 const FunctionType *FT = Callee->getFunctionType();
1034 // We require integer(i32)
1035 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1036 FT->getParamType(0) != Type::Int32Ty)
1039 // isdigit(c) -> (c-'0') <u 10
1040 Value *Op = CI->getOperand(1);
1041 Op = B.CreateSub(Op, ConstantInt::get(Type::Int32Ty, '0'), "isdigittmp");
1042 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::Int32Ty, 10), "isdigit");
1043 return B.CreateZExt(Op, CI->getType());
1047 //===---------------------------------------===//
1048 // 'isascii' Optimizations
1050 struct VISIBILITY_HIDDEN IsAsciiOpt : public LibCallOptimization {
1051 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1052 const FunctionType *FT = Callee->getFunctionType();
1053 // We require integer(i32)
1054 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1055 FT->getParamType(0) != Type::Int32Ty)
1058 // isascii(c) -> c <u 128
1059 Value *Op = CI->getOperand(1);
1060 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::Int32Ty, 128), "isascii");
1061 return B.CreateZExt(Op, CI->getType());
1065 //===---------------------------------------===//
1066 // 'abs', 'labs', 'llabs' Optimizations
1068 struct VISIBILITY_HIDDEN AbsOpt : public LibCallOptimization {
1069 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1070 const FunctionType *FT = Callee->getFunctionType();
1071 // We require integer(integer) where the types agree.
1072 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
1073 FT->getParamType(0) != FT->getReturnType())
1076 // abs(x) -> x >s -1 ? x : -x
1077 Value *Op = CI->getOperand(1);
1078 Value *Pos = B.CreateICmpSGT(Op,ConstantInt::getAllOnesValue(Op->getType()),
1080 Value *Neg = B.CreateNeg(Op, "neg");
1081 return B.CreateSelect(Pos, Op, Neg);
1086 //===---------------------------------------===//
1087 // 'toascii' Optimizations
1089 struct VISIBILITY_HIDDEN ToAsciiOpt : public LibCallOptimization {
1090 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1091 const FunctionType *FT = Callee->getFunctionType();
1092 // We require i32(i32)
1093 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
1094 FT->getParamType(0) != Type::Int32Ty)
1097 // isascii(c) -> c & 0x7f
1098 return B.CreateAnd(CI->getOperand(1), ConstantInt::get(CI->getType(),0x7F));
1102 //===----------------------------------------------------------------------===//
1103 // Formatting and IO Optimizations
1104 //===----------------------------------------------------------------------===//
1106 //===---------------------------------------===//
1107 // 'printf' Optimizations
1109 struct VISIBILITY_HIDDEN PrintFOpt : public LibCallOptimization {
1110 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1111 // Require one fixed pointer argument and an integer/void result.
1112 const FunctionType *FT = Callee->getFunctionType();
1113 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
1114 !(isa<IntegerType>(FT->getReturnType()) ||
1115 FT->getReturnType() == Type::VoidTy))
1118 // Check for a fixed format string.
1119 std::string FormatStr;
1120 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1123 // Empty format string -> noop.
1124 if (FormatStr.empty()) // Tolerate printf's declared void.
1125 return CI->use_empty() ? (Value*)CI : ConstantInt::get(CI->getType(), 0);
1127 // printf("x") -> putchar('x'), even for '%'.
1128 if (FormatStr.size() == 1) {
1129 EmitPutChar(ConstantInt::get(Type::Int32Ty, FormatStr[0]), B);
1130 return CI->use_empty() ? (Value*)CI : ConstantInt::get(CI->getType(), 1);
1133 // printf("foo\n") --> puts("foo")
1134 if (FormatStr[FormatStr.size()-1] == '\n' &&
1135 FormatStr.find('%') == std::string::npos) { // no format characters.
1136 // Create a string literal with no \n on it. We expect the constant merge
1137 // pass to be run after this pass, to merge duplicate strings.
1138 FormatStr.erase(FormatStr.end()-1);
1139 Constant *C = ConstantArray::get(FormatStr, true);
1140 C = new GlobalVariable(C->getType(), true,GlobalVariable::InternalLinkage,
1141 C, "str", Callee->getParent());
1143 return CI->use_empty() ? (Value*)CI :
1144 ConstantInt::get(CI->getType(), FormatStr.size()+1);
1147 // Optimize specific format strings.
1148 // printf("%c", chr) --> putchar(*(i8*)dst)
1149 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
1150 isa<IntegerType>(CI->getOperand(2)->getType())) {
1151 EmitPutChar(CI->getOperand(2), B);
1152 return CI->use_empty() ? (Value*)CI : ConstantInt::get(CI->getType(), 1);
1155 // printf("%s\n", str) --> puts(str)
1156 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
1157 isa<PointerType>(CI->getOperand(2)->getType()) &&
1159 EmitPutS(CI->getOperand(2), B);
1166 //===---------------------------------------===//
1167 // 'sprintf' Optimizations
1169 struct VISIBILITY_HIDDEN SPrintFOpt : public LibCallOptimization {
1170 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1171 // Require two fixed pointer arguments and an integer result.
1172 const FunctionType *FT = Callee->getFunctionType();
1173 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1174 !isa<PointerType>(FT->getParamType(1)) ||
1175 !isa<IntegerType>(FT->getReturnType()))
1178 // Check for a fixed format string.
1179 std::string FormatStr;
1180 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1183 // If we just have a format string (nothing else crazy) transform it.
1184 if (CI->getNumOperands() == 3) {
1185 // Make sure there's no % in the constant array. We could try to handle
1186 // %% -> % in the future if we cared.
1187 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1188 if (FormatStr[i] == '%')
1189 return 0; // we found a format specifier, bail out.
1191 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1192 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
1193 ConstantInt::get(TD->getIntPtrType(), FormatStr.size()+1),1,B);
1194 return ConstantInt::get(CI->getType(), FormatStr.size());
1197 // The remaining optimizations require the format string to be "%s" or "%c"
1198 // and have an extra operand.
1199 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1202 // Decode the second character of the format string.
1203 if (FormatStr[1] == 'c') {
1204 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1205 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1206 Value *V = B.CreateTrunc(CI->getOperand(3), Type::Int8Ty, "char");
1207 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1208 B.CreateStore(V, Ptr);
1209 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::Int32Ty, 1), "nul");
1210 B.CreateStore(Constant::getNullValue(Type::Int8Ty), Ptr);
1212 return ConstantInt::get(CI->getType(), 1);
1215 if (FormatStr[1] == 's') {
1216 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1217 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1219 Value *Len = EmitStrLen(CI->getOperand(3), B);
1220 Value *IncLen = B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1),
1222 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
1224 // The sprintf result is the unincremented number of bytes in the string.
1225 return B.CreateIntCast(Len, CI->getType(), false);
1231 //===---------------------------------------===//
1232 // 'fwrite' Optimizations
1234 struct VISIBILITY_HIDDEN FWriteOpt : public LibCallOptimization {
1235 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1236 // Require a pointer, an integer, an integer, a pointer, returning integer.
1237 const FunctionType *FT = Callee->getFunctionType();
1238 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1239 !isa<IntegerType>(FT->getParamType(1)) ||
1240 !isa<IntegerType>(FT->getParamType(2)) ||
1241 !isa<PointerType>(FT->getParamType(3)) ||
1242 !isa<IntegerType>(FT->getReturnType()))
1245 // Get the element size and count.
1246 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1247 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1248 if (!SizeC || !CountC) return 0;
1249 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
1251 // If this is writing zero records, remove the call (it's a noop).
1253 return ConstantInt::get(CI->getType(), 0);
1255 // If this is writing one byte, turn it into fputc.
1256 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1257 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1258 EmitFPutC(Char, CI->getOperand(4), B);
1259 return ConstantInt::get(CI->getType(), 1);
1266 //===---------------------------------------===//
1267 // 'fputs' Optimizations
1269 struct VISIBILITY_HIDDEN FPutsOpt : public LibCallOptimization {
1270 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1271 // Require two pointers. Also, we can't optimize if return value is used.
1272 const FunctionType *FT = Callee->getFunctionType();
1273 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1274 !isa<PointerType>(FT->getParamType(1)) ||
1278 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1279 uint64_t Len = GetStringLength(CI->getOperand(1));
1281 EmitFWrite(CI->getOperand(1), ConstantInt::get(TD->getIntPtrType(), Len-1),
1282 CI->getOperand(2), B);
1283 return CI; // Known to have no uses (see above).
1287 //===---------------------------------------===//
1288 // 'fprintf' Optimizations
1290 struct VISIBILITY_HIDDEN FPrintFOpt : public LibCallOptimization {
1291 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
1292 // Require two fixed paramters as pointers and integer result.
1293 const FunctionType *FT = Callee->getFunctionType();
1294 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1295 !isa<PointerType>(FT->getParamType(1)) ||
1296 !isa<IntegerType>(FT->getReturnType()))
1299 // All the optimizations depend on the format string.
1300 std::string FormatStr;
1301 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1304 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1305 if (CI->getNumOperands() == 3) {
1306 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1307 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
1308 return 0; // We found a format specifier.
1310 EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(),
1312 CI->getOperand(1), B);
1313 return ConstantInt::get(CI->getType(), FormatStr.size());
1316 // The remaining optimizations require the format string to be "%s" or "%c"
1317 // and have an extra operand.
1318 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1321 // Decode the second character of the format string.
1322 if (FormatStr[1] == 'c') {
1323 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1324 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1325 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
1326 return ConstantInt::get(CI->getType(), 1);
1329 if (FormatStr[1] == 's') {
1330 // fprintf(F, "%s", str) -> fputs(str, F)
1331 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1333 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1340 } // end anonymous namespace.
1342 //===----------------------------------------------------------------------===//
1343 // SimplifyLibCalls Pass Implementation
1344 //===----------------------------------------------------------------------===//
1347 /// This pass optimizes well known library functions from libc and libm.
1349 class VISIBILITY_HIDDEN SimplifyLibCalls : public FunctionPass {
1350 StringMap<LibCallOptimization*> Optimizations;
1351 // Miscellaneous LibCall Optimizations
1353 // String and Memory LibCall Optimizations
1354 StrCatOpt StrCat; StrChrOpt StrChr; StrCmpOpt StrCmp; StrNCmpOpt StrNCmp;
1355 StrCpyOpt StrCpy; StrLenOpt StrLen; StrToOpt StrTo; MemCmpOpt MemCmp;
1356 MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
1357 // Math Library Optimizations
1358 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
1359 // Integer Optimizations
1360 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1362 // Formatting and IO Optimizations
1363 SPrintFOpt SPrintF; PrintFOpt PrintF;
1364 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
1366 bool Modified; // This is only used by doInitialization.
1368 static char ID; // Pass identification
1369 SimplifyLibCalls() : FunctionPass(&ID) {}
1371 void InitOptimizations();
1372 bool runOnFunction(Function &F);
1374 void setDoesNotAccessMemory(Function &F);
1375 void setOnlyReadsMemory(Function &F);
1376 void setDoesNotThrow(Function &F);
1377 void setDoesNotCapture(Function &F, unsigned n);
1378 void setDoesNotAlias(Function &F, unsigned n);
1379 bool doInitialization(Module &M);
1381 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1382 AU.addRequired<TargetData>();
1385 char SimplifyLibCalls::ID = 0;
1386 } // end anonymous namespace.
1388 static RegisterPass<SimplifyLibCalls>
1389 X("simplify-libcalls", "Simplify well-known library calls");
1391 // Public interface to the Simplify LibCalls pass.
1392 FunctionPass *llvm::createSimplifyLibCallsPass() {
1393 return new SimplifyLibCalls();
1396 /// Optimizations - Populate the Optimizations map with all the optimizations
1398 void SimplifyLibCalls::InitOptimizations() {
1399 // Miscellaneous LibCall Optimizations
1400 Optimizations["exit"] = &Exit;
1402 // String and Memory LibCall Optimizations
1403 Optimizations["strcat"] = &StrCat;
1404 Optimizations["strchr"] = &StrChr;
1405 Optimizations["strcmp"] = &StrCmp;
1406 Optimizations["strncmp"] = &StrNCmp;
1407 Optimizations["strcpy"] = &StrCpy;
1408 Optimizations["strlen"] = &StrLen;
1409 Optimizations["strtol"] = &StrTo;
1410 Optimizations["strtod"] = &StrTo;
1411 Optimizations["strtof"] = &StrTo;
1412 Optimizations["strtoul"] = &StrTo;
1413 Optimizations["strtoll"] = &StrTo;
1414 Optimizations["strtold"] = &StrTo;
1415 Optimizations["strtoull"] = &StrTo;
1416 Optimizations["memcmp"] = &MemCmp;
1417 Optimizations["memcpy"] = &MemCpy;
1418 Optimizations["memmove"] = &MemMove;
1419 Optimizations["memset"] = &MemSet;
1421 // Math Library Optimizations
1422 Optimizations["powf"] = &Pow;
1423 Optimizations["pow"] = &Pow;
1424 Optimizations["powl"] = &Pow;
1425 Optimizations["llvm.pow.f32"] = &Pow;
1426 Optimizations["llvm.pow.f64"] = &Pow;
1427 Optimizations["llvm.pow.f80"] = &Pow;
1428 Optimizations["llvm.pow.f128"] = &Pow;
1429 Optimizations["llvm.pow.ppcf128"] = &Pow;
1430 Optimizations["exp2l"] = &Exp2;
1431 Optimizations["exp2"] = &Exp2;
1432 Optimizations["exp2f"] = &Exp2;
1433 Optimizations["llvm.exp2.ppcf128"] = &Exp2;
1434 Optimizations["llvm.exp2.f128"] = &Exp2;
1435 Optimizations["llvm.exp2.f80"] = &Exp2;
1436 Optimizations["llvm.exp2.f64"] = &Exp2;
1437 Optimizations["llvm.exp2.f32"] = &Exp2;
1440 Optimizations["floor"] = &UnaryDoubleFP;
1443 Optimizations["ceil"] = &UnaryDoubleFP;
1446 Optimizations["round"] = &UnaryDoubleFP;
1449 Optimizations["rint"] = &UnaryDoubleFP;
1451 #ifdef HAVE_NEARBYINTF
1452 Optimizations["nearbyint"] = &UnaryDoubleFP;
1455 // Integer Optimizations
1456 Optimizations["ffs"] = &FFS;
1457 Optimizations["ffsl"] = &FFS;
1458 Optimizations["ffsll"] = &FFS;
1459 Optimizations["abs"] = &Abs;
1460 Optimizations["labs"] = &Abs;
1461 Optimizations["llabs"] = &Abs;
1462 Optimizations["isdigit"] = &IsDigit;
1463 Optimizations["isascii"] = &IsAscii;
1464 Optimizations["toascii"] = &ToAscii;
1466 // Formatting and IO Optimizations
1467 Optimizations["sprintf"] = &SPrintF;
1468 Optimizations["printf"] = &PrintF;
1469 Optimizations["fwrite"] = &FWrite;
1470 Optimizations["fputs"] = &FPuts;
1471 Optimizations["fprintf"] = &FPrintF;
1475 /// runOnFunction - Top level algorithm.
1477 bool SimplifyLibCalls::runOnFunction(Function &F) {
1478 if (Optimizations.empty())
1479 InitOptimizations();
1481 const TargetData &TD = getAnalysis<TargetData>();
1483 IRBuilder<> Builder;
1485 bool Changed = false;
1486 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1487 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1488 // Ignore non-calls.
1489 CallInst *CI = dyn_cast<CallInst>(I++);
1492 // Ignore indirect calls and calls to non-external functions.
1493 Function *Callee = CI->getCalledFunction();
1494 if (Callee == 0 || !Callee->isDeclaration() ||
1495 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1498 // Ignore unknown calls.
1499 const char *CalleeName = Callee->getNameStart();
1500 StringMap<LibCallOptimization*>::iterator OMI =
1501 Optimizations.find(CalleeName, CalleeName+Callee->getNameLen());
1502 if (OMI == Optimizations.end()) continue;
1504 // Set the builder to the instruction after the call.
1505 Builder.SetInsertPoint(BB, I);
1507 // Try to optimize this call.
1508 Value *Result = OMI->second->OptimizeCall(CI, TD, Builder);
1509 if (Result == 0) continue;
1511 DEBUG(DOUT << "SimplifyLibCalls simplified: " << *CI;
1512 DOUT << " into: " << *Result << "\n");
1514 // Something changed!
1518 // Inspect the instruction after the call (which was potentially just
1522 if (CI != Result && !CI->use_empty()) {
1523 CI->replaceAllUsesWith(Result);
1524 if (!Result->hasName())
1525 Result->takeName(CI);
1527 CI->eraseFromParent();
1533 // Utility methods for doInitialization.
1535 void SimplifyLibCalls::setDoesNotAccessMemory(Function &F) {
1536 if (!F.doesNotAccessMemory()) {
1537 F.setDoesNotAccessMemory();
1542 void SimplifyLibCalls::setOnlyReadsMemory(Function &F) {
1543 if (!F.onlyReadsMemory()) {
1544 F.setOnlyReadsMemory();
1549 void SimplifyLibCalls::setDoesNotThrow(Function &F) {
1550 if (!F.doesNotThrow()) {
1551 F.setDoesNotThrow();
1556 void SimplifyLibCalls::setDoesNotCapture(Function &F, unsigned n) {
1557 if (!F.doesNotCapture(n)) {
1558 F.setDoesNotCapture(n);
1563 void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
1564 if (!F.doesNotAlias(n)) {
1565 F.setDoesNotAlias(n);
1571 /// doInitialization - Add attributes to well-known functions.
1573 bool SimplifyLibCalls::doInitialization(Module &M) {
1575 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1577 if (!F.isDeclaration())
1580 unsigned NameLen = F.getNameLen();
1584 const FunctionType *FTy = F.getFunctionType();
1586 const char *NameStr = F.getNameStart();
1587 switch (NameStr[0]) {
1589 if (NameLen == 6 && !strcmp(NameStr, "strlen")) {
1590 if (FTy->getNumParams() != 1 ||
1591 !isa<PointerType>(FTy->getParamType(0)))
1593 setOnlyReadsMemory(F);
1595 setDoesNotCapture(F, 1);
1596 } else if ((NameLen == 6 && !strcmp(NameStr, "strcpy")) ||
1597 (NameLen == 6 && !strcmp(NameStr, "stpcpy")) ||
1598 (NameLen == 6 && !strcmp(NameStr, "strcat")) ||
1599 (NameLen == 6 && !strcmp(NameStr, "strtol")) ||
1600 (NameLen == 6 && !strcmp(NameStr, "strtod")) ||
1601 (NameLen == 6 && !strcmp(NameStr, "strtof")) ||
1602 (NameLen == 7 && !strcmp(NameStr, "strtoul")) ||
1603 (NameLen == 7 && !strcmp(NameStr, "strtoll")) ||
1604 (NameLen == 7 && !strcmp(NameStr, "strtold")) ||
1605 (NameLen == 7 && !strcmp(NameStr, "strncat")) ||
1606 (NameLen == 7 && !strcmp(NameStr, "strncpy")) ||
1607 (NameLen == 8 && !strcmp(NameStr, "strtoull"))) {
1608 if (FTy->getNumParams() < 2 ||
1609 !isa<PointerType>(FTy->getParamType(1)))
1612 setDoesNotCapture(F, 2);
1613 } else if (NameLen == 7 && !strcmp(NameStr, "strxfrm")) {
1614 if (FTy->getNumParams() != 3 ||
1615 !isa<PointerType>(FTy->getParamType(0)) ||
1616 !isa<PointerType>(FTy->getParamType(1)))
1619 setDoesNotCapture(F, 1);
1620 setDoesNotCapture(F, 2);
1621 } else if ((NameLen == 6 && !strcmp(NameStr, "strcmp")) ||
1622 (NameLen == 6 && !strcmp(NameStr, "strspn")) ||
1623 (NameLen == 7 && !strcmp(NameStr, "strncmp")) ||
1624 (NameLen == 7 && !strcmp(NameStr, "strcspn")) ||
1625 (NameLen == 7 && !strcmp(NameStr, "strcoll")) ||
1626 (NameLen == 10 && !strcmp(NameStr, "strcasecmp")) ||
1627 (NameLen == 11 && !strcmp(NameStr, "strncasecmp"))) {
1628 if (FTy->getNumParams() < 2 ||
1629 !isa<PointerType>(FTy->getParamType(0)) ||
1630 !isa<PointerType>(FTy->getParamType(1)))
1632 setOnlyReadsMemory(F);
1634 setDoesNotCapture(F, 1);
1635 setDoesNotCapture(F, 2);
1636 } else if ((NameLen == 6 && !strcmp(NameStr, "strstr")) ||
1637 (NameLen == 7 && !strcmp(NameStr, "strpbrk"))) {
1638 if (FTy->getNumParams() != 2 ||
1639 !isa<PointerType>(FTy->getParamType(1)))
1641 setOnlyReadsMemory(F);
1643 setDoesNotCapture(F, 2);
1644 } else if ((NameLen == 6 && !strcmp(NameStr, "strtok")) ||
1645 (NameLen == 8 && !strcmp(NameStr, "strtok_r"))) {
1646 if (FTy->getNumParams() < 2 ||
1647 !isa<PointerType>(FTy->getParamType(1)))
1650 setDoesNotCapture(F, 2);
1651 } else if ((NameLen == 5 && !strcmp(NameStr, "scanf")) ||
1652 (NameLen == 6 && !strcmp(NameStr, "setbuf")) ||
1653 (NameLen == 7 && !strcmp(NameStr, "setvbuf"))) {
1654 if (FTy->getNumParams() < 1 ||
1655 !isa<PointerType>(FTy->getParamType(0)))
1658 setDoesNotCapture(F, 1);
1659 } else if ((NameLen == 6 && !strcmp(NameStr, "strdup")) ||
1660 (NameLen == 7 && !strcmp(NameStr, "strndup"))) {
1661 if (FTy->getNumParams() < 1 ||
1662 !isa<PointerType>(FTy->getReturnType()) ||
1663 !isa<PointerType>(FTy->getParamType(0)))
1666 setDoesNotAlias(F, 0);
1667 setDoesNotCapture(F, 1);
1668 } else if ((NameLen == 4 && !strcmp(NameStr, "stat")) ||
1669 (NameLen == 6 && !strcmp(NameStr, "sscanf")) ||
1670 (NameLen == 7 && !strcmp(NameStr, "sprintf")) ||
1671 (NameLen == 7 && !strcmp(NameStr, "statvfs"))) {
1672 if (FTy->getNumParams() < 2 ||
1673 !isa<PointerType>(FTy->getParamType(0)) ||
1674 !isa<PointerType>(FTy->getParamType(1)))
1677 setDoesNotCapture(F, 1);
1678 setDoesNotCapture(F, 2);
1679 } else if (NameLen == 8 && !strcmp(NameStr, "snprintf")) {
1680 if (FTy->getNumParams() != 3 ||
1681 !isa<PointerType>(FTy->getParamType(0)) ||
1682 !isa<PointerType>(FTy->getParamType(2)))
1685 setDoesNotCapture(F, 1);
1686 setDoesNotCapture(F, 3);
1687 } else if (NameLen == 9 && !strcmp(NameStr, "setitimer")) {
1688 if (FTy->getNumParams() != 3 ||
1689 !isa<PointerType>(FTy->getParamType(1)) ||
1690 !isa<PointerType>(FTy->getParamType(2)))
1693 setDoesNotCapture(F, 2);
1694 setDoesNotCapture(F, 3);
1695 } else if (NameLen == 6 && !strcmp(NameStr, "system")) {
1696 if (FTy->getNumParams() != 1 ||
1697 !isa<PointerType>(FTy->getParamType(0)))
1699 // May throw; "system" is a valid pthread cancellation point.
1700 setDoesNotCapture(F, 1);
1704 if (NameLen == 6 && !strcmp(NameStr, "memcmp")) {
1705 if (FTy->getNumParams() != 3 ||
1706 !isa<PointerType>(FTy->getParamType(0)) ||
1707 !isa<PointerType>(FTy->getParamType(1)))
1709 setOnlyReadsMemory(F);
1711 setDoesNotCapture(F, 1);
1712 setDoesNotCapture(F, 2);
1713 } else if ((NameLen == 6 && !strcmp(NameStr, "memchr")) ||
1714 (NameLen == 7 && !strcmp(NameStr, "memrchr"))) {
1715 if (FTy->getNumParams() != 3)
1717 setOnlyReadsMemory(F);
1719 } else if ((NameLen == 4 && !strcmp(NameStr, "modf")) ||
1720 (NameLen == 5 && !strcmp(NameStr, "modff")) ||
1721 (NameLen == 5 && !strcmp(NameStr, "modfl")) ||
1722 (NameLen == 6 && !strcmp(NameStr, "memcpy")) ||
1723 (NameLen == 7 && !strcmp(NameStr, "memccpy")) ||
1724 (NameLen == 7 && !strcmp(NameStr, "memmove"))) {
1725 if (FTy->getNumParams() < 2 ||
1726 !isa<PointerType>(FTy->getParamType(1)))
1729 setDoesNotCapture(F, 2);
1730 } else if (NameLen == 8 && !strcmp(NameStr, "memalign")) {
1731 if (!isa<PointerType>(FTy->getReturnType()))
1733 setDoesNotAlias(F, 0);
1734 } else if ((NameLen == 5 && !strcmp(NameStr, "mkdir")) ||
1735 (NameLen == 6 && !strcmp(NameStr, "mktime"))) {
1736 if (FTy->getNumParams() == 0 ||
1737 !isa<PointerType>(FTy->getParamType(0)))
1740 setDoesNotCapture(F, 1);
1744 if (NameLen == 7 && !strcmp(NameStr, "realloc")) {
1745 if (FTy->getNumParams() != 2 ||
1746 !isa<PointerType>(FTy->getParamType(0)) ||
1747 !isa<PointerType>(FTy->getReturnType()))
1750 setDoesNotAlias(F, 0);
1751 setDoesNotCapture(F, 1);
1752 } else if (NameLen == 4 && !strcmp(NameStr, "read")) {
1753 if (FTy->getNumParams() != 3 ||
1754 !isa<PointerType>(FTy->getParamType(1)))
1756 // May throw; "read" is a valid pthread cancellation point.
1757 setDoesNotCapture(F, 2);
1758 } else if ((NameLen == 5 && !strcmp(NameStr, "rmdir")) ||
1759 (NameLen == 6 && !strcmp(NameStr, "rewind")) ||
1760 (NameLen == 6 && !strcmp(NameStr, "remove")) ||
1761 (NameLen == 8 && !strcmp(NameStr, "realpath"))) {
1762 if (FTy->getNumParams() < 1 ||
1763 !isa<PointerType>(FTy->getParamType(0)))
1766 setDoesNotCapture(F, 1);
1767 } else if ((NameLen == 6 && !strcmp(NameStr, "rename")) ||
1768 (NameLen == 8 && !strcmp(NameStr, "readlink"))) {
1769 if (FTy->getNumParams() < 2 ||
1770 !isa<PointerType>(FTy->getParamType(0)) ||
1771 !isa<PointerType>(FTy->getParamType(1)))
1774 setDoesNotCapture(F, 1);
1775 setDoesNotCapture(F, 2);
1779 if (NameLen == 5 && !strcmp(NameStr, "write")) {
1780 if (FTy->getNumParams() != 3 ||
1781 !isa<PointerType>(FTy->getParamType(1)))
1783 // May throw; "write" is a valid pthread cancellation point.
1784 setDoesNotCapture(F, 2);
1788 if (NameLen == 5 && !strcmp(NameStr, "bcopy")) {
1789 if (FTy->getNumParams() != 3 ||
1790 !isa<PointerType>(FTy->getParamType(0)) ||
1791 !isa<PointerType>(FTy->getParamType(1)))
1794 setDoesNotCapture(F, 1);
1795 setDoesNotCapture(F, 2);
1796 } else if (NameLen == 4 && !strcmp(NameStr, "bcmp")) {
1797 if (FTy->getNumParams() != 3 ||
1798 !isa<PointerType>(FTy->getParamType(0)) ||
1799 !isa<PointerType>(FTy->getParamType(1)))
1802 setOnlyReadsMemory(F);
1803 setDoesNotCapture(F, 1);
1804 setDoesNotCapture(F, 2);
1805 } else if (NameLen == 5 && !strcmp(NameStr, "bzero")) {
1806 if (FTy->getNumParams() != 2 ||
1807 !isa<PointerType>(FTy->getParamType(0)))
1810 setDoesNotCapture(F, 1);
1814 if (NameLen == 6 && !strcmp(NameStr, "calloc")) {
1815 if (FTy->getNumParams() != 2 ||
1816 !isa<PointerType>(FTy->getReturnType()))
1819 setDoesNotAlias(F, 0);
1820 } else if ((NameLen == 5 && !strcmp(NameStr, "chmod")) ||
1821 (NameLen == 5 && !strcmp(NameStr, "chown")) ||
1822 (NameLen == 7 && !strcmp(NameStr, "ctermid")) ||
1823 (NameLen == 8 && !strcmp(NameStr, "clearerr")) ||
1824 (NameLen == 8 && !strcmp(NameStr, "closedir"))) {
1825 if (FTy->getNumParams() == 0 ||
1826 !isa<PointerType>(FTy->getParamType(0)))
1829 setDoesNotCapture(F, 1);
1833 if ((NameLen == 4 && !strcmp(NameStr, "atoi")) ||
1834 (NameLen == 4 && !strcmp(NameStr, "atol")) ||
1835 (NameLen == 4 && !strcmp(NameStr, "atof")) ||
1836 (NameLen == 5 && !strcmp(NameStr, "atoll"))) {
1837 if (FTy->getNumParams() != 1 ||
1838 !isa<PointerType>(FTy->getParamType(0)))
1841 setOnlyReadsMemory(F);
1842 setDoesNotCapture(F, 1);
1843 } else if (NameLen == 6 && !strcmp(NameStr, "access")) {
1844 if (FTy->getNumParams() != 2 ||
1845 !isa<PointerType>(FTy->getParamType(0)))
1848 setDoesNotCapture(F, 1);
1852 if (NameLen == 5 && !strcmp(NameStr, "fopen")) {
1853 if (FTy->getNumParams() != 2 ||
1854 !isa<PointerType>(FTy->getReturnType()) ||
1855 !isa<PointerType>(FTy->getParamType(0)) ||
1856 !isa<PointerType>(FTy->getParamType(1)))
1859 setDoesNotAlias(F, 0);
1860 setDoesNotCapture(F, 1);
1861 setDoesNotCapture(F, 2);
1862 } else if (NameLen == 6 && !strcmp(NameStr, "fdopen")) {
1863 if (FTy->getNumParams() != 2 ||
1864 !isa<PointerType>(FTy->getReturnType()) ||
1865 !isa<PointerType>(FTy->getParamType(1)))
1868 setDoesNotAlias(F, 0);
1869 setDoesNotCapture(F, 2);
1870 } else if ((NameLen == 4 && !strcmp(NameStr, "feof")) ||
1871 (NameLen == 4 && !strcmp(NameStr, "free")) ||
1872 (NameLen == 5 && !strcmp(NameStr, "fseek")) ||
1873 (NameLen == 5 && !strcmp(NameStr, "ftell")) ||
1874 (NameLen == 5 && !strcmp(NameStr, "fgetc")) ||
1875 (NameLen == 6 && !strcmp(NameStr, "fseeko")) ||
1876 (NameLen == 6 && !strcmp(NameStr, "ftello")) ||
1877 (NameLen == 6 && !strcmp(NameStr, "fileno")) ||
1878 (NameLen == 6 && !strcmp(NameStr, "fflush")) ||
1879 (NameLen == 6 && !strcmp(NameStr, "fclose")) ||
1880 (NameLen == 7 && !strcmp(NameStr, "fsetpos")) ||
1881 (NameLen == 9 && !strcmp(NameStr, "flockfile")) ||
1882 (NameLen == 11 && !strcmp(NameStr, "funlockfile")) ||
1883 (NameLen == 12 && !strcmp(NameStr, "ftrylockfile"))) {
1884 if (FTy->getNumParams() == 0 ||
1885 !isa<PointerType>(FTy->getParamType(0)))
1888 setDoesNotCapture(F, 1);
1889 } else if (NameLen == 6 && !strcmp(NameStr, "ferror")) {
1890 if (FTy->getNumParams() != 1 ||
1891 !isa<PointerType>(FTy->getParamType(0)))
1894 setDoesNotCapture(F, 1);
1895 setOnlyReadsMemory(F);
1896 } else if ((NameLen == 5 && !strcmp(NameStr, "fputc")) ||
1897 (NameLen == 5 && !strcmp(NameStr, "fstat")) ||
1898 (NameLen == 5 && !strcmp(NameStr, "frexp")) ||
1899 (NameLen == 6 && !strcmp(NameStr, "frexpf")) ||
1900 (NameLen == 6 && !strcmp(NameStr, "frexpl")) ||
1901 (NameLen == 8 && !strcmp(NameStr, "fstatvfs"))) {
1902 if (FTy->getNumParams() != 2 ||
1903 !isa<PointerType>(FTy->getParamType(1)))
1906 setDoesNotCapture(F, 2);
1907 } else if (NameLen == 5 && !strcmp(NameStr, "fgets")) {
1908 if (FTy->getNumParams() != 3 ||
1909 !isa<PointerType>(FTy->getParamType(0)) ||
1910 !isa<PointerType>(FTy->getParamType(2)))
1913 setDoesNotCapture(F, 3);
1914 } else if ((NameLen == 5 && !strcmp(NameStr, "fread")) ||
1915 (NameLen == 6 && !strcmp(NameStr, "fwrite"))) {
1916 if (FTy->getNumParams() != 4 ||
1917 !isa<PointerType>(FTy->getParamType(0)) ||
1918 !isa<PointerType>(FTy->getParamType(3)))
1921 setDoesNotCapture(F, 1);
1922 setDoesNotCapture(F, 4);
1923 } else if ((NameLen == 5 && !strcmp(NameStr, "fputs")) ||
1924 (NameLen == 6 && !strcmp(NameStr, "fscanf")) ||
1925 (NameLen == 7 && !strcmp(NameStr, "fprintf")) ||
1926 (NameLen == 7 && !strcmp(NameStr, "fgetpos"))) {
1927 if (FTy->getNumParams() < 2 ||
1928 !isa<PointerType>(FTy->getParamType(0)) ||
1929 !isa<PointerType>(FTy->getParamType(1)))
1932 setDoesNotCapture(F, 1);
1933 setDoesNotCapture(F, 2);
1937 if ((NameLen == 4 && !strcmp(NameStr, "getc")) ||
1938 (NameLen == 10 && !strcmp(NameStr, "getlogin_r")) ||
1939 (NameLen == 13 && !strcmp(NameStr, "getc_unlocked"))) {
1940 if (FTy->getNumParams() == 0 ||
1941 !isa<PointerType>(FTy->getParamType(0)))
1944 setDoesNotCapture(F, 1);
1945 } else if (NameLen == 6 && !strcmp(NameStr, "getenv")) {
1946 if (FTy->getNumParams() != 1 ||
1947 !isa<PointerType>(FTy->getParamType(0)))
1950 setOnlyReadsMemory(F);
1951 setDoesNotCapture(F, 1);
1952 } else if ((NameLen == 4 && !strcmp(NameStr, "gets")) ||
1953 (NameLen == 7 && !strcmp(NameStr, "getchar"))) {
1955 } else if (NameLen == 9 && !strcmp(NameStr, "getitimer")) {
1956 if (FTy->getNumParams() != 2 ||
1957 !isa<PointerType>(FTy->getParamType(1)))
1960 setDoesNotCapture(F, 2);
1961 } else if (NameLen == 8 && !strcmp(NameStr, "getpwnam")) {
1962 if (FTy->getNumParams() != 1 ||
1963 !isa<PointerType>(FTy->getParamType(0)))
1966 setDoesNotCapture(F, 1);
1970 if (NameLen == 6 && !strcmp(NameStr, "ungetc")) {
1971 if (FTy->getNumParams() != 2 ||
1972 !isa<PointerType>(FTy->getParamType(1)))
1975 setDoesNotCapture(F, 2);
1976 } else if ((NameLen == 5 && !strcmp(NameStr, "uname")) ||
1977 (NameLen == 6 && !strcmp(NameStr, "unlink")) ||
1978 (NameLen == 8 && !strcmp(NameStr, "unsetenv"))) {
1979 if (FTy->getNumParams() != 1 ||
1980 !isa<PointerType>(FTy->getParamType(0)))
1983 setDoesNotCapture(F, 1);
1984 } else if ((NameLen == 5 && !strcmp(NameStr, "utime")) ||
1985 (NameLen == 6 && !strcmp(NameStr, "utimes"))) {
1986 if (FTy->getNumParams() != 2 ||
1987 !isa<PointerType>(FTy->getParamType(0)) ||
1988 !isa<PointerType>(FTy->getParamType(1)))
1991 setDoesNotCapture(F, 1);
1992 setDoesNotCapture(F, 2);
1996 if (NameLen == 4 && !strcmp(NameStr, "putc")) {
1997 if (FTy->getNumParams() != 2 ||
1998 !isa<PointerType>(FTy->getParamType(1)))
2001 setDoesNotCapture(F, 2);
2002 } else if ((NameLen == 4 && !strcmp(NameStr, "puts")) ||
2003 (NameLen == 6 && !strcmp(NameStr, "printf")) ||
2004 (NameLen == 6 && !strcmp(NameStr, "perror"))) {
2005 if (FTy->getNumParams() != 1 ||
2006 !isa<PointerType>(FTy->getParamType(0)))
2009 setDoesNotCapture(F, 1);
2010 } else if ((NameLen == 5 && !strcmp(NameStr, "pread")) ||
2011 (NameLen == 6 && !strcmp(NameStr, "pwrite"))) {
2012 if (FTy->getNumParams() != 4 ||
2013 !isa<PointerType>(FTy->getParamType(1)))
2015 // May throw; these are valid pthread cancellation points.
2016 setDoesNotCapture(F, 2);
2017 } else if (NameLen == 7 && !strcmp(NameStr, "putchar")) {
2019 } else if (NameLen == 5 && !strcmp(NameStr, "popen")) {
2020 if (FTy->getNumParams() != 2 ||
2021 !isa<PointerType>(FTy->getReturnType()) ||
2022 !isa<PointerType>(FTy->getParamType(0)) ||
2023 !isa<PointerType>(FTy->getParamType(1)))
2026 setDoesNotAlias(F, 0);
2027 setDoesNotCapture(F, 1);
2028 setDoesNotCapture(F, 2);
2029 } else if (NameLen == 6 && !strcmp(NameStr, "pclose")) {
2030 if (FTy->getNumParams() != 1 ||
2031 !isa<PointerType>(FTy->getParamType(0)))
2034 setDoesNotCapture(F, 1);
2038 if (NameLen == 6 && !strcmp(NameStr, "vscanf")) {
2039 if (FTy->getNumParams() != 2 ||
2040 !isa<PointerType>(FTy->getParamType(1)))
2043 setDoesNotCapture(F, 1);
2044 } else if ((NameLen == 7 && !strcmp(NameStr, "vsscanf")) ||
2045 (NameLen == 7 && !strcmp(NameStr, "vfscanf"))) {
2046 if (FTy->getNumParams() != 3 ||
2047 !isa<PointerType>(FTy->getParamType(1)) ||
2048 !isa<PointerType>(FTy->getParamType(2)))
2051 setDoesNotCapture(F, 1);
2052 setDoesNotCapture(F, 2);
2053 } else if (NameLen == 6 && !strcmp(NameStr, "valloc")) {
2054 if (!isa<PointerType>(FTy->getReturnType()))
2057 setDoesNotAlias(F, 0);
2058 } else if (NameLen == 7 && !strcmp(NameStr, "vprintf")) {
2059 if (FTy->getNumParams() != 2 ||
2060 !isa<PointerType>(FTy->getParamType(0)))
2063 setDoesNotCapture(F, 1);
2064 } else if ((NameLen == 8 && !strcmp(NameStr, "vfprintf")) ||
2065 (NameLen == 8 && !strcmp(NameStr, "vsprintf"))) {
2066 if (FTy->getNumParams() != 3 ||
2067 !isa<PointerType>(FTy->getParamType(0)) ||
2068 !isa<PointerType>(FTy->getParamType(1)))
2071 setDoesNotCapture(F, 1);
2072 setDoesNotCapture(F, 2);
2073 } else if (NameLen == 9 && !strcmp(NameStr, "vsnprintf")) {
2074 if (FTy->getNumParams() != 4 ||
2075 !isa<PointerType>(FTy->getParamType(0)) ||
2076 !isa<PointerType>(FTy->getParamType(2)))
2079 setDoesNotCapture(F, 1);
2080 setDoesNotCapture(F, 3);
2084 if (NameLen == 4 && !strcmp(NameStr, "open")) {
2085 if (FTy->getNumParams() < 2 ||
2086 !isa<PointerType>(FTy->getParamType(0)))
2088 // May throw; "open" is a valid pthread cancellation point.
2089 setDoesNotCapture(F, 1);
2090 } else if (NameLen == 7 && !strcmp(NameStr, "opendir")) {
2091 if (FTy->getNumParams() != 1 ||
2092 !isa<PointerType>(FTy->getReturnType()) ||
2093 !isa<PointerType>(FTy->getParamType(0)))
2096 setDoesNotAlias(F, 0);
2097 setDoesNotCapture(F, 1);
2101 if (NameLen == 7 && !strcmp(NameStr, "tmpfile")) {
2102 if (!isa<PointerType>(FTy->getReturnType()))
2105 setDoesNotAlias(F, 0);
2106 } else if (NameLen == 5 && !strcmp(NameStr, "times")) {
2107 if (FTy->getNumParams() != 1 ||
2108 !isa<PointerType>(FTy->getParamType(0)))
2111 setDoesNotCapture(F, 1);
2115 if ((NameLen == 5 && !strcmp(NameStr, "htonl")) ||
2116 (NameLen == 5 && !strcmp(NameStr, "htons"))) {
2118 setDoesNotAccessMemory(F);
2122 if ((NameLen == 5 && !strcmp(NameStr, "ntohl")) ||
2123 (NameLen == 5 && !strcmp(NameStr, "ntohs"))) {
2125 setDoesNotAccessMemory(F);
2129 if (NameLen == 5 && !strcmp(NameStr, "lstat")) {
2130 if (FTy->getNumParams() != 2 ||
2131 !isa<PointerType>(FTy->getParamType(0)) ||
2132 !isa<PointerType>(FTy->getParamType(1)))
2135 setDoesNotCapture(F, 1);
2136 setDoesNotCapture(F, 2);
2137 } else if (NameLen == 6 && !strcmp(NameStr, "lchown")) {
2138 if (FTy->getNumParams() != 3 ||
2139 !isa<PointerType>(FTy->getParamType(0)))
2142 setDoesNotCapture(F, 1);
2146 if (NameLen == 5 && !strcmp(NameStr, "qsort")) {
2147 if (FTy->getNumParams() != 4 ||
2148 !isa<PointerType>(FTy->getParamType(3)))
2150 // May throw; places call through function pointer.
2151 setDoesNotCapture(F, 4);
2155 if ((NameLen == 8 && !strcmp(NameStr, "__strdup")) ||
2156 (NameLen == 9 && !strcmp(NameStr, "__strndup"))) {
2157 if (FTy->getNumParams() < 1 ||
2158 !isa<PointerType>(FTy->getReturnType()) ||
2159 !isa<PointerType>(FTy->getParamType(0)))
2162 setDoesNotAlias(F, 0);
2163 setDoesNotCapture(F, 1);
2164 } else if (NameLen == 10 && !strcmp(NameStr, "__strtok_r")) {
2165 if (FTy->getNumParams() != 3 ||
2166 !isa<PointerType>(FTy->getParamType(1)))
2169 setDoesNotCapture(F, 2);
2170 } else if (NameLen == 8 && !strcmp(NameStr, "_IO_getc")) {
2171 if (FTy->getNumParams() != 1 ||
2172 !isa<PointerType>(FTy->getParamType(0)))
2175 setDoesNotCapture(F, 1);
2176 } else if (NameLen == 8 && !strcmp(NameStr, "_IO_putc")) {
2177 if (FTy->getNumParams() != 2 ||
2178 !isa<PointerType>(FTy->getParamType(1)))
2181 setDoesNotCapture(F, 2);
2185 if (NameLen == 15 && !strcmp(NameStr, "\1__isoc99_scanf")) {
2186 if (FTy->getNumParams() < 1 ||
2187 !isa<PointerType>(FTy->getParamType(0)))
2190 setDoesNotCapture(F, 1);
2191 } else if ((NameLen == 7 && !strcmp(NameStr, "\1stat64")) ||
2192 (NameLen == 8 && !strcmp(NameStr, "\1lstat64")) ||
2193 (NameLen == 10 && !strcmp(NameStr, "\1statvfs64")) ||
2194 (NameLen == 16 && !strcmp(NameStr, "\1__isoc99_sscanf"))) {
2195 if (FTy->getNumParams() < 1 ||
2196 !isa<PointerType>(FTy->getParamType(0)) ||
2197 !isa<PointerType>(FTy->getParamType(1)))
2200 setDoesNotCapture(F, 1);
2201 setDoesNotCapture(F, 2);
2202 } else if (NameLen == 8 && !strcmp(NameStr, "\1fopen64")) {
2203 if (FTy->getNumParams() != 2 ||
2204 !isa<PointerType>(FTy->getReturnType()) ||
2205 !isa<PointerType>(FTy->getParamType(0)) ||
2206 !isa<PointerType>(FTy->getParamType(1)))
2209 setDoesNotAlias(F, 0);
2210 setDoesNotCapture(F, 1);
2211 setDoesNotCapture(F, 2);
2212 } else if ((NameLen == 9 && !strcmp(NameStr, "\1fseeko64")) ||
2213 (NameLen == 9 && !strcmp(NameStr, "\1ftello64"))) {
2214 if (FTy->getNumParams() == 0 ||
2215 !isa<PointerType>(FTy->getParamType(0)))
2218 setDoesNotCapture(F, 1);
2219 } else if (NameLen == 10 && !strcmp(NameStr, "\1tmpfile64")) {
2220 if (!isa<PointerType>(FTy->getReturnType()))
2223 setDoesNotAlias(F, 0);
2224 } else if ((NameLen == 8 && !strcmp(NameStr, "\1fstat64")) ||
2225 (NameLen == 11 && !strcmp(NameStr, "\1fstatvfs64"))) {
2226 if (FTy->getNumParams() != 2 ||
2227 !isa<PointerType>(FTy->getParamType(1)))
2230 setDoesNotCapture(F, 2);
2231 } else if (NameLen == 7 && !strcmp(NameStr, "\1open64")) {
2232 if (FTy->getNumParams() < 2 ||
2233 !isa<PointerType>(FTy->getParamType(0)))
2235 // May throw; "open" is a valid pthread cancellation point.
2236 setDoesNotCapture(F, 1);
2245 // Additional cases that we need to add to this file:
2248 // * cbrt(expN(X)) -> expN(x/3)
2249 // * cbrt(sqrt(x)) -> pow(x,1/6)
2250 // * cbrt(sqrt(x)) -> pow(x,1/9)
2253 // * cos(-x) -> cos(x)
2256 // * exp(log(x)) -> x
2259 // * log(exp(x)) -> x
2260 // * log(x**y) -> y*log(x)
2261 // * log(exp(y)) -> y*log(e)
2262 // * log(exp2(y)) -> y*log(2)
2263 // * log(exp10(y)) -> y*log(10)
2264 // * log(sqrt(x)) -> 0.5*log(x)
2265 // * log(pow(x,y)) -> y*log(x)
2267 // lround, lroundf, lroundl:
2268 // * lround(cnst) -> cnst'
2271 // * memcmp(x,y,l) -> cnst
2272 // (if all arguments are constant and strlen(x) <= l and strlen(y) <= l)
2275 // * pow(exp(x),y) -> exp(x*y)
2276 // * pow(sqrt(x),y) -> pow(x,y*0.5)
2277 // * pow(pow(x,y),z)-> pow(x,y*z)
2280 // * puts("") -> putchar("\n")
2282 // round, roundf, roundl:
2283 // * round(cnst) -> cnst'
2286 // * signbit(cnst) -> cnst'
2287 // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
2289 // sqrt, sqrtf, sqrtl:
2290 // * sqrt(expN(x)) -> expN(x*0.5)
2291 // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
2292 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
2295 // * stpcpy(str, "literal") ->
2296 // llvm.memcpy(str,"literal",strlen("literal")+1,1)
2298 // * strrchr(s,c) -> reverse_offset_of_in(c,s)
2299 // (if c is a constant integer and s is a constant string)
2300 // * strrchr(s1,0) -> strchr(s1,0)
2303 // * strncat(x,y,0) -> x
2304 // * strncat(x,y,0) -> x (if strlen(y) = 0)
2305 // * strncat(x,y,l) -> strcat(x,y) (if y and l are constants an l > strlen(y))
2308 // * strncpy(d,s,0) -> d
2309 // * strncpy(d,s,l) -> memcpy(d,s,l,1)
2310 // (if s and l are constants)
2313 // * strpbrk(s,a) -> offset_in_for(s,a)
2314 // (if s and a are both constant strings)
2315 // * strpbrk(s,"") -> 0
2316 // * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
2319 // * strspn(s,a) -> const_int (if both args are constant)
2320 // * strspn("",a) -> 0
2321 // * strspn(s,"") -> 0
2322 // * strcspn(s,a) -> const_int (if both args are constant)
2323 // * strcspn("",a) -> 0
2324 // * strcspn(s,"") -> strlen(a)
2327 // * strstr(x,x) -> x
2328 // * strstr(s1,s2) -> offset_of_s2_in(s1)
2329 // (if s1 and s2 are constant strings)
2332 // * tan(atan(x)) -> x
2334 // trunc, truncf, truncl:
2335 // * trunc(cnst) -> cnst'