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