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