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