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