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/Function.h"
23 #include "llvm/IR/IRBuilder.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/Type.h"
26 #include "llvm/Support/TypeSize.h"
31 #define DEBUG_TYPE "build-libcalls"
33 //- Infer Attributes ---------------------------------------------------------//
35 STATISTIC(NumReadNone
, "Number of functions inferred as readnone");
36 STATISTIC(NumInaccessibleMemOnly
,
37 "Number of functions inferred as inaccessiblememonly");
38 STATISTIC(NumReadOnly
, "Number of functions inferred as readonly");
39 STATISTIC(NumWriteOnly
, "Number of functions inferred as writeonly");
40 STATISTIC(NumArgMemOnly
, "Number of functions inferred as argmemonly");
41 STATISTIC(NumInaccessibleMemOrArgMemOnly
,
42 "Number of functions inferred as inaccessiblemem_or_argmemonly");
43 STATISTIC(NumNoUnwind
, "Number of functions inferred as nounwind");
44 STATISTIC(NumNoCapture
, "Number of arguments inferred as nocapture");
45 STATISTIC(NumWriteOnlyArg
, "Number of arguments inferred as writeonly");
46 STATISTIC(NumReadOnlyArg
, "Number of arguments inferred as readonly");
47 STATISTIC(NumNoAlias
, "Number of function returns inferred as noalias");
48 STATISTIC(NumNoUndef
, "Number of function returns inferred as noundef returns");
49 STATISTIC(NumReturnedArg
, "Number of arguments inferred as returned");
50 STATISTIC(NumWillReturn
, "Number of functions inferred as willreturn");
52 static bool setDoesNotAccessMemory(Function
&F
) {
53 if (F
.doesNotAccessMemory())
55 F
.setDoesNotAccessMemory();
60 static bool setOnlyAccessesInaccessibleMemory(Function
&F
) {
61 if (F
.onlyAccessesInaccessibleMemory())
63 F
.setOnlyAccessesInaccessibleMemory();
64 ++NumInaccessibleMemOnly
;
68 static bool setOnlyReadsMemory(Function
&F
) {
69 if (F
.onlyReadsMemory())
71 F
.setOnlyReadsMemory();
76 static bool setOnlyWritesMemory(Function
&F
) {
77 if (F
.onlyWritesMemory()) // writeonly or readnone
80 F
.setOnlyWritesMemory();
84 static bool setOnlyAccessesArgMemory(Function
&F
) {
85 if (F
.onlyAccessesArgMemory())
87 F
.setOnlyAccessesArgMemory();
92 static bool setOnlyAccessesInaccessibleMemOrArgMem(Function
&F
) {
93 if (F
.onlyAccessesInaccessibleMemOrArgMem())
95 F
.setOnlyAccessesInaccessibleMemOrArgMem();
96 ++NumInaccessibleMemOrArgMemOnly
;
100 static bool setDoesNotThrow(Function
&F
) {
101 if (F
.doesNotThrow())
108 static bool setRetDoesNotAlias(Function
&F
) {
109 if (F
.hasRetAttribute(Attribute::NoAlias
))
111 F
.addRetAttr(Attribute::NoAlias
);
116 static bool setDoesNotCapture(Function
&F
, unsigned ArgNo
) {
117 if (F
.hasParamAttribute(ArgNo
, Attribute::NoCapture
))
119 F
.addParamAttr(ArgNo
, Attribute::NoCapture
);
124 static bool setDoesNotAlias(Function
&F
, unsigned ArgNo
) {
125 if (F
.hasParamAttribute(ArgNo
, Attribute::NoAlias
))
127 F
.addParamAttr(ArgNo
, Attribute::NoAlias
);
132 static bool setOnlyReadsMemory(Function
&F
, unsigned ArgNo
) {
133 if (F
.hasParamAttribute(ArgNo
, Attribute::ReadOnly
))
135 F
.addParamAttr(ArgNo
, Attribute::ReadOnly
);
140 static bool setOnlyWritesMemory(Function
&F
, unsigned ArgNo
) {
141 if (F
.hasParamAttribute(ArgNo
, Attribute::WriteOnly
))
143 F
.addParamAttr(ArgNo
, Attribute::WriteOnly
);
148 static bool setRetNoUndef(Function
&F
) {
149 if (!F
.getReturnType()->isVoidTy() &&
150 !F
.hasRetAttribute(Attribute::NoUndef
)) {
151 F
.addRetAttr(Attribute::NoUndef
);
158 static bool setArgsNoUndef(Function
&F
) {
159 bool Changed
= false;
160 for (unsigned ArgNo
= 0; ArgNo
< F
.arg_size(); ++ArgNo
) {
161 if (!F
.hasParamAttribute(ArgNo
, Attribute::NoUndef
)) {
162 F
.addParamAttr(ArgNo
, Attribute::NoUndef
);
170 static bool setArgNoUndef(Function
&F
, unsigned ArgNo
) {
171 if (F
.hasParamAttribute(ArgNo
, Attribute::NoUndef
))
173 F
.addParamAttr(ArgNo
, Attribute::NoUndef
);
178 static bool setRetAndArgsNoUndef(Function
&F
) {
179 bool UndefAdded
= false;
180 UndefAdded
|= setRetNoUndef(F
);
181 UndefAdded
|= setArgsNoUndef(F
);
185 static bool setReturnedArg(Function
&F
, unsigned ArgNo
) {
186 if (F
.hasParamAttribute(ArgNo
, Attribute::Returned
))
188 F
.addParamAttr(ArgNo
, Attribute::Returned
);
193 static bool setNonLazyBind(Function
&F
) {
194 if (F
.hasFnAttribute(Attribute::NonLazyBind
))
196 F
.addFnAttr(Attribute::NonLazyBind
);
200 static bool setDoesNotFreeMemory(Function
&F
) {
201 if (F
.hasFnAttribute(Attribute::NoFree
))
203 F
.addFnAttr(Attribute::NoFree
);
207 static bool setWillReturn(Function
&F
) {
208 if (F
.hasFnAttribute(Attribute::WillReturn
))
210 F
.addFnAttr(Attribute::WillReturn
);
215 static bool setAlignedAllocParam(Function
&F
, unsigned ArgNo
) {
216 if (F
.hasParamAttribute(ArgNo
, Attribute::AllocAlign
))
218 F
.addParamAttr(ArgNo
, Attribute::AllocAlign
);
222 static bool setAllocatedPointerParam(Function
&F
, unsigned ArgNo
) {
223 if (F
.hasParamAttribute(ArgNo
, Attribute::AllocatedPointer
))
225 F
.addParamAttr(ArgNo
, Attribute::AllocatedPointer
);
229 static bool setAllocSize(Function
&F
, unsigned ElemSizeArg
,
230 std::optional
<unsigned> NumElemsArg
) {
231 if (F
.hasFnAttribute(Attribute::AllocSize
))
233 F
.addFnAttr(Attribute::getWithAllocSizeArgs(F
.getContext(), ElemSizeArg
,
238 static bool setAllocFamily(Function
&F
, StringRef Family
) {
239 if (F
.hasFnAttribute("alloc-family"))
241 F
.addFnAttr("alloc-family", Family
);
245 static bool setAllocKind(Function
&F
, AllocFnKind K
) {
246 if (F
.hasFnAttribute(Attribute::AllocKind
))
249 Attribute::get(F
.getContext(), Attribute::AllocKind
, uint64_t(K
)));
253 bool llvm::inferNonMandatoryLibFuncAttrs(Module
*M
, StringRef Name
,
254 const TargetLibraryInfo
&TLI
) {
255 Function
*F
= M
->getFunction(Name
);
258 return inferNonMandatoryLibFuncAttrs(*F
, TLI
);
261 bool llvm::inferNonMandatoryLibFuncAttrs(Function
&F
,
262 const TargetLibraryInfo
&TLI
) {
264 if (!(TLI
.getLibFunc(F
, TheLibFunc
) && TLI
.has(TheLibFunc
)))
267 bool Changed
= false;
269 if (F
.getParent() != nullptr && F
.getParent()->getRtLibUseGOT())
270 Changed
|= setNonLazyBind(F
);
272 switch (TheLibFunc
) {
274 case LibFunc_strnlen
:
276 Changed
|= setOnlyReadsMemory(F
);
277 Changed
|= setDoesNotThrow(F
);
278 Changed
|= setOnlyAccessesArgMemory(F
);
279 Changed
|= setWillReturn(F
);
280 Changed
|= setDoesNotCapture(F
, 0);
283 case LibFunc_strrchr
:
284 Changed
|= setOnlyAccessesArgMemory(F
);
285 Changed
|= setOnlyReadsMemory(F
);
286 Changed
|= setDoesNotThrow(F
);
287 Changed
|= setWillReturn(F
);
292 case LibFunc_strtoul
:
293 case LibFunc_strtoll
:
294 case LibFunc_strtold
:
295 case LibFunc_strtoull
:
296 Changed
|= setDoesNotThrow(F
);
297 Changed
|= setWillReturn(F
);
298 Changed
|= setDoesNotCapture(F
, 1);
299 Changed
|= setOnlyReadsMemory(F
, 0);
302 case LibFunc_strncat
:
303 Changed
|= setOnlyAccessesArgMemory(F
);
304 Changed
|= setDoesNotThrow(F
);
305 Changed
|= setWillReturn(F
);
306 Changed
|= setReturnedArg(F
, 0);
307 Changed
|= setDoesNotCapture(F
, 1);
308 Changed
|= setOnlyReadsMemory(F
, 1);
309 Changed
|= setDoesNotAlias(F
, 0);
310 Changed
|= setDoesNotAlias(F
, 1);
313 case LibFunc_strncpy
:
314 Changed
|= setReturnedArg(F
, 0);
317 case LibFunc_stpncpy
:
318 Changed
|= setOnlyAccessesArgMemory(F
);
319 Changed
|= setDoesNotThrow(F
);
320 Changed
|= setWillReturn(F
);
321 Changed
|= setDoesNotCapture(F
, 1);
322 Changed
|= setOnlyWritesMemory(F
, 0);
323 Changed
|= setOnlyReadsMemory(F
, 1);
324 Changed
|= setDoesNotAlias(F
, 0);
325 Changed
|= setDoesNotAlias(F
, 1);
327 case LibFunc_strxfrm
:
328 Changed
|= setDoesNotThrow(F
);
329 Changed
|= setWillReturn(F
);
330 Changed
|= setDoesNotCapture(F
, 0);
331 Changed
|= setDoesNotCapture(F
, 1);
332 Changed
|= setOnlyReadsMemory(F
, 1);
334 case LibFunc_strcmp
: // 0,1
335 case LibFunc_strspn
: // 0,1
336 case LibFunc_strncmp
: // 0,1
337 case LibFunc_strcspn
: // 0,1
338 Changed
|= setDoesNotThrow(F
);
339 Changed
|= setOnlyAccessesArgMemory(F
);
340 Changed
|= setWillReturn(F
);
341 Changed
|= setOnlyReadsMemory(F
);
342 Changed
|= setDoesNotCapture(F
, 0);
343 Changed
|= setDoesNotCapture(F
, 1);
345 case LibFunc_strcoll
:
346 case LibFunc_strcasecmp
: // 0,1
347 case LibFunc_strncasecmp
: //
348 // Those functions may depend on the locale, which may be accessed through
350 Changed
|= setOnlyReadsMemory(F
);
351 Changed
|= setDoesNotThrow(F
);
352 Changed
|= setWillReturn(F
);
353 Changed
|= setDoesNotCapture(F
, 0);
354 Changed
|= setDoesNotCapture(F
, 1);
357 case LibFunc_strpbrk
:
358 Changed
|= setOnlyAccessesArgMemory(F
);
359 Changed
|= setOnlyReadsMemory(F
);
360 Changed
|= setDoesNotThrow(F
);
361 Changed
|= setWillReturn(F
);
362 Changed
|= setDoesNotCapture(F
, 1);
365 case LibFunc_strtok_r
:
366 Changed
|= setDoesNotThrow(F
);
367 Changed
|= setWillReturn(F
);
368 Changed
|= setDoesNotCapture(F
, 1);
369 Changed
|= setOnlyReadsMemory(F
, 1);
372 Changed
|= setRetAndArgsNoUndef(F
);
373 Changed
|= setDoesNotThrow(F
);
374 Changed
|= setDoesNotCapture(F
, 0);
375 Changed
|= setOnlyReadsMemory(F
, 0);
378 case LibFunc_setvbuf
:
379 Changed
|= setRetAndArgsNoUndef(F
);
380 Changed
|= setDoesNotThrow(F
);
381 Changed
|= setDoesNotCapture(F
, 0);
383 case LibFunc_strndup
:
384 Changed
|= setArgNoUndef(F
, 1);
387 Changed
|= setAllocFamily(F
, "malloc");
388 Changed
|= setOnlyAccessesInaccessibleMemOrArgMem(F
);
389 Changed
|= setDoesNotThrow(F
);
390 Changed
|= setRetDoesNotAlias(F
);
391 Changed
|= setWillReturn(F
);
392 Changed
|= setDoesNotCapture(F
, 0);
393 Changed
|= setOnlyReadsMemory(F
, 0);
396 case LibFunc_statvfs
:
397 Changed
|= setRetAndArgsNoUndef(F
);
398 Changed
|= setDoesNotThrow(F
);
399 Changed
|= setDoesNotCapture(F
, 0);
400 Changed
|= setDoesNotCapture(F
, 1);
401 Changed
|= setOnlyReadsMemory(F
, 0);
404 Changed
|= setRetAndArgsNoUndef(F
);
405 Changed
|= setDoesNotThrow(F
);
406 Changed
|= setDoesNotCapture(F
, 0);
407 Changed
|= setDoesNotCapture(F
, 1);
408 Changed
|= setOnlyReadsMemory(F
, 0);
409 Changed
|= setOnlyReadsMemory(F
, 1);
411 case LibFunc_sprintf
:
412 Changed
|= setRetAndArgsNoUndef(F
);
413 Changed
|= setDoesNotThrow(F
);
414 Changed
|= setDoesNotCapture(F
, 0);
415 Changed
|= setDoesNotAlias(F
, 0);
416 Changed
|= setOnlyWritesMemory(F
, 0);
417 Changed
|= setDoesNotCapture(F
, 1);
418 Changed
|= setOnlyReadsMemory(F
, 1);
420 case LibFunc_snprintf
:
421 Changed
|= setRetAndArgsNoUndef(F
);
422 Changed
|= setDoesNotThrow(F
);
423 Changed
|= setDoesNotCapture(F
, 0);
424 Changed
|= setDoesNotAlias(F
, 0);
425 Changed
|= setOnlyWritesMemory(F
, 0);
426 Changed
|= setDoesNotCapture(F
, 2);
427 Changed
|= setOnlyReadsMemory(F
, 2);
429 case LibFunc_setitimer
:
430 Changed
|= setRetAndArgsNoUndef(F
);
431 Changed
|= setDoesNotThrow(F
);
432 Changed
|= setWillReturn(F
);
433 Changed
|= setDoesNotCapture(F
, 1);
434 Changed
|= setDoesNotCapture(F
, 2);
435 Changed
|= setOnlyReadsMemory(F
, 1);
438 // May throw; "system" is a valid pthread cancellation point.
439 Changed
|= setRetAndArgsNoUndef(F
);
440 Changed
|= setDoesNotCapture(F
, 0);
441 Changed
|= setOnlyReadsMemory(F
, 0);
443 case LibFunc_aligned_alloc
:
444 Changed
|= setAlignedAllocParam(F
, 0);
445 Changed
|= setAllocSize(F
, 1, std::nullopt
);
446 Changed
|= setAllocKind(F
, AllocFnKind::Alloc
| AllocFnKind::Uninitialized
| AllocFnKind::Aligned
);
450 case LibFunc_vec_malloc
:
451 Changed
|= setAllocFamily(F
, TheLibFunc
== LibFunc_vec_malloc
? "vec_malloc"
453 Changed
|= setAllocKind(F
, AllocFnKind::Alloc
| AllocFnKind::Uninitialized
);
454 Changed
|= setAllocSize(F
, 0, std::nullopt
);
455 Changed
|= setOnlyAccessesInaccessibleMemory(F
);
456 Changed
|= setRetAndArgsNoUndef(F
);
457 Changed
|= setDoesNotThrow(F
);
458 Changed
|= setRetDoesNotAlias(F
);
459 Changed
|= setWillReturn(F
);
462 Changed
|= setOnlyAccessesArgMemory(F
);
463 Changed
|= setOnlyReadsMemory(F
);
464 Changed
|= setDoesNotThrow(F
);
465 Changed
|= setWillReturn(F
);
466 Changed
|= setDoesNotCapture(F
, 0);
467 Changed
|= setDoesNotCapture(F
, 1);
470 case LibFunc_memrchr
:
471 Changed
|= setDoesNotThrow(F
);
472 Changed
|= setOnlyAccessesArgMemory(F
);
473 Changed
|= setOnlyReadsMemory(F
);
474 Changed
|= setWillReturn(F
);
479 Changed
|= setDoesNotThrow(F
);
480 Changed
|= setWillReturn(F
);
481 Changed
|= setOnlyAccessesArgMemory(F
);
482 Changed
|= setOnlyWritesMemory(F
);
483 Changed
|= setDoesNotCapture(F
, 1);
486 Changed
|= setDoesNotThrow(F
);
487 Changed
|= setOnlyAccessesArgMemory(F
);
488 Changed
|= setWillReturn(F
);
489 Changed
|= setDoesNotAlias(F
, 0);
490 Changed
|= setReturnedArg(F
, 0);
491 Changed
|= setOnlyWritesMemory(F
, 0);
492 Changed
|= setDoesNotAlias(F
, 1);
493 Changed
|= setDoesNotCapture(F
, 1);
494 Changed
|= setOnlyReadsMemory(F
, 1);
496 case LibFunc_memmove
:
497 Changed
|= setDoesNotThrow(F
);
498 Changed
|= setOnlyAccessesArgMemory(F
);
499 Changed
|= setWillReturn(F
);
500 Changed
|= setReturnedArg(F
, 0);
501 Changed
|= setOnlyWritesMemory(F
, 0);
502 Changed
|= setDoesNotCapture(F
, 1);
503 Changed
|= setOnlyReadsMemory(F
, 1);
505 case LibFunc_mempcpy
:
506 case LibFunc_memccpy
:
507 Changed
|= setWillReturn(F
);
509 case LibFunc_memcpy_chk
:
510 Changed
|= setDoesNotThrow(F
);
511 Changed
|= setOnlyAccessesArgMemory(F
);
512 Changed
|= setDoesNotAlias(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_memalign
:
519 Changed
|= setAllocFamily(F
, "malloc");
520 Changed
|= setAllocKind(F
, AllocFnKind::Alloc
| AllocFnKind::Aligned
|
521 AllocFnKind::Uninitialized
);
522 Changed
|= setAllocSize(F
, 1, std::nullopt
);
523 Changed
|= setAlignedAllocParam(F
, 0);
524 Changed
|= setOnlyAccessesInaccessibleMemory(F
);
525 Changed
|= setRetNoUndef(F
);
526 Changed
|= setDoesNotThrow(F
);
527 Changed
|= setRetDoesNotAlias(F
);
528 Changed
|= setWillReturn(F
);
531 Changed
|= setRetAndArgsNoUndef(F
);
532 Changed
|= setDoesNotThrow(F
);
533 Changed
|= setDoesNotCapture(F
, 0);
534 Changed
|= setOnlyReadsMemory(F
, 0);
537 Changed
|= setRetAndArgsNoUndef(F
);
538 Changed
|= setDoesNotThrow(F
);
539 Changed
|= setWillReturn(F
);
540 Changed
|= setDoesNotCapture(F
, 0);
542 case LibFunc_realloc
:
543 case LibFunc_reallocf
:
544 case LibFunc_vec_realloc
:
545 Changed
|= setAllocFamily(
546 F
, TheLibFunc
== LibFunc_vec_realloc
? "vec_malloc" : "malloc");
547 Changed
|= setAllocKind(F
, AllocFnKind::Realloc
);
548 Changed
|= setAllocatedPointerParam(F
, 0);
549 Changed
|= setAllocSize(F
, 1, std::nullopt
);
550 Changed
|= setOnlyAccessesInaccessibleMemOrArgMem(F
);
551 Changed
|= setRetNoUndef(F
);
552 Changed
|= setDoesNotThrow(F
);
553 Changed
|= setRetDoesNotAlias(F
);
554 Changed
|= setWillReturn(F
);
555 Changed
|= setDoesNotCapture(F
, 0);
556 Changed
|= setArgNoUndef(F
, 1);
559 // May throw; "read" is a valid pthread cancellation point.
560 Changed
|= setRetAndArgsNoUndef(F
);
561 Changed
|= setDoesNotCapture(F
, 1);
564 Changed
|= setRetAndArgsNoUndef(F
);
565 Changed
|= setDoesNotThrow(F
);
566 Changed
|= setDoesNotCapture(F
, 0);
570 case LibFunc_realpath
:
571 Changed
|= setRetAndArgsNoUndef(F
);
572 Changed
|= setDoesNotThrow(F
);
573 Changed
|= setDoesNotCapture(F
, 0);
574 Changed
|= setOnlyReadsMemory(F
, 0);
577 Changed
|= setRetAndArgsNoUndef(F
);
578 Changed
|= setDoesNotThrow(F
);
579 Changed
|= setDoesNotCapture(F
, 0);
580 Changed
|= setDoesNotCapture(F
, 1);
581 Changed
|= setOnlyReadsMemory(F
, 0);
582 Changed
|= setOnlyReadsMemory(F
, 1);
584 case LibFunc_readlink
:
585 Changed
|= setRetAndArgsNoUndef(F
);
586 Changed
|= setDoesNotThrow(F
);
587 Changed
|= setDoesNotCapture(F
, 0);
588 Changed
|= setDoesNotCapture(F
, 1);
589 Changed
|= setOnlyReadsMemory(F
, 0);
592 // May throw; "write" is a valid pthread cancellation point.
593 Changed
|= setRetAndArgsNoUndef(F
);
594 Changed
|= setDoesNotCapture(F
, 1);
595 Changed
|= setOnlyReadsMemory(F
, 1);
598 Changed
|= setDoesNotThrow(F
);
599 Changed
|= setOnlyAccessesArgMemory(F
);
600 Changed
|= setWillReturn(F
);
601 Changed
|= setDoesNotCapture(F
, 0);
602 Changed
|= setOnlyReadsMemory(F
, 0);
603 Changed
|= setOnlyWritesMemory(F
, 1);
604 Changed
|= setDoesNotCapture(F
, 1);
607 Changed
|= setDoesNotThrow(F
);
608 Changed
|= setOnlyAccessesArgMemory(F
);
609 Changed
|= setOnlyReadsMemory(F
);
610 Changed
|= setWillReturn(F
);
611 Changed
|= setDoesNotCapture(F
, 0);
612 Changed
|= setDoesNotCapture(F
, 1);
615 Changed
|= setDoesNotThrow(F
);
616 Changed
|= setOnlyAccessesArgMemory(F
);
617 Changed
|= setWillReturn(F
);
618 Changed
|= setDoesNotCapture(F
, 0);
619 Changed
|= setOnlyWritesMemory(F
, 0);
622 case LibFunc_vec_calloc
:
623 Changed
|= setAllocFamily(F
, TheLibFunc
== LibFunc_vec_calloc
? "vec_malloc"
625 Changed
|= setAllocKind(F
, AllocFnKind::Alloc
| AllocFnKind::Zeroed
);
626 Changed
|= setAllocSize(F
, 0, 1);
627 Changed
|= setOnlyAccessesInaccessibleMemory(F
);
628 Changed
|= setRetAndArgsNoUndef(F
);
629 Changed
|= setDoesNotThrow(F
);
630 Changed
|= setRetDoesNotAlias(F
);
631 Changed
|= setWillReturn(F
);
635 Changed
|= setRetAndArgsNoUndef(F
);
636 Changed
|= setDoesNotThrow(F
);
637 Changed
|= setDoesNotCapture(F
, 0);
638 Changed
|= setOnlyReadsMemory(F
, 0);
640 case LibFunc_ctermid
:
641 case LibFunc_clearerr
:
642 case LibFunc_closedir
:
643 Changed
|= setRetAndArgsNoUndef(F
);
644 Changed
|= setDoesNotThrow(F
);
645 Changed
|= setDoesNotCapture(F
, 0);
651 Changed
|= setDoesNotThrow(F
);
652 Changed
|= setOnlyReadsMemory(F
);
653 Changed
|= setWillReturn(F
);
654 Changed
|= setDoesNotCapture(F
, 0);
657 Changed
|= setRetAndArgsNoUndef(F
);
658 Changed
|= setDoesNotThrow(F
);
659 Changed
|= setDoesNotCapture(F
, 0);
660 Changed
|= setOnlyReadsMemory(F
, 0);
663 Changed
|= setRetAndArgsNoUndef(F
);
664 Changed
|= setDoesNotThrow(F
);
665 Changed
|= setRetDoesNotAlias(F
);
666 Changed
|= setDoesNotCapture(F
, 0);
667 Changed
|= setDoesNotCapture(F
, 1);
668 Changed
|= setOnlyReadsMemory(F
, 0);
669 Changed
|= setOnlyReadsMemory(F
, 1);
672 Changed
|= setRetAndArgsNoUndef(F
);
673 Changed
|= setDoesNotThrow(F
);
674 Changed
|= setRetDoesNotAlias(F
);
675 Changed
|= setDoesNotCapture(F
, 1);
676 Changed
|= setOnlyReadsMemory(F
, 1);
679 Changed
|= setRetAndArgsNoUndef(F
);
680 Changed
|= setDoesNotThrow(F
);
681 Changed
|= setDoesNotCapture(F
, 0);
684 case LibFunc_vec_free
:
685 Changed
|= setAllocFamily(F
, TheLibFunc
== LibFunc_vec_free
? "vec_malloc"
687 Changed
|= setAllocKind(F
, AllocFnKind::Free
);
688 Changed
|= setAllocatedPointerParam(F
, 0);
689 Changed
|= setOnlyAccessesInaccessibleMemOrArgMem(F
);
690 Changed
|= setArgsNoUndef(F
);
691 Changed
|= setDoesNotThrow(F
);
692 Changed
|= setWillReturn(F
);
693 Changed
|= setDoesNotCapture(F
, 0);
698 case LibFunc_fgetc_unlocked
:
704 case LibFunc_fsetpos
:
705 case LibFunc_flockfile
:
706 case LibFunc_funlockfile
:
707 case LibFunc_ftrylockfile
:
708 Changed
|= setRetAndArgsNoUndef(F
);
709 Changed
|= setDoesNotThrow(F
);
710 Changed
|= setDoesNotCapture(F
, 0);
713 Changed
|= setRetAndArgsNoUndef(F
);
714 Changed
|= setDoesNotThrow(F
);
715 Changed
|= setDoesNotCapture(F
, 0);
716 Changed
|= setOnlyReadsMemory(F
);
719 case LibFunc_fputc_unlocked
:
721 Changed
|= setRetAndArgsNoUndef(F
);
722 Changed
|= setDoesNotThrow(F
);
723 Changed
|= setDoesNotCapture(F
, 1);
728 Changed
|= setDoesNotThrow(F
);
729 Changed
|= setWillReturn(F
);
730 Changed
|= setOnlyAccessesArgMemory(F
);
731 Changed
|= setOnlyWritesMemory(F
);
732 Changed
|= setDoesNotCapture(F
, 1);
734 case LibFunc_fstatvfs
:
735 Changed
|= setRetAndArgsNoUndef(F
);
736 Changed
|= setDoesNotThrow(F
);
737 Changed
|= setDoesNotCapture(F
, 1);
740 case LibFunc_fgets_unlocked
:
741 Changed
|= setRetAndArgsNoUndef(F
);
742 Changed
|= setDoesNotThrow(F
);
743 Changed
|= setDoesNotCapture(F
, 2);
746 case LibFunc_fread_unlocked
:
747 Changed
|= setRetAndArgsNoUndef(F
);
748 Changed
|= setDoesNotThrow(F
);
749 Changed
|= setDoesNotCapture(F
, 0);
750 Changed
|= setDoesNotCapture(F
, 3);
753 case LibFunc_fwrite_unlocked
:
754 Changed
|= setRetAndArgsNoUndef(F
);
755 Changed
|= setDoesNotThrow(F
);
756 Changed
|= setDoesNotCapture(F
, 0);
757 Changed
|= setDoesNotCapture(F
, 3);
758 // FIXME: readonly #1?
761 case LibFunc_fputs_unlocked
:
762 Changed
|= setRetAndArgsNoUndef(F
);
763 Changed
|= setDoesNotThrow(F
);
764 Changed
|= setDoesNotCapture(F
, 0);
765 Changed
|= setDoesNotCapture(F
, 1);
766 Changed
|= setOnlyReadsMemory(F
, 0);
769 case LibFunc_fprintf
:
770 Changed
|= setRetAndArgsNoUndef(F
);
771 Changed
|= setDoesNotThrow(F
);
772 Changed
|= setDoesNotCapture(F
, 0);
773 Changed
|= setDoesNotCapture(F
, 1);
774 Changed
|= setOnlyReadsMemory(F
, 1);
776 case LibFunc_fgetpos
:
777 Changed
|= setRetAndArgsNoUndef(F
);
778 Changed
|= setDoesNotThrow(F
);
779 Changed
|= setDoesNotCapture(F
, 0);
780 Changed
|= setDoesNotCapture(F
, 1);
783 Changed
|= setRetAndArgsNoUndef(F
);
784 Changed
|= setDoesNotThrow(F
);
785 Changed
|= setDoesNotCapture(F
, 0);
787 case LibFunc_getlogin_r
:
788 Changed
|= setRetAndArgsNoUndef(F
);
789 Changed
|= setDoesNotThrow(F
);
790 Changed
|= setDoesNotCapture(F
, 0);
792 case LibFunc_getc_unlocked
:
793 Changed
|= setRetAndArgsNoUndef(F
);
794 Changed
|= setDoesNotThrow(F
);
795 Changed
|= setDoesNotCapture(F
, 0);
798 Changed
|= setRetAndArgsNoUndef(F
);
799 Changed
|= setDoesNotThrow(F
);
800 Changed
|= setOnlyReadsMemory(F
);
801 Changed
|= setDoesNotCapture(F
, 0);
804 case LibFunc_getchar
:
805 case LibFunc_getchar_unlocked
:
806 Changed
|= setRetAndArgsNoUndef(F
);
807 Changed
|= setDoesNotThrow(F
);
809 case LibFunc_getitimer
:
810 Changed
|= setRetAndArgsNoUndef(F
);
811 Changed
|= setDoesNotThrow(F
);
812 Changed
|= setDoesNotCapture(F
, 1);
814 case LibFunc_getpwnam
:
815 Changed
|= setRetAndArgsNoUndef(F
);
816 Changed
|= setDoesNotThrow(F
);
817 Changed
|= setDoesNotCapture(F
, 0);
818 Changed
|= setOnlyReadsMemory(F
, 0);
821 Changed
|= setRetAndArgsNoUndef(F
);
822 Changed
|= setDoesNotThrow(F
);
823 Changed
|= setDoesNotCapture(F
, 1);
826 Changed
|= setRetAndArgsNoUndef(F
);
827 Changed
|= setDoesNotThrow(F
);
828 Changed
|= setDoesNotCapture(F
, 0);
831 Changed
|= setRetAndArgsNoUndef(F
);
832 Changed
|= setDoesNotThrow(F
);
833 Changed
|= setDoesNotCapture(F
, 0);
834 Changed
|= setOnlyReadsMemory(F
, 0);
836 case LibFunc_unsetenv
:
837 Changed
|= setRetAndArgsNoUndef(F
);
838 Changed
|= setDoesNotThrow(F
);
839 Changed
|= setDoesNotCapture(F
, 0);
840 Changed
|= setOnlyReadsMemory(F
, 0);
844 Changed
|= setRetAndArgsNoUndef(F
);
845 Changed
|= setDoesNotThrow(F
);
846 Changed
|= setDoesNotCapture(F
, 0);
847 Changed
|= setDoesNotCapture(F
, 1);
848 Changed
|= setOnlyReadsMemory(F
, 0);
849 Changed
|= setOnlyReadsMemory(F
, 1);
852 case LibFunc_putc_unlocked
:
853 Changed
|= setRetAndArgsNoUndef(F
);
854 Changed
|= setDoesNotThrow(F
);
855 Changed
|= setDoesNotCapture(F
, 1);
860 Changed
|= setRetAndArgsNoUndef(F
);
861 Changed
|= setDoesNotThrow(F
);
862 Changed
|= setDoesNotCapture(F
, 0);
863 Changed
|= setOnlyReadsMemory(F
, 0);
866 // May throw; "pread" is a valid pthread cancellation point.
867 Changed
|= setRetAndArgsNoUndef(F
);
868 Changed
|= setDoesNotCapture(F
, 1);
871 // May throw; "pwrite" is a valid pthread cancellation point.
872 Changed
|= setRetAndArgsNoUndef(F
);
873 Changed
|= setDoesNotCapture(F
, 1);
874 Changed
|= setOnlyReadsMemory(F
, 1);
876 case LibFunc_putchar
:
877 case LibFunc_putchar_unlocked
:
878 Changed
|= setRetAndArgsNoUndef(F
);
879 Changed
|= setDoesNotThrow(F
);
882 Changed
|= setRetAndArgsNoUndef(F
);
883 Changed
|= setDoesNotThrow(F
);
884 Changed
|= setRetDoesNotAlias(F
);
885 Changed
|= setDoesNotCapture(F
, 0);
886 Changed
|= setDoesNotCapture(F
, 1);
887 Changed
|= setOnlyReadsMemory(F
, 0);
888 Changed
|= setOnlyReadsMemory(F
, 1);
891 Changed
|= setRetAndArgsNoUndef(F
);
892 Changed
|= setDoesNotThrow(F
);
893 Changed
|= setDoesNotCapture(F
, 0);
896 Changed
|= setRetAndArgsNoUndef(F
);
897 Changed
|= setDoesNotThrow(F
);
898 Changed
|= setDoesNotCapture(F
, 0);
899 Changed
|= setOnlyReadsMemory(F
, 0);
901 case LibFunc_vsscanf
:
902 Changed
|= setRetAndArgsNoUndef(F
);
903 Changed
|= setDoesNotThrow(F
);
904 Changed
|= setDoesNotCapture(F
, 0);
905 Changed
|= setDoesNotCapture(F
, 1);
906 Changed
|= setOnlyReadsMemory(F
, 0);
907 Changed
|= setOnlyReadsMemory(F
, 1);
909 case LibFunc_vfscanf
:
910 Changed
|= setRetAndArgsNoUndef(F
);
911 Changed
|= setDoesNotThrow(F
);
912 Changed
|= setDoesNotCapture(F
, 0);
913 Changed
|= setDoesNotCapture(F
, 1);
914 Changed
|= setOnlyReadsMemory(F
, 1);
916 case LibFunc_vprintf
:
917 Changed
|= setRetAndArgsNoUndef(F
);
918 Changed
|= setDoesNotThrow(F
);
919 Changed
|= setDoesNotCapture(F
, 0);
920 Changed
|= setOnlyReadsMemory(F
, 0);
922 case LibFunc_vfprintf
:
923 case LibFunc_vsprintf
:
924 Changed
|= setRetAndArgsNoUndef(F
);
925 Changed
|= setDoesNotThrow(F
);
926 Changed
|= setDoesNotCapture(F
, 0);
927 Changed
|= setDoesNotCapture(F
, 1);
928 Changed
|= setOnlyReadsMemory(F
, 1);
930 case LibFunc_vsnprintf
:
931 Changed
|= setRetAndArgsNoUndef(F
);
932 Changed
|= setDoesNotThrow(F
);
933 Changed
|= setDoesNotCapture(F
, 0);
934 Changed
|= setDoesNotCapture(F
, 2);
935 Changed
|= setOnlyReadsMemory(F
, 2);
938 // May throw; "open" is a valid pthread cancellation point.
939 Changed
|= setRetAndArgsNoUndef(F
);
940 Changed
|= setDoesNotCapture(F
, 0);
941 Changed
|= setOnlyReadsMemory(F
, 0);
943 case LibFunc_opendir
:
944 Changed
|= setRetAndArgsNoUndef(F
);
945 Changed
|= setDoesNotThrow(F
);
946 Changed
|= setRetDoesNotAlias(F
);
947 Changed
|= setDoesNotCapture(F
, 0);
948 Changed
|= setOnlyReadsMemory(F
, 0);
950 case LibFunc_tmpfile
:
951 Changed
|= setRetAndArgsNoUndef(F
);
952 Changed
|= setDoesNotThrow(F
);
953 Changed
|= setRetDoesNotAlias(F
);
956 Changed
|= setRetAndArgsNoUndef(F
);
957 Changed
|= setDoesNotThrow(F
);
958 Changed
|= setDoesNotCapture(F
, 0);
964 Changed
|= setDoesNotThrow(F
);
965 Changed
|= setDoesNotAccessMemory(F
);
968 Changed
|= setRetAndArgsNoUndef(F
);
969 Changed
|= setDoesNotThrow(F
);
970 Changed
|= setDoesNotCapture(F
, 0);
971 Changed
|= setDoesNotCapture(F
, 1);
972 Changed
|= setOnlyReadsMemory(F
, 0);
975 Changed
|= setRetAndArgsNoUndef(F
);
976 Changed
|= setDoesNotThrow(F
);
977 Changed
|= setDoesNotCapture(F
, 0);
978 Changed
|= setOnlyReadsMemory(F
, 0);
981 // May throw; places call through function pointer.
982 // Cannot give undef pointer/size
983 Changed
|= setRetAndArgsNoUndef(F
);
984 Changed
|= setDoesNotCapture(F
, 3);
986 case LibFunc_dunder_strndup
:
987 Changed
|= setArgNoUndef(F
, 1);
989 case LibFunc_dunder_strdup
:
990 Changed
|= setDoesNotThrow(F
);
991 Changed
|= setRetDoesNotAlias(F
);
992 Changed
|= setWillReturn(F
);
993 Changed
|= setDoesNotCapture(F
, 0);
994 Changed
|= setOnlyReadsMemory(F
, 0);
996 case LibFunc_dunder_strtok_r
:
997 Changed
|= setDoesNotThrow(F
);
998 Changed
|= setDoesNotCapture(F
, 1);
999 Changed
|= setOnlyReadsMemory(F
, 1);
1001 case LibFunc_under_IO_getc
:
1002 Changed
|= setRetAndArgsNoUndef(F
);
1003 Changed
|= setDoesNotThrow(F
);
1004 Changed
|= setDoesNotCapture(F
, 0);
1006 case LibFunc_under_IO_putc
:
1007 Changed
|= setRetAndArgsNoUndef(F
);
1008 Changed
|= setDoesNotThrow(F
);
1009 Changed
|= setDoesNotCapture(F
, 1);
1011 case LibFunc_dunder_isoc99_scanf
:
1012 Changed
|= setRetAndArgsNoUndef(F
);
1013 Changed
|= setDoesNotThrow(F
);
1014 Changed
|= setDoesNotCapture(F
, 0);
1015 Changed
|= setOnlyReadsMemory(F
, 0);
1017 case LibFunc_stat64
:
1018 case LibFunc_lstat64
:
1019 case LibFunc_statvfs64
:
1020 Changed
|= setRetAndArgsNoUndef(F
);
1021 Changed
|= setDoesNotThrow(F
);
1022 Changed
|= setDoesNotCapture(F
, 0);
1023 Changed
|= setDoesNotCapture(F
, 1);
1024 Changed
|= setOnlyReadsMemory(F
, 0);
1026 case LibFunc_dunder_isoc99_sscanf
:
1027 Changed
|= setRetAndArgsNoUndef(F
);
1028 Changed
|= setDoesNotThrow(F
);
1029 Changed
|= setDoesNotCapture(F
, 0);
1030 Changed
|= setDoesNotCapture(F
, 1);
1031 Changed
|= setOnlyReadsMemory(F
, 0);
1032 Changed
|= setOnlyReadsMemory(F
, 1);
1034 case LibFunc_fopen64
:
1035 Changed
|= setRetAndArgsNoUndef(F
);
1036 Changed
|= setDoesNotThrow(F
);
1037 Changed
|= setRetDoesNotAlias(F
);
1038 Changed
|= setDoesNotCapture(F
, 0);
1039 Changed
|= setDoesNotCapture(F
, 1);
1040 Changed
|= setOnlyReadsMemory(F
, 0);
1041 Changed
|= setOnlyReadsMemory(F
, 1);
1043 case LibFunc_fseeko64
:
1044 case LibFunc_ftello64
:
1045 Changed
|= setRetAndArgsNoUndef(F
);
1046 Changed
|= setDoesNotThrow(F
);
1047 Changed
|= setDoesNotCapture(F
, 0);
1049 case LibFunc_tmpfile64
:
1050 Changed
|= setRetAndArgsNoUndef(F
);
1051 Changed
|= setDoesNotThrow(F
);
1052 Changed
|= setRetDoesNotAlias(F
);
1054 case LibFunc_fstat64
:
1055 case LibFunc_fstatvfs64
:
1056 Changed
|= setRetAndArgsNoUndef(F
);
1057 Changed
|= setDoesNotThrow(F
);
1058 Changed
|= setDoesNotCapture(F
, 1);
1060 case LibFunc_open64
:
1061 // May throw; "open" is a valid pthread cancellation point.
1062 Changed
|= setRetAndArgsNoUndef(F
);
1063 Changed
|= setDoesNotCapture(F
, 0);
1064 Changed
|= setOnlyReadsMemory(F
, 0);
1066 case LibFunc_gettimeofday
:
1067 // Currently some platforms have the restrict keyword on the arguments to
1068 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1070 Changed
|= setRetAndArgsNoUndef(F
);
1071 Changed
|= setDoesNotThrow(F
);
1072 Changed
|= setDoesNotCapture(F
, 0);
1073 Changed
|= setDoesNotCapture(F
, 1);
1075 case LibFunc_memset_pattern4
:
1076 case LibFunc_memset_pattern8
:
1077 case LibFunc_memset_pattern16
:
1078 Changed
|= setDoesNotCapture(F
, 0);
1079 Changed
|= setDoesNotCapture(F
, 1);
1080 Changed
|= setOnlyReadsMemory(F
, 1);
1082 case LibFunc_memset
:
1083 Changed
|= setWillReturn(F
);
1085 case LibFunc_memset_chk
:
1086 Changed
|= setOnlyAccessesArgMemory(F
);
1087 Changed
|= setOnlyWritesMemory(F
, 0);
1088 Changed
|= setDoesNotThrow(F
);
1090 // int __nvvm_reflect(const char *)
1091 case LibFunc_nvvm_reflect
:
1092 Changed
|= setRetAndArgsNoUndef(F
);
1093 Changed
|= setDoesNotAccessMemory(F
);
1094 Changed
|= setDoesNotThrow(F
);
1097 case LibFunc_ldexpf
:
1098 case LibFunc_ldexpl
:
1099 Changed
|= setWillReturn(F
);
1105 case LibFunc_acoshf
:
1106 case LibFunc_acoshl
:
1111 case LibFunc_asinhf
:
1112 case LibFunc_asinhl
:
1116 case LibFunc_atan2f
:
1117 case LibFunc_atan2l
:
1120 case LibFunc_atanhf
:
1121 case LibFunc_atanhl
:
1129 case LibFunc_copysign
:
1130 case LibFunc_copysignf
:
1131 case LibFunc_copysignl
:
1139 case LibFunc_cospif
:
1147 case LibFunc_expm1f
:
1148 case LibFunc_expm1l
:
1156 case LibFunc_floorf
:
1157 case LibFunc_floorl
:
1170 case LibFunc_isascii
:
1171 case LibFunc_isdigit
:
1176 case LibFunc_log10f
:
1177 case LibFunc_log10l
:
1179 case LibFunc_log1pf
:
1180 case LibFunc_log1pl
:
1189 case LibFunc_nearbyint
:
1190 case LibFunc_nearbyintf
:
1191 case LibFunc_nearbyintl
:
1199 case LibFunc_roundf
:
1200 case LibFunc_roundl
:
1202 case LibFunc_sincospif_stret
:
1209 case LibFunc_sinpif
:
1219 case LibFunc_toascii
:
1221 case LibFunc_truncf
:
1222 case LibFunc_truncl
:
1223 Changed
|= setDoesNotThrow(F
);
1224 Changed
|= setDoesNotFreeMemory(F
);
1225 Changed
|= setOnlyWritesMemory(F
);
1226 Changed
|= setWillReturn(F
);
1229 // FIXME: It'd be really nice to cover all the library functions we're
1233 // We have to do this step after AllocKind has been inferred on functions so
1234 // we can reliably identify free-like and realloc-like functions.
1235 if (!isLibFreeFunction(&F
, TheLibFunc
) && !isReallocLikeFn(&F
))
1236 Changed
|= setDoesNotFreeMemory(F
);
1240 static void setArgExtAttr(Function
&F
, unsigned ArgNo
,
1241 const TargetLibraryInfo
&TLI
, bool Signed
= true) {
1242 Attribute::AttrKind ExtAttr
= TLI
.getExtAttrForI32Param(Signed
);
1243 if (ExtAttr
!= Attribute::None
&& !F
.hasParamAttribute(ArgNo
, ExtAttr
))
1244 F
.addParamAttr(ArgNo
, ExtAttr
);
1247 static void setRetExtAttr(Function
&F
,
1248 const TargetLibraryInfo
&TLI
, bool Signed
= true) {
1249 Attribute::AttrKind ExtAttr
= TLI
.getExtAttrForI32Return(Signed
);
1250 if (ExtAttr
!= Attribute::None
&& !F
.hasRetAttribute(ExtAttr
))
1251 F
.addRetAttr(ExtAttr
);
1254 // Modeled after X86TargetLowering::markLibCallAttributes.
1255 static void markRegisterParameterAttributes(Function
*F
) {
1256 if (!F
->arg_size() || F
->isVarArg())
1259 const CallingConv::ID CC
= F
->getCallingConv();
1260 if (CC
!= CallingConv::C
&& CC
!= CallingConv::X86_StdCall
)
1263 const Module
*M
= F
->getParent();
1264 unsigned N
= M
->getNumberRegisterParameters();
1268 const DataLayout
&DL
= M
->getDataLayout();
1270 for (Argument
&A
: F
->args()) {
1271 Type
*T
= A
.getType();
1272 if (!T
->isIntOrPtrTy())
1275 const TypeSize
&TS
= DL
.getTypeAllocSize(T
);
1279 assert(TS
<= 4 && "Need to account for parameters larger than word size");
1280 const unsigned NumRegs
= TS
> 4 ? 2 : 1;
1285 F
->addParamAttr(A
.getArgNo(), Attribute::InReg
);
1289 FunctionCallee
llvm::getOrInsertLibFunc(Module
*M
, const TargetLibraryInfo
&TLI
,
1290 LibFunc TheLibFunc
, FunctionType
*T
,
1291 AttributeList AttributeList
) {
1292 assert(TLI
.has(TheLibFunc
) &&
1293 "Creating call to non-existing library function.");
1294 StringRef Name
= TLI
.getName(TheLibFunc
);
1295 FunctionCallee C
= M
->getOrInsertFunction(Name
, T
, AttributeList
);
1297 // Make sure any mandatory argument attributes are added.
1299 // Any outgoing i32 argument should be handled with setArgExtAttr() which
1300 // will add an extension attribute if the target ABI requires it. Adding
1301 // argument extensions is typically done by the front end but when an
1302 // optimizer is building a library call on its own it has to take care of
1303 // this. Each such generated function must be handled here with sign or
1304 // zero extensions as needed. F is retreived with cast<> because we demand
1305 // of the caller to have called isLibFuncEmittable() first.
1306 Function
*F
= cast
<Function
>(C
.getCallee());
1307 assert(F
->getFunctionType() == T
&& "Function type does not match.");
1308 switch (TheLibFunc
) {
1310 case LibFunc_putchar
:
1311 setArgExtAttr(*F
, 0, TLI
);
1314 case LibFunc_ldexpf
:
1315 case LibFunc_ldexpl
:
1316 case LibFunc_memchr
:
1317 case LibFunc_memrchr
:
1318 case LibFunc_strchr
:
1319 setArgExtAttr(*F
, 1, TLI
);
1321 case LibFunc_memccpy
:
1322 setArgExtAttr(*F
, 2, TLI
);
1325 // These are functions that are known to not need any argument extension
1326 // on any target: A size_t argument (which may be an i32 on some targets)
1327 // should not trigger the assert below.
1329 setRetExtAttr(*F
, TLI
);
1331 case LibFunc_calloc
:
1332 case LibFunc_fwrite
:
1333 case LibFunc_malloc
:
1334 case LibFunc_memcmp
:
1335 case LibFunc_memcpy_chk
:
1336 case LibFunc_mempcpy
:
1337 case LibFunc_memset_pattern16
:
1338 case LibFunc_snprintf
:
1339 case LibFunc_stpncpy
:
1340 case LibFunc_strlcat
:
1341 case LibFunc_strlcpy
:
1342 case LibFunc_strncat
:
1343 case LibFunc_strncmp
:
1344 case LibFunc_strncpy
:
1345 case LibFunc_vsnprintf
:
1350 for (unsigned i
= 0; i
< T
->getNumParams(); i
++)
1351 assert(!isa
<IntegerType
>(T
->getParamType(i
)) &&
1352 "Unhandled integer argument.");
1357 markRegisterParameterAttributes(F
);
1362 FunctionCallee
llvm::getOrInsertLibFunc(Module
*M
, const TargetLibraryInfo
&TLI
,
1363 LibFunc TheLibFunc
, FunctionType
*T
) {
1364 return getOrInsertLibFunc(M
, TLI
, TheLibFunc
, T
, AttributeList());
1367 bool llvm::isLibFuncEmittable(const Module
*M
, const TargetLibraryInfo
*TLI
,
1368 LibFunc TheLibFunc
) {
1369 StringRef FuncName
= TLI
->getName(TheLibFunc
);
1370 if (!TLI
->has(TheLibFunc
))
1373 // Check if the Module already has a GlobalValue with the same name, in
1374 // which case it must be a Function with the expected type.
1375 if (GlobalValue
*GV
= M
->getNamedValue(FuncName
)) {
1376 if (auto *F
= dyn_cast
<Function
>(GV
))
1377 return TLI
->isValidProtoForLibFunc(*F
->getFunctionType(), TheLibFunc
, *M
);
1384 bool llvm::isLibFuncEmittable(const Module
*M
, const TargetLibraryInfo
*TLI
,
1387 return TLI
->getLibFunc(Name
, TheLibFunc
) &&
1388 isLibFuncEmittable(M
, TLI
, TheLibFunc
);
1391 bool llvm::hasFloatFn(const Module
*M
, const TargetLibraryInfo
*TLI
, Type
*Ty
,
1392 LibFunc DoubleFn
, LibFunc FloatFn
, LibFunc LongDoubleFn
) {
1393 switch (Ty
->getTypeID()) {
1394 case Type::HalfTyID
:
1396 case Type::FloatTyID
:
1397 return isLibFuncEmittable(M
, TLI
, FloatFn
);
1398 case Type::DoubleTyID
:
1399 return isLibFuncEmittable(M
, TLI
, DoubleFn
);
1401 return isLibFuncEmittable(M
, TLI
, LongDoubleFn
);
1405 StringRef
llvm::getFloatFn(const Module
*M
, const TargetLibraryInfo
*TLI
,
1406 Type
*Ty
, LibFunc DoubleFn
, LibFunc FloatFn
,
1407 LibFunc LongDoubleFn
, LibFunc
&TheLibFunc
) {
1408 assert(hasFloatFn(M
, TLI
, Ty
, DoubleFn
, FloatFn
, LongDoubleFn
) &&
1409 "Cannot get name for unavailable function!");
1411 switch (Ty
->getTypeID()) {
1412 case Type::HalfTyID
:
1413 llvm_unreachable("No name for HalfTy!");
1414 case Type::FloatTyID
:
1415 TheLibFunc
= FloatFn
;
1416 return TLI
->getName(FloatFn
);
1417 case Type::DoubleTyID
:
1418 TheLibFunc
= DoubleFn
;
1419 return TLI
->getName(DoubleFn
);
1421 TheLibFunc
= LongDoubleFn
;
1422 return TLI
->getName(LongDoubleFn
);
1426 //- Emit LibCalls ------------------------------------------------------------//
1428 static IntegerType
*getIntTy(IRBuilderBase
&B
, const TargetLibraryInfo
*TLI
) {
1429 return B
.getIntNTy(TLI
->getIntSize());
1432 static IntegerType
*getSizeTTy(IRBuilderBase
&B
, const TargetLibraryInfo
*TLI
) {
1433 const Module
*M
= B
.GetInsertBlock()->getModule();
1434 return B
.getIntNTy(TLI
->getSizeTSize(*M
));
1437 static Value
*emitLibCall(LibFunc TheLibFunc
, Type
*ReturnType
,
1438 ArrayRef
<Type
*> ParamTypes
,
1439 ArrayRef
<Value
*> Operands
, IRBuilderBase
&B
,
1440 const TargetLibraryInfo
*TLI
,
1441 bool IsVaArgs
= false) {
1442 Module
*M
= B
.GetInsertBlock()->getModule();
1443 if (!isLibFuncEmittable(M
, TLI
, TheLibFunc
))
1446 StringRef FuncName
= TLI
->getName(TheLibFunc
);
1447 FunctionType
*FuncType
= FunctionType::get(ReturnType
, ParamTypes
, IsVaArgs
);
1448 FunctionCallee Callee
= getOrInsertLibFunc(M
, *TLI
, TheLibFunc
, FuncType
);
1449 inferNonMandatoryLibFuncAttrs(M
, FuncName
, *TLI
);
1450 CallInst
*CI
= B
.CreateCall(Callee
, Operands
, FuncName
);
1451 if (const Function
*F
=
1452 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1453 CI
->setCallingConv(F
->getCallingConv());
1457 Value
*llvm::emitStrLen(Value
*Ptr
, IRBuilderBase
&B
, const DataLayout
&DL
,
1458 const TargetLibraryInfo
*TLI
) {
1459 Type
*CharPtrTy
= B
.getPtrTy();
1460 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1461 return emitLibCall(LibFunc_strlen
, SizeTTy
, CharPtrTy
, Ptr
, B
, TLI
);
1464 Value
*llvm::emitStrDup(Value
*Ptr
, IRBuilderBase
&B
,
1465 const TargetLibraryInfo
*TLI
) {
1466 Type
*CharPtrTy
= B
.getPtrTy();
1467 return emitLibCall(LibFunc_strdup
, CharPtrTy
, CharPtrTy
, Ptr
, B
, TLI
);
1470 Value
*llvm::emitStrChr(Value
*Ptr
, char C
, IRBuilderBase
&B
,
1471 const TargetLibraryInfo
*TLI
) {
1472 Type
*CharPtrTy
= B
.getPtrTy();
1473 Type
*IntTy
= getIntTy(B
, TLI
);
1474 return emitLibCall(LibFunc_strchr
, CharPtrTy
, {CharPtrTy
, IntTy
},
1475 {Ptr
, ConstantInt::get(IntTy
, C
)}, B
, TLI
);
1478 Value
*llvm::emitStrNCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilderBase
&B
,
1479 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1480 Type
*CharPtrTy
= B
.getPtrTy();
1481 Type
*IntTy
= getIntTy(B
, TLI
);
1482 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1484 LibFunc_strncmp
, IntTy
,
1485 {CharPtrTy
, CharPtrTy
, SizeTTy
},
1486 {Ptr1
, Ptr2
, Len
}, B
, TLI
);
1489 Value
*llvm::emitStrCpy(Value
*Dst
, Value
*Src
, IRBuilderBase
&B
,
1490 const TargetLibraryInfo
*TLI
) {
1491 Type
*CharPtrTy
= Dst
->getType();
1492 return emitLibCall(LibFunc_strcpy
, CharPtrTy
, {CharPtrTy
, CharPtrTy
},
1493 {Dst
, Src
}, B
, TLI
);
1496 Value
*llvm::emitStpCpy(Value
*Dst
, Value
*Src
, IRBuilderBase
&B
,
1497 const TargetLibraryInfo
*TLI
) {
1498 Type
*CharPtrTy
= B
.getPtrTy();
1499 return emitLibCall(LibFunc_stpcpy
, CharPtrTy
, {CharPtrTy
, CharPtrTy
},
1500 {Dst
, Src
}, B
, TLI
);
1503 Value
*llvm::emitStrNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilderBase
&B
,
1504 const TargetLibraryInfo
*TLI
) {
1505 Type
*CharPtrTy
= B
.getPtrTy();
1506 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1507 return emitLibCall(LibFunc_strncpy
, CharPtrTy
, {CharPtrTy
, CharPtrTy
, SizeTTy
},
1508 {Dst
, Src
, Len
}, B
, TLI
);
1511 Value
*llvm::emitStpNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilderBase
&B
,
1512 const TargetLibraryInfo
*TLI
) {
1513 Type
*CharPtrTy
= B
.getPtrTy();
1514 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1515 return emitLibCall(LibFunc_stpncpy
, CharPtrTy
, {CharPtrTy
, CharPtrTy
, SizeTTy
},
1516 {Dst
, Src
, Len
}, B
, TLI
);
1519 Value
*llvm::emitMemCpyChk(Value
*Dst
, Value
*Src
, Value
*Len
, Value
*ObjSize
,
1520 IRBuilderBase
&B
, const DataLayout
&DL
,
1521 const TargetLibraryInfo
*TLI
) {
1522 Module
*M
= B
.GetInsertBlock()->getModule();
1523 if (!isLibFuncEmittable(M
, TLI
, LibFunc_memcpy_chk
))
1527 AS
= AttributeList::get(M
->getContext(), AttributeList::FunctionIndex
,
1528 Attribute::NoUnwind
);
1529 Type
*VoidPtrTy
= B
.getPtrTy();
1530 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1531 FunctionCallee MemCpy
= getOrInsertLibFunc(M
, *TLI
, LibFunc_memcpy_chk
,
1532 AttributeList::get(M
->getContext(), AS
), VoidPtrTy
,
1533 VoidPtrTy
, VoidPtrTy
, SizeTTy
, SizeTTy
);
1534 CallInst
*CI
= B
.CreateCall(MemCpy
, {Dst
, Src
, Len
, ObjSize
});
1535 if (const Function
*F
=
1536 dyn_cast
<Function
>(MemCpy
.getCallee()->stripPointerCasts()))
1537 CI
->setCallingConv(F
->getCallingConv());
1541 Value
*llvm::emitMemPCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilderBase
&B
,
1542 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1543 Type
*VoidPtrTy
= B
.getPtrTy();
1544 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1545 return emitLibCall(LibFunc_mempcpy
, VoidPtrTy
,
1546 {VoidPtrTy
, VoidPtrTy
, SizeTTy
},
1547 {Dst
, Src
, Len
}, B
, TLI
);
1550 Value
*llvm::emitMemChr(Value
*Ptr
, Value
*Val
, Value
*Len
, IRBuilderBase
&B
,
1551 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1552 Type
*VoidPtrTy
= B
.getPtrTy();
1553 Type
*IntTy
= getIntTy(B
, TLI
);
1554 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1555 return emitLibCall(LibFunc_memchr
, VoidPtrTy
,
1556 {VoidPtrTy
, IntTy
, SizeTTy
},
1557 {Ptr
, Val
, Len
}, B
, TLI
);
1560 Value
*llvm::emitMemRChr(Value
*Ptr
, Value
*Val
, Value
*Len
, IRBuilderBase
&B
,
1561 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1562 Type
*VoidPtrTy
= B
.getPtrTy();
1563 Type
*IntTy
= getIntTy(B
, TLI
);
1564 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1565 return emitLibCall(LibFunc_memrchr
, VoidPtrTy
,
1566 {VoidPtrTy
, IntTy
, SizeTTy
},
1567 {Ptr
, Val
, Len
}, B
, TLI
);
1570 Value
*llvm::emitMemCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilderBase
&B
,
1571 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1572 Type
*VoidPtrTy
= B
.getPtrTy();
1573 Type
*IntTy
= getIntTy(B
, TLI
);
1574 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1575 return emitLibCall(LibFunc_memcmp
, IntTy
,
1576 {VoidPtrTy
, VoidPtrTy
, SizeTTy
},
1577 {Ptr1
, Ptr2
, Len
}, B
, TLI
);
1580 Value
*llvm::emitBCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilderBase
&B
,
1581 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1582 Type
*VoidPtrTy
= B
.getPtrTy();
1583 Type
*IntTy
= getIntTy(B
, TLI
);
1584 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1585 return emitLibCall(LibFunc_bcmp
, IntTy
,
1586 {VoidPtrTy
, VoidPtrTy
, SizeTTy
},
1587 {Ptr1
, Ptr2
, Len
}, B
, TLI
);
1590 Value
*llvm::emitMemCCpy(Value
*Ptr1
, Value
*Ptr2
, Value
*Val
, Value
*Len
,
1591 IRBuilderBase
&B
, const TargetLibraryInfo
*TLI
) {
1592 Type
*VoidPtrTy
= B
.getPtrTy();
1593 Type
*IntTy
= getIntTy(B
, TLI
);
1594 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1595 return emitLibCall(LibFunc_memccpy
, VoidPtrTy
,
1596 {VoidPtrTy
, VoidPtrTy
, IntTy
, SizeTTy
},
1597 {Ptr1
, Ptr2
, Val
, Len
}, B
, TLI
);
1600 Value
*llvm::emitSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
,
1601 ArrayRef
<Value
*> VariadicArgs
, IRBuilderBase
&B
,
1602 const TargetLibraryInfo
*TLI
) {
1603 Type
*CharPtrTy
= B
.getPtrTy();
1604 Type
*IntTy
= getIntTy(B
, TLI
);
1605 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1606 SmallVector
<Value
*, 8> Args
{Dest
, Size
, Fmt
};
1607 llvm::append_range(Args
, VariadicArgs
);
1608 return emitLibCall(LibFunc_snprintf
, IntTy
,
1609 {CharPtrTy
, SizeTTy
, CharPtrTy
},
1610 Args
, B
, TLI
, /*IsVaArgs=*/true);
1613 Value
*llvm::emitSPrintf(Value
*Dest
, Value
*Fmt
,
1614 ArrayRef
<Value
*> VariadicArgs
, IRBuilderBase
&B
,
1615 const TargetLibraryInfo
*TLI
) {
1616 Type
*CharPtrTy
= B
.getPtrTy();
1617 Type
*IntTy
= getIntTy(B
, TLI
);
1618 SmallVector
<Value
*, 8> Args
{Dest
, Fmt
};
1619 llvm::append_range(Args
, VariadicArgs
);
1620 return emitLibCall(LibFunc_sprintf
, IntTy
,
1621 {CharPtrTy
, CharPtrTy
}, Args
, B
, TLI
,
1625 Value
*llvm::emitStrCat(Value
*Dest
, Value
*Src
, IRBuilderBase
&B
,
1626 const TargetLibraryInfo
*TLI
) {
1627 Type
*CharPtrTy
= B
.getPtrTy();
1628 return emitLibCall(LibFunc_strcat
, CharPtrTy
,
1629 {CharPtrTy
, CharPtrTy
},
1630 {Dest
, Src
}, B
, TLI
);
1633 Value
*llvm::emitStrLCpy(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilderBase
&B
,
1634 const TargetLibraryInfo
*TLI
) {
1635 Type
*CharPtrTy
= B
.getPtrTy();
1636 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1637 return emitLibCall(LibFunc_strlcpy
, SizeTTy
,
1638 {CharPtrTy
, CharPtrTy
, SizeTTy
},
1639 {Dest
, Src
, Size
}, B
, TLI
);
1642 Value
*llvm::emitStrLCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilderBase
&B
,
1643 const TargetLibraryInfo
*TLI
) {
1644 Type
*CharPtrTy
= B
.getPtrTy();
1645 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1646 return emitLibCall(LibFunc_strlcat
, SizeTTy
,
1647 {CharPtrTy
, CharPtrTy
, SizeTTy
},
1648 {Dest
, Src
, Size
}, B
, TLI
);
1651 Value
*llvm::emitStrNCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilderBase
&B
,
1652 const TargetLibraryInfo
*TLI
) {
1653 Type
*CharPtrTy
= B
.getPtrTy();
1654 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1655 return emitLibCall(LibFunc_strncat
, CharPtrTy
,
1656 {CharPtrTy
, CharPtrTy
, SizeTTy
},
1657 {Dest
, Src
, Size
}, B
, TLI
);
1660 Value
*llvm::emitVSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
, Value
*VAList
,
1661 IRBuilderBase
&B
, const TargetLibraryInfo
*TLI
) {
1662 Type
*CharPtrTy
= B
.getPtrTy();
1663 Type
*IntTy
= getIntTy(B
, TLI
);
1664 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1666 LibFunc_vsnprintf
, IntTy
,
1667 {CharPtrTy
, SizeTTy
, CharPtrTy
, VAList
->getType()},
1668 {Dest
, Size
, Fmt
, VAList
}, B
, TLI
);
1671 Value
*llvm::emitVSPrintf(Value
*Dest
, Value
*Fmt
, Value
*VAList
,
1672 IRBuilderBase
&B
, const TargetLibraryInfo
*TLI
) {
1673 Type
*CharPtrTy
= B
.getPtrTy();
1674 Type
*IntTy
= getIntTy(B
, TLI
);
1675 return emitLibCall(LibFunc_vsprintf
, IntTy
,
1676 {CharPtrTy
, CharPtrTy
, VAList
->getType()},
1677 {Dest
, Fmt
, VAList
}, B
, TLI
);
1680 /// Append a suffix to the function name according to the type of 'Op'.
1681 static void appendTypeSuffix(Value
*Op
, StringRef
&Name
,
1682 SmallString
<20> &NameBuffer
) {
1683 if (!Op
->getType()->isDoubleTy()) {
1686 if (Op
->getType()->isFloatTy())
1695 static Value
*emitUnaryFloatFnCallHelper(Value
*Op
, LibFunc TheLibFunc
,
1696 StringRef Name
, IRBuilderBase
&B
,
1697 const AttributeList
&Attrs
,
1698 const TargetLibraryInfo
*TLI
) {
1699 assert((Name
!= "") && "Must specify Name to emitUnaryFloatFnCall");
1701 Module
*M
= B
.GetInsertBlock()->getModule();
1702 FunctionCallee Callee
= getOrInsertLibFunc(M
, *TLI
, TheLibFunc
, Op
->getType(),
1704 CallInst
*CI
= B
.CreateCall(Callee
, Op
, Name
);
1706 // The incoming attribute set may have come from a speculatable intrinsic, but
1707 // is being replaced with a library call which is not allowed to be
1710 Attrs
.removeFnAttribute(B
.getContext(), Attribute::Speculatable
));
1711 if (const Function
*F
=
1712 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1713 CI
->setCallingConv(F
->getCallingConv());
1718 Value
*llvm::emitUnaryFloatFnCall(Value
*Op
, const TargetLibraryInfo
*TLI
,
1719 StringRef Name
, IRBuilderBase
&B
,
1720 const AttributeList
&Attrs
) {
1721 SmallString
<20> NameBuffer
;
1722 appendTypeSuffix(Op
, Name
, NameBuffer
);
1725 TLI
->getLibFunc(Name
, TheLibFunc
);
1727 return emitUnaryFloatFnCallHelper(Op
, TheLibFunc
, Name
, B
, Attrs
, TLI
);
1730 Value
*llvm::emitUnaryFloatFnCall(Value
*Op
, const TargetLibraryInfo
*TLI
,
1731 LibFunc DoubleFn
, LibFunc FloatFn
,
1732 LibFunc LongDoubleFn
, IRBuilderBase
&B
,
1733 const AttributeList
&Attrs
) {
1734 // Get the name of the function according to TLI.
1735 Module
*M
= B
.GetInsertBlock()->getModule();
1737 StringRef Name
= getFloatFn(M
, TLI
, Op
->getType(), DoubleFn
, FloatFn
,
1738 LongDoubleFn
, TheLibFunc
);
1740 return emitUnaryFloatFnCallHelper(Op
, TheLibFunc
, Name
, B
, Attrs
, TLI
);
1743 static Value
*emitBinaryFloatFnCallHelper(Value
*Op1
, Value
*Op2
,
1745 StringRef Name
, IRBuilderBase
&B
,
1746 const AttributeList
&Attrs
,
1747 const TargetLibraryInfo
*TLI
) {
1748 assert((Name
!= "") && "Must specify Name to emitBinaryFloatFnCall");
1750 Module
*M
= B
.GetInsertBlock()->getModule();
1751 FunctionCallee Callee
= getOrInsertLibFunc(M
, *TLI
, TheLibFunc
, Op1
->getType(),
1752 Op1
->getType(), Op2
->getType());
1753 inferNonMandatoryLibFuncAttrs(M
, Name
, *TLI
);
1754 CallInst
*CI
= B
.CreateCall(Callee
, { Op1
, Op2
}, Name
);
1756 // The incoming attribute set may have come from a speculatable intrinsic, but
1757 // is being replaced with a library call which is not allowed to be
1760 Attrs
.removeFnAttribute(B
.getContext(), Attribute::Speculatable
));
1761 if (const Function
*F
=
1762 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1763 CI
->setCallingConv(F
->getCallingConv());
1768 Value
*llvm::emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
,
1769 const TargetLibraryInfo
*TLI
,
1770 StringRef Name
, IRBuilderBase
&B
,
1771 const AttributeList
&Attrs
) {
1772 assert((Name
!= "") && "Must specify Name to emitBinaryFloatFnCall");
1774 SmallString
<20> NameBuffer
;
1775 appendTypeSuffix(Op1
, Name
, NameBuffer
);
1778 TLI
->getLibFunc(Name
, TheLibFunc
);
1780 return emitBinaryFloatFnCallHelper(Op1
, Op2
, TheLibFunc
, Name
, B
, Attrs
, TLI
);
1783 Value
*llvm::emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
,
1784 const TargetLibraryInfo
*TLI
,
1785 LibFunc DoubleFn
, LibFunc FloatFn
,
1786 LibFunc LongDoubleFn
, IRBuilderBase
&B
,
1787 const AttributeList
&Attrs
) {
1788 // Get the name of the function according to TLI.
1789 Module
*M
= B
.GetInsertBlock()->getModule();
1791 StringRef Name
= getFloatFn(M
, TLI
, Op1
->getType(), DoubleFn
, FloatFn
,
1792 LongDoubleFn
, TheLibFunc
);
1794 return emitBinaryFloatFnCallHelper(Op1
, Op2
, TheLibFunc
, Name
, B
, Attrs
, TLI
);
1797 // Emit a call to putchar(int) with Char as the argument. Char must have
1798 // the same precision as int, which need not be 32 bits.
1799 Value
*llvm::emitPutChar(Value
*Char
, IRBuilderBase
&B
,
1800 const TargetLibraryInfo
*TLI
) {
1801 Module
*M
= B
.GetInsertBlock()->getModule();
1802 if (!isLibFuncEmittable(M
, TLI
, LibFunc_putchar
))
1805 Type
*IntTy
= getIntTy(B
, TLI
);
1806 StringRef PutCharName
= TLI
->getName(LibFunc_putchar
);
1807 FunctionCallee PutChar
= getOrInsertLibFunc(M
, *TLI
, LibFunc_putchar
,
1809 inferNonMandatoryLibFuncAttrs(M
, PutCharName
, *TLI
);
1810 CallInst
*CI
= B
.CreateCall(PutChar
, Char
, PutCharName
);
1812 if (const Function
*F
=
1813 dyn_cast
<Function
>(PutChar
.getCallee()->stripPointerCasts()))
1814 CI
->setCallingConv(F
->getCallingConv());
1818 Value
*llvm::emitPutS(Value
*Str
, IRBuilderBase
&B
,
1819 const TargetLibraryInfo
*TLI
) {
1820 Module
*M
= B
.GetInsertBlock()->getModule();
1821 if (!isLibFuncEmittable(M
, TLI
, LibFunc_puts
))
1824 Type
*IntTy
= getIntTy(B
, TLI
);
1825 StringRef PutsName
= TLI
->getName(LibFunc_puts
);
1826 FunctionCallee PutS
= getOrInsertLibFunc(M
, *TLI
, LibFunc_puts
, IntTy
,
1828 inferNonMandatoryLibFuncAttrs(M
, PutsName
, *TLI
);
1829 CallInst
*CI
= B
.CreateCall(PutS
, Str
, PutsName
);
1830 if (const Function
*F
=
1831 dyn_cast
<Function
>(PutS
.getCallee()->stripPointerCasts()))
1832 CI
->setCallingConv(F
->getCallingConv());
1836 Value
*llvm::emitFPutC(Value
*Char
, Value
*File
, IRBuilderBase
&B
,
1837 const TargetLibraryInfo
*TLI
) {
1838 Module
*M
= B
.GetInsertBlock()->getModule();
1839 if (!isLibFuncEmittable(M
, TLI
, LibFunc_fputc
))
1842 Type
*IntTy
= getIntTy(B
, TLI
);
1843 StringRef FPutcName
= TLI
->getName(LibFunc_fputc
);
1844 FunctionCallee F
= getOrInsertLibFunc(M
, *TLI
, LibFunc_fputc
, IntTy
,
1845 IntTy
, File
->getType());
1846 if (File
->getType()->isPointerTy())
1847 inferNonMandatoryLibFuncAttrs(M
, FPutcName
, *TLI
);
1848 CallInst
*CI
= B
.CreateCall(F
, {Char
, File
}, FPutcName
);
1850 if (const Function
*Fn
=
1851 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1852 CI
->setCallingConv(Fn
->getCallingConv());
1856 Value
*llvm::emitFPutS(Value
*Str
, Value
*File
, IRBuilderBase
&B
,
1857 const TargetLibraryInfo
*TLI
) {
1858 Module
*M
= B
.GetInsertBlock()->getModule();
1859 if (!isLibFuncEmittable(M
, TLI
, LibFunc_fputs
))
1862 Type
*IntTy
= getIntTy(B
, TLI
);
1863 StringRef FPutsName
= TLI
->getName(LibFunc_fputs
);
1864 FunctionCallee F
= getOrInsertLibFunc(M
, *TLI
, LibFunc_fputs
, IntTy
,
1865 B
.getPtrTy(), File
->getType());
1866 if (File
->getType()->isPointerTy())
1867 inferNonMandatoryLibFuncAttrs(M
, FPutsName
, *TLI
);
1868 CallInst
*CI
= B
.CreateCall(F
, {Str
, File
}, FPutsName
);
1870 if (const Function
*Fn
=
1871 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1872 CI
->setCallingConv(Fn
->getCallingConv());
1876 Value
*llvm::emitFWrite(Value
*Ptr
, Value
*Size
, Value
*File
, IRBuilderBase
&B
,
1877 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1878 Module
*M
= B
.GetInsertBlock()->getModule();
1879 if (!isLibFuncEmittable(M
, TLI
, LibFunc_fwrite
))
1882 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1883 StringRef FWriteName
= TLI
->getName(LibFunc_fwrite
);
1884 FunctionCallee F
= getOrInsertLibFunc(M
, *TLI
, LibFunc_fwrite
,
1885 SizeTTy
, B
.getPtrTy(), SizeTTy
,
1886 SizeTTy
, File
->getType());
1888 if (File
->getType()->isPointerTy())
1889 inferNonMandatoryLibFuncAttrs(M
, FWriteName
, *TLI
);
1891 B
.CreateCall(F
, {Ptr
, Size
,
1892 ConstantInt::get(SizeTTy
, 1), File
});
1894 if (const Function
*Fn
=
1895 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1896 CI
->setCallingConv(Fn
->getCallingConv());
1900 Value
*llvm::emitMalloc(Value
*Num
, IRBuilderBase
&B
, const DataLayout
&DL
,
1901 const TargetLibraryInfo
*TLI
) {
1902 Module
*M
= B
.GetInsertBlock()->getModule();
1903 if (!isLibFuncEmittable(M
, TLI
, LibFunc_malloc
))
1906 StringRef MallocName
= TLI
->getName(LibFunc_malloc
);
1907 Type
*SizeTTy
= getSizeTTy(B
, TLI
);
1908 FunctionCallee Malloc
= getOrInsertLibFunc(M
, *TLI
, LibFunc_malloc
,
1909 B
.getPtrTy(), SizeTTy
);
1910 inferNonMandatoryLibFuncAttrs(M
, MallocName
, *TLI
);
1911 CallInst
*CI
= B
.CreateCall(Malloc
, Num
, MallocName
);
1913 if (const Function
*F
=
1914 dyn_cast
<Function
>(Malloc
.getCallee()->stripPointerCasts()))
1915 CI
->setCallingConv(F
->getCallingConv());
1920 Value
*llvm::emitCalloc(Value
*Num
, Value
*Size
, IRBuilderBase
&B
,
1921 const TargetLibraryInfo
&TLI
) {
1922 Module
*M
= B
.GetInsertBlock()->getModule();
1923 if (!isLibFuncEmittable(M
, &TLI
, LibFunc_calloc
))
1926 StringRef CallocName
= TLI
.getName(LibFunc_calloc
);
1927 Type
*SizeTTy
= getSizeTTy(B
, &TLI
);
1928 FunctionCallee Calloc
= getOrInsertLibFunc(M
, TLI
, LibFunc_calloc
,
1929 B
.getPtrTy(), SizeTTy
, SizeTTy
);
1930 inferNonMandatoryLibFuncAttrs(M
, CallocName
, TLI
);
1931 CallInst
*CI
= B
.CreateCall(Calloc
, {Num
, Size
}, CallocName
);
1934 dyn_cast
<Function
>(Calloc
.getCallee()->stripPointerCasts()))
1935 CI
->setCallingConv(F
->getCallingConv());
1940 Value
*llvm::emitHotColdNew(Value
*Num
, IRBuilderBase
&B
,
1941 const TargetLibraryInfo
*TLI
, LibFunc NewFunc
,
1943 Module
*M
= B
.GetInsertBlock()->getModule();
1944 if (!isLibFuncEmittable(M
, TLI
, NewFunc
))
1947 StringRef Name
= TLI
->getName(NewFunc
);
1948 FunctionCallee Func
= M
->getOrInsertFunction(Name
, B
.getPtrTy(),
1949 Num
->getType(), B
.getInt8Ty());
1950 inferNonMandatoryLibFuncAttrs(M
, Name
, *TLI
);
1951 CallInst
*CI
= B
.CreateCall(Func
, {Num
, B
.getInt8(HotCold
)}, Name
);
1953 if (const Function
*F
=
1954 dyn_cast
<Function
>(Func
.getCallee()->stripPointerCasts()))
1955 CI
->setCallingConv(F
->getCallingConv());
1960 Value
*llvm::emitHotColdNewNoThrow(Value
*Num
, Value
*NoThrow
, IRBuilderBase
&B
,
1961 const TargetLibraryInfo
*TLI
,
1962 LibFunc NewFunc
, uint8_t HotCold
) {
1963 Module
*M
= B
.GetInsertBlock()->getModule();
1964 if (!isLibFuncEmittable(M
, TLI
, NewFunc
))
1967 StringRef Name
= TLI
->getName(NewFunc
);
1968 FunctionCallee Func
=
1969 M
->getOrInsertFunction(Name
, B
.getPtrTy(), Num
->getType(),
1970 NoThrow
->getType(), B
.getInt8Ty());
1971 inferNonMandatoryLibFuncAttrs(M
, Name
, *TLI
);
1972 CallInst
*CI
= B
.CreateCall(Func
, {Num
, NoThrow
, B
.getInt8(HotCold
)}, Name
);
1974 if (const Function
*F
=
1975 dyn_cast
<Function
>(Func
.getCallee()->stripPointerCasts()))
1976 CI
->setCallingConv(F
->getCallingConv());
1981 Value
*llvm::emitHotColdNewAligned(Value
*Num
, Value
*Align
, IRBuilderBase
&B
,
1982 const TargetLibraryInfo
*TLI
,
1983 LibFunc NewFunc
, uint8_t HotCold
) {
1984 Module
*M
= B
.GetInsertBlock()->getModule();
1985 if (!isLibFuncEmittable(M
, TLI
, NewFunc
))
1988 StringRef Name
= TLI
->getName(NewFunc
);
1989 FunctionCallee Func
= M
->getOrInsertFunction(
1990 Name
, B
.getPtrTy(), Num
->getType(), Align
->getType(), B
.getInt8Ty());
1991 inferNonMandatoryLibFuncAttrs(M
, Name
, *TLI
);
1992 CallInst
*CI
= B
.CreateCall(Func
, {Num
, Align
, B
.getInt8(HotCold
)}, Name
);
1994 if (const Function
*F
=
1995 dyn_cast
<Function
>(Func
.getCallee()->stripPointerCasts()))
1996 CI
->setCallingConv(F
->getCallingConv());
2001 Value
*llvm::emitHotColdNewAlignedNoThrow(Value
*Num
, Value
*Align
,
2002 Value
*NoThrow
, IRBuilderBase
&B
,
2003 const TargetLibraryInfo
*TLI
,
2004 LibFunc NewFunc
, uint8_t HotCold
) {
2005 Module
*M
= B
.GetInsertBlock()->getModule();
2006 if (!isLibFuncEmittable(M
, TLI
, NewFunc
))
2009 StringRef Name
= TLI
->getName(NewFunc
);
2010 FunctionCallee Func
= M
->getOrInsertFunction(
2011 Name
, B
.getPtrTy(), Num
->getType(), Align
->getType(),
2012 NoThrow
->getType(), B
.getInt8Ty());
2013 inferNonMandatoryLibFuncAttrs(M
, Name
, *TLI
);
2015 B
.CreateCall(Func
, {Num
, Align
, NoThrow
, B
.getInt8(HotCold
)}, Name
);
2017 if (const Function
*F
=
2018 dyn_cast
<Function
>(Func
.getCallee()->stripPointerCasts()))
2019 CI
->setCallingConv(F
->getCallingConv());