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(NumInaccessibleMemOnly
,
35 "Number of functions inferred as inaccessiblememonly");
36 STATISTIC(NumReadOnly
, "Number of functions inferred as readonly");
37 STATISTIC(NumArgMemOnly
, "Number of functions inferred as argmemonly");
38 STATISTIC(NumInaccessibleMemOrArgMemOnly
,
39 "Number of functions inferred as inaccessiblemem_or_argmemonly");
40 STATISTIC(NumNoUnwind
, "Number of functions inferred as nounwind");
41 STATISTIC(NumNoCapture
, "Number of arguments inferred as nocapture");
42 STATISTIC(NumWriteOnlyArg
, "Number of arguments inferred as writeonly");
43 STATISTIC(NumSExtArg
, "Number of arguments inferred as signext");
44 STATISTIC(NumReadOnlyArg
, "Number of arguments inferred as readonly");
45 STATISTIC(NumNoAlias
, "Number of function returns inferred as noalias");
46 STATISTIC(NumNoUndef
, "Number of function returns inferred as noundef returns");
47 STATISTIC(NumReturnedArg
, "Number of arguments inferred as returned");
48 STATISTIC(NumWillReturn
, "Number of functions inferred as willreturn");
50 static bool setDoesNotAccessMemory(Function
&F
) {
51 if (F
.doesNotAccessMemory())
53 F
.setDoesNotAccessMemory();
58 static bool setOnlyAccessesInaccessibleMemory(Function
&F
) {
59 if (F
.onlyAccessesInaccessibleMemory())
61 F
.setOnlyAccessesInaccessibleMemory();
62 ++NumInaccessibleMemOnly
;
66 static bool setOnlyReadsMemory(Function
&F
) {
67 if (F
.onlyReadsMemory())
69 F
.setOnlyReadsMemory();
74 static bool setOnlyAccessesArgMemory(Function
&F
) {
75 if (F
.onlyAccessesArgMemory())
77 F
.setOnlyAccessesArgMemory();
82 static bool setOnlyAccessesInaccessibleMemOrArgMem(Function
&F
) {
83 if (F
.onlyAccessesInaccessibleMemOrArgMem())
85 F
.setOnlyAccessesInaccessibleMemOrArgMem();
86 ++NumInaccessibleMemOrArgMemOnly
;
90 static bool setDoesNotThrow(Function
&F
) {
98 static bool setRetDoesNotAlias(Function
&F
) {
99 if (F
.hasRetAttribute(Attribute::NoAlias
))
101 F
.addRetAttr(Attribute::NoAlias
);
106 static bool setDoesNotCapture(Function
&F
, unsigned ArgNo
) {
107 if (F
.hasParamAttribute(ArgNo
, Attribute::NoCapture
))
109 F
.addParamAttr(ArgNo
, Attribute::NoCapture
);
114 static bool setDoesNotAlias(Function
&F
, unsigned ArgNo
) {
115 if (F
.hasParamAttribute(ArgNo
, Attribute::NoAlias
))
117 F
.addParamAttr(ArgNo
, Attribute::NoAlias
);
122 static bool setOnlyReadsMemory(Function
&F
, unsigned ArgNo
) {
123 if (F
.hasParamAttribute(ArgNo
, Attribute::ReadOnly
))
125 F
.addParamAttr(ArgNo
, Attribute::ReadOnly
);
130 static bool setOnlyWritesMemory(Function
&F
, unsigned ArgNo
) {
131 if (F
.hasParamAttribute(ArgNo
, Attribute::WriteOnly
))
133 F
.addParamAttr(ArgNo
, Attribute::WriteOnly
);
138 static bool setSignExtendedArg(Function
&F
, unsigned ArgNo
) {
139 if (F
.hasParamAttribute(ArgNo
, Attribute::SExt
))
141 F
.addParamAttr(ArgNo
, Attribute::SExt
);
146 static bool setRetNoUndef(Function
&F
) {
147 if (!F
.getReturnType()->isVoidTy() &&
148 !F
.hasRetAttribute(Attribute::NoUndef
)) {
149 F
.addRetAttr(Attribute::NoUndef
);
156 static bool setArgsNoUndef(Function
&F
) {
157 bool Changed
= false;
158 for (unsigned ArgNo
= 0; ArgNo
< F
.arg_size(); ++ArgNo
) {
159 if (!F
.hasParamAttribute(ArgNo
, Attribute::NoUndef
)) {
160 F
.addParamAttr(ArgNo
, Attribute::NoUndef
);
168 static bool setArgNoUndef(Function
&F
, unsigned ArgNo
) {
169 if (F
.hasParamAttribute(ArgNo
, Attribute::NoUndef
))
171 F
.addParamAttr(ArgNo
, Attribute::NoUndef
);
176 static bool setRetAndArgsNoUndef(Function
&F
) {
177 return setRetNoUndef(F
) | setArgsNoUndef(F
);
180 static bool setReturnedArg(Function
&F
, unsigned ArgNo
) {
181 if (F
.hasParamAttribute(ArgNo
, Attribute::Returned
))
183 F
.addParamAttr(ArgNo
, Attribute::Returned
);
188 static bool setNonLazyBind(Function
&F
) {
189 if (F
.hasFnAttribute(Attribute::NonLazyBind
))
191 F
.addFnAttr(Attribute::NonLazyBind
);
195 static bool setDoesNotFreeMemory(Function
&F
) {
196 if (F
.hasFnAttribute(Attribute::NoFree
))
198 F
.addFnAttr(Attribute::NoFree
);
202 static bool setWillReturn(Function
&F
) {
203 if (F
.hasFnAttribute(Attribute::WillReturn
))
205 F
.addFnAttr(Attribute::WillReturn
);
210 bool llvm::inferLibFuncAttributes(Module
*M
, StringRef Name
,
211 const TargetLibraryInfo
&TLI
) {
212 Function
*F
= M
->getFunction(Name
);
215 return inferLibFuncAttributes(*F
, TLI
);
218 bool llvm::inferLibFuncAttributes(Function
&F
, const TargetLibraryInfo
&TLI
) {
220 if (!(TLI
.getLibFunc(F
, TheLibFunc
) && TLI
.has(TheLibFunc
)))
223 bool Changed
= false;
225 if(!isLibFreeFunction(&F
, TheLibFunc
) && !isReallocLikeFn(&F
, &TLI
))
226 Changed
|= setDoesNotFreeMemory(F
);
228 if (F
.getParent() != nullptr && F
.getParent()->getRtLibUseGOT())
229 Changed
|= setNonLazyBind(F
);
231 switch (TheLibFunc
) {
234 Changed
|= setOnlyReadsMemory(F
);
235 Changed
|= setDoesNotThrow(F
);
236 Changed
|= setOnlyAccessesArgMemory(F
);
237 Changed
|= setWillReturn(F
);
238 Changed
|= setDoesNotCapture(F
, 0);
241 case LibFunc_strrchr
:
242 Changed
|= setOnlyAccessesArgMemory(F
);
243 Changed
|= setOnlyReadsMemory(F
);
244 Changed
|= setDoesNotThrow(F
);
245 Changed
|= setWillReturn(F
);
250 case LibFunc_strtoul
:
251 case LibFunc_strtoll
:
252 case LibFunc_strtold
:
253 case LibFunc_strtoull
:
254 Changed
|= setDoesNotThrow(F
);
255 Changed
|= setWillReturn(F
);
256 Changed
|= setDoesNotCapture(F
, 1);
257 Changed
|= setOnlyReadsMemory(F
, 0);
260 case LibFunc_strncat
:
261 Changed
|= setOnlyAccessesArgMemory(F
);
262 Changed
|= setDoesNotThrow(F
);
263 Changed
|= setWillReturn(F
);
264 Changed
|= setReturnedArg(F
, 0);
265 Changed
|= setDoesNotCapture(F
, 1);
266 Changed
|= setOnlyReadsMemory(F
, 1);
267 Changed
|= setDoesNotAlias(F
, 0);
268 Changed
|= setDoesNotAlias(F
, 1);
271 case LibFunc_strncpy
:
272 Changed
|= setReturnedArg(F
, 0);
275 case LibFunc_stpncpy
:
276 Changed
|= setOnlyAccessesArgMemory(F
);
277 Changed
|= setDoesNotThrow(F
);
278 Changed
|= setWillReturn(F
);
279 Changed
|= setDoesNotCapture(F
, 1);
280 Changed
|= setOnlyWritesMemory(F
, 0);
281 Changed
|= setOnlyReadsMemory(F
, 1);
282 Changed
|= setDoesNotAlias(F
, 0);
283 Changed
|= setDoesNotAlias(F
, 1);
285 case LibFunc_strxfrm
:
286 Changed
|= setDoesNotThrow(F
);
287 Changed
|= setWillReturn(F
);
288 Changed
|= setDoesNotCapture(F
, 0);
289 Changed
|= setDoesNotCapture(F
, 1);
290 Changed
|= setOnlyReadsMemory(F
, 1);
292 case LibFunc_strcmp
: // 0,1
293 case LibFunc_strspn
: // 0,1
294 case LibFunc_strncmp
: // 0,1
295 case LibFunc_strcspn
: // 0,1
296 Changed
|= setDoesNotThrow(F
);
297 Changed
|= setOnlyAccessesArgMemory(F
);
298 Changed
|= setWillReturn(F
);
299 Changed
|= setOnlyReadsMemory(F
);
300 Changed
|= setDoesNotCapture(F
, 0);
301 Changed
|= setDoesNotCapture(F
, 1);
303 case LibFunc_strcoll
:
304 case LibFunc_strcasecmp
: // 0,1
305 case LibFunc_strncasecmp
: //
306 // Those functions may depend on the locale, which may be accessed through
308 Changed
|= setOnlyReadsMemory(F
);
309 Changed
|= setDoesNotThrow(F
);
310 Changed
|= setWillReturn(F
);
311 Changed
|= setDoesNotCapture(F
, 0);
312 Changed
|= setDoesNotCapture(F
, 1);
315 case LibFunc_strpbrk
:
316 Changed
|= setOnlyAccessesArgMemory(F
);
317 Changed
|= setOnlyReadsMemory(F
);
318 Changed
|= setDoesNotThrow(F
);
319 Changed
|= setWillReturn(F
);
320 Changed
|= setDoesNotCapture(F
, 1);
323 case LibFunc_strtok_r
:
324 Changed
|= setDoesNotThrow(F
);
325 Changed
|= setWillReturn(F
);
326 Changed
|= setDoesNotCapture(F
, 1);
327 Changed
|= setOnlyReadsMemory(F
, 1);
330 Changed
|= setRetAndArgsNoUndef(F
);
331 Changed
|= setDoesNotThrow(F
);
332 Changed
|= setDoesNotCapture(F
, 0);
333 Changed
|= setOnlyReadsMemory(F
, 0);
336 case LibFunc_setvbuf
:
337 Changed
|= setRetAndArgsNoUndef(F
);
338 Changed
|= setDoesNotThrow(F
);
339 Changed
|= setDoesNotCapture(F
, 0);
341 case LibFunc_strndup
:
342 Changed
|= setArgNoUndef(F
, 1);
345 Changed
|= setOnlyAccessesInaccessibleMemOrArgMem(F
);
346 Changed
|= setDoesNotThrow(F
);
347 Changed
|= setRetDoesNotAlias(F
);
348 Changed
|= setWillReturn(F
);
349 Changed
|= setDoesNotCapture(F
, 0);
350 Changed
|= setOnlyReadsMemory(F
, 0);
353 case LibFunc_statvfs
:
354 Changed
|= setRetAndArgsNoUndef(F
);
355 Changed
|= setDoesNotThrow(F
);
356 Changed
|= setDoesNotCapture(F
, 0);
357 Changed
|= setDoesNotCapture(F
, 1);
358 Changed
|= setOnlyReadsMemory(F
, 0);
361 Changed
|= setRetAndArgsNoUndef(F
);
362 Changed
|= setDoesNotThrow(F
);
363 Changed
|= setDoesNotCapture(F
, 0);
364 Changed
|= setDoesNotCapture(F
, 1);
365 Changed
|= setOnlyReadsMemory(F
, 0);
366 Changed
|= setOnlyReadsMemory(F
, 1);
368 case LibFunc_sprintf
:
369 Changed
|= setRetAndArgsNoUndef(F
);
370 Changed
|= setDoesNotThrow(F
);
371 Changed
|= setDoesNotCapture(F
, 0);
372 Changed
|= setDoesNotAlias(F
, 0);
373 Changed
|= setOnlyWritesMemory(F
, 0);
374 Changed
|= setDoesNotCapture(F
, 1);
375 Changed
|= setOnlyReadsMemory(F
, 1);
377 case LibFunc_snprintf
:
378 Changed
|= setRetAndArgsNoUndef(F
);
379 Changed
|= setDoesNotThrow(F
);
380 Changed
|= setDoesNotCapture(F
, 0);
381 Changed
|= setDoesNotAlias(F
, 0);
382 Changed
|= setOnlyWritesMemory(F
, 0);
383 Changed
|= setDoesNotCapture(F
, 2);
384 Changed
|= setOnlyReadsMemory(F
, 2);
386 case LibFunc_setitimer
:
387 Changed
|= setRetAndArgsNoUndef(F
);
388 Changed
|= setDoesNotThrow(F
);
389 Changed
|= setWillReturn(F
);
390 Changed
|= setDoesNotCapture(F
, 1);
391 Changed
|= setDoesNotCapture(F
, 2);
392 Changed
|= setOnlyReadsMemory(F
, 1);
395 // May throw; "system" is a valid pthread cancellation point.
396 Changed
|= setRetAndArgsNoUndef(F
);
397 Changed
|= setDoesNotCapture(F
, 0);
398 Changed
|= setOnlyReadsMemory(F
, 0);
401 case LibFunc_vec_malloc
:
402 Changed
|= setOnlyAccessesInaccessibleMemory(F
);
403 Changed
|= setRetAndArgsNoUndef(F
);
404 Changed
|= setDoesNotThrow(F
);
405 Changed
|= setRetDoesNotAlias(F
);
406 Changed
|= setWillReturn(F
);
409 Changed
|= setOnlyAccessesArgMemory(F
);
410 Changed
|= setOnlyReadsMemory(F
);
411 Changed
|= setDoesNotThrow(F
);
412 Changed
|= setWillReturn(F
);
413 Changed
|= setDoesNotCapture(F
, 0);
414 Changed
|= setDoesNotCapture(F
, 1);
417 case LibFunc_memrchr
:
418 Changed
|= setDoesNotThrow(F
);
419 Changed
|= setOnlyAccessesArgMemory(F
);
420 Changed
|= setOnlyReadsMemory(F
);
421 Changed
|= setWillReturn(F
);
426 Changed
|= setDoesNotThrow(F
);
427 Changed
|= setWillReturn(F
);
428 Changed
|= setDoesNotCapture(F
, 1);
431 Changed
|= setDoesNotThrow(F
);
432 Changed
|= setOnlyAccessesArgMemory(F
);
433 Changed
|= setWillReturn(F
);
434 Changed
|= setDoesNotAlias(F
, 0);
435 Changed
|= setReturnedArg(F
, 0);
436 Changed
|= setOnlyWritesMemory(F
, 0);
437 Changed
|= setDoesNotAlias(F
, 1);
438 Changed
|= setDoesNotCapture(F
, 1);
439 Changed
|= setOnlyReadsMemory(F
, 1);
441 case LibFunc_memmove
:
442 Changed
|= setDoesNotThrow(F
);
443 Changed
|= setOnlyAccessesArgMemory(F
);
444 Changed
|= setWillReturn(F
);
445 Changed
|= setReturnedArg(F
, 0);
446 Changed
|= setOnlyWritesMemory(F
, 0);
447 Changed
|= setDoesNotCapture(F
, 1);
448 Changed
|= setOnlyReadsMemory(F
, 1);
450 case LibFunc_mempcpy
:
451 case LibFunc_memccpy
:
452 Changed
|= setDoesNotThrow(F
);
453 Changed
|= setOnlyAccessesArgMemory(F
);
454 Changed
|= setWillReturn(F
);
455 Changed
|= setDoesNotAlias(F
, 0);
456 Changed
|= setOnlyWritesMemory(F
, 0);
457 Changed
|= setDoesNotAlias(F
, 1);
458 Changed
|= setDoesNotCapture(F
, 1);
459 Changed
|= setOnlyReadsMemory(F
, 1);
461 case LibFunc_memcpy_chk
:
462 Changed
|= setDoesNotThrow(F
);
464 case LibFunc_memalign
:
465 Changed
|= setOnlyAccessesInaccessibleMemory(F
);
466 Changed
|= setRetNoUndef(F
);
467 Changed
|= setDoesNotThrow(F
);
468 Changed
|= setRetDoesNotAlias(F
);
469 Changed
|= setWillReturn(F
);
472 Changed
|= setRetAndArgsNoUndef(F
);
473 Changed
|= setDoesNotThrow(F
);
474 Changed
|= setDoesNotCapture(F
, 0);
475 Changed
|= setOnlyReadsMemory(F
, 0);
478 Changed
|= setRetAndArgsNoUndef(F
);
479 Changed
|= setDoesNotThrow(F
);
480 Changed
|= setWillReturn(F
);
481 Changed
|= setDoesNotCapture(F
, 0);
483 case LibFunc_realloc
:
484 case LibFunc_vec_realloc
:
485 Changed
|= setOnlyAccessesInaccessibleMemOrArgMem(F
);
486 Changed
|= setRetNoUndef(F
);
487 Changed
|= setDoesNotThrow(F
);
488 Changed
|= setRetDoesNotAlias(F
);
489 Changed
|= setWillReturn(F
);
490 Changed
|= setDoesNotCapture(F
, 0);
491 Changed
|= setArgNoUndef(F
, 1);
493 case LibFunc_reallocf
:
494 Changed
|= setRetNoUndef(F
);
495 Changed
|= setWillReturn(F
);
496 Changed
|= setArgNoUndef(F
, 1);
499 // May throw; "read" is a valid pthread cancellation point.
500 Changed
|= setRetAndArgsNoUndef(F
);
501 Changed
|= setDoesNotCapture(F
, 1);
504 Changed
|= setRetAndArgsNoUndef(F
);
505 Changed
|= setDoesNotThrow(F
);
506 Changed
|= setDoesNotCapture(F
, 0);
510 case LibFunc_realpath
:
511 Changed
|= setRetAndArgsNoUndef(F
);
512 Changed
|= setDoesNotThrow(F
);
513 Changed
|= setDoesNotCapture(F
, 0);
514 Changed
|= setOnlyReadsMemory(F
, 0);
517 Changed
|= setRetAndArgsNoUndef(F
);
518 Changed
|= setDoesNotThrow(F
);
519 Changed
|= setDoesNotCapture(F
, 0);
520 Changed
|= setDoesNotCapture(F
, 1);
521 Changed
|= setOnlyReadsMemory(F
, 0);
522 Changed
|= setOnlyReadsMemory(F
, 1);
524 case LibFunc_readlink
:
525 Changed
|= setRetAndArgsNoUndef(F
);
526 Changed
|= setDoesNotThrow(F
);
527 Changed
|= setDoesNotCapture(F
, 0);
528 Changed
|= setDoesNotCapture(F
, 1);
529 Changed
|= setOnlyReadsMemory(F
, 0);
532 // May throw; "write" is a valid pthread cancellation point.
533 Changed
|= setRetAndArgsNoUndef(F
);
534 Changed
|= setDoesNotCapture(F
, 1);
535 Changed
|= setOnlyReadsMemory(F
, 1);
537 case LibFunc_aligned_alloc
:
538 Changed
|= setOnlyAccessesInaccessibleMemory(F
);
539 Changed
|= setRetAndArgsNoUndef(F
);
540 Changed
|= setDoesNotThrow(F
);
541 Changed
|= setRetDoesNotAlias(F
);
542 Changed
|= setWillReturn(F
);
545 Changed
|= setDoesNotThrow(F
);
546 Changed
|= setOnlyAccessesArgMemory(F
);
547 Changed
|= setWillReturn(F
);
548 Changed
|= setDoesNotCapture(F
, 0);
549 Changed
|= setOnlyReadsMemory(F
, 0);
550 Changed
|= setOnlyWritesMemory(F
, 1);
551 Changed
|= setDoesNotCapture(F
, 1);
554 Changed
|= setDoesNotThrow(F
);
555 Changed
|= setOnlyAccessesArgMemory(F
);
556 Changed
|= setOnlyReadsMemory(F
);
557 Changed
|= setWillReturn(F
);
558 Changed
|= setDoesNotCapture(F
, 0);
559 Changed
|= setDoesNotCapture(F
, 1);
562 Changed
|= setDoesNotThrow(F
);
563 Changed
|= setOnlyAccessesArgMemory(F
);
564 Changed
|= setWillReturn(F
);
565 Changed
|= setDoesNotCapture(F
, 0);
566 Changed
|= setOnlyWritesMemory(F
, 0);
569 case LibFunc_vec_calloc
:
570 Changed
|= setRetAndArgsNoUndef(F
);
571 Changed
|= setDoesNotThrow(F
);
572 Changed
|= setRetDoesNotAlias(F
);
573 Changed
|= setWillReturn(F
);
577 Changed
|= setRetAndArgsNoUndef(F
);
578 Changed
|= setDoesNotThrow(F
);
579 Changed
|= setDoesNotCapture(F
, 0);
580 Changed
|= setOnlyReadsMemory(F
, 0);
582 case LibFunc_ctermid
:
583 case LibFunc_clearerr
:
584 case LibFunc_closedir
:
585 Changed
|= setRetAndArgsNoUndef(F
);
586 Changed
|= setDoesNotThrow(F
);
587 Changed
|= setDoesNotCapture(F
, 0);
593 Changed
|= setDoesNotThrow(F
);
594 Changed
|= setOnlyReadsMemory(F
);
595 Changed
|= setWillReturn(F
);
596 Changed
|= setDoesNotCapture(F
, 0);
599 Changed
|= setRetAndArgsNoUndef(F
);
600 Changed
|= setDoesNotThrow(F
);
601 Changed
|= setDoesNotCapture(F
, 0);
602 Changed
|= setOnlyReadsMemory(F
, 0);
605 Changed
|= setRetAndArgsNoUndef(F
);
606 Changed
|= setDoesNotThrow(F
);
607 Changed
|= setRetDoesNotAlias(F
);
608 Changed
|= setDoesNotCapture(F
, 0);
609 Changed
|= setDoesNotCapture(F
, 1);
610 Changed
|= setOnlyReadsMemory(F
, 0);
611 Changed
|= setOnlyReadsMemory(F
, 1);
614 Changed
|= setRetAndArgsNoUndef(F
);
615 Changed
|= setDoesNotThrow(F
);
616 Changed
|= setRetDoesNotAlias(F
);
617 Changed
|= setDoesNotCapture(F
, 1);
618 Changed
|= setOnlyReadsMemory(F
, 1);
621 Changed
|= setRetAndArgsNoUndef(F
);
622 Changed
|= setDoesNotThrow(F
);
623 Changed
|= setDoesNotCapture(F
, 0);
626 case LibFunc_vec_free
:
627 Changed
|= setOnlyAccessesInaccessibleMemOrArgMem(F
);
628 Changed
|= setArgsNoUndef(F
);
629 Changed
|= setDoesNotThrow(F
);
630 Changed
|= setWillReturn(F
);
631 Changed
|= setDoesNotCapture(F
, 0);
636 case LibFunc_fgetc_unlocked
:
642 case LibFunc_fsetpos
:
643 case LibFunc_flockfile
:
644 case LibFunc_funlockfile
:
645 case LibFunc_ftrylockfile
:
646 Changed
|= setRetAndArgsNoUndef(F
);
647 Changed
|= setDoesNotThrow(F
);
648 Changed
|= setDoesNotCapture(F
, 0);
651 Changed
|= setRetAndArgsNoUndef(F
);
652 Changed
|= setDoesNotThrow(F
);
653 Changed
|= setDoesNotCapture(F
, 0);
654 Changed
|= setOnlyReadsMemory(F
);
657 case LibFunc_fputc_unlocked
:
659 Changed
|= setRetAndArgsNoUndef(F
);
660 Changed
|= setDoesNotThrow(F
);
661 Changed
|= setDoesNotCapture(F
, 1);
666 Changed
|= setDoesNotThrow(F
);
667 Changed
|= setWillReturn(F
);
668 Changed
|= setDoesNotCapture(F
, 1);
670 case LibFunc_fstatvfs
:
671 Changed
|= setRetAndArgsNoUndef(F
);
672 Changed
|= setDoesNotThrow(F
);
673 Changed
|= setDoesNotCapture(F
, 1);
676 case LibFunc_fgets_unlocked
:
677 Changed
|= setRetAndArgsNoUndef(F
);
678 Changed
|= setDoesNotThrow(F
);
679 Changed
|= setDoesNotCapture(F
, 2);
682 case LibFunc_fread_unlocked
:
683 Changed
|= setRetAndArgsNoUndef(F
);
684 Changed
|= setDoesNotThrow(F
);
685 Changed
|= setDoesNotCapture(F
, 0);
686 Changed
|= setDoesNotCapture(F
, 3);
689 case LibFunc_fwrite_unlocked
:
690 Changed
|= setRetAndArgsNoUndef(F
);
691 Changed
|= setDoesNotThrow(F
);
692 Changed
|= setDoesNotCapture(F
, 0);
693 Changed
|= setDoesNotCapture(F
, 3);
694 // FIXME: readonly #1?
697 case LibFunc_fputs_unlocked
:
698 Changed
|= setRetAndArgsNoUndef(F
);
699 Changed
|= setDoesNotThrow(F
);
700 Changed
|= setDoesNotCapture(F
, 0);
701 Changed
|= setDoesNotCapture(F
, 1);
702 Changed
|= setOnlyReadsMemory(F
, 0);
705 case LibFunc_fprintf
:
706 Changed
|= setRetAndArgsNoUndef(F
);
707 Changed
|= setDoesNotThrow(F
);
708 Changed
|= setDoesNotCapture(F
, 0);
709 Changed
|= setDoesNotCapture(F
, 1);
710 Changed
|= setOnlyReadsMemory(F
, 1);
712 case LibFunc_fgetpos
:
713 Changed
|= setRetAndArgsNoUndef(F
);
714 Changed
|= setDoesNotThrow(F
);
715 Changed
|= setDoesNotCapture(F
, 0);
716 Changed
|= setDoesNotCapture(F
, 1);
719 Changed
|= setRetAndArgsNoUndef(F
);
720 Changed
|= setDoesNotThrow(F
);
721 Changed
|= setDoesNotCapture(F
, 0);
723 case LibFunc_getlogin_r
:
724 Changed
|= setRetAndArgsNoUndef(F
);
725 Changed
|= setDoesNotThrow(F
);
726 Changed
|= setDoesNotCapture(F
, 0);
728 case LibFunc_getc_unlocked
:
729 Changed
|= setRetAndArgsNoUndef(F
);
730 Changed
|= setDoesNotThrow(F
);
731 Changed
|= setDoesNotCapture(F
, 0);
734 Changed
|= setRetAndArgsNoUndef(F
);
735 Changed
|= setDoesNotThrow(F
);
736 Changed
|= setOnlyReadsMemory(F
);
737 Changed
|= setDoesNotCapture(F
, 0);
740 case LibFunc_getchar
:
741 case LibFunc_getchar_unlocked
:
742 Changed
|= setRetAndArgsNoUndef(F
);
743 Changed
|= setDoesNotThrow(F
);
745 case LibFunc_getitimer
:
746 Changed
|= setRetAndArgsNoUndef(F
);
747 Changed
|= setDoesNotThrow(F
);
748 Changed
|= setDoesNotCapture(F
, 1);
750 case LibFunc_getpwnam
:
751 Changed
|= setRetAndArgsNoUndef(F
);
752 Changed
|= setDoesNotThrow(F
);
753 Changed
|= setDoesNotCapture(F
, 0);
754 Changed
|= setOnlyReadsMemory(F
, 0);
757 Changed
|= setRetAndArgsNoUndef(F
);
758 Changed
|= setDoesNotThrow(F
);
759 Changed
|= setDoesNotCapture(F
, 1);
762 Changed
|= setRetAndArgsNoUndef(F
);
763 Changed
|= setDoesNotThrow(F
);
764 Changed
|= setDoesNotCapture(F
, 0);
767 Changed
|= setRetAndArgsNoUndef(F
);
768 Changed
|= setDoesNotThrow(F
);
769 Changed
|= setDoesNotCapture(F
, 0);
770 Changed
|= setOnlyReadsMemory(F
, 0);
772 case LibFunc_unsetenv
:
773 Changed
|= setRetAndArgsNoUndef(F
);
774 Changed
|= setDoesNotThrow(F
);
775 Changed
|= setDoesNotCapture(F
, 0);
776 Changed
|= setOnlyReadsMemory(F
, 0);
780 Changed
|= setRetAndArgsNoUndef(F
);
781 Changed
|= setDoesNotThrow(F
);
782 Changed
|= setDoesNotCapture(F
, 0);
783 Changed
|= setDoesNotCapture(F
, 1);
784 Changed
|= setOnlyReadsMemory(F
, 0);
785 Changed
|= setOnlyReadsMemory(F
, 1);
788 case LibFunc_putc_unlocked
:
789 Changed
|= setRetAndArgsNoUndef(F
);
790 Changed
|= setDoesNotThrow(F
);
791 Changed
|= setDoesNotCapture(F
, 1);
796 Changed
|= setRetAndArgsNoUndef(F
);
797 Changed
|= setDoesNotThrow(F
);
798 Changed
|= setDoesNotCapture(F
, 0);
799 Changed
|= setOnlyReadsMemory(F
, 0);
802 // May throw; "pread" is a valid pthread cancellation point.
803 Changed
|= setRetAndArgsNoUndef(F
);
804 Changed
|= setDoesNotCapture(F
, 1);
807 // May throw; "pwrite" is a valid pthread cancellation point.
808 Changed
|= setRetAndArgsNoUndef(F
);
809 Changed
|= setDoesNotCapture(F
, 1);
810 Changed
|= setOnlyReadsMemory(F
, 1);
812 case LibFunc_putchar
:
813 case LibFunc_putchar_unlocked
:
814 Changed
|= setRetAndArgsNoUndef(F
);
815 Changed
|= setDoesNotThrow(F
);
818 Changed
|= setRetAndArgsNoUndef(F
);
819 Changed
|= setDoesNotThrow(F
);
820 Changed
|= setRetDoesNotAlias(F
);
821 Changed
|= setDoesNotCapture(F
, 0);
822 Changed
|= setDoesNotCapture(F
, 1);
823 Changed
|= setOnlyReadsMemory(F
, 0);
824 Changed
|= setOnlyReadsMemory(F
, 1);
827 Changed
|= setRetAndArgsNoUndef(F
);
828 Changed
|= setDoesNotThrow(F
);
829 Changed
|= setDoesNotCapture(F
, 0);
832 Changed
|= setRetAndArgsNoUndef(F
);
833 Changed
|= setDoesNotThrow(F
);
834 Changed
|= setDoesNotCapture(F
, 0);
835 Changed
|= setOnlyReadsMemory(F
, 0);
837 case LibFunc_vsscanf
:
838 Changed
|= setRetAndArgsNoUndef(F
);
839 Changed
|= setDoesNotThrow(F
);
840 Changed
|= setDoesNotCapture(F
, 0);
841 Changed
|= setDoesNotCapture(F
, 1);
842 Changed
|= setOnlyReadsMemory(F
, 0);
843 Changed
|= setOnlyReadsMemory(F
, 1);
845 case LibFunc_vfscanf
:
846 Changed
|= setRetAndArgsNoUndef(F
);
847 Changed
|= setDoesNotThrow(F
);
848 Changed
|= setDoesNotCapture(F
, 0);
849 Changed
|= setDoesNotCapture(F
, 1);
850 Changed
|= setOnlyReadsMemory(F
, 1);
853 Changed
|= setOnlyAccessesInaccessibleMemory(F
);
854 Changed
|= setRetAndArgsNoUndef(F
);
855 Changed
|= setDoesNotThrow(F
);
856 Changed
|= setRetDoesNotAlias(F
);
857 Changed
|= setWillReturn(F
);
859 case LibFunc_vprintf
:
860 Changed
|= setRetAndArgsNoUndef(F
);
861 Changed
|= setDoesNotThrow(F
);
862 Changed
|= setDoesNotCapture(F
, 0);
863 Changed
|= setOnlyReadsMemory(F
, 0);
865 case LibFunc_vfprintf
:
866 case LibFunc_vsprintf
:
867 Changed
|= setRetAndArgsNoUndef(F
);
868 Changed
|= setDoesNotThrow(F
);
869 Changed
|= setDoesNotCapture(F
, 0);
870 Changed
|= setDoesNotCapture(F
, 1);
871 Changed
|= setOnlyReadsMemory(F
, 1);
873 case LibFunc_vsnprintf
:
874 Changed
|= setRetAndArgsNoUndef(F
);
875 Changed
|= setDoesNotThrow(F
);
876 Changed
|= setDoesNotCapture(F
, 0);
877 Changed
|= setDoesNotCapture(F
, 2);
878 Changed
|= setOnlyReadsMemory(F
, 2);
881 // May throw; "open" is a valid pthread cancellation point.
882 Changed
|= setRetAndArgsNoUndef(F
);
883 Changed
|= setDoesNotCapture(F
, 0);
884 Changed
|= setOnlyReadsMemory(F
, 0);
886 case LibFunc_opendir
:
887 Changed
|= setRetAndArgsNoUndef(F
);
888 Changed
|= setDoesNotThrow(F
);
889 Changed
|= setRetDoesNotAlias(F
);
890 Changed
|= setDoesNotCapture(F
, 0);
891 Changed
|= setOnlyReadsMemory(F
, 0);
893 case LibFunc_tmpfile
:
894 Changed
|= setRetAndArgsNoUndef(F
);
895 Changed
|= setDoesNotThrow(F
);
896 Changed
|= setRetDoesNotAlias(F
);
899 Changed
|= setRetAndArgsNoUndef(F
);
900 Changed
|= setDoesNotThrow(F
);
901 Changed
|= setDoesNotCapture(F
, 0);
907 Changed
|= setDoesNotThrow(F
);
908 Changed
|= setDoesNotAccessMemory(F
);
911 Changed
|= setRetAndArgsNoUndef(F
);
912 Changed
|= setDoesNotThrow(F
);
913 Changed
|= setDoesNotCapture(F
, 0);
914 Changed
|= setDoesNotCapture(F
, 1);
915 Changed
|= setOnlyReadsMemory(F
, 0);
918 Changed
|= setRetAndArgsNoUndef(F
);
919 Changed
|= setDoesNotThrow(F
);
920 Changed
|= setDoesNotCapture(F
, 0);
921 Changed
|= setOnlyReadsMemory(F
, 0);
924 // May throw; places call through function pointer.
925 // Cannot give undef pointer/size
926 Changed
|= setRetAndArgsNoUndef(F
);
927 Changed
|= setDoesNotCapture(F
, 3);
929 case LibFunc_dunder_strndup
:
930 Changed
|= setArgNoUndef(F
, 1);
932 case LibFunc_dunder_strdup
:
933 Changed
|= setDoesNotThrow(F
);
934 Changed
|= setRetDoesNotAlias(F
);
935 Changed
|= setWillReturn(F
);
936 Changed
|= setDoesNotCapture(F
, 0);
937 Changed
|= setOnlyReadsMemory(F
, 0);
939 case LibFunc_dunder_strtok_r
:
940 Changed
|= setDoesNotThrow(F
);
941 Changed
|= setDoesNotCapture(F
, 1);
942 Changed
|= setOnlyReadsMemory(F
, 1);
944 case LibFunc_under_IO_getc
:
945 Changed
|= setRetAndArgsNoUndef(F
);
946 Changed
|= setDoesNotThrow(F
);
947 Changed
|= setDoesNotCapture(F
, 0);
949 case LibFunc_under_IO_putc
:
950 Changed
|= setRetAndArgsNoUndef(F
);
951 Changed
|= setDoesNotThrow(F
);
952 Changed
|= setDoesNotCapture(F
, 1);
954 case LibFunc_dunder_isoc99_scanf
:
955 Changed
|= setRetAndArgsNoUndef(F
);
956 Changed
|= setDoesNotThrow(F
);
957 Changed
|= setDoesNotCapture(F
, 0);
958 Changed
|= setOnlyReadsMemory(F
, 0);
961 case LibFunc_lstat64
:
962 case LibFunc_statvfs64
:
963 Changed
|= setRetAndArgsNoUndef(F
);
964 Changed
|= setDoesNotThrow(F
);
965 Changed
|= setDoesNotCapture(F
, 0);
966 Changed
|= setDoesNotCapture(F
, 1);
967 Changed
|= setOnlyReadsMemory(F
, 0);
969 case LibFunc_dunder_isoc99_sscanf
:
970 Changed
|= setRetAndArgsNoUndef(F
);
971 Changed
|= setDoesNotThrow(F
);
972 Changed
|= setDoesNotCapture(F
, 0);
973 Changed
|= setDoesNotCapture(F
, 1);
974 Changed
|= setOnlyReadsMemory(F
, 0);
975 Changed
|= setOnlyReadsMemory(F
, 1);
977 case LibFunc_fopen64
:
978 Changed
|= setRetAndArgsNoUndef(F
);
979 Changed
|= setDoesNotThrow(F
);
980 Changed
|= setRetDoesNotAlias(F
);
981 Changed
|= setDoesNotCapture(F
, 0);
982 Changed
|= setDoesNotCapture(F
, 1);
983 Changed
|= setOnlyReadsMemory(F
, 0);
984 Changed
|= setOnlyReadsMemory(F
, 1);
986 case LibFunc_fseeko64
:
987 case LibFunc_ftello64
:
988 Changed
|= setRetAndArgsNoUndef(F
);
989 Changed
|= setDoesNotThrow(F
);
990 Changed
|= setDoesNotCapture(F
, 0);
992 case LibFunc_tmpfile64
:
993 Changed
|= setRetAndArgsNoUndef(F
);
994 Changed
|= setDoesNotThrow(F
);
995 Changed
|= setRetDoesNotAlias(F
);
997 case LibFunc_fstat64
:
998 case LibFunc_fstatvfs64
:
999 Changed
|= setRetAndArgsNoUndef(F
);
1000 Changed
|= setDoesNotThrow(F
);
1001 Changed
|= setDoesNotCapture(F
, 1);
1003 case LibFunc_open64
:
1004 // May throw; "open" is a valid pthread cancellation point.
1005 Changed
|= setRetAndArgsNoUndef(F
);
1006 Changed
|= setDoesNotCapture(F
, 0);
1007 Changed
|= setOnlyReadsMemory(F
, 0);
1009 case LibFunc_gettimeofday
:
1010 // Currently some platforms have the restrict keyword on the arguments to
1011 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1013 Changed
|= setRetAndArgsNoUndef(F
);
1014 Changed
|= setDoesNotThrow(F
);
1015 Changed
|= setDoesNotCapture(F
, 0);
1016 Changed
|= setDoesNotCapture(F
, 1);
1018 // TODO: add LibFunc entries for:
1019 // case LibFunc_memset_pattern4:
1020 // case LibFunc_memset_pattern8:
1021 case LibFunc_memset_pattern16
:
1022 Changed
|= setOnlyAccessesArgMemory(F
);
1023 Changed
|= setDoesNotCapture(F
, 0);
1024 Changed
|= setOnlyWritesMemory(F
, 0);
1025 Changed
|= setDoesNotCapture(F
, 1);
1026 Changed
|= setOnlyReadsMemory(F
, 1);
1028 case LibFunc_memset
:
1029 Changed
|= setOnlyAccessesArgMemory(F
);
1030 Changed
|= setWillReturn(F
);
1031 Changed
|= setDoesNotThrow(F
);
1032 Changed
|= setOnlyWritesMemory(F
, 0);
1034 // int __nvvm_reflect(const char *)
1035 case LibFunc_nvvm_reflect
:
1036 Changed
|= setRetAndArgsNoUndef(F
);
1037 Changed
|= setDoesNotAccessMemory(F
);
1038 Changed
|= setDoesNotThrow(F
);
1041 case LibFunc_ldexpf
:
1042 case LibFunc_ldexpl
:
1043 Changed
|= setSignExtendedArg(F
, 1);
1044 Changed
|= setWillReturn(F
);
1050 case LibFunc_acoshf
:
1051 case LibFunc_acoshl
:
1056 case LibFunc_asinhf
:
1057 case LibFunc_asinhl
:
1061 case LibFunc_atan2f
:
1062 case LibFunc_atan2l
:
1065 case LibFunc_atanhf
:
1066 case LibFunc_atanhl
:
1074 case LibFunc_copysign
:
1075 case LibFunc_copysignf
:
1076 case LibFunc_copysignl
:
1084 case LibFunc_cospif
:
1092 case LibFunc_expm1f
:
1093 case LibFunc_expm1l
:
1101 case LibFunc_floorf
:
1102 case LibFunc_floorl
:
1115 case LibFunc_isascii
:
1116 case LibFunc_isdigit
:
1121 case LibFunc_log10f
:
1122 case LibFunc_log10l
:
1124 case LibFunc_log1pf
:
1125 case LibFunc_log1pl
:
1134 case LibFunc_nearbyint
:
1135 case LibFunc_nearbyintf
:
1136 case LibFunc_nearbyintl
:
1144 case LibFunc_roundf
:
1145 case LibFunc_roundl
:
1147 case LibFunc_sincospif_stret
:
1154 case LibFunc_sinpif
:
1158 case LibFunc_strnlen
:
1165 case LibFunc_toascii
:
1167 case LibFunc_truncf
:
1168 case LibFunc_truncl
:
1169 Changed
|= setDoesNotThrow(F
);
1170 Changed
|= setDoesNotFreeMemory(F
);
1171 Changed
|= setWillReturn(F
);
1174 // FIXME: It'd be really nice to cover all the library functions we're
1180 bool llvm::hasFloatFn(const TargetLibraryInfo
*TLI
, Type
*Ty
,
1181 LibFunc DoubleFn
, LibFunc FloatFn
, LibFunc LongDoubleFn
) {
1182 switch (Ty
->getTypeID()) {
1183 case Type::HalfTyID
:
1185 case Type::FloatTyID
:
1186 return TLI
->has(FloatFn
);
1187 case Type::DoubleTyID
:
1188 return TLI
->has(DoubleFn
);
1190 return TLI
->has(LongDoubleFn
);
1194 StringRef
llvm::getFloatFnName(const TargetLibraryInfo
*TLI
, Type
*Ty
,
1195 LibFunc DoubleFn
, LibFunc FloatFn
,
1196 LibFunc LongDoubleFn
) {
1197 assert(hasFloatFn(TLI
, Ty
, DoubleFn
, FloatFn
, LongDoubleFn
) &&
1198 "Cannot get name for unavailable function!");
1200 switch (Ty
->getTypeID()) {
1201 case Type::HalfTyID
:
1202 llvm_unreachable("No name for HalfTy!");
1203 case Type::FloatTyID
:
1204 return TLI
->getName(FloatFn
);
1205 case Type::DoubleTyID
:
1206 return TLI
->getName(DoubleFn
);
1208 return TLI
->getName(LongDoubleFn
);
1212 //- Emit LibCalls ------------------------------------------------------------//
1214 Value
*llvm::castToCStr(Value
*V
, IRBuilderBase
&B
) {
1215 unsigned AS
= V
->getType()->getPointerAddressSpace();
1216 return B
.CreateBitCast(V
, B
.getInt8PtrTy(AS
), "cstr");
1219 static Value
*emitLibCall(LibFunc TheLibFunc
, Type
*ReturnType
,
1220 ArrayRef
<Type
*> ParamTypes
,
1221 ArrayRef
<Value
*> Operands
, IRBuilderBase
&B
,
1222 const TargetLibraryInfo
*TLI
,
1223 bool IsVaArgs
= false) {
1224 if (!TLI
->has(TheLibFunc
))
1227 Module
*M
= B
.GetInsertBlock()->getModule();
1228 StringRef FuncName
= TLI
->getName(TheLibFunc
);
1229 FunctionType
*FuncType
= FunctionType::get(ReturnType
, ParamTypes
, IsVaArgs
);
1230 FunctionCallee Callee
= M
->getOrInsertFunction(FuncName
, FuncType
);
1231 inferLibFuncAttributes(M
, FuncName
, *TLI
);
1232 CallInst
*CI
= B
.CreateCall(Callee
, Operands
, FuncName
);
1233 if (const Function
*F
=
1234 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1235 CI
->setCallingConv(F
->getCallingConv());
1239 Value
*llvm::emitStrLen(Value
*Ptr
, IRBuilderBase
&B
, const DataLayout
&DL
,
1240 const TargetLibraryInfo
*TLI
) {
1241 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1242 return emitLibCall(LibFunc_strlen
, DL
.getIntPtrType(Context
),
1243 B
.getInt8PtrTy(), castToCStr(Ptr
, B
), B
, TLI
);
1246 Value
*llvm::emitStrDup(Value
*Ptr
, IRBuilderBase
&B
,
1247 const TargetLibraryInfo
*TLI
) {
1248 return emitLibCall(LibFunc_strdup
, B
.getInt8PtrTy(), B
.getInt8PtrTy(),
1249 castToCStr(Ptr
, B
), B
, TLI
);
1252 Value
*llvm::emitStrChr(Value
*Ptr
, char C
, IRBuilderBase
&B
,
1253 const TargetLibraryInfo
*TLI
) {
1254 Type
*I8Ptr
= B
.getInt8PtrTy();
1255 Type
*I32Ty
= B
.getInt32Ty();
1256 return emitLibCall(LibFunc_strchr
, I8Ptr
, {I8Ptr
, I32Ty
},
1257 {castToCStr(Ptr
, B
), ConstantInt::get(I32Ty
, C
)}, B
, TLI
);
1260 Value
*llvm::emitStrNCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilderBase
&B
,
1261 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1262 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1264 LibFunc_strncmp
, B
.getInt32Ty(),
1265 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
1266 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
1269 Value
*llvm::emitStrCpy(Value
*Dst
, Value
*Src
, IRBuilderBase
&B
,
1270 const TargetLibraryInfo
*TLI
) {
1271 Type
*I8Ptr
= B
.getInt8PtrTy();
1272 return emitLibCall(LibFunc_strcpy
, I8Ptr
, {I8Ptr
, I8Ptr
},
1273 {castToCStr(Dst
, B
), castToCStr(Src
, B
)}, B
, TLI
);
1276 Value
*llvm::emitStpCpy(Value
*Dst
, Value
*Src
, IRBuilderBase
&B
,
1277 const TargetLibraryInfo
*TLI
) {
1278 Type
*I8Ptr
= B
.getInt8PtrTy();
1279 return emitLibCall(LibFunc_stpcpy
, I8Ptr
, {I8Ptr
, I8Ptr
},
1280 {castToCStr(Dst
, B
), castToCStr(Src
, B
)}, B
, TLI
);
1283 Value
*llvm::emitStrNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilderBase
&B
,
1284 const TargetLibraryInfo
*TLI
) {
1285 Type
*I8Ptr
= B
.getInt8PtrTy();
1286 return emitLibCall(LibFunc_strncpy
, I8Ptr
, {I8Ptr
, I8Ptr
, Len
->getType()},
1287 {castToCStr(Dst
, B
), castToCStr(Src
, B
), Len
}, B
, TLI
);
1290 Value
*llvm::emitStpNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilderBase
&B
,
1291 const TargetLibraryInfo
*TLI
) {
1292 Type
*I8Ptr
= B
.getInt8PtrTy();
1293 return emitLibCall(LibFunc_stpncpy
, I8Ptr
, {I8Ptr
, I8Ptr
, Len
->getType()},
1294 {castToCStr(Dst
, B
), castToCStr(Src
, B
), Len
}, B
, TLI
);
1297 Value
*llvm::emitMemCpyChk(Value
*Dst
, Value
*Src
, Value
*Len
, Value
*ObjSize
,
1298 IRBuilderBase
&B
, const DataLayout
&DL
,
1299 const TargetLibraryInfo
*TLI
) {
1300 if (!TLI
->has(LibFunc_memcpy_chk
))
1303 Module
*M
= B
.GetInsertBlock()->getModule();
1305 AS
= AttributeList::get(M
->getContext(), AttributeList::FunctionIndex
,
1306 Attribute::NoUnwind
);
1307 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1308 FunctionCallee MemCpy
= M
->getOrInsertFunction(
1309 "__memcpy_chk", AttributeList::get(M
->getContext(), AS
), B
.getInt8PtrTy(),
1310 B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
),
1311 DL
.getIntPtrType(Context
));
1312 Dst
= castToCStr(Dst
, B
);
1313 Src
= castToCStr(Src
, B
);
1314 CallInst
*CI
= B
.CreateCall(MemCpy
, {Dst
, Src
, Len
, ObjSize
});
1315 if (const Function
*F
=
1316 dyn_cast
<Function
>(MemCpy
.getCallee()->stripPointerCasts()))
1317 CI
->setCallingConv(F
->getCallingConv());
1321 Value
*llvm::emitMemPCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilderBase
&B
,
1322 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1323 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1325 LibFunc_mempcpy
, B
.getInt8PtrTy(),
1326 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
1327 {Dst
, Src
, Len
}, B
, TLI
);
1330 Value
*llvm::emitMemChr(Value
*Ptr
, Value
*Val
, Value
*Len
, IRBuilderBase
&B
,
1331 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1332 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1334 LibFunc_memchr
, B
.getInt8PtrTy(),
1335 {B
.getInt8PtrTy(), B
.getInt32Ty(), DL
.getIntPtrType(Context
)},
1336 {castToCStr(Ptr
, B
), Val
, Len
}, B
, TLI
);
1339 Value
*llvm::emitMemCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilderBase
&B
,
1340 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1341 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1343 LibFunc_memcmp
, B
.getInt32Ty(),
1344 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
1345 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
1348 Value
*llvm::emitBCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilderBase
&B
,
1349 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1350 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1352 LibFunc_bcmp
, B
.getInt32Ty(),
1353 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
1354 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
1357 Value
*llvm::emitMemCCpy(Value
*Ptr1
, Value
*Ptr2
, Value
*Val
, Value
*Len
,
1358 IRBuilderBase
&B
, const TargetLibraryInfo
*TLI
) {
1360 LibFunc_memccpy
, B
.getInt8PtrTy(),
1361 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), B
.getInt32Ty(), Len
->getType()},
1362 {Ptr1
, Ptr2
, Val
, Len
}, B
, TLI
);
1365 Value
*llvm::emitSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
,
1366 ArrayRef
<Value
*> VariadicArgs
, IRBuilderBase
&B
,
1367 const TargetLibraryInfo
*TLI
) {
1368 SmallVector
<Value
*, 8> Args
{castToCStr(Dest
, B
), Size
, castToCStr(Fmt
, B
)};
1369 llvm::append_range(Args
, VariadicArgs
);
1370 return emitLibCall(LibFunc_snprintf
, B
.getInt32Ty(),
1371 {B
.getInt8PtrTy(), Size
->getType(), B
.getInt8PtrTy()},
1372 Args
, B
, TLI
, /*IsVaArgs=*/true);
1375 Value
*llvm::emitSPrintf(Value
*Dest
, Value
*Fmt
,
1376 ArrayRef
<Value
*> VariadicArgs
, IRBuilderBase
&B
,
1377 const TargetLibraryInfo
*TLI
) {
1378 SmallVector
<Value
*, 8> Args
{castToCStr(Dest
, B
), castToCStr(Fmt
, B
)};
1379 llvm::append_range(Args
, VariadicArgs
);
1380 return emitLibCall(LibFunc_sprintf
, B
.getInt32Ty(),
1381 {B
.getInt8PtrTy(), B
.getInt8PtrTy()}, Args
, B
, TLI
,
1385 Value
*llvm::emitStrCat(Value
*Dest
, Value
*Src
, IRBuilderBase
&B
,
1386 const TargetLibraryInfo
*TLI
) {
1387 return emitLibCall(LibFunc_strcat
, B
.getInt8PtrTy(),
1388 {B
.getInt8PtrTy(), B
.getInt8PtrTy()},
1389 {castToCStr(Dest
, B
), castToCStr(Src
, B
)}, B
, TLI
);
1392 Value
*llvm::emitStrLCpy(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilderBase
&B
,
1393 const TargetLibraryInfo
*TLI
) {
1394 return emitLibCall(LibFunc_strlcpy
, Size
->getType(),
1395 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
1396 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
1399 Value
*llvm::emitStrLCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilderBase
&B
,
1400 const TargetLibraryInfo
*TLI
) {
1401 return emitLibCall(LibFunc_strlcat
, Size
->getType(),
1402 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
1403 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
1406 Value
*llvm::emitStrNCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilderBase
&B
,
1407 const TargetLibraryInfo
*TLI
) {
1408 return emitLibCall(LibFunc_strncat
, B
.getInt8PtrTy(),
1409 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
1410 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
1413 Value
*llvm::emitVSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
, Value
*VAList
,
1414 IRBuilderBase
&B
, const TargetLibraryInfo
*TLI
) {
1416 LibFunc_vsnprintf
, B
.getInt32Ty(),
1417 {B
.getInt8PtrTy(), Size
->getType(), B
.getInt8PtrTy(), VAList
->getType()},
1418 {castToCStr(Dest
, B
), Size
, castToCStr(Fmt
, B
), VAList
}, B
, TLI
);
1421 Value
*llvm::emitVSPrintf(Value
*Dest
, Value
*Fmt
, Value
*VAList
,
1422 IRBuilderBase
&B
, const TargetLibraryInfo
*TLI
) {
1423 return emitLibCall(LibFunc_vsprintf
, B
.getInt32Ty(),
1424 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), VAList
->getType()},
1425 {castToCStr(Dest
, B
), castToCStr(Fmt
, B
), VAList
}, B
, TLI
);
1428 /// Append a suffix to the function name according to the type of 'Op'.
1429 static void appendTypeSuffix(Value
*Op
, StringRef
&Name
,
1430 SmallString
<20> &NameBuffer
) {
1431 if (!Op
->getType()->isDoubleTy()) {
1434 if (Op
->getType()->isFloatTy())
1443 static Value
*emitUnaryFloatFnCallHelper(Value
*Op
, StringRef Name
,
1445 const AttributeList
&Attrs
) {
1446 assert((Name
!= "") && "Must specify Name to emitUnaryFloatFnCall");
1448 Module
*M
= B
.GetInsertBlock()->getModule();
1449 FunctionCallee Callee
=
1450 M
->getOrInsertFunction(Name
, Op
->getType(), Op
->getType());
1451 CallInst
*CI
= B
.CreateCall(Callee
, Op
, Name
);
1453 // The incoming attribute set may have come from a speculatable intrinsic, but
1454 // is being replaced with a library call which is not allowed to be
1457 Attrs
.removeFnAttribute(B
.getContext(), Attribute::Speculatable
));
1458 if (const Function
*F
=
1459 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1460 CI
->setCallingConv(F
->getCallingConv());
1465 Value
*llvm::emitUnaryFloatFnCall(Value
*Op
, StringRef Name
, IRBuilderBase
&B
,
1466 const AttributeList
&Attrs
) {
1467 SmallString
<20> NameBuffer
;
1468 appendTypeSuffix(Op
, Name
, NameBuffer
);
1470 return emitUnaryFloatFnCallHelper(Op
, Name
, B
, Attrs
);
1473 Value
*llvm::emitUnaryFloatFnCall(Value
*Op
, const TargetLibraryInfo
*TLI
,
1474 LibFunc DoubleFn
, LibFunc FloatFn
,
1475 LibFunc LongDoubleFn
, IRBuilderBase
&B
,
1476 const AttributeList
&Attrs
) {
1477 // Get the name of the function according to TLI.
1478 StringRef Name
= getFloatFnName(TLI
, Op
->getType(),
1479 DoubleFn
, FloatFn
, LongDoubleFn
);
1481 return emitUnaryFloatFnCallHelper(Op
, Name
, B
, Attrs
);
1484 static Value
*emitBinaryFloatFnCallHelper(Value
*Op1
, Value
*Op2
,
1485 StringRef Name
, IRBuilderBase
&B
,
1486 const AttributeList
&Attrs
,
1487 const TargetLibraryInfo
*TLI
= nullptr) {
1488 assert((Name
!= "") && "Must specify Name to emitBinaryFloatFnCall");
1490 Module
*M
= B
.GetInsertBlock()->getModule();
1491 FunctionCallee Callee
= M
->getOrInsertFunction(Name
, Op1
->getType(),
1492 Op1
->getType(), Op2
->getType());
1494 inferLibFuncAttributes(M
, Name
, *TLI
);
1495 CallInst
*CI
= B
.CreateCall(Callee
, { Op1
, Op2
}, Name
);
1497 // The incoming attribute set may have come from a speculatable intrinsic, but
1498 // is being replaced with a library call which is not allowed to be
1501 Attrs
.removeFnAttribute(B
.getContext(), Attribute::Speculatable
));
1502 if (const Function
*F
=
1503 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1504 CI
->setCallingConv(F
->getCallingConv());
1509 Value
*llvm::emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
, StringRef Name
,
1511 const AttributeList
&Attrs
) {
1512 assert((Name
!= "") && "Must specify Name to emitBinaryFloatFnCall");
1514 SmallString
<20> NameBuffer
;
1515 appendTypeSuffix(Op1
, Name
, NameBuffer
);
1517 return emitBinaryFloatFnCallHelper(Op1
, Op2
, Name
, B
, Attrs
);
1520 Value
*llvm::emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
,
1521 const TargetLibraryInfo
*TLI
,
1522 LibFunc DoubleFn
, LibFunc FloatFn
,
1523 LibFunc LongDoubleFn
, IRBuilderBase
&B
,
1524 const AttributeList
&Attrs
) {
1525 // Get the name of the function according to TLI.
1526 StringRef Name
= getFloatFnName(TLI
, Op1
->getType(),
1527 DoubleFn
, FloatFn
, LongDoubleFn
);
1529 return emitBinaryFloatFnCallHelper(Op1
, Op2
, Name
, B
, Attrs
, TLI
);
1532 Value
*llvm::emitPutChar(Value
*Char
, IRBuilderBase
&B
,
1533 const TargetLibraryInfo
*TLI
) {
1534 if (!TLI
->has(LibFunc_putchar
))
1537 Module
*M
= B
.GetInsertBlock()->getModule();
1538 StringRef PutCharName
= TLI
->getName(LibFunc_putchar
);
1539 FunctionCallee PutChar
=
1540 M
->getOrInsertFunction(PutCharName
, B
.getInt32Ty(), B
.getInt32Ty());
1541 inferLibFuncAttributes(M
, PutCharName
, *TLI
);
1542 CallInst
*CI
= B
.CreateCall(PutChar
,
1543 B
.CreateIntCast(Char
,
1549 if (const Function
*F
=
1550 dyn_cast
<Function
>(PutChar
.getCallee()->stripPointerCasts()))
1551 CI
->setCallingConv(F
->getCallingConv());
1555 Value
*llvm::emitPutS(Value
*Str
, IRBuilderBase
&B
,
1556 const TargetLibraryInfo
*TLI
) {
1557 if (!TLI
->has(LibFunc_puts
))
1560 Module
*M
= B
.GetInsertBlock()->getModule();
1561 StringRef PutsName
= TLI
->getName(LibFunc_puts
);
1562 FunctionCallee PutS
=
1563 M
->getOrInsertFunction(PutsName
, B
.getInt32Ty(), B
.getInt8PtrTy());
1564 inferLibFuncAttributes(M
, PutsName
, *TLI
);
1565 CallInst
*CI
= B
.CreateCall(PutS
, castToCStr(Str
, B
), PutsName
);
1566 if (const Function
*F
=
1567 dyn_cast
<Function
>(PutS
.getCallee()->stripPointerCasts()))
1568 CI
->setCallingConv(F
->getCallingConv());
1572 Value
*llvm::emitFPutC(Value
*Char
, Value
*File
, IRBuilderBase
&B
,
1573 const TargetLibraryInfo
*TLI
) {
1574 if (!TLI
->has(LibFunc_fputc
))
1577 Module
*M
= B
.GetInsertBlock()->getModule();
1578 StringRef FPutcName
= TLI
->getName(LibFunc_fputc
);
1579 FunctionCallee F
= M
->getOrInsertFunction(FPutcName
, B
.getInt32Ty(),
1580 B
.getInt32Ty(), File
->getType());
1581 if (File
->getType()->isPointerTy())
1582 inferLibFuncAttributes(M
, FPutcName
, *TLI
);
1583 Char
= B
.CreateIntCast(Char
, B
.getInt32Ty(), /*isSigned*/true,
1585 CallInst
*CI
= B
.CreateCall(F
, {Char
, File
}, FPutcName
);
1587 if (const Function
*Fn
=
1588 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1589 CI
->setCallingConv(Fn
->getCallingConv());
1593 Value
*llvm::emitFPutS(Value
*Str
, Value
*File
, IRBuilderBase
&B
,
1594 const TargetLibraryInfo
*TLI
) {
1595 if (!TLI
->has(LibFunc_fputs
))
1598 Module
*M
= B
.GetInsertBlock()->getModule();
1599 StringRef FPutsName
= TLI
->getName(LibFunc_fputs
);
1600 FunctionCallee F
= M
->getOrInsertFunction(FPutsName
, B
.getInt32Ty(),
1601 B
.getInt8PtrTy(), File
->getType());
1602 if (File
->getType()->isPointerTy())
1603 inferLibFuncAttributes(M
, FPutsName
, *TLI
);
1604 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Str
, B
), File
}, FPutsName
);
1606 if (const Function
*Fn
=
1607 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1608 CI
->setCallingConv(Fn
->getCallingConv());
1612 Value
*llvm::emitFWrite(Value
*Ptr
, Value
*Size
, Value
*File
, IRBuilderBase
&B
,
1613 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1614 if (!TLI
->has(LibFunc_fwrite
))
1617 Module
*M
= B
.GetInsertBlock()->getModule();
1618 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1619 StringRef FWriteName
= TLI
->getName(LibFunc_fwrite
);
1620 FunctionCallee F
= M
->getOrInsertFunction(
1621 FWriteName
, DL
.getIntPtrType(Context
), B
.getInt8PtrTy(),
1622 DL
.getIntPtrType(Context
), DL
.getIntPtrType(Context
), File
->getType());
1624 if (File
->getType()->isPointerTy())
1625 inferLibFuncAttributes(M
, FWriteName
, *TLI
);
1627 B
.CreateCall(F
, {castToCStr(Ptr
, B
), Size
,
1628 ConstantInt::get(DL
.getIntPtrType(Context
), 1), File
});
1630 if (const Function
*Fn
=
1631 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1632 CI
->setCallingConv(Fn
->getCallingConv());
1636 Value
*llvm::emitMalloc(Value
*Num
, IRBuilderBase
&B
, const DataLayout
&DL
,
1637 const TargetLibraryInfo
*TLI
) {
1638 if (!TLI
->has(LibFunc_malloc
))
1641 Module
*M
= B
.GetInsertBlock()->getModule();
1642 StringRef MallocName
= TLI
->getName(LibFunc_malloc
);
1643 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1644 FunctionCallee Malloc
= M
->getOrInsertFunction(MallocName
, B
.getInt8PtrTy(),
1645 DL
.getIntPtrType(Context
));
1646 inferLibFuncAttributes(M
, MallocName
, *TLI
);
1647 CallInst
*CI
= B
.CreateCall(Malloc
, Num
, MallocName
);
1649 if (const Function
*F
=
1650 dyn_cast
<Function
>(Malloc
.getCallee()->stripPointerCasts()))
1651 CI
->setCallingConv(F
->getCallingConv());
1656 Value
*llvm::emitCalloc(Value
*Num
, Value
*Size
, IRBuilderBase
&B
,
1657 const TargetLibraryInfo
&TLI
) {
1658 if (!TLI
.has(LibFunc_calloc
))
1661 Module
*M
= B
.GetInsertBlock()->getModule();
1662 StringRef CallocName
= TLI
.getName(LibFunc_calloc
);
1663 const DataLayout
&DL
= M
->getDataLayout();
1664 IntegerType
*PtrType
= DL
.getIntPtrType((B
.GetInsertBlock()->getContext()));
1665 FunctionCallee Calloc
=
1666 M
->getOrInsertFunction(CallocName
, B
.getInt8PtrTy(), PtrType
, PtrType
);
1667 inferLibFuncAttributes(M
, CallocName
, TLI
);
1668 CallInst
*CI
= B
.CreateCall(Calloc
, {Num
, Size
}, CallocName
);
1671 dyn_cast
<Function
>(Calloc
.getCallee()->stripPointerCasts()))
1672 CI
->setCallingConv(F
->getCallingConv());