Home | History | Annotate | Download | only in Utils
      1 //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements some functions that will create standard C libcalls.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/Transforms/Utils/BuildLibCalls.h"
     15 #include "llvm/ADT/SmallString.h"
     16 #include "llvm/IR/Constants.h"
     17 #include "llvm/IR/DataLayout.h"
     18 #include "llvm/IR/Function.h"
     19 #include "llvm/IR/IRBuilder.h"
     20 #include "llvm/IR/Intrinsics.h"
     21 #include "llvm/IR/LLVMContext.h"
     22 #include "llvm/IR/Module.h"
     23 #include "llvm/IR/Type.h"
     24 #include "llvm/Target/TargetLibraryInfo.h"
     25 
     26 using namespace llvm;
     27 
     28 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
     29 Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
     30   return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
     31 }
     32 
     33 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
     34 /// specified pointer.  This always returns an integer value of size intptr_t.
     35 Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
     36                         const TargetLibraryInfo *TLI) {
     37   if (!TLI->has(LibFunc::strlen))
     38     return 0;
     39 
     40   Module *M = B.GetInsertBlock()->getParent()->getParent();
     41   AttributeSet AS[2];
     42   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
     43   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
     44   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
     45                             ArrayRef<Attribute::AttrKind>(AVs, 2));
     46 
     47   LLVMContext &Context = B.GetInsertBlock()->getContext();
     48   Constant *StrLen = M->getOrInsertFunction("strlen",
     49                                             AttributeSet::get(M->getContext(),
     50                                                               AS),
     51                                             TD->getIntPtrType(Context),
     52                                             B.getInt8PtrTy(),
     53                                             NULL);
     54   CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
     55   if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
     56     CI->setCallingConv(F->getCallingConv());
     57 
     58   return CI;
     59 }
     60 
     61 /// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
     62 /// specified pointer.  Ptr is required to be some pointer type, MaxLen must
     63 /// be of size_t type, and the return value has 'intptr_t' type.
     64 Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
     65                          const DataLayout *TD, const TargetLibraryInfo *TLI) {
     66   if (!TLI->has(LibFunc::strnlen))
     67     return 0;
     68 
     69   Module *M = B.GetInsertBlock()->getParent()->getParent();
     70   AttributeSet AS[2];
     71   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
     72   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
     73   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
     74                             ArrayRef<Attribute::AttrKind>(AVs, 2));
     75 
     76   LLVMContext &Context = B.GetInsertBlock()->getContext();
     77   Constant *StrNLen = M->getOrInsertFunction("strnlen",
     78                                              AttributeSet::get(M->getContext(),
     79                                                               AS),
     80                                              TD->getIntPtrType(Context),
     81                                              B.getInt8PtrTy(),
     82                                              TD->getIntPtrType(Context),
     83                                              NULL);
     84   CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
     85   if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
     86     CI->setCallingConv(F->getCallingConv());
     87 
     88   return CI;
     89 }
     90 
     91 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
     92 /// specified pointer and character.  Ptr is required to be some pointer type,
     93 /// and the return value has 'i8*' type.
     94 Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
     95                         const DataLayout *TD, const TargetLibraryInfo *TLI) {
     96   if (!TLI->has(LibFunc::strchr))
     97     return 0;
     98 
     99   Module *M = B.GetInsertBlock()->getParent()->getParent();
    100   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
    101   AttributeSet AS =
    102     AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
    103                       ArrayRef<Attribute::AttrKind>(AVs, 2));
    104 
    105   Type *I8Ptr = B.getInt8PtrTy();
    106   Type *I32Ty = B.getInt32Ty();
    107   Constant *StrChr = M->getOrInsertFunction("strchr",
    108                                             AttributeSet::get(M->getContext(),
    109                                                              AS),
    110                                             I8Ptr, I8Ptr, I32Ty, NULL);
    111   CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
    112                                ConstantInt::get(I32Ty, C), "strchr");
    113   if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
    114     CI->setCallingConv(F->getCallingConv());
    115   return CI;
    116 }
    117 
    118 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
    119 Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
    120                          IRBuilder<> &B, const DataLayout *TD,
    121                          const TargetLibraryInfo *TLI) {
    122   if (!TLI->has(LibFunc::strncmp))
    123     return 0;
    124 
    125   Module *M = B.GetInsertBlock()->getParent()->getParent();
    126   AttributeSet AS[3];
    127   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
    128   AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
    129   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
    130   AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
    131                             ArrayRef<Attribute::AttrKind>(AVs, 2));
    132 
    133   LLVMContext &Context = B.GetInsertBlock()->getContext();
    134   Value *StrNCmp = M->getOrInsertFunction("strncmp",
    135                                           AttributeSet::get(M->getContext(),
    136                                                            AS),
    137                                           B.getInt32Ty(),
    138                                           B.getInt8PtrTy(),
    139                                           B.getInt8PtrTy(),
    140                                           TD->getIntPtrType(Context), NULL);
    141   CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
    142                                CastToCStr(Ptr2, B), Len, "strncmp");
    143 
    144   if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
    145     CI->setCallingConv(F->getCallingConv());
    146 
    147   return CI;
    148 }
    149 
    150 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
    151 /// specified pointer arguments.
    152 Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
    153                         const DataLayout *TD, const TargetLibraryInfo *TLI,
    154                         StringRef Name) {
    155   if (!TLI->has(LibFunc::strcpy))
    156     return 0;
    157 
    158   Module *M = B.GetInsertBlock()->getParent()->getParent();
    159   AttributeSet AS[2];
    160   AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
    161   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
    162                             Attribute::NoUnwind);
    163   Type *I8Ptr = B.getInt8PtrTy();
    164   Value *StrCpy = M->getOrInsertFunction(Name,
    165                                          AttributeSet::get(M->getContext(), AS),
    166                                          I8Ptr, I8Ptr, I8Ptr, NULL);
    167   CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
    168                                Name);
    169   if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
    170     CI->setCallingConv(F->getCallingConv());
    171   return CI;
    172 }
    173 
    174 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
    175 /// specified pointer arguments.
    176 Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
    177                          IRBuilder<> &B, const DataLayout *TD,
    178                          const TargetLibraryInfo *TLI, StringRef Name) {
    179   if (!TLI->has(LibFunc::strncpy))
    180     return 0;
    181 
    182   Module *M = B.GetInsertBlock()->getParent()->getParent();
    183   AttributeSet AS[2];
    184   AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
    185   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
    186                             Attribute::NoUnwind);
    187   Type *I8Ptr = B.getInt8PtrTy();
    188   Value *StrNCpy = M->getOrInsertFunction(Name,
    189                                           AttributeSet::get(M->getContext(),
    190                                                             AS),
    191                                           I8Ptr, I8Ptr, I8Ptr,
    192                                           Len->getType(), NULL);
    193   CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
    194                                Len, "strncpy");
    195   if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
    196     CI->setCallingConv(F->getCallingConv());
    197   return CI;
    198 }
    199 
    200 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
    201 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
    202 /// are pointers.
    203 Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
    204                            IRBuilder<> &B, const DataLayout *TD,
    205                            const TargetLibraryInfo *TLI) {
    206   if (!TLI->has(LibFunc::memcpy_chk))
    207     return 0;
    208 
    209   Module *M = B.GetInsertBlock()->getParent()->getParent();
    210   AttributeSet AS;
    211   AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
    212                          Attribute::NoUnwind);
    213   LLVMContext &Context = B.GetInsertBlock()->getContext();
    214   Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
    215                                          AttributeSet::get(M->getContext(), AS),
    216                                          B.getInt8PtrTy(),
    217                                          B.getInt8PtrTy(),
    218                                          B.getInt8PtrTy(),
    219                                          TD->getIntPtrType(Context),
    220                                          TD->getIntPtrType(Context), NULL);
    221   Dst = CastToCStr(Dst, B);
    222   Src = CastToCStr(Src, B);
    223   CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
    224   if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
    225     CI->setCallingConv(F->getCallingConv());
    226   return CI;
    227 }
    228 
    229 /// EmitMemChr - Emit a call to the memchr function.  This assumes that Ptr is
    230 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
    231 Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
    232                         Value *Len, IRBuilder<> &B, const DataLayout *TD,
    233                         const TargetLibraryInfo *TLI) {
    234   if (!TLI->has(LibFunc::memchr))
    235     return 0;
    236 
    237   Module *M = B.GetInsertBlock()->getParent()->getParent();
    238   AttributeSet AS;
    239   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
    240   AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
    241                          ArrayRef<Attribute::AttrKind>(AVs, 2));
    242   LLVMContext &Context = B.GetInsertBlock()->getContext();
    243   Value *MemChr = M->getOrInsertFunction("memchr",
    244                                          AttributeSet::get(M->getContext(), AS),
    245                                          B.getInt8PtrTy(),
    246                                          B.getInt8PtrTy(),
    247                                          B.getInt32Ty(),
    248                                          TD->getIntPtrType(Context),
    249                                          NULL);
    250   CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
    251 
    252   if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
    253     CI->setCallingConv(F->getCallingConv());
    254 
    255   return CI;
    256 }
    257 
    258 /// EmitMemCmp - Emit a call to the memcmp function.
    259 Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
    260                         Value *Len, IRBuilder<> &B, const DataLayout *TD,
    261                         const TargetLibraryInfo *TLI) {
    262   if (!TLI->has(LibFunc::memcmp))
    263     return 0;
    264 
    265   Module *M = B.GetInsertBlock()->getParent()->getParent();
    266   AttributeSet AS[3];
    267   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
    268   AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
    269   Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
    270   AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
    271                             ArrayRef<Attribute::AttrKind>(AVs, 2));
    272 
    273   LLVMContext &Context = B.GetInsertBlock()->getContext();
    274   Value *MemCmp = M->getOrInsertFunction("memcmp",
    275                                          AttributeSet::get(M->getContext(), AS),
    276                                          B.getInt32Ty(),
    277                                          B.getInt8PtrTy(),
    278                                          B.getInt8PtrTy(),
    279                                          TD->getIntPtrType(Context), NULL);
    280   CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
    281                                Len, "memcmp");
    282 
    283   if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
    284     CI->setCallingConv(F->getCallingConv());
    285 
    286   return CI;
    287 }
    288 
    289 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
    290 /// 'floor').  This function is known to take a single of type matching 'Op' and
    291 /// returns one value with the same type.  If 'Op' is a long double, 'l' is
    292 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
    293 Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
    294                                   const AttributeSet &Attrs) {
    295   SmallString<20> NameBuffer;
    296   if (!Op->getType()->isDoubleTy()) {
    297     // If we need to add a suffix, copy into NameBuffer.
    298     NameBuffer += Name;
    299     if (Op->getType()->isFloatTy())
    300       NameBuffer += 'f'; // floorf
    301     else
    302       NameBuffer += 'l'; // floorl
    303     Name = NameBuffer;
    304   }
    305 
    306   Module *M = B.GetInsertBlock()->getParent()->getParent();
    307   Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
    308                                          Op->getType(), NULL);
    309   CallInst *CI = B.CreateCall(Callee, Op, Name);
    310   CI->setAttributes(Attrs);
    311   if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
    312     CI->setCallingConv(F->getCallingConv());
    313 
    314   return CI;
    315 }
    316 
    317 /// EmitPutChar - Emit a call to the putchar function.  This assumes that Char
    318 /// is an integer.
    319 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
    320                          const TargetLibraryInfo *TLI) {
    321   if (!TLI->has(LibFunc::putchar))
    322     return 0;
    323 
    324   Module *M = B.GetInsertBlock()->getParent()->getParent();
    325   Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
    326                                           B.getInt32Ty(), NULL);
    327   CallInst *CI = B.CreateCall(PutChar,
    328                               B.CreateIntCast(Char,
    329                               B.getInt32Ty(),
    330                               /*isSigned*/true,
    331                               "chari"),
    332                               "putchar");
    333 
    334   if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
    335     CI->setCallingConv(F->getCallingConv());
    336   return CI;
    337 }
    338 
    339 /// EmitPutS - Emit a call to the puts function.  This assumes that Str is
    340 /// some pointer.
    341 Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
    342                       const TargetLibraryInfo *TLI) {
    343   if (!TLI->has(LibFunc::puts))
    344     return 0;
    345 
    346   Module *M = B.GetInsertBlock()->getParent()->getParent();
    347   AttributeSet AS[2];
    348   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
    349   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
    350                             Attribute::NoUnwind);
    351 
    352   Value *PutS = M->getOrInsertFunction("puts",
    353                                        AttributeSet::get(M->getContext(), AS),
    354                                        B.getInt32Ty(),
    355                                        B.getInt8PtrTy(),
    356                                        NULL);
    357   CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
    358   if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
    359     CI->setCallingConv(F->getCallingConv());
    360   return CI;
    361 }
    362 
    363 /// EmitFPutC - Emit a call to the fputc function.  This assumes that Char is
    364 /// an integer and File is a pointer to FILE.
    365 Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
    366                        const DataLayout *TD, const TargetLibraryInfo *TLI) {
    367   if (!TLI->has(LibFunc::fputc))
    368     return 0;
    369 
    370   Module *M = B.GetInsertBlock()->getParent()->getParent();
    371   AttributeSet AS[2];
    372   AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
    373   AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
    374                             Attribute::NoUnwind);
    375   Constant *F;
    376   if (File->getType()->isPointerTy())
    377     F = M->getOrInsertFunction("fputc",
    378                                AttributeSet::get(M->getContext(), AS),
    379                                B.getInt32Ty(),
    380                                B.getInt32Ty(), File->getType(),
    381                                NULL);
    382   else
    383     F = M->getOrInsertFunction("fputc",
    384                                B.getInt32Ty(),
    385                                B.getInt32Ty(),
    386                                File->getType(), NULL);
    387   Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
    388                          "chari");
    389   CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
    390 
    391   if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
    392     CI->setCallingConv(Fn->getCallingConv());
    393   return CI;
    394 }
    395 
    396 /// EmitFPutS - Emit a call to the puts function.  Str is required to be a
    397 /// pointer and File is a pointer to FILE.
    398 Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
    399                        const DataLayout *TD, const TargetLibraryInfo *TLI) {
    400   if (!TLI->has(LibFunc::fputs))
    401     return 0;
    402 
    403   Module *M = B.GetInsertBlock()->getParent()->getParent();
    404   AttributeSet AS[3];
    405   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
    406   AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
    407   AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
    408                             Attribute::NoUnwind);
    409   StringRef FPutsName = TLI->getName(LibFunc::fputs);
    410   Constant *F;
    411   if (File->getType()->isPointerTy())
    412     F = M->getOrInsertFunction(FPutsName,
    413                                AttributeSet::get(M->getContext(), AS),
    414                                B.getInt32Ty(),
    415                                B.getInt8PtrTy(),
    416                                File->getType(), NULL);
    417   else
    418     F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
    419                                B.getInt8PtrTy(),
    420                                File->getType(), NULL);
    421   CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
    422 
    423   if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
    424     CI->setCallingConv(Fn->getCallingConv());
    425   return CI;
    426 }
    427 
    428 /// EmitFWrite - Emit a call to the fwrite function.  This assumes that Ptr is
    429 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
    430 Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
    431                         IRBuilder<> &B, const DataLayout *TD,
    432                         const TargetLibraryInfo *TLI) {
    433   if (!TLI->has(LibFunc::fwrite))
    434     return 0;
    435 
    436   Module *M = B.GetInsertBlock()->getParent()->getParent();
    437   AttributeSet AS[3];
    438   AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
    439   AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
    440   AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
    441                             Attribute::NoUnwind);
    442   LLVMContext &Context = B.GetInsertBlock()->getContext();
    443   StringRef FWriteName = TLI->getName(LibFunc::fwrite);
    444   Constant *F;
    445   if (File->getType()->isPointerTy())
    446     F = M->getOrInsertFunction(FWriteName,
    447                                AttributeSet::get(M->getContext(), AS),
    448                                TD->getIntPtrType(Context),
    449                                B.getInt8PtrTy(),
    450                                TD->getIntPtrType(Context),
    451                                TD->getIntPtrType(Context),
    452                                File->getType(), NULL);
    453   else
    454     F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
    455                                B.getInt8PtrTy(),
    456                                TD->getIntPtrType(Context),
    457                                TD->getIntPtrType(Context),
    458                                File->getType(), NULL);
    459   CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
    460                         ConstantInt::get(TD->getIntPtrType(Context), 1), File);
    461 
    462   if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
    463     CI->setCallingConv(Fn->getCallingConv());
    464   return CI;
    465 }
    466 
    467 SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
    468 
    469 bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
    470                                      const TargetLibraryInfo *TLI) {
    471   // We really need DataLayout for later.
    472   if (!TD) return false;
    473 
    474   this->CI = CI;
    475   Function *Callee = CI->getCalledFunction();
    476   StringRef Name = Callee->getName();
    477   FunctionType *FT = Callee->getFunctionType();
    478   LLVMContext &Context = CI->getParent()->getContext();
    479   IRBuilder<> B(CI);
    480 
    481   if (Name == "__memcpy_chk") {
    482     // Check if this has the right signature.
    483     if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
    484         !FT->getParamType(0)->isPointerTy() ||
    485         !FT->getParamType(1)->isPointerTy() ||
    486         FT->getParamType(2) != TD->getIntPtrType(Context) ||
    487         FT->getParamType(3) != TD->getIntPtrType(Context))
    488       return false;
    489 
    490     if (isFoldable(3, 2, false)) {
    491       B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
    492                      CI->getArgOperand(2), 1);
    493       replaceCall(CI->getArgOperand(0));
    494       return true;
    495     }
    496     return false;
    497   }
    498 
    499   // Should be similar to memcpy.
    500   if (Name == "__mempcpy_chk") {
    501     return false;
    502   }
    503 
    504   if (Name == "__memmove_chk") {
    505     // Check if this has the right signature.
    506     if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
    507         !FT->getParamType(0)->isPointerTy() ||
    508         !FT->getParamType(1)->isPointerTy() ||
    509         FT->getParamType(2) != TD->getIntPtrType(Context) ||
    510         FT->getParamType(3) != TD->getIntPtrType(Context))
    511       return false;
    512 
    513     if (isFoldable(3, 2, false)) {
    514       B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
    515                       CI->getArgOperand(2), 1);
    516       replaceCall(CI->getArgOperand(0));
    517       return true;
    518     }
    519     return false;
    520   }
    521 
    522   if (Name == "__memset_chk") {
    523     // Check if this has the right signature.
    524     if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
    525         !FT->getParamType(0)->isPointerTy() ||
    526         !FT->getParamType(1)->isIntegerTy() ||
    527         FT->getParamType(2) != TD->getIntPtrType(Context) ||
    528         FT->getParamType(3) != TD->getIntPtrType(Context))
    529       return false;
    530 
    531     if (isFoldable(3, 2, false)) {
    532       Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
    533                                    false);
    534       B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
    535       replaceCall(CI->getArgOperand(0));
    536       return true;
    537     }
    538     return false;
    539   }
    540 
    541   if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
    542     // Check if this has the right signature.
    543     if (FT->getNumParams() != 3 ||
    544         FT->getReturnType() != FT->getParamType(0) ||
    545         FT->getParamType(0) != FT->getParamType(1) ||
    546         FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
    547         FT->getParamType(2) != TD->getIntPtrType(Context))
    548       return 0;
    549 
    550 
    551     // If a) we don't have any length information, or b) we know this will
    552     // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
    553     // st[rp]cpy_chk call which may fail at runtime if the size is too long.
    554     // TODO: It might be nice to get a maximum length out of the possible
    555     // string lengths for varying.
    556     if (isFoldable(2, 1, true)) {
    557       Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
    558                               TLI, Name.substr(2, 6));
    559       if (!Ret)
    560         return false;
    561       replaceCall(Ret);
    562       return true;
    563     }
    564     return false;
    565   }
    566 
    567   if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
    568     // Check if this has the right signature.
    569     if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
    570         FT->getParamType(0) != FT->getParamType(1) ||
    571         FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
    572         !FT->getParamType(2)->isIntegerTy() ||
    573         FT->getParamType(3) != TD->getIntPtrType(Context))
    574       return false;
    575 
    576     if (isFoldable(3, 2, false)) {
    577       Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
    578                                CI->getArgOperand(2), B, TD, TLI,
    579                                Name.substr(2, 7));
    580       if (!Ret)
    581         return false;
    582       replaceCall(Ret);
    583       return true;
    584     }
    585     return false;
    586   }
    587 
    588   if (Name == "__strcat_chk") {
    589     return false;
    590   }
    591 
    592   if (Name == "__strncat_chk") {
    593     return false;
    594   }
    595 
    596   return false;
    597 }
    598