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 LLVM_FALLTHROUGH; 310 case LibFunc_memmove: 311 Changed |= setReturnedArg(F, 0); 312 LLVM_FALLTHROUGH; 313 case LibFunc_mempcpy: 314 case LibFunc_memccpy: 315 Changed |= setDoesNotThrow(F); 316 Changed |= setDoesNotCapture(F, 1); 317 Changed |= setOnlyReadsMemory(F, 1); 318 return Changed; 319 case LibFunc_memcpy_chk: 320 Changed |= setDoesNotThrow(F); 321 return Changed; 322 case LibFunc_memalign: 323 Changed |= setRetDoesNotAlias(F); 324 return Changed; 325 case LibFunc_mkdir: 326 Changed |= setDoesNotThrow(F); 327 Changed |= setDoesNotCapture(F, 0); 328 Changed |= setOnlyReadsMemory(F, 0); 329 return Changed; 330 case LibFunc_mktime: 331 Changed |= setDoesNotThrow(F); 332 Changed |= setDoesNotCapture(F, 0); 333 return Changed; 334 case LibFunc_realloc: 335 Changed |= setDoesNotThrow(F); 336 Changed |= setRetDoesNotAlias(F); 337 Changed |= setDoesNotCapture(F, 0); 338 return Changed; 339 case LibFunc_read: 340 // May throw; "read" is a valid pthread cancellation point. 341 Changed |= setDoesNotCapture(F, 1); 342 return Changed; 343 case LibFunc_rewind: 344 Changed |= setDoesNotThrow(F); 345 Changed |= setDoesNotCapture(F, 0); 346 return Changed; 347 case LibFunc_rmdir: 348 case LibFunc_remove: 349 case LibFunc_realpath: 350 Changed |= setDoesNotThrow(F); 351 Changed |= setDoesNotCapture(F, 0); 352 Changed |= setOnlyReadsMemory(F, 0); 353 return Changed; 354 case LibFunc_rename: 355 Changed |= setDoesNotThrow(F); 356 Changed |= setDoesNotCapture(F, 0); 357 Changed |= setDoesNotCapture(F, 1); 358 Changed |= setOnlyReadsMemory(F, 0); 359 Changed |= setOnlyReadsMemory(F, 1); 360 return Changed; 361 case LibFunc_readlink: 362 Changed |= setDoesNotThrow(F); 363 Changed |= setDoesNotCapture(F, 0); 364 Changed |= setDoesNotCapture(F, 1); 365 Changed |= setOnlyReadsMemory(F, 0); 366 return Changed; 367 case LibFunc_write: 368 // May throw; "write" is a valid pthread cancellation point. 369 Changed |= setDoesNotCapture(F, 1); 370 Changed |= setOnlyReadsMemory(F, 1); 371 return Changed; 372 case LibFunc_bcopy: 373 Changed |= setDoesNotThrow(F); 374 Changed |= setDoesNotCapture(F, 0); 375 Changed |= setDoesNotCapture(F, 1); 376 Changed |= setOnlyReadsMemory(F, 0); 377 return Changed; 378 case LibFunc_bcmp: 379 Changed |= setDoesNotThrow(F); 380 Changed |= setOnlyReadsMemory(F); 381 Changed |= setDoesNotCapture(F, 0); 382 Changed |= setDoesNotCapture(F, 1); 383 return Changed; 384 case LibFunc_bzero: 385 Changed |= setDoesNotThrow(F); 386 Changed |= setDoesNotCapture(F, 0); 387 return Changed; 388 case LibFunc_calloc: 389 Changed |= setDoesNotThrow(F); 390 Changed |= setRetDoesNotAlias(F); 391 return Changed; 392 case LibFunc_chmod: 393 case LibFunc_chown: 394 Changed |= setDoesNotThrow(F); 395 Changed |= setDoesNotCapture(F, 0); 396 Changed |= setOnlyReadsMemory(F, 0); 397 return Changed; 398 case LibFunc_ctermid: 399 case LibFunc_clearerr: 400 case LibFunc_closedir: 401 Changed |= setDoesNotThrow(F); 402 Changed |= setDoesNotCapture(F, 0); 403 return Changed; 404 case LibFunc_atoi: 405 case LibFunc_atol: 406 case LibFunc_atof: 407 case LibFunc_atoll: 408 Changed |= setDoesNotThrow(F); 409 Changed |= setOnlyReadsMemory(F); 410 Changed |= setDoesNotCapture(F, 0); 411 return Changed; 412 case LibFunc_access: 413 Changed |= setDoesNotThrow(F); 414 Changed |= setDoesNotCapture(F, 0); 415 Changed |= setOnlyReadsMemory(F, 0); 416 return Changed; 417 case LibFunc_fopen: 418 Changed |= setDoesNotThrow(F); 419 Changed |= setRetDoesNotAlias(F); 420 Changed |= setDoesNotCapture(F, 0); 421 Changed |= setDoesNotCapture(F, 1); 422 Changed |= setOnlyReadsMemory(F, 0); 423 Changed |= setOnlyReadsMemory(F, 1); 424 return Changed; 425 case LibFunc_fdopen: 426 Changed |= setDoesNotThrow(F); 427 Changed |= setRetDoesNotAlias(F); 428 Changed |= setDoesNotCapture(F, 1); 429 Changed |= setOnlyReadsMemory(F, 1); 430 return Changed; 431 case LibFunc_feof: 432 case LibFunc_free: 433 case LibFunc_fseek: 434 case LibFunc_ftell: 435 case LibFunc_fgetc: 436 case LibFunc_fgetc_unlocked: 437 case LibFunc_fseeko: 438 case LibFunc_ftello: 439 case LibFunc_fileno: 440 case LibFunc_fflush: 441 case LibFunc_fclose: 442 case LibFunc_fsetpos: 443 case LibFunc_flockfile: 444 case LibFunc_funlockfile: 445 case LibFunc_ftrylockfile: 446 Changed |= setDoesNotThrow(F); 447 Changed |= setDoesNotCapture(F, 0); 448 return Changed; 449 case LibFunc_ferror: 450 Changed |= setDoesNotThrow(F); 451 Changed |= setDoesNotCapture(F, 0); 452 Changed |= setOnlyReadsMemory(F); 453 return Changed; 454 case LibFunc_fputc: 455 case LibFunc_fputc_unlocked: 456 case LibFunc_fstat: 457 case LibFunc_frexp: 458 case LibFunc_frexpf: 459 case LibFunc_frexpl: 460 case LibFunc_fstatvfs: 461 Changed |= setDoesNotThrow(F); 462 Changed |= setDoesNotCapture(F, 1); 463 return Changed; 464 case LibFunc_fgets: 465 case LibFunc_fgets_unlocked: 466 Changed |= setDoesNotThrow(F); 467 Changed |= setDoesNotCapture(F, 2); 468 return Changed; 469 case LibFunc_fread: 470 case LibFunc_fread_unlocked: 471 Changed |= setDoesNotThrow(F); 472 Changed |= setDoesNotCapture(F, 0); 473 Changed |= setDoesNotCapture(F, 3); 474 return Changed; 475 case LibFunc_fwrite: 476 case LibFunc_fwrite_unlocked: 477 Changed |= setDoesNotThrow(F); 478 Changed |= setDoesNotCapture(F, 0); 479 Changed |= setDoesNotCapture(F, 3); 480 // FIXME: readonly #1? 481 return Changed; 482 case LibFunc_fputs: 483 case LibFunc_fputs_unlocked: 484 Changed |= setDoesNotThrow(F); 485 Changed |= setDoesNotCapture(F, 0); 486 Changed |= setDoesNotCapture(F, 1); 487 Changed |= setOnlyReadsMemory(F, 0); 488 return Changed; 489 case LibFunc_fscanf: 490 case LibFunc_fprintf: 491 Changed |= setDoesNotThrow(F); 492 Changed |= setDoesNotCapture(F, 0); 493 Changed |= setDoesNotCapture(F, 1); 494 Changed |= setOnlyReadsMemory(F, 1); 495 return Changed; 496 case LibFunc_fgetpos: 497 Changed |= setDoesNotThrow(F); 498 Changed |= setDoesNotCapture(F, 0); 499 Changed |= setDoesNotCapture(F, 1); 500 return Changed; 501 case LibFunc_getc: 502 case LibFunc_getlogin_r: 503 case LibFunc_getc_unlocked: 504 Changed |= setDoesNotThrow(F); 505 Changed |= setDoesNotCapture(F, 0); 506 return Changed; 507 case LibFunc_getenv: 508 Changed |= setDoesNotThrow(F); 509 Changed |= setOnlyReadsMemory(F); 510 Changed |= setDoesNotCapture(F, 0); 511 return Changed; 512 case LibFunc_gets: 513 case LibFunc_getchar: 514 case LibFunc_getchar_unlocked: 515 Changed |= setDoesNotThrow(F); 516 return Changed; 517 case LibFunc_getitimer: 518 Changed |= setDoesNotThrow(F); 519 Changed |= setDoesNotCapture(F, 1); 520 return Changed; 521 case LibFunc_getpwnam: 522 Changed |= setDoesNotThrow(F); 523 Changed |= setDoesNotCapture(F, 0); 524 Changed |= setOnlyReadsMemory(F, 0); 525 return Changed; 526 case LibFunc_ungetc: 527 Changed |= setDoesNotThrow(F); 528 Changed |= setDoesNotCapture(F, 1); 529 return Changed; 530 case LibFunc_uname: 531 Changed |= setDoesNotThrow(F); 532 Changed |= setDoesNotCapture(F, 0); 533 return Changed; 534 case LibFunc_unlink: 535 Changed |= setDoesNotThrow(F); 536 Changed |= setDoesNotCapture(F, 0); 537 Changed |= setOnlyReadsMemory(F, 0); 538 return Changed; 539 case LibFunc_unsetenv: 540 Changed |= setDoesNotThrow(F); 541 Changed |= setDoesNotCapture(F, 0); 542 Changed |= setOnlyReadsMemory(F, 0); 543 return Changed; 544 case LibFunc_utime: 545 case LibFunc_utimes: 546 Changed |= setDoesNotThrow(F); 547 Changed |= setDoesNotCapture(F, 0); 548 Changed |= setDoesNotCapture(F, 1); 549 Changed |= setOnlyReadsMemory(F, 0); 550 Changed |= setOnlyReadsMemory(F, 1); 551 return Changed; 552 case LibFunc_putc: 553 case LibFunc_putc_unlocked: 554 Changed |= setDoesNotThrow(F); 555 Changed |= setDoesNotCapture(F, 1); 556 return Changed; 557 case LibFunc_puts: 558 case LibFunc_printf: 559 case LibFunc_perror: 560 Changed |= setDoesNotThrow(F); 561 Changed |= setDoesNotCapture(F, 0); 562 Changed |= setOnlyReadsMemory(F, 0); 563 return Changed; 564 case LibFunc_pread: 565 // May throw; "pread" is a valid pthread cancellation point. 566 Changed |= setDoesNotCapture(F, 1); 567 return Changed; 568 case LibFunc_pwrite: 569 // May throw; "pwrite" is a valid pthread cancellation point. 570 Changed |= setDoesNotCapture(F, 1); 571 Changed |= setOnlyReadsMemory(F, 1); 572 return Changed; 573 case LibFunc_putchar: 574 case LibFunc_putchar_unlocked: 575 Changed |= setDoesNotThrow(F); 576 return Changed; 577 case LibFunc_popen: 578 Changed |= setDoesNotThrow(F); 579 Changed |= setRetDoesNotAlias(F); 580 Changed |= setDoesNotCapture(F, 0); 581 Changed |= setDoesNotCapture(F, 1); 582 Changed |= setOnlyReadsMemory(F, 0); 583 Changed |= setOnlyReadsMemory(F, 1); 584 return Changed; 585 case LibFunc_pclose: 586 Changed |= setDoesNotThrow(F); 587 Changed |= setDoesNotCapture(F, 0); 588 return Changed; 589 case LibFunc_vscanf: 590 Changed |= setDoesNotThrow(F); 591 Changed |= setDoesNotCapture(F, 0); 592 Changed |= setOnlyReadsMemory(F, 0); 593 return Changed; 594 case LibFunc_vsscanf: 595 Changed |= setDoesNotThrow(F); 596 Changed |= setDoesNotCapture(F, 0); 597 Changed |= setDoesNotCapture(F, 1); 598 Changed |= setOnlyReadsMemory(F, 0); 599 Changed |= setOnlyReadsMemory(F, 1); 600 return Changed; 601 case LibFunc_vfscanf: 602 Changed |= setDoesNotThrow(F); 603 Changed |= setDoesNotCapture(F, 0); 604 Changed |= setDoesNotCapture(F, 1); 605 Changed |= setOnlyReadsMemory(F, 1); 606 return Changed; 607 case LibFunc_valloc: 608 Changed |= setDoesNotThrow(F); 609 Changed |= setRetDoesNotAlias(F); 610 return Changed; 611 case LibFunc_vprintf: 612 Changed |= setDoesNotThrow(F); 613 Changed |= setDoesNotCapture(F, 0); 614 Changed |= setOnlyReadsMemory(F, 0); 615 return Changed; 616 case LibFunc_vfprintf: 617 case LibFunc_vsprintf: 618 Changed |= setDoesNotThrow(F); 619 Changed |= setDoesNotCapture(F, 0); 620 Changed |= setDoesNotCapture(F, 1); 621 Changed |= setOnlyReadsMemory(F, 1); 622 return Changed; 623 case LibFunc_vsnprintf: 624 Changed |= setDoesNotThrow(F); 625 Changed |= setDoesNotCapture(F, 0); 626 Changed |= setDoesNotCapture(F, 2); 627 Changed |= setOnlyReadsMemory(F, 2); 628 return Changed; 629 case LibFunc_open: 630 // May throw; "open" is a valid pthread cancellation point. 631 Changed |= setDoesNotCapture(F, 0); 632 Changed |= setOnlyReadsMemory(F, 0); 633 return Changed; 634 case LibFunc_opendir: 635 Changed |= setDoesNotThrow(F); 636 Changed |= setRetDoesNotAlias(F); 637 Changed |= setDoesNotCapture(F, 0); 638 Changed |= setOnlyReadsMemory(F, 0); 639 return Changed; 640 case LibFunc_tmpfile: 641 Changed |= setDoesNotThrow(F); 642 Changed |= setRetDoesNotAlias(F); 643 return Changed; 644 case LibFunc_times: 645 Changed |= setDoesNotThrow(F); 646 Changed |= setDoesNotCapture(F, 0); 647 return Changed; 648 case LibFunc_htonl: 649 case LibFunc_htons: 650 case LibFunc_ntohl: 651 case LibFunc_ntohs: 652 Changed |= setDoesNotThrow(F); 653 Changed |= setDoesNotAccessMemory(F); 654 return Changed; 655 case LibFunc_lstat: 656 Changed |= setDoesNotThrow(F); 657 Changed |= setDoesNotCapture(F, 0); 658 Changed |= setDoesNotCapture(F, 1); 659 Changed |= setOnlyReadsMemory(F, 0); 660 return Changed; 661 case LibFunc_lchown: 662 Changed |= setDoesNotThrow(F); 663 Changed |= setDoesNotCapture(F, 0); 664 Changed |= setOnlyReadsMemory(F, 0); 665 return Changed; 666 case LibFunc_qsort: 667 // May throw; places call through function pointer. 668 Changed |= setDoesNotCapture(F, 3); 669 return Changed; 670 case LibFunc_dunder_strdup: 671 case LibFunc_dunder_strndup: 672 Changed |= setDoesNotThrow(F); 673 Changed |= setRetDoesNotAlias(F); 674 Changed |= setDoesNotCapture(F, 0); 675 Changed |= setOnlyReadsMemory(F, 0); 676 return Changed; 677 case LibFunc_dunder_strtok_r: 678 Changed |= setDoesNotThrow(F); 679 Changed |= setDoesNotCapture(F, 1); 680 Changed |= setOnlyReadsMemory(F, 1); 681 return Changed; 682 case LibFunc_under_IO_getc: 683 Changed |= setDoesNotThrow(F); 684 Changed |= setDoesNotCapture(F, 0); 685 return Changed; 686 case LibFunc_under_IO_putc: 687 Changed |= setDoesNotThrow(F); 688 Changed |= setDoesNotCapture(F, 1); 689 return Changed; 690 case LibFunc_dunder_isoc99_scanf: 691 Changed |= setDoesNotThrow(F); 692 Changed |= setDoesNotCapture(F, 0); 693 Changed |= setOnlyReadsMemory(F, 0); 694 return Changed; 695 case LibFunc_stat64: 696 case LibFunc_lstat64: 697 case LibFunc_statvfs64: 698 Changed |= setDoesNotThrow(F); 699 Changed |= setDoesNotCapture(F, 0); 700 Changed |= setDoesNotCapture(F, 1); 701 Changed |= setOnlyReadsMemory(F, 0); 702 return Changed; 703 case LibFunc_dunder_isoc99_sscanf: 704 Changed |= setDoesNotThrow(F); 705 Changed |= setDoesNotCapture(F, 0); 706 Changed |= setDoesNotCapture(F, 1); 707 Changed |= setOnlyReadsMemory(F, 0); 708 Changed |= setOnlyReadsMemory(F, 1); 709 return Changed; 710 case LibFunc_fopen64: 711 Changed |= setDoesNotThrow(F); 712 Changed |= setRetDoesNotAlias(F); 713 Changed |= setDoesNotCapture(F, 0); 714 Changed |= setDoesNotCapture(F, 1); 715 Changed |= setOnlyReadsMemory(F, 0); 716 Changed |= setOnlyReadsMemory(F, 1); 717 return Changed; 718 case LibFunc_fseeko64: 719 case LibFunc_ftello64: 720 Changed |= setDoesNotThrow(F); 721 Changed |= setDoesNotCapture(F, 0); 722 return Changed; 723 case LibFunc_tmpfile64: 724 Changed |= setDoesNotThrow(F); 725 Changed |= setRetDoesNotAlias(F); 726 return Changed; 727 case LibFunc_fstat64: 728 case LibFunc_fstatvfs64: 729 Changed |= setDoesNotThrow(F); 730 Changed |= setDoesNotCapture(F, 1); 731 return Changed; 732 case LibFunc_open64: 733 // May throw; "open" is a valid pthread cancellation point. 734 Changed |= setDoesNotCapture(F, 0); 735 Changed |= setOnlyReadsMemory(F, 0); 736 return Changed; 737 case LibFunc_gettimeofday: 738 // Currently some platforms have the restrict keyword on the arguments to 739 // gettimeofday. To be conservative, do not add noalias to gettimeofday's 740 // arguments. 741 Changed |= setDoesNotThrow(F); 742 Changed |= setDoesNotCapture(F, 0); 743 Changed |= setDoesNotCapture(F, 1); 744 return Changed; 745 case LibFunc_Znwj: // new(unsigned int) 746 case LibFunc_Znwm: // new(unsigned long) 747 case LibFunc_Znaj: // new[](unsigned int) 748 case LibFunc_Znam: // new[](unsigned long) 749 case LibFunc_msvc_new_int: // new(unsigned int) 750 case LibFunc_msvc_new_longlong: // new(unsigned long long) 751 case LibFunc_msvc_new_array_int: // new[](unsigned int) 752 case LibFunc_msvc_new_array_longlong: // new[](unsigned long long) 753 // Operator new always returns a nonnull noalias pointer 754 Changed |= setRetNonNull(F); 755 Changed |= setRetDoesNotAlias(F); 756 return Changed; 757 // TODO: add LibFunc entries for: 758 // case LibFunc_memset_pattern4: 759 // case LibFunc_memset_pattern8: 760 case LibFunc_memset_pattern16: 761 Changed |= setOnlyAccessesArgMemory(F); 762 Changed |= setDoesNotCapture(F, 0); 763 Changed |= setDoesNotCapture(F, 1); 764 Changed |= setOnlyReadsMemory(F, 1); 765 return Changed; 766 // int __nvvm_reflect(const char *) 767 case LibFunc_nvvm_reflect: 768 Changed |= setDoesNotAccessMemory(F); 769 Changed |= setDoesNotThrow(F); 770 return Changed; 771 772 default: 773 // FIXME: It'd be really nice to cover all the library functions we're 774 // aware of here. 775 return false; 776 } 777 } 778 779 bool llvm::hasFloatFn(const TargetLibraryInfo *TLI, Type *Ty, 780 LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn) { 781 switch (Ty->getTypeID()) { 782 case Type::HalfTyID: 783 return false; 784 case Type::FloatTyID: 785 return TLI->has(FloatFn); 786 case Type::DoubleTyID: 787 return TLI->has(DoubleFn); 788 default: 789 return TLI->has(LongDoubleFn); 790 } 791 } 792 793 StringRef llvm::getFloatFnName(const TargetLibraryInfo *TLI, Type *Ty, 794 LibFunc DoubleFn, LibFunc FloatFn, 795 LibFunc LongDoubleFn) { 796 assert(hasFloatFn(TLI, Ty, DoubleFn, FloatFn, LongDoubleFn) && 797 "Cannot get name for unavailable function!"); 798 799 switch (Ty->getTypeID()) { 800 case Type::HalfTyID: 801 llvm_unreachable("No name for HalfTy!"); 802 case Type::FloatTyID: 803 return TLI->getName(FloatFn); 804 case Type::DoubleTyID: 805 return TLI->getName(DoubleFn); 806 default: 807 return TLI->getName(LongDoubleFn); 808 } 809 } 810 811 //- Emit LibCalls ------------------------------------------------------------// 812 813 Value *llvm::castToCStr(Value *V, IRBuilder<> &B) { 814 unsigned AS = V->getType()->getPointerAddressSpace(); 815 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr"); 816 } 817 818 static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType, 819 ArrayRef<Type *> ParamTypes, 820 ArrayRef<Value *> Operands, IRBuilder<> &B, 821 const TargetLibraryInfo *TLI, 822 bool IsVaArgs = false) { 823 if (!TLI->has(TheLibFunc)) 824 return nullptr; 825 826 Module *M = B.GetInsertBlock()->getModule(); 827 StringRef FuncName = TLI->getName(TheLibFunc); 828 FunctionType *FuncType = FunctionType::get(ReturnType, ParamTypes, IsVaArgs); 829 FunctionCallee Callee = M->getOrInsertFunction(FuncName, FuncType); 830 inferLibFuncAttributes(M, FuncName, *TLI); 831 CallInst *CI = B.CreateCall(Callee, Operands, FuncName); 832 if (const Function *F = 833 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 834 CI->setCallingConv(F->getCallingConv()); 835 return CI; 836 } 837 838 Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL, 839 const TargetLibraryInfo *TLI) { 840 LLVMContext &Context = B.GetInsertBlock()->getContext(); 841 return emitLibCall(LibFunc_strlen, DL.getIntPtrType(Context), 842 B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI); 843 } 844 845 Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B, 846 const TargetLibraryInfo *TLI) { 847 Type *I8Ptr = B.getInt8PtrTy(); 848 Type *I32Ty = B.getInt32Ty(); 849 return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, I32Ty}, 850 {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, B, TLI); 851 } 852 853 Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, 854 const DataLayout &DL, const TargetLibraryInfo *TLI) { 855 LLVMContext &Context = B.GetInsertBlock()->getContext(); 856 return emitLibCall( 857 LibFunc_strncmp, B.getInt32Ty(), 858 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, 859 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); 860 } 861 862 Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, 863 const TargetLibraryInfo *TLI) { 864 Type *I8Ptr = B.getInt8PtrTy(); 865 return emitLibCall(LibFunc_strcpy, I8Ptr, {I8Ptr, I8Ptr}, 866 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI); 867 } 868 869 Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B, 870 const TargetLibraryInfo *TLI) { 871 Type *I8Ptr = B.getInt8PtrTy(); 872 return emitLibCall(LibFunc_stpcpy, I8Ptr, {I8Ptr, I8Ptr}, 873 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI); 874 } 875 876 Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, 877 const TargetLibraryInfo *TLI) { 878 Type *I8Ptr = B.getInt8PtrTy(); 879 return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()}, 880 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI); 881 } 882 883 Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, 884 const TargetLibraryInfo *TLI) { 885 Type *I8Ptr = B.getInt8PtrTy(); 886 return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()}, 887 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI); 888 } 889 890 Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, 891 IRBuilder<> &B, const DataLayout &DL, 892 const TargetLibraryInfo *TLI) { 893 if (!TLI->has(LibFunc_memcpy_chk)) 894 return nullptr; 895 896 Module *M = B.GetInsertBlock()->getModule(); 897 AttributeList AS; 898 AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex, 899 Attribute::NoUnwind); 900 LLVMContext &Context = B.GetInsertBlock()->getContext(); 901 FunctionCallee MemCpy = M->getOrInsertFunction( 902 "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(), 903 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), 904 DL.getIntPtrType(Context)); 905 Dst = castToCStr(Dst, B); 906 Src = castToCStr(Src, B); 907 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize}); 908 if (const Function *F = 909 dyn_cast<Function>(MemCpy.getCallee()->stripPointerCasts())) 910 CI->setCallingConv(F->getCallingConv()); 911 return CI; 912 } 913 914 Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B, 915 const DataLayout &DL, const TargetLibraryInfo *TLI) { 916 LLVMContext &Context = B.GetInsertBlock()->getContext(); 917 return emitLibCall( 918 LibFunc_memchr, B.getInt8PtrTy(), 919 {B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context)}, 920 {castToCStr(Ptr, B), Val, Len}, B, TLI); 921 } 922 923 Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, 924 const DataLayout &DL, const TargetLibraryInfo *TLI) { 925 LLVMContext &Context = B.GetInsertBlock()->getContext(); 926 return emitLibCall( 927 LibFunc_memcmp, B.getInt32Ty(), 928 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, 929 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); 930 } 931 932 Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, 933 const DataLayout &DL, const TargetLibraryInfo *TLI) { 934 LLVMContext &Context = B.GetInsertBlock()->getContext(); 935 return emitLibCall( 936 LibFunc_bcmp, B.getInt32Ty(), 937 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, 938 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); 939 } 940 941 Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, 942 IRBuilder<> &B, const TargetLibraryInfo *TLI) { 943 return emitLibCall( 944 LibFunc_memccpy, B.getInt8PtrTy(), 945 {B.getInt8PtrTy(), B.getInt8PtrTy(), B.getInt32Ty(), Len->getType()}, 946 {Ptr1, Ptr2, Val, Len}, B, TLI); 947 } 948 949 Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, 950 ArrayRef<Value *> VariadicArgs, IRBuilder<> &B, 951 const TargetLibraryInfo *TLI) { 952 SmallVector<Value *, 8> Args{castToCStr(Dest, B), Size, castToCStr(Fmt, B)}; 953 Args.insert(Args.end(), VariadicArgs.begin(), VariadicArgs.end()); 954 return emitLibCall(LibFunc_snprintf, B.getInt32Ty(), 955 {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy()}, 956 Args, B, TLI, /*IsVaArgs=*/true); 957 } 958 959 Value *llvm::emitSPrintf(Value *Dest, Value *Fmt, 960 ArrayRef<Value *> VariadicArgs, IRBuilder<> &B, 961 const TargetLibraryInfo *TLI) { 962 SmallVector<Value *, 8> Args{castToCStr(Dest, B), castToCStr(Fmt, B)}; 963 Args.insert(Args.end(), VariadicArgs.begin(), VariadicArgs.end()); 964 return emitLibCall(LibFunc_sprintf, B.getInt32Ty(), 965 {B.getInt8PtrTy(), B.getInt8PtrTy()}, Args, B, TLI, 966 /*IsVaArgs=*/true); 967 } 968 969 Value *llvm::emitStrCat(Value *Dest, Value *Src, IRBuilder<> &B, 970 const TargetLibraryInfo *TLI) { 971 return emitLibCall(LibFunc_strcat, B.getInt8PtrTy(), 972 {B.getInt8PtrTy(), B.getInt8PtrTy()}, 973 {castToCStr(Dest, B), castToCStr(Src, B)}, B, TLI); 974 } 975 976 Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B, 977 const TargetLibraryInfo *TLI) { 978 return emitLibCall(LibFunc_strlcpy, Size->getType(), 979 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()}, 980 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); 981 } 982 983 Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B, 984 const TargetLibraryInfo *TLI) { 985 return emitLibCall(LibFunc_strlcat, Size->getType(), 986 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()}, 987 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); 988 } 989 990 Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B, 991 const TargetLibraryInfo *TLI) { 992 return emitLibCall(LibFunc_strncat, B.getInt8PtrTy(), 993 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()}, 994 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); 995 } 996 997 Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, 998 IRBuilder<> &B, const TargetLibraryInfo *TLI) { 999 return emitLibCall( 1000 LibFunc_vsnprintf, B.getInt32Ty(), 1001 {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy(), VAList->getType()}, 1002 {castToCStr(Dest, B), Size, castToCStr(Fmt, B), VAList}, B, TLI); 1003 } 1004 1005 Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, 1006 IRBuilder<> &B, const TargetLibraryInfo *TLI) { 1007 return emitLibCall(LibFunc_vsprintf, B.getInt32Ty(), 1008 {B.getInt8PtrTy(), B.getInt8PtrTy(), VAList->getType()}, 1009 {castToCStr(Dest, B), castToCStr(Fmt, B), VAList}, B, TLI); 1010 } 1011 1012 /// Append a suffix to the function name according to the type of 'Op'. 1013 static void appendTypeSuffix(Value *Op, StringRef &Name, 1014 SmallString<20> &NameBuffer) { 1015 if (!Op->getType()->isDoubleTy()) { 1016 NameBuffer += Name; 1017 1018 if (Op->getType()->isFloatTy()) 1019 NameBuffer += 'f'; 1020 else 1021 NameBuffer += 'l'; 1022 1023 Name = NameBuffer; 1024 } 1025 } 1026 1027 static Value *emitUnaryFloatFnCallHelper(Value *Op, StringRef Name, 1028 IRBuilder<> &B, 1029 const AttributeList &Attrs) { 1030 assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall"); 1031 1032 Module *M = B.GetInsertBlock()->getModule(); 1033 FunctionCallee Callee = 1034 M->getOrInsertFunction(Name, Op->getType(), Op->getType()); 1035 CallInst *CI = B.CreateCall(Callee, Op, Name); 1036 1037 // The incoming attribute set may have come from a speculatable intrinsic, but 1038 // is being replaced with a library call which is not allowed to be 1039 // speculatable. 1040 CI->setAttributes(Attrs.removeAttribute(B.getContext(), 1041 AttributeList::FunctionIndex, 1042 Attribute::Speculatable)); 1043 if (const Function *F = 1044 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 1045 CI->setCallingConv(F->getCallingConv()); 1046 1047 return CI; 1048 } 1049 1050 Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, 1051 const AttributeList &Attrs) { 1052 SmallString<20> NameBuffer; 1053 appendTypeSuffix(Op, Name, NameBuffer); 1054 1055 return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs); 1056 } 1057 1058 Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, 1059 LibFunc DoubleFn, LibFunc FloatFn, 1060 LibFunc LongDoubleFn, IRBuilder<> &B, 1061 const AttributeList &Attrs) { 1062 // Get the name of the function according to TLI. 1063 StringRef Name = getFloatFnName(TLI, Op->getType(), 1064 DoubleFn, FloatFn, LongDoubleFn); 1065 1066 return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs); 1067 } 1068 1069 static Value *emitBinaryFloatFnCallHelper(Value *Op1, Value *Op2, 1070 StringRef Name, IRBuilder<> &B, 1071 const AttributeList &Attrs) { 1072 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall"); 1073 1074 Module *M = B.GetInsertBlock()->getModule(); 1075 FunctionCallee Callee = M->getOrInsertFunction(Name, Op1->getType(), 1076 Op1->getType(), Op2->getType()); 1077 CallInst *CI = B.CreateCall(Callee, { Op1, Op2 }, Name); 1078 1079 // The incoming attribute set may have come from a speculatable intrinsic, but 1080 // is being replaced with a library call which is not allowed to be 1081 // speculatable. 1082 CI->setAttributes(Attrs.removeAttribute(B.getContext(), 1083 AttributeList::FunctionIndex, 1084 Attribute::Speculatable)); 1085 if (const Function *F = 1086 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 1087 CI->setCallingConv(F->getCallingConv()); 1088 1089 return CI; 1090 } 1091 1092 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, 1093 IRBuilder<> &B, const AttributeList &Attrs) { 1094 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall"); 1095 1096 SmallString<20> NameBuffer; 1097 appendTypeSuffix(Op1, Name, NameBuffer); 1098 1099 return emitBinaryFloatFnCallHelper(Op1, Op2, Name, B, Attrs); 1100 } 1101 1102 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, 1103 const TargetLibraryInfo *TLI, 1104 LibFunc DoubleFn, LibFunc FloatFn, 1105 LibFunc LongDoubleFn, IRBuilder<> &B, 1106 const AttributeList &Attrs) { 1107 // Get the name of the function according to TLI. 1108 StringRef Name = getFloatFnName(TLI, Op1->getType(), 1109 DoubleFn, FloatFn, LongDoubleFn); 1110 1111 return emitBinaryFloatFnCallHelper(Op1, Op2, Name, B, Attrs); 1112 } 1113 1114 Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B, 1115 const TargetLibraryInfo *TLI) { 1116 if (!TLI->has(LibFunc_putchar)) 1117 return nullptr; 1118 1119 Module *M = B.GetInsertBlock()->getModule(); 1120 StringRef PutCharName = TLI->getName(LibFunc_putchar); 1121 FunctionCallee PutChar = 1122 M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty()); 1123 inferLibFuncAttributes(M, PutCharName, *TLI); 1124 CallInst *CI = B.CreateCall(PutChar, 1125 B.CreateIntCast(Char, 1126 B.getInt32Ty(), 1127 /*isSigned*/true, 1128 "chari"), 1129 PutCharName); 1130 1131 if (const Function *F = 1132 dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts())) 1133 CI->setCallingConv(F->getCallingConv()); 1134 return CI; 1135 } 1136 1137 Value *llvm::emitPutS(Value *Str, IRBuilder<> &B, 1138 const TargetLibraryInfo *TLI) { 1139 if (!TLI->has(LibFunc_puts)) 1140 return nullptr; 1141 1142 Module *M = B.GetInsertBlock()->getModule(); 1143 StringRef PutsName = TLI->getName(LibFunc_puts); 1144 FunctionCallee PutS = 1145 M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy()); 1146 inferLibFuncAttributes(M, PutsName, *TLI); 1147 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName); 1148 if (const Function *F = 1149 dyn_cast<Function>(PutS.getCallee()->stripPointerCasts())) 1150 CI->setCallingConv(F->getCallingConv()); 1151 return CI; 1152 } 1153 1154 Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B, 1155 const TargetLibraryInfo *TLI) { 1156 if (!TLI->has(LibFunc_fputc)) 1157 return nullptr; 1158 1159 Module *M = B.GetInsertBlock()->getModule(); 1160 StringRef FPutcName = TLI->getName(LibFunc_fputc); 1161 FunctionCallee F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(), 1162 B.getInt32Ty(), File->getType()); 1163 if (File->getType()->isPointerTy()) 1164 inferLibFuncAttributes(M, FPutcName, *TLI); 1165 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, 1166 "chari"); 1167 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName); 1168 1169 if (const Function *Fn = 1170 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1171 CI->setCallingConv(Fn->getCallingConv()); 1172 return CI; 1173 } 1174 1175 Value *llvm::emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B, 1176 const TargetLibraryInfo *TLI) { 1177 if (!TLI->has(LibFunc_fputc_unlocked)) 1178 return nullptr; 1179 1180 Module *M = B.GetInsertBlock()->getModule(); 1181 StringRef FPutcUnlockedName = TLI->getName(LibFunc_fputc_unlocked); 1182 FunctionCallee F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(), 1183 B.getInt32Ty(), File->getType()); 1184 if (File->getType()->isPointerTy()) 1185 inferLibFuncAttributes(M, FPutcUnlockedName, *TLI); 1186 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari"); 1187 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcUnlockedName); 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, IRBuilder<> &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::emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B, 1215 const TargetLibraryInfo *TLI) { 1216 if (!TLI->has(LibFunc_fputs_unlocked)) 1217 return nullptr; 1218 1219 Module *M = B.GetInsertBlock()->getModule(); 1220 StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked); 1221 FunctionCallee F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(), 1222 B.getInt8PtrTy(), File->getType()); 1223 if (File->getType()->isPointerTy()) 1224 inferLibFuncAttributes(M, FPutsUnlockedName, *TLI); 1225 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsUnlockedName); 1226 1227 if (const Function *Fn = 1228 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1229 CI->setCallingConv(Fn->getCallingConv()); 1230 return CI; 1231 } 1232 1233 Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, 1234 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1235 if (!TLI->has(LibFunc_fwrite)) 1236 return nullptr; 1237 1238 Module *M = B.GetInsertBlock()->getModule(); 1239 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1240 StringRef FWriteName = TLI->getName(LibFunc_fwrite); 1241 FunctionCallee F = M->getOrInsertFunction( 1242 FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1243 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1244 1245 if (File->getType()->isPointerTy()) 1246 inferLibFuncAttributes(M, FWriteName, *TLI); 1247 CallInst *CI = 1248 B.CreateCall(F, {castToCStr(Ptr, B), Size, 1249 ConstantInt::get(DL.getIntPtrType(Context), 1), File}); 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::emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL, 1258 const TargetLibraryInfo *TLI) { 1259 if (!TLI->has(LibFunc_malloc)) 1260 return nullptr; 1261 1262 Module *M = B.GetInsertBlock()->getModule(); 1263 StringRef MallocName = TLI->getName(LibFunc_malloc); 1264 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1265 FunctionCallee Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(), 1266 DL.getIntPtrType(Context)); 1267 inferLibFuncAttributes(M, MallocName, *TLI); 1268 CallInst *CI = B.CreateCall(Malloc, Num, MallocName); 1269 1270 if (const Function *F = 1271 dyn_cast<Function>(Malloc.getCallee()->stripPointerCasts())) 1272 CI->setCallingConv(F->getCallingConv()); 1273 1274 return CI; 1275 } 1276 1277 Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs, 1278 IRBuilder<> &B, const TargetLibraryInfo &TLI) { 1279 if (!TLI.has(LibFunc_calloc)) 1280 return nullptr; 1281 1282 Module *M = B.GetInsertBlock()->getModule(); 1283 StringRef CallocName = TLI.getName(LibFunc_calloc); 1284 const DataLayout &DL = M->getDataLayout(); 1285 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext())); 1286 FunctionCallee Calloc = M->getOrInsertFunction( 1287 CallocName, Attrs, B.getInt8PtrTy(), PtrType, PtrType); 1288 inferLibFuncAttributes(M, CallocName, TLI); 1289 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName); 1290 1291 if (const auto *F = 1292 dyn_cast<Function>(Calloc.getCallee()->stripPointerCasts())) 1293 CI->setCallingConv(F->getCallingConv()); 1294 1295 return CI; 1296 } 1297 1298 Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, 1299 IRBuilder<> &B, const DataLayout &DL, 1300 const TargetLibraryInfo *TLI) { 1301 if (!TLI->has(LibFunc_fwrite_unlocked)) 1302 return nullptr; 1303 1304 Module *M = B.GetInsertBlock()->getModule(); 1305 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1306 StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked); 1307 FunctionCallee F = M->getOrInsertFunction( 1308 FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1309 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1310 1311 if (File->getType()->isPointerTy()) 1312 inferLibFuncAttributes(M, FWriteUnlockedName, *TLI); 1313 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File}); 1314 1315 if (const Function *Fn = 1316 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1317 CI->setCallingConv(Fn->getCallingConv()); 1318 return CI; 1319 } 1320 1321 Value *llvm::emitFGetCUnlocked(Value *File, IRBuilder<> &B, 1322 const TargetLibraryInfo *TLI) { 1323 if (!TLI->has(LibFunc_fgetc_unlocked)) 1324 return nullptr; 1325 1326 Module *M = B.GetInsertBlock()->getModule(); 1327 StringRef FGetCUnlockedName = TLI->getName(LibFunc_fgetc_unlocked); 1328 FunctionCallee F = M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(), 1329 File->getType()); 1330 if (File->getType()->isPointerTy()) 1331 inferLibFuncAttributes(M, FGetCUnlockedName, *TLI); 1332 CallInst *CI = B.CreateCall(F, File, FGetCUnlockedName); 1333 1334 if (const Function *Fn = 1335 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1336 CI->setCallingConv(Fn->getCallingConv()); 1337 return CI; 1338 } 1339 1340 Value *llvm::emitFGetSUnlocked(Value *Str, Value *Size, Value *File, 1341 IRBuilder<> &B, const TargetLibraryInfo *TLI) { 1342 if (!TLI->has(LibFunc_fgets_unlocked)) 1343 return nullptr; 1344 1345 Module *M = B.GetInsertBlock()->getModule(); 1346 StringRef FGetSUnlockedName = TLI->getName(LibFunc_fgets_unlocked); 1347 FunctionCallee F = 1348 M->getOrInsertFunction(FGetSUnlockedName, B.getInt8PtrTy(), 1349 B.getInt8PtrTy(), B.getInt32Ty(), File->getType()); 1350 inferLibFuncAttributes(M, FGetSUnlockedName, *TLI); 1351 CallInst *CI = 1352 B.CreateCall(F, {castToCStr(Str, B), Size, File}, FGetSUnlockedName); 1353 1354 if (const Function *Fn = 1355 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1356 CI->setCallingConv(Fn->getCallingConv()); 1357 return CI; 1358 } 1359 1360 Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File, 1361 IRBuilder<> &B, const DataLayout &DL, 1362 const TargetLibraryInfo *TLI) { 1363 if (!TLI->has(LibFunc_fread_unlocked)) 1364 return nullptr; 1365 1366 Module *M = B.GetInsertBlock()->getModule(); 1367 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1368 StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked); 1369 FunctionCallee F = M->getOrInsertFunction( 1370 FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(), 1371 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType()); 1372 1373 if (File->getType()->isPointerTy()) 1374 inferLibFuncAttributes(M, FReadUnlockedName, *TLI); 1375 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File}); 1376 1377 if (const Function *Fn = 1378 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1379 CI->setCallingConv(Fn->getCallingConv()); 1380 return CI; 1381 } 1382