De-constify Types in FunctionType::get().
[oota-llvm.git] / lib / Transforms / Scalar / SimplifyLibCalls.cpp
index 81460bcf4de704eacf2c8b38dbc986e383ebffbe..66c302844144334dd392e8d3301474648a97b24d 100644 (file)
 #include "llvm/Support/IRBuilder.h"
 #include "llvm/Analysis/ValueTracking.h"
 #include "llvm/Target/TargetData.h"
+#include "llvm/Target/TargetLibraryInfo.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/Config/config.h"
+#include "llvm/Config/config.h"            // FIXME: Shouldn't depend on host!
 using namespace llvm;
 
 STATISTIC(NumSimplified, "Number of library calls simplified");
@@ -48,6 +49,7 @@ class LibCallOptimization {
 protected:
   Function *Caller;
   const TargetData *TD;
+  const TargetLibraryInfo *TLI;
   LLVMContext* Context;
 public:
   LibCallOptimization() { }
@@ -61,9 +63,11 @@ public:
   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
     =0;
 
-  Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
+  Value *OptimizeCall(CallInst *CI, const TargetData *TD,
+                      const TargetLibraryInfo *TLI, IRBuilder<> &B) {
     Caller = CI->getParent()->getParent();
     this->TD = TD;
+    this->TLI = TLI;
     if (CI->getCalledFunction())
       Context = &CI->getCalledFunction()->getContext();
 
@@ -96,6 +100,15 @@ static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
   }
   return true;
 }
+static bool CallHasFloatingPointArgument(const CallInst *CI) {
+  for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
+       it != e; ++it) {
+    if ((*it)->getType()->isFloatingPointTy())
+      return true;
+  }
+  return false;
+}
 
 /// IsOnlyUsedInEqualityComparison - Return true if it is only used in equality
 /// comparisons with With.
@@ -123,7 +136,7 @@ struct StrCatOpt : public LibCallOptimization {
     // Verify the "strcat" function prototype.
     const FunctionType *FT = Callee->getFunctionType();
     if (FT->getNumParams() != 2 ||
-        FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
+        FT->getReturnType() != B.getInt8PtrTy() ||
         FT->getParamType(0) != FT->getReturnType() ||
         FT->getParamType(1) != FT->getReturnType())
       return 0;
@@ -160,9 +173,8 @@ struct StrCatOpt : public LibCallOptimization {
 
     // We have enough information to now generate the memcpy call to do the
     // concatenation for us.  Make a memcpy to copy the nul byte with align = 1.
-    EmitMemCpy(CpyDst, Src,
-               ConstantInt::get(TD->getIntPtrType(*Context), Len+1),
-                                1, false, B, TD);
+    B.CreateMemCpy(CpyDst, Src,
+                   ConstantInt::get(TD->getIntPtrType(*Context), Len + 1), 1);
   }
 };
 
@@ -174,7 +186,7 @@ struct StrNCatOpt : public StrCatOpt {
     // Verify the "strncat" function prototype.
     const FunctionType *FT = Callee->getFunctionType();
     if (FT->getNumParams() != 3 ||
-        FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
+        FT->getReturnType() != B.getInt8PtrTy() ||
         FT->getParamType(0) != FT->getReturnType() ||
         FT->getParamType(1) != FT->getReturnType() ||
         !FT->getParamType(2)->isIntegerTy())
@@ -222,8 +234,9 @@ struct StrChrOpt : public LibCallOptimization {
     // Verify the "strchr" function prototype.
     const FunctionType *FT = Callee->getFunctionType();
     if (FT->getNumParams() != 2 ||
-        FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
-        FT->getParamType(0) != FT->getReturnType())
+        FT->getReturnType() != B.getInt8PtrTy() ||
+        FT->getParamType(0) != FT->getReturnType() ||
+        !FT->getParamType(1)->isIntegerTy(32))
       return 0;
 
     Value *SrcStr = CI->getArgOperand(0);
@@ -259,8 +272,7 @@ struct StrChrOpt : public LibCallOptimization {
       return Constant::getNullValue(CI->getType());
 
     // strchr(s+n,c)  -> gep(s+n+i,c)
-    Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
-    return B.CreateGEP(SrcStr, Idx, "strchr");
+    return B.CreateGEP(SrcStr, B.getInt64(I), "strchr");
   }
 };
 
@@ -272,8 +284,9 @@ struct StrRChrOpt : public LibCallOptimization {
     // Verify the "strrchr" function prototype.
     const FunctionType *FT = Callee->getFunctionType();
     if (FT->getNumParams() != 2 ||
-        FT->getReturnType() != Type::getInt8PtrTy(*Context) ||
-        FT->getParamType(0) != FT->getReturnType())
+        FT->getReturnType() != B.getInt8PtrTy() ||
+        FT->getParamType(0) != FT->getReturnType() ||
+        !FT->getParamType(1)->isIntegerTy(32))
       return 0;
 
     Value *SrcStr = CI->getArgOperand(0);
@@ -300,8 +313,7 @@ struct StrRChrOpt : public LibCallOptimization {
       return Constant::getNullValue(CI->getType());
 
     // strrchr(s+n,c) -> gep(s+n+i,c)
-    Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
-    return B.CreateGEP(SrcStr, Idx, "strrchr");
+    return B.CreateGEP(SrcStr, B.getInt64(I), "strrchr");
   }
 };
 
@@ -315,7 +327,7 @@ struct StrCmpOpt : public LibCallOptimization {
     if (FT->getNumParams() != 2 ||
         !FT->getReturnType()->isIntegerTy(32) ||
         FT->getParamType(0) != FT->getParamType(1) ||
-        FT->getParamType(0) != Type::getInt8PtrTy(*Context))
+        FT->getParamType(0) != B.getInt8PtrTy())
       return 0;
 
     Value *Str1P = CI->getArgOperand(0), *Str2P = CI->getArgOperand(1);
@@ -363,7 +375,7 @@ struct StrNCmpOpt : public LibCallOptimization {
     if (FT->getNumParams() != 3 ||
         !FT->getReturnType()->isIntegerTy(32) ||
         FT->getParamType(0) != FT->getParamType(1) ||
-        FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
+        FT->getParamType(0) != B.getInt8PtrTy() ||
         !FT->getParamType(2)->isIntegerTy())
       return 0;
 
@@ -418,7 +430,7 @@ struct StrCpyOpt : public LibCallOptimization {
     if (FT->getNumParams() != NumParams ||
         FT->getReturnType() != FT->getParamType(0) ||
         FT->getParamType(0) != FT->getParamType(1) ||
-        FT->getParamType(0) != Type::getInt8PtrTy(*Context))
+        FT->getParamType(0) != B.getInt8PtrTy())
       return 0;
 
     Value *Dst = CI->getArgOperand(0), *Src = CI->getArgOperand(1);
@@ -439,9 +451,8 @@ struct StrCpyOpt : public LibCallOptimization {
                     ConstantInt::get(TD->getIntPtrType(*Context), Len),
                     CI->getArgOperand(2), B, TD);
     else
-      EmitMemCpy(Dst, Src,
-                 ConstantInt::get(TD->getIntPtrType(*Context), Len),
-                                  1, false, B, TD);
+      B.CreateMemCpy(Dst, Src,
+                     ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
     return Dst;
   }
 };
