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