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_strchr: 1309 setArgExtAttr(*F, 1, TLI); 1310 break; 1311 case LibFunc_memccpy: 1312 setArgExtAttr(*F, 2, TLI); 1313 break; 1314 1315 // These are functions that are known to not need any argument extension 1316 // on any target: A size_t argument (which may be an i32 on some targets) 1317 // should not trigger the assert below. 1318 case LibFunc_bcmp: 1319 case LibFunc_calloc: 1320 case LibFunc_fwrite: 1321 case LibFunc_malloc: 1322 case LibFunc_memcmp: 1323 case LibFunc_memcpy_chk: 1324 case LibFunc_mempcpy: 1325 case LibFunc_memset_pattern16: 1326 case LibFunc_snprintf: 1327 case LibFunc_stpncpy: 1328 case LibFunc_strlcat: 1329 case LibFunc_strlcpy: 1330 case LibFunc_strncat: 1331 case LibFunc_strncmp: 1332 case LibFunc_strncpy: 1333 case LibFunc_vsnprintf: 1334 break; 1335 1336 default: 1337 #ifndef NDEBUG 1338 for (unsigned i = 0; i < T->getNumParams(); i++) 1339 assert(!isa<IntegerType>(T->getParamType(i)) && 1340 "Unhandled integer argument."); 1341 #endif 1342 break; 1343 } 1344 1345 markRegisterParameterAttributes(F); 1346 1347 return C; 1348 } 1349 1350 FunctionCallee llvm::getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI, 1351 LibFunc TheLibFunc, FunctionType *T) { 1352 return getOrInsertLibFunc(M, TLI, TheLibFunc, T, AttributeList()); 1353 } 1354 1355 bool llvm::isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI, 1356 LibFunc TheLibFunc) { 1357 StringRef FuncName = TLI->getName(TheLibFunc); 1358 if (!TLI->has(TheLibFunc)) 1359 return false; 1360 1361 // Check if the Module already has a GlobalValue with the same name, in 1362 // which case it must be a Function with the expected type. 1363 if (GlobalValue *GV = M->getNamedValue(FuncName)) { 1364 if (auto *F = dyn_cast<Function>(GV)) 1365 return TLI->isValidProtoForLibFunc(*F->getFunctionType(), TheLibFunc, *M); 1366 return false; 1367 } 1368 1369 return true; 1370 } 1371 1372 bool llvm::isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI, 1373 StringRef Name) { 1374 LibFunc TheLibFunc; 1375 return TLI->getLibFunc(Name, TheLibFunc) && 1376 isLibFuncEmittable(M, TLI, TheLibFunc); 1377 } 1378 1379 bool llvm::hasFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty, 1380 LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn) { 1381 switch (Ty->getTypeID()) { 1382 case Type::HalfTyID: 1383 return false; 1384 case Type::FloatTyID: 1385 return isLibFuncEmittable(M, TLI, FloatFn); 1386 case Type::DoubleTyID: 1387 return isLibFuncEmittable(M, TLI, DoubleFn); 1388 default: 1389 return isLibFuncEmittable(M, TLI, LongDoubleFn); 1390 } 1391 } 1392 1393 StringRef llvm::getFloatFn(const Module *M, const TargetLibraryInfo *TLI, 1394 Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, 1395 LibFunc LongDoubleFn, LibFunc &TheLibFunc) { 1396 assert(hasFloatFn(M, TLI, Ty, DoubleFn, FloatFn, LongDoubleFn) && 1397 "Cannot get name for unavailable function!"); 1398 1399 switch (Ty->getTypeID()) { 1400 case Type::HalfTyID: 1401 llvm_unreachable("No name for HalfTy!"); 1402 case Type::FloatTyID: 1403 TheLibFunc = FloatFn; 1404 return TLI->getName(FloatFn); 1405 case Type::DoubleTyID: 1406 TheLibFunc = DoubleFn; 1407 return TLI->getName(DoubleFn); 1408 default: 1409 TheLibFunc = LongDoubleFn; 1410 return TLI->getName(LongDoubleFn); 1411 } 1412 } 1413 1414 //- Emit LibCalls ------------------------------------------------------------// 1415 1416 Value *llvm::castToCStr(Value *V, IRBuilderBase &B) { 1417 unsigned AS = V->getType()->getPointerAddressSpace(); 1418 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr"); 1419 } 1420 1421 static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType, 1422 ArrayRef<Type *> ParamTypes, 1423 ArrayRef<Value *> Operands, IRBuilderBase &B, 1424 const TargetLibraryInfo *TLI, 1425 bool IsVaArgs = false) { 1426 Module *M = B.GetInsertBlock()->getModule(); 1427 if (!isLibFuncEmittable(M, TLI, TheLibFunc)) 1428 return nullptr; 1429 1430 StringRef FuncName = TLI->getName(TheLibFunc); 1431 FunctionType *FuncType = FunctionType::get(ReturnType, ParamTypes, IsVaArgs); 1432 FunctionCallee Callee = getOrInsertLibFunc(M, *TLI, TheLibFunc, FuncType); 1433 inferNonMandatoryLibFuncAttrs(M, FuncName, *TLI); 1434 CallInst *CI = B.CreateCall(Callee, Operands, FuncName); 1435 if (const Function *F = 1436 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 1437 CI->setCallingConv(F->getCallingConv()); 1438 return CI; 1439 } 1440 1441 Value *llvm::emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, 1442 const TargetLibraryInfo *TLI) { 1443 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1444 return emitLibCall(LibFunc_strlen, DL.getIntPtrType(Context), 1445 B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI); 1446 } 1447 1448 Value *llvm::emitStrDup(Value *Ptr, IRBuilderBase &B, 1449 const TargetLibraryInfo *TLI) { 1450 return emitLibCall(LibFunc_strdup, B.getInt8PtrTy(), B.getInt8PtrTy(), 1451 castToCStr(Ptr, B), B, TLI); 1452 } 1453 1454 Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilderBase &B, 1455 const TargetLibraryInfo *TLI) { 1456 Type *I8Ptr = B.getInt8PtrTy(); 1457 Type *I32Ty = B.getInt32Ty(); 1458 return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, I32Ty}, 1459 {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, B, TLI); 1460 } 1461 1462 Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 1463 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1464 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1465 return emitLibCall( 1466 LibFunc_strncmp, B.getInt32Ty(), 1467 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, 1468 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); 1469 } 1470 1471 Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B, 1472 const TargetLibraryInfo *TLI) { 1473 Type *I8Ptr = Dst->getType(); 1474 return emitLibCall(LibFunc_strcpy, I8Ptr, {I8Ptr, I8Ptr}, 1475 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI); 1476 } 1477 1478 Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B, 1479 const TargetLibraryInfo *TLI) { 1480 Type *I8Ptr = B.getInt8PtrTy(); 1481 return emitLibCall(LibFunc_stpcpy, I8Ptr, {I8Ptr, I8Ptr}, 1482 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI); 1483 } 1484 1485 Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 1486 const TargetLibraryInfo *TLI) { 1487 Type *I8Ptr = B.getInt8PtrTy(); 1488 return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()}, 1489 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI); 1490 } 1491 1492 Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 1493 const TargetLibraryInfo *TLI) { 1494 Type *I8Ptr = B.getInt8PtrTy(); 1495 return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()}, 1496 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI); 1497 } 1498 1499 Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, 1500 IRBuilderBase &B, const DataLayout &DL, 1501 const TargetLibraryInfo *TLI) { 1502 Module *M = B.GetInsertBlock()->getModule(); 1503 if (!isLibFuncEmittable(M, TLI, LibFunc_memcpy_chk)) 1504 return nullptr; 1505 1506 AttributeList AS; 1507 AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex, 1508 Attribute::NoUnwind); 1509 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1510 FunctionCallee MemCpy = getOrInsertLibFunc(M, *TLI, LibFunc_memcpy_chk, 1511 AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(), 1512 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), 1513 DL.getIntPtrType(Context)); 1514 Dst = castToCStr(Dst, B); 1515 Src = castToCStr(Src, B); 1516 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize}); 1517 if (const Function *F = 1518 dyn_cast<Function>(MemCpy.getCallee()->stripPointerCasts())) 1519 CI->setCallingConv(F->getCallingConv()); 1520 return CI; 1521 } 1522 1523 Value *llvm::emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, 1524 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1525 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1526 return emitLibCall( 1527 LibFunc_mempcpy, B.getInt8PtrTy(), 1528 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, 1529 {Dst, Src, Len}, B, TLI); 1530 } 1531 1532 Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, 1533 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1534 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1535 return emitLibCall( 1536 LibFunc_memchr, B.getInt8PtrTy(), 1537 {B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context)}, 1538 {castToCStr(Ptr, B), Val, Len}, B, TLI); 1539 } 1540 1541 Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 1542 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1543 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1544 return emitLibCall( 1545 LibFunc_memcmp, B.getInt32Ty(), 1546 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, 1547 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); 1548 } 1549 1550 Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, 1551 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1552 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1553 return emitLibCall( 1554 LibFunc_bcmp, B.getInt32Ty(), 1555 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)}, 1556 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI); 1557 } 1558 1559 Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, 1560 IRBuilderBase &B, const TargetLibraryInfo *TLI) { 1561 return emitLibCall( 1562 LibFunc_memccpy, B.getInt8PtrTy(), 1563 {B.getInt8PtrTy(), B.getInt8PtrTy(), B.getInt32Ty(), Len->getType()}, 1564 {Ptr1, Ptr2, Val, Len}, B, TLI); 1565 } 1566 1567 Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, 1568 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B, 1569 const TargetLibraryInfo *TLI) { 1570 SmallVector<Value *, 8> Args{castToCStr(Dest, B), Size, castToCStr(Fmt, B)}; 1571 llvm::append_range(Args, VariadicArgs); 1572 return emitLibCall(LibFunc_snprintf, B.getInt32Ty(), 1573 {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy()}, 1574 Args, B, TLI, /*IsVaArgs=*/true); 1575 } 1576 1577 Value *llvm::emitSPrintf(Value *Dest, Value *Fmt, 1578 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B, 1579 const TargetLibraryInfo *TLI) { 1580 SmallVector<Value *, 8> Args{castToCStr(Dest, B), castToCStr(Fmt, B)}; 1581 llvm::append_range(Args, VariadicArgs); 1582 return emitLibCall(LibFunc_sprintf, B.getInt32Ty(), 1583 {B.getInt8PtrTy(), B.getInt8PtrTy()}, Args, B, TLI, 1584 /*IsVaArgs=*/true); 1585 } 1586 1587 Value *llvm::emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, 1588 const TargetLibraryInfo *TLI) { 1589 return emitLibCall(LibFunc_strcat, B.getInt8PtrTy(), 1590 {B.getInt8PtrTy(), B.getInt8PtrTy()}, 1591 {castToCStr(Dest, B), castToCStr(Src, B)}, B, TLI); 1592 } 1593 1594 Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 1595 const TargetLibraryInfo *TLI) { 1596 return emitLibCall(LibFunc_strlcpy, Size->getType(), 1597 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()}, 1598 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); 1599 } 1600 1601 Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 1602 const TargetLibraryInfo *TLI) { 1603 return emitLibCall(LibFunc_strlcat, Size->getType(), 1604 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()}, 1605 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); 1606 } 1607 1608 Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, 1609 const TargetLibraryInfo *TLI) { 1610 return emitLibCall(LibFunc_strncat, B.getInt8PtrTy(), 1611 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()}, 1612 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI); 1613 } 1614 1615 Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, 1616 IRBuilderBase &B, const TargetLibraryInfo *TLI) { 1617 return emitLibCall( 1618 LibFunc_vsnprintf, B.getInt32Ty(), 1619 {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy(), VAList->getType()}, 1620 {castToCStr(Dest, B), Size, castToCStr(Fmt, B), VAList}, B, TLI); 1621 } 1622 1623 Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, 1624 IRBuilderBase &B, const TargetLibraryInfo *TLI) { 1625 return emitLibCall(LibFunc_vsprintf, B.getInt32Ty(), 1626 {B.getInt8PtrTy(), B.getInt8PtrTy(), VAList->getType()}, 1627 {castToCStr(Dest, B), castToCStr(Fmt, B), VAList}, B, TLI); 1628 } 1629 1630 /// Append a suffix to the function name according to the type of 'Op'. 1631 static void appendTypeSuffix(Value *Op, StringRef &Name, 1632 SmallString<20> &NameBuffer) { 1633 if (!Op->getType()->isDoubleTy()) { 1634 NameBuffer += Name; 1635 1636 if (Op->getType()->isFloatTy()) 1637 NameBuffer += 'f'; 1638 else 1639 NameBuffer += 'l'; 1640 1641 Name = NameBuffer; 1642 } 1643 } 1644 1645 static Value *emitUnaryFloatFnCallHelper(Value *Op, LibFunc TheLibFunc, 1646 StringRef Name, IRBuilderBase &B, 1647 const AttributeList &Attrs, 1648 const TargetLibraryInfo *TLI) { 1649 assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall"); 1650 1651 Module *M = B.GetInsertBlock()->getModule(); 1652 FunctionCallee Callee = getOrInsertLibFunc(M, *TLI, TheLibFunc, Op->getType(), 1653 Op->getType()); 1654 CallInst *CI = B.CreateCall(Callee, Op, Name); 1655 1656 // The incoming attribute set may have come from a speculatable intrinsic, but 1657 // is being replaced with a library call which is not allowed to be 1658 // speculatable. 1659 CI->setAttributes( 1660 Attrs.removeFnAttribute(B.getContext(), Attribute::Speculatable)); 1661 if (const Function *F = 1662 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 1663 CI->setCallingConv(F->getCallingConv()); 1664 1665 return CI; 1666 } 1667 1668 Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, 1669 StringRef Name, IRBuilderBase &B, 1670 const AttributeList &Attrs) { 1671 SmallString<20> NameBuffer; 1672 appendTypeSuffix(Op, Name, NameBuffer); 1673 1674 LibFunc TheLibFunc; 1675 TLI->getLibFunc(Name, TheLibFunc); 1676 1677 return emitUnaryFloatFnCallHelper(Op, TheLibFunc, Name, B, Attrs, TLI); 1678 } 1679 1680 Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, 1681 LibFunc DoubleFn, LibFunc FloatFn, 1682 LibFunc LongDoubleFn, IRBuilderBase &B, 1683 const AttributeList &Attrs) { 1684 // Get the name of the function according to TLI. 1685 Module *M = B.GetInsertBlock()->getModule(); 1686 LibFunc TheLibFunc; 1687 StringRef Name = getFloatFn(M, TLI, Op->getType(), DoubleFn, FloatFn, 1688 LongDoubleFn, TheLibFunc); 1689 1690 return emitUnaryFloatFnCallHelper(Op, TheLibFunc, Name, B, Attrs, TLI); 1691 } 1692 1693 static Value *emitBinaryFloatFnCallHelper(Value *Op1, Value *Op2, 1694 LibFunc TheLibFunc, 1695 StringRef Name, IRBuilderBase &B, 1696 const AttributeList &Attrs, 1697 const TargetLibraryInfo *TLI) { 1698 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall"); 1699 1700 Module *M = B.GetInsertBlock()->getModule(); 1701 FunctionCallee Callee = getOrInsertLibFunc(M, *TLI, TheLibFunc, Op1->getType(), 1702 Op1->getType(), Op2->getType()); 1703 inferNonMandatoryLibFuncAttrs(M, Name, *TLI); 1704 CallInst *CI = B.CreateCall(Callee, { Op1, Op2 }, Name); 1705 1706 // The incoming attribute set may have come from a speculatable intrinsic, but 1707 // is being replaced with a library call which is not allowed to be 1708 // speculatable. 1709 CI->setAttributes( 1710 Attrs.removeFnAttribute(B.getContext(), Attribute::Speculatable)); 1711 if (const Function *F = 1712 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts())) 1713 CI->setCallingConv(F->getCallingConv()); 1714 1715 return CI; 1716 } 1717 1718 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, 1719 const TargetLibraryInfo *TLI, 1720 StringRef Name, IRBuilderBase &B, 1721 const AttributeList &Attrs) { 1722 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall"); 1723 1724 SmallString<20> NameBuffer; 1725 appendTypeSuffix(Op1, Name, NameBuffer); 1726 1727 LibFunc TheLibFunc; 1728 TLI->getLibFunc(Name, TheLibFunc); 1729 1730 return emitBinaryFloatFnCallHelper(Op1, Op2, TheLibFunc, Name, B, Attrs, TLI); 1731 } 1732 1733 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, 1734 const TargetLibraryInfo *TLI, 1735 LibFunc DoubleFn, LibFunc FloatFn, 1736 LibFunc LongDoubleFn, IRBuilderBase &B, 1737 const AttributeList &Attrs) { 1738 // Get the name of the function according to TLI. 1739 Module *M = B.GetInsertBlock()->getModule(); 1740 LibFunc TheLibFunc; 1741 StringRef Name = getFloatFn(M, TLI, Op1->getType(), DoubleFn, FloatFn, 1742 LongDoubleFn, TheLibFunc); 1743 1744 return emitBinaryFloatFnCallHelper(Op1, Op2, TheLibFunc, Name, B, Attrs, TLI); 1745 } 1746 1747 Value *llvm::emitPutChar(Value *Char, IRBuilderBase &B, 1748 const TargetLibraryInfo *TLI) { 1749 Module *M = B.GetInsertBlock()->getModule(); 1750 if (!isLibFuncEmittable(M, TLI, LibFunc_putchar)) 1751 return nullptr; 1752 1753 StringRef PutCharName = TLI->getName(LibFunc_putchar); 1754 FunctionCallee PutChar = getOrInsertLibFunc(M, *TLI, LibFunc_putchar, 1755 B.getInt32Ty(), B.getInt32Ty()); 1756 inferNonMandatoryLibFuncAttrs(M, PutCharName, *TLI); 1757 CallInst *CI = B.CreateCall(PutChar, 1758 B.CreateIntCast(Char, 1759 B.getInt32Ty(), 1760 /*isSigned*/true, 1761 "chari"), 1762 PutCharName); 1763 1764 if (const Function *F = 1765 dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts())) 1766 CI->setCallingConv(F->getCallingConv()); 1767 return CI; 1768 } 1769 1770 Value *llvm::emitPutS(Value *Str, IRBuilderBase &B, 1771 const TargetLibraryInfo *TLI) { 1772 Module *M = B.GetInsertBlock()->getModule(); 1773 if (!isLibFuncEmittable(M, TLI, LibFunc_puts)) 1774 return nullptr; 1775 1776 StringRef PutsName = TLI->getName(LibFunc_puts); 1777 FunctionCallee PutS = getOrInsertLibFunc(M, *TLI, LibFunc_puts, B.getInt32Ty(), 1778 B.getInt8PtrTy()); 1779 inferNonMandatoryLibFuncAttrs(M, PutsName, *TLI); 1780 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName); 1781 if (const Function *F = 1782 dyn_cast<Function>(PutS.getCallee()->stripPointerCasts())) 1783 CI->setCallingConv(F->getCallingConv()); 1784 return CI; 1785 } 1786 1787 Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilderBase &B, 1788 const TargetLibraryInfo *TLI) { 1789 Module *M = B.GetInsertBlock()->getModule(); 1790 if (!isLibFuncEmittable(M, TLI, LibFunc_fputc)) 1791 return nullptr; 1792 1793 StringRef FPutcName = TLI->getName(LibFunc_fputc); 1794 FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputc, B.getInt32Ty(), 1795 B.getInt32Ty(), File->getType()); 1796 if (File->getType()->isPointerTy()) 1797 inferNonMandatoryLibFuncAttrs(M, FPutcName, *TLI); 1798 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, 1799 "chari"); 1800 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName); 1801 1802 if (const Function *Fn = 1803 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1804 CI->setCallingConv(Fn->getCallingConv()); 1805 return CI; 1806 } 1807 1808 Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilderBase &B, 1809 const TargetLibraryInfo *TLI) { 1810 Module *M = B.GetInsertBlock()->getModule(); 1811 if (!isLibFuncEmittable(M, TLI, LibFunc_fputs)) 1812 return nullptr; 1813 1814 StringRef FPutsName = TLI->getName(LibFunc_fputs); 1815 FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputs, B.getInt32Ty(), 1816 B.getInt8PtrTy(), File->getType()); 1817 if (File->getType()->isPointerTy()) 1818 inferNonMandatoryLibFuncAttrs(M, FPutsName, *TLI); 1819 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName); 1820 1821 if (const Function *Fn = 1822 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1823 CI->setCallingConv(Fn->getCallingConv()); 1824 return CI; 1825 } 1826 1827 Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, 1828 const DataLayout &DL, const TargetLibraryInfo *TLI) { 1829 Module *M = B.GetInsertBlock()->getModule(); 1830 if (!isLibFuncEmittable(M, TLI, LibFunc_fwrite)) 1831 return nullptr; 1832 1833 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1834 StringRef FWriteName = TLI->getName(LibFunc_fwrite); 1835 FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fwrite, 1836 DL.getIntPtrType(Context), B.getInt8PtrTy(), DL.getIntPtrType(Context), 1837 DL.getIntPtrType(Context), File->getType()); 1838 1839 if (File->getType()->isPointerTy()) 1840 inferNonMandatoryLibFuncAttrs(M, FWriteName, *TLI); 1841 CallInst *CI = 1842 B.CreateCall(F, {castToCStr(Ptr, B), Size, 1843 ConstantInt::get(DL.getIntPtrType(Context), 1), File}); 1844 1845 if (const Function *Fn = 1846 dyn_cast<Function>(F.getCallee()->stripPointerCasts())) 1847 CI->setCallingConv(Fn->getCallingConv()); 1848 return CI; 1849 } 1850 1851 Value *llvm::emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, 1852 const TargetLibraryInfo *TLI) { 1853 Module *M = B.GetInsertBlock()->getModule(); 1854 if (!isLibFuncEmittable(M, TLI, LibFunc_malloc)) 1855 return nullptr; 1856 1857 StringRef MallocName = TLI->getName(LibFunc_malloc); 1858 LLVMContext &Context = B.GetInsertBlock()->getContext(); 1859 FunctionCallee Malloc = getOrInsertLibFunc(M, *TLI, LibFunc_malloc, 1860 B.getInt8PtrTy(), DL.getIntPtrType(Context)); 1861 inferNonMandatoryLibFuncAttrs(M, MallocName, *TLI); 1862 CallInst *CI = B.CreateCall(Malloc, Num, MallocName); 1863 1864 if (const Function *F = 1865 dyn_cast<Function>(Malloc.getCallee()->stripPointerCasts())) 1866 CI->setCallingConv(F->getCallingConv()); 1867 1868 return CI; 1869 } 1870 1871 Value *llvm::emitCalloc(Value *Num, Value *Size, IRBuilderBase &B, 1872 const TargetLibraryInfo &TLI) { 1873 Module *M = B.GetInsertBlock()->getModule(); 1874 if (!isLibFuncEmittable(M, &TLI, LibFunc_calloc)) 1875 return nullptr; 1876 1877 StringRef CallocName = TLI.getName(LibFunc_calloc); 1878 const DataLayout &DL = M->getDataLayout(); 1879 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext())); 1880 FunctionCallee Calloc = getOrInsertLibFunc(M, TLI, LibFunc_calloc, 1881 B.getInt8PtrTy(), PtrType, PtrType); 1882 inferNonMandatoryLibFuncAttrs(M, CallocName, TLI); 1883 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName); 1884 1885 if (const auto *F = 1886 dyn_cast<Function>(Calloc.getCallee()->stripPointerCasts())) 1887 CI->setCallingConv(F->getCallingConv()); 1888 1889 return CI; 1890 } 1891