@@ -454,7 +465,7 @@ struct StrNCpyOpt : public LibCallOptimization {
     const FunctionType *FT = Callee->getFunctionType();
     if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
         FT->getParamType(0) != FT->getParamType(1) ||
-        FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
+        FT->getParamType(0) != B.getInt8PtrTy() ||
         !FT->getParamType(2)->isIntegerTy())
       return 0;
 
@@ -469,8 +480,7 @@ struct StrNCpyOpt : public LibCallOptimization {
 
     if (SrcLen == 0) {
       // strncpy(x, "", y) -> memset(x, '\0', y, 1)
-      EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
-                 LenOp, false, B, TD);
+      B.CreateMemSet(Dst, B.getInt8('\0'), LenOp, 1);
       return Dst;
     }
 
@@ -489,9 +499,8 @@ struct StrNCpyOpt : public LibCallOptimization {
     if (Len > SrcLen+1) return 0;
 
     // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
-    EmitMemCpy(Dst, Src,
-               ConstantInt::get(TD->getIntPtrType(*Context), Len),
-                                1, false, B, TD);
+    B.CreateMemCpy(Dst, Src,
+                   ConstantInt::get(TD->getIntPtrType(*Context), Len), 1);
 
     return Dst;
   }
@@ -504,7 +513,7 @@ struct StrLenOpt : public LibCallOptimization {
   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
     const FunctionType *FT = Callee->getFunctionType();
     if (FT->getNumParams() != 1 ||
-        FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
+        FT->getParamType(0) != B.getInt8PtrTy() ||
         !FT->getReturnType()->isIntegerTy())
       return 0;
 
@@ -530,7 +539,7 @@ struct StrPBrkOpt : public LibCallOptimization {
   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
     const FunctionType *FT = Callee->getFunctionType();
     if (FT->getNumParams() != 2 ||
-        FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
+        FT->getParamType(0) != B.getInt8PtrTy() ||
         FT->getParamType(1) != FT->getParamType(0) ||
         FT->getReturnType() != FT->getParamType(0))
       return 0;
@@ -550,8 +559,7 @@ struct StrPBrkOpt : public LibCallOptimization {
       if (I == std::string::npos) // No match.
         return Constant::getNullValue(CI->getType());
 
-      Value *Idx = ConstantInt::get(Type::getInt64Ty(*Context), I);
-      return B.CreateGEP(CI->getArgOperand(0), Idx, "strpbrk");
+      return B.CreateGEP(CI->getArgOperand(0), B.getInt64(I), "strpbrk");
     }
 
     // strpbrk(s, "a") -> strchr(s, 'a')
@@ -575,7 +583,8 @@ struct StrToOpt : public LibCallOptimization {
 
     Value *EndPtr = CI->getArgOperand(1);
     if (isa<ConstantPointerNull>(EndPtr)) {
-      CI->setOnlyReadsMemory();
+      // With a null EndPtr, this function won't capture the main argument.
+      // It would be readonly too, except that it still may write to errno.
       CI->addAttribute(1, Attribute::NoCapture);
     }
 
@@ -590,7 +599,7 @@ struct StrSpnOpt : public LibCallOptimization {
   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
     const FunctionType *FT = Callee->getFunctionType();
     if (FT->getNumParams() != 2 ||
-        FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
+        FT->getParamType(0) != B.getInt8PtrTy() ||
         FT->getParamType(1) != FT->getParamType(0) ||
         !FT->getReturnType()->isIntegerTy())
       return 0;
@@ -619,7 +628,7 @@ struct StrCSpnOpt : public LibCallOptimization {
   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
     const FunctionType *FT = Callee->getFunctionType();
     if (FT->getNumParams() != 2 ||
-        FT->getParamType(0) != Type::getInt8PtrTy(*Context) ||
+        FT->getParamType(0) != B.getInt8PtrTy() ||
         FT->getParamType(1) != FT->getParamType(0) ||
         !FT->getReturnType()->isIntegerTy())
       return 0;
@@ -772,8 +781,8 @@ struct MemCpyOpt : public LibCallOptimization {
       return 0;
 
     // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
-    EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
-               CI->getArgOperand(2), 1, false, B, TD);
+    B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
+                   CI->getArgOperand(2), 1);
     return CI->getArgOperand(0);
   }
 };
@@ -794,8 +803,8 @@ struct MemMoveOpt : public LibCallOptimization {
       return 0;
 
     // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
-    EmitMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
-                CI->getArgOperand(2), 1, false, B, TD);
+    B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
+                    CI->getArgOperand(2), 1);
     return CI->getArgOperand(0);
   }
 };
@@ -816,9 +825,8 @@ struct MemSetOpt : public LibCallOptimization {
       return 0;
 
     // memset(p, v, n) -> llvm.memset(p, v, n, 1)
-    Value *Val = B.CreateIntCast(CI->getArgOperand(1),
-                                 Type::getInt8Ty(*Context), false);
-    EmitMemSet(CI->getArgOperand(0), Val,  CI->getArgOperand(2), false, B, TD);
+    Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), false);
+    B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
     return CI->getArgOperand(0);
   }
 };
@@ -900,12 +908,10 @@ struct Exp2Opt : public LibCallOptimization {
     Value *LdExpArg = 0;
     if (SIToFPInst *OpC = dyn_cast<SIToFPInst>(Op)) {
       if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() <= 32)
-        LdExpArg = B.CreateSExt(OpC->getOperand(0),
-                                Type::getInt32Ty(*Context), "tmp");
+        LdExpArg = B.CreateSExt(OpC->getOperand(0), B.getInt32Ty(), "tmp");
     } else if (UIToFPInst *OpC = dyn_cast<UIToFPInst>(Op)) {
       if (OpC->getOperand(0)->getType()->getPrimitiveSizeInBits() < 32)
-        LdExpArg = B.CreateZExt(OpC->getOperand(0),
-                                Type::getInt32Ty(*Context), "tmp");
+        LdExpArg = B.CreateZExt(OpC->getOperand(0), B.getInt32Ty(), "tmp");
     }
 
     if (LdExpArg) {
@@ -924,7 +930,7 @@ struct Exp2Opt : public LibCallOptimization {
       Module *M = Caller->getParent();
       Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
                                              Op->getType(),
-                                             Type::getInt32Ty(*Context),NULL);
+                                             B.getInt32Ty(), NULL);
       CallInst *CI = B.CreateCall2(Callee, One, LdExpArg);
       if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
         CI->setCallingConv(F->getCallingConv());
