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