LLVM API Documentation

BuildLibCalls.cpp
Go to the documentation of this file.
00001 //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
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 some functions that will create standard C libcalls.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Transforms/Utils/BuildLibCalls.h"
00015 #include "llvm/ADT/SmallString.h"
00016 #include "llvm/IR/Constants.h"
00017 #include "llvm/IR/DataLayout.h"
00018 #include "llvm/IR/Function.h"
00019 #include "llvm/IR/IRBuilder.h"
00020 #include "llvm/IR/Intrinsics.h"
00021 #include "llvm/IR/LLVMContext.h"
00022 #include "llvm/IR/Module.h"
00023 #include "llvm/IR/Type.h"
00024 #include "llvm/Target/TargetLibraryInfo.h"
00025 
00026 using namespace llvm;
00027 
00028 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
00029 Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
00030   return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
00031 }
00032 
00033 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
00034 /// specified pointer.  This always returns an integer value of size intptr_t.
00035 Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
00036                         const TargetLibraryInfo *TLI) {
00037   if (!TLI->has(LibFunc::strlen))
00038     return 0;
00039 
00040   Module *M = B.GetInsertBlock()->getParent()->getParent();
00041   AttributeSet AS[2];
00042   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
00043   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
00044   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00045                             ArrayRef<Attribute::AttrKind>(AVs, 2));
00046 
00047   LLVMContext &Context = B.GetInsertBlock()->getContext();
00048   Constant *StrLen = M->getOrInsertFunction("strlen",
00049                                             AttributeSet::get(M->getContext(),
00050                                                               AS),
00051                                             TD->getIntPtrType(Context),
00052                                             B.getInt8PtrTy(),
00053                                             NULL);
00054   CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
00055   if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
00056     CI->setCallingConv(F->getCallingConv());
00057 
00058   return CI;
00059 }
00060 
00061 /// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
00062 /// specified pointer.  Ptr is required to be some pointer type, MaxLen must
00063 /// be of size_t type, and the return value has 'intptr_t' type.
00064 Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
00065                          const DataLayout *TD, const TargetLibraryInfo *TLI) {
00066   if (!TLI->has(LibFunc::strnlen))
00067     return 0;
00068 
00069   Module *M = B.GetInsertBlock()->getParent()->getParent();
00070   AttributeSet AS[2];
00071   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
00072   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
00073   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00074                             ArrayRef<Attribute::AttrKind>(AVs, 2));
00075 
00076   LLVMContext &Context = B.GetInsertBlock()->getContext();
00077   Constant *StrNLen = M->getOrInsertFunction("strnlen",
00078                                              AttributeSet::get(M->getContext(),
00079                                                               AS),
00080                                              TD->getIntPtrType(Context),
00081                                              B.getInt8PtrTy(),
00082                                              TD->getIntPtrType(Context),
00083                                              NULL);
00084   CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
00085   if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
00086     CI->setCallingConv(F->getCallingConv());
00087 
00088   return CI;
00089 }
00090 
00091 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
00092 /// specified pointer and character.  Ptr is required to be some pointer type,
00093 /// and the return value has 'i8*' type.
00094 Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
00095                         const DataLayout *TD, const TargetLibraryInfo *TLI) {
00096   if (!TLI->has(LibFunc::strchr))
00097     return 0;
00098 
00099   Module *M = B.GetInsertBlock()->getParent()->getParent();
00100   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
00101   AttributeSet AS =
00102     AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00103                       ArrayRef<Attribute::AttrKind>(AVs, 2));
00104 
00105   Type *I8Ptr = B.getInt8PtrTy();
00106   Type *I32Ty = B.getInt32Ty();
00107   Constant *StrChr = M->getOrInsertFunction("strchr",
00108                                             AttributeSet::get(M->getContext(),
00109                                                              AS),
00110                                             I8Ptr, I8Ptr, I32Ty, NULL);
00111   CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
00112                                ConstantInt::get(I32Ty, C), "strchr");
00113   if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
00114     CI->setCallingConv(F->getCallingConv());
00115   return CI;
00116 }
00117 
00118 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
00119 Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
00120                          IRBuilder<> &B, const DataLayout *TD,
00121                          const TargetLibraryInfo *TLI) {
00122   if (!TLI->has(LibFunc::strncmp))
00123     return 0;
00124 
00125   Module *M = B.GetInsertBlock()->getParent()->getParent();
00126   AttributeSet AS[3];
00127   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
00128   AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
00129   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
00130   AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00131                             ArrayRef<Attribute::AttrKind>(AVs, 2));
00132 
00133   LLVMContext &Context = B.GetInsertBlock()->getContext();
00134   Value *StrNCmp = M->getOrInsertFunction("strncmp",
00135                                           AttributeSet::get(M->getContext(),
00136                                                            AS),
00137                                           B.getInt32Ty(),
00138                                           B.getInt8PtrTy(),
00139                                           B.getInt8PtrTy(),
00140                                           TD->getIntPtrType(Context), NULL);
00141   CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
00142                                CastToCStr(Ptr2, B), Len, "strncmp");
00143 
00144   if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
00145     CI->setCallingConv(F->getCallingConv());
00146 
00147   return CI;
00148 }
00149 
00150 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
00151 /// specified pointer arguments.
00152 Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
00153                         const DataLayout *TD, const TargetLibraryInfo *TLI,
00154                         StringRef Name) {
00155   if (!TLI->has(LibFunc::strcpy))
00156     return 0;
00157 
00158   Module *M = B.GetInsertBlock()->getParent()->getParent();
00159   AttributeSet AS[2];
00160   AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
00161   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00162                             Attribute::NoUnwind);
00163   Type *I8Ptr = B.getInt8PtrTy();
00164   Value *StrCpy = M->getOrInsertFunction(Name,
00165                                          AttributeSet::get(M->getContext(), AS),
00166                                          I8Ptr, I8Ptr, I8Ptr, NULL);
00167   CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
00168                                Name);
00169   if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
00170     CI->setCallingConv(F->getCallingConv());
00171   return CI;
00172 }
00173 
00174 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
00175 /// specified pointer arguments.
00176 Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
00177                          IRBuilder<> &B, const DataLayout *TD,
00178                          const TargetLibraryInfo *TLI, StringRef Name) {
00179   if (!TLI->has(LibFunc::strncpy))
00180     return 0;
00181 
00182   Module *M = B.GetInsertBlock()->getParent()->getParent();
00183   AttributeSet AS[2];
00184   AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
00185   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00186                             Attribute::NoUnwind);
00187   Type *I8Ptr = B.getInt8PtrTy();
00188   Value *StrNCpy = M->getOrInsertFunction(Name,
00189                                           AttributeSet::get(M->getContext(),
00190                                                             AS),
00191                                           I8Ptr, I8Ptr, I8Ptr,
00192                                           Len->getType(), NULL);
00193   CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
00194                                Len, "strncpy");
00195   if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
00196     CI->setCallingConv(F->getCallingConv());
00197   return CI;
00198 }
00199 
00200 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
00201 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
00202 /// are pointers.
00203 Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
00204                            IRBuilder<> &B, const DataLayout *TD,
00205                            const TargetLibraryInfo *TLI) {
00206   if (!TLI->has(LibFunc::memcpy_chk))
00207     return 0;
00208 
00209   Module *M = B.GetInsertBlock()->getParent()->getParent();
00210   AttributeSet AS;
00211   AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00212                          Attribute::NoUnwind);
00213   LLVMContext &Context = B.GetInsertBlock()->getContext();
00214   Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
00215                                          AttributeSet::get(M->getContext(), AS),
00216                                          B.getInt8PtrTy(),
00217                                          B.getInt8PtrTy(),
00218                                          B.getInt8PtrTy(),
00219                                          TD->getIntPtrType(Context),
00220                                          TD->getIntPtrType(Context), NULL);
00221   Dst = CastToCStr(Dst, B);
00222   Src = CastToCStr(Src, B);
00223   CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
00224   if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
00225     CI->setCallingConv(F->getCallingConv());
00226   return CI;
00227 }
00228 
00229 /// EmitMemChr - Emit a call to the memchr function.  This assumes that Ptr is
00230 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
00231 Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
00232                         Value *Len, IRBuilder<> &B, const DataLayout *TD,
00233                         const TargetLibraryInfo *TLI) {
00234   if (!TLI->has(LibFunc::memchr))
00235     return 0;
00236 
00237   Module *M = B.GetInsertBlock()->getParent()->getParent();
00238   AttributeSet AS;
00239   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
00240   AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00241                          ArrayRef<Attribute::AttrKind>(AVs, 2));
00242   LLVMContext &Context = B.GetInsertBlock()->getContext();
00243   Value *MemChr = M->getOrInsertFunction("memchr",
00244                                          AttributeSet::get(M->getContext(), AS),
00245                                          B.getInt8PtrTy(),
00246                                          B.getInt8PtrTy(),
00247                                          B.getInt32Ty(),
00248                                          TD->getIntPtrType(Context),
00249                                          NULL);
00250   CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
00251 
00252   if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
00253     CI->setCallingConv(F->getCallingConv());
00254 
00255   return CI;
00256 }
00257 
00258 /// EmitMemCmp - Emit a call to the memcmp function.
00259 Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
00260                         Value *Len, IRBuilder<> &B, const DataLayout *TD,
00261                         const TargetLibraryInfo *TLI) {
00262   if (!TLI->has(LibFunc::memcmp))
00263     return 0;
00264 
00265   Module *M = B.GetInsertBlock()->getParent()->getParent();
00266   AttributeSet AS[3];
00267   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
00268   AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
00269   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
00270   AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00271                             ArrayRef<Attribute::AttrKind>(AVs, 2));
00272 
00273   LLVMContext &Context = B.GetInsertBlock()->getContext();
00274   Value *MemCmp = M->getOrInsertFunction("memcmp",
00275                                          AttributeSet::get(M->getContext(), AS),
00276                                          B.getInt32Ty(),
00277                                          B.getInt8PtrTy(),
00278                                          B.getInt8PtrTy(),
00279                                          TD->getIntPtrType(Context), NULL);
00280   CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
00281                                Len, "memcmp");
00282 
00283   if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
00284     CI->setCallingConv(F->getCallingConv());
00285 
00286   return CI;
00287 }
00288 
00289 /// Append a suffix to the function name according to the type of 'Op'.
00290 static void AppendTypeSuffix(Value *Op, StringRef &Name, SmallString<20> &NameBuffer) {
00291   if (!Op->getType()->isDoubleTy()) {
00292       NameBuffer += Name;
00293 
00294     if (Op->getType()->isFloatTy())
00295       NameBuffer += 'f';
00296     else
00297       NameBuffer += 'l';
00298 
00299     Name = NameBuffer;
00300   }  
00301   return;
00302 }
00303 
00304 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
00305 /// 'floor').  This function is known to take a single of type matching 'Op' and
00306 /// returns one value with the same type.  If 'Op' is a long double, 'l' is
00307 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
00308 Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
00309                                   const AttributeSet &Attrs) {
00310   SmallString<20> NameBuffer;
00311   AppendTypeSuffix(Op, Name, NameBuffer);   
00312 
00313   Module *M = B.GetInsertBlock()->getParent()->getParent();
00314   Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
00315                                          Op->getType(), NULL);
00316   CallInst *CI = B.CreateCall(Callee, Op, Name);
00317   CI->setAttributes(Attrs);
00318   if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
00319     CI->setCallingConv(F->getCallingConv());
00320 
00321   return CI;
00322 }
00323 
00324 /// EmitBinaryFloatFnCall - Emit a call to the binary function named 'Name'
00325 /// (e.g. 'fmin').  This function is known to take type matching 'Op1' and 'Op2'
00326 /// and return one value with the same type.  If 'Op1/Op2' are long double, 'l'
00327 /// is added as the suffix of name, if 'Op1/Op2' is a float, we add a 'f'
00328 /// suffix.
00329 Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
00330                                   IRBuilder<> &B, const AttributeSet &Attrs) {
00331   SmallString<20> NameBuffer;
00332   AppendTypeSuffix(Op1, Name, NameBuffer);   
00333 
00334   Module *M = B.GetInsertBlock()->getParent()->getParent();
00335   Value *Callee = M->getOrInsertFunction(Name, Op1->getType(),
00336                                          Op1->getType(), Op2->getType(), NULL);
00337   CallInst *CI = B.CreateCall2(Callee, Op1, Op2, Name);
00338   CI->setAttributes(Attrs);
00339   if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
00340     CI->setCallingConv(F->getCallingConv());
00341 
00342   return CI;
00343 }
00344 
00345 /// EmitPutChar - Emit a call to the putchar function.  This assumes that Char
00346 /// is an integer.
00347 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
00348                          const TargetLibraryInfo *TLI) {
00349   if (!TLI->has(LibFunc::putchar))
00350     return 0;
00351 
00352   Module *M = B.GetInsertBlock()->getParent()->getParent();
00353   Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
00354                                           B.getInt32Ty(), NULL);
00355   CallInst *CI = B.CreateCall(PutChar,
00356                               B.CreateIntCast(Char,
00357                               B.getInt32Ty(),
00358                               /*isSigned*/true,
00359                               "chari"),
00360                               "putchar");
00361 
00362   if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
00363     CI->setCallingConv(F->getCallingConv());
00364   return CI;
00365 }
00366 
00367 /// EmitPutS - Emit a call to the puts function.  This assumes that Str is
00368 /// some pointer.
00369 Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
00370                       const TargetLibraryInfo *TLI) {
00371   if (!TLI->has(LibFunc::puts))
00372     return 0;
00373 
00374   Module *M = B.GetInsertBlock()->getParent()->getParent();
00375   AttributeSet AS[2];
00376   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
00377   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00378                             Attribute::NoUnwind);
00379 
00380   Value *PutS = M->getOrInsertFunction("puts",
00381                                        AttributeSet::get(M->getContext(), AS),
00382                                        B.getInt32Ty(),
00383                                        B.getInt8PtrTy(),
00384                                        NULL);
00385   CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
00386   if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
00387     CI->setCallingConv(F->getCallingConv());
00388   return CI;
00389 }
00390 
00391 /// EmitFPutC - Emit a call to the fputc function.  This assumes that Char is
00392 /// an integer and File is a pointer to FILE.
00393 Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
00394                        const DataLayout *TD, const TargetLibraryInfo *TLI) {
00395   if (!TLI->has(LibFunc::fputc))
00396     return 0;
00397 
00398   Module *M = B.GetInsertBlock()->getParent()->getParent();
00399   AttributeSet AS[2];
00400   AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
00401   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00402                             Attribute::NoUnwind);
00403   Constant *F;
00404   if (File->getType()->isPointerTy())
00405     F = M->getOrInsertFunction("fputc",
00406                                AttributeSet::get(M->getContext(), AS),
00407                                B.getInt32Ty(),
00408                                B.getInt32Ty(), File->getType(),
00409                                NULL);
00410   else
00411     F = M->getOrInsertFunction("fputc",
00412                                B.getInt32Ty(),
00413                                B.getInt32Ty(),
00414                                File->getType(), NULL);
00415   Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
00416                          "chari");
00417   CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
00418 
00419   if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
00420     CI->setCallingConv(Fn->getCallingConv());
00421   return CI;
00422 }
00423 
00424 /// EmitFPutS - Emit a call to the puts function.  Str is required to be a
00425 /// pointer and File is a pointer to FILE.
00426 Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
00427                        const DataLayout *TD, const TargetLibraryInfo *TLI) {
00428   if (!TLI->has(LibFunc::fputs))
00429     return 0;
00430 
00431   Module *M = B.GetInsertBlock()->getParent()->getParent();
00432   AttributeSet AS[3];
00433   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
00434   AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
00435   AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00436                             Attribute::NoUnwind);
00437   StringRef FPutsName = TLI->getName(LibFunc::fputs);
00438   Constant *F;
00439   if (File->getType()->isPointerTy())
00440     F = M->getOrInsertFunction(FPutsName,
00441                                AttributeSet::get(M->getContext(), AS),
00442                                B.getInt32Ty(),
00443                                B.getInt8PtrTy(),
00444                                File->getType(), NULL);
00445   else
00446     F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
00447                                B.getInt8PtrTy(),
00448                                File->getType(), NULL);
00449   CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
00450 
00451   if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
00452     CI->setCallingConv(Fn->getCallingConv());
00453   return CI;
00454 }
00455 
00456 /// EmitFWrite - Emit a call to the fwrite function.  This assumes that Ptr is
00457 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
00458 Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
00459                         IRBuilder<> &B, const DataLayout *TD,
00460                         const TargetLibraryInfo *TLI) {
00461   if (!TLI->has(LibFunc::fwrite))
00462     return 0;
00463 
00464   Module *M = B.GetInsertBlock()->getParent()->getParent();
00465   AttributeSet AS[3];
00466   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
00467   AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
00468   AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
00469                             Attribute::NoUnwind);
00470   LLVMContext &Context = B.GetInsertBlock()->getContext();
00471   StringRef FWriteName = TLI->getName(LibFunc::fwrite);
00472   Constant *F;
00473   if (File->getType()->isPointerTy())
00474     F = M->getOrInsertFunction(FWriteName,
00475                                AttributeSet::get(M->getContext(), AS),
00476                                TD->getIntPtrType(Context),
00477                                B.getInt8PtrTy(),
00478                                TD->getIntPtrType(Context),
00479                                TD->getIntPtrType(Context),
00480                                File->getType(), NULL);
00481   else
00482     F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
00483                                B.getInt8PtrTy(),
00484                                TD->getIntPtrType(Context),
00485                                TD->getIntPtrType(Context),
00486                                File->getType(), NULL);
00487   CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
00488                         ConstantInt::get(TD->getIntPtrType(Context), 1), File);
00489 
00490   if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
00491     CI->setCallingConv(Fn->getCallingConv());
00492   return CI;
00493 }
00494 
00495 SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
00496 
00497 bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
00498                                      const TargetLibraryInfo *TLI) {
00499   // We really need DataLayout for later.
00500   if (!TD) return false;
00501   
00502   this->CI = CI;
00503   Function *Callee = CI->getCalledFunction();
00504   StringRef Name = Callee->getName();
00505   FunctionType *FT = Callee->getFunctionType();
00506   LLVMContext &Context = CI->getParent()->getContext();
00507   IRBuilder<> B(CI);
00508 
00509   if (Name == "__memcpy_chk") {
00510     // Check if this has the right signature.
00511     if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
00512         !FT->getParamType(0)->isPointerTy() ||
00513         !FT->getParamType(1)->isPointerTy() ||
00514         FT->getParamType(2) != TD->getIntPtrType(Context) ||
00515         FT->getParamType(3) != TD->getIntPtrType(Context))
00516       return false;
00517 
00518     if (isFoldable(3, 2, false)) {
00519       B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
00520                      CI->getArgOperand(2), 1);
00521       replaceCall(CI->getArgOperand(0));
00522       return true;
00523     }
00524     return false;
00525   }
00526 
00527   // Should be similar to memcpy.
00528   if (Name == "__mempcpy_chk") {
00529     return false;
00530   }
00531 
00532   if (Name == "__memmove_chk") {
00533     // Check if this has the right signature.
00534     if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
00535         !FT->getParamType(0)->isPointerTy() ||
00536         !FT->getParamType(1)->isPointerTy() ||
00537         FT->getParamType(2) != TD->getIntPtrType(Context) ||
00538         FT->getParamType(3) != TD->getIntPtrType(Context))
00539       return false;
00540 
00541     if (isFoldable(3, 2, false)) {
00542       B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
00543                       CI->getArgOperand(2), 1);
00544       replaceCall(CI->getArgOperand(0));
00545       return true;
00546     }
00547     return false;
00548   }
00549 
00550   if (Name == "__memset_chk") {
00551     // Check if this has the right signature.
00552     if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
00553         !FT->getParamType(0)->isPointerTy() ||
00554         !FT->getParamType(1)->isIntegerTy() ||
00555         FT->getParamType(2) != TD->getIntPtrType(Context) ||
00556         FT->getParamType(3) != TD->getIntPtrType(Context))
00557       return false;
00558 
00559     if (isFoldable(3, 2, false)) {
00560       Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
00561                                    false);
00562       B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
00563       replaceCall(CI->getArgOperand(0));
00564       return true;
00565     }
00566     return false;
00567   }
00568 
00569   if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
00570     // Check if this has the right signature.
00571     if (FT->getNumParams() != 3 ||
00572         FT->getReturnType() != FT->getParamType(0) ||
00573         FT->getParamType(0) != FT->getParamType(1) ||
00574         FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
00575         FT->getParamType(2) != TD->getIntPtrType(Context))
00576       return 0;
00577     
00578     
00579     // If a) we don't have any length information, or b) we know this will
00580     // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
00581     // st[rp]cpy_chk call which may fail at runtime if the size is too long.
00582     // TODO: It might be nice to get a maximum length out of the possible
00583     // string lengths for varying.
00584     if (isFoldable(2, 1, true)) {
00585       Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
00586                               TLI, Name.substr(2, 6));
00587       if (!Ret)
00588         return false;
00589       replaceCall(Ret);
00590       return true;
00591     }
00592     return false;
00593   }
00594 
00595   if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
00596     // Check if this has the right signature.
00597     if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
00598         FT->getParamType(0) != FT->getParamType(1) ||
00599         FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
00600         !FT->getParamType(2)->isIntegerTy() ||
00601         FT->getParamType(3) != TD->getIntPtrType(Context))
00602       return false;
00603 
00604     if (isFoldable(3, 2, false)) {
00605       Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
00606                                CI->getArgOperand(2), B, TD, TLI,
00607                                Name.substr(2, 7));
00608       if (!Ret)
00609         return false;
00610       replaceCall(Ret);
00611       return true;
00612     }
00613     return false;
00614   }
00615 
00616   if (Name == "__strcat_chk") {
00617     return false;
00618   }
00619 
00620   if (Name == "__strncat_chk") {
00621     return false;
00622   }
00623 
00624   return false;
00625 }