@@ -954,7 +960,7 @@ struct UnaryDoubleFPOpt : public LibCallOptimization {
     Value *V = Cast->getOperand(0);
     V = EmitUnaryFloatFnCall(V, Callee->getName().data(), B,
                              Callee->getAttributes());
-    return B.CreateFPExt(V, Type::getDoubleTy(*Context));
+    return B.CreateFPExt(V, B.getDoubleTy());
   }
 };
 
@@ -981,21 +987,20 @@ struct FFSOpt : public LibCallOptimization {
     if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
       if (CI->getValue() == 0)  // ffs(0) -> 0.
         return Constant::getNullValue(CI->getType());
-      return ConstantInt::get(Type::getInt32Ty(*Context), // ffs(c) -> cttz(c)+1
-                              CI->getValue().countTrailingZeros()+1);
+      // ffs(c) -> cttz(c)+1
+      return B.getInt32(CI->getValue().countTrailingZeros() + 1);
     }
 
     // ffs(x) -> x != 0 ? (i32)llvm.cttz(x)+1 : 0
-    const Type *ArgType = Op->getType();
+    Type *ArgType = Op->getType();
     Value *F = Intrinsic::getDeclaration(Callee->getParent(),
                                          Intrinsic::cttz, &ArgType, 1);
     Value *V = B.CreateCall(F, Op, "cttz");
     V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
-    V = B.CreateIntCast(V, Type::getInt32Ty(*Context), false, "tmp");
+    V = B.CreateIntCast(V, B.getInt32Ty(), false, "tmp");
 
     Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
-    return B.CreateSelect(Cond, V,
-                          ConstantInt::get(Type::getInt32Ty(*Context), 0));
+    return B.CreateSelect(Cond, V, B.getInt32(0));
   }
 };
 
@@ -1012,10 +1017,8 @@ struct IsDigitOpt : public LibCallOptimization {
 
     // isdigit(c) -> (c-'0') <u 10
     Value *Op = CI->getArgOperand(0);
-    Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
-                     "isdigittmp");
-    Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
-                         "isdigit");
+    Op = B.CreateSub(Op, B.getInt32('0'), "isdigittmp");
+    Op = B.CreateICmpULT(Op, B.getInt32(10), "isdigit");
     return B.CreateZExt(Op, CI->getType());
   }
 };
@@ -1033,8 +1036,7 @@ struct IsAsciiOpt : public LibCallOptimization {
 
     // isascii(c) -> c <u 128
     Value *Op = CI->getArgOperand(0);
-    Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
-                         "isascii");
+    Op = B.CreateICmpULT(Op, B.getInt32(128), "isascii");
     return B.CreateZExt(Op, CI->getType());
   }
 };
@@ -1052,8 +1054,7 @@ struct AbsOpt : public LibCallOptimization {
 
     // abs(x) -> x >s -1 ? x : -x
     Value *Op = CI->getArgOperand(0);
-    Value *Pos = B.CreateICmpSGT(Op,
-                             Constant::getAllOnesValue(Op->getType()),
+    Value *Pos = B.CreateICmpSGT(Op, Constant::getAllOnesValue(Op->getType()),
                                  "ispos");
     Value *Neg = B.CreateNeg(Op, "neg");
     return B.CreateSelect(Pos, Op, Neg);
@@ -1086,14 +1087,8 @@ struct ToAsciiOpt : public LibCallOptimization {
 // 'printf' Optimizations
 
 struct PrintFOpt : public LibCallOptimization {
-  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
-    // Require one fixed pointer argument and an integer/void result.
-    const FunctionType *FT = Callee->getFunctionType();
-    if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
-        !(FT->getReturnType()->isIntegerTy() ||
-          FT->getReturnType()->isVoidTy()))
-      return 0;
-
+  Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
+                                   IRBuilder<> &B) {
     // Check for a fixed format string.
     std::string FormatStr;
     if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
@@ -1104,11 +1099,15 @@ struct PrintFOpt : public LibCallOptimization {
       return CI->use_empty() ? (Value*)CI :
                                ConstantInt::get(CI->getType(), 0);
 
-    // printf("x") -> putchar('x'), even for '%'.  Return the result of putchar
-    // in case there is an error writing to stdout.
+    // Do not do any of the following transformations if the printf return value
+    // is used, in general the printf return value is not compatible with either
+    // putchar() or puts().
+    if (!CI->use_empty())
+      return 0;
+
+    // printf("x") -> putchar('x'), even for '%'.
     if (FormatStr.size() == 1) {
-      Value *Res = EmitPutChar(ConstantInt::get(Type::getInt32Ty(*Context),
-                                                FormatStr[0]), B, TD);
+      Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD);
       if (CI->use_empty()) return CI;
       return B.CreateIntCast(Res, CI->getType(), true);
     }
@@ -1139,27 +1138,46 @@ struct PrintFOpt : public LibCallOptimization {
 
     // printf("%s\n", str) --> puts(str)
     if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
-        CI->getArgOperand(1)->getType()->isPointerTy() &&
-        CI->use_empty()) {
+        CI->getArgOperand(1)->getType()->isPointerTy()) {
       EmitPutS(CI->getArgOperand(1), B, TD);
       return CI;
     }
     return 0;
   }
+
+  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
+    // Require one fixed pointer argument and an integer/void result.
+    const FunctionType *FT = Callee->getFunctionType();
+    if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
+        !(FT->getReturnType()->isIntegerTy() ||
+          FT->getReturnType()->isVoidTy()))
+      return 0;
+
+    if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
+      return V;
+    }
+
+    // printf(format, ...) -> iprintf(format, ...) if no floating point
+    // arguments.
+    if (TLI->has(LibFunc::iprintf) && !CallHasFloatingPointArgument(CI)) {
+      Module *M = B.GetInsertBlock()->getParent()->getParent();
+      Constant *IPrintFFn =
+        M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
+      CallInst *New = cast<CallInst>(CI->clone());
+      New->setCalledFunction(IPrintFFn);
+      B.Insert(New);
+      return New;
+    }
+    return 0;
+  }
 };
 
 //===---------------------------------------===//
 // 'sprintf' Optimizations
 
 struct SPrintFOpt : public LibCallOptimization {
-  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
-    // Require two fixed pointer arguments and an integer result.
-    const FunctionType *FT = Callee->getFunctionType();
-    if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
-        !FT->getParamType(1)->isPointerTy() ||
-        !FT->getReturnType()->isIntegerTy())
-      return 0;
-
+  Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
+                                   IRBuilder<> &B) {
     // Check for a fixed format string.
     std::string FormatStr;
     if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
@@ -1177,9 +1195,9 @@ struct SPrintFOpt : public LibCallOptimization {
       if (!TD) return 0;
 
       // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
-      EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),   // Copy the
-                 ConstantInt::get(TD->getIntPtrType(*Context), // nul byte.
-                 FormatStr.size() + 1), 1, false, B, TD);
+      B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
+                     ConstantInt::get(TD->getIntPtrType(*Context), // Copy the
+                                      FormatStr.size() + 1), 1);   // nul byte.
       return ConstantInt::get(CI->getType(), FormatStr.size());
     }
 
