1 //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
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())
58 F
.setDoesNotAccessMemory();
63 static bool setIsCold(Function
&F
) {
64 if (F
.hasFnAttribute(Attribute::Cold
))
66 F
.addFnAttr(Attribute::Cold
);
71 static bool setNoReturn(Function
&F
) {
72 if (F
.hasFnAttribute(Attribute::NoReturn
))
74 F
.addFnAttr(Attribute::NoReturn
);
79 static bool setOnlyAccessesInaccessibleMemory(Function
&F
) {
80 if (F
.onlyAccessesInaccessibleMemory())
82 F
.setOnlyAccessesInaccessibleMemory();
83 ++NumInaccessibleMemOnly
;
87 static bool setOnlyReadsMemory(Function
&F
) {
88 if (F
.onlyReadsMemory())
90 F
.setOnlyReadsMemory();
95 static bool setOnlyWritesMemory(Function
&F
) {
96 if (F
.onlyWritesMemory()) // writeonly or readnone
99 F
.setOnlyWritesMemory();
103 static bool setOnlyAccessesArgMemory(Function
&F
) {
104 if (F
.onlyAccessesArgMemory())
106 F
.setOnlyAccessesArgMemory();
111 static bool setOnlyAccessesInaccessibleMemOrArgMem(Function
&F
) {
112 if (F
.onlyAccessesInaccessibleMemOrArgMem())
114 F
.setOnlyAccessesInaccessibleMemOrArgMem();
115 ++NumInaccessibleMemOrArgMemOnly
;
119 static bool setDoesNotThrow(Function
&F
) {
120 if (F
.doesNotThrow())
127 static bool setRetDoesNotAlias(Function
&F
) {
128 if (F
.hasRetAttribute(Attribute::NoAlias
))
130 F
.addRetAttr(Attribute::NoAlias
);
135 static bool setDoesNotCapture(Function
&F
, unsigned ArgNo
) {
136 if (F
.hasParamAttribute(ArgNo
, Attribute::NoCapture
))
138 F
.addParamAttr(ArgNo
, Attribute::NoCapture
);
143 static bool setDoesNotAlias(Function
&F
, unsigned ArgNo
) {
144 if (F
.hasParamAttribute(ArgNo
, Attribute::NoAlias
))
146 F
.addParamAttr(ArgNo
, Attribute::NoAlias
);
151 static bool setOnlyReadsMemory(Function
&F
, unsigned ArgNo
) {
152 if (F
.hasParamAttribute(ArgNo
, Attribute::ReadOnly
))
154 F
.addParamAttr(ArgNo
, Attribute::ReadOnly
);
159 static bool setOnlyWritesMemory(Function
&F
, unsigned ArgNo
) {
160 if (F
.hasParamAttribute(ArgNo
, Attribute::WriteOnly
))
162 F
.addParamAttr(ArgNo
, Attribute::WriteOnly
);
167 static bool setRetNoUndef(Function
&F
) {
168 if (!F
.getReturnType()->isVoidTy() &&
169 !F
.hasRetAttribute(Attribute::NoUndef
)) {
170 F
.addRetAttr(Attribute::NoUndef
);
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
);
189 static bool setArgNoUndef(Function
&F
, unsigned ArgNo
) {
190 if (F
.hasParamAttribute(ArgNo
, Attribute::NoUndef
))
192 F
.addParamAttr(ArgNo
, Attribute::NoUndef
);
197 static bool setRetAndArgsNoUndef(Function
&F
) {
198 bool UndefAdded
= false;
199 UndefAdded
|= setRetNoUndef(F
);
200 UndefAdded
|= setArgsNoUndef(F
);
204 static bool setReturnedArg(Function
&F
, unsigned ArgNo
) {
205 if (F
.hasParamAttribute(ArgNo
, Attribute::Returned
))
207 F
.addParamAttr(ArgNo
, Attribute::Returned
);
212 static bool setNonLazyBind(Function
&F
) {
213 if (F
.hasFnAttribute(Attribute::NonLazyBind
))
215 F
.addFnAttr(Attribute::NonLazyBind
);
219 static bool setDoesNotFreeMemory(Function
&F
) {
220 if (F
.hasFnAttribute(Attribute::NoFree
))
222 F
.addFnAttr(Attribute::NoFree
);
226 static bool setWillReturn(Function
&F
) {
227 if (F
.hasFnAttribute(Attribute::WillReturn
))
229 F
.addFnAttr(Attribute::WillReturn
);
234 static bool setAlignedAllocParam(Function
&F
, unsigned ArgNo
) {
235 if (F
.hasParamAttribute(ArgNo
, Attribute::AllocAlign
))
237 F
.addParamAttr(ArgNo
, Attribute::AllocAlign
);
241 static bool setAllocatedPointerParam(Function
&F
, unsigned ArgNo
) {
242 if (F
.hasParamAttribute(ArgNo
, Attribute::AllocatedPointer
))
244 F
.addParamAttr(ArgNo
, Attribute::AllocatedPointer
);
248 static bool setAllocSize(Function
&F
, unsigned ElemSizeArg
,
249 std::optional
<unsigned> NumElemsArg
) {
250 if (F
.hasFnAttribute(Attribute::AllocSize
))
252 F
.addFnAttr(Attribute::getWithAllocSizeArgs(F
.getContext(), ElemSizeArg
,
257 static bool setAllocFamily(Function
&F
, StringRef Family
) {
258 if (F
.hasFnAttribute("alloc-family"))
260 F
.addFnAttr("alloc-family", Family
);
264 static bool setAllocKind(Function
&F
, AllocFnKind K
) {
265 if (F
.hasFnAttribute(Attribute::AllocKind
))
268 Attribute::get(F
.getContext(), Attribute::AllocKind
, uint64_t(K
)));
272 bool llvm::inferNonMandatoryLibFuncAttrs(Module
*M
, StringRef Name
,
273 const TargetLibraryInfo
&TLI
) {
274 Function
*F
= M
->getFunction(Name
);
277 return inferNonMandatoryLibFuncAttrs(*F
, TLI
);
280 bool llvm::inferNonMandatoryLibFuncAttrs(Function
&F
,
281 const TargetLibraryInfo
&TLI
) {
283 if (!(TLI
.getLibFunc(F
, TheLibFunc
) && TLI
.has(TheLibFunc
)))
286 bool Changed
= false;
288 if (F
.getParent() != nullptr && F
.getParent()->getRtLibUseGOT())
289 Changed
|= setNonLazyBind(F
);
291 switch (TheLibFunc
) {
296 case LibFunc_strnlen
:
298 Changed
|= setOnlyReadsMemory(F
);
299 Changed
|= setDoesNotThrow(F
);
300 Changed
|= setOnlyAccessesArgMemory(F
);
301 Changed
|= setWillReturn(F
);
302 Changed
|= setDoesNotCapture(F
, 0);
305 case LibFunc_strrchr
:
306 Changed
|= setOnlyAccessesArgMemory(F
);
307 Changed
|= setOnlyReadsMemory(F
);
308 Changed
|= setDoesNotThrow(F
);
309 Changed
|= setWillReturn(F
);
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);
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);
335 case LibFunc_strncpy
:
336 Changed
|= setReturnedArg(F
, 0);
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);
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);
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);
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
372 Changed
|= setOnlyReadsMemory(F
);
373 Changed
|= setDoesNotThrow(F
);
374 Changed
|= setWillReturn(F
);
375 Changed
|= setDoesNotCapture(F
, 0);
376 Changed
|= setDoesNotCapture(F
, 1);
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);
387 case LibFunc_strtok_r
:
388 Changed
|= setDoesNotThrow(F
);
389 Changed
|= setWillReturn(F
);
390 Changed
|= setDoesNotCapture(F
, 1);
391 Changed
|= setOnlyReadsMemory(F
, 1);
394 Changed
|= setRetAndArgsNoUndef(F
);
395 Changed
|= setDoesNotThrow(F
);
396 Changed
|= setDoesNotCapture(F
, 0);
397 Changed
|= setOnlyReadsMemory(F
, 0);
400 case LibFunc_setvbuf
:
401 Changed
|= setRetAndArgsNoUndef(F
);
402 Changed
|= setDoesNotThrow(F
);
403 Changed
|= setDoesNotCapture(F
, 0);
405 case LibFunc_strndup
:
406 Changed
|= setArgNoUndef(F
, 1);
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);
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);
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);
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);
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);
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);
460 // May throw; "system" is a valid pthread cancellation point.
461 Changed
|= setRetAndArgsNoUndef(F
);
462 Changed
|= setDoesNotCapture(F
, 0);
463 Changed
|= setOnlyReadsMemory(F
, 0);
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
);
472 case LibFunc_vec_malloc
:
473 Changed
|= setAllocFamily(F
, TheLibFunc
== LibFunc_vec_malloc
? "vec_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
);
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);
492 case LibFunc_memrchr
:
493 Changed
|= setDoesNotThrow(F
);
494 Changed
|= setOnlyAccessesArgMemory(F
);
495 Changed
|= setOnlyReadsMemory(F
);
496 Changed
|= setWillReturn(F
);
501 Changed
|= setDoesNotThrow(F
);
502 Changed
|= setWillReturn(F
);
503 Changed
|= setOnlyAccessesArgMemory(F
);
504 Changed
|= setOnlyWritesMemory(F
);
505 Changed
|= setDoesNotCapture(F
, 1);
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);
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);
527 case LibFunc_mempcpy
:
528 case LibFunc_memccpy
:
529 Changed
|= setWillReturn(F
);
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);
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
);
553 Changed
|= setRetAndArgsNoUndef(F
);
554 Changed
|= setDoesNotThrow(F
);
555 Changed
|= setDoesNotCapture(F
, 0);
556 Changed
|= setOnlyReadsMemory(F
, 0);
559 Changed
|= setRetAndArgsNoUndef(F
);
560 Changed
|= setDoesNotThrow(F
);
561 Changed
|= setWillReturn(F
);
562 Changed
|= setDoesNotCapture(F
, 0);
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);
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);
595 // May throw; "read" is a valid pthread cancellation point.
596 Changed
|= setRetAndArgsNoUndef(F
);
597 Changed
|= setDoesNotCapture(F
, 1);
600 Changed
|= setRetAndArgsNoUndef(F
);
601 Changed
|= setDoesNotThrow(F
);
602 Changed
|= setDoesNotCapture(F
, 0);
606 case LibFunc_realpath
:
607 Changed
|= setRetAndArgsNoUndef(F
);
608 Changed
|= setDoesNotThrow(F
);
609 Changed
|= setDoesNotCapture(F
, 0);
610 Changed
|= setOnlyReadsMemory(F
, 0);
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);
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);
628 // May throw; "write" is a valid pthread cancellation point.
629 Changed
|= setRetAndArgsNoUndef(F
);
630 Changed
|= setDoesNotCapture(F
, 1);
631 Changed
|= setOnlyReadsMemory(F
, 1);
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);
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);
651 Changed
|= setDoesNotThrow(F
);
652 Changed
|= setOnlyAccessesArgMemory(F
);
653 Changed
|= setWillReturn(F
);
654 Changed
|= setDoesNotCapture(F
, 0);
655 Changed
|= setOnlyWritesMemory(F
, 0);
658 case LibFunc_vec_calloc
:
659 Changed
|= setAllocFamily(F
, TheLibFunc
== LibFunc_vec_calloc
? "vec_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
);
671 Changed
|= setRetAndArgsNoUndef(F
);
672 Changed
|= setDoesNotThrow(F
);
673 Changed
|= setDoesNotCapture(F
, 0);
674 Changed
|= setOnlyReadsMemory(F
, 0);
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);
687 Changed
|= setDoesNotThrow(F
);
688 Changed
|= setOnlyReadsMemory(F
);
689 Changed
|= setWillReturn(F
);
690 Changed
|= setDoesNotCapture(F
, 0);
693 Changed
|= setRetAndArgsNoUndef(F
);
694 Changed
|= setDoesNotThrow(F
);
695 Changed
|= setDoesNotCapture(F
, 0);
696 Changed
|= setOnlyReadsMemory(F
, 0);
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);
708 Changed
|= setRetAndArgsNoUndef(F
);
709 Changed
|= setDoesNotThrow(F
);
710 Changed
|= setRetDoesNotAlias(F
);
711 Changed
|= setDoesNotCapture(F
, 1);
712 Changed
|= setOnlyReadsMemory(F
, 1);
715 Changed
|= setRetAndArgsNoUndef(F
);
716 Changed
|= setDoesNotThrow(F
);
717 Changed
|= setDoesNotCapture(F
, 0);
720 case LibFunc_vec_free
:
721 Changed
|= setAllocFamily(F
, TheLibFunc
== LibFunc_vec_free
? "vec_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);
734 case LibFunc_fgetc_unlocked
:
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);
749 Changed
|= setRetAndArgsNoUndef(F
);
750 Changed
|= setDoesNotThrow(F
);
751 Changed
|= setDoesNotCapture(F
, 0);
752 Changed
|= setOnlyReadsMemory(F
);
755 case LibFunc_fputc_unlocked
:
757 Changed
|= setRetAndArgsNoUndef(F
);
758 Changed
|= setDoesNotThrow(F
);
759 Changed
|= setDoesNotCapture(F
, 1);
764 Changed
|= setDoesNotThrow(F
);
765 Changed
|= setWillReturn(F
);
766 Changed
|= setOnlyAccessesArgMemory(F
);
767 Changed
|= setOnlyWritesMemory(F
);
768 Changed
|= setDoesNotCapture(F
, 1);
770 case LibFunc_fstatvfs
:
771 Changed
|= setRetAndArgsNoUndef(F
);
772 Changed
|= setDoesNotThrow(F
);
773 Changed
|= setDoesNotCapture(F
, 1);
776 case LibFunc_fgets_unlocked
:
777 Changed
|= setRetAndArgsNoUndef(F
);
778 Changed
|= setDoesNotThrow(F
);
779 Changed
|= setDoesNotCapture(F
, 2);
782 case LibFunc_fread_unlocked
:
783 Changed
|= setRetAndArgsNoUndef(F
);
784 Changed
|= setDoesNotThrow(F
);
785 Changed
|= setDoesNotCapture(F
, 0);
786 Changed
|= setDoesNotCapture(F
, 3);
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?
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);
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);
812 case LibFunc_fgetpos
:
813 Changed
|= setRetAndArgsNoUndef(F
);
814 Changed
|= setDoesNotThrow(F
);
815 Changed
|= setDoesNotCapture(F
, 0);
816 Changed
|= setDoesNotCapture(F
, 1);
819 Changed
|= setRetAndArgsNoUndef(F
);
820 Changed
|= setDoesNotThrow(F
);
821 Changed
|= setDoesNotCapture(F
, 0);
823 case LibFunc_getlogin_r
:
824 Changed
|= setRetAndArgsNoUndef(F
);
825 Changed
|= setDoesNotThrow(F
);
826 Changed
|= setDoesNotCapture(F
, 0);
828 case LibFunc_getc_unlocked
:
829 Changed
|= setRetAndArgsNoUndef(F
);
830 Changed
|= setDoesNotThrow(F
);
831 Changed
|= setDoesNotCapture(F
, 0);
834 Changed
|= setRetAndArgsNoUndef(F
);
835 Changed
|= setDoesNotThrow(F
);
836 Changed
|= setOnlyReadsMemory(F
);
837 Changed
|= setDoesNotCapture(F
, 0);
840 case LibFunc_getchar
:
841 case LibFunc_getchar_unlocked
:
842 Changed
|= setRetAndArgsNoUndef(F
);
843 Changed
|= setDoesNotThrow(F
);
845 case LibFunc_getitimer
:
846 Changed
|= setRetAndArgsNoUndef(F
);
847 Changed
|= setDoesNotThrow(F
);
848 Changed
|= setDoesNotCapture(F
, 1);
850 case LibFunc_getpwnam
:
851 Changed
|= setRetAndArgsNoUndef(F
);
852 Changed
|= setDoesNotThrow(F
);
853 Changed
|= setDoesNotCapture(F
, 0);
854 Changed
|= setOnlyReadsMemory(F
, 0);
857 Changed
|= setRetAndArgsNoUndef(F
);
858 Changed
|= setDoesNotThrow(F
);
859 Changed
|= setDoesNotCapture(F
, 1);
862 Changed
|= setRetAndArgsNoUndef(F
);
863 Changed
|= setDoesNotThrow(F
);
864 Changed
|= setDoesNotCapture(F
, 0);
867 Changed
|= setRetAndArgsNoUndef(F
);
868 Changed
|= setDoesNotThrow(F
);
869 Changed
|= setDoesNotCapture(F
, 0);
870 Changed
|= setOnlyReadsMemory(F
, 0);
872 case LibFunc_unsetenv
:
873 Changed
|= setRetAndArgsNoUndef(F
);
874 Changed
|= setDoesNotThrow(F
);
875 Changed
|= setDoesNotCapture(F
, 0);
876 Changed
|= setOnlyReadsMemory(F
, 0);
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);
888 case LibFunc_putc_unlocked
:
889 Changed
|= setRetAndArgsNoUndef(F
);
890 Changed
|= setDoesNotThrow(F
);
891 Changed
|= setDoesNotCapture(F
, 1);
896 Changed
|= setRetAndArgsNoUndef(F
);
897 Changed
|= setDoesNotThrow(F
);
898 Changed
|= setDoesNotCapture(F
, 0);
899 Changed
|= setOnlyReadsMemory(F
, 0);
902 // May throw; "pread" is a valid pthread cancellation point.
903 Changed
|= setRetAndArgsNoUndef(F
);
904 Changed
|= setDoesNotCapture(F
, 1);
907 // May throw; "pwrite" is a valid pthread cancellation point.
908 Changed
|= setRetAndArgsNoUndef(F
);
909 Changed
|= setDoesNotCapture(F
, 1);
910 Changed
|= setOnlyReadsMemory(F
, 1);
912 case LibFunc_putchar
:
913 case LibFunc_putchar_unlocked
:
914 Changed
|= setRetAndArgsNoUndef(F
);
915 Changed
|= setDoesNotThrow(F
);
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);
927 Changed
|= setRetAndArgsNoUndef(F
);
928 Changed
|= setDoesNotThrow(F
);
929 Changed
|= setDoesNotCapture(F
, 0);
932 Changed
|= setRetAndArgsNoUndef(F
);
933 Changed
|= setDoesNotThrow(F
);
934 Changed
|= setDoesNotCapture(F
, 0);
935 Changed
|= setOnlyReadsMemory(F
, 0);
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);
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);
952 case LibFunc_vprintf
:
953 Changed
|= setRetAndArgsNoUndef(F
);
954 Changed
|= setDoesNotThrow(F
);
955 Changed
|= setDoesNotCapture(F
, 0);
956 Changed
|= setOnlyReadsMemory(F
, 0);
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);
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);
974 // May throw; "open" is a valid pthread cancellation point.
975 Changed
|= setRetAndArgsNoUndef(F
);
976 Changed
|= setDoesNotCapture(F
, 0);
977 Changed
|= setOnlyReadsMemory(F
, 0);
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);
986 case LibFunc_tmpfile
:
987 Changed
|= setRetAndArgsNoUndef(F
);
988 Changed
|= setDoesNotThrow(F
);
989 Changed
|= setRetDoesNotAlias(F
);
992 Changed
|= setRetAndArgsNoUndef(F
);
993 Changed
|= setDoesNotThrow(F
);
994 Changed
|= setDoesNotCapture(F
, 0);
1000 Changed
|= setDoesNotThrow(F
);
1001 Changed
|= setDoesNotAccessMemory(F
);
1004 Changed
|= setRetAndArgsNoUndef(F
);
1005 Changed
|= setDoesNotThrow(F
);
1006 Changed
|= setDoesNotCapture(F
, 0);
1007 Changed
|= setDoesNotCapture(F
, 1);
1008 Changed
|= setOnlyReadsMemory(F
, 0);
1010 case LibFunc_lchown
:
1011 Changed
|= setRetAndArgsNoUndef(F
);
1012 Changed
|= setDoesNotThrow(F
);
1013 Changed
|= setDoesNotCapture(F
, 0);
1014 Changed
|= setOnlyReadsMemory(F
, 0);
1017 // May throw; places call through function pointer.
1018 // Cannot give undef pointer/size
1019 Changed
|= setRetAndArgsNoUndef(F
);
1020 Changed
|= setDoesNotCapture(F
, 3);
1022 case LibFunc_dunder_strndup
:
1023 Changed
|= setArgNoUndef(F
, 1);
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);
1032 case LibFunc_dunder_strtok_r
:
1033 Changed
|= setDoesNotThrow(F
);
1034 Changed
|= setDoesNotCapture(F
, 1);
1035 Changed
|= setOnlyReadsMemory(F
, 1);
1037 case LibFunc_under_IO_getc
:
1038 Changed
|= setRetAndArgsNoUndef(F
);
1039 Changed
|= setDoesNotThrow(F
);
1040 Changed
|= setDoesNotCapture(F
, 0);
1042 case LibFunc_under_IO_putc
:
1043 Changed
|= setRetAndArgsNoUndef(F
);
1044 Changed
|= setDoesNotThrow(F
);
1045 Changed
|= setDoesNotCapture(F
, 1);
1047 case LibFunc_dunder_isoc99_scanf
:
1048 Changed
|= setRetAndArgsNoUndef(F
);
1049 Changed
|= setDoesNotThrow(F
);
1050 Changed
|= setDoesNotCapture(F
, 0);
1051 Changed
|= setOnlyReadsMemory(F
, 0);
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);
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);
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);
1079 case LibFunc_fseeko64
:
1080 case LibFunc_ftello64
:
1081 Changed
|= setRetAndArgsNoUndef(F
);
1082 Changed
|= setDoesNotThrow(F
);
1083 Changed
|= setDoesNotCapture(F
, 0);
1085 case LibFunc_tmpfile64
:
1086 Changed
|= setRetAndArgsNoUndef(F
);
1087 Changed
|= setDoesNotThrow(F
);
1088 Changed
|= setRetDoesNotAlias(F
);
1090 case LibFunc_fstat64
:
1091 case LibFunc_fstatvfs64
:
1092 Changed
|= setRetAndArgsNoUndef(F
);
1093 Changed
|= setDoesNotThrow(F
);
1094 Changed
|= setDoesNotCapture(F
, 1);
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);
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
1106 Changed
|= setRetAndArgsNoUndef(F
);
1107 Changed
|= setDoesNotThrow(F
);
1108 Changed
|= setDoesNotCapture(F
, 0);
1109 Changed
|= setDoesNotCapture(F
, 1);
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);
1118 case LibFunc_memset
:
1119 Changed
|= setWillReturn(F
);
1121 case LibFunc_memset_chk
:
1122 Changed
|= setOnlyAccessesArgMemory(F
);
1123 Changed
|= setOnlyWritesMemory(F
, 0);
1124 Changed
|= setDoesNotThrow(F
);
1127 Changed
|= setIsCold(F
);
1129 case LibFunc_terminate
:
1130 Changed
|= setIsCold(F
);
1131 Changed
|= setNoReturn(F
);
1133 case LibFunc_cxa_throw
:
1134 Changed
|= setIsCold(F
);
1135 Changed
|= setNoReturn(F
);
1136 // Don't add `nofree` on `__cxa_throw`
1138 // int __nvvm_reflect(const char *)
1139 case LibFunc_nvvm_reflect
:
1140 Changed
|= setRetAndArgsNoUndef(F
);
1141 Changed
|= setDoesNotAccessMemory(F
);
1142 Changed
|= setDoesNotThrow(F
);
1145 case LibFunc_ldexpf
:
1146 case LibFunc_ldexpl
:
1147 Changed
|= setWillReturn(F
);
1149 case LibFunc_remquo
:
1150 case LibFunc_remquof
:
1151 case LibFunc_remquol
:
1152 Changed
|= setDoesNotCapture(F
, 2);
1158 case LibFunc_acoshf
:
1159 case LibFunc_acoshl
:
1164 case LibFunc_asinhf
:
1165 case LibFunc_asinhl
:
1169 case LibFunc_atan2f
:
1170 case LibFunc_atan2l
:
1173 case LibFunc_atanhf
:
1174 case LibFunc_atanhl
:
1182 case LibFunc_copysign
:
1183 case LibFunc_copysignf
:
1184 case LibFunc_copysignl
:
1192 case LibFunc_cospif
:
1196 case LibFunc_tgamma
:
1197 case LibFunc_tgammaf
:
1198 case LibFunc_tgammal
:
1206 case LibFunc_expm1f
:
1207 case LibFunc_expm1l
:
1218 case LibFunc_floorf
:
1219 case LibFunc_floorl
:
1233 case LibFunc_hypotf
:
1234 case LibFunc_hypotl
:
1235 case LibFunc_isascii
:
1236 case LibFunc_isdigit
:
1241 case LibFunc_log10f
:
1242 case LibFunc_log10l
:
1244 case LibFunc_log1pf
:
1245 case LibFunc_log1pl
:
1253 case LibFunc_ilogbf
:
1254 case LibFunc_ilogbl
:
1257 case LibFunc_nearbyint
:
1258 case LibFunc_nearbyintf
:
1259 case LibFunc_nearbyintl
:
1263 case LibFunc_remainder
:
1264 case LibFunc_remainderf
:
1265 case LibFunc_remainderl
:
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
:
1279 case LibFunc_sincospif_stret
:
1286 case LibFunc_sinpif
:
1296 case LibFunc_toascii
:
1298 case LibFunc_truncf
:
1299 case LibFunc_truncl
:
1300 Changed
|= setDoesNotThrow(F
);
1301 Changed
|= setDoesNotFreeMemory(F
);
1302 Changed
|= setOnlyWritesMemory(F
);
1303 Changed
|= setWillReturn(F
);
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
);
1318 // FIXME: It'd be really nice to cover all the library functions we're
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
);
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())
1348 const CallingConv::ID CC
= F
->getCallingConv();
1349 if (CC
!= CallingConv::C
&& CC
!= CallingConv::X86_StdCall
)
1352 const Module
*M
= F
->getParent();
1353 unsigned N
= M
->getNumberRegisterParameters();
1357 const DataLayout
&DL
= M
->getDataLayout();
1359 for (Argument
&A
: F
->args()) {
1360 Type
*T
= A
.getType();
1361 if (!T
->isIntOrPtrTy())
1364 const TypeSize
&TS
= DL
.getTypeAllocSize(T
);
1368 assert(TS
<= 4 && "Need to account for parameters larger than word size");
1369 const unsigned NumRegs
= TS
> 4 ? 2 : 1;
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
) {
1399 case LibFunc_putchar
:
1400 setArgExtAttr(*F
, 0, TLI
);
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
);
1410 case LibFunc_memccpy
:
1411 setArgExtAttr(*F
, 2, TLI
);
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.
1418 setRetExtAttr(*F
, TLI
);
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
:
1439 for (unsigned i
= 0; i
< T
->getNumParams(); i
++)
1440 assert(!isa
<IntegerType
>(T
->getParamType(i
)) &&
1441 "Unhandled integer argument.");
1446 markRegisterParameterAttributes(F
);
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
))
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
);
1473 bool llvm::isLibFuncEmittable(const Module
*M
, const TargetLibraryInfo
*TLI
,
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
:
1485 case Type::FloatTyID
:
1486 return isLibFuncEmittable(M
, TLI
, FloatFn
);
1487 case Type::DoubleTyID
:
1488 return isLibFuncEmittable(M
, TLI
, DoubleFn
);
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
);
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
))
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());
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
);
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
))
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());
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
,
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
);
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()) {
1775 if (Op
->getType()->isFloatTy())
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(),
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
1799 Attrs
.removeFnAttribute(B
.getContext(), Attribute::Speculatable
));
1800 if (const Function
*F
=
1801 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1802 CI
->setCallingConv(F
->getCallingConv());
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
);
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();
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
,
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
1849 Attrs
.removeFnAttribute(B
.getContext(), Attribute::Speculatable
));
1850 if (const Function
*F
=
1851 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1852 CI
->setCallingConv(F
->getCallingConv());
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
);
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();
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
))
1894 Type
*IntTy
= getIntTy(B
, TLI
);
1895 StringRef PutCharName
= TLI
->getName(LibFunc_putchar
);
1896 FunctionCallee PutChar
= getOrInsertLibFunc(M
, *TLI
, LibFunc_putchar
,
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());
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
))
1913 Type
*IntTy
= getIntTy(B
, TLI
);
1914 StringRef PutsName
= TLI
->getName(LibFunc_puts
);
1915 FunctionCallee PutS
= getOrInsertLibFunc(M
, *TLI
, LibFunc_puts
, IntTy
,
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());
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
))
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());
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
))
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());
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
))
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
);
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());
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
))
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());
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
))
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
);
2023 dyn_cast
<Function
>(Calloc
.getCallee()->stripPointerCasts()))
2024 CI
->setCallingConv(F
->getCallingConv());
2029 Value
*llvm::emitHotColdSizeReturningNew(Value
*Num
, IRBuilderBase
&B
,
2030 const TargetLibraryInfo
*TLI
,
2031 LibFunc SizeFeedbackNewFunc
,
2033 Module
*M
= B
.GetInsertBlock()->getModule();
2034 if (!isLibFuncEmittable(M
, TLI
, SizeFeedbackNewFunc
))
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());
2053 Value
*llvm::emitHotColdSizeReturningNewAligned(Value
*Num
, Value
*Align
,
2055 const TargetLibraryInfo
*TLI
,
2056 LibFunc SizeFeedbackNewFunc
,
2058 Module
*M
= B
.GetInsertBlock()->getModule();
2059 if (!isLibFuncEmittable(M
, TLI
, SizeFeedbackNewFunc
))
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
);
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());
2079 Value
*llvm::emitHotColdNew(Value
*Num
, IRBuilderBase
&B
,
2080 const TargetLibraryInfo
*TLI
, LibFunc NewFunc
,
2082 Module
*M
= B
.GetInsertBlock()->getModule();
2083 if (!isLibFuncEmittable(M
, TLI
, NewFunc
))
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());
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
))
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());
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
))
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());
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
))
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
);
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());