BuildLibCalls.cpp revision 280031
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 26using namespace llvm; 27 28/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*. 29Value *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. 36Value *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 nullptr); 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. 64Value *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 nullptr); 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. 93Value *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, nullptr); 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. 117Value *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), nullptr); 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. 149Value *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, nullptr); 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. 173Value *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(), nullptr); 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. 200Value *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), nullptr); 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. 228Value *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 nullptr); 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. 255Value *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), nullptr); 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'. 285static 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. 303Value *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(), nullptr); 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. 324Value *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(), nullptr); 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. 342Value *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(), nullptr); 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. 364Value *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 nullptr); 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. 388Value *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 nullptr); 405 else 406 F = M->getOrInsertFunction("fputc", 407 B.getInt32Ty(), 408 B.getInt32Ty(), 409 File->getType(), nullptr); 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. 421Value *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(), nullptr); 440 else 441 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(), 442 B.getInt8PtrTy(), 443 File->getType(), nullptr); 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. 453Value *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(), nullptr); 476 else 477 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context), 478 B.getInt8PtrTy(), 479 TD->getIntPtrType(Context), 480 TD->getIntPtrType(Context), 481 File->getType(), nullptr); 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