LLVM API Documentation

SimplifyLibCalls.cpp

Go to the documentation of this file.
00001 //===- SimplifyLibCalls.cpp - Optimize specific well-known library calls --===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements a simple pass that applies a variety of small
00011 // optimizations for calls to specific well-known function calls (e.g. runtime
00012 // library functions). For example, a call to the function "exit(3)" that
00013 // occurs within the main() function can be transformed into a simple "return 3"
00014 // instruction. Any optimization that takes this form (replace call to library
00015 // function with simpler code that provides the same result) belongs in this
00016 // file.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #define DEBUG_TYPE "simplify-libcalls"
00021 #include "llvm/Transforms/Scalar.h"
00022 #include "llvm/Intrinsics.h"
00023 #include "llvm/Module.h"
00024 #include "llvm/Pass.h"
00025 #include "llvm/Support/IRBuilder.h"
00026 #include "llvm/Analysis/ValueTracking.h"
00027 #include "llvm/Target/TargetData.h"
00028 #include "llvm/ADT/SmallPtrSet.h"
00029 #include "llvm/ADT/StringMap.h"
00030 #include "llvm/ADT/Statistic.h"
00031 #include "llvm/Support/Compiler.h"
00032 #include "llvm/Support/Debug.h"
00033 #include "llvm/Config/config.h"
00034 using namespace llvm;
00035 
00036 STATISTIC(NumSimplified, "Number of library calls simplified");
00037 
00038 //===----------------------------------------------------------------------===//
00039 // Optimizer Base Class
00040 //===----------------------------------------------------------------------===//
00041 
00042 /// This class is the abstract base class for the set of optimizations that
00043 /// corresponds to one library call.
00044 namespace {
00045 class VISIBILITY_HIDDEN LibCallOptimization {
00046 protected:
00047   Function *Caller;
00048   const TargetData *TD;
00049 public:
00050   LibCallOptimization() { }
00051   virtual ~LibCallOptimization() {}
00052 
00053   /// CallOptimizer - This pure virtual method is implemented by base classes to
00054   /// do various optimizations.  If this returns null then no transformation was
00055   /// performed.  If it returns CI, then it transformed the call and CI is to be
00056   /// deleted.  If it returns something else, replace CI with the new value and
00057   /// delete CI.
00058   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) 
00059     =0;
00060   
00061   Value *OptimizeCall(CallInst *CI, const TargetData &TD, IRBuilder<> &B) {
00062     Caller = CI->getParent()->getParent();
00063     this->TD = &TD;
00064     return CallOptimizer(CI->getCalledFunction(), CI, B);
00065   }
00066 
00067   /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
00068   Value *CastToCStr(Value *V, IRBuilder<> &B);
00069 
00070   /// EmitStrLen - Emit a call to the strlen function to the builder, for the
00071   /// specified pointer.  Ptr is required to be some pointer type, and the
00072   /// return value has 'intptr_t' type.
00073   Value *EmitStrLen(Value *Ptr, IRBuilder<> &B);
00074   
00075   /// EmitMemCpy - Emit a call to the memcpy function to the builder.  This
00076   /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
00077   Value *EmitMemCpy(Value *Dst, Value *Src, Value *Len, 
00078                     unsigned Align, IRBuilder<> &B);
00079   
00080   /// EmitMemChr - Emit a call to the memchr function.  This assumes that Ptr is
00081   /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
00082   Value *EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B);
00083     
00084   /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
00085   /// 'floor').  This function is known to take a single of type matching 'Op'
00086   /// and returns one value with the same type.  If 'Op' is a long double, 'l'
00087   /// is added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
00088   Value *EmitUnaryFloatFnCall(Value *Op, const char *Name, IRBuilder<> &B);
00089   
00090   /// EmitPutChar - Emit a call to the putchar function.  This assumes that Char
00091   /// is an integer.
00092   void EmitPutChar(Value *Char, IRBuilder<> &B);
00093   
00094   /// EmitPutS - Emit a call to the puts function.  This assumes that Str is
00095   /// some pointer.
00096   void EmitPutS(Value *Str, IRBuilder<> &B);
00097     
00098   /// EmitFPutC - Emit a call to the fputc function.  This assumes that Char is
00099   /// an i32, and File is a pointer to FILE.
00100   void EmitFPutC(Value *Char, Value *File, IRBuilder<> &B);
00101   
00102   /// EmitFPutS - Emit a call to the puts function.  Str is required to be a
00103   /// pointer and File is a pointer to FILE.
00104   void EmitFPutS(Value *Str, Value *File, IRBuilder<> &B);
00105   
00106   /// EmitFWrite - Emit a call to the fwrite function.  This assumes that Ptr is
00107   /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
00108   void EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B);
00109     
00110 };
00111 } // End anonymous namespace.
00112 
00113 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
00114 Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
00115   return B.CreateBitCast(V, PointerType::getUnqual(Type::Int8Ty), "cstr");
00116 }
00117 
00118 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
00119 /// specified pointer.  This always returns an integer value of size intptr_t.
00120 Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder<> &B) {
00121   Module *M = Caller->getParent();
00122   Constant *StrLen =M->getOrInsertFunction("strlen", TD->getIntPtrType(),
00123                                            PointerType::getUnqual(Type::Int8Ty),
00124                                            NULL);
00125   return B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
00126 }
00127 
00128 /// EmitMemCpy - Emit a call to the memcpy function to the builder.  This always
00129 /// expects that the size has type 'intptr_t' and Dst/Src are pointers.
00130 Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
00131                                        unsigned Align, IRBuilder<> &B) {
00132   Module *M = Caller->getParent();
00133   Intrinsic::ID IID = Len->getType() == Type::Int32Ty ?
00134                            Intrinsic::memcpy_i32 : Intrinsic::memcpy_i64;
00135   Value *MemCpy = Intrinsic::getDeclaration(M, IID);
00136   return B.CreateCall4(MemCpy, CastToCStr(Dst, B), CastToCStr(Src, B), Len,
00137                        ConstantInt::get(Type::Int32Ty, Align));
00138 }
00139 
00140 /// EmitMemChr - Emit a call to the memchr function.  This assumes that Ptr is
00141 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
00142 Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
00143                                        Value *Len, IRBuilder<> &B) {
00144   Module *M = Caller->getParent();
00145   Value *MemChr = M->getOrInsertFunction("memchr",
00146                                          PointerType::getUnqual(Type::Int8Ty),
00147                                          PointerType::getUnqual(Type::Int8Ty),
00148                                          Type::Int32Ty, TD->getIntPtrType(),
00149                                          NULL);
00150   return B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
00151 }
00152 
00153 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
00154 /// 'floor').  This function is known to take a single of type matching 'Op' and
00155 /// returns one value with the same type.  If 'Op' is a long double, 'l' is
00156 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
00157 Value *LibCallOptimization::EmitUnaryFloatFnCall(Value *Op, const char *Name,
00158                                                  IRBuilder<> &B) {
00159   char NameBuffer[20];
00160   if (Op->getType() != Type::DoubleTy) {
00161     // If we need to add a suffix, copy into NameBuffer.
00162     unsigned NameLen = strlen(Name);
00163     assert(NameLen < sizeof(NameBuffer)-2);
00164     memcpy(NameBuffer, Name, NameLen);
00165     if (Op->getType() == Type::FloatTy)
00166       NameBuffer[NameLen] = 'f';  // floorf
00167     else
00168       NameBuffer[NameLen] = 'l';  // floorl
00169     NameBuffer[NameLen+1] = 0;
00170     Name = NameBuffer;
00171   }
00172   
00173   Module *M = Caller->getParent();
00174   Value *Callee = M->getOrInsertFunction(Name, Op->getType(), 
00175                                          Op->getType(), NULL);
00176   return B.CreateCall(Callee, Op, Name);
00177 }
00178 
00179 /// EmitPutChar - Emit a call to the putchar function.  This assumes that Char
00180 /// is an integer.
00181 void LibCallOptimization::EmitPutChar(Value *Char, IRBuilder<> &B) {
00182   Module *M = Caller->getParent();
00183   Value *F = M->getOrInsertFunction("putchar", Type::Int32Ty,
00184                                     Type::Int32Ty, NULL);
00185   B.CreateCall(F, B.CreateIntCast(Char, Type::Int32Ty, "chari"), "putchar");
00186 }
00187 
00188 /// EmitPutS - Emit a call to the puts function.  This assumes that Str is
00189 /// some pointer.
00190 void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
00191   Module *M = Caller->getParent();
00192   Value *F = M->getOrInsertFunction("puts", Type::Int32Ty,
00193                                     PointerType::getUnqual(Type::Int8Ty), NULL);
00194   B.CreateCall(F, CastToCStr(Str, B), "puts");
00195 }
00196 
00197 /// EmitFPutC - Emit a call to the fputc function.  This assumes that Char is
00198 /// an integer and File is a pointer to FILE.
00199 void LibCallOptimization::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B) {
00200   Module *M = Caller->getParent();
00201   Constant *F = M->getOrInsertFunction("fputc", Type::Int32Ty, Type::Int32Ty,
00202                                        File->getType(), NULL);
00203   Char = B.CreateIntCast(Char, Type::Int32Ty, "chari");
00204   B.CreateCall2(F, Char, File, "fputc");
00205 }
00206 
00207 /// EmitFPutS - Emit a call to the puts function.  Str is required to be a
00208 /// pointer and File is a pointer to FILE.
00209 void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
00210   Module *M = Caller->getParent();
00211   Constant *F = M->getOrInsertFunction("fputs", Type::Int32Ty,
00212                                        PointerType::getUnqual(Type::Int8Ty),
00213                                        File->getType(), NULL);
00214   B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
00215 }
00216 
00217 /// EmitFWrite - Emit a call to the fwrite function.  This assumes that Ptr is
00218 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
00219 void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
00220                                      IRBuilder<> &B) {
00221   Module *M = Caller->getParent();
00222   Constant *F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(),
00223                                        PointerType::getUnqual(Type::Int8Ty),
00224                                        TD->getIntPtrType(), TD->getIntPtrType(),
00225                                        File->getType(), NULL);
00226   B.CreateCall4(F, CastToCStr(Ptr, B), Size, 
00227                 ConstantInt::get(TD->getIntPtrType(), 1), File);
00228 }
00229 
00230 //===----------------------------------------------------------------------===//
00231 // Helper Functions
00232 //===----------------------------------------------------------------------===//
00233 
00234 /// GetStringLengthH - If we can compute the length of the string pointed to by
00235 /// the specified pointer, return 'len+1'.  If we can't, return 0.
00236 static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
00237   // Look through noop bitcast instructions.
00238   if (BitCastInst *BCI = dyn_cast<BitCastInst>(V))
00239     return GetStringLengthH(BCI->getOperand(0), PHIs);
00240   
00241   // If this is a PHI node, there are two cases: either we have already seen it
00242   // or we haven't.
00243   if (PHINode *PN = dyn_cast<PHINode>(V)) {
00244     if (!PHIs.insert(PN))
00245       return ~0ULL;  // already in the set.
00246     
00247     // If it was new, see if all the input strings are the same length.
00248     uint64_t LenSoFar = ~0ULL;
00249     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
00250       uint64_t Len = GetStringLengthH(PN->getIncomingValue(i), PHIs);
00251       if (Len == 0) return 0; // Unknown length -> unknown.
00252       
00253       if (Len == ~0ULL) continue;
00254       
00255       if (Len != LenSoFar && LenSoFar != ~0ULL)
00256         return 0;    // Disagree -> unknown.
00257       LenSoFar = Len;
00258     }
00259     
00260     // Success, all agree.
00261     return LenSoFar;
00262   }
00263   
00264   // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
00265   if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
00266     uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs);
00267     if (Len1 == 0) return 0;
00268     uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs);
00269     if (Len2 == 0) return 0;
00270     if (Len1 == ~0ULL) return Len2;
00271     if (Len2 == ~0ULL) return Len1;
00272     if (Len1 != Len2) return 0;
00273     return Len1;
00274   }
00275   
00276   // If the value is not a GEP instruction nor a constant expression with a
00277   // GEP instruction, then return unknown.
00278   User *GEP = 0;
00279   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
00280     GEP = GEPI;
00281   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
00282     if (CE->getOpcode() != Instruction::GetElementPtr)
00283       return 0;
00284     GEP = CE;
00285   } else {
00286     return 0;
00287   }
00288   
00289   // Make sure the GEP has exactly three arguments.
00290   if (GEP->getNumOperands() != 3)
00291     return 0;
00292   
00293   // Check to make sure that the first operand of the GEP is an integer and
00294   // has value 0 so that we are sure we're indexing into the initializer.
00295   if (ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
00296     if (!Idx->isZero())
00297       return 0;
00298   } else
00299     return 0;
00300   
00301   // If the second index isn't a ConstantInt, then this is a variable index
00302   // into the array.  If this occurs, we can't say anything meaningful about
00303   // the string.
00304   uint64_t StartIdx = 0;
00305   if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
00306     StartIdx = CI->getZExtValue();
00307   else
00308     return 0;
00309   
00310   // The GEP instruction, constant or instruction, must reference a global
00311   // variable that is a constant and is initialized. The referenced constant
00312   // initializer is the array that we'll use for optimization.
00313   GlobalVariable* GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
00314   if (!GV || !GV->isConstant() || !GV->hasInitializer())
00315     return 0;
00316   Constant *GlobalInit = GV->getInitializer();
00317   
00318   // Handle the ConstantAggregateZero case, which is a degenerate case. The
00319   // initializer is constant zero so the length of the string must be zero.
00320   if (isa<ConstantAggregateZero>(GlobalInit))
00321     return 1;  // Len = 0 offset by 1.
00322   
00323   // Must be a Constant Array
00324   ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit);
00325   if (!Array || Array->getType()->getElementType() != Type::Int8Ty)
00326     return false;
00327   
00328   // Get the number of elements in the array
00329   uint64_t NumElts = Array->getType()->getNumElements();
00330   
00331   // Traverse the constant array from StartIdx (derived above) which is
00332   // the place the GEP refers to in the array.
00333   for (unsigned i = StartIdx; i != NumElts; ++i) {
00334     Constant *Elt = Array->getOperand(i);
00335     ConstantInt *CI = dyn_cast<ConstantInt>(Elt);
00336     if (!CI) // This array isn't suitable, non-int initializer.
00337       return 0;
00338     if (CI->isZero())
00339       return i-StartIdx+1; // We found end of string, success!
00340   }
00341   
00342   return 0; // The array isn't null terminated, conservatively return 'unknown'.
00343 }
00344 
00345 /// GetStringLength - If we can compute the length of the string pointed to by
00346 /// the specified pointer, return 'len+1'.  If we can't, return 0.
00347 static uint64_t GetStringLength(Value *V) {
00348   if (!isa<PointerType>(V->getType())) return 0;
00349   
00350   SmallPtrSet<PHINode*, 32> PHIs;
00351   uint64_t Len = GetStringLengthH(V, PHIs);
00352   // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
00353   // an empty string as a length.
00354   return Len == ~0ULL ? 1 : Len;
00355 }
00356 
00357 /// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
00358 /// value is equal or not-equal to zero. 
00359 static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
00360   for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
00361        UI != E; ++UI) {
00362     if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
00363       if (IC->isEquality())
00364         if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
00365           if (C->isNullValue())
00366             continue;
00367     // Unknown instruction.
00368     return false;
00369   }
00370   return true;
00371 }
00372 
00373 //===----------------------------------------------------------------------===//
00374 // Miscellaneous LibCall Optimizations
00375 //===----------------------------------------------------------------------===//
00376 
00377 namespace {
00378 //===---------------------------------------===//
00379 // 'exit' Optimizations
00380 
00381 /// ExitOpt - int main() { exit(4); } --> int main() { return 4; }
00382 struct VISIBILITY_HIDDEN ExitOpt : public LibCallOptimization {
00383   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00384     // Verify we have a reasonable prototype for exit.
00385     if (Callee->arg_size() == 0 || !CI->use_empty())
00386       return 0;
00387 
00388     // Verify the caller is main, and that the result type of main matches the
00389     // argument type of exit.
00390     if (!Caller->isName("main") || !Caller->hasExternalLinkage() ||
00391         Caller->getReturnType() != CI->getOperand(1)->getType())
00392       return 0;
00393 
00394     TerminatorInst *OldTI = CI->getParent()->getTerminator();
00395     
00396     // Create the return after the call.
00397     ReturnInst *RI = B.CreateRet(CI->getOperand(1));
00398 
00399     // Drop all successor phi node entries.
00400     for (unsigned i = 0, e = OldTI->getNumSuccessors(); i != e; ++i)
00401       OldTI->getSuccessor(i)->removePredecessor(CI->getParent());
00402     
00403     // Erase all instructions from after our return instruction until the end of
00404     // the block.
00405     BasicBlock::iterator FirstDead = RI; ++FirstDead;
00406     CI->getParent()->getInstList().erase(FirstDead, CI->getParent()->end());
00407     return CI;
00408   }
00409 };
00410 
00411 //===----------------------------------------------------------------------===//
00412 // String and Memory LibCall Optimizations
00413 //===----------------------------------------------------------------------===//
00414 
00415 //===---------------------------------------===//
00416 // 'strcat' Optimizations
00417 
00418 struct VISIBILITY_HIDDEN StrCatOpt : public LibCallOptimization {
00419   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00420     // Verify the "strcat" function prototype.
00421     const FunctionType *FT = Callee->getFunctionType();
00422     if (FT->getNumParams() != 2 ||
00423         FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
00424         FT->getParamType(0) != FT->getReturnType() ||
00425         FT->getParamType(1) != FT->getReturnType())
00426       return 0;
00427     
00428     // Extract some information from the instruction
00429     Value *Dst = CI->getOperand(1);
00430     Value *Src = CI->getOperand(2);
00431     
00432     // See if we can get the length of the input string.
00433     uint64_t Len = GetStringLength(Src);
00434     if (Len == 0) return 0;
00435     --Len;  // Unbias length.
00436     
00437     // Handle the simple, do-nothing case: strcat(x, "") -> x
00438     if (Len == 0)
00439       return Dst;
00440     
00441     // We need to find the end of the destination string.  That's where the
00442     // memory is to be moved to. We just generate a call to strlen.
00443     Value *DstLen = EmitStrLen(Dst, B);
00444     
00445     // Now that we have the destination's length, we must index into the
00446     // destination's pointer to get the actual memcpy destination (end of
00447     // the string .. we're concatenating).
00448     Dst = B.CreateGEP(Dst, DstLen, "endptr");
00449     
00450     // We have enough information to now generate the memcpy call to do the
00451     // concatenation for us.  Make a memcpy to copy the nul byte with align = 1.
00452     EmitMemCpy(Dst, Src, ConstantInt::get(TD->getIntPtrType(), Len+1), 1, B);
00453     return Dst;
00454   }
00455 };
00456 
00457 //===---------------------------------------===//
00458 // 'strchr' Optimizations
00459 
00460 struct VISIBILITY_HIDDEN StrChrOpt : public LibCallOptimization {
00461   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00462     // Verify the "strchr" function prototype.
00463     const FunctionType *FT = Callee->getFunctionType();
00464     if (FT->getNumParams() != 2 ||
00465         FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
00466         FT->getParamType(0) != FT->getReturnType())
00467       return 0;
00468     
00469     Value *SrcStr = CI->getOperand(1);
00470     
00471     // If the second operand is non-constant, see if we can compute the length
00472     // of the input string and turn this into memchr.
00473     ConstantInt *CharC = dyn_cast<ConstantInt>(CI->getOperand(2));
00474     if (CharC == 0) {
00475       uint64_t Len = GetStringLength(SrcStr);
00476       if (Len == 0 || FT->getParamType(1) != Type::Int32Ty) // memchr needs i32.
00477         return 0;
00478       
00479       return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
00480                         ConstantInt::get(TD->getIntPtrType(), Len), B);
00481     }
00482 
00483     // Otherwise, the character is a constant, see if the first argument is
00484     // a string literal.  If so, we can constant fold.
00485     std::string Str;
00486     if (!GetConstantStringInfo(SrcStr, Str))
00487       return 0;
00488     
00489     // strchr can find the nul character.
00490     Str += '\0';
00491     char CharValue = CharC->getSExtValue();
00492     
00493     // Compute the offset.
00494     uint64_t i = 0;
00495     while (1) {
00496       if (i == Str.size())    // Didn't find the char.  strchr returns null.
00497         return Constant::getNullValue(CI->getType());
00498       // Did we find our match?
00499       if (Str[i] == CharValue)
00500         break;
00501       ++i;
00502     }
00503     
00504     // strchr(s+n,c)  -> gep(s+n+i,c)
00505     Value *Idx = ConstantInt::get(Type::Int64Ty, i);
00506     return B.CreateGEP(SrcStr, Idx, "strchr");
00507   }
00508 };
00509 
00510 //===---------------------------------------===//
00511 // 'strcmp' Optimizations
00512 
00513 struct VISIBILITY_HIDDEN StrCmpOpt : public LibCallOptimization {
00514   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00515     // Verify the "strcmp" function prototype.
00516     const FunctionType *FT = Callee->getFunctionType();
00517     if (FT->getNumParams() != 2 || FT->getReturnType() != Type::Int32Ty ||
00518         FT->getParamType(0) != FT->getParamType(1) ||
00519         FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty))
00520       return 0;
00521     
00522     Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
00523     if (Str1P == Str2P)      // strcmp(x,x)  -> 0
00524       return ConstantInt::get(CI->getType(), 0);
00525     
00526     std::string Str1, Str2;
00527     bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
00528     bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
00529     
00530     if (HasStr1 && Str1.empty()) // strcmp("", x) -> *x
00531       return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
00532     
00533     if (HasStr2 && Str2.empty()) // strcmp(x,"") -> *x
00534       return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
00535     
00536     // strcmp(x, y)  -> cnst  (if both x and y are constant strings)
00537     if (HasStr1 && HasStr2)
00538       return ConstantInt::get(CI->getType(), strcmp(Str1.c_str(),Str2.c_str()));
00539     return 0;
00540   }
00541 };
00542 
00543 //===---------------------------------------===//
00544 // 'strncmp' Optimizations
00545 
00546 struct VISIBILITY_HIDDEN StrNCmpOpt : public LibCallOptimization {
00547   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00548     // Verify the "strncmp" function prototype.
00549     const FunctionType *FT = Callee->getFunctionType();
00550     if (FT->getNumParams() != 3 || FT->getReturnType() != Type::Int32Ty ||
00551         FT->getParamType(0) != FT->getParamType(1) ||
00552         FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
00553         !isa<IntegerType>(FT->getParamType(2)))
00554       return 0;
00555     
00556     Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
00557     if (Str1P == Str2P)      // strncmp(x,x,n)  -> 0
00558       return ConstantInt::get(CI->getType(), 0);
00559     
00560     // Get the length argument if it is constant.
00561     uint64_t Length;
00562     if (ConstantInt *LengthArg = dyn_cast<ConstantInt>(CI->getOperand(3)))
00563       Length = LengthArg->getZExtValue();
00564     else
00565       return 0;
00566     
00567     if (Length == 0) // strncmp(x,y,0)   -> 0
00568       return ConstantInt::get(CI->getType(), 0);
00569     
00570     std::string Str1, Str2;
00571     bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
00572     bool HasStr2 = GetConstantStringInfo(Str2P, Str2);
00573     
00574     if (HasStr1 && Str1.empty())  // strncmp("", x, n) -> *x
00575       return B.CreateZExt(B.CreateLoad(Str2P, "strcmpload"), CI->getType());
00576     
00577     if (HasStr2 && Str2.empty())  // strncmp(x, "", n) -> *x
00578       return B.CreateZExt(B.CreateLoad(Str1P, "strcmpload"), CI->getType());
00579     
00580     // strncmp(x, y)  -> cnst  (if both x and y are constant strings)
00581     if (HasStr1 && HasStr2)
00582       return ConstantInt::get(CI->getType(),
00583                               strncmp(Str1.c_str(), Str2.c_str(), Length));
00584     return 0;
00585   }
00586 };
00587 
00588 
00589 //===---------------------------------------===//
00590 // 'strcpy' Optimizations
00591 
00592 struct VISIBILITY_HIDDEN StrCpyOpt : public LibCallOptimization {
00593   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00594     // Verify the "strcpy" function prototype.
00595     const FunctionType *FT = Callee->getFunctionType();
00596     if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
00597         FT->getParamType(0) != FT->getParamType(1) ||
00598         FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty))
00599       return 0;
00600     
00601     Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
00602     if (Dst == Src)      // strcpy(x,x)  -> x
00603       return Src;
00604     
00605     // See if we can get the length of the input string.
00606     uint64_t Len = GetStringLength(Src);
00607     if (Len == 0) return 0;
00608     
00609     // We have enough information to now generate the memcpy call to do the
00610     // concatenation for us.  Make a memcpy to copy the nul byte with align = 1.
00611     EmitMemCpy(Dst, Src, ConstantInt::get(TD->getIntPtrType(), Len), 1, B);
00612     return Dst;
00613   }
00614 };
00615 
00616 
00617 
00618 //===---------------------------------------===//
00619 // 'strlen' Optimizations
00620 
00621 struct VISIBILITY_HIDDEN StrLenOpt : public LibCallOptimization {
00622   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00623     const FunctionType *FT = Callee->getFunctionType();
00624     if (FT->getNumParams() != 1 ||
00625         FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
00626         !isa<IntegerType>(FT->getReturnType()))
00627       return 0;
00628     
00629     Value *Src = CI->getOperand(1);
00630 
00631     // Constant folding: strlen("xyz") -> 3
00632     if (uint64_t Len = GetStringLength(Src))
00633       return ConstantInt::get(CI->getType(), Len-1);
00634 
00635     // Handle strlen(p) != 0.
00636     if (!IsOnlyUsedInZeroEqualityComparison(CI)) return 0;
00637 
00638     // strlen(x) != 0 --> *x != 0
00639     // strlen(x) == 0 --> *x == 0
00640     return B.CreateZExt(B.CreateLoad(Src, "strlenfirst"), CI->getType());
00641   }
00642 };
00643 
00644 //===---------------------------------------===//
00645 // 'memcmp' Optimizations
00646 
00647 struct VISIBILITY_HIDDEN MemCmpOpt : public LibCallOptimization {
00648   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00649     const FunctionType *FT = Callee->getFunctionType();
00650     if (FT->getNumParams() != 3 || !isa<PointerType>(FT->getParamType(0)) ||
00651         !isa<PointerType>(FT->getParamType(1)) ||
00652         FT->getReturnType() != Type::Int32Ty)
00653       return 0;
00654 
00655     Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
00656 
00657     if (LHS == RHS)  // memcmp(s,s,x) -> 0
00658       return Constant::getNullValue(CI->getType());
00659 
00660     // Make sure we have a constant length.
00661     ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
00662     if (!LenC) return 0;
00663     uint64_t Len = LenC->getZExtValue();
00664 
00665     if (Len == 0) // memcmp(s1,s2,0) -> 0
00666       return Constant::getNullValue(CI->getType());
00667 
00668     if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
00669       Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
00670       Value *RHSV = B.CreateLoad(CastToCStr(RHS, B), "rhsv");
00671       return B.CreateZExt(B.CreateSub(LHSV, RHSV, "chardiff"), CI->getType());
00672     }
00673 
00674     // memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS)  != 0
00675     // memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS)  != 0
00676     if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
00677       const Type *PTy = PointerType::getUnqual(Len == 2 ?
00678                                                Type::Int16Ty : Type::Int32Ty);
00679       LHS = B.CreateBitCast(LHS, PTy, "tmp");
00680       RHS = B.CreateBitCast(RHS, PTy, "tmp");
00681       LoadInst *LHSV = B.CreateLoad(LHS, "lhsv");
00682       LoadInst *RHSV = B.CreateLoad(RHS, "rhsv");
00683       LHSV->setAlignment(1); RHSV->setAlignment(1);  // Unaligned loads.
00684       return B.CreateZExt(B.CreateXor(LHSV, RHSV, "shortdiff"), CI->getType());
00685     }
00686 
00687     return 0;
00688   }
00689 };
00690 
00691 //===---------------------------------------===//
00692 // 'memcpy' Optimizations
00693 
00694 struct VISIBILITY_HIDDEN MemCpyOpt : public LibCallOptimization {
00695   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00696     const FunctionType *FT = Callee->getFunctionType();
00697     if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
00698         !isa<PointerType>(FT->getParamType(0)) ||
00699         !isa<PointerType>(FT->getParamType(1)) ||
00700         FT->getParamType(2) != TD->getIntPtrType())
00701       return 0;
00702 
00703     // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
00704     EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 1, B);
00705     return CI->getOperand(1);
00706   }
00707 };
00708 
00709 //===----------------------------------------------------------------------===//
00710 // Math Library Optimizations
00711 //===----------------------------------------------------------------------===//
00712 
00713 //===---------------------------------------===//
00714 // 'pow*' Optimizations
00715 
00716 struct VISIBILITY_HIDDEN PowOpt : public LibCallOptimization {
00717   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00718     const FunctionType *FT = Callee->getFunctionType();
00719     // Just make sure this has 2 arguments of the same FP type, which match the
00720     // result type.
00721     if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
00722         FT->getParamType(0) != FT->getParamType(1) ||
00723         !FT->getParamType(0)->isFloatingPoint())
00724       return 0;
00725     
00726     Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
00727     if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) {
00728       if (Op1C->isExactlyValue(1.0))  // pow(1.0, x) -> 1.0
00729         return Op1C;
00730       if (Op1C->isExactlyValue(2.0))  // pow(2.0, x) -> exp2(x)
00731         return EmitUnaryFloatFnCall(Op2, "exp2", B);
00732     }
00733     
00734     ConstantFP *Op2C = dyn_cast<ConstantFP>(Op2);
00735     if (Op2C == 0) return 0;
00736     
00737     if (Op2C->getValueAPF().isZero())  // pow(x, 0.0) -> 1.0
00738       return ConstantFP::get(CI->getType(), 1.0);
00739     
00740     if (Op2C->isExactlyValue(0.5)) {
00741       // FIXME: This is not safe for -0.0 and -inf.  This can only be done when
00742       // 'unsafe' math optimizations are allowed.
00743       // x    pow(x, 0.5)  sqrt(x)
00744       // ---------------------------------------------
00745       // -0.0    +0.0       -0.0
00746       // -inf    +inf       NaN
00747 #if 0
00748       // pow(x, 0.5) -> sqrt(x)
00749       return B.CreateCall(get_sqrt(), Op1, "sqrt");
00750 #endif
00751     }
00752     
00753     if (Op2C->isExactlyValue(1.0))  // pow(x, 1.0) -> x
00754       return Op1;
00755     if (Op2C->isExactlyValue(2.0))  // pow(x, 2.0) -> x*x
00756       return B.CreateMul(Op1, Op1, "pow2");
00757     if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
00758       return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), Op1, "powrecip");
00759     return 0;
00760   }
00761 };
00762 
00763 //===---------------------------------------===//
00764 // 'exp2' Optimizations
00765 
00766 struct VISIBILITY_HIDDEN Exp2Opt : public LibCallOptimization {
00767   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00768     const FunctionType *FT = Callee->getFunctionType();
00769     // Just make sure this has 1 argument of FP type, which matches the
00770     // result type.
00771     if (FT->getNumParams() != 1 || FT->getReturnType() != FT->getParamType(0) ||
00772         !FT->getParamType(0)->isFloatingPoint())
00773       return 0;
00774     
00775     Value *Op = CI->getOperand(1);
00776     // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x))  if sizeof(x) <= 32
00777     // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x))  if sizeof(x) < 32
00778     Value *LdExpArg = 0;
00779     if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
00780       if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
00781         LdExpArg = B.CreateSExt(OpC->getOperand(0), Type::Int32Ty, "tmp");
00782     } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
00783       if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
00784         LdExpArg = B.CreateZExt(OpC->getOperand(0), Type::Int32Ty, "tmp");
00785     }
00786     
00787     if (LdExpArg) {
00788       const char *Name;
00789       if (Op->getType() == Type::FloatTy)
00790         Name = "ldexpf";
00791       else if (Op->getType() == Type::DoubleTy)
00792         Name = "ldexp";
00793       else
00794         Name = "ldexpl";
00795 
00796       Constant *One = ConstantFP::get(APFloat(1.0f));
00797       if (Op->getType() != Type::FloatTy)
00798         One = ConstantExpr::getFPExtend(One, Op->getType());
00799 
00800       Module *M = Caller->getParent();
00801       Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
00802                                              Op->getType(), Type::Int32Ty,NULL);
00803       return B.CreateCall2(Callee, One, LdExpArg);
00804     }
00805     return 0;
00806   }
00807 };
00808     
00809 
00810 //===---------------------------------------===//
00811 // Double -> Float Shrinking Optimizations for Unary Functions like 'floor'
00812 
00813 struct VISIBILITY_HIDDEN UnaryDoubleFPOpt : public LibCallOptimization {
00814   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00815     const FunctionType *FT = Callee->getFunctionType();
00816     if (FT->getNumParams() != 1 || FT->getReturnType() != Type::DoubleTy ||
00817         FT->getParamType(0) != Type::DoubleTy)
00818       return 0;
00819     
00820     // If this is something like 'floor((double)floatval)', convert to floorf.
00821     FPExtInst *Cast = dyn_cast<FPExtInst>(CI->getOperand(1));
00822     if (Cast == 0 || Cast->getOperand(0)->getType() != Type::FloatTy)
00823       return 0;
00824 
00825     // floor((double)floatval) -> (double)floorf(floatval)
00826     Value *V = Cast->getOperand(0);
00827     V = EmitUnaryFloatFnCall(V, Callee->getNameStart(), B);
00828     return B.CreateFPExt(V, Type::DoubleTy);
00829   }
00830 };
00831 
00832 //===----------------------------------------------------------------------===//
00833 // Integer Optimizations
00834 //===----------------------------------------------------------------------===//
00835 
00836 //===---------------------------------------===//
00837 // 'ffs*' Optimizations
00838 
00839 struct VISIBILITY_HIDDEN FFSOpt : public LibCallOptimization {
00840   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
00841     const FunctionType *FT = Callee->getFunctionType();
00842     // Just make sure this has 2 arguments of the same FP type, which match the
00843     // result type.
00844     if (FT->getNumParams() != 1 || FT->getReturnType() != Type::Int32Ty ||
00845         !isa<IntegerType>(FT->getParamType(0)))
00846       return 0;
00847     
00848     Value *Op = CI->getOperand(1);
00849     
00850     // Constant fold.
00851     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
00852       if (CI->getValue() == 0)  // ffs(0) -> 0.
00853         return Constant::getNullValue(CI->getType());
00854       return ConstantInt::get(Type::Int32Ty, // ffs(c) -> cttz(c)+1
00855                               CI->getValue().countTrailingZeros()+1);
00856     }
00857     
00858     // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
00859     const Type *ArgType = Op->getType();
00860     Value *F = Intrinsic::getDeclaration(Callee->getParent(),
00861                                          Intrinsic::cttz, &ArgType, 1);
00862     Value *V = B.CreateCall(F, Op, "cttz");
00863     V = B.CreateAdd(V, ConstantInt::get(Type::Int32Ty, 1), "tmp");
00864     V = B.CreateIntCast(V, Type::Int32Ty, false, "tmp");
00865     
00866     Value *Cond