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