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