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