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::HalfTyID: 750 return false; 751 case Type::FloatTyID: 752 return TLI->has(FloatFn); 753 case Type::DoubleTyID: 754 return TLI->has(DoubleFn); 755 default: 756 return TLI->has(LongDoubleFn); 757 } 758 } 759 760 //- Emit LibCalls ------------------------------------------------------------// 761 762 Value *llvm::castToCStr(Value *V, IRBuilder<> &B) { 763 unsigned AS = V->getType()->getPointerAddressSpace(); 764 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr"); 765 } 766 767 Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL, 768 const TargetLibraryInfo *TLI) { 769 if (!TLI->has(LibFunc_strlen)) 770 return nullptr; 771 772 Module *M = B.GetInsertBlock()->getModule(); 773 LLVMContext &Context = B.GetInsertBlock()->getContext(); 774 Constant *StrLen = M->getOrInsertFunction("strlen", DL.getIntPtrType(Context), 775 B.getInt8PtrTy()); 776 inferLibFuncAttributes(*M->getFunction("strlen"), *TLI); 777 CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), "strlen"); 778 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts())) 779 CI->setCallingConv(F->getCallingConv()); 780 781 return CI; 782 } 783 784 Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B, 785 const TargetLibraryInfo *TLI) { 786 if (!TLI->has(LibFunc_strchr)) 787 return nullptr; 788 789 Module *M = B.GetInsertBlock()->getModule(); 790 Type *I8Ptr = B.getInt8PtrTy(); 791 Type *I32Ty = B.getInt32Ty(); 792 Constant *StrChr = 793 M->getOrInsertFunction("strchr", I8Ptr, I8Ptr, I32Ty); 794 inferLibFuncAttributes(*M->getFunction("strchr"), *TLI); 795 CallInst *CI = B.CreateCall( 796 StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr"); 797 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts())) 798 CI->setCallingConv(F->getCallingConv()); 799 return CI; 800 } 801 802 Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, 803 const DataLayout &DL, const TargetLibraryInfo *TLI) { 804 if (!TLI->has(LibFunc_strncmp)) 805 return nullptr; 806 807 Module *M = B.GetInsertBlock()->getModule(); 808 LLVMContext &Context = B.GetInsertBlock()->getContext(); 809 Value *StrNCmp = M->getOrInsertFunction("strncmp", B.getInt32Ty(), 810 B.getInt8PtrTy(), B.getInt8PtrTy(), 811 DL.getIntPtrType(Context)); 812 inferLibFuncAttributes(*M->getFunction("strncmp"), *TLI); 813 CallInst *CI = B.CreateCall( 814 StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "strncmp"); 815 816 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts())) 817 CI->setCallingConv(F->getCallingConv()); 818 819 return CI; 820 } 821 822 Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, 823 const TargetLibraryInfo *TLI, StringRef Name) { 824 if (!TLI->has(LibFunc_strcpy)) 825 return nullptr; 826 827 Module *M = B.GetInsertBlock()->getModule(); 828 Type *I8Ptr = B.getInt8PtrTy(); 829 Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr); 830 inferLibFuncAttributes(*M->getFunction(Name), *TLI); 831 CallInst *CI = 832 B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name); 833 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts())) 834 CI->setCallingConv(F->getCallingConv()); 835 return CI; 836 } 837 838 Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, 839 const TargetLibraryInfo *TLI, StringRef Name) { 840 if (!TLI->has(LibFunc_strncpy)) 841 return nullptr; 842 843 Module *M = B.GetInsertBlock()->getModule(); 844 Type *I8Ptr = B.getInt8PtrTy(); 845 Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, 846 Len->getType()); 847 inferLibFuncAttributes(*M->getFunction(Name), *TLI); 848 CallInst *CI = B.CreateCall( 849 StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, "strncpy"); 850 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts())) 851 CI->setCallingConv(F->getCallingConv()); 852 return CI; 853 } 854 855 Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, 856 IRBuilder<> &B, const DataLayout &DL, 857 const TargetLibraryInfo *TLI) { 858 if (!TLI->has(LibFunc_memcpy_chk)) 859 return nullptr; 860 861 Module *M = B.GetInsertBlock()->getModule(); 862 AttributeList AS; 863 AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex, 864 Attribute::NoUnwind); 865 LLVMContext &Context = B.GetInsertBlock()->getContext(); 866 Value *MemCpy = M->getOrInsertFunction( 867 "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(), 868 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), 869 DL.getIntPtrType(Context)); 870 Dst = castToCStr(Dst, B); 871 Src = castToCStr(Src, B); 872 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize}); 873 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts())) 874 CI->setCallingConv(F->getCallingConv()); 875 return CI; 876 } 877 878 Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B, 879 const DataLayout &DL, const TargetLibraryInfo *TLI) { 880 if (!TLI->has(LibFunc_memchr)) 881 return nullptr; 882 883 Module *M = B.GetInsertBlock()->getModule(); 884 LLVMContext &Context = B.GetInsertBlock()->getContext(); 885 Value *MemChr = M->getOrInsertFunction("memchr", B.getInt8PtrTy(), 886 B.getInt8PtrTy(), B.getInt32Ty(), 887 DL.getIntPtrType(Context)); 888 inferLibFuncAttributes(*M->getFunction("memchr"), *TLI); 889 CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, "memchr"); 890 891 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts())) 892 CI->setCallingConv(F->getCallingConv()); 893 894 return CI; 895 } 896 897 Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, 898 const DataLayout &DL, const TargetLibraryInfo *TLI) { 899 if (!TLI->has(LibFunc_memcmp)) 900 return nullptr; 901 902 Module *M = B.GetInsertBlock()->getModule(); 903 LLVMContext &Context = B.GetInsertBlock()->getContext(); 904 Value *MemCmp = M->getOrInsertFunction("memcmp", B.getInt32Ty(), 905 B.getInt8PtrTy(), B.getInt8PtrTy(), 906 DL.getIntPtrType(Context)); 907 inferLibFuncAttributes(*M->getFunction("memcmp"), *TLI); 908 CallInst *CI = B.CreateCall( 909 MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "memcmp"); 910 911 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts())) 912 CI->setCallingConv(F->getCallingConv()); 913 914 return CI; 915 } 916 917 /// Append a suffix to the function name according to the type of 'Op'. 918 static void appendTypeSuffix(Value *Op, StringRef &Name, 919 SmallString<20> &NameBuffer) { 920 if (!Op->getType()->isDoubleTy()) { 921 NameBuffer += Name; 922 923 if (Op->getType()->isFloatTy()) 924 NameBuffer += 'f'; 925 else 926 NameBuffer += 'l'; 927 928 Name = NameBuffer; 929 } 930 } 931 932 Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, 933 const AttributeList &Attrs) { 934 SmallString<20> NameBuffer; 935 appendTypeSuffix(Op, Name, NameBuffer); 936 937 Module *M = B.GetInsertBlock()->getModule(); 938 Value *Callee = M->getOrInsertFunction(Name, Op->getType(), 939 Op->getType()); 940 CallInst *CI = B.CreateCall(Callee, Op, Name); 941 942 // The incoming attribute set may have come from a speculatable intrinsic, but 943 // is being replaced with a library call which is not allowed to be 944 // speculatable. 945 CI->setAttributes(Attrs.removeAttribute(B.getContext(), 946 AttributeList::FunctionIndex, 947 Attribute::Speculatable)); 948 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) 949 CI->setCallingConv(F->getCallingConv()); 950 951 return CI; 952 } 953 954 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, 955 IRBuilder<> &B, const AttributeList &Attrs) { 956 SmallString<20> NameBuffer; 957 appendTypeSuffix(Op1, Name, NameBuffer); 958 959 Module *M = B.GetInsertBlock()->getModule(); 960 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(), 961 Op2->getType()); 962 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name); 963 CI->setAttributes(Attrs); 964 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) 965 CI->setCallingConv(F->getCallingConv()); 966 967 return CI; 968 } 969 970 Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B, 971 const TargetLibraryInfo *TLI) { 972 if (!TLI->has(LibFunc_putchar)) 973 return nullptr; 974 975 Module *M = B.GetInsertBlock()->getModule(); 976 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(), B.getInt32Ty()); 977 inferLibFuncAttributes(*M->getFunction("putchar"), *TLI); 978 CallInst *CI = B.CreateCall(PutChar, 979 B.CreateIntCast(Char, 980 B.getInt32Ty(), 981 /*isSigned*/true, 982 "chari"), 983 "putchar"); 984 985 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts())) 986 CI->setCallingConv(F->getCallingConv()); 987 return CI; 988 } 989 990 Value *llvm::emitPutS(Value *Str, IRBuilder<> &B, 991 const TargetLibraryInfo *TLI) { 992 if (!TLI->has(LibFunc_puts)) 993 return nullptr; 994 995 Module *M = B.GetInsertBlock()->getModule(); 996 Value *PutS = 997 M->getOrInsertFunction("puts", B.getInt32Ty(), B.getInt8PtrTy()); 998 inferLibFuncAttributes(*M->getFunction("puts"), *TLI); 999 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), "puts"); 1000 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts())) 1001 CI->setCallingConv(F->getCallingConv()); 1002 return CI; 1003 } 1004 1005 Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B, 1006 const TargetLibraryInfo *TLI) { 1007 if (!TLI->has(LibFunc_fputc)) 1008 return nullptr; 1009 1010 Module *M = B.GetInsertBlock()->getModule(); 1011 Constant *F = M->getOrInsertFunction("fputc", B.getInt32Ty(), B.getInt32Ty(), 1012 File->getType()); 1013 if (File->getType()->isPointerTy()) 1014 inferLibFuncAttributes(*M->getFunction("fputc"), *TLI); 1015 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, 1016 "chari"); 1017 CallInst *CI = B.CreateCall(F, {Char, File}, "fputc"); 1018 1019 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1020 CI->setCallingConv(Fn->getCallingConv()); 1021 return CI; 1022 } 1023 1024 Value *llvm::emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B, 1025 const TargetLibraryInfo *TLI) { 1026 if (!TLI->has(LibFunc_fputc_unlocked)) 1027 return nullptr; 1028 1029 Module *M = B.GetInsertBlock()->getModule(); 1030 Constant *F = M->getOrInsertFunction("fputc_unlocked", B.getInt32Ty(), 1031 B.getInt32Ty(), File->getType()); 1032 if (File->getType()->isPointerTy()) 1033 inferLibFuncAttributes(*M->getFunction("fputc_unlocked"), *TLI); 1034 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari"); 1035 CallInst *CI = B.CreateCall(F, {Char, File}, "fputc_unlocked"); 1036 1037 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1038 CI->setCallingConv(Fn->getCallingConv()); 1039 return CI; 1040 } 1041 1042 Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B, 1043 const TargetLibraryInfo *TLI) { 1044 if (!TLI->has(LibFunc_fputs)) 1045 return nullptr; 1046 1047 Module *M = B.GetInsertBlock()->getModule(); 1048 StringRef FPutsName = TLI->getName(LibFunc_fputs); 1049 Constant *F = M->getOrInsertFunction( 1050 FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType()); 1051 if (File->getType()->isPointerTy()) 1052 inferLibFuncAttributes(*M->getFunction(FPutsName), *TLI); 1053 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs"); 1054 1055 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1056 CI->setCallingConv(Fn->getCallingConv()); 1057 return CI; 1058 } 1059 1060 Value *llvm::emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B, 1061 const TargetLibraryInfo *TLI) { 1062 if (!TLI->has(LibFunc_fputs_unlocked)) 1063 return nullptr; 1064 1065 Module *M = B.GetInsertBlock()->getModule(); 1066 StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked); 1067 Constant *F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(), 1068 B.getInt8PtrTy(), File->getType()); 1069 if (File->getType()->isPointerTy()) 1070 inferLibFuncAttributes(*M->getFunction(FPutsUnlockedName), *TLI); 1071 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs_unlocked"); 1072 1073 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1074 CI->setCallingConv(Fn->getCallingConv()); 1075 return CI; 1076 } 1077 1078 Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, 1079 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1080 if (!TLI->has(LibFunc_fwrite)) 1081 return nullptr; 1082 1083 Module *M = B.GetInsertBlock()->getModule(); 1084 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1085 StringRef FWriteName = TLI->getName(LibFunc_fwrite); 1086 Constant *F = M->getOrInsertFunction( 1087 FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1088 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1089 1090 if (File->getType()->isPointerTy()) 1091 inferLibFuncAttributes(*M->getFunction(FWriteName), *TLI); 1092 CallInst *CI = 1093 B.CreateCall(F, {castToCStr(Ptr, B), Size, 1094 ConstantInt::get(DL.getIntPtrType(Context), 1), File}); 1095 1096 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1097 CI->setCallingConv(Fn->getCallingConv()); 1098 return CI; 1099 } 1100 1101 Value *llvm::emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL, 1102 const TargetLibraryInfo *TLI) { 1103 if (!TLI->has(LibFunc_malloc)) 1104 return nullptr; 1105 1106 Module *M = B.GetInsertBlock()->getModule(); 1107 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1108 Value *Malloc = M->getOrInsertFunction("malloc", B.getInt8PtrTy(), 1109 DL.getIntPtrType(Context)); 1110 inferLibFuncAttributes(*M->getFunction("malloc"), *TLI); 1111 CallInst *CI = B.CreateCall(Malloc, Num, "malloc"); 1112 1113 if (const Function *F = dyn_cast<Function>(Malloc->stripPointerCasts())) 1114 CI->setCallingConv(F->getCallingConv()); 1115 1116 return CI; 1117 } 1118 1119 Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs, 1120 IRBuilder<> &B, const TargetLibraryInfo &TLI) { 1121 if (!TLI.has(LibFunc_calloc)) 1122 return nullptr; 1123 1124 Module *M = B.GetInsertBlock()->getModule(); 1125 const DataLayout &DL = M->getDataLayout(); 1126 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext())); 1127 Value *Calloc = M->getOrInsertFunction("calloc", Attrs, B.getInt8PtrTy(), 1128 PtrType, PtrType); 1129 inferLibFuncAttributes(*M->getFunction("calloc"), TLI); 1130 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, "calloc"); 1131 1132 if (const auto *F = dyn_cast<Function>(Calloc->stripPointerCasts())) 1133 CI->setCallingConv(F->getCallingConv()); 1134 1135 return CI; 1136 } 1137 1138 Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, 1139 IRBuilder<> &B, const DataLayout &DL, 1140 const TargetLibraryInfo *TLI) { 1141 if (!TLI->has(LibFunc_fwrite_unlocked)) 1142 return nullptr; 1143 1144 Module *M = B.GetInsertBlock()->getModule(); 1145 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1146 StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked); 1147 Constant *F = M->getOrInsertFunction( 1148 FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1149 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1150 1151 if (File->getType()->isPointerTy()) 1152 inferLibFuncAttributes(*M->getFunction(FWriteUnlockedName), *TLI); 1153 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File}); 1154 1155 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1156 CI->setCallingConv(Fn->getCallingConv()); 1157 return CI; 1158 } 1159 1160 Value *llvm::emitFGetCUnlocked(Value *File, IRBuilder<> &B, 1161 const TargetLibraryInfo *TLI) { 1162 if (!TLI->has(LibFunc_fgetc_unlocked)) 1163 return nullptr; 1164 1165 Module *M = B.GetInsertBlock()->getModule(); 1166 Constant *F = 1167 M->getOrInsertFunction("fgetc_unlocked", B.getInt32Ty(), File->getType()); 1168 if (File->getType()->isPointerTy()) 1169 inferLibFuncAttributes(*M->getFunction("fgetc_unlocked"), *TLI); 1170 CallInst *CI = B.CreateCall(F, File, "fgetc_unlocked"); 1171 1172 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1173 CI->setCallingConv(Fn->getCallingConv()); 1174 return CI; 1175 } 1176 1177 Value *llvm::emitFGetSUnlocked(Value *Str, Value *Size, Value *File, 1178 IRBuilder<> &B, const TargetLibraryInfo *TLI) { 1179 if (!TLI->has(LibFunc_fgets_unlocked)) 1180 return nullptr; 1181 1182 Module *M = B.GetInsertBlock()->getModule(); 1183 Constant *F = 1184 M->getOrInsertFunction("fgets_unlocked", B.getInt8PtrTy(), 1185 B.getInt8PtrTy(), B.getInt32Ty(), File->getType()); 1186 inferLibFuncAttributes(*M->getFunction("fgets_unlocked"), *TLI); 1187 CallInst *CI = 1188 B.CreateCall(F, {castToCStr(Str, B), Size, File}, "fgets_unlocked"); 1189 1190 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1191 CI->setCallingConv(Fn->getCallingConv()); 1192 return CI; 1193 } 1194 1195 Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, 1196 IRBuilder<> &B, const DataLayout &DL, 1197 const TargetLibraryInfo *TLI) { 1198 if (!TLI->has(LibFunc_fread_unlocked)) 1199 return nullptr; 1200 1201 Module *M = B.GetInsertBlock()->getModule(); 1202 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1203 StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked); 1204 Constant *F = M->getOrInsertFunction( 1205 FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1206 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1207 1208 if (File->getType()->isPointerTy()) 1209 inferLibFuncAttributes(*M->getFunction(FReadUnlockedName), *TLI); 1210 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File}); 1211 1212 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) 1213 CI->setCallingConv(Fn->getCallingConv()); 1214 return CI; 1215 } 1216