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/MemoryBuiltins.h" 17 #include "llvm/Analysis/TargetLibraryInfo.h" 18 #include "llvm/IR/Argument.h" 19 #include "llvm/IR/CallingConv.h" 20 #include "llvm/IR/Constants.h" 21 #include "llvm/IR/DataLayout.h" 22 #include "llvm/IR/Function.h" 23 #include "llvm/IR/IRBuilder.h" 24 #include "llvm/IR/Module.h" 25 #include "llvm/IR/Type.h" 26 #include "llvm/Support/TypeSize.h" 27 #include <optional> 28 29 using namespace llvm; 30 31 #define DEBUG_TYPE "build-libcalls" 32 33 //- Infer Attributes ---------------------------------------------------------// 34 35 STATISTIC(NumReadNone, "Number of functions inferred as readnone"); 36 STATISTIC(NumInaccessibleMemOnly, 37 "Number of functions inferred as inaccessiblememonly"); 38 STATISTIC(NumReadOnly, "Number of functions inferred as readonly"); 39 STATISTIC(NumWriteOnly, "Number of functions inferred as writeonly"); 40 STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly"); 41 STATISTIC(NumInaccessibleMemOrArgMemOnly, 42 "Number of functions inferred as inaccessiblemem_or_argmemonly"); 43 STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind"); 44 STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture"); 45 STATISTIC(NumWriteOnlyArg, "Number of arguments inferred as writeonly"); 46 STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly"); 47 STATISTIC(NumNoAlias, "Number of function returns inferred as noalias"); 48 STATISTIC(NumNoUndef, "Number of function returns inferred as noundef returns"); 49 STATISTIC(NumReturnedArg, "Number of arguments inferred as returned"); 50 STATISTIC(NumWillReturn, "Number of functions inferred as willreturn"); 51 52 static bool setDoesNotAccessMemory(Function &F) { 53 if (F.doesNotAccessMemory()) 54 return false; 55 F.setDoesNotAccessMemory(); 56 ++NumReadNone; 57 return true; 58 } 59 60 static bool setOnlyAccessesInaccessibleMemory(Function &F) { 61 if (F.onlyAccessesInaccessibleMemory()) 62 return false; 63 F.setOnlyAccessesInaccessibleMemory(); 64 ++NumInaccessibleMemOnly; 65 return true; 66 } 67 68 static bool setOnlyReadsMemory(Function &F) { 69 if (F.onlyReadsMemory()) 70 return false; 71 F.setOnlyReadsMemory(); 72 ++NumReadOnly; 73 return true; 74 } 75 76 static bool setOnlyWritesMemory(Function &F) { 77 if (F.onlyWritesMemory()) // writeonly or readnone 78 return false; 79 ++NumWriteOnly; 80 F.setOnlyWritesMemory(); 81 return true; 82 } 83 84 static bool setOnlyAccessesArgMemory(Function &F) { 85 if (F.onlyAccessesArgMemory()) 86 return false; 87 F.setOnlyAccessesArgMemory(); 88 ++NumArgMemOnly; 89 return true; 90 } 91 92 static bool setOnlyAccessesInaccessibleMemOrArgMem(Function &F) { 93 if (F.onlyAccessesInaccessibleMemOrArgMem()) 94 return false; 95 F.setOnlyAccessesInaccessibleMemOrArgMem(); 96 ++NumInaccessibleMemOrArgMemOnly; 97 return true; 98 } 99 100 static bool setDoesNotThrow(Function &F) { 101 if (F.doesNotThrow()) 102 return false; 103 F.setDoesNotThrow(); 104 ++NumNoUnwind; 105 return true; 106 } 107 108 static bool setRetDoesNotAlias(Function &F) { 109 if (F.hasRetAttribute(Attribute::NoAlias)) 110 return false; 111 F.addRetAttr(Attribute::NoAlias); 112 ++NumNoAlias; 113 return true; 114 } 115 116 static bool setDoesNotCapture(Function &F, unsigned ArgNo) { 117 if (F.hasParamAttribute(ArgNo, Attribute::NoCapture)) 118 return false; 119 F.addParamAttr(ArgNo, Attribute::NoCapture); 120 ++NumNoCapture; 121 return true; 122 } 123 124 static bool setDoesNotAlias(Function &F, unsigned ArgNo) { 125 if (F.hasParamAttribute(ArgNo, Attribute::NoAlias)) 126 return false; 127 F.addParamAttr(ArgNo, Attribute::NoAlias); 128 ++NumNoAlias; 129 return true; 130 } 131 132 static bool setOnlyReadsMemory(Function &F, unsigned ArgNo) { 133 if (F.hasParamAttribute(ArgNo, Attribute::ReadOnly)) 134 return false; 135 F.addParamAttr(ArgNo, Attribute::ReadOnly); 136 ++NumReadOnlyArg; 137 return true; 138 } 139 140 static bool setOnlyWritesMemory(Function &F, unsigned ArgNo) { 141 if (F.hasParamAttribute(ArgNo, Attribute::WriteOnly)) 142 return false; 143 F.addParamAttr(ArgNo, Attribute::WriteOnly); 144 ++NumWriteOnlyArg; 145 return true; 146 } 147 148 static bool setRetNoUndef(Function &F) { 149 if (!F.getReturnType()->isVoidTy() && 150 !F.hasRetAttribute(Attribute::NoUndef)) { 151 F.addRetAttr(Attribute::NoUndef); 152 ++NumNoUndef; 153 return true; 154 } 155 return false; 156 } 157 158 static bool setArgsNoUndef(Function &F) { 159 bool Changed = false; 160 for (unsigned ArgNo = 0; ArgNo < F.arg_size(); ++ArgNo) { 161 if (!F.hasParamAttribute(ArgNo, Attribute::NoUndef)) { 162 F.addParamAttr(ArgNo, Attribute::NoUndef); 163 ++NumNoUndef; 164 Changed = true; 165 } 166 } 167 return Changed; 168 } 169 170 static bool setArgNoUndef(Function &F, unsigned ArgNo) { 171 if (F.hasParamAttribute(ArgNo, Attribute::NoUndef)) 172 return false; 173 F.addParamAttr(ArgNo, Attribute::NoUndef); 174 ++NumNoUndef; 175 return true; 176 } 177 178 static bool setRetAndArgsNoUndef(Function &F) { 179 bool UndefAdded = false; 180 UndefAdded |= setRetNoUndef(F); 181 UndefAdded |= setArgsNoUndef(F); 182 return UndefAdded; 183 } 184 185 static bool setReturnedArg(Function &F, unsigned ArgNo) { 186 if (F.hasParamAttribute(ArgNo, Attribute::Returned)) 187 return false; 188 F.addParamAttr(ArgNo, Attribute::Returned); 189 ++NumReturnedArg; 190 return true; 191 } 192 193 static bool setNonLazyBind(Function &F) { 194 if (F.hasFnAttribute(Attribute::NonLazyBind)) 195 return false; 196 F.addFnAttr(Attribute::NonLazyBind); 197 return true; 198 } 199 200 static bool setDoesNotFreeMemory(Function &F) { 201 if (F.hasFnAttribute(Attribute::NoFree)) 202 return false; 203 F.addFnAttr(Attribute::NoFree); 204 return true; 205 } 206 207 static bool setWillReturn(Function &F) { 208 if (F.hasFnAttribute(Attribute::WillReturn)) 209 return false; 210 F.addFnAttr(Attribute::WillReturn); 211 ++NumWillReturn; 212 return true; 213 } 214 215 static bool setAlignedAllocParam(Function &F, unsigned ArgNo) { 216 if (F.hasParamAttribute(ArgNo, Attribute::AllocAlign)) 217 return false; 218 F.addParamAttr(ArgNo, Attribute::AllocAlign); 219 return true; 220 } 221 222 static bool setAllocatedPointerParam(Function &F, unsigned ArgNo) { 223 if (F.hasParamAttribute(ArgNo, Attribute::AllocatedPointer)) 224 return false; 225 F.addParamAttr(ArgNo, Attribute::AllocatedPointer); 226 return true; 227 } 228 229 static bool setAllocSize(Function &F, unsigned ElemSizeArg, 230 std::optional<unsigned> NumElemsArg) { 231 if (F.hasFnAttribute(Attribute::AllocSize)) 232 return false; 233 F.addFnAttr(Attribute::getWithAllocSizeArgs(F.getContext(), ElemSizeArg, 234 NumElemsArg)); 235 return true; 236 } 237 238 static bool setAllocFamily(Function &F, StringRef Family) { 239 if (F.hasFnAttribute("alloc-family")) 240 return false; 241 F.addFnAttr("alloc-family", Family); 242 return true; 243 } 244 245 static bool setAllocKind(Function &F, AllocFnKind K) { 246 if (F.hasFnAttribute(Attribute::AllocKind)) 247 return false; 248 F.addFnAttr( 249 Attribute::get(F.getContext(), Attribute::AllocKind, uint64_t(K))); 250 return true; 251 } 252 253 bool llvm::inferNonMandatoryLibFuncAttrs(Module *M, StringRef Name, 254 const TargetLibraryInfo &TLI) { 255 Function *F = M->getFunction(Name); 256 if (!F) 257 return false; 258 return inferNonMandatoryLibFuncAttrs(*F, TLI); 259 } 260 261 bool llvm::inferNonMandatoryLibFuncAttrs(Function &F, 262 const TargetLibraryInfo &TLI) { 263 LibFunc TheLibFunc; 264 if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc))) 265 return false; 266 267 bool Changed = false; 268 269 if (F.getParent() != nullptr && F.getParent()->getRtLibUseGOT()) 270 Changed |= setNonLazyBind(F); 271 272 switch (TheLibFunc) { 273 case LibFunc_strlen: 274 case LibFunc_strnlen: 275 case LibFunc_wcslen: 276 Changed |= setOnlyReadsMemory(F); 277 Changed |= setDoesNotThrow(F); 278 Changed |= setOnlyAccessesArgMemory(F); 279 Changed |= setWillReturn(F); 280 Changed |= setDoesNotCapture(F, 0); 281 break; 282 case LibFunc_strchr: 283 case LibFunc_strrchr: 284 Changed |= setOnlyAccessesArgMemory(F); 285 Changed |= setOnlyReadsMemory(F); 286 Changed |= setDoesNotThrow(F); 287 Changed |= setWillReturn(F); 288 break; 289 case LibFunc_strtol: 290 case LibFunc_strtod: 291 case LibFunc_strtof: 292 case LibFunc_strtoul: 293 case LibFunc_strtoll: 294 case LibFunc_strtold: 295 case LibFunc_strtoull: 296 Changed |= setDoesNotThrow(F); 297 Changed |= setWillReturn(F); 298 Changed |= setDoesNotCapture(F, 1); 299 Changed |= setOnlyReadsMemory(F, 0); 300 break; 301 case LibFunc_strcat: 302 case LibFunc_strncat: 303 Changed |= setOnlyAccessesArgMemory(F); 304 Changed |= setDoesNotThrow(F); 305 Changed |= setWillReturn(F); 306 Changed |= setReturnedArg(F, 0); 307 Changed |= setDoesNotCapture(F, 1); 308 Changed |= setOnlyReadsMemory(F, 1); 309 Changed |= setDoesNotAlias(F, 0); 310 Changed |= setDoesNotAlias(F, 1); 311 break; 312 case LibFunc_strcpy: 313 case LibFunc_strncpy: 314 Changed |= setReturnedArg(F, 0); 315 [[fallthrough]]; 316 case LibFunc_stpcpy: 317 case LibFunc_stpncpy: 318 Changed |= setOnlyAccessesArgMemory(F); 319 Changed |= setDoesNotThrow(F); 320 Changed |= setWillReturn(F); 321 Changed |= setDoesNotCapture(F, 1); 322 Changed |= setOnlyWritesMemory(F, 0); 323 Changed |= setOnlyReadsMemory(F, 1); 324 Changed |= setDoesNotAlias(F, 0); 325 Changed |= setDoesNotAlias(F, 1); 326 break; 327 case LibFunc_strxfrm: 328 Changed |= setDoesNotThrow(F); 329 Changed |= setWillReturn(F); 330 Changed |= setDoesNotCapture(F, 0); 331 Changed |= setDoesNotCapture(F, 1); 332 Changed |= setOnlyReadsMemory(F, 1); 333 break; 334 case LibFunc_strcmp: // 0,1 335 case LibFunc_strspn: // 0,1 336 case LibFunc_strncmp: // 0,1 337 case LibFunc_strcspn: // 0,1 338 Changed |= setDoesNotThrow(F); 339 Changed |= setOnlyAccessesArgMemory(F); 340 Changed |= setWillReturn(F); 341 Changed |= setOnlyReadsMemory(F); 342 Changed |= setDoesNotCapture(F, 0); 343 Changed |= setDoesNotCapture(F, 1); 344 break; 345 case LibFunc_strcoll: 346 case LibFunc_strcasecmp: // 0,1 347 case LibFunc_strncasecmp: // 348 // Those functions may depend on the locale, which may be accessed through 349 // global memory. 350 Changed |= setOnlyReadsMemory(F); 351 Changed |= setDoesNotThrow(F); 352 Changed |= setWillReturn(F); 353 Changed |= setDoesNotCapture(F, 0); 354 Changed |= setDoesNotCapture(F, 1); 355 break; 356 case LibFunc_strstr: 357 case LibFunc_strpbrk: 358 Changed |= setOnlyAccessesArgMemory(F); 359 Changed |= setOnlyReadsMemory(F); 360 Changed |= setDoesNotThrow(F); 361 Changed |= setWillReturn(F); 362 Changed |= setDoesNotCapture(F, 1); 363 break; 364 case LibFunc_strtok: 365 case LibFunc_strtok_r: 366 Changed |= setDoesNotThrow(F); 367 Changed |= setWillReturn(F); 368 Changed |= setDoesNotCapture(F, 1); 369 Changed |= setOnlyReadsMemory(F, 1); 370 break; 371 case LibFunc_scanf: 372 Changed |= setRetAndArgsNoUndef(F); 373 Changed |= setDoesNotThrow(F); 374 Changed |= setDoesNotCapture(F, 0); 375 Changed |= setOnlyReadsMemory(F, 0); 376 break; 377 case LibFunc_setbuf: 378 case LibFunc_setvbuf: 379 Changed |= setRetAndArgsNoUndef(F); 380 Changed |= setDoesNotThrow(F); 381 Changed |= setDoesNotCapture(F, 0); 382 break; 383 case LibFunc_strndup: 384 Changed |= setArgNoUndef(F, 1); 385 [[fallthrough]]; 386 case LibFunc_strdup: 387 Changed |= setAllocFamily(F, "malloc"); 388 Changed |= setOnlyAccessesInaccessibleMemOrArgMem(F); 389 Changed |= setDoesNotThrow(F); 390 Changed |= setRetDoesNotAlias(F); 391 Changed |= setWillReturn(F); 392 Changed |= setDoesNotCapture(F, 0); 393 Changed |= setOnlyReadsMemory(F, 0); 394 break; 395 case LibFunc_stat: 396 case LibFunc_statvfs: 397 Changed |= setRetAndArgsNoUndef(F); 398 Changed |= setDoesNotThrow(F); 399 Changed |= setDoesNotCapture(F, 0); 400 Changed |= setDoesNotCapture(F, 1); 401 Changed |= setOnlyReadsMemory(F, 0); 402 break; 403 case LibFunc_sscanf: 404 Changed |= setRetAndArgsNoUndef(F); 405 Changed |= setDoesNotThrow(F); 406 Changed |= setDoesNotCapture(F, 0); 407 Changed |= setDoesNotCapture(F, 1); 408 Changed |= setOnlyReadsMemory(F, 0); 409 Changed |= setOnlyReadsMemory(F, 1); 410 break; 411 case LibFunc_sprintf: 412 Changed |= setRetAndArgsNoUndef(F); 413 Changed |= setDoesNotThrow(F); 414 Changed |= setDoesNotCapture(F, 0); 415 Changed |= setDoesNotAlias(F, 0); 416 Changed |= setOnlyWritesMemory(F, 0); 417 Changed |= setDoesNotCapture(F, 1); 418 Changed |= setOnlyReadsMemory(F, 1); 419 break; 420 case LibFunc_snprintf: 421 Changed |= setRetAndArgsNoUndef(F); 422 Changed |= setDoesNotThrow(F); 423 Changed |= setDoesNotCapture(F, 0); 424 Changed |= setDoesNotAlias(F, 0); 425 Changed |= setOnlyWritesMemory(F, 0); 426 Changed |= setDoesNotCapture(F, 2); 427 Changed |= setOnlyReadsMemory(F, 2); 428 break; 429 case LibFunc_setitimer: 430 Changed |= setRetAndArgsNoUndef(F); 431 Changed |= setDoesNotThrow(F); 432 Changed |= setWillReturn(F); 433 Changed |= setDoesNotCapture(F, 1); 434 Changed |= setDoesNotCapture(F, 2); 435 Changed |= setOnlyReadsMemory(F, 1); 436 break; 437 case LibFunc_system: 438 // May throw; "system" is a valid pthread cancellation point. 439 Changed |= setRetAndArgsNoUndef(F); 440 Changed |= setDoesNotCapture(F, 0); 441 Changed |= setOnlyReadsMemory(F, 0); 442 break; 443 case LibFunc_aligned_alloc: 444 Changed |= setAlignedAllocParam(F, 0); 445 Changed |= setAllocSize(F, 1, std::nullopt); 446 Changed |= setAllocKind(F, AllocFnKind::Alloc | AllocFnKind::Uninitialized | AllocFnKind::Aligned); 447 [[fallthrough]]; 448 case LibFunc_valloc: 449 case LibFunc_malloc: 450 case LibFunc_vec_malloc: 451 Changed |= setAllocFamily(F, TheLibFunc == LibFunc_vec_malloc ? "vec_malloc" 452 : "malloc"); 453 Changed |= setAllocKind(F, AllocFnKind::Alloc | AllocFnKind::Uninitialized); 454 Changed |= setAllocSize(F, 0, std::nullopt); 455 Changed |= setOnlyAccessesInaccessibleMemory(F); 456 Changed |= setRetAndArgsNoUndef(F); 457 Changed |= setDoesNotThrow(F); 458 Changed |= setRetDoesNotAlias(F); 459 Changed |= setWillReturn(F); 460 break; 461 case LibFunc_memcmp: 462 Changed |= setOnlyAccessesArgMemory(F); 463 Changed |= setOnlyReadsMemory(F); 464 Changed |= setDoesNotThrow(F); 465 Changed |= setWillReturn(F); 466 Changed |= setDoesNotCapture(F, 0); 467 Changed |= setDoesNotCapture(F, 1); 468 break; 469 case LibFunc_memchr: 470 case LibFunc_memrchr: 471 Changed |= setDoesNotThrow(F); 472 Changed |= setOnlyAccessesArgMemory(F); 473 Changed |= setOnlyReadsMemory(F); 474 Changed |= setWillReturn(F); 475 break; 476 case LibFunc_modf: 477 case LibFunc_modff: 478 case LibFunc_modfl: 479 Changed |= setDoesNotThrow(F); 480 Changed |= setWillReturn(F); 481 Changed |= setDoesNotCapture(F, 1); 482 break; 483 case LibFunc_memcpy: 484 Changed |= setDoesNotThrow(F); 485 Changed |= setOnlyAccessesArgMemory(F); 486 Changed |= setWillReturn(F); 487 Changed |= setDoesNotAlias(F, 0); 488 Changed |= setReturnedArg(F, 0); 489 Changed |= setOnlyWritesMemory(F, 0); 490 Changed |= setDoesNotAlias(F, 1); 491 Changed |= setDoesNotCapture(F, 1); 492 Changed |= setOnlyReadsMemory(F, 1); 493 break; 494 case LibFunc_memmove: 495 Changed |= setDoesNotThrow(F); 496 Changed |= setOnlyAccessesArgMemory(F); 497 Changed |= setWillReturn(F); 498 Changed |= setReturnedArg(F, 0); 499 Changed |= setOnlyWritesMemory(F, 0); 500 Changed |= setDoesNotCapture(F, 1); 501 Changed |= setOnlyReadsMemory(F, 1); 502 break; 503 case LibFunc_mempcpy: 504 case LibFunc_memccpy: 505 Changed |= setWillReturn(F); 506 [[fallthrough]]; 507 case LibFunc_memcpy_chk: 508 Changed |= setDoesNotThrow(F); 509 Changed |= setOnlyAccessesArgMemory(F); 510 Changed |= setDoesNotAlias(F, 0); 511 Changed |= setOnlyWritesMemory(F, 0); 512 Changed |= setDoesNotAlias(F, 1); 513 Changed |= setDoesNotCapture(F, 1); 514 Changed |= setOnlyReadsMemory(F, 1); 515 break; 516 case LibFunc_memalign: 517 Changed |= setAllocFamily(F, "malloc"); 518 Changed |= setAllocKind(F, AllocFnKind::Alloc | AllocFnKind::Aligned | 519 AllocFnKind::Uninitialized); 520 Changed |= setAllocSize(F, 1, std::nullopt); 521 Changed |= setAlignedAllocParam(F, 0); 522 Changed |= setOnlyAccessesInaccessibleMemory(F); 523 Changed |= setRetNoUndef(F); 524 Changed |= setDoesNotThrow(F); 525 Changed |= setRetDoesNotAlias(F); 526 Changed |= setWillReturn(F); 527 break; 528 case LibFunc_mkdir: 529 Changed |= setRetAndArgsNoUndef(F); 530 Changed |= setDoesNotThrow(F); 531 Changed |= setDoesNotCapture(F, 0); 532 Changed |= setOnlyReadsMemory(F, 0); 533 break; 534 case LibFunc_mktime: 535 Changed |= setRetAndArgsNoUndef(F); 536 Changed |= setDoesNotThrow(F); 537 Changed |= setWillReturn(F); 538 Changed |= setDoesNotCapture(F, 0); 539 break; 540 case LibFunc_realloc: 541 case LibFunc_reallocf: 542 case LibFunc_vec_realloc: 543 Changed |= setAllocFamily( 544 F, TheLibFunc == LibFunc_vec_realloc ? "vec_malloc" : "malloc"); 545 Changed |= setAllocKind(F, AllocFnKind::Realloc); 546 Changed |= setAllocatedPointerParam(F, 0); 547 Changed |= setAllocSize(F, 1, std::nullopt); 548 Changed |= setOnlyAccessesInaccessibleMemOrArgMem(F); 549 Changed |= setRetNoUndef(F); 550 Changed |= setDoesNotThrow(F); 551 Changed |= setRetDoesNotAlias(F); 552 Changed |= setWillReturn(F); 553 Changed |= setDoesNotCapture(F, 0); 554 Changed |= setArgNoUndef(F, 1); 555 break; 556 case LibFunc_read: 557 // May throw; "read" is a valid pthread cancellation point. 558 Changed |= setRetAndArgsNoUndef(F); 559 Changed |= setDoesNotCapture(F, 1); 560 break; 561 case LibFunc_rewind: 562 Changed |= setRetAndArgsNoUndef(F); 563 Changed |= setDoesNotThrow(F); 564 Changed |= setDoesNotCapture(F, 0); 565 break; 566 case LibFunc_rmdir: 567 case LibFunc_remove: 568 case LibFunc_realpath: 569 Changed |= setRetAndArgsNoUndef(F); 570 Changed |= setDoesNotThrow(F); 571 Changed |= setDoesNotCapture(F, 0); 572 Changed |= setOnlyReadsMemory(F, 0); 573 break; 574 case LibFunc_rename: 575 Changed |= setRetAndArgsNoUndef(F); 576 Changed |= setDoesNotThrow(F); 577 Changed |= setDoesNotCapture(F, 0); 578 Changed |= setDoesNotCapture(F, 1); 579 Changed |= setOnlyReadsMemory(F, 0); 580 Changed |= setOnlyReadsMemory(F, 1); 581 break; 582 case LibFunc_readlink: 583 Changed |= setRetAndArgsNoUndef(F); 584 Changed |= setDoesNotThrow(F); 585 Changed |= setDoesNotCapture(F, 0); 586 Changed |= setDoesNotCapture(F, 1); 587 Changed |= setOnlyReadsMemory(F, 0); 588 break; 589 case LibFunc_write: 590 // May throw; "write" is a valid pthread cancellation point. 591 Changed |= setRetAndArgsNoUndef(F); 592 Changed |= setDoesNotCapture(F, 1); 593 Changed |= setOnlyReadsMemory(F, 1); 594 break; 595 case LibFunc_bcopy: 596 Changed |= setDoesNotThrow(F); 597 Changed |= setOnlyAccessesArgMemory(F); 598 Changed |= setWillReturn(F); 599 Changed |= setDoesNotCapture(F, 0); 600 Changed |= setOnlyReadsMemory(F, 0); 601 Changed |= setOnlyWritesMemory(F, 1); 602 Changed |= setDoesNotCapture(F, 1); 603 break; 604 case LibFunc_bcmp: 605 Changed |= setDoesNotThrow(F); 606 Changed |= setOnlyAccessesArgMemory(F); 607 Changed |= setOnlyReadsMemory(F); 608 Changed |= setWillReturn(F); 609 Changed |= setDoesNotCapture(F, 0); 610 Changed |= setDoesNotCapture(F, 1); 611 break; 612 case LibFunc_bzero: 613 Changed |= setDoesNotThrow(F); 614 Changed |= setOnlyAccessesArgMemory(F); 615 Changed |= setWillReturn(F); 616 Changed |= setDoesNotCapture(F, 0); 617 Changed |= setOnlyWritesMemory(F, 0); 618 break; 619 case LibFunc_calloc: 620 case LibFunc_vec_calloc: 621 Changed |= setAllocFamily(F, TheLibFunc == LibFunc_vec_calloc ? "vec_malloc" 622 : "malloc"); 623 Changed |= setAllocKind(F, AllocFnKind::Alloc | AllocFnKind::Zeroed); 624 Changed |= setAllocSize(F, 0, 1); 625 Changed |= setOnlyAccessesInaccessibleMemory(F); 626 Changed |= setRetAndArgsNoUndef(F); 627 Changed |= setDoesNotThrow(F); 628 Changed |= setRetDoesNotAlias(F); 629 Changed |= setWillReturn(F); 630 break; 631 case LibFunc_chmod: 632 case LibFunc_chown: 633 Changed |= setRetAndArgsNoUndef(F); 634 Changed |= setDoesNotThrow(F); 635 Changed |= setDoesNotCapture(F, 0); 636 Changed |= setOnlyReadsMemory(F, 0); 637 break; 638 case LibFunc_ctermid: 639 case LibFunc_clearerr: 640 case LibFunc_closedir: 641 Changed |= setRetAndArgsNoUndef(F); 642 Changed |= setDoesNotThrow(F); 643 Changed |= setDoesNotCapture(F, 0); 644 break; 645 case LibFunc_atoi: 646 case LibFunc_atol: 647 case LibFunc_atof: 648 case LibFunc_atoll: 649 Changed |= setDoesNotThrow(F); 650 Changed |= setOnlyReadsMemory(F); 651 Changed |= setWillReturn(F); 652 Changed |= setDoesNotCapture(F, 0); 653 break; 654 case LibFunc_access: 655 Changed |= setRetAndArgsNoUndef(F); 656 Changed |= setDoesNotThrow(F); 657 Changed |= setDoesNotCapture(F, 0); 658 Changed |= setOnlyReadsMemory(F, 0); 659 break; 660 case LibFunc_fopen: 661 Changed |= setRetAndArgsNoUndef(F); 662 Changed |= setDoesNotThrow(F); 663 Changed |= setRetDoesNotAlias(F); 664 Changed |= setDoesNotCapture(F, 0); 665 Changed |= setDoesNotCapture(F, 1); 666 Changed |= setOnlyReadsMemory(F, 0); 667 Changed |= setOnlyReadsMemory(F, 1); 668 break; 669 case LibFunc_fdopen: 670 Changed |= setRetAndArgsNoUndef(F); 671 Changed |= setDoesNotThrow(F); 672 Changed |= setRetDoesNotAlias(F); 673 Changed |= setDoesNotCapture(F, 1); 674 Changed |= setOnlyReadsMemory(F, 1); 675 break; 676 case LibFunc_feof: 677 Changed |= setRetAndArgsNoUndef(F); 678 Changed |= setDoesNotThrow(F); 679 Changed |= setDoesNotCapture(F, 0); 680 break; 681 case LibFunc_free: 682 case LibFunc_vec_free: 683 Changed |= setAllocFamily(F, TheLibFunc == LibFunc_vec_free ? "vec_malloc" 684 : "malloc"); 685 Changed |= setAllocKind(F, AllocFnKind::Free); 686 Changed |= setAllocatedPointerParam(F, 0); 687 Changed |= setOnlyAccessesInaccessibleMemOrArgMem(F); 688 Changed |= setArgsNoUndef(F); 689 Changed |= setDoesNotThrow(F); 690 Changed |= setWillReturn(F); 691 Changed |= setDoesNotCapture(F, 0); 692 break; 693 case LibFunc_fseek: 694 case LibFunc_ftell: 695 case LibFunc_fgetc: 696 case LibFunc_fgetc_unlocked: 697 case LibFunc_fseeko: 698 case LibFunc_ftello: 699 case LibFunc_fileno: 700 case LibFunc_fflush: 701 case LibFunc_fclose: 702 case LibFunc_fsetpos: 703 case LibFunc_flockfile: 704 case LibFunc_funlockfile: 705 case LibFunc_ftrylockfile: 706 Changed |= setRetAndArgsNoUndef(F); 707 Changed |= setDoesNotThrow(F); 708 Changed |= setDoesNotCapture(F, 0); 709 break; 710 case LibFunc_ferror: 711 Changed |= setRetAndArgsNoUndef(F); 712 Changed |= setDoesNotThrow(F); 713 Changed |= setDoesNotCapture(F, 0); 714 Changed |= setOnlyReadsMemory(F); 715 break; 716 case LibFunc_fputc: 717 case LibFunc_fputc_unlocked: 718 case LibFunc_fstat: 719 Changed |= setRetAndArgsNoUndef(F); 720 Changed |= setDoesNotThrow(F); 721 Changed |= setDoesNotCapture(F, 1); 722 break; 723 case LibFunc_frexp: 724 case LibFunc_frexpf: 725 case LibFunc_frexpl: 726 Changed |= setDoesNotThrow(F); 727 Changed |= setWillReturn(F); 728 Changed |= setDoesNotCapture(F, 1); 729 break; 730 case LibFunc_fstatvfs: 731 Changed |= setRetAndArgsNoUndef(F); 732 Changed |= setDoesNotThrow(F); 733 Changed |= setDoesNotCapture(F, 1); 734 break; 735 case LibFunc_fgets: 736 case LibFunc_fgets_unlocked: 737 Changed |= setRetAndArgsNoUndef(F); 738 Changed |= setDoesNotThrow(F); 739 Changed |= setDoesNotCapture(F, 2); 740 break; 741 case LibFunc_fread: 742 case LibFunc_fread_unlocked: 743 Changed |= setRetAndArgsNoUndef(F); 744 Changed |= setDoesNotThrow(F); 745 Changed |= setDoesNotCapture(F, 0); 746 Changed |= setDoesNotCapture(F, 3); 747 break; 748 case LibFunc_fwrite: 749 case LibFunc_fwrite_unlocked: 750 Changed |= setRetAndArgsNoUndef(F); 751 Changed |= setDoesNotThrow(F); 752 Changed |= setDoesNotCapture(F, 0); 753 Changed |= setDoesNotCapture(F, 3); 754 // FIXME: readonly #1? 755 break; 756 case LibFunc_fputs: 757 case LibFunc_fputs_unlocked: 758 Changed |= setRetAndArgsNoUndef(F); 759 Changed |= setDoesNotThrow(F); 760 Changed |= setDoesNotCapture(F, 0); 761 Changed |= setDoesNotCapture(F, 1); 762 Changed |= setOnlyReadsMemory(F, 0); 763 break; 764 case LibFunc_fscanf: 765 case LibFunc_fprintf: 766 Changed |= setRetAndArgsNoUndef(F); 767 Changed |= setDoesNotThrow(F); 768 Changed |= setDoesNotCapture(F, 0); 769 Changed |= setDoesNotCapture(F, 1); 770 Changed |= setOnlyReadsMemory(F, 1); 771 break; 772 case LibFunc_fgetpos: 773 Changed |= setRetAndArgsNoUndef(F); 774 Changed |= setDoesNotThrow(F); 775 Changed |= setDoesNotCapture(F, 0); 776 Changed |= setDoesNotCapture(F, 1); 777 break; 778 case LibFunc_getc: 779 Changed |= setRetAndArgsNoUndef(F); 780 Changed |= setDoesNotThrow(F); 781 Changed |= setDoesNotCapture(F, 0); 782 break; 783 case LibFunc_getlogin_r: 784 Changed |= setRetAndArgsNoUndef(F); 785 Changed |= setDoesNotThrow(F); 786 Changed |= setDoesNotCapture(F, 0); 787 break; 788 case LibFunc_getc_unlocked: 789 Changed |= setRetAndArgsNoUndef(F); 790 Changed |= setDoesNotThrow(F); 791 Changed |= setDoesNotCapture(F, 0); 792 break; 793 case LibFunc_getenv: 794 Changed |= setRetAndArgsNoUndef(F); 795 Changed |= setDoesNotThrow(F); 796 Changed |= setOnlyReadsMemory(F); 797 Changed |= setDoesNotCapture(F, 0); 798 break; 799 case LibFunc_gets: 800 case LibFunc_getchar: 801 case LibFunc_getchar_unlocked: 802 Changed |= setRetAndArgsNoUndef(F); 803 Changed |= setDoesNotThrow(F); 804 break; 805 case LibFunc_getitimer: 806 Changed |= setRetAndArgsNoUndef(F); 807 Changed |= setDoesNotThrow(F); 808 Changed |= setDoesNotCapture(F, 1); 809 break; 810 case LibFunc_getpwnam: 811 Changed |= setRetAndArgsNoUndef(F); 812 Changed |= setDoesNotThrow(F); 813 Changed |= setDoesNotCapture(F, 0); 814 Changed |= setOnlyReadsMemory(F, 0); 815 break; 816 case LibFunc_ungetc: 817 Changed |= setRetAndArgsNoUndef(F); 818 Changed |= setDoesNotThrow(F); 819 Changed |= setDoesNotCapture(F, 1); 820 break; 821 case LibFunc_uname: 822 Changed |= setRetAndArgsNoUndef(F); 823 Changed |= setDoesNotThrow(F); 824 Changed |= setDoesNotCapture(F, 0); 825 break; 826 case LibFunc_unlink: 827 Changed |= setRetAndArgsNoUndef(F); 828 Changed |= setDoesNotThrow(F); 829 Changed |= setDoesNotCapture(F, 0); 830 Changed |= setOnlyReadsMemory(F, 0); 831 break; 832 case LibFunc_unsetenv: 833 Changed |= setRetAndArgsNoUndef(F); 834 Changed |= setDoesNotThrow(F); 835 Changed |= setDoesNotCapture(F, 0); 836 Changed |= setOnlyReadsMemory(F, 0); 837 break; 838 case LibFunc_utime: 839 case LibFunc_utimes: 840 Changed |= setRetAndArgsNoUndef(F); 841 Changed |= setDoesNotThrow(F); 842 Changed |= setDoesNotCapture(F, 0); 843 Changed |= setDoesNotCapture(F, 1); 844 Changed |= setOnlyReadsMemory(F, 0); 845 Changed |= setOnlyReadsMemory(F, 1); 846 break; 847 case LibFunc_putc: 848 case LibFunc_putc_unlocked: 849 Changed |= setRetAndArgsNoUndef(F); 850 Changed |= setDoesNotThrow(F); 851 Changed |= setDoesNotCapture(F, 1); 852 break; 853 case LibFunc_puts: 854 case LibFunc_printf: 855 case LibFunc_perror: 856 Changed |= setRetAndArgsNoUndef(F); 857 Changed |= setDoesNotThrow(F); 858 Changed |= setDoesNotCapture(F, 0); 859 Changed |= setOnlyReadsMemory(F, 0); 860 break; 861 case LibFunc_pread: 862 // May throw; "pread" is a valid pthread cancellation point. 863 Changed |= setRetAndArgsNoUndef(F); 864 Changed |= setDoesNotCapture(F, 1); 865 break; 866 case LibFunc_pwrite: 867 // May throw; "pwrite" is a valid pthread cancellation point. 868 Changed |= setRetAndArgsNoUndef(F); 869 Changed |= setDoesNotCapture(F, 1); 870 Changed |= setOnlyReadsMemory(F, 1); 871 break; 872 case LibFunc_putchar: 873 case LibFunc_putchar_unlocked: 874 Changed |= setRetAndArgsNoUndef(F); 875 Changed |= setDoesNotThrow(F); 876 break; 877 case LibFunc_popen: 878 Changed |= setRetAndArgsNoUndef(F); 879 Changed |= setDoesNotThrow(F); 880 Changed |= setRetDoesNotAlias(F); 881 Changed |= setDoesNotCapture(F, 0); 882 Changed |= setDoesNotCapture(F, 1); 883 Changed |= setOnlyReadsMemory(F, 0); 884 Changed |= setOnlyReadsMemory(F, 1); 885 break; 886 case LibFunc_pclose: 887 Changed |= setRetAndArgsNoUndef(F); 888 Changed |= setDoesNotThrow(F); 889 Changed |= setDoesNotCapture(F, 0); 890 break; 891 case LibFunc_vscanf: 892 Changed |= setRetAndArgsNoUndef(F); 893 Changed |= setDoesNotThrow(F); 894 Changed |= setDoesNotCapture(F, 0); 895 Changed |= setOnlyReadsMemory(F, 0); 896 break; 897 case LibFunc_vsscanf: 898 Changed |= setRetAndArgsNoUndef(F); 899 Changed |= setDoesNotThrow(F); 900 Changed |= setDoesNotCapture(F, 0); 901 Changed |= setDoesNotCapture(F, 1); 902 Changed |= setOnlyReadsMemory(F, 0); 903 Changed |= setOnlyReadsMemory(F, 1); 904 break; 905 case LibFunc_vfscanf: 906 Changed |= setRetAndArgsNoUndef(F); 907 Changed |= setDoesNotThrow(F); 908 Changed |= setDoesNotCapture(F, 0); 909 Changed |= setDoesNotCapture(F, 1); 910 Changed |= setOnlyReadsMemory(F, 1); 911 break; 912 case LibFunc_vprintf: 913 Changed |= setRetAndArgsNoUndef(F); 914 Changed |= setDoesNotThrow(F); 915 Changed |= setDoesNotCapture(F, 0); 916 Changed |= setOnlyReadsMemory(F, 0); 917 break; 918 case LibFunc_vfprintf: 919 case LibFunc_vsprintf: 920 Changed |= setRetAndArgsNoUndef(F); 921 Changed |= setDoesNotThrow(F); 922 Changed |= setDoesNotCapture(F, 0); 923 Changed |= setDoesNotCapture(F, 1); 924 Changed |= setOnlyReadsMemory(F, 1); 925 break; 926 case LibFunc_vsnprintf: 927 Changed |= setRetAndArgsNoUndef(F); 928 Changed |= setDoesNotThrow(F); 929 Changed |= setDoesNotCapture(F, 0); 930 Changed |= setDoesNotCapture(F, 2); 931 Changed |= setOnlyReadsMemory(F, 2); 932 break; 933 case LibFunc_open: 934 // May throw; "open" is a valid pthread cancellation point. 935 Changed |= setRetAndArgsNoUndef(F); 936 Changed |= setDoesNotCapture(F, 0); 937 Changed |= setOnlyReadsMemory(F, 0); 938 break; 939 case LibFunc_opendir: 940 Changed |= setRetAndArgsNoUndef(F); 941 Changed |= setDoesNotThrow(F); 942 Changed |= setRetDoesNotAlias(F); 943 Changed |= setDoesNotCapture(F, 0); 944 Changed |= setOnlyReadsMemory(F, 0); 945 break; 946 case LibFunc_tmpfile: 947 Changed |= setRetAndArgsNoUndef(F); 948 Changed |= setDoesNotThrow(F); 949 Changed |= setRetDoesNotAlias(F); 950 break; 951 case LibFunc_times: 952 Changed |= setRetAndArgsNoUndef(F); 953 Changed |= setDoesNotThrow(F); 954 Changed |= setDoesNotCapture(F, 0); 955 break; 956 case LibFunc_htonl: 957 case LibFunc_htons: 958 case LibFunc_ntohl: 959 case LibFunc_ntohs: 960 Changed |= setDoesNotThrow(F); 961 Changed |= setDoesNotAccessMemory(F); 962 break; 963 case LibFunc_lstat: 964 Changed |= setRetAndArgsNoUndef(F); 965 Changed |= setDoesNotThrow(F); 966 Changed |= setDoesNotCapture(F, 0); 967 Changed |= setDoesNotCapture(F, 1); 968 Changed |= setOnlyReadsMemory(F, 0); 969 break; 970 case LibFunc_lchown: 971 Changed |= setRetAndArgsNoUndef(F); 972 Changed |= setDoesNotThrow(F); 973 Changed |= setDoesNotCapture(F, 0); 974 Changed |= setOnlyReadsMemory(F, 0); 975 break; 976 case LibFunc_qsort: 977 // May throw; places call through function pointer. 978 // Cannot give undef pointer/size 979 Changed |= setRetAndArgsNoUndef(F); 980 Changed |= setDoesNotCapture(F, 3); 981 break; 982 case LibFunc_dunder_strndup: 983 Changed |= setArgNoUndef(F, 1); 984 [[fallthrough]]; 985 case LibFunc_dunder_strdup: 986 Changed |= setDoesNotThrow(F); 987 Changed |= setRetDoesNotAlias(F); 988 Changed |= setWillReturn(F); 989 Changed |= setDoesNotCapture(F, 0); 990 Changed |= setOnlyReadsMemory(F, 0); 991 break; 992 case LibFunc_dunder_strtok_r: 993 Changed |= setDoesNotThrow(F); 994 Changed |= setDoesNotCapture(F, 1); 995 Changed |= setOnlyReadsMemory(F, 1); 996 break; 997 case LibFunc_under_IO_getc: 998 Changed |= setRetAndArgsNoUndef(F); 999 Changed |= setDoesNotThrow(F); 1000 Changed |= setDoesNotCapture(F, 0); 1001 break; 1002 case LibFunc_under_IO_putc: 1003 Changed |= setRetAndArgsNoUndef(F); 1004 Changed |= setDoesNotThrow(F); 1005 Changed |= setDoesNotCapture(F, 1); 1006 break; 1007 case LibFunc_dunder_isoc99_scanf: 1008 Changed |= setRetAndArgsNoUndef(F); 1009 Changed |= setDoesNotThrow(F); 1010 Changed |= setDoesNotCapture(F, 0); 1011 Changed |= setOnlyReadsMemory(F, 0); 1012 break; 1013 case LibFunc_stat64: 1014 case LibFunc_lstat64: 1015 case LibFunc_statvfs64: 1016 Changed |= setRetAndArgsNoUndef(F); 1017 Changed |= setDoesNotThrow(F); 1018 Changed |= setDoesNotCapture(F, 0); 1019 Changed |= setDoesNotCapture(F, 1); 1020 Changed |= setOnlyReadsMemory(F, 0); 1021 break; 1022 case LibFunc_dunder_isoc99_sscanf: 1023 Changed |= setRetAndArgsNoUndef(F); 1024 Changed |= setDoesNotThrow(F); 1025 Changed |= setDoesNotCapture(F, 0); 1026 Changed |= setDoesNotCapture(F, 1); 1027 Changed |= setOnlyReadsMemory(F, 0); 1028 Changed |= setOnlyReadsMemory(F, 1); 1029 break; 1030 case LibFunc_fopen64: 1031 Changed |= setRetAndArgsNoUndef(F); 1032 Changed |= setDoesNotThrow(F); 1033 Changed |= setRetDoesNotAlias(F); 1034 Changed |= setDoesNotCapture(F, 0); 1035 Changed |= setDoesNotCapture(F, 1); 1036 Changed |= setOnlyReadsMemory(F, 0); 1037 Changed |= setOnlyReadsMemory(F, 1); 1038 break; 1039 case LibFunc_fseeko64: 1040 case LibFunc_ftello64: 1041 Changed |= setRetAndArgsNoUndef(F); 1042 Changed |= setDoesNotThrow(F); 1043 Changed |= setDoesNotCapture(F, 0); 1044 break; 1045 case LibFunc_tmpfile64: 1046 Changed |= setRetAndArgsNoUndef(F); 1047 Changed |= setDoesNotThrow(F); 1048 Changed |= setRetDoesNotAlias(F); 1049 break; 1050 case LibFunc_fstat64: 1051 case LibFunc_fstatvfs64: 1052 Changed |= setRetAndArgsNoUndef(F); 1053 Changed |= setDoesNotThrow(F); 1054 Changed |= setDoesNotCapture(F, 1); 1055 break; 1056 case LibFunc_open64: 1057 // May throw; "open" is a valid pthread cancellation point. 1058 Changed |= setRetAndArgsNoUndef(F); 1059 Changed |= setDoesNotCapture(F, 0); 1060 Changed |= setOnlyReadsMemory(F, 0); 1061 break; 1062 case LibFunc_gettimeofday: 1063 // Currently some platforms have the restrict keyword on the arguments to 1064 // gettimeofday. To be conservative, do not add noalias to gettimeofday's 1065 // arguments. 1066 Changed |= setRetAndArgsNoUndef(F); 1067 Changed |= setDoesNotThrow(F); 1068 Changed |= setDoesNotCapture(F, 0); 1069 Changed |= setDoesNotCapture(F, 1); 1070 break; 1071 case LibFunc_memset_pattern4: 1072 case LibFunc_memset_pattern8: 1073 case LibFunc_memset_pattern16: 1074 Changed |= setDoesNotCapture(F, 0); 1075 Changed |= setDoesNotCapture(F, 1); 1076 Changed |= setOnlyReadsMemory(F, 1); 1077 [[fallthrough]]; 1078 case LibFunc_memset: 1079 Changed |= setWillReturn(F); 1080 [[fallthrough]]; 1081 case LibFunc_memset_chk: 1082 Changed |= setOnlyAccessesArgMemory(F); 1083 Changed |= setOnlyWritesMemory(F, 0); 1084 Changed |= setDoesNotThrow(F); 1085 break; 1086 // int __nvvm_reflect(const char *) 1087 case LibFunc_nvvm_reflect: 1088 Changed |= setRetAndArgsNoUndef(F); 1089 Changed |= setDoesNotAccessMemory(F); 1090 Changed |= setDoesNotThrow(F); 1091 break; 1092 case LibFunc_ldexp: 1093 case LibFunc_ldexpf: 1094 case LibFunc_ldexpl: 1095 Changed |= setWillReturn(F); 1096 break; 1097 case LibFunc_abs: 1098 case LibFunc_acos: 1099 case LibFunc_acosf: 1100 case LibFunc_acosh: 1101 case LibFunc_acoshf: 1102 case LibFunc_acoshl: 1103 case LibFunc_acosl: 1104 case LibFunc_asin: 1105 case LibFunc_asinf: 1106 case LibFunc_asinh: 1107 case LibFunc_asinhf: 1108 case LibFunc_asinhl: 1109 case LibFunc_asinl: 1110 case LibFunc_atan: 1111 case LibFunc_atan2: 1112 case LibFunc_atan2f: 1113 case LibFunc_atan2l: 1114 case LibFunc_atanf: 1115 case LibFunc_atanh: 1116 case LibFunc_atanhf: 1117 case LibFunc_atanhl: 1118 case LibFunc_atanl: 1119 case LibFunc_cbrt: 1120 case LibFunc_cbrtf: 1121 case LibFunc_cbrtl: 1122 case LibFunc_ceil: 1123 case LibFunc_ceilf: 1124 case LibFunc_ceill: 1125 case LibFunc_copysign: 1126 case LibFunc_copysignf: 1127 case LibFunc_copysignl: 1128 case LibFunc_cos: 1129 case LibFunc_cosh: 1130 case LibFunc_coshf: 1131 case LibFunc_coshl: 1132 case LibFunc_cosf: 1133 case LibFunc_cosl: 1134 case LibFunc_cospi: 1135 case LibFunc_cospif: 1136 case LibFunc_exp: 1137 case LibFunc_expf: 1138 case LibFunc_expl: 1139 case LibFunc_exp2: 1140 case LibFunc_exp2f: 1141 case LibFunc_exp2l: 1142 case LibFunc_expm1: 1143 case LibFunc_expm1f: 1144 case LibFunc_expm1l: 1145 case LibFunc_fabs: 1146 case LibFunc_fabsf: 1147 case LibFunc_fabsl: 1148 case LibFunc_ffs: 1149 case LibFunc_ffsl: 1150 case LibFunc_ffsll: 1151 case LibFunc_floor: 1152 case LibFunc_floorf: 1153 case LibFunc_floorl: 1154 case LibFunc_fls: 1155 case LibFunc_flsl: 1156 case LibFunc_flsll: 1157 case LibFunc_fmax: 1158 case LibFunc_fmaxf: 1159 case LibFunc_fmaxl: 1160 case LibFunc_fmin: 1161 case LibFunc_fminf: 1162 case LibFunc_fminl: 1163 case LibFunc_fmod: 1164 case LibFunc_fmodf: 1165 case LibFunc_fmodl: 1166 case LibFunc_isascii: 1167 case LibFunc_isdigit: 1168 case LibFunc_labs: 1169 case LibFunc_llabs: 1170 case LibFunc_log: 1171 case LibFunc_log10: 1172 case LibFunc_log10f: 1173 case LibFunc_log10l: 1174 case LibFunc_log1p: 1175 case LibFunc_log1pf: 1176 case LibFunc_log1pl: 1177 case LibFunc_log2: 1178 case LibFunc_log2f: 1179 case LibFunc_log2l: 1180 case LibFunc_logb: 1181 case LibFunc_logbf: 1182 case LibFunc_logbl: 1183 case LibFunc_logf: 1184 case LibFunc_logl: 1185 case LibFunc_nearbyint: 1186 case LibFunc_nearbyintf: 1187 case LibFunc_nearbyintl: 1188 case LibFunc_pow: 1189 case LibFunc_powf: 1190 case LibFunc_powl: 1191 case LibFunc_rint: 1192 case LibFunc_rintf: 1193 case LibFunc_rintl: 1194 case LibFunc_round: 1195 case LibFunc_roundf: 1196 case LibFunc_roundl: 1197 case LibFunc_sin: 1198 case LibFunc_sincospif_stret: 1199 case LibFunc_sinf: 1200 case LibFunc_sinh: 1201 case LibFunc_sinhf: 1202 case LibFunc_sinhl: 1203 case LibFunc_sinl: 1204 case LibFunc_sinpi: 1205 case LibFunc_sinpif: 1206 case LibFunc_sqrt: 1207 case LibFunc_sqrtf: 1208 case LibFunc_sqrtl: 1209 case LibFunc_tan: 1210 case LibFunc_tanf: 1211 case LibFunc_tanh: 1212 case LibFunc_tanhf: 1213 case LibFunc_tanhl: 1214 case LibFunc_tanl: 1215 case LibFunc_toascii: 1216 case LibFunc_trunc: 1217 case LibFunc_truncf: 1218 case LibFunc_truncl: 1219 Changed |= setDoesNotThrow(F); 1220 Changed |= setDoesNotFreeMemory(F); 1221 Changed |= setOnlyWritesMemory(F); 1222 Changed |= setWillReturn(F); 1223 break; 1224 default: 1225 // FIXME: It'd be really nice to cover all the library functions we're 1226 // aware of here. 1227 break; 1228 } 1229 // We have to do this step after AllocKind has been inferred on functions so 1230 // we can reliably identify free-like and realloc-like functions. 1231 if (!isLibFreeFunction(&F, TheLibFunc) && !isReallocLikeFn(&F)) 1232 Changed |= setDoesNotFreeMemory(F); 1233 return Changed; 1234 } 1235 1236 static void setArgExtAttr(Function &F, unsigned ArgNo, 1237 const TargetLibraryInfo &TLI, bool Signed = true) { 1238 Attribute::AttrKind ExtAttr = TLI.getExtAttrForI32Param(Signed); 1239 if (ExtAttr != Attribute::None && !F.hasParamAttribute(ArgNo, ExtAttr)) 1240 F.addParamAttr(ArgNo, ExtAttr); 1241 } 1242 1243 static void setRetExtAttr(Function &F, 1244 const TargetLibraryInfo &TLI, bool Signed = true) { 1245 Attribute::AttrKind ExtAttr = TLI.getExtAttrForI32Return(Signed); 1246 if (ExtAttr != Attribute::None && !F.hasRetAttribute(ExtAttr)) 1247 F.addRetAttr(ExtAttr); 1248 } 1249 1250 // Modeled after X86TargetLowering::markLibCallAttributes. 1251 static void markRegisterParameterAttributes(Function *F) { 1252 if (!F->arg_size() || F->isVarArg()) 1253 return; 1254 1255 const CallingConv::ID CC = F->getCallingConv(); 1256 if (CC != CallingConv::C && CC != CallingConv::X86_StdCall) 1257 return; 1258 1259 const Module *M = F->getParent(); 1260 unsigned N = M->getNumberRegisterParameters(); 1261 if (!N) 1262 return; 1263 1264 const DataLayout &DL = M->getDataLayout(); 1265 1266 for (Argument &A : F->args()) { 1267 Type *T = A.getType(); 1268 if (!T->isIntOrPtrTy()) 1269 continue; 1270 1271 const TypeSize &TS = DL.getTypeAllocSize(T); 1272 if (TS > 8) 1273 continue; 1274 1275 assert(TS <= 4 && "Need to account for parameters larger than word size"); 1276 const unsigned NumRegs = TS > 4 ? 2 : 1; 1277 if (N < NumRegs) 1278 return; 1279 1280 N -= NumRegs; 1281 F->addParamAttr(A.getArgNo(), Attribute::InReg); 1282 } 1283 } 1284 1285 FunctionCallee llvm::getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI, 1286 LibFunc TheLibFunc, FunctionType *T, 1287 AttributeList AttributeList) { 1288 assert(TLI.has(TheLibFunc) && 1289 "Creating call to non-existing library function."); 1290 StringRef Name = TLI.getName(TheLibFunc); 1291 FunctionCallee C = M->getOrInsertFunction(Name, T, AttributeList); 1292 1293 // Make sure any mandatory argument attributes are added. 1294 1295 // Any outgoing i32 argument should be handled with setArgExtAttr() which 1296 // will add an extension attribute if the target ABI requires it. Adding 1297 // argument extensions is typically done by the front end but when an 1298 // optimizer is building a library call on its own it has to take care of 1299 // this. Each such generated function must be handled here with sign or 1300 // zero extensions as needed. F is retreived with cast<> because we demand 1301 // of the caller to have called isLibFuncEmittable() first. 1302 Function *F = cast<Function>(C.getCallee()); 1303 assert(F->getFunctionType() == T && "Function type does not match."); 1304 switch (TheLibFunc) { 1305 case LibFunc_fputc: 1306 case LibFunc_putchar: 1307 setArgExtAttr(*F, 0, TLI); 1308 break; 1309 case LibFunc_ldexp: 1310 case LibFunc_ldexpf: 1311 case LibFunc_ldexpl: 1312 case LibFunc_memchr: 1313 case LibFunc_memrchr: 1314 case LibFunc_strchr: 1315 setArgExtAttr(*F, 1, TLI); 1316 break; 1317 case LibFunc_memccpy: 1318 setArgExtAttr(*F, 2, TLI); 1319 break; 1320 1321 // These are functions that are known to not need any argument extension 1322 // on any target: A size_t argument (which may be an i32 on some targets) 1323 // should not trigger the assert below. 1324 case LibFunc_bcmp: 1325 setRetExtAttr(*F, TLI); 1326 break; 1327 case LibFunc_calloc: 1328 case LibFunc_fwrite: 1329 case LibFunc_malloc: 1330 case LibFunc_memcmp: 1331 case LibFunc_memcpy_chk: 1332 case LibFunc_mempcpy: 1333 case LibFunc_memset_pattern16: 1334 case LibFunc_snprintf: 1335 case LibFunc_stpncpy: 1336 case LibFunc_strlcat: 1337 case LibFunc_strlcpy: 1338 case LibFunc_strncat: 1339 case LibFunc_strncmp: 1340 case LibFunc_strncpy: 1341 case LibFunc_vsnprintf: 1342 break; 1343 1344 default: 1345 #ifndef NDEBUG 1346 for (unsigned i = 0; i < T->getNumParams(); i++) 1347 assert(!isa<IntegerType>(T->getParamType(i)) && 1348 "Unhandled integer argument."); 1349 #endif 1350 break; 1351 } 1352 1353 markRegisterParameterAttributes(F); 1354 1355 return C; 1356 } 1357 1358 FunctionCallee llvm::getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI, 1359 LibFunc TheLibFunc, FunctionType *T) { 1360 return getOrInsertLibFunc(M, TLI, TheLibFunc, T, AttributeList()); 1361 } 1362 1363 bool llvm::isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI, 1364 LibFunc TheLibFunc) { 1365 StringRef FuncName = TLI->getName(TheLibFunc); 1366 if (!TLI->has(TheLibFunc)) 1367 return false; 1368 1369 // Check if the Module already has a GlobalValue with the same name, in 1370 // which case it must be a Function with the expected type. 1371 if (GlobalValue *GV = M->getNamedValue(FuncName)) { 1372 if (auto *F = dyn_cast<Function>(GV)) 1373 return TLI->isValidProtoForLibFunc(*F->getFunctionType(), TheLibFunc, *M); 1374 return false; 1375 } 1376 1377 return true; 1378 } 1379 1380 bool llvm::isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI, 1381 StringRef Name) { 1382 LibFunc TheLibFunc; 1383 return TLI->getLibFunc(Name, TheLibFunc) && 1384 isLibFuncEmittable(M, TLI, TheLibFunc); 1385 } 1386 1387 bool llvm::hasFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty, 1388 LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn) { 1389 switch (Ty->getTypeID()) { 1390 case Type::HalfTyID: 1391 return false; 1392 case Type::FloatTyID: 1393 return isLibFuncEmittable(M, TLI, FloatFn); 1394 case Type::DoubleTyID: 1395 return isLibFuncEmittable(M, TLI, DoubleFn); 1396 default: 1397 return isLibFuncEmittable(M, TLI, LongDoubleFn); 1398 } 1399 } 1400 1401 StringRef llvm::getFloatFn(const Module *M, const TargetLibraryInfo *TLI, 1402 Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, 1403 LibFunc LongDoubleFn, LibFunc &TheLibFunc) { 1404 assert(hasFloatFn(M, TLI, Ty, DoubleFn, FloatFn, LongDoubleFn) && 1405 "Cannot get name for unavailable function!"); 1406 1407 switch (Ty->getTypeID()) { 1408 case Type::HalfTyID: 1409 llvm_unreachable("No name for HalfTy!"); 1410 case Type::FloatTyID: 1411 TheLibFunc = FloatFn; 1412 return TLI->getName(FloatFn); 1413 case Type::DoubleTyID: 1414 TheLibFunc = DoubleFn; 1415 return TLI->getName(DoubleFn); 1416 default: 1417 TheLibFunc = LongDoubleFn; 1418 return TLI->getName(LongDoubleFn); 1419 } 1420 } 1421 1422 //- Emit LibCalls ------------------------------------------------------------// 1423 1424 Value *llvm::castToCStr(Value *V, IRBuilderBase &B) { 1425 unsigned AS = V->getType()->getPointerAddressSpace(); 1426 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr"); 1427 } 1428 1429 static IntegerType *getIntTy(IRBuilderBase &B, const TargetLibraryInfo *TLI) { 1430 return B.getIntNTy(TLI->getIntSize()); 1431 } 1432 1433 static IntegerType *getSizeTTy(IRBuilderBase &B, const TargetLibraryInfo *TLI) { 1434 const Module *M = B.GetInsertBlock()->getModule(); 1435 return B.getIntNTy(TLI->getSizeTSize(*M)); 1436 } 1437 1438 static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType, 1439 ArrayRef<Type *> ParamTypes, 1440 ArrayRef<Value *> Operands, IRBuilderBase &B, 1441 const TargetLibraryInfo *TLI, 1442 bool IsVaArgs = false) { 1443 Module *M = B.GetInsertBlock()->getModule(); 1444 if (!isLibFuncEmittable(M, TLI, TheLibFunc)) 1445 return nullptr; 1446 1447 StringRef FuncName = TLI->getName(TheLibFunc); 1448 FunctionType *FuncType = FunctionType::get(ReturnType, ParamTypes, IsVaArgs); 1449 FunctionCallee Callee = getOrInsertLibFunc(M, *TLI, TheLibFunc, FuncType); 1450 inferNonMandatoryLibFuncAttrs(M, FuncName, *TLI); 1451 CallInst *CI = B.CreateCall(Callee, Operands, FuncName); 1452 if (const Function *F = 1453 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 1454 CI->setCallingConv(F->getCallingConv()); 1455 return CI; 1456 } 1457 1458 Value *llvm::emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, 1459 const TargetLibraryInfo *TLI) { 1460 Type *SizeTTy = getSizeTTy(B, TLI); 1461 return emitLibCall(LibFunc_strlen, SizeTTy, 1462 B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI); 1463 } 1464 1465 Value *llvm::emitStrDup(Value *Ptr, IRBuilderBase &B, 1466 const TargetLibraryInfo *TLI) { 1467 return emitLibCall(LibFunc_strdup, B.getInt8PtrTy(), B.getInt8PtrTy(), 1468 castToCStr(Ptr, B), B, TLI); 1469 } 1470 1471 Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilderBase &B, 1472 const TargetLibraryInfo *TLI) { 1473 Type *I8Ptr = B.getInt8PtrTy(); 1474 Type *IntTy = getIntTy(B, TLI); 1475 return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, IntTy}, 1476 {castToCStr(Ptr, B), ConstantInt::get(IntTy, C)}, B, TLI); 1477 } 1478 1479 Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 1480 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1481 Type *IntTy = getIntTy(B, TLI); 1482 Type *SizeTTy = getSizeTTy(B, TLI); 1483 return emitLibCall( 1484 LibFunc_strncmp, IntTy, 1485 {B.getInt8PtrTy(), B.getInt8PtrTy(), SizeTTy}, 1486 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); 1487 } 1488 1489 Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B, 1490 const TargetLibraryInfo *TLI) { 1491 Type *I8Ptr = Dst->getType(); 1492 return emitLibCall(LibFunc_strcpy, I8Ptr, {I8Ptr, I8Ptr}, 1493 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI); 1494 } 1495 1496 Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B, 1497 const TargetLibraryInfo *TLI) { 1498 Type *I8Ptr = B.getInt8PtrTy(); 1499 return emitLibCall(LibFunc_stpcpy, I8Ptr, {I8Ptr, I8Ptr}, 1500 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI); 1501 } 1502 1503 Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 1504 const TargetLibraryInfo *TLI) { 1505 Type *I8Ptr = B.getInt8PtrTy(); 1506 Type *SizeTTy = getSizeTTy(B, TLI); 1507 return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, SizeTTy}, 1508 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI); 1509 } 1510 1511 Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 1512 const TargetLibraryInfo *TLI) { 1513 Type *I8Ptr = B.getInt8PtrTy(); 1514 Type *SizeTTy = getSizeTTy(B, TLI); 1515 return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, SizeTTy}, 1516 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI); 1517 } 1518 1519 Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, 1520 IRBuilderBase &B, const DataLayout &DL, 1521 const TargetLibraryInfo *TLI) { 1522 Module *M = B.GetInsertBlock()->getModule(); 1523 if (!isLibFuncEmittable(M, TLI, LibFunc_memcpy_chk)) 1524 return nullptr; 1525 1526 AttributeList AS; 1527 AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex, 1528 Attribute::NoUnwind); 1529 Type *I8Ptr = B.getInt8PtrTy(); 1530 Type *SizeTTy = getSizeTTy(B, TLI); 1531 FunctionCallee MemCpy = getOrInsertLibFunc(M, *TLI, LibFunc_memcpy_chk, 1532 AttributeList::get(M->getContext(), AS), I8Ptr, 1533 I8Ptr, I8Ptr, SizeTTy, SizeTTy); 1534 Dst = castToCStr(Dst, B); 1535 Src = castToCStr(Src, B); 1536 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize}); 1537 if (const Function *F = 1538 dyn_cast<Function>(MemCpy.getCallee()->stripPointerCasts())) 1539 CI->setCallingConv(F->getCallingConv()); 1540 return CI; 1541 } 1542 1543 Value *llvm::emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 1544 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1545 Type *I8Ptr = B.getInt8PtrTy(); 1546 Type *SizeTTy = getSizeTTy(B, TLI); 1547 return emitLibCall(LibFunc_mempcpy, I8Ptr, 1548 {I8Ptr, I8Ptr, SizeTTy}, 1549 {Dst, Src, Len}, B, TLI); 1550 } 1551 1552 Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, 1553 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1554 Type *I8Ptr = B.getInt8PtrTy(); 1555 Type *IntTy = getIntTy(B, TLI); 1556 Type *SizeTTy = getSizeTTy(B, TLI); 1557 return emitLibCall(LibFunc_memchr, I8Ptr, 1558 {I8Ptr, IntTy, SizeTTy}, 1559 {castToCStr(Ptr, B), Val, Len}, B, TLI); 1560 } 1561 1562 Value *llvm::emitMemRChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, 1563 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1564 Type *I8Ptr = B.getInt8PtrTy(); 1565 Type *IntTy = getIntTy(B, TLI); 1566 Type *SizeTTy = getSizeTTy(B, TLI); 1567 return emitLibCall(LibFunc_memrchr, I8Ptr, 1568 {I8Ptr, IntTy, SizeTTy}, 1569 {castToCStr(Ptr, B), Val, Len}, B, TLI); 1570 } 1571 1572 Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 1573 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1574 Type *I8Ptr = B.getInt8PtrTy(); 1575 Type *IntTy = getIntTy(B, TLI); 1576 Type *SizeTTy = getSizeTTy(B, TLI); 1577 return emitLibCall(LibFunc_memcmp, IntTy, 1578 {I8Ptr, I8Ptr, SizeTTy}, 1579 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); 1580 } 1581 1582 Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 1583 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1584 Type *I8Ptr = B.getInt8PtrTy(); 1585 Type *IntTy = getIntTy(B, TLI); 1586 Type *SizeTTy = getSizeTTy(B, TLI); 1587 return emitLibCall(LibFunc_bcmp, IntTy, 1588 {I8Ptr, I8Ptr, SizeTTy}, 1589 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); 1590 } 1591 1592 Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, 1593 IRBuilderBase &B, const TargetLibraryInfo *TLI) { 1594 Type *I8Ptr = B.getInt8PtrTy(); 1595 Type *IntTy = getIntTy(B, TLI); 1596 Type *SizeTTy = getSizeTTy(B, TLI); 1597 return emitLibCall(LibFunc_memccpy, I8Ptr, 1598 {I8Ptr, I8Ptr, IntTy, SizeTTy}, 1599 {Ptr1, Ptr2, Val, Len}, B, TLI); 1600 } 1601 1602 Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, 1603 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B, 1604 const TargetLibraryInfo *TLI) { 1605 Type *I8Ptr = B.getInt8PtrTy(); 1606 Type *IntTy = getIntTy(B, TLI); 1607 Type *SizeTTy = getSizeTTy(B, TLI); 1608 SmallVector<Value *, 8> Args{castToCStr(Dest, B), Size, castToCStr(Fmt, B)}; 1609 llvm::append_range(Args, VariadicArgs); 1610 return emitLibCall(LibFunc_snprintf, IntTy, 1611 {I8Ptr, SizeTTy, I8Ptr}, 1612 Args, B, TLI, /*IsVaArgs=*/true); 1613 } 1614 1615 Value *llvm::emitSPrintf(Value *Dest, Value *Fmt, 1616 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B, 1617 const TargetLibraryInfo *TLI) { 1618 Type *I8Ptr = B.getInt8PtrTy(); 1619 Type *IntTy = getIntTy(B, TLI); 1620 SmallVector<Value *, 8> Args{castToCStr(Dest, B), castToCStr(Fmt, B)}; 1621 llvm::append_range(Args, VariadicArgs); 1622 return emitLibCall(LibFunc_sprintf, IntTy, 1623 {I8Ptr, I8Ptr}, Args, B, TLI, 1624 /*IsVaArgs=*/true); 1625 } 1626 1627 Value *llvm::emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, 1628 const TargetLibraryInfo *TLI) { 1629 return emitLibCall(LibFunc_strcat, B.getInt8PtrTy(), 1630 {B.getInt8PtrTy(), B.getInt8PtrTy()}, 1631 {castToCStr(Dest, B), castToCStr(Src, B)}, B, TLI); 1632 } 1633 1634 Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 1635 const TargetLibraryInfo *TLI) { 1636 Type *I8Ptr = B.getInt8PtrTy(); 1637 Type *SizeTTy = getSizeTTy(B, TLI); 1638 return emitLibCall(LibFunc_strlcpy, SizeTTy, 1639 {I8Ptr, I8Ptr, SizeTTy}, 1640 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); 1641 } 1642 1643 Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 1644 const TargetLibraryInfo *TLI) { 1645 Type *I8Ptr = B.getInt8PtrTy(); 1646 Type *SizeTTy = getSizeTTy(B, TLI); 1647 return emitLibCall(LibFunc_strlcat, SizeTTy, 1648 {I8Ptr, I8Ptr, SizeTTy}, 1649 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); 1650 } 1651 1652 Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 1653 const TargetLibraryInfo *TLI) { 1654 Type *I8Ptr = B.getInt8PtrTy(); 1655 Type *SizeTTy = getSizeTTy(B, TLI); 1656 return emitLibCall(LibFunc_strncat, I8Ptr, 1657 {I8Ptr, I8Ptr, SizeTTy}, 1658 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); 1659 } 1660 1661 Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, 1662 IRBuilderBase &B, const TargetLibraryInfo *TLI) { 1663 Type *I8Ptr = B.getInt8PtrTy(); 1664 Type *IntTy = getIntTy(B, TLI); 1665 Type *SizeTTy = getSizeTTy(B, TLI); 1666 return emitLibCall( 1667 LibFunc_vsnprintf, IntTy, 1668 {I8Ptr, SizeTTy, I8Ptr, VAList->getType()}, 1669 {castToCStr(Dest, B), Size, castToCStr(Fmt, B), VAList}, B, TLI); 1670 } 1671 1672 Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, 1673 IRBuilderBase &B, const TargetLibraryInfo *TLI) { 1674 Type *I8Ptr = B.getInt8PtrTy(); 1675 Type *IntTy = getIntTy(B, TLI); 1676 return emitLibCall(LibFunc_vsprintf, IntTy, 1677 {I8Ptr, I8Ptr, VAList->getType()}, 1678 {castToCStr(Dest, B), castToCStr(Fmt, B), VAList}, B, TLI); 1679 } 1680 1681 /// Append a suffix to the function name according to the type of 'Op'. 1682 static void appendTypeSuffix(Value *Op, StringRef &Name, 1683 SmallString<20> &NameBuffer) { 1684 if (!Op->getType()->isDoubleTy()) { 1685 NameBuffer += Name; 1686 1687 if (Op->getType()->isFloatTy()) 1688 NameBuffer += 'f'; 1689 else 1690 NameBuffer += 'l'; 1691 1692 Name = NameBuffer; 1693 } 1694 } 1695 1696 static Value *emitUnaryFloatFnCallHelper(Value *Op, LibFunc TheLibFunc, 1697 StringRef Name, IRBuilderBase &B, 1698 const AttributeList &Attrs, 1699 const TargetLibraryInfo *TLI) { 1700 assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall"); 1701 1702 Module *M = B.GetInsertBlock()->getModule(); 1703 FunctionCallee Callee = getOrInsertLibFunc(M, *TLI, TheLibFunc, Op->getType(), 1704 Op->getType()); 1705 CallInst *CI = B.CreateCall(Callee, Op, Name); 1706 1707 // The incoming attribute set may have come from a speculatable intrinsic, but 1708 // is being replaced with a library call which is not allowed to be 1709 // speculatable. 1710 CI->setAttributes( 1711 Attrs.removeFnAttribute(B.getContext(), Attribute::Speculatable)); 1712 if (const Function *F = 1713 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 1714 CI->setCallingConv(F->getCallingConv()); 1715 1716 return CI; 1717 } 1718 1719 Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, 1720 StringRef Name, IRBuilderBase &B, 1721 const AttributeList &Attrs) { 1722 SmallString<20> NameBuffer; 1723 appendTypeSuffix(Op, Name, NameBuffer); 1724 1725 LibFunc TheLibFunc; 1726 TLI->getLibFunc(Name, TheLibFunc); 1727 1728 return emitUnaryFloatFnCallHelper(Op, TheLibFunc, Name, B, Attrs, TLI); 1729 } 1730 1731 Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, 1732 LibFunc DoubleFn, LibFunc FloatFn, 1733 LibFunc LongDoubleFn, IRBuilderBase &B, 1734 const AttributeList &Attrs) { 1735 // Get the name of the function according to TLI. 1736 Module *M = B.GetInsertBlock()->getModule(); 1737 LibFunc TheLibFunc; 1738 StringRef Name = getFloatFn(M, TLI, Op->getType(), DoubleFn, FloatFn, 1739 LongDoubleFn, TheLibFunc); 1740 1741 return emitUnaryFloatFnCallHelper(Op, TheLibFunc, Name, B, Attrs, TLI); 1742 } 1743 1744 static Value *emitBinaryFloatFnCallHelper(Value *Op1, Value *Op2, 1745 LibFunc TheLibFunc, 1746 StringRef Name, IRBuilderBase &B, 1747 const AttributeList &Attrs, 1748 const TargetLibraryInfo *TLI) { 1749 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall"); 1750 1751 Module *M = B.GetInsertBlock()->getModule(); 1752 FunctionCallee Callee = getOrInsertLibFunc(M, *TLI, TheLibFunc, Op1->getType(), 1753 Op1->getType(), Op2->getType()); 1754 inferNonMandatoryLibFuncAttrs(M, Name, *TLI); 1755 CallInst *CI = B.CreateCall(Callee, { Op1, Op2 }, Name); 1756 1757 // The incoming attribute set may have come from a speculatable intrinsic, but 1758 // is being replaced with a library call which is not allowed to be 1759 // speculatable. 1760 CI->setAttributes( 1761 Attrs.removeFnAttribute(B.getContext(), Attribute::Speculatable)); 1762 if (const Function *F = 1763 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 1764 CI->setCallingConv(F->getCallingConv()); 1765 1766 return CI; 1767 } 1768 1769 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, 1770 const TargetLibraryInfo *TLI, 1771 StringRef Name, IRBuilderBase &B, 1772 const AttributeList &Attrs) { 1773 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall"); 1774 1775 SmallString<20> NameBuffer; 1776 appendTypeSuffix(Op1, Name, NameBuffer); 1777 1778 LibFunc TheLibFunc; 1779 TLI->getLibFunc(Name, TheLibFunc); 1780 1781 return emitBinaryFloatFnCallHelper(Op1, Op2, TheLibFunc, Name, B, Attrs, TLI); 1782 } 1783 1784 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, 1785 const TargetLibraryInfo *TLI, 1786 LibFunc DoubleFn, LibFunc FloatFn, 1787 LibFunc LongDoubleFn, IRBuilderBase &B, 1788 const AttributeList &Attrs) { 1789 // Get the name of the function according to TLI. 1790 Module *M = B.GetInsertBlock()->getModule(); 1791 LibFunc TheLibFunc; 1792 StringRef Name = getFloatFn(M, TLI, Op1->getType(), DoubleFn, FloatFn, 1793 LongDoubleFn, TheLibFunc); 1794 1795 return emitBinaryFloatFnCallHelper(Op1, Op2, TheLibFunc, Name, B, Attrs, TLI); 1796 } 1797 1798 // Emit a call to putchar(int) with Char as the argument. Char must have 1799 // the same precision as int, which need not be 32 bits. 1800 Value *llvm::emitPutChar(Value *Char, IRBuilderBase &B, 1801 const TargetLibraryInfo *TLI) { 1802 Module *M = B.GetInsertBlock()->getModule(); 1803 if (!isLibFuncEmittable(M, TLI, LibFunc_putchar)) 1804 return nullptr; 1805 1806 Type *IntTy = getIntTy(B, TLI); 1807 StringRef PutCharName = TLI->getName(LibFunc_putchar); 1808 FunctionCallee PutChar = getOrInsertLibFunc(M, *TLI, LibFunc_putchar, 1809 IntTy, IntTy); 1810 inferNonMandatoryLibFuncAttrs(M, PutCharName, *TLI); 1811 CallInst *CI = B.CreateCall(PutChar, Char, PutCharName); 1812 1813 if (const Function *F = 1814 dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts())) 1815 CI->setCallingConv(F->getCallingConv()); 1816 return CI; 1817 } 1818 1819 Value *llvm::emitPutS(Value *Str, IRBuilderBase &B, 1820 const TargetLibraryInfo *TLI) { 1821 Module *M = B.GetInsertBlock()->getModule(); 1822 if (!isLibFuncEmittable(M, TLI, LibFunc_puts)) 1823 return nullptr; 1824 1825 Type *IntTy = getIntTy(B, TLI); 1826 StringRef PutsName = TLI->getName(LibFunc_puts); 1827 FunctionCallee PutS = getOrInsertLibFunc(M, *TLI, LibFunc_puts, IntTy, 1828 B.getInt8PtrTy()); 1829 inferNonMandatoryLibFuncAttrs(M, PutsName, *TLI); 1830 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName); 1831 if (const Function *F = 1832 dyn_cast<Function>(PutS.getCallee()->stripPointerCasts())) 1833 CI->setCallingConv(F->getCallingConv()); 1834 return CI; 1835 } 1836 1837 Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilderBase &B, 1838 const TargetLibraryInfo *TLI) { 1839 Module *M = B.GetInsertBlock()->getModule(); 1840 if (!isLibFuncEmittable(M, TLI, LibFunc_fputc)) 1841 return nullptr; 1842 1843 Type *IntTy = getIntTy(B, TLI); 1844 StringRef FPutcName = TLI->getName(LibFunc_fputc); 1845 FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputc, IntTy, 1846 IntTy, File->getType()); 1847 if (File->getType()->isPointerTy()) 1848 inferNonMandatoryLibFuncAttrs(M, FPutcName, *TLI); 1849 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName); 1850 1851 if (const Function *Fn = 1852 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1853 CI->setCallingConv(Fn->getCallingConv()); 1854 return CI; 1855 } 1856 1857 Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilderBase &B, 1858 const TargetLibraryInfo *TLI) { 1859 Module *M = B.GetInsertBlock()->getModule(); 1860 if (!isLibFuncEmittable(M, TLI, LibFunc_fputs)) 1861 return nullptr; 1862 1863 Type *IntTy = getIntTy(B, TLI); 1864 StringRef FPutsName = TLI->getName(LibFunc_fputs); 1865 FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputs, IntTy, 1866 B.getInt8PtrTy(), File->getType()); 1867 if (File->getType()->isPointerTy()) 1868 inferNonMandatoryLibFuncAttrs(M, FPutsName, *TLI); 1869 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName); 1870 1871 if (const Function *Fn = 1872 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1873 CI->setCallingConv(Fn->getCallingConv()); 1874 return CI; 1875 } 1876 1877 Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, 1878 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1879 Module *M = B.GetInsertBlock()->getModule(); 1880 if (!isLibFuncEmittable(M, TLI, LibFunc_fwrite)) 1881 return nullptr; 1882 1883 Type *SizeTTy = getSizeTTy(B, TLI); 1884 StringRef FWriteName = TLI->getName(LibFunc_fwrite); 1885 FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fwrite, 1886 SizeTTy, B.getInt8PtrTy(), SizeTTy, 1887 SizeTTy, File->getType()); 1888 1889 if (File->getType()->isPointerTy()) 1890 inferNonMandatoryLibFuncAttrs(M, FWriteName, *TLI); 1891 CallInst *CI = 1892 B.CreateCall(F, {castToCStr(Ptr, B), Size, 1893 ConstantInt::get(SizeTTy, 1), File}); 1894 1895 if (const Function *Fn = 1896 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1897 CI->setCallingConv(Fn->getCallingConv()); 1898 return CI; 1899 } 1900 1901 Value *llvm::emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, 1902 const TargetLibraryInfo *TLI) { 1903 Module *M = B.GetInsertBlock()->getModule(); 1904 if (!isLibFuncEmittable(M, TLI, LibFunc_malloc)) 1905 return nullptr; 1906 1907 StringRef MallocName = TLI->getName(LibFunc_malloc); 1908 Type *SizeTTy = getSizeTTy(B, TLI); 1909 FunctionCallee Malloc = getOrInsertLibFunc(M, *TLI, LibFunc_malloc, 1910 B.getInt8PtrTy(), SizeTTy); 1911 inferNonMandatoryLibFuncAttrs(M, MallocName, *TLI); 1912 CallInst *CI = B.CreateCall(Malloc, Num, MallocName); 1913 1914 if (const Function *F = 1915 dyn_cast<Function>(Malloc.getCallee()->stripPointerCasts())) 1916 CI->setCallingConv(F->getCallingConv()); 1917 1918 return CI; 1919 } 1920 1921 Value *llvm::emitCalloc(Value *Num, Value *Size, IRBuilderBase &B, 1922 const TargetLibraryInfo &TLI) { 1923 Module *M = B.GetInsertBlock()->getModule(); 1924 if (!isLibFuncEmittable(M, &TLI, LibFunc_calloc)) 1925 return nullptr; 1926 1927 StringRef CallocName = TLI.getName(LibFunc_calloc); 1928 Type *SizeTTy = getSizeTTy(B, &TLI); 1929 FunctionCallee Calloc = getOrInsertLibFunc(M, TLI, LibFunc_calloc, 1930 B.getInt8PtrTy(), SizeTTy, SizeTTy); 1931 inferNonMandatoryLibFuncAttrs(M, CallocName, TLI); 1932 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName); 1933 1934 if (const auto *F = 1935 dyn_cast<Function>(Calloc.getCallee()->stripPointerCasts())) 1936 CI->setCallingConv(F->getCallingConv()); 1937 1938 return CI; 1939 } 1940