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