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