1 //===- InferFunctionAttrs.cpp - Infer implicit function attributes --------===//
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 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
11 #include "llvm/ADT/Statistic.h"
12 #include "llvm/Analysis/TargetLibraryInfo.h"
13 #include "llvm/Analysis/MemoryBuiltins.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/OptBisect.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "inferattrs"
23 
24 STATISTIC(NumReadNone, "Number of functions inferred as readnone");
25 STATISTIC(NumReadOnly, "Number of functions inferred as readonly");
26 STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly");
27 STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind");
28 STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture");
29 STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly");
30 STATISTIC(NumNoAlias, "Number of function returns inferred as noalias");
31 STATISTIC(NumNonNull, "Number of function returns inferred as nonnull returns");
32 
33 static bool setDoesNotAccessMemory(Function &F) {
34   if (F.doesNotAccessMemory())
35     return false;
36   F.setDoesNotAccessMemory();
37   ++NumReadNone;
38   return true;
39 }
40 
41 static bool setOnlyReadsMemory(Function &F) {
42   if (F.onlyReadsMemory())
43     return false;
44   F.setOnlyReadsMemory();
45   ++NumReadOnly;
46   return true;
47 }
48 
49 static bool setOnlyAccessesArgMemory(Function &F) {
50   if (F.onlyAccessesArgMemory())
51     return false;
52   F.setOnlyAccessesArgMemory ();
53   ++NumArgMemOnly;
54   return true;
55 }
56 
57 
58 static bool setDoesNotThrow(Function &F) {
59   if (F.doesNotThrow())
60     return false;
61   F.setDoesNotThrow();
62   ++NumNoUnwind;
63   return true;
64 }
65 
66 static bool setDoesNotCapture(Function &F, unsigned n) {
67   if (F.doesNotCapture(n))
68     return false;
69   F.setDoesNotCapture(n);
70   ++NumNoCapture;
71   return true;
72 }
73 
74 static bool setOnlyReadsMemory(Function &F, unsigned n) {
75   if (F.onlyReadsMemory(n))
76     return false;
77   F.setOnlyReadsMemory(n);
78   ++NumReadOnlyArg;
79   return true;
80 }
81 
82 static bool setDoesNotAlias(Function &F, unsigned n) {
83   if (F.doesNotAlias(n))
84     return false;
85   F.setDoesNotAlias(n);
86   ++NumNoAlias;
87   return true;
88 }
89 
90 static bool setNonNull(Function &F, unsigned n) {
91   assert((n != AttributeSet::ReturnIndex ||
92           F.getReturnType()->isPointerTy()) &&
93          "nonnull applies only to pointers");
94   if (F.getAttributes().hasAttribute(n, Attribute::NonNull))
95     return false;
96   F.addAttribute(n, Attribute::NonNull);
97   ++NumNonNull;
98   return true;
99 }
100 
101 /// Analyze the name and prototype of the given function and set any applicable
102 /// attributes.
103 ///
104 /// Returns true if any attributes were set and false otherwise.
105 static bool inferPrototypeAttributes(Function &F,
106                                      const TargetLibraryInfo &TLI) {
107   if (F.hasFnAttribute(Attribute::OptimizeNone))
108     return false;
109 
110   FunctionType *FTy = F.getFunctionType();
111   LibFunc::Func TheLibFunc;
112   if (!(TLI.getLibFunc(F.getName(), TheLibFunc) && TLI.has(TheLibFunc)))
113     return false;
114 
115   bool Changed = false;
116   switch (TheLibFunc) {
117   case LibFunc::strlen:
118     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
119       return false;
120     Changed |= setOnlyReadsMemory(F);
121     Changed |= setDoesNotThrow(F);
122     Changed |= setDoesNotCapture(F, 1);
123     return Changed;
124   case LibFunc::strchr:
125   case LibFunc::strrchr:
126     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
127         !FTy->getParamType(1)->isIntegerTy())
128       return false;
129     Changed |= setOnlyReadsMemory(F);
130     Changed |= setDoesNotThrow(F);
131     return Changed;
132   case LibFunc::strtol:
133   case LibFunc::strtod:
134   case LibFunc::strtof:
135   case LibFunc::strtoul:
136   case LibFunc::strtoll:
137   case LibFunc::strtold:
138   case LibFunc::strtoull:
139     if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
140       return false;
141     Changed |= setDoesNotThrow(F);
142     Changed |= setDoesNotCapture(F, 2);
143     Changed |= setOnlyReadsMemory(F, 1);
144     return Changed;
145   case LibFunc::strcpy:
146   case LibFunc::stpcpy:
147   case LibFunc::strcat:
148   case LibFunc::strncat:
149   case LibFunc::strncpy:
150   case LibFunc::stpncpy:
151     if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
152       return false;
153     Changed |= setDoesNotThrow(F);
154     Changed |= setDoesNotCapture(F, 2);
155     Changed |= setOnlyReadsMemory(F, 2);
156     return Changed;
157   case LibFunc::strxfrm:
158     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
159         !FTy->getParamType(1)->isPointerTy())
160       return false;
161     Changed |= setDoesNotThrow(F);
162     Changed |= setDoesNotCapture(F, 1);
163     Changed |= setDoesNotCapture(F, 2);
164     Changed |= setOnlyReadsMemory(F, 2);
165     return Changed;
166   case LibFunc::strcmp:      // 0,1
167   case LibFunc::strspn:      // 0,1
168   case LibFunc::strncmp:     // 0,1
169   case LibFunc::strcspn:     // 0,1
170   case LibFunc::strcoll:     // 0,1
171   case LibFunc::strcasecmp:  // 0,1
172   case LibFunc::strncasecmp: //
173     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
174         !FTy->getParamType(1)->isPointerTy())
175       return false;
176     Changed |= setOnlyReadsMemory(F);
177     Changed |= setDoesNotThrow(F);
178     Changed |= setDoesNotCapture(F, 1);
179     Changed |= setDoesNotCapture(F, 2);
180     return Changed;
181   case LibFunc::strstr:
182   case LibFunc::strpbrk:
183     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
184       return false;
185     Changed |= setOnlyReadsMemory(F);
186     Changed |= setDoesNotThrow(F);
187     Changed |= setDoesNotCapture(F, 2);
188     return Changed;
189   case LibFunc::strtok:
190   case LibFunc::strtok_r:
191     if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
192       return false;
193     Changed |= setDoesNotThrow(F);
194     Changed |= setDoesNotCapture(F, 2);
195     Changed |= setOnlyReadsMemory(F, 2);
196     return Changed;
197   case LibFunc::scanf:
198     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
199       return false;
200     Changed |= setDoesNotThrow(F);
201     Changed |= setDoesNotCapture(F, 1);
202     Changed |= setOnlyReadsMemory(F, 1);
203     return Changed;
204   case LibFunc::setbuf:
205   case LibFunc::setvbuf:
206     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
207       return false;
208     Changed |= setDoesNotThrow(F);
209     Changed |= setDoesNotCapture(F, 1);
210     return Changed;
211   case LibFunc::strdup:
212   case LibFunc::strndup:
213     if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
214         !FTy->getParamType(0)->isPointerTy())
215       return false;
216     Changed |= setDoesNotThrow(F);
217     Changed |= setDoesNotAlias(F, 0);
218     Changed |= setDoesNotCapture(F, 1);
219     Changed |= setOnlyReadsMemory(F, 1);
220     return Changed;
221   case LibFunc::stat:
222   case LibFunc::statvfs:
223     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
224         !FTy->getParamType(1)->isPointerTy())
225       return false;
226     Changed |= setDoesNotThrow(F);
227     Changed |= setDoesNotCapture(F, 1);
228     Changed |= setDoesNotCapture(F, 2);
229     Changed |= setOnlyReadsMemory(F, 1);
230     return Changed;
231   case LibFunc::sscanf:
232     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
233         !FTy->getParamType(1)->isPointerTy())
234       return false;
235     Changed |= setDoesNotThrow(F);
236     Changed |= setDoesNotCapture(F, 1);
237     Changed |= setDoesNotCapture(F, 2);
238     Changed |= setOnlyReadsMemory(F, 1);
239     Changed |= setOnlyReadsMemory(F, 2);
240     return Changed;
241   case LibFunc::sprintf:
242     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
243         !FTy->getParamType(1)->isPointerTy())
244       return false;
245     Changed |= setDoesNotThrow(F);
246     Changed |= setDoesNotCapture(F, 1);
247     Changed |= setDoesNotCapture(F, 2);
248     Changed |= setOnlyReadsMemory(F, 2);
249     return Changed;
250   case LibFunc::snprintf:
251     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
252         !FTy->getParamType(2)->isPointerTy())
253       return false;
254     Changed |= setDoesNotThrow(F);
255     Changed |= setDoesNotCapture(F, 1);
256     Changed |= setDoesNotCapture(F, 3);
257     Changed |= setOnlyReadsMemory(F, 3);
258     return Changed;
259   case LibFunc::setitimer:
260     if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
261         !FTy->getParamType(2)->isPointerTy())
262       return false;
263     Changed |= setDoesNotThrow(F);
264     Changed |= setDoesNotCapture(F, 2);
265     Changed |= setDoesNotCapture(F, 3);
266     Changed |= setOnlyReadsMemory(F, 2);
267     return Changed;
268   case LibFunc::system:
269     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
270       return false;
271     // May throw; "system" is a valid pthread cancellation point.
272     Changed |= setDoesNotCapture(F, 1);
273     Changed |= setOnlyReadsMemory(F, 1);
274     return Changed;
275   case LibFunc::malloc:
276     if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy())
277       return false;
278     Changed |= setDoesNotThrow(F);
279     Changed |= setDoesNotAlias(F, 0);
280     return Changed;
281   case LibFunc::memcmp:
282     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
283         !FTy->getParamType(1)->isPointerTy())
284       return false;
285     Changed |= setOnlyReadsMemory(F);
286     Changed |= setDoesNotThrow(F);
287     Changed |= setDoesNotCapture(F, 1);
288     Changed |= setDoesNotCapture(F, 2);
289     return Changed;
290   case LibFunc::memchr:
291   case LibFunc::memrchr:
292     if (FTy->getNumParams() != 3)
293       return false;
294     Changed |= setOnlyReadsMemory(F);
295     Changed |= setDoesNotThrow(F);
296     return Changed;
297   case LibFunc::modf:
298   case LibFunc::modff:
299   case LibFunc::modfl:
300     if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
301       return false;
302     Changed |= setDoesNotThrow(F);
303     Changed |= setDoesNotCapture(F, 2);
304     return Changed;
305   case LibFunc::memcpy:
306   case LibFunc::memccpy:
307   case LibFunc::memmove:
308     if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
309       return false;
310     Changed |= setDoesNotThrow(F);
311     Changed |= setDoesNotCapture(F, 2);
312     Changed |= setOnlyReadsMemory(F, 2);
313     return Changed;
314   case LibFunc::memalign:
315     if (!FTy->getReturnType()->isPointerTy())
316       return false;
317     Changed |= setDoesNotAlias(F, 0);
318     return Changed;
319   case LibFunc::mkdir:
320     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
321       return false;
322     Changed |= setDoesNotThrow(F);
323     Changed |= setDoesNotCapture(F, 1);
324     Changed |= setOnlyReadsMemory(F, 1);
325     return Changed;
326   case LibFunc::mktime:
327     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
328       return false;
329     Changed |= setDoesNotThrow(F);
330     Changed |= setDoesNotCapture(F, 1);
331     return Changed;
332   case LibFunc::realloc:
333     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
334         !FTy->getReturnType()->isPointerTy())
335       return false;
336     Changed |= setDoesNotThrow(F);
337     Changed |= setDoesNotAlias(F, 0);
338     Changed |= setDoesNotCapture(F, 1);
339     return Changed;
340   case LibFunc::read:
341     if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
342       return false;
343     // May throw; "read" is a valid pthread cancellation point.
344     Changed |= setDoesNotCapture(F, 2);
345     return Changed;
346   case LibFunc::rewind:
347     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
348       return false;
349     Changed |= setDoesNotThrow(F);
350     Changed |= setDoesNotCapture(F, 1);
351     return Changed;
352   case LibFunc::rmdir:
353   case LibFunc::remove:
354   case LibFunc::realpath:
355     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
356       return false;
357     Changed |= setDoesNotThrow(F);
358     Changed |= setDoesNotCapture(F, 1);
359     Changed |= setOnlyReadsMemory(F, 1);
360     return Changed;
361   case LibFunc::rename:
362     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
363         !FTy->getParamType(1)->isPointerTy())
364       return false;
365     Changed |= setDoesNotThrow(F);
366     Changed |= setDoesNotCapture(F, 1);
367     Changed |= setDoesNotCapture(F, 2);
368     Changed |= setOnlyReadsMemory(F, 1);
369     Changed |= setOnlyReadsMemory(F, 2);
370     return Changed;
371   case LibFunc::readlink:
372     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
373         !FTy->getParamType(1)->isPointerTy())
374       return false;
375     Changed |= setDoesNotThrow(F);
376     Changed |= setDoesNotCapture(F, 1);
377     Changed |= setDoesNotCapture(F, 2);
378     Changed |= setOnlyReadsMemory(F, 1);
379     return Changed;
380   case LibFunc::write:
381     if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
382       return false;
383     // May throw; "write" is a valid pthread cancellation point.
384     Changed |= setDoesNotCapture(F, 2);
385     Changed |= setOnlyReadsMemory(F, 2);
386     return Changed;
387   case LibFunc::bcopy:
388     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
389         !FTy->getParamType(1)->isPointerTy())
390       return false;
391     Changed |= setDoesNotThrow(F);
392     Changed |= setDoesNotCapture(F, 1);
393     Changed |= setDoesNotCapture(F, 2);
394     Changed |= setOnlyReadsMemory(F, 1);
395     return Changed;
396   case LibFunc::bcmp:
397     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
398         !FTy->getParamType(1)->isPointerTy())
399       return false;
400     Changed |= setDoesNotThrow(F);
401     Changed |= setOnlyReadsMemory(F);
402     Changed |= setDoesNotCapture(F, 1);
403     Changed |= setDoesNotCapture(F, 2);
404     return Changed;
405   case LibFunc::bzero:
406     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
407       return false;
408     Changed |= setDoesNotThrow(F);
409     Changed |= setDoesNotCapture(F, 1);
410     return Changed;
411   case LibFunc::calloc:
412     if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy())
413       return false;
414     Changed |= setDoesNotThrow(F);
415     Changed |= setDoesNotAlias(F, 0);
416     return Changed;
417   case LibFunc::chmod:
418   case LibFunc::chown:
419     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
420       return false;
421     Changed |= setDoesNotThrow(F);
422     Changed |= setDoesNotCapture(F, 1);
423     Changed |= setOnlyReadsMemory(F, 1);
424     return Changed;
425   case LibFunc::ctermid:
426   case LibFunc::clearerr:
427   case LibFunc::closedir:
428     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
429       return false;
430     Changed |= setDoesNotThrow(F);
431     Changed |= setDoesNotCapture(F, 1);
432     return Changed;
433   case LibFunc::atoi:
434   case LibFunc::atol:
435   case LibFunc::atof:
436   case LibFunc::atoll:
437     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
438       return false;
439     Changed |= setDoesNotThrow(F);
440     Changed |= setOnlyReadsMemory(F);
441     Changed |= setDoesNotCapture(F, 1);
442     return Changed;
443   case LibFunc::access:
444     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
445       return false;
446     Changed |= setDoesNotThrow(F);
447     Changed |= setDoesNotCapture(F, 1);
448     Changed |= setOnlyReadsMemory(F, 1);
449     return Changed;
450   case LibFunc::fopen:
451     if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
452         !FTy->getParamType(0)->isPointerTy() ||
453         !FTy->getParamType(1)->isPointerTy())
454       return false;
455     Changed |= setDoesNotThrow(F);
456     Changed |= setDoesNotAlias(F, 0);
457     Changed |= setDoesNotCapture(F, 1);
458     Changed |= setDoesNotCapture(F, 2);
459     Changed |= setOnlyReadsMemory(F, 1);
460     Changed |= setOnlyReadsMemory(F, 2);
461     return Changed;
462   case LibFunc::fdopen:
463     if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
464         !FTy->getParamType(1)->isPointerTy())
465       return false;
466     Changed |= setDoesNotThrow(F);
467     Changed |= setDoesNotAlias(F, 0);
468     Changed |= setDoesNotCapture(F, 2);
469     Changed |= setOnlyReadsMemory(F, 2);
470     return Changed;
471   case LibFunc::feof:
472   case LibFunc::free:
473   case LibFunc::fseek:
474   case LibFunc::ftell:
475   case LibFunc::fgetc:
476   case LibFunc::fseeko:
477   case LibFunc::ftello:
478   case LibFunc::fileno:
479   case LibFunc::fflush:
480   case LibFunc::fclose:
481   case LibFunc::fsetpos:
482   case LibFunc::flockfile:
483   case LibFunc::funlockfile:
484   case LibFunc::ftrylockfile:
485     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
486       return false;
487     Changed |= setDoesNotThrow(F);
488     Changed |= setDoesNotCapture(F, 1);
489     return Changed;
490   case LibFunc::ferror:
491     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
492       return false;
493     Changed |= setDoesNotThrow(F);
494     Changed |= setDoesNotCapture(F, 1);
495     Changed |= setOnlyReadsMemory(F);
496     return Changed;
497   case LibFunc::fputc:
498   case LibFunc::fstat:
499   case LibFunc::frexp:
500   case LibFunc::frexpf:
501   case LibFunc::frexpl:
502   case LibFunc::fstatvfs:
503     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
504       return false;
505     Changed |= setDoesNotThrow(F);
506     Changed |= setDoesNotCapture(F, 2);
507     return Changed;
508   case LibFunc::fgets:
509     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
510         !FTy->getParamType(2)->isPointerTy())
511       return false;
512     Changed |= setDoesNotThrow(F);
513     Changed |= setDoesNotCapture(F, 3);
514     return Changed;
515   case LibFunc::fread:
516     if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
517         !FTy->getParamType(3)->isPointerTy())
518       return false;
519     Changed |= setDoesNotThrow(F);
520     Changed |= setDoesNotCapture(F, 1);
521     Changed |= setDoesNotCapture(F, 4);
522     return Changed;
523   case LibFunc::fwrite:
524     if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
525         !FTy->getParamType(3)->isPointerTy())
526       return false;
527     Changed |= setDoesNotThrow(F);
528     Changed |= setDoesNotCapture(F, 1);
529     Changed |= setDoesNotCapture(F, 4);
530     return Changed;
531   case LibFunc::fputs:
532     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
533         !FTy->getParamType(1)->isPointerTy())
534       return false;
535     Changed |= setDoesNotThrow(F);
536     Changed |= setDoesNotCapture(F, 1);
537     Changed |= setDoesNotCapture(F, 2);
538     Changed |= setOnlyReadsMemory(F, 1);
539     return Changed;
540   case LibFunc::fscanf:
541   case LibFunc::fprintf:
542     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
543         !FTy->getParamType(1)->isPointerTy())
544       return false;
545     Changed |= setDoesNotThrow(F);
546     Changed |= setDoesNotCapture(F, 1);
547     Changed |= setDoesNotCapture(F, 2);
548     Changed |= setOnlyReadsMemory(F, 2);
549     return Changed;
550   case LibFunc::fgetpos:
551     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
552         !FTy->getParamType(1)->isPointerTy())
553       return false;
554     Changed |= setDoesNotThrow(F);
555     Changed |= setDoesNotCapture(F, 1);
556     Changed |= setDoesNotCapture(F, 2);
557     return Changed;
558   case LibFunc::getc:
559   case LibFunc::getlogin_r:
560   case LibFunc::getc_unlocked:
561     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
562       return false;
563     Changed |= setDoesNotThrow(F);
564     Changed |= setDoesNotCapture(F, 1);
565     return Changed;
566   case LibFunc::getenv:
567     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
568       return false;
569     Changed |= setDoesNotThrow(F);
570     Changed |= setOnlyReadsMemory(F);
571     Changed |= setDoesNotCapture(F, 1);
572     return Changed;
573   case LibFunc::gets:
574   case LibFunc::getchar:
575     Changed |= setDoesNotThrow(F);
576     return Changed;
577   case LibFunc::getitimer:
578     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
579       return false;
580     Changed |= setDoesNotThrow(F);
581     Changed |= setDoesNotCapture(F, 2);
582     return Changed;
583   case LibFunc::getpwnam:
584     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
585       return false;
586     Changed |= setDoesNotThrow(F);
587     Changed |= setDoesNotCapture(F, 1);
588     Changed |= setOnlyReadsMemory(F, 1);
589     return Changed;
590   case LibFunc::ungetc:
591     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
592       return false;
593     Changed |= setDoesNotThrow(F);
594     Changed |= setDoesNotCapture(F, 2);
595     return Changed;
596   case LibFunc::uname:
597     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
598       return false;
599     Changed |= setDoesNotThrow(F);
600     Changed |= setDoesNotCapture(F, 1);
601     return Changed;
602   case LibFunc::unlink:
603     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
604       return false;
605     Changed |= setDoesNotThrow(F);
606     Changed |= setDoesNotCapture(F, 1);
607     Changed |= setOnlyReadsMemory(F, 1);
608     return Changed;
609   case LibFunc::unsetenv:
610     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
611       return false;
612     Changed |= setDoesNotThrow(F);
613     Changed |= setDoesNotCapture(F, 1);
614     Changed |= setOnlyReadsMemory(F, 1);
615     return Changed;
616   case LibFunc::utime:
617   case LibFunc::utimes:
618     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
619         !FTy->getParamType(1)->isPointerTy())
620       return false;
621     Changed |= setDoesNotThrow(F);
622     Changed |= setDoesNotCapture(F, 1);
623     Changed |= setDoesNotCapture(F, 2);
624     Changed |= setOnlyReadsMemory(F, 1);
625     Changed |= setOnlyReadsMemory(F, 2);
626     return Changed;
627   case LibFunc::putc:
628     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
629       return false;
630     Changed |= setDoesNotThrow(F);
631     Changed |= setDoesNotCapture(F, 2);
632     return Changed;
633   case LibFunc::puts:
634   case LibFunc::printf:
635   case LibFunc::perror:
636     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
637       return false;
638     Changed |= setDoesNotThrow(F);
639     Changed |= setDoesNotCapture(F, 1);
640     Changed |= setOnlyReadsMemory(F, 1);
641     return Changed;
642   case LibFunc::pread:
643     if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
644       return false;
645     // May throw; "pread" is a valid pthread cancellation point.
646     Changed |= setDoesNotCapture(F, 2);
647     return Changed;
648   case LibFunc::pwrite:
649     if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
650       return false;
651     // May throw; "pwrite" is a valid pthread cancellation point.
652     Changed |= setDoesNotCapture(F, 2);
653     Changed |= setOnlyReadsMemory(F, 2);
654     return Changed;
655   case LibFunc::putchar:
656     Changed |= setDoesNotThrow(F);
657     return Changed;
658   case LibFunc::popen:
659     if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
660         !FTy->getParamType(0)->isPointerTy() ||
661         !FTy->getParamType(1)->isPointerTy())
662       return false;
663     Changed |= setDoesNotThrow(F);
664     Changed |= setDoesNotAlias(F, 0);
665     Changed |= setDoesNotCapture(F, 1);
666     Changed |= setDoesNotCapture(F, 2);
667     Changed |= setOnlyReadsMemory(F, 1);
668     Changed |= setOnlyReadsMemory(F, 2);
669     return Changed;
670   case LibFunc::pclose:
671     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
672       return false;
673     Changed |= setDoesNotThrow(F);
674     Changed |= setDoesNotCapture(F, 1);
675     return Changed;
676   case LibFunc::vscanf:
677     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
678       return false;
679     Changed |= setDoesNotThrow(F);
680     Changed |= setDoesNotCapture(F, 1);
681     Changed |= setOnlyReadsMemory(F, 1);
682     return Changed;
683   case LibFunc::vsscanf:
684     if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
685         !FTy->getParamType(2)->isPointerTy())
686       return false;
687     Changed |= setDoesNotThrow(F);
688     Changed |= setDoesNotCapture(F, 1);
689     Changed |= setDoesNotCapture(F, 2);
690     Changed |= setOnlyReadsMemory(F, 1);
691     Changed |= setOnlyReadsMemory(F, 2);
692     return Changed;
693   case LibFunc::vfscanf:
694     if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
695         !FTy->getParamType(2)->isPointerTy())
696       return false;
697     Changed |= setDoesNotThrow(F);
698     Changed |= setDoesNotCapture(F, 1);
699     Changed |= setDoesNotCapture(F, 2);
700     Changed |= setOnlyReadsMemory(F, 2);
701     return Changed;
702   case LibFunc::valloc:
703     if (!FTy->getReturnType()->isPointerTy())
704       return false;
705     Changed |= setDoesNotThrow(F);
706     Changed |= setDoesNotAlias(F, 0);
707     return Changed;
708   case LibFunc::vprintf:
709     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
710       return false;
711     Changed |= setDoesNotThrow(F);
712     Changed |= setDoesNotCapture(F, 1);
713     Changed |= setOnlyReadsMemory(F, 1);
714     return Changed;
715   case LibFunc::vfprintf:
716   case LibFunc::vsprintf:
717     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
718         !FTy->getParamType(1)->isPointerTy())
719       return false;
720     Changed |= setDoesNotThrow(F);
721     Changed |= setDoesNotCapture(F, 1);
722     Changed |= setDoesNotCapture(F, 2);
723     Changed |= setOnlyReadsMemory(F, 2);
724     return Changed;
725   case LibFunc::vsnprintf:
726     if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
727         !FTy->getParamType(2)->isPointerTy())
728       return false;
729     Changed |= setDoesNotThrow(F);
730     Changed |= setDoesNotCapture(F, 1);
731     Changed |= setDoesNotCapture(F, 3);
732     Changed |= setOnlyReadsMemory(F, 3);
733     return Changed;
734   case LibFunc::open:
735     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
736       return false;
737     // May throw; "open" is a valid pthread cancellation point.
738     Changed |= setDoesNotCapture(F, 1);
739     Changed |= setOnlyReadsMemory(F, 1);
740     return Changed;
741   case LibFunc::opendir:
742     if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy() ||
743         !FTy->getParamType(0)->isPointerTy())
744       return false;
745     Changed |= setDoesNotThrow(F);
746     Changed |= setDoesNotAlias(F, 0);
747     Changed |= setDoesNotCapture(F, 1);
748     Changed |= setOnlyReadsMemory(F, 1);
749     return Changed;
750   case LibFunc::tmpfile:
751     if (!FTy->getReturnType()->isPointerTy())
752       return false;
753     Changed |= setDoesNotThrow(F);
754     Changed |= setDoesNotAlias(F, 0);
755     return Changed;
756   case LibFunc::times:
757     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
758       return false;
759     Changed |= setDoesNotThrow(F);
760     Changed |= setDoesNotCapture(F, 1);
761     return Changed;
762   case LibFunc::htonl:
763   case LibFunc::htons:
764   case LibFunc::ntohl:
765   case LibFunc::ntohs:
766     Changed |= setDoesNotThrow(F);
767     Changed |= setDoesNotAccessMemory(F);
768     return Changed;
769   case LibFunc::lstat:
770     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
771         !FTy->getParamType(1)->isPointerTy())
772       return false;
773     Changed |= setDoesNotThrow(F);
774     Changed |= setDoesNotCapture(F, 1);
775     Changed |= setDoesNotCapture(F, 2);
776     Changed |= setOnlyReadsMemory(F, 1);
777     return Changed;
778   case LibFunc::lchown:
779     if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
780       return false;
781     Changed |= setDoesNotThrow(F);
782     Changed |= setDoesNotCapture(F, 1);
783     Changed |= setOnlyReadsMemory(F, 1);
784     return Changed;
785   case LibFunc::qsort:
786     if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
787       return false;
788     // May throw; places call through function pointer.
789     Changed |= setDoesNotCapture(F, 4);
790     return Changed;
791   case LibFunc::dunder_strdup:
792   case LibFunc::dunder_strndup:
793     if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
794         !FTy->getParamType(0)->isPointerTy())
795       return false;
796     Changed |= setDoesNotThrow(F);
797     Changed |= setDoesNotAlias(F, 0);
798     Changed |= setDoesNotCapture(F, 1);
799     Changed |= setOnlyReadsMemory(F, 1);
800     return Changed;
801   case LibFunc::dunder_strtok_r:
802     if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
803       return false;
804     Changed |= setDoesNotThrow(F);
805     Changed |= setDoesNotCapture(F, 2);
806     Changed |= setOnlyReadsMemory(F, 2);
807     return Changed;
808   case LibFunc::under_IO_getc:
809     if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
810       return false;
811     Changed |= setDoesNotThrow(F);
812     Changed |= setDoesNotCapture(F, 1);
813     return Changed;
814   case LibFunc::under_IO_putc:
815     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
816       return false;
817     Changed |= setDoesNotThrow(F);
818     Changed |= setDoesNotCapture(F, 2);
819     return Changed;
820   case LibFunc::dunder_isoc99_scanf:
821     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
822       return false;
823     Changed |= setDoesNotThrow(F);
824     Changed |= setDoesNotCapture(F, 1);
825     Changed |= setOnlyReadsMemory(F, 1);
826     return Changed;
827   case LibFunc::stat64:
828   case LibFunc::lstat64:
829   case LibFunc::statvfs64:
830     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
831         !FTy->getParamType(1)->isPointerTy())
832       return false;
833     Changed |= setDoesNotThrow(F);
834     Changed |= setDoesNotCapture(F, 1);
835     Changed |= setDoesNotCapture(F, 2);
836     Changed |= setOnlyReadsMemory(F, 1);
837     return Changed;
838   case LibFunc::dunder_isoc99_sscanf:
839     if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
840         !FTy->getParamType(1)->isPointerTy())
841       return false;
842     Changed |= setDoesNotThrow(F);
843     Changed |= setDoesNotCapture(F, 1);
844     Changed |= setDoesNotCapture(F, 2);
845     Changed |= setOnlyReadsMemory(F, 1);
846     Changed |= setOnlyReadsMemory(F, 2);
847     return Changed;
848   case LibFunc::fopen64:
849     if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
850         !FTy->getParamType(0)->isPointerTy() ||
851         !FTy->getParamType(1)->isPointerTy())
852       return false;
853     Changed |= setDoesNotThrow(F);
854     Changed |= setDoesNotAlias(F, 0);
855     Changed |= setDoesNotCapture(F, 1);
856     Changed |= setDoesNotCapture(F, 2);
857     Changed |= setOnlyReadsMemory(F, 1);
858     Changed |= setOnlyReadsMemory(F, 2);
859     return Changed;
860   case LibFunc::fseeko64:
861   case LibFunc::ftello64:
862     if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
863       return false;
864     Changed |= setDoesNotThrow(F);
865     Changed |= setDoesNotCapture(F, 1);
866     return Changed;
867   case LibFunc::tmpfile64:
868     if (!FTy->getReturnType()->isPointerTy())
869       return false;
870     Changed |= setDoesNotThrow(F);
871     Changed |= setDoesNotAlias(F, 0);
872     return Changed;
873   case LibFunc::fstat64:
874   case LibFunc::fstatvfs64:
875     if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
876       return false;
877     Changed |= setDoesNotThrow(F);
878     Changed |= setDoesNotCapture(F, 2);
879     return Changed;
880   case LibFunc::open64:
881     if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
882       return false;
883     // May throw; "open" is a valid pthread cancellation point.
884     Changed |= setDoesNotCapture(F, 1);
885     Changed |= setOnlyReadsMemory(F, 1);
886     return Changed;
887   case LibFunc::gettimeofday:
888     if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
889         !FTy->getParamType(1)->isPointerTy())
890       return false;
891     // Currently some platforms have the restrict keyword on the arguments to
892     // gettimeofday. To be conservative, do not add noalias to gettimeofday's
893     // arguments.
894     Changed |= setDoesNotThrow(F);
895     Changed |= setDoesNotCapture(F, 1);
896     Changed |= setDoesNotCapture(F, 2);
897     return Changed;
898 
899   case LibFunc::Znwj: // new(unsigned int)
900   case LibFunc::Znwm: // new(unsigned long)
901   case LibFunc::Znaj: // new[](unsigned int)
902   case LibFunc::Znam: // new[](unsigned long)
903   case LibFunc::msvc_new_int: // new(unsigned int)
904   case LibFunc::msvc_new_longlong: // new(unsigned long long)
905   case LibFunc::msvc_new_array_int: // new[](unsigned int)
906   case LibFunc::msvc_new_array_longlong: // new[](unsigned long long)
907     if (FTy->getNumParams() != 1)
908       return false;
909     // Operator new always returns a nonnull noalias pointer
910     Changed |= setNonNull(F, AttributeSet::ReturnIndex);
911     Changed |= setDoesNotAlias(F, AttributeSet::ReturnIndex);
912     return Changed;
913 
914   //TODO: add LibFunc entries for:
915   //case LibFunc::memset_pattern4:
916   //case LibFunc::memset_pattern8:
917   case LibFunc::memset_pattern16:
918     if (FTy->isVarArg() || FTy->getNumParams() != 3 ||
919         !isa<PointerType>(FTy->getParamType(0)) ||
920         !isa<PointerType>(FTy->getParamType(1)) ||
921         !isa<IntegerType>(FTy->getParamType(2)))
922       return false;
923 
924     Changed |= setOnlyAccessesArgMemory(F);
925     Changed |= setOnlyReadsMemory(F, 2);
926     return Changed;
927 
928   // int __nvvm_reflect(const char *)
929   case LibFunc::nvvm_reflect:
930     if (FTy->getNumParams() != 1 || !isa<PointerType>(FTy->getParamType(0)))
931       return false;
932 
933     Changed |= setDoesNotAccessMemory(F);
934     Changed |= setDoesNotThrow(F);
935     return Changed;
936 
937   default:
938     // FIXME: It'd be really nice to cover all the library functions we're
939     // aware of here.
940     return false;
941   }
942 }
943 
944 static bool inferAllPrototypeAttributes(Module &M,
945                                         const TargetLibraryInfo &TLI) {
946   bool Changed = false;
947 
948   for (Function &F : M.functions())
949     // We only infer things using the prototype if the definition isn't around
950     // to analyze directly.
951     if (F.isDeclaration())
952       Changed |= inferPrototypeAttributes(F, TLI);
953 
954   return Changed;
955 }
956 
957 PreservedAnalyses InferFunctionAttrsPass::run(Module &M,
958                                               AnalysisManager<Module> &AM) {
959   if (skipPassForModule(name(), M))
960     return PreservedAnalyses::all();
961 
962   auto &TLI = AM.getResult<TargetLibraryAnalysis>(M);
963 
964   if (!inferAllPrototypeAttributes(M, TLI))
965     // If we didn't infer anything, preserve all analyses.
966     return PreservedAnalyses::all();
967 
968   // Otherwise, we may have changed fundamental function attributes, so clear
969   // out all the passes.
970   return PreservedAnalyses::none();
971 }
972 
973 namespace {
974 struct InferFunctionAttrsLegacyPass : public ModulePass {
975   static char ID; // Pass identification, replacement for typeid
976   InferFunctionAttrsLegacyPass() : ModulePass(ID) {
977     initializeInferFunctionAttrsLegacyPassPass(
978         *PassRegistry::getPassRegistry());
979   }
980 
981   void getAnalysisUsage(AnalysisUsage &AU) const override {
982     AU.addRequired<TargetLibraryInfoWrapperPass>();
983   }
984 
985   bool runOnModule(Module &M) override {
986     if (skipModule(M))
987       return false;
988 
989     auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
990     return inferAllPrototypeAttributes(M, TLI);
991   }
992 };
993 }
994 
995 char InferFunctionAttrsLegacyPass::ID = 0;
996 INITIALIZE_PASS_BEGIN(InferFunctionAttrsLegacyPass, "inferattrs",
997                       "Infer set function attributes", false, false)
998 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
999 INITIALIZE_PASS_END(InferFunctionAttrsLegacyPass, "inferattrs",
1000                     "Infer set function attributes", false, false)
1001 
1002 Pass *llvm::createInferFunctionAttrsLegacyPass() {
1003   return new InferFunctionAttrsLegacyPass();
1004 }
1005