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 /// Append a suffix to the function name according to the type of 'Op'. 290 static void AppendTypeSuffix(Value *Op, StringRef &Name, SmallString<20> &NameBuffer) { 291 if (!Op->getType()->isDoubleTy()) { 292 NameBuffer += Name; 293 294 if (Op->getType()->isFloatTy()) 295 NameBuffer += 'f'; 296 else 297 NameBuffer += 'l'; 298 299 Name = NameBuffer; 300 } 301 return; 302 } 303 304 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g. 305 /// 'floor'). This function is known to take a single of type matching 'Op' and 306 /// returns one value with the same type. If 'Op' is a long double, 'l' is 307 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix. 308 Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, 309 const AttributeSet &Attrs) { 310 SmallString<20> NameBuffer; 311 AppendTypeSuffix(Op, Name, NameBuffer); 312 313 Module *M = B.GetInsertBlock()->getParent()->getParent(); 314 Value *Callee = M->getOrInsertFunction(Name, Op->getType(), 315 Op->getType(), NULL); 316 CallInst *CI = B.CreateCall(Callee, Op, Name); 317 CI->setAttributes(Attrs); 318 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) 319 CI->setCallingConv(F->getCallingConv()); 320 321 return CI; 322 } 323 324 /// EmitBinaryFloatFnCall - Emit a call to the binary function named 'Name' 325 /// (e.g. 'fmin'). This function is known to take type matching 'Op1' and 'Op2' 326 /// and return one value with the same type. If 'Op1/Op2' are long double, 'l' 327 /// is added as the suffix of name, if 'Op1/Op2' is a float, we add a 'f' 328 /// suffix. 329 Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, 330 IRBuilder<> &B, const AttributeSet &Attrs) { 331 SmallString<20> NameBuffer; 332 AppendTypeSuffix(Op1, Name, NameBuffer); 333 334 Module *M = B.GetInsertBlock()->getParent()->getParent(); 335 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), 336 Op1->getType(), Op2->getType(), NULL); 337 CallInst *CI = B.CreateCall2(Callee, Op1, Op2, Name); 338 CI->setAttributes(Attrs); 339 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) 340 CI->setCallingConv(F->getCallingConv()); 341 342 return CI; 343 } 344 345 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char 346 /// is an integer. 347 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD, 348 const TargetLibraryInfo *TLI) { 349 if (!TLI->has(LibFunc::putchar)) 350 return 0; 351 352 Module *M = B.GetInsertBlock()->getParent()->getParent(); 353 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(), 354 B.getInt32Ty(), NULL); 355 CallInst *CI = B.CreateCall(PutChar, 356 B.CreateIntCast(Char, 357 B.getInt32Ty(), 358 /*isSigned*/true, 359 "chari"), 360 "putchar"); 361 362 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts())) 363 CI->setCallingConv(F->getCallingConv()); 364 return CI; 365 } 366 367 /// EmitPutS - Emit a call to the puts function. This assumes that Str is 368 /// some pointer. 369 Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD, 370 const TargetLibraryInfo *TLI) { 371 if (!TLI->has(LibFunc::puts)) 372 return 0; 373 374 Module *M = B.GetInsertBlock()->getParent()->getParent(); 375 AttributeSet AS[2]; 376 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); 377 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, 378 Attribute::NoUnwind); 379 380 Value *PutS = M->getOrInsertFunction("puts", 381 AttributeSet::get(M->getContext(), AS), 382 B.getInt32Ty(), 383 B.getInt8PtrTy(), 384 NULL); 385 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts"); 386 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts())) 387 CI->setCallingConv(F->getCallingConv()); 388 return CI; 389 } 390 391 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is 392 /// an integer and File is a pointer to FILE. 393 Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B, 394 const DataLayout *TD, const TargetLibraryInfo *TLI) { 395 if (!TLI->has(LibFunc::fputc)) 396 return 0; 397 398 Module *M = B.GetInsertBlock()->getParent()->getParent(); 399 AttributeSet AS[2]; 400 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); 401 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, 402 Attribute::NoUnwind); 403 Constant *F; 404 if (File->getType()->isPointerTy()) 405 F = M->getOrInsertFunction("fputc", 406 AttributeSet::get(M->getContext(), AS), 407 B.getInt32Ty(), 408 B.getInt32Ty(), File->getType(), 409 NULL); 410 else 411 F = M->getOrInsertFunction("fputc", 412 B.getInt32Ty(), 413 B.getInt32Ty(), 414 File->getType(), NULL); 415 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, 416 "chari"); 417 CallInst *CI = B.CreateCall2(F, Char, File, "fputc"); 418 419 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 420 CI->setCallingConv(Fn->getCallingConv()); 421 return CI; 422 } 423 424 /// EmitFPutS - Emit a call to the puts function. Str is required to be a 425 /// pointer and File is a pointer to FILE. 426 Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, 427 const DataLayout *TD, const TargetLibraryInfo *TLI) { 428 if (!TLI->has(LibFunc::fputs)) 429 return 0; 430 431 Module *M = B.GetInsertBlock()->getParent()->getParent(); 432 AttributeSet AS[3]; 433 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); 434 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); 435 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, 436 Attribute::NoUnwind); 437 StringRef FPutsName = TLI->getName(LibFunc::fputs); 438 Constant *F; 439 if (File->getType()->isPointerTy()) 440 F = M->getOrInsertFunction(FPutsName, 441 AttributeSet::get(M->getContext(), AS), 442 B.getInt32Ty(), 443 B.getInt8PtrTy(), 444 File->getType(), NULL); 445 else 446 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(), 447 B.getInt8PtrTy(), 448 File->getType(), NULL); 449 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs"); 450 451 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 452 CI->setCallingConv(Fn->getCallingConv()); 453 return CI; 454 } 455 456 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is 457 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE. 458 Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, 459 IRBuilder<> &B, const DataLayout *TD, 460 const TargetLibraryInfo *TLI) { 461 if (!TLI->has(LibFunc::fwrite)) 462 return 0; 463 464 Module *M = B.GetInsertBlock()->getParent()->getParent(); 465 AttributeSet AS[3]; 466 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); 467 AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture); 468 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, 469 Attribute::NoUnwind); 470 LLVMContext &Context = B.GetInsertBlock()->getContext(); 471 StringRef FWriteName = TLI->getName(LibFunc::fwrite); 472 Constant *F; 473 if (File->getType()->isPointerTy()) 474 F = M->getOrInsertFunction(FWriteName, 475 AttributeSet::get(M->getContext(), AS), 476 TD->getIntPtrType(Context), 477 B.getInt8PtrTy(), 478 TD->getIntPtrType(Context), 479 TD->getIntPtrType(Context), 480 File->getType(), NULL); 481 else 482 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context), 483 B.getInt8PtrTy(), 484 TD->getIntPtrType(Context), 485 TD->getIntPtrType(Context), 486 File->getType(), NULL); 487 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size, 488 ConstantInt::get(TD->getIntPtrType(Context), 1), File); 489 490 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 491 CI->setCallingConv(Fn->getCallingConv()); 492 return CI; 493 } 494 495 SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { } 496 497 bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD, 498 const TargetLibraryInfo *TLI) { 499 // We really need DataLayout for later. 500 if (!TD) return false; 501 502 this->CI = CI; 503 Function *Callee = CI->getCalledFunction(); 504 StringRef Name = Callee->getName(); 505 FunctionType *FT = Callee->getFunctionType(); 506 LLVMContext &Context = CI->getParent()->getContext(); 507 IRBuilder<> B(CI); 508 509 if (Name == "__memcpy_chk") { 510 // Check if this has the right signature. 511 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || 512 !FT->getParamType(0)->isPointerTy() || 513 !FT->getParamType(1)->isPointerTy() || 514 FT->getParamType(2) != TD->getIntPtrType(Context) || 515 FT->getParamType(3) != TD->getIntPtrType(Context)) 516 return false; 517 518 if (isFoldable(3, 2, false)) { 519 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1), 520 CI->getArgOperand(2), 1); 521 replaceCall(CI->getArgOperand(0)); 522 return true; 523 } 524 return false; 525 } 526 527 // Should be similar to memcpy. 528 if (Name == "__mempcpy_chk") { 529 return false; 530 } 531 532 if (Name == "__memmove_chk") { 533 // Check if this has the right signature. 534 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || 535 !FT->getParamType(0)->isPointerTy() || 536 !FT->getParamType(1)->isPointerTy() || 537 FT->getParamType(2) != TD->getIntPtrType(Context) || 538 FT->getParamType(3) != TD->getIntPtrType(Context)) 539 return false; 540 541 if (isFoldable(3, 2, false)) { 542 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1), 543 CI->getArgOperand(2), 1); 544 replaceCall(CI->getArgOperand(0)); 545 return true; 546 } 547 return false; 548 } 549 550 if (Name == "__memset_chk") { 551 // Check if this has the right signature. 552 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || 553 !FT->getParamType(0)->isPointerTy() || 554 !FT->getParamType(1)->isIntegerTy() || 555 FT->getParamType(2) != TD->getIntPtrType(Context) || 556 FT->getParamType(3) != TD->getIntPtrType(Context)) 557 return false; 558 559 if (isFoldable(3, 2, false)) { 560 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(), 561 false); 562 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1); 563 replaceCall(CI->getArgOperand(0)); 564 return true; 565 } 566 return false; 567 } 568 569 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") { 570 // Check if this has the right signature. 571 if (FT->getNumParams() != 3 || 572 FT->getReturnType() != FT->getParamType(0) || 573 FT->getParamType(0) != FT->getParamType(1) || 574 FT->getParamType(0) != Type::getInt8PtrTy(Context) || 575 FT->getParamType(2) != TD->getIntPtrType(Context)) 576 return 0; 577 578 579 // If a) we don't have any length information, or b) we know this will 580 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our 581 // st[rp]cpy_chk call which may fail at runtime if the size is too long. 582 // TODO: It might be nice to get a maximum length out of the possible 583 // string lengths for varying. 584 if (isFoldable(2, 1, true)) { 585 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD, 586 TLI, Name.substr(2, 6)); 587 if (!Ret) 588 return false; 589 replaceCall(Ret); 590 return true; 591 } 592 return false; 593 } 594 595 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") { 596 // Check if this has the right signature. 597 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || 598 FT->getParamType(0) != FT->getParamType(1) || 599 FT->getParamType(0) != Type::getInt8PtrTy(Context) || 600 !FT->getParamType(2)->isIntegerTy() || 601 FT->getParamType(3) != TD->getIntPtrType(Context)) 602 return false; 603 604 if (isFoldable(3, 2, false)) { 605 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1), 606 CI->getArgOperand(2), B, TD, TLI, 607 Name.substr(2, 7)); 608 if (!Ret) 609 return false; 610 replaceCall(Ret); 611 return true; 612 } 613 return false; 614 } 615 616 if (Name == "__strcat_chk") { 617 return false; 618 } 619 620 if (Name == "__strncat_chk") { 621 return false; 622 } 623 624 return false; 625 } 626