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