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