BuildLibCalls.cpp revision 341825
1//===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements some functions that will create standard C libcalls.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/Utils/BuildLibCalls.h"
15#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Statistic.h"
17#include "llvm/Analysis/TargetLibraryInfo.h"
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/DataLayout.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/IRBuilder.h"
22#include "llvm/IR/Intrinsics.h"
23#include "llvm/IR/LLVMContext.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/Type.h"
26
27using namespace llvm;
28
29#define DEBUG_TYPE "build-libcalls"
30
31//- Infer Attributes ---------------------------------------------------------//
32
33STATISTIC(NumReadNone, "Number of functions inferred as readnone");
34STATISTIC(NumReadOnly, "Number of functions inferred as readonly");
35STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly");
36STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind");
37STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture");
38STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly");
39STATISTIC(NumNoAlias, "Number of function returns inferred as noalias");
40STATISTIC(NumNonNull, "Number of function returns inferred as nonnull returns");
41
42static bool setDoesNotAccessMemory(Function &F) {
43  if (F.doesNotAccessMemory())
44    return false;
45  F.setDoesNotAccessMemory();
46  ++NumReadNone;
47  return true;
48}
49
50static bool setOnlyReadsMemory(Function &F) {
51  if (F.onlyReadsMemory())
52    return false;
53  F.setOnlyReadsMemory();
54  ++NumReadOnly;
55  return true;
56}
57
58static bool setOnlyAccessesArgMemory(Function &F) {
59  if (F.onlyAccessesArgMemory())
60    return false;
61  F.setOnlyAccessesArgMemory();
62  ++NumArgMemOnly;
63  return true;
64}
65
66static bool setDoesNotThrow(Function &F) {
67  if (F.doesNotThrow())
68    return false;
69  F.setDoesNotThrow();
70  ++NumNoUnwind;
71  return true;
72}
73
74static bool setRetDoesNotAlias(Function &F) {
75  if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias))
76    return false;
77  F.addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
78  ++NumNoAlias;
79  return true;
80}
81
82static bool setDoesNotCapture(Function &F, unsigned ArgNo) {
83  if (F.hasParamAttribute(ArgNo, Attribute::NoCapture))
84    return false;
85  F.addParamAttr(ArgNo, Attribute::NoCapture);
86  ++NumNoCapture;
87  return true;
88}
89
90static bool setOnlyReadsMemory(Function &F, unsigned ArgNo) {
91  if (F.hasParamAttribute(ArgNo, Attribute::ReadOnly))
92    return false;
93  F.addParamAttr(ArgNo, Attribute::ReadOnly);
94  ++NumReadOnlyArg;
95  return true;
96}
97
98static bool setRetNonNull(Function &F) {
99  assert(F.getReturnType()->isPointerTy() &&
100         "nonnull applies only to pointers");
101  if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NonNull))
102    return false;
103  F.addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
104  ++NumNonNull;
105  return true;
106}
107
108static bool setNonLazyBind(Function &F) {
109  if (F.hasFnAttribute(Attribute::NonLazyBind))
110    return false;
111  F.addFnAttr(Attribute::NonLazyBind);
112  return true;
113}
114
115bool llvm::inferLibFuncAttributes(Module *M, StringRef Name,
116                                  const TargetLibraryInfo &TLI) {
117  Function *F = M->getFunction(Name);
118  if (!F)
119    return false;
120  return inferLibFuncAttributes(*F, TLI);
121}
122
123bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
124  LibFunc TheLibFunc;
125  if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc)))
126    return false;
127
128  bool Changed = false;
129
130  if (F.getParent() != nullptr && F.getParent()->getRtLibUseGOT())
131    Changed |= setNonLazyBind(F);
132
133  switch (TheLibFunc) {
134  case LibFunc_strlen:
135  case LibFunc_wcslen:
136    Changed |= setOnlyReadsMemory(F);
137    Changed |= setDoesNotThrow(F);
138    Changed |= setOnlyAccessesArgMemory(F);
139    Changed |= setDoesNotCapture(F, 0);
140    return Changed;
141  case LibFunc_strchr:
142  case LibFunc_strrchr:
143    Changed |= setOnlyReadsMemory(F);
144    Changed |= setDoesNotThrow(F);
145    return Changed;
146  case LibFunc_strtol:
147  case LibFunc_strtod:
148  case LibFunc_strtof:
149  case LibFunc_strtoul:
150  case LibFunc_strtoll:
151  case LibFunc_strtold:
152  case LibFunc_strtoull:
153    Changed |= setDoesNotThrow(F);
154    Changed |= setDoesNotCapture(F, 1);
155    Changed |= setOnlyReadsMemory(F, 0);
156    return Changed;
157  case LibFunc_strcpy:
158  case LibFunc_stpcpy:
159  case LibFunc_strcat:
160  case LibFunc_strncat:
161  case LibFunc_strncpy:
162  case LibFunc_stpncpy:
163    Changed |= setDoesNotThrow(F);
164    Changed |= setDoesNotCapture(F, 1);
165    Changed |= setOnlyReadsMemory(F, 1);
166    return Changed;
167  case LibFunc_strxfrm:
168    Changed |= setDoesNotThrow(F);
169    Changed |= setDoesNotCapture(F, 0);
170    Changed |= setDoesNotCapture(F, 1);
171    Changed |= setOnlyReadsMemory(F, 1);
172    return Changed;
173  case LibFunc_strcmp:      // 0,1
174  case LibFunc_strspn:      // 0,1
175  case LibFunc_strncmp:     // 0,1
176  case LibFunc_strcspn:     // 0,1
177  case LibFunc_strcoll:     // 0,1
178  case LibFunc_strcasecmp:  // 0,1
179  case LibFunc_strncasecmp: //
180    Changed |= setOnlyReadsMemory(F);
181    Changed |= setDoesNotThrow(F);
182    Changed |= setDoesNotCapture(F, 0);
183    Changed |= setDoesNotCapture(F, 1);
184    return Changed;
185  case LibFunc_strstr:
186  case LibFunc_strpbrk:
187    Changed |= setOnlyReadsMemory(F);
188    Changed |= setDoesNotThrow(F);
189    Changed |= setDoesNotCapture(F, 1);
190    return Changed;
191  case LibFunc_strtok:
192  case LibFunc_strtok_r:
193    Changed |= setDoesNotThrow(F);
194    Changed |= setDoesNotCapture(F, 1);
195    Changed |= setOnlyReadsMemory(F, 1);
196    return Changed;
197  case LibFunc_scanf:
198    Changed |= setDoesNotThrow(F);
199    Changed |= setDoesNotCapture(F, 0);
200    Changed |= setOnlyReadsMemory(F, 0);
201    return Changed;
202  case LibFunc_setbuf:
203  case LibFunc_setvbuf:
204    Changed |= setDoesNotThrow(F);
205    Changed |= setDoesNotCapture(F, 0);
206    return Changed;
207  case LibFunc_strdup:
208  case LibFunc_strndup:
209    Changed |= setDoesNotThrow(F);
210    Changed |= setRetDoesNotAlias(F);
211    Changed |= setDoesNotCapture(F, 0);
212    Changed |= setOnlyReadsMemory(F, 0);
213    return Changed;
214  case LibFunc_stat:
215  case LibFunc_statvfs:
216    Changed |= setDoesNotThrow(F);
217    Changed |= setDoesNotCapture(F, 0);
218    Changed |= setDoesNotCapture(F, 1);
219    Changed |= setOnlyReadsMemory(F, 0);
220    return Changed;
221  case LibFunc_sscanf:
222    Changed |= setDoesNotThrow(F);
223    Changed |= setDoesNotCapture(F, 0);
224    Changed |= setDoesNotCapture(F, 1);
225    Changed |= setOnlyReadsMemory(F, 0);
226    Changed |= setOnlyReadsMemory(F, 1);
227    return Changed;
228  case LibFunc_sprintf:
229    Changed |= setDoesNotThrow(F);
230    Changed |= setDoesNotCapture(F, 0);
231    Changed |= setDoesNotCapture(F, 1);
232    Changed |= setOnlyReadsMemory(F, 1);
233    return Changed;
234  case LibFunc_snprintf:
235    Changed |= setDoesNotThrow(F);
236    Changed |= setDoesNotCapture(F, 0);
237    Changed |= setDoesNotCapture(F, 2);
238    Changed |= setOnlyReadsMemory(F, 2);
239    return Changed;
240  case LibFunc_setitimer:
241    Changed |= setDoesNotThrow(F);
242    Changed |= setDoesNotCapture(F, 1);
243    Changed |= setDoesNotCapture(F, 2);
244    Changed |= setOnlyReadsMemory(F, 1);
245    return Changed;
246  case LibFunc_system:
247    // May throw; "system" is a valid pthread cancellation point.
248    Changed |= setDoesNotCapture(F, 0);
249    Changed |= setOnlyReadsMemory(F, 0);
250    return Changed;
251  case LibFunc_malloc:
252    Changed |= setDoesNotThrow(F);
253    Changed |= setRetDoesNotAlias(F);
254    return Changed;
255  case LibFunc_memcmp:
256    Changed |= setOnlyReadsMemory(F);
257    Changed |= setDoesNotThrow(F);
258    Changed |= setDoesNotCapture(F, 0);
259    Changed |= setDoesNotCapture(F, 1);
260    return Changed;
261  case LibFunc_memchr:
262  case LibFunc_memrchr:
263    Changed |= setOnlyReadsMemory(F);
264    Changed |= setDoesNotThrow(F);
265    return Changed;
266  case LibFunc_modf:
267  case LibFunc_modff:
268  case LibFunc_modfl:
269    Changed |= setDoesNotThrow(F);
270    Changed |= setDoesNotCapture(F, 1);
271    return Changed;
272  case LibFunc_memcpy:
273  case LibFunc_mempcpy:
274  case LibFunc_memccpy:
275  case LibFunc_memmove:
276    Changed |= setDoesNotThrow(F);
277    Changed |= setDoesNotCapture(F, 1);
278    Changed |= setOnlyReadsMemory(F, 1);
279    return Changed;
280  case LibFunc_memcpy_chk:
281    Changed |= setDoesNotThrow(F);
282    return Changed;
283  case LibFunc_memalign:
284    Changed |= setRetDoesNotAlias(F);
285    return Changed;
286  case LibFunc_mkdir:
287    Changed |= setDoesNotThrow(F);
288    Changed |= setDoesNotCapture(F, 0);
289    Changed |= setOnlyReadsMemory(F, 0);
290    return Changed;
291  case LibFunc_mktime:
292    Changed |= setDoesNotThrow(F);
293    Changed |= setDoesNotCapture(F, 0);
294    return Changed;
295  case LibFunc_realloc:
296    Changed |= setDoesNotThrow(F);
297    Changed |= setRetDoesNotAlias(F);
298    Changed |= setDoesNotCapture(F, 0);
299    return Changed;
300  case LibFunc_read:
301    // May throw; "read" is a valid pthread cancellation point.
302    Changed |= setDoesNotCapture(F, 1);
303    return Changed;
304  case LibFunc_rewind:
305    Changed |= setDoesNotThrow(F);
306    Changed |= setDoesNotCapture(F, 0);
307    return Changed;
308  case LibFunc_rmdir:
309  case LibFunc_remove:
310  case LibFunc_realpath:
311    Changed |= setDoesNotThrow(F);
312    Changed |= setDoesNotCapture(F, 0);
313    Changed |= setOnlyReadsMemory(F, 0);
314    return Changed;
315  case LibFunc_rename:
316    Changed |= setDoesNotThrow(F);
317    Changed |= setDoesNotCapture(F, 0);
318    Changed |= setDoesNotCapture(F, 1);
319    Changed |= setOnlyReadsMemory(F, 0);
320    Changed |= setOnlyReadsMemory(F, 1);
321    return Changed;
322  case LibFunc_readlink:
323    Changed |= setDoesNotThrow(F);
324    Changed |= setDoesNotCapture(F, 0);
325    Changed |= setDoesNotCapture(F, 1);
326    Changed |= setOnlyReadsMemory(F, 0);
327    return Changed;
328  case LibFunc_write:
329    // May throw; "write" is a valid pthread cancellation point.
330    Changed |= setDoesNotCapture(F, 1);
331    Changed |= setOnlyReadsMemory(F, 1);
332    return Changed;
333  case LibFunc_bcopy:
334    Changed |= setDoesNotThrow(F);
335    Changed |= setDoesNotCapture(F, 0);
336    Changed |= setDoesNotCapture(F, 1);
337    Changed |= setOnlyReadsMemory(F, 0);
338    return Changed;
339  case LibFunc_bcmp:
340    Changed |= setDoesNotThrow(F);
341    Changed |= setOnlyReadsMemory(F);
342    Changed |= setDoesNotCapture(F, 0);
343    Changed |= setDoesNotCapture(F, 1);
344    return Changed;
345  case LibFunc_bzero:
346    Changed |= setDoesNotThrow(F);
347    Changed |= setDoesNotCapture(F, 0);
348    return Changed;
349  case LibFunc_calloc:
350    Changed |= setDoesNotThrow(F);
351    Changed |= setRetDoesNotAlias(F);
352    return Changed;
353  case LibFunc_chmod:
354  case LibFunc_chown:
355    Changed |= setDoesNotThrow(F);
356    Changed |= setDoesNotCapture(F, 0);
357    Changed |= setOnlyReadsMemory(F, 0);
358    return Changed;
359  case LibFunc_ctermid:
360  case LibFunc_clearerr:
361  case LibFunc_closedir:
362    Changed |= setDoesNotThrow(F);
363    Changed |= setDoesNotCapture(F, 0);
364    return Changed;
365  case LibFunc_atoi:
366  case LibFunc_atol:
367  case LibFunc_atof:
368  case LibFunc_atoll:
369    Changed |= setDoesNotThrow(F);
370    Changed |= setOnlyReadsMemory(F);
371    Changed |= setDoesNotCapture(F, 0);
372    return Changed;
373  case LibFunc_access:
374    Changed |= setDoesNotThrow(F);
375    Changed |= setDoesNotCapture(F, 0);
376    Changed |= setOnlyReadsMemory(F, 0);
377    return Changed;
378  case LibFunc_fopen:
379    Changed |= setDoesNotThrow(F);
380    Changed |= setRetDoesNotAlias(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_fdopen:
387    Changed |= setDoesNotThrow(F);
388    Changed |= setRetDoesNotAlias(F);
389    Changed |= setDoesNotCapture(F, 1);
390    Changed |= setOnlyReadsMemory(F, 1);
391    return Changed;
392  case LibFunc_feof:
393  case LibFunc_free:
394  case LibFunc_fseek:
395  case LibFunc_ftell:
396  case LibFunc_fgetc:
397  case LibFunc_fgetc_unlocked:
398  case LibFunc_fseeko:
399  case LibFunc_ftello:
400  case LibFunc_fileno:
401  case LibFunc_fflush:
402  case LibFunc_fclose:
403  case LibFunc_fsetpos:
404  case LibFunc_flockfile:
405  case LibFunc_funlockfile:
406  case LibFunc_ftrylockfile:
407    Changed |= setDoesNotThrow(F);
408    Changed |= setDoesNotCapture(F, 0);
409    return Changed;
410  case LibFunc_ferror:
411    Changed |= setDoesNotThrow(F);
412    Changed |= setDoesNotCapture(F, 0);
413    Changed |= setOnlyReadsMemory(F);
414    return Changed;
415  case LibFunc_fputc:
416  case LibFunc_fputc_unlocked:
417  case LibFunc_fstat:
418  case LibFunc_frexp:
419  case LibFunc_frexpf:
420  case LibFunc_frexpl:
421  case LibFunc_fstatvfs:
422    Changed |= setDoesNotThrow(F);
423    Changed |= setDoesNotCapture(F, 1);
424    return Changed;
425  case LibFunc_fgets:
426  case LibFunc_fgets_unlocked:
427    Changed |= setDoesNotThrow(F);
428    Changed |= setDoesNotCapture(F, 2);
429    return Changed;
430  case LibFunc_fread:
431  case LibFunc_fread_unlocked:
432    Changed |= setDoesNotThrow(F);
433    Changed |= setDoesNotCapture(F, 0);
434    Changed |= setDoesNotCapture(F, 3);
435    return Changed;
436  case LibFunc_fwrite:
437  case LibFunc_fwrite_unlocked:
438    Changed |= setDoesNotThrow(F);
439    Changed |= setDoesNotCapture(F, 0);
440    Changed |= setDoesNotCapture(F, 3);
441    // FIXME: readonly #1?
442    return Changed;
443  case LibFunc_fputs:
444  case LibFunc_fputs_unlocked:
445    Changed |= setDoesNotThrow(F);
446    Changed |= setDoesNotCapture(F, 0);
447    Changed |= setDoesNotCapture(F, 1);
448    Changed |= setOnlyReadsMemory(F, 0);
449    return Changed;
450  case LibFunc_fscanf:
451  case LibFunc_fprintf:
452    Changed |= setDoesNotThrow(F);
453    Changed |= setDoesNotCapture(F, 0);
454    Changed |= setDoesNotCapture(F, 1);
455    Changed |= setOnlyReadsMemory(F, 1);
456    return Changed;
457  case LibFunc_fgetpos:
458    Changed |= setDoesNotThrow(F);
459    Changed |= setDoesNotCapture(F, 0);
460    Changed |= setDoesNotCapture(F, 1);
461    return Changed;
462  case LibFunc_getc:
463  case LibFunc_getlogin_r:
464  case LibFunc_getc_unlocked:
465    Changed |= setDoesNotThrow(F);
466    Changed |= setDoesNotCapture(F, 0);
467    return Changed;
468  case LibFunc_getenv:
469    Changed |= setDoesNotThrow(F);
470    Changed |= setOnlyReadsMemory(F);
471    Changed |= setDoesNotCapture(F, 0);
472    return Changed;
473  case LibFunc_gets:
474  case LibFunc_getchar:
475  case LibFunc_getchar_unlocked:
476    Changed |= setDoesNotThrow(F);
477    return Changed;
478  case LibFunc_getitimer:
479    Changed |= setDoesNotThrow(F);
480    Changed |= setDoesNotCapture(F, 1);
481    return Changed;
482  case LibFunc_getpwnam:
483    Changed |= setDoesNotThrow(F);
484    Changed |= setDoesNotCapture(F, 0);
485    Changed |= setOnlyReadsMemory(F, 0);
486    return Changed;
487  case LibFunc_ungetc:
488    Changed |= setDoesNotThrow(F);
489    Changed |= setDoesNotCapture(F, 1);
490    return Changed;
491  case LibFunc_uname:
492    Changed |= setDoesNotThrow(F);
493    Changed |= setDoesNotCapture(F, 0);
494    return Changed;
495  case LibFunc_unlink:
496    Changed |= setDoesNotThrow(F);
497    Changed |= setDoesNotCapture(F, 0);
498    Changed |= setOnlyReadsMemory(F, 0);
499    return Changed;
500  case LibFunc_unsetenv:
501    Changed |= setDoesNotThrow(F);
502    Changed |= setDoesNotCapture(F, 0);
503    Changed |= setOnlyReadsMemory(F, 0);
504    return Changed;
505  case LibFunc_utime:
506  case LibFunc_utimes:
507    Changed |= setDoesNotThrow(F);
508    Changed |= setDoesNotCapture(F, 0);
509    Changed |= setDoesNotCapture(F, 1);
510    Changed |= setOnlyReadsMemory(F, 0);
511    Changed |= setOnlyReadsMemory(F, 1);
512    return Changed;
513  case LibFunc_putc:
514  case LibFunc_putc_unlocked:
515    Changed |= setDoesNotThrow(F);
516    Changed |= setDoesNotCapture(F, 1);
517    return Changed;
518  case LibFunc_puts:
519  case LibFunc_printf:
520  case LibFunc_perror:
521    Changed |= setDoesNotThrow(F);
522    Changed |= setDoesNotCapture(F, 0);
523    Changed |= setOnlyReadsMemory(F, 0);
524    return Changed;
525  case LibFunc_pread:
526    // May throw; "pread" is a valid pthread cancellation point.
527    Changed |= setDoesNotCapture(F, 1);
528    return Changed;
529  case LibFunc_pwrite:
530    // May throw; "pwrite" is a valid pthread cancellation point.
531    Changed |= setDoesNotCapture(F, 1);
532    Changed |= setOnlyReadsMemory(F, 1);
533    return Changed;
534  case LibFunc_putchar:
535  case LibFunc_putchar_unlocked:
536    Changed |= setDoesNotThrow(F);
537    return Changed;
538  case LibFunc_popen:
539    Changed |= setDoesNotThrow(F);
540    Changed |= setRetDoesNotAlias(F);
541    Changed |= setDoesNotCapture(F, 0);
542    Changed |= setDoesNotCapture(F, 1);
543    Changed |= setOnlyReadsMemory(F, 0);
544    Changed |= setOnlyReadsMemory(F, 1);
545    return Changed;
546  case LibFunc_pclose:
547    Changed |= setDoesNotThrow(F);
548    Changed |= setDoesNotCapture(F, 0);
549    return Changed;
550  case LibFunc_vscanf:
551    Changed |= setDoesNotThrow(F);
552    Changed |= setDoesNotCapture(F, 0);
553    Changed |= setOnlyReadsMemory(F, 0);
554    return Changed;
555  case LibFunc_vsscanf:
556    Changed |= setDoesNotThrow(F);
557    Changed |= setDoesNotCapture(F, 0);
558    Changed |= setDoesNotCapture(F, 1);
559    Changed |= setOnlyReadsMemory(F, 0);
560    Changed |= setOnlyReadsMemory(F, 1);
561    return Changed;
562  case LibFunc_vfscanf:
563    Changed |= setDoesNotThrow(F);
564    Changed |= setDoesNotCapture(F, 0);
565    Changed |= setDoesNotCapture(F, 1);
566    Changed |= setOnlyReadsMemory(F, 1);
567    return Changed;
568  case LibFunc_valloc:
569    Changed |= setDoesNotThrow(F);
570    Changed |= setRetDoesNotAlias(F);
571    return Changed;
572  case LibFunc_vprintf:
573    Changed |= setDoesNotThrow(F);
574    Changed |= setDoesNotCapture(F, 0);
575    Changed |= setOnlyReadsMemory(F, 0);
576    return Changed;
577  case LibFunc_vfprintf:
578  case LibFunc_vsprintf:
579    Changed |= setDoesNotThrow(F);
580    Changed |= setDoesNotCapture(F, 0);
581    Changed |= setDoesNotCapture(F, 1);
582    Changed |= setOnlyReadsMemory(F, 1);
583    return Changed;
584  case LibFunc_vsnprintf:
585    Changed |= setDoesNotThrow(F);
586    Changed |= setDoesNotCapture(F, 0);
587    Changed |= setDoesNotCapture(F, 2);
588    Changed |= setOnlyReadsMemory(F, 2);
589    return Changed;
590  case LibFunc_open:
591    // May throw; "open" is a valid pthread cancellation point.
592    Changed |= setDoesNotCapture(F, 0);
593    Changed |= setOnlyReadsMemory(F, 0);
594    return Changed;
595  case LibFunc_opendir:
596    Changed |= setDoesNotThrow(F);
597    Changed |= setRetDoesNotAlias(F);
598    Changed |= setDoesNotCapture(F, 0);
599    Changed |= setOnlyReadsMemory(F, 0);
600    return Changed;
601  case LibFunc_tmpfile:
602    Changed |= setDoesNotThrow(F);
603    Changed |= setRetDoesNotAlias(F);
604    return Changed;
605  case LibFunc_times:
606    Changed |= setDoesNotThrow(F);
607    Changed |= setDoesNotCapture(F, 0);
608    return Changed;
609  case LibFunc_htonl:
610  case LibFunc_htons:
611  case LibFunc_ntohl:
612  case LibFunc_ntohs:
613    Changed |= setDoesNotThrow(F);
614    Changed |= setDoesNotAccessMemory(F);
615    return Changed;
616  case LibFunc_lstat:
617    Changed |= setDoesNotThrow(F);
618    Changed |= setDoesNotCapture(F, 0);
619    Changed |= setDoesNotCapture(F, 1);
620    Changed |= setOnlyReadsMemory(F, 0);
621    return Changed;
622  case LibFunc_lchown:
623    Changed |= setDoesNotThrow(F);
624    Changed |= setDoesNotCapture(F, 0);
625    Changed |= setOnlyReadsMemory(F, 0);
626    return Changed;
627  case LibFunc_qsort:
628    // May throw; places call through function pointer.
629    Changed |= setDoesNotCapture(F, 3);
630    return Changed;
631  case LibFunc_dunder_strdup:
632  case LibFunc_dunder_strndup:
633    Changed |= setDoesNotThrow(F);
634    Changed |= setRetDoesNotAlias(F);
635    Changed |= setDoesNotCapture(F, 0);
636    Changed |= setOnlyReadsMemory(F, 0);
637    return Changed;
638  case LibFunc_dunder_strtok_r:
639    Changed |= setDoesNotThrow(F);
640    Changed |= setDoesNotCapture(F, 1);
641    Changed |= setOnlyReadsMemory(F, 1);
642    return Changed;
643  case LibFunc_under_IO_getc:
644    Changed |= setDoesNotThrow(F);
645    Changed |= setDoesNotCapture(F, 0);
646    return Changed;
647  case LibFunc_under_IO_putc:
648    Changed |= setDoesNotThrow(F);
649    Changed |= setDoesNotCapture(F, 1);
650    return Changed;
651  case LibFunc_dunder_isoc99_scanf:
652    Changed |= setDoesNotThrow(F);
653    Changed |= setDoesNotCapture(F, 0);
654    Changed |= setOnlyReadsMemory(F, 0);
655    return Changed;
656  case LibFunc_stat64:
657  case LibFunc_lstat64:
658  case LibFunc_statvfs64:
659    Changed |= setDoesNotThrow(F);
660    Changed |= setDoesNotCapture(F, 0);
661    Changed |= setDoesNotCapture(F, 1);
662    Changed |= setOnlyReadsMemory(F, 0);
663    return Changed;
664  case LibFunc_dunder_isoc99_sscanf:
665    Changed |= setDoesNotThrow(F);
666    Changed |= setDoesNotCapture(F, 0);
667    Changed |= setDoesNotCapture(F, 1);
668    Changed |= setOnlyReadsMemory(F, 0);
669    Changed |= setOnlyReadsMemory(F, 1);
670    return Changed;
671  case LibFunc_fopen64:
672    Changed |= setDoesNotThrow(F);
673    Changed |= setRetDoesNotAlias(F);
674    Changed |= setDoesNotCapture(F, 0);
675    Changed |= setDoesNotCapture(F, 1);
676    Changed |= setOnlyReadsMemory(F, 0);
677    Changed |= setOnlyReadsMemory(F, 1);
678    return Changed;
679  case LibFunc_fseeko64:
680  case LibFunc_ftello64:
681    Changed |= setDoesNotThrow(F);
682    Changed |= setDoesNotCapture(F, 0);
683    return Changed;
684  case LibFunc_tmpfile64:
685    Changed |= setDoesNotThrow(F);
686    Changed |= setRetDoesNotAlias(F);
687    return Changed;
688  case LibFunc_fstat64:
689  case LibFunc_fstatvfs64:
690    Changed |= setDoesNotThrow(F);
691    Changed |= setDoesNotCapture(F, 1);
692    return Changed;
693  case LibFunc_open64:
694    // May throw; "open" is a valid pthread cancellation point.
695    Changed |= setDoesNotCapture(F, 0);
696    Changed |= setOnlyReadsMemory(F, 0);
697    return Changed;
698  case LibFunc_gettimeofday:
699    // Currently some platforms have the restrict keyword on the arguments to
700    // gettimeofday. To be conservative, do not add noalias to gettimeofday's
701    // arguments.
702    Changed |= setDoesNotThrow(F);
703    Changed |= setDoesNotCapture(F, 0);
704    Changed |= setDoesNotCapture(F, 1);
705    return Changed;
706  case LibFunc_Znwj: // new(unsigned int)
707  case LibFunc_Znwm: // new(unsigned long)
708  case LibFunc_Znaj: // new[](unsigned int)
709  case LibFunc_Znam: // new[](unsigned long)
710  case LibFunc_msvc_new_int: // new(unsigned int)
711  case LibFunc_msvc_new_longlong: // new(unsigned long long)
712  case LibFunc_msvc_new_array_int: // new[](unsigned int)
713  case LibFunc_msvc_new_array_longlong: // new[](unsigned long long)
714    // Operator new always returns a nonnull noalias pointer
715    Changed |= setRetNonNull(F);
716    Changed |= setRetDoesNotAlias(F);
717    return Changed;
718  // TODO: add LibFunc entries for:
719  // case LibFunc_memset_pattern4:
720  // case LibFunc_memset_pattern8:
721  case LibFunc_memset_pattern16:
722    Changed |= setOnlyAccessesArgMemory(F);
723    Changed |= setDoesNotCapture(F, 0);
724    Changed |= setDoesNotCapture(F, 1);
725    Changed |= setOnlyReadsMemory(F, 1);
726    return Changed;
727  // int __nvvm_reflect(const char *)
728  case LibFunc_nvvm_reflect:
729    Changed |= setDoesNotAccessMemory(F);
730    Changed |= setDoesNotThrow(F);
731    return Changed;
732
733  default:
734    // FIXME: It'd be really nice to cover all the library functions we're
735    // aware of here.
736    return false;
737  }
738}
739
740bool llvm::hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
741                           LibFunc DoubleFn, LibFunc FloatFn,
742                           LibFunc LongDoubleFn) {
743  switch (Ty->getTypeID()) {
744  case Type::FloatTyID:
745    return TLI->has(FloatFn);
746  case Type::DoubleTyID:
747    return TLI->has(DoubleFn);
748  default:
749    return TLI->has(LongDoubleFn);
750  }
751}
752
753//- Emit LibCalls ------------------------------------------------------------//
754
755Value *llvm::castToCStr(Value *V, IRBuilder<> &B) {
756  unsigned AS = V->getType()->getPointerAddressSpace();
757  return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
758}
759
760Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
761                        const TargetLibraryInfo *TLI) {
762  if (!TLI->has(LibFunc_strlen))
763    return nullptr;
764
765  Module *M = B.GetInsertBlock()->getModule();
766  StringRef StrlenName = TLI->getName(LibFunc_strlen);
767  LLVMContext &Context = B.GetInsertBlock()->getContext();
768  Constant *StrLen = M->getOrInsertFunction(StrlenName, DL.getIntPtrType(Context),
769                                            B.getInt8PtrTy());
770  inferLibFuncAttributes(M, StrlenName, *TLI);
771  CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), StrlenName);
772  if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
773    CI->setCallingConv(F->getCallingConv());
774
775  return CI;
776}
777
778Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
779                        const TargetLibraryInfo *TLI) {
780  if (!TLI->has(LibFunc_strchr))
781    return nullptr;
782
783  Module *M = B.GetInsertBlock()->getModule();
784  StringRef StrChrName = TLI->getName(LibFunc_strchr);
785  Type *I8Ptr = B.getInt8PtrTy();
786  Type *I32Ty = B.getInt32Ty();
787  Constant *StrChr =
788      M->getOrInsertFunction(StrChrName, I8Ptr, I8Ptr, I32Ty);
789  inferLibFuncAttributes(M, StrChrName, *TLI);
790  CallInst *CI = B.CreateCall(
791      StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, StrChrName);
792  if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
793    CI->setCallingConv(F->getCallingConv());
794  return CI;
795}
796
797Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
798                         const DataLayout &DL, const TargetLibraryInfo *TLI) {
799  if (!TLI->has(LibFunc_strncmp))
800    return nullptr;
801
802  Module *M = B.GetInsertBlock()->getModule();
803  StringRef StrNCmpName = TLI->getName(LibFunc_strncmp);
804  LLVMContext &Context = B.GetInsertBlock()->getContext();
805  Value *StrNCmp = M->getOrInsertFunction(StrNCmpName, B.getInt32Ty(),
806                                          B.getInt8PtrTy(), B.getInt8PtrTy(),
807                                          DL.getIntPtrType(Context));
808  inferLibFuncAttributes(M, StrNCmpName, *TLI);
809  CallInst *CI = B.CreateCall(
810      StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, StrNCmpName);
811
812  if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
813    CI->setCallingConv(F->getCallingConv());
814
815  return CI;
816}
817
818Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
819                        const TargetLibraryInfo *TLI, StringRef Name) {
820  if (!TLI->has(LibFunc_strcpy))
821    return nullptr;
822
823  Module *M = B.GetInsertBlock()->getModule();
824  Type *I8Ptr = B.getInt8PtrTy();
825  Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
826  inferLibFuncAttributes(M, Name, *TLI);
827  CallInst *CI =
828      B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
829  if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
830    CI->setCallingConv(F->getCallingConv());
831  return CI;
832}
833
834Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
835                         const TargetLibraryInfo *TLI, StringRef Name) {
836  if (!TLI->has(LibFunc_strncpy))
837    return nullptr;
838
839  Module *M = B.GetInsertBlock()->getModule();
840  Type *I8Ptr = B.getInt8PtrTy();
841  Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr,
842                                          Len->getType());
843  inferLibFuncAttributes(M, Name, *TLI);
844  CallInst *CI = B.CreateCall(
845      StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, Name);
846  if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
847    CI->setCallingConv(F->getCallingConv());
848  return CI;
849}
850
851Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
852                           IRBuilder<> &B, const DataLayout &DL,
853                           const TargetLibraryInfo *TLI) {
854  if (!TLI->has(LibFunc_memcpy_chk))
855    return nullptr;
856
857  Module *M = B.GetInsertBlock()->getModule();
858  AttributeList AS;
859  AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex,
860                          Attribute::NoUnwind);
861  LLVMContext &Context = B.GetInsertBlock()->getContext();
862  Value *MemCpy = M->getOrInsertFunction(
863      "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
864      B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
865      DL.getIntPtrType(Context));
866  Dst = castToCStr(Dst, B);
867  Src = castToCStr(Src, B);
868  CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
869  if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
870    CI->setCallingConv(F->getCallingConv());
871  return CI;
872}
873
874Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
875                        const DataLayout &DL, const TargetLibraryInfo *TLI) {
876  if (!TLI->has(LibFunc_memchr))
877    return nullptr;
878
879  Module *M = B.GetInsertBlock()->getModule();
880  StringRef MemChrName = TLI->getName(LibFunc_memchr);
881  LLVMContext &Context = B.GetInsertBlock()->getContext();
882  Value *MemChr = M->getOrInsertFunction(MemChrName, B.getInt8PtrTy(),
883                                         B.getInt8PtrTy(), B.getInt32Ty(),
884                                         DL.getIntPtrType(Context));
885  inferLibFuncAttributes(M, MemChrName, *TLI);
886  CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, MemChrName);
887
888  if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
889    CI->setCallingConv(F->getCallingConv());
890
891  return CI;
892}
893
894Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
895                        const DataLayout &DL, const TargetLibraryInfo *TLI) {
896  if (!TLI->has(LibFunc_memcmp))
897    return nullptr;
898
899  Module *M = B.GetInsertBlock()->getModule();
900  StringRef MemCmpName = TLI->getName(LibFunc_memcmp);
901  LLVMContext &Context = B.GetInsertBlock()->getContext();
902  Value *MemCmp = M->getOrInsertFunction(MemCmpName, B.getInt32Ty(),
903                                         B.getInt8PtrTy(), B.getInt8PtrTy(),
904                                         DL.getIntPtrType(Context));
905  inferLibFuncAttributes(M, MemCmpName, *TLI);
906  CallInst *CI = B.CreateCall(
907      MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, MemCmpName);
908
909  if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
910    CI->setCallingConv(F->getCallingConv());
911
912  return CI;
913}
914
915/// Append a suffix to the function name according to the type of 'Op'.
916static void appendTypeSuffix(Value *Op, StringRef &Name,
917                             SmallString<20> &NameBuffer) {
918  if (!Op->getType()->isDoubleTy()) {
919      NameBuffer += Name;
920
921    if (Op->getType()->isFloatTy())
922      NameBuffer += 'f';
923    else
924      NameBuffer += 'l';
925
926    Name = NameBuffer;
927  }
928}
929
930Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
931                                  const AttributeList &Attrs) {
932  SmallString<20> NameBuffer;
933  appendTypeSuffix(Op, Name, NameBuffer);
934
935  Module *M = B.GetInsertBlock()->getModule();
936  Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
937                                         Op->getType());
938  CallInst *CI = B.CreateCall(Callee, Op, Name);
939
940  // The incoming attribute set may have come from a speculatable intrinsic, but
941  // is being replaced with a library call which is not allowed to be
942  // speculatable.
943  CI->setAttributes(Attrs.removeAttribute(B.getContext(),
944                                          AttributeList::FunctionIndex,
945                                          Attribute::Speculatable));
946  if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
947    CI->setCallingConv(F->getCallingConv());
948
949  return CI;
950}
951
952Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
953                                   IRBuilder<> &B, const AttributeList &Attrs) {
954  SmallString<20> NameBuffer;
955  appendTypeSuffix(Op1, Name, NameBuffer);
956
957  Module *M = B.GetInsertBlock()->getModule();
958  Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(),
959                                         Op2->getType());
960  CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
961  CI->setAttributes(Attrs);
962  if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
963    CI->setCallingConv(F->getCallingConv());
964
965  return CI;
966}
967
968Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B,
969                         const TargetLibraryInfo *TLI) {
970  if (!TLI->has(LibFunc_putchar))
971    return nullptr;
972
973  Module *M = B.GetInsertBlock()->getModule();
974  StringRef PutCharName = TLI->getName(LibFunc_putchar);
975  Value *PutChar = M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
976  inferLibFuncAttributes(M, PutCharName, *TLI);
977  CallInst *CI = B.CreateCall(PutChar,
978                              B.CreateIntCast(Char,
979                              B.getInt32Ty(),
980                              /*isSigned*/true,
981                              "chari"),
982                              PutCharName);
983
984  if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
985    CI->setCallingConv(F->getCallingConv());
986  return CI;
987}
988
989Value *llvm::emitPutS(Value *Str, IRBuilder<> &B,
990                      const TargetLibraryInfo *TLI) {
991  if (!TLI->has(LibFunc_puts))
992    return nullptr;
993
994  Module *M = B.GetInsertBlock()->getModule();
995  StringRef PutsName = TLI->getName(LibFunc_puts);
996  Value *PutS =
997      M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy());
998  inferLibFuncAttributes(M, PutsName, *TLI);
999  CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName);
1000  if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
1001    CI->setCallingConv(F->getCallingConv());
1002  return CI;
1003}
1004
1005Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
1006                       const TargetLibraryInfo *TLI) {
1007  if (!TLI->has(LibFunc_fputc))
1008    return nullptr;
1009
1010  Module *M = B.GetInsertBlock()->getModule();
1011  StringRef FPutcName = TLI->getName(LibFunc_fputc);
1012  Constant *F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(), B.getInt32Ty(),
1013                                       File->getType());
1014  if (File->getType()->isPointerTy())
1015    inferLibFuncAttributes(M, FPutcName, *TLI);
1016  Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
1017                         "chari");
1018  CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
1019
1020  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
1021    CI->setCallingConv(Fn->getCallingConv());
1022  return CI;
1023}
1024
1025Value *llvm::emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
1026                               const TargetLibraryInfo *TLI) {
1027  if (!TLI->has(LibFunc_fputc_unlocked))
1028    return nullptr;
1029
1030  Module *M = B.GetInsertBlock()->getModule();
1031  StringRef FPutcUnlockedName = TLI->getName(LibFunc_fputc_unlocked);
1032  Constant *F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(),
1033                                       B.getInt32Ty(), File->getType());
1034  if (File->getType()->isPointerTy())
1035    inferLibFuncAttributes(M, FPutcUnlockedName, *TLI);
1036  Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari");
1037  CallInst *CI = B.CreateCall(F, {Char, File}, FPutcUnlockedName);
1038
1039  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
1040    CI->setCallingConv(Fn->getCallingConv());
1041  return CI;
1042}
1043
1044Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
1045                       const TargetLibraryInfo *TLI) {
1046  if (!TLI->has(LibFunc_fputs))
1047    return nullptr;
1048
1049  Module *M = B.GetInsertBlock()->getModule();
1050  StringRef FPutsName = TLI->getName(LibFunc_fputs);
1051  Constant *F = M->getOrInsertFunction(
1052      FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType());
1053  if (File->getType()->isPointerTy())
1054    inferLibFuncAttributes(M, FPutsName, *TLI);
1055  CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName);
1056
1057  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
1058    CI->setCallingConv(Fn->getCallingConv());
1059  return CI;
1060}
1061
1062Value *llvm::emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
1063                               const TargetLibraryInfo *TLI) {
1064  if (!TLI->has(LibFunc_fputs_unlocked))
1065    return nullptr;
1066
1067  Module *M = B.GetInsertBlock()->getModule();
1068  StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked);
1069  Constant *F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(),
1070                                       B.getInt8PtrTy(), File->getType());
1071  if (File->getType()->isPointerTy())
1072    inferLibFuncAttributes(M, FPutsUnlockedName, *TLI);
1073  CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsUnlockedName);
1074
1075  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
1076    CI->setCallingConv(Fn->getCallingConv());
1077  return CI;
1078}
1079
1080Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
1081                        const DataLayout &DL, const TargetLibraryInfo *TLI) {
1082  if (!TLI->has(LibFunc_fwrite))
1083    return nullptr;
1084
1085  Module *M = B.GetInsertBlock()->getModule();
1086  LLVMContext &Context = B.GetInsertBlock()->getContext();
1087  StringRef FWriteName = TLI->getName(LibFunc_fwrite);
1088  Constant *F = M->getOrInsertFunction(
1089      FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
1090      DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
1091
1092  if (File->getType()->isPointerTy())
1093    inferLibFuncAttributes(M, FWriteName, *TLI);
1094  CallInst *CI =
1095      B.CreateCall(F, {castToCStr(Ptr, B), Size,
1096                       ConstantInt::get(DL.getIntPtrType(Context), 1), File});
1097
1098  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
1099    CI->setCallingConv(Fn->getCallingConv());
1100  return CI;
1101}
1102
1103Value *llvm::emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
1104                        const TargetLibraryInfo *TLI) {
1105  if (!TLI->has(LibFunc_malloc))
1106    return nullptr;
1107
1108  Module *M = B.GetInsertBlock()->getModule();
1109  StringRef MallocName = TLI->getName(LibFunc_malloc);
1110  LLVMContext &Context = B.GetInsertBlock()->getContext();
1111  Value *Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(),
1112                                         DL.getIntPtrType(Context));
1113  inferLibFuncAttributes(M, MallocName, *TLI);
1114  CallInst *CI = B.CreateCall(Malloc, Num, MallocName);
1115
1116  if (const Function *F = dyn_cast<Function>(Malloc->stripPointerCasts()))
1117    CI->setCallingConv(F->getCallingConv());
1118
1119  return CI;
1120}
1121
1122Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
1123                        IRBuilder<> &B, const TargetLibraryInfo &TLI) {
1124  if (!TLI.has(LibFunc_calloc))
1125    return nullptr;
1126
1127  Module *M = B.GetInsertBlock()->getModule();
1128  StringRef CallocName = TLI.getName(LibFunc_calloc);
1129  const DataLayout &DL = M->getDataLayout();
1130  IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext()));
1131  Value *Calloc = M->getOrInsertFunction(CallocName, Attrs, B.getInt8PtrTy(),
1132                                         PtrType, PtrType);
1133  inferLibFuncAttributes(M, CallocName, TLI);
1134  CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
1135
1136  if (const auto *F = dyn_cast<Function>(Calloc->stripPointerCasts()))
1137    CI->setCallingConv(F->getCallingConv());
1138
1139  return CI;
1140}
1141
1142Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
1143                                IRBuilder<> &B, const DataLayout &DL,
1144                                const TargetLibraryInfo *TLI) {
1145  if (!TLI->has(LibFunc_fwrite_unlocked))
1146    return nullptr;
1147
1148  Module *M = B.GetInsertBlock()->getModule();
1149  LLVMContext &Context = B.GetInsertBlock()->getContext();
1150  StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked);
1151  Constant *F = M->getOrInsertFunction(
1152      FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
1153      DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
1154
1155  if (File->getType()->isPointerTy())
1156    inferLibFuncAttributes(M, FWriteUnlockedName, *TLI);
1157  CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
1158
1159  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
1160    CI->setCallingConv(Fn->getCallingConv());
1161  return CI;
1162}
1163
1164Value *llvm::emitFGetCUnlocked(Value *File, IRBuilder<> &B,
1165                               const TargetLibraryInfo *TLI) {
1166  if (!TLI->has(LibFunc_fgetc_unlocked))
1167    return nullptr;
1168
1169  Module *M = B.GetInsertBlock()->getModule();
1170  StringRef FGetCUnlockedName = TLI->getName(LibFunc_fgetc_unlocked);
1171  Constant *F =
1172      M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(), File->getType());
1173  if (File->getType()->isPointerTy())
1174    inferLibFuncAttributes(M, FGetCUnlockedName, *TLI);
1175  CallInst *CI = B.CreateCall(F, File, FGetCUnlockedName);
1176
1177  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
1178    CI->setCallingConv(Fn->getCallingConv());
1179  return CI;
1180}
1181
1182Value *llvm::emitFGetSUnlocked(Value *Str, Value *Size, Value *File,
1183                               IRBuilder<> &B, const TargetLibraryInfo *TLI) {
1184  if (!TLI->has(LibFunc_fgets_unlocked))
1185    return nullptr;
1186
1187  Module *M = B.GetInsertBlock()->getModule();
1188  StringRef FGetSUnlockedName = TLI->getName(LibFunc_fgets_unlocked);
1189  Constant *F =
1190      M->getOrInsertFunction(FGetSUnlockedName, B.getInt8PtrTy(),
1191                             B.getInt8PtrTy(), B.getInt32Ty(), File->getType());
1192  inferLibFuncAttributes(M, FGetSUnlockedName, *TLI);
1193  CallInst *CI =
1194      B.CreateCall(F, {castToCStr(Str, B), Size, File}, FGetSUnlockedName);
1195
1196  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
1197    CI->setCallingConv(Fn->getCallingConv());
1198  return CI;
1199}
1200
1201Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
1202                               IRBuilder<> &B, const DataLayout &DL,
1203                               const TargetLibraryInfo *TLI) {
1204  if (!TLI->has(LibFunc_fread_unlocked))
1205    return nullptr;
1206
1207  Module *M = B.GetInsertBlock()->getModule();
1208  LLVMContext &Context = B.GetInsertBlock()->getContext();
1209  StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked);
1210  Constant *F = M->getOrInsertFunction(
1211      FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
1212      DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
1213
1214  if (File->getType()->isPointerTy())
1215    inferLibFuncAttributes(M, FReadUnlockedName, *TLI);
1216  CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
1217
1218  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
1219    CI->setCallingConv(Fn->getCallingConv());
1220  return CI;
1221}
1222