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