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