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