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);
310 case LibFunc_memmove
:
311 Changed
|= setReturnedArg(F
, 0);
313 case LibFunc_mempcpy
:
314 case LibFunc_memccpy
:
315 Changed
|= setDoesNotThrow(F
);
316 Changed
|= setDoesNotCapture(F
, 1);
317 Changed
|= setOnlyReadsMemory(F
, 1);
319 case LibFunc_memcpy_chk
:
320 Changed
|= setDoesNotThrow(F
);
322 case LibFunc_memalign
:
323 Changed
|= setRetDoesNotAlias(F
);
326 Changed
|= setDoesNotThrow(F
);
327 Changed
|= setDoesNotCapture(F
, 0);
328 Changed
|= setOnlyReadsMemory(F
, 0);
331 Changed
|= setDoesNotThrow(F
);
332 Changed
|= setDoesNotCapture(F
, 0);
334 case LibFunc_realloc
:
335 Changed
|= setDoesNotThrow(F
);
336 Changed
|= setRetDoesNotAlias(F
);
337 Changed
|= setDoesNotCapture(F
, 0);
340 // May throw; "read" is a valid pthread cancellation point.
341 Changed
|= setDoesNotCapture(F
, 1);
344 Changed
|= setDoesNotThrow(F
);
345 Changed
|= setDoesNotCapture(F
, 0);
349 case LibFunc_realpath
:
350 Changed
|= setDoesNotThrow(F
);
351 Changed
|= setDoesNotCapture(F
, 0);
352 Changed
|= setOnlyReadsMemory(F
, 0);
355 Changed
|= setDoesNotThrow(F
);
356 Changed
|= setDoesNotCapture(F
, 0);
357 Changed
|= setDoesNotCapture(F
, 1);
358 Changed
|= setOnlyReadsMemory(F
, 0);
359 Changed
|= setOnlyReadsMemory(F
, 1);
361 case LibFunc_readlink
:
362 Changed
|= setDoesNotThrow(F
);
363 Changed
|= setDoesNotCapture(F
, 0);
364 Changed
|= setDoesNotCapture(F
, 1);
365 Changed
|= setOnlyReadsMemory(F
, 0);
368 // May throw; "write" is a valid pthread cancellation point.
369 Changed
|= setDoesNotCapture(F
, 1);
370 Changed
|= setOnlyReadsMemory(F
, 1);
373 Changed
|= setDoesNotThrow(F
);
374 Changed
|= setDoesNotCapture(F
, 0);
375 Changed
|= setDoesNotCapture(F
, 1);
376 Changed
|= setOnlyReadsMemory(F
, 0);
379 Changed
|= setDoesNotThrow(F
);
380 Changed
|= setOnlyReadsMemory(F
);
381 Changed
|= setDoesNotCapture(F
, 0);
382 Changed
|= setDoesNotCapture(F
, 1);
385 Changed
|= setDoesNotThrow(F
);
386 Changed
|= setDoesNotCapture(F
, 0);
389 Changed
|= setDoesNotThrow(F
);
390 Changed
|= setRetDoesNotAlias(F
);
394 Changed
|= setDoesNotThrow(F
);
395 Changed
|= setDoesNotCapture(F
, 0);
396 Changed
|= setOnlyReadsMemory(F
, 0);
398 case LibFunc_ctermid
:
399 case LibFunc_clearerr
:
400 case LibFunc_closedir
:
401 Changed
|= setDoesNotThrow(F
);
402 Changed
|= setDoesNotCapture(F
, 0);
408 Changed
|= setDoesNotThrow(F
);
409 Changed
|= setOnlyReadsMemory(F
);
410 Changed
|= setDoesNotCapture(F
, 0);
413 Changed
|= setDoesNotThrow(F
);
414 Changed
|= setDoesNotCapture(F
, 0);
415 Changed
|= setOnlyReadsMemory(F
, 0);
418 Changed
|= setDoesNotThrow(F
);
419 Changed
|= setRetDoesNotAlias(F
);
420 Changed
|= setDoesNotCapture(F
, 0);
421 Changed
|= setDoesNotCapture(F
, 1);
422 Changed
|= setOnlyReadsMemory(F
, 0);
423 Changed
|= setOnlyReadsMemory(F
, 1);
426 Changed
|= setDoesNotThrow(F
);
427 Changed
|= setRetDoesNotAlias(F
);
428 Changed
|= setDoesNotCapture(F
, 1);
429 Changed
|= setOnlyReadsMemory(F
, 1);
436 case LibFunc_fgetc_unlocked
:
442 case LibFunc_fsetpos
:
443 case LibFunc_flockfile
:
444 case LibFunc_funlockfile
:
445 case LibFunc_ftrylockfile
:
446 Changed
|= setDoesNotThrow(F
);
447 Changed
|= setDoesNotCapture(F
, 0);
450 Changed
|= setDoesNotThrow(F
);
451 Changed
|= setDoesNotCapture(F
, 0);
452 Changed
|= setOnlyReadsMemory(F
);
455 case LibFunc_fputc_unlocked
:
460 case LibFunc_fstatvfs
:
461 Changed
|= setDoesNotThrow(F
);
462 Changed
|= setDoesNotCapture(F
, 1);
465 case LibFunc_fgets_unlocked
:
466 Changed
|= setDoesNotThrow(F
);
467 Changed
|= setDoesNotCapture(F
, 2);
470 case LibFunc_fread_unlocked
:
471 Changed
|= setDoesNotThrow(F
);
472 Changed
|= setDoesNotCapture(F
, 0);
473 Changed
|= setDoesNotCapture(F
, 3);
476 case LibFunc_fwrite_unlocked
:
477 Changed
|= setDoesNotThrow(F
);
478 Changed
|= setDoesNotCapture(F
, 0);
479 Changed
|= setDoesNotCapture(F
, 3);
480 // FIXME: readonly #1?
483 case LibFunc_fputs_unlocked
:
484 Changed
|= setDoesNotThrow(F
);
485 Changed
|= setDoesNotCapture(F
, 0);
486 Changed
|= setDoesNotCapture(F
, 1);
487 Changed
|= setOnlyReadsMemory(F
, 0);
490 case LibFunc_fprintf
:
491 Changed
|= setDoesNotThrow(F
);
492 Changed
|= setDoesNotCapture(F
, 0);
493 Changed
|= setDoesNotCapture(F
, 1);
494 Changed
|= setOnlyReadsMemory(F
, 1);
496 case LibFunc_fgetpos
:
497 Changed
|= setDoesNotThrow(F
);
498 Changed
|= setDoesNotCapture(F
, 0);
499 Changed
|= setDoesNotCapture(F
, 1);
502 case LibFunc_getlogin_r
:
503 case LibFunc_getc_unlocked
:
504 Changed
|= setDoesNotThrow(F
);
505 Changed
|= setDoesNotCapture(F
, 0);
508 Changed
|= setDoesNotThrow(F
);
509 Changed
|= setOnlyReadsMemory(F
);
510 Changed
|= setDoesNotCapture(F
, 0);
513 case LibFunc_getchar
:
514 case LibFunc_getchar_unlocked
:
515 Changed
|= setDoesNotThrow(F
);
517 case LibFunc_getitimer
:
518 Changed
|= setDoesNotThrow(F
);
519 Changed
|= setDoesNotCapture(F
, 1);
521 case LibFunc_getpwnam
:
522 Changed
|= setDoesNotThrow(F
);
523 Changed
|= setDoesNotCapture(F
, 0);
524 Changed
|= setOnlyReadsMemory(F
, 0);
527 Changed
|= setDoesNotThrow(F
);
528 Changed
|= setDoesNotCapture(F
, 1);
531 Changed
|= setDoesNotThrow(F
);
532 Changed
|= setDoesNotCapture(F
, 0);
535 Changed
|= setDoesNotThrow(F
);
536 Changed
|= setDoesNotCapture(F
, 0);
537 Changed
|= setOnlyReadsMemory(F
, 0);
539 case LibFunc_unsetenv
:
540 Changed
|= setDoesNotThrow(F
);
541 Changed
|= setDoesNotCapture(F
, 0);
542 Changed
|= setOnlyReadsMemory(F
, 0);
546 Changed
|= setDoesNotThrow(F
);
547 Changed
|= setDoesNotCapture(F
, 0);
548 Changed
|= setDoesNotCapture(F
, 1);
549 Changed
|= setOnlyReadsMemory(F
, 0);
550 Changed
|= setOnlyReadsMemory(F
, 1);
553 case LibFunc_putc_unlocked
:
554 Changed
|= setDoesNotThrow(F
);
555 Changed
|= setDoesNotCapture(F
, 1);
560 Changed
|= setDoesNotThrow(F
);
561 Changed
|= setDoesNotCapture(F
, 0);
562 Changed
|= setOnlyReadsMemory(F
, 0);
565 // May throw; "pread" is a valid pthread cancellation point.
566 Changed
|= setDoesNotCapture(F
, 1);
569 // May throw; "pwrite" is a valid pthread cancellation point.
570 Changed
|= setDoesNotCapture(F
, 1);
571 Changed
|= setOnlyReadsMemory(F
, 1);
573 case LibFunc_putchar
:
574 case LibFunc_putchar_unlocked
:
575 Changed
|= setDoesNotThrow(F
);
578 Changed
|= setDoesNotThrow(F
);
579 Changed
|= setRetDoesNotAlias(F
);
580 Changed
|= setDoesNotCapture(F
, 0);
581 Changed
|= setDoesNotCapture(F
, 1);
582 Changed
|= setOnlyReadsMemory(F
, 0);
583 Changed
|= setOnlyReadsMemory(F
, 1);
586 Changed
|= setDoesNotThrow(F
);
587 Changed
|= setDoesNotCapture(F
, 0);
590 Changed
|= setDoesNotThrow(F
);
591 Changed
|= setDoesNotCapture(F
, 0);
592 Changed
|= setOnlyReadsMemory(F
, 0);
594 case LibFunc_vsscanf
:
595 Changed
|= setDoesNotThrow(F
);
596 Changed
|= setDoesNotCapture(F
, 0);
597 Changed
|= setDoesNotCapture(F
, 1);
598 Changed
|= setOnlyReadsMemory(F
, 0);
599 Changed
|= setOnlyReadsMemory(F
, 1);
601 case LibFunc_vfscanf
:
602 Changed
|= setDoesNotThrow(F
);
603 Changed
|= setDoesNotCapture(F
, 0);
604 Changed
|= setDoesNotCapture(F
, 1);
605 Changed
|= setOnlyReadsMemory(F
, 1);
608 Changed
|= setDoesNotThrow(F
);
609 Changed
|= setRetDoesNotAlias(F
);
611 case LibFunc_vprintf
:
612 Changed
|= setDoesNotThrow(F
);
613 Changed
|= setDoesNotCapture(F
, 0);
614 Changed
|= setOnlyReadsMemory(F
, 0);
616 case LibFunc_vfprintf
:
617 case LibFunc_vsprintf
:
618 Changed
|= setDoesNotThrow(F
);
619 Changed
|= setDoesNotCapture(F
, 0);
620 Changed
|= setDoesNotCapture(F
, 1);
621 Changed
|= setOnlyReadsMemory(F
, 1);
623 case LibFunc_vsnprintf
:
624 Changed
|= setDoesNotThrow(F
);
625 Changed
|= setDoesNotCapture(F
, 0);
626 Changed
|= setDoesNotCapture(F
, 2);
627 Changed
|= setOnlyReadsMemory(F
, 2);
630 // May throw; "open" is a valid pthread cancellation point.
631 Changed
|= setDoesNotCapture(F
, 0);
632 Changed
|= setOnlyReadsMemory(F
, 0);
634 case LibFunc_opendir
:
635 Changed
|= setDoesNotThrow(F
);
636 Changed
|= setRetDoesNotAlias(F
);
637 Changed
|= setDoesNotCapture(F
, 0);
638 Changed
|= setOnlyReadsMemory(F
, 0);
640 case LibFunc_tmpfile
:
641 Changed
|= setDoesNotThrow(F
);
642 Changed
|= setRetDoesNotAlias(F
);
645 Changed
|= setDoesNotThrow(F
);
646 Changed
|= setDoesNotCapture(F
, 0);
652 Changed
|= setDoesNotThrow(F
);
653 Changed
|= setDoesNotAccessMemory(F
);
656 Changed
|= setDoesNotThrow(F
);
657 Changed
|= setDoesNotCapture(F
, 0);
658 Changed
|= setDoesNotCapture(F
, 1);
659 Changed
|= setOnlyReadsMemory(F
, 0);
662 Changed
|= setDoesNotThrow(F
);
663 Changed
|= setDoesNotCapture(F
, 0);
664 Changed
|= setOnlyReadsMemory(F
, 0);
667 // May throw; places call through function pointer.
668 Changed
|= setDoesNotCapture(F
, 3);
670 case LibFunc_dunder_strdup
:
671 case LibFunc_dunder_strndup
:
672 Changed
|= setDoesNotThrow(F
);
673 Changed
|= setRetDoesNotAlias(F
);
674 Changed
|= setDoesNotCapture(F
, 0);
675 Changed
|= setOnlyReadsMemory(F
, 0);
677 case LibFunc_dunder_strtok_r
:
678 Changed
|= setDoesNotThrow(F
);
679 Changed
|= setDoesNotCapture(F
, 1);
680 Changed
|= setOnlyReadsMemory(F
, 1);
682 case LibFunc_under_IO_getc
:
683 Changed
|= setDoesNotThrow(F
);
684 Changed
|= setDoesNotCapture(F
, 0);
686 case LibFunc_under_IO_putc
:
687 Changed
|= setDoesNotThrow(F
);
688 Changed
|= setDoesNotCapture(F
, 1);
690 case LibFunc_dunder_isoc99_scanf
:
691 Changed
|= setDoesNotThrow(F
);
692 Changed
|= setDoesNotCapture(F
, 0);
693 Changed
|= setOnlyReadsMemory(F
, 0);
696 case LibFunc_lstat64
:
697 case LibFunc_statvfs64
:
698 Changed
|= setDoesNotThrow(F
);
699 Changed
|= setDoesNotCapture(F
, 0);
700 Changed
|= setDoesNotCapture(F
, 1);
701 Changed
|= setOnlyReadsMemory(F
, 0);
703 case LibFunc_dunder_isoc99_sscanf
:
704 Changed
|= setDoesNotThrow(F
);
705 Changed
|= setDoesNotCapture(F
, 0);
706 Changed
|= setDoesNotCapture(F
, 1);
707 Changed
|= setOnlyReadsMemory(F
, 0);
708 Changed
|= setOnlyReadsMemory(F
, 1);
710 case LibFunc_fopen64
:
711 Changed
|= setDoesNotThrow(F
);
712 Changed
|= setRetDoesNotAlias(F
);
713 Changed
|= setDoesNotCapture(F
, 0);
714 Changed
|= setDoesNotCapture(F
, 1);
715 Changed
|= setOnlyReadsMemory(F
, 0);
716 Changed
|= setOnlyReadsMemory(F
, 1);
718 case LibFunc_fseeko64
:
719 case LibFunc_ftello64
:
720 Changed
|= setDoesNotThrow(F
);
721 Changed
|= setDoesNotCapture(F
, 0);
723 case LibFunc_tmpfile64
:
724 Changed
|= setDoesNotThrow(F
);
725 Changed
|= setRetDoesNotAlias(F
);
727 case LibFunc_fstat64
:
728 case LibFunc_fstatvfs64
:
729 Changed
|= setDoesNotThrow(F
);
730 Changed
|= setDoesNotCapture(F
, 1);
733 // May throw; "open" is a valid pthread cancellation point.
734 Changed
|= setDoesNotCapture(F
, 0);
735 Changed
|= setOnlyReadsMemory(F
, 0);
737 case LibFunc_gettimeofday
:
738 // Currently some platforms have the restrict keyword on the arguments to
739 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
741 Changed
|= setDoesNotThrow(F
);
742 Changed
|= setDoesNotCapture(F
, 0);
743 Changed
|= setDoesNotCapture(F
, 1);
745 case LibFunc_Znwj
: // new(unsigned int)
746 case LibFunc_Znwm
: // new(unsigned long)
747 case LibFunc_Znaj
: // new[](unsigned int)
748 case LibFunc_Znam
: // new[](unsigned long)
749 case LibFunc_msvc_new_int
: // new(unsigned int)
750 case LibFunc_msvc_new_longlong
: // new(unsigned long long)
751 case LibFunc_msvc_new_array_int
: // new[](unsigned int)
752 case LibFunc_msvc_new_array_longlong
: // new[](unsigned long long)
753 // Operator new always returns a nonnull noalias pointer
754 Changed
|= setRetNonNull(F
);
755 Changed
|= setRetDoesNotAlias(F
);
757 // TODO: add LibFunc entries for:
758 // case LibFunc_memset_pattern4:
759 // case LibFunc_memset_pattern8:
760 case LibFunc_memset_pattern16
:
761 Changed
|= setOnlyAccessesArgMemory(F
);
762 Changed
|= setDoesNotCapture(F
, 0);
763 Changed
|= setDoesNotCapture(F
, 1);
764 Changed
|= setOnlyReadsMemory(F
, 1);
766 // int __nvvm_reflect(const char *)
767 case LibFunc_nvvm_reflect
:
768 Changed
|= setDoesNotAccessMemory(F
);
769 Changed
|= setDoesNotThrow(F
);
773 // FIXME: It'd be really nice to cover all the library functions we're
779 bool llvm::hasFloatFn(const TargetLibraryInfo
*TLI
, Type
*Ty
,
780 LibFunc DoubleFn
, LibFunc FloatFn
, LibFunc LongDoubleFn
) {
781 switch (Ty
->getTypeID()) {
784 case Type::FloatTyID
:
785 return TLI
->has(FloatFn
);
786 case Type::DoubleTyID
:
787 return TLI
->has(DoubleFn
);
789 return TLI
->has(LongDoubleFn
);
793 StringRef
llvm::getFloatFnName(const TargetLibraryInfo
*TLI
, Type
*Ty
,
794 LibFunc DoubleFn
, LibFunc FloatFn
,
795 LibFunc LongDoubleFn
) {
796 assert(hasFloatFn(TLI
, Ty
, DoubleFn
, FloatFn
, LongDoubleFn
) &&
797 "Cannot get name for unavailable function!");
799 switch (Ty
->getTypeID()) {
801 llvm_unreachable("No name for HalfTy!");
802 case Type::FloatTyID
:
803 return TLI
->getName(FloatFn
);
804 case Type::DoubleTyID
:
805 return TLI
->getName(DoubleFn
);
807 return TLI
->getName(LongDoubleFn
);
811 //- Emit LibCalls ------------------------------------------------------------//
813 Value
*llvm::castToCStr(Value
*V
, IRBuilder
<> &B
) {
814 unsigned AS
= V
->getType()->getPointerAddressSpace();
815 return B
.CreateBitCast(V
, B
.getInt8PtrTy(AS
), "cstr");
818 static Value
*emitLibCall(LibFunc TheLibFunc
, Type
*ReturnType
,
819 ArrayRef
<Type
*> ParamTypes
,
820 ArrayRef
<Value
*> Operands
, IRBuilder
<> &B
,
821 const TargetLibraryInfo
*TLI
,
822 bool IsVaArgs
= false) {
823 if (!TLI
->has(TheLibFunc
))
826 Module
*M
= B
.GetInsertBlock()->getModule();
827 StringRef FuncName
= TLI
->getName(TheLibFunc
);
828 FunctionType
*FuncType
= FunctionType::get(ReturnType
, ParamTypes
, IsVaArgs
);
829 FunctionCallee Callee
= M
->getOrInsertFunction(FuncName
, FuncType
);
830 inferLibFuncAttributes(M
, FuncName
, *TLI
);
831 CallInst
*CI
= B
.CreateCall(Callee
, Operands
, FuncName
);
832 if (const Function
*F
=
833 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
834 CI
->setCallingConv(F
->getCallingConv());
838 Value
*llvm::emitStrLen(Value
*Ptr
, IRBuilder
<> &B
, const DataLayout
&DL
,
839 const TargetLibraryInfo
*TLI
) {
840 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
841 return emitLibCall(LibFunc_strlen
, DL
.getIntPtrType(Context
),
842 B
.getInt8PtrTy(), castToCStr(Ptr
, B
), B
, TLI
);
845 Value
*llvm::emitStrDup(Value
*Ptr
, IRBuilder
<> &B
,
846 const TargetLibraryInfo
*TLI
) {
847 return emitLibCall(LibFunc_strdup
, B
.getInt8PtrTy(), B
.getInt8PtrTy(),
848 castToCStr(Ptr
, B
), B
, TLI
);
851 Value
*llvm::emitStrChr(Value
*Ptr
, char C
, IRBuilder
<> &B
,
852 const TargetLibraryInfo
*TLI
) {
853 Type
*I8Ptr
= B
.getInt8PtrTy();
854 Type
*I32Ty
= B
.getInt32Ty();
855 return emitLibCall(LibFunc_strchr
, I8Ptr
, {I8Ptr
, I32Ty
},
856 {castToCStr(Ptr
, B
), ConstantInt::get(I32Ty
, C
)}, B
, TLI
);
859 Value
*llvm::emitStrNCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
860 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
861 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
863 LibFunc_strncmp
, B
.getInt32Ty(),
864 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
865 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
868 Value
*llvm::emitStrCpy(Value
*Dst
, Value
*Src
, IRBuilder
<> &B
,
869 const TargetLibraryInfo
*TLI
) {
870 Type
*I8Ptr
= B
.getInt8PtrTy();
871 return emitLibCall(LibFunc_strcpy
, I8Ptr
, {I8Ptr
, I8Ptr
},
872 {castToCStr(Dst
, B
), castToCStr(Src
, B
)}, B
, TLI
);
875 Value
*llvm::emitStpCpy(Value
*Dst
, Value
*Src
, IRBuilder
<> &B
,
876 const TargetLibraryInfo
*TLI
) {
877 Type
*I8Ptr
= B
.getInt8PtrTy();
878 return emitLibCall(LibFunc_stpcpy
, I8Ptr
, {I8Ptr
, I8Ptr
},
879 {castToCStr(Dst
, B
), castToCStr(Src
, B
)}, B
, TLI
);
882 Value
*llvm::emitStrNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilder
<> &B
,
883 const TargetLibraryInfo
*TLI
) {
884 Type
*I8Ptr
= B
.getInt8PtrTy();
885 return emitLibCall(LibFunc_strncpy
, I8Ptr
, {I8Ptr
, I8Ptr
, Len
->getType()},
886 {castToCStr(Dst
, B
), castToCStr(Src
, B
), Len
}, B
, TLI
);
889 Value
*llvm::emitStpNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilder
<> &B
,
890 const TargetLibraryInfo
*TLI
) {
891 Type
*I8Ptr
= B
.getInt8PtrTy();
892 return emitLibCall(LibFunc_stpncpy
, I8Ptr
, {I8Ptr
, I8Ptr
, Len
->getType()},
893 {castToCStr(Dst
, B
), castToCStr(Src
, B
), Len
}, B
, TLI
);
896 Value
*llvm::emitMemCpyChk(Value
*Dst
, Value
*Src
, Value
*Len
, Value
*ObjSize
,
897 IRBuilder
<> &B
, const DataLayout
&DL
,
898 const TargetLibraryInfo
*TLI
) {
899 if (!TLI
->has(LibFunc_memcpy_chk
))
902 Module
*M
= B
.GetInsertBlock()->getModule();
904 AS
= AttributeList::get(M
->getContext(), AttributeList::FunctionIndex
,
905 Attribute::NoUnwind
);
906 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
907 FunctionCallee MemCpy
= M
->getOrInsertFunction(
908 "__memcpy_chk", AttributeList::get(M
->getContext(), AS
), B
.getInt8PtrTy(),
909 B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
),
910 DL
.getIntPtrType(Context
));
911 Dst
= castToCStr(Dst
, B
);
912 Src
= castToCStr(Src
, B
);
913 CallInst
*CI
= B
.CreateCall(MemCpy
, {Dst
, Src
, Len
, ObjSize
});
914 if (const Function
*F
=
915 dyn_cast
<Function
>(MemCpy
.getCallee()->stripPointerCasts()))
916 CI
->setCallingConv(F
->getCallingConv());
920 Value
*llvm::emitMemChr(Value
*Ptr
, Value
*Val
, Value
*Len
, IRBuilder
<> &B
,
921 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
922 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
924 LibFunc_memchr
, B
.getInt8PtrTy(),
925 {B
.getInt8PtrTy(), B
.getInt32Ty(), DL
.getIntPtrType(Context
)},
926 {castToCStr(Ptr
, B
), Val
, Len
}, B
, TLI
);
929 Value
*llvm::emitMemCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
930 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
931 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
933 LibFunc_memcmp
, B
.getInt32Ty(),
934 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
935 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
938 Value
*llvm::emitBCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
939 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
940 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
942 LibFunc_bcmp
, B
.getInt32Ty(),
943 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
944 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
947 Value
*llvm::emitMemCCpy(Value
*Ptr1
, Value
*Ptr2
, Value
*Val
, Value
*Len
,
948 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
) {
950 LibFunc_memccpy
, B
.getInt8PtrTy(),
951 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), B
.getInt32Ty(), Len
->getType()},
952 {Ptr1
, Ptr2
, Val
, Len
}, B
, TLI
);
955 Value
*llvm::emitSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
,
956 ArrayRef
<Value
*> VariadicArgs
, IRBuilder
<> &B
,
957 const TargetLibraryInfo
*TLI
) {
958 SmallVector
<Value
*, 8> Args
{castToCStr(Dest
, B
), Size
, castToCStr(Fmt
, B
)};
959 Args
.insert(Args
.end(), VariadicArgs
.begin(), VariadicArgs
.end());
960 return emitLibCall(LibFunc_snprintf
, B
.getInt32Ty(),
961 {B
.getInt8PtrTy(), Size
->getType(), B
.getInt8PtrTy()},
962 Args
, B
, TLI
, /*IsVaArgs=*/true);
965 Value
*llvm::emitSPrintf(Value
*Dest
, Value
*Fmt
,
966 ArrayRef
<Value
*> VariadicArgs
, IRBuilder
<> &B
,
967 const TargetLibraryInfo
*TLI
) {
968 SmallVector
<Value
*, 8> Args
{castToCStr(Dest
, B
), castToCStr(Fmt
, B
)};
969 Args
.insert(Args
.end(), VariadicArgs
.begin(), VariadicArgs
.end());
970 return emitLibCall(LibFunc_sprintf
, B
.getInt32Ty(),
971 {B
.getInt8PtrTy(), B
.getInt8PtrTy()}, Args
, B
, TLI
,
975 Value
*llvm::emitStrCat(Value
*Dest
, Value
*Src
, IRBuilder
<> &B
,
976 const TargetLibraryInfo
*TLI
) {
977 return emitLibCall(LibFunc_strcat
, B
.getInt8PtrTy(),
978 {B
.getInt8PtrTy(), B
.getInt8PtrTy()},
979 {castToCStr(Dest
, B
), castToCStr(Src
, B
)}, B
, TLI
);
982 Value
*llvm::emitStrLCpy(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilder
<> &B
,
983 const TargetLibraryInfo
*TLI
) {
984 return emitLibCall(LibFunc_strlcpy
, Size
->getType(),
985 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
986 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
989 Value
*llvm::emitStrLCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilder
<> &B
,
990 const TargetLibraryInfo
*TLI
) {
991 return emitLibCall(LibFunc_strlcat
, Size
->getType(),
992 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
993 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
996 Value
*llvm::emitStrNCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilder
<> &B
,
997 const TargetLibraryInfo
*TLI
) {
998 return emitLibCall(LibFunc_strncat
, B
.getInt8PtrTy(),
999 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
1000 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
1003 Value
*llvm::emitVSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
, Value
*VAList
,
1004 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
) {
1006 LibFunc_vsnprintf
, B
.getInt32Ty(),
1007 {B
.getInt8PtrTy(), Size
->getType(), B
.getInt8PtrTy(), VAList
->getType()},
1008 {castToCStr(Dest
, B
), Size
, castToCStr(Fmt
, B
), VAList
}, B
, TLI
);
1011 Value
*llvm::emitVSPrintf(Value
*Dest
, Value
*Fmt
, Value
*VAList
,
1012 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
) {
1013 return emitLibCall(LibFunc_vsprintf
, B
.getInt32Ty(),
1014 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), VAList
->getType()},
1015 {castToCStr(Dest
, B
), castToCStr(Fmt
, B
), VAList
}, B
, TLI
);
1018 /// Append a suffix to the function name according to the type of 'Op'.
1019 static void appendTypeSuffix(Value
*Op
, StringRef
&Name
,
1020 SmallString
<20> &NameBuffer
) {
1021 if (!Op
->getType()->isDoubleTy()) {
1024 if (Op
->getType()->isFloatTy())
1033 static Value
*emitUnaryFloatFnCallHelper(Value
*Op
, StringRef Name
,
1035 const AttributeList
&Attrs
) {
1036 assert((Name
!= "") && "Must specify Name to emitUnaryFloatFnCall");
1038 Module
*M
= B
.GetInsertBlock()->getModule();
1039 FunctionCallee Callee
=
1040 M
->getOrInsertFunction(Name
, Op
->getType(), Op
->getType());
1041 CallInst
*CI
= B
.CreateCall(Callee
, Op
, Name
);
1043 // The incoming attribute set may have come from a speculatable intrinsic, but
1044 // is being replaced with a library call which is not allowed to be
1046 CI
->setAttributes(Attrs
.removeAttribute(B
.getContext(),
1047 AttributeList::FunctionIndex
,
1048 Attribute::Speculatable
));
1049 if (const Function
*F
=
1050 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1051 CI
->setCallingConv(F
->getCallingConv());
1056 Value
*llvm::emitUnaryFloatFnCall(Value
*Op
, StringRef Name
, IRBuilder
<> &B
,
1057 const AttributeList
&Attrs
) {
1058 SmallString
<20> NameBuffer
;
1059 appendTypeSuffix(Op
, Name
, NameBuffer
);
1061 return emitUnaryFloatFnCallHelper(Op
, Name
, B
, Attrs
);
1064 Value
*llvm::emitUnaryFloatFnCall(Value
*Op
, const TargetLibraryInfo
*TLI
,
1065 LibFunc DoubleFn
, LibFunc FloatFn
,
1066 LibFunc LongDoubleFn
, IRBuilder
<> &B
,
1067 const AttributeList
&Attrs
) {
1068 // Get the name of the function according to TLI.
1069 StringRef Name
= getFloatFnName(TLI
, Op
->getType(),
1070 DoubleFn
, FloatFn
, LongDoubleFn
);
1072 return emitUnaryFloatFnCallHelper(Op
, Name
, B
, Attrs
);
1075 static Value
*emitBinaryFloatFnCallHelper(Value
*Op1
, Value
*Op2
,
1076 StringRef Name
, IRBuilder
<> &B
,
1077 const AttributeList
&Attrs
) {
1078 assert((Name
!= "") && "Must specify Name to emitBinaryFloatFnCall");
1080 Module
*M
= B
.GetInsertBlock()->getModule();
1081 FunctionCallee Callee
= M
->getOrInsertFunction(Name
, Op1
->getType(),
1082 Op1
->getType(), Op2
->getType());
1083 CallInst
*CI
= B
.CreateCall(Callee
, { Op1
, Op2
}, Name
);
1085 // The incoming attribute set may have come from a speculatable intrinsic, but
1086 // is being replaced with a library call which is not allowed to be
1088 CI
->setAttributes(Attrs
.removeAttribute(B
.getContext(),
1089 AttributeList::FunctionIndex
,
1090 Attribute::Speculatable
));
1091 if (const Function
*F
=
1092 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1093 CI
->setCallingConv(F
->getCallingConv());
1098 Value
*llvm::emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
, StringRef Name
,
1099 IRBuilder
<> &B
, const AttributeList
&Attrs
) {
1100 assert((Name
!= "") && "Must specify Name to emitBinaryFloatFnCall");
1102 SmallString
<20> NameBuffer
;
1103 appendTypeSuffix(Op1
, Name
, NameBuffer
);
1105 return emitBinaryFloatFnCallHelper(Op1
, Op2
, Name
, B
, Attrs
);
1108 Value
*llvm::emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
,
1109 const TargetLibraryInfo
*TLI
,
1110 LibFunc DoubleFn
, LibFunc FloatFn
,
1111 LibFunc LongDoubleFn
, IRBuilder
<> &B
,
1112 const AttributeList
&Attrs
) {
1113 // Get the name of the function according to TLI.
1114 StringRef Name
= getFloatFnName(TLI
, Op1
->getType(),
1115 DoubleFn
, FloatFn
, LongDoubleFn
);
1117 return emitBinaryFloatFnCallHelper(Op1
, Op2
, Name
, B
, Attrs
);
1120 Value
*llvm::emitPutChar(Value
*Char
, IRBuilder
<> &B
,
1121 const TargetLibraryInfo
*TLI
) {
1122 if (!TLI
->has(LibFunc_putchar
))
1125 Module
*M
= B
.GetInsertBlock()->getModule();
1126 StringRef PutCharName
= TLI
->getName(LibFunc_putchar
);
1127 FunctionCallee PutChar
=
1128 M
->getOrInsertFunction(PutCharName
, B
.getInt32Ty(), B
.getInt32Ty());
1129 inferLibFuncAttributes(M
, PutCharName
, *TLI
);
1130 CallInst
*CI
= B
.CreateCall(PutChar
,
1131 B
.CreateIntCast(Char
,
1137 if (const Function
*F
=
1138 dyn_cast
<Function
>(PutChar
.getCallee()->stripPointerCasts()))
1139 CI
->setCallingConv(F
->getCallingConv());
1143 Value
*llvm::emitPutS(Value
*Str
, IRBuilder
<> &B
,
1144 const TargetLibraryInfo
*TLI
) {
1145 if (!TLI
->has(LibFunc_puts
))
1148 Module
*M
= B
.GetInsertBlock()->getModule();
1149 StringRef PutsName
= TLI
->getName(LibFunc_puts
);
1150 FunctionCallee PutS
=
1151 M
->getOrInsertFunction(PutsName
, B
.getInt32Ty(), B
.getInt8PtrTy());
1152 inferLibFuncAttributes(M
, PutsName
, *TLI
);
1153 CallInst
*CI
= B
.CreateCall(PutS
, castToCStr(Str
, B
), PutsName
);
1154 if (const Function
*F
=
1155 dyn_cast
<Function
>(PutS
.getCallee()->stripPointerCasts()))
1156 CI
->setCallingConv(F
->getCallingConv());
1160 Value
*llvm::emitFPutC(Value
*Char
, Value
*File
, IRBuilder
<> &B
,
1161 const TargetLibraryInfo
*TLI
) {
1162 if (!TLI
->has(LibFunc_fputc
))
1165 Module
*M
= B
.GetInsertBlock()->getModule();
1166 StringRef FPutcName
= TLI
->getName(LibFunc_fputc
);
1167 FunctionCallee F
= M
->getOrInsertFunction(FPutcName
, B
.getInt32Ty(),
1168 B
.getInt32Ty(), File
->getType());
1169 if (File
->getType()->isPointerTy())
1170 inferLibFuncAttributes(M
, FPutcName
, *TLI
);
1171 Char
= B
.CreateIntCast(Char
, B
.getInt32Ty(), /*isSigned*/true,
1173 CallInst
*CI
= B
.CreateCall(F
, {Char
, File
}, FPutcName
);
1175 if (const Function
*Fn
=
1176 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1177 CI
->setCallingConv(Fn
->getCallingConv());
1181 Value
*llvm::emitFPutCUnlocked(Value
*Char
, Value
*File
, IRBuilder
<> &B
,
1182 const TargetLibraryInfo
*TLI
) {
1183 if (!TLI
->has(LibFunc_fputc_unlocked
))
1186 Module
*M
= B
.GetInsertBlock()->getModule();
1187 StringRef FPutcUnlockedName
= TLI
->getName(LibFunc_fputc_unlocked
);
1188 FunctionCallee F
= M
->getOrInsertFunction(FPutcUnlockedName
, B
.getInt32Ty(),
1189 B
.getInt32Ty(), File
->getType());
1190 if (File
->getType()->isPointerTy())
1191 inferLibFuncAttributes(M
, FPutcUnlockedName
, *TLI
);
1192 Char
= B
.CreateIntCast(Char
, B
.getInt32Ty(), /*isSigned*/ true, "chari");
1193 CallInst
*CI
= B
.CreateCall(F
, {Char
, File
}, FPutcUnlockedName
);
1195 if (const Function
*Fn
=
1196 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1197 CI
->setCallingConv(Fn
->getCallingConv());
1201 Value
*llvm::emitFPutS(Value
*Str
, Value
*File
, IRBuilder
<> &B
,
1202 const TargetLibraryInfo
*TLI
) {
1203 if (!TLI
->has(LibFunc_fputs
))
1206 Module
*M
= B
.GetInsertBlock()->getModule();
1207 StringRef FPutsName
= TLI
->getName(LibFunc_fputs
);
1208 FunctionCallee F
= M
->getOrInsertFunction(FPutsName
, B
.getInt32Ty(),
1209 B
.getInt8PtrTy(), File
->getType());
1210 if (File
->getType()->isPointerTy())
1211 inferLibFuncAttributes(M
, FPutsName
, *TLI
);
1212 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Str
, B
), File
}, FPutsName
);
1214 if (const Function
*Fn
=
1215 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1216 CI
->setCallingConv(Fn
->getCallingConv());
1220 Value
*llvm::emitFPutSUnlocked(Value
*Str
, Value
*File
, IRBuilder
<> &B
,
1221 const TargetLibraryInfo
*TLI
) {
1222 if (!TLI
->has(LibFunc_fputs_unlocked
))
1225 Module
*M
= B
.GetInsertBlock()->getModule();
1226 StringRef FPutsUnlockedName
= TLI
->getName(LibFunc_fputs_unlocked
);
1227 FunctionCallee F
= M
->getOrInsertFunction(FPutsUnlockedName
, B
.getInt32Ty(),
1228 B
.getInt8PtrTy(), File
->getType());
1229 if (File
->getType()->isPointerTy())
1230 inferLibFuncAttributes(M
, FPutsUnlockedName
, *TLI
);
1231 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Str
, B
), File
}, FPutsUnlockedName
);
1233 if (const Function
*Fn
=
1234 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1235 CI
->setCallingConv(Fn
->getCallingConv());
1239 Value
*llvm::emitFWrite(Value
*Ptr
, Value
*Size
, Value
*File
, IRBuilder
<> &B
,
1240 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1241 if (!TLI
->has(LibFunc_fwrite
))
1244 Module
*M
= B
.GetInsertBlock()->getModule();
1245 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1246 StringRef FWriteName
= TLI
->getName(LibFunc_fwrite
);
1247 FunctionCallee F
= M
->getOrInsertFunction(
1248 FWriteName
, DL
.getIntPtrType(Context
), B
.getInt8PtrTy(),
1249 DL
.getIntPtrType(Context
), DL
.getIntPtrType(Context
), File
->getType());
1251 if (File
->getType()->isPointerTy())
1252 inferLibFuncAttributes(M
, FWriteName
, *TLI
);
1254 B
.CreateCall(F
, {castToCStr(Ptr
, B
), Size
,
1255 ConstantInt::get(DL
.getIntPtrType(Context
), 1), File
});
1257 if (const Function
*Fn
=
1258 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1259 CI
->setCallingConv(Fn
->getCallingConv());
1263 Value
*llvm::emitMalloc(Value
*Num
, IRBuilder
<> &B
, const DataLayout
&DL
,
1264 const TargetLibraryInfo
*TLI
) {
1265 if (!TLI
->has(LibFunc_malloc
))
1268 Module
*M
= B
.GetInsertBlock()->getModule();
1269 StringRef MallocName
= TLI
->getName(LibFunc_malloc
);
1270 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1271 FunctionCallee Malloc
= M
->getOrInsertFunction(MallocName
, B
.getInt8PtrTy(),
1272 DL
.getIntPtrType(Context
));
1273 inferLibFuncAttributes(M
, MallocName
, *TLI
);
1274 CallInst
*CI
= B
.CreateCall(Malloc
, Num
, MallocName
);
1276 if (const Function
*F
=
1277 dyn_cast
<Function
>(Malloc
.getCallee()->stripPointerCasts()))
1278 CI
->setCallingConv(F
->getCallingConv());
1283 Value
*llvm::emitCalloc(Value
*Num
, Value
*Size
, const AttributeList
&Attrs
,
1284 IRBuilder
<> &B
, const TargetLibraryInfo
&TLI
) {
1285 if (!TLI
.has(LibFunc_calloc
))
1288 Module
*M
= B
.GetInsertBlock()->getModule();
1289 StringRef CallocName
= TLI
.getName(LibFunc_calloc
);
1290 const DataLayout
&DL
= M
->getDataLayout();
1291 IntegerType
*PtrType
= DL
.getIntPtrType((B
.GetInsertBlock()->getContext()));
1292 FunctionCallee Calloc
= M
->getOrInsertFunction(
1293 CallocName
, Attrs
, B
.getInt8PtrTy(), PtrType
, PtrType
);
1294 inferLibFuncAttributes(M
, CallocName
, TLI
);
1295 CallInst
*CI
= B
.CreateCall(Calloc
, {Num
, Size
}, CallocName
);
1298 dyn_cast
<Function
>(Calloc
.getCallee()->stripPointerCasts()))
1299 CI
->setCallingConv(F
->getCallingConv());
1304 Value
*llvm::emitFWriteUnlocked(Value
*Ptr
, Value
*Size
, Value
*N
, Value
*File
,
1305 IRBuilder
<> &B
, const DataLayout
&DL
,
1306 const TargetLibraryInfo
*TLI
) {
1307 if (!TLI
->has(LibFunc_fwrite_unlocked
))
1310 Module
*M
= B
.GetInsertBlock()->getModule();
1311 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1312 StringRef FWriteUnlockedName
= TLI
->getName(LibFunc_fwrite_unlocked
);
1313 FunctionCallee F
= M
->getOrInsertFunction(
1314 FWriteUnlockedName
, DL
.getIntPtrType(Context
), B
.getInt8PtrTy(),
1315 DL
.getIntPtrType(Context
), DL
.getIntPtrType(Context
), File
->getType());
1317 if (File
->getType()->isPointerTy())
1318 inferLibFuncAttributes(M
, FWriteUnlockedName
, *TLI
);
1319 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Ptr
, B
), Size
, N
, File
});
1321 if (const Function
*Fn
=
1322 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1323 CI
->setCallingConv(Fn
->getCallingConv());
1327 Value
*llvm::emitFGetCUnlocked(Value
*File
, IRBuilder
<> &B
,
1328 const TargetLibraryInfo
*TLI
) {
1329 if (!TLI
->has(LibFunc_fgetc_unlocked
))
1332 Module
*M
= B
.GetInsertBlock()->getModule();
1333 StringRef FGetCUnlockedName
= TLI
->getName(LibFunc_fgetc_unlocked
);
1334 FunctionCallee F
= M
->getOrInsertFunction(FGetCUnlockedName
, B
.getInt32Ty(),
1336 if (File
->getType()->isPointerTy())
1337 inferLibFuncAttributes(M
, FGetCUnlockedName
, *TLI
);
1338 CallInst
*CI
= B
.CreateCall(F
, File
, FGetCUnlockedName
);
1340 if (const Function
*Fn
=
1341 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1342 CI
->setCallingConv(Fn
->getCallingConv());
1346 Value
*llvm::emitFGetSUnlocked(Value
*Str
, Value
*Size
, Value
*File
,
1347 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
) {
1348 if (!TLI
->has(LibFunc_fgets_unlocked
))
1351 Module
*M
= B
.GetInsertBlock()->getModule();
1352 StringRef FGetSUnlockedName
= TLI
->getName(LibFunc_fgets_unlocked
);
1354 M
->getOrInsertFunction(FGetSUnlockedName
, B
.getInt8PtrTy(),
1355 B
.getInt8PtrTy(), B
.getInt32Ty(), File
->getType());
1356 inferLibFuncAttributes(M
, FGetSUnlockedName
, *TLI
);
1358 B
.CreateCall(F
, {castToCStr(Str
, B
), Size
, File
}, FGetSUnlockedName
);
1360 if (const Function
*Fn
=
1361 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1362 CI
->setCallingConv(Fn
->getCallingConv());
1366 Value
*llvm::emitFReadUnlocked(Value
*Ptr
, Value
*Size
, Value
*N
, Value
*File
,
1367 IRBuilder
<> &B
, const DataLayout
&DL
,
1368 const TargetLibraryInfo
*TLI
) {
1369 if (!TLI
->has(LibFunc_fread_unlocked
))
1372 Module
*M
= B
.GetInsertBlock()->getModule();
1373 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1374 StringRef FReadUnlockedName
= TLI
->getName(LibFunc_fread_unlocked
);
1375 FunctionCallee F
= M
->getOrInsertFunction(
1376 FReadUnlockedName
, DL
.getIntPtrType(Context
), B
.getInt8PtrTy(),
1377 DL
.getIntPtrType(Context
), DL
.getIntPtrType(Context
), File
->getType());
1379 if (File
->getType()->isPointerTy())
1380 inferLibFuncAttributes(M
, FReadUnlockedName
, *TLI
);
1381 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Ptr
, B
), Size
, N
, File
});
1383 if (const Function
*Fn
=
1384 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1385 CI
->setCallingConv(Fn
->getCallingConv());