@@ -1193,13 +1211,11 @@ struct SPrintFOpt : public LibCallOptimization {
     if (FormatStr[1] == 'c') {
       // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
       if (!CI->getArgOperand(2)->getType()->isIntegerTy()) return 0;
-      Value *V = B.CreateTrunc(CI->getArgOperand(2),
-                               Type::getInt8Ty(*Context), "char");
+      Value *V = B.CreateTrunc(CI->getArgOperand(2), B.getInt8Ty(), "char");
       Value *Ptr = CastToCStr(CI->getArgOperand(0), B);
       B.CreateStore(V, Ptr);
-      Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
-                        "nul");
-      B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
+      Ptr = B.CreateGEP(Ptr, B.getInt32(1), "nul");
+      B.CreateStore(B.getInt8(0), Ptr);
 
       return ConstantInt::get(CI->getType(), 1);
     }
@@ -1215,14 +1231,39 @@ struct SPrintFOpt : public LibCallOptimization {
       Value *IncLen = B.CreateAdd(Len,
                                   ConstantInt::get(Len->getType(), 1),
                                   "leninc");
-      EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(2),
-                 IncLen, 1, false, B, TD);
+      B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(2), IncLen, 1);
 
       // The sprintf result is the unincremented number of bytes in the string.
       return B.CreateIntCast(Len, CI->getType(), false);
     }
     return 0;
   }
+
+  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
+    // Require two fixed pointer arguments and an integer result.
+    const FunctionType *FT = Callee->getFunctionType();
+    if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
+        !FT->getParamType(1)->isPointerTy() ||
+        !FT->getReturnType()->isIntegerTy())
+      return 0;
+
+    if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
+      return V;
+    }
+
+    // sprintf(str, format, ...) -> siprintf(str, format, ...) if no floating
+    // point arguments.
+    if (TLI->has(LibFunc::siprintf) && !CallHasFloatingPointArgument(CI)) {
+      Module *M = B.GetInsertBlock()->getParent()->getParent();
+      Constant *SIPrintFFn =
+        M->getOrInsertFunction("siprintf", FT, Callee->getAttributes());
+      CallInst *New = cast<CallInst>(CI->clone());
+      New->setCalledFunction(SIPrintFFn);
+      B.Insert(New);
+      return New;
+    }
+    return 0;
+  }
 };
 
 //===---------------------------------------===//
@@ -1289,14 +1330,8 @@ struct FPutsOpt : public LibCallOptimization {
 // 'fprintf' Optimizations
 
 struct FPrintFOpt : public LibCallOptimization {
-  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
-    // Require two fixed paramters as pointers and integer result.
-    const FunctionType *FT = Callee->getFunctionType();
-    if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
-        !FT->getParamType(1)->isPointerTy() ||
-        !FT->getReturnType()->isIntegerTy())
-      return 0;
-
+  Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
+                                   IRBuilder<> &B) {
     // All the optimizations depend on the format string.
     std::string FormatStr;
     if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr))
@@ -1341,6 +1376,60 @@ struct FPrintFOpt : public LibCallOptimization {
     }
     return 0;
   }
+
+  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
+    // Require two fixed paramters as pointers and integer result.
+    const FunctionType *FT = Callee->getFunctionType();
+    if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() ||
+        !FT->getParamType(1)->isPointerTy() ||
+        !FT->getReturnType()->isIntegerTy())
+      return 0;
+
+    if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
+      return V;
+    }
+
+    // fprintf(stream, format, ...) -> fiprintf(stream, format, ...) if no
+    // floating point arguments.
+    if (TLI->has(LibFunc::fiprintf) && !CallHasFloatingPointArgument(CI)) {
+      Module *M = B.GetInsertBlock()->getParent()->getParent();
+      Constant *FIPrintFFn =
+        M->getOrInsertFunction("fiprintf", FT, Callee->getAttributes());
+      CallInst *New = cast<CallInst>(CI->clone());
+      New->setCalledFunction(FIPrintFFn);
+      B.Insert(New);
+      return New;
+    }
+    return 0;
+  }
+};
+
+//===---------------------------------------===//
+// 'puts' Optimizations
+
+struct PutsOpt : public LibCallOptimization {
+  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
+    // Require one fixed pointer argument and an integer/void result.
+    const FunctionType *FT = Callee->getFunctionType();
+    if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
+        !(FT->getReturnType()->isIntegerTy() ||
+          FT->getReturnType()->isVoidTy()))
+      return 0;
+
+    // Check for a constant string.
+    std::string Str;
+    if (!GetConstantStringInfo(CI->getArgOperand(0), Str))
+      return 0;
+
+    if (Str.empty() && CI->use_empty()) {
+      // puts("") -> putchar('\n')
+      Value *Res = EmitPutChar(B.getInt32('\n'), B, TD);
+      if (CI->use_empty()) return CI;
+      return B.CreateIntCast(Res, CI->getType(), true);
+    }
+
+    return 0;
+  }
 };
 
 } // end anonymous namespace.
