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/TargetLibraryInfo.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Type.h"
25 #include "llvm/Analysis/MemoryBuiltins.h"
29 #define DEBUG_TYPE "build-libcalls"
31 //- Infer Attributes ---------------------------------------------------------//
33 STATISTIC(NumReadNone, "Number of functions inferred as readnone");
34 STATISTIC(NumReadOnly, "Number of functions inferred as readonly");
35 STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly");
36 STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind");
37 STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture");
38 STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly");
39 STATISTIC(NumNoAlias, "Number of function returns inferred as noalias");
40 STATISTIC(NumNonNull, "Number of function returns inferred as nonnull returns");
41 STATISTIC(NumReturnedArg, "Number of arguments inferred as returned");
43 static bool setDoesNotAccessMemory(Function &F) {
44 if (F.doesNotAccessMemory())
46 F.setDoesNotAccessMemory();
51 static bool setOnlyReadsMemory(Function &F) {
52 if (F.onlyReadsMemory())
54 F.setOnlyReadsMemory();
59 static bool setOnlyAccessesArgMemory(Function &F) {
60 if (F.onlyAccessesArgMemory())
62 F.setOnlyAccessesArgMemory();
67 static bool setDoesNotThrow(Function &F) {
75 static bool setRetDoesNotAlias(Function &F) {
76 if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias))
78 F.addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
83 static bool setDoesNotCapture(Function &F, unsigned ArgNo) {
84 if (F.hasParamAttribute(ArgNo, Attribute::NoCapture))
86 F.addParamAttr(ArgNo, Attribute::NoCapture);
91 static bool setDoesNotAlias(Function &F, unsigned ArgNo) {
92 if (F.hasParamAttribute(ArgNo, Attribute::NoAlias))
94 F.addParamAttr(ArgNo, Attribute::NoAlias);
99 static bool setOnlyReadsMemory(Function &F, unsigned ArgNo) {
100 if (F.hasParamAttribute(ArgNo, Attribute::ReadOnly))
102 F.addParamAttr(ArgNo, Attribute::ReadOnly);
107 static bool setRetNonNull(Function &F) {
108 assert(F.getReturnType()->isPointerTy() &&
109 "nonnull applies only to pointers");
110 if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NonNull))
112 F.addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
117 static bool setReturnedArg(Function &F, unsigned ArgNo) {
118 if (F.hasParamAttribute(ArgNo, Attribute::Returned))
120 F.addParamAttr(ArgNo, Attribute::Returned);
125 static bool setNonLazyBind(Function &F) {
126 if (F.hasFnAttribute(Attribute::NonLazyBind))
128 F.addFnAttr(Attribute::NonLazyBind);
132 static bool setDoesNotFreeMemory(Function &F) {
133 if (F.hasFnAttribute(Attribute::NoFree))
135 F.addFnAttr(Attribute::NoFree);
139 bool llvm::inferLibFuncAttributes(Module *M, StringRef Name,
140 const TargetLibraryInfo &TLI) {
141 Function *F = M->getFunction(Name);
144 return inferLibFuncAttributes(*F, TLI);
147 bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
149 if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc)))
152 bool Changed = false;
154 if(!isLibFreeFunction(&F, TheLibFunc) && !isReallocLikeFn(&F, &TLI))
155 Changed |= setDoesNotFreeMemory(F);
157 if (F.getParent() != nullptr && F.getParent()->getRtLibUseGOT())
158 Changed |= setNonLazyBind(F);
160 switch (TheLibFunc) {
163 Changed |= setOnlyReadsMemory(F);
164 Changed |= setDoesNotThrow(F);
165 Changed |= setOnlyAccessesArgMemory(F);
166 Changed |= setDoesNotCapture(F, 0);
169 case LibFunc_strrchr:
170 Changed |= setOnlyReadsMemory(F);
171 Changed |= setDoesNotThrow(F);
176 case LibFunc_strtoul:
177 case LibFunc_strtoll:
178 case LibFunc_strtold:
179 case LibFunc_strtoull:
180 Changed |= setDoesNotThrow(F);
181 Changed |= setDoesNotCapture(F, 1);
182 Changed |= setOnlyReadsMemory(F, 0);
185 case LibFunc_strncpy:
186 Changed |= setDoesNotAlias(F, 0);
187 Changed |= setDoesNotAlias(F, 1);
190 case LibFunc_strncat:
191 Changed |= setReturnedArg(F, 0);
194 case LibFunc_stpncpy:
195 Changed |= setDoesNotThrow(F);
196 Changed |= setDoesNotCapture(F, 1);
197 Changed |= setOnlyReadsMemory(F, 1);
199 case LibFunc_strxfrm:
200 Changed |= setDoesNotThrow(F);
201 Changed |= setDoesNotCapture(F, 0);
202 Changed |= setDoesNotCapture(F, 1);
203 Changed |= setOnlyReadsMemory(F, 1);
205 case LibFunc_strcmp: // 0,1
206 case LibFunc_strspn: // 0,1
207 case LibFunc_strncmp: // 0,1
208 case LibFunc_strcspn: // 0,1
209 case LibFunc_strcoll: // 0,1
210 case LibFunc_strcasecmp: // 0,1
211 case LibFunc_strncasecmp: //
212 Changed |= setOnlyReadsMemory(F);
213 Changed |= setDoesNotThrow(F);
214 Changed |= setDoesNotCapture(F, 0);
215 Changed |= setDoesNotCapture(F, 1);
218 case LibFunc_strpbrk:
219 Changed |= setOnlyReadsMemory(F);
220 Changed |= setDoesNotThrow(F);
221 Changed |= setDoesNotCapture(F, 1);
224 case LibFunc_strtok_r:
225 Changed |= setDoesNotThrow(F);
226 Changed |= setDoesNotCapture(F, 1);
227 Changed |= setOnlyReadsMemory(F, 1);
230 Changed |= setDoesNotThrow(F);
231 Changed |= setDoesNotCapture(F, 0);
232 Changed |= setOnlyReadsMemory(F, 0);
235 case LibFunc_setvbuf:
236 Changed |= setDoesNotThrow(F);
237 Changed |= setDoesNotCapture(F, 0);
240 case LibFunc_strndup:
241 Changed |= setDoesNotThrow(F);
242 Changed |= setRetDoesNotAlias(F);
243 Changed |= setDoesNotCapture(F, 0);
244 Changed |= setOnlyReadsMemory(F, 0);
247 case LibFunc_statvfs:
248 Changed |= setDoesNotThrow(F);
249 Changed |= setDoesNotCapture(F, 0);
250 Changed |= setDoesNotCapture(F, 1);
251 Changed |= setOnlyReadsMemory(F, 0);
254 Changed |= setDoesNotThrow(F);
255 Changed |= setDoesNotCapture(F, 0);
256 Changed |= setDoesNotCapture(F, 1);
257 Changed |= setOnlyReadsMemory(F, 0);
258 Changed |= setOnlyReadsMemory(F, 1);
260 case LibFunc_sprintf:
261 Changed |= setDoesNotThrow(F);
262 Changed |= setDoesNotCapture(F, 0);
263 Changed |= setDoesNotAlias(F, 0);
264 Changed |= setDoesNotCapture(F, 1);
265 Changed |= setOnlyReadsMemory(F, 1);
267 case LibFunc_snprintf:
268 Changed |= setDoesNotThrow(F);
269 Changed |= setDoesNotCapture(F, 0);
270 Changed |= setDoesNotAlias(F, 0);
271 Changed |= setDoesNotCapture(F, 2);
272 Changed |= setOnlyReadsMemory(F, 2);
274 case LibFunc_setitimer:
275 Changed |= setDoesNotThrow(F);
276 Changed |= setDoesNotCapture(F, 1);
277 Changed |= setDoesNotCapture(F, 2);
278 Changed |= setOnlyReadsMemory(F, 1);
281 // May throw; "system" is a valid pthread cancellation point.
282 Changed |= setDoesNotCapture(F, 0);
283 Changed |= setOnlyReadsMemory(F, 0);
286 Changed |= setDoesNotThrow(F);
287 Changed |= setRetDoesNotAlias(F);
290 Changed |= setOnlyReadsMemory(F);
291 Changed |= setDoesNotThrow(F);
292 Changed |= setDoesNotCapture(F, 0);
293 Changed |= setDoesNotCapture(F, 1);
296 case LibFunc_memrchr:
297 Changed |= setOnlyReadsMemory(F);
298 Changed |= setDoesNotThrow(F);
303 Changed |= setDoesNotThrow(F);
304 Changed |= setDoesNotCapture(F, 1);
307 Changed |= setDoesNotAlias(F, 0);
308 Changed |= setDoesNotAlias(F, 1);
309 Changed |= setReturnedArg(F, 0);
310 Changed |= setDoesNotThrow(F);
311 Changed |= setDoesNotCapture(F, 1);
312 Changed |= setOnlyReadsMemory(F, 1);
314 case LibFunc_memmove:
315 Changed |= setReturnedArg(F, 0);
316 Changed |= setDoesNotThrow(F);
317 Changed |= setDoesNotCapture(F, 1);
318 Changed |= setOnlyReadsMemory(F, 1);
320 case LibFunc_mempcpy:
321 case LibFunc_memccpy:
322 Changed |= setDoesNotAlias(F, 0);
323 Changed |= setDoesNotAlias(F, 1);
324 Changed |= setDoesNotThrow(F);
325 Changed |= setDoesNotCapture(F, 1);
326 Changed |= setOnlyReadsMemory(F, 1);
328 case LibFunc_memcpy_chk:
329 Changed |= setDoesNotThrow(F);
331 case LibFunc_memalign:
332 Changed |= setRetDoesNotAlias(F);
335 Changed |= setDoesNotThrow(F);
336 Changed |= setDoesNotCapture(F, 0);
337 Changed |= setOnlyReadsMemory(F, 0);
340 Changed |= setDoesNotThrow(F);
341 Changed |= setDoesNotCapture(F, 0);
343 case LibFunc_realloc:
344 Changed |= setDoesNotThrow(F);
345 Changed |= setRetDoesNotAlias(F);
346 Changed |= setDoesNotCapture(F, 0);
349 // May throw; "read" is a valid pthread cancellation point.
350 Changed |= setDoesNotCapture(F, 1);
353 Changed |= setDoesNotThrow(F);
354 Changed |= setDoesNotCapture(F, 0);
358 case LibFunc_realpath:
359 Changed |= setDoesNotThrow(F);
360 Changed |= setDoesNotCapture(F, 0);
361 Changed |= setOnlyReadsMemory(F, 0);
364 Changed |= setDoesNotThrow(F);
365 Changed |= setDoesNotCapture(F, 0);
366 Changed |= setDoesNotCapture(F, 1);
367 Changed |= setOnlyReadsMemory(F, 0);
368 Changed |= setOnlyReadsMemory(F, 1);
370 case LibFunc_readlink:
371 Changed |= setDoesNotThrow(F);
372 Changed |= setDoesNotCapture(F, 0);
373 Changed |= setDoesNotCapture(F, 1);
374 Changed |= setOnlyReadsMemory(F, 0);
377 // May throw; "write" is a valid pthread cancellation point.
378 Changed |= setDoesNotCapture(F, 1);
379 Changed |= setOnlyReadsMemory(F, 1);
381 case LibFunc_aligned_alloc:
382 Changed |= setDoesNotThrow(F);
383 Changed |= setRetDoesNotAlias(F);
386 Changed |= setDoesNotThrow(F);
387 Changed |= setDoesNotCapture(F, 0);
388 Changed |= setDoesNotCapture(F, 1);
389 Changed |= setOnlyReadsMemory(F, 0);
392 Changed |= setDoesNotThrow(F);
393 Changed |= setOnlyReadsMemory(F);
394 Changed |= setDoesNotCapture(F, 0);
395 Changed |= setDoesNotCapture(F, 1);
398 Changed |= setDoesNotThrow(F);
399 Changed |= setDoesNotCapture(F, 0);
402 Changed |= setDoesNotThrow(F);
403 Changed |= setRetDoesNotAlias(F);
407 Changed |= setDoesNotThrow(F);
408 Changed |= setDoesNotCapture(F, 0);
409 Changed |= setOnlyReadsMemory(F, 0);
411 case LibFunc_ctermid:
412 case LibFunc_clearerr:
413 case LibFunc_closedir:
414 Changed |= setDoesNotThrow(F);
415 Changed |= setDoesNotCapture(F, 0);
421 Changed |= setDoesNotThrow(F);
422 Changed |= setOnlyReadsMemory(F);
423 Changed |= setDoesNotCapture(F, 0);
426 Changed |= setDoesNotThrow(F);
427 Changed |= setDoesNotCapture(F, 0);
428 Changed |= setOnlyReadsMemory(F, 0);
431 Changed |= setDoesNotThrow(F);
432 Changed |= setRetDoesNotAlias(F);
433 Changed |= setDoesNotCapture(F, 0);
434 Changed |= setDoesNotCapture(F, 1);
435 Changed |= setOnlyReadsMemory(F, 0);
436 Changed |= setOnlyReadsMemory(F, 1);
439 Changed |= setDoesNotThrow(F);
440 Changed |= setRetDoesNotAlias(F);
441 Changed |= setDoesNotCapture(F, 1);
442 Changed |= setOnlyReadsMemory(F, 1);
449 case LibFunc_fgetc_unlocked:
455 case LibFunc_fsetpos:
456 case LibFunc_flockfile:
457 case LibFunc_funlockfile:
458 case LibFunc_ftrylockfile:
459 Changed |= setDoesNotThrow(F);
460 Changed |= setDoesNotCapture(F, 0);
463 Changed |= setDoesNotThrow(F);
464 Changed |= setDoesNotCapture(F, 0);
465 Changed |= setOnlyReadsMemory(F);
468 case LibFunc_fputc_unlocked:
473 case LibFunc_fstatvfs:
474 Changed |= setDoesNotThrow(F);
475 Changed |= setDoesNotCapture(F, 1);
478 case LibFunc_fgets_unlocked:
479 Changed |= setDoesNotThrow(F);
480 Changed |= setDoesNotCapture(F, 2);
483 case LibFunc_fread_unlocked:
484 Changed |= setDoesNotThrow(F);
485 Changed |= setDoesNotCapture(F, 0);
486 Changed |= setDoesNotCapture(F, 3);
489 case LibFunc_fwrite_unlocked:
490 Changed |= setDoesNotThrow(F);
491 Changed |= setDoesNotCapture(F, 0);
492 Changed |= setDoesNotCapture(F, 3);
493 // FIXME: readonly #1?
496 case LibFunc_fputs_unlocked:
497 Changed |= setDoesNotThrow(F);
498 Changed |= setDoesNotCapture(F, 0);
499 Changed |= setDoesNotCapture(F, 1);
500 Changed |= setOnlyReadsMemory(F, 0);
503 case LibFunc_fprintf:
504 Changed |= setDoesNotThrow(F);
505 Changed |= setDoesNotCapture(F, 0);
506 Changed |= setDoesNotCapture(F, 1);
507 Changed |= setOnlyReadsMemory(F, 1);
509 case LibFunc_fgetpos:
510 Changed |= setDoesNotThrow(F);
511 Changed |= setDoesNotCapture(F, 0);
512 Changed |= setDoesNotCapture(F, 1);
515 case LibFunc_getlogin_r:
516 case LibFunc_getc_unlocked:
517 Changed |= setDoesNotThrow(F);
518 Changed |= setDoesNotCapture(F, 0);
521 Changed |= setDoesNotThrow(F);
522 Changed |= setOnlyReadsMemory(F);
523 Changed |= setDoesNotCapture(F, 0);
526 case LibFunc_getchar:
527 case LibFunc_getchar_unlocked:
528 Changed |= setDoesNotThrow(F);
530 case LibFunc_getitimer:
531 Changed |= setDoesNotThrow(F);
532 Changed |= setDoesNotCapture(F, 1);
534 case LibFunc_getpwnam:
535 Changed |= setDoesNotThrow(F);
536 Changed |= setDoesNotCapture(F, 0);
537 Changed |= setOnlyReadsMemory(F, 0);
540 Changed |= setDoesNotThrow(F);
541 Changed |= setDoesNotCapture(F, 1);
544 Changed |= setDoesNotThrow(F);
545 Changed |= setDoesNotCapture(F, 0);
548 Changed |= setDoesNotThrow(F);
549 Changed |= setDoesNotCapture(F, 0);
550 Changed |= setOnlyReadsMemory(F, 0);
552 case LibFunc_unsetenv:
553 Changed |= setDoesNotThrow(F);
554 Changed |= setDoesNotCapture(F, 0);
555 Changed |= setOnlyReadsMemory(F, 0);
559 Changed |= setDoesNotThrow(F);
560 Changed |= setDoesNotCapture(F, 0);
561 Changed |= setDoesNotCapture(F, 1);
562 Changed |= setOnlyReadsMemory(F, 0);
563 Changed |= setOnlyReadsMemory(F, 1);
566 case LibFunc_putc_unlocked:
567 Changed |= setDoesNotThrow(F);
568 Changed |= setDoesNotCapture(F, 1);
573 Changed |= setDoesNotThrow(F);
574 Changed |= setDoesNotCapture(F, 0);
575 Changed |= setOnlyReadsMemory(F, 0);
578 // May throw; "pread" is a valid pthread cancellation point.
579 Changed |= setDoesNotCapture(F, 1);
582 // May throw; "pwrite" is a valid pthread cancellation point.
583 Changed |= setDoesNotCapture(F, 1);
584 Changed |= setOnlyReadsMemory(F, 1);
586 case LibFunc_putchar:
587 case LibFunc_putchar_unlocked:
588 Changed |= setDoesNotThrow(F);
591 Changed |= setDoesNotThrow(F);
592 Changed |= setRetDoesNotAlias(F);
593 Changed |= setDoesNotCapture(F, 0);
594 Changed |= setDoesNotCapture(F, 1);
595 Changed |= setOnlyReadsMemory(F, 0);
596 Changed |= setOnlyReadsMemory(F, 1);
599 Changed |= setDoesNotThrow(F);
600 Changed |= setDoesNotCapture(F, 0);
603 Changed |= setDoesNotThrow(F);
604 Changed |= setDoesNotCapture(F, 0);
605 Changed |= setOnlyReadsMemory(F, 0);
607 case LibFunc_vsscanf:
608 Changed |= setDoesNotThrow(F);
609 Changed |= setDoesNotCapture(F, 0);
610 Changed |= setDoesNotCapture(F, 1);
611 Changed |= setOnlyReadsMemory(F, 0);
612 Changed |= setOnlyReadsMemory(F, 1);
614 case LibFunc_vfscanf:
615 Changed |= setDoesNotThrow(F);
616 Changed |= setDoesNotCapture(F, 0);
617 Changed |= setDoesNotCapture(F, 1);
618 Changed |= setOnlyReadsMemory(F, 1);
621 Changed |= setDoesNotThrow(F);
622 Changed |= setRetDoesNotAlias(F);
624 case LibFunc_vprintf:
625 Changed |= setDoesNotThrow(F);
626 Changed |= setDoesNotCapture(F, 0);
627 Changed |= setOnlyReadsMemory(F, 0);
629 case LibFunc_vfprintf:
630 case LibFunc_vsprintf:
631 Changed |= setDoesNotThrow(F);
632 Changed |= setDoesNotCapture(F, 0);
633 Changed |= setDoesNotCapture(F, 1);
634 Changed |= setOnlyReadsMemory(F, 1);
636 case LibFunc_vsnprintf:
637 Changed |= setDoesNotThrow(F);
638 Changed |= setDoesNotCapture(F, 0);
639 Changed |= setDoesNotCapture(F, 2);
640 Changed |= setOnlyReadsMemory(F, 2);
643 // May throw; "open" is a valid pthread cancellation point.
644 Changed |= setDoesNotCapture(F, 0);
645 Changed |= setOnlyReadsMemory(F, 0);
647 case LibFunc_opendir:
648 Changed |= setDoesNotThrow(F);
649 Changed |= setRetDoesNotAlias(F);
650 Changed |= setDoesNotCapture(F, 0);
651 Changed |= setOnlyReadsMemory(F, 0);
653 case LibFunc_tmpfile:
654 Changed |= setDoesNotThrow(F);
655 Changed |= setRetDoesNotAlias(F);
658 Changed |= setDoesNotThrow(F);
659 Changed |= setDoesNotCapture(F, 0);
665 Changed |= setDoesNotThrow(F);
666 Changed |= setDoesNotAccessMemory(F);
669 Changed |= setDoesNotThrow(F);
670 Changed |= setDoesNotCapture(F, 0);
671 Changed |= setDoesNotCapture(F, 1);
672 Changed |= setOnlyReadsMemory(F, 0);
675 Changed |= setDoesNotThrow(F);
676 Changed |= setDoesNotCapture(F, 0);
677 Changed |= setOnlyReadsMemory(F, 0);
680 // May throw; places call through function pointer.
681 Changed |= setDoesNotCapture(F, 3);
683 case LibFunc_dunder_strdup:
684 case LibFunc_dunder_strndup:
685 Changed |= setDoesNotThrow(F);
686 Changed |= setRetDoesNotAlias(F);
687 Changed |= setDoesNotCapture(F, 0);
688 Changed |= setOnlyReadsMemory(F, 0);
690 case LibFunc_dunder_strtok_r:
691 Changed |= setDoesNotThrow(F);
692 Changed |= setDoesNotCapture(F, 1);
693 Changed |= setOnlyReadsMemory(F, 1);
695 case LibFunc_under_IO_getc:
696 Changed |= setDoesNotThrow(F);
697 Changed |= setDoesNotCapture(F, 0);
699 case LibFunc_under_IO_putc:
700 Changed |= setDoesNotThrow(F);
701 Changed |= setDoesNotCapture(F, 1);
703 case LibFunc_dunder_isoc99_scanf:
704 Changed |= setDoesNotThrow(F);
705 Changed |= setDoesNotCapture(F, 0);
706 Changed |= setOnlyReadsMemory(F, 0);
709 case LibFunc_lstat64:
710 case LibFunc_statvfs64:
711 Changed |= setDoesNotThrow(F);
712 Changed |= setDoesNotCapture(F, 0);
713 Changed |= setDoesNotCapture(F, 1);
714 Changed |= setOnlyReadsMemory(F, 0);
716 case LibFunc_dunder_isoc99_sscanf:
717 Changed |= setDoesNotThrow(F);
718 Changed |= setDoesNotCapture(F, 0);
719 Changed |= setDoesNotCapture(F, 1);
720 Changed |= setOnlyReadsMemory(F, 0);
721 Changed |= setOnlyReadsMemory(F, 1);
723 case LibFunc_fopen64:
724 Changed |= setDoesNotThrow(F);
725 Changed |= setRetDoesNotAlias(F);
726 Changed |= setDoesNotCapture(F, 0);
727 Changed |= setDoesNotCapture(F, 1);
728 Changed |= setOnlyReadsMemory(F, 0);
729 Changed |= setOnlyReadsMemory(F, 1);
731 case LibFunc_fseeko64:
732 case LibFunc_ftello64:
733 Changed |= setDoesNotThrow(F);
734 Changed |= setDoesNotCapture(F, 0);
736 case LibFunc_tmpfile64:
737 Changed |= setDoesNotThrow(F);
738 Changed |= setRetDoesNotAlias(F);
740 case LibFunc_fstat64:
741 case LibFunc_fstatvfs64:
742 Changed |= setDoesNotThrow(F);
743 Changed |= setDoesNotCapture(F, 1);
746 // May throw; "open" is a valid pthread cancellation point.
747 Changed |= setDoesNotCapture(F, 0);
748 Changed |= setOnlyReadsMemory(F, 0);
750 case LibFunc_gettimeofday:
751 // Currently some platforms have the restrict keyword on the arguments to
752 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
754 Changed |= setDoesNotThrow(F);
755 Changed |= setDoesNotCapture(F, 0);
756 Changed |= setDoesNotCapture(F, 1);
758 case LibFunc_Znwj: // new(unsigned int)
759 case LibFunc_Znwm: // new(unsigned long)
760 case LibFunc_Znaj: // new[](unsigned int)
761 case LibFunc_Znam: // new[](unsigned long)
762 case LibFunc_msvc_new_int: // new(unsigned int)
763 case LibFunc_msvc_new_longlong: // new(unsigned long long)
764 case LibFunc_msvc_new_array_int: // new[](unsigned int)
765 case LibFunc_msvc_new_array_longlong: // new[](unsigned long long)
766 // Operator new always returns a nonnull noalias pointer
767 Changed |= setRetNonNull(F);
768 Changed |= setRetDoesNotAlias(F);
770 // TODO: add LibFunc entries for:
771 // case LibFunc_memset_pattern4:
772 // case LibFunc_memset_pattern8:
773 case LibFunc_memset_pattern16:
774 Changed |= setOnlyAccessesArgMemory(F);
775 Changed |= setDoesNotCapture(F, 0);
776 Changed |= setDoesNotCapture(F, 1);
777 Changed |= setOnlyReadsMemory(F, 1);
779 // int __nvvm_reflect(const char *)
780 case LibFunc_nvvm_reflect:
781 Changed |= setDoesNotAccessMemory(F);
782 Changed |= setDoesNotThrow(F);
786 // FIXME: It'd be really nice to cover all the library functions we're
792 bool llvm::hasFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
793 LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn) {
794 switch (Ty->getTypeID()) {
797 case Type::FloatTyID:
798 return TLI->has(FloatFn);
799 case Type::DoubleTyID:
800 return TLI->has(DoubleFn);
802 return TLI->has(LongDoubleFn);
806 StringRef llvm::getFloatFnName(const TargetLibraryInfo *TLI, Type *Ty,
807 LibFunc DoubleFn, LibFunc FloatFn,
808 LibFunc LongDoubleFn) {
809 assert(hasFloatFn(TLI, Ty, DoubleFn, FloatFn, LongDoubleFn) &&
810 "Cannot get name for unavailable function!");
812 switch (Ty->getTypeID()) {
814 llvm_unreachable("No name for HalfTy!");
815 case Type::FloatTyID:
816 return TLI->getName(FloatFn);
817 case Type::DoubleTyID:
818 return TLI->getName(DoubleFn);
820 return TLI->getName(LongDoubleFn);
824 //- Emit LibCalls ------------------------------------------------------------//
826 Value *llvm::castToCStr(Value *V, IRBuilderBase &B) {
827 unsigned AS = V->getType()->getPointerAddressSpace();
828 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
831 static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType,
832 ArrayRef<Type *> ParamTypes,
833 ArrayRef<Value *> Operands, IRBuilderBase &B,
834 const TargetLibraryInfo *TLI,
835 bool IsVaArgs = false) {
836 if (!TLI->has(TheLibFunc))
839 Module *M = B.GetInsertBlock()->getModule();
840 StringRef FuncName = TLI->getName(TheLibFunc);
841 FunctionType *FuncType = FunctionType::get(ReturnType, ParamTypes, IsVaArgs);
842 FunctionCallee Callee = M->getOrInsertFunction(FuncName, FuncType);
843 inferLibFuncAttributes(M, FuncName, *TLI);
844 CallInst *CI = B.CreateCall(Callee, Operands, FuncName);
845 if (const Function *F =
846 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
847 CI->setCallingConv(F->getCallingConv());
851 Value *llvm::emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL,
852 const TargetLibraryInfo *TLI) {
853 LLVMContext &Context = B.GetInsertBlock()->getContext();
854 return emitLibCall(LibFunc_strlen, DL.getIntPtrType(Context),
855 B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI);
858 Value *llvm::emitStrDup(Value *Ptr, IRBuilderBase &B,
859 const TargetLibraryInfo *TLI) {
860 return emitLibCall(LibFunc_strdup, B.getInt8PtrTy(), B.getInt8PtrTy(),
861 castToCStr(Ptr, B), B, TLI);
864 Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilderBase &B,
865 const TargetLibraryInfo *TLI) {
866 Type *I8Ptr = B.getInt8PtrTy();
867 Type *I32Ty = B.getInt32Ty();
868 return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, I32Ty},
869 {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, B, TLI);
872 Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
873 const DataLayout &DL, const TargetLibraryInfo *TLI) {
874 LLVMContext &Context = B.GetInsertBlock()->getContext();
876 LibFunc_strncmp, B.getInt32Ty(),
877 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
878 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
881 Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B,
882 const TargetLibraryInfo *TLI) {
883 Type *I8Ptr = B.getInt8PtrTy();
884 return emitLibCall(LibFunc_strcpy, I8Ptr, {I8Ptr, I8Ptr},
885 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI);
888 Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B,
889 const TargetLibraryInfo *TLI) {
890 Type *I8Ptr = B.getInt8PtrTy();
891 return emitLibCall(LibFunc_stpcpy, I8Ptr, {I8Ptr, I8Ptr},
892 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI);
895 Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
896 const TargetLibraryInfo *TLI) {
897 Type *I8Ptr = B.getInt8PtrTy();
898 return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()},
899 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI);
902 Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
903 const TargetLibraryInfo *TLI) {
904 Type *I8Ptr = B.getInt8PtrTy();
905 return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()},
906 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI);
909 Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
910 IRBuilderBase &B, const DataLayout &DL,
911 const TargetLibraryInfo *TLI) {
912 if (!TLI->has(LibFunc_memcpy_chk))
915 Module *M = B.GetInsertBlock()->getModule();
917 AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex,
918 Attribute::NoUnwind);
919 LLVMContext &Context = B.GetInsertBlock()->getContext();
920 FunctionCallee MemCpy = M->getOrInsertFunction(
921 "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
922 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
923 DL.getIntPtrType(Context));
924 Dst = castToCStr(Dst, B);
925 Src = castToCStr(Src, B);
926 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
927 if (const Function *F =
928 dyn_cast<Function>(MemCpy.getCallee()->stripPointerCasts()))
929 CI->setCallingConv(F->getCallingConv());
933 Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B,
934 const DataLayout &DL, const TargetLibraryInfo *TLI) {
935 LLVMContext &Context = B.GetInsertBlock()->getContext();
937 LibFunc_memchr, B.getInt8PtrTy(),
938 {B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context)},
939 {castToCStr(Ptr, B), Val, Len}, B, TLI);
942 Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
943 const DataLayout &DL, const TargetLibraryInfo *TLI) {
944 LLVMContext &Context = B.GetInsertBlock()->getContext();
946 LibFunc_memcmp, B.getInt32Ty(),
947 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
948 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
951 Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
952 const DataLayout &DL, const TargetLibraryInfo *TLI) {
953 LLVMContext &Context = B.GetInsertBlock()->getContext();
955 LibFunc_bcmp, B.getInt32Ty(),
956 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
957 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
960 Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
961 IRBuilderBase &B, const TargetLibraryInfo *TLI) {
963 LibFunc_memccpy, B.getInt8PtrTy(),
964 {B.getInt8PtrTy(), B.getInt8PtrTy(), B.getInt32Ty(), Len->getType()},
965 {Ptr1, Ptr2, Val, Len}, B, TLI);
968 Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
969 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B,
970 const TargetLibraryInfo *TLI) {
971 SmallVector<Value *, 8> Args{castToCStr(Dest, B), Size, castToCStr(Fmt, B)};
972 Args.insert(Args.end(), VariadicArgs.begin(), VariadicArgs.end());
973 return emitLibCall(LibFunc_snprintf, B.getInt32Ty(),
974 {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy()},
975 Args, B, TLI, /*IsVaArgs=*/true);
978 Value *llvm::emitSPrintf(Value *Dest, Value *Fmt,
979 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B,
980 const TargetLibraryInfo *TLI) {
981 SmallVector<Value *, 8> Args{castToCStr(Dest, B), castToCStr(Fmt, B)};
982 Args.insert(Args.end(), VariadicArgs.begin(), VariadicArgs.end());
983 return emitLibCall(LibFunc_sprintf, B.getInt32Ty(),
984 {B.getInt8PtrTy(), B.getInt8PtrTy()}, Args, B, TLI,
988 Value *llvm::emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B,
989 const TargetLibraryInfo *TLI) {
990 return emitLibCall(LibFunc_strcat, B.getInt8PtrTy(),
991 {B.getInt8PtrTy(), B.getInt8PtrTy()},
992 {castToCStr(Dest, B), castToCStr(Src, B)}, B, TLI);
995 Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
996 const TargetLibraryInfo *TLI) {
997 return emitLibCall(LibFunc_strlcpy, Size->getType(),
998 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()},
999 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI);
1002 Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
1003 const TargetLibraryInfo *TLI) {
1004 return emitLibCall(LibFunc_strlcat, Size->getType(),
1005 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()},
1006 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI);
1009 Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
1010 const TargetLibraryInfo *TLI) {
1011 return emitLibCall(LibFunc_strncat, B.getInt8PtrTy(),
1012 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()},
1013 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI);
1016 Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
1017 IRBuilderBase &B, const TargetLibraryInfo *TLI) {
1019 LibFunc_vsnprintf, B.getInt32Ty(),
1020 {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy(), VAList->getType()},
1021 {castToCStr(Dest, B), Size, castToCStr(Fmt, B), VAList}, B, TLI);
1024 Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList,
1025 IRBuilderBase &B, const TargetLibraryInfo *TLI) {
1026 return emitLibCall(LibFunc_vsprintf, B.getInt32Ty(),
1027 {B.getInt8PtrTy(), B.getInt8PtrTy(), VAList->getType()},
1028 {castToCStr(Dest, B), castToCStr(Fmt, B), VAList}, B, TLI);
1031 /// Append a suffix to the function name according to the type of 'Op'.
1032 static void appendTypeSuffix(Value *Op, StringRef &Name,
1033 SmallString<20> &NameBuffer) {
1034 if (!Op->getType()->isDoubleTy()) {
1037 if (Op->getType()->isFloatTy())
1046 static Value *emitUnaryFloatFnCallHelper(Value *Op, StringRef Name,
1048 const AttributeList &Attrs) {
1049 assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall");
1051 Module *M = B.GetInsertBlock()->getModule();
1052 FunctionCallee Callee =
1053 M->getOrInsertFunction(Name, Op->getType(), Op->getType());
1054 CallInst *CI = B.CreateCall(Callee, Op, Name);
1056 // The incoming attribute set may have come from a speculatable intrinsic, but
1057 // is being replaced with a library call which is not allowed to be
1059 CI->setAttributes(Attrs.removeAttribute(B.getContext(),
1060 AttributeList::FunctionIndex,
1061 Attribute::Speculatable));
1062 if (const Function *F =
1063 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
1064 CI->setCallingConv(F->getCallingConv());
1069 Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilderBase &B,
1070 const AttributeList &Attrs) {
1071 SmallString<20> NameBuffer;
1072 appendTypeSuffix(Op, Name, NameBuffer);
1074 return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs);
1077 Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
1078 LibFunc DoubleFn, LibFunc FloatFn,
1079 LibFunc LongDoubleFn, IRBuilderBase &B,
1080 const AttributeList &Attrs) {
1081 // Get the name of the function according to TLI.
1082 StringRef Name = getFloatFnName(TLI, Op->getType(),
1083 DoubleFn, FloatFn, LongDoubleFn);
1085 return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs);
1088 static Value *emitBinaryFloatFnCallHelper(Value *Op1, Value *Op2,
1089 StringRef Name, IRBuilderBase &B,
1090 const AttributeList &Attrs) {
1091 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
1093 Module *M = B.GetInsertBlock()->getModule();
1094 FunctionCallee Callee = M->getOrInsertFunction(Name, Op1->getType(),
1095 Op1->getType(), Op2->getType());
1096 CallInst *CI = B.CreateCall(Callee, { Op1, Op2 }, Name);
1098 // The incoming attribute set may have come from a speculatable intrinsic, but
1099 // is being replaced with a library call which is not allowed to be
1101 CI->setAttributes(Attrs.removeAttribute(B.getContext(),
1102 AttributeList::FunctionIndex,
1103 Attribute::Speculatable));
1104 if (const Function *F =
1105 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
1106 CI->setCallingConv(F->getCallingConv());
1111 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
1113 const AttributeList &Attrs) {
1114 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
1116 SmallString<20> NameBuffer;
1117 appendTypeSuffix(Op1, Name, NameBuffer);
1119 return emitBinaryFloatFnCallHelper(Op1, Op2, Name, B, Attrs);
1122 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2,
1123 const TargetLibraryInfo *TLI,
1124 LibFunc DoubleFn, LibFunc FloatFn,
1125 LibFunc LongDoubleFn, IRBuilderBase &B,
1126 const AttributeList &Attrs) {
1127 // Get the name of the function according to TLI.
1128 StringRef Name = getFloatFnName(TLI, Op1->getType(),
1129 DoubleFn, FloatFn, LongDoubleFn);
1131 return emitBinaryFloatFnCallHelper(Op1, Op2, Name, B, Attrs);
1134 Value *llvm::emitPutChar(Value *Char, IRBuilderBase &B,
1135 const TargetLibraryInfo *TLI) {
1136 if (!TLI->has(LibFunc_putchar))
1139 Module *M = B.GetInsertBlock()->getModule();
1140 StringRef PutCharName = TLI->getName(LibFunc_putchar);
1141 FunctionCallee PutChar =
1142 M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
1143 inferLibFuncAttributes(M, PutCharName, *TLI);
1144 CallInst *CI = B.CreateCall(PutChar,
1145 B.CreateIntCast(Char,
1151 if (const Function *F =
1152 dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts()))
1153 CI->setCallingConv(F->getCallingConv());
1157 Value *llvm::emitPutS(Value *Str, IRBuilderBase &B,
1158 const TargetLibraryInfo *TLI) {
1159 if (!TLI->has(LibFunc_puts))
1162 Module *M = B.GetInsertBlock()->getModule();
1163 StringRef PutsName = TLI->getName(LibFunc_puts);
1164 FunctionCallee PutS =
1165 M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy());
1166 inferLibFuncAttributes(M, PutsName, *TLI);
1167 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName);
1168 if (const Function *F =
1169 dyn_cast<Function>(PutS.getCallee()->stripPointerCasts()))
1170 CI->setCallingConv(F->getCallingConv());
1174 Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilderBase &B,
1175 const TargetLibraryInfo *TLI) {
1176 if (!TLI->has(LibFunc_fputc))
1179 Module *M = B.GetInsertBlock()->getModule();
1180 StringRef FPutcName = TLI->getName(LibFunc_fputc);
1181 FunctionCallee F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(),
1182 B.getInt32Ty(), File->getType());
1183 if (File->getType()->isPointerTy())
1184 inferLibFuncAttributes(M, FPutcName, *TLI);
1185 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
1187 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
1189 if (const Function *Fn =
1190 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1191 CI->setCallingConv(Fn->getCallingConv());
1195 Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilderBase &B,
1196 const TargetLibraryInfo *TLI) {
1197 if (!TLI->has(LibFunc_fputs))
1200 Module *M = B.GetInsertBlock()->getModule();
1201 StringRef FPutsName = TLI->getName(LibFunc_fputs);
1202 FunctionCallee F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
1203 B.getInt8PtrTy(), File->getType());
1204 if (File->getType()->isPointerTy())
1205 inferLibFuncAttributes(M, FPutsName, *TLI);
1206 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName);
1208 if (const Function *Fn =
1209 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1210 CI->setCallingConv(Fn->getCallingConv());
1214 Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B,
1215 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1216 if (!TLI->has(LibFunc_fwrite))
1219 Module *M = B.GetInsertBlock()->getModule();
1220 LLVMContext &Context = B.GetInsertBlock()->getContext();
1221 StringRef FWriteName = TLI->getName(LibFunc_fwrite);
1222 FunctionCallee F = M->getOrInsertFunction(
1223 FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
1224 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
1226 if (File->getType()->isPointerTy())
1227 inferLibFuncAttributes(M, FWriteName, *TLI);
1229 B.CreateCall(F, {castToCStr(Ptr, B), Size,
1230 ConstantInt::get(DL.getIntPtrType(Context), 1), File});
1232 if (const Function *Fn =
1233 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1234 CI->setCallingConv(Fn->getCallingConv());
1238 Value *llvm::emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL,
1239 const TargetLibraryInfo *TLI) {
1240 if (!TLI->has(LibFunc_malloc))
1243 Module *M = B.GetInsertBlock()->getModule();
1244 StringRef MallocName = TLI->getName(LibFunc_malloc);
1245 LLVMContext &Context = B.GetInsertBlock()->getContext();
1246 FunctionCallee Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(),
1247 DL.getIntPtrType(Context));
1248 inferLibFuncAttributes(M, MallocName, *TLI);
1249 CallInst *CI = B.CreateCall(Malloc, Num, MallocName);
1251 if (const Function *F =
1252 dyn_cast<Function>(Malloc.getCallee()->stripPointerCasts()))
1253 CI->setCallingConv(F->getCallingConv());
1258 Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
1259 IRBuilderBase &B, const TargetLibraryInfo &TLI) {
1260 if (!TLI.has(LibFunc_calloc))
1263 Module *M = B.GetInsertBlock()->getModule();
1264 StringRef CallocName = TLI.getName(LibFunc_calloc);
1265 const DataLayout &DL = M->getDataLayout();
1266 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext()));
1267 FunctionCallee Calloc = M->getOrInsertFunction(
1268 CallocName, Attrs, B.getInt8PtrTy(), PtrType, PtrType);
1269 inferLibFuncAttributes(M, CallocName, TLI);
1270 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
1273 dyn_cast<Function>(Calloc.getCallee()->stripPointerCasts()))
1274 CI->setCallingConv(F->getCallingConv());