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