1 //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements some functions that will create standard C libcalls.
12 //===----------------------------------------------------------------------===//
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"
28 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
29 Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
30 unsigned AS = V->getType()->getPointerAddressSpace();
31 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
34 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
35 /// specified pointer. This always returns an integer value of size intptr_t.
36 Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
37 const TargetLibraryInfo *TLI) {
38 if (!TLI->has(LibFunc::strlen))
41 Module *M = B.GetInsertBlock()->getParent()->getParent();
43 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
44 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
45 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
46 ArrayRef<Attribute::AttrKind>(AVs, 2));
48 LLVMContext &Context = B.GetInsertBlock()->getContext();
49 Constant *StrLen = M->getOrInsertFunction("strlen",
50 AttributeSet::get(M->getContext(),
52 TD->getIntPtrType(Context),
55 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
56 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
57 CI->setCallingConv(F->getCallingConv());
62 /// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
63 /// specified pointer. Ptr is required to be some pointer type, MaxLen must
64 /// be of size_t type, and the return value has 'intptr_t' type.
65 Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
66 const DataLayout *TD, const TargetLibraryInfo *TLI) {
67 if (!TLI->has(LibFunc::strnlen))
70 Module *M = B.GetInsertBlock()->getParent()->getParent();
72 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
73 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
74 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
75 ArrayRef<Attribute::AttrKind>(AVs, 2));
77 LLVMContext &Context = B.GetInsertBlock()->getContext();
78 Constant *StrNLen = M->getOrInsertFunction("strnlen",
79 AttributeSet::get(M->getContext(),
81 TD->getIntPtrType(Context),
83 TD->getIntPtrType(Context),
85 CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
86 if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
87 CI->setCallingConv(F->getCallingConv());
92 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
93 /// specified pointer and character. Ptr is required to be some pointer type,
94 /// and the return value has 'i8*' type.
95 Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
96 const DataLayout *TD, const TargetLibraryInfo *TLI) {
97 if (!TLI->has(LibFunc::strchr))
100 Module *M = B.GetInsertBlock()->getParent()->getParent();
101 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
103 AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
104 ArrayRef<Attribute::AttrKind>(AVs, 2));
106 Type *I8Ptr = B.getInt8PtrTy();
107 Type *I32Ty = B.getInt32Ty();
108 Constant *StrChr = M->getOrInsertFunction("strchr",
109 AttributeSet::get(M->getContext(),
111 I8Ptr, I8Ptr, I32Ty, NULL);
112 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
113 ConstantInt::get(I32Ty, C), "strchr");
114 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
115 CI->setCallingConv(F->getCallingConv());
119 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
120 Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
121 IRBuilder<> &B, const DataLayout *TD,
122 const TargetLibraryInfo *TLI) {
123 if (!TLI->has(LibFunc::strncmp))
126 Module *M = B.GetInsertBlock()->getParent()->getParent();
128 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
129 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
130 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
131 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
132 ArrayRef<Attribute::AttrKind>(AVs, 2));
134 LLVMContext &Context = B.GetInsertBlock()->getContext();
135 Value *StrNCmp = M->getOrInsertFunction("strncmp",
136 AttributeSet::get(M->getContext(),
141 TD->getIntPtrType(Context), NULL);
142 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
143 CastToCStr(Ptr2, B), Len, "strncmp");
145 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
146 CI->setCallingConv(F->getCallingConv());
151 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
152 /// specified pointer arguments.
153 Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
154 const DataLayout *TD, const TargetLibraryInfo *TLI,
156 if (!TLI->has(LibFunc::strcpy))
159 Module *M = B.GetInsertBlock()->getParent()->getParent();
161 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
162 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
163 Attribute::NoUnwind);
164 Type *I8Ptr = B.getInt8PtrTy();
165 Value *StrCpy = M->getOrInsertFunction(Name,
166 AttributeSet::get(M->getContext(), AS),
167 I8Ptr, I8Ptr, I8Ptr, NULL);
168 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
170 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
171 CI->setCallingConv(F->getCallingConv());
175 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
176 /// specified pointer arguments.
177 Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
178 IRBuilder<> &B, const DataLayout *TD,
179 const TargetLibraryInfo *TLI, StringRef Name) {
180 if (!TLI->has(LibFunc::strncpy))
183 Module *M = B.GetInsertBlock()->getParent()->getParent();
185 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
186 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
187 Attribute::NoUnwind);
188 Type *I8Ptr = B.getInt8PtrTy();
189 Value *StrNCpy = M->getOrInsertFunction(Name,
190 AttributeSet::get(M->getContext(),
193 Len->getType(), NULL);
194 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
196 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
197 CI->setCallingConv(F->getCallingConv());
201 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
202 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
204 Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
205 IRBuilder<> &B, const DataLayout *TD,
206 const TargetLibraryInfo *TLI) {
207 if (!TLI->has(LibFunc::memcpy_chk))
210 Module *M = B.GetInsertBlock()->getParent()->getParent();
212 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
213 Attribute::NoUnwind);
214 LLVMContext &Context = B.GetInsertBlock()->getContext();
215 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
216 AttributeSet::get(M->getContext(), AS),
220 TD->getIntPtrType(Context),
221 TD->getIntPtrType(Context), NULL);
222 Dst = CastToCStr(Dst, B);
223 Src = CastToCStr(Src, B);
224 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
225 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
226 CI->setCallingConv(F->getCallingConv());
230 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
231 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
232 Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
233 Value *Len, IRBuilder<> &B, const DataLayout *TD,
234 const TargetLibraryInfo *TLI) {
235 if (!TLI->has(LibFunc::memchr))
238 Module *M = B.GetInsertBlock()->getParent()->getParent();
240 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
241 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
242 ArrayRef<Attribute::AttrKind>(AVs, 2));
243 LLVMContext &Context = B.GetInsertBlock()->getContext();
244 Value *MemChr = M->getOrInsertFunction("memchr",
245 AttributeSet::get(M->getContext(), AS),
249 TD->getIntPtrType(Context),
251 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
253 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
254 CI->setCallingConv(F->getCallingConv());
259 /// EmitMemCmp - Emit a call to the memcmp function.
260 Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
261 Value *Len, IRBuilder<> &B, const DataLayout *TD,
262 const TargetLibraryInfo *TLI) {
263 if (!TLI->has(LibFunc::memcmp))
266 Module *M = B.GetInsertBlock()->getParent()->getParent();
268 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
269 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
270 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
271 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
272 ArrayRef<Attribute::AttrKind>(AVs, 2));
274 LLVMContext &Context = B.GetInsertBlock()->getContext();
275 Value *MemCmp = M->getOrInsertFunction("memcmp",
276 AttributeSet::get(M->getContext(), AS),
280 TD->getIntPtrType(Context), NULL);
281 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
284 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
285 CI->setCallingConv(F->getCallingConv());
290 /// Append a suffix to the function name according to the type of 'Op'.
291 static void AppendTypeSuffix(Value *Op, StringRef &Name, SmallString<20> &NameBuffer) {
292 if (!Op->getType()->isDoubleTy()) {
295 if (Op->getType()->isFloatTy())
305 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
306 /// 'floor'). This function is known to take a single of type matching 'Op' and
307 /// returns one value with the same type. If 'Op' is a long double, 'l' is
308 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
309 Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
310 const AttributeSet &Attrs) {
311 SmallString<20> NameBuffer;
312 AppendTypeSuffix(Op, Name, NameBuffer);
314 Module *M = B.GetInsertBlock()->getParent()->getParent();
315 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
316 Op->getType(), NULL);
317 CallInst *CI = B.CreateCall(Callee, Op, Name);
318 CI->setAttributes(Attrs);
319 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
320 CI->setCallingConv(F->getCallingConv());
325 /// EmitBinaryFloatFnCall - Emit a call to the binary function named 'Name'
326 /// (e.g. 'fmin'). This function is known to take type matching 'Op1' and 'Op2'
327 /// and return one value with the same type. If 'Op1/Op2' are long double, 'l'
328 /// is added as the suffix of name, if 'Op1/Op2' is a float, we add a 'f'
330 Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
331 IRBuilder<> &B, const AttributeSet &Attrs) {
332 SmallString<20> NameBuffer;
333 AppendTypeSuffix(Op1, Name, NameBuffer);
335 Module *M = B.GetInsertBlock()->getParent()->getParent();
336 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(),
337 Op1->getType(), Op2->getType(), NULL);
338 CallInst *CI = B.CreateCall2(Callee, Op1, Op2, Name);
339 CI->setAttributes(Attrs);
340 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
341 CI->setCallingConv(F->getCallingConv());
346 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
348 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
349 const TargetLibraryInfo *TLI) {
350 if (!TLI->has(LibFunc::putchar))
353 Module *M = B.GetInsertBlock()->getParent()->getParent();
354 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
355 B.getInt32Ty(), NULL);
356 CallInst *CI = B.CreateCall(PutChar,
357 B.CreateIntCast(Char,
363 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
364 CI->setCallingConv(F->getCallingConv());
368 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
370 Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
371 const TargetLibraryInfo *TLI) {
372 if (!TLI->has(LibFunc::puts))
375 Module *M = B.GetInsertBlock()->getParent()->getParent();
377 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
378 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
379 Attribute::NoUnwind);
381 Value *PutS = M->getOrInsertFunction("puts",
382 AttributeSet::get(M->getContext(), AS),
386 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
387 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
388 CI->setCallingConv(F->getCallingConv());
392 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
393 /// an integer and File is a pointer to FILE.
394 Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
395 const DataLayout *TD, const TargetLibraryInfo *TLI) {
396 if (!TLI->has(LibFunc::fputc))
399 Module *M = B.GetInsertBlock()->getParent()->getParent();
401 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
402 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
403 Attribute::NoUnwind);
405 if (File->getType()->isPointerTy())
406 F = M->getOrInsertFunction("fputc",
407 AttributeSet::get(M->getContext(), AS),
409 B.getInt32Ty(), File->getType(),
412 F = M->getOrInsertFunction("fputc",
415 File->getType(), NULL);
416 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
418 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
420 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
421 CI->setCallingConv(Fn->getCallingConv());
425 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
426 /// pointer and File is a pointer to FILE.
427 Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
428 const DataLayout *TD, const TargetLibraryInfo *TLI) {
429 if (!TLI->has(LibFunc::fputs))
432 Module *M = B.GetInsertBlock()->getParent()->getParent();
434 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
435 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
436 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
437 Attribute::NoUnwind);
438 StringRef FPutsName = TLI->getName(LibFunc::fputs);
440 if (File->getType()->isPointerTy())
441 F = M->getOrInsertFunction(FPutsName,
442 AttributeSet::get(M->getContext(), AS),
445 File->getType(), NULL);
447 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
449 File->getType(), NULL);
450 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
452 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
453 CI->setCallingConv(Fn->getCallingConv());
457 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
458 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
459 Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
460 IRBuilder<> &B, const DataLayout *TD,
461 const TargetLibraryInfo *TLI) {
462 if (!TLI->has(LibFunc::fwrite))
465 Module *M = B.GetInsertBlock()->getParent()->getParent();
467 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
468 AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
469 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
470 Attribute::NoUnwind);
471 LLVMContext &Context = B.GetInsertBlock()->getContext();
472 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
474 if (File->getType()->isPointerTy())
475 F = M->getOrInsertFunction(FWriteName,
476 AttributeSet::get(M->getContext(), AS),
477 TD->getIntPtrType(Context),
479 TD->getIntPtrType(Context),
480 TD->getIntPtrType(Context),
481 File->getType(), NULL);
483 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
485 TD->getIntPtrType(Context),
486 TD->getIntPtrType(Context),
487 File->getType(), NULL);
488 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
489 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
491 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
492 CI->setCallingConv(Fn->getCallingConv());
496 SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
498 bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
499 const TargetLibraryInfo *TLI) {
500 // We really need DataLayout for later.
501 if (!TD) return false;
504 Function *Callee = CI->getCalledFunction();
505 StringRef Name = Callee->getName();
506 FunctionType *FT = Callee->getFunctionType();
507 LLVMContext &Context = CI->getParent()->getContext();
510 if (Name == "__memcpy_chk") {
511 // Check if this has the right signature.
512 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
513 !FT->getParamType(0)->isPointerTy() ||
514 !FT->getParamType(1)->isPointerTy() ||
515 FT->getParamType(2) != TD->getIntPtrType(Context) ||
516 FT->getParamType(3) != TD->getIntPtrType(Context))
519 if (isFoldable(3, 2, false)) {
520 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
521 CI->getArgOperand(2), 1);
522 replaceCall(CI->getArgOperand(0));
528 // Should be similar to memcpy.
529 if (Name == "__mempcpy_chk") {
533 if (Name == "__memmove_chk") {
534 // Check if this has the right signature.
535 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
536 !FT->getParamType(0)->isPointerTy() ||
537 !FT->getParamType(1)->isPointerTy() ||
538 FT->getParamType(2) != TD->getIntPtrType(Context) ||
539 FT->getParamType(3) != TD->getIntPtrType(Context))
542 if (isFoldable(3, 2, false)) {
543 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
544 CI->getArgOperand(2), 1);
545 replaceCall(CI->getArgOperand(0));
551 if (Name == "__memset_chk") {
552 // Check if this has the right signature.
553 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
554 !FT->getParamType(0)->isPointerTy() ||
555 !FT->getParamType(1)->isIntegerTy() ||
556 FT->getParamType(2) != TD->getIntPtrType(Context) ||
557 FT->getParamType(3) != TD->getIntPtrType(Context))
560 if (isFoldable(3, 2, false)) {
561 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
563 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
564 replaceCall(CI->getArgOperand(0));
570 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
571 // Check if this has the right signature.
572 if (FT->getNumParams() != 3 ||
573 FT->getReturnType() != FT->getParamType(0) ||
574 FT->getParamType(0) != FT->getParamType(1) ||
575 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
576 FT->getParamType(2) != TD->getIntPtrType(Context))
580 // If a) we don't have any length information, or b) we know this will
581 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
582 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
583 // TODO: It might be nice to get a maximum length out of the possible
584 // string lengths for varying.
585 if (isFoldable(2, 1, true)) {
586 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
587 TLI, Name.substr(2, 6));
596 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
597 // Check if this has the right signature.
598 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
599 FT->getParamType(0) != FT->getParamType(1) ||
600 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
601 !FT->getParamType(2)->isIntegerTy() ||
602 FT->getParamType(3) != TD->getIntPtrType(Context))
605 if (isFoldable(3, 2, false)) {
606 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
607 CI->getArgOperand(2), B, TD, TLI,
617 if (Name == "__strcat_chk") {
621 if (Name == "__strncat_chk") {