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