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");
38 //===----------------------------------------------------------------------===//
39 // Optimizer Base Class
40 //===----------------------------------------------------------------------===//
42 /// This class is the abstract base class for the set of optimizations that
43 /// corresponds to one library call.
45 class VISIBILITY_HIDDEN LibCallOptimization {
50 LibCallOptimization() { }
51 virtual ~LibCallOptimization() {}
53 /// CallOptimizer - This pure virtual method is implemented by base classes to
54 /// do various optimizations. If this returns null then no transformation was
55 /// performed. If it returns CI, then it transformed the call and CI is to be
56 /// deleted. If it returns something else, replace CI with the new value and
58 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) =0;
60 Value *OptimizeCall(CallInst *CI, const TargetData &TD, IRBuilder &B) {
61 Caller = CI->getParent()->getParent();
63 return CallOptimizer(CI->getCalledFunction(), CI, B);
66 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
67 Value *CastToCStr(Value *V, IRBuilder &B);
69 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
70 /// specified pointer. Ptr is required to be some pointer type, and the
71 /// return value has 'intptr_t' type.
72 Value *EmitStrLen(Value *Ptr, IRBuilder &B);
74 /// EmitMemCpy - Emit a call to the memcpy function to the builder. This
75 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
76 Value *EmitMemCpy(Value *Dst, Value *Src, Value *Len,
77 unsigned Align, IRBuilder &B);
79 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
80 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
81 Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder &B);
83 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
84 /// 'floor'). This function is known to take a single of type matching 'Op'
85 /// and returns one value with the same type. If 'Op' is a long double, 'l'
86 /// is added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
87 Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder &B);
89 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
91 void EmitPutChar(Value *Char, IRBuilder &B);
93 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
95 void EmitPutS(Value *Str, IRBuilder &B);
97 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
98 /// an i32, and File is a pointer to FILE.
99 void EmitFPutC(Value *Char, Value *File, IRBuilder &B);
101 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
102 /// pointer and File is a pointer to FILE.
103 void EmitFPutS(Value *Str, Value *File, IRBuilder &B);
105 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
106 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
107 void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder &B);
110 } // End anonymous namespace.
112 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
113 Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder &B) {
114 return B.CreateBitCast(V, PointerType::getUnqual(Type::Int8Ty), "cstr");
117 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
118 /// specified pointer. This always returns an integer value of size intptr_t.
119 Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder &B) {
120 Module *M = Caller->getParent();
121 Constant *StrLen =M->getOrInsertFunction("strlen", TD->getIntPtrType(),
122 PointerType::getUnqual(Type::Int8Ty),
124 return B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
127 /// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
128 /// expects that the size has type 'intptr_t' and Dst/Src are pointers.
129 Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
130 unsigned Align, IRBuilder &B) {
131 Module *M = Caller->getParent();
132 Intrinsic::ID IID = Len->getType() == Type::Int32Ty ?
133 Intrinsic::memcpy_i32 : Intrinsic::memcpy_i64;
134 Value *MemCpy = Intrinsic::getDeclaration(M, IID);
135 return B.CreateCall4(MemCpy, CastToCStr(Dst, B), CastToCStr(Src, B), Len,
136 ConstantInt::get(Type::Int32Ty, Align));
139 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
140 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
141 Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
142 Value *Len, IRBuilder &B) {
143 Module *M = Caller->getParent();
144 Value *MemChr = M->getOrInsertFunction("memchr",
145 PointerType::getUnqual(Type::Int8Ty),
146 PointerType::getUnqual(Type::Int8Ty),
147 Type::Int32Ty, TD->getIntPtrType(),
149 return B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
152 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
153 /// 'floor'). This function is known to take a single of type matching 'Op' and
154 /// returns one value with the same type. If 'Op' is a long double, 'l' is
155 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
156 Value *LibCallOptimization::EmitUnaryFloatFnCall(Value *Op, const char *Name,
159 if (Op->getType() != Type::DoubleTy) {
160 // If we need to add a suffix, copy into NameBuffer.
161 unsigned NameLen = strlen(Name);
162 assert(NameLen < sizeof(NameBuffer)-2);
163 memcpy(NameBuffer, Name, NameLen);
164 if (Op->getType() == Type::FloatTy)
165 NameBuffer[NameLen] = 'f'; // floorf
167 NameBuffer[NameLen] = 'l'; // floorl
168 NameBuffer[NameLen+1] = 0;
172 Module *M = Caller->getParent();
173 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
174 Op->getType(), NULL);
175 return B.CreateCall(Callee, Op, Name);
178 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
180 void LibCallOptimization::EmitPutChar(Value *Char, IRBuilder &B) {
181 Module *M = Caller->getParent();
182 Value *F = M->getOrInsertFunction("putchar", Type::Int32Ty,
183 Type::Int32Ty, NULL);
184 B.CreateCall(F, B.CreateIntCast(Char, Type::Int32Ty, "chari"), "putchar");
187 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
189 void LibCallOptimization::EmitPutS(Value *Str, IRBuilder &B) {
190 Module *M = Caller->getParent();
191 Value *F = M->getOrInsertFunction("puts", Type::Int32Ty,
192 PointerType::getUnqual(Type::Int8Ty), NULL);
193 B.CreateCall(F, CastToCStr(Str, B), "puts");
196 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
197 /// an integer and File is a pointer to FILE.
198 void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder &B) {
199 Module *M = Caller->getParent();
200 Constant *F = M->getOrInsertFunction("fputc", Type::Int32Ty, Type::Int32Ty,
201 File->getType(), NULL);
202 Char = B.CreateIntCast(Char, Type::Int32Ty, "chari");
203 B.CreateCall2(F, Char, File, "fputc");
206 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
207 /// pointer and File is a pointer to FILE.
208 void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder &B) {
209 Module *M = Caller->getParent();
210 Constant *F = M->getOrInsertFunction("fputs", Type::Int32Ty,
211 PointerType::getUnqual(Type::Int8Ty),
212 File->getType(), NULL);
213 B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
216 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
217 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
218 void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
220 Module *M = Caller->getParent();
221 Constant *F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(),
222 PointerType::getUnqual(Type::Int8Ty),
223 TD->getIntPtrType(), TD->getIntPtrType(),
224 File->getType(), NULL);
225 B.CreateCall4(F, CastToCStr(Ptr, B), Size,
226 ConstantInt::get(TD->getIntPtrType(), 1), File);
229 //===----------------------------------------------------------------------===//
231 //===----------------------------------------------------------------------===//
233 /// GetStringLengthH - If we can compute the length of the string pointed to by
234 /// the specified pointer, return 'len+1'. If we can't, return 0.
235 static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
236 // Look through noop bitcast instructions.
237 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
238 return GetStringLengthH(BCI->getOperand(0), PHIs);
240 // If this is a PHI node, there are two cases: either we have already seen it
242 if (PHINode *PN = dyn_cast<PHINode>(V)) {
243 if (!PHIs.insert(PN))
244 return ~0ULL; // already in the set.
246 // If it was new, see if all the input strings are the same length.
247 uint64_t LenSoFar = ~0ULL;
248 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
249 uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
250 if (Len == 0) return 0; // Unknown length -> unknown.
252 if (Len == ~0ULL) continue;
254 if (Len != LenSoFar && LenSoFar != ~0ULL)
255 return 0; // Disagree -> unknown.
259 // Success, all agree.
263 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
264 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
265 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
266 if (Len1 == 0) return 0;
267 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
268 if (Len2 == 0) return 0;
269 if (Len1 == ~0ULL) return Len2;
270 if (Len2 == ~0ULL) return Len1;
271 if (Len1 != Len2) return 0;
275 // If the value is not a GEP instruction nor a constant expression with a
276 // GEP instruction, then return unknown.
278 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
280 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
281 if (CE->getOpcode() != Instruction::GetElementPtr)
288 // Make sure the GEP has exactly three arguments.
289 if (GEP->getNumOperands() != 3)
292 // Check to make sure that the first operand of the GEP is an integer and
293 // has value 0 so that we are sure we're indexing into the initializer.
294 if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
300 // If the second index isn't a ConstantInt, then this is a variable index
301 // into the array. If this occurs, we can't say anything meaningful about
303 uint64_t StartIdx = 0;
304 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
305 StartIdx = CI->getZExtValue();
309 // The GEP instruction, constant or instruction, must reference a global
310 // variable that is a constant and is initialized. The referenced constant
311 // initializer is the array that we'll use for optimization.
312 GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
313 if (!GV || !GV->isConstant() || !GV->hasInitializer())
315 Constant *GlobalInit = GV->getInitializer();
317 // Handle the ConstantAggregateZero case, which is a degenerate case. The
318 // initializer is constant zero so the length of the string must be zero.
319 if (isa<ConstantAggregateZero>(GlobalInit))
320 return 1; // Len = 0 offset by 1.
322 // Must be a Constant Array
323 ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
324 if (!Array || Array->getType()->getElementType() != Type::Int8Ty)
327 // Get the number of elements in the array
328 uint64_t NumElts = Array->getType()->getNumElements();
330 // Traverse the constant array from StartIdx (derived above) which is
331 // the place the GEP refers to in the array.
332 for (unsigned i = StartIdx; i != NumElts; ++i) {
333 Constant *Elt = Array->getOperand(i);
334 ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
335 if (!CI) // This array isn't suitable, non-int initializer.
338 return i-StartIdx+1; // We found end of string, success!
341 return 0; // The array isn't null terminated, conservatively return 'unknown'.
344 /// GetStringLength - If we can compute the length of the string pointed to by
345 /// the specified pointer, return 'len+1'. If we can't, return 0.
346 static uint64_t GetStringLength(Value *V) {
347 if (!isa<PointerType>(V->getType())) return 0;
349 SmallPtrSet<PHINode*, 32> PHIs;
350 uint64_t Len = GetStringLengthH(V, PHIs);
351 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
352 // an empty string as a length.
353 return Len == ~0ULL ? 1 : Len;
356 /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
357 /// value is equal or not-equal to zero.
358 static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
359 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
361 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
362 if (IC->isEquality())
363 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
364 if (C->isNullValue())
366 // Unknown instruction.
372 //===----------------------------------------------------------------------===//
373 // Miscellaneous LibCall Optimizations
374 //===----------------------------------------------------------------------===//
377 //===---------------------------------------===//
378 // 'exit' Optimizations
380 /// ExitOpt - int main() { exit(4); } --> int main() { return 4; }
381 struct VISIBILITY_HIDDEN ExitOpt : public LibCallOptimization {
382 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
383 // Verify we have a reasonable prototype for exit.
384 if (Callee->arg_size() == 0 || !CI->use_empty())
387 // Verify the caller is main, and that the result type of main matches the
388 // argument type of exit.
389 if (!Caller->isName("main") || !Caller->hasExternalLinkage() ||
390 Caller->getReturnType() != CI->getOperand(1)->getType())
393 TerminatorInst *OldTI = CI->getParent()->getTerminator();
395 // Create the return after the call.
396 ReturnInst *RI = B.CreateRet(CI->getOperand(1));
398 // Drop all successor phi node entries.
399 for (unsigned i = 0, e = OldTI->getNumSuccessors(); i != e; ++i)
400 OldTI->getSuccessor(i)->removePredecessor(CI->getParent());
402 // Erase all instructions from after our return instruction until the end of
404 BasicBlock::iterator FirstDead = RI; ++FirstDead;
405 CI->getParent()->getInstList().erase(FirstDead, CI->getParent()->end());
410 //===----------------------------------------------------------------------===//
411 // String and Memory LibCall Optimizations
412 //===----------------------------------------------------------------------===//
414 //===---------------------------------------===//
415 // 'strcat' Optimizations
417 struct VISIBILITY_HIDDEN StrCatOpt : public LibCallOptimization {
418 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
419 // Verify the "strcat" function prototype.
420 const FunctionType *FT = Callee->getFunctionType();
421 if (FT->getNumParams() != 2 ||
422 FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
423 FT->getParamType(0) != FT->getReturnType() ||
424 FT->getParamType(1) != FT->getReturnType())
427 // Extract some information from the instruction
428 Value *Dst = CI->getOperand(1);
429 Value *Src = CI->getOperand(2);
431 // See if we can get the length of the input string.
432 uint64_t Len = GetStringLength(Src);
433 if (Len == 0) return 0;
434 --Len; // Unbias length.
436 // Handle the simple, do-nothing case: strcat(x, "") -> x
440 // We need to find the end of the destination string. That's where the
441 // memory is to be moved to. We just generate a call to strlen.
442 Value *DstLen = EmitStrLen(Dst, B);
444 // Now that we have the destination's length, we must index into the
445 // destination's pointer to get the actual memcpy destination (end of
446 // the string .. we're concatenating).
447 Dst = B.CreateGEP(Dst, DstLen, "endptr");
449 // We have enough information to now generate the memcpy call to do the
450 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
451 EmitMemCpy(Dst, Src, ConstantInt::get(TD->getIntPtrType(), Len+1), 1, B);
456 //===---------------------------------------===//
457 // 'strchr' Optimizations
459 struct VISIBILITY_HIDDEN StrChrOpt : public LibCallOptimization {
460 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
461 // Verify the "strchr" function prototype.
462 const FunctionType *FT = Callee->getFunctionType();
463 if (FT->getNumParams() != 2 ||
464 FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
465 FT->getParamType(0) != FT->getReturnType())
468 Value *SrcStr = CI->getOperand(1);
470 // If the second operand is non-constant, see if we can compute the length
471 // of the input string and turn this into memchr.
472 ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
474 uint64_t Len = GetStringLength(SrcStr);
475 if (Len == 0 || FT->getParamType(1) != Type::Int32Ty) // memchr needs i32.
478 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
479 ConstantInt::get(TD->getIntPtrType(), Len), B);
482 // Otherwise, the character is a constant, see if the first argument is
483 // a string literal. If so, we can constant fold.
485 if (!GetConstantStringInfo(SrcStr, Str))
488 // strchr can find the nul character.
490 char CharValue = CharC->getSExtValue();
492 // Compute the offset.
495 if (i == Str.size()) // Didn't find the char. strchr returns null.
496 return Constant::getNullValue(CI->getType());
497 // Did we find our match?
498 if (Str[i] == CharValue)
503 // strchr(s+n,c) -> gep(s+n+i,c)
504 Value *Idx = ConstantInt::get(Type::Int64Ty, i);
505 return B.CreateGEP(SrcStr, Idx, "strchr");
509 //===---------------------------------------===//
510 // 'strcmp' Optimizations
512 struct VISIBILITY_HIDDEN StrCmpOpt : public LibCallOptimization {
513 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
514 // Verify the "strcmp" function prototype.
515 const FunctionType *FT = Callee->getFunctionType();
516 if (FT->getNumParams() != 2 || FT->getReturnType() != Type::Int32Ty ||
517 FT->getParamType(0) != FT->getParamType(1) ||
518 FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty))
521 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
522 if (Str1P == Str2P) // strcmp(x,x) -> 0
523 return ConstantInt::get(CI->getType(), 0);
525 std::string Str1, Str2;
526 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
527 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
529 if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
530 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
532 if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
533 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
535 // strcmp(x, y) -> cnst (if both x and y are constant strings)
536 if (HasStr1 && HasStr2)
537 return ConstantInt::get(CI->getType(), strcmp(Str1.c_str(),Str2.c_str()));
542 //===---------------------------------------===//
543 // 'strncmp' Optimizations
545 struct VISIBILITY_HIDDEN StrNCmpOpt : public LibCallOptimization {
546 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
547 // Verify the "strncmp" function prototype.
548 const FunctionType *FT = Callee->getFunctionType();
549 if (FT->getNumParams() != 3 || FT->getReturnType() != Type::Int32Ty ||
550 FT->getParamType(0) != FT->getParamType(1) ||
551 FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
552 !isa<IntegerType>(FT->getParamType(2)))
555 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
556 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
557 return ConstantInt::get(CI->getType(), 0);
559 // Get the length argument if it is constant.
561 if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
562 Length = LengthArg->getZExtValue();
566 if (Length == 0) // strncmp(x,y,0) -> 0
567 return ConstantInt::get(CI->getType(), 0);
569 std::string Str1, Str2;
570 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
571 bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
573 if (HasStr1 && Str1.empty()) // strncmp("", x, n) -> *x
574 return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
576 if (HasStr2 && Str2.empty()) // strncmp(x, "", n) -> *x
577 return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
579 // strncmp(x, y) -> cnst (if both x and y are constant strings)
580 if (HasStr1 && HasStr2)
581 return ConstantInt::get(CI->getType(),
582 strncmp(Str1.c_str(), Str2.c_str(), Length));
588 //===---------------------------------------===//
589 // 'strcpy' Optimizations
591 struct VISIBILITY_HIDDEN StrCpyOpt : public LibCallOptimization {
592 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
593 // Verify the "strcpy" function prototype.
594 const FunctionType *FT = Callee->getFunctionType();
595 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
596 FT->getParamType(0) != FT->getParamType(1) ||
597 FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty))
600 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
601 if (Dst == Src) // strcpy(x,x) -> x
604 // See if we can get the length of the input string.
605 uint64_t Len = GetStringLength(Src);
606 if (Len == 0) return 0;
608 // We have enough information to now generate the memcpy call to do the
609 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
610 EmitMemCpy(Dst, Src, ConstantInt::get(TD->getIntPtrType(), Len), 1, B);
617 //===---------------------------------------===//
618 // 'strlen' Optimizations
620 struct VISIBILITY_HIDDEN StrLenOpt : public LibCallOptimization {
621 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
622 const FunctionType *FT = Callee->getFunctionType();
623 if (FT->getNumParams() != 1 ||
624 FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
625 !isa<IntegerType>(FT->getReturnType()))
628 Value *Src = CI->getOperand(1);
630 // Constant folding: strlen("xyz") -> 3
631 if (uint64_t Len = GetStringLength(Src))
632 return ConstantInt::get(CI->getType(), Len-1);
634 // Handle strlen(p) != 0.
635 if (!IsOnlyUsedInZeroEqualityComparison(CI)) return 0;
637 // strlen(x) != 0 --> *x != 0
638 // strlen(x) == 0 --> *x == 0
639 return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
643 //===---------------------------------------===//
644 // 'memcmp' Optimizations
646 struct VISIBILITY_HIDDEN MemCmpOpt : public LibCallOptimization {
647 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
648 const FunctionType *FT = Callee->getFunctionType();
649 if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
650 !isa<PointerType>(FT->getParamType(1)) ||
651 FT->getReturnType() != Type::Int32Ty)
654 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
656 if (LHS == RHS) // memcmp(s,s,x) -> 0
657 return Constant::getNullValue(CI->getType());
659 // Make sure we have a constant length.
660 ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
662 uint64_t Len = LenC->getZExtValue();
664 if (Len == 0) // memcmp(s1,s2,0) -> 0
665 return Constant::getNullValue(CI->getType());
667 if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
668 Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
669 Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
670 return B.CreateZExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
673 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS) != 0
674 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS) != 0
675 if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
676 const Type *PTy = PointerType::getUnqual(Len == 2 ?
677 Type::Int16Ty : Type::Int32Ty);
678 LHS = B.CreateBitCast(LHS, PTy, "tmp");
679 RHS = B.CreateBitCast(RHS, PTy, "tmp");
680 LoadInst *LHSV = B.CreateLoad(LHS, "lhsv");
681 LoadInst *RHSV = B.CreateLoad(RHS, "rhsv");
682 LHSV->setAlignment(1); RHSV->setAlignment(1); // Unaligned loads.
683 return B.CreateZExt(B.CreateXor(LHSV, RHSV, "shortdiff"), CI->getType());
690 //===---------------------------------------===//
691 // 'memcpy' Optimizations
693 struct VISIBILITY_HIDDEN MemCpyOpt : public LibCallOptimization {
694 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
695 const FunctionType *FT = Callee->getFunctionType();
696 if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
697 !isa<PointerType>(FT->getParamType(0)) ||
698 !isa<PointerType>(FT->getParamType(1)) ||
699 FT->getParamType(2) != TD->getIntPtrType())
702 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
703 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
704 return CI->getOperand(1);
708 //===----------------------------------------------------------------------===//
709 // Math Library Optimizations
710 //===----------------------------------------------------------------------===//
712 //===---------------------------------------===//
713 // 'pow*' Optimizations
715 struct VISIBILITY_HIDDEN PowOpt : public LibCallOptimization {
716 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
717 const FunctionType *FT = Callee->getFunctionType();
718 // Just make sure this has 2 arguments of the same FP type, which match the
720 if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
721 FT->getParamType(0) != FT->getParamType(1) ||
722 !FT->getParamType(0)->isFloatingPoint())
725 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
726 if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
727 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
729 if (Op1C->isExactlyValue(2.0)) // pow(2.0, x) -> exp2(x)
730 return EmitUnaryFloatFnCall(Op2, "exp2", B);
733 ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
734 if (Op2C == 0) return 0;
736 if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
737 return ConstantFP::get(CI->getType(), 1.0);
739 if (Op2C->isExactlyValue(0.5)) {
740 // FIXME: This is not safe for -0.0 and -inf. This can only be done when
741 // 'unsafe' math optimizations are allowed.
742 // x pow(x, 0.5) sqrt(x)
743 // ---------------------------------------------
747 // pow(x, 0.5) -> sqrt(x)
748 return B.CreateCall(get_sqrt(), Op1, "sqrt");
752 if (Op2C->isExactlyValue(1.0)) // pow(x, 1.0) -> x
754 if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
755 return B.CreateMul(Op1, Op1, "pow2");
756 if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
757 return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), Op1, "powrecip");
762 //===---------------------------------------===//
763 // 'exp2' Optimizations
765 struct VISIBILITY_HIDDEN Exp2Opt : public LibCallOptimization {
766 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
767 const FunctionType *FT = Callee->getFunctionType();
768 // Just make sure this has 1 argument of FP type, which matches the
770 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
771 !FT->getParamType(0)->isFloatingPoint())
774 Value *Op = CI->getOperand(1);
775 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
776 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
778 if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
779 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
780 LdExpArg = B.CreateSExt(OpC->getOperand(0), Type::Int32Ty, "tmp");
781 } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
782 if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
783 LdExpArg = B.CreateZExt(OpC->getOperand(0), Type::Int32Ty, "tmp");
788 if (Op->getType() == Type::FloatTy)
790 else if (Op->getType() == Type::DoubleTy)
795 Constant *One = ConstantFP::get(APFloat(1.0f));
796 if (Op->getType() != Type::FloatTy)
797 One = ConstantExpr::getFPExtend(One, Op->getType());
799 Module *M = Caller->getParent();
800 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
801 Op->getType(), Type::Int32Ty,NULL);
802 return B.CreateCall2(Callee, One, LdExpArg);
809 //===---------------------------------------===//
810 // Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
812 struct VISIBILITY_HIDDEN UnaryDoubleFPOpt : public LibCallOptimization {
813 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
814 const FunctionType *FT = Callee->getFunctionType();
815 if (FT->getNumParams() != 1 || FT->getReturnType() != Type::DoubleTy ||
816 FT->getParamType(0) != Type::DoubleTy)
819 // If this is something like 'floor((double)floatval)', convert to floorf.
820 FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
821 if (Cast == 0 || Cast->getOperand(0)->getType() != Type::FloatTy)
824 // floor((double)floatval) -> (double)floorf(floatval)
825 Value *V = Cast->getOperand(0);
826 V = EmitUnaryFloatFnCall(V, Callee->getNameStart(), B);
827 return B.CreateFPExt(V, Type::DoubleTy);
831 //===----------------------------------------------------------------------===//
832 // Integer Optimizations
833 //===----------------------------------------------------------------------===//
835 //===---------------------------------------===//
836 // 'ffs*' Optimizations
838 struct VISIBILITY_HIDDEN FFSOpt : public LibCallOptimization {
839 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
840 const FunctionType *FT = Callee->getFunctionType();
841 // Just make sure this has 2 arguments of the same FP type, which match the
843 if (FT->getNumParams() != 1 || FT->getReturnType() != Type::Int32Ty ||
844 !isa<IntegerType>(FT->getParamType(0)))
847 Value *Op = CI->getOperand(1);
850 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
851 if (CI->getValue() == 0) // ffs(0) -> 0.
852 return Constant::getNullValue(CI->getType());
853 return ConstantInt::get(Type::Int32Ty, // ffs(c) -> cttz(c)+1
854 CI->getValue().countTrailingZeros()+1);
857 // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
858 const Type *ArgType = Op->getType();
859 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
860 Intrinsic::cttz, &ArgType, 1);
861 Value *V = B.CreateCall(F, Op, "cttz");
862 V = B.CreateAdd(V, ConstantInt::get(Type::Int32Ty, 1), "tmp");
863 V = B.CreateIntCast(V, Type::Int32Ty, false, "tmp");
865 Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
866 return B.CreateSelect(Cond, V, ConstantInt::get(Type::Int32Ty, 0));
870 //===---------------------------------------===//
871 // 'isdigit' Optimizations
873 struct VISIBILITY_HIDDEN IsDigitOpt : public LibCallOptimization {
874 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
875 const FunctionType *FT = Callee->getFunctionType();
876 // We require integer(i32)
877 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
878 FT->getParamType(0) != Type::Int32Ty)
881 // isdigit(c) -> (c-'0') <u 10
882 Value *Op = CI->getOperand(1);
883 Op = B.CreateSub(Op, ConstantInt::get(Type::Int32Ty, '0'), "isdigittmp");
884 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::Int32Ty, 10), "isdigit");
885 return B.CreateZExt(Op, CI->getType());
889 //===---------------------------------------===//
890 // 'isascii' Optimizations
892 struct VISIBILITY_HIDDEN IsAsciiOpt : public LibCallOptimization {
893 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
894 const FunctionType *FT = Callee->getFunctionType();
895 // We require integer(i32)
896 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
897 FT->getParamType(0) != Type::Int32Ty)
900 // isascii(c) -> c <u 128
901 Value *Op = CI->getOperand(1);
902 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::Int32Ty, 128), "isascii");
903 return B.CreateZExt(Op, CI->getType());
907 //===---------------------------------------===//
908 // 'abs', 'labs', 'llabs' Optimizations
910 struct VISIBILITY_HIDDEN AbsOpt : public LibCallOptimization {
911 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
912 const FunctionType *FT = Callee->getFunctionType();
913 // We require integer(integer) where the types agree.
914 if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
915 FT->getParamType(0) != FT->getReturnType())
918 // abs(x) -> x >s -1 ? x : -x
919 Value *Op = CI->getOperand(1);
920 Value *Pos = B.CreateICmpSGT(Op,ConstantInt::getAllOnesValue(Op->getType()),
922 Value *Neg = B.CreateNeg(Op, "neg");
923 return B.CreateSelect(Pos, Op, Neg);
928 //===---------------------------------------===//
929 // 'toascii' Optimizations
931 struct VISIBILITY_HIDDEN ToAsciiOpt : public LibCallOptimization {
932 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
933 const FunctionType *FT = Callee->getFunctionType();
934 // We require i32(i32)
935 if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
936 FT->getParamType(0) != Type::Int32Ty)
939 // isascii(c) -> c & 0x7f
940 return B.CreateAnd(CI->getOperand(1), ConstantInt::get(CI->getType(),0x7F));
944 //===----------------------------------------------------------------------===//
945 // Formatting and IO Optimizations
946 //===----------------------------------------------------------------------===//
948 //===---------------------------------------===//
949 // 'printf' Optimizations
951 struct VISIBILITY_HIDDEN PrintFOpt : public LibCallOptimization {
952 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
953 // Require one fixed pointer argument and an integer/void result.
954 const FunctionType *FT = Callee->getFunctionType();
955 if (FT->getNumParams() < 1 || !isa<PointerType>(FT->getParamType(0)) ||
956 !(isa<IntegerType>(FT->getReturnType()) ||
957 FT->getReturnType() == Type::VoidTy))
960 // Check for a fixed format string.
961 std::string FormatStr;
962 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
965 // Empty format string -> noop.
966 if (FormatStr.empty()) // Tolerate printf's declared void.
967 return CI->use_empty() ? (Value*)CI : ConstantInt::get(CI->getType(), 0);
969 // printf("x") -> putchar('x'), even for '%'.
970 if (FormatStr.size() == 1) {
971 EmitPutChar(ConstantInt::get(Type::Int32Ty, FormatStr[0]), B);
972 return CI->use_empty() ? (Value*)CI : ConstantInt::get(CI->getType(), 1);
975 // printf("foo\n") --> puts("foo")
976 if (FormatStr[FormatStr.size()-1] == '\n' &&
977 FormatStr.find('%') == std::string::npos) { // no format characters.
978 // Create a string literal with no \n on it. We expect the constant merge
979 // pass to be run after this pass, to merge duplicate strings.
980 FormatStr.erase(FormatStr.end()-1);
981 Constant *C = ConstantArray::get(FormatStr, true);
982 C = new GlobalVariable(C->getType(), true,GlobalVariable::InternalLinkage,
983 C, "str", Callee->getParent());
985 return CI->use_empty() ? (Value*)CI :
986 ConstantInt::get(CI->getType(), FormatStr.size()+1);
989 // Optimize specific format strings.
990 // printf("%c", chr) --> putchar(*(i8*)dst)
991 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
992 isa<IntegerType>(CI->getOperand(2)->getType())) {
993 EmitPutChar(CI->getOperand(2), B);
994 return CI->use_empty() ? (Value*)CI : ConstantInt::get(CI->getType(), 1);
997 // printf("%s\n", str) --> puts(str)
998 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
999 isa<PointerType>(CI->getOperand(2)->getType()) &&
1001 EmitPutS(CI->getOperand(2), B);
1008 //===---------------------------------------===//
1009 // 'sprintf' Optimizations
1011 struct VISIBILITY_HIDDEN SPrintFOpt : public LibCallOptimization {
1012 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
1013 // Require two fixed pointer arguments and an integer result.
1014 const FunctionType *FT = Callee->getFunctionType();
1015 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1016 !isa<PointerType>(FT->getParamType(1)) ||
1017 !isa<IntegerType>(FT->getReturnType()))
1020 // Check for a fixed format string.
1021 std::string FormatStr;
1022 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1025 // If we just have a format string (nothing else crazy) transform it.
1026 if (CI->getNumOperands() == 3) {
1027 // Make sure there's no % in the constant array. We could try to handle
1028 // %% -> % in the future if we cared.
1029 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1030 if (FormatStr[i] == '%')
1031 return 0; // we found a format specifier, bail out.
1033 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1034 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
1035 ConstantInt::get(TD->getIntPtrType(), FormatStr.size()+1),1,B);
1036 return ConstantInt::get(CI->getType(), FormatStr.size());
1039 // The remaining optimizations require the format string to be "%s" or "%c"
1040 // and have an extra operand.
1041 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1044 // Decode the second character of the format string.
1045 if (FormatStr[1] == 'c') {
1046 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1047 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1048 Value *V = B.CreateTrunc(CI->getOperand(3), Type::Int8Ty, "char");
1049 Value *Ptr = CastToCStr(CI->getOperand(1), B);
1050 B.CreateStore(V, Ptr);
1051 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::Int32Ty, 1), "nul");
1052 B.CreateStore(Constant::getNullValue(Type::Int8Ty), Ptr);
1054 return ConstantInt::get(CI->getType(), 1);
1057 if (FormatStr[1] == 's') {
1058 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1059 if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
1061 Value *Len = EmitStrLen(CI->getOperand(3), B);
1062 Value *IncLen = B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1),
1064 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
1066 // The sprintf result is the unincremented number of bytes in the string.
1067 return B.CreateIntCast(Len, CI->getType(), false);
1073 //===---------------------------------------===//
1074 // 'fwrite' Optimizations
1076 struct VISIBILITY_HIDDEN FWriteOpt : public LibCallOptimization {
1077 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
1078 // Require a pointer, an integer, an integer, a pointer, returning integer.
1079 const FunctionType *FT = Callee->getFunctionType();
1080 if (FT->getNumParams() != 4 || !isa<PointerType>(FT->getParamType(0)) ||
1081 !isa<IntegerType>(FT->getParamType(1)) ||
1082 !isa<IntegerType>(FT->getParamType(2)) ||
1083 !isa<PointerType>(FT->getParamType(3)) ||
1084 !isa<IntegerType>(FT->getReturnType()))
1087 // Get the element size and count.
1088 ConstantInt *SizeC = dyn_cast<ConstantInt>(CI->getOperand(2));
1089 ConstantInt *CountC = dyn_cast<ConstantInt>(CI->getOperand(3));
1090 if (!SizeC || !CountC) return 0;
1091 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
1093 // If this is writing zero records, remove the call (it's a noop).
1095 return ConstantInt::get(CI->getType(), 0);
1097 // If this is writing one byte, turn it into fputc.
1098 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1099 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1100 EmitFPutC(Char, CI->getOperand(4), B);
1101 return ConstantInt::get(CI->getType(), 1);
1108 //===---------------------------------------===//
1109 // 'fputs' Optimizations
1111 struct VISIBILITY_HIDDEN FPutsOpt : public LibCallOptimization {
1112 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
1113 // Require two pointers. Also, we can't optimize if return value is used.
1114 const FunctionType *FT = Callee->getFunctionType();
1115 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1116 !isa<PointerType>(FT->getParamType(1)) ||
1120 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1121 uint64_t Len = GetStringLength(CI->getOperand(1));
1123 EmitFWrite(CI->getOperand(1), ConstantInt::get(TD->getIntPtrType(), Len-1),
1124 CI->getOperand(2), B);
1125 return CI; // Known to have no uses (see above).
1129 //===---------------------------------------===//
1130 // 'fprintf' Optimizations
1132 struct VISIBILITY_HIDDEN FPrintFOpt : public LibCallOptimization {
1133 virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
1134 // Require two fixed paramters as pointers and integer result.
1135 const FunctionType *FT = Callee->getFunctionType();
1136 if (FT->getNumParams() != 2 || !isa<PointerType>(FT->getParamType(0)) ||
1137 !isa<PointerType>(FT->getParamType(1)) ||
1138 !isa<IntegerType>(FT->getReturnType()))
1141 // All the optimizations depend on the format string.
1142 std::string FormatStr;
1143 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
1146 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
1147 if (CI->getNumOperands() == 3) {
1148 for (unsigned i = 0, e = FormatStr.size(); i != e; ++i)
1149 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
1150 return 0; // We found a format specifier.
1152 EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(),
1154 CI->getOperand(1), B);
1155 return ConstantInt::get(CI->getType(), FormatStr.size());
1158 // The remaining optimizations require the format string to be "%s" or "%c"
1159 // and have an extra operand.
1160 if (FormatStr.size() != 2 || FormatStr[0] != '%' || CI->getNumOperands() <4)
1163 // Decode the second character of the format string.
1164 if (FormatStr[1] == 'c') {
1165 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1166 if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
1167 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
1168 return ConstantInt::get(CI->getType(), 1);
1171 if (FormatStr[1] == 's') {
1172 // fprintf(F, "%s", str) -> fputs(str, F)
1173 if (!isa<PointerType>(CI->getOperand(3)->getType()) || !CI->use_empty())
1175 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B);
1182 } // end anonymous namespace.
1184 //===----------------------------------------------------------------------===//
1185 // SimplifyLibCalls Pass Implementation
1186 //===----------------------------------------------------------------------===//
1189 /// This pass optimizes well known library functions from libc and libm.
1191 class VISIBILITY_HIDDEN SimplifyLibCalls : public FunctionPass {
1192 StringMap<LibCallOptimization*> Optimizations;
1193 // Miscellaneous LibCall Optimizations
1195 // String and Memory LibCall Optimizations
1196 StrCatOpt StrCat; StrChrOpt StrChr; StrCmpOpt StrCmp; StrNCmpOpt StrNCmp;
1197 StrCpyOpt StrCpy; StrLenOpt StrLen; MemCmpOpt MemCmp; MemCpyOpt MemCpy;
1198 // Math Library Optimizations
1199 PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
1200 // Integer Optimizations
1201 FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
1203 // Formatting and IO Optimizations
1204 SPrintFOpt SPrintF; PrintFOpt PrintF;
1205 FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
1207 static char ID; // Pass identification
1208 SimplifyLibCalls() : FunctionPass((intptr_t)&ID) {}
1210 void InitOptimizations();
1211 bool runOnFunction(Function &F);
1213 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1214 AU.addRequired<TargetData>();
1217 char SimplifyLibCalls::ID = 0;
1218 } // end anonymous namespace.
1220 static RegisterPass<SimplifyLibCalls>
1221 X("simplify-libcalls", "Simplify well-known library calls");
1223 // Public interface to the Simplify LibCalls pass.
1224 FunctionPass *llvm::createSimplifyLibCallsPass() {
1225 return new SimplifyLibCalls();
1228 /// Optimizations - Populate the Optimizations map with all the optimizations
1230 void SimplifyLibCalls::InitOptimizations() {
1231 // Miscellaneous LibCall Optimizations
1232 Optimizations["exit"] = &Exit;
1234 // String and Memory LibCall Optimizations
1235 Optimizations["strcat"] = &StrCat;
1236 Optimizations["strchr"] = &StrChr;
1237 Optimizations["strcmp"] = &StrCmp;
1238 Optimizations["strncmp"] = &StrNCmp;
1239 Optimizations["strcpy"] = &StrCpy;
1240 Optimizations["strlen"] = &StrLen;
1241 Optimizations["memcmp"] = &MemCmp;
1242 Optimizations["memcpy"] = &MemCpy;
1244 // Math Library Optimizations
1245 Optimizations["powf"] = &Pow;
1246 Optimizations["pow"] = &Pow;
1247 Optimizations["powl"] = &Pow;
1248 Optimizations["exp2l"] = &Exp2;
1249 Optimizations["exp2"] = &Exp2;
1250 Optimizations["exp2f"] = &Exp2;
1253 Optimizations["floor"] = &UnaryDoubleFP;
1256 Optimizations["ceil"] = &UnaryDoubleFP;
1259 Optimizations["round"] = &UnaryDoubleFP;
1262 Optimizations["rint"] = &UnaryDoubleFP;
1264 #ifdef HAVE_NEARBYINTF
1265 Optimizations["nearbyint"] = &UnaryDoubleFP;
1268 // Integer Optimizations
1269 Optimizations["ffs"] = &FFS;
1270 Optimizations["ffsl"] = &FFS;
1271 Optimizations["ffsll"] = &FFS;
1272 Optimizations["abs"] = &Abs;
1273 Optimizations["labs"] = &Abs;
1274 Optimizations["llabs"] = &Abs;
1275 Optimizations["isdigit"] = &IsDigit;
1276 Optimizations["isascii"] = &IsAscii;
1277 Optimizations["toascii"] = &ToAscii;
1279 // Formatting and IO Optimizations
1280 Optimizations["sprintf"] = &SPrintF;
1281 Optimizations["printf"] = &PrintF;
1282 Optimizations["fwrite"] = &FWrite;
1283 Optimizations["fputs"] = &FPuts;
1284 Optimizations["fprintf"] = &FPrintF;
1288 /// runOnFunction - Top level algorithm.
1290 bool SimplifyLibCalls::runOnFunction(Function &F) {
1291 if (Optimizations.empty())
1292 InitOptimizations();
1294 const TargetData &TD = getAnalysis<TargetData>();
1298 bool Changed = false;
1299 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1300 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
1301 // Ignore non-calls.
1302 CallInst *CI = dyn_cast<CallInst>(I++);
1305 // Ignore indirect calls and calls to non-external functions.
1306 Function *Callee = CI->getCalledFunction();
1307 if (Callee == 0 || !Callee->isDeclaration() ||
1308 !(Callee->hasExternalLinkage() || Callee->hasDLLImportLinkage()))
1311 // Ignore unknown calls.
1312 const char *CalleeName = Callee->getNameStart();
1313 StringMap<LibCallOptimization*>::iterator OMI =
1314 Optimizations.find(CalleeName, CalleeName+Callee->getNameLen());
1315 if (OMI == Optimizations.end()) continue;
1317 // Set the builder to the instruction after the call.
1318 Builder.SetInsertPoint(BB, I);
1320 // Try to optimize this call.
1321 Value *Result = OMI->second->OptimizeCall(CI, TD, Builder);
1322 if (Result == 0) continue;
1324 DEBUG(DOUT << "SimplifyLibCalls simplified: " << *CI;
1325 DOUT << " into: " << *Result << "\n");
1327 // Something changed!
1331 // Inspect the instruction after the call (which was potentially just
1335 if (CI != Result && !CI->use_empty()) {
1336 CI->replaceAllUsesWith(Result);
1337 if (!Result->hasName())
1338 Result->takeName(CI);
1340 CI->eraseFromParent();
1348 // Additional cases that we need to add to this file:
1351 // * cbrt(expN(X)) -> expN(x/3)
1352 // * cbrt(sqrt(x)) -> pow(x,1/6)
1353 // * cbrt(sqrt(x)) -> pow(x,1/9)
1356 // * cos(-x) -> cos(x)
1359 // * exp(log(x)) -> x
1362 // * log(exp(x)) -> x
1363 // * log(x**y) -> y*log(x)
1364 // * log(exp(y)) -> y*log(e)
1365 // * log(exp2(y)) -> y*log(2)
1366 // * log(exp10(y)) -> y*log(10)
1367 // * log(sqrt(x)) -> 0.5*log(x)
1368 // * log(pow(x,y)) -> y*log(x)
1370 // lround, lroundf, lroundl:
1371 // * lround(cnst) -> cnst'
1374 // * memcmp(x,y,l) -> cnst
1375 // (if all arguments are constant and strlen(x) <= l and strlen(y) <= l)
1378 // * memmove(d,s,l,a) -> memcpy(d,s,l,a)
1379 // (if s is a global constant array)
1382 // * pow(exp(x),y) -> exp(x*y)
1383 // * pow(sqrt(x),y) -> pow(x,y*0.5)
1384 // * pow(pow(x,y),z)-> pow(x,y*z)
1387 // * puts("") -> putchar("\n")
1389 // round, roundf, roundl:
1390 // * round(cnst) -> cnst'
1393 // * signbit(cnst) -> cnst'
1394 // * signbit(nncst) -> 0 (if pstv is a non-negative constant)
1396 // sqrt, sqrtf, sqrtl:
1397 // * sqrt(expN(x)) -> expN(x*0.5)
1398 // * sqrt(Nroot(x)) -> pow(x,1/(2*N))
1399 // * sqrt(pow(x,y)) -> pow(|x|,y*0.5)
1402 // * stpcpy(str, "literal") ->
1403 // llvm.memcpy(str,"literal",strlen("literal")+1,1)
1405 // * strrchr(s,c) -> reverse_offset_of_in(c,s)
1406 // (if c is a constant integer and s is a constant string)
1407 // * strrchr(s1,0) -> strchr(s1,0)
1410 // * strncat(x,y,0) -> x
1411 // * strncat(x,y,0) -> x (if strlen(y) = 0)
1412 // * strncat(x,y,l) -> strcat(x,y) (if y and l are constants an l > strlen(y))
1415 // * strncpy(d,s,0) -> d
1416 // * strncpy(d,s,l) -> memcpy(d,s,l,1)
1417 // (if s and l are constants)
1420 // * strpbrk(s,a) -> offset_in_for(s,a)
1421 // (if s and a are both constant strings)
1422 // * strpbrk(s,"") -> 0
1423 // * strpbrk(s,a) -> strchr(s,a[0]) (if a is constant string of length 1)
1426 // * strspn(s,a) -> const_int (if both args are constant)
1427 // * strspn("",a) -> 0
1428 // * strspn(s,"") -> 0
1429 // * strcspn(s,a) -> const_int (if both args are constant)
1430 // * strcspn("",a) -> 0
1431 // * strcspn(s,"") -> strlen(a)
1434 // * strstr(x,x) -> x
1435 // * strstr(s1,s2) -> offset_of_s2_in(s1)
1436 // (if s1 and s2 are constant strings)
1439 // * tan(atan(x)) -> x
1441 // trunc, truncf, truncl:
1442 // * trunc(cnst) -> cnst'