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