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