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);
382 Changed
|= setDoesNotThrow(F
);
383 Changed
|= setDoesNotCapture(F
, 0);
384 Changed
|= setDoesNotCapture(F
, 1);
385 Changed
|= setOnlyReadsMemory(F
, 0);
388 Changed
|= setDoesNotThrow(F
);
389 Changed
|= setOnlyReadsMemory(F
);
390 Changed
|= setDoesNotCapture(F
, 0);
391 Changed
|= setDoesNotCapture(F
, 1);
394 Changed
|= setDoesNotThrow(F
);
395 Changed
|= setDoesNotCapture(F
, 0);
398 Changed
|= setDoesNotThrow(F
);
399 Changed
|= setRetDoesNotAlias(F
);
403 Changed
|= setDoesNotThrow(F
);
404 Changed
|= setDoesNotCapture(F
, 0);
405 Changed
|= setOnlyReadsMemory(F
, 0);
407 case LibFunc_ctermid
:
408 case LibFunc_clearerr
:
409 case LibFunc_closedir
:
410 Changed
|= setDoesNotThrow(F
);
411 Changed
|= setDoesNotCapture(F
, 0);
417 Changed
|= setDoesNotThrow(F
);
418 Changed
|= setOnlyReadsMemory(F
);
419 Changed
|= setDoesNotCapture(F
, 0);
422 Changed
|= setDoesNotThrow(F
);
423 Changed
|= setDoesNotCapture(F
, 0);
424 Changed
|= setOnlyReadsMemory(F
, 0);
427 Changed
|= setDoesNotThrow(F
);
428 Changed
|= setRetDoesNotAlias(F
);
429 Changed
|= setDoesNotCapture(F
, 0);
430 Changed
|= setDoesNotCapture(F
, 1);
431 Changed
|= setOnlyReadsMemory(F
, 0);
432 Changed
|= setOnlyReadsMemory(F
, 1);
435 Changed
|= setDoesNotThrow(F
);
436 Changed
|= setRetDoesNotAlias(F
);
437 Changed
|= setDoesNotCapture(F
, 1);
438 Changed
|= setOnlyReadsMemory(F
, 1);
445 case LibFunc_fgetc_unlocked
:
451 case LibFunc_fsetpos
:
452 case LibFunc_flockfile
:
453 case LibFunc_funlockfile
:
454 case LibFunc_ftrylockfile
:
455 Changed
|= setDoesNotThrow(F
);
456 Changed
|= setDoesNotCapture(F
, 0);
459 Changed
|= setDoesNotThrow(F
);
460 Changed
|= setDoesNotCapture(F
, 0);
461 Changed
|= setOnlyReadsMemory(F
);
464 case LibFunc_fputc_unlocked
:
469 case LibFunc_fstatvfs
:
470 Changed
|= setDoesNotThrow(F
);
471 Changed
|= setDoesNotCapture(F
, 1);
474 case LibFunc_fgets_unlocked
:
475 Changed
|= setDoesNotThrow(F
);
476 Changed
|= setDoesNotCapture(F
, 2);
479 case LibFunc_fread_unlocked
:
480 Changed
|= setDoesNotThrow(F
);
481 Changed
|= setDoesNotCapture(F
, 0);
482 Changed
|= setDoesNotCapture(F
, 3);
485 case LibFunc_fwrite_unlocked
:
486 Changed
|= setDoesNotThrow(F
);
487 Changed
|= setDoesNotCapture(F
, 0);
488 Changed
|= setDoesNotCapture(F
, 3);
489 // FIXME: readonly #1?
492 case LibFunc_fputs_unlocked
:
493 Changed
|= setDoesNotThrow(F
);
494 Changed
|= setDoesNotCapture(F
, 0);
495 Changed
|= setDoesNotCapture(F
, 1);
496 Changed
|= setOnlyReadsMemory(F
, 0);
499 case LibFunc_fprintf
:
500 Changed
|= setDoesNotThrow(F
);
501 Changed
|= setDoesNotCapture(F
, 0);
502 Changed
|= setDoesNotCapture(F
, 1);
503 Changed
|= setOnlyReadsMemory(F
, 1);
505 case LibFunc_fgetpos
:
506 Changed
|= setDoesNotThrow(F
);
507 Changed
|= setDoesNotCapture(F
, 0);
508 Changed
|= setDoesNotCapture(F
, 1);
511 case LibFunc_getlogin_r
:
512 case LibFunc_getc_unlocked
:
513 Changed
|= setDoesNotThrow(F
);
514 Changed
|= setDoesNotCapture(F
, 0);
517 Changed
|= setDoesNotThrow(F
);
518 Changed
|= setOnlyReadsMemory(F
);
519 Changed
|= setDoesNotCapture(F
, 0);
522 case LibFunc_getchar
:
523 case LibFunc_getchar_unlocked
:
524 Changed
|= setDoesNotThrow(F
);
526 case LibFunc_getitimer
:
527 Changed
|= setDoesNotThrow(F
);
528 Changed
|= setDoesNotCapture(F
, 1);
530 case LibFunc_getpwnam
:
531 Changed
|= setDoesNotThrow(F
);
532 Changed
|= setDoesNotCapture(F
, 0);
533 Changed
|= setOnlyReadsMemory(F
, 0);
536 Changed
|= setDoesNotThrow(F
);
537 Changed
|= setDoesNotCapture(F
, 1);
540 Changed
|= setDoesNotThrow(F
);
541 Changed
|= setDoesNotCapture(F
, 0);
544 Changed
|= setDoesNotThrow(F
);
545 Changed
|= setDoesNotCapture(F
, 0);
546 Changed
|= setOnlyReadsMemory(F
, 0);
548 case LibFunc_unsetenv
:
549 Changed
|= setDoesNotThrow(F
);
550 Changed
|= setDoesNotCapture(F
, 0);
551 Changed
|= setOnlyReadsMemory(F
, 0);
555 Changed
|= setDoesNotThrow(F
);
556 Changed
|= setDoesNotCapture(F
, 0);
557 Changed
|= setDoesNotCapture(F
, 1);
558 Changed
|= setOnlyReadsMemory(F
, 0);
559 Changed
|= setOnlyReadsMemory(F
, 1);
562 case LibFunc_putc_unlocked
:
563 Changed
|= setDoesNotThrow(F
);
564 Changed
|= setDoesNotCapture(F
, 1);
569 Changed
|= setDoesNotThrow(F
);
570 Changed
|= setDoesNotCapture(F
, 0);
571 Changed
|= setOnlyReadsMemory(F
, 0);
574 // May throw; "pread" is a valid pthread cancellation point.
575 Changed
|= setDoesNotCapture(F
, 1);
578 // May throw; "pwrite" is a valid pthread cancellation point.
579 Changed
|= setDoesNotCapture(F
, 1);
580 Changed
|= setOnlyReadsMemory(F
, 1);
582 case LibFunc_putchar
:
583 case LibFunc_putchar_unlocked
:
584 Changed
|= setDoesNotThrow(F
);
587 Changed
|= setDoesNotThrow(F
);
588 Changed
|= setRetDoesNotAlias(F
);
589 Changed
|= setDoesNotCapture(F
, 0);
590 Changed
|= setDoesNotCapture(F
, 1);
591 Changed
|= setOnlyReadsMemory(F
, 0);
592 Changed
|= setOnlyReadsMemory(F
, 1);
595 Changed
|= setDoesNotThrow(F
);
596 Changed
|= setDoesNotCapture(F
, 0);
599 Changed
|= setDoesNotThrow(F
);
600 Changed
|= setDoesNotCapture(F
, 0);
601 Changed
|= setOnlyReadsMemory(F
, 0);
603 case LibFunc_vsscanf
:
604 Changed
|= setDoesNotThrow(F
);
605 Changed
|= setDoesNotCapture(F
, 0);
606 Changed
|= setDoesNotCapture(F
, 1);
607 Changed
|= setOnlyReadsMemory(F
, 0);
608 Changed
|= setOnlyReadsMemory(F
, 1);
610 case LibFunc_vfscanf
:
611 Changed
|= setDoesNotThrow(F
);
612 Changed
|= setDoesNotCapture(F
, 0);
613 Changed
|= setDoesNotCapture(F
, 1);
614 Changed
|= setOnlyReadsMemory(F
, 1);
617 Changed
|= setDoesNotThrow(F
);
618 Changed
|= setRetDoesNotAlias(F
);
620 case LibFunc_vprintf
:
621 Changed
|= setDoesNotThrow(F
);
622 Changed
|= setDoesNotCapture(F
, 0);
623 Changed
|= setOnlyReadsMemory(F
, 0);
625 case LibFunc_vfprintf
:
626 case LibFunc_vsprintf
:
627 Changed
|= setDoesNotThrow(F
);
628 Changed
|= setDoesNotCapture(F
, 0);
629 Changed
|= setDoesNotCapture(F
, 1);
630 Changed
|= setOnlyReadsMemory(F
, 1);
632 case LibFunc_vsnprintf
:
633 Changed
|= setDoesNotThrow(F
);
634 Changed
|= setDoesNotCapture(F
, 0);
635 Changed
|= setDoesNotCapture(F
, 2);
636 Changed
|= setOnlyReadsMemory(F
, 2);
639 // May throw; "open" is a valid pthread cancellation point.
640 Changed
|= setDoesNotCapture(F
, 0);
641 Changed
|= setOnlyReadsMemory(F
, 0);
643 case LibFunc_opendir
:
644 Changed
|= setDoesNotThrow(F
);
645 Changed
|= setRetDoesNotAlias(F
);
646 Changed
|= setDoesNotCapture(F
, 0);
647 Changed
|= setOnlyReadsMemory(F
, 0);
649 case LibFunc_tmpfile
:
650 Changed
|= setDoesNotThrow(F
);
651 Changed
|= setRetDoesNotAlias(F
);
654 Changed
|= setDoesNotThrow(F
);
655 Changed
|= setDoesNotCapture(F
, 0);
661 Changed
|= setDoesNotThrow(F
);
662 Changed
|= setDoesNotAccessMemory(F
);
665 Changed
|= setDoesNotThrow(F
);
666 Changed
|= setDoesNotCapture(F
, 0);
667 Changed
|= setDoesNotCapture(F
, 1);
668 Changed
|= setOnlyReadsMemory(F
, 0);
671 Changed
|= setDoesNotThrow(F
);
672 Changed
|= setDoesNotCapture(F
, 0);
673 Changed
|= setOnlyReadsMemory(F
, 0);
676 // May throw; places call through function pointer.
677 Changed
|= setDoesNotCapture(F
, 3);
679 case LibFunc_dunder_strdup
:
680 case LibFunc_dunder_strndup
:
681 Changed
|= setDoesNotThrow(F
);
682 Changed
|= setRetDoesNotAlias(F
);
683 Changed
|= setDoesNotCapture(F
, 0);
684 Changed
|= setOnlyReadsMemory(F
, 0);
686 case LibFunc_dunder_strtok_r
:
687 Changed
|= setDoesNotThrow(F
);
688 Changed
|= setDoesNotCapture(F
, 1);
689 Changed
|= setOnlyReadsMemory(F
, 1);
691 case LibFunc_under_IO_getc
:
692 Changed
|= setDoesNotThrow(F
);
693 Changed
|= setDoesNotCapture(F
, 0);
695 case LibFunc_under_IO_putc
:
696 Changed
|= setDoesNotThrow(F
);
697 Changed
|= setDoesNotCapture(F
, 1);
699 case LibFunc_dunder_isoc99_scanf
:
700 Changed
|= setDoesNotThrow(F
);
701 Changed
|= setDoesNotCapture(F
, 0);
702 Changed
|= setOnlyReadsMemory(F
, 0);
705 case LibFunc_lstat64
:
706 case LibFunc_statvfs64
:
707 Changed
|= setDoesNotThrow(F
);
708 Changed
|= setDoesNotCapture(F
, 0);
709 Changed
|= setDoesNotCapture(F
, 1);
710 Changed
|= setOnlyReadsMemory(F
, 0);
712 case LibFunc_dunder_isoc99_sscanf
:
713 Changed
|= setDoesNotThrow(F
);
714 Changed
|= setDoesNotCapture(F
, 0);
715 Changed
|= setDoesNotCapture(F
, 1);
716 Changed
|= setOnlyReadsMemory(F
, 0);
717 Changed
|= setOnlyReadsMemory(F
, 1);
719 case LibFunc_fopen64
:
720 Changed
|= setDoesNotThrow(F
);
721 Changed
|= setRetDoesNotAlias(F
);
722 Changed
|= setDoesNotCapture(F
, 0);
723 Changed
|= setDoesNotCapture(F
, 1);
724 Changed
|= setOnlyReadsMemory(F
, 0);
725 Changed
|= setOnlyReadsMemory(F
, 1);
727 case LibFunc_fseeko64
:
728 case LibFunc_ftello64
:
729 Changed
|= setDoesNotThrow(F
);
730 Changed
|= setDoesNotCapture(F
, 0);
732 case LibFunc_tmpfile64
:
733 Changed
|= setDoesNotThrow(F
);
734 Changed
|= setRetDoesNotAlias(F
);
736 case LibFunc_fstat64
:
737 case LibFunc_fstatvfs64
:
738 Changed
|= setDoesNotThrow(F
);
739 Changed
|= setDoesNotCapture(F
, 1);
742 // May throw; "open" is a valid pthread cancellation point.
743 Changed
|= setDoesNotCapture(F
, 0);
744 Changed
|= setOnlyReadsMemory(F
, 0);
746 case LibFunc_gettimeofday
:
747 // Currently some platforms have the restrict keyword on the arguments to
748 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
750 Changed
|= setDoesNotThrow(F
);
751 Changed
|= setDoesNotCapture(F
, 0);
752 Changed
|= setDoesNotCapture(F
, 1);
754 case LibFunc_Znwj
: // new(unsigned int)
755 case LibFunc_Znwm
: // new(unsigned long)
756 case LibFunc_Znaj
: // new[](unsigned int)
757 case LibFunc_Znam
: // new[](unsigned long)
758 case LibFunc_msvc_new_int
: // new(unsigned int)
759 case LibFunc_msvc_new_longlong
: // new(unsigned long long)
760 case LibFunc_msvc_new_array_int
: // new[](unsigned int)
761 case LibFunc_msvc_new_array_longlong
: // new[](unsigned long long)
762 // Operator new always returns a nonnull noalias pointer
763 Changed
|= setRetNonNull(F
);
764 Changed
|= setRetDoesNotAlias(F
);
766 // TODO: add LibFunc entries for:
767 // case LibFunc_memset_pattern4:
768 // case LibFunc_memset_pattern8:
769 case LibFunc_memset_pattern16
:
770 Changed
|= setOnlyAccessesArgMemory(F
);
771 Changed
|= setDoesNotCapture(F
, 0);
772 Changed
|= setDoesNotCapture(F
, 1);
773 Changed
|= setOnlyReadsMemory(F
, 1);
775 // int __nvvm_reflect(const char *)
776 case LibFunc_nvvm_reflect
:
777 Changed
|= setDoesNotAccessMemory(F
);
778 Changed
|= setDoesNotThrow(F
);
782 // FIXME: It'd be really nice to cover all the library functions we're
788 bool llvm::hasFloatFn(const TargetLibraryInfo
*TLI
, Type
*Ty
,
789 LibFunc DoubleFn
, LibFunc FloatFn
, LibFunc LongDoubleFn
) {
790 switch (Ty
->getTypeID()) {
793 case Type::FloatTyID
:
794 return TLI
->has(FloatFn
);
795 case Type::DoubleTyID
:
796 return TLI
->has(DoubleFn
);
798 return TLI
->has(LongDoubleFn
);
802 StringRef
llvm::getFloatFnName(const TargetLibraryInfo
*TLI
, Type
*Ty
,
803 LibFunc DoubleFn
, LibFunc FloatFn
,
804 LibFunc LongDoubleFn
) {
805 assert(hasFloatFn(TLI
, Ty
, DoubleFn
, FloatFn
, LongDoubleFn
) &&
806 "Cannot get name for unavailable function!");
808 switch (Ty
->getTypeID()) {
810 llvm_unreachable("No name for HalfTy!");
811 case Type::FloatTyID
:
812 return TLI
->getName(FloatFn
);
813 case Type::DoubleTyID
:
814 return TLI
->getName(DoubleFn
);
816 return TLI
->getName(LongDoubleFn
);
820 //- Emit LibCalls ------------------------------------------------------------//
822 Value
*llvm::castToCStr(Value
*V
, IRBuilder
<> &B
) {
823 unsigned AS
= V
->getType()->getPointerAddressSpace();
824 return B
.CreateBitCast(V
, B
.getInt8PtrTy(AS
), "cstr");
827 static Value
*emitLibCall(LibFunc TheLibFunc
, Type
*ReturnType
,
828 ArrayRef
<Type
*> ParamTypes
,
829 ArrayRef
<Value
*> Operands
, IRBuilder
<> &B
,
830 const TargetLibraryInfo
*TLI
,
831 bool IsVaArgs
= false) {
832 if (!TLI
->has(TheLibFunc
))
835 Module
*M
= B
.GetInsertBlock()->getModule();
836 StringRef FuncName
= TLI
->getName(TheLibFunc
);
837 FunctionType
*FuncType
= FunctionType::get(ReturnType
, ParamTypes
, IsVaArgs
);
838 FunctionCallee Callee
= M
->getOrInsertFunction(FuncName
, FuncType
);
839 inferLibFuncAttributes(M
, FuncName
, *TLI
);
840 CallInst
*CI
= B
.CreateCall(Callee
, Operands
, FuncName
);
841 if (const Function
*F
=
842 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
843 CI
->setCallingConv(F
->getCallingConv());
847 Value
*llvm::emitStrLen(Value
*Ptr
, IRBuilder
<> &B
, const DataLayout
&DL
,
848 const TargetLibraryInfo
*TLI
) {
849 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
850 return emitLibCall(LibFunc_strlen
, DL
.getIntPtrType(Context
),
851 B
.getInt8PtrTy(), castToCStr(Ptr
, B
), B
, TLI
);
854 Value
*llvm::emitStrDup(Value
*Ptr
, IRBuilder
<> &B
,
855 const TargetLibraryInfo
*TLI
) {
856 return emitLibCall(LibFunc_strdup
, B
.getInt8PtrTy(), B
.getInt8PtrTy(),
857 castToCStr(Ptr
, B
), B
, TLI
);
860 Value
*llvm::emitStrChr(Value
*Ptr
, char C
, IRBuilder
<> &B
,
861 const TargetLibraryInfo
*TLI
) {
862 Type
*I8Ptr
= B
.getInt8PtrTy();
863 Type
*I32Ty
= B
.getInt32Ty();
864 return emitLibCall(LibFunc_strchr
, I8Ptr
, {I8Ptr
, I32Ty
},
865 {castToCStr(Ptr
, B
), ConstantInt::get(I32Ty
, C
)}, B
, TLI
);
868 Value
*llvm::emitStrNCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
869 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
870 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
872 LibFunc_strncmp
, B
.getInt32Ty(),
873 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
874 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
877 Value
*llvm::emitStrCpy(Value
*Dst
, Value
*Src
, IRBuilder
<> &B
,
878 const TargetLibraryInfo
*TLI
) {
879 Type
*I8Ptr
= B
.getInt8PtrTy();
880 return emitLibCall(LibFunc_strcpy
, I8Ptr
, {I8Ptr
, I8Ptr
},
881 {castToCStr(Dst
, B
), castToCStr(Src
, B
)}, B
, TLI
);
884 Value
*llvm::emitStpCpy(Value
*Dst
, Value
*Src
, IRBuilder
<> &B
,
885 const TargetLibraryInfo
*TLI
) {
886 Type
*I8Ptr
= B
.getInt8PtrTy();
887 return emitLibCall(LibFunc_stpcpy
, I8Ptr
, {I8Ptr
, I8Ptr
},
888 {castToCStr(Dst
, B
), castToCStr(Src
, B
)}, B
, TLI
);
891 Value
*llvm::emitStrNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilder
<> &B
,
892 const TargetLibraryInfo
*TLI
) {
893 Type
*I8Ptr
= B
.getInt8PtrTy();
894 return emitLibCall(LibFunc_strncpy
, I8Ptr
, {I8Ptr
, I8Ptr
, Len
->getType()},
895 {castToCStr(Dst
, B
), castToCStr(Src
, B
), Len
}, B
, TLI
);
898 Value
*llvm::emitStpNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilder
<> &B
,
899 const TargetLibraryInfo
*TLI
) {
900 Type
*I8Ptr
= B
.getInt8PtrTy();
901 return emitLibCall(LibFunc_stpncpy
, I8Ptr
, {I8Ptr
, I8Ptr
, Len
->getType()},
902 {castToCStr(Dst
, B
), castToCStr(Src
, B
), Len
}, B
, TLI
);
905 Value
*llvm::emitMemCpyChk(Value
*Dst
, Value
*Src
, Value
*Len
, Value
*ObjSize
,
906 IRBuilder
<> &B
, const DataLayout
&DL
,
907 const TargetLibraryInfo
*TLI
) {
908 if (!TLI
->has(LibFunc_memcpy_chk
))
911 Module
*M
= B
.GetInsertBlock()->getModule();
913 AS
= AttributeList::get(M
->getContext(), AttributeList::FunctionIndex
,
914 Attribute::NoUnwind
);
915 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
916 FunctionCallee MemCpy
= M
->getOrInsertFunction(
917 "__memcpy_chk", AttributeList::get(M
->getContext(), AS
), B
.getInt8PtrTy(),
918 B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
),
919 DL
.getIntPtrType(Context
));
920 Dst
= castToCStr(Dst
, B
);
921 Src
= castToCStr(Src
, B
);
922 CallInst
*CI
= B
.CreateCall(MemCpy
, {Dst
, Src
, Len
, ObjSize
});
923 if (const Function
*F
=
924 dyn_cast
<Function
>(MemCpy
.getCallee()->stripPointerCasts()))
925 CI
->setCallingConv(F
->getCallingConv());
929 Value
*llvm::emitMemChr(Value
*Ptr
, Value
*Val
, Value
*Len
, IRBuilder
<> &B
,
930 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
931 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
933 LibFunc_memchr
, B
.getInt8PtrTy(),
934 {B
.getInt8PtrTy(), B
.getInt32Ty(), DL
.getIntPtrType(Context
)},
935 {castToCStr(Ptr
, B
), Val
, Len
}, B
, TLI
);
938 Value
*llvm::emitMemCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
939 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
940 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
942 LibFunc_memcmp
, B
.getInt32Ty(),
943 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
944 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
947 Value
*llvm::emitBCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
948 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
949 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
951 LibFunc_bcmp
, B
.getInt32Ty(),
952 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
953 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
956 Value
*llvm::emitMemCCpy(Value
*Ptr1
, Value
*Ptr2
, Value
*Val
, Value
*Len
,
957 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
) {
959 LibFunc_memccpy
, B
.getInt8PtrTy(),
960 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), B
.getInt32Ty(), Len
->getType()},
961 {Ptr1
, Ptr2
, Val
, Len
}, B
, TLI
);
964 Value
*llvm::emitSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
,
965 ArrayRef
<Value
*> VariadicArgs
, IRBuilder
<> &B
,
966 const TargetLibraryInfo
*TLI
) {
967 SmallVector
<Value
*, 8> Args
{castToCStr(Dest
, B
), Size
, castToCStr(Fmt
, B
)};
968 Args
.insert(Args
.end(), VariadicArgs
.begin(), VariadicArgs
.end());
969 return emitLibCall(LibFunc_snprintf
, B
.getInt32Ty(),
970 {B
.getInt8PtrTy(), Size
->getType(), B
.getInt8PtrTy()},
971 Args
, B
, TLI
, /*IsVaArgs=*/true);
974 Value
*llvm::emitSPrintf(Value
*Dest
, Value
*Fmt
,
975 ArrayRef
<Value
*> VariadicArgs
, IRBuilder
<> &B
,
976 const TargetLibraryInfo
*TLI
) {
977 SmallVector
<Value
*, 8> Args
{castToCStr(Dest
, B
), castToCStr(Fmt
, B
)};
978 Args
.insert(Args
.end(), VariadicArgs
.begin(), VariadicArgs
.end());
979 return emitLibCall(LibFunc_sprintf
, B
.getInt32Ty(),
980 {B
.getInt8PtrTy(), B
.getInt8PtrTy()}, Args
, B
, TLI
,
984 Value
*llvm::emitStrCat(Value
*Dest
, Value
*Src
, IRBuilder
<> &B
,
985 const TargetLibraryInfo
*TLI
) {
986 return emitLibCall(LibFunc_strcat
, B
.getInt8PtrTy(),
987 {B
.getInt8PtrTy(), B
.getInt8PtrTy()},
988 {castToCStr(Dest
, B
), castToCStr(Src
, B
)}, B
, TLI
);
991 Value
*llvm::emitStrLCpy(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilder
<> &B
,
992 const TargetLibraryInfo
*TLI
) {
993 return emitLibCall(LibFunc_strlcpy
, Size
->getType(),
994 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
995 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
998 Value
*llvm::emitStrLCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilder
<> &B
,
999 const TargetLibraryInfo
*TLI
) {
1000 return emitLibCall(LibFunc_strlcat
, Size
->getType(),
1001 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
1002 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
1005 Value
*llvm::emitStrNCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilder
<> &B
,
1006 const TargetLibraryInfo
*TLI
) {
1007 return emitLibCall(LibFunc_strncat
, B
.getInt8PtrTy(),
1008 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
1009 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
1012 Value
*llvm::emitVSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
, Value
*VAList
,
1013 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
) {
1015 LibFunc_vsnprintf
, B
.getInt32Ty(),
1016 {B
.getInt8PtrTy(), Size
->getType(), B
.getInt8PtrTy(), VAList
->getType()},
1017 {castToCStr(Dest
, B
), Size
, castToCStr(Fmt
, B
), VAList
}, B
, TLI
);
1020 Value
*llvm::emitVSPrintf(Value
*Dest
, Value
*Fmt
, Value
*VAList
,
1021 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
) {
1022 return emitLibCall(LibFunc_vsprintf
, B
.getInt32Ty(),
1023 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), VAList
->getType()},
1024 {castToCStr(Dest
, B
), castToCStr(Fmt
, B
), VAList
}, B
, TLI
);
1027 /// Append a suffix to the function name according to the type of 'Op'.
1028 static void appendTypeSuffix(Value
*Op
, StringRef
&Name
,
1029 SmallString
<20> &NameBuffer
) {
1030 if (!Op
->getType()->isDoubleTy()) {
1033 if (Op
->getType()->isFloatTy())
1042 static Value
*emitUnaryFloatFnCallHelper(Value
*Op
, StringRef Name
,
1044 const AttributeList
&Attrs
) {
1045 assert((Name
!= "") && "Must specify Name to emitUnaryFloatFnCall");
1047 Module
*M
= B
.GetInsertBlock()->getModule();
1048 FunctionCallee Callee
=
1049 M
->getOrInsertFunction(Name
, Op
->getType(), Op
->getType());
1050 CallInst
*CI
= B
.CreateCall(Callee
, Op
, Name
);
1052 // The incoming attribute set may have come from a speculatable intrinsic, but
1053 // is being replaced with a library call which is not allowed to be
1055 CI
->setAttributes(Attrs
.removeAttribute(B
.getContext(),
1056 AttributeList::FunctionIndex
,
1057 Attribute::Speculatable
));
1058 if (const Function
*F
=
1059 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1060 CI
->setCallingConv(F
->getCallingConv());
1065 Value
*llvm::emitUnaryFloatFnCall(Value
*Op
, StringRef Name
, IRBuilder
<> &B
,
1066 const AttributeList
&Attrs
) {
1067 SmallString
<20> NameBuffer
;
1068 appendTypeSuffix(Op
, Name
, NameBuffer
);
1070 return emitUnaryFloatFnCallHelper(Op
, Name
, B
, Attrs
);
1073 Value
*llvm::emitUnaryFloatFnCall(Value
*Op
, const TargetLibraryInfo
*TLI
,
1074 LibFunc DoubleFn
, LibFunc FloatFn
,
1075 LibFunc LongDoubleFn
, IRBuilder
<> &B
,
1076 const AttributeList
&Attrs
) {
1077 // Get the name of the function according to TLI.
1078 StringRef Name
= getFloatFnName(TLI
, Op
->getType(),
1079 DoubleFn
, FloatFn
, LongDoubleFn
);
1081 return emitUnaryFloatFnCallHelper(Op
, Name
, B
, Attrs
);
1084 static Value
*emitBinaryFloatFnCallHelper(Value
*Op1
, Value
*Op2
,
1085 StringRef Name
, IRBuilder
<> &B
,
1086 const AttributeList
&Attrs
) {
1087 assert((Name
!= "") && "Must specify Name to emitBinaryFloatFnCall");
1089 Module
*M
= B
.GetInsertBlock()->getModule();
1090 FunctionCallee Callee
= M
->getOrInsertFunction(Name
, Op1
->getType(),
1091 Op1
->getType(), Op2
->getType());
1092 CallInst
*CI
= B
.CreateCall(Callee
, { Op1
, Op2
}, Name
);
1094 // The incoming attribute set may have come from a speculatable intrinsic, but
1095 // is being replaced with a library call which is not allowed to be
1097 CI
->setAttributes(Attrs
.removeAttribute(B
.getContext(),
1098 AttributeList::FunctionIndex
,
1099 Attribute::Speculatable
));
1100 if (const Function
*F
=
1101 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1102 CI
->setCallingConv(F
->getCallingConv());
1107 Value
*llvm::emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
, StringRef Name
,
1108 IRBuilder
<> &B
, const AttributeList
&Attrs
) {
1109 assert((Name
!= "") && "Must specify Name to emitBinaryFloatFnCall");
1111 SmallString
<20> NameBuffer
;
1112 appendTypeSuffix(Op1
, Name
, NameBuffer
);
1114 return emitBinaryFloatFnCallHelper(Op1
, Op2
, Name
, B
, Attrs
);
1117 Value
*llvm::emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
,
1118 const TargetLibraryInfo
*TLI
,
1119 LibFunc DoubleFn
, LibFunc FloatFn
,
1120 LibFunc LongDoubleFn
, IRBuilder
<> &B
,
1121 const AttributeList
&Attrs
) {
1122 // Get the name of the function according to TLI.
1123 StringRef Name
= getFloatFnName(TLI
, Op1
->getType(),
1124 DoubleFn
, FloatFn
, LongDoubleFn
);
1126 return emitBinaryFloatFnCallHelper(Op1
, Op2
, Name
, B
, Attrs
);
1129 Value
*llvm::emitPutChar(Value
*Char
, IRBuilder
<> &B
,
1130 const TargetLibraryInfo
*TLI
) {
1131 if (!TLI
->has(LibFunc_putchar
))
1134 Module
*M
= B
.GetInsertBlock()->getModule();
1135 StringRef PutCharName
= TLI
->getName(LibFunc_putchar
);
1136 FunctionCallee PutChar
=
1137 M
->getOrInsertFunction(PutCharName
, B
.getInt32Ty(), B
.getInt32Ty());
1138 inferLibFuncAttributes(M
, PutCharName
, *TLI
);
1139 CallInst
*CI
= B
.CreateCall(PutChar
,
1140 B
.CreateIntCast(Char
,
1146 if (const Function
*F
=
1147 dyn_cast
<Function
>(PutChar
.getCallee()->stripPointerCasts()))
1148 CI
->setCallingConv(F
->getCallingConv());
1152 Value
*llvm::emitPutS(Value
*Str
, IRBuilder
<> &B
,
1153 const TargetLibraryInfo
*TLI
) {
1154 if (!TLI
->has(LibFunc_puts
))
1157 Module
*M
= B
.GetInsertBlock()->getModule();
1158 StringRef PutsName
= TLI
->getName(LibFunc_puts
);
1159 FunctionCallee PutS
=
1160 M
->getOrInsertFunction(PutsName
, B
.getInt32Ty(), B
.getInt8PtrTy());
1161 inferLibFuncAttributes(M
, PutsName
, *TLI
);
1162 CallInst
*CI
= B
.CreateCall(PutS
, castToCStr(Str
, B
), PutsName
);
1163 if (const Function
*F
=
1164 dyn_cast
<Function
>(PutS
.getCallee()->stripPointerCasts()))
1165 CI
->setCallingConv(F
->getCallingConv());
1169 Value
*llvm::emitFPutC(Value
*Char
, Value
*File
, IRBuilder
<> &B
,
1170 const TargetLibraryInfo
*TLI
) {
1171 if (!TLI
->has(LibFunc_fputc
))
1174 Module
*M
= B
.GetInsertBlock()->getModule();
1175 StringRef FPutcName
= TLI
->getName(LibFunc_fputc
);
1176 FunctionCallee F
= M
->getOrInsertFunction(FPutcName
, B
.getInt32Ty(),
1177 B
.getInt32Ty(), File
->getType());
1178 if (File
->getType()->isPointerTy())
1179 inferLibFuncAttributes(M
, FPutcName
, *TLI
);
1180 Char
= B
.CreateIntCast(Char
, B
.getInt32Ty(), /*isSigned*/true,
1182 CallInst
*CI
= B
.CreateCall(F
, {Char
, File
}, FPutcName
);
1184 if (const Function
*Fn
=
1185 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1186 CI
->setCallingConv(Fn
->getCallingConv());
1190 Value
*llvm::emitFPutCUnlocked(Value
*Char
, Value
*File
, IRBuilder
<> &B
,
1191 const TargetLibraryInfo
*TLI
) {
1192 if (!TLI
->has(LibFunc_fputc_unlocked
))
1195 Module
*M
= B
.GetInsertBlock()->getModule();
1196 StringRef FPutcUnlockedName
= TLI
->getName(LibFunc_fputc_unlocked
);
1197 FunctionCallee F
= M
->getOrInsertFunction(FPutcUnlockedName
, B
.getInt32Ty(),
1198 B
.getInt32Ty(), File
->getType());
1199 if (File
->getType()->isPointerTy())
1200 inferLibFuncAttributes(M
, FPutcUnlockedName
, *TLI
);
1201 Char
= B
.CreateIntCast(Char
, B
.getInt32Ty(), /*isSigned*/ true, "chari");
1202 CallInst
*CI
= B
.CreateCall(F
, {Char
, File
}, FPutcUnlockedName
);
1204 if (const Function
*Fn
=
1205 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1206 CI
->setCallingConv(Fn
->getCallingConv());
1210 Value
*llvm::emitFPutS(Value
*Str
, Value
*File
, IRBuilder
<> &B
,
1211 const TargetLibraryInfo
*TLI
) {
1212 if (!TLI
->has(LibFunc_fputs
))
1215 Module
*M
= B
.GetInsertBlock()->getModule();
1216 StringRef FPutsName
= TLI
->getName(LibFunc_fputs
);
1217 FunctionCallee F
= M
->getOrInsertFunction(FPutsName
, B
.getInt32Ty(),
1218 B
.getInt8PtrTy(), File
->getType());
1219 if (File
->getType()->isPointerTy())
1220 inferLibFuncAttributes(M
, FPutsName
, *TLI
);
1221 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Str
, B
), File
}, FPutsName
);
1223 if (const Function
*Fn
=
1224 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1225 CI
->setCallingConv(Fn
->getCallingConv());
1229 Value
*llvm::emitFPutSUnlocked(Value
*Str
, Value
*File
, IRBuilder
<> &B
,
1230 const TargetLibraryInfo
*TLI
) {
1231 if (!TLI
->has(LibFunc_fputs_unlocked
))
1234 Module
*M
= B
.GetInsertBlock()->getModule();
1235 StringRef FPutsUnlockedName
= TLI
->getName(LibFunc_fputs_unlocked
);
1236 FunctionCallee F
= M
->getOrInsertFunction(FPutsUnlockedName
, B
.getInt32Ty(),
1237 B
.getInt8PtrTy(), File
->getType());
1238 if (File
->getType()->isPointerTy())
1239 inferLibFuncAttributes(M
, FPutsUnlockedName
, *TLI
);
1240 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Str
, B
), File
}, FPutsUnlockedName
);
1242 if (const Function
*Fn
=
1243 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1244 CI
->setCallingConv(Fn
->getCallingConv());
1248 Value
*llvm::emitFWrite(Value
*Ptr
, Value
*Size
, Value
*File
, IRBuilder
<> &B
,
1249 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1250 if (!TLI
->has(LibFunc_fwrite
))
1253 Module
*M
= B
.GetInsertBlock()->getModule();
1254 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1255 StringRef FWriteName
= TLI
->getName(LibFunc_fwrite
);
1256 FunctionCallee F
= M
->getOrInsertFunction(
1257 FWriteName
, DL
.getIntPtrType(Context
), B
.getInt8PtrTy(),
1258 DL
.getIntPtrType(Context
), DL
.getIntPtrType(Context
), File
->getType());
1260 if (File
->getType()->isPointerTy())
1261 inferLibFuncAttributes(M
, FWriteName
, *TLI
);
1263 B
.CreateCall(F
, {castToCStr(Ptr
, B
), Size
,
1264 ConstantInt::get(DL
.getIntPtrType(Context
), 1), File
});
1266 if (const Function
*Fn
=
1267 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1268 CI
->setCallingConv(Fn
->getCallingConv());
1272 Value
*llvm::emitMalloc(Value
*Num
, IRBuilder
<> &B
, const DataLayout
&DL
,
1273 const TargetLibraryInfo
*TLI
) {
1274 if (!TLI
->has(LibFunc_malloc
))
1277 Module
*M
= B
.GetInsertBlock()->getModule();
1278 StringRef MallocName
= TLI
->getName(LibFunc_malloc
);
1279 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1280 FunctionCallee Malloc
= M
->getOrInsertFunction(MallocName
, B
.getInt8PtrTy(),
1281 DL
.getIntPtrType(Context
));
1282 inferLibFuncAttributes(M
, MallocName
, *TLI
);
1283 CallInst
*CI
= B
.CreateCall(Malloc
, Num
, MallocName
);
1285 if (const Function
*F
=
1286 dyn_cast
<Function
>(Malloc
.getCallee()->stripPointerCasts()))
1287 CI
->setCallingConv(F
->getCallingConv());
1292 Value
*llvm::emitCalloc(Value
*Num
, Value
*Size
, const AttributeList
&Attrs
,
1293 IRBuilder
<> &B
, const TargetLibraryInfo
&TLI
) {
1294 if (!TLI
.has(LibFunc_calloc
))
1297 Module
*M
= B
.GetInsertBlock()->getModule();
1298 StringRef CallocName
= TLI
.getName(LibFunc_calloc
);
1299 const DataLayout
&DL
= M
->getDataLayout();
1300 IntegerType
*PtrType
= DL
.getIntPtrType((B
.GetInsertBlock()->getContext()));
1301 FunctionCallee Calloc
= M
->getOrInsertFunction(
1302 CallocName
, Attrs
, B
.getInt8PtrTy(), PtrType
, PtrType
);
1303 inferLibFuncAttributes(M
, CallocName
, TLI
);
1304 CallInst
*CI
= B
.CreateCall(Calloc
, {Num
, Size
}, CallocName
);
1307 dyn_cast
<Function
>(Calloc
.getCallee()->stripPointerCasts()))
1308 CI
->setCallingConv(F
->getCallingConv());
1313 Value
*llvm::emitFWriteUnlocked(Value
*Ptr
, Value
*Size
, Value
*N
, Value
*File
,
1314 IRBuilder
<> &B
, const DataLayout
&DL
,
1315 const TargetLibraryInfo
*TLI
) {
1316 if (!TLI
->has(LibFunc_fwrite_unlocked
))
1319 Module
*M
= B
.GetInsertBlock()->getModule();
1320 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1321 StringRef FWriteUnlockedName
= TLI
->getName(LibFunc_fwrite_unlocked
);
1322 FunctionCallee F
= M
->getOrInsertFunction(
1323 FWriteUnlockedName
, DL
.getIntPtrType(Context
), B
.getInt8PtrTy(),
1324 DL
.getIntPtrType(Context
), DL
.getIntPtrType(Context
), File
->getType());
1326 if (File
->getType()->isPointerTy())
1327 inferLibFuncAttributes(M
, FWriteUnlockedName
, *TLI
);
1328 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Ptr
, B
), Size
, N
, File
});
1330 if (const Function
*Fn
=
1331 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1332 CI
->setCallingConv(Fn
->getCallingConv());
1336 Value
*llvm::emitFGetCUnlocked(Value
*File
, IRBuilder
<> &B
,
1337 const TargetLibraryInfo
*TLI
) {
1338 if (!TLI
->has(LibFunc_fgetc_unlocked
))
1341 Module
*M
= B
.GetInsertBlock()->getModule();
1342 StringRef FGetCUnlockedName
= TLI
->getName(LibFunc_fgetc_unlocked
);
1343 FunctionCallee F
= M
->getOrInsertFunction(FGetCUnlockedName
, B
.getInt32Ty(),
1345 if (File
->getType()->isPointerTy())
1346 inferLibFuncAttributes(M
, FGetCUnlockedName
, *TLI
);
1347 CallInst
*CI
= B
.CreateCall(F
, File
, FGetCUnlockedName
);
1349 if (const Function
*Fn
=
1350 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1351 CI
->setCallingConv(Fn
->getCallingConv());
1355 Value
*llvm::emitFGetSUnlocked(Value
*Str
, Value
*Size
, Value
*File
,
1356 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
) {
1357 if (!TLI
->has(LibFunc_fgets_unlocked
))
1360 Module
*M
= B
.GetInsertBlock()->getModule();
1361 StringRef FGetSUnlockedName
= TLI
->getName(LibFunc_fgets_unlocked
);
1363 M
->getOrInsertFunction(FGetSUnlockedName
, B
.getInt8PtrTy(),
1364 B
.getInt8PtrTy(), B
.getInt32Ty(), File
->getType());
1365 inferLibFuncAttributes(M
, FGetSUnlockedName
, *TLI
);
1367 B
.CreateCall(F
, {castToCStr(Str
, B
), Size
, File
}, FGetSUnlockedName
);
1369 if (const Function
*Fn
=
1370 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1371 CI
->setCallingConv(Fn
->getCallingConv());
1375 Value
*llvm::emitFReadUnlocked(Value
*Ptr
, Value
*Size
, Value
*N
, Value
*File
,
1376 IRBuilder
<> &B
, const DataLayout
&DL
,
1377 const TargetLibraryInfo
*TLI
) {
1378 if (!TLI
->has(LibFunc_fread_unlocked
))
1381 Module
*M
= B
.GetInsertBlock()->getModule();
1382 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1383 StringRef FReadUnlockedName
= TLI
->getName(LibFunc_fread_unlocked
);
1384 FunctionCallee F
= M
->getOrInsertFunction(
1385 FReadUnlockedName
, DL
.getIntPtrType(Context
), B
.getInt8PtrTy(),
1386 DL
.getIntPtrType(Context
), DL
.getIntPtrType(Context
), File
->getType());
1388 if (File
->getType()->isPointerTy())
1389 inferLibFuncAttributes(M
, FReadUnlockedName
, *TLI
);
1390 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Ptr
, B
), Size
, N
, File
});
1392 if (const Function
*Fn
=
1393 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1394 CI
->setCallingConv(Fn
->getCallingConv());