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