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/MemoryBuiltins.h"
17 #include "llvm/Analysis/TargetLibraryInfo.h"
18 #include "llvm/IR/Argument.h"
19 #include "llvm/IR/CallingConv.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/IRBuilder.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/Type.h"
26 #include "llvm/Support/TypeSize.h"
30 #define DEBUG_TYPE "build-libcalls"
32 //- Infer Attributes ---------------------------------------------------------//
34 STATISTIC(NumReadNone
, "Number of functions inferred as readnone");
35 STATISTIC(NumInaccessibleMemOnly
,
36 "Number of functions inferred as inaccessiblememonly");
37 STATISTIC(NumReadOnly
, "Number of functions inferred as readonly");
38 STATISTIC(NumWriteOnly
, "Number of functions inferred as writeonly");
39 STATISTIC(NumArgMemOnly
, "Number of functions inferred as argmemonly");
40 STATISTIC(NumInaccessibleMemOrArgMemOnly
,
41 "Number of functions inferred as inaccessiblemem_or_argmemonly");
42 STATISTIC(NumNoUnwind
, "Number of functions inferred as nounwind");
43 STATISTIC(NumNoCapture
, "Number of arguments inferred as nocapture");
44 STATISTIC(NumWriteOnlyArg
, "Number of arguments inferred as writeonly");
45 STATISTIC(NumReadOnlyArg
, "Number of arguments inferred as readonly");
46 STATISTIC(NumNoAlias
, "Number of function returns inferred as noalias");
47 STATISTIC(NumNoUndef
, "Number of function returns inferred as noundef returns");
48 STATISTIC(NumReturnedArg
, "Number of arguments inferred as returned");
49 STATISTIC(NumWillReturn
, "Number of functions inferred as willreturn");
51 static bool setDoesNotAccessMemory(Function
&F
) {
52 if (F
.doesNotAccessMemory())
54 F
.setDoesNotAccessMemory();
59 static bool setOnlyAccessesInaccessibleMemory(Function
&F
) {
60 if (F
.onlyAccessesInaccessibleMemory())
62 F
.setOnlyAccessesInaccessibleMemory();
63 ++NumInaccessibleMemOnly
;
67 static bool setOnlyReadsMemory(Function
&F
) {
68 if (F
.onlyReadsMemory())
70 F
.setOnlyReadsMemory();
75 static bool setOnlyWritesMemory(Function
&F
) {
76 if (F
.onlyWritesMemory()) // writeonly or readnone
78 // Turn readonly and writeonly into readnone.
79 if (F
.hasFnAttribute(Attribute::ReadOnly
)) {
80 F
.removeFnAttr(Attribute::ReadOnly
);
81 return setDoesNotAccessMemory(F
);
84 F
.setOnlyWritesMemory();
88 static bool setOnlyAccessesArgMemory(Function
&F
) {
89 if (F
.onlyAccessesArgMemory())
91 F
.setOnlyAccessesArgMemory();
96 static bool setOnlyAccessesInaccessibleMemOrArgMem(Function
&F
) {
97 if (F
.onlyAccessesInaccessibleMemOrArgMem())
99 F
.setOnlyAccessesInaccessibleMemOrArgMem();
100 ++NumInaccessibleMemOrArgMemOnly
;
104 static bool setDoesNotThrow(Function
&F
) {
105 if (F
.doesNotThrow())
112 static bool setRetDoesNotAlias(Function
&F
) {
113 if (F
.hasRetAttribute(Attribute::NoAlias
))
115 F
.addRetAttr(Attribute::NoAlias
);
120 static bool setDoesNotCapture(Function
&F
, unsigned ArgNo
) {
121 if (F
.hasParamAttribute(ArgNo
, Attribute::NoCapture
))
123 F
.addParamAttr(ArgNo
, Attribute::NoCapture
);
128 static bool setDoesNotAlias(Function
&F
, unsigned ArgNo
) {
129 if (F
.hasParamAttribute(ArgNo
, Attribute::NoAlias
))
131 F
.addParamAttr(ArgNo
, Attribute::NoAlias
);
136 static bool setOnlyReadsMemory(Function
&F
, unsigned ArgNo
) {
137 if (F
.hasParamAttribute(ArgNo
, Attribute::ReadOnly
))
139 F
.addParamAttr(ArgNo
, Attribute::ReadOnly
);
144 static bool setOnlyWritesMemory(Function
&F
, unsigned ArgNo
) {
145 if (F
.hasParamAttribute(ArgNo
, Attribute::WriteOnly
))
147 F
.addParamAttr(ArgNo
, Attribute::WriteOnly
);
152 static bool setRetNoUndef(Function
&F
) {
153 if (!F
.getReturnType()->isVoidTy() &&
154 !F
.hasRetAttribute(Attribute::NoUndef
)) {
155 F
.addRetAttr(Attribute::NoUndef
);
162 static bool setArgsNoUndef(Function
&F
) {
163 bool Changed
= false;
164 for (unsigned ArgNo
= 0; ArgNo
< F
.arg_size(); ++ArgNo
) {
165 if (!F
.hasParamAttribute(ArgNo
, Attribute::NoUndef
)) {
166 F
.addParamAttr(ArgNo
, Attribute::NoUndef
);
174 static bool setArgNoUndef(Function
&F
, unsigned ArgNo
) {
175 if (F
.hasParamAttribute(ArgNo
, Attribute::NoUndef
))
177 F
.addParamAttr(ArgNo
, Attribute::NoUndef
);
182 static bool setRetAndArgsNoUndef(Function
&F
) {
183 bool UndefAdded
= false;
184 UndefAdded
|= setRetNoUndef(F
);
185 UndefAdded
|= setArgsNoUndef(F
);
189 static bool setReturnedArg(Function
&F
, unsigned ArgNo
) {
190 if (F
.hasParamAttribute(ArgNo
, Attribute::Returned
))
192 F
.addParamAttr(ArgNo
, Attribute::Returned
);
197 static bool setNonLazyBind(Function
&F
) {
198 if (F
.hasFnAttribute(Attribute::NonLazyBind
))
200 F
.addFnAttr(Attribute::NonLazyBind
);
204 static bool setDoesNotFreeMemory(Function
&F
) {
205 if (F
.hasFnAttribute(Attribute::NoFree
))
207 F
.addFnAttr(Attribute::NoFree
);
211 static bool setWillReturn(Function
&F
) {
212 if (F
.hasFnAttribute(Attribute::WillReturn
))
214 F
.addFnAttr(Attribute::WillReturn
);
219 static bool setAlignedAllocParam(Function
&F
, unsigned ArgNo
) {
220 if (F
.hasParamAttribute(ArgNo
, Attribute::AllocAlign
))
222 F
.addParamAttr(ArgNo
, Attribute::AllocAlign
);
226 static bool setAllocatedPointerParam(Function
&F
, unsigned ArgNo
) {
227 if (F
.hasParamAttribute(ArgNo
, Attribute::AllocatedPointer
))
229 F
.addParamAttr(ArgNo
, Attribute::AllocatedPointer
);
233 static bool setAllocSize(Function
&F
, unsigned ElemSizeArg
,
234 Optional
<unsigned> NumElemsArg
) {
235 if (F
.hasFnAttribute(Attribute::AllocSize
))
237 F
.addFnAttr(Attribute::getWithAllocSizeArgs(F
.getContext(), ElemSizeArg
,
242 static bool setAllocFamily(Function
&F
, StringRef Family
) {
243 if (F
.hasFnAttribute("alloc-family"))
245 F
.addFnAttr("alloc-family", Family
);
249 static bool setAllocKind(Function
&F
, AllocFnKind K
) {
250 if (F
.hasFnAttribute(Attribute::AllocKind
))
253 Attribute::get(F
.getContext(), Attribute::AllocKind
, uint64_t(K
)));
257 bool llvm::inferNonMandatoryLibFuncAttrs(Module
*M
, StringRef Name
,
258 const TargetLibraryInfo
&TLI
) {
259 Function
*F
= M
->getFunction(Name
);
262 return inferNonMandatoryLibFuncAttrs(*F
, TLI
);
265 bool llvm::inferNonMandatoryLibFuncAttrs(Function
&F
,
266 const TargetLibraryInfo
&TLI
) {
268 if (!(TLI
.getLibFunc(F
, TheLibFunc
) && TLI
.has(TheLibFunc
)))
271 bool Changed
= false;
273 if (F
.getParent() != nullptr && F
.getParent()->getRtLibUseGOT())
274 Changed
|= setNonLazyBind(F
);
276 switch (TheLibFunc
) {
278 case LibFunc_strnlen
:
280 Changed
|= setOnlyReadsMemory(F
);
281 Changed
|= setDoesNotThrow(F
);
282 Changed
|= setOnlyAccessesArgMemory(F
);
283 Changed
|= setWillReturn(F
);
284 Changed
|= setDoesNotCapture(F
, 0);
287 case LibFunc_strrchr
:
288 Changed
|= setOnlyAccessesArgMemory(F
);
289 Changed
|= setOnlyReadsMemory(F
);
290 Changed
|= setDoesNotThrow(F
);
291 Changed
|= setWillReturn(F
);
296 case LibFunc_strtoul
:
297 case LibFunc_strtoll
:
298 case LibFunc_strtold
:
299 case LibFunc_strtoull
:
300 Changed
|= setDoesNotThrow(F
);
301 Changed
|= setWillReturn(F
);
302 Changed
|= setDoesNotCapture(F
, 1);
303 Changed
|= setOnlyReadsMemory(F
, 0);
306 case LibFunc_strncat
:
307 Changed
|= setOnlyAccessesArgMemory(F
);
308 Changed
|= setDoesNotThrow(F
);
309 Changed
|= setWillReturn(F
);
310 Changed
|= setReturnedArg(F
, 0);
311 Changed
|= setDoesNotCapture(F
, 1);
312 Changed
|= setOnlyReadsMemory(F
, 1);
313 Changed
|= setDoesNotAlias(F
, 0);
314 Changed
|= setDoesNotAlias(F
, 1);
317 case LibFunc_strncpy
:
318 Changed
|= setReturnedArg(F
, 0);
321 case LibFunc_stpncpy
:
322 Changed
|= setOnlyAccessesArgMemory(F
);
323 Changed
|= setDoesNotThrow(F
);
324 Changed
|= setWillReturn(F
);
325 Changed
|= setDoesNotCapture(F
, 1);
326 Changed
|= setOnlyWritesMemory(F
, 0);
327 Changed
|= setOnlyReadsMemory(F
, 1);
328 Changed
|= setDoesNotAlias(F
, 0);
329 Changed
|= setDoesNotAlias(F
, 1);
331 case LibFunc_strxfrm
:
332 Changed
|= setDoesNotThrow(F
);
333 Changed
|= setWillReturn(F
);
334 Changed
|= setDoesNotCapture(F
, 0);
335 Changed
|= setDoesNotCapture(F
, 1);
336 Changed
|= setOnlyReadsMemory(F
, 1);
338 case LibFunc_strcmp
: // 0,1
339 case LibFunc_strspn
: // 0,1
340 case LibFunc_strncmp
: // 0,1
341 case LibFunc_strcspn
: // 0,1
342 Changed
|= setDoesNotThrow(F
);
343 Changed
|= setOnlyAccessesArgMemory(F
);
344 Changed
|= setWillReturn(F
);
345 Changed
|= setOnlyReadsMemory(F
);
346 Changed
|= setDoesNotCapture(F
, 0);
347 Changed
|= setDoesNotCapture(F
, 1);
349 case LibFunc_strcoll
:
350 case LibFunc_strcasecmp
: // 0,1
351 case LibFunc_strncasecmp
: //
352 // Those functions may depend on the locale, which may be accessed through
354 Changed
|= setOnlyReadsMemory(F
);
355 Changed
|= setDoesNotThrow(F
);
356 Changed
|= setWillReturn(F
);
357 Changed
|= setDoesNotCapture(F
, 0);
358 Changed
|= setDoesNotCapture(F
, 1);
361 case LibFunc_strpbrk
:
362 Changed
|= setOnlyAccessesArgMemory(F
);
363 Changed
|= setOnlyReadsMemory(F
);
364 Changed
|= setDoesNotThrow(F
);
365 Changed
|= setWillReturn(F
);
366 Changed
|= setDoesNotCapture(F
, 1);
369 case LibFunc_strtok_r
:
370 Changed
|= setDoesNotThrow(F
);
371 Changed
|= setWillReturn(F
);
372 Changed
|= setDoesNotCapture(F
, 1);
373 Changed
|= setOnlyReadsMemory(F
, 1);
376 Changed
|= setRetAndArgsNoUndef(F
);
377 Changed
|= setDoesNotThrow(F
);
378 Changed
|= setDoesNotCapture(F
, 0);
379 Changed
|= setOnlyReadsMemory(F
, 0);
382 case LibFunc_setvbuf
:
383 Changed
|= setRetAndArgsNoUndef(F
);
384 Changed
|= setDoesNotThrow(F
);
385 Changed
|= setDoesNotCapture(F
, 0);
387 case LibFunc_strndup
:
388 Changed
|= setArgNoUndef(F
, 1);
391 Changed
|= setAllocFamily(F
, "malloc");
392 Changed
|= setOnlyAccessesInaccessibleMemOrArgMem(F
);
393 Changed
|= setDoesNotThrow(F
);
394 Changed
|= setRetDoesNotAlias(F
);
395 Changed
|= setWillReturn(F
);
396 Changed
|= setDoesNotCapture(F
, 0);
397 Changed
|= setOnlyReadsMemory(F
, 0);
400 case LibFunc_statvfs
:
401 Changed
|= setRetAndArgsNoUndef(F
);
402 Changed
|= setDoesNotThrow(F
);
403 Changed
|= setDoesNotCapture(F
, 0);
404 Changed
|= setDoesNotCapture(F
, 1);
405 Changed
|= setOnlyReadsMemory(F
, 0);
408 Changed
|= setRetAndArgsNoUndef(F
);
409 Changed
|= setDoesNotThrow(F
);
410 Changed
|= setDoesNotCapture(F
, 0);
411 Changed
|= setDoesNotCapture(F
, 1);
412 Changed
|= setOnlyReadsMemory(F
, 0);
413 Changed
|= setOnlyReadsMemory(F
, 1);
415 case LibFunc_sprintf
:
416 Changed
|= setRetAndArgsNoUndef(F
);
417 Changed
|= setDoesNotThrow(F
);
418 Changed
|= setDoesNotCapture(F
, 0);
419 Changed
|= setDoesNotAlias(F
, 0);
420 Changed
|= setOnlyWritesMemory(F
, 0);
421 Changed
|= setDoesNotCapture(F
, 1);
422 Changed
|= setOnlyReadsMemory(F
, 1);
424 case LibFunc_snprintf
:
425 Changed
|= setRetAndArgsNoUndef(F
);
426 Changed
|= setDoesNotThrow(F
);
427 Changed
|= setDoesNotCapture(F
, 0);
428 Changed
|= setDoesNotAlias(F
, 0);
429 Changed
|= setOnlyWritesMemory(F
, 0);
430 Changed
|= setDoesNotCapture(F
, 2);
431 Changed
|= setOnlyReadsMemory(F
, 2);
433 case LibFunc_setitimer
:
434 Changed
|= setRetAndArgsNoUndef(F
);
435 Changed
|= setDoesNotThrow(F
);
436 Changed
|= setWillReturn(F
);
437 Changed
|= setDoesNotCapture(F
, 1);
438 Changed
|= setDoesNotCapture(F
, 2);
439 Changed
|= setOnlyReadsMemory(F
, 1);
442 // May throw; "system" is a valid pthread cancellation point.
443 Changed
|= setRetAndArgsNoUndef(F
);
444 Changed
|= setDoesNotCapture(F
, 0);
445 Changed
|= setOnlyReadsMemory(F
, 0);
447 case LibFunc_aligned_alloc
:
448 Changed
|= setAlignedAllocParam(F
, 0);
449 Changed
|= setAllocSize(F
, 1, None
);
450 Changed
|= setAllocKind(F
, AllocFnKind::Alloc
| AllocFnKind::Uninitialized
| AllocFnKind::Aligned
);
454 case LibFunc_vec_malloc
:
455 Changed
|= setAllocFamily(F
, TheLibFunc
== LibFunc_vec_malloc
? "vec_malloc"
457 Changed
|= setAllocKind(F
, AllocFnKind::Alloc
| AllocFnKind::Uninitialized
);
458 Changed
|= setAllocSize(F
, 0, None
);
459 Changed
|= setOnlyAccessesInaccessibleMemory(F
);
460 Changed
|= setRetAndArgsNoUndef(F
);
461 Changed
|= setDoesNotThrow(F
);
462 Changed
|= setRetDoesNotAlias(F
);
463 Changed
|= setWillReturn(F
);
466 Changed
|= setOnlyAccessesArgMemory(F
);
467 Changed
|= setOnlyReadsMemory(F
);
468 Changed
|= setDoesNotThrow(F
);
469 Changed
|= setWillReturn(F
);
470 Changed
|= setDoesNotCapture(F
, 0);
471 Changed
|= setDoesNotCapture(F
, 1);
474 case LibFunc_memrchr
:
475 Changed
|= setDoesNotThrow(F
);
476 Changed
|= setOnlyAccessesArgMemory(F
);
477 Changed
|= setOnlyReadsMemory(F
);
478 Changed
|= setWillReturn(F
);
483 Changed
|= setDoesNotThrow(F
);
484 Changed
|= setWillReturn(F
);
485 Changed
|= setDoesNotCapture(F
, 1);
488 Changed
|= setDoesNotThrow(F
);
489 Changed
|= setOnlyAccessesArgMemory(F
);
490 Changed
|= setWillReturn(F
);
491 Changed
|= setDoesNotAlias(F
, 0);
492 Changed
|= setReturnedArg(F
, 0);
493 Changed
|= setOnlyWritesMemory(F
, 0);
494 Changed
|= setDoesNotAlias(F
, 1);
495 Changed
|= setDoesNotCapture(F
, 1);
496 Changed
|= setOnlyReadsMemory(F
, 1);
498 case LibFunc_memmove
:
499 Changed
|= setDoesNotThrow(F
);
500 Changed
|= setOnlyAccessesArgMemory(F
);
501 Changed
|= setWillReturn(F
);
502 Changed
|= setReturnedArg(F
, 0);
503 Changed
|= setOnlyWritesMemory(F
, 0);
504 Changed
|= setDoesNotCapture(F
, 1);
505 Changed
|= setOnlyReadsMemory(F
, 1);
507 case LibFunc_mempcpy
:
508 case LibFunc_memccpy
:
509 Changed
|= setWillReturn(F
);
511 case LibFunc_memcpy_chk
:
512 Changed
|= setDoesNotThrow(F
);
513 Changed
|= setOnlyAccessesArgMemory(F
);
514 Changed
|= setDoesNotAlias(F
, 0);
515 Changed
|= setOnlyWritesMemory(F
, 0);
516 Changed
|= setDoesNotAlias(F
, 1);
517 Changed
|= setDoesNotCapture(F
, 1);
518 Changed
|= setOnlyReadsMemory(F
, 1);
520 case LibFunc_memalign
:
521 Changed
|= setAllocFamily(F
, "malloc");
522 Changed
|= setAllocKind(F
, AllocFnKind::Alloc
| AllocFnKind::Aligned
|
523 AllocFnKind::Uninitialized
);
524 Changed
|= setAllocSize(F
, 1, None
);
525 Changed
|= setAlignedAllocParam(F
, 0);
526 Changed
|= setOnlyAccessesInaccessibleMemory(F
);
527 Changed
|= setRetNoUndef(F
);
528 Changed
|= setDoesNotThrow(F
);
529 Changed
|= setRetDoesNotAlias(F
);
530 Changed
|= setWillReturn(F
);
533 Changed
|= setRetAndArgsNoUndef(F
);
534 Changed
|= setDoesNotThrow(F
);
535 Changed
|= setDoesNotCapture(F
, 0);
536 Changed
|= setOnlyReadsMemory(F
, 0);
539 Changed
|= setRetAndArgsNoUndef(F
);
540 Changed
|= setDoesNotThrow(F
);
541 Changed
|= setWillReturn(F
);
542 Changed
|= setDoesNotCapture(F
, 0);
544 case LibFunc_realloc
:
545 case LibFunc_reallocf
:
546 case LibFunc_vec_realloc
:
547 Changed
|= setAllocFamily(
548 F
, TheLibFunc
== LibFunc_vec_realloc
? "vec_malloc" : "malloc");
549 Changed
|= setAllocKind(F
, AllocFnKind::Realloc
);
550 Changed
|= setAllocatedPointerParam(F
, 0);
551 Changed
|= setAllocSize(F
, 1, None
);
552 Changed
|= setOnlyAccessesInaccessibleMemOrArgMem(F
);
553 Changed
|= setRetNoUndef(F
);
554 Changed
|= setDoesNotThrow(F
);
555 Changed
|= setRetDoesNotAlias(F
);
556 Changed
|= setWillReturn(F
);
557 Changed
|= setDoesNotCapture(F
, 0);
558 Changed
|= setArgNoUndef(F
, 1);
561 // May throw; "read" is a valid pthread cancellation point.
562 Changed
|= setRetAndArgsNoUndef(F
);
563 Changed
|= setDoesNotCapture(F
, 1);
566 Changed
|= setRetAndArgsNoUndef(F
);
567 Changed
|= setDoesNotThrow(F
);
568 Changed
|= setDoesNotCapture(F
, 0);
572 case LibFunc_realpath
:
573 Changed
|= setRetAndArgsNoUndef(F
);
574 Changed
|= setDoesNotThrow(F
);
575 Changed
|= setDoesNotCapture(F
, 0);
576 Changed
|= setOnlyReadsMemory(F
, 0);
579 Changed
|= setRetAndArgsNoUndef(F
);
580 Changed
|= setDoesNotThrow(F
);
581 Changed
|= setDoesNotCapture(F
, 0);
582 Changed
|= setDoesNotCapture(F
, 1);
583 Changed
|= setOnlyReadsMemory(F
, 0);
584 Changed
|= setOnlyReadsMemory(F
, 1);
586 case LibFunc_readlink
:
587 Changed
|= setRetAndArgsNoUndef(F
);
588 Changed
|= setDoesNotThrow(F
);
589 Changed
|= setDoesNotCapture(F
, 0);
590 Changed
|= setDoesNotCapture(F
, 1);
591 Changed
|= setOnlyReadsMemory(F
, 0);
594 // May throw; "write" is a valid pthread cancellation point.
595 Changed
|= setRetAndArgsNoUndef(F
);
596 Changed
|= setDoesNotCapture(F
, 1);
597 Changed
|= setOnlyReadsMemory(F
, 1);
600 Changed
|= setDoesNotThrow(F
);
601 Changed
|= setOnlyAccessesArgMemory(F
);
602 Changed
|= setWillReturn(F
);
603 Changed
|= setDoesNotCapture(F
, 0);
604 Changed
|= setOnlyReadsMemory(F
, 0);
605 Changed
|= setOnlyWritesMemory(F
, 1);
606 Changed
|= setDoesNotCapture(F
, 1);
609 Changed
|= setDoesNotThrow(F
);
610 Changed
|= setOnlyAccessesArgMemory(F
);
611 Changed
|= setOnlyReadsMemory(F
);
612 Changed
|= setWillReturn(F
);
613 Changed
|= setDoesNotCapture(F
, 0);
614 Changed
|= setDoesNotCapture(F
, 1);
617 Changed
|= setDoesNotThrow(F
);
618 Changed
|= setOnlyAccessesArgMemory(F
);
619 Changed
|= setWillReturn(F
);
620 Changed
|= setDoesNotCapture(F
, 0);
621 Changed
|= setOnlyWritesMemory(F
, 0);
624 case LibFunc_vec_calloc
:
625 Changed
|= setAllocFamily(F
, TheLibFunc
== LibFunc_vec_calloc
? "vec_malloc"
627 Changed
|= setAllocKind(F
, AllocFnKind::Alloc
| AllocFnKind::Zeroed
);
628 Changed
|= setAllocSize(F
, 0, 1);
629 Changed
|= setOnlyAccessesInaccessibleMemory(F
);
630 Changed
|= setRetAndArgsNoUndef(F
);
631 Changed
|= setDoesNotThrow(F
);
632 Changed
|= setRetDoesNotAlias(F
);
633 Changed
|= setWillReturn(F
);
637 Changed
|= setRetAndArgsNoUndef(F
);
638 Changed
|= setDoesNotThrow(F
);
639 Changed
|= setDoesNotCapture(F
, 0);
640 Changed
|= setOnlyReadsMemory(F
, 0);
642 case LibFunc_ctermid
:
643 case LibFunc_clearerr
:
644 case LibFunc_closedir
:
645 Changed
|= setRetAndArgsNoUndef(F
);
646 Changed
|= setDoesNotThrow(F
);
647 Changed
|= setDoesNotCapture(F
, 0);
653 Changed
|= setDoesNotThrow(F
);
654 Changed
|= setOnlyReadsMemory(F
);
655 Changed
|= setWillReturn(F
);
656 Changed
|= setDoesNotCapture(F
, 0);
659 Changed
|= setRetAndArgsNoUndef(F
);
660 Changed
|= setDoesNotThrow(F
);
661 Changed
|= setDoesNotCapture(F
, 0);
662 Changed
|= setOnlyReadsMemory(F
, 0);
665 Changed
|= setRetAndArgsNoUndef(F
);
666 Changed
|= setDoesNotThrow(F
);
667 Changed
|= setRetDoesNotAlias(F
);
668 Changed
|= setDoesNotCapture(F
, 0);
669 Changed
|= setDoesNotCapture(F
, 1);
670 Changed
|= setOnlyReadsMemory(F
, 0);
671 Changed
|= setOnlyReadsMemory(F
, 1);
674 Changed
|= setRetAndArgsNoUndef(F
);
675 Changed
|= setDoesNotThrow(F
);
676 Changed
|= setRetDoesNotAlias(F
);
677 Changed
|= setDoesNotCapture(F
, 1);
678 Changed
|= setOnlyReadsMemory(F
, 1);
681 Changed
|= setRetAndArgsNoUndef(F
);
682 Changed
|= setDoesNotThrow(F
);
683 Changed
|= setDoesNotCapture(F
, 0);
686 case LibFunc_vec_free
:
687 Changed
|= setAllocFamily(F
, TheLibFunc
== LibFunc_vec_free
? "vec_malloc"
689 Changed
|= setAllocKind(F
, AllocFnKind::Free
);
690 Changed
|= setAllocatedPointerParam(F
, 0);
691 Changed
|= setOnlyAccessesInaccessibleMemOrArgMem(F
);
692 Changed
|= setArgsNoUndef(F
);
693 Changed
|= setDoesNotThrow(F
);
694 Changed
|= setWillReturn(F
);
695 Changed
|= setDoesNotCapture(F
, 0);
700 case LibFunc_fgetc_unlocked
:
706 case LibFunc_fsetpos
:
707 case LibFunc_flockfile
:
708 case LibFunc_funlockfile
:
709 case LibFunc_ftrylockfile
:
710 Changed
|= setRetAndArgsNoUndef(F
);
711 Changed
|= setDoesNotThrow(F
);
712 Changed
|= setDoesNotCapture(F
, 0);
715 Changed
|= setRetAndArgsNoUndef(F
);
716 Changed
|= setDoesNotThrow(F
);
717 Changed
|= setDoesNotCapture(F
, 0);
718 Changed
|= setOnlyReadsMemory(F
);
721 case LibFunc_fputc_unlocked
:
723 Changed
|= setRetAndArgsNoUndef(F
);
724 Changed
|= setDoesNotThrow(F
);
725 Changed
|= setDoesNotCapture(F
, 1);
730 Changed
|= setDoesNotThrow(F
);
731 Changed
|= setWillReturn(F
);
732 Changed
|= setDoesNotCapture(F
, 1);
734 case LibFunc_fstatvfs
:
735 Changed
|= setRetAndArgsNoUndef(F
);
736 Changed
|= setDoesNotThrow(F
);
737 Changed
|= setDoesNotCapture(F
, 1);
740 case LibFunc_fgets_unlocked
:
741 Changed
|= setRetAndArgsNoUndef(F
);
742 Changed
|= setDoesNotThrow(F
);
743 Changed
|= setDoesNotCapture(F
, 2);
746 case LibFunc_fread_unlocked
:
747 Changed
|= setRetAndArgsNoUndef(F
);
748 Changed
|= setDoesNotThrow(F
);
749 Changed
|= setDoesNotCapture(F
, 0);
750 Changed
|= setDoesNotCapture(F
, 3);
753 case LibFunc_fwrite_unlocked
:
754 Changed
|= setRetAndArgsNoUndef(F
);
755 Changed
|= setDoesNotThrow(F
);
756 Changed
|= setDoesNotCapture(F
, 0);
757 Changed
|= setDoesNotCapture(F
, 3);
758 // FIXME: readonly #1?
761 case LibFunc_fputs_unlocked
:
762 Changed
|= setRetAndArgsNoUndef(F
);
763 Changed
|= setDoesNotThrow(F
);
764 Changed
|= setDoesNotCapture(F
, 0);
765 Changed
|= setDoesNotCapture(F
, 1);
766 Changed
|= setOnlyReadsMemory(F
, 0);
769 case LibFunc_fprintf
:
770 Changed
|= setRetAndArgsNoUndef(F
);
771 Changed
|= setDoesNotThrow(F
);
772 Changed
|= setDoesNotCapture(F
, 0);
773 Changed
|= setDoesNotCapture(F
, 1);
774 Changed
|= setOnlyReadsMemory(F
, 1);
776 case LibFunc_fgetpos
:
777 Changed
|= setRetAndArgsNoUndef(F
);
778 Changed
|= setDoesNotThrow(F
);
779 Changed
|= setDoesNotCapture(F
, 0);
780 Changed
|= setDoesNotCapture(F
, 1);
783 Changed
|= setRetAndArgsNoUndef(F
);
784 Changed
|= setDoesNotThrow(F
);
785 Changed
|= setDoesNotCapture(F
, 0);
787 case LibFunc_getlogin_r
:
788 Changed
|= setRetAndArgsNoUndef(F
);
789 Changed
|= setDoesNotThrow(F
);
790 Changed
|= setDoesNotCapture(F
, 0);
792 case LibFunc_getc_unlocked
:
793 Changed
|= setRetAndArgsNoUndef(F
);
794 Changed
|= setDoesNotThrow(F
);
795 Changed
|= setDoesNotCapture(F
, 0);
798 Changed
|= setRetAndArgsNoUndef(F
);
799 Changed
|= setDoesNotThrow(F
);
800 Changed
|= setOnlyReadsMemory(F
);
801 Changed
|= setDoesNotCapture(F
, 0);
804 case LibFunc_getchar
:
805 case LibFunc_getchar_unlocked
:
806 Changed
|= setRetAndArgsNoUndef(F
);
807 Changed
|= setDoesNotThrow(F
);
809 case LibFunc_getitimer
:
810 Changed
|= setRetAndArgsNoUndef(F
);
811 Changed
|= setDoesNotThrow(F
);
812 Changed
|= setDoesNotCapture(F
, 1);
814 case LibFunc_getpwnam
:
815 Changed
|= setRetAndArgsNoUndef(F
);
816 Changed
|= setDoesNotThrow(F
);
817 Changed
|= setDoesNotCapture(F
, 0);
818 Changed
|= setOnlyReadsMemory(F
, 0);
821 Changed
|= setRetAndArgsNoUndef(F
);
822 Changed
|= setDoesNotThrow(F
);
823 Changed
|= setDoesNotCapture(F
, 1);
826 Changed
|= setRetAndArgsNoUndef(F
);
827 Changed
|= setDoesNotThrow(F
);
828 Changed
|= setDoesNotCapture(F
, 0);
831 Changed
|= setRetAndArgsNoUndef(F
);
832 Changed
|= setDoesNotThrow(F
);
833 Changed
|= setDoesNotCapture(F
, 0);
834 Changed
|= setOnlyReadsMemory(F
, 0);
836 case LibFunc_unsetenv
:
837 Changed
|= setRetAndArgsNoUndef(F
);
838 Changed
|= setDoesNotThrow(F
);
839 Changed
|= setDoesNotCapture(F
, 0);
840 Changed
|= setOnlyReadsMemory(F
, 0);
844 Changed
|= setRetAndArgsNoUndef(F
);
845 Changed
|= setDoesNotThrow(F
);
846 Changed
|= setDoesNotCapture(F
, 0);
847 Changed
|= setDoesNotCapture(F
, 1);
848 Changed
|= setOnlyReadsMemory(F
, 0);
849 Changed
|= setOnlyReadsMemory(F
, 1);
852 case LibFunc_putc_unlocked
:
853 Changed
|= setRetAndArgsNoUndef(F
);
854 Changed
|= setDoesNotThrow(F
);
855 Changed
|= setDoesNotCapture(F
, 1);
860 Changed
|= setRetAndArgsNoUndef(F
);
861 Changed
|= setDoesNotThrow(F
);
862 Changed
|= setDoesNotCapture(F
, 0);
863 Changed
|= setOnlyReadsMemory(F
, 0);
866 // May throw; "pread" is a valid pthread cancellation point.
867 Changed
|= setRetAndArgsNoUndef(F
);
868 Changed
|= setDoesNotCapture(F
, 1);
871 // May throw; "pwrite" is a valid pthread cancellation point.
872 Changed
|= setRetAndArgsNoUndef(F
);
873 Changed
|= setDoesNotCapture(F
, 1);
874 Changed
|= setOnlyReadsMemory(F
, 1);
876 case LibFunc_putchar
:
877 case LibFunc_putchar_unlocked
:
878 Changed
|= setRetAndArgsNoUndef(F
);
879 Changed
|= setDoesNotThrow(F
);
882 Changed
|= setRetAndArgsNoUndef(F
);
883 Changed
|= setDoesNotThrow(F
);
884 Changed
|= setRetDoesNotAlias(F
);
885 Changed
|= setDoesNotCapture(F
, 0);
886 Changed
|= setDoesNotCapture(F
, 1);
887 Changed
|= setOnlyReadsMemory(F
, 0);
888 Changed
|= setOnlyReadsMemory(F
, 1);
891 Changed
|= setRetAndArgsNoUndef(F
);
892 Changed
|= setDoesNotThrow(F
);
893 Changed
|= setDoesNotCapture(F
, 0);
896 Changed
|= setRetAndArgsNoUndef(F
);
897 Changed
|= setDoesNotThrow(F
);
898 Changed
|= setDoesNotCapture(F
, 0);
899 Changed
|= setOnlyReadsMemory(F
, 0);
901 case LibFunc_vsscanf
:
902 Changed
|= setRetAndArgsNoUndef(F
);
903 Changed
|= setDoesNotThrow(F
);
904 Changed
|= setDoesNotCapture(F
, 0);
905 Changed
|= setDoesNotCapture(F
, 1);
906 Changed
|= setOnlyReadsMemory(F
, 0);
907 Changed
|= setOnlyReadsMemory(F
, 1);
909 case LibFunc_vfscanf
:
910 Changed
|= setRetAndArgsNoUndef(F
);
911 Changed
|= setDoesNotThrow(F
);
912 Changed
|= setDoesNotCapture(F
, 0);
913 Changed
|= setDoesNotCapture(F
, 1);
914 Changed
|= setOnlyReadsMemory(F
, 1);
916 case LibFunc_vprintf
:
917 Changed
|= setRetAndArgsNoUndef(F
);
918 Changed
|= setDoesNotThrow(F
);
919 Changed
|= setDoesNotCapture(F
, 0);
920 Changed
|= setOnlyReadsMemory(F
, 0);
922 case LibFunc_vfprintf
:
923 case LibFunc_vsprintf
:
924 Changed
|= setRetAndArgsNoUndef(F
);
925 Changed
|= setDoesNotThrow(F
);
926 Changed
|= setDoesNotCapture(F
, 0);
927 Changed
|= setDoesNotCapture(F
, 1);
928 Changed
|= setOnlyReadsMemory(F
, 1);
930 case LibFunc_vsnprintf
:
931 Changed
|= setRetAndArgsNoUndef(F
);
932 Changed
|= setDoesNotThrow(F
);
933 Changed
|= setDoesNotCapture(F
, 0);
934 Changed
|= setDoesNotCapture(F
, 2);
935 Changed
|= setOnlyReadsMemory(F
, 2);
938 // May throw; "open" is a valid pthread cancellation point.
939 Changed
|= setRetAndArgsNoUndef(F
);
940 Changed
|= setDoesNotCapture(F
, 0);
941 Changed
|= setOnlyReadsMemory(F
, 0);
943 case LibFunc_opendir
:
944 Changed
|= setRetAndArgsNoUndef(F
);
945 Changed
|= setDoesNotThrow(F
);
946 Changed
|= setRetDoesNotAlias(F
);
947 Changed
|= setDoesNotCapture(F
, 0);
948 Changed
|= setOnlyReadsMemory(F
, 0);
950 case LibFunc_tmpfile
:
951 Changed
|= setRetAndArgsNoUndef(F
);
952 Changed
|= setDoesNotThrow(F
);
953 Changed
|= setRetDoesNotAlias(F
);
956 Changed
|= setRetAndArgsNoUndef(F
);
957 Changed
|= setDoesNotThrow(F
);
958 Changed
|= setDoesNotCapture(F
, 0);
964 Changed
|= setDoesNotThrow(F
);
965 Changed
|= setDoesNotAccessMemory(F
);
968 Changed
|= setRetAndArgsNoUndef(F
);
969 Changed
|= setDoesNotThrow(F
);
970 Changed
|= setDoesNotCapture(F
, 0);
971 Changed
|= setDoesNotCapture(F
, 1);
972 Changed
|= setOnlyReadsMemory(F
, 0);
975 Changed
|= setRetAndArgsNoUndef(F
);
976 Changed
|= setDoesNotThrow(F
);
977 Changed
|= setDoesNotCapture(F
, 0);
978 Changed
|= setOnlyReadsMemory(F
, 0);
981 // May throw; places call through function pointer.
982 // Cannot give undef pointer/size
983 Changed
|= setRetAndArgsNoUndef(F
);
984 Changed
|= setDoesNotCapture(F
, 3);
986 case LibFunc_dunder_strndup
:
987 Changed
|= setArgNoUndef(F
, 1);
989 case LibFunc_dunder_strdup
:
990 Changed
|= setDoesNotThrow(F
);
991 Changed
|= setRetDoesNotAlias(F
);
992 Changed
|= setWillReturn(F
);
993 Changed
|= setDoesNotCapture(F
, 0);
994 Changed
|= setOnlyReadsMemory(F
, 0);
996 case LibFunc_dunder_strtok_r
:
997 Changed
|= setDoesNotThrow(F
);
998 Changed
|= setDoesNotCapture(F
, 1);
999 Changed
|= setOnlyReadsMemory(F
, 1);
1001 case LibFunc_under_IO_getc
:
1002 Changed
|= setRetAndArgsNoUndef(F
);
1003 Changed
|= setDoesNotThrow(F
);
1004 Changed
|= setDoesNotCapture(F
, 0);
1006 case LibFunc_under_IO_putc
:
1007 Changed
|= setRetAndArgsNoUndef(F
);
1008 Changed
|= setDoesNotThrow(F
);
1009 Changed
|= setDoesNotCapture(F
, 1);
1011 case LibFunc_dunder_isoc99_scanf
:
1012 Changed
|= setRetAndArgsNoUndef(F
);
1013 Changed
|= setDoesNotThrow(F
);
1014 Changed
|= setDoesNotCapture(F
, 0);
1015 Changed
|= setOnlyReadsMemory(F
, 0);
1017 case LibFunc_stat64
:
1018 case LibFunc_lstat64
:
1019 case LibFunc_statvfs64
:
1020 Changed
|= setRetAndArgsNoUndef(F
);
1021 Changed
|= setDoesNotThrow(F
);
1022 Changed
|= setDoesNotCapture(F
, 0);
1023 Changed
|= setDoesNotCapture(F
, 1);
1024 Changed
|= setOnlyReadsMemory(F
, 0);
1026 case LibFunc_dunder_isoc99_sscanf
:
1027 Changed
|= setRetAndArgsNoUndef(F
);
1028 Changed
|= setDoesNotThrow(F
);
1029 Changed
|= setDoesNotCapture(F
, 0);
1030 Changed
|= setDoesNotCapture(F
, 1);
1031 Changed
|= setOnlyReadsMemory(F
, 0);
1032 Changed
|= setOnlyReadsMemory(F
, 1);
1034 case LibFunc_fopen64
:
1035 Changed
|= setRetAndArgsNoUndef(F
);
1036 Changed
|= setDoesNotThrow(F
);
1037 Changed
|= setRetDoesNotAlias(F
);
1038 Changed
|= setDoesNotCapture(F
, 0);
1039 Changed
|= setDoesNotCapture(F
, 1);
1040 Changed
|= setOnlyReadsMemory(F
, 0);
1041 Changed
|= setOnlyReadsMemory(F
, 1);
1043 case LibFunc_fseeko64
:
1044 case LibFunc_ftello64
:
1045 Changed
|= setRetAndArgsNoUndef(F
);
1046 Changed
|= setDoesNotThrow(F
);
1047 Changed
|= setDoesNotCapture(F
, 0);
1049 case LibFunc_tmpfile64
:
1050 Changed
|= setRetAndArgsNoUndef(F
);
1051 Changed
|= setDoesNotThrow(F
);
1052 Changed
|= setRetDoesNotAlias(F
);
1054 case LibFunc_fstat64
:
1055 case LibFunc_fstatvfs64
:
1056 Changed
|= setRetAndArgsNoUndef(F
);
1057 Changed
|= setDoesNotThrow(F
);
1058 Changed
|= setDoesNotCapture(F
, 1);
1060 case LibFunc_open64
:
1061 // May throw; "open" is a valid pthread cancellation point.
1062 Changed
|= setRetAndArgsNoUndef(F
);
1063 Changed
|= setDoesNotCapture(F
, 0);
1064 Changed
|= setOnlyReadsMemory(F
, 0);
1066 case LibFunc_gettimeofday
:
1067 // Currently some platforms have the restrict keyword on the arguments to
1068 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1070 Changed
|= setRetAndArgsNoUndef(F
);
1071 Changed
|= setDoesNotThrow(F
);
1072 Changed
|= setDoesNotCapture(F
, 0);
1073 Changed
|= setDoesNotCapture(F
, 1);
1075 case LibFunc_memset_pattern4
:
1076 case LibFunc_memset_pattern8
:
1077 case LibFunc_memset_pattern16
:
1078 Changed
|= setDoesNotCapture(F
, 0);
1079 Changed
|= setDoesNotCapture(F
, 1);
1080 Changed
|= setOnlyReadsMemory(F
, 1);
1082 case LibFunc_memset
:
1083 Changed
|= setWillReturn(F
);
1085 case LibFunc_memset_chk
:
1086 Changed
|= setOnlyAccessesArgMemory(F
);
1087 Changed
|= setOnlyWritesMemory(F
, 0);
1088 Changed
|= setDoesNotThrow(F
);
1090 // int __nvvm_reflect(const char *)
1091 case LibFunc_nvvm_reflect
:
1092 Changed
|= setRetAndArgsNoUndef(F
);
1093 Changed
|= setDoesNotAccessMemory(F
);
1094 Changed
|= setDoesNotThrow(F
);
1097 case LibFunc_ldexpf
:
1098 case LibFunc_ldexpl
:
1099 Changed
|= setWillReturn(F
);
1105 case LibFunc_acoshf
:
1106 case LibFunc_acoshl
:
1111 case LibFunc_asinhf
:
1112 case LibFunc_asinhl
:
1116 case LibFunc_atan2f
:
1117 case LibFunc_atan2l
:
1120 case LibFunc_atanhf
:
1121 case LibFunc_atanhl
:
1129 case LibFunc_copysign
:
1130 case LibFunc_copysignf
:
1131 case LibFunc_copysignl
:
1139 case LibFunc_cospif
:
1147 case LibFunc_expm1f
:
1148 case LibFunc_expm1l
:
1156 case LibFunc_floorf
:
1157 case LibFunc_floorl
:
1170 case LibFunc_isascii
:
1171 case LibFunc_isdigit
:
1176 case LibFunc_log10f
:
1177 case LibFunc_log10l
:
1179 case LibFunc_log1pf
:
1180 case LibFunc_log1pl
:
1189 case LibFunc_nearbyint
:
1190 case LibFunc_nearbyintf
:
1191 case LibFunc_nearbyintl
:
1199 case LibFunc_roundf
:
1200 case LibFunc_roundl
:
1202 case LibFunc_sincospif_stret
:
1209 case LibFunc_sinpif
:
1219 case LibFunc_toascii
:
1221 case LibFunc_truncf
:
1222 case LibFunc_truncl
:
1223 Changed
|= setDoesNotThrow(F
);
1224 Changed
|= setDoesNotFreeMemory(F
);
1225 Changed
|= setOnlyWritesMemory(F
);
1226 Changed
|= setWillReturn(F
);
1229 // FIXME: It'd be really nice to cover all the library functions we're
1233 // We have to do this step after AllocKind has been inferred on functions so
1234 // we can reliably identify free-like and realloc-like functions.
1235 if (!isLibFreeFunction(&F
, TheLibFunc
) && !isReallocLikeFn(&F
, &TLI
))
1236 Changed
|= setDoesNotFreeMemory(F
);
1240 static void setArgExtAttr(Function
&F
, unsigned ArgNo
,
1241 const TargetLibraryInfo
&TLI
, bool Signed
= true) {
1242 Attribute::AttrKind ExtAttr
= TLI
.getExtAttrForI32Param(Signed
);
1243 if (ExtAttr
!= Attribute::None
&& !F
.hasParamAttribute(ArgNo
, ExtAttr
))
1244 F
.addParamAttr(ArgNo
, ExtAttr
);
1247 // Modeled after X86TargetLowering::markLibCallAttributes.
1248 static void markRegisterParameterAttributes(Function
*F
) {
1249 if (!F
->arg_size() || F
->isVarArg())
1252 const CallingConv::ID CC
= F
->getCallingConv();
1253 if (CC
!= CallingConv::C
&& CC
!= CallingConv::X86_StdCall
)
1256 const Module
*M
= F
->getParent();
1257 unsigned N
= M
->getNumberRegisterParameters();
1261 const DataLayout
&DL
= M
->getDataLayout();
1263 for (Argument
&A
: F
->args()) {
1264 Type
*T
= A
.getType();
1265 if (!T
->isIntOrPtrTy())
1268 const TypeSize
&TS
= DL
.getTypeAllocSize(T
);
1272 assert(TS
<= 4 && "Need to account for parameters larger than word size");
1273 const unsigned NumRegs
= TS
> 4 ? 2 : 1;
1278 F
->addParamAttr(A
.getArgNo(), Attribute::InReg
);
1282 FunctionCallee
llvm::getOrInsertLibFunc(Module
*M
, const TargetLibraryInfo
&TLI
,
1283 LibFunc TheLibFunc
, FunctionType
*T
,
1284 AttributeList AttributeList
) {
1285 assert(TLI
.has(TheLibFunc
) &&
1286 "Creating call to non-existing library function.");
1287 StringRef Name
= TLI
.getName(TheLibFunc
);
1288 FunctionCallee C
= M
->getOrInsertFunction(Name
, T
, AttributeList
);
1290 // Make sure any mandatory argument attributes are added.
1292 // Any outgoing i32 argument should be handled with setArgExtAttr() which
1293 // will add an extension attribute if the target ABI requires it. Adding
1294 // argument extensions is typically done by the front end but when an
1295 // optimizer is building a library call on its own it has to take care of
1296 // this. Each such generated function must be handled here with sign or
1297 // zero extensions as needed. F is retreived with cast<> because we demand
1298 // of the caller to have called isLibFuncEmittable() first.
1299 Function
*F
= cast
<Function
>(C
.getCallee());
1300 assert(F
->getFunctionType() == T
&& "Function type does not match.");
1301 switch (TheLibFunc
) {
1303 case LibFunc_putchar
:
1304 setArgExtAttr(*F
, 0, TLI
);
1307 case LibFunc_ldexpf
:
1308 case LibFunc_ldexpl
:
1309 case LibFunc_memchr
:
1310 case LibFunc_memrchr
:
1311 case LibFunc_strchr
:
1312 setArgExtAttr(*F
, 1, TLI
);
1314 case LibFunc_memccpy
:
1315 setArgExtAttr(*F
, 2, TLI
);
1318 // These are functions that are known to not need any argument extension
1319 // on any target: A size_t argument (which may be an i32 on some targets)
1320 // should not trigger the assert below.
1322 case LibFunc_calloc
:
1323 case LibFunc_fwrite
:
1324 case LibFunc_malloc
:
1325 case LibFunc_memcmp
:
1326 case LibFunc_memcpy_chk
:
1327 case LibFunc_mempcpy
:
1328 case LibFunc_memset_pattern16
:
1329 case LibFunc_snprintf
:
1330 case LibFunc_stpncpy
:
1331 case LibFunc_strlcat
:
1332 case LibFunc_strlcpy
:
1333 case LibFunc_strncat
:
1334 case LibFunc_strncmp
:
1335 case LibFunc_strncpy
:
1336 case LibFunc_vsnprintf
:
1341 for (unsigned i
= 0; i
< T
->getNumParams(); i
++)
1342 assert(!isa
<IntegerType
>(T
->getParamType(i
)) &&
1343 "Unhandled integer argument.");
1348 markRegisterParameterAttributes(F
);
1353 FunctionCallee
llvm::getOrInsertLibFunc(Module
*M
, const TargetLibraryInfo
&TLI
,
1354 LibFunc TheLibFunc
, FunctionType
*T
) {
1355 return getOrInsertLibFunc(M
, TLI
, TheLibFunc
, T
, AttributeList());
1358 bool llvm::isLibFuncEmittable(const Module
*M
, const TargetLibraryInfo
*TLI
,
1359 LibFunc TheLibFunc
) {
1360 StringRef FuncName
= TLI
->getName(TheLibFunc
);
1361 if (!TLI
->has(TheLibFunc
))
1364 // Check if the Module already has a GlobalValue with the same name, in
1365 // which case it must be a Function with the expected type.
1366 if (GlobalValue
*GV
= M
->getNamedValue(FuncName
)) {
1367 if (auto *F
= dyn_cast
<Function
>(GV
))
1368 return TLI
->isValidProtoForLibFunc(*F
->getFunctionType(), TheLibFunc
, *M
);
1375 bool llvm::isLibFuncEmittable(const Module
*M
, const TargetLibraryInfo
*TLI
,
1378 return TLI
->getLibFunc(Name
, TheLibFunc
) &&
1379 isLibFuncEmittable(M
, TLI
, TheLibFunc
);
1382 bool llvm::hasFloatFn(const Module
*M
, const TargetLibraryInfo
*TLI
, Type
*Ty
,
1383 LibFunc DoubleFn
, LibFunc FloatFn
, LibFunc LongDoubleFn
) {
1384 switch (Ty
->getTypeID()) {
1385 case Type::HalfTyID
:
1387 case Type::FloatTyID
:
1388 return isLibFuncEmittable(M
, TLI
, FloatFn
);
1389 case Type::DoubleTyID
:
1390 return isLibFuncEmittable(M
, TLI
, DoubleFn
);
1392 return isLibFuncEmittable(M
, TLI
, LongDoubleFn
);
1396 StringRef
llvm::getFloatFn(const Module
*M
, const TargetLibraryInfo
*TLI
,
1397 Type
*Ty
, LibFunc DoubleFn
, LibFunc FloatFn
,
1398 LibFunc LongDoubleFn
, LibFunc
&TheLibFunc
) {
1399 assert(hasFloatFn(M
, TLI
, Ty
, DoubleFn
, FloatFn
, LongDoubleFn
) &&
1400 "Cannot get name for unavailable function!");
1402 switch (Ty
->getTypeID()) {
1403 case Type::HalfTyID
:
1404 llvm_unreachable("No name for HalfTy!");
1405 case Type::FloatTyID
:
1406 TheLibFunc
= FloatFn
;
1407 return TLI
->getName(FloatFn
);
1408 case Type::DoubleTyID
:
1409 TheLibFunc
= DoubleFn
;
1410 return TLI
->getName(DoubleFn
);
1412 TheLibFunc
= LongDoubleFn
;
1413 return TLI
->getName(LongDoubleFn
);
1417 //- Emit LibCalls ------------------------------------------------------------//
1419 Value
*llvm::castToCStr(Value
*V
, IRBuilderBase
&B
) {
1420 unsigned AS
= V
->getType()->getPointerAddressSpace();
1421 return B
.CreateBitCast(V
, B
.getInt8PtrTy(AS
), "cstr");
1424 static Value
*emitLibCall(LibFunc TheLibFunc
, Type
*ReturnType
,
1425 ArrayRef
<Type
*> ParamTypes
,
1426 ArrayRef
<Value
*> Operands
, IRBuilderBase
&B
,
1427 const TargetLibraryInfo
*TLI
,
1428 bool IsVaArgs
= false) {
1429 Module
*M
= B
.GetInsertBlock()->getModule();
1430 if (!isLibFuncEmittable(M
, TLI
, TheLibFunc
))
1433 StringRef FuncName
= TLI
->getName(TheLibFunc
);
1434 FunctionType
*FuncType
= FunctionType::get(ReturnType
, ParamTypes
, IsVaArgs
);
1435 FunctionCallee Callee
= getOrInsertLibFunc(M
, *TLI
, TheLibFunc
, FuncType
);
1436 inferNonMandatoryLibFuncAttrs(M
, FuncName
, *TLI
);
1437 CallInst
*CI
= B
.CreateCall(Callee
, Operands
, FuncName
);
1438 if (const Function
*F
=
1439 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1440 CI
->setCallingConv(F
->getCallingConv());
1444 Value
*llvm::emitStrLen(Value
*Ptr
, IRBuilderBase
&B
, const DataLayout
&DL
,
1445 const TargetLibraryInfo
*TLI
) {
1446 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1447 return emitLibCall(LibFunc_strlen
, DL
.getIntPtrType(Context
),
1448 B
.getInt8PtrTy(), castToCStr(Ptr
, B
), B
, TLI
);
1451 Value
*llvm::emitStrDup(Value
*Ptr
, IRBuilderBase
&B
,
1452 const TargetLibraryInfo
*TLI
) {
1453 return emitLibCall(LibFunc_strdup
, B
.getInt8PtrTy(), B
.getInt8PtrTy(),
1454 castToCStr(Ptr
, B
), B
, TLI
);
1457 Value
*llvm::emitStrChr(Value
*Ptr
, char C
, IRBuilderBase
&B
,
1458 const TargetLibraryInfo
*TLI
) {
1459 Type
*I8Ptr
= B
.getInt8PtrTy();
1460 Type
*I32Ty
= B
.getInt32Ty();
1461 return emitLibCall(LibFunc_strchr
, I8Ptr
, {I8Ptr
, I32Ty
},
1462 {castToCStr(Ptr
, B
), ConstantInt::get(I32Ty
, C
)}, B
, TLI
);
1465 Value
*llvm::emitStrNCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilderBase
&B
,
1466 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1467 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1469 LibFunc_strncmp
, B
.getInt32Ty(),
1470 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
1471 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
1474 Value
*llvm::emitStrCpy(Value
*Dst
, Value
*Src
, IRBuilderBase
&B
,
1475 const TargetLibraryInfo
*TLI
) {
1476 Type
*I8Ptr
= Dst
->getType();
1477 return emitLibCall(LibFunc_strcpy
, I8Ptr
, {I8Ptr
, I8Ptr
},
1478 {castToCStr(Dst
, B
), castToCStr(Src
, B
)}, B
, TLI
);
1481 Value
*llvm::emitStpCpy(Value
*Dst
, Value
*Src
, IRBuilderBase
&B
,
1482 const TargetLibraryInfo
*TLI
) {
1483 Type
*I8Ptr
= B
.getInt8PtrTy();
1484 return emitLibCall(LibFunc_stpcpy
, I8Ptr
, {I8Ptr
, I8Ptr
},
1485 {castToCStr(Dst
, B
), castToCStr(Src
, B
)}, B
, TLI
);
1488 Value
*llvm::emitStrNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilderBase
&B
,
1489 const TargetLibraryInfo
*TLI
) {
1490 Type
*I8Ptr
= B
.getInt8PtrTy();
1491 return emitLibCall(LibFunc_strncpy
, I8Ptr
, {I8Ptr
, I8Ptr
, Len
->getType()},
1492 {castToCStr(Dst
, B
), castToCStr(Src
, B
), Len
}, B
, TLI
);
1495 Value
*llvm::emitStpNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilderBase
&B
,
1496 const TargetLibraryInfo
*TLI
) {
1497 Type
*I8Ptr
= B
.getInt8PtrTy();
1498 return emitLibCall(LibFunc_stpncpy
, I8Ptr
, {I8Ptr
, I8Ptr
, Len
->getType()},
1499 {castToCStr(Dst
, B
), castToCStr(Src
, B
), Len
}, B
, TLI
);
1502 Value
*llvm::emitMemCpyChk(Value
*Dst
, Value
*Src
, Value
*Len
, Value
*ObjSize
,
1503 IRBuilderBase
&B
, const DataLayout
&DL
,
1504 const TargetLibraryInfo
*TLI
) {
1505 Module
*M
= B
.GetInsertBlock()->getModule();
1506 if (!isLibFuncEmittable(M
, TLI
, LibFunc_memcpy_chk
))
1510 AS
= AttributeList::get(M
->getContext(), AttributeList::FunctionIndex
,
1511 Attribute::NoUnwind
);
1512 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1513 FunctionCallee MemCpy
= getOrInsertLibFunc(M
, *TLI
, LibFunc_memcpy_chk
,
1514 AttributeList::get(M
->getContext(), AS
), B
.getInt8PtrTy(),
1515 B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
),
1516 DL
.getIntPtrType(Context
));
1517 Dst
= castToCStr(Dst
, B
);
1518 Src
= castToCStr(Src
, B
);
1519 CallInst
*CI
= B
.CreateCall(MemCpy
, {Dst
, Src
, Len
, ObjSize
});
1520 if (const Function
*F
=
1521 dyn_cast
<Function
>(MemCpy
.getCallee()->stripPointerCasts()))
1522 CI
->setCallingConv(F
->getCallingConv());
1526 Value
*llvm::emitMemPCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilderBase
&B
,
1527 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1528 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1530 LibFunc_mempcpy
, B
.getInt8PtrTy(),
1531 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
1532 {Dst
, Src
, Len
}, B
, TLI
);
1535 Value
*llvm::emitMemChr(Value
*Ptr
, Value
*Val
, Value
*Len
, IRBuilderBase
&B
,
1536 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1537 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1539 LibFunc_memchr
, B
.getInt8PtrTy(),
1540 {B
.getInt8PtrTy(), B
.getInt32Ty(), DL
.getIntPtrType(Context
)},
1541 {castToCStr(Ptr
, B
), Val
, Len
}, B
, TLI
);
1544 Value
*llvm::emitMemRChr(Value
*Ptr
, Value
*Val
, Value
*Len
, IRBuilderBase
&B
,
1545 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1546 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1548 LibFunc_memrchr
, B
.getInt8PtrTy(),
1549 {B
.getInt8PtrTy(), B
.getInt32Ty(), DL
.getIntPtrType(Context
)},
1550 {castToCStr(Ptr
, B
), Val
, Len
}, B
, TLI
);
1553 Value
*llvm::emitMemCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilderBase
&B
,
1554 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1555 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1557 LibFunc_memcmp
, B
.getInt32Ty(),
1558 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
1559 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
1562 Value
*llvm::emitBCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilderBase
&B
,
1563 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1564 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1566 LibFunc_bcmp
, B
.getInt32Ty(),
1567 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
)},
1568 {castToCStr(Ptr1
, B
), castToCStr(Ptr2
, B
), Len
}, B
, TLI
);
1571 Value
*llvm::emitMemCCpy(Value
*Ptr1
, Value
*Ptr2
, Value
*Val
, Value
*Len
,
1572 IRBuilderBase
&B
, const TargetLibraryInfo
*TLI
) {
1574 LibFunc_memccpy
, B
.getInt8PtrTy(),
1575 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), B
.getInt32Ty(), Len
->getType()},
1576 {Ptr1
, Ptr2
, Val
, Len
}, B
, TLI
);
1579 Value
*llvm::emitSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
,
1580 ArrayRef
<Value
*> VariadicArgs
, IRBuilderBase
&B
,
1581 const TargetLibraryInfo
*TLI
) {
1582 SmallVector
<Value
*, 8> Args
{castToCStr(Dest
, B
), Size
, castToCStr(Fmt
, B
)};
1583 llvm::append_range(Args
, VariadicArgs
);
1584 return emitLibCall(LibFunc_snprintf
, B
.getInt32Ty(),
1585 {B
.getInt8PtrTy(), Size
->getType(), B
.getInt8PtrTy()},
1586 Args
, B
, TLI
, /*IsVaArgs=*/true);
1589 Value
*llvm::emitSPrintf(Value
*Dest
, Value
*Fmt
,
1590 ArrayRef
<Value
*> VariadicArgs
, IRBuilderBase
&B
,
1591 const TargetLibraryInfo
*TLI
) {
1592 SmallVector
<Value
*, 8> Args
{castToCStr(Dest
, B
), castToCStr(Fmt
, B
)};
1593 llvm::append_range(Args
, VariadicArgs
);
1594 return emitLibCall(LibFunc_sprintf
, B
.getInt32Ty(),
1595 {B
.getInt8PtrTy(), B
.getInt8PtrTy()}, Args
, B
, TLI
,
1599 Value
*llvm::emitStrCat(Value
*Dest
, Value
*Src
, IRBuilderBase
&B
,
1600 const TargetLibraryInfo
*TLI
) {
1601 return emitLibCall(LibFunc_strcat
, B
.getInt8PtrTy(),
1602 {B
.getInt8PtrTy(), B
.getInt8PtrTy()},
1603 {castToCStr(Dest
, B
), castToCStr(Src
, B
)}, B
, TLI
);
1606 Value
*llvm::emitStrLCpy(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilderBase
&B
,
1607 const TargetLibraryInfo
*TLI
) {
1608 return emitLibCall(LibFunc_strlcpy
, Size
->getType(),
1609 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
1610 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
1613 Value
*llvm::emitStrLCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilderBase
&B
,
1614 const TargetLibraryInfo
*TLI
) {
1615 return emitLibCall(LibFunc_strlcat
, Size
->getType(),
1616 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
1617 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
1620 Value
*llvm::emitStrNCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilderBase
&B
,
1621 const TargetLibraryInfo
*TLI
) {
1622 return emitLibCall(LibFunc_strncat
, B
.getInt8PtrTy(),
1623 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), Size
->getType()},
1624 {castToCStr(Dest
, B
), castToCStr(Src
, B
), Size
}, B
, TLI
);
1627 Value
*llvm::emitVSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
, Value
*VAList
,
1628 IRBuilderBase
&B
, const TargetLibraryInfo
*TLI
) {
1630 LibFunc_vsnprintf
, B
.getInt32Ty(),
1631 {B
.getInt8PtrTy(), Size
->getType(), B
.getInt8PtrTy(), VAList
->getType()},
1632 {castToCStr(Dest
, B
), Size
, castToCStr(Fmt
, B
), VAList
}, B
, TLI
);
1635 Value
*llvm::emitVSPrintf(Value
*Dest
, Value
*Fmt
, Value
*VAList
,
1636 IRBuilderBase
&B
, const TargetLibraryInfo
*TLI
) {
1637 return emitLibCall(LibFunc_vsprintf
, B
.getInt32Ty(),
1638 {B
.getInt8PtrTy(), B
.getInt8PtrTy(), VAList
->getType()},
1639 {castToCStr(Dest
, B
), castToCStr(Fmt
, B
), VAList
}, B
, TLI
);
1642 /// Append a suffix to the function name according to the type of 'Op'.
1643 static void appendTypeSuffix(Value
*Op
, StringRef
&Name
,
1644 SmallString
<20> &NameBuffer
) {
1645 if (!Op
->getType()->isDoubleTy()) {
1648 if (Op
->getType()->isFloatTy())
1657 static Value
*emitUnaryFloatFnCallHelper(Value
*Op
, LibFunc TheLibFunc
,
1658 StringRef Name
, IRBuilderBase
&B
,
1659 const AttributeList
&Attrs
,
1660 const TargetLibraryInfo
*TLI
) {
1661 assert((Name
!= "") && "Must specify Name to emitUnaryFloatFnCall");
1663 Module
*M
= B
.GetInsertBlock()->getModule();
1664 FunctionCallee Callee
= getOrInsertLibFunc(M
, *TLI
, TheLibFunc
, Op
->getType(),
1666 CallInst
*CI
= B
.CreateCall(Callee
, Op
, Name
);
1668 // The incoming attribute set may have come from a speculatable intrinsic, but
1669 // is being replaced with a library call which is not allowed to be
1672 Attrs
.removeFnAttribute(B
.getContext(), Attribute::Speculatable
));
1673 if (const Function
*F
=
1674 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1675 CI
->setCallingConv(F
->getCallingConv());
1680 Value
*llvm::emitUnaryFloatFnCall(Value
*Op
, const TargetLibraryInfo
*TLI
,
1681 StringRef Name
, IRBuilderBase
&B
,
1682 const AttributeList
&Attrs
) {
1683 SmallString
<20> NameBuffer
;
1684 appendTypeSuffix(Op
, Name
, NameBuffer
);
1687 TLI
->getLibFunc(Name
, TheLibFunc
);
1689 return emitUnaryFloatFnCallHelper(Op
, TheLibFunc
, Name
, B
, Attrs
, TLI
);
1692 Value
*llvm::emitUnaryFloatFnCall(Value
*Op
, const TargetLibraryInfo
*TLI
,
1693 LibFunc DoubleFn
, LibFunc FloatFn
,
1694 LibFunc LongDoubleFn
, IRBuilderBase
&B
,
1695 const AttributeList
&Attrs
) {
1696 // Get the name of the function according to TLI.
1697 Module
*M
= B
.GetInsertBlock()->getModule();
1699 StringRef Name
= getFloatFn(M
, TLI
, Op
->getType(), DoubleFn
, FloatFn
,
1700 LongDoubleFn
, TheLibFunc
);
1702 return emitUnaryFloatFnCallHelper(Op
, TheLibFunc
, Name
, B
, Attrs
, TLI
);
1705 static Value
*emitBinaryFloatFnCallHelper(Value
*Op1
, Value
*Op2
,
1707 StringRef Name
, IRBuilderBase
&B
,
1708 const AttributeList
&Attrs
,
1709 const TargetLibraryInfo
*TLI
) {
1710 assert((Name
!= "") && "Must specify Name to emitBinaryFloatFnCall");
1712 Module
*M
= B
.GetInsertBlock()->getModule();
1713 FunctionCallee Callee
= getOrInsertLibFunc(M
, *TLI
, TheLibFunc
, Op1
->getType(),
1714 Op1
->getType(), Op2
->getType());
1715 inferNonMandatoryLibFuncAttrs(M
, Name
, *TLI
);
1716 CallInst
*CI
= B
.CreateCall(Callee
, { Op1
, Op2
}, Name
);
1718 // The incoming attribute set may have come from a speculatable intrinsic, but
1719 // is being replaced with a library call which is not allowed to be
1722 Attrs
.removeFnAttribute(B
.getContext(), Attribute::Speculatable
));
1723 if (const Function
*F
=
1724 dyn_cast
<Function
>(Callee
.getCallee()->stripPointerCasts()))
1725 CI
->setCallingConv(F
->getCallingConv());
1730 Value
*llvm::emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
,
1731 const TargetLibraryInfo
*TLI
,
1732 StringRef Name
, IRBuilderBase
&B
,
1733 const AttributeList
&Attrs
) {
1734 assert((Name
!= "") && "Must specify Name to emitBinaryFloatFnCall");
1736 SmallString
<20> NameBuffer
;
1737 appendTypeSuffix(Op1
, Name
, NameBuffer
);
1740 TLI
->getLibFunc(Name
, TheLibFunc
);
1742 return emitBinaryFloatFnCallHelper(Op1
, Op2
, TheLibFunc
, Name
, B
, Attrs
, TLI
);
1745 Value
*llvm::emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
,
1746 const TargetLibraryInfo
*TLI
,
1747 LibFunc DoubleFn
, LibFunc FloatFn
,
1748 LibFunc LongDoubleFn
, IRBuilderBase
&B
,
1749 const AttributeList
&Attrs
) {
1750 // Get the name of the function according to TLI.
1751 Module
*M
= B
.GetInsertBlock()->getModule();
1753 StringRef Name
= getFloatFn(M
, TLI
, Op1
->getType(), DoubleFn
, FloatFn
,
1754 LongDoubleFn
, TheLibFunc
);
1756 return emitBinaryFloatFnCallHelper(Op1
, Op2
, TheLibFunc
, Name
, B
, Attrs
, TLI
);
1759 // Emit a call to putchar(int) with Char as the argument. Char must have
1760 // the same precision as int, which need not be 32 bits.
1761 Value
*llvm::emitPutChar(Value
*Char
, IRBuilderBase
&B
,
1762 const TargetLibraryInfo
*TLI
) {
1763 Module
*M
= B
.GetInsertBlock()->getModule();
1764 if (!isLibFuncEmittable(M
, TLI
, LibFunc_putchar
))
1767 Type
*Ty
= Char
->getType();
1768 StringRef PutCharName
= TLI
->getName(LibFunc_putchar
);
1769 FunctionCallee PutChar
= getOrInsertLibFunc(M
, *TLI
, LibFunc_putchar
, Ty
, Ty
);
1770 inferNonMandatoryLibFuncAttrs(M
, PutCharName
, *TLI
);
1771 CallInst
*CI
= B
.CreateCall(PutChar
, Char
, PutCharName
);
1773 if (const Function
*F
=
1774 dyn_cast
<Function
>(PutChar
.getCallee()->stripPointerCasts()))
1775 CI
->setCallingConv(F
->getCallingConv());
1779 Value
*llvm::emitPutS(Value
*Str
, IRBuilderBase
&B
,
1780 const TargetLibraryInfo
*TLI
) {
1781 Module
*M
= B
.GetInsertBlock()->getModule();
1782 if (!isLibFuncEmittable(M
, TLI
, LibFunc_puts
))
1785 StringRef PutsName
= TLI
->getName(LibFunc_puts
);
1786 FunctionCallee PutS
= getOrInsertLibFunc(M
, *TLI
, LibFunc_puts
, B
.getInt32Ty(),
1788 inferNonMandatoryLibFuncAttrs(M
, PutsName
, *TLI
);
1789 CallInst
*CI
= B
.CreateCall(PutS
, castToCStr(Str
, B
), PutsName
);
1790 if (const Function
*F
=
1791 dyn_cast
<Function
>(PutS
.getCallee()->stripPointerCasts()))
1792 CI
->setCallingConv(F
->getCallingConv());
1796 Value
*llvm::emitFPutC(Value
*Char
, Value
*File
, IRBuilderBase
&B
,
1797 const TargetLibraryInfo
*TLI
) {
1798 Module
*M
= B
.GetInsertBlock()->getModule();
1799 if (!isLibFuncEmittable(M
, TLI
, LibFunc_fputc
))
1802 StringRef FPutcName
= TLI
->getName(LibFunc_fputc
);
1803 FunctionCallee F
= getOrInsertLibFunc(M
, *TLI
, LibFunc_fputc
, B
.getInt32Ty(),
1804 B
.getInt32Ty(), File
->getType());
1805 if (File
->getType()->isPointerTy())
1806 inferNonMandatoryLibFuncAttrs(M
, FPutcName
, *TLI
);
1807 Char
= B
.CreateIntCast(Char
, B
.getInt32Ty(), /*isSigned*/true,
1809 CallInst
*CI
= B
.CreateCall(F
, {Char
, File
}, FPutcName
);
1811 if (const Function
*Fn
=
1812 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1813 CI
->setCallingConv(Fn
->getCallingConv());
1817 Value
*llvm::emitFPutS(Value
*Str
, Value
*File
, IRBuilderBase
&B
,
1818 const TargetLibraryInfo
*TLI
) {
1819 Module
*M
= B
.GetInsertBlock()->getModule();
1820 if (!isLibFuncEmittable(M
, TLI
, LibFunc_fputs
))
1823 StringRef FPutsName
= TLI
->getName(LibFunc_fputs
);
1824 FunctionCallee F
= getOrInsertLibFunc(M
, *TLI
, LibFunc_fputs
, B
.getInt32Ty(),
1825 B
.getInt8PtrTy(), File
->getType());
1826 if (File
->getType()->isPointerTy())
1827 inferNonMandatoryLibFuncAttrs(M
, FPutsName
, *TLI
);
1828 CallInst
*CI
= B
.CreateCall(F
, {castToCStr(Str
, B
), File
}, FPutsName
);
1830 if (const Function
*Fn
=
1831 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1832 CI
->setCallingConv(Fn
->getCallingConv());
1836 Value
*llvm::emitFWrite(Value
*Ptr
, Value
*Size
, Value
*File
, IRBuilderBase
&B
,
1837 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
) {
1838 Module
*M
= B
.GetInsertBlock()->getModule();
1839 if (!isLibFuncEmittable(M
, TLI
, LibFunc_fwrite
))
1842 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1843 StringRef FWriteName
= TLI
->getName(LibFunc_fwrite
);
1844 FunctionCallee F
= getOrInsertLibFunc(M
, *TLI
, LibFunc_fwrite
,
1845 DL
.getIntPtrType(Context
), B
.getInt8PtrTy(), DL
.getIntPtrType(Context
),
1846 DL
.getIntPtrType(Context
), File
->getType());
1848 if (File
->getType()->isPointerTy())
1849 inferNonMandatoryLibFuncAttrs(M
, FWriteName
, *TLI
);
1851 B
.CreateCall(F
, {castToCStr(Ptr
, B
), Size
,
1852 ConstantInt::get(DL
.getIntPtrType(Context
), 1), File
});
1854 if (const Function
*Fn
=
1855 dyn_cast
<Function
>(F
.getCallee()->stripPointerCasts()))
1856 CI
->setCallingConv(Fn
->getCallingConv());
1860 Value
*llvm::emitMalloc(Value
*Num
, IRBuilderBase
&B
, const DataLayout
&DL
,
1861 const TargetLibraryInfo
*TLI
) {
1862 Module
*M
= B
.GetInsertBlock()->getModule();
1863 if (!isLibFuncEmittable(M
, TLI
, LibFunc_malloc
))
1866 StringRef MallocName
= TLI
->getName(LibFunc_malloc
);
1867 LLVMContext
&Context
= B
.GetInsertBlock()->getContext();
1868 FunctionCallee Malloc
= getOrInsertLibFunc(M
, *TLI
, LibFunc_malloc
,
1869 B
.getInt8PtrTy(), DL
.getIntPtrType(Context
));
1870 inferNonMandatoryLibFuncAttrs(M
, MallocName
, *TLI
);
1871 CallInst
*CI
= B
.CreateCall(Malloc
, Num
, MallocName
);
1873 if (const Function
*F
=
1874 dyn_cast
<Function
>(Malloc
.getCallee()->stripPointerCasts()))
1875 CI
->setCallingConv(F
->getCallingConv());
1880 Value
*llvm::emitCalloc(Value
*Num
, Value
*Size
, IRBuilderBase
&B
,
1881 const TargetLibraryInfo
&TLI
) {
1882 Module
*M
= B
.GetInsertBlock()->getModule();
1883 if (!isLibFuncEmittable(M
, &TLI
, LibFunc_calloc
))
1886 StringRef CallocName
= TLI
.getName(LibFunc_calloc
);
1887 const DataLayout
&DL
= M
->getDataLayout();
1888 IntegerType
*PtrType
= DL
.getIntPtrType((B
.GetInsertBlock()->getContext()));
1889 FunctionCallee Calloc
= getOrInsertLibFunc(M
, TLI
, LibFunc_calloc
,
1890 B
.getInt8PtrTy(), PtrType
, PtrType
);
1891 inferNonMandatoryLibFuncAttrs(M
, CallocName
, TLI
);
1892 CallInst
*CI
= B
.CreateCall(Calloc
, {Num
, Size
}, CallocName
);
1895 dyn_cast
<Function
>(Calloc
.getCallee()->stripPointerCasts()))
1896 CI
->setCallingConv(F
->getCallingConv());