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