@@ -1353,6 +1442,8 @@ namespace {
   /// This pass optimizes well known library functions from libc and libm.
   ///
   class SimplifyLibCalls : public FunctionPass {
+    TargetLibraryInfo *TLI;
+    
     StringMap<LibCallOptimization*> Optimizations;
     // String and Memory LibCall Optimizations
     StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrRChrOpt StrRChr;
@@ -1368,11 +1459,14 @@ namespace {
     // Formatting and IO Optimizations
     SPrintFOpt SPrintF; PrintFOpt PrintF;
     FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
-
+    PutsOpt Puts;
+    
     bool Modified;  // This is only used by doInitialization.
   public:
     static char ID; // Pass identification
-    SimplifyLibCalls() : FunctionPass(ID), StrCpy(false), StrCpyChk(true) {}
+    SimplifyLibCalls() : FunctionPass(ID), StrCpy(false), StrCpyChk(true) {
+      initializeSimplifyLibCallsPass(*PassRegistry::getPassRegistry());
+    }
     void InitOptimizations();
     bool runOnFunction(Function &F);
 
@@ -1383,14 +1477,20 @@ namespace {
     void setDoesNotAlias(Function &F, unsigned n);
     bool doInitialization(Module &M);
 
+    void inferPrototypeAttributes(Function &F);
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AU.addRequired<TargetLibraryInfo>();
     }
   };
-  char SimplifyLibCalls::ID = 0;
 } // end anonymous namespace.
 
-INITIALIZE_PASS(SimplifyLibCalls, "simplify-libcalls",
-                "Simplify well-known library calls", false, false);
+char SimplifyLibCalls::ID = 0;
+
+INITIALIZE_PASS_BEGIN(SimplifyLibCalls, "simplify-libcalls",
+                      "Simplify well-known library calls", false, false)
+INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
+INITIALIZE_PASS_END(SimplifyLibCalls, "simplify-libcalls",
+                    "Simplify well-known library calls", false, false)
 
 // Public interface to the Simplify LibCalls pass.
 FunctionPass *llvm::createSimplifyLibCallsPass() {
@@ -1422,9 +1522,9 @@ void SimplifyLibCalls::InitOptimizations() {
   Optimizations["strcspn"] = &StrCSpn;
   Optimizations["strstr"] = &StrStr;
   Optimizations["memcmp"] = &MemCmp;
-  Optimizations["memcpy"] = &MemCpy;
+  if (TLI->has(LibFunc::memcpy)) Optimizations["memcpy"] = &MemCpy;
   Optimizations["memmove"] = &MemMove;
-  Optimizations["memset"] = &MemSet;
+  if (TLI->has(LibFunc::memset)) Optimizations["memset"] = &MemSet;
 
   // _chk variants of String and Memory LibCall Optimizations.
   Optimizations["__strcpy_chk"] = &StrCpyChk;
@@ -1480,12 +1580,15 @@ void SimplifyLibCalls::InitOptimizations() {
   Optimizations["fwrite"] = &FWrite;
   Optimizations["fputs"] = &FPuts;
   Optimizations["fprintf"] = &FPrintF;
+  Optimizations["puts"] = &Puts;
 }
 
 
 /// runOnFunction - Top level algorithm.
 ///
 bool SimplifyLibCalls::runOnFunction(Function &F) {
+  TLI = &getAnalysis<TargetLibraryInfo>();
+
   if (Optimizations.empty())
     InitOptimizations();
 
@@ -1513,8 +1616,11 @@ bool SimplifyLibCalls::runOnFunction(Function &F) {
       // Set the builder to the instruction after the call.
       Builder.SetInsertPoint(BB, I);
 
+      // Use debug location of CI for all new instructions.
+      Builder.SetCurrentDebugLocation(CI->getDebugLoc());
+
       // Try to optimize this call.
-      Value *Result = LCO->OptimizeCall(CI, TD, Builder);
+      Value *Result = LCO->OptimizeCall(CI, TD, TLI, Builder);
       if (Result == 0) continue;
 
       DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;
@@ -1577,688 +1683,654 @@ void SimplifyLibCalls::setDoesNotAlias(Function &F, unsigned n) {
   }
 }
 
+
+void SimplifyLibCalls::inferPrototypeAttributes(Function &F) {
+  const FunctionType *FTy = F.getFunctionType();
+  
+  StringRef Name = F.getName();
+  switch (Name[0]) {
+  case 's':
+    if (Name == "strlen") {
+      if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setOnlyReadsMemory(F);
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "strchr" ||
+               Name == "strrchr") {
+      if (FTy->getNumParams() != 2 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isIntegerTy())
+        return;
+      setOnlyReadsMemory(F);
+      setDoesNotThrow(F);
+    } else if (Name == "strcpy" ||
+               Name == "stpcpy" ||
+               Name == "strcat" ||
+               Name == "strtol" ||
+               Name == "strtod" ||
+               Name == "strtof" ||
+               Name == "strtoul" ||
+               Name == "strtoll" ||
+               Name == "strtold" ||
+               Name == "strncat" ||
+               Name == "strncpy" ||
+               Name == "strtoull") {
+      if (FTy->getNumParams() < 2 ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "strxfrm") {
+      if (FTy->getNumParams() != 3 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "strcmp" ||
+               Name == "strspn" ||
+               Name == "strncmp" ||
+               Name == "strcspn" ||
+               Name == "strcoll" ||
+               Name == "strcasecmp" ||
+               Name == "strncasecmp") {
+      if (FTy->getNumParams() < 2 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setOnlyReadsMemory(F);
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "strstr" ||
+               Name == "strpbrk") {
+      if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
+        return;
+      setOnlyReadsMemory(F);
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "strtok" ||
+               Name == "strtok_r") {
+      if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "scanf" ||
+               Name == "setbuf" ||
+               Name == "setvbuf") {
+      if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "strdup" ||
+               Name == "strndup") {
+      if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
+          !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "stat" ||
+               Name == "sscanf" ||
+               Name == "sprintf" ||
+               Name == "statvfs") {
+      if (FTy->getNumParams() < 2 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "snprintf") {
+      if (FTy->getNumParams() != 3 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(2)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 3);
+    } else if (Name == "setitimer") {
+      if (FTy->getNumParams() != 3 ||
+          !FTy->getParamType(1)->isPointerTy() ||
+          !FTy->getParamType(2)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 2);
+      setDoesNotCapture(F, 3);
+    } else if (Name == "system") {
+      if (FTy->getNumParams() != 1 ||
+          !FTy->getParamType(0)->isPointerTy())
+        return;
+      // May throw; "system" is a valid pthread cancellation point.
+      setDoesNotCapture(F, 1);
+    }
+    break;
+  case 'm':
+    if (Name == "malloc") {
+      if (FTy->getNumParams() != 1 ||
+          !FTy->getReturnType()->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+    } else if (Name == "memcmp") {
+      if (FTy->getNumParams() != 3 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setOnlyReadsMemory(F);
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "memchr" ||
+               Name == "memrchr") {
+      if (FTy->getNumParams() != 3)
+        return;
+      setOnlyReadsMemory(F);
+      setDoesNotThrow(F);
+    } else if (Name == "modf" ||
+               Name == "modff" ||
+               Name == "modfl" ||
+               Name == "memcpy" ||
+               Name == "memccpy" ||
+               Name == "memmove") {
+      if (FTy->getNumParams() < 2 ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "memalign") {
+      if (!FTy->getReturnType()->isPointerTy())
+        return;
+      setDoesNotAlias(F, 0);
+    } else if (Name == "mkdir" ||
+               Name == "mktime") {
+      if (FTy->getNumParams() == 0 ||
+          !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    }
+    break;
+  case 'r':
+    if (Name == "realloc") {
+      if (FTy->getNumParams() != 2 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getReturnType()->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "read") {
+      if (FTy->getNumParams() != 3 ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      // May throw; "read" is a valid pthread cancellation point.
+      setDoesNotCapture(F, 2);
+    } else if (Name == "rmdir" ||
+               Name == "rewind" ||
+               Name == "remove" ||
+               Name == "realpath") {
+      if (FTy->getNumParams() < 1 ||
+          !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "rename" ||
+               Name == "readlink") {
+      if (FTy->getNumParams() < 2 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    }
+    break;
+  case 'w':
+    if (Name == "write") {
+      if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
+        return;
+      // May throw; "write" is a valid pthread cancellation point.
+      setDoesNotCapture(F, 2);
+    }
+    break;
+  case 'b':
+    if (Name == "bcopy") {
+      if (FTy->getNumParams() != 3 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "bcmp") {
+      if (FTy->getNumParams() != 3 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setOnlyReadsMemory(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "bzero") {
+      if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    }
+    break;
+  case 'c':
+    if (Name == "calloc") {
+      if (FTy->getNumParams() != 2 ||
+          !FTy->getReturnType()->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+    } else if (Name == "chmod" ||
+               Name == "chown" ||
+               Name == "ctermid" ||
+               Name == "clearerr" ||
+               Name == "closedir") {
+      if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    }
+    break;
+  case 'a':
+    if (Name == "atoi" ||
+        Name == "atol" ||
+        Name == "atof" ||
+        Name == "atoll") {
+      if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setOnlyReadsMemory(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "access") {
+      if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    }
+    break;
+  case 'f':
+    if (Name == "fopen") {
+      if (FTy->getNumParams() != 2 ||
+          !FTy->getReturnType()->isPointerTy() ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "fdopen") {
+      if (FTy->getNumParams() != 2 ||
+          !FTy->getReturnType()->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "feof" ||
+               Name == "free" ||
+               Name == "fseek" ||
+               Name == "ftell" ||
+               Name == "fgetc" ||
+               Name == "fseeko" ||
+               Name == "ftello" ||
+               Name == "fileno" ||
+               Name == "fflush" ||
+               Name == "fclose" ||
+               Name == "fsetpos" ||
+               Name == "flockfile" ||
+               Name == "funlockfile" ||
+               Name == "ftrylockfile") {
+      if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "ferror") {
+      if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setOnlyReadsMemory(F);
+    } else if (Name == "fputc" ||
+               Name == "fstat" ||
+               Name == "frexp" ||
+               Name == "frexpf" ||
+               Name == "frexpl" ||
+               Name == "fstatvfs") {
+      if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "fgets") {
+      if (FTy->getNumParams() != 3 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(2)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 3);
+    } else if (Name == "fread" ||
+               Name == "fwrite") {
+      if (FTy->getNumParams() != 4 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(3)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 4);
+    } else if (Name == "fputs" ||
+               Name == "fscanf" ||
+               Name == "fprintf" ||
+               Name == "fgetpos") {
+      if (FTy->getNumParams() < 2 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    }
+    break;
+  case 'g':
+    if (Name == "getc" ||
+        Name == "getlogin_r" ||
+        Name == "getc_unlocked") {
+      if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "getenv") {
+      if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setOnlyReadsMemory(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "gets" ||
+               Name == "getchar") {
+      setDoesNotThrow(F);
+    } else if (Name == "getitimer") {
+      if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "getpwnam") {
+      if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    }
+    break;
+  case 'u':
+    if (Name == "ungetc") {
+      if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "uname" ||
+               Name == "unlink" ||
+               Name == "unsetenv") {
+      if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "utime" ||
+               Name == "utimes") {
+      if (FTy->getNumParams() != 2 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    }
+    break;
+  case 'p':
+    if (Name == "putc") {
+      if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "puts" ||
+               Name == "printf" ||
+               Name == "perror") {
+      if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "pread" ||
+               Name == "pwrite") {
+      if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
+        return;
+      // May throw; these are valid pthread cancellation points.
+      setDoesNotCapture(F, 2);
+    } else if (Name == "putchar") {
+      setDoesNotThrow(F);
+    } else if (Name == "popen") {
+      if (FTy->getNumParams() != 2 ||
+          !FTy->getReturnType()->isPointerTy() ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "pclose") {
+      if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    }
+    break;
+  case 'v':
+    if (Name == "vscanf") {
+      if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "vsscanf" ||
+               Name == "vfscanf") {
+      if (FTy->getNumParams() != 3 ||
+          !FTy->getParamType(1)->isPointerTy() ||
+          !FTy->getParamType(2)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "valloc") {
+      if (!FTy->getReturnType()->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+    } else if (Name == "vprintf") {
+      if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "vfprintf" ||
+               Name == "vsprintf") {
+      if (FTy->getNumParams() != 3 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "vsnprintf") {
+      if (FTy->getNumParams() != 4 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(2)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 3);
+    }
+    break;
+  case 'o':
+    if (Name == "open") {
+      if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      // May throw; "open" is a valid pthread cancellation point.
+      setDoesNotCapture(F, 1);
+    } else if (Name == "opendir") {
+      if (FTy->getNumParams() != 1 ||
+          !FTy->getReturnType()->isPointerTy() ||
+          !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+      setDoesNotCapture(F, 1);
+    }
+    break;
+  case 't':
+    if (Name == "tmpfile") {
+      if (!FTy->getReturnType()->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+    } else if (Name == "times") {
+      if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    }
+    break;
+  case 'h':
+    if (Name == "htonl" ||
+        Name == "htons") {
+      setDoesNotThrow(F);
+      setDoesNotAccessMemory(F);
+    }
+    break;
+  case 'n':
+    if (Name == "ntohl" ||
+        Name == "ntohs") {
+      setDoesNotThrow(F);
+      setDoesNotAccessMemory(F);
+    }
+    break;
+  case 'l':
+    if (Name == "lstat") {
+      if (FTy->getNumParams() != 2 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "lchown") {
+      if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    }
+    break;
+  case 'q':
+    if (Name == "qsort") {
+      if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
+        return;
+      // May throw; places call through function pointer.
+      setDoesNotCapture(F, 4);
+    }
+    break;
+  case '_':
+    if (Name == "__strdup" ||
+        Name == "__strndup") {
+      if (FTy->getNumParams() < 1 ||
+          !FTy->getReturnType()->isPointerTy() ||
+          !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "__strtok_r") {
+      if (FTy->getNumParams() != 3 ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "_IO_getc") {
+      if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "_IO_putc") {
+      if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 2);
+    }
+    break;
+  case 1:
+    if (Name == "\1__isoc99_scanf") {
+      if (FTy->getNumParams() < 1 ||
+          !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "\1stat64" ||
+               Name == "\1lstat64" ||
+               Name == "\1statvfs64" ||
+               Name == "\1__isoc99_sscanf") {
+      if (FTy->getNumParams() < 1 ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "\1fopen64") {
+      if (FTy->getNumParams() != 2 ||
+          !FTy->getReturnType()->isPointerTy() ||
+          !FTy->getParamType(0)->isPointerTy() ||
+          !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+      setDoesNotCapture(F, 1);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "\1fseeko64" ||
+               Name == "\1ftello64") {
+      if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 1);
+    } else if (Name == "\1tmpfile64") {
+      if (!FTy->getReturnType()->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotAlias(F, 0);
+    } else if (Name == "\1fstat64" ||
+               Name == "\1fstatvfs64") {
+      if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
+        return;
+      setDoesNotThrow(F);
+      setDoesNotCapture(F, 2);
+    } else if (Name == "\1open64") {
+      if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
+        return;
+      // May throw; "open" is a valid pthread cancellation point.
+      setDoesNotCapture(F, 1);
+    }
+    break;
+  }
+}
+
 /// doInitialization - Add attributes to well-known functions.
 ///
 bool SimplifyLibCalls::doInitialization(Module &M) {
   Modified = false;
   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
     Function &F = *I;
-    if (!F.isDeclaration())
-      continue;
-
-    if (!F.hasName())
-      continue;
-
-    const FunctionType *FTy = F.getFunctionType();
-
-    StringRef Name = F.getName();
-    switch (Name[0]) {
-      case 's':
-        if (Name == "strlen") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setOnlyReadsMemory(F);
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "strchr" ||
-                   Name == "strrchr") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isIntegerTy())
-            continue;
-          setOnlyReadsMemory(F);
-          setDoesNotThrow(F);
-        } else if (Name == "strcpy" ||
-                   Name == "stpcpy" ||
-                   Name == "strcat" ||
-                   Name == "strtol" ||
-                   Name == "strtod" ||
-                   Name == "strtof" ||
-                   Name == "strtoul" ||
-                   Name == "strtoll" ||
-                   Name == "strtold" ||
-                   Name == "strncat" ||
-                   Name == "strncpy" ||
-                   Name == "strtoull") {
-          if (FTy->getNumParams() < 2 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "strxfrm") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "strcmp" ||
-                   Name == "strspn" ||
-                   Name == "strncmp" ||
-                   Name == "strcspn" ||
-                   Name == "strcoll" ||
-                   Name == "strcasecmp" ||
-                   Name == "strncasecmp") {
-          if (FTy->getNumParams() < 2 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setOnlyReadsMemory(F);
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "strstr" ||
-                   Name == "strpbrk") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setOnlyReadsMemory(F);
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "strtok" ||
-                   Name == "strtok_r") {
-          if (FTy->getNumParams() < 2 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "scanf" ||
-                   Name == "setbuf" ||
-                   Name == "setvbuf") {
-          if (FTy->getNumParams() < 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "strdup" ||
-                   Name == "strndup") {
-          if (FTy->getNumParams() < 1 ||
-              !FTy->getReturnType()->isPointerTy() ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "stat" ||
-                   Name == "sscanf" ||
-                   Name == "sprintf" ||
-                   Name == "statvfs") {
-          if (FTy->getNumParams() < 2 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "snprintf") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(2)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 3);
-        } else if (Name == "setitimer") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(1)->isPointerTy() ||
-              !FTy->getParamType(2)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 2);
-          setDoesNotCapture(F, 3);
-        } else if (Name == "system") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          // May throw; "system" is a valid pthread cancellation point.
-          setDoesNotCapture(F, 1);
-        }
-        break;
-      case 'm':
-        if (Name == "malloc") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getReturnType()->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-        } else if (Name == "memcmp") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setOnlyReadsMemory(F);
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "memchr" ||
-                   Name == "memrchr") {
-          if (FTy->getNumParams() != 3)
-            continue;
-          setOnlyReadsMemory(F);
-          setDoesNotThrow(F);
-        } else if (Name == "modf" ||
-                   Name == "modff" ||
-                   Name == "modfl" ||
-                   Name == "memcpy" ||
-                   Name == "memccpy" ||
-                   Name == "memmove") {
-          if (FTy->getNumParams() < 2 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "memalign") {
-          if (!FTy->getReturnType()->isPointerTy())
-            continue;
-          setDoesNotAlias(F, 0);
-        } else if (Name == "mkdir" ||
-                   Name == "mktime") {
-          if (FTy->getNumParams() == 0 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        }
-        break;
-      case 'r':
-        if (Name == "realloc") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getReturnType()->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "read") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          // May throw; "read" is a valid pthread cancellation point.
-          setDoesNotCapture(F, 2);
-        } else if (Name == "rmdir" ||
-                   Name == "rewind" ||
-                   Name == "remove" ||
-                   Name == "realpath") {
-          if (FTy->getNumParams() < 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "rename" ||
-                   Name == "readlink") {
-          if (FTy->getNumParams() < 2 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        }
-        break;
-      case 'w':
-        if (Name == "write") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          // May throw; "write" is a valid pthread cancellation point.
-          setDoesNotCapture(F, 2);
-        }
-        break;
-      case 'b':
-        if (Name == "bcopy") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "bcmp") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setOnlyReadsMemory(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "bzero") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        }
-        break;
-      case 'c':
-        if (Name == "calloc") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getReturnType()->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-        } else if (Name == "chmod" ||
-                   Name == "chown" ||
-                   Name == "ctermid" ||
-                   Name == "clearerr" ||
-                   Name == "closedir") {
-          if (FTy->getNumParams() == 0 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        }
-        break;
-      case 'a':
-        if (Name == "atoi" ||
-            Name == "atol" ||
-            Name == "atof" ||
-            Name == "atoll") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setOnlyReadsMemory(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "access") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        }
-        break;
-      case 'f':
-        if (Name == "fopen") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getReturnType()->isPointerTy() ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "fdopen") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getReturnType()->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "feof" ||
-                   Name == "free" ||
-                   Name == "fseek" ||
-                   Name == "ftell" ||
-                   Name == "fgetc" ||
-                   Name == "fseeko" ||
-                   Name == "ftello" ||
-                   Name == "fileno" ||
-                   Name == "fflush" ||
-                   Name == "fclose" ||
-                   Name == "fsetpos" ||
-                   Name == "flockfile" ||
-                   Name == "funlockfile" ||
-                   Name == "ftrylockfile") {
-          if (FTy->getNumParams() == 0 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "ferror") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setOnlyReadsMemory(F);
-        } else if (Name == "fputc" ||
-                   Name == "fstat" ||
-                   Name == "frexp" ||
-                   Name == "frexpf" ||
-                   Name == "frexpl" ||
-                   Name == "fstatvfs") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "fgets") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(2)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 3);
-        } else if (Name == "fread" ||
-                   Name == "fwrite") {
-          if (FTy->getNumParams() != 4 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(3)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 4);
-        } else if (Name == "fputs" ||
-                   Name == "fscanf" ||
-                   Name == "fprintf" ||
-                   Name == "fgetpos") {
-          if (FTy->getNumParams() < 2 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        }
-        break;
-      case 'g':
-        if (Name == "getc" ||
-            Name == "getlogin_r" ||
-            Name == "getc_unlocked") {
-          if (FTy->getNumParams() == 0 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "getenv") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setOnlyReadsMemory(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "gets" ||
-                   Name == "getchar") {
-          setDoesNotThrow(F);
-        } else if (Name == "getitimer") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "getpwnam") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        }
-        break;
-      case 'u':
-        if (Name == "ungetc") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "uname" ||
-                   Name == "unlink" ||
-                   Name == "unsetenv") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "utime" ||
-                   Name == "utimes") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        }
-        break;
-      case 'p':
-        if (Name == "putc") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "puts" ||
-                   Name == "printf" ||
-                   Name == "perror") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "pread" ||
-                   Name == "pwrite") {
-          if (FTy->getNumParams() != 4 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          // May throw; these are valid pthread cancellation points.
-          setDoesNotCapture(F, 2);
-        } else if (Name == "putchar") {
-          setDoesNotThrow(F);
-        } else if (Name == "popen") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getReturnType()->isPointerTy() ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "pclose") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        }
-        break;
-      case 'v':
-        if (Name == "vscanf") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "vsscanf" ||
-                   Name == "vfscanf") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(1)->isPointerTy() ||
-              !FTy->getParamType(2)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "valloc") {
-          if (!FTy->getReturnType()->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-        } else if (Name == "vprintf") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "vfprintf" ||
-                   Name == "vsprintf") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "vsnprintf") {
-          if (FTy->getNumParams() != 4 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(2)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 3);
-        }
-        break;
-      case 'o':
-        if (Name == "open") {
-          if (FTy->getNumParams() < 2 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          // May throw; "open" is a valid pthread cancellation point.
-          setDoesNotCapture(F, 1);
-        } else if (Name == "opendir") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getReturnType()->isPointerTy() ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-          setDoesNotCapture(F, 1);
-        }
-        break;
-      case 't':
-        if (Name == "tmpfile") {
-          if (!FTy->getReturnType()->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-        } else if (Name == "times") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        }
-        break;
-      case 'h':
-        if (Name == "htonl" ||
-            Name == "htons") {
-          setDoesNotThrow(F);
-          setDoesNotAccessMemory(F);
-        }
-        break;
-      case 'n':
-        if (Name == "ntohl" ||
-            Name == "ntohs") {
-          setDoesNotThrow(F);
-          setDoesNotAccessMemory(F);
-        }
-        break;
-      case 'l':
-        if (Name == "lstat") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "lchown") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        }
-        break;
-      case 'q':
-        if (Name == "qsort") {
-          if (FTy->getNumParams() != 4 ||
-              !FTy->getParamType(3)->isPointerTy())
-            continue;
-          // May throw; places call through function pointer.
-          setDoesNotCapture(F, 4);
-        }
-        break;
-      case '_':
-        if (Name == "__strdup" ||
-            Name == "__strndup") {
-          if (FTy->getNumParams() < 1 ||
-              !FTy->getReturnType()->isPointerTy() ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "__strtok_r") {
-          if (FTy->getNumParams() != 3 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "_IO_getc") {
-          if (FTy->getNumParams() != 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "_IO_putc") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 2);
-        }
-        break;
-      case 1:
-        if (Name == "\1__isoc99_scanf") {
-          if (FTy->getNumParams() < 1 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "\1stat64" ||
-                   Name == "\1lstat64" ||
-                   Name == "\1statvfs64" ||
-                   Name == "\1__isoc99_sscanf") {
-          if (FTy->getNumParams() < 1 ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "\1fopen64") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getReturnType()->isPointerTy() ||
-              !FTy->getParamType(0)->isPointerTy() ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-          setDoesNotCapture(F, 1);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "\1fseeko64" ||
-                   Name == "\1ftello64") {
-          if (FTy->getNumParams() == 0 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 1);
-        } else if (Name == "\1tmpfile64") {
-          if (!FTy->getReturnType()->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotAlias(F, 0);
-        } else if (Name == "\1fstat64" ||
-                   Name == "\1fstatvfs64") {
-          if (FTy->getNumParams() != 2 ||
-              !FTy->getParamType(1)->isPointerTy())
-            continue;
-          setDoesNotThrow(F);
-          setDoesNotCapture(F, 2);
-        } else if (Name == "\1open64") {
-          if (FTy->getNumParams() < 2 ||
-              !FTy->getParamType(0)->isPointerTy())
-            continue;
-          // May throw; "open" is a valid pthread cancellation point.
-          setDoesNotCapture(F, 1);
-        }
-        break;
-    }
+    if (F.isDeclaration() && F.hasName())
+      inferPrototypeAttributes(F);
   }
   return Modified;
 }
@@ -2294,9 +2366,6 @@ bool SimplifyLibCalls::doInitialization(Module &M) {
 //   * pow(sqrt(x),y) -> pow(x,y*0.5)
 //   * pow(pow(x,y),z)-> pow(x,y*z)
 //
-// puts:
-//   * puts("") -> putchar('\n')
-//
 // round, roundf, roundl:
 //   * round(cnst) -> cnst'
 //