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