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