LLVM 20.0.0git
BuildLibCalls.cpp
Go to the documentation of this file.
1//===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements some functions that will create standard C libcalls.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/ADT/Statistic.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"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/Module.h"
26#include "llvm/IR/Type.h"
28#include <optional>
29
30using namespace llvm;
31
32#define DEBUG_TYPE "build-libcalls"
33
34//- Infer Attributes ---------------------------------------------------------//
35
36STATISTIC(NumReadNone, "Number of functions inferred as readnone");
37STATISTIC(NumInaccessibleMemOnly,
38 "Number of functions inferred as inaccessiblememonly");
39STATISTIC(NumReadOnly, "Number of functions inferred as readonly");
40STATISTIC(NumWriteOnly, "Number of functions inferred as writeonly");
41STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly");
42STATISTIC(NumInaccessibleMemOrArgMemOnly,
43 "Number of functions inferred as inaccessiblemem_or_argmemonly");
44STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind");
45STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture");
46STATISTIC(NumWriteOnlyArg, "Number of arguments inferred as writeonly");
47STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly");
48STATISTIC(NumNoAlias, "Number of function returns inferred as noalias");
49STATISTIC(NumNoUndef, "Number of function returns inferred as noundef returns");
50STATISTIC(NumReturnedArg, "Number of arguments inferred as returned");
51STATISTIC(NumWillReturn, "Number of functions inferred as willreturn");
52STATISTIC(NumCold, "Number of functions inferred as cold");
53STATISTIC(NumNoReturn, "Number of functions inferred as no return");
54
56 if (F.doesNotAccessMemory())
57 return false;
58 F.setDoesNotAccessMemory();
59 ++NumReadNone;
60 return true;
61}
62
63static bool setIsCold(Function &F) {
64 if (F.hasFnAttribute(Attribute::Cold))
65 return false;
66 F.addFnAttr(Attribute::Cold);
67 ++NumCold;
68 return true;
69}
70
71static bool setNoReturn(Function &F) {
72 if (F.hasFnAttribute(Attribute::NoReturn))
73 return false;
74 F.addFnAttr(Attribute::NoReturn);
75 ++NumNoReturn;
76 return true;
77}
78
80 if (F.onlyAccessesInaccessibleMemory())
81 return false;
82 F.setOnlyAccessesInaccessibleMemory();
83 ++NumInaccessibleMemOnly;
84 return true;
85}
86
88 if (F.onlyReadsMemory())
89 return false;
90 F.setOnlyReadsMemory();
91 ++NumReadOnly;
92 return true;
93}
94
96 if (F.onlyWritesMemory()) // writeonly or readnone
97 return false;
98 ++NumWriteOnly;
99 F.setOnlyWritesMemory();
100 return true;
101}
102
104 if (F.onlyAccessesArgMemory())
105 return false;
106 F.setOnlyAccessesArgMemory();
107 ++NumArgMemOnly;
108 return true;
109}
110
112 if (F.onlyAccessesInaccessibleMemOrArgMem())
113 return false;
114 F.setOnlyAccessesInaccessibleMemOrArgMem();
115 ++NumInaccessibleMemOrArgMemOnly;
116 return true;
117}
118
120 if (F.doesNotThrow())
121 return false;
122 F.setDoesNotThrow();
123 ++NumNoUnwind;
124 return true;
125}
126
128 if (F.hasRetAttribute(Attribute::NoAlias))
129 return false;
130 F.addRetAttr(Attribute::NoAlias);
131 ++NumNoAlias;
132 return true;
133}
134
135static bool setDoesNotCapture(Function &F, unsigned ArgNo) {
136 if (F.hasParamAttribute(ArgNo, Attribute::NoCapture))
137 return false;
138 F.addParamAttr(ArgNo, Attribute::NoCapture);
139 ++NumNoCapture;
140 return true;
141}
142
143static bool setDoesNotAlias(Function &F, unsigned ArgNo) {
144 if (F.hasParamAttribute(ArgNo, Attribute::NoAlias))
145 return false;
146 F.addParamAttr(ArgNo, Attribute::NoAlias);
147 ++NumNoAlias;
148 return true;
149}
150
151static bool setOnlyReadsMemory(Function &F, unsigned ArgNo) {
152 if (F.hasParamAttribute(ArgNo, Attribute::ReadOnly))
153 return false;
154 F.addParamAttr(ArgNo, Attribute::ReadOnly);
155 ++NumReadOnlyArg;
156 return true;
157}
158
159static bool setOnlyWritesMemory(Function &F, unsigned ArgNo) {
160 if (F.hasParamAttribute(ArgNo, Attribute::WriteOnly))
161 return false;
162 F.addParamAttr(ArgNo, Attribute::WriteOnly);
163 ++NumWriteOnlyArg;
164 return true;
165}
166
167static bool setRetNoUndef(Function &F) {
168 if (!F.getReturnType()->isVoidTy() &&
169 !F.hasRetAttribute(Attribute::NoUndef)) {
170 F.addRetAttr(Attribute::NoUndef);
171 ++NumNoUndef;
172 return true;
173 }
174 return false;
175}
176
177static bool setArgsNoUndef(Function &F) {
178 bool Changed = false;
179 for (unsigned ArgNo = 0; ArgNo < F.arg_size(); ++ArgNo) {
180 if (!F.hasParamAttribute(ArgNo, Attribute::NoUndef)) {
181 F.addParamAttr(ArgNo, Attribute::NoUndef);
182 ++NumNoUndef;
183 Changed = true;
184 }
185 }
186 return Changed;
187}
188
189static bool setArgNoUndef(Function &F, unsigned ArgNo) {
190 if (F.hasParamAttribute(ArgNo, Attribute::NoUndef))
191 return false;
192 F.addParamAttr(ArgNo, Attribute::NoUndef);
193 ++NumNoUndef;
194 return true;
195}
196
198 bool UndefAdded = false;
199 UndefAdded |= setRetNoUndef(F);
200 UndefAdded |= setArgsNoUndef(F);
201 return UndefAdded;
202}
203
204static bool setReturnedArg(Function &F, unsigned ArgNo) {
205 if (F.hasParamAttribute(ArgNo, Attribute::Returned))
206 return false;
207 F.addParamAttr(ArgNo, Attribute::Returned);
208 ++NumReturnedArg;
209 return true;
210}
211
212static bool setNonLazyBind(Function &F) {
213 if (F.hasFnAttribute(Attribute::NonLazyBind))
214 return false;
215 F.addFnAttr(Attribute::NonLazyBind);
216 return true;
217}
218
220 if (F.hasFnAttribute(Attribute::NoFree))
221 return false;
222 F.addFnAttr(Attribute::NoFree);
223 return true;
224}
225
226static bool setWillReturn(Function &F) {
227 if (F.hasFnAttribute(Attribute::WillReturn))
228 return false;
229 F.addFnAttr(Attribute::WillReturn);
230 ++NumWillReturn;
231 return true;
232}
233
234static bool setAlignedAllocParam(Function &F, unsigned ArgNo) {
235 if (F.hasParamAttribute(ArgNo, Attribute::AllocAlign))
236 return false;
237 F.addParamAttr(ArgNo, Attribute::AllocAlign);
238 return true;
239}
240
241static bool setAllocatedPointerParam(Function &F, unsigned ArgNo) {
242 if (F.hasParamAttribute(ArgNo, Attribute::AllocatedPointer))
243 return false;
244 F.addParamAttr(ArgNo, Attribute::AllocatedPointer);
245 return true;
246}
247
248static bool setAllocSize(Function &F, unsigned ElemSizeArg,
249 std::optional<unsigned> NumElemsArg) {
250 if (F.hasFnAttribute(Attribute::AllocSize))
251 return false;
252 F.addFnAttr(Attribute::getWithAllocSizeArgs(F.getContext(), ElemSizeArg,
253 NumElemsArg));
254 return true;
255}
256
257static bool setAllocFamily(Function &F, StringRef Family) {
258 if (F.hasFnAttribute("alloc-family"))
259 return false;
260 F.addFnAttr("alloc-family", Family);
261 return true;
262}
263
265 if (F.hasFnAttribute(Attribute::AllocKind))
266 return false;
267 F.addFnAttr(
268 Attribute::get(F.getContext(), Attribute::AllocKind, uint64_t(K)));
269 return true;
270}
271
273 const TargetLibraryInfo &TLI) {
274 Function *F = M->getFunction(Name);
275 if (!F)
276 return false;
277 return inferNonMandatoryLibFuncAttrs(*F, TLI);
278}
279
281 const TargetLibraryInfo &TLI) {
282 LibFunc TheLibFunc;
283 if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc)))
284 return false;
285
286 bool Changed = false;
287
288 if (F.getParent() != nullptr && F.getParent()->getRtLibUseGOT())
289 Changed |= setNonLazyBind(F);
290
291 switch (TheLibFunc) {
292 case LibFunc_nan:
293 case LibFunc_nanf:
294 case LibFunc_nanl:
295 case LibFunc_strlen:
296 case LibFunc_strnlen:
297 case LibFunc_wcslen:
298 Changed |= setOnlyReadsMemory(F);
299 Changed |= setDoesNotThrow(F);
300 Changed |= setOnlyAccessesArgMemory(F);
301 Changed |= setWillReturn(F);
302 Changed |= setDoesNotCapture(F, 0);
303 break;
304 case LibFunc_strchr:
305 case LibFunc_strrchr:
306 Changed |= setOnlyAccessesArgMemory(F);
307 Changed |= setOnlyReadsMemory(F);
308 Changed |= setDoesNotThrow(F);
309 Changed |= setWillReturn(F);
310 break;
311 case LibFunc_strtol:
312 case LibFunc_strtod:
313 case LibFunc_strtof:
314 case LibFunc_strtoul:
315 case LibFunc_strtoll:
316 case LibFunc_strtold:
317 case LibFunc_strtoull:
318 Changed |= setDoesNotThrow(F);
319 Changed |= setWillReturn(F);
320 Changed |= setDoesNotCapture(F, 1);
321 Changed |= setOnlyReadsMemory(F, 0);
322 break;
323 case LibFunc_strcat:
324 case LibFunc_strncat:
325 Changed |= setOnlyAccessesArgMemory(F);
326 Changed |= setDoesNotThrow(F);
327 Changed |= setWillReturn(F);
328 Changed |= setReturnedArg(F, 0);
329 Changed |= setDoesNotCapture(F, 1);
330 Changed |= setOnlyReadsMemory(F, 1);
331 Changed |= setDoesNotAlias(F, 0);
332 Changed |= setDoesNotAlias(F, 1);
333 break;
334 case LibFunc_strcpy:
335 case LibFunc_strncpy:
336 Changed |= setReturnedArg(F, 0);
337 [[fallthrough]];
338 case LibFunc_stpcpy:
339 case LibFunc_stpncpy:
340 Changed |= setOnlyAccessesArgMemory(F);
341 Changed |= setDoesNotThrow(F);
342 Changed |= setWillReturn(F);
343 Changed |= setDoesNotCapture(F, 1);
344 Changed |= setOnlyWritesMemory(F, 0);
345 Changed |= setOnlyReadsMemory(F, 1);
346 Changed |= setDoesNotAlias(F, 0);
347 Changed |= setDoesNotAlias(F, 1);
348 break;
349 case LibFunc_strxfrm:
350 Changed |= setDoesNotThrow(F);
351 Changed |= setWillReturn(F);
352 Changed |= setDoesNotCapture(F, 0);
353 Changed |= setDoesNotCapture(F, 1);
354 Changed |= setOnlyReadsMemory(F, 1);
355 break;
356 case LibFunc_strcmp: // 0,1
357 case LibFunc_strspn: // 0,1
358 case LibFunc_strncmp: // 0,1
359 case LibFunc_strcspn: // 0,1
360 Changed |= setDoesNotThrow(F);
361 Changed |= setOnlyAccessesArgMemory(F);
362 Changed |= setWillReturn(F);
363 Changed |= setOnlyReadsMemory(F);
364 Changed |= setDoesNotCapture(F, 0);
365 Changed |= setDoesNotCapture(F, 1);
366 break;
367 case LibFunc_strcoll:
368 case LibFunc_strcasecmp: // 0,1
369 case LibFunc_strncasecmp: //
370 // Those functions may depend on the locale, which may be accessed through
371 // global memory.
372 Changed |= setOnlyReadsMemory(F);
373 Changed |= setDoesNotThrow(F);
374 Changed |= setWillReturn(F);
375 Changed |= setDoesNotCapture(F, 0);
376 Changed |= setDoesNotCapture(F, 1);
377 break;
378 case LibFunc_strstr:
379 case LibFunc_strpbrk:
380 Changed |= setOnlyAccessesArgMemory(F);
381 Changed |= setOnlyReadsMemory(F);
382 Changed |= setDoesNotThrow(F);
383 Changed |= setWillReturn(F);
384 Changed |= setDoesNotCapture(F, 1);
385 break;
386 case LibFunc_strtok:
387 case LibFunc_strtok_r:
388 Changed |= setDoesNotThrow(F);
389 Changed |= setWillReturn(F);
390 Changed |= setDoesNotCapture(F, 1);
391 Changed |= setOnlyReadsMemory(F, 1);
392 break;
393 case LibFunc_scanf:
394 Changed |= setRetAndArgsNoUndef(F);
395 Changed |= setDoesNotThrow(F);
396 Changed |= setDoesNotCapture(F, 0);
397 Changed |= setOnlyReadsMemory(F, 0);
398 break;
399 case LibFunc_setbuf:
400 case LibFunc_setvbuf:
401 Changed |= setRetAndArgsNoUndef(F);
402 Changed |= setDoesNotThrow(F);
403 Changed |= setDoesNotCapture(F, 0);
404 break;
405 case LibFunc_strndup:
406 Changed |= setArgNoUndef(F, 1);
407 [[fallthrough]];
408 case LibFunc_strdup:
409 Changed |= setAllocFamily(F, "malloc");
411 Changed |= setDoesNotThrow(F);
412 Changed |= setRetDoesNotAlias(F);
413 Changed |= setWillReturn(F);
414 Changed |= setDoesNotCapture(F, 0);
415 Changed |= setOnlyReadsMemory(F, 0);
416 break;
417 case LibFunc_stat:
418 case LibFunc_statvfs:
419 Changed |= setRetAndArgsNoUndef(F);
420 Changed |= setDoesNotThrow(F);
421 Changed |= setDoesNotCapture(F, 0);
422 Changed |= setDoesNotCapture(F, 1);
423 Changed |= setOnlyReadsMemory(F, 0);
424 break;
425 case LibFunc_sscanf:
426 Changed |= setRetAndArgsNoUndef(F);
427 Changed |= setDoesNotThrow(F);
428 Changed |= setDoesNotCapture(F, 0);
429 Changed |= setDoesNotCapture(F, 1);
430 Changed |= setOnlyReadsMemory(F, 0);
431 Changed |= setOnlyReadsMemory(F, 1);
432 break;
433 case LibFunc_sprintf:
434 Changed |= setRetAndArgsNoUndef(F);
435 Changed |= setDoesNotThrow(F);
436 Changed |= setDoesNotCapture(F, 0);
437 Changed |= setDoesNotAlias(F, 0);
438 Changed |= setOnlyWritesMemory(F, 0);
439 Changed |= setDoesNotCapture(F, 1);
440 Changed |= setOnlyReadsMemory(F, 1);
441 break;
442 case LibFunc_snprintf:
443 Changed |= setRetAndArgsNoUndef(F);
444 Changed |= setDoesNotThrow(F);
445 Changed |= setDoesNotCapture(F, 0);
446 Changed |= setDoesNotAlias(F, 0);
447 Changed |= setOnlyWritesMemory(F, 0);
448 Changed |= setDoesNotCapture(F, 2);
449 Changed |= setOnlyReadsMemory(F, 2);
450 break;
451 case LibFunc_setitimer:
452 Changed |= setRetAndArgsNoUndef(F);
453 Changed |= setDoesNotThrow(F);
454 Changed |= setWillReturn(F);
455 Changed |= setDoesNotCapture(F, 1);
456 Changed |= setDoesNotCapture(F, 2);
457 Changed |= setOnlyReadsMemory(F, 1);
458 break;
459 case LibFunc_system:
460 // May throw; "system" is a valid pthread cancellation point.
461 Changed |= setRetAndArgsNoUndef(F);
462 Changed |= setDoesNotCapture(F, 0);
463 Changed |= setOnlyReadsMemory(F, 0);
464 break;
465 case LibFunc_aligned_alloc:
466 Changed |= setAlignedAllocParam(F, 0);
467 Changed |= setAllocSize(F, 1, std::nullopt);
468 Changed |= setAllocKind(F, AllocFnKind::Alloc | AllocFnKind::Uninitialized | AllocFnKind::Aligned);
469 [[fallthrough]];
470 case LibFunc_valloc:
471 case LibFunc_malloc:
472 case LibFunc_vec_malloc:
473 Changed |= setAllocFamily(F, TheLibFunc == LibFunc_vec_malloc ? "vec_malloc"
474 : "malloc");
475 Changed |= setAllocKind(F, AllocFnKind::Alloc | AllocFnKind::Uninitialized);
476 Changed |= setAllocSize(F, 0, std::nullopt);
478 Changed |= setRetAndArgsNoUndef(F);
479 Changed |= setDoesNotThrow(F);
480 Changed |= setRetDoesNotAlias(F);
481 Changed |= setWillReturn(F);
482 break;
483 case LibFunc_memcmp:
484 Changed |= setOnlyAccessesArgMemory(F);
485 Changed |= setOnlyReadsMemory(F);
486 Changed |= setDoesNotThrow(F);
487 Changed |= setWillReturn(F);
488 Changed |= setDoesNotCapture(F, 0);
489 Changed |= setDoesNotCapture(F, 1);
490 break;
491 case LibFunc_memchr:
492 case LibFunc_memrchr:
493 Changed |= setDoesNotThrow(F);
494 Changed |= setOnlyAccessesArgMemory(F);
495 Changed |= setOnlyReadsMemory(F);
496 Changed |= setWillReturn(F);
497 break;
498 case LibFunc_modf:
499 case LibFunc_modff:
500 case LibFunc_modfl:
501 Changed |= setDoesNotThrow(F);
502 Changed |= setWillReturn(F);
503 Changed |= setOnlyAccessesArgMemory(F);
504 Changed |= setOnlyWritesMemory(F);
505 Changed |= setDoesNotCapture(F, 1);
506 break;
507 case LibFunc_memcpy:
508 Changed |= setDoesNotThrow(F);
509 Changed |= setOnlyAccessesArgMemory(F);
510 Changed |= setWillReturn(F);
511 Changed |= setDoesNotAlias(F, 0);
512 Changed |= setReturnedArg(F, 0);
513 Changed |= setOnlyWritesMemory(F, 0);
514 Changed |= setDoesNotAlias(F, 1);
515 Changed |= setDoesNotCapture(F, 1);
516 Changed |= setOnlyReadsMemory(F, 1);
517 break;
518 case LibFunc_memmove:
519 Changed |= setDoesNotThrow(F);
520 Changed |= setOnlyAccessesArgMemory(F);
521 Changed |= setWillReturn(F);
522 Changed |= setReturnedArg(F, 0);
523 Changed |= setOnlyWritesMemory(F, 0);
524 Changed |= setDoesNotCapture(F, 1);
525 Changed |= setOnlyReadsMemory(F, 1);
526 break;
527 case LibFunc_mempcpy:
528 case LibFunc_memccpy:
529 Changed |= setWillReturn(F);
530 [[fallthrough]];
531 case LibFunc_memcpy_chk:
532 Changed |= setDoesNotThrow(F);
533 Changed |= setOnlyAccessesArgMemory(F);
534 Changed |= setDoesNotAlias(F, 0);
535 Changed |= setOnlyWritesMemory(F, 0);
536 Changed |= setDoesNotAlias(F, 1);
537 Changed |= setDoesNotCapture(F, 1);
538 Changed |= setOnlyReadsMemory(F, 1);
539 break;
540 case LibFunc_memalign:
541 Changed |= setAllocFamily(F, "malloc");
542 Changed |= setAllocKind(F, AllocFnKind::Alloc | AllocFnKind::Aligned |
543 AllocFnKind::Uninitialized);
544 Changed |= setAllocSize(F, 1, std::nullopt);
545 Changed |= setAlignedAllocParam(F, 0);
547 Changed |= setRetNoUndef(F);
548 Changed |= setDoesNotThrow(F);
549 Changed |= setRetDoesNotAlias(F);
550 Changed |= setWillReturn(F);
551 break;
552 case LibFunc_mkdir:
553 Changed |= setRetAndArgsNoUndef(F);
554 Changed |= setDoesNotThrow(F);
555 Changed |= setDoesNotCapture(F, 0);
556 Changed |= setOnlyReadsMemory(F, 0);
557 break;
558 case LibFunc_mktime:
559 Changed |= setRetAndArgsNoUndef(F);
560 Changed |= setDoesNotThrow(F);
561 Changed |= setWillReturn(F);
562 Changed |= setDoesNotCapture(F, 0);
563 break;
564 case LibFunc_realloc:
565 case LibFunc_reallocf:
566 case LibFunc_vec_realloc:
567 Changed |= setAllocFamily(
568 F, TheLibFunc == LibFunc_vec_realloc ? "vec_malloc" : "malloc");
569 Changed |= setAllocKind(F, AllocFnKind::Realloc);
570 Changed |= setAllocatedPointerParam(F, 0);
571 Changed |= setAllocSize(F, 1, std::nullopt);
573 Changed |= setRetNoUndef(F);
574 Changed |= setDoesNotThrow(F);
575 Changed |= setRetDoesNotAlias(F);
576 Changed |= setWillReturn(F);
577 Changed |= setDoesNotCapture(F, 0);
578 Changed |= setArgNoUndef(F, 1);
579 break;
580 case LibFunc_reallocarray:
581 Changed |= setAllocFamily(F, "malloc");
582 Changed |= setAllocKind(F, AllocFnKind::Realloc);
583 Changed |= setAllocatedPointerParam(F, 0);
584 Changed |= setAllocSize(F, 1, 2);
586 Changed |= setRetNoUndef(F);
587 Changed |= setDoesNotThrow(F);
588 Changed |= setRetDoesNotAlias(F);
589 Changed |= setWillReturn(F);
590 Changed |= setDoesNotCapture(F, 0);
591 Changed |= setArgNoUndef(F, 1);
592 Changed |= setArgNoUndef(F, 2);
593 break;
594 case LibFunc_read:
595 // May throw; "read" is a valid pthread cancellation point.
596 Changed |= setRetAndArgsNoUndef(F);
597 Changed |= setDoesNotCapture(F, 1);
598 break;
599 case LibFunc_rewind:
600 Changed |= setRetAndArgsNoUndef(F);
601 Changed |= setDoesNotThrow(F);
602 Changed |= setDoesNotCapture(F, 0);
603 break;
604 case LibFunc_rmdir:
605 case LibFunc_remove:
606 case LibFunc_realpath:
607 Changed |= setRetAndArgsNoUndef(F);
608 Changed |= setDoesNotThrow(F);
609 Changed |= setDoesNotCapture(F, 0);
610 Changed |= setOnlyReadsMemory(F, 0);
611 break;
612 case LibFunc_rename:
613 Changed |= setRetAndArgsNoUndef(F);
614 Changed |= setDoesNotThrow(F);
615 Changed |= setDoesNotCapture(F, 0);
616 Changed |= setDoesNotCapture(F, 1);
617 Changed |= setOnlyReadsMemory(F, 0);
618 Changed |= setOnlyReadsMemory(F, 1);
619 break;
620 case LibFunc_readlink:
621 Changed |= setRetAndArgsNoUndef(F);
622 Changed |= setDoesNotThrow(F);
623 Changed |= setDoesNotCapture(F, 0);
624 Changed |= setDoesNotCapture(F, 1);
625 Changed |= setOnlyReadsMemory(F, 0);
626 break;
627 case LibFunc_write:
628 // May throw; "write" is a valid pthread cancellation point.
629 Changed |= setRetAndArgsNoUndef(F);
630 Changed |= setDoesNotCapture(F, 1);
631 Changed |= setOnlyReadsMemory(F, 1);
632 break;
633 case LibFunc_bcopy:
634 Changed |= setDoesNotThrow(F);
635 Changed |= setOnlyAccessesArgMemory(F);
636 Changed |= setWillReturn(F);
637 Changed |= setDoesNotCapture(F, 0);
638 Changed |= setOnlyReadsMemory(F, 0);
639 Changed |= setOnlyWritesMemory(F, 1);
640 Changed |= setDoesNotCapture(F, 1);
641 break;
642 case LibFunc_bcmp:
643 Changed |= setDoesNotThrow(F);
644 Changed |= setOnlyAccessesArgMemory(F);
645 Changed |= setOnlyReadsMemory(F);
646 Changed |= setWillReturn(F);
647 Changed |= setDoesNotCapture(F, 0);
648 Changed |= setDoesNotCapture(F, 1);
649 break;
650 case LibFunc_bzero:
651 Changed |= setDoesNotThrow(F);
652 Changed |= setOnlyAccessesArgMemory(F);
653 Changed |= setWillReturn(F);
654 Changed |= setDoesNotCapture(F, 0);
655 Changed |= setOnlyWritesMemory(F, 0);
656 break;
657 case LibFunc_calloc:
658 case LibFunc_vec_calloc:
659 Changed |= setAllocFamily(F, TheLibFunc == LibFunc_vec_calloc ? "vec_malloc"
660 : "malloc");
661 Changed |= setAllocKind(F, AllocFnKind::Alloc | AllocFnKind::Zeroed);
662 Changed |= setAllocSize(F, 0, 1);
664 Changed |= setRetAndArgsNoUndef(F);
665 Changed |= setDoesNotThrow(F);
666 Changed |= setRetDoesNotAlias(F);
667 Changed |= setWillReturn(F);
668 break;
669 case LibFunc_chmod:
670 case LibFunc_chown:
671 Changed |= setRetAndArgsNoUndef(F);
672 Changed |= setDoesNotThrow(F);
673 Changed |= setDoesNotCapture(F, 0);
674 Changed |= setOnlyReadsMemory(F, 0);
675 break;
676 case LibFunc_ctermid:
677 case LibFunc_clearerr:
678 case LibFunc_closedir:
679 Changed |= setRetAndArgsNoUndef(F);
680 Changed |= setDoesNotThrow(F);
681 Changed |= setDoesNotCapture(F, 0);
682 break;
683 case LibFunc_atoi:
684 case LibFunc_atol:
685 case LibFunc_atof:
686 case LibFunc_atoll:
687 Changed |= setDoesNotThrow(F);
688 Changed |= setOnlyReadsMemory(F);
689 Changed |= setWillReturn(F);
690 Changed |= setDoesNotCapture(F, 0);
691 break;
692 case LibFunc_access:
693 Changed |= setRetAndArgsNoUndef(F);
694 Changed |= setDoesNotThrow(F);
695 Changed |= setDoesNotCapture(F, 0);
696 Changed |= setOnlyReadsMemory(F, 0);
697 break;
698 case LibFunc_fopen:
699 Changed |= setRetAndArgsNoUndef(F);
700 Changed |= setDoesNotThrow(F);
701 Changed |= setRetDoesNotAlias(F);
702 Changed |= setDoesNotCapture(F, 0);
703 Changed |= setDoesNotCapture(F, 1);
704 Changed |= setOnlyReadsMemory(F, 0);
705 Changed |= setOnlyReadsMemory(F, 1);
706 break;
707 case LibFunc_fdopen:
708 Changed |= setRetAndArgsNoUndef(F);
709 Changed |= setDoesNotThrow(F);
710 Changed |= setRetDoesNotAlias(F);
711 Changed |= setDoesNotCapture(F, 1);
712 Changed |= setOnlyReadsMemory(F, 1);
713 break;
714 case LibFunc_feof:
715 Changed |= setRetAndArgsNoUndef(F);
716 Changed |= setDoesNotThrow(F);
717 Changed |= setDoesNotCapture(F, 0);
718 break;
719 case LibFunc_free:
720 case LibFunc_vec_free:
721 Changed |= setAllocFamily(F, TheLibFunc == LibFunc_vec_free ? "vec_malloc"
722 : "malloc");
723 Changed |= setAllocKind(F, AllocFnKind::Free);
724 Changed |= setAllocatedPointerParam(F, 0);
726 Changed |= setArgsNoUndef(F);
727 Changed |= setDoesNotThrow(F);
728 Changed |= setWillReturn(F);
729 Changed |= setDoesNotCapture(F, 0);
730 break;
731 case LibFunc_fseek:
732 case LibFunc_ftell:
733 case LibFunc_fgetc:
734 case LibFunc_fgetc_unlocked:
735 case LibFunc_fseeko:
736 case LibFunc_ftello:
737 case LibFunc_fileno:
738 case LibFunc_fflush:
739 case LibFunc_fclose:
740 case LibFunc_fsetpos:
741 case LibFunc_flockfile:
742 case LibFunc_funlockfile:
743 case LibFunc_ftrylockfile:
744 Changed |= setRetAndArgsNoUndef(F);
745 Changed |= setDoesNotThrow(F);
746 Changed |= setDoesNotCapture(F, 0);
747 break;
748 case LibFunc_ferror:
749 Changed |= setRetAndArgsNoUndef(F);
750 Changed |= setDoesNotThrow(F);
751 Changed |= setDoesNotCapture(F, 0);
752 Changed |= setOnlyReadsMemory(F);
753 break;
754 case LibFunc_fputc:
755 case LibFunc_fputc_unlocked:
756 case LibFunc_fstat:
757 Changed |= setRetAndArgsNoUndef(F);
758 Changed |= setDoesNotThrow(F);
759 Changed |= setDoesNotCapture(F, 1);
760 break;
761 case LibFunc_frexp:
762 case LibFunc_frexpf:
763 case LibFunc_frexpl:
764 Changed |= setDoesNotThrow(F);
765 Changed |= setWillReturn(F);
766 Changed |= setOnlyAccessesArgMemory(F);
767 Changed |= setOnlyWritesMemory(F);
768 Changed |= setDoesNotCapture(F, 1);
769 break;
770 case LibFunc_fstatvfs:
771 Changed |= setRetAndArgsNoUndef(F);
772 Changed |= setDoesNotThrow(F);
773 Changed |= setDoesNotCapture(F, 1);
774 break;
775 case LibFunc_fgets:
776 case LibFunc_fgets_unlocked:
777 Changed |= setRetAndArgsNoUndef(F);
778 Changed |= setDoesNotThrow(F);
779 Changed |= setDoesNotCapture(F, 2);
780 break;
781 case LibFunc_fread:
782 case LibFunc_fread_unlocked:
783 Changed |= setRetAndArgsNoUndef(F);
784 Changed |= setDoesNotThrow(F);
785 Changed |= setDoesNotCapture(F, 0);
786 Changed |= setDoesNotCapture(F, 3);
787 break;
788 case LibFunc_fwrite:
789 case LibFunc_fwrite_unlocked:
790 Changed |= setRetAndArgsNoUndef(F);
791 Changed |= setDoesNotThrow(F);
792 Changed |= setDoesNotCapture(F, 0);
793 Changed |= setDoesNotCapture(F, 3);
794 // FIXME: readonly #1?
795 break;
796 case LibFunc_fputs:
797 case LibFunc_fputs_unlocked:
798 Changed |= setRetAndArgsNoUndef(F);
799 Changed |= setDoesNotThrow(F);
800 Changed |= setDoesNotCapture(F, 0);
801 Changed |= setDoesNotCapture(F, 1);
802 Changed |= setOnlyReadsMemory(F, 0);
803 break;
804 case LibFunc_fscanf:
805 case LibFunc_fprintf:
806 Changed |= setRetAndArgsNoUndef(F);
807 Changed |= setDoesNotThrow(F);
808 Changed |= setDoesNotCapture(F, 0);
809 Changed |= setDoesNotCapture(F, 1);
810 Changed |= setOnlyReadsMemory(F, 1);
811 break;
812 case LibFunc_fgetpos:
813 Changed |= setRetAndArgsNoUndef(F);
814 Changed |= setDoesNotThrow(F);
815 Changed |= setDoesNotCapture(F, 0);
816 Changed |= setDoesNotCapture(F, 1);
817 break;
818 case LibFunc_getc:
819 Changed |= setRetAndArgsNoUndef(F);
820 Changed |= setDoesNotThrow(F);
821 Changed |= setDoesNotCapture(F, 0);
822 break;
823 case LibFunc_getlogin_r:
824 Changed |= setRetAndArgsNoUndef(F);
825 Changed |= setDoesNotThrow(F);
826 Changed |= setDoesNotCapture(F, 0);
827 break;
828 case LibFunc_getc_unlocked:
829 Changed |= setRetAndArgsNoUndef(F);
830 Changed |= setDoesNotThrow(F);
831 Changed |= setDoesNotCapture(F, 0);
832 break;
833 case LibFunc_getenv:
834 Changed |= setRetAndArgsNoUndef(F);
835 Changed |= setDoesNotThrow(F);
836 Changed |= setOnlyReadsMemory(F);
837 Changed |= setDoesNotCapture(F, 0);
838 break;
839 case LibFunc_gets:
840 case LibFunc_getchar:
841 case LibFunc_getchar_unlocked:
842 Changed |= setRetAndArgsNoUndef(F);
843 Changed |= setDoesNotThrow(F);
844 break;
845 case LibFunc_getitimer:
846 Changed |= setRetAndArgsNoUndef(F);
847 Changed |= setDoesNotThrow(F);
848 Changed |= setDoesNotCapture(F, 1);
849 break;
850 case LibFunc_getpwnam:
851 Changed |= setRetAndArgsNoUndef(F);
852 Changed |= setDoesNotThrow(F);
853 Changed |= setDoesNotCapture(F, 0);
854 Changed |= setOnlyReadsMemory(F, 0);
855 break;
856 case LibFunc_ungetc:
857 Changed |= setRetAndArgsNoUndef(F);
858 Changed |= setDoesNotThrow(F);
859 Changed |= setDoesNotCapture(F, 1);
860 break;
861 case LibFunc_uname:
862 Changed |= setRetAndArgsNoUndef(F);
863 Changed |= setDoesNotThrow(F);
864 Changed |= setDoesNotCapture(F, 0);
865 break;
866 case LibFunc_unlink:
867 Changed |= setRetAndArgsNoUndef(F);
868 Changed |= setDoesNotThrow(F);
869 Changed |= setDoesNotCapture(F, 0);
870 Changed |= setOnlyReadsMemory(F, 0);
871 break;
872 case LibFunc_unsetenv:
873 Changed |= setRetAndArgsNoUndef(F);
874 Changed |= setDoesNotThrow(F);
875 Changed |= setDoesNotCapture(F, 0);
876 Changed |= setOnlyReadsMemory(F, 0);
877 break;
878 case LibFunc_utime:
879 case LibFunc_utimes:
880 Changed |= setRetAndArgsNoUndef(F);
881 Changed |= setDoesNotThrow(F);
882 Changed |= setDoesNotCapture(F, 0);
883 Changed |= setDoesNotCapture(F, 1);
884 Changed |= setOnlyReadsMemory(F, 0);
885 Changed |= setOnlyReadsMemory(F, 1);
886 break;
887 case LibFunc_putc:
888 case LibFunc_putc_unlocked:
889 Changed |= setRetAndArgsNoUndef(F);
890 Changed |= setDoesNotThrow(F);
891 Changed |= setDoesNotCapture(F, 1);
892 break;
893 case LibFunc_puts:
894 case LibFunc_printf:
895 case LibFunc_perror:
896 Changed |= setRetAndArgsNoUndef(F);
897 Changed |= setDoesNotThrow(F);
898 Changed |= setDoesNotCapture(F, 0);
899 Changed |= setOnlyReadsMemory(F, 0);
900 break;
901 case LibFunc_pread:
902 // May throw; "pread" is a valid pthread cancellation point.
903 Changed |= setRetAndArgsNoUndef(F);
904 Changed |= setDoesNotCapture(F, 1);
905 break;
906 case LibFunc_pwrite:
907 // May throw; "pwrite" is a valid pthread cancellation point.
908 Changed |= setRetAndArgsNoUndef(F);
909 Changed |= setDoesNotCapture(F, 1);
910 Changed |= setOnlyReadsMemory(F, 1);
911 break;
912 case LibFunc_putchar:
913 case LibFunc_putchar_unlocked:
914 Changed |= setRetAndArgsNoUndef(F);
915 Changed |= setDoesNotThrow(F);
916 break;
917 case LibFunc_popen:
918 Changed |= setRetAndArgsNoUndef(F);
919 Changed |= setDoesNotThrow(F);
920 Changed |= setRetDoesNotAlias(F);
921 Changed |= setDoesNotCapture(F, 0);
922 Changed |= setDoesNotCapture(F, 1);
923 Changed |= setOnlyReadsMemory(F, 0);
924 Changed |= setOnlyReadsMemory(F, 1);
925 break;
926 case LibFunc_pclose:
927 Changed |= setRetAndArgsNoUndef(F);
928 Changed |= setDoesNotThrow(F);
929 Changed |= setDoesNotCapture(F, 0);
930 break;
931 case LibFunc_vscanf:
932 Changed |= setRetAndArgsNoUndef(F);
933 Changed |= setDoesNotThrow(F);
934 Changed |= setDoesNotCapture(F, 0);
935 Changed |= setOnlyReadsMemory(F, 0);
936 break;
937 case LibFunc_vsscanf:
938 Changed |= setRetAndArgsNoUndef(F);
939 Changed |= setDoesNotThrow(F);
940 Changed |= setDoesNotCapture(F, 0);
941 Changed |= setDoesNotCapture(F, 1);
942 Changed |= setOnlyReadsMemory(F, 0);
943 Changed |= setOnlyReadsMemory(F, 1);
944 break;
945 case LibFunc_vfscanf:
946 Changed |= setRetAndArgsNoUndef(F);
947 Changed |= setDoesNotThrow(F);
948 Changed |= setDoesNotCapture(F, 0);
949 Changed |= setDoesNotCapture(F, 1);
950 Changed |= setOnlyReadsMemory(F, 1);
951 break;
952 case LibFunc_vprintf:
953 Changed |= setRetAndArgsNoUndef(F);
954 Changed |= setDoesNotThrow(F);
955 Changed |= setDoesNotCapture(F, 0);
956 Changed |= setOnlyReadsMemory(F, 0);
957 break;
958 case LibFunc_vfprintf:
959 case LibFunc_vsprintf:
960 Changed |= setRetAndArgsNoUndef(F);
961 Changed |= setDoesNotThrow(F);
962 Changed |= setDoesNotCapture(F, 0);
963 Changed |= setDoesNotCapture(F, 1);
964 Changed |= setOnlyReadsMemory(F, 1);
965 break;
966 case LibFunc_vsnprintf:
967 Changed |= setRetAndArgsNoUndef(F);
968 Changed |= setDoesNotThrow(F);
969 Changed |= setDoesNotCapture(F, 0);
970 Changed |= setDoesNotCapture(F, 2);
971 Changed |= setOnlyReadsMemory(F, 2);
972 break;
973 case LibFunc_open:
974 // May throw; "open" is a valid pthread cancellation point.
975 Changed |= setRetAndArgsNoUndef(F);
976 Changed |= setDoesNotCapture(F, 0);
977 Changed |= setOnlyReadsMemory(F, 0);
978 break;
979 case LibFunc_opendir:
980 Changed |= setRetAndArgsNoUndef(F);
981 Changed |= setDoesNotThrow(F);
982 Changed |= setRetDoesNotAlias(F);
983 Changed |= setDoesNotCapture(F, 0);
984 Changed |= setOnlyReadsMemory(F, 0);
985 break;
986 case LibFunc_tmpfile:
987 Changed |= setRetAndArgsNoUndef(F);
988 Changed |= setDoesNotThrow(F);
989 Changed |= setRetDoesNotAlias(F);
990 break;
991 case LibFunc_times:
992 Changed |= setRetAndArgsNoUndef(F);
993 Changed |= setDoesNotThrow(F);
994 Changed |= setDoesNotCapture(F, 0);
995 break;
996 case LibFunc_htonl:
997 case LibFunc_htons:
998 case LibFunc_ntohl:
999 case LibFunc_ntohs:
1000 Changed |= setDoesNotThrow(F);
1001 Changed |= setDoesNotAccessMemory(F);
1002 break;
1003 case LibFunc_lstat:
1004 Changed |= setRetAndArgsNoUndef(F);
1005 Changed |= setDoesNotThrow(F);
1006 Changed |= setDoesNotCapture(F, 0);
1007 Changed |= setDoesNotCapture(F, 1);
1008 Changed |= setOnlyReadsMemory(F, 0);
1009 break;
1010 case LibFunc_lchown:
1011 Changed |= setRetAndArgsNoUndef(F);
1012 Changed |= setDoesNotThrow(F);
1013 Changed |= setDoesNotCapture(F, 0);
1014 Changed |= setOnlyReadsMemory(F, 0);
1015 break;
1016 case LibFunc_qsort:
1017 // May throw; places call through function pointer.
1018 // Cannot give undef pointer/size
1019 Changed |= setRetAndArgsNoUndef(F);
1020 Changed |= setDoesNotCapture(F, 3);
1021 break;
1022 case LibFunc_dunder_strndup:
1023 Changed |= setArgNoUndef(F, 1);
1024 [[fallthrough]];
1025 case LibFunc_dunder_strdup:
1026 Changed |= setDoesNotThrow(F);
1027 Changed |= setRetDoesNotAlias(F);
1028 Changed |= setWillReturn(F);
1029 Changed |= setDoesNotCapture(F, 0);
1030 Changed |= setOnlyReadsMemory(F, 0);
1031 break;
1032 case LibFunc_dunder_strtok_r:
1033 Changed |= setDoesNotThrow(F);
1034 Changed |= setDoesNotCapture(F, 1);
1035 Changed |= setOnlyReadsMemory(F, 1);
1036 break;
1037 case LibFunc_under_IO_getc:
1038 Changed |= setRetAndArgsNoUndef(F);
1039 Changed |= setDoesNotThrow(F);
1040 Changed |= setDoesNotCapture(F, 0);
1041 break;
1042 case LibFunc_under_IO_putc:
1043 Changed |= setRetAndArgsNoUndef(F);
1044 Changed |= setDoesNotThrow(F);
1045 Changed |= setDoesNotCapture(F, 1);
1046 break;
1047 case LibFunc_dunder_isoc99_scanf:
1048 Changed |= setRetAndArgsNoUndef(F);
1049 Changed |= setDoesNotThrow(F);
1050 Changed |= setDoesNotCapture(F, 0);
1051 Changed |= setOnlyReadsMemory(F, 0);
1052 break;
1053 case LibFunc_stat64:
1054 case LibFunc_lstat64:
1055 case LibFunc_statvfs64:
1056 Changed |= setRetAndArgsNoUndef(F);
1057 Changed |= setDoesNotThrow(F);
1058 Changed |= setDoesNotCapture(F, 0);
1059 Changed |= setDoesNotCapture(F, 1);
1060 Changed |= setOnlyReadsMemory(F, 0);
1061 break;
1062 case LibFunc_dunder_isoc99_sscanf:
1063 Changed |= setRetAndArgsNoUndef(F);
1064 Changed |= setDoesNotThrow(F);
1065 Changed |= setDoesNotCapture(F, 0);
1066 Changed |= setDoesNotCapture(F, 1);
1067 Changed |= setOnlyReadsMemory(F, 0);
1068 Changed |= setOnlyReadsMemory(F, 1);
1069 break;
1070 case LibFunc_fopen64:
1071 Changed |= setRetAndArgsNoUndef(F);
1072 Changed |= setDoesNotThrow(F);
1073 Changed |= setRetDoesNotAlias(F);
1074 Changed |= setDoesNotCapture(F, 0);
1075 Changed |= setDoesNotCapture(F, 1);
1076 Changed |= setOnlyReadsMemory(F, 0);
1077 Changed |= setOnlyReadsMemory(F, 1);
1078 break;
1079 case LibFunc_fseeko64:
1080 case LibFunc_ftello64:
1081 Changed |= setRetAndArgsNoUndef(F);
1082 Changed |= setDoesNotThrow(F);
1083 Changed |= setDoesNotCapture(F, 0);
1084 break;
1085 case LibFunc_tmpfile64:
1086 Changed |= setRetAndArgsNoUndef(F);
1087 Changed |= setDoesNotThrow(F);
1088 Changed |= setRetDoesNotAlias(F);
1089 break;
1090 case LibFunc_fstat64:
1091 case LibFunc_fstatvfs64:
1092 Changed |= setRetAndArgsNoUndef(F);
1093 Changed |= setDoesNotThrow(F);
1094 Changed |= setDoesNotCapture(F, 1);
1095 break;
1096 case LibFunc_open64:
1097 // May throw; "open" is a valid pthread cancellation point.
1098 Changed |= setRetAndArgsNoUndef(F);
1099 Changed |= setDoesNotCapture(F, 0);
1100 Changed |= setOnlyReadsMemory(F, 0);
1101 break;
1102 case LibFunc_gettimeofday:
1103 // Currently some platforms have the restrict keyword on the arguments to
1104 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1105 // arguments.
1106 Changed |= setRetAndArgsNoUndef(F);
1107 Changed |= setDoesNotThrow(F);
1108 Changed |= setDoesNotCapture(F, 0);
1109 Changed |= setDoesNotCapture(F, 1);
1110 break;
1111 case LibFunc_memset_pattern4:
1112 case LibFunc_memset_pattern8:
1113 case LibFunc_memset_pattern16:
1114 Changed |= setDoesNotCapture(F, 0);
1115 Changed |= setDoesNotCapture(F, 1);
1116 Changed |= setOnlyReadsMemory(F, 1);
1117 [[fallthrough]];
1118 case LibFunc_memset:
1119 Changed |= setWillReturn(F);
1120 [[fallthrough]];
1121 case LibFunc_memset_chk:
1122 Changed |= setOnlyAccessesArgMemory(F);
1123 Changed |= setOnlyWritesMemory(F, 0);
1124 Changed |= setDoesNotThrow(F);
1125 break;
1126 case LibFunc_abort:
1127 Changed |= setIsCold(F);
1128 break;
1129 case LibFunc_terminate:
1130 Changed |= setIsCold(F);
1131 Changed |= setNoReturn(F);
1132 break;
1133 case LibFunc_cxa_throw:
1134 Changed |= setIsCold(F);
1135 Changed |= setNoReturn(F);
1136 // Don't add `nofree` on `__cxa_throw`
1137 return Changed;
1138 // int __nvvm_reflect(const char *)
1139 case LibFunc_nvvm_reflect:
1140 Changed |= setRetAndArgsNoUndef(F);
1141 Changed |= setDoesNotAccessMemory(F);
1142 Changed |= setDoesNotThrow(F);
1143 break;
1144 case LibFunc_ldexp:
1145 case LibFunc_ldexpf:
1146 case LibFunc_ldexpl:
1147 Changed |= setWillReturn(F);
1148 break;
1149 case LibFunc_remquo:
1150 case LibFunc_remquof:
1151 case LibFunc_remquol:
1152 Changed |= setDoesNotCapture(F, 2);
1153 [[fallthrough]];
1154 case LibFunc_abs:
1155 case LibFunc_acos:
1156 case LibFunc_acosf:
1157 case LibFunc_acosh:
1158 case LibFunc_acoshf:
1159 case LibFunc_acoshl:
1160 case LibFunc_acosl:
1161 case LibFunc_asin:
1162 case LibFunc_asinf:
1163 case LibFunc_asinh:
1164 case LibFunc_asinhf:
1165 case LibFunc_asinhl:
1166 case LibFunc_asinl:
1167 case LibFunc_atan:
1168 case LibFunc_atan2:
1169 case LibFunc_atan2f:
1170 case LibFunc_atan2l:
1171 case LibFunc_atanf:
1172 case LibFunc_atanh:
1173 case LibFunc_atanhf:
1174 case LibFunc_atanhl:
1175 case LibFunc_atanl:
1176 case LibFunc_cbrt:
1177 case LibFunc_cbrtf:
1178 case LibFunc_cbrtl:
1179 case LibFunc_ceil:
1180 case LibFunc_ceilf:
1181 case LibFunc_ceill:
1182 case LibFunc_copysign:
1183 case LibFunc_copysignf:
1184 case LibFunc_copysignl:
1185 case LibFunc_cos:
1186 case LibFunc_cosh:
1187 case LibFunc_coshf:
1188 case LibFunc_coshl:
1189 case LibFunc_cosf:
1190 case LibFunc_cosl:
1191 case LibFunc_cospi:
1192 case LibFunc_cospif:
1193 case LibFunc_erf:
1194 case LibFunc_erff:
1195 case LibFunc_erfl:
1196 case LibFunc_tgamma:
1197 case LibFunc_tgammaf:
1198 case LibFunc_tgammal:
1199 case LibFunc_exp:
1200 case LibFunc_expf:
1201 case LibFunc_expl:
1202 case LibFunc_exp2:
1203 case LibFunc_exp2f:
1204 case LibFunc_exp2l:
1205 case LibFunc_expm1:
1206 case LibFunc_expm1f:
1207 case LibFunc_expm1l:
1208 case LibFunc_fabs:
1209 case LibFunc_fabsf:
1210 case LibFunc_fabsl:
1211 case LibFunc_fdim:
1212 case LibFunc_fdiml:
1213 case LibFunc_fdimf:
1214 case LibFunc_ffs:
1215 case LibFunc_ffsl:
1216 case LibFunc_ffsll:
1217 case LibFunc_floor:
1218 case LibFunc_floorf:
1219 case LibFunc_floorl:
1220 case LibFunc_fls:
1221 case LibFunc_flsl:
1222 case LibFunc_flsll:
1223 case LibFunc_fmax:
1224 case LibFunc_fmaxf:
1225 case LibFunc_fmaxl:
1226 case LibFunc_fmin:
1227 case LibFunc_fminf:
1228 case LibFunc_fminl:
1229 case LibFunc_fmod:
1230 case LibFunc_fmodf:
1231 case LibFunc_fmodl:
1232 case LibFunc_hypot:
1233 case LibFunc_hypotf:
1234 case LibFunc_hypotl:
1235 case LibFunc_isascii:
1236 case LibFunc_isdigit:
1237 case LibFunc_labs:
1238 case LibFunc_llabs:
1239 case LibFunc_log:
1240 case LibFunc_log10:
1241 case LibFunc_log10f:
1242 case LibFunc_log10l:
1243 case LibFunc_log1p:
1244 case LibFunc_log1pf:
1245 case LibFunc_log1pl:
1246 case LibFunc_log2:
1247 case LibFunc_log2f:
1248 case LibFunc_log2l:
1249 case LibFunc_logb:
1250 case LibFunc_logbf:
1251 case LibFunc_logbl:
1252 case LibFunc_ilogb:
1253 case LibFunc_ilogbf:
1254 case LibFunc_ilogbl:
1255 case LibFunc_logf:
1256 case LibFunc_logl:
1257 case LibFunc_nearbyint:
1258 case LibFunc_nearbyintf:
1259 case LibFunc_nearbyintl:
1260 case LibFunc_pow:
1261 case LibFunc_powf:
1262 case LibFunc_powl:
1263 case LibFunc_remainder:
1264 case LibFunc_remainderf:
1265 case LibFunc_remainderl:
1266 case LibFunc_rint:
1267 case LibFunc_rintf:
1268 case LibFunc_rintl:
1269 case LibFunc_round:
1270 case LibFunc_roundf:
1271 case LibFunc_roundl:
1272 case LibFunc_scalbln:
1273 case LibFunc_scalblnf:
1274 case LibFunc_scalblnl:
1275 case LibFunc_scalbn:
1276 case LibFunc_scalbnf:
1277 case LibFunc_scalbnl:
1278 case LibFunc_sin:
1279 case LibFunc_sincospif_stret:
1280 case LibFunc_sinf:
1281 case LibFunc_sinh:
1282 case LibFunc_sinhf:
1283 case LibFunc_sinhl:
1284 case LibFunc_sinl:
1285 case LibFunc_sinpi:
1286 case LibFunc_sinpif:
1287 case LibFunc_sqrt:
1288 case LibFunc_sqrtf:
1289 case LibFunc_sqrtl:
1290 case LibFunc_tan:
1291 case LibFunc_tanf:
1292 case LibFunc_tanh:
1293 case LibFunc_tanhf:
1294 case LibFunc_tanhl:
1295 case LibFunc_tanl:
1296 case LibFunc_toascii:
1297 case LibFunc_trunc:
1298 case LibFunc_truncf:
1299 case LibFunc_truncl:
1300 Changed |= setDoesNotThrow(F);
1301 Changed |= setDoesNotFreeMemory(F);
1302 Changed |= setOnlyWritesMemory(F);
1303 Changed |= setWillReturn(F);
1304 break;
1305 case LibFunc_sincos:
1306 case LibFunc_sincosf:
1307 case LibFunc_sincosl:
1308 Changed |= setDoesNotThrow(F);
1309 Changed |= setDoesNotFreeMemory(F);
1310 Changed |= setOnlyWritesMemory(F);
1311 Changed |= setOnlyWritesMemory(F, 1);
1312 Changed |= setOnlyWritesMemory(F, 2);
1313 Changed |= setDoesNotCapture(F, 1);
1314 Changed |= setDoesNotCapture(F, 2);
1315 Changed |= setWillReturn(F);
1316 break;
1317 default:
1318 // FIXME: It'd be really nice to cover all the library functions we're
1319 // aware of here.
1320 break;
1321 }
1322 // We have to do this step after AllocKind has been inferred on functions so
1323 // we can reliably identify free-like and realloc-like functions.
1324 if (!isLibFreeFunction(&F, TheLibFunc) && !isReallocLikeFn(&F))
1325 Changed |= setDoesNotFreeMemory(F);
1326 return Changed;
1327}
1328
1329static void setArgExtAttr(Function &F, unsigned ArgNo,
1330 const TargetLibraryInfo &TLI, bool Signed = true) {
1331 Attribute::AttrKind ExtAttr = TLI.getExtAttrForI32Param(Signed);
1332 if (ExtAttr != Attribute::None && !F.hasParamAttribute(ArgNo, ExtAttr))
1333 F.addParamAttr(ArgNo, ExtAttr);
1334}
1335
1337 const TargetLibraryInfo &TLI, bool Signed = true) {
1338 Attribute::AttrKind ExtAttr = TLI.getExtAttrForI32Return(Signed);
1339 if (ExtAttr != Attribute::None && !F.hasRetAttribute(ExtAttr))
1340 F.addRetAttr(ExtAttr);
1341}
1342
1343// Modeled after X86TargetLowering::markLibCallAttributes.
1345 if (!F->arg_size() || F->isVarArg())
1346 return;
1347
1348 const CallingConv::ID CC = F->getCallingConv();
1350 return;
1351
1352 const Module *M = F->getParent();
1353 unsigned N = M->getNumberRegisterParameters();
1354 if (!N)
1355 return;
1356
1357 const DataLayout &DL = M->getDataLayout();
1358
1359 for (Argument &A : F->args()) {
1360 Type *T = A.getType();
1361 if (!T->isIntOrPtrTy())
1362 continue;
1363
1364 const TypeSize &TS = DL.getTypeAllocSize(T);
1365 if (TS > 8)
1366 continue;
1367
1368 assert(TS <= 4 && "Need to account for parameters larger than word size");
1369 const unsigned NumRegs = TS > 4 ? 2 : 1;
1370 if (N < NumRegs)
1371 return;
1372
1373 N -= NumRegs;
1374 F->addParamAttr(A.getArgNo(), Attribute::InReg);
1375 }
1376}
1377
1379 LibFunc TheLibFunc, FunctionType *T,
1381 assert(TLI.has(TheLibFunc) &&
1382 "Creating call to non-existing library function.");
1383 StringRef Name = TLI.getName(TheLibFunc);
1384 FunctionCallee C = M->getOrInsertFunction(Name, T, AttributeList);
1385
1386 // Make sure any mandatory argument attributes are added.
1387
1388 // Any outgoing i32 argument should be handled with setArgExtAttr() which
1389 // will add an extension attribute if the target ABI requires it. Adding
1390 // argument extensions is typically done by the front end but when an
1391 // optimizer is building a library call on its own it has to take care of
1392 // this. Each such generated function must be handled here with sign or
1393 // zero extensions as needed. F is retreived with cast<> because we demand
1394 // of the caller to have called isLibFuncEmittable() first.
1395 Function *F = cast<Function>(C.getCallee());
1396 assert(F->getFunctionType() == T && "Function type does not match.");
1397 switch (TheLibFunc) {
1398 case LibFunc_fputc:
1399 case LibFunc_putchar:
1400 setArgExtAttr(*F, 0, TLI);
1401 break;
1402 case LibFunc_ldexp:
1403 case LibFunc_ldexpf:
1404 case LibFunc_ldexpl:
1405 case LibFunc_memchr:
1406 case LibFunc_memrchr:
1407 case LibFunc_strchr:
1408 setArgExtAttr(*F, 1, TLI);
1409 break;
1410 case LibFunc_memccpy:
1411 setArgExtAttr(*F, 2, TLI);
1412 break;
1413
1414 // These are functions that are known to not need any argument extension
1415 // on any target: A size_t argument (which may be an i32 on some targets)
1416 // should not trigger the assert below.
1417 case LibFunc_bcmp:
1418 setRetExtAttr(*F, TLI);
1419 break;
1420 case LibFunc_calloc:
1421 case LibFunc_fwrite:
1422 case LibFunc_malloc:
1423 case LibFunc_memcmp:
1424 case LibFunc_memcpy_chk:
1425 case LibFunc_mempcpy:
1426 case LibFunc_memset_pattern16:
1427 case LibFunc_snprintf:
1428 case LibFunc_stpncpy:
1429 case LibFunc_strlcat:
1430 case LibFunc_strlcpy:
1431 case LibFunc_strncat:
1432 case LibFunc_strncmp:
1433 case LibFunc_strncpy:
1434 case LibFunc_vsnprintf:
1435 break;
1436
1437 default:
1438#ifndef NDEBUG
1439 for (unsigned i = 0; i < T->getNumParams(); i++)
1440 assert(!isa<IntegerType>(T->getParamType(i)) &&
1441 "Unhandled integer argument.");
1442#endif
1443 break;
1444 }
1445
1447
1448 return C;
1449}
1450
1452 LibFunc TheLibFunc, FunctionType *T) {
1453 return getOrInsertLibFunc(M, TLI, TheLibFunc, T, AttributeList());
1454}
1455
1457 LibFunc TheLibFunc) {
1458 StringRef FuncName = TLI->getName(TheLibFunc);
1459 if (!TLI->has(TheLibFunc))
1460 return false;
1461
1462 // Check if the Module already has a GlobalValue with the same name, in
1463 // which case it must be a Function with the expected type.
1464 if (GlobalValue *GV = M->getNamedValue(FuncName)) {
1465 if (auto *F = dyn_cast<Function>(GV))
1466 return TLI->isValidProtoForLibFunc(*F->getFunctionType(), TheLibFunc, *M);
1467 return false;
1468 }
1469
1470 return true;
1471}
1472
1474 StringRef Name) {
1475 LibFunc TheLibFunc;
1476 return TLI->getLibFunc(Name, TheLibFunc) &&
1477 isLibFuncEmittable(M, TLI, TheLibFunc);
1478}
1479
1480bool llvm::hasFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty,
1481 LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn) {
1482 switch (Ty->getTypeID()) {
1483 case Type::HalfTyID:
1484 return false;
1485 case Type::FloatTyID:
1486 return isLibFuncEmittable(M, TLI, FloatFn);
1487 case Type::DoubleTyID:
1488 return isLibFuncEmittable(M, TLI, DoubleFn);
1489 default:
1490 return isLibFuncEmittable(M, TLI, LongDoubleFn);
1491 }
1492}
1493
1495 Type *Ty, LibFunc DoubleFn, LibFunc FloatFn,
1496 LibFunc LongDoubleFn, LibFunc &TheLibFunc) {
1497 assert(hasFloatFn(M, TLI, Ty, DoubleFn, FloatFn, LongDoubleFn) &&
1498 "Cannot get name for unavailable function!");
1499
1500 switch (Ty->getTypeID()) {
1501 case Type::HalfTyID:
1502 llvm_unreachable("No name for HalfTy!");
1503 case Type::FloatTyID:
1504 TheLibFunc = FloatFn;
1505 return TLI->getName(FloatFn);
1506 case Type::DoubleTyID:
1507 TheLibFunc = DoubleFn;
1508 return TLI->getName(DoubleFn);
1509 default:
1510 TheLibFunc = LongDoubleFn;
1511 return TLI->getName(LongDoubleFn);
1512 }
1513}
1514
1515//- Emit LibCalls ------------------------------------------------------------//
1516
1518 return B.getIntNTy(TLI->getIntSize());
1519}
1520
1522 const Module *M = B.GetInsertBlock()->getModule();
1523 return B.getIntNTy(TLI->getSizeTSize(*M));
1524}
1525
1526static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType,
1527 ArrayRef<Type *> ParamTypes,
1529 const TargetLibraryInfo *TLI,
1530 bool IsVaArgs = false) {
1531 Module *M = B.GetInsertBlock()->getModule();
1532 if (!isLibFuncEmittable(M, TLI, TheLibFunc))
1533 return nullptr;
1534
1535 StringRef FuncName = TLI->getName(TheLibFunc);
1536 FunctionType *FuncType = FunctionType::get(ReturnType, ParamTypes, IsVaArgs);
1537 FunctionCallee Callee = getOrInsertLibFunc(M, *TLI, TheLibFunc, FuncType);
1538 inferNonMandatoryLibFuncAttrs(M, FuncName, *TLI);
1539 CallInst *CI = B.CreateCall(Callee, Operands, FuncName);
1540 if (const Function *F =
1541 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
1542 CI->setCallingConv(F->getCallingConv());
1543 return CI;
1544}
1545
1547 const TargetLibraryInfo *TLI) {
1548 Type *CharPtrTy = Ptr->getType();
1549 Type *SizeTTy = getSizeTTy(B, TLI);
1550 return emitLibCall(LibFunc_strlen, SizeTTy, CharPtrTy, Ptr, B, TLI);
1551}
1552
1554 const TargetLibraryInfo *TLI) {
1555 Type *CharPtrTy = Ptr->getType();
1556 return emitLibCall(LibFunc_strdup, CharPtrTy, CharPtrTy, Ptr, B, TLI);
1557}
1558
1560 const TargetLibraryInfo *TLI) {
1561 Type *CharPtrTy = Ptr->getType();
1562 Type *IntTy = getIntTy(B, TLI);
1563 return emitLibCall(LibFunc_strchr, CharPtrTy, {CharPtrTy, IntTy},
1564 {Ptr, ConstantInt::get(IntTy, C)}, B, TLI);
1565}
1566
1568 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1569 Type *CharPtrTy = Ptr1->getType();
1570 assert(CharPtrTy == Ptr2->getType());
1571 Type *IntTy = getIntTy(B, TLI);
1572 Type *SizeTTy = getSizeTTy(B, TLI);
1573 return emitLibCall(
1574 LibFunc_strncmp, IntTy,
1575 {CharPtrTy, CharPtrTy, SizeTTy},
1576 {Ptr1, Ptr2, Len}, B, TLI);
1577}
1578
1580 const TargetLibraryInfo *TLI) {
1581 Type *CharPtrTy = Dst->getType();
1582 assert(CharPtrTy == Src->getType());
1583 return emitLibCall(LibFunc_strcpy, CharPtrTy, {CharPtrTy, CharPtrTy},
1584 {Dst, Src}, B, TLI);
1585}
1586
1588 const TargetLibraryInfo *TLI) {
1589 Type *CharPtrTy = Dst->getType();
1590 assert(CharPtrTy == Src->getType());
1591 return emitLibCall(LibFunc_stpcpy, CharPtrTy, {CharPtrTy, CharPtrTy},
1592 {Dst, Src}, B, TLI);
1593}
1594
1596 const TargetLibraryInfo *TLI) {
1597 Type *CharPtrTy = Dst->getType();
1598 assert(CharPtrTy == Src->getType());
1599 Type *SizeTTy = getSizeTTy(B, TLI);
1600 return emitLibCall(LibFunc_strncpy, CharPtrTy, {CharPtrTy, CharPtrTy, SizeTTy},
1601 {Dst, Src, Len}, B, TLI);
1602}
1603
1605 const TargetLibraryInfo *TLI) {
1606 Type *CharPtrTy = Dst->getType();
1607 assert(CharPtrTy == Src->getType());
1608 Type *SizeTTy = getSizeTTy(B, TLI);
1609 return emitLibCall(LibFunc_stpncpy, CharPtrTy, {CharPtrTy, CharPtrTy, SizeTTy},
1610 {Dst, Src, Len}, B, TLI);
1611}
1612
1613Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
1614 IRBuilderBase &B, const DataLayout &DL,
1615 const TargetLibraryInfo *TLI) {
1616 Module *M = B.GetInsertBlock()->getModule();
1617 if (!isLibFuncEmittable(M, TLI, LibFunc_memcpy_chk))
1618 return nullptr;
1619
1620 AttributeList AS;
1622 Attribute::NoUnwind);
1623 Type *VoidPtrTy = Dst->getType();
1624 assert(VoidPtrTy == Src->getType());
1625 Type *SizeTTy = getSizeTTy(B, TLI);
1626 FunctionCallee MemCpy = getOrInsertLibFunc(M, *TLI, LibFunc_memcpy_chk,
1627 AttributeList::get(M->getContext(), AS), VoidPtrTy,
1628 VoidPtrTy, VoidPtrTy, SizeTTy, SizeTTy);
1629 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
1630 if (const Function *F =
1631 dyn_cast<Function>(MemCpy.getCallee()->stripPointerCasts()))
1632 CI->setCallingConv(F->getCallingConv());
1633 return CI;
1634}
1635
1637 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1638 Type *VoidPtrTy = Dst->getType();
1639 assert(VoidPtrTy == Src->getType());
1640 Type *SizeTTy = getSizeTTy(B, TLI);
1641 return emitLibCall(LibFunc_mempcpy, VoidPtrTy,
1642 {VoidPtrTy, VoidPtrTy, SizeTTy},
1643 {Dst, Src, Len}, B, TLI);
1644}
1645
1647 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1648 Type *VoidPtrTy = Ptr->getType();
1649 Type *IntTy = getIntTy(B, TLI);
1650 Type *SizeTTy = getSizeTTy(B, TLI);
1651 return emitLibCall(LibFunc_memchr, VoidPtrTy,
1652 {VoidPtrTy, IntTy, SizeTTy},
1653 {Ptr, Val, Len}, B, TLI);
1654}
1655
1657 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1658 Type *VoidPtrTy = Ptr->getType();
1659 Type *IntTy = getIntTy(B, TLI);
1660 Type *SizeTTy = getSizeTTy(B, TLI);
1661 return emitLibCall(LibFunc_memrchr, VoidPtrTy,
1662 {VoidPtrTy, IntTy, SizeTTy},
1663 {Ptr, Val, Len}, B, TLI);
1664}
1665
1667 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1668 Type *VoidPtrTy = Ptr1->getType();
1669 assert(VoidPtrTy == Ptr2->getType());
1670 Type *IntTy = getIntTy(B, TLI);
1671 Type *SizeTTy = getSizeTTy(B, TLI);
1672 return emitLibCall(LibFunc_memcmp, IntTy,
1673 {VoidPtrTy, VoidPtrTy, SizeTTy},
1674 {Ptr1, Ptr2, Len}, B, TLI);
1675}
1676
1678 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1679 Type *VoidPtrTy = Ptr1->getType();
1680 assert(VoidPtrTy == Ptr2->getType());
1681 Type *IntTy = getIntTy(B, TLI);
1682 Type *SizeTTy = getSizeTTy(B, TLI);
1683 return emitLibCall(LibFunc_bcmp, IntTy,
1684 {VoidPtrTy, VoidPtrTy, SizeTTy},
1685 {Ptr1, Ptr2, Len}, B, TLI);
1686}
1687
1688Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
1689 IRBuilderBase &B, const TargetLibraryInfo *TLI) {
1690 Type *VoidPtrTy = Ptr1->getType();
1691 assert(VoidPtrTy == Ptr2->getType());
1692 Type *IntTy = getIntTy(B, TLI);
1693 Type *SizeTTy = getSizeTTy(B, TLI);
1694 return emitLibCall(LibFunc_memccpy, VoidPtrTy,
1695 {VoidPtrTy, VoidPtrTy, IntTy, SizeTTy},
1696 {Ptr1, Ptr2, Val, Len}, B, TLI);
1697}
1698
1700 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B,
1701 const TargetLibraryInfo *TLI) {
1702 Type *CharPtrTy = Dest->getType();
1703 assert(CharPtrTy == Fmt->getType());
1704 Type *IntTy = getIntTy(B, TLI);
1705 Type *SizeTTy = getSizeTTy(B, TLI);
1706 SmallVector<Value *, 8> Args{Dest, Size, Fmt};
1707 llvm::append_range(Args, VariadicArgs);
1708 return emitLibCall(LibFunc_snprintf, IntTy,
1709 {CharPtrTy, SizeTTy, CharPtrTy},
1710 Args, B, TLI, /*IsVaArgs=*/true);
1711}
1712
1714 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B,
1715 const TargetLibraryInfo *TLI) {
1716 Type *CharPtrTy = Dest->getType();
1717 Type *IntTy = getIntTy(B, TLI);
1718 SmallVector<Value *, 8> Args{Dest, Fmt};
1719 llvm::append_range(Args, VariadicArgs);
1720 return emitLibCall(LibFunc_sprintf, IntTy,
1721 {CharPtrTy, CharPtrTy}, Args, B, TLI,
1722 /*IsVaArgs=*/true);
1723}
1724
1726 const TargetLibraryInfo *TLI) {
1727 Type *CharPtrTy = Dest->getType();
1728 assert(CharPtrTy == Src->getType());
1729 return emitLibCall(LibFunc_strcat, CharPtrTy,
1730 {CharPtrTy, CharPtrTy},
1731 {Dest, Src}, B, TLI);
1732}
1733
1735 const TargetLibraryInfo *TLI) {
1736 Type *CharPtrTy = Dest->getType();
1737 assert(CharPtrTy == Src->getType());
1738 Type *SizeTTy = getSizeTTy(B, TLI);
1739 return emitLibCall(LibFunc_strlcpy, SizeTTy,
1740 {CharPtrTy, CharPtrTy, SizeTTy},
1741 {Dest, Src, Size}, B, TLI);
1742}
1743
1745 const TargetLibraryInfo *TLI) {
1746 Type *CharPtrTy = Dest->getType();
1747 assert(CharPtrTy == Src->getType());
1748 Type *SizeTTy = getSizeTTy(B, TLI);
1749 return emitLibCall(LibFunc_strlcat, SizeTTy,
1750 {CharPtrTy, CharPtrTy, SizeTTy},
1751 {Dest, Src, Size}, B, TLI);
1752}
1753
1755 const TargetLibraryInfo *TLI) {
1756 Type *CharPtrTy = Dest->getType();
1757 assert(CharPtrTy == Src->getType());
1758 Type *SizeTTy = getSizeTTy(B, TLI);
1759 return emitLibCall(LibFunc_strncat, CharPtrTy,
1760 {CharPtrTy, CharPtrTy, SizeTTy},
1761 {Dest, Src, Size}, B, TLI);
1762}
1763
1765 IRBuilderBase &B, const TargetLibraryInfo *TLI) {
1766 Type *CharPtrTy = Dest->getType();
1767 assert(CharPtrTy == Fmt->getType());
1768 Type *IntTy = getIntTy(B, TLI);
1769 Type *SizeTTy = getSizeTTy(B, TLI);
1770 return emitLibCall(
1771 LibFunc_vsnprintf, IntTy,
1772 {CharPtrTy, SizeTTy, CharPtrTy, VAList->getType()},
1773 {Dest, Size, Fmt, VAList}, B, TLI);
1774}
1775
1777 IRBuilderBase &B, const TargetLibraryInfo *TLI) {
1778 Type *CharPtrTy = Dest->getType();
1779 assert(CharPtrTy == Fmt->getType());
1780 Type *IntTy = getIntTy(B, TLI);
1781 return emitLibCall(LibFunc_vsprintf, IntTy,
1782 {CharPtrTy, CharPtrTy, VAList->getType()},
1783 {Dest, Fmt, VAList}, B, TLI);
1784}
1785
1786/// Append a suffix to the function name according to the type of 'Op'.
1788 SmallString<20> &NameBuffer) {
1789 if (!Op->getType()->isDoubleTy()) {
1790 NameBuffer += Name;
1791
1792 if (Op->getType()->isFloatTy())
1793 NameBuffer += 'f';
1794 else
1795 NameBuffer += 'l';
1796
1797 Name = NameBuffer;
1798 }
1799}
1800
1803 const AttributeList &Attrs,
1804 const TargetLibraryInfo *TLI) {
1805 assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall");
1806
1807 Module *M = B.GetInsertBlock()->getModule();
1808 FunctionCallee Callee = getOrInsertLibFunc(M, *TLI, TheLibFunc, Op->getType(),
1809 Op->getType());
1810 CallInst *CI = B.CreateCall(Callee, Op, Name);
1811
1812 // The incoming attribute set may have come from a speculatable intrinsic, but
1813 // is being replaced with a library call which is not allowed to be
1814 // speculatable.
1815 CI->setAttributes(
1816 Attrs.removeFnAttribute(B.getContext(), Attribute::Speculatable));
1817 if (const Function *F =
1818 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
1819 CI->setCallingConv(F->getCallingConv());
1820
1821 return CI;
1822}
1823
1826 const AttributeList &Attrs) {
1827 SmallString<20> NameBuffer;
1828 appendTypeSuffix(Op, Name, NameBuffer);
1829
1830 LibFunc TheLibFunc;
1831 TLI->getLibFunc(Name, TheLibFunc);
1832
1833 return emitUnaryFloatFnCallHelper(Op, TheLibFunc, Name, B, Attrs, TLI);
1834}
1835
1837 LibFunc DoubleFn, LibFunc FloatFn,
1838 LibFunc LongDoubleFn, IRBuilderBase &B,
1839 const AttributeList &Attrs) {
1840 // Get the name of the function according to TLI.
1841 Module *M = B.GetInsertBlock()->getModule();
1842 LibFunc TheLibFunc;
1843 StringRef Name = getFloatFn(M, TLI, Op->getType(), DoubleFn, FloatFn,
1844 LongDoubleFn, TheLibFunc);
1845
1846 return emitUnaryFloatFnCallHelper(Op, TheLibFunc, Name, B, Attrs, TLI);
1847}
1848
1850 LibFunc TheLibFunc,
1852 const AttributeList &Attrs,
1853 const TargetLibraryInfo *TLI) {
1854 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
1855
1856 Module *M = B.GetInsertBlock()->getModule();
1857 FunctionCallee Callee = getOrInsertLibFunc(M, *TLI, TheLibFunc, Op1->getType(),
1858 Op1->getType(), Op2->getType());
1860 CallInst *CI = B.CreateCall(Callee, { Op1, Op2 }, Name);
1861
1862 // The incoming attribute set may have come from a speculatable intrinsic, but
1863 // is being replaced with a library call which is not allowed to be
1864 // speculatable.
1865 CI->setAttributes(
1866 Attrs.removeFnAttribute(B.getContext(), Attribute::Speculatable));
1867 if (const Function *F =
1868 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
1869 CI->setCallingConv(F->getCallingConv());
1870
1871 return CI;
1872}
1873
1875 const TargetLibraryInfo *TLI,
1877 const AttributeList &Attrs) {
1878 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
1879
1880 SmallString<20> NameBuffer;
1881 appendTypeSuffix(Op1, Name, NameBuffer);
1882
1883 LibFunc TheLibFunc;
1884 TLI->getLibFunc(Name, TheLibFunc);
1885
1886 return emitBinaryFloatFnCallHelper(Op1, Op2, TheLibFunc, Name, B, Attrs, TLI);
1887}
1888
1890 const TargetLibraryInfo *TLI,
1891 LibFunc DoubleFn, LibFunc FloatFn,
1892 LibFunc LongDoubleFn, IRBuilderBase &B,
1893 const AttributeList &Attrs) {
1894 // Get the name of the function according to TLI.
1895 Module *M = B.GetInsertBlock()->getModule();
1896 LibFunc TheLibFunc;
1897 StringRef Name = getFloatFn(M, TLI, Op1->getType(), DoubleFn, FloatFn,
1898 LongDoubleFn, TheLibFunc);
1899
1900 return emitBinaryFloatFnCallHelper(Op1, Op2, TheLibFunc, Name, B, Attrs, TLI);
1901}
1902
1903// Emit a call to putchar(int) with Char as the argument. Char must have
1904// the same precision as int, which need not be 32 bits.
1906 const TargetLibraryInfo *TLI) {
1907 Module *M = B.GetInsertBlock()->getModule();
1908 if (!isLibFuncEmittable(M, TLI, LibFunc_putchar))
1909 return nullptr;
1910
1911 Type *IntTy = getIntTy(B, TLI);
1912 StringRef PutCharName = TLI->getName(LibFunc_putchar);
1913 FunctionCallee PutChar = getOrInsertLibFunc(M, *TLI, LibFunc_putchar,
1914 IntTy, IntTy);
1915 inferNonMandatoryLibFuncAttrs(M, PutCharName, *TLI);
1916 CallInst *CI = B.CreateCall(PutChar, Char, PutCharName);
1917
1918 if (const Function *F =
1919 dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts()))
1920 CI->setCallingConv(F->getCallingConv());
1921 return CI;
1922}
1923
1925 const TargetLibraryInfo *TLI) {
1926 Module *M = B.GetInsertBlock()->getModule();
1927 if (!isLibFuncEmittable(M, TLI, LibFunc_puts))
1928 return nullptr;
1929
1930 Type *IntTy = getIntTy(B, TLI);
1931 StringRef PutsName = TLI->getName(LibFunc_puts);
1932 FunctionCallee PutS =
1933 getOrInsertLibFunc(M, *TLI, LibFunc_puts, IntTy, Str->getType());
1934 inferNonMandatoryLibFuncAttrs(M, PutsName, *TLI);
1935 CallInst *CI = B.CreateCall(PutS, Str, PutsName);
1936 if (const Function *F =
1937 dyn_cast<Function>(PutS.getCallee()->stripPointerCasts()))
1938 CI->setCallingConv(F->getCallingConv());
1939 return CI;
1940}
1941
1943 const TargetLibraryInfo *TLI) {
1944 Module *M = B.GetInsertBlock()->getModule();
1945 if (!isLibFuncEmittable(M, TLI, LibFunc_fputc))
1946 return nullptr;
1947
1948 Type *IntTy = getIntTy(B, TLI);
1949 StringRef FPutcName = TLI->getName(LibFunc_fputc);
1950 FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputc, IntTy,
1951 IntTy, File->getType());
1952 if (File->getType()->isPointerTy())
1953 inferNonMandatoryLibFuncAttrs(M, FPutcName, *TLI);
1954 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
1955
1956 if (const Function *Fn =
1957 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1958 CI->setCallingConv(Fn->getCallingConv());
1959 return CI;
1960}
1961
1963 const TargetLibraryInfo *TLI) {
1964 Module *M = B.GetInsertBlock()->getModule();
1965 if (!isLibFuncEmittable(M, TLI, LibFunc_fputs))
1966 return nullptr;
1967
1968 Type *IntTy = getIntTy(B, TLI);
1969 StringRef FPutsName = TLI->getName(LibFunc_fputs);
1970 FunctionCallee F = getOrInsertLibFunc(M, *TLI, LibFunc_fputs, IntTy,
1971 Str->getType(), File->getType());
1972 if (File->getType()->isPointerTy())
1973 inferNonMandatoryLibFuncAttrs(M, FPutsName, *TLI);
1974 CallInst *CI = B.CreateCall(F, {Str, File}, FPutsName);
1975
1976 if (const Function *Fn =
1977 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1978 CI->setCallingConv(Fn->getCallingConv());
1979 return CI;
1980}
1981
1983 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1984 Module *M = B.GetInsertBlock()->getModule();
1985 if (!isLibFuncEmittable(M, TLI, LibFunc_fwrite))
1986 return nullptr;
1987
1988 Type *SizeTTy = getSizeTTy(B, TLI);
1989 StringRef FWriteName = TLI->getName(LibFunc_fwrite);
1991 getOrInsertLibFunc(M, *TLI, LibFunc_fwrite, SizeTTy, Ptr->getType(),
1992 SizeTTy, SizeTTy, File->getType());
1993
1994 if (File->getType()->isPointerTy())
1995 inferNonMandatoryLibFuncAttrs(M, FWriteName, *TLI);
1996 CallInst *CI =
1997 B.CreateCall(F, {Ptr, Size,
1998 ConstantInt::get(SizeTTy, 1), File});
1999
2000 if (const Function *Fn =
2001 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
2002 CI->setCallingConv(Fn->getCallingConv());
2003 return CI;
2004}
2005
2007 const DataLayout &DL, const TargetLibraryInfo *TLI) {
2008 Module *M = B.GetInsertBlock()->getModule();
2009 if (!isLibFuncEmittable(M, TLI, LibFunc_malloc))
2010 return nullptr;
2011
2012 StringRef MallocName = TLI->getName(LibFunc_malloc);
2013 Type *SizeTTy = getSizeTTy(B, TLI);
2015 getOrInsertLibFunc(M, *TLI, LibFunc_malloc, RetTy, SizeTTy);
2016 inferNonMandatoryLibFuncAttrs(M, MallocName, *TLI);
2017 CallInst *CI = B.CreateCall(Malloc, Num, MallocName);
2018
2019 if (const Function *F =
2020 dyn_cast<Function>(Malloc.getCallee()->stripPointerCasts()))
2021 CI->setCallingConv(F->getCallingConv());
2022
2023 return CI;
2024}
2025
2027 const TargetLibraryInfo &TLI) {
2028 Module *M = B.GetInsertBlock()->getModule();
2029 if (!isLibFuncEmittable(M, &TLI, LibFunc_calloc))
2030 return nullptr;
2031
2032 StringRef CallocName = TLI.getName(LibFunc_calloc);
2033 Type *SizeTTy = getSizeTTy(B, &TLI);
2034 FunctionCallee Calloc =
2035 getOrInsertLibFunc(M, TLI, LibFunc_calloc, RetTy, SizeTTy, SizeTTy);
2036 inferNonMandatoryLibFuncAttrs(M, CallocName, TLI);
2037 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
2038
2039 if (const auto *F =
2040 dyn_cast<Function>(Calloc.getCallee()->stripPointerCasts()))
2041 CI->setCallingConv(F->getCallingConv());
2042
2043 return CI;
2044}
2045
2048 const TargetLibraryInfo *TLI,
2049 LibFunc SizeFeedbackNewFunc,
2050 uint8_t HotCold) {
2051 Module *M = B.GetInsertBlock()->getModule();
2052 if (!isLibFuncEmittable(M, TLI, SizeFeedbackNewFunc))
2053 return nullptr;
2054
2055 StringRef Name = TLI->getName(SizeFeedbackNewFunc);
2056
2057 // __sized_ptr_t struct return type { void*, size_t }
2058 StructType *SizedPtrT =
2059 StructType::get(M->getContext(), {RetPtrTy, Num->getType()});
2060 FunctionCallee Func =
2061 M->getOrInsertFunction(Name, SizedPtrT, Num->getType(), B.getInt8Ty());
2063 CallInst *CI = B.CreateCall(Func, {Num, B.getInt8(HotCold)}, "sized_ptr");
2064
2065 if (const Function *F = dyn_cast<Function>(Func.getCallee()))
2066 CI->setCallingConv(F->getCallingConv());
2067
2068 return CI;
2069}
2070
2073 const TargetLibraryInfo *TLI,
2074 LibFunc SizeFeedbackNewFunc,
2075 uint8_t HotCold) {
2076 Module *M = B.GetInsertBlock()->getModule();
2077 if (!isLibFuncEmittable(M, TLI, SizeFeedbackNewFunc))
2078 return nullptr;
2079
2080 StringRef Name = TLI->getName(SizeFeedbackNewFunc);
2081
2082 // __sized_ptr_t struct return type { void*, size_t }
2083 StructType *SizedPtrT =
2084 StructType::get(M->getContext(), {RetPtrTy, Num->getType()});
2085 FunctionCallee Func = M->getOrInsertFunction(Name, SizedPtrT, Num->getType(),
2086 Align->getType(), B.getInt8Ty());
2088 CallInst *CI =
2089 B.CreateCall(Func, {Num, Align, B.getInt8(HotCold)}, "sized_ptr");
2090
2091 if (const Function *F = dyn_cast<Function>(Func.getCallee()))
2092 CI->setCallingConv(F->getCallingConv());
2093
2094 return CI;
2095}
2096
2098 const TargetLibraryInfo *TLI, LibFunc NewFunc,
2099 uint8_t HotCold) {
2100 Module *M = B.GetInsertBlock()->getModule();
2101 if (!isLibFuncEmittable(M, TLI, NewFunc))
2102 return nullptr;
2103
2104 StringRef Name = TLI->getName(NewFunc);
2105 FunctionCallee Func =
2106 M->getOrInsertFunction(Name, RetTy, Num->getType(), B.getInt8Ty());
2108 CallInst *CI = B.CreateCall(Func, {Num, B.getInt8(HotCold)}, Name);
2109
2110 if (const Function *F =
2111 dyn_cast<Function>(Func.getCallee()->stripPointerCasts()))
2112 CI->setCallingConv(F->getCallingConv());
2113
2114 return CI;
2115}
2116
2119 const TargetLibraryInfo *TLI,
2120 LibFunc NewFunc, uint8_t HotCold) {
2121 Module *M = B.GetInsertBlock()->getModule();
2122 if (!isLibFuncEmittable(M, TLI, NewFunc))
2123 return nullptr;
2124
2125 StringRef Name = TLI->getName(NewFunc);
2126 FunctionCallee Func = M->getOrInsertFunction(
2127 Name, RetTy, Num->getType(), NoThrow->getType(), B.getInt8Ty());
2129 CallInst *CI = B.CreateCall(Func, {Num, NoThrow, B.getInt8(HotCold)}, Name);
2130
2131 if (const Function *F =
2132 dyn_cast<Function>(Func.getCallee()->stripPointerCasts()))
2133 CI->setCallingConv(F->getCallingConv());
2134
2135 return CI;
2136}
2137
2140 const TargetLibraryInfo *TLI,
2141 LibFunc NewFunc, uint8_t HotCold) {
2142 Module *M = B.GetInsertBlock()->getModule();
2143 if (!isLibFuncEmittable(M, TLI, NewFunc))
2144 return nullptr;
2145
2146 StringRef Name = TLI->getName(NewFunc);
2147 FunctionCallee Func = M->getOrInsertFunction(Name, RetTy, Num->getType(),
2148 Align->getType(), B.getInt8Ty());
2150 CallInst *CI = B.CreateCall(Func, {Num, Align, B.getInt8(HotCold)}, Name);
2151
2152 if (const Function *F =
2153 dyn_cast<Function>(Func.getCallee()->stripPointerCasts()))
2154 CI->setCallingConv(F->getCallingConv());
2155
2156 return CI;
2157}
2158
2160 Value *NoThrow, IRBuilderBase &B,
2161 const TargetLibraryInfo *TLI,
2162 LibFunc NewFunc, uint8_t HotCold) {
2163 Module *M = B.GetInsertBlock()->getModule();
2164 if (!isLibFuncEmittable(M, TLI, NewFunc))
2165 return nullptr;
2166
2167 StringRef Name = TLI->getName(NewFunc);
2168 FunctionCallee Func =
2169 M->getOrInsertFunction(Name, RetTy, Num->getType(), Align->getType(),
2170 NoThrow->getType(), B.getInt8Ty());
2172 CallInst *CI =
2173 B.CreateCall(Func, {Num, Align, NoThrow, B.getInt8(HotCold)}, Name);
2174
2175 if (const Function *F =
2176 dyn_cast<Function>(Func.getCallee()->stripPointerCasts()))
2177 CI->setCallingConv(F->getCallingConv());
2178
2179 return CI;
2180}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool setRetNoUndef(Function &F)
static void appendTypeSuffix(Value *Op, StringRef &Name, SmallString< 20 > &NameBuffer)
Append a suffix to the function name according to the type of 'Op'.
static bool setDoesNotAlias(Function &F, unsigned ArgNo)
static bool setDoesNotAccessMemory(Function &F)
static bool setArgsNoUndef(Function &F)
static IntegerType * getSizeTTy(IRBuilderBase &B, const TargetLibraryInfo *TLI)
static Value * emitUnaryFloatFnCallHelper(Value *Op, LibFunc TheLibFunc, StringRef Name, IRBuilderBase &B, const AttributeList &Attrs, const TargetLibraryInfo *TLI)
static bool setAllocatedPointerParam(Function &F, unsigned ArgNo)
static void setRetExtAttr(Function &F, const TargetLibraryInfo &TLI, bool Signed=true)
static Value * emitLibCall(LibFunc TheLibFunc, Type *ReturnType, ArrayRef< Type * > ParamTypes, ArrayRef< Value * > Operands, IRBuilderBase &B, const TargetLibraryInfo *TLI, bool IsVaArgs=false)
static bool setNonLazyBind(Function &F)
static bool setIsCold(Function &F)
static bool setOnlyAccessesInaccessibleMemOrArgMem(Function &F)
static bool setAllocSize(Function &F, unsigned ElemSizeArg, std::optional< unsigned > NumElemsArg)
static bool setAlignedAllocParam(Function &F, unsigned ArgNo)
static bool setRetAndArgsNoUndef(Function &F)
static bool setRetDoesNotAlias(Function &F)
static bool setReturnedArg(Function &F, unsigned ArgNo)
static Value * emitBinaryFloatFnCallHelper(Value *Op1, Value *Op2, LibFunc TheLibFunc, StringRef Name, IRBuilderBase &B, const AttributeList &Attrs, const TargetLibraryInfo *TLI)
static bool setDoesNotCapture(Function &F, unsigned ArgNo)
static bool setDoesNotThrow(Function &F)
static bool setWillReturn(Function &F)
static bool setAllocKind(Function &F, AllocFnKind K)
static bool setNoReturn(Function &F)
static bool setOnlyAccessesInaccessibleMemory(Function &F)
static IntegerType * getIntTy(IRBuilderBase &B, const TargetLibraryInfo *TLI)
static bool setAllocFamily(Function &F, StringRef Family)
static bool setArgNoUndef(Function &F, unsigned ArgNo)
static void setArgExtAttr(Function &F, unsigned ArgNo, const TargetLibraryInfo &TLI, bool Signed=true)
static bool setOnlyAccessesArgMemory(Function &F)
static bool setOnlyWritesMemory(Function &F)
static bool setOnlyReadsMemory(Function &F)
static bool setDoesNotFreeMemory(Function &F)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
return RetTy
std::string Name
uint64_t Size
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
mir Rename Register Operands
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute > > Attrs)
Create an AttributeList with the specified parameters in it.
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:95
static Attribute getWithAllocSizeArgs(LLVMContext &Context, unsigned ElemSizeArg, const std::optional< unsigned > &NumElemsArg)
Definition: Attributes.cpp:292
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:86
@ None
No attributes have been set.
Definition: Attributes.h:88
void setCallingConv(CallingConv::ID CC)
Definition: InstrTypes.h:1411
void setAttributes(AttributeList A)
Set the attributes for this call.
Definition: InstrTypes.h:1428
This class represents a function call, abstracting a target machine's calling convention.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
Class to represent function types.
Definition: DerivedTypes.h:105
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:91
Class to represent integer types.
Definition: DerivedTypes.h:42
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Class to represent struct types.
Definition: DerivedTypes.h:218
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:406
Provides information about what library functions are available for the current target.
bool isValidProtoForLibFunc(const FunctionType &FTy, LibFunc F, const Module &M) const
Return true if the function type FTy is valid for the library function F, regardless of whether the f...
bool has(LibFunc F) const
Tests whether a library function is available.
unsigned getSizeTSize(const Module &M) const
Returns the size of the size_t type in bits.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
StringRef getName(LibFunc F) const
unsigned getIntSize() const
Get size of a C-level int or unsigned int, in bits.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
@ HalfTyID
16-bit floating point type
Definition: Type.h:56
@ FloatTyID
32-bit floating point type
Definition: Type.h:58
@ DoubleTyID
64-bit floating point type
Definition: Type.h:59
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:136
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:694
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ X86_StdCall
stdcall is mostly used by the Win32 API.
Definition: CallingConv.h:99
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Value * emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, StringRef Name, IRBuilderBase &B, const AttributeList &Attrs)
Emit a call to the unary function named 'Name' (e.g.
Value * emitStrChr(Value *Ptr, char C, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strchr function to the builder, for the specified pointer and character.
Value * emitHotColdNewNoThrow(Type *RetTy, Value *Num, Value *NoThrow, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
Value * emitPutChar(Value *Char, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the putchar function. This assumes that Char is an 'int'.
Value * emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the __memcpy_chk function to the builder.
Value * emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strncpy function to the builder, for the specified pointer arguments and length.
Value * emitMalloc(Type *RetTy, Value *Num, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the malloc function.
Value * emitHotColdSizeReturningNew(Type *RetTy, Value *Num, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
AllocFnKind
Definition: Attributes.h:49
Value * emitSPrintf(Value *Dest, Value *Fmt, ArrayRef< Value * > VariadicArgs, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the sprintf function.
Value * emitMemRChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memrchr function, analogously to emitMemChr.
Value * emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcat function.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2115
bool hasFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Check whether the overloaded floating point function corresponding to Ty is available.
bool inferNonMandatoryLibFuncAttrs(Module *M, StringRef Name, const TargetLibraryInfo &TLI)
Analyze the name and prototype of the given function and set any applicable attributes.
bool isLibFreeFunction(const Function *F, const LibFunc TLIFn)
isLibFreeFunction - Returns true if the function is a builtin free()
Value * emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strncat function.
bool isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI, LibFunc TheLibFunc)
Check whether the library function is available on target and also that it in the current Module is a...
Value * emitHotColdNew(Type *RetTy, Value *Num, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
Emit a call to the hot/cold operator new function.
Value * emitHotColdNewAlignedNoThrow(Type *RetTy, Value *Num, Value *Align, Value *NoThrow, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
Value * emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the vsnprintf function.
Value * emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strncmp function to the builder.
Value * emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memcmp function.
Value * emitBinaryFloatFnCall(Value *Op1, Value *Op2, const TargetLibraryInfo *TLI, StringRef Name, IRBuilderBase &B, const AttributeList &Attrs)
Emit a call to the binary function named 'Name' (e.g.
Value * emitHotColdSizeReturningNewAligned(Type *RetTy, Value *Num, Value *Align, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
Value * emitFPutS(Value *Str, Value *File, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the fputs function.
Value * emitStrDup(Value *Ptr, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strdup function to the builder, for the specified pointer.
Value * emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the bcmp function.
void markRegisterParameterAttributes(Function *F)
Value * emitCalloc(Type *RetTy, Value *Num, Value *Size, IRBuilderBase &B, const TargetLibraryInfo &TLI)
Emit a call to the calloc function.
StringRef getFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn, LibFunc &TheLibFunc)
Get the name of the overloaded floating point function corresponding to Ty.
FunctionCallee getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI, LibFunc TheLibFunc, FunctionType *T, AttributeList AttributeList)
Calls getOrInsertFunction() and then makes sure to add mandatory argument attributes.
Value * emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strlen function to the builder, for the specified pointer.
Value * emitFPutC(Value *Char, Value *File, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the fputc function.
Value * emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the stpncpy function to the builder, for the specified pointer arguments and length.
Value * emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strcat function.
Value * emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the vsprintf function.
bool isReallocLikeFn(const Function *F)
Tests if a function is a call or invoke to a library function that reallocates memory (e....
Value * emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the fwrite function.
Value * emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, ArrayRef< Value * > Args, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the snprintf function.
Value * emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the stpcpy function to the builder, for the specified pointer arguments.
Value * emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memchr function.
Value * emitPutS(Value *Str, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the puts function. This assumes that Str is some pointer.
Value * emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the memccpy function.
Value * emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcpy function.
Value * emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strcpy function to the builder, for the specified pointer arguments.
Value * emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the mempcpy function.
Value * emitHotColdNewAligned(Type *RetTy, Value *Num, Value *Align, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39