[InstCombine] Signed saturation patterns
[llvm-core.git] / lib / Transforms / Utils / BuildLibCalls.cpp
blob71316ce8f7583981cc37a2d28a28cffa886016c4
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.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Transforms/Utils/BuildLibCalls.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/Analysis/TargetLibraryInfo.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Type.h"
25 #include "llvm/Analysis/MemoryBuiltins.h"
27 using namespace llvm;
29 #define DEBUG_TYPE "build-libcalls"
31 //- Infer Attributes ---------------------------------------------------------//
33 STATISTIC(NumReadNone, "Number of functions inferred as readnone");
34 STATISTIC(NumReadOnly, "Number of functions inferred as readonly");
35 STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly");
36 STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind");
37 STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture");
38 STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly");
39 STATISTIC(NumNoAlias, "Number of function returns inferred as noalias");
40 STATISTIC(NumNonNull, "Number of function returns inferred as nonnull returns");
41 STATISTIC(NumReturnedArg, "Number of arguments inferred as returned");
43 static bool setDoesNotAccessMemory(Function &F) {
44 if (F.doesNotAccessMemory())
45 return false;
46 F.setDoesNotAccessMemory();
47 ++NumReadNone;
48 return true;
51 static bool setOnlyReadsMemory(Function &F) {
52 if (F.onlyReadsMemory())
53 return false;
54 F.setOnlyReadsMemory();
55 ++NumReadOnly;
56 return true;
59 static bool setOnlyAccessesArgMemory(Function &F) {
60 if (F.onlyAccessesArgMemory())
61 return false;
62 F.setOnlyAccessesArgMemory();
63 ++NumArgMemOnly;
64 return true;
67 static bool setDoesNotThrow(Function &F) {
68 if (F.doesNotThrow())
69 return false;
70 F.setDoesNotThrow();
71 ++NumNoUnwind;
72 return true;
75 static bool setRetDoesNotAlias(Function &F) {
76 if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias))
77 return false;
78 F.addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
79 ++NumNoAlias;
80 return true;
83 static bool setDoesNotCapture(Function &F, unsigned ArgNo) {
84 if (F.hasParamAttribute(ArgNo, Attribute::NoCapture))
85 return false;
86 F.addParamAttr(ArgNo, Attribute::NoCapture);
87 ++NumNoCapture;
88 return true;
91 static bool setDoesNotAlias(Function &F, unsigned ArgNo) {
92 if (F.hasParamAttribute(ArgNo, Attribute::NoAlias))
93 return false;
94 F.addParamAttr(ArgNo, Attribute::NoAlias);
95 ++NumNoAlias;
96 return true;
99 static bool setOnlyReadsMemory(Function &F, unsigned ArgNo) {
100 if (F.hasParamAttribute(ArgNo, Attribute::ReadOnly))
101 return false;
102 F.addParamAttr(ArgNo, Attribute::ReadOnly);
103 ++NumReadOnlyArg;
104 return true;
107 static bool setRetNonNull(Function &F) {
108 assert(F.getReturnType()->isPointerTy() &&
109 "nonnull applies only to pointers");
110 if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NonNull))
111 return false;
112 F.addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
113 ++NumNonNull;
114 return true;
117 static bool setReturnedArg(Function &F, unsigned ArgNo) {
118 if (F.hasParamAttribute(ArgNo, Attribute::Returned))
119 return false;
120 F.addParamAttr(ArgNo, Attribute::Returned);
121 ++NumReturnedArg;
122 return true;
125 static bool setNonLazyBind(Function &F) {
126 if (F.hasFnAttribute(Attribute::NonLazyBind))
127 return false;
128 F.addFnAttr(Attribute::NonLazyBind);
129 return true;
132 static bool setDoesNotFreeMemory(Function &F) {
133 if (F.hasFnAttribute(Attribute::NoFree))
134 return false;
135 F.addFnAttr(Attribute::NoFree);
136 return true;
139 bool llvm::inferLibFuncAttributes(Module *M, StringRef Name,
140 const TargetLibraryInfo &TLI) {
141 Function *F = M->getFunction(Name);
142 if (!F)
143 return false;
144 return inferLibFuncAttributes(*F, TLI);
147 bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
148 LibFunc TheLibFunc;
149 if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc)))
150 return false;
152 bool Changed = false;
154 if(!isLibFreeFunction(&F, TheLibFunc) && !isReallocLikeFn(&F, &TLI))
155 Changed |= setDoesNotFreeMemory(F);
157 if (F.getParent() != nullptr && F.getParent()->getRtLibUseGOT())
158 Changed |= setNonLazyBind(F);
160 switch (TheLibFunc) {
161 case LibFunc_strlen:
162 case LibFunc_wcslen:
163 Changed |= setOnlyReadsMemory(F);
164 Changed |= setDoesNotThrow(F);
165 Changed |= setOnlyAccessesArgMemory(F);
166 Changed |= setDoesNotCapture(F, 0);
167 return Changed;
168 case LibFunc_strchr:
169 case LibFunc_strrchr:
170 Changed |= setOnlyReadsMemory(F);
171 Changed |= setDoesNotThrow(F);
172 return Changed;
173 case LibFunc_strtol:
174 case LibFunc_strtod:
175 case LibFunc_strtof:
176 case LibFunc_strtoul:
177 case LibFunc_strtoll:
178 case LibFunc_strtold:
179 case LibFunc_strtoull:
180 Changed |= setDoesNotThrow(F);
181 Changed |= setDoesNotCapture(F, 1);
182 Changed |= setOnlyReadsMemory(F, 0);
183 return Changed;
184 case LibFunc_strcpy:
185 case LibFunc_strncpy:
186 Changed |= setDoesNotAlias(F, 0);
187 Changed |= setDoesNotAlias(F, 1);
188 LLVM_FALLTHROUGH;
189 case LibFunc_strcat:
190 case LibFunc_strncat:
191 Changed |= setReturnedArg(F, 0);
192 LLVM_FALLTHROUGH;
193 case LibFunc_stpcpy:
194 case LibFunc_stpncpy:
195 Changed |= setDoesNotThrow(F);
196 Changed |= setDoesNotCapture(F, 1);
197 Changed |= setOnlyReadsMemory(F, 1);
198 return Changed;
199 case LibFunc_strxfrm:
200 Changed |= setDoesNotThrow(F);
201 Changed |= setDoesNotCapture(F, 0);
202 Changed |= setDoesNotCapture(F, 1);
203 Changed |= setOnlyReadsMemory(F, 1);
204 return Changed;
205 case LibFunc_strcmp: // 0,1
206 case LibFunc_strspn: // 0,1
207 case LibFunc_strncmp: // 0,1
208 case LibFunc_strcspn: // 0,1
209 case LibFunc_strcoll: // 0,1
210 case LibFunc_strcasecmp: // 0,1
211 case LibFunc_strncasecmp: //
212 Changed |= setOnlyReadsMemory(F);
213 Changed |= setDoesNotThrow(F);
214 Changed |= setDoesNotCapture(F, 0);
215 Changed |= setDoesNotCapture(F, 1);
216 return Changed;
217 case LibFunc_strstr:
218 case LibFunc_strpbrk:
219 Changed |= setOnlyReadsMemory(F);
220 Changed |= setDoesNotThrow(F);
221 Changed |= setDoesNotCapture(F, 1);
222 return Changed;
223 case LibFunc_strtok:
224 case LibFunc_strtok_r:
225 Changed |= setDoesNotThrow(F);
226 Changed |= setDoesNotCapture(F, 1);
227 Changed |= setOnlyReadsMemory(F, 1);
228 return Changed;
229 case LibFunc_scanf:
230 Changed |= setDoesNotThrow(F);
231 Changed |= setDoesNotCapture(F, 0);
232 Changed |= setOnlyReadsMemory(F, 0);
233 return Changed;
234 case LibFunc_setbuf:
235 case LibFunc_setvbuf:
236 Changed |= setDoesNotThrow(F);
237 Changed |= setDoesNotCapture(F, 0);
238 return Changed;
239 case LibFunc_strdup:
240 case LibFunc_strndup:
241 Changed |= setDoesNotThrow(F);
242 Changed |= setRetDoesNotAlias(F);
243 Changed |= setDoesNotCapture(F, 0);
244 Changed |= setOnlyReadsMemory(F, 0);
245 return Changed;
246 case LibFunc_stat:
247 case LibFunc_statvfs:
248 Changed |= setDoesNotThrow(F);
249 Changed |= setDoesNotCapture(F, 0);
250 Changed |= setDoesNotCapture(F, 1);
251 Changed |= setOnlyReadsMemory(F, 0);
252 return Changed;
253 case LibFunc_sscanf:
254 Changed |= setDoesNotThrow(F);
255 Changed |= setDoesNotCapture(F, 0);
256 Changed |= setDoesNotCapture(F, 1);
257 Changed |= setOnlyReadsMemory(F, 0);
258 Changed |= setOnlyReadsMemory(F, 1);
259 return Changed;
260 case LibFunc_sprintf:
261 Changed |= setDoesNotThrow(F);
262 Changed |= setDoesNotCapture(F, 0);
263 Changed |= setDoesNotAlias(F, 0);
264 Changed |= setDoesNotCapture(F, 1);
265 Changed |= setOnlyReadsMemory(F, 1);
266 return Changed;
267 case LibFunc_snprintf:
268 Changed |= setDoesNotThrow(F);
269 Changed |= setDoesNotCapture(F, 0);
270 Changed |= setDoesNotAlias(F, 0);
271 Changed |= setDoesNotCapture(F, 2);
272 Changed |= setOnlyReadsMemory(F, 2);
273 return Changed;
274 case LibFunc_setitimer:
275 Changed |= setDoesNotThrow(F);
276 Changed |= setDoesNotCapture(F, 1);
277 Changed |= setDoesNotCapture(F, 2);
278 Changed |= setOnlyReadsMemory(F, 1);
279 return Changed;
280 case LibFunc_system:
281 // May throw; "system" is a valid pthread cancellation point.
282 Changed |= setDoesNotCapture(F, 0);
283 Changed |= setOnlyReadsMemory(F, 0);
284 return Changed;
285 case LibFunc_malloc:
286 Changed |= setDoesNotThrow(F);
287 Changed |= setRetDoesNotAlias(F);
288 return Changed;
289 case LibFunc_memcmp:
290 Changed |= setOnlyReadsMemory(F);
291 Changed |= setDoesNotThrow(F);
292 Changed |= setDoesNotCapture(F, 0);
293 Changed |= setDoesNotCapture(F, 1);
294 return Changed;
295 case LibFunc_memchr:
296 case LibFunc_memrchr:
297 Changed |= setOnlyReadsMemory(F);
298 Changed |= setDoesNotThrow(F);
299 return Changed;
300 case LibFunc_modf:
301 case LibFunc_modff:
302 case LibFunc_modfl:
303 Changed |= setDoesNotThrow(F);
304 Changed |= setDoesNotCapture(F, 1);
305 return Changed;
306 case LibFunc_memcpy:
307 Changed |= setDoesNotAlias(F, 0);
308 Changed |= setDoesNotAlias(F, 1);
309 Changed |= setReturnedArg(F, 0);
310 Changed |= setDoesNotThrow(F);
311 Changed |= setDoesNotCapture(F, 1);
312 Changed |= setOnlyReadsMemory(F, 1);
313 return Changed;
314 case LibFunc_memmove:
315 Changed |= setReturnedArg(F, 0);
316 Changed |= setDoesNotThrow(F);
317 Changed |= setDoesNotCapture(F, 1);
318 Changed |= setOnlyReadsMemory(F, 1);
319 return Changed;
320 case LibFunc_mempcpy:
321 case LibFunc_memccpy:
322 Changed |= setDoesNotAlias(F, 0);
323 Changed |= setDoesNotAlias(F, 1);
324 Changed |= setDoesNotThrow(F);
325 Changed |= setDoesNotCapture(F, 1);
326 Changed |= setOnlyReadsMemory(F, 1);
327 return Changed;
328 case LibFunc_memcpy_chk:
329 Changed |= setDoesNotThrow(F);
330 return Changed;
331 case LibFunc_memalign:
332 Changed |= setRetDoesNotAlias(F);
333 return Changed;
334 case LibFunc_mkdir:
335 Changed |= setDoesNotThrow(F);
336 Changed |= setDoesNotCapture(F, 0);
337 Changed |= setOnlyReadsMemory(F, 0);
338 return Changed;
339 case LibFunc_mktime:
340 Changed |= setDoesNotThrow(F);
341 Changed |= setDoesNotCapture(F, 0);
342 return Changed;
343 case LibFunc_realloc:
344 Changed |= setDoesNotThrow(F);
345 Changed |= setRetDoesNotAlias(F);
346 Changed |= setDoesNotCapture(F, 0);
347 return Changed;
348 case LibFunc_read:
349 // May throw; "read" is a valid pthread cancellation point.
350 Changed |= setDoesNotCapture(F, 1);
351 return Changed;
352 case LibFunc_rewind:
353 Changed |= setDoesNotThrow(F);
354 Changed |= setDoesNotCapture(F, 0);
355 return Changed;
356 case LibFunc_rmdir:
357 case LibFunc_remove:
358 case LibFunc_realpath:
359 Changed |= setDoesNotThrow(F);
360 Changed |= setDoesNotCapture(F, 0);
361 Changed |= setOnlyReadsMemory(F, 0);
362 return Changed;
363 case LibFunc_rename:
364 Changed |= setDoesNotThrow(F);
365 Changed |= setDoesNotCapture(F, 0);
366 Changed |= setDoesNotCapture(F, 1);
367 Changed |= setOnlyReadsMemory(F, 0);
368 Changed |= setOnlyReadsMemory(F, 1);
369 return Changed;
370 case LibFunc_readlink:
371 Changed |= setDoesNotThrow(F);
372 Changed |= setDoesNotCapture(F, 0);
373 Changed |= setDoesNotCapture(F, 1);
374 Changed |= setOnlyReadsMemory(F, 0);
375 return Changed;
376 case LibFunc_write:
377 // May throw; "write" is a valid pthread cancellation point.
378 Changed |= setDoesNotCapture(F, 1);
379 Changed |= setOnlyReadsMemory(F, 1);
380 return Changed;
381 case LibFunc_bcopy:
382 Changed |= setDoesNotThrow(F);
383 Changed |= setDoesNotCapture(F, 0);
384 Changed |= setDoesNotCapture(F, 1);
385 Changed |= setOnlyReadsMemory(F, 0);
386 return Changed;
387 case LibFunc_bcmp:
388 Changed |= setDoesNotThrow(F);
389 Changed |= setOnlyReadsMemory(F);
390 Changed |= setDoesNotCapture(F, 0);
391 Changed |= setDoesNotCapture(F, 1);
392 return Changed;
393 case LibFunc_bzero:
394 Changed |= setDoesNotThrow(F);
395 Changed |= setDoesNotCapture(F, 0);
396 return Changed;
397 case LibFunc_calloc:
398 Changed |= setDoesNotThrow(F);
399 Changed |= setRetDoesNotAlias(F);
400 return Changed;
401 case LibFunc_chmod:
402 case LibFunc_chown:
403 Changed |= setDoesNotThrow(F);
404 Changed |= setDoesNotCapture(F, 0);
405 Changed |= setOnlyReadsMemory(F, 0);
406 return Changed;
407 case LibFunc_ctermid:
408 case LibFunc_clearerr:
409 case LibFunc_closedir:
410 Changed |= setDoesNotThrow(F);
411 Changed |= setDoesNotCapture(F, 0);
412 return Changed;
413 case LibFunc_atoi:
414 case LibFunc_atol:
415 case LibFunc_atof:
416 case LibFunc_atoll:
417 Changed |= setDoesNotThrow(F);
418 Changed |= setOnlyReadsMemory(F);
419 Changed |= setDoesNotCapture(F, 0);
420 return Changed;
421 case LibFunc_access:
422 Changed |= setDoesNotThrow(F);
423 Changed |= setDoesNotCapture(F, 0);
424 Changed |= setOnlyReadsMemory(F, 0);
425 return Changed;
426 case LibFunc_fopen:
427 Changed |= setDoesNotThrow(F);
428 Changed |= setRetDoesNotAlias(F);
429 Changed |= setDoesNotCapture(F, 0);
430 Changed |= setDoesNotCapture(F, 1);
431 Changed |= setOnlyReadsMemory(F, 0);
432 Changed |= setOnlyReadsMemory(F, 1);
433 return Changed;
434 case LibFunc_fdopen:
435 Changed |= setDoesNotThrow(F);
436 Changed |= setRetDoesNotAlias(F);
437 Changed |= setDoesNotCapture(F, 1);
438 Changed |= setOnlyReadsMemory(F, 1);
439 return Changed;
440 case LibFunc_feof:
441 case LibFunc_free:
442 case LibFunc_fseek:
443 case LibFunc_ftell:
444 case LibFunc_fgetc:
445 case LibFunc_fgetc_unlocked:
446 case LibFunc_fseeko:
447 case LibFunc_ftello:
448 case LibFunc_fileno:
449 case LibFunc_fflush:
450 case LibFunc_fclose:
451 case LibFunc_fsetpos:
452 case LibFunc_flockfile:
453 case LibFunc_funlockfile:
454 case LibFunc_ftrylockfile:
455 Changed |= setDoesNotThrow(F);
456 Changed |= setDoesNotCapture(F, 0);
457 return Changed;
458 case LibFunc_ferror:
459 Changed |= setDoesNotThrow(F);
460 Changed |= setDoesNotCapture(F, 0);
461 Changed |= setOnlyReadsMemory(F);
462 return Changed;
463 case LibFunc_fputc:
464 case LibFunc_fputc_unlocked:
465 case LibFunc_fstat:
466 case LibFunc_frexp:
467 case LibFunc_frexpf:
468 case LibFunc_frexpl:
469 case LibFunc_fstatvfs:
470 Changed |= setDoesNotThrow(F);
471 Changed |= setDoesNotCapture(F, 1);
472 return Changed;
473 case LibFunc_fgets:
474 case LibFunc_fgets_unlocked:
475 Changed |= setDoesNotThrow(F);
476 Changed |= setDoesNotCapture(F, 2);
477 return Changed;
478 case LibFunc_fread:
479 case LibFunc_fread_unlocked:
480 Changed |= setDoesNotThrow(F);
481 Changed |= setDoesNotCapture(F, 0);
482 Changed |= setDoesNotCapture(F, 3);
483 return Changed;
484 case LibFunc_fwrite:
485 case LibFunc_fwrite_unlocked:
486 Changed |= setDoesNotThrow(F);
487 Changed |= setDoesNotCapture(F, 0);
488 Changed |= setDoesNotCapture(F, 3);
489 // FIXME: readonly #1?
490 return Changed;
491 case LibFunc_fputs:
492 case LibFunc_fputs_unlocked:
493 Changed |= setDoesNotThrow(F);
494 Changed |= setDoesNotCapture(F, 0);
495 Changed |= setDoesNotCapture(F, 1);
496 Changed |= setOnlyReadsMemory(F, 0);
497 return Changed;
498 case LibFunc_fscanf:
499 case LibFunc_fprintf:
500 Changed |= setDoesNotThrow(F);
501 Changed |= setDoesNotCapture(F, 0);
502 Changed |= setDoesNotCapture(F, 1);
503 Changed |= setOnlyReadsMemory(F, 1);
504 return Changed;
505 case LibFunc_fgetpos:
506 Changed |= setDoesNotThrow(F);
507 Changed |= setDoesNotCapture(F, 0);
508 Changed |= setDoesNotCapture(F, 1);
509 return Changed;
510 case LibFunc_getc:
511 case LibFunc_getlogin_r:
512 case LibFunc_getc_unlocked:
513 Changed |= setDoesNotThrow(F);
514 Changed |= setDoesNotCapture(F, 0);
515 return Changed;
516 case LibFunc_getenv:
517 Changed |= setDoesNotThrow(F);
518 Changed |= setOnlyReadsMemory(F);
519 Changed |= setDoesNotCapture(F, 0);
520 return Changed;
521 case LibFunc_gets:
522 case LibFunc_getchar:
523 case LibFunc_getchar_unlocked:
524 Changed |= setDoesNotThrow(F);
525 return Changed;
526 case LibFunc_getitimer:
527 Changed |= setDoesNotThrow(F);
528 Changed |= setDoesNotCapture(F, 1);
529 return Changed;
530 case LibFunc_getpwnam:
531 Changed |= setDoesNotThrow(F);
532 Changed |= setDoesNotCapture(F, 0);
533 Changed |= setOnlyReadsMemory(F, 0);
534 return Changed;
535 case LibFunc_ungetc:
536 Changed |= setDoesNotThrow(F);
537 Changed |= setDoesNotCapture(F, 1);
538 return Changed;
539 case LibFunc_uname:
540 Changed |= setDoesNotThrow(F);
541 Changed |= setDoesNotCapture(F, 0);
542 return Changed;
543 case LibFunc_unlink:
544 Changed |= setDoesNotThrow(F);
545 Changed |= setDoesNotCapture(F, 0);
546 Changed |= setOnlyReadsMemory(F, 0);
547 return Changed;
548 case LibFunc_unsetenv:
549 Changed |= setDoesNotThrow(F);
550 Changed |= setDoesNotCapture(F, 0);
551 Changed |= setOnlyReadsMemory(F, 0);
552 return Changed;
553 case LibFunc_utime:
554 case LibFunc_utimes:
555 Changed |= setDoesNotThrow(F);
556 Changed |= setDoesNotCapture(F, 0);
557 Changed |= setDoesNotCapture(F, 1);
558 Changed |= setOnlyReadsMemory(F, 0);
559 Changed |= setOnlyReadsMemory(F, 1);
560 return Changed;
561 case LibFunc_putc:
562 case LibFunc_putc_unlocked:
563 Changed |= setDoesNotThrow(F);
564 Changed |= setDoesNotCapture(F, 1);
565 return Changed;
566 case LibFunc_puts:
567 case LibFunc_printf:
568 case LibFunc_perror:
569 Changed |= setDoesNotThrow(F);
570 Changed |= setDoesNotCapture(F, 0);
571 Changed |= setOnlyReadsMemory(F, 0);
572 return Changed;
573 case LibFunc_pread:
574 // May throw; "pread" is a valid pthread cancellation point.
575 Changed |= setDoesNotCapture(F, 1);
576 return Changed;
577 case LibFunc_pwrite:
578 // May throw; "pwrite" is a valid pthread cancellation point.
579 Changed |= setDoesNotCapture(F, 1);
580 Changed |= setOnlyReadsMemory(F, 1);
581 return Changed;
582 case LibFunc_putchar:
583 case LibFunc_putchar_unlocked:
584 Changed |= setDoesNotThrow(F);
585 return Changed;
586 case LibFunc_popen:
587 Changed |= setDoesNotThrow(F);
588 Changed |= setRetDoesNotAlias(F);
589 Changed |= setDoesNotCapture(F, 0);
590 Changed |= setDoesNotCapture(F, 1);
591 Changed |= setOnlyReadsMemory(F, 0);
592 Changed |= setOnlyReadsMemory(F, 1);
593 return Changed;
594 case LibFunc_pclose:
595 Changed |= setDoesNotThrow(F);
596 Changed |= setDoesNotCapture(F, 0);
597 return Changed;
598 case LibFunc_vscanf:
599 Changed |= setDoesNotThrow(F);
600 Changed |= setDoesNotCapture(F, 0);
601 Changed |= setOnlyReadsMemory(F, 0);
602 return Changed;
603 case LibFunc_vsscanf:
604 Changed |= setDoesNotThrow(F);
605 Changed |= setDoesNotCapture(F, 0);
606 Changed |= setDoesNotCapture(F, 1);
607 Changed |= setOnlyReadsMemory(F, 0);
608 Changed |= setOnlyReadsMemory(F, 1);
609 return Changed;
610 case LibFunc_vfscanf:
611 Changed |= setDoesNotThrow(F);
612 Changed |= setDoesNotCapture(F, 0);
613 Changed |= setDoesNotCapture(F, 1);
614 Changed |= setOnlyReadsMemory(F, 1);
615 return Changed;
616 case LibFunc_valloc:
617 Changed |= setDoesNotThrow(F);
618 Changed |= setRetDoesNotAlias(F);
619 return Changed;
620 case LibFunc_vprintf:
621 Changed |= setDoesNotThrow(F);
622 Changed |= setDoesNotCapture(F, 0);
623 Changed |= setOnlyReadsMemory(F, 0);
624 return Changed;
625 case LibFunc_vfprintf:
626 case LibFunc_vsprintf:
627 Changed |= setDoesNotThrow(F);
628 Changed |= setDoesNotCapture(F, 0);
629 Changed |= setDoesNotCapture(F, 1);
630 Changed |= setOnlyReadsMemory(F, 1);
631 return Changed;
632 case LibFunc_vsnprintf:
633 Changed |= setDoesNotThrow(F);
634 Changed |= setDoesNotCapture(F, 0);
635 Changed |= setDoesNotCapture(F, 2);
636 Changed |= setOnlyReadsMemory(F, 2);
637 return Changed;
638 case LibFunc_open:
639 // May throw; "open" is a valid pthread cancellation point.
640 Changed |= setDoesNotCapture(F, 0);
641 Changed |= setOnlyReadsMemory(F, 0);
642 return Changed;
643 case LibFunc_opendir:
644 Changed |= setDoesNotThrow(F);
645 Changed |= setRetDoesNotAlias(F);
646 Changed |= setDoesNotCapture(F, 0);
647 Changed |= setOnlyReadsMemory(F, 0);
648 return Changed;
649 case LibFunc_tmpfile:
650 Changed |= setDoesNotThrow(F);
651 Changed |= setRetDoesNotAlias(F);
652 return Changed;
653 case LibFunc_times:
654 Changed |= setDoesNotThrow(F);
655 Changed |= setDoesNotCapture(F, 0);
656 return Changed;
657 case LibFunc_htonl:
658 case LibFunc_htons:
659 case LibFunc_ntohl:
660 case LibFunc_ntohs:
661 Changed |= setDoesNotThrow(F);
662 Changed |= setDoesNotAccessMemory(F);
663 return Changed;
664 case LibFunc_lstat:
665 Changed |= setDoesNotThrow(F);
666 Changed |= setDoesNotCapture(F, 0);
667 Changed |= setDoesNotCapture(F, 1);
668 Changed |= setOnlyReadsMemory(F, 0);
669 return Changed;
670 case LibFunc_lchown:
671 Changed |= setDoesNotThrow(F);
672 Changed |= setDoesNotCapture(F, 0);
673 Changed |= setOnlyReadsMemory(F, 0);
674 return Changed;
675 case LibFunc_qsort:
676 // May throw; places call through function pointer.
677 Changed |= setDoesNotCapture(F, 3);
678 return Changed;
679 case LibFunc_dunder_strdup:
680 case LibFunc_dunder_strndup:
681 Changed |= setDoesNotThrow(F);
682 Changed |= setRetDoesNotAlias(F);
683 Changed |= setDoesNotCapture(F, 0);
684 Changed |= setOnlyReadsMemory(F, 0);
685 return Changed;
686 case LibFunc_dunder_strtok_r:
687 Changed |= setDoesNotThrow(F);
688 Changed |= setDoesNotCapture(F, 1);
689 Changed |= setOnlyReadsMemory(F, 1);
690 return Changed;
691 case LibFunc_under_IO_getc:
692 Changed |= setDoesNotThrow(F);
693 Changed |= setDoesNotCapture(F, 0);
694 return Changed;
695 case LibFunc_under_IO_putc:
696 Changed |= setDoesNotThrow(F);
697 Changed |= setDoesNotCapture(F, 1);
698 return Changed;
699 case LibFunc_dunder_isoc99_scanf:
700 Changed |= setDoesNotThrow(F);
701 Changed |= setDoesNotCapture(F, 0);
702 Changed |= setOnlyReadsMemory(F, 0);
703 return Changed;
704 case LibFunc_stat64:
705 case LibFunc_lstat64:
706 case LibFunc_statvfs64:
707 Changed |= setDoesNotThrow(F);
708 Changed |= setDoesNotCapture(F, 0);
709 Changed |= setDoesNotCapture(F, 1);
710 Changed |= setOnlyReadsMemory(F, 0);
711 return Changed;
712 case LibFunc_dunder_isoc99_sscanf:
713 Changed |= setDoesNotThrow(F);
714 Changed |= setDoesNotCapture(F, 0);
715 Changed |= setDoesNotCapture(F, 1);
716 Changed |= setOnlyReadsMemory(F, 0);
717 Changed |= setOnlyReadsMemory(F, 1);
718 return Changed;
719 case LibFunc_fopen64:
720 Changed |= setDoesNotThrow(F);
721 Changed |= setRetDoesNotAlias(F);
722 Changed |= setDoesNotCapture(F, 0);
723 Changed |= setDoesNotCapture(F, 1);
724 Changed |= setOnlyReadsMemory(F, 0);
725 Changed |= setOnlyReadsMemory(F, 1);
726 return Changed;
727 case LibFunc_fseeko64:
728 case LibFunc_ftello64:
729 Changed |= setDoesNotThrow(F);
730 Changed |= setDoesNotCapture(F, 0);
731 return Changed;
732 case LibFunc_tmpfile64:
733 Changed |= setDoesNotThrow(F);
734 Changed |= setRetDoesNotAlias(F);
735 return Changed;
736 case LibFunc_fstat64:
737 case LibFunc_fstatvfs64:
738 Changed |= setDoesNotThrow(F);
739 Changed |= setDoesNotCapture(F, 1);
740 return Changed;
741 case LibFunc_open64:
742 // May throw; "open" is a valid pthread cancellation point.
743 Changed |= setDoesNotCapture(F, 0);
744 Changed |= setOnlyReadsMemory(F, 0);
745 return Changed;
746 case LibFunc_gettimeofday:
747 // Currently some platforms have the restrict keyword on the arguments to
748 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
749 // arguments.
750 Changed |= setDoesNotThrow(F);
751 Changed |= setDoesNotCapture(F, 0);
752 Changed |= setDoesNotCapture(F, 1);
753 return Changed;
754 case LibFunc_Znwj: // new(unsigned int)
755 case LibFunc_Znwm: // new(unsigned long)
756 case LibFunc_Znaj: // new[](unsigned int)
757 case LibFunc_Znam: // new[](unsigned long)
758 case LibFunc_msvc_new_int: // new(unsigned int)
759 case LibFunc_msvc_new_longlong: // new(unsigned long long)
760 case LibFunc_msvc_new_array_int: // new[](unsigned int)
761 case LibFunc_msvc_new_array_longlong: // new[](unsigned long long)
762 // Operator new always returns a nonnull noalias pointer
763 Changed |= setRetNonNull(F);
764 Changed |= setRetDoesNotAlias(F);
765 return Changed;
766 // TODO: add LibFunc entries for:
767 // case LibFunc_memset_pattern4:
768 // case LibFunc_memset_pattern8:
769 case LibFunc_memset_pattern16:
770 Changed |= setOnlyAccessesArgMemory(F);
771 Changed |= setDoesNotCapture(F, 0);
772 Changed |= setDoesNotCapture(F, 1);
773 Changed |= setOnlyReadsMemory(F, 1);
774 return Changed;
775 // int __nvvm_reflect(const char *)
776 case LibFunc_nvvm_reflect:
777 Changed |= setDoesNotAccessMemory(F);
778 Changed |= setDoesNotThrow(F);
779 return Changed;
781 default:
782 // FIXME: It'd be really nice to cover all the library functions we're
783 // aware of here.
784 return false;
788 bool llvm::hasFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
789 LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn) {
790 switch (Ty->getTypeID()) {
791 case Type::HalfTyID:
792 return false;
793 case Type::FloatTyID:
794 return TLI->has(FloatFn);
795 case Type::DoubleTyID:
796 return TLI->has(DoubleFn);
797 default:
798 return TLI->has(LongDoubleFn);
802 StringRef llvm::getFloatFnName(const TargetLibraryInfo *TLI, Type *Ty,
803 LibFunc DoubleFn, LibFunc FloatFn,
804 LibFunc LongDoubleFn) {
805 assert(hasFloatFn(TLI, Ty, DoubleFn, FloatFn, LongDoubleFn) &&
806 "Cannot get name for unavailable function!");
808 switch (Ty->getTypeID()) {
809 case Type::HalfTyID:
810 llvm_unreachable("No name for HalfTy!");
811 case Type::FloatTyID:
812 return TLI->getName(FloatFn);
813 case Type::DoubleTyID:
814 return TLI->getName(DoubleFn);
815 default:
816 return TLI->getName(LongDoubleFn);
820 //- Emit LibCalls ------------------------------------------------------------//
822 Value *llvm::castToCStr(Value *V, IRBuilder<> &B) {
823 unsigned AS = V->getType()->getPointerAddressSpace();
824 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
827 static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType,
828 ArrayRef<Type *> ParamTypes,
829 ArrayRef<Value *> Operands, IRBuilder<> &B,
830 const TargetLibraryInfo *TLI,
831 bool IsVaArgs = false) {
832 if (!TLI->has(TheLibFunc))
833 return nullptr;
835 Module *M = B.GetInsertBlock()->getModule();
836 StringRef FuncName = TLI->getName(TheLibFunc);
837 FunctionType *FuncType = FunctionType::get(ReturnType, ParamTypes, IsVaArgs);
838 FunctionCallee Callee = M->getOrInsertFunction(FuncName, FuncType);
839 inferLibFuncAttributes(M, FuncName, *TLI);
840 CallInst *CI = B.CreateCall(Callee, Operands, FuncName);
841 if (const Function *F =
842 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
843 CI->setCallingConv(F->getCallingConv());
844 return CI;
847 Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
848 const TargetLibraryInfo *TLI) {
849 LLVMContext &Context = B.GetInsertBlock()->getContext();
850 return emitLibCall(LibFunc_strlen, DL.getIntPtrType(Context),
851 B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI);
854 Value *llvm::emitStrDup(Value *Ptr, IRBuilder<> &B,
855 const TargetLibraryInfo *TLI) {
856 return emitLibCall(LibFunc_strdup, B.getInt8PtrTy(), B.getInt8PtrTy(),
857 castToCStr(Ptr, B), B, TLI);
860 Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
861 const TargetLibraryInfo *TLI) {
862 Type *I8Ptr = B.getInt8PtrTy();
863 Type *I32Ty = B.getInt32Ty();
864 return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, I32Ty},
865 {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, B, TLI);
868 Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
869 const DataLayout &DL, const TargetLibraryInfo *TLI) {
870 LLVMContext &Context = B.GetInsertBlock()->getContext();
871 return emitLibCall(
872 LibFunc_strncmp, B.getInt32Ty(),
873 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
874 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
877 Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
878 const TargetLibraryInfo *TLI) {
879 Type *I8Ptr = B.getInt8PtrTy();
880 return emitLibCall(LibFunc_strcpy, I8Ptr, {I8Ptr, I8Ptr},
881 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI);
884 Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B,
885 const TargetLibraryInfo *TLI) {
886 Type *I8Ptr = B.getInt8PtrTy();
887 return emitLibCall(LibFunc_stpcpy, I8Ptr, {I8Ptr, I8Ptr},
888 {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI);
891 Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
892 const TargetLibraryInfo *TLI) {
893 Type *I8Ptr = B.getInt8PtrTy();
894 return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()},
895 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI);
898 Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
899 const TargetLibraryInfo *TLI) {
900 Type *I8Ptr = B.getInt8PtrTy();
901 return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()},
902 {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI);
905 Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
906 IRBuilder<> &B, const DataLayout &DL,
907 const TargetLibraryInfo *TLI) {
908 if (!TLI->has(LibFunc_memcpy_chk))
909 return nullptr;
911 Module *M = B.GetInsertBlock()->getModule();
912 AttributeList AS;
913 AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex,
914 Attribute::NoUnwind);
915 LLVMContext &Context = B.GetInsertBlock()->getContext();
916 FunctionCallee MemCpy = M->getOrInsertFunction(
917 "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
918 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
919 DL.getIntPtrType(Context));
920 Dst = castToCStr(Dst, B);
921 Src = castToCStr(Src, B);
922 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
923 if (const Function *F =
924 dyn_cast<Function>(MemCpy.getCallee()->stripPointerCasts()))
925 CI->setCallingConv(F->getCallingConv());
926 return CI;
929 Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
930 const DataLayout &DL, const TargetLibraryInfo *TLI) {
931 LLVMContext &Context = B.GetInsertBlock()->getContext();
932 return emitLibCall(
933 LibFunc_memchr, B.getInt8PtrTy(),
934 {B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context)},
935 {castToCStr(Ptr, B), Val, Len}, B, TLI);
938 Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
939 const DataLayout &DL, const TargetLibraryInfo *TLI) {
940 LLVMContext &Context = B.GetInsertBlock()->getContext();
941 return emitLibCall(
942 LibFunc_memcmp, B.getInt32Ty(),
943 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
944 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
947 Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
948 const DataLayout &DL, const TargetLibraryInfo *TLI) {
949 LLVMContext &Context = B.GetInsertBlock()->getContext();
950 return emitLibCall(
951 LibFunc_bcmp, B.getInt32Ty(),
952 {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
953 {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
956 Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
957 IRBuilder<> &B, const TargetLibraryInfo *TLI) {
958 return emitLibCall(
959 LibFunc_memccpy, B.getInt8PtrTy(),
960 {B.getInt8PtrTy(), B.getInt8PtrTy(), B.getInt32Ty(), Len->getType()},
961 {Ptr1, Ptr2, Val, Len}, B, TLI);
964 Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
965 ArrayRef<Value *> VariadicArgs, IRBuilder<> &B,
966 const TargetLibraryInfo *TLI) {
967 SmallVector<Value *, 8> Args{castToCStr(Dest, B), Size, castToCStr(Fmt, B)};
968 Args.insert(Args.end(), VariadicArgs.begin(), VariadicArgs.end());
969 return emitLibCall(LibFunc_snprintf, B.getInt32Ty(),
970 {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy()},
971 Args, B, TLI, /*IsVaArgs=*/true);
974 Value *llvm::emitSPrintf(Value *Dest, Value *Fmt,
975 ArrayRef<Value *> VariadicArgs, IRBuilder<> &B,
976 const TargetLibraryInfo *TLI) {
977 SmallVector<Value *, 8> Args{castToCStr(Dest, B), castToCStr(Fmt, B)};
978 Args.insert(Args.end(), VariadicArgs.begin(), VariadicArgs.end());
979 return emitLibCall(LibFunc_sprintf, B.getInt32Ty(),
980 {B.getInt8PtrTy(), B.getInt8PtrTy()}, Args, B, TLI,
981 /*IsVaArgs=*/true);
984 Value *llvm::emitStrCat(Value *Dest, Value *Src, IRBuilder<> &B,
985 const TargetLibraryInfo *TLI) {
986 return emitLibCall(LibFunc_strcat, B.getInt8PtrTy(),
987 {B.getInt8PtrTy(), B.getInt8PtrTy()},
988 {castToCStr(Dest, B), castToCStr(Src, B)}, B, TLI);
991 Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B,
992 const TargetLibraryInfo *TLI) {
993 return emitLibCall(LibFunc_strlcpy, Size->getType(),
994 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()},
995 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI);
998 Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B,
999 const TargetLibraryInfo *TLI) {
1000 return emitLibCall(LibFunc_strlcat, Size->getType(),
1001 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()},
1002 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI);
1005 Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B,
1006 const TargetLibraryInfo *TLI) {
1007 return emitLibCall(LibFunc_strncat, B.getInt8PtrTy(),
1008 {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()},
1009 {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI);
1012 Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
1013 IRBuilder<> &B, const TargetLibraryInfo *TLI) {
1014 return emitLibCall(
1015 LibFunc_vsnprintf, B.getInt32Ty(),
1016 {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy(), VAList->getType()},
1017 {castToCStr(Dest, B), Size, castToCStr(Fmt, B), VAList}, B, TLI);
1020 Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList,
1021 IRBuilder<> &B, const TargetLibraryInfo *TLI) {
1022 return emitLibCall(LibFunc_vsprintf, B.getInt32Ty(),
1023 {B.getInt8PtrTy(), B.getInt8PtrTy(), VAList->getType()},
1024 {castToCStr(Dest, B), castToCStr(Fmt, B), VAList}, B, TLI);
1027 /// Append a suffix to the function name according to the type of 'Op'.
1028 static void appendTypeSuffix(Value *Op, StringRef &Name,
1029 SmallString<20> &NameBuffer) {
1030 if (!Op->getType()->isDoubleTy()) {
1031 NameBuffer += Name;
1033 if (Op->getType()->isFloatTy())
1034 NameBuffer += 'f';
1035 else
1036 NameBuffer += 'l';
1038 Name = NameBuffer;
1042 static Value *emitUnaryFloatFnCallHelper(Value *Op, StringRef Name,
1043 IRBuilder<> &B,
1044 const AttributeList &Attrs) {
1045 assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall");
1047 Module *M = B.GetInsertBlock()->getModule();
1048 FunctionCallee Callee =
1049 M->getOrInsertFunction(Name, Op->getType(), Op->getType());
1050 CallInst *CI = B.CreateCall(Callee, Op, Name);
1052 // The incoming attribute set may have come from a speculatable intrinsic, but
1053 // is being replaced with a library call which is not allowed to be
1054 // speculatable.
1055 CI->setAttributes(Attrs.removeAttribute(B.getContext(),
1056 AttributeList::FunctionIndex,
1057 Attribute::Speculatable));
1058 if (const Function *F =
1059 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
1060 CI->setCallingConv(F->getCallingConv());
1062 return CI;
1065 Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
1066 const AttributeList &Attrs) {
1067 SmallString<20> NameBuffer;
1068 appendTypeSuffix(Op, Name, NameBuffer);
1070 return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs);
1073 Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
1074 LibFunc DoubleFn, LibFunc FloatFn,
1075 LibFunc LongDoubleFn, IRBuilder<> &B,
1076 const AttributeList &Attrs) {
1077 // Get the name of the function according to TLI.
1078 StringRef Name = getFloatFnName(TLI, Op->getType(),
1079 DoubleFn, FloatFn, LongDoubleFn);
1081 return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs);
1084 static Value *emitBinaryFloatFnCallHelper(Value *Op1, Value *Op2,
1085 StringRef Name, IRBuilder<> &B,
1086 const AttributeList &Attrs) {
1087 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
1089 Module *M = B.GetInsertBlock()->getModule();
1090 FunctionCallee Callee = M->getOrInsertFunction(Name, Op1->getType(),
1091 Op1->getType(), Op2->getType());
1092 CallInst *CI = B.CreateCall(Callee, { Op1, Op2 }, Name);
1094 // The incoming attribute set may have come from a speculatable intrinsic, but
1095 // is being replaced with a library call which is not allowed to be
1096 // speculatable.
1097 CI->setAttributes(Attrs.removeAttribute(B.getContext(),
1098 AttributeList::FunctionIndex,
1099 Attribute::Speculatable));
1100 if (const Function *F =
1101 dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
1102 CI->setCallingConv(F->getCallingConv());
1104 return CI;
1107 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
1108 IRBuilder<> &B, const AttributeList &Attrs) {
1109 assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
1111 SmallString<20> NameBuffer;
1112 appendTypeSuffix(Op1, Name, NameBuffer);
1114 return emitBinaryFloatFnCallHelper(Op1, Op2, Name, B, Attrs);
1117 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2,
1118 const TargetLibraryInfo *TLI,
1119 LibFunc DoubleFn, LibFunc FloatFn,
1120 LibFunc LongDoubleFn, IRBuilder<> &B,
1121 const AttributeList &Attrs) {
1122 // Get the name of the function according to TLI.
1123 StringRef Name = getFloatFnName(TLI, Op1->getType(),
1124 DoubleFn, FloatFn, LongDoubleFn);
1126 return emitBinaryFloatFnCallHelper(Op1, Op2, Name, B, Attrs);
1129 Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B,
1130 const TargetLibraryInfo *TLI) {
1131 if (!TLI->has(LibFunc_putchar))
1132 return nullptr;
1134 Module *M = B.GetInsertBlock()->getModule();
1135 StringRef PutCharName = TLI->getName(LibFunc_putchar);
1136 FunctionCallee PutChar =
1137 M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
1138 inferLibFuncAttributes(M, PutCharName, *TLI);
1139 CallInst *CI = B.CreateCall(PutChar,
1140 B.CreateIntCast(Char,
1141 B.getInt32Ty(),
1142 /*isSigned*/true,
1143 "chari"),
1144 PutCharName);
1146 if (const Function *F =
1147 dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts()))
1148 CI->setCallingConv(F->getCallingConv());
1149 return CI;
1152 Value *llvm::emitPutS(Value *Str, IRBuilder<> &B,
1153 const TargetLibraryInfo *TLI) {
1154 if (!TLI->has(LibFunc_puts))
1155 return nullptr;
1157 Module *M = B.GetInsertBlock()->getModule();
1158 StringRef PutsName = TLI->getName(LibFunc_puts);
1159 FunctionCallee PutS =
1160 M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy());
1161 inferLibFuncAttributes(M, PutsName, *TLI);
1162 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName);
1163 if (const Function *F =
1164 dyn_cast<Function>(PutS.getCallee()->stripPointerCasts()))
1165 CI->setCallingConv(F->getCallingConv());
1166 return CI;
1169 Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
1170 const TargetLibraryInfo *TLI) {
1171 if (!TLI->has(LibFunc_fputc))
1172 return nullptr;
1174 Module *M = B.GetInsertBlock()->getModule();
1175 StringRef FPutcName = TLI->getName(LibFunc_fputc);
1176 FunctionCallee F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(),
1177 B.getInt32Ty(), File->getType());
1178 if (File->getType()->isPointerTy())
1179 inferLibFuncAttributes(M, FPutcName, *TLI);
1180 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
1181 "chari");
1182 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
1184 if (const Function *Fn =
1185 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1186 CI->setCallingConv(Fn->getCallingConv());
1187 return CI;
1190 Value *llvm::emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
1191 const TargetLibraryInfo *TLI) {
1192 if (!TLI->has(LibFunc_fputc_unlocked))
1193 return nullptr;
1195 Module *M = B.GetInsertBlock()->getModule();
1196 StringRef FPutcUnlockedName = TLI->getName(LibFunc_fputc_unlocked);
1197 FunctionCallee F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(),
1198 B.getInt32Ty(), File->getType());
1199 if (File->getType()->isPointerTy())
1200 inferLibFuncAttributes(M, FPutcUnlockedName, *TLI);
1201 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari");
1202 CallInst *CI = B.CreateCall(F, {Char, File}, FPutcUnlockedName);
1204 if (const Function *Fn =
1205 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1206 CI->setCallingConv(Fn->getCallingConv());
1207 return CI;
1210 Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
1211 const TargetLibraryInfo *TLI) {
1212 if (!TLI->has(LibFunc_fputs))
1213 return nullptr;
1215 Module *M = B.GetInsertBlock()->getModule();
1216 StringRef FPutsName = TLI->getName(LibFunc_fputs);
1217 FunctionCallee F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
1218 B.getInt8PtrTy(), File->getType());
1219 if (File->getType()->isPointerTy())
1220 inferLibFuncAttributes(M, FPutsName, *TLI);
1221 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName);
1223 if (const Function *Fn =
1224 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1225 CI->setCallingConv(Fn->getCallingConv());
1226 return CI;
1229 Value *llvm::emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
1230 const TargetLibraryInfo *TLI) {
1231 if (!TLI->has(LibFunc_fputs_unlocked))
1232 return nullptr;
1234 Module *M = B.GetInsertBlock()->getModule();
1235 StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked);
1236 FunctionCallee F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(),
1237 B.getInt8PtrTy(), File->getType());
1238 if (File->getType()->isPointerTy())
1239 inferLibFuncAttributes(M, FPutsUnlockedName, *TLI);
1240 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsUnlockedName);
1242 if (const Function *Fn =
1243 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1244 CI->setCallingConv(Fn->getCallingConv());
1245 return CI;
1248 Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
1249 const DataLayout &DL, const TargetLibraryInfo *TLI) {
1250 if (!TLI->has(LibFunc_fwrite))
1251 return nullptr;
1253 Module *M = B.GetInsertBlock()->getModule();
1254 LLVMContext &Context = B.GetInsertBlock()->getContext();
1255 StringRef FWriteName = TLI->getName(LibFunc_fwrite);
1256 FunctionCallee F = M->getOrInsertFunction(
1257 FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
1258 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
1260 if (File->getType()->isPointerTy())
1261 inferLibFuncAttributes(M, FWriteName, *TLI);
1262 CallInst *CI =
1263 B.CreateCall(F, {castToCStr(Ptr, B), Size,
1264 ConstantInt::get(DL.getIntPtrType(Context), 1), File});
1266 if (const Function *Fn =
1267 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1268 CI->setCallingConv(Fn->getCallingConv());
1269 return CI;
1272 Value *llvm::emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
1273 const TargetLibraryInfo *TLI) {
1274 if (!TLI->has(LibFunc_malloc))
1275 return nullptr;
1277 Module *M = B.GetInsertBlock()->getModule();
1278 StringRef MallocName = TLI->getName(LibFunc_malloc);
1279 LLVMContext &Context = B.GetInsertBlock()->getContext();
1280 FunctionCallee Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(),
1281 DL.getIntPtrType(Context));
1282 inferLibFuncAttributes(M, MallocName, *TLI);
1283 CallInst *CI = B.CreateCall(Malloc, Num, MallocName);
1285 if (const Function *F =
1286 dyn_cast<Function>(Malloc.getCallee()->stripPointerCasts()))
1287 CI->setCallingConv(F->getCallingConv());
1289 return CI;
1292 Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
1293 IRBuilder<> &B, const TargetLibraryInfo &TLI) {
1294 if (!TLI.has(LibFunc_calloc))
1295 return nullptr;
1297 Module *M = B.GetInsertBlock()->getModule();
1298 StringRef CallocName = TLI.getName(LibFunc_calloc);
1299 const DataLayout &DL = M->getDataLayout();
1300 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext()));
1301 FunctionCallee Calloc = M->getOrInsertFunction(
1302 CallocName, Attrs, B.getInt8PtrTy(), PtrType, PtrType);
1303 inferLibFuncAttributes(M, CallocName, TLI);
1304 CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
1306 if (const auto *F =
1307 dyn_cast<Function>(Calloc.getCallee()->stripPointerCasts()))
1308 CI->setCallingConv(F->getCallingConv());
1310 return CI;
1313 Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
1314 IRBuilder<> &B, const DataLayout &DL,
1315 const TargetLibraryInfo *TLI) {
1316 if (!TLI->has(LibFunc_fwrite_unlocked))
1317 return nullptr;
1319 Module *M = B.GetInsertBlock()->getModule();
1320 LLVMContext &Context = B.GetInsertBlock()->getContext();
1321 StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked);
1322 FunctionCallee F = M->getOrInsertFunction(
1323 FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
1324 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
1326 if (File->getType()->isPointerTy())
1327 inferLibFuncAttributes(M, FWriteUnlockedName, *TLI);
1328 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
1330 if (const Function *Fn =
1331 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1332 CI->setCallingConv(Fn->getCallingConv());
1333 return CI;
1336 Value *llvm::emitFGetCUnlocked(Value *File, IRBuilder<> &B,
1337 const TargetLibraryInfo *TLI) {
1338 if (!TLI->has(LibFunc_fgetc_unlocked))
1339 return nullptr;
1341 Module *M = B.GetInsertBlock()->getModule();
1342 StringRef FGetCUnlockedName = TLI->getName(LibFunc_fgetc_unlocked);
1343 FunctionCallee F = M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(),
1344 File->getType());
1345 if (File->getType()->isPointerTy())
1346 inferLibFuncAttributes(M, FGetCUnlockedName, *TLI);
1347 CallInst *CI = B.CreateCall(F, File, FGetCUnlockedName);
1349 if (const Function *Fn =
1350 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1351 CI->setCallingConv(Fn->getCallingConv());
1352 return CI;
1355 Value *llvm::emitFGetSUnlocked(Value *Str, Value *Size, Value *File,
1356 IRBuilder<> &B, const TargetLibraryInfo *TLI) {
1357 if (!TLI->has(LibFunc_fgets_unlocked))
1358 return nullptr;
1360 Module *M = B.GetInsertBlock()->getModule();
1361 StringRef FGetSUnlockedName = TLI->getName(LibFunc_fgets_unlocked);
1362 FunctionCallee F =
1363 M->getOrInsertFunction(FGetSUnlockedName, B.getInt8PtrTy(),
1364 B.getInt8PtrTy(), B.getInt32Ty(), File->getType());
1365 inferLibFuncAttributes(M, FGetSUnlockedName, *TLI);
1366 CallInst *CI =
1367 B.CreateCall(F, {castToCStr(Str, B), Size, File}, FGetSUnlockedName);
1369 if (const Function *Fn =
1370 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1371 CI->setCallingConv(Fn->getCallingConv());
1372 return CI;
1375 Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
1376 IRBuilder<> &B, const DataLayout &DL,
1377 const TargetLibraryInfo *TLI) {
1378 if (!TLI->has(LibFunc_fread_unlocked))
1379 return nullptr;
1381 Module *M = B.GetInsertBlock()->getModule();
1382 LLVMContext &Context = B.GetInsertBlock()->getContext();
1383 StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked);
1384 FunctionCallee F = M->getOrInsertFunction(
1385 FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
1386 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
1388 if (File->getType()->isPointerTy())
1389 inferLibFuncAttributes(M, FReadUnlockedName, *TLI);
1390 CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
1392 if (const Function *Fn =
1393 dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1394 CI->setCallingConv(Fn->getCallingConv());
1395 return CI;