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