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