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 setOnlyReadsMemory(Function
&F
, unsigned ArgNo
) {
92 if (F
.hasParamAttribute(ArgNo
, Attribute::ReadOnly
))
94 F
.addParamAttr(ArgNo
, Attribute::ReadOnly
);
99 static bool setRetNonNull(Function
&F
) {
100 assert(F
.getReturnType()->isPointerTy() &&
101 "nonnull applies only to pointers");
102 if (F
.hasAttribute(AttributeList::ReturnIndex
, Attribute::NonNull
))
104 F
.addAttribute(AttributeList::ReturnIndex
, Attribute::NonNull
);
109 static bool setReturnedArg(Function
&F
, unsigned ArgNo
) {
110 if (F
.hasParamAttribute(ArgNo
, Attribute::Returned
))
112 F
.addParamAttr(ArgNo
, Attribute::Returned
);
117 static bool setNonLazyBind(Function
&F
) {
118 if (F
.hasFnAttribute(Attribute::NonLazyBind
))
120 F
.addFnAttr(Attribute::NonLazyBind
);
124 static bool setDoesNotFreeMemory(Function
&F
) {
125 if (F
.hasFnAttribute(Attribute::NoFree
))
127 F
.addFnAttr(Attribute::NoFree
);
131 bool llvm::inferLibFuncAttributes(Module
*M
, StringRef Name
,
132 const TargetLibraryInfo
&TLI
) {
133 Function
*F
= M
->getFunction(Name
);
136 return inferLibFuncAttributes(*F
, TLI
);
139 bool llvm::inferLibFuncAttributes(Function
&F
, const TargetLibraryInfo
&TLI
) {
141 if (!(TLI
.getLibFunc(F
, TheLibFunc
) && TLI
.has(TheLibFunc
)))
144 bool Changed
= false;
146 if(!isLibFreeFunction(&F
, TheLibFunc
) && !isReallocLikeFn(&F
, &TLI
))
147 Changed
|= setDoesNotFreeMemory(F
);
149 if (F
.getParent() != nullptr && F
.getParent()->getRtLibUseGOT())
150 Changed
|= setNonLazyBind(F
);
152 switch (TheLibFunc
) {
155 Changed
|= setOnlyReadsMemory(F
);
156 Changed
|= setDoesNotThrow(F
);
157 Changed
|= setOnlyAccessesArgMemory(F
);
158 Changed
|= setDoesNotCapture(F
, 0);
161 case LibFunc_strrchr
:
162 Changed
|= setOnlyReadsMemory(F
);
163 Changed
|= setDoesNotThrow(F
);
168 case LibFunc_strtoul
:
169 case LibFunc_strtoll
:
170 case LibFunc_strtold
:
171 case LibFunc_strtoull
:
172 Changed
|= setDoesNotThrow(F
);
173 Changed
|= setDoesNotCapture(F
, 1);
174 Changed
|= setOnlyReadsMemory(F
, 0);
177 case LibFunc_strncpy
:
179 case LibFunc_strncat
:
180 Changed
|= setReturnedArg(F
, 0);
183 case LibFunc_stpncpy
:
184 Changed
|= setDoesNotThrow(F
);
185 Changed
|= setDoesNotCapture(F
, 1);
186 Changed
|= setOnlyReadsMemory(F
, 1);
188 case LibFunc_strxfrm
:
189 Changed
|= setDoesNotThrow(F
);
190 Changed
|= setDoesNotCapture(F
, 0);
191 Changed
|= setDoesNotCapture(F
, 1);
192 Changed
|= setOnlyReadsMemory(F
, 1);
194 case LibFunc_strcmp
: // 0,1
195 case LibFunc_strspn
: // 0,1
196 case LibFunc_strncmp
: // 0,1
197 case LibFunc_strcspn
: // 0,1
198 case LibFunc_strcoll
: // 0,1
199 case LibFunc_strcasecmp
: // 0,1
200 case LibFunc_strncasecmp
: //
201 Changed
|= setOnlyReadsMemory(F
);
202 Changed
|= setDoesNotThrow(F
);
203 Changed
|= setDoesNotCapture(F
, 0);
204 Changed
|= setDoesNotCapture(F
, 1);
207 case LibFunc_strpbrk
:
208 Changed
|= setOnlyReadsMemory(F
);
209 Changed
|= setDoesNotThrow(F
);
210 Changed
|= setDoesNotCapture(F
, 1);
213 case LibFunc_strtok_r
:
214 Changed
|= setDoesNotThrow(F
);
215 Changed
|= setDoesNotCapture(F
, 1);
216 Changed
|= setOnlyReadsMemory(F
, 1);
219 Changed
|= setDoesNotThrow(F
);
220 Changed
|= setDoesNotCapture(F
, 0);
221 Changed
|= setOnlyReadsMemory(F
, 0);
224 case LibFunc_setvbuf
:
225 Changed
|= setDoesNotThrow(F
);
226 Changed
|= setDoesNotCapture(F
, 0);
229 case LibFunc_strndup
:
230 Changed
|= setDoesNotThrow(F
);
231 Changed
|= setRetDoesNotAlias(F
);
232 Changed
|= setDoesNotCapture(F
, 0);
233 Changed
|= setOnlyReadsMemory(F
, 0);
236 case LibFunc_statvfs
:
237 Changed
|= setDoesNotThrow(F
);
238 Changed
|= setDoesNotCapture(F
, 0);
239 Changed
|= setDoesNotCapture(F
, 1);
240 Changed
|= setOnlyReadsMemory(F
, 0);
243 Changed
|= setDoesNotThrow(F
);
244 Changed
|= setDoesNotCapture(F
, 0);
245 Changed
|= setDoesNotCapture(F
, 1);
246 Changed
|= setOnlyReadsMemory(F
, 0);
247 Changed
|= setOnlyReadsMemory(F
, 1);
249 case LibFunc_sprintf
:
250 Changed
|= setDoesNotThrow(F
);
251 Changed
|= setDoesNotCapture(F
, 0);
252 Changed
|= setDoesNotCapture(F
, 1);
253 Changed
|= setOnlyReadsMemory(F
, 1);
255 case LibFunc_snprintf
:
256 Changed
|= setDoesNotThrow(F
);
257 Changed
|= setDoesNotCapture(F
, 0);
258 Changed
|= setDoesNotCapture(F
, 2);
259 Changed
|= setOnlyReadsMemory(F
, 2);
261 case LibFunc_setitimer
:
262 Changed
|= setDoesNotThrow(F
);
263 Changed
|= setDoesNotCapture(F
, 1);
264 Changed
|= setDoesNotCapture(F
, 2);
265 Changed
|= setOnlyReadsMemory(F
, 1);
268 // May throw; "system" is a valid pthread cancellation point.
269 Changed
|= setDoesNotCapture(F
, 0);
270 Changed
|= setOnlyReadsMemory(F
, 0);
273 Changed
|= setDoesNotThrow(F
);
274 Changed
|= setRetDoesNotAlias(F
);
277 Changed
|= setOnlyReadsMemory(F
);
278 Changed
|= setDoesNotThrow(F
);
279 Changed
|= setDoesNotCapture(F
, 0);
280 Changed
|= setDoesNotCapture(F
, 1);
283 case LibFunc_memrchr
:
284 Changed
|= setOnlyReadsMemory(F
);
285 Changed
|= setDoesNotThrow(F
);
290 Changed
|= setDoesNotThrow(F
);
291 Changed
|= setDoesNotCapture(F
, 1);
294 case LibFunc_memmove
:
295 Changed
|= setReturnedArg(F
, 0);
297 case LibFunc_mempcpy
:
298 case LibFunc_memccpy
:
299 Changed
|= setDoesNotThrow(F
);
300 Changed
|= setDoesNotCapture(F
, 1);
301 Changed
|= setOnlyReadsMemory(F
, 1);
303 case LibFunc_memcpy_chk
:
304 Changed
|= setDoesNotThrow(F
);
306 case LibFunc_memalign
:
307 Changed
|= setRetDoesNotAlias(F
);
310 Changed
|= setDoesNotThrow(F
);
311 Changed
|= setDoesNotCapture(F
, 0);
312 Changed
|= setOnlyReadsMemory(F
, 0);
315 Changed
|= setDoesNotThrow(F
);
316 Changed
|= setDoesNotCapture(F
, 0);
318 case LibFunc_realloc
:
319 Changed
|= setDoesNotThrow(F
);
320 Changed
|= setRetDoesNotAlias(F
);
321 Changed
|= setDoesNotCapture(F
, 0);
324 // May throw; "read" is a valid pthread cancellation point.
325 Changed
|= setDoesNotCapture(F
, 1);
328 Changed
|= setDoesNotThrow(F
);
329 Changed
|= setDoesNotCapture(F
, 0);
333 case LibFunc_realpath
:
334 Changed
|= setDoesNotThrow(F
);
335 Changed
|= setDoesNotCapture(F
, 0);
336 Changed
|= setOnlyReadsMemory(F
, 0);
339 Changed
|= setDoesNotThrow(F
);
340 Changed
|= setDoesNotCapture(F
, 0);
341 Changed
|= setDoesNotCapture(F
, 1);
342 Changed
|= setOnlyReadsMemory(F
, 0);
343 Changed
|= setOnlyReadsMemory(F
, 1);
345 case LibFunc_readlink
:
346 Changed
|= setDoesNotThrow(F
);
347 Changed
|= setDoesNotCapture(F
, 0);
348 Changed
|= setDoesNotCapture(F
, 1);
349 Changed
|= setOnlyReadsMemory(F
, 0);
352 // May throw; "write" is a valid pthread cancellation point.
353 Changed
|= setDoesNotCapture(F
, 1);
354 Changed
|= setOnlyReadsMemory(F
, 1);
357 Changed
|= setDoesNotThrow(F
);
358 Changed
|= setDoesNotCapture(F
, 0);
359 Changed
|= setDoesNotCapture(F
, 1);
360 Changed
|= setOnlyReadsMemory(F
, 0);
363 Changed
|= setDoesNotThrow(F
);
364 Changed
|= setOnlyReadsMemory(F
);
365 Changed
|= setDoesNotCapture(F
, 0);
366 Changed
|= setDoesNotCapture(F
, 1);
369 Changed
|= setDoesNotThrow(F
);
370 Changed
|= setDoesNotCapture(F
, 0);
373 Changed
|= setDoesNotThrow(F
);
374 Changed
|= setRetDoesNotAlias(F
);
378 Changed
|= setDoesNotThrow(F
);
379 Changed
|= setDoesNotCapture(F
, 0);
380 Changed
|= setOnlyReadsMemory(F
, 0);
382 case LibFunc_ctermid
:
383 case LibFunc_clearerr
:
384 case LibFunc_closedir
:
385 Changed
|= setDoesNotThrow(F
);
386 Changed
|= setDoesNotCapture(F
, 0);
392 Changed
|= setDoesNotThrow(F
);
393 Changed
|= setOnlyReadsMemory(F
);
394 Changed
|= setDoesNotCapture(F
, 0);
397 Changed
|= setDoesNotThrow(F
);
398 Changed
|= setDoesNotCapture(F
, 0);
399 Changed
|= setOnlyReadsMemory(F
, 0);
402 Changed
|= setDoesNotThrow(F
);
403 Changed
|= setRetDoesNotAlias(F
);
404 Changed
|= setDoesNotCapture(F
, 0);
405 Changed
|= setDoesNotCapture(F
, 1);
406 Changed
|= setOnlyReadsMemory(F
, 0);
407 Changed
|= setOnlyReadsMemory(F
, 1);
410 Changed
|= setDoesNotThrow(F
);
411 Changed
|= setRetDoesNotAlias(F
);
412 Changed
|= setDoesNotCapture(F
, 1);
413 Changed
|= setOnlyReadsMemory(F
, 1);
420 case LibFunc_fgetc_unlocked
:
426 case LibFunc_fsetpos
:
427 case LibFunc_flockfile
:
428 case LibFunc_funlockfile
:
429 case LibFunc_ftrylockfile
:
430 Changed
|= setDoesNotThrow(F
);
431 Changed
|= setDoesNotCapture(F
, 0);
434 Changed
|= setDoesNotThrow(F
);
435 Changed
|= setDoesNotCapture(F
, 0);
436 Changed
|= setOnlyReadsMemory(F
);
439 case LibFunc_fputc_unlocked
:
444 case LibFunc_fstatvfs
:
445 Changed
|= setDoesNotThrow(F
);
446 Changed
|= setDoesNotCapture(F
, 1);
449 case LibFunc_fgets_unlocked
:
450 Changed
|= setDoesNotThrow(F
);
451 Changed
|= setDoesNotCapture(F
, 2);
454 case LibFunc_fread_unlocked
:
455 Changed
|= setDoesNotThrow(F
);
456 Changed
|= setDoesNotCapture(F
, 0);
457 Changed
|= setDoesNotCapture(F
, 3);
460 case LibFunc_fwrite_unlocked
:
461 Changed
|= setDoesNotThrow(F
);
462 Changed
|= setDoesNotCapture(F
, 0);
463 Changed
|= setDoesNotCapture(F
, 3);
464 // FIXME: readonly #1?
467 case LibFunc_fputs_unlocked
:
468 Changed
|= setDoesNotThrow(F
);
469 Changed
|= setDoesNotCapture(F
, 0);
470 Changed
|= setDoesNotCapture(F
, 1);
471 Changed
|= setOnlyReadsMemory(F
, 0);
474 case LibFunc_fprintf
:
475 Changed
|= setDoesNotThrow(F
);
476 Changed
|= setDoesNotCapture(F
, 0);
477 Changed
|= setDoesNotCapture(F
, 1);
478 Changed
|= setOnlyReadsMemory(F
, 1);
480 case LibFunc_fgetpos
:
481 Changed
|= setDoesNotThrow(F
);
482 Changed
|= setDoesNotCapture(F
, 0);
483 Changed
|= setDoesNotCapture(F
, 1);
486 case LibFunc_getlogin_r
:
487 case LibFunc_getc_unlocked
:
488 Changed
|= setDoesNotThrow(F
);
489 Changed
|= setDoesNotCapture(F
, 0);
492 Changed
|= setDoesNotThrow(F
);
493 Changed
|= setOnlyReadsMemory(F
);
494 Changed
|= setDoesNotCapture(F
, 0);
497 case LibFunc_getchar
:
498 case LibFunc_getchar_unlocked
:
499 Changed
|= setDoesNotThrow(F
);
501 case LibFunc_getitimer
:
502 Changed
|= setDoesNotThrow(F
);
503 Changed
|= setDoesNotCapture(F
, 1);
505 case LibFunc_getpwnam
:
506 Changed
|= setDoesNotThrow(F
);
507 Changed
|= setDoesNotCapture(F
, 0);
508 Changed
|= setOnlyReadsMemory(F
, 0);
511 Changed
|= setDoesNotThrow(F
);
512 Changed
|= setDoesNotCapture(F
, 1);
515 Changed
|= setDoesNotThrow(F
);
516 Changed
|= setDoesNotCapture(F
, 0);
519 Changed
|= setDoesNotThrow(F
);
520 Changed
|= setDoesNotCapture(F
, 0);
521 Changed
|= setOnlyReadsMemory(F
, 0);
523 case LibFunc_unsetenv
:
524 Changed
|= setDoesNotThrow(F
);
525 Changed
|= setDoesNotCapture(F
, 0);
526 Changed
|= setOnlyReadsMemory(F
, 0);
530 Changed
|= setDoesNotThrow(F
);
531 Changed
|= setDoesNotCapture(F
, 0);
532 Changed
|= setDoesNotCapture(F
, 1);
533 Changed
|= setOnlyReadsMemory(F
, 0);
534 Changed
|= setOnlyReadsMemory(F
, 1);
537 case LibFunc_putc_unlocked
:
538 Changed
|= setDoesNotThrow(F
);
539 Changed
|= setDoesNotCapture(F
, 1);
544 Changed
|= setDoesNotThrow(F
);
545 Changed
|= setDoesNotCapture(F
, 0);
546 Changed
|= setOnlyReadsMemory(F
, 0);
549 // May throw; "pread" is a valid pthread cancellation point.
550 Changed
|= setDoesNotCapture(F
, 1);
553 // May throw; "pwrite" is a valid pthread cancellation point.
554 Changed
|= setDoesNotCapture(F
, 1);
555 Changed
|= setOnlyReadsMemory(F
, 1);
557 case LibFunc_putchar
:
558 case LibFunc_putchar_unlocked
:
559 Changed
|= setDoesNotThrow(F
);
562 Changed
|= setDoesNotThrow(F
);
563 Changed
|= setRetDoesNotAlias(F
);
564 Changed
|= setDoesNotCapture(F
, 0);
565 Changed
|= setDoesNotCapture(F
, 1);
566 Changed
|= setOnlyReadsMemory(F
, 0);
567 Changed
|= setOnlyReadsMemory(F
, 1);
570 Changed
|= setDoesNotThrow(F
);
571 Changed
|= setDoesNotCapture(F
, 0);
574 Changed
|= setDoesNotThrow(F
);
575 Changed
|= setDoesNotCapture(F
, 0);
576 Changed
|= setOnlyReadsMemory(F
, 0);
578 case LibFunc_vsscanf
:
579 Changed
|= setDoesNotThrow(F
);
580 Changed
|= setDoesNotCapture(F
, 0);
581 Changed
|= setDoesNotCapture(F
, 1);
582 Changed
|= setOnlyReadsMemory(F
, 0);
583 Changed
|= setOnlyReadsMemory(F
, 1);
585 case LibFunc_vfscanf
:
586 Changed
|= setDoesNotThrow(F
);
587 Changed
|= setDoesNotCapture(F
, 0);
588 Changed
|= setDoesNotCapture(F
, 1);
589 Changed
|= setOnlyReadsMemory(F
, 1);
592 Changed
|= setDoesNotThrow(F
);
593 Changed
|= setRetDoesNotAlias(F
);
595 case LibFunc_vprintf
:
596 Changed
|= setDoesNotThrow(F
);
597 Changed
|= setDoesNotCapture(F
, 0);
598 Changed
|= setOnlyReadsMemory(F
, 0);
600 case LibFunc_vfprintf
:
601 case LibFunc_vsprintf
:
602 Changed
|= setDoesNotThrow(F
);
603 Changed
|= setDoesNotCapture(F
, 0);
604 Changed
|= setDoesNotCapture(F
, 1);
605 Changed
|= setOnlyReadsMemory(F
, 1);
607 case LibFunc_vsnprintf
:
608 Changed
|= setDoesNotThrow(F
);
609 Changed
|= setDoesNotCapture(F
, 0);
610 Changed
|= setDoesNotCapture(F
, 2);
611 Changed
|= setOnlyReadsMemory(F
, 2);
614 // May throw; "open" is a valid pthread cancellation point.
615 Changed
|= setDoesNotCapture(F
, 0);
616 Changed
|= setOnlyReadsMemory(F
, 0);
618 case LibFunc_opendir
:
619 Changed
|= setDoesNotThrow(F
);
620 Changed
|= setRetDoesNotAlias(F
);
621 Changed
|= setDoesNotCapture(F
, 0);
622 Changed
|= setOnlyReadsMemory(F
, 0);
624 case LibFunc_tmpfile
:
625 Changed
|= setDoesNotThrow(F
);
626 Changed
|= setRetDoesNotAlias(F
);
629 Changed
|= setDoesNotThrow(F
);
630 Changed
|= setDoesNotCapture(F
, 0);
636 Changed
|= setDoesNotThrow(F
);
637 Changed
|= setDoesNotAccessMemory(F
);
640 Changed
|= setDoesNotThrow(F
);
641 Changed
|= setDoesNotCapture(F
, 0);
642 Changed
|= setDoesNotCapture(F
, 1);
643 Changed
|= setOnlyReadsMemory(F
, 0);
646 Changed
|= setDoesNotThrow(F
);
647 Changed
|= setDoesNotCapture(F
, 0);
648 Changed
|= setOnlyReadsMemory(F
, 0);
651 // May throw; places call through function pointer.
652 Changed
|= setDoesNotCapture(F
, 3);
654 case LibFunc_dunder_strdup
:
655 case LibFunc_dunder_strndup
:
656 Changed
|= setDoesNotThrow(F
);
657 Changed
|= setRetDoesNotAlias(F
);
658 Changed
|= setDoesNotCapture(F
, 0);
659 Changed
|= setOnlyReadsMemory(F
, 0);
661 case LibFunc_dunder_strtok_r
:
662 Changed
|= setDoesNotThrow(F
);
663 Changed
|= setDoesNotCapture(F
, 1);
664 Changed
|= setOnlyReadsMemory(F
, 1);
666 case LibFunc_under_IO_getc
:
667 Changed
|= setDoesNotThrow(F
);
668 Changed
|= setDoesNotCapture(F
, 0);
670 case LibFunc_under_IO_putc
:
671 Changed
|= setDoesNotThrow(F
);
672 Changed
|= setDoesNotCapture(F
, 1);
674 case LibFunc_dunder_isoc99_scanf
:
675 Changed
|= setDoesNotThrow(F
);
676 Changed
|= setDoesNotCapture(F
, 0);
677 Changed
|= setOnlyReadsMemory(F
, 0);
680 case LibFunc_lstat64
:
681 case LibFunc_statvfs64
:
682 Changed
|= setDoesNotThrow(F
);
683 Changed
|= setDoesNotCapture(F
, 0);
684 Changed
|= setDoesNotCapture(F
, 1);
685 Changed
|= setOnlyReadsMemory(F
, 0);
687 case LibFunc_dunder_isoc99_sscanf
:
688 Changed
|= setDoesNotThrow(F
);
689 Changed
|= setDoesNotCapture(F
, 0);
690 Changed
|= setDoesNotCapture(F
, 1);
691 Changed
|= setOnlyReadsMemory(F
, 0);
692 Changed
|= setOnlyReadsMemory(F
, 1);
694 case LibFunc_fopen64
:
695 Changed
|= setDoesNotThrow(F
);
696 Changed
|= setRetDoesNotAlias(F
);
697 Changed
|= setDoesNotCapture(F
, 0);
698 Changed
|= setDoesNotCapture(F
, 1);
699 Changed
|= setOnlyReadsMemory(F
, 0);
700 Changed
|= setOnlyReadsMemory(F
, 1);
702 case LibFunc_fseeko64
:
703 case LibFunc_ftello64
:
704 Changed
|= setDoesNotThrow(F
);
705 Changed
|= setDoesNotCapture(F
, 0);
707 case LibFunc_tmpfile64
:
708 Changed
|= setDoesNotThrow(F
);
709 Changed
|= setRetDoesNotAlias(F
);
711 case LibFunc_fstat64
:
712 case LibFunc_fstatvfs64
:
713 Changed
|= setDoesNotThrow(F
);
714 Changed
|= setDoesNotCapture(F
, 1);
717 // May throw; "open" is a valid pthread cancellation point.
718 Changed
|= setDoesNotCapture(F
, 0);
719 Changed
|= setOnlyReadsMemory(F
, 0);
721 case LibFunc_gettimeofday
:
722 // Currently some platforms have the restrict keyword on the arguments to
723 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
725 Changed
|= setDoesNotThrow(F
);
726 Changed
|= setDoesNotCapture(F
, 0);
727 Changed
|= setDoesNotCapture(F
, 1);
729 case LibFunc_Znwj
: // new(unsigned int)
730 case LibFunc_Znwm
: // new(unsigned long)
731 case LibFunc_Znaj
: // new[](unsigned int)
732 case LibFunc_Znam
: // new[](unsigned long)
733 case LibFunc_msvc_new_int
: // new(unsigned int)
734 case LibFunc_msvc_new_longlong
: // new(unsigned long long)
735 case LibFunc_msvc_new_array_int
: // new[](unsigned int)
736 case LibFunc_msvc_new_array_longlong
: // new[](unsigned long long)
737 // Operator new always returns a nonnull noalias pointer
738 Changed
|= setRetNonNull(F
);
739 Changed
|= setRetDoesNotAlias(F
);
741 // TODO: add LibFunc entries for:
742 // case LibFunc_memset_pattern4:
743 // case LibFunc_memset_pattern8:
744 case LibFunc_memset_pattern16
:
745 Changed
|= setOnlyAccessesArgMemory(F
);
746 Changed
|= setDoesNotCapture(F
, 0);
747 Changed
|= setDoesNotCapture(F
, 1);
748 Changed
|= setOnlyReadsMemory(F
, 1);
750 // int __nvvm_reflect(const char *)
751 case LibFunc_nvvm_reflect
:
752 Changed
|= setDoesNotAccessMemory(F
);
753 Changed
|= setDoesNotThrow(F
);
757 // FIXME: It'd be really nice to cover all the library functions we're
763 bool llvm::hasUnaryFloatFn(const TargetLibraryInfo
*TLI
, Type
*Ty
,
764 LibFunc DoubleFn
, LibFunc FloatFn
,
765 LibFunc LongDoubleFn
) {
766 switch (Ty
->getTypeID()) {
769 case Type::FloatTyID
:
770 return TLI
->has(FloatFn
);
771 case Type::DoubleTyID
:
772 return TLI
->has(DoubleFn
);
774 return TLI
->has(LongDoubleFn
);
778 StringRef
llvm::getUnaryFloatFn(const TargetLibraryInfo
*TLI
, Type
*Ty
,
779 LibFunc DoubleFn
, LibFunc FloatFn
,
780 LibFunc LongDoubleFn
) {
781 assert(hasUnaryFloatFn(TLI
, Ty
, DoubleFn
, FloatFn
, LongDoubleFn
) &&
782 "Cannot get name for unavailable function!");
784 switch (Ty
->getTypeID()) {
786 llvm_unreachable("No name for HalfTy!");
787 case Type::FloatTyID
:
788 return TLI
->getName(FloatFn
);
789 case Type::DoubleTyID
:
790 return TLI
->getName(DoubleFn
);
792 return TLI
->getName(LongDoubleFn
);
796 //- Emit LibCalls ------------------------------------------------------------//
798 Value
*llvm::castToCStr(Value
*V
, IRBuilder
<> &B
) {
799 unsigned AS
= V
->getType()->getPointerAddressSpace();
800 return B
.CreateBitCast(V
, B
.getInt8PtrTy(AS
), "cstr");
803 static Value
*emitLibCall(LibFunc TheLibFunc
, Type
*ReturnType
,
804 ArrayRef
<Type
*> ParamTypes
,
805 ArrayRef
<Value
*> Operands
, IRBuilder
<> &B
,
806 const TargetLibraryInfo
*TLI
,
807 bool IsVaArgs
= false) {
808 if (!TLI
->has(TheLibFunc
))
811 Module
*M
= B
.GetInsertBlock()->getModule();
812 StringRef FuncName
= TLI
->getName(TheLibFunc
);
813 FunctionType
*FuncType
= FunctionType::get(ReturnType
, ParamTypes
, IsVaArgs
);
814 FunctionCallee Callee
= M
->getOrInsertFunction(FuncName
, FuncType
);
815 inferLibFuncAttributes(M
, FuncName
, *TLI
);
816 CallInst
*CI
= B
.CreateCall(Callee
, Operands
, FuncName
);
817 if (const Function
*F
=
818 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
819 CI
->setCallingConv(F
->getCallingConv());
823 Value
*llvm::emitStrLen(Value
*Ptr
, IRBuilder
<> &B
, const DataLayout
&DL
,
824 const TargetLibraryInfo
*TLI
) {
825 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
826 return emitLibCall(LibFunc_strlen
, DL
.getIntPtrType(Context
),
827 B
.getInt8PtrTy(), castToCStr(Ptr
, B
), B
, TLI
);
830 Value
*llvm::emitStrChr(Value
*Ptr
, char C
, IRBuilder
<> &B
,
831 const TargetLibraryInfo
*TLI
) {
832 Type
*I8Ptr
= B
.getInt8PtrTy();
833 Type
*I32Ty
= B
.getInt32Ty();
834 return emitLibCall(LibFunc_strchr
, I8Ptr
, {I8Ptr
, I32Ty
},
835 {castToCStr(Ptr
, B
), ConstantInt::get(I32Ty
, C
)}, B
, TLI
);
838 Value
*llvm::emitStrNCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
839 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
840 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
842 LibFunc_strncmp
, B
.getInt32Ty(),
843 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
844 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
847 Value
*llvm::emitStrCpy(Value
*Dst
, Value
*Src
, IRBuilder
<> &B
,
848 const TargetLibraryInfo
*TLI
) {
849 Type
*I8Ptr
= B
.getInt8PtrTy();
850 return emitLibCall(LibFunc_strcpy
, I8Ptr
, {I8Ptr
, I8Ptr
},
851 {castToCStr(Dst
, B
), castToCStr(Src
, B
)}, B
, TLI
);
854 Value
*llvm::emitStpCpy(Value
*Dst
, Value
*Src
, IRBuilder
<> &B
,
855 const TargetLibraryInfo
*TLI
) {
856 Type
*I8Ptr
= B
.getInt8PtrTy();
857 return emitLibCall(LibFunc_stpcpy
, I8Ptr
, {I8Ptr
, I8Ptr
},
858 {castToCStr(Dst
, B
), castToCStr(Src
, B
)}, B
, TLI
);
861 Value
*llvm::emitStrNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilder
<> &B
,
862 const TargetLibraryInfo
*TLI
) {
863 Type
*I8Ptr
= B
.getInt8PtrTy();
864 return emitLibCall(LibFunc_strncpy
, I8Ptr
, {I8Ptr
, I8Ptr
, Len
->getType()},
865 {castToCStr(Dst
, B
), castToCStr(Src
, B
), Len
}, B
, TLI
);
868 Value
*llvm::emitStpNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilder
<> &B
,
869 const TargetLibraryInfo
*TLI
) {
870 Type
*I8Ptr
= B
.getInt8PtrTy();
871 return emitLibCall(LibFunc_stpncpy
, I8Ptr
, {I8Ptr
, I8Ptr
, Len
->getType()},
872 {castToCStr(Dst
, B
), castToCStr(Src
, B
), Len
}, B
, TLI
);
875 Value
*llvm::emitMemCpyChk(Value
*Dst
, Value
*Src
, Value
*Len
, Value
*ObjSize
,
876 IRBuilder
<> &B
, const DataLayout
&DL
,
877 const TargetLibraryInfo
*TLI
) {
878 if (!TLI
->has(LibFunc_memcpy_chk
))
881 Module
*M
= B
.GetInsertBlock()->getModule();
883 AS
= AttributeList::get(M
->getContext(), AttributeList::FunctionIndex
,
884 Attribute::NoUnwind
);
885 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
886 FunctionCallee MemCpy
= M
->getOrInsertFunction(
887 "__memcpy_chk", AttributeList::get(M
->getContext(), AS
), B
.getInt8PtrTy(),
888 B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
),
889 DL
.getIntPtrType(Context
));
890 Dst
= castToCStr(Dst
, B
);
891 Src
= castToCStr(Src
, B
);
892 CallInst
*CI
= B
.CreateCall(MemCpy
, {Dst
, Src
, Len
, ObjSize
});
893 if (const Function
*F
=
894 dyn_cast
<Function
>(MemCpy
.getCallee()->stripPointerCasts()))
895 CI
->setCallingConv(F
->getCallingConv());
899 Value
*llvm::emitMemChr(Value
*Ptr
, Value
*Val
, Value
*Len
, IRBuilder
<> &B
,
900 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
901 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
903 LibFunc_memchr
, B
.getInt8PtrTy(),
904 {B
.getInt8PtrTy(), B
.getInt32Ty(), DL
.getIntPtrType(Context
)},
905 {castToCStr(Ptr
, B
), Val
, Len
}, B
, TLI
);
908 Value
*llvm::emitMemCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
909 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
910 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
912 LibFunc_memcmp
, B
.getInt32Ty(),
913 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
914 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
917 Value
*llvm::emitBCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
918 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
919 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
921 LibFunc_bcmp
, B
.getInt32Ty(),
922 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
923 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
926 Value
*llvm::emitMemCCpy(Value
*Ptr1
, Value
*Ptr2
, Value
*Val
, Value
*Len
,
927 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
) {
929 LibFunc_memccpy
, B
.getInt8PtrTy(),
930 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), B
.getInt32Ty(), Len
->getType()},
931 {Ptr1
, Ptr2
, Val
, Len
}, B
, TLI
);
934 Value
*llvm::emitSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
,
935 ArrayRef
<Value
*> VariadicArgs
, IRBuilder
<> &B
,
936 const TargetLibraryInfo
*TLI
) {
937 SmallVector
<Value
*, 8> Args
{castToCStr(Dest
, B
), Size
, castToCStr(Fmt
, B
)};
938 Args
.insert(Args
.end(), VariadicArgs
.begin(), VariadicArgs
.end());
939 return emitLibCall(LibFunc_snprintf
, B
.getInt32Ty(),
940 {B
.getInt8PtrTy(), Size
->getType(), B
.getInt8PtrTy()},
941 Args
, B
, TLI
, /*IsVaArgs=*/true);
944 Value
*llvm::emitSPrintf(Value
*Dest
, Value
*Fmt
,
945 ArrayRef
<Value
*> VariadicArgs
, IRBuilder
<> &B
,
946 const TargetLibraryInfo
*TLI
) {
947 SmallVector
<Value
*, 8> Args
{castToCStr(Dest
, B
), castToCStr(Fmt
, B
)};
948 Args
.insert(Args
.end(), VariadicArgs
.begin(), VariadicArgs
.end());
949 return emitLibCall(LibFunc_sprintf
, B
.getInt32Ty(),
950 {B
.getInt8PtrTy(), B
.getInt8PtrTy()}, Args
, B
, TLI
,
954 Value
*llvm::emitStrCat(Value
*Dest
, Value
*Src
, IRBuilder
<> &B
,
955 const TargetLibraryInfo
*TLI
) {
956 return emitLibCall(LibFunc_strcat
, B
.getInt8PtrTy(),
957 {B
.getInt8PtrTy(), B
.getInt8PtrTy()},
958 {castToCStr(Dest
, B
), castToCStr(Src
, B
)}, B
, TLI
);
961 Value
*llvm::emitStrLCpy(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilder
<> &B
,
962 const TargetLibraryInfo
*TLI
) {
963 return emitLibCall(LibFunc_strlcpy
, Size
->getType(),
964 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
965 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
968 Value
*llvm::emitStrLCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilder
<> &B
,
969 const TargetLibraryInfo
*TLI
) {
970 return emitLibCall(LibFunc_strlcat
, Size
->getType(),
971 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
972 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
975 Value
*llvm::emitStrNCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilder
<> &B
,
976 const TargetLibraryInfo
*TLI
) {
977 return emitLibCall(LibFunc_strncat
, B
.getInt8PtrTy(),
978 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
979 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
982 Value
*llvm::emitVSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
, Value
*VAList
,
983 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
) {
985 LibFunc_vsnprintf
, B
.getInt32Ty(),
986 {B
.getInt8PtrTy(), Size
->getType(), B
.getInt8PtrTy(), VAList
->getType()},
987 {castToCStr(Dest
, B
), Size
, castToCStr(Fmt
, B
), VAList
}, B
, TLI
);
990 Value
*llvm::emitVSPrintf(Value
*Dest
, Value
*Fmt
, Value
*VAList
,
991 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
) {
992 return emitLibCall(LibFunc_vsprintf
, B
.getInt32Ty(),
993 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), VAList
->getType()},
994 {castToCStr(Dest
, B
), castToCStr(Fmt
, B
), VAList
}, B
, TLI
);
997 /// Append a suffix to the function name according to the type of 'Op'.
998 static void appendTypeSuffix(Value
*Op
, StringRef
&Name
,
999 SmallString
<20> &NameBuffer
) {
1000 if (!Op
->getType()->isDoubleTy()) {
1003 if (Op
->getType()->isFloatTy())
1012 static Value
*emitUnaryFloatFnCallHelper(Value
*Op
, StringRef Name
,
1014 const AttributeList
&Attrs
) {
1015 assert((Name
!= "") && "Must specify Name to emitUnaryFloatFnCall");
1017 Module
*M
= B
.GetInsertBlock()->getModule();
1018 FunctionCallee Callee
=
1019 M
->getOrInsertFunction(Name
, Op
->getType(), Op
->getType());
1020 CallInst
*CI
= B
.CreateCall(Callee
, Op
, Name
);
1022 // The incoming attribute set may have come from a speculatable intrinsic, but
1023 // is being replaced with a library call which is not allowed to be
1025 CI
->setAttributes(Attrs
.removeAttribute(B
.getContext(),
1026 AttributeList::FunctionIndex
,
1027 Attribute::Speculatable
));
1028 if (const Function
*F
=
1029 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1030 CI
->setCallingConv(F
->getCallingConv());
1035 Value
*llvm::emitUnaryFloatFnCall(Value
*Op
, StringRef Name
, IRBuilder
<> &B
,
1036 const AttributeList
&Attrs
) {
1037 SmallString
<20> NameBuffer
;
1038 appendTypeSuffix(Op
, Name
, NameBuffer
);
1040 return emitUnaryFloatFnCallHelper(Op
, Name
, B
, Attrs
);
1043 Value
*llvm::emitUnaryFloatFnCall(Value
*Op
, const TargetLibraryInfo
*TLI
,
1044 LibFunc DoubleFn
, LibFunc FloatFn
,
1045 LibFunc LongDoubleFn
, IRBuilder
<> &B
,
1046 const AttributeList
&Attrs
) {
1047 // Get the name of the function according to TLI.
1048 StringRef Name
= getUnaryFloatFn(TLI
, Op
->getType(),
1049 DoubleFn
, FloatFn
, LongDoubleFn
);
1051 return emitUnaryFloatFnCallHelper(Op
, Name
, B
, Attrs
);
1054 Value
*llvm::emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
, StringRef Name
,
1055 IRBuilder
<> &B
, const AttributeList
&Attrs
) {
1056 assert((Name
!= "") && "Must specify Name to emitBinaryFloatFnCall");
1058 SmallString
<20> NameBuffer
;
1059 appendTypeSuffix(Op1
, Name
, NameBuffer
);
1061 Module
*M
= B
.GetInsertBlock()->getModule();
1062 FunctionCallee Callee
= M
->getOrInsertFunction(
1063 Name
, Op1
->getType(), Op1
->getType(), Op2
->getType());
1064 CallInst
*CI
= B
.CreateCall(Callee
, {Op1
, Op2
}, Name
);
1065 CI
->setAttributes(Attrs
);
1066 if (const Function
*F
=
1067 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1068 CI
->setCallingConv(F
->getCallingConv());
1073 Value
*llvm::emitPutChar(Value
*Char
, IRBuilder
<> &B
,
1074 const TargetLibraryInfo
*TLI
) {
1075 if (!TLI
->has(LibFunc_putchar
))
1078 Module
*M
= B
.GetInsertBlock()->getModule();
1079 StringRef PutCharName
= TLI
->getName(LibFunc_putchar
);
1080 FunctionCallee PutChar
=
1081 M
->getOrInsertFunction(PutCharName
, B
.getInt32Ty(), B
.getInt32Ty());
1082 inferLibFuncAttributes(M
, PutCharName
, *TLI
);
1083 CallInst
*CI
= B
.CreateCall(PutChar
,
1084 B
.CreateIntCast(Char
,
1090 if (const Function
*F
=
1091 dyn_cast
<Function
>(PutChar
.getCallee()->stripPointerCasts()))
1092 CI
->setCallingConv(F
->getCallingConv());
1096 Value
*llvm::emitPutS(Value
*Str
, IRBuilder
<> &B
,
1097 const TargetLibraryInfo
*TLI
) {
1098 if (!TLI
->has(LibFunc_puts
))
1101 Module
*M
= B
.GetInsertBlock()->getModule();
1102 StringRef PutsName
= TLI
->getName(LibFunc_puts
);
1103 FunctionCallee PutS
=
1104 M
->getOrInsertFunction(PutsName
, B
.getInt32Ty(), B
.getInt8PtrTy());
1105 inferLibFuncAttributes(M
, PutsName
, *TLI
);
1106 CallInst
*CI
= B
.CreateCall(PutS
, castToCStr(Str
, B
), PutsName
);
1107 if (const Function
*F
=
1108 dyn_cast
<Function
>(PutS
.getCallee()->stripPointerCasts()))
1109 CI
->setCallingConv(F
->getCallingConv());
1113 Value
*llvm::emitFPutC(Value
*Char
, Value
*File
, IRBuilder
<> &B
,
1114 const TargetLibraryInfo
*TLI
) {
1115 if (!TLI
->has(LibFunc_fputc
))
1118 Module
*M
= B
.GetInsertBlock()->getModule();
1119 StringRef FPutcName
= TLI
->getName(LibFunc_fputc
);
1120 FunctionCallee F
= M
->getOrInsertFunction(FPutcName
, B
.getInt32Ty(),
1121 B
.getInt32Ty(), File
->getType());
1122 if (File
->getType()->isPointerTy())
1123 inferLibFuncAttributes(M
, FPutcName
, *TLI
);
1124 Char
= B
.CreateIntCast(Char
, B
.getInt32Ty(), /*isSigned*/true,
1126 CallInst
*CI
= B
.CreateCall(F
, {Char
, File
}, FPutcName
);
1128 if (const Function
*Fn
=
1129 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1130 CI
->setCallingConv(Fn
->getCallingConv());
1134 Value
*llvm::emitFPutCUnlocked(Value
*Char
, Value
*File
, IRBuilder
<> &B
,
1135 const TargetLibraryInfo
*TLI
) {
1136 if (!TLI
->has(LibFunc_fputc_unlocked
))
1139 Module
*M
= B
.GetInsertBlock()->getModule();
1140 StringRef FPutcUnlockedName
= TLI
->getName(LibFunc_fputc_unlocked
);
1141 FunctionCallee F
= M
->getOrInsertFunction(FPutcUnlockedName
, B
.getInt32Ty(),
1142 B
.getInt32Ty(), File
->getType());
1143 if (File
->getType()->isPointerTy())
1144 inferLibFuncAttributes(M
, FPutcUnlockedName
, *TLI
);
1145 Char
= B
.CreateIntCast(Char
, B
.getInt32Ty(), /*isSigned*/ true, "chari");
1146 CallInst
*CI
= B
.CreateCall(F
, {Char
, File
}, FPutcUnlockedName
);
1148 if (const Function
*Fn
=
1149 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1150 CI
->setCallingConv(Fn
->getCallingConv());
1154 Value
*llvm::emitFPutS(Value
*Str
, Value
*File
, IRBuilder
<> &B
,
1155 const TargetLibraryInfo
*TLI
) {
1156 if (!TLI
->has(LibFunc_fputs
))
1159 Module
*M
= B
.GetInsertBlock()->getModule();
1160 StringRef FPutsName
= TLI
->getName(LibFunc_fputs
);
1161 FunctionCallee F
= M
->getOrInsertFunction(FPutsName
, B
.getInt32Ty(),
1162 B
.getInt8PtrTy(), File
->getType());
1163 if (File
->getType()->isPointerTy())
1164 inferLibFuncAttributes(M
, FPutsName
, *TLI
);
1165 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Str
, B
), File
}, FPutsName
);
1167 if (const Function
*Fn
=
1168 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1169 CI
->setCallingConv(Fn
->getCallingConv());
1173 Value
*llvm::emitFPutSUnlocked(Value
*Str
, Value
*File
, IRBuilder
<> &B
,
1174 const TargetLibraryInfo
*TLI
) {
1175 if (!TLI
->has(LibFunc_fputs_unlocked
))
1178 Module
*M
= B
.GetInsertBlock()->getModule();
1179 StringRef FPutsUnlockedName
= TLI
->getName(LibFunc_fputs_unlocked
);
1180 FunctionCallee F
= M
->getOrInsertFunction(FPutsUnlockedName
, B
.getInt32Ty(),
1181 B
.getInt8PtrTy(), File
->getType());
1182 if (File
->getType()->isPointerTy())
1183 inferLibFuncAttributes(M
, FPutsUnlockedName
, *TLI
);
1184 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Str
, B
), File
}, FPutsUnlockedName
);
1186 if (const Function
*Fn
=
1187 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1188 CI
->setCallingConv(Fn
->getCallingConv());
1192 Value
*llvm::emitFWrite(Value
*Ptr
, Value
*Size
, Value
*File
, IRBuilder
<> &B
,
1193 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1194 if (!TLI
->has(LibFunc_fwrite
))
1197 Module
*M
= B
.GetInsertBlock()->getModule();
1198 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1199 StringRef FWriteName
= TLI
->getName(LibFunc_fwrite
);
1200 FunctionCallee F
= M
->getOrInsertFunction(
1201 FWriteName
, DL
.getIntPtrType(Context
), B
.getInt8PtrTy(),
1202 DL
.getIntPtrType(Context
), DL
.getIntPtrType(Context
), File
->getType());
1204 if (File
->getType()->isPointerTy())
1205 inferLibFuncAttributes(M
, FWriteName
, *TLI
);
1207 B
.CreateCall(F
, {castToCStr(Ptr
, B
), Size
,
1208 ConstantInt::get(DL
.getIntPtrType(Context
), 1), File
});
1210 if (const Function
*Fn
=
1211 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1212 CI
->setCallingConv(Fn
->getCallingConv());
1216 Value
*llvm::emitMalloc(Value
*Num
, IRBuilder
<> &B
, const DataLayout
&DL
,
1217 const TargetLibraryInfo
*TLI
) {
1218 if (!TLI
->has(LibFunc_malloc
))
1221 Module
*M
= B
.GetInsertBlock()->getModule();
1222 StringRef MallocName
= TLI
->getName(LibFunc_malloc
);
1223 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1224 FunctionCallee Malloc
= M
->getOrInsertFunction(MallocName
, B
.getInt8PtrTy(),
1225 DL
.getIntPtrType(Context
));
1226 inferLibFuncAttributes(M
, MallocName
, *TLI
);
1227 CallInst
*CI
= B
.CreateCall(Malloc
, Num
, MallocName
);
1229 if (const Function
*F
=
1230 dyn_cast
<Function
>(Malloc
.getCallee()->stripPointerCasts()))
1231 CI
->setCallingConv(F
->getCallingConv());
1236 Value
*llvm::emitCalloc(Value
*Num
, Value
*Size
, const AttributeList
&Attrs
,
1237 IRBuilder
<> &B
, const TargetLibraryInfo
&TLI
) {
1238 if (!TLI
.has(LibFunc_calloc
))
1241 Module
*M
= B
.GetInsertBlock()->getModule();
1242 StringRef CallocName
= TLI
.getName(LibFunc_calloc
);
1243 const DataLayout
&DL
= M
->getDataLayout();
1244 IntegerType
*PtrType
= DL
.getIntPtrType((B
.GetInsertBlock()->getContext()));
1245 FunctionCallee Calloc
= M
->getOrInsertFunction(
1246 CallocName
, Attrs
, B
.getInt8PtrTy(), PtrType
, PtrType
);
1247 inferLibFuncAttributes(M
, CallocName
, TLI
);
1248 CallInst
*CI
= B
.CreateCall(Calloc
, {Num
, Size
}, CallocName
);
1251 dyn_cast
<Function
>(Calloc
.getCallee()->stripPointerCasts()))
1252 CI
->setCallingConv(F
->getCallingConv());
1257 Value
*llvm::emitFWriteUnlocked(Value
*Ptr
, Value
*Size
, Value
*N
, Value
*File
,
1258 IRBuilder
<> &B
, const DataLayout
&DL
,
1259 const TargetLibraryInfo
*TLI
) {
1260 if (!TLI
->has(LibFunc_fwrite_unlocked
))
1263 Module
*M
= B
.GetInsertBlock()->getModule();
1264 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1265 StringRef FWriteUnlockedName
= TLI
->getName(LibFunc_fwrite_unlocked
);
1266 FunctionCallee F
= M
->getOrInsertFunction(
1267 FWriteUnlockedName
, DL
.getIntPtrType(Context
), B
.getInt8PtrTy(),
1268 DL
.getIntPtrType(Context
), DL
.getIntPtrType(Context
), File
->getType());
1270 if (File
->getType()->isPointerTy())
1271 inferLibFuncAttributes(M
, FWriteUnlockedName
, *TLI
);
1272 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Ptr
, B
), Size
, N
, File
});
1274 if (const Function
*Fn
=
1275 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1276 CI
->setCallingConv(Fn
->getCallingConv());
1280 Value
*llvm::emitFGetCUnlocked(Value
*File
, IRBuilder
<> &B
,
1281 const TargetLibraryInfo
*TLI
) {
1282 if (!TLI
->has(LibFunc_fgetc_unlocked
))
1285 Module
*M
= B
.GetInsertBlock()->getModule();
1286 StringRef FGetCUnlockedName
= TLI
->getName(LibFunc_fgetc_unlocked
);
1287 FunctionCallee F
= M
->getOrInsertFunction(FGetCUnlockedName
, B
.getInt32Ty(),
1289 if (File
->getType()->isPointerTy())
1290 inferLibFuncAttributes(M
, FGetCUnlockedName
, *TLI
);
1291 CallInst
*CI
= B
.CreateCall(F
, File
, FGetCUnlockedName
);
1293 if (const Function
*Fn
=
1294 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1295 CI
->setCallingConv(Fn
->getCallingConv());
1299 Value
*llvm::emitFGetSUnlocked(Value
*Str
, Value
*Size
, Value
*File
,
1300 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
) {
1301 if (!TLI
->has(LibFunc_fgets_unlocked
))
1304 Module
*M
= B
.GetInsertBlock()->getModule();
1305 StringRef FGetSUnlockedName
= TLI
->getName(LibFunc_fgets_unlocked
);
1307 M
->getOrInsertFunction(FGetSUnlockedName
, B
.getInt8PtrTy(),
1308 B
.getInt8PtrTy(), B
.getInt32Ty(), File
->getType());
1309 inferLibFuncAttributes(M
, FGetSUnlockedName
, *TLI
);
1311 B
.CreateCall(F
, {castToCStr(Str
, B
), Size
, File
}, FGetSUnlockedName
);
1313 if (const Function
*Fn
=
1314 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1315 CI
->setCallingConv(Fn
->getCallingConv());
1319 Value
*llvm::emitFReadUnlocked(Value
*Ptr
, Value
*Size
, Value
*N
, Value
*File
,
1320 IRBuilder
<> &B
, const DataLayout
&DL
,
1321 const TargetLibraryInfo
*TLI
) {
1322 if (!TLI
->has(LibFunc_fread_unlocked
))
1325 Module
*M
= B
.GetInsertBlock()->getModule();
1326 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1327 StringRef FReadUnlockedName
= TLI
->getName(LibFunc_fread_unlocked
);
1328 FunctionCallee F
= M
->getOrInsertFunction(
1329 FReadUnlockedName
, DL
.getIntPtrType(Context
), B
.getInt8PtrTy(),
1330 DL
.getIntPtrType(Context
), DL
.getIntPtrType(Context
), File
->getType());
1332 if (File
->getType()->isPointerTy())
1333 inferLibFuncAttributes(M
, FReadUnlockedName
, *TLI
);
1334 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Ptr
, B
), Size
, N
, File
});
1336 if (const Function
*Fn
=
1337 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1338 CI
->setCallingConv(Fn
->getCallingConv());