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 Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, 934 const DataLayout &DL, const TargetLibraryInfo *TLI) { 935 if (!TLI->has(LibFunc_memcmp)) 936 return nullptr; 937 938 Module *M = B.GetInsertBlock()->getModule(); 939 StringRef MemCmpName = TLI->getName(LibFunc_memcmp); 940 LLVMContext &Context = B.GetInsertBlock()->getContext(); 941 FunctionCallee MemCmp = 942 M->getOrInsertFunction(MemCmpName, B.getInt32Ty(), B.getInt8PtrTy(), 943 B.getInt8PtrTy(), DL.getIntPtrType(Context)); 944 inferLibFuncAttributes(M, MemCmpName, *TLI); 945 CallInst *CI = B.CreateCall( 946 MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, MemCmpName); 947 948 if (const Function *F = 949 dyn_cast<Function>(MemCmp.getCallee()->stripPointerCasts())) 950 CI->setCallingConv(F->getCallingConv()); 951 952 return CI; 953 } 954 955 /// Append a suffix to the function name according to the type of 'Op'. 956 static void appendTypeSuffix(Value *Op, StringRef &Name, 957 SmallString<20> &NameBuffer) { 958 if (!Op->getType()->isDoubleTy()) { 959 NameBuffer += Name; 960 961 if (Op->getType()->isFloatTy()) 962 NameBuffer += 'f'; 963 else 964 NameBuffer += 'l'; 965 966 Name = NameBuffer; 967 } 968 } 969 970 static Value *emitUnaryFloatFnCallHelper(Value *Op, StringRef Name, 971 IRBuilder<> &B, 972 const AttributeList &Attrs) { 973 assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall"); 974 975 Module *M = B.GetInsertBlock()->getModule(); 976 FunctionCallee Callee = 977 M->getOrInsertFunction(Name, Op->getType(), Op->getType()); 978 CallInst *CI = B.CreateCall(Callee, Op, Name); 979 980 // The incoming attribute set may have come from a speculatable intrinsic, but 981 // is being replaced with a library call which is not allowed to be 982 // speculatable. 983 CI->setAttributes(Attrs.removeAttribute(B.getContext(), 984 AttributeList::FunctionIndex, 985 Attribute::Speculatable)); 986 if (const Function *F = 987 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 988 CI->setCallingConv(F->getCallingConv()); 989 990 return CI; 991 } 992 993 Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, 994 const AttributeList &Attrs) { 995 SmallString<20> NameBuffer; 996 appendTypeSuffix(Op, Name, NameBuffer); 997 998 return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs); 999 } 1000 1001 Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, 1002 LibFunc DoubleFn, LibFunc FloatFn, 1003 LibFunc LongDoubleFn, IRBuilder<> &B, 1004 const AttributeList &Attrs) { 1005 // Get the name of the function according to TLI. 1006 StringRef Name = getUnaryFloatFn(TLI, Op->getType(), 1007 DoubleFn, FloatFn, LongDoubleFn); 1008 1009 return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs); 1010 } 1011 1012 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, 1013 IRBuilder<> &B, const AttributeList &Attrs) { 1014 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall"); 1015 1016 SmallString<20> NameBuffer; 1017 appendTypeSuffix(Op1, Name, NameBuffer); 1018 1019 Module *M = B.GetInsertBlock()->getModule(); 1020 FunctionCallee Callee = M->getOrInsertFunction( 1021 Name, Op1->getType(), Op1->getType(), Op2->getType()); 1022 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name); 1023 CI->setAttributes(Attrs); 1024 if (const Function *F = 1025 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 1026 CI->setCallingConv(F->getCallingConv()); 1027 1028 return CI; 1029 } 1030 1031 Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B, 1032 const TargetLibraryInfo *TLI) { 1033 if (!TLI->has(LibFunc_putchar)) 1034 return nullptr; 1035 1036 Module *M = B.GetInsertBlock()->getModule(); 1037 StringRef PutCharName = TLI->getName(LibFunc_putchar); 1038 FunctionCallee PutChar = 1039 M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty()); 1040 inferLibFuncAttributes(M, PutCharName, *TLI); 1041 CallInst *CI = B.CreateCall(PutChar, 1042 B.CreateIntCast(Char, 1043 B.getInt32Ty(), 1044 /*isSigned*/true, 1045 "chari"), 1046 PutCharName); 1047 1048 if (const Function *F = 1049 dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts())) 1050 CI->setCallingConv(F->getCallingConv()); 1051 return CI; 1052 } 1053 1054 Value *llvm::emitPutS(Value *Str, IRBuilder<> &B, 1055 const TargetLibraryInfo *TLI) { 1056 if (!TLI->has(LibFunc_puts)) 1057 return nullptr; 1058 1059 Module *M = B.GetInsertBlock()->getModule(); 1060 StringRef PutsName = TLI->getName(LibFunc_puts); 1061 FunctionCallee PutS = 1062 M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy()); 1063 inferLibFuncAttributes(M, PutsName, *TLI); 1064 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName); 1065 if (const Function *F = 1066 dyn_cast<Function>(PutS.getCallee()->stripPointerCasts())) 1067 CI->setCallingConv(F->getCallingConv()); 1068 return CI; 1069 } 1070 1071 Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B, 1072 const TargetLibraryInfo *TLI) { 1073 if (!TLI->has(LibFunc_fputc)) 1074 return nullptr; 1075 1076 Module *M = B.GetInsertBlock()->getModule(); 1077 StringRef FPutcName = TLI->getName(LibFunc_fputc); 1078 FunctionCallee F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(), 1079 B.getInt32Ty(), File->getType()); 1080 if (File->getType()->isPointerTy()) 1081 inferLibFuncAttributes(M, FPutcName, *TLI); 1082 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, 1083 "chari"); 1084 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName); 1085 1086 if (const Function *Fn = 1087 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1088 CI->setCallingConv(Fn->getCallingConv()); 1089 return CI; 1090 } 1091 1092 Value *llvm::emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B, 1093 const TargetLibraryInfo *TLI) { 1094 if (!TLI->has(LibFunc_fputc_unlocked)) 1095 return nullptr; 1096 1097 Module *M = B.GetInsertBlock()->getModule(); 1098 StringRef FPutcUnlockedName = TLI->getName(LibFunc_fputc_unlocked); 1099 FunctionCallee F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(), 1100 B.getInt32Ty(), File->getType()); 1101 if (File->getType()->isPointerTy()) 1102 inferLibFuncAttributes(M, FPutcUnlockedName, *TLI); 1103 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari"); 1104 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcUnlockedName); 1105 1106 if (const Function *Fn = 1107 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1108 CI->setCallingConv(Fn->getCallingConv()); 1109 return CI; 1110 } 1111 1112 Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B, 1113 const TargetLibraryInfo *TLI) { 1114 if (!TLI->has(LibFunc_fputs)) 1115 return nullptr; 1116 1117 Module *M = B.GetInsertBlock()->getModule(); 1118 StringRef FPutsName = TLI->getName(LibFunc_fputs); 1119 FunctionCallee F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(), 1120 B.getInt8PtrTy(), File->getType()); 1121 if (File->getType()->isPointerTy()) 1122 inferLibFuncAttributes(M, FPutsName, *TLI); 1123 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName); 1124 1125 if (const Function *Fn = 1126 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1127 CI->setCallingConv(Fn->getCallingConv()); 1128 return CI; 1129 } 1130 1131 Value *llvm::emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B, 1132 const TargetLibraryInfo *TLI) { 1133 if (!TLI->has(LibFunc_fputs_unlocked)) 1134 return nullptr; 1135 1136 Module *M = B.GetInsertBlock()->getModule(); 1137 StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked); 1138 FunctionCallee F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(), 1139 B.getInt8PtrTy(), File->getType()); 1140 if (File->getType()->isPointerTy()) 1141 inferLibFuncAttributes(M, FPutsUnlockedName, *TLI); 1142 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsUnlockedName); 1143 1144 if (const Function *Fn = 1145 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1146 CI->setCallingConv(Fn->getCallingConv()); 1147 return CI; 1148 } 1149 1150 Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, 1151 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1152 if (!TLI->has(LibFunc_fwrite)) 1153 return nullptr; 1154 1155 Module *M = B.GetInsertBlock()->getModule(); 1156 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1157 StringRef FWriteName = TLI->getName(LibFunc_fwrite); 1158 FunctionCallee F = M->getOrInsertFunction( 1159 FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1160 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1161 1162 if (File->getType()->isPointerTy()) 1163 inferLibFuncAttributes(M, FWriteName, *TLI); 1164 CallInst *CI = 1165 B.CreateCall(F, {castToCStr(Ptr, B), Size, 1166 ConstantInt::get(DL.getIntPtrType(Context), 1), File}); 1167 1168 if (const Function *Fn = 1169 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1170 CI->setCallingConv(Fn->getCallingConv()); 1171 return CI; 1172 } 1173 1174 Value *llvm::emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL, 1175 const TargetLibraryInfo *TLI) { 1176 if (!TLI->has(LibFunc_malloc)) 1177 return nullptr; 1178 1179 Module *M = B.GetInsertBlock()->getModule(); 1180 StringRef MallocName = TLI->getName(LibFunc_malloc); 1181 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1182 FunctionCallee Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(), 1183 DL.getIntPtrType(Context)); 1184 inferLibFuncAttributes(M, MallocName, *TLI); 1185 CallInst *CI = B.CreateCall(Malloc, Num, MallocName); 1186 1187 if (const Function *F = 1188 dyn_cast<Function>(Malloc.getCallee()->stripPointerCasts())) 1189 CI->setCallingConv(F->getCallingConv()); 1190 1191 return CI; 1192 } 1193 1194 Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs, 1195 IRBuilder<> &B, const TargetLibraryInfo &TLI) { 1196 if (!TLI.has(LibFunc_calloc)) 1197 return nullptr; 1198 1199 Module *M = B.GetInsertBlock()->getModule(); 1200 StringRef CallocName = TLI.getName(LibFunc_calloc); 1201 const DataLayout &DL = M->getDataLayout(); 1202 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext())); 1203 FunctionCallee Calloc = M->getOrInsertFunction( 1204 CallocName, Attrs, B.getInt8PtrTy(), PtrType, PtrType); 1205 inferLibFuncAttributes(M, CallocName, TLI); 1206 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName); 1207 1208 if (const auto *F = 1209 dyn_cast<Function>(Calloc.getCallee()->stripPointerCasts())) 1210 CI->setCallingConv(F->getCallingConv()); 1211 1212 return CI; 1213 } 1214 1215 Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, 1216 IRBuilder<> &B, const DataLayout &DL, 1217 const TargetLibraryInfo *TLI) { 1218 if (!TLI->has(LibFunc_fwrite_unlocked)) 1219 return nullptr; 1220 1221 Module *M = B.GetInsertBlock()->getModule(); 1222 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1223 StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked); 1224 FunctionCallee F = M->getOrInsertFunction( 1225 FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1226 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1227 1228 if (File->getType()->isPointerTy()) 1229 inferLibFuncAttributes(M, FWriteUnlockedName, *TLI); 1230 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File}); 1231 1232 if (const Function *Fn = 1233 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1234 CI->setCallingConv(Fn->getCallingConv()); 1235 return CI; 1236 } 1237 1238 Value *llvm::emitFGetCUnlocked(Value *File, IRBuilder<> &B, 1239 const TargetLibraryInfo *TLI) { 1240 if (!TLI->has(LibFunc_fgetc_unlocked)) 1241 return nullptr; 1242 1243 Module *M = B.GetInsertBlock()->getModule(); 1244 StringRef FGetCUnlockedName = TLI->getName(LibFunc_fgetc_unlocked); 1245 FunctionCallee F = M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(), 1246 File->getType()); 1247 if (File->getType()->isPointerTy()) 1248 inferLibFuncAttributes(M, FGetCUnlockedName, *TLI); 1249 CallInst *CI = B.CreateCall(F, File, FGetCUnlockedName); 1250 1251 if (const Function *Fn = 1252 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1253 CI->setCallingConv(Fn->getCallingConv()); 1254 return CI; 1255 } 1256 1257 Value *llvm::emitFGetSUnlocked(Value *Str, Value *Size, Value *File, 1258 IRBuilder<> &B, const TargetLibraryInfo *TLI) { 1259 if (!TLI->has(LibFunc_fgets_unlocked)) 1260 return nullptr; 1261 1262 Module *M = B.GetInsertBlock()->getModule(); 1263 StringRef FGetSUnlockedName = TLI->getName(LibFunc_fgets_unlocked); 1264 FunctionCallee F = 1265 M->getOrInsertFunction(FGetSUnlockedName, B.getInt8PtrTy(), 1266 B.getInt8PtrTy(), B.getInt32Ty(), File->getType()); 1267 inferLibFuncAttributes(M, FGetSUnlockedName, *TLI); 1268 CallInst *CI = 1269 B.CreateCall(F, {castToCStr(Str, B), Size, File}, FGetSUnlockedName); 1270 1271 if (const Function *Fn = 1272 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1273 CI->setCallingConv(Fn->getCallingConv()); 1274 return CI; 1275 } 1276 1277 Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, 1278 IRBuilder<> &B, const DataLayout &DL, 1279 const TargetLibraryInfo *TLI) { 1280 if (!TLI->has(LibFunc_fread_unlocked)) 1281 return nullptr; 1282 1283 Module *M = B.GetInsertBlock()->getModule(); 1284 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1285 StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked); 1286 FunctionCallee F = M->getOrInsertFunction( 1287 FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1288 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1289 1290 if (File->getType()->isPointerTy()) 1291 inferLibFuncAttributes(M, FReadUnlockedName, *TLI); 1292 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File}); 1293 1294 if (const Function *Fn = 1295 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1296 CI->setCallingConv(Fn->getCallingConv()); 1297 return CI; 1298 } 1299