]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r302418, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Transforms / Utils / BuildLibCalls.cpp
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
27 using namespace llvm;
28
29 #define DEBUG_TYPE "build-libcalls"
30
31 //- Infer Attributes ---------------------------------------------------------//
32
33 STATISTIC(NumReadNone, "Number of functions inferred as readnone");
34 STATISTIC(NumReadOnly, "Number of functions inferred as readonly");
35 STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly");
36 STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind");
37 STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture");
38 STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly");
39 STATISTIC(NumNoAlias, "Number of function returns inferred as noalias");
40 STATISTIC(NumNonNull, "Number of function returns inferred as nonnull returns");
41
42 static bool setDoesNotAccessMemory(Function &F) {
43   if (F.doesNotAccessMemory())
44     return false;
45   F.setDoesNotAccessMemory();
46   ++NumReadNone;
47   return true;
48 }
49
50 static bool setOnlyReadsMemory(Function &F) {
51   if (F.onlyReadsMemory())
52     return false;
53   F.setOnlyReadsMemory();
54   ++NumReadOnly;
55   return true;
56 }
57
58 static bool setOnlyAccessesArgMemory(Function &F) {
59   if (F.onlyAccessesArgMemory())
60     return false;
61   F.setOnlyAccessesArgMemory();
62   ++NumArgMemOnly;
63   return true;
64 }
65
66 static bool setDoesNotThrow(Function &F) {
67   if (F.doesNotThrow())
68     return false;
69   F.setDoesNotThrow();
70   ++NumNoUnwind;
71   return true;
72 }
73
74 static 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
82 static 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
90 static 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
98 static 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
108 bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
109   LibFunc TheLibFunc;
110   if (!(TLI.getLibFunc(F, TheLibFunc) && TLI.has(TheLibFunc)))
111     return false;
112
113   bool Changed = false;
114   switch (TheLibFunc) {
115   case LibFunc_strlen:
116   case LibFunc_wcslen:
117     Changed |= setOnlyReadsMemory(F);
118     Changed |= setDoesNotThrow(F);
119     Changed |= setDoesNotCapture(F, 0);
120     return Changed;
121   case LibFunc_strchr:
122   case LibFunc_strrchr:
123     Changed |= setOnlyReadsMemory(F);
124     Changed |= setDoesNotThrow(F);
125     return Changed;
126   case LibFunc_strtol:
127   case LibFunc_strtod:
128   case LibFunc_strtof:
129   case LibFunc_strtoul:
130   case LibFunc_strtoll:
131   case LibFunc_strtold:
132   case LibFunc_strtoull:
133     Changed |= setDoesNotThrow(F);
134     Changed |= setDoesNotCapture(F, 1);
135     Changed |= setOnlyReadsMemory(F, 0);
136     return Changed;
137   case LibFunc_strcpy:
138   case LibFunc_stpcpy:
139   case LibFunc_strcat:
140   case LibFunc_strncat:
141   case LibFunc_strncpy:
142   case LibFunc_stpncpy:
143     Changed |= setDoesNotThrow(F);
144     Changed |= setDoesNotCapture(F, 1);
145     Changed |= setOnlyReadsMemory(F, 1);
146     return Changed;
147   case LibFunc_strxfrm:
148     Changed |= setDoesNotThrow(F);
149     Changed |= setDoesNotCapture(F, 0);
150     Changed |= setDoesNotCapture(F, 1);
151     Changed |= setOnlyReadsMemory(F, 1);
152     return Changed;
153   case LibFunc_strcmp:      // 0,1
154   case LibFunc_strspn:      // 0,1
155   case LibFunc_strncmp:     // 0,1
156   case LibFunc_strcspn:     // 0,1
157   case LibFunc_strcoll:     // 0,1
158   case LibFunc_strcasecmp:  // 0,1
159   case LibFunc_strncasecmp: //
160     Changed |= setOnlyReadsMemory(F);
161     Changed |= setDoesNotThrow(F);
162     Changed |= setDoesNotCapture(F, 0);
163     Changed |= setDoesNotCapture(F, 1);
164     return Changed;
165   case LibFunc_strstr:
166   case LibFunc_strpbrk:
167     Changed |= setOnlyReadsMemory(F);
168     Changed |= setDoesNotThrow(F);
169     Changed |= setDoesNotCapture(F, 1);
170     return Changed;
171   case LibFunc_strtok:
172   case LibFunc_strtok_r:
173     Changed |= setDoesNotThrow(F);
174     Changed |= setDoesNotCapture(F, 1);
175     Changed |= setOnlyReadsMemory(F, 1);
176     return Changed;
177   case LibFunc_scanf:
178     Changed |= setDoesNotThrow(F);
179     Changed |= setDoesNotCapture(F, 0);
180     Changed |= setOnlyReadsMemory(F, 0);
181     return Changed;
182   case LibFunc_setbuf:
183   case LibFunc_setvbuf:
184     Changed |= setDoesNotThrow(F);
185     Changed |= setDoesNotCapture(F, 0);
186     return Changed;
187   case LibFunc_strdup:
188   case LibFunc_strndup:
189     Changed |= setDoesNotThrow(F);
190     Changed |= setRetDoesNotAlias(F);
191     Changed |= setDoesNotCapture(F, 0);
192     Changed |= setOnlyReadsMemory(F, 0);
193     return Changed;
194   case LibFunc_stat:
195   case LibFunc_statvfs:
196     Changed |= setDoesNotThrow(F);
197     Changed |= setDoesNotCapture(F, 0);
198     Changed |= setDoesNotCapture(F, 1);
199     Changed |= setOnlyReadsMemory(F, 0);
200     return Changed;
201   case LibFunc_sscanf:
202     Changed |= setDoesNotThrow(F);
203     Changed |= setDoesNotCapture(F, 0);
204     Changed |= setDoesNotCapture(F, 1);
205     Changed |= setOnlyReadsMemory(F, 0);
206     Changed |= setOnlyReadsMemory(F, 1);
207     return Changed;
208   case LibFunc_sprintf:
209     Changed |= setDoesNotThrow(F);
210     Changed |= setDoesNotCapture(F, 0);
211     Changed |= setDoesNotCapture(F, 1);
212     Changed |= setOnlyReadsMemory(F, 1);
213     return Changed;
214   case LibFunc_snprintf:
215     Changed |= setDoesNotThrow(F);
216     Changed |= setDoesNotCapture(F, 0);
217     Changed |= setDoesNotCapture(F, 2);
218     Changed |= setOnlyReadsMemory(F, 2);
219     return Changed;
220   case LibFunc_setitimer:
221     Changed |= setDoesNotThrow(F);
222     Changed |= setDoesNotCapture(F, 1);
223     Changed |= setDoesNotCapture(F, 2);
224     Changed |= setOnlyReadsMemory(F, 1);
225     return Changed;
226   case LibFunc_system:
227     // May throw; "system" is a valid pthread cancellation point.
228     Changed |= setDoesNotCapture(F, 0);
229     Changed |= setOnlyReadsMemory(F, 0);
230     return Changed;
231   case LibFunc_malloc:
232     Changed |= setDoesNotThrow(F);
233     Changed |= setRetDoesNotAlias(F);
234     return Changed;
235   case LibFunc_memcmp:
236     Changed |= setOnlyReadsMemory(F);
237     Changed |= setDoesNotThrow(F);
238     Changed |= setDoesNotCapture(F, 0);
239     Changed |= setDoesNotCapture(F, 1);
240     return Changed;
241   case LibFunc_memchr:
242   case LibFunc_memrchr:
243     Changed |= setOnlyReadsMemory(F);
244     Changed |= setDoesNotThrow(F);
245     return Changed;
246   case LibFunc_modf:
247   case LibFunc_modff:
248   case LibFunc_modfl:
249     Changed |= setDoesNotThrow(F);
250     Changed |= setDoesNotCapture(F, 1);
251     return Changed;
252   case LibFunc_memcpy:
253   case LibFunc_mempcpy:
254   case LibFunc_memccpy:
255   case LibFunc_memmove:
256     Changed |= setDoesNotThrow(F);
257     Changed |= setDoesNotCapture(F, 1);
258     Changed |= setOnlyReadsMemory(F, 1);
259     return Changed;
260   case LibFunc_memcpy_chk:
261     Changed |= setDoesNotThrow(F);
262     return Changed;
263   case LibFunc_memalign:
264     Changed |= setRetDoesNotAlias(F);
265     return Changed;
266   case LibFunc_mkdir:
267     Changed |= setDoesNotThrow(F);
268     Changed |= setDoesNotCapture(F, 0);
269     Changed |= setOnlyReadsMemory(F, 0);
270     return Changed;
271   case LibFunc_mktime:
272     Changed |= setDoesNotThrow(F);
273     Changed |= setDoesNotCapture(F, 0);
274     return Changed;
275   case LibFunc_realloc:
276     Changed |= setDoesNotThrow(F);
277     Changed |= setRetDoesNotAlias(F);
278     Changed |= setDoesNotCapture(F, 0);
279     return Changed;
280   case LibFunc_read:
281     // May throw; "read" is a valid pthread cancellation point.
282     Changed |= setDoesNotCapture(F, 1);
283     return Changed;
284   case LibFunc_rewind:
285     Changed |= setDoesNotThrow(F);
286     Changed |= setDoesNotCapture(F, 0);
287     return Changed;
288   case LibFunc_rmdir:
289   case LibFunc_remove:
290   case LibFunc_realpath:
291     Changed |= setDoesNotThrow(F);
292     Changed |= setDoesNotCapture(F, 0);
293     Changed |= setOnlyReadsMemory(F, 0);
294     return Changed;
295   case LibFunc_rename:
296     Changed |= setDoesNotThrow(F);
297     Changed |= setDoesNotCapture(F, 0);
298     Changed |= setDoesNotCapture(F, 1);
299     Changed |= setOnlyReadsMemory(F, 0);
300     Changed |= setOnlyReadsMemory(F, 1);
301     return Changed;
302   case LibFunc_readlink:
303     Changed |= setDoesNotThrow(F);
304     Changed |= setDoesNotCapture(F, 0);
305     Changed |= setDoesNotCapture(F, 1);
306     Changed |= setOnlyReadsMemory(F, 0);
307     return Changed;
308   case LibFunc_write:
309     // May throw; "write" is a valid pthread cancellation point.
310     Changed |= setDoesNotCapture(F, 1);
311     Changed |= setOnlyReadsMemory(F, 1);
312     return Changed;
313   case LibFunc_bcopy:
314     Changed |= setDoesNotThrow(F);
315     Changed |= setDoesNotCapture(F, 0);
316     Changed |= setDoesNotCapture(F, 1);
317     Changed |= setOnlyReadsMemory(F, 0);
318     return Changed;
319   case LibFunc_bcmp:
320     Changed |= setDoesNotThrow(F);
321     Changed |= setOnlyReadsMemory(F);
322     Changed |= setDoesNotCapture(F, 0);
323     Changed |= setDoesNotCapture(F, 1);
324     return Changed;
325   case LibFunc_bzero:
326     Changed |= setDoesNotThrow(F);
327     Changed |= setDoesNotCapture(F, 0);
328     return Changed;
329   case LibFunc_calloc:
330     Changed |= setDoesNotThrow(F);
331     Changed |= setRetDoesNotAlias(F);
332     return Changed;
333   case LibFunc_chmod:
334   case LibFunc_chown:
335     Changed |= setDoesNotThrow(F);
336     Changed |= setDoesNotCapture(F, 0);
337     Changed |= setOnlyReadsMemory(F, 0);
338     return Changed;
339   case LibFunc_ctermid:
340   case LibFunc_clearerr:
341   case LibFunc_closedir:
342     Changed |= setDoesNotThrow(F);
343     Changed |= setDoesNotCapture(F, 0);
344     return Changed;
345   case LibFunc_atoi:
346   case LibFunc_atol:
347   case LibFunc_atof:
348   case LibFunc_atoll:
349     Changed |= setDoesNotThrow(F);
350     Changed |= setOnlyReadsMemory(F);
351     Changed |= setDoesNotCapture(F, 0);
352     return Changed;
353   case LibFunc_access:
354     Changed |= setDoesNotThrow(F);
355     Changed |= setDoesNotCapture(F, 0);
356     Changed |= setOnlyReadsMemory(F, 0);
357     return Changed;
358   case LibFunc_fopen:
359     Changed |= setDoesNotThrow(F);
360     Changed |= setRetDoesNotAlias(F);
361     Changed |= setDoesNotCapture(F, 0);
362     Changed |= setDoesNotCapture(F, 1);
363     Changed |= setOnlyReadsMemory(F, 0);
364     Changed |= setOnlyReadsMemory(F, 1);
365     return Changed;
366   case LibFunc_fdopen:
367     Changed |= setDoesNotThrow(F);
368     Changed |= setRetDoesNotAlias(F);
369     Changed |= setDoesNotCapture(F, 1);
370     Changed |= setOnlyReadsMemory(F, 1);
371     return Changed;
372   case LibFunc_feof:
373   case LibFunc_free:
374   case LibFunc_fseek:
375   case LibFunc_ftell:
376   case LibFunc_fgetc:
377   case LibFunc_fseeko:
378   case LibFunc_ftello:
379   case LibFunc_fileno:
380   case LibFunc_fflush:
381   case LibFunc_fclose:
382   case LibFunc_fsetpos:
383   case LibFunc_flockfile:
384   case LibFunc_funlockfile:
385   case LibFunc_ftrylockfile:
386     Changed |= setDoesNotThrow(F);
387     Changed |= setDoesNotCapture(F, 0);
388     return Changed;
389   case LibFunc_ferror:
390     Changed |= setDoesNotThrow(F);
391     Changed |= setDoesNotCapture(F, 0);
392     Changed |= setOnlyReadsMemory(F);
393     return Changed;
394   case LibFunc_fputc:
395   case LibFunc_fstat:
396   case LibFunc_frexp:
397   case LibFunc_frexpf:
398   case LibFunc_frexpl:
399   case LibFunc_fstatvfs:
400     Changed |= setDoesNotThrow(F);
401     Changed |= setDoesNotCapture(F, 1);
402     return Changed;
403   case LibFunc_fgets:
404     Changed |= setDoesNotThrow(F);
405     Changed |= setDoesNotCapture(F, 2);
406     return Changed;
407   case LibFunc_fread:
408     Changed |= setDoesNotThrow(F);
409     Changed |= setDoesNotCapture(F, 0);
410     Changed |= setDoesNotCapture(F, 3);
411     return Changed;
412   case LibFunc_fwrite:
413     Changed |= setDoesNotThrow(F);
414     Changed |= setDoesNotCapture(F, 0);
415     Changed |= setDoesNotCapture(F, 3);
416     // FIXME: readonly #1?
417     return Changed;
418   case LibFunc_fputs:
419     Changed |= setDoesNotThrow(F);
420     Changed |= setDoesNotCapture(F, 0);
421     Changed |= setDoesNotCapture(F, 1);
422     Changed |= setOnlyReadsMemory(F, 0);
423     return Changed;
424   case LibFunc_fscanf:
425   case LibFunc_fprintf:
426     Changed |= setDoesNotThrow(F);
427     Changed |= setDoesNotCapture(F, 0);
428     Changed |= setDoesNotCapture(F, 1);
429     Changed |= setOnlyReadsMemory(F, 1);
430     return Changed;
431   case LibFunc_fgetpos:
432     Changed |= setDoesNotThrow(F);
433     Changed |= setDoesNotCapture(F, 0);
434     Changed |= setDoesNotCapture(F, 1);
435     return Changed;
436   case LibFunc_getc:
437   case LibFunc_getlogin_r:
438   case LibFunc_getc_unlocked:
439     Changed |= setDoesNotThrow(F);
440     Changed |= setDoesNotCapture(F, 0);
441     return Changed;
442   case LibFunc_getenv:
443     Changed |= setDoesNotThrow(F);
444     Changed |= setOnlyReadsMemory(F);
445     Changed |= setDoesNotCapture(F, 0);
446     return Changed;
447   case LibFunc_gets:
448   case LibFunc_getchar:
449     Changed |= setDoesNotThrow(F);
450     return Changed;
451   case LibFunc_getitimer:
452     Changed |= setDoesNotThrow(F);
453     Changed |= setDoesNotCapture(F, 1);
454     return Changed;
455   case LibFunc_getpwnam:
456     Changed |= setDoesNotThrow(F);
457     Changed |= setDoesNotCapture(F, 0);
458     Changed |= setOnlyReadsMemory(F, 0);
459     return Changed;
460   case LibFunc_ungetc:
461     Changed |= setDoesNotThrow(F);
462     Changed |= setDoesNotCapture(F, 1);
463     return Changed;
464   case LibFunc_uname:
465     Changed |= setDoesNotThrow(F);
466     Changed |= setDoesNotCapture(F, 0);
467     return Changed;
468   case LibFunc_unlink:
469     Changed |= setDoesNotThrow(F);
470     Changed |= setDoesNotCapture(F, 0);
471     Changed |= setOnlyReadsMemory(F, 0);
472     return Changed;
473   case LibFunc_unsetenv:
474     Changed |= setDoesNotThrow(F);
475     Changed |= setDoesNotCapture(F, 0);
476     Changed |= setOnlyReadsMemory(F, 0);
477     return Changed;
478   case LibFunc_utime:
479   case LibFunc_utimes:
480     Changed |= setDoesNotThrow(F);
481     Changed |= setDoesNotCapture(F, 0);
482     Changed |= setDoesNotCapture(F, 1);
483     Changed |= setOnlyReadsMemory(F, 0);
484     Changed |= setOnlyReadsMemory(F, 1);
485     return Changed;
486   case LibFunc_putc:
487     Changed |= setDoesNotThrow(F);
488     Changed |= setDoesNotCapture(F, 1);
489     return Changed;
490   case LibFunc_puts:
491   case LibFunc_printf:
492   case LibFunc_perror:
493     Changed |= setDoesNotThrow(F);
494     Changed |= setDoesNotCapture(F, 0);
495     Changed |= setOnlyReadsMemory(F, 0);
496     return Changed;
497   case LibFunc_pread:
498     // May throw; "pread" is a valid pthread cancellation point.
499     Changed |= setDoesNotCapture(F, 1);
500     return Changed;
501   case LibFunc_pwrite:
502     // May throw; "pwrite" is a valid pthread cancellation point.
503     Changed |= setDoesNotCapture(F, 1);
504     Changed |= setOnlyReadsMemory(F, 1);
505     return Changed;
506   case LibFunc_putchar:
507     Changed |= setDoesNotThrow(F);
508     return Changed;
509   case LibFunc_popen:
510     Changed |= setDoesNotThrow(F);
511     Changed |= setRetDoesNotAlias(F);
512     Changed |= setDoesNotCapture(F, 0);
513     Changed |= setDoesNotCapture(F, 1);
514     Changed |= setOnlyReadsMemory(F, 0);
515     Changed |= setOnlyReadsMemory(F, 1);
516     return Changed;
517   case LibFunc_pclose:
518     Changed |= setDoesNotThrow(F);
519     Changed |= setDoesNotCapture(F, 0);
520     return Changed;
521   case LibFunc_vscanf:
522     Changed |= setDoesNotThrow(F);
523     Changed |= setDoesNotCapture(F, 0);
524     Changed |= setOnlyReadsMemory(F, 0);
525     return Changed;
526   case LibFunc_vsscanf:
527     Changed |= setDoesNotThrow(F);
528     Changed |= setDoesNotCapture(F, 0);
529     Changed |= setDoesNotCapture(F, 1);
530     Changed |= setOnlyReadsMemory(F, 0);
531     Changed |= setOnlyReadsMemory(F, 1);
532     return Changed;
533   case LibFunc_vfscanf:
534     Changed |= setDoesNotThrow(F);
535     Changed |= setDoesNotCapture(F, 0);
536     Changed |= setDoesNotCapture(F, 1);
537     Changed |= setOnlyReadsMemory(F, 1);
538     return Changed;
539   case LibFunc_valloc:
540     Changed |= setDoesNotThrow(F);
541     Changed |= setRetDoesNotAlias(F);
542     return Changed;
543   case LibFunc_vprintf:
544     Changed |= setDoesNotThrow(F);
545     Changed |= setDoesNotCapture(F, 0);
546     Changed |= setOnlyReadsMemory(F, 0);
547     return Changed;
548   case LibFunc_vfprintf:
549   case LibFunc_vsprintf:
550     Changed |= setDoesNotThrow(F);
551     Changed |= setDoesNotCapture(F, 0);
552     Changed |= setDoesNotCapture(F, 1);
553     Changed |= setOnlyReadsMemory(F, 1);
554     return Changed;
555   case LibFunc_vsnprintf:
556     Changed |= setDoesNotThrow(F);
557     Changed |= setDoesNotCapture(F, 0);
558     Changed |= setDoesNotCapture(F, 2);
559     Changed |= setOnlyReadsMemory(F, 2);
560     return Changed;
561   case LibFunc_open:
562     // May throw; "open" is a valid pthread cancellation point.
563     Changed |= setDoesNotCapture(F, 0);
564     Changed |= setOnlyReadsMemory(F, 0);
565     return Changed;
566   case LibFunc_opendir:
567     Changed |= setDoesNotThrow(F);
568     Changed |= setRetDoesNotAlias(F);
569     Changed |= setDoesNotCapture(F, 0);
570     Changed |= setOnlyReadsMemory(F, 0);
571     return Changed;
572   case LibFunc_tmpfile:
573     Changed |= setDoesNotThrow(F);
574     Changed |= setRetDoesNotAlias(F);
575     return Changed;
576   case LibFunc_times:
577     Changed |= setDoesNotThrow(F);
578     Changed |= setDoesNotCapture(F, 0);
579     return Changed;
580   case LibFunc_htonl:
581   case LibFunc_htons:
582   case LibFunc_ntohl:
583   case LibFunc_ntohs:
584     Changed |= setDoesNotThrow(F);
585     Changed |= setDoesNotAccessMemory(F);
586     return Changed;
587   case LibFunc_lstat:
588     Changed |= setDoesNotThrow(F);
589     Changed |= setDoesNotCapture(F, 0);
590     Changed |= setDoesNotCapture(F, 1);
591     Changed |= setOnlyReadsMemory(F, 0);
592     return Changed;
593   case LibFunc_lchown:
594     Changed |= setDoesNotThrow(F);
595     Changed |= setDoesNotCapture(F, 0);
596     Changed |= setOnlyReadsMemory(F, 0);
597     return Changed;
598   case LibFunc_qsort:
599     // May throw; places call through function pointer.
600     Changed |= setDoesNotCapture(F, 3);
601     return Changed;
602   case LibFunc_dunder_strdup:
603   case LibFunc_dunder_strndup:
604     Changed |= setDoesNotThrow(F);
605     Changed |= setRetDoesNotAlias(F);
606     Changed |= setDoesNotCapture(F, 0);
607     Changed |= setOnlyReadsMemory(F, 0);
608     return Changed;
609   case LibFunc_dunder_strtok_r:
610     Changed |= setDoesNotThrow(F);
611     Changed |= setDoesNotCapture(F, 1);
612     Changed |= setOnlyReadsMemory(F, 1);
613     return Changed;
614   case LibFunc_under_IO_getc:
615     Changed |= setDoesNotThrow(F);
616     Changed |= setDoesNotCapture(F, 0);
617     return Changed;
618   case LibFunc_under_IO_putc:
619     Changed |= setDoesNotThrow(F);
620     Changed |= setDoesNotCapture(F, 1);
621     return Changed;
622   case LibFunc_dunder_isoc99_scanf:
623     Changed |= setDoesNotThrow(F);
624     Changed |= setDoesNotCapture(F, 0);
625     Changed |= setOnlyReadsMemory(F, 0);
626     return Changed;
627   case LibFunc_stat64:
628   case LibFunc_lstat64:
629   case LibFunc_statvfs64:
630     Changed |= setDoesNotThrow(F);
631     Changed |= setDoesNotCapture(F, 0);
632     Changed |= setDoesNotCapture(F, 1);
633     Changed |= setOnlyReadsMemory(F, 0);
634     return Changed;
635   case LibFunc_dunder_isoc99_sscanf:
636     Changed |= setDoesNotThrow(F);
637     Changed |= setDoesNotCapture(F, 0);
638     Changed |= setDoesNotCapture(F, 1);
639     Changed |= setOnlyReadsMemory(F, 0);
640     Changed |= setOnlyReadsMemory(F, 1);
641     return Changed;
642   case LibFunc_fopen64:
643     Changed |= setDoesNotThrow(F);
644     Changed |= setRetDoesNotAlias(F);
645     Changed |= setDoesNotCapture(F, 0);
646     Changed |= setDoesNotCapture(F, 1);
647     Changed |= setOnlyReadsMemory(F, 0);
648     Changed |= setOnlyReadsMemory(F, 1);
649     return Changed;
650   case LibFunc_fseeko64:
651   case LibFunc_ftello64:
652     Changed |= setDoesNotThrow(F);
653     Changed |= setDoesNotCapture(F, 0);
654     return Changed;
655   case LibFunc_tmpfile64:
656     Changed |= setDoesNotThrow(F);
657     Changed |= setRetDoesNotAlias(F);
658     return Changed;
659   case LibFunc_fstat64:
660   case LibFunc_fstatvfs64:
661     Changed |= setDoesNotThrow(F);
662     Changed |= setDoesNotCapture(F, 1);
663     return Changed;
664   case LibFunc_open64:
665     // May throw; "open" is a valid pthread cancellation point.
666     Changed |= setDoesNotCapture(F, 0);
667     Changed |= setOnlyReadsMemory(F, 0);
668     return Changed;
669   case LibFunc_gettimeofday:
670     // Currently some platforms have the restrict keyword on the arguments to
671     // gettimeofday. To be conservative, do not add noalias to gettimeofday's
672     // arguments.
673     Changed |= setDoesNotThrow(F);
674     Changed |= setDoesNotCapture(F, 0);
675     Changed |= setDoesNotCapture(F, 1);
676     return Changed;
677   case LibFunc_Znwj: // new(unsigned int)
678   case LibFunc_Znwm: // new(unsigned long)
679   case LibFunc_Znaj: // new[](unsigned int)
680   case LibFunc_Znam: // new[](unsigned long)
681   case LibFunc_msvc_new_int: // new(unsigned int)
682   case LibFunc_msvc_new_longlong: // new(unsigned long long)
683   case LibFunc_msvc_new_array_int: // new[](unsigned int)
684   case LibFunc_msvc_new_array_longlong: // new[](unsigned long long)
685     // Operator new always returns a nonnull noalias pointer
686     Changed |= setRetNonNull(F);
687     Changed |= setRetDoesNotAlias(F);
688     return Changed;
689   //TODO: add LibFunc entries for:
690   //case LibFunc_memset_pattern4:
691   //case LibFunc_memset_pattern8:
692   case LibFunc_memset_pattern16:
693     Changed |= setOnlyAccessesArgMemory(F);
694     Changed |= setDoesNotCapture(F, 0);
695     Changed |= setDoesNotCapture(F, 1);
696     Changed |= setOnlyReadsMemory(F, 1);
697     return Changed;
698   // int __nvvm_reflect(const char *)
699   case LibFunc_nvvm_reflect:
700     Changed |= setDoesNotAccessMemory(F);
701     Changed |= setDoesNotThrow(F);
702     return Changed;
703
704   default:
705     // FIXME: It'd be really nice to cover all the library functions we're
706     // aware of here.
707     return false;
708   }
709 }
710
711 //- Emit LibCalls ------------------------------------------------------------//
712
713 Value *llvm::castToCStr(Value *V, IRBuilder<> &B) {
714   unsigned AS = V->getType()->getPointerAddressSpace();
715   return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
716 }
717
718 Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
719                         const TargetLibraryInfo *TLI) {
720   if (!TLI->has(LibFunc_strlen))
721     return nullptr;
722
723   Module *M = B.GetInsertBlock()->getModule();
724   LLVMContext &Context = B.GetInsertBlock()->getContext();
725   Constant *StrLen = M->getOrInsertFunction("strlen", DL.getIntPtrType(Context),
726                                             B.getInt8PtrTy());
727   inferLibFuncAttributes(*M->getFunction("strlen"), *TLI);
728   CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), "strlen");
729   if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
730     CI->setCallingConv(F->getCallingConv());
731
732   return CI;
733 }
734
735 Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
736                         const TargetLibraryInfo *TLI) {
737   if (!TLI->has(LibFunc_strchr))
738     return nullptr;
739
740   Module *M = B.GetInsertBlock()->getModule();
741   Type *I8Ptr = B.getInt8PtrTy();
742   Type *I32Ty = B.getInt32Ty();
743   Constant *StrChr =
744       M->getOrInsertFunction("strchr", I8Ptr, I8Ptr, I32Ty);
745   inferLibFuncAttributes(*M->getFunction("strchr"), *TLI);
746   CallInst *CI = B.CreateCall(
747       StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
748   if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
749     CI->setCallingConv(F->getCallingConv());
750   return CI;
751 }
752
753 Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
754                          const DataLayout &DL, const TargetLibraryInfo *TLI) {
755   if (!TLI->has(LibFunc_strncmp))
756     return nullptr;
757
758   Module *M = B.GetInsertBlock()->getModule();
759   LLVMContext &Context = B.GetInsertBlock()->getContext();
760   Value *StrNCmp = M->getOrInsertFunction("strncmp", B.getInt32Ty(),
761                                           B.getInt8PtrTy(), B.getInt8PtrTy(),
762                                           DL.getIntPtrType(Context));
763   inferLibFuncAttributes(*M->getFunction("strncmp"), *TLI);
764   CallInst *CI = B.CreateCall(
765       StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "strncmp");
766
767   if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
768     CI->setCallingConv(F->getCallingConv());
769
770   return CI;
771 }
772
773 Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
774                         const TargetLibraryInfo *TLI, StringRef Name) {
775   if (!TLI->has(LibFunc_strcpy))
776     return nullptr;
777
778   Module *M = B.GetInsertBlock()->getModule();
779   Type *I8Ptr = B.getInt8PtrTy();
780   Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
781   inferLibFuncAttributes(*M->getFunction(Name), *TLI);
782   CallInst *CI =
783       B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
784   if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
785     CI->setCallingConv(F->getCallingConv());
786   return CI;
787 }
788
789 Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
790                          const TargetLibraryInfo *TLI, StringRef Name) {
791   if (!TLI->has(LibFunc_strncpy))
792     return nullptr;
793
794   Module *M = B.GetInsertBlock()->getModule();
795   Type *I8Ptr = B.getInt8PtrTy();
796   Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr,
797                                           Len->getType());
798   inferLibFuncAttributes(*M->getFunction(Name), *TLI);
799   CallInst *CI = B.CreateCall(
800       StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, "strncpy");
801   if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
802     CI->setCallingConv(F->getCallingConv());
803   return CI;
804 }
805
806 Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
807                            IRBuilder<> &B, const DataLayout &DL,
808                            const TargetLibraryInfo *TLI) {
809   if (!TLI->has(LibFunc_memcpy_chk))
810     return nullptr;
811
812   Module *M = B.GetInsertBlock()->getModule();
813   AttributeList AS;
814   AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex,
815                           Attribute::NoUnwind);
816   LLVMContext &Context = B.GetInsertBlock()->getContext();
817   Value *MemCpy = M->getOrInsertFunction(
818       "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
819       B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
820       DL.getIntPtrType(Context));
821   Dst = castToCStr(Dst, B);
822   Src = castToCStr(Src, B);
823   CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
824   if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
825     CI->setCallingConv(F->getCallingConv());
826   return CI;
827 }
828
829 Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
830                         const DataLayout &DL, const TargetLibraryInfo *TLI) {
831   if (!TLI->has(LibFunc_memchr))
832     return nullptr;
833
834   Module *M = B.GetInsertBlock()->getModule();
835   LLVMContext &Context = B.GetInsertBlock()->getContext();
836   Value *MemChr = M->getOrInsertFunction("memchr", B.getInt8PtrTy(),
837                                          B.getInt8PtrTy(), B.getInt32Ty(),
838                                          DL.getIntPtrType(Context));
839   inferLibFuncAttributes(*M->getFunction("memchr"), *TLI);
840   CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, "memchr");
841
842   if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
843     CI->setCallingConv(F->getCallingConv());
844
845   return CI;
846 }
847
848 Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
849                         const DataLayout &DL, const TargetLibraryInfo *TLI) {
850   if (!TLI->has(LibFunc_memcmp))
851     return nullptr;
852
853   Module *M = B.GetInsertBlock()->getModule();
854   LLVMContext &Context = B.GetInsertBlock()->getContext();
855   Value *MemCmp = M->getOrInsertFunction("memcmp", B.getInt32Ty(),
856                                          B.getInt8PtrTy(), B.getInt8PtrTy(),
857                                          DL.getIntPtrType(Context));
858   inferLibFuncAttributes(*M->getFunction("memcmp"), *TLI);
859   CallInst *CI = B.CreateCall(
860       MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "memcmp");
861
862   if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
863     CI->setCallingConv(F->getCallingConv());
864
865   return CI;
866 }
867
868 /// Append a suffix to the function name according to the type of 'Op'.
869 static void appendTypeSuffix(Value *Op, StringRef &Name,
870                              SmallString<20> &NameBuffer) {
871   if (!Op->getType()->isDoubleTy()) {
872       NameBuffer += Name;
873
874     if (Op->getType()->isFloatTy())
875       NameBuffer += 'f';
876     else
877       NameBuffer += 'l';
878
879     Name = NameBuffer;
880   }  
881 }
882
883 Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
884                                   const AttributeList &Attrs) {
885   SmallString<20> NameBuffer;
886   appendTypeSuffix(Op, Name, NameBuffer);
887
888   Module *M = B.GetInsertBlock()->getModule();
889   Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
890                                          Op->getType());
891   CallInst *CI = B.CreateCall(Callee, Op, Name);
892
893   // The incoming attribute set may have come from a speculatable intrinsic, but
894   // is being replaced with a library call which is not allowed to be
895   // speculatable.
896   CI->setAttributes(Attrs.removeAttribute(B.getContext(),
897                                           AttributeList::FunctionIndex,
898                                           Attribute::Speculatable));
899   if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
900     CI->setCallingConv(F->getCallingConv());
901
902   return CI;
903 }
904
905 Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
906                                    IRBuilder<> &B, const AttributeList &Attrs) {
907   SmallString<20> NameBuffer;
908   appendTypeSuffix(Op1, Name, NameBuffer);
909
910   Module *M = B.GetInsertBlock()->getModule();
911   Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(),
912                                          Op2->getType());
913   CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
914   CI->setAttributes(Attrs);
915   if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
916     CI->setCallingConv(F->getCallingConv());
917
918   return CI;
919 }
920
921 Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B,
922                          const TargetLibraryInfo *TLI) {
923   if (!TLI->has(LibFunc_putchar))
924     return nullptr;
925
926   Module *M = B.GetInsertBlock()->getModule();
927   Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(), B.getInt32Ty());
928   inferLibFuncAttributes(*M->getFunction("putchar"), *TLI);
929   CallInst *CI = B.CreateCall(PutChar,
930                               B.CreateIntCast(Char,
931                               B.getInt32Ty(),
932                               /*isSigned*/true,
933                               "chari"),
934                               "putchar");
935
936   if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
937     CI->setCallingConv(F->getCallingConv());
938   return CI;
939 }
940
941 Value *llvm::emitPutS(Value *Str, IRBuilder<> &B,
942                       const TargetLibraryInfo *TLI) {
943   if (!TLI->has(LibFunc_puts))
944     return nullptr;
945
946   Module *M = B.GetInsertBlock()->getModule();
947   Value *PutS =
948       M->getOrInsertFunction("puts", B.getInt32Ty(), B.getInt8PtrTy());
949   inferLibFuncAttributes(*M->getFunction("puts"), *TLI);
950   CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), "puts");
951   if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
952     CI->setCallingConv(F->getCallingConv());
953   return CI;
954 }
955
956 Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
957                        const TargetLibraryInfo *TLI) {
958   if (!TLI->has(LibFunc_fputc))
959     return nullptr;
960
961   Module *M = B.GetInsertBlock()->getModule();
962   Constant *F = M->getOrInsertFunction("fputc", B.getInt32Ty(), B.getInt32Ty(),
963                                        File->getType());
964   if (File->getType()->isPointerTy())
965     inferLibFuncAttributes(*M->getFunction("fputc"), *TLI);
966   Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
967                          "chari");
968   CallInst *CI = B.CreateCall(F, {Char, File}, "fputc");
969
970   if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
971     CI->setCallingConv(Fn->getCallingConv());
972   return CI;
973 }
974
975 Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
976                        const TargetLibraryInfo *TLI) {
977   if (!TLI->has(LibFunc_fputs))
978     return nullptr;
979
980   Module *M = B.GetInsertBlock()->getModule();
981   StringRef FPutsName = TLI->getName(LibFunc_fputs);
982   Constant *F = M->getOrInsertFunction(
983       FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType());
984   if (File->getType()->isPointerTy())
985     inferLibFuncAttributes(*M->getFunction(FPutsName), *TLI);
986   CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs");
987
988   if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
989     CI->setCallingConv(Fn->getCallingConv());
990   return CI;
991 }
992
993 Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
994                         const DataLayout &DL, const TargetLibraryInfo *TLI) {
995   if (!TLI->has(LibFunc_fwrite))
996     return nullptr;
997
998   Module *M = B.GetInsertBlock()->getModule();
999   LLVMContext &Context = B.GetInsertBlock()->getContext();
1000   StringRef FWriteName = TLI->getName(LibFunc_fwrite);
1001   Constant *F = M->getOrInsertFunction(
1002       FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
1003       DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
1004
1005   if (File->getType()->isPointerTy())
1006     inferLibFuncAttributes(*M->getFunction(FWriteName), *TLI);
1007   CallInst *CI =
1008       B.CreateCall(F, {castToCStr(Ptr, B), Size,
1009                        ConstantInt::get(DL.getIntPtrType(Context), 1), File});
1010
1011   if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
1012     CI->setCallingConv(Fn->getCallingConv());
1013   return CI;
1014 }