1 //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements some functions that will create standard C libcalls. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Transforms/Utils/BuildLibCalls.h" 14 #include "llvm/ADT/SmallString.h" 15 #include "llvm/ADT/Statistic.h" 16 #include "llvm/Analysis/TargetLibraryInfo.h" 17 #include "llvm/IR/Constants.h" 18 #include "llvm/IR/DataLayout.h" 19 #include "llvm/IR/Function.h" 20 #include "llvm/IR/IRBuilder.h" 21 #include "llvm/IR/Intrinsics.h" 22 #include "llvm/IR/LLVMContext.h" 23 #include "llvm/IR/Module.h" 24 #include "llvm/IR/Type.h" 25 #include "llvm/Analysis/MemoryBuiltins.h" 26 27 using namespace llvm; 28 29 #define DEBUG_TYPE "build-libcalls" 30 31 //- Infer Attributes ---------------------------------------------------------// 32 33 STATISTIC(NumReadNone, "Number of functions inferred as readnone"); 34 STATISTIC(NumReadOnly, "Number of functions inferred as readonly"); 35 STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly"); 36 STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind"); 37 STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture"); 38 STATISTIC(NumWriteOnlyArg, "Number of arguments inferred as writeonly"); 39 STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly"); 40 STATISTIC(NumNoAlias, "Number of function returns inferred as noalias"); 41 STATISTIC(NumNoUndef, "Number of function returns inferred as noundef returns"); 42 STATISTIC(NumNonNull, "Number of function returns inferred as nonnull returns"); 43 STATISTIC(NumReturnedArg, "Number of arguments inferred as returned"); 44 45 static bool setDoesNotAccessMemory(Function &F) { 46 if (F.doesNotAccessMemory()) 47 return false; 48 F.setDoesNotAccessMemory(); 49 ++NumReadNone; 50 return true; 51 } 52 53 static bool setOnlyReadsMemory(Function &F) { 54 if (F.onlyReadsMemory()) 55 return false; 56 F.setOnlyReadsMemory(); 57 ++NumReadOnly; 58 return true; 59 } 60 61 static bool setOnlyAccessesArgMemory(Function &F) { 62 if (F.onlyAccessesArgMemory()) 63 return false; 64 F.setOnlyAccessesArgMemory(); 65 ++NumArgMemOnly; 66 return true; 67 } 68 69 static bool setDoesNotThrow(Function &F) { 70 if (F.doesNotThrow()) 71 return false; 72 F.setDoesNotThrow(); 73 ++NumNoUnwind; 74 return true; 75 } 76 77 static bool setRetDoesNotAlias(Function &F) { 78 if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias)) 79 return false; 80 F.addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias); 81 ++NumNoAlias; 82 return true; 83 } 84 85 static bool setDoesNotCapture(Function &F, unsigned ArgNo) { 86 if (F.hasParamAttribute(ArgNo, Attribute::NoCapture)) 87 return false; 88 F.addParamAttr(ArgNo, Attribute::NoCapture); 89 ++NumNoCapture; 90 return true; 91 } 92 93 static bool setDoesNotAlias(Function &F, unsigned ArgNo) { 94 if (F.hasParamAttribute(ArgNo, Attribute::NoAlias)) 95 return false; 96 F.addParamAttr(ArgNo, Attribute::NoAlias); 97 ++NumNoAlias; 98 return true; 99 } 100 101 static bool setOnlyReadsMemory(Function &F, unsigned ArgNo) { 102 if (F.hasParamAttribute(ArgNo, Attribute::ReadOnly)) 103 return false; 104 F.addParamAttr(ArgNo, Attribute::ReadOnly); 105 ++NumReadOnlyArg; 106 return true; 107 } 108 109 static bool setOnlyWritesMemory(Function &F, unsigned ArgNo) { 110 if (F.hasParamAttribute(ArgNo, Attribute::WriteOnly)) 111 return false; 112 F.addParamAttr(ArgNo, Attribute::WriteOnly); 113 ++NumWriteOnlyArg; 114 return true; 115 } 116 117 118 static bool setRetNoUndef(Function &F) { 119 if (!F.getReturnType()->isVoidTy() && 120 !F.hasAttribute(AttributeList::ReturnIndex, Attribute::NoUndef)) { 121 F.addAttribute(AttributeList::ReturnIndex, Attribute::NoUndef); 122 ++NumNoUndef; 123 return true; 124 } 125 return false; 126 } 127 128 static bool setArgsNoUndef(Function &F) { 129 bool Changed = false; 130 for (unsigned ArgNo = 0; ArgNo < F.arg_size(); ++ArgNo) { 131 if (!F.hasParamAttribute(ArgNo, Attribute::NoUndef)) { 132 F.addParamAttr(ArgNo, Attribute::NoUndef); 133 ++NumNoUndef; 134 Changed = true; 135 } 136 } 137 return Changed; 138 } 139 140 static bool setRetAndArgsNoUndef(Function &F) { 141 return setRetNoUndef(F) | setArgsNoUndef(F); 142 } 143 144 static bool setRetNonNull(Function &F) { 145 assert(F.getReturnType()->isPointerTy() && 146 "nonnull applies only to pointers"); 147 if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NonNull)) 148 return false; 149 F.addAttribute(AttributeList::ReturnIndex, Attribute::NonNull); 150 ++NumNonNull; 151 return true; 152 } 153 154 static bool setReturnedArg(Function &F, unsigned ArgNo) { 155 if (F.hasParamAttribute(ArgNo, Attribute::Returned)) 156 return false; 157 F.addParamAttr(ArgNo, Attribute::Returned); 158 ++NumReturnedArg; 159 return true; 160 } 161 162 static bool setNonLazyBind(Function &F) { 163 if (F.hasFnAttribute(Attribute::NonLazyBind)) 164 return false; 165 F.addFnAttr(Attribute::NonLazyBind); 166 return true; 167 } 168 169 static bool setDoesNotFreeMemory(Function &F) { 170 if (F.hasFnAttribute(Attribute::NoFree)) 171 return false; 172 F.addFnAttr(Attribute::NoFree); 173 return true; 174 } 175 176 bool llvm::inferLibFuncAttributes(Module *M, StringRef Name, 177 const TargetLibraryInfo &TLI) { 178 Function *F = M->getFunction(Name); 179 if (!F) 180 return false; 181 return inferLibFuncAttributes(*F, TLI); 182 } 183 184 bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) { 185 LibFunc TheLibFunc; 186 if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc))) 187 return false; 188 189 bool Changed = false; 190 191 if(!isLibFreeFunction(&F, TheLibFunc) && !isReallocLikeFn(&F, &TLI)) 192 Changed |= setDoesNotFreeMemory(F); 193 194 if (F.getParent() != nullptr && F.getParent()->getRtLibUseGOT()) 195 Changed |= setNonLazyBind(F); 196 197 switch (TheLibFunc) { 198 case LibFunc_strlen: 199 case LibFunc_wcslen: 200 Changed |= setOnlyReadsMemory(F); 201 Changed |= setDoesNotThrow(F); 202 Changed |= setOnlyAccessesArgMemory(F); 203 Changed |= setDoesNotCapture(F, 0); 204 return Changed; 205 case LibFunc_strchr: 206 case LibFunc_strrchr: 207 Changed |= setOnlyAccessesArgMemory(F); 208 Changed |= setOnlyReadsMemory(F); 209 Changed |= setDoesNotThrow(F); 210 return Changed; 211 case LibFunc_strtol: 212 case LibFunc_strtod: 213 case LibFunc_strtof: 214 case LibFunc_strtoul: 215 case LibFunc_strtoll: 216 case LibFunc_strtold: 217 case LibFunc_strtoull: 218 Changed |= setDoesNotThrow(F); 219 Changed |= setDoesNotCapture(F, 1); 220 Changed |= setOnlyReadsMemory(F, 0); 221 return Changed; 222 case LibFunc_strcpy: 223 case LibFunc_strncpy: 224 case LibFunc_strcat: 225 case LibFunc_strncat: 226 Changed |= setReturnedArg(F, 0); 227 LLVM_FALLTHROUGH; 228 case LibFunc_stpcpy: 229 case LibFunc_stpncpy: 230 Changed |= setOnlyAccessesArgMemory(F); 231 Changed |= setDoesNotThrow(F); 232 Changed |= setDoesNotCapture(F, 1); 233 Changed |= setOnlyWritesMemory(F, 0); 234 Changed |= setOnlyReadsMemory(F, 1); 235 Changed |= setDoesNotAlias(F, 0); 236 Changed |= setDoesNotAlias(F, 1); 237 return Changed; 238 case LibFunc_strxfrm: 239 Changed |= setDoesNotThrow(F); 240 Changed |= setDoesNotCapture(F, 0); 241 Changed |= setDoesNotCapture(F, 1); 242 Changed |= setOnlyReadsMemory(F, 1); 243 return Changed; 244 case LibFunc_strcmp: // 0,1 245 case LibFunc_strspn: // 0,1 246 case LibFunc_strncmp: // 0,1 247 case LibFunc_strcspn: // 0,1 248 Changed |= setDoesNotThrow(F); 249 Changed |= setOnlyAccessesArgMemory(F); 250 Changed |= setOnlyReadsMemory(F); 251 Changed |= setDoesNotCapture(F, 0); 252 Changed |= setDoesNotCapture(F, 1); 253 return Changed; 254 case LibFunc_strcoll: 255 case LibFunc_strcasecmp: // 0,1 256 case LibFunc_strncasecmp: // 257 // Those functions may depend on the locale, which may be accessed through 258 // global memory. 259 Changed |= setOnlyReadsMemory(F); 260 Changed |= setDoesNotThrow(F); 261 Changed |= setDoesNotCapture(F, 0); 262 Changed |= setDoesNotCapture(F, 1); 263 return Changed; 264 case LibFunc_strstr: 265 case LibFunc_strpbrk: 266 Changed |= setOnlyAccessesArgMemory(F); 267 Changed |= setOnlyReadsMemory(F); 268 Changed |= setDoesNotThrow(F); 269 Changed |= setDoesNotCapture(F, 1); 270 return Changed; 271 case LibFunc_strtok: 272 case LibFunc_strtok_r: 273 Changed |= setDoesNotThrow(F); 274 Changed |= setDoesNotCapture(F, 1); 275 Changed |= setOnlyReadsMemory(F, 1); 276 return Changed; 277 case LibFunc_scanf: 278 Changed |= setRetAndArgsNoUndef(F); 279 Changed |= setDoesNotThrow(F); 280 Changed |= setDoesNotCapture(F, 0); 281 Changed |= setOnlyReadsMemory(F, 0); 282 return Changed; 283 case LibFunc_setbuf: 284 case LibFunc_setvbuf: 285 Changed |= setRetAndArgsNoUndef(F); 286 Changed |= setDoesNotThrow(F); 287 Changed |= setDoesNotCapture(F, 0); 288 return Changed; 289 case LibFunc_strdup: 290 case LibFunc_strndup: 291 Changed |= setDoesNotThrow(F); 292 Changed |= setRetDoesNotAlias(F); 293 Changed |= setDoesNotCapture(F, 0); 294 Changed |= setOnlyReadsMemory(F, 0); 295 return Changed; 296 case LibFunc_stat: 297 case LibFunc_statvfs: 298 Changed |= setRetAndArgsNoUndef(F); 299 Changed |= setDoesNotThrow(F); 300 Changed |= setDoesNotCapture(F, 0); 301 Changed |= setDoesNotCapture(F, 1); 302 Changed |= setOnlyReadsMemory(F, 0); 303 return Changed; 304 case LibFunc_sscanf: 305 Changed |= setRetAndArgsNoUndef(F); 306 Changed |= setDoesNotThrow(F); 307 Changed |= setDoesNotCapture(F, 0); 308 Changed |= setDoesNotCapture(F, 1); 309 Changed |= setOnlyReadsMemory(F, 0); 310 Changed |= setOnlyReadsMemory(F, 1); 311 return Changed; 312 case LibFunc_sprintf: 313 Changed |= setRetAndArgsNoUndef(F); 314 Changed |= setDoesNotThrow(F); 315 Changed |= setDoesNotCapture(F, 0); 316 Changed |= setDoesNotAlias(F, 0); 317 Changed |= setOnlyWritesMemory(F, 0); 318 Changed |= setDoesNotCapture(F, 1); 319 Changed |= setOnlyReadsMemory(F, 1); 320 return Changed; 321 case LibFunc_snprintf: 322 Changed |= setRetAndArgsNoUndef(F); 323 Changed |= setDoesNotThrow(F); 324 Changed |= setDoesNotCapture(F, 0); 325 Changed |= setDoesNotAlias(F, 0); 326 Changed |= setOnlyWritesMemory(F, 0); 327 Changed |= setDoesNotCapture(F, 2); 328 Changed |= setOnlyReadsMemory(F, 2); 329 return Changed; 330 case LibFunc_setitimer: 331 Changed |= setRetAndArgsNoUndef(F); 332 Changed |= setDoesNotThrow(F); 333 Changed |= setDoesNotCapture(F, 1); 334 Changed |= setDoesNotCapture(F, 2); 335 Changed |= setOnlyReadsMemory(F, 1); 336 return Changed; 337 case LibFunc_system: 338 // May throw; "system" is a valid pthread cancellation point. 339 Changed |= setRetAndArgsNoUndef(F); 340 Changed |= setDoesNotCapture(F, 0); 341 Changed |= setOnlyReadsMemory(F, 0); 342 return Changed; 343 case LibFunc_malloc: 344 Changed |= setRetNoUndef(F); 345 Changed |= setDoesNotThrow(F); 346 Changed |= setRetDoesNotAlias(F); 347 return Changed; 348 case LibFunc_memcmp: 349 Changed |= setOnlyAccessesArgMemory(F); 350 Changed |= setOnlyReadsMemory(F); 351 Changed |= setDoesNotThrow(F); 352 Changed |= setDoesNotCapture(F, 0); 353 Changed |= setDoesNotCapture(F, 1); 354 return Changed; 355 case LibFunc_memchr: 356 case LibFunc_memrchr: 357 Changed |= setDoesNotThrow(F); 358 Changed |= setOnlyAccessesArgMemory(F); 359 Changed |= setOnlyReadsMemory(F); 360 return Changed; 361 case LibFunc_modf: 362 case LibFunc_modff: 363 case LibFunc_modfl: 364 Changed |= setDoesNotThrow(F); 365 Changed |= setDoesNotCapture(F, 1); 366 return Changed; 367 case LibFunc_memcpy: 368 Changed |= setDoesNotThrow(F); 369 Changed |= setOnlyAccessesArgMemory(F); 370 Changed |= setDoesNotAlias(F, 0); 371 Changed |= setReturnedArg(F, 0); 372 Changed |= setOnlyWritesMemory(F, 0); 373 Changed |= setDoesNotAlias(F, 1); 374 Changed |= setDoesNotCapture(F, 1); 375 Changed |= setOnlyReadsMemory(F, 1); 376 return Changed; 377 case LibFunc_memmove: 378 Changed |= setDoesNotThrow(F); 379 Changed |= setOnlyAccessesArgMemory(F); 380 Changed |= setReturnedArg(F, 0); 381 Changed |= setOnlyWritesMemory(F, 0); 382 Changed |= setDoesNotCapture(F, 1); 383 Changed |= setOnlyReadsMemory(F, 1); 384 return Changed; 385 case LibFunc_mempcpy: 386 case LibFunc_memccpy: 387 Changed |= setDoesNotThrow(F); 388 Changed |= setOnlyAccessesArgMemory(F); 389 Changed |= setDoesNotAlias(F, 0); 390 Changed |= setOnlyWritesMemory(F, 0); 391 Changed |= setDoesNotAlias(F, 1); 392 Changed |= setDoesNotCapture(F, 1); 393 Changed |= setOnlyReadsMemory(F, 1); 394 return Changed; 395 case LibFunc_memcpy_chk: 396 Changed |= setDoesNotThrow(F); 397 return Changed; 398 case LibFunc_memalign: 399 Changed |= setRetDoesNotAlias(F); 400 return Changed; 401 case LibFunc_mkdir: 402 Changed |= setRetAndArgsNoUndef(F); 403 Changed |= setDoesNotThrow(F); 404 Changed |= setDoesNotCapture(F, 0); 405 Changed |= setOnlyReadsMemory(F, 0); 406 return Changed; 407 case LibFunc_mktime: 408 Changed |= setRetAndArgsNoUndef(F); 409 Changed |= setDoesNotThrow(F); 410 Changed |= setDoesNotCapture(F, 0); 411 return Changed; 412 case LibFunc_realloc: 413 Changed |= setRetNoUndef(F); 414 Changed |= setDoesNotThrow(F); 415 Changed |= setRetDoesNotAlias(F); 416 Changed |= setDoesNotCapture(F, 0); 417 return Changed; 418 case LibFunc_reallocf: 419 Changed |= setRetNoUndef(F); 420 return Changed; 421 case LibFunc_read: 422 // May throw; "read" is a valid pthread cancellation point. 423 Changed |= setRetAndArgsNoUndef(F); 424 Changed |= setDoesNotCapture(F, 1); 425 return Changed; 426 case LibFunc_rewind: 427 Changed |= setRetAndArgsNoUndef(F); 428 Changed |= setDoesNotThrow(F); 429 Changed |= setDoesNotCapture(F, 0); 430 return Changed; 431 case LibFunc_rmdir: 432 case LibFunc_remove: 433 case LibFunc_realpath: 434 Changed |= setRetAndArgsNoUndef(F); 435 Changed |= setDoesNotThrow(F); 436 Changed |= setDoesNotCapture(F, 0); 437 Changed |= setOnlyReadsMemory(F, 0); 438 return Changed; 439 case LibFunc_rename: 440 Changed |= setRetAndArgsNoUndef(F); 441 Changed |= setDoesNotThrow(F); 442 Changed |= setDoesNotCapture(F, 0); 443 Changed |= setDoesNotCapture(F, 1); 444 Changed |= setOnlyReadsMemory(F, 0); 445 Changed |= setOnlyReadsMemory(F, 1); 446 return Changed; 447 case LibFunc_readlink: 448 Changed |= setRetAndArgsNoUndef(F); 449 Changed |= setDoesNotThrow(F); 450 Changed |= setDoesNotCapture(F, 0); 451 Changed |= setDoesNotCapture(F, 1); 452 Changed |= setOnlyReadsMemory(F, 0); 453 return Changed; 454 case LibFunc_write: 455 // May throw; "write" is a valid pthread cancellation point. 456 Changed |= setRetAndArgsNoUndef(F); 457 Changed |= setDoesNotCapture(F, 1); 458 Changed |= setOnlyReadsMemory(F, 1); 459 return Changed; 460 case LibFunc_aligned_alloc: 461 Changed |= setRetNoUndef(F); 462 Changed |= setDoesNotThrow(F); 463 Changed |= setRetDoesNotAlias(F); 464 return Changed; 465 case LibFunc_bcopy: 466 Changed |= setDoesNotThrow(F); 467 Changed |= setOnlyAccessesArgMemory(F); 468 Changed |= setDoesNotCapture(F, 0); 469 Changed |= setOnlyReadsMemory(F, 0); 470 Changed |= setOnlyWritesMemory(F, 1); 471 Changed |= setDoesNotCapture(F, 1); 472 return Changed; 473 case LibFunc_bcmp: 474 Changed |= setDoesNotThrow(F); 475 Changed |= setOnlyAccessesArgMemory(F); 476 Changed |= setOnlyReadsMemory(F); 477 Changed |= setDoesNotCapture(F, 0); 478 Changed |= setDoesNotCapture(F, 1); 479 return Changed; 480 case LibFunc_bzero: 481 Changed |= setDoesNotThrow(F); 482 Changed |= setOnlyAccessesArgMemory(F); 483 Changed |= setDoesNotCapture(F, 0); 484 Changed |= setOnlyWritesMemory(F, 0); 485 return Changed; 486 case LibFunc_calloc: 487 Changed |= setRetNoUndef(F); 488 Changed |= setDoesNotThrow(F); 489 Changed |= setRetDoesNotAlias(F); 490 return Changed; 491 case LibFunc_chmod: 492 case LibFunc_chown: 493 Changed |= setRetAndArgsNoUndef(F); 494 Changed |= setDoesNotThrow(F); 495 Changed |= setDoesNotCapture(F, 0); 496 Changed |= setOnlyReadsMemory(F, 0); 497 return Changed; 498 case LibFunc_ctermid: 499 case LibFunc_clearerr: 500 case LibFunc_closedir: 501 Changed |= setRetAndArgsNoUndef(F); 502 Changed |= setDoesNotThrow(F); 503 Changed |= setDoesNotCapture(F, 0); 504 return Changed; 505 case LibFunc_atoi: 506 case LibFunc_atol: 507 case LibFunc_atof: 508 case LibFunc_atoll: 509 Changed |= setDoesNotThrow(F); 510 Changed |= setOnlyReadsMemory(F); 511 Changed |= setDoesNotCapture(F, 0); 512 return Changed; 513 case LibFunc_access: 514 Changed |= setRetAndArgsNoUndef(F); 515 Changed |= setDoesNotThrow(F); 516 Changed |= setDoesNotCapture(F, 0); 517 Changed |= setOnlyReadsMemory(F, 0); 518 return Changed; 519 case LibFunc_fopen: 520 Changed |= setRetAndArgsNoUndef(F); 521 Changed |= setDoesNotThrow(F); 522 Changed |= setRetDoesNotAlias(F); 523 Changed |= setDoesNotCapture(F, 0); 524 Changed |= setDoesNotCapture(F, 1); 525 Changed |= setOnlyReadsMemory(F, 0); 526 Changed |= setOnlyReadsMemory(F, 1); 527 return Changed; 528 case LibFunc_fdopen: 529 Changed |= setRetAndArgsNoUndef(F); 530 Changed |= setDoesNotThrow(F); 531 Changed |= setRetDoesNotAlias(F); 532 Changed |= setDoesNotCapture(F, 1); 533 Changed |= setOnlyReadsMemory(F, 1); 534 return Changed; 535 case LibFunc_feof: 536 Changed |= setRetAndArgsNoUndef(F); 537 Changed |= setDoesNotThrow(F); 538 Changed |= setDoesNotCapture(F, 0); 539 return Changed; 540 case LibFunc_free: 541 Changed |= setArgsNoUndef(F); 542 Changed |= setDoesNotThrow(F); 543 Changed |= setDoesNotCapture(F, 0); 544 return Changed; 545 case LibFunc_fseek: 546 case LibFunc_ftell: 547 case LibFunc_fgetc: 548 case LibFunc_fgetc_unlocked: 549 case LibFunc_fseeko: 550 case LibFunc_ftello: 551 case LibFunc_fileno: 552 case LibFunc_fflush: 553 case LibFunc_fclose: 554 case LibFunc_fsetpos: 555 case LibFunc_flockfile: 556 case LibFunc_funlockfile: 557 case LibFunc_ftrylockfile: 558 Changed |= setRetAndArgsNoUndef(F); 559 Changed |= setDoesNotThrow(F); 560 Changed |= setDoesNotCapture(F, 0); 561 return Changed; 562 case LibFunc_ferror: 563 Changed |= setRetAndArgsNoUndef(F); 564 Changed |= setDoesNotThrow(F); 565 Changed |= setDoesNotCapture(F, 0); 566 Changed |= setOnlyReadsMemory(F); 567 return Changed; 568 case LibFunc_fputc: 569 case LibFunc_fputc_unlocked: 570 case LibFunc_fstat: 571 Changed |= setRetAndArgsNoUndef(F); 572 Changed |= setDoesNotThrow(F); 573 Changed |= setDoesNotCapture(F, 1); 574 return Changed; 575 case LibFunc_frexp: 576 case LibFunc_frexpf: 577 case LibFunc_frexpl: 578 Changed |= setDoesNotThrow(F); 579 Changed |= setDoesNotCapture(F, 1); 580 return Changed; 581 case LibFunc_fstatvfs: 582 Changed |= setRetAndArgsNoUndef(F); 583 Changed |= setDoesNotThrow(F); 584 Changed |= setDoesNotCapture(F, 1); 585 return Changed; 586 case LibFunc_fgets: 587 case LibFunc_fgets_unlocked: 588 Changed |= setRetAndArgsNoUndef(F); 589 Changed |= setDoesNotThrow(F); 590 Changed |= setDoesNotCapture(F, 2); 591 return Changed; 592 case LibFunc_fread: 593 case LibFunc_fread_unlocked: 594 Changed |= setRetAndArgsNoUndef(F); 595 Changed |= setDoesNotThrow(F); 596 Changed |= setDoesNotCapture(F, 0); 597 Changed |= setDoesNotCapture(F, 3); 598 return Changed; 599 case LibFunc_fwrite: 600 case LibFunc_fwrite_unlocked: 601 Changed |= setRetAndArgsNoUndef(F); 602 Changed |= setDoesNotThrow(F); 603 Changed |= setDoesNotCapture(F, 0); 604 Changed |= setDoesNotCapture(F, 3); 605 // FIXME: readonly #1? 606 return Changed; 607 case LibFunc_fputs: 608 case LibFunc_fputs_unlocked: 609 Changed |= setRetAndArgsNoUndef(F); 610 Changed |= setDoesNotThrow(F); 611 Changed |= setDoesNotCapture(F, 0); 612 Changed |= setDoesNotCapture(F, 1); 613 Changed |= setOnlyReadsMemory(F, 0); 614 return Changed; 615 case LibFunc_fscanf: 616 case LibFunc_fprintf: 617 Changed |= setRetAndArgsNoUndef(F); 618 Changed |= setDoesNotThrow(F); 619 Changed |= setDoesNotCapture(F, 0); 620 Changed |= setDoesNotCapture(F, 1); 621 Changed |= setOnlyReadsMemory(F, 1); 622 return Changed; 623 case LibFunc_fgetpos: 624 Changed |= setRetAndArgsNoUndef(F); 625 Changed |= setDoesNotThrow(F); 626 Changed |= setDoesNotCapture(F, 0); 627 Changed |= setDoesNotCapture(F, 1); 628 return Changed; 629 case LibFunc_getc: 630 Changed |= setRetAndArgsNoUndef(F); 631 Changed |= setDoesNotThrow(F); 632 Changed |= setDoesNotCapture(F, 0); 633 return Changed; 634 case LibFunc_getlogin_r: 635 Changed |= setRetAndArgsNoUndef(F); 636 Changed |= setDoesNotThrow(F); 637 Changed |= setDoesNotCapture(F, 0); 638 return Changed; 639 case LibFunc_getc_unlocked: 640 Changed |= setRetAndArgsNoUndef(F); 641 Changed |= setDoesNotThrow(F); 642 Changed |= setDoesNotCapture(F, 0); 643 return Changed; 644 case LibFunc_getenv: 645 Changed |= setRetAndArgsNoUndef(F); 646 Changed |= setDoesNotThrow(F); 647 Changed |= setOnlyReadsMemory(F); 648 Changed |= setDoesNotCapture(F, 0); 649 return Changed; 650 case LibFunc_gets: 651 case LibFunc_getchar: 652 case LibFunc_getchar_unlocked: 653 Changed |= setRetAndArgsNoUndef(F); 654 Changed |= setDoesNotThrow(F); 655 return Changed; 656 case LibFunc_getitimer: 657 Changed |= setRetAndArgsNoUndef(F); 658 Changed |= setDoesNotThrow(F); 659 Changed |= setDoesNotCapture(F, 1); 660 return Changed; 661 case LibFunc_getpwnam: 662 Changed |= setRetAndArgsNoUndef(F); 663 Changed |= setDoesNotThrow(F); 664 Changed |= setDoesNotCapture(F, 0); 665 Changed |= setOnlyReadsMemory(F, 0); 666 return Changed; 667 case LibFunc_ungetc: 668 Changed |= setRetAndArgsNoUndef(F); 669 Changed |= setDoesNotThrow(F); 670 Changed |= setDoesNotCapture(F, 1); 671 return Changed; 672 case LibFunc_uname: 673 Changed |= setRetAndArgsNoUndef(F); 674 Changed |= setDoesNotThrow(F); 675 Changed |= setDoesNotCapture(F, 0); 676 return Changed; 677 case LibFunc_unlink: 678 Changed |= setRetAndArgsNoUndef(F); 679 Changed |= setDoesNotThrow(F); 680 Changed |= setDoesNotCapture(F, 0); 681 Changed |= setOnlyReadsMemory(F, 0); 682 return Changed; 683 case LibFunc_unsetenv: 684 Changed |= setRetAndArgsNoUndef(F); 685 Changed |= setDoesNotThrow(F); 686 Changed |= setDoesNotCapture(F, 0); 687 Changed |= setOnlyReadsMemory(F, 0); 688 return Changed; 689 case LibFunc_utime: 690 case LibFunc_utimes: 691 Changed |= setRetAndArgsNoUndef(F); 692 Changed |= setDoesNotThrow(F); 693 Changed |= setDoesNotCapture(F, 0); 694 Changed |= setDoesNotCapture(F, 1); 695 Changed |= setOnlyReadsMemory(F, 0); 696 Changed |= setOnlyReadsMemory(F, 1); 697 return Changed; 698 case LibFunc_putc: 699 case LibFunc_putc_unlocked: 700 Changed |= setRetAndArgsNoUndef(F); 701 Changed |= setDoesNotThrow(F); 702 Changed |= setDoesNotCapture(F, 1); 703 return Changed; 704 case LibFunc_puts: 705 case LibFunc_printf: 706 case LibFunc_perror: 707 Changed |= setRetAndArgsNoUndef(F); 708 Changed |= setDoesNotThrow(F); 709 Changed |= setDoesNotCapture(F, 0); 710 Changed |= setOnlyReadsMemory(F, 0); 711 return Changed; 712 case LibFunc_pread: 713 // May throw; "pread" is a valid pthread cancellation point. 714 Changed |= setRetAndArgsNoUndef(F); 715 Changed |= setDoesNotCapture(F, 1); 716 return Changed; 717 case LibFunc_pwrite: 718 // May throw; "pwrite" is a valid pthread cancellation point. 719 Changed |= setRetAndArgsNoUndef(F); 720 Changed |= setDoesNotCapture(F, 1); 721 Changed |= setOnlyReadsMemory(F, 1); 722 return Changed; 723 case LibFunc_putchar: 724 case LibFunc_putchar_unlocked: 725 Changed |= setRetAndArgsNoUndef(F); 726 Changed |= setDoesNotThrow(F); 727 return Changed; 728 case LibFunc_popen: 729 Changed |= setRetAndArgsNoUndef(F); 730 Changed |= setDoesNotThrow(F); 731 Changed |= setRetDoesNotAlias(F); 732 Changed |= setDoesNotCapture(F, 0); 733 Changed |= setDoesNotCapture(F, 1); 734 Changed |= setOnlyReadsMemory(F, 0); 735 Changed |= setOnlyReadsMemory(F, 1); 736 return Changed; 737 case LibFunc_pclose: 738 Changed |= setRetAndArgsNoUndef(F); 739 Changed |= setDoesNotThrow(F); 740 Changed |= setDoesNotCapture(F, 0); 741 return Changed; 742 case LibFunc_vscanf: 743 Changed |= setRetAndArgsNoUndef(F); 744 Changed |= setDoesNotThrow(F); 745 Changed |= setDoesNotCapture(F, 0); 746 Changed |= setOnlyReadsMemory(F, 0); 747 return Changed; 748 case LibFunc_vsscanf: 749 Changed |= setRetAndArgsNoUndef(F); 750 Changed |= setDoesNotThrow(F); 751 Changed |= setDoesNotCapture(F, 0); 752 Changed |= setDoesNotCapture(F, 1); 753 Changed |= setOnlyReadsMemory(F, 0); 754 Changed |= setOnlyReadsMemory(F, 1); 755 return Changed; 756 case LibFunc_vfscanf: 757 Changed |= setRetAndArgsNoUndef(F); 758 Changed |= setDoesNotThrow(F); 759 Changed |= setDoesNotCapture(F, 0); 760 Changed |= setDoesNotCapture(F, 1); 761 Changed |= setOnlyReadsMemory(F, 1); 762 return Changed; 763 case LibFunc_valloc: 764 Changed |= setRetNoUndef(F); 765 Changed |= setDoesNotThrow(F); 766 Changed |= setRetDoesNotAlias(F); 767 return Changed; 768 case LibFunc_vprintf: 769 Changed |= setRetAndArgsNoUndef(F); 770 Changed |= setDoesNotThrow(F); 771 Changed |= setDoesNotCapture(F, 0); 772 Changed |= setOnlyReadsMemory(F, 0); 773 return Changed; 774 case LibFunc_vfprintf: 775 case LibFunc_vsprintf: 776 Changed |= setRetAndArgsNoUndef(F); 777 Changed |= setDoesNotThrow(F); 778 Changed |= setDoesNotCapture(F, 0); 779 Changed |= setDoesNotCapture(F, 1); 780 Changed |= setOnlyReadsMemory(F, 1); 781 return Changed; 782 case LibFunc_vsnprintf: 783 Changed |= setRetAndArgsNoUndef(F); 784 Changed |= setDoesNotThrow(F); 785 Changed |= setDoesNotCapture(F, 0); 786 Changed |= setDoesNotCapture(F, 2); 787 Changed |= setOnlyReadsMemory(F, 2); 788 return Changed; 789 case LibFunc_open: 790 // May throw; "open" is a valid pthread cancellation point. 791 Changed |= setRetAndArgsNoUndef(F); 792 Changed |= setDoesNotCapture(F, 0); 793 Changed |= setOnlyReadsMemory(F, 0); 794 return Changed; 795 case LibFunc_opendir: 796 Changed |= setRetAndArgsNoUndef(F); 797 Changed |= setDoesNotThrow(F); 798 Changed |= setRetDoesNotAlias(F); 799 Changed |= setDoesNotCapture(F, 0); 800 Changed |= setOnlyReadsMemory(F, 0); 801 return Changed; 802 case LibFunc_tmpfile: 803 Changed |= setRetAndArgsNoUndef(F); 804 Changed |= setDoesNotThrow(F); 805 Changed |= setRetDoesNotAlias(F); 806 return Changed; 807 case LibFunc_times: 808 Changed |= setRetAndArgsNoUndef(F); 809 Changed |= setDoesNotThrow(F); 810 Changed |= setDoesNotCapture(F, 0); 811 return Changed; 812 case LibFunc_htonl: 813 case LibFunc_htons: 814 case LibFunc_ntohl: 815 case LibFunc_ntohs: 816 Changed |= setDoesNotThrow(F); 817 Changed |= setDoesNotAccessMemory(F); 818 return Changed; 819 case LibFunc_lstat: 820 Changed |= setRetAndArgsNoUndef(F); 821 Changed |= setDoesNotThrow(F); 822 Changed |= setDoesNotCapture(F, 0); 823 Changed |= setDoesNotCapture(F, 1); 824 Changed |= setOnlyReadsMemory(F, 0); 825 return Changed; 826 case LibFunc_lchown: 827 Changed |= setRetAndArgsNoUndef(F); 828 Changed |= setDoesNotThrow(F); 829 Changed |= setDoesNotCapture(F, 0); 830 Changed |= setOnlyReadsMemory(F, 0); 831 return Changed; 832 case LibFunc_qsort: 833 // May throw; places call through function pointer. 834 // Cannot give undef pointer/size 835 Changed |= setRetAndArgsNoUndef(F); 836 Changed |= setDoesNotCapture(F, 3); 837 return Changed; 838 case LibFunc_dunder_strdup: 839 case LibFunc_dunder_strndup: 840 Changed |= setDoesNotThrow(F); 841 Changed |= setRetDoesNotAlias(F); 842 Changed |= setDoesNotCapture(F, 0); 843 Changed |= setOnlyReadsMemory(F, 0); 844 return Changed; 845 case LibFunc_dunder_strtok_r: 846 Changed |= setDoesNotThrow(F); 847 Changed |= setDoesNotCapture(F, 1); 848 Changed |= setOnlyReadsMemory(F, 1); 849 return Changed; 850 case LibFunc_under_IO_getc: 851 Changed |= setRetAndArgsNoUndef(F); 852 Changed |= setDoesNotThrow(F); 853 Changed |= setDoesNotCapture(F, 0); 854 return Changed; 855 case LibFunc_under_IO_putc: 856 Changed |= setRetAndArgsNoUndef(F); 857 Changed |= setDoesNotThrow(F); 858 Changed |= setDoesNotCapture(F, 1); 859 return Changed; 860 case LibFunc_dunder_isoc99_scanf: 861 Changed |= setRetAndArgsNoUndef(F); 862 Changed |= setDoesNotThrow(F); 863 Changed |= setDoesNotCapture(F, 0); 864 Changed |= setOnlyReadsMemory(F, 0); 865 return Changed; 866 case LibFunc_stat64: 867 case LibFunc_lstat64: 868 case LibFunc_statvfs64: 869 Changed |= setRetAndArgsNoUndef(F); 870 Changed |= setDoesNotThrow(F); 871 Changed |= setDoesNotCapture(F, 0); 872 Changed |= setDoesNotCapture(F, 1); 873 Changed |= setOnlyReadsMemory(F, 0); 874 return Changed; 875 case LibFunc_dunder_isoc99_sscanf: 876 Changed |= setRetAndArgsNoUndef(F); 877 Changed |= setDoesNotThrow(F); 878 Changed |= setDoesNotCapture(F, 0); 879 Changed |= setDoesNotCapture(F, 1); 880 Changed |= setOnlyReadsMemory(F, 0); 881 Changed |= setOnlyReadsMemory(F, 1); 882 return Changed; 883 case LibFunc_fopen64: 884 Changed |= setRetAndArgsNoUndef(F); 885 Changed |= setDoesNotThrow(F); 886 Changed |= setRetDoesNotAlias(F); 887 Changed |= setDoesNotCapture(F, 0); 888 Changed |= setDoesNotCapture(F, 1); 889 Changed |= setOnlyReadsMemory(F, 0); 890 Changed |= setOnlyReadsMemory(F, 1); 891 return Changed; 892 case LibFunc_fseeko64: 893 case LibFunc_ftello64: 894 Changed |= setRetAndArgsNoUndef(F); 895 Changed |= setDoesNotThrow(F); 896 Changed |= setDoesNotCapture(F, 0); 897 return Changed; 898 case LibFunc_tmpfile64: 899 Changed |= setRetAndArgsNoUndef(F); 900 Changed |= setDoesNotThrow(F); 901 Changed |= setRetDoesNotAlias(F); 902 return Changed; 903 case LibFunc_fstat64: 904 case LibFunc_fstatvfs64: 905 Changed |= setRetAndArgsNoUndef(F); 906 Changed |= setDoesNotThrow(F); 907 Changed |= setDoesNotCapture(F, 1); 908 return Changed; 909 case LibFunc_open64: 910 // May throw; "open" is a valid pthread cancellation point. 911 Changed |= setRetAndArgsNoUndef(F); 912 Changed |= setDoesNotCapture(F, 0); 913 Changed |= setOnlyReadsMemory(F, 0); 914 return Changed; 915 case LibFunc_gettimeofday: 916 // Currently some platforms have the restrict keyword on the arguments to 917 // gettimeofday. To be conservative, do not add noalias to gettimeofday's 918 // arguments. 919 Changed |= setRetAndArgsNoUndef(F); 920 Changed |= setDoesNotThrow(F); 921 Changed |= setDoesNotCapture(F, 0); 922 Changed |= setDoesNotCapture(F, 1); 923 return Changed; 924 case LibFunc_Znwj: // new(unsigned int) 925 case LibFunc_Znwm: // new(unsigned long) 926 case LibFunc_Znaj: // new[](unsigned int) 927 case LibFunc_Znam: // new[](unsigned long) 928 case LibFunc_msvc_new_int: // new(unsigned int) 929 case LibFunc_msvc_new_longlong: // new(unsigned long long) 930 case LibFunc_msvc_new_array_int: // new[](unsigned int) 931 case LibFunc_msvc_new_array_longlong: // new[](unsigned long long) 932 // Operator new always returns a nonnull noalias pointer 933 Changed |= setRetNoUndef(F); 934 Changed |= setRetNonNull(F); 935 Changed |= setRetDoesNotAlias(F); 936 return Changed; 937 // TODO: add LibFunc entries for: 938 // case LibFunc_memset_pattern4: 939 // case LibFunc_memset_pattern8: 940 case LibFunc_memset_pattern16: 941 Changed |= setOnlyAccessesArgMemory(F); 942 Changed |= setDoesNotCapture(F, 0); 943 Changed |= setOnlyWritesMemory(F, 0); 944 Changed |= setDoesNotCapture(F, 1); 945 Changed |= setOnlyReadsMemory(F, 1); 946 return Changed; 947 // int __nvvm_reflect(const char *) 948 case LibFunc_nvvm_reflect: 949 Changed |= setRetAndArgsNoUndef(F); 950 Changed |= setDoesNotAccessMemory(F); 951 Changed |= setDoesNotThrow(F); 952 return Changed; 953 954 default: 955 // FIXME: It'd be really nice to cover all the library functions we're 956 // aware of here. 957 return false; 958 } 959 } 960 961 bool llvm::hasFloatFn(const TargetLibraryInfo *TLI, Type *Ty, 962 LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn) { 963 switch (Ty->getTypeID()) { 964 case Type::HalfTyID: 965 return false; 966 case Type::FloatTyID: 967 return TLI->has(FloatFn); 968 case Type::DoubleTyID: 969 return TLI->has(DoubleFn); 970 default: 971 return TLI->has(LongDoubleFn); 972 } 973 } 974 975 StringRef llvm::getFloatFnName(const TargetLibraryInfo *TLI, Type *Ty, 976 LibFunc DoubleFn, LibFunc FloatFn, 977 LibFunc LongDoubleFn) { 978 assert(hasFloatFn(TLI, Ty, DoubleFn, FloatFn, LongDoubleFn) && 979 "Cannot get name for unavailable function!"); 980 981 switch (Ty->getTypeID()) { 982 case Type::HalfTyID: 983 llvm_unreachable("No name for HalfTy!"); 984 case Type::FloatTyID: 985 return TLI->getName(FloatFn); 986 case Type::DoubleTyID: 987 return TLI->getName(DoubleFn); 988 default: 989 return TLI->getName(LongDoubleFn); 990 } 991 } 992 993 //- Emit LibCalls ------------------------------------------------------------// 994 995 Value *llvm::castToCStr(Value *V, IRBuilderBase &B) { 996 unsigned AS = V->getType()->getPointerAddressSpace(); 997 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr"); 998 } 999 1000 static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType, 1001 ArrayRef<Type *> ParamTypes, 1002 ArrayRef<Value *> Operands, IRBuilderBase &B, 1003 const TargetLibraryInfo *TLI, 1004 bool IsVaArgs = false) { 1005 if (!TLI->has(TheLibFunc)) 1006 return nullptr; 1007 1008 Module *M = B.GetInsertBlock()->getModule(); 1009 StringRef FuncName = TLI->getName(TheLibFunc); 1010 FunctionType *FuncType = FunctionType::get(ReturnType, ParamTypes, IsVaArgs); 1011 FunctionCallee Callee = M->getOrInsertFunction(FuncName, FuncType); 1012 inferLibFuncAttributes(M, FuncName, *TLI); 1013 CallInst *CI = B.CreateCall(Callee, Operands, FuncName); 1014 if (const Function *F = 1015 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 1016 CI->setCallingConv(F->getCallingConv()); 1017 return CI; 1018 } 1019 1020 Value *llvm::emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, 1021 const TargetLibraryInfo *TLI) { 1022 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1023 return emitLibCall(LibFunc_strlen, DL.getIntPtrType(Context), 1024 B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI); 1025 } 1026 1027 Value *llvm::emitStrDup(Value *Ptr, IRBuilderBase &B, 1028 const TargetLibraryInfo *TLI) { 1029 return emitLibCall(LibFunc_strdup, B.getInt8PtrTy(), B.getInt8PtrTy(), 1030 castToCStr(Ptr, B), B, TLI); 1031 } 1032 1033 Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilderBase &B, 1034 const TargetLibraryInfo *TLI) { 1035 Type *I8Ptr = B.getInt8PtrTy(); 1036 Type *I32Ty = B.getInt32Ty(); 1037 return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, I32Ty}, 1038 {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, B, TLI); 1039 } 1040 1041 Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 1042 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1043 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1044 return emitLibCall( 1045 LibFunc_strncmp, B.getInt32Ty(), 1046 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, 1047 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); 1048 } 1049 1050 Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B, 1051 const TargetLibraryInfo *TLI) { 1052 Type *I8Ptr = B.getInt8PtrTy(); 1053 return emitLibCall(LibFunc_strcpy, I8Ptr, {I8Ptr, I8Ptr}, 1054 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI); 1055 } 1056 1057 Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B, 1058 const TargetLibraryInfo *TLI) { 1059 Type *I8Ptr = B.getInt8PtrTy(); 1060 return emitLibCall(LibFunc_stpcpy, I8Ptr, {I8Ptr, I8Ptr}, 1061 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI); 1062 } 1063 1064 Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 1065 const TargetLibraryInfo *TLI) { 1066 Type *I8Ptr = B.getInt8PtrTy(); 1067 return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()}, 1068 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI); 1069 } 1070 1071 Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 1072 const TargetLibraryInfo *TLI) { 1073 Type *I8Ptr = B.getInt8PtrTy(); 1074 return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()}, 1075 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI); 1076 } 1077 1078 Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, 1079 IRBuilderBase &B, const DataLayout &DL, 1080 const TargetLibraryInfo *TLI) { 1081 if (!TLI->has(LibFunc_memcpy_chk)) 1082 return nullptr; 1083 1084 Module *M = B.GetInsertBlock()->getModule(); 1085 AttributeList AS; 1086 AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex, 1087 Attribute::NoUnwind); 1088 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1089 FunctionCallee MemCpy = M->getOrInsertFunction( 1090 "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(), 1091 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), 1092 DL.getIntPtrType(Context)); 1093 Dst = castToCStr(Dst, B); 1094 Src = castToCStr(Src, B); 1095 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize}); 1096 if (const Function *F = 1097 dyn_cast<Function>(MemCpy.getCallee()->stripPointerCasts())) 1098 CI->setCallingConv(F->getCallingConv()); 1099 return CI; 1100 } 1101 1102 Value *llvm::emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 1103 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1104 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1105 return emitLibCall( 1106 LibFunc_mempcpy, B.getInt8PtrTy(), 1107 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, 1108 {Dst, Src, Len}, B, TLI); 1109 } 1110 1111 Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, 1112 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1113 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1114 return emitLibCall( 1115 LibFunc_memchr, B.getInt8PtrTy(), 1116 {B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context)}, 1117 {castToCStr(Ptr, B), Val, Len}, B, TLI); 1118 } 1119 1120 Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 1121 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1122 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1123 return emitLibCall( 1124 LibFunc_memcmp, B.getInt32Ty(), 1125 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, 1126 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); 1127 } 1128 1129 Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 1130 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1131 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1132 return emitLibCall( 1133 LibFunc_bcmp, B.getInt32Ty(), 1134 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, 1135 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); 1136 } 1137 1138 Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, 1139 IRBuilderBase &B, const TargetLibraryInfo *TLI) { 1140 return emitLibCall( 1141 LibFunc_memccpy, B.getInt8PtrTy(), 1142 {B.getInt8PtrTy(), B.getInt8PtrTy(), B.getInt32Ty(), Len->getType()}, 1143 {Ptr1, Ptr2, Val, Len}, B, TLI); 1144 } 1145 1146 Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, 1147 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B, 1148 const TargetLibraryInfo *TLI) { 1149 SmallVector<Value *, 8> Args{castToCStr(Dest, B), Size, castToCStr(Fmt, B)}; 1150 Args.insert(Args.end(), VariadicArgs.begin(), VariadicArgs.end()); 1151 return emitLibCall(LibFunc_snprintf, B.getInt32Ty(), 1152 {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy()}, 1153 Args, B, TLI, /*IsVaArgs=*/true); 1154 } 1155 1156 Value *llvm::emitSPrintf(Value *Dest, Value *Fmt, 1157 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B, 1158 const TargetLibraryInfo *TLI) { 1159 SmallVector<Value *, 8> Args{castToCStr(Dest, B), castToCStr(Fmt, B)}; 1160 Args.insert(Args.end(), VariadicArgs.begin(), VariadicArgs.end()); 1161 return emitLibCall(LibFunc_sprintf, B.getInt32Ty(), 1162 {B.getInt8PtrTy(), B.getInt8PtrTy()}, Args, B, TLI, 1163 /*IsVaArgs=*/true); 1164 } 1165 1166 Value *llvm::emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, 1167 const TargetLibraryInfo *TLI) { 1168 return emitLibCall(LibFunc_strcat, B.getInt8PtrTy(), 1169 {B.getInt8PtrTy(), B.getInt8PtrTy()}, 1170 {castToCStr(Dest, B), castToCStr(Src, B)}, B, TLI); 1171 } 1172 1173 Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 1174 const TargetLibraryInfo *TLI) { 1175 return emitLibCall(LibFunc_strlcpy, Size->getType(), 1176 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()}, 1177 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); 1178 } 1179 1180 Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 1181 const TargetLibraryInfo *TLI) { 1182 return emitLibCall(LibFunc_strlcat, Size->getType(), 1183 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()}, 1184 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); 1185 } 1186 1187 Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 1188 const TargetLibraryInfo *TLI) { 1189 return emitLibCall(LibFunc_strncat, B.getInt8PtrTy(), 1190 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()}, 1191 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); 1192 } 1193 1194 Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, 1195 IRBuilderBase &B, const TargetLibraryInfo *TLI) { 1196 return emitLibCall( 1197 LibFunc_vsnprintf, B.getInt32Ty(), 1198 {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy(), VAList->getType()}, 1199 {castToCStr(Dest, B), Size, castToCStr(Fmt, B), VAList}, B, TLI); 1200 } 1201 1202 Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, 1203 IRBuilderBase &B, const TargetLibraryInfo *TLI) { 1204 return emitLibCall(LibFunc_vsprintf, B.getInt32Ty(), 1205 {B.getInt8PtrTy(), B.getInt8PtrTy(), VAList->getType()}, 1206 {castToCStr(Dest, B), castToCStr(Fmt, B), VAList}, B, TLI); 1207 } 1208 1209 /// Append a suffix to the function name according to the type of 'Op'. 1210 static void appendTypeSuffix(Value *Op, StringRef &Name, 1211 SmallString<20> &NameBuffer) { 1212 if (!Op->getType()->isDoubleTy()) { 1213 NameBuffer += Name; 1214 1215 if (Op->getType()->isFloatTy()) 1216 NameBuffer += 'f'; 1217 else 1218 NameBuffer += 'l'; 1219 1220 Name = NameBuffer; 1221 } 1222 } 1223 1224 static Value *emitUnaryFloatFnCallHelper(Value *Op, StringRef Name, 1225 IRBuilderBase &B, 1226 const AttributeList &Attrs) { 1227 assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall"); 1228 1229 Module *M = B.GetInsertBlock()->getModule(); 1230 FunctionCallee Callee = 1231 M->getOrInsertFunction(Name, Op->getType(), Op->getType()); 1232 CallInst *CI = B.CreateCall(Callee, Op, Name); 1233 1234 // The incoming attribute set may have come from a speculatable intrinsic, but 1235 // is being replaced with a library call which is not allowed to be 1236 // speculatable. 1237 CI->setAttributes(Attrs.removeAttribute(B.getContext(), 1238 AttributeList::FunctionIndex, 1239 Attribute::Speculatable)); 1240 if (const Function *F = 1241 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 1242 CI->setCallingConv(F->getCallingConv()); 1243 1244 return CI; 1245 } 1246 1247 Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilderBase &B, 1248 const AttributeList &Attrs) { 1249 SmallString<20> NameBuffer; 1250 appendTypeSuffix(Op, Name, NameBuffer); 1251 1252 return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs); 1253 } 1254 1255 Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, 1256 LibFunc DoubleFn, LibFunc FloatFn, 1257 LibFunc LongDoubleFn, IRBuilderBase &B, 1258 const AttributeList &Attrs) { 1259 // Get the name of the function according to TLI. 1260 StringRef Name = getFloatFnName(TLI, Op->getType(), 1261 DoubleFn, FloatFn, LongDoubleFn); 1262 1263 return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs); 1264 } 1265 1266 static Value *emitBinaryFloatFnCallHelper(Value *Op1, Value *Op2, 1267 StringRef Name, IRBuilderBase &B, 1268 const AttributeList &Attrs) { 1269 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall"); 1270 1271 Module *M = B.GetInsertBlock()->getModule(); 1272 FunctionCallee Callee = M->getOrInsertFunction(Name, Op1->getType(), 1273 Op1->getType(), Op2->getType()); 1274 CallInst *CI = B.CreateCall(Callee, { Op1, Op2 }, Name); 1275 1276 // The incoming attribute set may have come from a speculatable intrinsic, but 1277 // is being replaced with a library call which is not allowed to be 1278 // speculatable. 1279 CI->setAttributes(Attrs.removeAttribute(B.getContext(), 1280 AttributeList::FunctionIndex, 1281 Attribute::Speculatable)); 1282 if (const Function *F = 1283 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 1284 CI->setCallingConv(F->getCallingConv()); 1285 1286 return CI; 1287 } 1288 1289 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, 1290 IRBuilderBase &B, 1291 const AttributeList &Attrs) { 1292 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall"); 1293 1294 SmallString<20> NameBuffer; 1295 appendTypeSuffix(Op1, Name, NameBuffer); 1296 1297 return emitBinaryFloatFnCallHelper(Op1, Op2, Name, B, Attrs); 1298 } 1299 1300 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, 1301 const TargetLibraryInfo *TLI, 1302 LibFunc DoubleFn, LibFunc FloatFn, 1303 LibFunc LongDoubleFn, IRBuilderBase &B, 1304 const AttributeList &Attrs) { 1305 // Get the name of the function according to TLI. 1306 StringRef Name = getFloatFnName(TLI, Op1->getType(), 1307 DoubleFn, FloatFn, LongDoubleFn); 1308 1309 return emitBinaryFloatFnCallHelper(Op1, Op2, Name, B, Attrs); 1310 } 1311 1312 Value *llvm::emitPutChar(Value *Char, IRBuilderBase &B, 1313 const TargetLibraryInfo *TLI) { 1314 if (!TLI->has(LibFunc_putchar)) 1315 return nullptr; 1316 1317 Module *M = B.GetInsertBlock()->getModule(); 1318 StringRef PutCharName = TLI->getName(LibFunc_putchar); 1319 FunctionCallee PutChar = 1320 M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty()); 1321 inferLibFuncAttributes(M, PutCharName, *TLI); 1322 CallInst *CI = B.CreateCall(PutChar, 1323 B.CreateIntCast(Char, 1324 B.getInt32Ty(), 1325 /*isSigned*/true, 1326 "chari"), 1327 PutCharName); 1328 1329 if (const Function *F = 1330 dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts())) 1331 CI->setCallingConv(F->getCallingConv()); 1332 return CI; 1333 } 1334 1335 Value *llvm::emitPutS(Value *Str, IRBuilderBase &B, 1336 const TargetLibraryInfo *TLI) { 1337 if (!TLI->has(LibFunc_puts)) 1338 return nullptr; 1339 1340 Module *M = B.GetInsertBlock()->getModule(); 1341 StringRef PutsName = TLI->getName(LibFunc_puts); 1342 FunctionCallee PutS = 1343 M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy()); 1344 inferLibFuncAttributes(M, PutsName, *TLI); 1345 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName); 1346 if (const Function *F = 1347 dyn_cast<Function>(PutS.getCallee()->stripPointerCasts())) 1348 CI->setCallingConv(F->getCallingConv()); 1349 return CI; 1350 } 1351 1352 Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilderBase &B, 1353 const TargetLibraryInfo *TLI) { 1354 if (!TLI->has(LibFunc_fputc)) 1355 return nullptr; 1356 1357 Module *M = B.GetInsertBlock()->getModule(); 1358 StringRef FPutcName = TLI->getName(LibFunc_fputc); 1359 FunctionCallee F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(), 1360 B.getInt32Ty(), File->getType()); 1361 if (File->getType()->isPointerTy()) 1362 inferLibFuncAttributes(M, FPutcName, *TLI); 1363 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, 1364 "chari"); 1365 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName); 1366 1367 if (const Function *Fn = 1368 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1369 CI->setCallingConv(Fn->getCallingConv()); 1370 return CI; 1371 } 1372 1373 Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilderBase &B, 1374 const TargetLibraryInfo *TLI) { 1375 if (!TLI->has(LibFunc_fputs)) 1376 return nullptr; 1377 1378 Module *M = B.GetInsertBlock()->getModule(); 1379 StringRef FPutsName = TLI->getName(LibFunc_fputs); 1380 FunctionCallee F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(), 1381 B.getInt8PtrTy(), File->getType()); 1382 if (File->getType()->isPointerTy()) 1383 inferLibFuncAttributes(M, FPutsName, *TLI); 1384 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName); 1385 1386 if (const Function *Fn = 1387 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1388 CI->setCallingConv(Fn->getCallingConv()); 1389 return CI; 1390 } 1391 1392 Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, 1393 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1394 if (!TLI->has(LibFunc_fwrite)) 1395 return nullptr; 1396 1397 Module *M = B.GetInsertBlock()->getModule(); 1398 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1399 StringRef FWriteName = TLI->getName(LibFunc_fwrite); 1400 FunctionCallee F = M->getOrInsertFunction( 1401 FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1402 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1403 1404 if (File->getType()->isPointerTy()) 1405 inferLibFuncAttributes(M, FWriteName, *TLI); 1406 CallInst *CI = 1407 B.CreateCall(F, {castToCStr(Ptr, B), Size, 1408 ConstantInt::get(DL.getIntPtrType(Context), 1), File}); 1409 1410 if (const Function *Fn = 1411 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1412 CI->setCallingConv(Fn->getCallingConv()); 1413 return CI; 1414 } 1415 1416 Value *llvm::emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, 1417 const TargetLibraryInfo *TLI) { 1418 if (!TLI->has(LibFunc_malloc)) 1419 return nullptr; 1420 1421 Module *M = B.GetInsertBlock()->getModule(); 1422 StringRef MallocName = TLI->getName(LibFunc_malloc); 1423 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1424 FunctionCallee Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(), 1425 DL.getIntPtrType(Context)); 1426 inferLibFuncAttributes(M, MallocName, *TLI); 1427 CallInst *CI = B.CreateCall(Malloc, Num, MallocName); 1428 1429 if (const Function *F = 1430 dyn_cast<Function>(Malloc.getCallee()->stripPointerCasts())) 1431 CI->setCallingConv(F->getCallingConv()); 1432 1433 return CI; 1434 } 1435 1436 Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs, 1437 IRBuilderBase &B, const TargetLibraryInfo &TLI) { 1438 if (!TLI.has(LibFunc_calloc)) 1439 return nullptr; 1440 1441 Module *M = B.GetInsertBlock()->getModule(); 1442 StringRef CallocName = TLI.getName(LibFunc_calloc); 1443 const DataLayout &DL = M->getDataLayout(); 1444 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext())); 1445 FunctionCallee Calloc = M->getOrInsertFunction( 1446 CallocName, Attrs, B.getInt8PtrTy(), PtrType, PtrType); 1447 inferLibFuncAttributes(M, CallocName, TLI); 1448 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName); 1449 1450 if (const auto *F = 1451 dyn_cast<Function>(Calloc.getCallee()->stripPointerCasts())) 1452 CI->setCallingConv(F->getCallingConv()); 1453 1454 return CI; 1455 } 1456