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