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