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