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