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