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_ceil:
1231 case LibFunc_ceilf:
1232 case LibFunc_ceill:
1233 case LibFunc_cos:
1234 case LibFunc_cosh:
1235 case LibFunc_coshf:
1236 case LibFunc_coshl:
1237 case LibFunc_cosf:
1238 case LibFunc_cosl:
1239 case LibFunc_cospi:
1240 case LibFunc_cospif:
1241 case LibFunc_erf:
1242 case LibFunc_erff:
1243 case LibFunc_erfl:
1244 case LibFunc_tgamma:
1245 case LibFunc_tgammaf:
1246 case LibFunc_tgammal:
1247 case LibFunc_exp:
1248 case LibFunc_expf:
1249 case LibFunc_expl:
1250 case LibFunc_exp2:
1251 case LibFunc_exp2f:
1252 case LibFunc_exp2l:
1253 case LibFunc_expm1:
1254 case LibFunc_expm1f:
1255 case LibFunc_expm1l:
1256 case LibFunc_fdim:
1257 case LibFunc_fdiml:
1258 case LibFunc_fdimf:
1259 case LibFunc_fmod:
1260 case LibFunc_fmodf:
1261 case LibFunc_fmodl:
1262 case LibFunc_hypot:
1263 case LibFunc_hypotf:
1264 case LibFunc_hypotl:
1265 case LibFunc_ldexp:
1266 case LibFunc_ldexpf:
1267 case LibFunc_ldexpl:
1268 case LibFunc_log:
1269 case LibFunc_log10:
1270 case LibFunc_log10f:
1271 case LibFunc_log10l:
1272 case LibFunc_log1p:
1273 case LibFunc_log1pf:
1274 case LibFunc_log1pl:
1275 case LibFunc_log2:
1276 case LibFunc_log2f:
1277 case LibFunc_log2l:
1278 case LibFunc_logb:
1279 case LibFunc_logbf:
1280 case LibFunc_logbl:
1281 case LibFunc_ilogb:
1282 case LibFunc_ilogbf:
1283 case LibFunc_ilogbl:
1284 case LibFunc_logf:
1285 case LibFunc_logl:
1286 case LibFunc_pow:
1287 case LibFunc_powf:
1288 case LibFunc_powl:
1289 case LibFunc_remainder:
1290 case LibFunc_remainderf:
1291 case LibFunc_remainderl:
1292 case LibFunc_rint:
1293 case LibFunc_rintf:
1294 case LibFunc_rintl:
1295 case LibFunc_round:
1296 case LibFunc_roundf:
1297 case LibFunc_roundl:
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_fabs:
1336 case LibFunc_fabsf:
1337 case LibFunc_fabsl:
1338 case LibFunc_ffs:
1339 case LibFunc_ffsl:
1340 case LibFunc_ffsll:
1341 case LibFunc_floor:
1342 case LibFunc_floorf:
1343 case LibFunc_floorl:
1344 case LibFunc_fls:
1345 case LibFunc_flsl:
1346 case LibFunc_flsll:
1347 case LibFunc_fmax:
1348 case LibFunc_fmaxf:
1349 case LibFunc_fmaxl:
1350 case LibFunc_fmin:
1351 case LibFunc_fminf:
1352 case LibFunc_fminl:
1353 case LibFunc_labs:
1354 case LibFunc_llabs:
1355 case LibFunc_nearbyint:
1356 case LibFunc_nearbyintf:
1357 case LibFunc_nearbyintl:
1358 case LibFunc_toascii:
1359 case LibFunc_trunc:
1360 case LibFunc_truncf:
1361 case LibFunc_truncl:
1362 Changed |= setDoesNotAccessMemory(F);
1363 [[fallthrough]];
1364 case LibFunc_isascii:
1365 case LibFunc_isdigit:
1366 Changed |= setDoesNotThrow(F);
1367 Changed |= setDoesNotCallback(F);
1368 Changed |= setDoesNotFreeMemory(F);
1369 Changed |= setWillReturn(F);
1370 break;
1371 case LibFunc_sincos:
1372 case LibFunc_sincosf:
1373 case LibFunc_sincosl:
1374 Changed |= setDoesNotCapture(F, ArgNo: 1);
1375 Changed |= setOnlyWritesMemory(F, ArgNo: 1);
1376 [[fallthrough]];
1377 case LibFunc_remquo:
1378 case LibFunc_remquof:
1379 case LibFunc_remquol:
1380 Changed |= setDoesNotThrow(F);
1381 Changed |= setDoesNotCallback(F);
1382 Changed |= setDoesNotFreeMemory(F);
1383 Changed |= setOnlyWritesMemory(F, ArgNo: 2);
1384 Changed |= setDoesNotCapture(F, ArgNo: 2);
1385 Changed |= setWillReturn(F);
1386 Changed |= setOnlyWritesArgMemOrErrnoMem(F);
1387 break;
1388 default:
1389 // FIXME: It'd be really nice to cover all the library functions we're
1390 // aware of here.
1391 break;
1392 }
1393 // We have to do this step after AllocKind has been inferred on functions so
1394 // we can reliably identify free-like and realloc-like functions.
1395 if (!isLibFreeFunction(F: &F, TLIFn: TheLibFunc) && !isReallocLikeFn(F: &F))
1396 Changed |= setDoesNotFreeMemory(F);
1397 return Changed;
1398}
1399
1400static void setArgExtAttr(Function &F, unsigned ArgNo,
1401 const TargetLibraryInfo &TLI, bool Signed = true) {
1402 Attribute::AttrKind ExtAttr = TLI.getExtAttrForI32Param(Signed);
1403 if (ExtAttr != Attribute::None && !F.hasParamAttribute(ArgNo, Kind: ExtAttr))
1404 F.addParamAttr(ArgNo, Kind: ExtAttr);
1405}
1406
1407static void setRetExtAttr(Function &F,
1408 const TargetLibraryInfo &TLI, bool Signed = true) {
1409 Attribute::AttrKind ExtAttr = TLI.getExtAttrForI32Return(Signed);
1410 if (ExtAttr != Attribute::None && !F.hasRetAttribute(Kind: ExtAttr))
1411 F.addRetAttr(Kind: ExtAttr);
1412}
1413
1414// Modeled after X86TargetLowering::markLibCallAttributes.
1415void llvm::markRegisterParameterAttributes(Function *F) {
1416 if (!F->arg_size() || F->isVarArg())
1417 return;
1418
1419 const CallingConv::ID CC = F->getCallingConv();
1420 if (CC != CallingConv::C && CC != CallingConv::X86_StdCall)
1421 return;
1422
1423 const Module *M = F->getParent();
1424 unsigned N = M->getNumberRegisterParameters();
1425 if (!N)
1426 return;
1427
1428 const DataLayout &DL = M->getDataLayout();
1429
1430 for (Argument &A : F->args()) {
1431 Type *T = A.getType();
1432 if (!T->isIntOrPtrTy())
1433 continue;
1434
1435 const TypeSize &TS = DL.getTypeAllocSize(Ty: T);
1436 if (TS > 8)
1437 continue;
1438
1439 assert(TS <= 4 && "Need to account for parameters larger than word size");
1440 const unsigned NumRegs = TS > 4 ? 2 : 1;
1441 if (N < NumRegs)
1442 return;
1443
1444 N -= NumRegs;
1445 F->addParamAttr(ArgNo: A.getArgNo(), Kind: Attribute::InReg);
1446 }
1447}
1448
1449FunctionCallee llvm::getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI,
1450 LibFunc TheLibFunc, FunctionType *T,
1451 AttributeList AttributeList) {
1452 assert(TLI.has(TheLibFunc) &&
1453 "Creating call to non-existing library function.");
1454 StringRef Name = TLI.getName(F: TheLibFunc);
1455 FunctionCallee C = M->getOrInsertFunction(Name, T, AttributeList);
1456
1457 // Make sure any mandatory argument attributes are added.
1458
1459 // Any outgoing i32 argument should be handled with setArgExtAttr() which
1460 // will add an extension attribute if the target ABI requires it. Adding
1461 // argument extensions is typically done by the front end but when an
1462 // optimizer is building a library call on its own it has to take care of
1463 // this. Each such generated function must be handled here with sign or
1464 // zero extensions as needed. F is retreived with cast<> because we demand
1465 // of the caller to have called isLibFuncEmittable() first.
1466 Function *F = cast<Function>(Val: C.getCallee());
1467 assert(F->getFunctionType() == T && "Function type does not match.");
1468 switch (TheLibFunc) {
1469 case LibFunc_fputc:
1470 case LibFunc_putchar:
1471 setArgExtAttr(F&: *F, ArgNo: 0, TLI);
1472 break;
1473 case LibFunc_ldexp:
1474 case LibFunc_ldexpf:
1475 case LibFunc_ldexpl:
1476 case LibFunc_memchr:
1477 case LibFunc_memrchr:
1478 case LibFunc_strchr:
1479 setArgExtAttr(F&: *F, ArgNo: 1, TLI);
1480 break;
1481 case LibFunc_memccpy:
1482 setArgExtAttr(F&: *F, ArgNo: 2, TLI);
1483 break;
1484
1485 // These are functions that are known to not need any argument extension
1486 // on any target: A size_t argument (which may be an i32 on some targets)
1487 // should not trigger the assert below.
1488 case LibFunc_bcmp:
1489 setRetExtAttr(F&: *F, TLI);
1490 break;
1491 case LibFunc_calloc:
1492 case LibFunc_fwrite:
1493 case LibFunc_malloc:
1494 case LibFunc_memcmp:
1495 case LibFunc_memcpy_chk:
1496 case LibFunc_mempcpy:
1497 case LibFunc_memset_pattern16:
1498 case LibFunc_snprintf:
1499 case LibFunc_stpncpy:
1500 case LibFunc_strlcat:
1501 case LibFunc_strlcpy:
1502 case LibFunc_strncat:
1503 case LibFunc_strncmp:
1504 case LibFunc_strncpy:
1505 case LibFunc_vsnprintf:
1506 break;
1507
1508 default:
1509#ifndef NDEBUG
1510 for (unsigned i = 0; i < T->getNumParams(); i++)
1511 assert(!isa<IntegerType>(T->getParamType(i)) &&
1512 "Unhandled integer argument.");
1513#endif
1514 break;
1515 }
1516
1517 markRegisterParameterAttributes(F);
1518
1519 return C;
1520}
1521
1522FunctionCallee llvm::getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI,
1523 LibFunc TheLibFunc, FunctionType *T) {
1524 return getOrInsertLibFunc(M, TLI, TheLibFunc, T, AttributeList: AttributeList());
1525}
1526
1527bool llvm::isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI,
1528 LibFunc TheLibFunc) {
1529 StringRef FuncName = TLI->getName(F: TheLibFunc);
1530 if (!TLI->has(F: TheLibFunc))
1531 return false;
1532
1533 // Check if the Module already has a GlobalValue with the same name, in
1534 // which case it must be a Function with the expected type.
1535 if (GlobalValue *GV = M->getNamedValue(Name: FuncName)) {
1536 if (auto *F = dyn_cast<Function>(Val: GV))
1537 return TLI->isValidProtoForLibFunc(FTy: *F->getFunctionType(), F: TheLibFunc, M: *M);
1538 return false;
1539 }
1540
1541 return true;
1542}
1543
1544bool llvm::isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI,
1545 StringRef Name) {
1546 LibFunc TheLibFunc;
1547 return TLI->getLibFunc(funcName: Name, F&: TheLibFunc) &&
1548 isLibFuncEmittable(M, TLI, TheLibFunc);
1549}
1550
1551bool llvm::hasFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty,
1552 LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn) {
1553 switch (Ty->getTypeID()) {
1554 case Type::HalfTyID:
1555 return false;
1556 case Type::FloatTyID:
1557 return isLibFuncEmittable(M, TLI, TheLibFunc: FloatFn);
1558 case Type::DoubleTyID:
1559 return isLibFuncEmittable(M, TLI, TheLibFunc: DoubleFn);
1560 default:
1561 return isLibFuncEmittable(M, TLI, TheLibFunc: LongDoubleFn);
1562 }
1563}
1564
1565StringRef llvm::getFloatFn(const Module *M, const TargetLibraryInfo *TLI,
1566 Type *Ty, LibFunc DoubleFn, LibFunc FloatFn,
1567 LibFunc LongDoubleFn, LibFunc &TheLibFunc) {
1568 assert(hasFloatFn(M, TLI, Ty, DoubleFn, FloatFn, LongDoubleFn) &&
1569 "Cannot get name for unavailable function!");
1570
1571 switch (Ty->getTypeID()) {
1572 case Type::HalfTyID:
1573 llvm_unreachable("No name for HalfTy!");
1574 case Type::FloatTyID:
1575 TheLibFunc = FloatFn;
1576 return TLI->getName(F: FloatFn);
1577 case Type::DoubleTyID:
1578 TheLibFunc = DoubleFn;
1579 return TLI->getName(F: DoubleFn);
1580 default:
1581 TheLibFunc = LongDoubleFn;
1582 return TLI->getName(F: LongDoubleFn);
1583 }
1584}
1585
1586//- Emit LibCalls ------------------------------------------------------------//
1587
1588static IntegerType *getIntTy(IRBuilderBase &B, const TargetLibraryInfo *TLI) {
1589 return B.getIntNTy(N: TLI->getIntSize());
1590}
1591
1592static IntegerType *getSizeTTy(IRBuilderBase &B, const TargetLibraryInfo *TLI) {
1593 const Module *M = B.GetInsertBlock()->getModule();
1594 return B.getIntNTy(N: TLI->getSizeTSize(M: *M));
1595}
1596
1597static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType,
1598 ArrayRef<Type *> ParamTypes,
1599 ArrayRef<Value *> Operands, IRBuilderBase &B,
1600 const TargetLibraryInfo *TLI,
1601 bool IsVaArgs = false) {
1602 Module *M = B.GetInsertBlock()->getModule();
1603 if (!isLibFuncEmittable(M, TLI, TheLibFunc))
1604 return nullptr;
1605
1606 StringRef FuncName = TLI->getName(F: TheLibFunc);
1607 FunctionType *FuncType = FunctionType::get(Result: ReturnType, Params: ParamTypes, isVarArg: IsVaArgs);
1608 FunctionCallee Callee = getOrInsertLibFunc(M, TLI: *TLI, TheLibFunc, T: FuncType);
1609 inferNonMandatoryLibFuncAttrs(M, Name: FuncName, TLI: *TLI);
1610 CallInst *CI = B.CreateCall(Callee, Args: Operands, Name: FuncName);
1611 if (const Function *F =
1612 dyn_cast<Function>(Val: Callee.getCallee()->stripPointerCasts()))
1613 CI->setCallingConv(F->getCallingConv());
1614 return CI;
1615}
1616
1617Value *llvm::emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL,
1618 const TargetLibraryInfo *TLI) {
1619 Type *CharPtrTy = B.getPtrTy();
1620 Type *SizeTTy = getSizeTTy(B, TLI);
1621 return emitLibCall(TheLibFunc: LibFunc_strlen, ReturnType: SizeTTy, ParamTypes: CharPtrTy, Operands: Ptr, B, TLI);
1622}
1623
1624Value *llvm::emitWcsLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL,
1625 const TargetLibraryInfo *TLI) {
1626 assert(Ptr && Ptr->getType()->isPointerTy() &&
1627 "Argument to wcslen intrinsic must be a pointer.");
1628 Type *PtrTy = B.getPtrTy();
1629 Type *SizeTTy = getSizeTTy(B, TLI);
1630 return emitLibCall(TheLibFunc: LibFunc_wcslen, ReturnType: SizeTTy, ParamTypes: PtrTy, Operands: Ptr, B, TLI);
1631}
1632
1633Value *llvm::emitStrDup(Value *Ptr, IRBuilderBase &B,
1634 const TargetLibraryInfo *TLI) {
1635 Type *CharPtrTy = B.getPtrTy();
1636 return emitLibCall(TheLibFunc: LibFunc_strdup, ReturnType: CharPtrTy, ParamTypes: CharPtrTy, Operands: Ptr, B, TLI);
1637}
1638
1639Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilderBase &B,
1640 const TargetLibraryInfo *TLI) {
1641 Type *CharPtrTy = B.getPtrTy();
1642 Type *IntTy = getIntTy(B, TLI);
1643 return emitLibCall(TheLibFunc: LibFunc_strchr, ReturnType: CharPtrTy, ParamTypes: {CharPtrTy, IntTy},
1644 Operands: {Ptr, ConstantInt::get(Ty: IntTy, V: C)}, B, TLI);
1645}
1646
1647Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
1648 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1649 Type *CharPtrTy = B.getPtrTy();
1650 Type *IntTy = getIntTy(B, TLI);
1651 Type *SizeTTy = getSizeTTy(B, TLI);
1652 return emitLibCall(
1653 TheLibFunc: LibFunc_strncmp, ReturnType: IntTy,
1654 ParamTypes: {CharPtrTy, CharPtrTy, SizeTTy},
1655 Operands: {Ptr1, Ptr2, Len}, B, TLI);
1656}
1657
1658Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B,
1659 const TargetLibraryInfo *TLI) {
1660 Type *CharPtrTy = Dst->getType();
1661 return emitLibCall(TheLibFunc: LibFunc_strcpy, ReturnType: CharPtrTy, ParamTypes: {CharPtrTy, CharPtrTy},
1662 Operands: {Dst, Src}, B, TLI);
1663}
1664
1665Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B,
1666 const TargetLibraryInfo *TLI) {
1667 Type *CharPtrTy = B.getPtrTy();
1668 return emitLibCall(TheLibFunc: LibFunc_stpcpy, ReturnType: CharPtrTy, ParamTypes: {CharPtrTy, CharPtrTy},
1669 Operands: {Dst, Src}, B, TLI);
1670}
1671
1672Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
1673 const TargetLibraryInfo *TLI) {
1674 Type *CharPtrTy = B.getPtrTy();
1675 Type *SizeTTy = getSizeTTy(B, TLI);
1676 return emitLibCall(TheLibFunc: LibFunc_strncpy, ReturnType: CharPtrTy, ParamTypes: {CharPtrTy, CharPtrTy, SizeTTy},
1677 Operands: {Dst, Src, Len}, B, TLI);
1678}
1679
1680Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
1681 const TargetLibraryInfo *TLI) {
1682 Type *CharPtrTy = B.getPtrTy();
1683 Type *SizeTTy = getSizeTTy(B, TLI);
1684 return emitLibCall(TheLibFunc: LibFunc_stpncpy, ReturnType: CharPtrTy, ParamTypes: {CharPtrTy, CharPtrTy, SizeTTy},
1685 Operands: {Dst, Src, Len}, B, TLI);
1686}
1687
1688Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
1689 IRBuilderBase &B, const DataLayout &DL,
1690 const TargetLibraryInfo *TLI) {
1691 Module *M = B.GetInsertBlock()->getModule();
1692 if (!isLibFuncEmittable(M, TLI, TheLibFunc: LibFunc_memcpy_chk))
1693 return nullptr;
1694
1695 AttributeList AS;
1696 AS = AttributeList::get(C&: M->getContext(), Index: AttributeList::FunctionIndex,
1697 Kinds: Attribute::NoUnwind);
1698 Type *VoidPtrTy = B.getPtrTy();
1699 Type *SizeTTy = getSizeTTy(B, TLI);
1700 FunctionCallee MemCpy = getOrInsertLibFunc(M, TLI: *TLI, TheLibFunc: LibFunc_memcpy_chk,
1701 AttributeList: AttributeList::get(C&: M->getContext(), Attrs: AS), RetTy: VoidPtrTy,
1702 Args: VoidPtrTy, Args: VoidPtrTy, Args: SizeTTy, Args: SizeTTy);
1703 CallInst *CI = B.CreateCall(Callee: MemCpy, Args: {Dst, Src, Len, ObjSize});
1704 if (const Function *F =
1705 dyn_cast<Function>(Val: MemCpy.getCallee()->stripPointerCasts()))
1706 CI->setCallingConv(F->getCallingConv());
1707 return CI;
1708}
1709
1710Value *llvm::emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
1711 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1712 Type *VoidPtrTy = B.getPtrTy();
1713 Type *SizeTTy = getSizeTTy(B, TLI);
1714 return emitLibCall(TheLibFunc: LibFunc_mempcpy, ReturnType: VoidPtrTy,
1715 ParamTypes: {VoidPtrTy, VoidPtrTy, SizeTTy},
1716 Operands: {Dst, Src, Len}, B, TLI);
1717}
1718
1719Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B,
1720 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1721 Type *VoidPtrTy = B.getPtrTy();
1722 Type *IntTy = getIntTy(B, TLI);
1723 Type *SizeTTy = getSizeTTy(B, TLI);
1724 return emitLibCall(TheLibFunc: LibFunc_memchr, ReturnType: VoidPtrTy,
1725 ParamTypes: {VoidPtrTy, IntTy, SizeTTy},
1726 Operands: {Ptr, Val, Len}, B, TLI);
1727}
1728
1729Value *llvm::emitMemRChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B,
1730 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1731 Type *VoidPtrTy = B.getPtrTy();
1732 Type *IntTy = getIntTy(B, TLI);
1733 Type *SizeTTy = getSizeTTy(B, TLI);
1734 return emitLibCall(TheLibFunc: LibFunc_memrchr, ReturnType: VoidPtrTy,
1735 ParamTypes: {VoidPtrTy, IntTy, SizeTTy},
1736 Operands: {Ptr, Val, Len}, B, TLI);
1737}
1738
1739Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
1740 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1741 Type *VoidPtrTy = B.getPtrTy();
1742 Type *IntTy = getIntTy(B, TLI);
1743 Type *SizeTTy = getSizeTTy(B, TLI);
1744 return emitLibCall(TheLibFunc: LibFunc_memcmp, ReturnType: IntTy,
1745 ParamTypes: {VoidPtrTy, VoidPtrTy, SizeTTy},
1746 Operands: {Ptr1, Ptr2, Len}, B, TLI);
1747}
1748
1749Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
1750 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1751 Type *VoidPtrTy = B.getPtrTy();
1752 Type *IntTy = getIntTy(B, TLI);
1753 Type *SizeTTy = getSizeTTy(B, TLI);
1754 return emitLibCall(TheLibFunc: LibFunc_bcmp, ReturnType: IntTy,
1755 ParamTypes: {VoidPtrTy, VoidPtrTy, SizeTTy},
1756 Operands: {Ptr1, Ptr2, Len}, B, TLI);
1757}
1758
1759Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
1760 IRBuilderBase &B, const TargetLibraryInfo *TLI) {
1761 Type *VoidPtrTy = B.getPtrTy();
1762 Type *IntTy = getIntTy(B, TLI);
1763 Type *SizeTTy = getSizeTTy(B, TLI);
1764 return emitLibCall(TheLibFunc: LibFunc_memccpy, ReturnType: VoidPtrTy,
1765 ParamTypes: {VoidPtrTy, VoidPtrTy, IntTy, SizeTTy},
1766 Operands: {Ptr1, Ptr2, Val, Len}, B, TLI);
1767}
1768
1769Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
1770 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B,
1771 const TargetLibraryInfo *TLI) {
1772 Type *CharPtrTy = B.getPtrTy();
1773 Type *IntTy = getIntTy(B, TLI);
1774 Type *SizeTTy = getSizeTTy(B, TLI);
1775 SmallVector<Value *, 8> Args{Dest, Size, Fmt};
1776 llvm::append_range(C&: Args, R&: VariadicArgs);
1777 return emitLibCall(TheLibFunc: LibFunc_snprintf, ReturnType: IntTy,
1778 ParamTypes: {CharPtrTy, SizeTTy, CharPtrTy},
1779 Operands: Args, B, TLI, /*IsVaArgs=*/true);
1780}
1781
1782Value *llvm::emitSPrintf(Value *Dest, Value *Fmt,
1783 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B,
1784 const TargetLibraryInfo *TLI) {
1785 Type *CharPtrTy = B.getPtrTy();
1786 Type *IntTy = getIntTy(B, TLI);
1787 SmallVector<Value *, 8> Args{Dest, Fmt};
1788 llvm::append_range(C&: Args, R&: VariadicArgs);
1789 return emitLibCall(TheLibFunc: LibFunc_sprintf, ReturnType: IntTy,
1790 ParamTypes: {CharPtrTy, CharPtrTy}, Operands: Args, B, TLI,
1791 /*IsVaArgs=*/true);
1792}
1793
1794Value *llvm::emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B,
1795 const TargetLibraryInfo *TLI) {
1796 Type *CharPtrTy = B.getPtrTy();
1797 return emitLibCall(TheLibFunc: LibFunc_strcat, ReturnType: CharPtrTy,
1798 ParamTypes: {CharPtrTy, CharPtrTy},
1799 Operands: {Dest, Src}, B, TLI);
1800}
1801
1802Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
1803 const TargetLibraryInfo *TLI) {
1804 Type *CharPtrTy = B.getPtrTy();
1805 Type *SizeTTy = getSizeTTy(B, TLI);
1806 return emitLibCall(TheLibFunc: LibFunc_strlcpy, ReturnType: SizeTTy,
1807 ParamTypes: {CharPtrTy, CharPtrTy, SizeTTy},
1808 Operands: {Dest, Src, Size}, B, TLI);
1809}
1810
1811Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
1812 const TargetLibraryInfo *TLI) {
1813 Type *CharPtrTy = B.getPtrTy();
1814 Type *SizeTTy = getSizeTTy(B, TLI);
1815 return emitLibCall(TheLibFunc: LibFunc_strlcat, ReturnType: SizeTTy,
1816 ParamTypes: {CharPtrTy, CharPtrTy, SizeTTy},
1817 Operands: {Dest, Src, Size}, B, TLI);
1818}
1819
1820Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B,
1821 const TargetLibraryInfo *TLI) {
1822 Type *CharPtrTy = B.getPtrTy();
1823 Type *SizeTTy = getSizeTTy(B, TLI);
1824 return emitLibCall(TheLibFunc: LibFunc_strncat, ReturnType: CharPtrTy,
1825 ParamTypes: {CharPtrTy, CharPtrTy, SizeTTy},
1826 Operands: {Dest, Src, Size}, B, TLI);
1827}
1828
1829Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
1830 IRBuilderBase &B, const TargetLibraryInfo *TLI) {
1831 Type *CharPtrTy = B.getPtrTy();
1832 Type *IntTy = getIntTy(B, TLI);
1833 Type *SizeTTy = getSizeTTy(B, TLI);
1834 return emitLibCall(
1835 TheLibFunc: LibFunc_vsnprintf, ReturnType: IntTy,
1836 ParamTypes: {CharPtrTy, SizeTTy, CharPtrTy, VAList->getType()},
1837 Operands: {Dest, Size, Fmt, VAList}, B, TLI);
1838}
1839
1840Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList,
1841 IRBuilderBase &B, const TargetLibraryInfo *TLI) {
1842 Type *CharPtrTy = B.getPtrTy();
1843 Type *IntTy = getIntTy(B, TLI);
1844 return emitLibCall(TheLibFunc: LibFunc_vsprintf, ReturnType: IntTy,
1845 ParamTypes: {CharPtrTy, CharPtrTy, VAList->getType()},
1846 Operands: {Dest, Fmt, VAList}, B, TLI);
1847}
1848
1849/// Append a suffix to the function name according to the type of 'Op'.
1850static void appendTypeSuffix(Value *Op, StringRef &Name,
1851 SmallString<20> &NameBuffer) {
1852 if (!Op->getType()->isDoubleTy()) {
1853 NameBuffer += Name;
1854
1855 if (Op->getType()->isFloatTy())
1856 NameBuffer += 'f';
1857 else
1858 NameBuffer += 'l';
1859
1860 Name = NameBuffer;
1861 }
1862}
1863
1864static Value *emitUnaryFloatFnCallHelper(Value *Op, LibFunc TheLibFunc,
1865 StringRef Name, IRBuilderBase &B,
1866 const AttributeList &Attrs,
1867 const TargetLibraryInfo *TLI) {
1868 assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall");
1869
1870 Module *M = B.GetInsertBlock()->getModule();
1871 FunctionCallee Callee = getOrInsertLibFunc(M, TLI: *TLI, TheLibFunc, RetTy: Op->getType(),
1872 Args: Op->getType());
1873 CallInst *CI = B.CreateCall(Callee, Args: Op, Name);
1874
1875 // The incoming attribute set may have come from a speculatable intrinsic, but
1876 // is being replaced with a library call which is not allowed to be
1877 // speculatable.
1878 CI->setAttributes(
1879 Attrs.removeFnAttribute(C&: B.getContext(), Kind: Attribute::Speculatable));
1880 if (const Function *F =
1881 dyn_cast<Function>(Val: Callee.getCallee()->stripPointerCasts()))
1882 CI->setCallingConv(F->getCallingConv());
1883
1884 return CI;
1885}
1886
1887Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
1888 StringRef Name, IRBuilderBase &B,
1889 const AttributeList &Attrs) {
1890 SmallString<20> NameBuffer;
1891 appendTypeSuffix(Op, Name, NameBuffer);
1892
1893 LibFunc TheLibFunc;
1894 TLI->getLibFunc(funcName: Name, F&: TheLibFunc);
1895
1896 return emitUnaryFloatFnCallHelper(Op, TheLibFunc, Name, B, Attrs, TLI);
1897}
1898
1899Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
1900 LibFunc DoubleFn, LibFunc FloatFn,
1901 LibFunc LongDoubleFn, IRBuilderBase &B,
1902 const AttributeList &Attrs) {
1903 // Get the name of the function according to TLI.
1904 Module *M = B.GetInsertBlock()->getModule();
1905 LibFunc TheLibFunc;
1906 StringRef Name = getFloatFn(M, TLI, Ty: Op->getType(), DoubleFn, FloatFn,
1907 LongDoubleFn, TheLibFunc);
1908
1909 return emitUnaryFloatFnCallHelper(Op, TheLibFunc, Name, B, Attrs, TLI);
1910}
1911
1912static Value *emitBinaryFloatFnCallHelper(Value *Op1, Value *Op2,
1913 LibFunc TheLibFunc,
1914 StringRef Name, IRBuilderBase &B,
1915 const AttributeList &Attrs,
1916 const TargetLibraryInfo *TLI) {
1917 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
1918
1919 Module *M = B.GetInsertBlock()->getModule();
1920 FunctionCallee Callee = getOrInsertLibFunc(M, TLI: *TLI, TheLibFunc, RetTy: Op1->getType(),
1921 Args: Op1->getType(), Args: Op2->getType());
1922 inferNonMandatoryLibFuncAttrs(M, Name, TLI: *TLI);
1923 CallInst *CI = B.CreateCall(Callee, Args: { Op1, Op2 }, Name);
1924
1925 // The incoming attribute set may have come from a speculatable intrinsic, but
1926 // is being replaced with a library call which is not allowed to be
1927 // speculatable.
1928 CI->setAttributes(
1929 Attrs.removeFnAttribute(C&: B.getContext(), Kind: Attribute::Speculatable));
1930 if (const Function *F =
1931 dyn_cast<Function>(Val: Callee.getCallee()->stripPointerCasts()))
1932 CI->setCallingConv(F->getCallingConv());
1933
1934 return CI;
1935}
1936
1937Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2,
1938 const TargetLibraryInfo *TLI,
1939 StringRef Name, IRBuilderBase &B,
1940 const AttributeList &Attrs) {
1941 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
1942
1943 SmallString<20> NameBuffer;
1944 appendTypeSuffix(Op: Op1, Name, NameBuffer);
1945
1946 LibFunc TheLibFunc;
1947 TLI->getLibFunc(funcName: Name, F&: TheLibFunc);
1948
1949 return emitBinaryFloatFnCallHelper(Op1, Op2, TheLibFunc, Name, B, Attrs, TLI);
1950}
1951
1952Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2,
1953 const TargetLibraryInfo *TLI,
1954 LibFunc DoubleFn, LibFunc FloatFn,
1955 LibFunc LongDoubleFn, IRBuilderBase &B,
1956 const AttributeList &Attrs) {
1957 // Get the name of the function according to TLI.
1958 Module *M = B.GetInsertBlock()->getModule();
1959 LibFunc TheLibFunc;
1960 StringRef Name = getFloatFn(M, TLI, Ty: Op1->getType(), DoubleFn, FloatFn,
1961 LongDoubleFn, TheLibFunc);
1962
1963 return emitBinaryFloatFnCallHelper(Op1, Op2, TheLibFunc, Name, B, Attrs, TLI);
1964}
1965
1966// Emit a call to putchar(int) with Char as the argument. Char must have
1967// the same precision as int, which need not be 32 bits.
1968Value *llvm::emitPutChar(Value *Char, IRBuilderBase &B,
1969 const TargetLibraryInfo *TLI) {
1970 Module *M = B.GetInsertBlock()->getModule();
1971 if (!isLibFuncEmittable(M, TLI, TheLibFunc: LibFunc_putchar))
1972 return nullptr;
1973
1974 Type *IntTy = getIntTy(B, TLI);
1975 StringRef PutCharName = TLI->getName(F: LibFunc_putchar);
1976 FunctionCallee PutChar = getOrInsertLibFunc(M, TLI: *TLI, TheLibFunc: LibFunc_putchar,
1977 RetTy: IntTy, Args: IntTy);
1978 inferNonMandatoryLibFuncAttrs(M, Name: PutCharName, TLI: *TLI);
1979 CallInst *CI = B.CreateCall(Callee: PutChar, Args: Char, Name: PutCharName);
1980
1981 if (const Function *F =
1982 dyn_cast<Function>(Val: PutChar.getCallee()->stripPointerCasts()))
1983 CI->setCallingConv(F->getCallingConv());
1984 return CI;
1985}
1986
1987Value *llvm::emitPutS(Value *Str, IRBuilderBase &B,
1988 const TargetLibraryInfo *TLI) {
1989 Module *M = B.GetInsertBlock()->getModule();
1990 if (!isLibFuncEmittable(M, TLI, TheLibFunc: LibFunc_puts))
1991 return nullptr;
1992
1993 Type *IntTy = getIntTy(B, TLI);
1994 StringRef PutsName = TLI->getName(F: LibFunc_puts);
1995 FunctionCallee PutS =
1996 getOrInsertLibFunc(M, TLI: *TLI, TheLibFunc: LibFunc_puts, RetTy: IntTy, Args: B.getPtrTy());
1997 inferNonMandatoryLibFuncAttrs(M, Name: PutsName, TLI: *TLI);
1998 CallInst *CI = B.CreateCall(Callee: PutS, Args: Str, Name: PutsName);
1999 if (const Function *F =
2000 dyn_cast<Function>(Val: PutS.getCallee()->stripPointerCasts()))
2001 CI->setCallingConv(F->getCallingConv());
2002 return CI;
2003}
2004
2005Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilderBase &B,
2006 const TargetLibraryInfo *TLI) {
2007 Module *M = B.GetInsertBlock()->getModule();
2008 if (!isLibFuncEmittable(M, TLI, TheLibFunc: LibFunc_fputc))
2009 return nullptr;
2010
2011 Type *IntTy = getIntTy(B, TLI);
2012 StringRef FPutcName = TLI->getName(F: LibFunc_fputc);
2013 FunctionCallee F = getOrInsertLibFunc(M, TLI: *TLI, TheLibFunc: LibFunc_fputc, RetTy: IntTy,
2014 Args: IntTy, Args: File->getType());
2015 if (File->getType()->isPointerTy())
2016 inferNonMandatoryLibFuncAttrs(M, Name: FPutcName, TLI: *TLI);
2017 CallInst *CI = B.CreateCall(Callee: F, Args: {Char, File}, Name: FPutcName);
2018
2019 if (const Function *Fn =
2020 dyn_cast<Function>(Val: F.getCallee()->stripPointerCasts()))
2021 CI->setCallingConv(Fn->getCallingConv());
2022 return CI;
2023}
2024
2025Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilderBase &B,
2026 const TargetLibraryInfo *TLI) {
2027 Module *M = B.GetInsertBlock()->getModule();
2028 if (!isLibFuncEmittable(M, TLI, TheLibFunc: LibFunc_fputs))
2029 return nullptr;
2030
2031 Type *IntTy = getIntTy(B, TLI);
2032 StringRef FPutsName = TLI->getName(F: LibFunc_fputs);
2033 FunctionCallee F = getOrInsertLibFunc(M, TLI: *TLI, TheLibFunc: LibFunc_fputs, RetTy: IntTy,
2034 Args: B.getPtrTy(), Args: File->getType());
2035 if (File->getType()->isPointerTy())
2036 inferNonMandatoryLibFuncAttrs(M, Name: FPutsName, TLI: *TLI);
2037 CallInst *CI = B.CreateCall(Callee: F, Args: {Str, File}, Name: FPutsName);
2038
2039 if (const Function *Fn =
2040 dyn_cast<Function>(Val: F.getCallee()->stripPointerCasts()))
2041 CI->setCallingConv(Fn->getCallingConv());
2042 return CI;
2043}
2044
2045Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B,
2046 const DataLayout &DL, const TargetLibraryInfo *TLI) {
2047 Module *M = B.GetInsertBlock()->getModule();
2048 if (!isLibFuncEmittable(M, TLI, TheLibFunc: LibFunc_fwrite))
2049 return nullptr;
2050
2051 Type *SizeTTy = getSizeTTy(B, TLI);
2052 StringRef FWriteName = TLI->getName(F: LibFunc_fwrite);
2053 FunctionCallee F =
2054 getOrInsertLibFunc(M, TLI: *TLI, TheLibFunc: LibFunc_fwrite, RetTy: SizeTTy, Args: B.getPtrTy(),
2055 Args: SizeTTy, Args: SizeTTy, Args: File->getType());
2056
2057 if (File->getType()->isPointerTy())
2058 inferNonMandatoryLibFuncAttrs(M, Name: FWriteName, TLI: *TLI);
2059 CallInst *CI =
2060 B.CreateCall(Callee: F, Args: {Ptr, Size,
2061 ConstantInt::get(Ty: SizeTTy, V: 1), File});
2062
2063 if (const Function *Fn =
2064 dyn_cast<Function>(Val: F.getCallee()->stripPointerCasts()))
2065 CI->setCallingConv(Fn->getCallingConv());
2066 return CI;
2067}
2068
2069Value *llvm::emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL,
2070 const TargetLibraryInfo *TLI) {
2071 Module *M = B.GetInsertBlock()->getModule();
2072 if (!isLibFuncEmittable(M, TLI, TheLibFunc: LibFunc_malloc))
2073 return nullptr;
2074
2075 StringRef MallocName = TLI->getName(F: LibFunc_malloc);
2076 Type *SizeTTy = getSizeTTy(B, TLI);
2077 FunctionCallee Malloc =
2078 getOrInsertLibFunc(M, TLI: *TLI, TheLibFunc: LibFunc_malloc, RetTy: B.getPtrTy(), Args: SizeTTy);
2079 inferNonMandatoryLibFuncAttrs(M, Name: MallocName, TLI: *TLI);
2080 CallInst *CI = B.CreateCall(Callee: Malloc, Args: Num, Name: MallocName);
2081
2082 if (const Function *F =
2083 dyn_cast<Function>(Val: Malloc.getCallee()->stripPointerCasts()))
2084 CI->setCallingConv(F->getCallingConv());
2085
2086 return CI;
2087}
2088
2089Value *llvm::emitCalloc(Value *Num, Value *Size, IRBuilderBase &B,
2090 const TargetLibraryInfo &TLI, unsigned AddrSpace) {
2091 Module *M = B.GetInsertBlock()->getModule();
2092 if (!isLibFuncEmittable(M, TLI: &TLI, TheLibFunc: LibFunc_calloc))
2093 return nullptr;
2094
2095 StringRef CallocName = TLI.getName(F: LibFunc_calloc);
2096 Type *SizeTTy = getSizeTTy(B, TLI: &TLI);
2097 FunctionCallee Calloc = getOrInsertLibFunc(
2098 M, TLI, TheLibFunc: LibFunc_calloc, RetTy: B.getPtrTy(AddrSpace), Args: SizeTTy, Args: SizeTTy);
2099 inferNonMandatoryLibFuncAttrs(M, Name: CallocName, TLI);
2100 CallInst *CI = B.CreateCall(Callee: Calloc, Args: {Num, Size}, Name: CallocName);
2101
2102 if (const auto *F =
2103 dyn_cast<Function>(Val: Calloc.getCallee()->stripPointerCasts()))
2104 CI->setCallingConv(F->getCallingConv());
2105
2106 return CI;
2107}
2108
2109Value *llvm::emitHotColdSizeReturningNew(Value *Num, IRBuilderBase &B,
2110 const TargetLibraryInfo *TLI,
2111 LibFunc SizeFeedbackNewFunc,
2112 uint8_t HotCold) {
2113 Module *M = B.GetInsertBlock()->getModule();
2114 if (!isLibFuncEmittable(M, TLI, TheLibFunc: SizeFeedbackNewFunc))
2115 return nullptr;
2116
2117 StringRef Name = TLI->getName(F: SizeFeedbackNewFunc);
2118
2119 // __sized_ptr_t struct return type { void*, size_t }
2120 StructType *SizedPtrT =
2121 StructType::get(Context&: M->getContext(), Elements: {B.getPtrTy(), Num->getType()});
2122 FunctionCallee Func =
2123 M->getOrInsertFunction(Name, RetTy: SizedPtrT, Args: Num->getType(), Args: B.getInt8Ty());
2124 inferNonMandatoryLibFuncAttrs(M, Name, TLI: *TLI);
2125 CallInst *CI = B.CreateCall(Callee: Func, Args: {Num, B.getInt8(C: HotCold)}, Name: "sized_ptr");
2126
2127 if (const Function *F = dyn_cast<Function>(Val: Func.getCallee()))
2128 CI->setCallingConv(F->getCallingConv());
2129
2130 return CI;
2131}
2132
2133Value *llvm::emitHotColdSizeReturningNewAligned(Value *Num, Value *Align,
2134 IRBuilderBase &B,
2135 const TargetLibraryInfo *TLI,
2136 LibFunc SizeFeedbackNewFunc,
2137 uint8_t HotCold) {
2138 Module *M = B.GetInsertBlock()->getModule();
2139 if (!isLibFuncEmittable(M, TLI, TheLibFunc: SizeFeedbackNewFunc))
2140 return nullptr;
2141
2142 StringRef Name = TLI->getName(F: SizeFeedbackNewFunc);
2143
2144 // __sized_ptr_t struct return type { void*, size_t }
2145 StructType *SizedPtrT =
2146 StructType::get(Context&: M->getContext(), Elements: {B.getPtrTy(), Num->getType()});
2147 FunctionCallee Func = M->getOrInsertFunction(Name, RetTy: SizedPtrT, Args: Num->getType(),
2148 Args: Align->getType(), Args: B.getInt8Ty());
2149 inferNonMandatoryLibFuncAttrs(M, Name, TLI: *TLI);
2150 CallInst *CI =
2151 B.CreateCall(Callee: Func, Args: {Num, Align, B.getInt8(C: HotCold)}, Name: "sized_ptr");
2152
2153 if (const Function *F = dyn_cast<Function>(Val: Func.getCallee()))
2154 CI->setCallingConv(F->getCallingConv());
2155
2156 return CI;
2157}
2158
2159Value *llvm::emitHotColdNew(Value *Num, IRBuilderBase &B,
2160 const TargetLibraryInfo *TLI, LibFunc NewFunc,
2161 uint8_t HotCold) {
2162 Module *M = B.GetInsertBlock()->getModule();
2163 if (!isLibFuncEmittable(M, TLI, TheLibFunc: NewFunc))
2164 return nullptr;
2165
2166 StringRef Name = TLI->getName(F: NewFunc);
2167 FunctionCallee Func =
2168 M->getOrInsertFunction(Name, RetTy: B.getPtrTy(), Args: Num->getType(), Args: B.getInt8Ty());
2169 inferNonMandatoryLibFuncAttrs(M, Name, TLI: *TLI);
2170 CallInst *CI = B.CreateCall(Callee: Func, Args: {Num, B.getInt8(C: HotCold)}, Name);
2171
2172 if (const Function *F =
2173 dyn_cast<Function>(Val: Func.getCallee()->stripPointerCasts()))
2174 CI->setCallingConv(F->getCallingConv());
2175
2176 return CI;
2177}
2178
2179Value *llvm::emitHotColdNewNoThrow(Value *Num, Value *NoThrow, IRBuilderBase &B,
2180 const TargetLibraryInfo *TLI,
2181 LibFunc NewFunc, uint8_t HotCold) {
2182 Module *M = B.GetInsertBlock()->getModule();
2183 if (!isLibFuncEmittable(M, TLI, TheLibFunc: NewFunc))
2184 return nullptr;
2185
2186 StringRef Name = TLI->getName(F: NewFunc);
2187 FunctionCallee Func = M->getOrInsertFunction(
2188 Name, RetTy: B.getPtrTy(), Args: Num->getType(), Args: NoThrow->getType(), Args: B.getInt8Ty());
2189 inferNonMandatoryLibFuncAttrs(M, Name, TLI: *TLI);
2190 CallInst *CI = B.CreateCall(Callee: Func, Args: {Num, NoThrow, B.getInt8(C: HotCold)}, Name);
2191
2192 if (const Function *F =
2193 dyn_cast<Function>(Val: Func.getCallee()->stripPointerCasts()))
2194 CI->setCallingConv(F->getCallingConv());
2195
2196 return CI;
2197}
2198
2199Value *llvm::emitHotColdNewAligned(Value *Num, Value *Align, IRBuilderBase &B,
2200 const TargetLibraryInfo *TLI,
2201 LibFunc NewFunc, uint8_t HotCold) {
2202 Module *M = B.GetInsertBlock()->getModule();
2203 if (!isLibFuncEmittable(M, TLI, TheLibFunc: NewFunc))
2204 return nullptr;
2205
2206 StringRef Name = TLI->getName(F: NewFunc);
2207 FunctionCallee Func = M->getOrInsertFunction(
2208 Name, RetTy: B.getPtrTy(), Args: Num->getType(), Args: Align->getType(), Args: B.getInt8Ty());
2209 inferNonMandatoryLibFuncAttrs(M, Name, TLI: *TLI);
2210 CallInst *CI = B.CreateCall(Callee: Func, Args: {Num, Align, B.getInt8(C: HotCold)}, Name);
2211
2212 if (const Function *F =
2213 dyn_cast<Function>(Val: Func.getCallee()->stripPointerCasts()))
2214 CI->setCallingConv(F->getCallingConv());
2215
2216 return CI;
2217}
2218
2219Value *llvm::emitHotColdNewAlignedNoThrow(Value *Num, Value *Align,
2220 Value *NoThrow, IRBuilderBase &B,
2221 const TargetLibraryInfo *TLI,
2222 LibFunc NewFunc, uint8_t HotCold) {
2223 Module *M = B.GetInsertBlock()->getModule();
2224 if (!isLibFuncEmittable(M, TLI, TheLibFunc: NewFunc))
2225 return nullptr;
2226
2227 StringRef Name = TLI->getName(F: NewFunc);
2228 FunctionCallee Func = M->getOrInsertFunction(
2229 Name, RetTy: B.getPtrTy(), Args: Num->getType(), Args: Align->getType(), Args: NoThrow->getType(),
2230 Args: B.getInt8Ty());
2231 inferNonMandatoryLibFuncAttrs(M, Name, TLI: *TLI);
2232 CallInst *CI =
2233 B.CreateCall(Callee: Func, Args: {Num, Align, NoThrow, B.getInt8(C: HotCold)}, Name);
2234
2235 if (const Function *F =
2236 dyn_cast<Function>(Val: Func.getCallee()->stripPointerCasts()))
2237 CI->setCallingConv(F->getCallingConv());
2238
2239 return CI;
2240}
2241