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