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/ADT/Statistic.h" 17 #include "llvm/Analysis/TargetLibraryInfo.h" 18 #include "llvm/IR/Constants.h" 19 #include "llvm/IR/DataLayout.h" 20 #include "llvm/IR/Function.h" 21 #include "llvm/IR/IRBuilder.h" 22 #include "llvm/IR/Intrinsics.h" 23 #include "llvm/IR/LLVMContext.h" 24 #include "llvm/IR/Module.h" 25 #include "llvm/IR/Type.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(NumReadOnlyArg, "Number of arguments inferred as readonly"); 39 STATISTIC(NumNoAlias, "Number of function returns inferred as noalias"); 40 STATISTIC(NumNonNull, "Number of function returns inferred as nonnull returns"); 41 STATISTIC(NumReturnedArg, "Number of arguments inferred as returned"); 42 43 static bool setDoesNotAccessMemory(Function &F) { 44 if (F.doesNotAccessMemory()) 45 return false; 46 F.setDoesNotAccessMemory(); 47 ++NumReadNone; 48 return true; 49 } 50 51 static bool setOnlyReadsMemory(Function &F) { 52 if (F.onlyReadsMemory()) 53 return false; 54 F.setOnlyReadsMemory(); 55 ++NumReadOnly; 56 return true; 57 } 58 59 static bool setOnlyAccessesArgMemory(Function &F) { 60 if (F.onlyAccessesArgMemory()) 61 return false; 62 F.setOnlyAccessesArgMemory(); 63 ++NumArgMemOnly; 64 return true; 65 } 66 67 static bool setDoesNotThrow(Function &F) { 68 if (F.doesNotThrow()) 69 return false; 70 F.setDoesNotThrow(); 71 ++NumNoUnwind; 72 return true; 73 } 74 75 static bool setRetDoesNotAlias(Function &F) { 76 if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias)) 77 return false; 78 F.addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias); 79 ++NumNoAlias; 80 return true; 81 } 82 83 static bool setDoesNotCapture(Function &F, unsigned ArgNo) { 84 if (F.hasParamAttribute(ArgNo, Attribute::NoCapture)) 85 return false; 86 F.addParamAttr(ArgNo, Attribute::NoCapture); 87 ++NumNoCapture; 88 return true; 89 } 90 91 static bool setOnlyReadsMemory(Function &F, unsigned ArgNo) { 92 if (F.hasParamAttribute(ArgNo, Attribute::ReadOnly)) 93 return false; 94 F.addParamAttr(ArgNo, Attribute::ReadOnly); 95 ++NumReadOnlyArg; 96 return true; 97 } 98 99 static bool setRetNonNull(Function &F) { 100 assert(F.getReturnType()->isPointerTy() && 101 "nonnull applies only to pointers"); 102 if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NonNull)) 103 return false; 104 F.addAttribute(AttributeList::ReturnIndex, Attribute::NonNull); 105 ++NumNonNull; 106 return true; 107 } 108 109 static bool setReturnedArg(Function &F, unsigned ArgNo) { 110 if (F.hasParamAttribute(ArgNo, Attribute::Returned)) 111 return false; 112 F.addParamAttr(ArgNo, Attribute::Returned); 113 ++NumReturnedArg; 114 return true; 115 } 116 117 static bool setNonLazyBind(Function &F) { 118 if (F.hasFnAttribute(Attribute::NonLazyBind)) 119 return false; 120 F.addFnAttr(Attribute::NonLazyBind); 121 return true; 122 } 123 124 bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) { 125 LibFunc TheLibFunc; 126 if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc))) 127 return false; 128 129 bool Changed = false; 130 131 if (F.getParent() != nullptr && F.getParent()->getRtLibUseGOT()) 132 Changed |= setNonLazyBind(F); 133 134 switch (TheLibFunc) { 135 case LibFunc_strlen: 136 case LibFunc_wcslen: 137 Changed |= setOnlyReadsMemory(F); 138 Changed |= setDoesNotThrow(F); 139 Changed |= setOnlyAccessesArgMemory(F); 140 Changed |= setDoesNotCapture(F, 0); 141 return Changed; 142 case LibFunc_strchr: 143 case LibFunc_strrchr: 144 Changed |= setOnlyReadsMemory(F); 145 Changed |= setDoesNotThrow(F); 146 return Changed; 147 case LibFunc_strtol: 148 case LibFunc_strtod: 149 case LibFunc_strtof: 150 case LibFunc_strtoul: 151 case LibFunc_strtoll: 152 case LibFunc_strtold: 153 case LibFunc_strtoull: 154 Changed |= setDoesNotThrow(F); 155 Changed |= setDoesNotCapture(F, 1); 156 Changed |= setOnlyReadsMemory(F, 0); 157 return Changed; 158 case LibFunc_strcpy: 159 case LibFunc_strncpy: 160 case LibFunc_strcat: 161 case LibFunc_strncat: 162 Changed |= setReturnedArg(F, 0); 163 LLVM_FALLTHROUGH; 164 case LibFunc_stpcpy: 165 case LibFunc_stpncpy: 166 Changed |= setDoesNotThrow(F); 167 Changed |= setDoesNotCapture(F, 1); 168 Changed |= setOnlyReadsMemory(F, 1); 169 return Changed; 170 case LibFunc_strxfrm: 171 Changed |= setDoesNotThrow(F); 172 Changed |= setDoesNotCapture(F, 0); 173 Changed |= setDoesNotCapture(F, 1); 174 Changed |= setOnlyReadsMemory(F, 1); 175 return Changed; 176 case LibFunc_strcmp: // 0,1 177 case LibFunc_strspn: // 0,1 178 case LibFunc_strncmp: // 0,1 179 case LibFunc_strcspn: // 0,1 180 case LibFunc_strcoll: // 0,1 181 case LibFunc_strcasecmp: // 0,1 182 case LibFunc_strncasecmp: // 183 Changed |= setOnlyReadsMemory(F); 184 Changed |= setDoesNotThrow(F); 185 Changed |= setDoesNotCapture(F, 0); 186 Changed |= setDoesNotCapture(F, 1); 187 return Changed; 188 case LibFunc_strstr: 189 case LibFunc_strpbrk: 190 Changed |= setOnlyReadsMemory(F); 191 Changed |= setDoesNotThrow(F); 192 Changed |= setDoesNotCapture(F, 1); 193 return Changed; 194 case LibFunc_strtok: 195 case LibFunc_strtok_r: 196 Changed |= setDoesNotThrow(F); 197 Changed |= setDoesNotCapture(F, 1); 198 Changed |= setOnlyReadsMemory(F, 1); 199 return Changed; 200 case LibFunc_scanf: 201 Changed |= setDoesNotThrow(F); 202 Changed |= setDoesNotCapture(F, 0); 203 Changed |= setOnlyReadsMemory(F, 0); 204 return Changed; 205 case LibFunc_setbuf: 206 case LibFunc_setvbuf: 207 Changed |= setDoesNotThrow(F); 208 Changed |= setDoesNotCapture(F, 0); 209 return Changed; 210 case LibFunc_strdup: 211 case LibFunc_strndup: 212 Changed |= setDoesNotThrow(F); 213 Changed |= setRetDoesNotAlias(F); 214 Changed |= setDoesNotCapture(F, 0); 215 Changed |= setOnlyReadsMemory(F, 0); 216 return Changed; 217 case LibFunc_stat: 218 case LibFunc_statvfs: 219 Changed |= setDoesNotThrow(F); 220 Changed |= setDoesNotCapture(F, 0); 221 Changed |= setDoesNotCapture(F, 1); 222 Changed |= setOnlyReadsMemory(F, 0); 223 return Changed; 224 case LibFunc_sscanf: 225 Changed |= setDoesNotThrow(F); 226 Changed |= setDoesNotCapture(F, 0); 227 Changed |= setDoesNotCapture(F, 1); 228 Changed |= setOnlyReadsMemory(F, 0); 229 Changed |= setOnlyReadsMemory(F, 1); 230 return Changed; 231 case LibFunc_sprintf: 232 Changed |= setDoesNotThrow(F); 233 Changed |= setDoesNotCapture(F, 0); 234 Changed |= setDoesNotCapture(F, 1); 235 Changed |= setOnlyReadsMemory(F, 1); 236 return Changed; 237 case LibFunc_snprintf: 238 Changed |= setDoesNotThrow(F); 239 Changed |= setDoesNotCapture(F, 0); 240 Changed |= setDoesNotCapture(F, 2); 241 Changed |= setOnlyReadsMemory(F, 2); 242 return Changed; 243 case LibFunc_setitimer: 244 Changed |= setDoesNotThrow(F); 245 Changed |= setDoesNotCapture(F, 1); 246 Changed |= setDoesNotCapture(F, 2); 247 Changed |= setOnlyReadsMemory(F, 1); 248 return Changed; 249 case LibFunc_system: 250 // May throw; "system" is a valid pthread cancellation point. 251 Changed |= setDoesNotCapture(F, 0); 252 Changed |= setOnlyReadsMemory(F, 0); 253 return Changed; 254 case LibFunc_malloc: 255 Changed |= setDoesNotThrow(F); 256 Changed |= setRetDoesNotAlias(F); 257 return Changed; 258 case LibFunc_memcmp: 259 Changed |= setOnlyReadsMemory(F); 260 Changed |= setDoesNotThrow(F); 261 Changed |= setDoesNotCapture(F, 0); 262 Changed |= setDoesNotCapture(F, 1); 263 return Changed; 264 case LibFunc_memchr: 265 case LibFunc_memrchr: 266 Changed |= setOnlyReadsMemory(F); 267 Changed |= setDoesNotThrow(F); 268 return Changed; 269 case LibFunc_modf: 270 case LibFunc_modff: 271 case LibFunc_modfl: 272 Changed |= setDoesNotThrow(F); 273 Changed |= setDoesNotCapture(F, 1); 274 return Changed; 275 case LibFunc_memcpy: 276 case LibFunc_memmove: 277 Changed |= setReturnedArg(F, 0); 278 LLVM_FALLTHROUGH; 279 case LibFunc_mempcpy: 280 case LibFunc_memccpy: 281 Changed |= setDoesNotThrow(F); 282 Changed |= setDoesNotCapture(F, 1); 283 Changed |= setOnlyReadsMemory(F, 1); 284 return Changed; 285 case LibFunc_memcpy_chk: 286 Changed |= setDoesNotThrow(F); 287 return Changed; 288 case LibFunc_memalign: 289 Changed |= setRetDoesNotAlias(F); 290 return Changed; 291 case LibFunc_mkdir: 292 Changed |= setDoesNotThrow(F); 293 Changed |= setDoesNotCapture(F, 0); 294 Changed |= setOnlyReadsMemory(F, 0); 295 return Changed; 296 case LibFunc_mktime: 297 Changed |= setDoesNotThrow(F); 298 Changed |= setDoesNotCapture(F, 0); 299 return Changed; 300 case LibFunc_realloc: 301 Changed |= setDoesNotThrow(F); 302 Changed |= setRetDoesNotAlias(F); 303 Changed |= setDoesNotCapture(F, 0); 304 return Changed; 305 case LibFunc_read: 306 // May throw; "read" is a valid pthread cancellation point. 307 Changed |= setDoesNotCapture(F, 1); 308 return Changed; 309 case LibFunc_rewind: 310 Changed |= setDoesNotThrow(F); 311 Changed |= setDoesNotCapture(F, 0); 312 return Changed; 313 case LibFunc_rmdir: 314 case LibFunc_remove: 315 case LibFunc_realpath: 316 Changed |= setDoesNotThrow(F); 317 Changed |= setDoesNotCapture(F, 0); 318 Changed |= setOnlyReadsMemory(F, 0); 319 return Changed; 320 case LibFunc_rename: 321 Changed |= setDoesNotThrow(F); 322 Changed |= setDoesNotCapture(F, 0); 323 Changed |= setDoesNotCapture(F, 1); 324 Changed |= setOnlyReadsMemory(F, 0); 325 Changed |= setOnlyReadsMemory(F, 1); 326 return Changed; 327 case LibFunc_readlink: 328 Changed |= setDoesNotThrow(F); 329 Changed |= setDoesNotCapture(F, 0); 330 Changed |= setDoesNotCapture(F, 1); 331 Changed |= setOnlyReadsMemory(F, 0); 332 return Changed; 333 case LibFunc_write: 334 // May throw; "write" is a valid pthread cancellation point. 335 Changed |= setDoesNotCapture(F, 1); 336 Changed |= setOnlyReadsMemory(F, 1); 337 return Changed; 338 case LibFunc_bcopy: 339 Changed |= setDoesNotThrow(F); 340 Changed |= setDoesNotCapture(F, 0); 341 Changed |= setDoesNotCapture(F, 1); 342 Changed |= setOnlyReadsMemory(F, 0); 343 return Changed; 344 case LibFunc_bcmp: 345 Changed |= setDoesNotThrow(F); 346 Changed |= setOnlyReadsMemory(F); 347 Changed |= setDoesNotCapture(F, 0); 348 Changed |= setDoesNotCapture(F, 1); 349 return Changed; 350 case LibFunc_bzero: 351 Changed |= setDoesNotThrow(F); 352 Changed |= setDoesNotCapture(F, 0); 353 return Changed; 354 case LibFunc_calloc: 355 Changed |= setDoesNotThrow(F); 356 Changed |= setRetDoesNotAlias(F); 357 return Changed; 358 case LibFunc_chmod: 359 case LibFunc_chown: 360 Changed |= setDoesNotThrow(F); 361 Changed |= setDoesNotCapture(F, 0); 362 Changed |= setOnlyReadsMemory(F, 0); 363 return Changed; 364 case LibFunc_ctermid: 365 case LibFunc_clearerr: 366 case LibFunc_closedir: 367 Changed |= setDoesNotThrow(F); 368 Changed |= setDoesNotCapture(F, 0); 369 return Changed; 370 case LibFunc_atoi: 371 case LibFunc_atol: 372 case LibFunc_atof: 373 case LibFunc_atoll: 374 Changed |= setDoesNotThrow(F); 375 Changed |= setOnlyReadsMemory(F); 376 Changed |= setDoesNotCapture(F, 0); 377 return Changed; 378 case LibFunc_access: 379 Changed |= setDoesNotThrow(F); 380 Changed |= setDoesNotCapture(F, 0); 381 Changed |= setOnlyReadsMemory(F, 0); 382 return Changed; 383 case LibFunc_fopen: 384 Changed |= setDoesNotThrow(F); 385 Changed |= setRetDoesNotAlias(F); 386 Changed |= setDoesNotCapture(F, 0); 387 Changed |= setDoesNotCapture(F, 1); 388 Changed |= setOnlyReadsMemory(F, 0); 389 Changed |= setOnlyReadsMemory(F, 1); 390 return Changed; 391 case LibFunc_fdopen: 392 Changed |= setDoesNotThrow(F); 393 Changed |= setRetDoesNotAlias(F); 394 Changed |= setDoesNotCapture(F, 1); 395 Changed |= setOnlyReadsMemory(F, 1); 396 return Changed; 397 case LibFunc_feof: 398 case LibFunc_free: 399 case LibFunc_fseek: 400 case LibFunc_ftell: 401 case LibFunc_fgetc: 402 case LibFunc_fgetc_unlocked: 403 case LibFunc_fseeko: 404 case LibFunc_ftello: 405 case LibFunc_fileno: 406 case LibFunc_fflush: 407 case LibFunc_fclose: 408 case LibFunc_fsetpos: 409 case LibFunc_flockfile: 410 case LibFunc_funlockfile: 411 case LibFunc_ftrylockfile: 412 Changed |= setDoesNotThrow(F); 413 Changed |= setDoesNotCapture(F, 0); 414 return Changed; 415 case LibFunc_ferror: 416 Changed |= setDoesNotThrow(F); 417 Changed |= setDoesNotCapture(F, 0); 418 Changed |= setOnlyReadsMemory(F); 419 return Changed; 420 case LibFunc_fputc: 421 case LibFunc_fputc_unlocked: 422 case LibFunc_fstat: 423 case LibFunc_frexp: 424 case LibFunc_frexpf: 425 case LibFunc_frexpl: 426 case LibFunc_fstatvfs: 427 Changed |= setDoesNotThrow(F); 428 Changed |= setDoesNotCapture(F, 1); 429 return Changed; 430 case LibFunc_fgets: 431 case LibFunc_fgets_unlocked: 432 Changed |= setDoesNotThrow(F); 433 Changed |= setDoesNotCapture(F, 2); 434 return Changed; 435 case LibFunc_fread: 436 case LibFunc_fread_unlocked: 437 Changed |= setDoesNotThrow(F); 438 Changed |= setDoesNotCapture(F, 0); 439 Changed |= setDoesNotCapture(F, 3); 440 return Changed; 441 case LibFunc_fwrite: 442 case LibFunc_fwrite_unlocked: 443 Changed |= setDoesNotThrow(F); 444 Changed |= setDoesNotCapture(F, 0); 445 Changed |= setDoesNotCapture(F, 3); 446 // FIXME: readonly #1? 447 return Changed; 448 case LibFunc_fputs: 449 case LibFunc_fputs_unlocked: 450 Changed |= setDoesNotThrow(F); 451 Changed |= setDoesNotCapture(F, 0); 452 Changed |= setDoesNotCapture(F, 1); 453 Changed |= setOnlyReadsMemory(F, 0); 454 return Changed; 455 case LibFunc_fscanf: 456 case LibFunc_fprintf: 457 Changed |= setDoesNotThrow(F); 458 Changed |= setDoesNotCapture(F, 0); 459 Changed |= setDoesNotCapture(F, 1); 460 Changed |= setOnlyReadsMemory(F, 1); 461 return Changed; 462 case LibFunc_fgetpos: 463 Changed |= setDoesNotThrow(F); 464 Changed |= setDoesNotCapture(F, 0); 465 Changed |= setDoesNotCapture(F, 1); 466 return Changed; 467 case LibFunc_getc: 468 case LibFunc_getlogin_r: 469 case LibFunc_getc_unlocked: 470 Changed |= setDoesNotThrow(F); 471 Changed |= setDoesNotCapture(F, 0); 472 return Changed; 473 case LibFunc_getenv: 474 Changed |= setDoesNotThrow(F); 475 Changed |= setOnlyReadsMemory(F); 476 Changed |= setDoesNotCapture(F, 0); 477 return Changed; 478 case LibFunc_gets: 479 case LibFunc_getchar: 480 case LibFunc_getchar_unlocked: 481 Changed |= setDoesNotThrow(F); 482 return Changed; 483 case LibFunc_getitimer: 484 Changed |= setDoesNotThrow(F); 485 Changed |= setDoesNotCapture(F, 1); 486 return Changed; 487 case LibFunc_getpwnam: 488 Changed |= setDoesNotThrow(F); 489 Changed |= setDoesNotCapture(F, 0); 490 Changed |= setOnlyReadsMemory(F, 0); 491 return Changed; 492 case LibFunc_ungetc: 493 Changed |= setDoesNotThrow(F); 494 Changed |= setDoesNotCapture(F, 1); 495 return Changed; 496 case LibFunc_uname: 497 Changed |= setDoesNotThrow(F); 498 Changed |= setDoesNotCapture(F, 0); 499 return Changed; 500 case LibFunc_unlink: 501 Changed |= setDoesNotThrow(F); 502 Changed |= setDoesNotCapture(F, 0); 503 Changed |= setOnlyReadsMemory(F, 0); 504 return Changed; 505 case LibFunc_unsetenv: 506 Changed |= setDoesNotThrow(F); 507 Changed |= setDoesNotCapture(F, 0); 508 Changed |= setOnlyReadsMemory(F, 0); 509 return Changed; 510 case LibFunc_utime: 511 case LibFunc_utimes: 512 Changed |= setDoesNotThrow(F); 513 Changed |= setDoesNotCapture(F, 0); 514 Changed |= setDoesNotCapture(F, 1); 515 Changed |= setOnlyReadsMemory(F, 0); 516 Changed |= setOnlyReadsMemory(F, 1); 517 return Changed; 518 case LibFunc_putc: 519 case LibFunc_putc_unlocked: 520 Changed |= setDoesNotThrow(F); 521 Changed |= setDoesNotCapture(F, 1); 522 return Changed; 523 case LibFunc_puts: 524 case LibFunc_printf: 525 case LibFunc_perror: 526 Changed |= setDoesNotThrow(F); 527 Changed |= setDoesNotCapture(F, 0); 528 Changed |= setOnlyReadsMemory(F, 0); 529 return Changed; 530 case LibFunc_pread: 531 // May throw; "pread" is a valid pthread cancellation point. 532 Changed |= setDoesNotCapture(F, 1); 533 return Changed; 534 case LibFunc_pwrite: 535 // May throw; "pwrite" is a valid pthread cancellation point. 536 Changed |= setDoesNotCapture(F, 1); 537 Changed |= setOnlyReadsMemory(F, 1); 538 return Changed; 539 case LibFunc_putchar: 540 case LibFunc_putchar_unlocked: 541 Changed |= setDoesNotThrow(F); 542 return Changed; 543 case LibFunc_popen: 544 Changed |= setDoesNotThrow(F); 545 Changed |= setRetDoesNotAlias(F); 546 Changed |= setDoesNotCapture(F, 0); 547 Changed |= setDoesNotCapture(F, 1); 548 Changed |= setOnlyReadsMemory(F, 0); 549 Changed |= setOnlyReadsMemory(F, 1); 550 return Changed; 551 case LibFunc_pclose: 552 Changed |= setDoesNotThrow(F); 553 Changed |= setDoesNotCapture(F, 0); 554 return Changed; 555 case LibFunc_vscanf: 556 Changed |= setDoesNotThrow(F); 557 Changed |= setDoesNotCapture(F, 0); 558 Changed |= setOnlyReadsMemory(F, 0); 559 return Changed; 560 case LibFunc_vsscanf: 561 Changed |= setDoesNotThrow(F); 562 Changed |= setDoesNotCapture(F, 0); 563 Changed |= setDoesNotCapture(F, 1); 564 Changed |= setOnlyReadsMemory(F, 0); 565 Changed |= setOnlyReadsMemory(F, 1); 566 return Changed; 567 case LibFunc_vfscanf: 568 Changed |= setDoesNotThrow(F); 569 Changed |= setDoesNotCapture(F, 0); 570 Changed |= setDoesNotCapture(F, 1); 571 Changed |= setOnlyReadsMemory(F, 1); 572 return Changed; 573 case LibFunc_valloc: 574 Changed |= setDoesNotThrow(F); 575 Changed |= setRetDoesNotAlias(F); 576 return Changed; 577 case LibFunc_vprintf: 578 Changed |= setDoesNotThrow(F); 579 Changed |= setDoesNotCapture(F, 0); 580 Changed |= setOnlyReadsMemory(F, 0); 581 return Changed; 582 case LibFunc_vfprintf: 583 case LibFunc_vsprintf: 584 Changed |= setDoesNotThrow(F); 585 Changed |= setDoesNotCapture(F, 0); 586 Changed |= setDoesNotCapture(F, 1); 587 Changed |= setOnlyReadsMemory(F, 1); 588 return Changed; 589 case LibFunc_vsnprintf: 590 Changed |= setDoesNotThrow(F); 591 Changed |= setDoesNotCapture(F, 0); 592 Changed |= setDoesNotCapture(F, 2); 593 Changed |= setOnlyReadsMemory(F, 2); 594 return Changed; 595 case LibFunc_open: 596 // May throw; "open" is a valid pthread cancellation point. 597 Changed |= setDoesNotCapture(F, 0); 598 Changed |= setOnlyReadsMemory(F, 0); 599 return Changed; 600 case LibFunc_opendir: 601 Changed |= setDoesNotThrow(F); 602 Changed |= setRetDoesNotAlias(F); 603 Changed |= setDoesNotCapture(F, 0); 604 Changed |= setOnlyReadsMemory(F, 0); 605 return Changed; 606 case LibFunc_tmpfile: 607 Changed |= setDoesNotThrow(F); 608 Changed |= setRetDoesNotAlias(F); 609 return Changed; 610 case LibFunc_times: 611 Changed |= setDoesNotThrow(F); 612 Changed |= setDoesNotCapture(F, 0); 613 return Changed; 614 case LibFunc_htonl: 615 case LibFunc_htons: 616 case LibFunc_ntohl: 617 case LibFunc_ntohs: 618 Changed |= setDoesNotThrow(F); 619 Changed |= setDoesNotAccessMemory(F); 620 return Changed; 621 case LibFunc_lstat: 622 Changed |= setDoesNotThrow(F); 623 Changed |= setDoesNotCapture(F, 0); 624 Changed |= setDoesNotCapture(F, 1); 625 Changed |= setOnlyReadsMemory(F, 0); 626 return Changed; 627 case LibFunc_lchown: 628 Changed |= setDoesNotThrow(F); 629 Changed |= setDoesNotCapture(F, 0); 630 Changed |= setOnlyReadsMemory(F, 0); 631 return Changed; 632 case LibFunc_qsort: 633 // May throw; places call through function pointer. 634 Changed |= setDoesNotCapture(F, 3); 635 return Changed; 636 case LibFunc_dunder_strdup: 637 case LibFunc_dunder_strndup: 638 Changed |= setDoesNotThrow(F); 639 Changed |= setRetDoesNotAlias(F); 640 Changed |= setDoesNotCapture(F, 0); 641 Changed |= setOnlyReadsMemory(F, 0); 642 return Changed; 643 case LibFunc_dunder_strtok_r: 644 Changed |= setDoesNotThrow(F); 645 Changed |= setDoesNotCapture(F, 1); 646 Changed |= setOnlyReadsMemory(F, 1); 647 return Changed; 648 case LibFunc_under_IO_getc: 649 Changed |= setDoesNotThrow(F); 650 Changed |= setDoesNotCapture(F, 0); 651 return Changed; 652 case LibFunc_under_IO_putc: 653 Changed |= setDoesNotThrow(F); 654 Changed |= setDoesNotCapture(F, 1); 655 return Changed; 656 case LibFunc_dunder_isoc99_scanf: 657 Changed |= setDoesNotThrow(F); 658 Changed |= setDoesNotCapture(F, 0); 659 Changed |= setOnlyReadsMemory(F, 0); 660 return Changed; 661 case LibFunc_stat64: 662 case LibFunc_lstat64: 663 case LibFunc_statvfs64: 664 Changed |= setDoesNotThrow(F); 665 Changed |= setDoesNotCapture(F, 0); 666 Changed |= setDoesNotCapture(F, 1); 667 Changed |= setOnlyReadsMemory(F, 0); 668 return Changed; 669 case LibFunc_dunder_isoc99_sscanf: 670 Changed |= setDoesNotThrow(F); 671 Changed |= setDoesNotCapture(F, 0); 672 Changed |= setDoesNotCapture(F, 1); 673 Changed |= setOnlyReadsMemory(F, 0); 674 Changed |= setOnlyReadsMemory(F, 1); 675 return Changed; 676 case LibFunc_fopen64: 677 Changed |= setDoesNotThrow(F); 678 Changed |= setRetDoesNotAlias(F); 679 Changed |= setDoesNotCapture(F, 0); 680 Changed |= setDoesNotCapture(F, 1); 681 Changed |= setOnlyReadsMemory(F, 0); 682 Changed |= setOnlyReadsMemory(F, 1); 683 return Changed; 684 case LibFunc_fseeko64: 685 case LibFunc_ftello64: 686 Changed |= setDoesNotThrow(F); 687 Changed |= setDoesNotCapture(F, 0); 688 return Changed; 689 case LibFunc_tmpfile64: 690 Changed |= setDoesNotThrow(F); 691 Changed |= setRetDoesNotAlias(F); 692 return Changed; 693 case LibFunc_fstat64: 694 case LibFunc_fstatvfs64: 695 Changed |= setDoesNotThrow(F); 696 Changed |= setDoesNotCapture(F, 1); 697 return Changed; 698 case LibFunc_open64: 699 // May throw; "open" is a valid pthread cancellation point. 700 Changed |= setDoesNotCapture(F, 0); 701 Changed |= setOnlyReadsMemory(F, 0); 702 return Changed; 703 case LibFunc_gettimeofday: 704 // Currently some platforms have the restrict keyword on the arguments to 705 // gettimeofday. To be conservative, do not add noalias to gettimeofday's 706 // arguments. 707 Changed |= setDoesNotThrow(F); 708 Changed |= setDoesNotCapture(F, 0); 709 Changed |= setDoesNotCapture(F, 1); 710 return Changed; 711 case LibFunc_Znwj: // new(unsigned int) 712 case LibFunc_Znwm: // new(unsigned long) 713 case LibFunc_Znaj: // new[](unsigned int) 714 case LibFunc_Znam: // new[](unsigned long) 715 case LibFunc_msvc_new_int: // new(unsigned int) 716 case LibFunc_msvc_new_longlong: // new(unsigned long long) 717 case LibFunc_msvc_new_array_int: // new[](unsigned int) 718 case LibFunc_msvc_new_array_longlong: // new[](unsigned long long) 719 // Operator new always returns a nonnull noalias pointer 720 Changed |= setRetNonNull(F); 721 Changed |= setRetDoesNotAlias(F); 722 return Changed; 723 // TODO: add LibFunc entries for: 724 // case LibFunc_memset_pattern4: 725 // case LibFunc_memset_pattern8: 726 case LibFunc_memset_pattern16: 727 Changed |= setOnlyAccessesArgMemory(F); 728 Changed |= setDoesNotCapture(F, 0); 729 Changed |= setDoesNotCapture(F, 1); 730 Changed |= setOnlyReadsMemory(F, 1); 731 return Changed; 732 // int __nvvm_reflect(const char *) 733 case LibFunc_nvvm_reflect: 734 Changed |= setDoesNotAccessMemory(F); 735 Changed |= setDoesNotThrow(F); 736 return Changed; 737 738 default: 739 // FIXME: It'd be really nice to cover all the library functions we're 740 // aware of here. 741 return false; 742 } 743 } 744 745 bool llvm::hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty, 746 LibFunc DoubleFn, LibFunc FloatFn, 747 LibFunc LongDoubleFn) { 748 switch (Ty->getTypeID()) { 749 case Type::FloatTyID: 750 return TLI->has(FloatFn); 751 case Type::DoubleTyID: 752 return TLI->has(DoubleFn); 753 default: 754 return TLI->has(LongDoubleFn); 755 } 756 } 757 758 //- Emit LibCalls ------------------------------------------------------------// 759 760 Value *llvm::castToCStr(Value *V, IRBuilder<> &B) { 761 unsigned AS = V->getType()->getPointerAddressSpace(); 762 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr"); 763 } 764 765 Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL, 766 const TargetLibraryInfo *TLI) { 767 if (!TLI->has(LibFunc_strlen)) 768 return nullptr; 769 770 Module *M = B.GetInsertBlock()->getModule(); 771 LLVMContext &Context = B.GetInsertBlock()->getContext(); 772 Constant *StrLen = M->getOrInsertFunction("strlen", DL.getIntPtrType(Context), 773 B.getInt8PtrTy()); 774 inferLibFuncAttributes(*M->getFunction("strlen"), *TLI); 775 CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), "strlen"); 776 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts())) 777 CI->setCallingConv(F->getCallingConv()); 778 779 return CI; 780 } 781 782 Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B, 783 const TargetLibraryInfo *TLI) { 784 if (!TLI->has(LibFunc_strchr)) 785 return nullptr; 786 787 Module *M = B.GetInsertBlock()->getModule(); 788 Type *I8Ptr = B.getInt8PtrTy(); 789 Type *I32Ty = B.getInt32Ty(); 790 Constant *StrChr = 791 M->getOrInsertFunction("strchr", I8Ptr, I8Ptr, I32Ty); 792 inferLibFuncAttributes(*M->getFunction("strchr"), *TLI); 793 CallInst *CI = B.CreateCall( 794 StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr"); 795 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts())) 796 CI->setCallingConv(F->getCallingConv()); 797 return CI; 798 } 799 800 Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, 801 const DataLayout &DL, const TargetLibraryInfo *TLI) { 802 if (!TLI->has(LibFunc_strncmp)) 803 return nullptr; 804 805 Module *M = B.GetInsertBlock()->getModule(); 806 LLVMContext &Context = B.GetInsertBlock()->getContext(); 807 Value *StrNCmp = M->getOrInsertFunction("strncmp", B.getInt32Ty(), 808 B.getInt8PtrTy(), B.getInt8PtrTy(), 809 DL.getIntPtrType(Context)); 810 inferLibFuncAttributes(*M->getFunction("strncmp"), *TLI); 811 CallInst *CI = B.CreateCall( 812 StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "strncmp"); 813 814 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts())) 815 CI->setCallingConv(F->getCallingConv()); 816 817 return CI; 818 } 819 820 Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, 821 const TargetLibraryInfo *TLI, StringRef Name) { 822 if (!TLI->has(LibFunc_strcpy)) 823 return nullptr; 824 825 Module *M = B.GetInsertBlock()->getModule(); 826 Type *I8Ptr = B.getInt8PtrTy(); 827 Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr); 828 inferLibFuncAttributes(*M->getFunction(Name), *TLI); 829 CallInst *CI = 830 B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name); 831 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts())) 832 CI->setCallingConv(F->getCallingConv()); 833 return CI; 834 } 835 836 Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, 837 const TargetLibraryInfo *TLI, StringRef Name) { 838 if (!TLI->has(LibFunc_strncpy)) 839 return nullptr; 840 841 Module *M = B.GetInsertBlock()->getModule(); 842 Type *I8Ptr = B.getInt8PtrTy(); 843 Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, 844 Len->getType()); 845 inferLibFuncAttributes(*M->getFunction(Name), *TLI); 846 CallInst *CI = B.CreateCall( 847 StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, "strncpy"); 848 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts())) 849 CI->setCallingConv(F->getCallingConv()); 850 return CI; 851 } 852 853 Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, 854 IRBuilder<> &B, const DataLayout &DL, 855 const TargetLibraryInfo *TLI) { 856 if (!TLI->has(LibFunc_memcpy_chk)) 857 return nullptr; 858 859 Module *M = B.GetInsertBlock()->getModule(); 860 AttributeList AS; 861 AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex, 862 Attribute::NoUnwind); 863 LLVMContext &Context = B.GetInsertBlock()->getContext(); 864 Value *MemCpy = M->getOrInsertFunction( 865 "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(), 866 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), 867 DL.getIntPtrType(Context)); 868 Dst = castToCStr(Dst, B); 869 Src = castToCStr(Src, B); 870 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize}); 871 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts())) 872 CI->setCallingConv(F->getCallingConv()); 873 return CI; 874 } 875 876 Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B, 877 const DataLayout &DL, const TargetLibraryInfo *TLI) { 878 if (!TLI->has(LibFunc_memchr)) 879 return nullptr; 880 881 Module *M = B.GetInsertBlock()->getModule(); 882 LLVMContext &Context = B.GetInsertBlock()->getContext(); 883 Value *MemChr = M->getOrInsertFunction("memchr", B.getInt8PtrTy(), 884 B.getInt8PtrTy(), B.getInt32Ty(), 885 DL.getIntPtrType(Context)); 886 inferLibFuncAttributes(*M->getFunction("memchr"), *TLI); 887 CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, "memchr"); 888 889 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts())) 890 CI->setCallingConv(F->getCallingConv()); 891 892 return CI; 893 } 894 895 Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, 896 const DataLayout &DL, const TargetLibraryInfo *TLI) { 897 if (!TLI->has(LibFunc_memcmp)) 898 return nullptr; 899 900 Module *M = B.GetInsertBlock()->getModule(); 901 LLVMContext &Context = B.GetInsertBlock()->getContext(); 902 Value *MemCmp = M->getOrInsertFunction("memcmp", B.getInt32Ty(), 903 B.getInt8PtrTy(), B.getInt8PtrTy(), 904 DL.getIntPtrType(Context)); 905 inferLibFuncAttributes(*M->getFunction("memcmp"), *TLI); 906 CallInst *CI = B.CreateCall( 907 MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "memcmp"); 908 909 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts())) 910 CI->setCallingConv(F->getCallingConv()); 911 912 return CI; 913 } 914 915 /// Append a suffix to the function name according to the type of 'Op'. 916 static void appendTypeSuffix(Value *Op, StringRef &Name, 917 SmallString<20> &NameBuffer) { 918 if (!Op->getType()->isDoubleTy()) { 919 NameBuffer += Name; 920 921 if (Op->getType()->isFloatTy()) 922 NameBuffer += 'f'; 923 else 924 NameBuffer += 'l'; 925 926 Name = NameBuffer; 927 } 928 } 929 930 Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, 931 const AttributeList &Attrs) { 932 SmallString<20> NameBuffer; 933 appendTypeSuffix(Op, Name, NameBuffer); 934 935 Module *M = B.GetInsertBlock()->getModule(); 936 Value *Callee = M->getOrInsertFunction(Name, Op->getType(), 937 Op->getType()); 938 CallInst *CI = B.CreateCall(Callee, Op, Name); 939 940 // The incoming attribute set may have come from a speculatable intrinsic, but 941 // is being replaced with a library call which is not allowed to be 942 // speculatable. 943 CI->setAttributes(Attrs.removeAttribute(B.getContext(), 944 AttributeList::FunctionIndex, 945 Attribute::Speculatable)); 946 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) 947 CI->setCallingConv(F->getCallingConv()); 948 949 return CI; 950 } 951 952 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, 953 IRBuilder<> &B, const AttributeList &Attrs) { 954 SmallString<20> NameBuffer; 955 appendTypeSuffix(Op1, Name, NameBuffer); 956 957 Module *M = B.GetInsertBlock()->getModule(); 958 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(), 959 Op2->getType()); 960 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name); 961 CI->setAttributes(Attrs); 962 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) 963 CI->setCallingConv(F->getCallingConv()); 964 965 return CI; 966 } 967 968 Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B, 969 const TargetLibraryInfo *TLI) { 970 if (!TLI->has(LibFunc_putchar)) 971 return nullptr; 972 973 Module *M = B.GetInsertBlock()->getModule(); 974 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(), B.getInt32Ty()); 975 inferLibFuncAttributes(*M->getFunction("putchar"), *TLI); 976 CallInst *CI = B.CreateCall(PutChar, 977 B.CreateIntCast(Char, 978 B.getInt32Ty(), 979 /*isSigned*/true, 980 "chari"), 981 "putchar"); 982 983 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts())) 984 CI->setCallingConv(F->getCallingConv()); 985 return CI; 986 } 987 988 Value *llvm::emitPutS(Value *Str, IRBuilder<> &B, 989 const TargetLibraryInfo *TLI) { 990 if (!TLI->has(LibFunc_puts)) 991 return nullptr; 992 993 Module *M = B.GetInsertBlock()->getModule(); 994 Value *PutS = 995 M->getOrInsertFunction("puts", B.getInt32Ty(), B.getInt8PtrTy()); 996 inferLibFuncAttributes(*M->getFunction("puts"), *TLI); 997 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), "puts"); 998 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts())) 999 CI->setCallingConv(F->getCallingConv()); 1000 return CI; 1001 } 1002 1003 Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B, 1004 const TargetLibraryInfo *TLI) { 1005 if (!TLI->has(LibFunc_fputc)) 1006 return nullptr; 1007 1008 Module *M = B.GetInsertBlock()->getModule(); 1009 Constant *F = M->getOrInsertFunction("fputc", B.getInt32Ty(), B.getInt32Ty(), 1010 File->getType()); 1011 if (File->getType()->isPointerTy()) 1012 inferLibFuncAttributes(*M->getFunction("fputc"), *TLI); 1013 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, 1014 "chari"); 1015 CallInst *CI = B.CreateCall(F, {Char, File}, "fputc"); 1016 1017 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1018 CI->setCallingConv(Fn->getCallingConv()); 1019 return CI; 1020 } 1021 1022 Value *llvm::emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B, 1023 const TargetLibraryInfo *TLI) { 1024 if (!TLI->has(LibFunc_fputc_unlocked)) 1025 return nullptr; 1026 1027 Module *M = B.GetInsertBlock()->getModule(); 1028 Constant *F = M->getOrInsertFunction("fputc_unlocked", B.getInt32Ty(), 1029 B.getInt32Ty(), File->getType()); 1030 if (File->getType()->isPointerTy()) 1031 inferLibFuncAttributes(*M->getFunction("fputc_unlocked"), *TLI); 1032 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari"); 1033 CallInst *CI = B.CreateCall(F, {Char, File}, "fputc_unlocked"); 1034 1035 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1036 CI->setCallingConv(Fn->getCallingConv()); 1037 return CI; 1038 } 1039 1040 Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B, 1041 const TargetLibraryInfo *TLI) { 1042 if (!TLI->has(LibFunc_fputs)) 1043 return nullptr; 1044 1045 Module *M = B.GetInsertBlock()->getModule(); 1046 StringRef FPutsName = TLI->getName(LibFunc_fputs); 1047 Constant *F = M->getOrInsertFunction( 1048 FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType()); 1049 if (File->getType()->isPointerTy()) 1050 inferLibFuncAttributes(*M->getFunction(FPutsName), *TLI); 1051 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs"); 1052 1053 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1054 CI->setCallingConv(Fn->getCallingConv()); 1055 return CI; 1056 } 1057 1058 Value *llvm::emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B, 1059 const TargetLibraryInfo *TLI) { 1060 if (!TLI->has(LibFunc_fputs_unlocked)) 1061 return nullptr; 1062 1063 Module *M = B.GetInsertBlock()->getModule(); 1064 StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked); 1065 Constant *F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(), 1066 B.getInt8PtrTy(), File->getType()); 1067 if (File->getType()->isPointerTy()) 1068 inferLibFuncAttributes(*M->getFunction(FPutsUnlockedName), *TLI); 1069 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs_unlocked"); 1070 1071 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1072 CI->setCallingConv(Fn->getCallingConv()); 1073 return CI; 1074 } 1075 1076 Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, 1077 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1078 if (!TLI->has(LibFunc_fwrite)) 1079 return nullptr; 1080 1081 Module *M = B.GetInsertBlock()->getModule(); 1082 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1083 StringRef FWriteName = TLI->getName(LibFunc_fwrite); 1084 Constant *F = M->getOrInsertFunction( 1085 FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1086 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1087 1088 if (File->getType()->isPointerTy()) 1089 inferLibFuncAttributes(*M->getFunction(FWriteName), *TLI); 1090 CallInst *CI = 1091 B.CreateCall(F, {castToCStr(Ptr, B), Size, 1092 ConstantInt::get(DL.getIntPtrType(Context), 1), File}); 1093 1094 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1095 CI->setCallingConv(Fn->getCallingConv()); 1096 return CI; 1097 } 1098 1099 Value *llvm::emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL, 1100 const TargetLibraryInfo *TLI) { 1101 if (!TLI->has(LibFunc_malloc)) 1102 return nullptr; 1103 1104 Module *M = B.GetInsertBlock()->getModule(); 1105 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1106 Value *Malloc = M->getOrInsertFunction("malloc", B.getInt8PtrTy(), 1107 DL.getIntPtrType(Context)); 1108 inferLibFuncAttributes(*M->getFunction("malloc"), *TLI); 1109 CallInst *CI = B.CreateCall(Malloc, Num, "malloc"); 1110 1111 if (const Function *F = dyn_cast<Function>(Malloc->stripPointerCasts())) 1112 CI->setCallingConv(F->getCallingConv()); 1113 1114 return CI; 1115 } 1116 1117 Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs, 1118 IRBuilder<> &B, const TargetLibraryInfo &TLI) { 1119 if (!TLI.has(LibFunc_calloc)) 1120 return nullptr; 1121 1122 Module *M = B.GetInsertBlock()->getModule(); 1123 const DataLayout &DL = M->getDataLayout(); 1124 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext())); 1125 Value *Calloc = M->getOrInsertFunction("calloc", Attrs, B.getInt8PtrTy(), 1126 PtrType, PtrType); 1127 inferLibFuncAttributes(*M->getFunction("calloc"), TLI); 1128 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, "calloc"); 1129 1130 if (const auto *F = dyn_cast<Function>(Calloc->stripPointerCasts())) 1131 CI->setCallingConv(F->getCallingConv()); 1132 1133 return CI; 1134 } 1135 1136 Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, 1137 IRBuilder<> &B, const DataLayout &DL, 1138 const TargetLibraryInfo *TLI) { 1139 if (!TLI->has(LibFunc_fwrite_unlocked)) 1140 return nullptr; 1141 1142 Module *M = B.GetInsertBlock()->getModule(); 1143 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1144 StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked); 1145 Constant *F = M->getOrInsertFunction( 1146 FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1147 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1148 1149 if (File->getType()->isPointerTy()) 1150 inferLibFuncAttributes(*M->getFunction(FWriteUnlockedName), *TLI); 1151 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File}); 1152 1153 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1154 CI->setCallingConv(Fn->getCallingConv()); 1155 return CI; 1156 } 1157 1158 Value *llvm::emitFGetCUnlocked(Value *File, IRBuilder<> &B, 1159 const TargetLibraryInfo *TLI) { 1160 if (!TLI->has(LibFunc_fgetc_unlocked)) 1161 return nullptr; 1162 1163 Module *M = B.GetInsertBlock()->getModule(); 1164 Constant *F = 1165 M->getOrInsertFunction("fgetc_unlocked", B.getInt32Ty(), File->getType()); 1166 if (File->getType()->isPointerTy()) 1167 inferLibFuncAttributes(*M->getFunction("fgetc_unlocked"), *TLI); 1168 CallInst *CI = B.CreateCall(F, File, "fgetc_unlocked"); 1169 1170 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1171 CI->setCallingConv(Fn->getCallingConv()); 1172 return CI; 1173 } 1174 1175 Value *llvm::emitFGetSUnlocked(Value *Str, Value *Size, Value *File, 1176 IRBuilder<> &B, const TargetLibraryInfo *TLI) { 1177 if (!TLI->has(LibFunc_fgets_unlocked)) 1178 return nullptr; 1179 1180 Module *M = B.GetInsertBlock()->getModule(); 1181 Constant *F = 1182 M->getOrInsertFunction("fgets_unlocked", B.getInt8PtrTy(), 1183 B.getInt8PtrTy(), B.getInt32Ty(), File->getType()); 1184 inferLibFuncAttributes(*M->getFunction("fgets_unlocked"), *TLI); 1185 CallInst *CI = 1186 B.CreateCall(F, {castToCStr(Str, B), Size, File}, "fgets_unlocked"); 1187 1188 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1189 CI->setCallingConv(Fn->getCallingConv()); 1190 return CI; 1191 } 1192 1193 Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, 1194 IRBuilder<> &B, const DataLayout &DL, 1195 const TargetLibraryInfo *TLI) { 1196 if (!TLI->has(LibFunc_fread_unlocked)) 1197 return nullptr; 1198 1199 Module *M = B.GetInsertBlock()->getModule(); 1200 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1201 StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked); 1202 Constant *F = M->getOrInsertFunction( 1203 FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1204 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1205 1206 if (File->getType()->isPointerTy()) 1207 inferLibFuncAttributes(*M->getFunction(FReadUnlockedName), *TLI); 1208 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File}); 1209 1210 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1211 CI->setCallingConv(Fn->getCallingConv()); 1212 return CI; 1213 } 1214