Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements some functions that will create standard C libcalls.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Transforms/Utils/BuildLibCalls.h"
14
#include "llvm/ADT/SmallString.h"
15
#include "llvm/ADT/Statistic.h"
16
#include "llvm/Analysis/TargetLibraryInfo.h"
17
#include "llvm/IR/Constants.h"
18
#include "llvm/IR/DataLayout.h"
19
#include "llvm/IR/Function.h"
20
#include "llvm/IR/IRBuilder.h"
21
#include "llvm/IR/Intrinsics.h"
22
#include "llvm/IR/LLVMContext.h"
23
#include "llvm/IR/Module.h"
24
#include "llvm/IR/Type.h"
25
#include "llvm/Analysis/MemoryBuiltins.h"
26
27
using namespace llvm;
28
29
#define DEBUG_TYPE "build-libcalls"
30
31
//- Infer Attributes ---------------------------------------------------------//
32
33
STATISTIC(NumReadNone, "Number of functions inferred as readnone");
34
STATISTIC(NumReadOnly, "Number of functions inferred as readonly");
35
STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly");
36
STATISTIC(NumNoUnwind, "Number of functions inferred as nounwind");
37
STATISTIC(NumNoCapture, "Number of arguments inferred as nocapture");
38
STATISTIC(NumReadOnlyArg, "Number of arguments inferred as readonly");
39
STATISTIC(NumNoAlias, "Number of function returns inferred as noalias");
40
STATISTIC(NumNonNull, "Number of function returns inferred as nonnull returns");
41
STATISTIC(NumReturnedArg, "Number of arguments inferred as returned");
42
43
3
static bool setDoesNotAccessMemory(Function &F) {
44
3
  if (F.doesNotAccessMemory())
45
0
    return false;
46
3
  F.setDoesNotAccessMemory();
47
3
  ++NumReadNone;
48
3
  return true;
49
3
}
50
51
8.00k
static bool setOnlyReadsMemory(Function &F) {
52
8.00k
  if (F.onlyReadsMemory())
53
816
    return false;
54
7.18k
  F.setOnlyReadsMemory();
55
7.18k
  ++NumReadOnly;
56
7.18k
  return true;
57
7.18k
}
58
59
7.90k
static bool setOnlyAccessesArgMemory(Function &F) {
60
7.90k
  if (F.onlyAccessesArgMemory())
61
5.94k
    return false;
62
1.96k
  F.setOnlyAccessesArgMemory();
63
1.96k
  ++NumArgMemOnly;
64
1.96k
  return true;
65
1.96k
}
66
67
41.6k
static bool setDoesNotThrow(Function &F) {
68
41.6k
  if (F.doesNotThrow())
69
19.4k
    return false;
70
22.2k
  F.setDoesNotThrow();
71
22.2k
  ++NumNoUnwind;
72
22.2k
  return true;
73
22.2k
}
74
75
3.99k
static bool setRetDoesNotAlias(Function &F) {
76
3.99k
  if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias))
77
466
    return false;
78
3.52k
  F.addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
79
3.52k
  ++NumNoAlias;
80
3.52k
  return true;
81
3.52k
}
82
83
64.2k
static bool setDoesNotCapture(Function &F, unsigned ArgNo) {
84
64.2k
  if (F.hasParamAttribute(ArgNo, Attribute::NoCapture))
85
33.5k
    return false;
86
30.7k
  F.addParamAttr(ArgNo, Attribute::NoCapture);
87
30.7k
  ++NumNoCapture;
88
30.7k
  return true;
89
30.7k
}
90
91
31.2k
static bool setOnlyReadsMemory(Function &F, unsigned ArgNo) {
92
31.2k
  if (F.hasParamAttribute(ArgNo, Attribute::ReadOnly))
93
21.4k
    return false;
94
9.79k
  F.addParamAttr(ArgNo, Attribute::ReadOnly);
95
9.79k
  ++NumReadOnlyArg;
96
9.79k
  return true;
97
9.79k
}
98
99
449
static bool setRetNonNull(Function &F) {
100
449
  assert(F.getReturnType()->isPointerTy() &&
101
449
         "nonnull applies only to pointers");
102
449
  if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NonNull))
103
0
    return false;
104
449
  F.addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
105
449
  ++NumNonNull;
106
449
  return true;
107
449
}
108
109
2.13k
static bool setReturnedArg(Function &F, unsigned ArgNo) {
110
2.13k
  if (F.hasParamAttribute(ArgNo, Attribute::Returned))
111
423
    return false;
112
1.70k
  F.addParamAttr(ArgNo, Attribute::Returned);
113
1.70k
  ++NumReturnedArg;
114
1.70k
  return true;
115
1.70k
}
116
117
3
static bool setNonLazyBind(Function &F) {
118
3
  if (F.hasFnAttribute(Attribute::NonLazyBind))
119
0
    return false;
120
3
  F.addFnAttr(Attribute::NonLazyBind);
121
3
  return true;
122
3
}
123
124
151k
static bool setDoesNotFreeMemory(Function &F) {
125
151k
  if (F.hasFnAttribute(Attribute::NoFree))
126
24.8k
    return false;
127
127k
  F.addFnAttr(Attribute::NoFree);
128
127k
  return true;
129
127k
}
130
131
bool llvm::inferLibFuncAttributes(Module *M, StringRef Name,
132
26.3k
                                  const TargetLibraryInfo &TLI) {
133
26.3k
  Function *F = M->getFunction(Name);
134
26.3k
  if (!F)
135
1
    return false;
136
26.3k
  return inferLibFuncAttributes(*F, TLI);
137
26.3k
}
138
139
908k
bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
140
908k
  LibFunc TheLibFunc;
141
908k
  if (!(TLI.getLibFunc(F, TheLibFunc) && 
TLI.has(TheLibFunc)157k
))
142
753k
    return false;
143
154k
144
154k
  bool Changed = false;
145
154k
146
154k
  if(!isLibFreeFunction(&F, TheLibFunc) && 
!isReallocLikeFn(&F, &TLI)152k
)
147
151k
    Changed |= setDoesNotFreeMemory(F);
148
154k
149
154k
  if (F.getParent() != nullptr && 
F.getParent()->getRtLibUseGOT()154k
)
150
3
    Changed |= setNonLazyBind(F);
151
154k
152
154k
  switch (TheLibFunc) {
153
154k
  case LibFunc_strlen:
154
1.90k
  case LibFunc_wcslen:
155
1.90k
    Changed |= setOnlyReadsMemory(F);
156
1.90k
    Changed |= setDoesNotThrow(F);
157
1.90k
    Changed |= setOnlyAccessesArgMemory(F);
158
1.90k
    Changed |= setDoesNotCapture(F, 0);
159
1.90k
    return Changed;
160
1.90k
  case LibFunc_strchr:
161
140
  case LibFunc_strrchr:
162
140
    Changed |= setOnlyReadsMemory(F);
163
140
    Changed |= setDoesNotThrow(F);
164
140
    return Changed;
165
140
  case LibFunc_strtol:
166
59
  case LibFunc_strtod:
167
59
  case LibFunc_strtof:
168
59
  case LibFunc_strtoul:
169
59
  case LibFunc_strtoll:
170
59
  case LibFunc_strtold:
171
59
  case LibFunc_strtoull:
172
59
    Changed |= setDoesNotThrow(F);
173
59
    Changed |= setDoesNotCapture(F, 1);
174
59
    Changed |= setOnlyReadsMemory(F, 0);
175
59
    return Changed;
176
733
  case LibFunc_strcpy:
177
733
  case LibFunc_strncpy:
178
733
  case LibFunc_strcat:
179
733
  case LibFunc_strncat:
180
733
    Changed |= setReturnedArg(F, 0);
181
733
    LLVM_FALLTHROUGH;
182
746
  case LibFunc_stpcpy:
183
746
  case LibFunc_stpncpy:
184
746
    Changed |= setDoesNotThrow(F);
185
746
    Changed |= setDoesNotCapture(F, 1);
186
746
    Changed |= setOnlyReadsMemory(F, 1);
187
746
    return Changed;
188
746
  case LibFunc_strxfrm:
189
4
    Changed |= setDoesNotThrow(F);
190
4
    Changed |= setDoesNotCapture(F, 0);
191
4
    Changed |= setDoesNotCapture(F, 1);
192
4
    Changed |= setOnlyReadsMemory(F, 1);
193
4
    return Changed;
194
746
  case LibFunc_strcmp:      // 0,1
195
379
  case LibFunc_strspn:      // 0,1
196
379
  case LibFunc_strncmp:     // 0,1
197
379
  case LibFunc_strcspn:     // 0,1
198
379
  case LibFunc_strcoll:     // 0,1
199
379
  case LibFunc_strcasecmp:  // 0,1
200
379
  case LibFunc_strncasecmp: //
201
379
    Changed |= setOnlyReadsMemory(F);
202
379
    Changed |= setDoesNotThrow(F);
203
379
    Changed |= setDoesNotCapture(F, 0);
204
379
    Changed |= setDoesNotCapture(F, 1);
205
379
    return Changed;
206
379
  case LibFunc_strstr:
207
31
  case LibFunc_strpbrk:
208
31
    Changed |= setOnlyReadsMemory(F);
209
31
    Changed |= setDoesNotThrow(F);
210
31
    Changed |= setDoesNotCapture(F, 1);
211
31
    return Changed;
212
31
  case LibFunc_strtok:
213
17
  case LibFunc_strtok_r:
214
17
    Changed |= setDoesNotThrow(F);
215
17
    Changed |= setDoesNotCapture(F, 1);
216
17
    Changed |= setOnlyReadsMemory(F, 1);
217
17
    return Changed;
218
18
  case LibFunc_scanf:
219
18
    Changed |= setDoesNotThrow(F);
220
18
    Changed |= setDoesNotCapture(F, 0);
221
18
    Changed |= setOnlyReadsMemory(F, 0);
222
18
    return Changed;
223
17
  case LibFunc_setbuf:
224
10
  case LibFunc_setvbuf:
225
10
    Changed |= setDoesNotThrow(F);
226
10
    Changed |= setDoesNotCapture(F, 0);
227
10
    return Changed;
228
47
  case LibFunc_strdup:
229
47
  case LibFunc_strndup:
230
47
    Changed |= setDoesNotThrow(F);
231
47
    Changed |= setRetDoesNotAlias(F);
232
47
    Changed |= setDoesNotCapture(F, 0);
233
47
    Changed |= setOnlyReadsMemory(F, 0);
234
47
    return Changed;
235
47
  case LibFunc_stat:
236
8
  case LibFunc_statvfs:
237
8
    Changed |= setDoesNotThrow(F);
238
8
    Changed |= setDoesNotCapture(F, 0);
239
8
    Changed |= setDoesNotCapture(F, 1);
240
8
    Changed |= setOnlyReadsMemory(F, 0);
241
8
    return Changed;
242
68
  case LibFunc_sscanf:
243
68
    Changed |= setDoesNotThrow(F);
244
68
    Changed |= setDoesNotCapture(F, 0);
245
68
    Changed |= setDoesNotCapture(F, 1);
246
68
    Changed |= setOnlyReadsMemory(F, 0);
247
68
    Changed |= setOnlyReadsMemory(F, 1);
248
68
    return Changed;
249
224
  case LibFunc_sprintf:
250
224
    Changed |= setDoesNotThrow(F);
251
224
    Changed |= setDoesNotCapture(F, 0);
252
224
    Changed |= setDoesNotCapture(F, 1);
253
224
    Changed |= setOnlyReadsMemory(F, 1);
254
224
    return Changed;
255
194
  case LibFunc_snprintf:
256
194
    Changed |= setDoesNotThrow(F);
257
194
    Changed |= setDoesNotCapture(F, 0);
258
194
    Changed |= setDoesNotCapture(F, 2);
259
194
    Changed |= setOnlyReadsMemory(F, 2);
260
194
    return Changed;
261
8
  case LibFunc_setitimer:
262
5
    Changed |= setDoesNotThrow(F);
263
5
    Changed |= setDoesNotCapture(F, 1);
264
5
    Changed |= setDoesNotCapture(F, 2);
265
5
    Changed |= setOnlyReadsMemory(F, 1);
266
5
    return Changed;
267
8
  case LibFunc_system:
268
4
    // May throw; "system" is a valid pthread cancellation point.
269
4
    Changed |= setDoesNotCapture(F, 0);
270
4
    Changed |= setOnlyReadsMemory(F, 0);
271
4
    return Changed;
272
1.83k
  case LibFunc_malloc:
273
1.83k
    Changed |= setDoesNotThrow(F);
274
1.83k
    Changed |= setRetDoesNotAlias(F);
275
1.83k
    return Changed;
276
1.87k
  case LibFunc_memcmp:
277
1.87k
    Changed |= setOnlyReadsMemory(F);
278
1.87k
    Changed |= setDoesNotThrow(F);
279
1.87k
    Changed |= setDoesNotCapture(F, 0);
280
1.87k
    Changed |= setDoesNotCapture(F, 1);
281
1.87k
    return Changed;
282
52
  case LibFunc_memchr:
283
52
  case LibFunc_memrchr:
284
52
    Changed |= setOnlyReadsMemory(F);
285
52
    Changed |= setDoesNotThrow(F);
286
52
    return Changed;
287
52
  case LibFunc_modf:
288
14
  case LibFunc_modff:
289
14
  case LibFunc_modfl:
290
14
    Changed |= setDoesNotThrow(F);
291
14
    Changed |= setDoesNotCapture(F, 1);
292
14
    return Changed;
293
1.39k
  case LibFunc_memcpy:
294
1.39k
  case LibFunc_memmove:
295
1.39k
    Changed |= setReturnedArg(F, 0);
296
1.39k
    LLVM_FALLTHROUGH;
297
1.40k
  case LibFunc_mempcpy:
298
1.40k
  case LibFunc_memccpy:
299
1.40k
    Changed |= setDoesNotThrow(F);
300
1.40k
    Changed |= setDoesNotCapture(F, 1);
301
1.40k
    Changed |= setOnlyReadsMemory(F, 1);
302
1.40k
    return Changed;
303
1.40k
  case LibFunc_memcpy_chk:
304
210
    Changed |= setDoesNotThrow(F);
305
210
    return Changed;
306
1.40k
  case LibFunc_memalign:
307
1
    Changed |= setRetDoesNotAlias(F);
308
1
    return Changed;
309
1.40k
  case LibFunc_mkdir:
310
37
    Changed |= setDoesNotThrow(F);
311
37
    Changed |= setDoesNotCapture(F, 0);
312
37
    Changed |= setOnlyReadsMemory(F, 0);
313
37
    return Changed;
314
1.40k
  case LibFunc_mktime:
315
4
    Changed |= setDoesNotThrow(F);
316
4
    Changed |= setDoesNotCapture(F, 0);
317
4
    return Changed;
318
1.40k
  case LibFunc_realloc:
319
84
    Changed |= setDoesNotThrow(F);
320
84
    Changed |= setRetDoesNotAlias(F);
321
84
    Changed |= setDoesNotCapture(F, 0);
322
84
    return Changed;
323
1.40k
  case LibFunc_read:
324
4
    // May throw; "read" is a valid pthread cancellation point.
325
4
    Changed |= setDoesNotCapture(F, 1);
326
4
    return Changed;
327
1.40k
  case LibFunc_rewind:
328
29
    Changed |= setDoesNotThrow(F);
329
29
    Changed |= setDoesNotCapture(F, 0);
330
29
    return Changed;
331
1.40k
  case LibFunc_rmdir:
332
27
  case LibFunc_remove:
333
27
  case LibFunc_realpath:
334
27
    Changed |= setDoesNotThrow(F);
335
27
    Changed |= setDoesNotCapture(F, 0);
336
27
    Changed |= setOnlyReadsMemory(F, 0);
337
27
    return Changed;
338
27
  case LibFunc_rename:
339
13
    Changed |= setDoesNotThrow(F);
340
13
    Changed |= setDoesNotCapture(F, 0);
341
13
    Changed |= setDoesNotCapture(F, 1);
342
13
    Changed |= setOnlyReadsMemory(F, 0);
343
13
    Changed |= setOnlyReadsMemory(F, 1);
344
13
    return Changed;
345
27
  case LibFunc_readlink:
346
6
    Changed |= setDoesNotThrow(F);
347
6
    Changed |= setDoesNotCapture(F, 0);
348
6
    Changed |= setDoesNotCapture(F, 1);
349
6
    Changed |= setOnlyReadsMemory(F, 0);
350
6
    return Changed;
351
1.39k
  case LibFunc_write:
352
1.39k
    // May throw; "write" is a valid pthread cancellation point.
353
1.39k
    Changed |= setDoesNotCapture(F, 1);
354
1.39k
    Changed |= setOnlyReadsMemory(F, 1);
355
1.39k
    return Changed;
356
27
  case LibFunc_bcopy:
357
4
    Changed |= setDoesNotThrow(F);
358
4
    Changed |= setDoesNotCapture(F, 0);
359
4
    Changed |= setDoesNotCapture(F, 1);
360
4
    Changed |= setOnlyReadsMemory(F, 0);
361
4
    return Changed;
362
506
  case LibFunc_bcmp:
363
506
    Changed |= setDoesNotThrow(F);
364
506
    Changed |= setOnlyReadsMemory(F);
365
506
    Changed |= setDoesNotCapture(F, 0);
366
506
    Changed |= setDoesNotCapture(F, 1);
367
506
    return Changed;
368
27
  case LibFunc_bzero:
369
4
    Changed |= setDoesNotThrow(F);
370
4
    Changed |= setDoesNotCapture(F, 0);
371
4
    return Changed;
372
149
  case LibFunc_calloc:
373
149
    Changed |= setDoesNotThrow(F);
374
149
    Changed |= setRetDoesNotAlias(F);
375
149
    return Changed;
376
27
  case LibFunc_chmod:
377
10
  case LibFunc_chown:
378
10
    Changed |= setDoesNotThrow(F);
379
10
    Changed |= setDoesNotCapture(F, 0);
380
10
    Changed |= setOnlyReadsMemory(F, 0);
381
10
    return Changed;
382
14
  case LibFunc_ctermid:
383
14
  case LibFunc_clearerr:
384
14
  case LibFunc_closedir:
385
14
    Changed |= setDoesNotThrow(F);
386
14
    Changed |= setDoesNotCapture(F, 0);
387
14
    return Changed;
388
1.63k
  case LibFunc_atoi:
389
1.63k
  case LibFunc_atol:
390
1.63k
  case LibFunc_atof:
391
1.63k
  case LibFunc_atoll:
392
1.63k
    Changed |= setDoesNotThrow(F);
393
1.63k
    Changed |= setOnlyReadsMemory(F);
394
1.63k
    Changed |= setDoesNotCapture(F, 0);
395
1.63k
    return Changed;
396
1.63k
  case LibFunc_access:
397
13
    Changed |= setDoesNotThrow(F);
398
13
    Changed |= setDoesNotCapture(F, 0);
399
13
    Changed |= setOnlyReadsMemory(F, 0);
400
13
    return Changed;
401
1.63k
  case LibFunc_fopen:
402
1.39k
    Changed |= setDoesNotThrow(F);
403
1.39k
    Changed |= setRetDoesNotAlias(F);
404
1.39k
    Changed |= setDoesNotCapture(F, 0);
405
1.39k
    Changed |= setDoesNotCapture(F, 1);
406
1.39k
    Changed |= setOnlyReadsMemory(F, 0);
407
1.39k
    Changed |= setOnlyReadsMemory(F, 1);
408
1.39k
    return Changed;
409
1.63k
  case LibFunc_fdopen:
410
4
    Changed |= setDoesNotThrow(F);
411
4
    Changed |= setRetDoesNotAlias(F);
412
4
    Changed |= setDoesNotCapture(F, 1);
413
4
    Changed |= setOnlyReadsMemory(F, 1);
414
4
    return Changed;
415
3.98k
  case LibFunc_feof:
416
3.98k
  case LibFunc_free:
417
3.98k
  case LibFunc_fseek:
418
3.98k
  case LibFunc_ftell:
419
3.98k
  case LibFunc_fgetc:
420
3.98k
  case LibFunc_fgetc_unlocked:
421
3.98k
  case LibFunc_fseeko:
422
3.98k
  case LibFunc_ftello:
423
3.98k
  case LibFunc_fileno:
424
3.98k
  case LibFunc_fflush:
425
3.98k
  case LibFunc_fclose:
426
3.98k
  case LibFunc_fsetpos:
427
3.98k
  case LibFunc_flockfile:
428
3.98k
  case LibFunc_funlockfile:
429
3.98k
  case LibFunc_ftrylockfile:
430
3.98k
    Changed |= setDoesNotThrow(F);
431
3.98k
    Changed |= setDoesNotCapture(F, 0);
432
3.98k
    return Changed;
433
3.98k
  case LibFunc_ferror:
434
28
    Changed |= setDoesNotThrow(F);
435
28
    Changed |= setDoesNotCapture(F, 0);
436
28
    Changed |= setOnlyReadsMemory(F);
437
28
    return Changed;
438
3.98k
  case LibFunc_fputc:
439
386
  case LibFunc_fputc_unlocked:
440
386
  case LibFunc_fstat:
441
386
  case LibFunc_frexp:
442
386
  case LibFunc_frexpf:
443
386
  case LibFunc_frexpl:
444
386
  case LibFunc_fstatvfs:
445
386
    Changed |= setDoesNotThrow(F);
446
386
    Changed |= setDoesNotCapture(F, 1);
447
386
    return Changed;
448
386
  case LibFunc_fgets:
449
57
  case LibFunc_fgets_unlocked:
450
57
    Changed |= setDoesNotThrow(F);
451
57
    Changed |= setDoesNotCapture(F, 2);
452
57
    return Changed;
453
123
  case LibFunc_fread:
454
123
  case LibFunc_fread_unlocked:
455
123
    Changed |= setDoesNotThrow(F);
456
123
    Changed |= setDoesNotCapture(F, 0);
457
123
    Changed |= setDoesNotCapture(F, 3);
458
123
    return Changed;
459
3.74k
  case LibFunc_fwrite:
460
3.74k
  case LibFunc_fwrite_unlocked:
461
3.74k
    Changed |= setDoesNotThrow(F);
462
3.74k
    Changed |= setDoesNotCapture(F, 0);
463
3.74k
    Changed |= setDoesNotCapture(F, 3);
464
3.74k
    // FIXME: readonly #1?
465
3.74k
    return Changed;
466
3.74k
  case LibFunc_fputs:
467
50
  case LibFunc_fputs_unlocked:
468
50
    Changed |= setDoesNotThrow(F);
469
50
    Changed |= setDoesNotCapture(F, 0);
470
50
    Changed |= setDoesNotCapture(F, 1);
471
50
    Changed |= setOnlyReadsMemory(F, 0);
472
50
    return Changed;
473
531
  case LibFunc_fscanf:
474
531
  case LibFunc_fprintf:
475
531
    Changed |= setDoesNotThrow(F);
476
531
    Changed |= setDoesNotCapture(F, 0);
477
531
    Changed |= setDoesNotCapture(F, 1);
478
531
    Changed |= setOnlyReadsMemory(F, 1);
479
531
    return Changed;
480
531
  case LibFunc_fgetpos:
481
5
    Changed |= setDoesNotThrow(F);
482
5
    Changed |= setDoesNotCapture(F, 0);
483
5
    Changed |= setDoesNotCapture(F, 1);
484
5
    return Changed;
485
531
  case LibFunc_getc:
486
70
  case LibFunc_getlogin_r:
487
70
  case LibFunc_getc_unlocked:
488
70
    Changed |= setDoesNotThrow(F);
489
70
    Changed |= setDoesNotCapture(F, 0);
490
70
    return Changed;
491
1.44k
  case LibFunc_getenv:
492
1.44k
    Changed |= setDoesNotThrow(F);
493
1.44k
    Changed |= setOnlyReadsMemory(F);
494
1.44k
    Changed |= setDoesNotCapture(F, 0);
495
1.44k
    return Changed;
496
70
  case LibFunc_gets:
497
26
  case LibFunc_getchar:
498
26
  case LibFunc_getchar_unlocked:
499
26
    Changed |= setDoesNotThrow(F);
500
26
    return Changed;
501
26
  case LibFunc_getitimer:
502
4
    Changed |= setDoesNotThrow(F);
503
4
    Changed |= setDoesNotCapture(F, 1);
504
4
    return Changed;
505
26
  case LibFunc_getpwnam:
506
8
    Changed |= setDoesNotThrow(F);
507
8
    Changed |= setDoesNotCapture(F, 0);
508
8
    Changed |= setOnlyReadsMemory(F, 0);
509
8
    return Changed;
510
26
  case LibFunc_ungetc:
511
23
    Changed |= setDoesNotThrow(F);
512
23
    Changed |= setDoesNotCapture(F, 1);
513
23
    return Changed;
514
26
  case LibFunc_uname:
515
13
    Changed |= setDoesNotThrow(F);
516
13
    Changed |= setDoesNotCapture(F, 0);
517
13
    return Changed;
518
29
  case LibFunc_unlink:
519
29
    Changed |= setDoesNotThrow(F);
520
29
    Changed |= setDoesNotCapture(F, 0);
521
29
    Changed |= setOnlyReadsMemory(F, 0);
522
29
    return Changed;
523
26
  case LibFunc_unsetenv:
524
4
    Changed |= setDoesNotThrow(F);
525
4
    Changed |= setDoesNotCapture(F, 0);
526
4
    Changed |= setOnlyReadsMemory(F, 0);
527
4
    return Changed;
528
26
  case LibFunc_utime:
529
14
  case LibFunc_utimes:
530
14
    Changed |= setDoesNotThrow(F);
531
14
    Changed |= setDoesNotCapture(F, 0);
532
14
    Changed |= setDoesNotCapture(F, 1);
533
14
    Changed |= setOnlyReadsMemory(F, 0);
534
14
    Changed |= setOnlyReadsMemory(F, 1);
535
14
    return Changed;
536
48
  case LibFunc_putc:
537
48
  case LibFunc_putc_unlocked:
538
48
    Changed |= setDoesNotThrow(F);
539
48
    Changed |= setDoesNotCapture(F, 1);
540
48
    return Changed;
541
15.9k
  case LibFunc_puts:
542
15.9k
  case LibFunc_printf:
543
15.9k
  case LibFunc_perror:
544
15.9k
    Changed |= setDoesNotThrow(F);
545
15.9k
    Changed |= setDoesNotCapture(F, 0);
546
15.9k
    Changed |= setOnlyReadsMemory(F, 0);
547
15.9k
    return Changed;
548
15.9k
  case LibFunc_pread:
549
4
    // May throw; "pread" is a valid pthread cancellation point.
550
4
    Changed |= setDoesNotCapture(F, 1);
551
4
    return Changed;
552
15.9k
  case LibFunc_pwrite:
553
4
    // May throw; "pwrite" is a valid pthread cancellation point.
554
4
    Changed |= setDoesNotCapture(F, 1);
555
4
    Changed |= setOnlyReadsMemory(F, 1);
556
4
    return Changed;
557
15.9k
  case LibFunc_putchar:
558
449
  case LibFunc_putchar_unlocked:
559
449
    Changed |= setDoesNotThrow(F);
560
449
    return Changed;
561
449
  case LibFunc_popen:
562
4
    Changed |= setDoesNotThrow(F);
563
4
    Changed |= setRetDoesNotAlias(F);
564
4
    Changed |= setDoesNotCapture(F, 0);
565
4
    Changed |= setDoesNotCapture(F, 1);
566
4
    Changed |= setOnlyReadsMemory(F, 0);
567
4
    Changed |= setOnlyReadsMemory(F, 1);
568
4
    return Changed;
569
449
  case LibFunc_pclose:
570
7
    Changed |= setDoesNotThrow(F);
571
7
    Changed |= setDoesNotCapture(F, 0);
572
7
    return Changed;
573
449
  case LibFunc_vscanf:
574
4
    Changed |= setDoesNotThrow(F);
575
4
    Changed |= setDoesNotCapture(F, 0);
576
4
    Changed |= setOnlyReadsMemory(F, 0);
577
4
    return Changed;
578
449
  case LibFunc_vsscanf:
579
4
    Changed |= setDoesNotThrow(F);
580
4
    Changed |= setDoesNotCapture(F, 0);
581
4
    Changed |= setDoesNotCapture(F, 1);
582
4
    Changed |= setOnlyReadsMemory(F, 0);
583
4
    Changed |= setOnlyReadsMemory(F, 1);
584
4
    return Changed;
585
449
  case LibFunc_vfscanf:
586
4
    Changed |= setDoesNotThrow(F);
587
4
    Changed |= setDoesNotCapture(F, 0);
588
4
    Changed |= setDoesNotCapture(F, 1);
589
4
    Changed |= setOnlyReadsMemory(F, 1);
590
4
    return Changed;
591
449
  case LibFunc_valloc:
592
5
    Changed |= setDoesNotThrow(F);
593
5
    Changed |= setRetDoesNotAlias(F);
594
5
    return Changed;
595
449
  case LibFunc_vprintf:
596
4
    Changed |= setDoesNotThrow(F);
597
4
    Changed |= setDoesNotCapture(F, 0);
598
4
    Changed |= setOnlyReadsMemory(F, 0);
599
4
    return Changed;
600
449
  case LibFunc_vfprintf:
601
21
  case LibFunc_vsprintf:
602
21
    Changed |= setDoesNotThrow(F);
603
21
    Changed |= setDoesNotCapture(F, 0);
604
21
    Changed |= setDoesNotCapture(F, 1);
605
21
    Changed |= setOnlyReadsMemory(F, 1);
606
21
    return Changed;
607
21
  case LibFunc_vsnprintf:
608
16
    Changed |= setDoesNotThrow(F);
609
16
    Changed |= setDoesNotCapture(F, 0);
610
16
    Changed |= setDoesNotCapture(F, 2);
611
16
    Changed |= setOnlyReadsMemory(F, 2);
612
16
    return Changed;
613
1.39k
  case LibFunc_open:
614
1.39k
    // May throw; "open" is a valid pthread cancellation point.
615
1.39k
    Changed |= setDoesNotCapture(F, 0);
616
1.39k
    Changed |= setOnlyReadsMemory(F, 0);
617
1.39k
    return Changed;
618
21
  case LibFunc_opendir:
619
4
    Changed |= setDoesNotThrow(F);
620
4
    Changed |= setRetDoesNotAlias(F);
621
4
    Changed |= setDoesNotCapture(F, 0);
622
4
    Changed |= setOnlyReadsMemory(F, 0);
623
4
    return Changed;
624
21
  case LibFunc_tmpfile:
625
7
    Changed |= setDoesNotThrow(F);
626
7
    Changed |= setRetDoesNotAlias(F);
627
7
    return Changed;
628
21
  case LibFunc_times:
629
5
    Changed |= setDoesNotThrow(F);
630
5
    Changed |= setDoesNotCapture(F, 0);
631
5
    return Changed;
632
21
  case LibFunc_htonl:
633
0
  case LibFunc_htons:
634
0
  case LibFunc_ntohl:
635
0
  case LibFunc_ntohs:
636
0
    Changed |= setDoesNotThrow(F);
637
0
    Changed |= setDoesNotAccessMemory(F);
638
0
    return Changed;
639
4
  case LibFunc_lstat:
640
4
    Changed |= setDoesNotThrow(F);
641
4
    Changed |= setDoesNotCapture(F, 0);
642
4
    Changed |= setDoesNotCapture(F, 1);
643
4
    Changed |= setOnlyReadsMemory(F, 0);
644
4
    return Changed;
645
4
  case LibFunc_lchown:
646
4
    Changed |= setDoesNotThrow(F);
647
4
    Changed |= setDoesNotCapture(F, 0);
648
4
    Changed |= setOnlyReadsMemory(F, 0);
649
4
    return Changed;
650
32
  case LibFunc_qsort:
651
32
    // May throw; places call through function pointer.
652
32
    Changed |= setDoesNotCapture(F, 3);
653
32
    return Changed;
654
0
  case LibFunc_dunder_strdup:
655
0
  case LibFunc_dunder_strndup:
656
0
    Changed |= setDoesNotThrow(F);
657
0
    Changed |= setRetDoesNotAlias(F);
658
0
    Changed |= setDoesNotCapture(F, 0);
659
0
    Changed |= setOnlyReadsMemory(F, 0);
660
0
    return Changed;
661
0
  case LibFunc_dunder_strtok_r:
662
0
    Changed |= setDoesNotThrow(F);
663
0
    Changed |= setDoesNotCapture(F, 1);
664
0
    Changed |= setOnlyReadsMemory(F, 1);
665
0
    return Changed;
666
0
  case LibFunc_under_IO_getc:
667
0
    Changed |= setDoesNotThrow(F);
668
0
    Changed |= setDoesNotCapture(F, 0);
669
0
    return Changed;
670
0
  case LibFunc_under_IO_putc:
671
0
    Changed |= setDoesNotThrow(F);
672
0
    Changed |= setDoesNotCapture(F, 1);
673
0
    return Changed;
674
0
  case LibFunc_dunder_isoc99_scanf:
675
0
    Changed |= setDoesNotThrow(F);
676
0
    Changed |= setDoesNotCapture(F, 0);
677
0
    Changed |= setOnlyReadsMemory(F, 0);
678
0
    return Changed;
679
3
  case LibFunc_stat64:
680
3
  case LibFunc_lstat64:
681
3
  case LibFunc_statvfs64:
682
3
    Changed |= setDoesNotThrow(F);
683
3
    Changed |= setDoesNotCapture(F, 0);
684
3
    Changed |= setDoesNotCapture(F, 1);
685
3
    Changed |= setOnlyReadsMemory(F, 0);
686
3
    return Changed;
687
3
  case LibFunc_dunder_isoc99_sscanf:
688
0
    Changed |= setDoesNotThrow(F);
689
0
    Changed |= setDoesNotCapture(F, 0);
690
0
    Changed |= setDoesNotCapture(F, 1);
691
0
    Changed |= setOnlyReadsMemory(F, 0);
692
0
    Changed |= setOnlyReadsMemory(F, 1);
693
0
    return Changed;
694
3
  case LibFunc_fopen64:
695
0
    Changed |= setDoesNotThrow(F);
696
0
    Changed |= setRetDoesNotAlias(F);
697
0
    Changed |= setDoesNotCapture(F, 0);
698
0
    Changed |= setDoesNotCapture(F, 1);
699
0
    Changed |= setOnlyReadsMemory(F, 0);
700
0
    Changed |= setOnlyReadsMemory(F, 1);
701
0
    return Changed;
702
3
  case LibFunc_fseeko64:
703
2
  case LibFunc_ftello64:
704
2
    Changed |= setDoesNotThrow(F);
705
2
    Changed |= setDoesNotCapture(F, 0);
706
2
    return Changed;
707
2
  case LibFunc_tmpfile64:
708
1
    Changed |= setDoesNotThrow(F);
709
1
    Changed |= setRetDoesNotAlias(F);
710
1
    return Changed;
711
2
  case LibFunc_fstat64:
712
2
  case LibFunc_fstatvfs64:
713
2
    Changed |= setDoesNotThrow(F);
714
2
    Changed |= setDoesNotCapture(F, 1);
715
2
    return Changed;
716
2
  case LibFunc_open64:
717
1
    // May throw; "open" is a valid pthread cancellation point.
718
1
    Changed |= setDoesNotCapture(F, 0);
719
1
    Changed |= setOnlyReadsMemory(F, 0);
720
1
    return Changed;
721
1.42k
  case LibFunc_gettimeofday:
722
1.42k
    // Currently some platforms have the restrict keyword on the arguments to
723
1.42k
    // gettimeofday. To be conservative, do not add noalias to gettimeofday's
724
1.42k
    // arguments.
725
1.42k
    Changed |= setDoesNotThrow(F);
726
1.42k
    Changed |= setDoesNotCapture(F, 0);
727
1.42k
    Changed |= setDoesNotCapture(F, 1);
728
1.42k
    return Changed;
729
449
  case LibFunc_Znwj: // new(unsigned int)
730
449
  case LibFunc_Znwm: // new(unsigned long)
731
449
  case LibFunc_Znaj: // new[](unsigned int)
732
449
  case LibFunc_Znam: // new[](unsigned long)
733
449
  case LibFunc_msvc_new_int: // new(unsigned int)
734
449
  case LibFunc_msvc_new_longlong: // new(unsigned long long)
735
449
  case LibFunc_msvc_new_array_int: // new[](unsigned int)
736
449
  case LibFunc_msvc_new_array_longlong: // new[](unsigned long long)
737
449
    // Operator new always returns a nonnull noalias pointer
738
449
    Changed |= setRetNonNull(F);
739
449
    Changed |= setRetDoesNotAlias(F);
740
449
    return Changed;
741
449
  // TODO: add LibFunc entries for:
742
449
  // case LibFunc_memset_pattern4:
743
449
  // case LibFunc_memset_pattern8:
744
6.00k
  case LibFunc_memset_pattern16:
745
6.00k
    Changed |= setOnlyAccessesArgMemory(F);
746
6.00k
    Changed |= setDoesNotCapture(F, 0);
747
6.00k
    Changed |= setDoesNotCapture(F, 1);
748
6.00k
    Changed |= setOnlyReadsMemory(F, 1);
749
6.00k
    return Changed;
750
449
  // int __nvvm_reflect(const char *)
751
449
  case LibFunc_nvvm_reflect:
752
3
    Changed |= setDoesNotAccessMemory(F);
753
3
    Changed |= setDoesNotThrow(F);
754
3
    return Changed;
755
449
756
103k
  default:
757
103k
    // FIXME: It'd be really nice to cover all the library functions we're
758
103k
    // aware of here.
759
103k
    return false;
760
154k
  }
761
154k
}
762
763
bool llvm::hasUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
764
                           LibFunc DoubleFn, LibFunc FloatFn,
765
1.85k
                           LibFunc LongDoubleFn) {
766
1.85k
  switch (Ty->getTypeID()) {
767
1.85k
  case Type::HalfTyID:
768
1
    return false;
769
1.85k
  case Type::FloatTyID:
770
287
    return TLI->has(FloatFn);
771
1.85k
  case Type::DoubleTyID:
772
1.32k
    return TLI->has(DoubleFn);
773
1.85k
  default:
774
237
    return TLI->has(LongDoubleFn);
775
1.85k
  }
776
1.85k
}
777
778
StringRef llvm::getUnaryFloatFn(const TargetLibraryInfo *TLI, Type *Ty,
779
                                LibFunc DoubleFn, LibFunc FloatFn,
780
118
                                LibFunc LongDoubleFn) {
781
118
  assert(hasUnaryFloatFn(TLI, Ty, DoubleFn, FloatFn, LongDoubleFn) &&
782
118
         "Cannot get name for unavailable function!");
783
118
784
118
  switch (Ty->getTypeID()) {
785
118
  case Type::HalfTyID:
786
0
    llvm_unreachable("No name for HalfTy!");
787
118
  case Type::FloatTyID:
788
49
    return TLI->getName(FloatFn);
789
118
  case Type::DoubleTyID:
790
67
    return TLI->getName(DoubleFn);
791
118
  default:
792
2
    return TLI->getName(LongDoubleFn);
793
118
  }
794
118
}
795
796
//- Emit LibCalls ------------------------------------------------------------//
797
798
21.9k
Value *llvm::castToCStr(Value *V, IRBuilder<> &B) {
799
21.9k
  unsigned AS = V->getType()->getPointerAddressSpace();
800
21.9k
  return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
801
21.9k
}
802
803
static Value *emitLibCall(LibFunc TheLibFunc, Type *ReturnType,
804
                          ArrayRef<Type *> ParamTypes,
805
                          ArrayRef<Value *> Operands, IRBuilder<> &B,
806
                          const TargetLibraryInfo *TLI,
807
2.03k
                          bool IsVaArgs = false) {
808
2.03k
  if (!TLI->has(TheLibFunc))
809
0
    return nullptr;
810
2.03k
811
2.03k
  Module *M = B.GetInsertBlock()->getModule();
812
2.03k
  StringRef FuncName = TLI->getName(TheLibFunc);
813
2.03k
  FunctionType *FuncType = FunctionType::get(ReturnType, ParamTypes, IsVaArgs);
814
2.03k
  FunctionCallee Callee = M->getOrInsertFunction(FuncName, FuncType);
815
2.03k
  inferLibFuncAttributes(M, FuncName, *TLI);
816
2.03k
  CallInst *CI = B.CreateCall(Callee, Operands, FuncName);
817
2.03k
  if (const Function *F =
818
2.03k
          dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
819
2.03k
    CI->setCallingConv(F->getCallingConv());
820
2.03k
  return CI;
821
2.03k
}
822
823
Value *llvm::emitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
824
68
                        const TargetLibraryInfo *TLI) {
825
68
  LLVMContext &Context = B.GetInsertBlock()->getContext();
826
68
  return emitLibCall(LibFunc_strlen, DL.getIntPtrType(Context),
827
68
                     B.getInt8PtrTy(), castToCStr(Ptr, B), B, TLI);
828
68
}
829
830
Value *llvm::emitStrChr(Value *Ptr, char C, IRBuilder<> &B,
831
23
                        const TargetLibraryInfo *TLI) {
832
23
  Type *I8Ptr = B.getInt8PtrTy();
833
23
  Type *I32Ty = B.getInt32Ty();
834
23
  return emitLibCall(LibFunc_strchr, I8Ptr, {I8Ptr, I32Ty},
835
23
                     {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, B, TLI);
836
23
}
837
838
Value *llvm::emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
839
1
                         const DataLayout &DL, const TargetLibraryInfo *TLI) {
840
1
  LLVMContext &Context = B.GetInsertBlock()->getContext();
841
1
  return emitLibCall(
842
1
      LibFunc_strncmp, B.getInt32Ty(),
843
1
      {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
844
1
      {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
845
1
}
846
847
Value *llvm::emitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
848
396
                        const TargetLibraryInfo *TLI) {
849
396
  Type *I8Ptr = B.getInt8PtrTy();
850
396
  return emitLibCall(LibFunc_strcpy, I8Ptr, {I8Ptr, I8Ptr},
851
396
                     {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI);
852
396
}
853
854
Value *llvm::emitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B,
855
4
                        const TargetLibraryInfo *TLI) {
856
4
  Type *I8Ptr = B.getInt8PtrTy();
857
4
  return emitLibCall(LibFunc_stpcpy, I8Ptr, {I8Ptr, I8Ptr},
858
4
                     {castToCStr(Dst, B), castToCStr(Src, B)}, B, TLI);
859
4
}
860
861
Value *llvm::emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
862
162
                         const TargetLibraryInfo *TLI) {
863
162
  Type *I8Ptr = B.getInt8PtrTy();
864
162
  return emitLibCall(LibFunc_strncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()},
865
162
                     {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI);
866
162
}
867
868
Value *llvm::emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
869
0
                         const TargetLibraryInfo *TLI) {
870
0
  Type *I8Ptr = B.getInt8PtrTy();
871
0
  return emitLibCall(LibFunc_stpncpy, I8Ptr, {I8Ptr, I8Ptr, Len->getType()},
872
0
                     {castToCStr(Dst, B), castToCStr(Src, B), Len}, B, TLI);
873
0
}
874
875
Value *llvm::emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
876
                           IRBuilder<> &B, const DataLayout &DL,
877
34
                           const TargetLibraryInfo *TLI) {
878
34
  if (!TLI->has(LibFunc_memcpy_chk))
879
0
    return nullptr;
880
34
881
34
  Module *M = B.GetInsertBlock()->getModule();
882
34
  AttributeList AS;
883
34
  AS = AttributeList::get(M->getContext(), AttributeList::FunctionIndex,
884
34
                          Attribute::NoUnwind);
885
34
  LLVMContext &Context = B.GetInsertBlock()->getContext();
886
34
  FunctionCallee MemCpy = M->getOrInsertFunction(
887
34
      "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
888
34
      B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
889
34
      DL.getIntPtrType(Context));
890
34
  Dst = castToCStr(Dst, B);
891
34
  Src = castToCStr(Src, B);
892
34
  CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
893
34
  if (const Function *F =
894
34
          dyn_cast<Function>(MemCpy.getCallee()->stripPointerCasts()))
895
34
    CI->setCallingConv(F->getCallingConv());
896
34
  return CI;
897
34
}
898
899
Value *llvm::emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
900
27
                        const DataLayout &DL, const TargetLibraryInfo *TLI) {
901
27
  LLVMContext &Context = B.GetInsertBlock()->getContext();
902
27
  return emitLibCall(
903
27
      LibFunc_memchr, B.getInt8PtrTy(),
904
27
      {B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context)},
905
27
      {castToCStr(Ptr, B), Val, Len}, B, TLI);
906
27
}
907
908
Value *llvm::emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
909
391
                        const DataLayout &DL, const TargetLibraryInfo *TLI) {
910
391
  LLVMContext &Context = B.GetInsertBlock()->getContext();
911
391
  return emitLibCall(
912
391
      LibFunc_memcmp, B.getInt32Ty(),
913
391
      {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
914
391
      {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
915
391
}
916
917
Value *llvm::emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
918
504
                      const DataLayout &DL, const TargetLibraryInfo *TLI) {
919
504
  LLVMContext &Context = B.GetInsertBlock()->getContext();
920
504
  return emitLibCall(
921
504
      LibFunc_bcmp, B.getInt32Ty(),
922
504
      {B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context)},
923
504
      {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, B, TLI);
924
504
}
925
926
Value *llvm::emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
927
1
                         IRBuilder<> &B, const TargetLibraryInfo *TLI) {
928
1
  return emitLibCall(
929
1
      LibFunc_memccpy, B.getInt8PtrTy(),
930
1
      {B.getInt8PtrTy(), B.getInt8PtrTy(), B.getInt32Ty(), Len->getType()},
931
1
      {Ptr1, Ptr2, Val, Len}, B, TLI);
932
1
}
933
934
Value *llvm::emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
935
                          ArrayRef<Value *> VariadicArgs, IRBuilder<> &B,
936
187
                          const TargetLibraryInfo *TLI) {
937
187
  SmallVector<Value *, 8> Args{castToCStr(Dest, B), Size, castToCStr(Fmt, B)};
938
187
  Args.insert(Args.end(), VariadicArgs.begin(), VariadicArgs.end());
939
187
  return emitLibCall(LibFunc_snprintf, B.getInt32Ty(),
940
187
                     {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy()},
941
187
                     Args, B, TLI, /*IsVaArgs=*/true);
942
187
}
943
944
Value *llvm::emitSPrintf(Value *Dest, Value *Fmt,
945
                         ArrayRef<Value *> VariadicArgs, IRBuilder<> &B,
946
171
                         const TargetLibraryInfo *TLI) {
947
171
  SmallVector<Value *, 8> Args{castToCStr(Dest, B), castToCStr(Fmt, B)};
948
171
  Args.insert(Args.end(), VariadicArgs.begin(), VariadicArgs.end());
949
171
  return emitLibCall(LibFunc_sprintf, B.getInt32Ty(),
950
171
                     {B.getInt8PtrTy(), B.getInt8PtrTy()}, Args, B, TLI,
951
171
                     /*IsVaArgs=*/true);
952
171
}
953
954
Value *llvm::emitStrCat(Value *Dest, Value *Src, IRBuilder<> &B,
955
81
                        const TargetLibraryInfo *TLI) {
956
81
  return emitLibCall(LibFunc_strcat, B.getInt8PtrTy(),
957
81
                     {B.getInt8PtrTy(), B.getInt8PtrTy()},
958
81
                     {castToCStr(Dest, B), castToCStr(Src, B)}, B, TLI);
959
81
}
960
961
Value *llvm::emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B,
962
1
                         const TargetLibraryInfo *TLI) {
963
1
  return emitLibCall(LibFunc_strlcpy, Size->getType(),
964
1
                     {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()},
965
1
                     {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI);
966
1
}
967
968
Value *llvm::emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B,
969
1
                         const TargetLibraryInfo *TLI) {
970
1
  return emitLibCall(LibFunc_strlcat, Size->getType(),
971
1
                     {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()},
972
1
                     {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI);
973
1
}
974
975
Value *llvm::emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilder<> &B,
976
10
                         const TargetLibraryInfo *TLI) {
977
10
  return emitLibCall(LibFunc_strncat, B.getInt8PtrTy(),
978
10
                     {B.getInt8PtrTy(), B.getInt8PtrTy(), Size->getType()},
979
10
                     {castToCStr(Dest, B), castToCStr(Src, B), Size}, B, TLI);
980
10
}
981
982
Value *llvm::emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
983
10
                           IRBuilder<> &B, const TargetLibraryInfo *TLI) {
984
10
  return emitLibCall(
985
10
      LibFunc_vsnprintf, B.getInt32Ty(),
986
10
      {B.getInt8PtrTy(), Size->getType(), B.getInt8PtrTy(), VAList->getType()},
987
10
      {castToCStr(Dest, B), Size, castToCStr(Fmt, B), VAList}, B, TLI);
988
10
}
989
990
Value *llvm::emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList,
991
1
                          IRBuilder<> &B, const TargetLibraryInfo *TLI) {
992
1
  return emitLibCall(LibFunc_vsprintf, B.getInt32Ty(),
993
1
                     {B.getInt8PtrTy(), B.getInt8PtrTy(), VAList->getType()},
994
1
                     {castToCStr(Dest, B), castToCStr(Fmt, B), VAList}, B, TLI);
995
1
}
996
997
/// Append a suffix to the function name according to the type of 'Op'.
998
static void appendTypeSuffix(Value *Op, StringRef &Name,
999
79
                             SmallString<20> &NameBuffer) {
1000
79
  if (!Op->getType()->isDoubleTy()) {
1001
77
      NameBuffer += Name;
1002
77
1003
77
    if (Op->getType()->isFloatTy())
1004
77
      NameBuffer += 'f';
1005
0
    else
1006
0
      NameBuffer += 'l';
1007
77
1008
77
    Name = NameBuffer;
1009
77
  }
1010
79
}
1011
1012
static Value *emitUnaryFloatFnCallHelper(Value *Op, StringRef Name,
1013
                                         IRBuilder<> &B,
1014
183
                                         const AttributeList &Attrs) {
1015
183
  assert((Name != "") && "Must specify Name to emitUnaryFloatFnCall");
1016
183
1017
183
  Module *M = B.GetInsertBlock()->getModule();
1018
183
  FunctionCallee Callee =
1019
183
      M->getOrInsertFunction(Name, Op->getType(), Op->getType());
1020
183
  CallInst *CI = B.CreateCall(Callee, Op, Name);
1021
183
1022
183
  // The incoming attribute set may have come from a speculatable intrinsic, but
1023
183
  // is being replaced with a library call which is not allowed to be
1024
183
  // speculatable.
1025
183
  CI->setAttributes(Attrs.removeAttribute(B.getContext(),
1026
183
                                          AttributeList::FunctionIndex,
1027
183
                                          Attribute::Speculatable));
1028
183
  if (const Function *F =
1029
183
          dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
1030
183
    CI->setCallingConv(F->getCallingConv());
1031
183
1032
183
  return CI;
1033
183
}
1034
1035
Value *llvm::emitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
1036
65
                                  const AttributeList &Attrs) {
1037
65
  SmallString<20> NameBuffer;
1038
65
  appendTypeSuffix(Op, Name, NameBuffer);
1039
65
1040
65
  return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs);
1041
65
}
1042
1043
Value *llvm::emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI,
1044
                                  LibFunc DoubleFn, LibFunc FloatFn,
1045
                                  LibFunc LongDoubleFn, IRBuilder<> &B,
1046
118
                                  const AttributeList &Attrs) {
1047
118
  // Get the name of the function according to TLI.
1048
118
  StringRef Name = getUnaryFloatFn(TLI, Op->getType(),
1049
118
                                   DoubleFn, FloatFn, LongDoubleFn);
1050
118
1051
118
  return emitUnaryFloatFnCallHelper(Op, Name, B, Attrs);
1052
118
}
1053
1054
Value *llvm::emitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
1055
14
                                   IRBuilder<> &B, const AttributeList &Attrs) {
1056
14
  assert((Name != "") && "Must specify Name to emitBinaryFloatFnCall");
1057
14
1058
14
  SmallString<20> NameBuffer;
1059
14
  appendTypeSuffix(Op1, Name, NameBuffer);
1060
14
1061
14
  Module *M = B.GetInsertBlock()->getModule();
1062
14
  FunctionCallee Callee = M->getOrInsertFunction(
1063
14
      Name, Op1->getType(), Op1->getType(), Op2->getType());
1064
14
  CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
1065
14
  CI->setAttributes(Attrs);
1066
14
  if (const Function *F =
1067
14
          dyn_cast<Function>(Callee.getCallee()->stripPointerCasts()))
1068
14
    CI->setCallingConv(F->getCallingConv());
1069
14
1070
14
  return CI;
1071
14
}
1072
1073
Value *llvm::emitPutChar(Value *Char, IRBuilder<> &B,
1074
406
                         const TargetLibraryInfo *TLI) {
1075
406
  if (!TLI->has(LibFunc_putchar))
1076
0
    return nullptr;
1077
406
1078
406
  Module *M = B.GetInsertBlock()->getModule();
1079
406
  StringRef PutCharName = TLI->getName(LibFunc_putchar);
1080
406
  FunctionCallee PutChar =
1081
406
      M->getOrInsertFunction(PutCharName, B.getInt32Ty(), B.getInt32Ty());
1082
406
  inferLibFuncAttributes(M, PutCharName, *TLI);
1083
406
  CallInst *CI = B.CreateCall(PutChar,
1084
406
                              B.CreateIntCast(Char,
1085
406
                              B.getInt32Ty(),
1086
406
                              /*isSigned*/true,
1087
406
                              "chari"),
1088
406
                              PutCharName);
1089
406
1090
406
  if (const Function *F =
1091
406
          dyn_cast<Function>(PutChar.getCallee()->stripPointerCasts()))
1092
406
    CI->setCallingConv(F->getCallingConv());
1093
406
  return CI;
1094
406
}
1095
1096
Value *llvm::emitPutS(Value *Str, IRBuilder<> &B,
1097
15.1k
                      const TargetLibraryInfo *TLI) {
1098
15.1k
  if (!TLI->has(LibFunc_puts))
1099
0
    return nullptr;
1100
15.1k
1101
15.1k
  Module *M = B.GetInsertBlock()->getModule();
1102
15.1k
  StringRef PutsName = TLI->getName(LibFunc_puts);
1103
15.1k
  FunctionCallee PutS =
1104
15.1k
      M->getOrInsertFunction(PutsName, B.getInt32Ty(), B.getInt8PtrTy());
1105
15.1k
  inferLibFuncAttributes(M, PutsName, *TLI);
1106
15.1k
  CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), PutsName);
1107
15.1k
  if (const Function *F =
1108
15.1k
          dyn_cast<Function>(PutS.getCallee()->stripPointerCasts()))
1109
15.1k
    CI->setCallingConv(F->getCallingConv());
1110
15.1k
  return CI;
1111
15.1k
}
1112
1113
Value *llvm::emitFPutC(Value *Char, Value *File, IRBuilder<> &B,
1114
339
                       const TargetLibraryInfo *TLI) {
1115
339
  if (!TLI->has(LibFunc_fputc))
1116
0
    return nullptr;
1117
339
1118
339
  Module *M = B.GetInsertBlock()->getModule();
1119
339
  StringRef FPutcName = TLI->getName(LibFunc_fputc);
1120
339
  FunctionCallee F = M->getOrInsertFunction(FPutcName, B.getInt32Ty(),
1121
339
                                            B.getInt32Ty(), File->getType());
1122
339
  if (File->getType()->isPointerTy())
1123
339
    inferLibFuncAttributes(M, FPutcName, *TLI);
1124
339
  Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
1125
339
                         "chari");
1126
339
  CallInst *CI = B.CreateCall(F, {Char, File}, FPutcName);
1127
339
1128
339
  if (const Function *Fn =
1129
339
          dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1130
339
    CI->setCallingConv(Fn->getCallingConv());
1131
339
  return CI;
1132
339
}
1133
1134
Value *llvm::emitFPutCUnlocked(Value *Char, Value *File, IRBuilder<> &B,
1135
3
                               const TargetLibraryInfo *TLI) {
1136
3
  if (!TLI->has(LibFunc_fputc_unlocked))
1137
1
    return nullptr;
1138
2
1139
2
  Module *M = B.GetInsertBlock()->getModule();
1140
2
  StringRef FPutcUnlockedName = TLI->getName(LibFunc_fputc_unlocked);
1141
2
  FunctionCallee F = M->getOrInsertFunction(FPutcUnlockedName, B.getInt32Ty(),
1142
2
                                            B.getInt32Ty(), File->getType());
1143
2
  if (File->getType()->isPointerTy())
1144
2
    inferLibFuncAttributes(M, FPutcUnlockedName, *TLI);
1145
2
  Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/ true, "chari");
1146
2
  CallInst *CI = B.CreateCall(F, {Char, File}, FPutcUnlockedName);
1147
2
1148
2
  if (const Function *Fn =
1149
2
          dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1150
2
    CI->setCallingConv(Fn->getCallingConv());
1151
2
  return CI;
1152
2
}
1153
1154
Value *llvm::emitFPutS(Value *Str, Value *File, IRBuilder<> &B,
1155
47
                       const TargetLibraryInfo *TLI) {
1156
47
  if (!TLI->has(LibFunc_fputs))
1157
0
    return nullptr;
1158
47
1159
47
  Module *M = B.GetInsertBlock()->getModule();
1160
47
  StringRef FPutsName = TLI->getName(LibFunc_fputs);
1161
47
  FunctionCallee F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
1162
47
                                            B.getInt8PtrTy(), File->getType());
1163
47
  if (File->getType()->isPointerTy())
1164
47
    inferLibFuncAttributes(M, FPutsName, *TLI);
1165
47
  CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsName);
1166
47
1167
47
  if (const Function *Fn =
1168
47
          dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1169
47
    CI->setCallingConv(Fn->getCallingConv());
1170
47
  return CI;
1171
47
}
1172
1173
Value *llvm::emitFPutSUnlocked(Value *Str, Value *File, IRBuilder<> &B,
1174
0
                               const TargetLibraryInfo *TLI) {
1175
0
  if (!TLI->has(LibFunc_fputs_unlocked))
1176
0
    return nullptr;
1177
0
1178
0
  Module *M = B.GetInsertBlock()->getModule();
1179
0
  StringRef FPutsUnlockedName = TLI->getName(LibFunc_fputs_unlocked);
1180
0
  FunctionCallee F = M->getOrInsertFunction(FPutsUnlockedName, B.getInt32Ty(),
1181
0
                                            B.getInt8PtrTy(), File->getType());
1182
0
  if (File->getType()->isPointerTy())
1183
0
    inferLibFuncAttributes(M, FPutsUnlockedName, *TLI);
1184
0
  CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, FPutsUnlockedName);
1185
0
1186
0
  if (const Function *Fn =
1187
0
          dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1188
0
    CI->setCallingConv(Fn->getCallingConv());
1189
0
  return CI;
1190
0
}
1191
1192
Value *llvm::emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
1193
2.34k
                        const DataLayout &DL, const TargetLibraryInfo *TLI) {
1194
2.34k
  if (!TLI->has(LibFunc_fwrite))
1195
0
    return nullptr;
1196
2.34k
1197
2.34k
  Module *M = B.GetInsertBlock()->getModule();
1198
2.34k
  LLVMContext &Context = B.GetInsertBlock()->getContext();
1199
2.34k
  StringRef FWriteName = TLI->getName(LibFunc_fwrite);
1200
2.34k
  FunctionCallee F = M->getOrInsertFunction(
1201
2.34k
      FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
1202
2.34k
      DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
1203
2.34k
1204
2.34k
  if (File->getType()->isPointerTy())
1205
2.34k
    inferLibFuncAttributes(M, FWriteName, *TLI);
1206
2.34k
  CallInst *CI =
1207
2.34k
      B.CreateCall(F, {castToCStr(Ptr, B), Size,
1208
2.34k
                       ConstantInt::get(DL.getIntPtrType(Context), 1), File});
1209
2.34k
1210
2.34k
  if (const Function *Fn =
1211
2.34k
          dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1212
2.34k
    CI->setCallingConv(Fn->getCallingConv());
1213
2.34k
  return CI;
1214
2.34k
}
1215
1216
Value *llvm::emitMalloc(Value *Num, IRBuilder<> &B, const DataLayout &DL,
1217
21
                        const TargetLibraryInfo *TLI) {
1218
21
  if (!TLI->has(LibFunc_malloc))
1219
0
    return nullptr;
1220
21
1221
21
  Module *M = B.GetInsertBlock()->getModule();
1222
21
  StringRef MallocName = TLI->getName(LibFunc_malloc);
1223
21
  LLVMContext &Context = B.GetInsertBlock()->getContext();
1224
21
  FunctionCallee Malloc = M->getOrInsertFunction(MallocName, B.getInt8PtrTy(),
1225
21
                                                 DL.getIntPtrType(Context));
1226
21
  inferLibFuncAttributes(M, MallocName, *TLI);
1227
21
  CallInst *CI = B.CreateCall(Malloc, Num, MallocName);
1228
21
1229
21
  if (const Function *F =
1230
21
          dyn_cast<Function>(Malloc.getCallee()->stripPointerCasts()))
1231
21
    CI->setCallingConv(F->getCallingConv());
1232
21
1233
21
  return CI;
1234
21
}
1235
1236
Value *llvm::emitCalloc(Value *Num, Value *Size, const AttributeList &Attrs,
1237
1
                        IRBuilder<> &B, const TargetLibraryInfo &TLI) {
1238
1
  if (!TLI.has(LibFunc_calloc))
1239
0
    return nullptr;
1240
1
1241
1
  Module *M = B.GetInsertBlock()->getModule();
1242
1
  StringRef CallocName = TLI.getName(LibFunc_calloc);
1243
1
  const DataLayout &DL = M->getDataLayout();
1244
1
  IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext()));
1245
1
  FunctionCallee Calloc = M->getOrInsertFunction(
1246
1
      CallocName, Attrs, B.getInt8PtrTy(), PtrType, PtrType);
1247
1
  inferLibFuncAttributes(M, CallocName, TLI);
1248
1
  CallInst *CI = B.CreateCall(Calloc, {Num, Size}, CallocName);
1249
1
1250
1
  if (const auto *F =
1251
1
          dyn_cast<Function>(Calloc.getCallee()->stripPointerCasts()))
1252
1
    CI->setCallingConv(F->getCallingConv());
1253
1
1254
1
  return CI;
1255
1
}
1256
1257
Value *llvm::emitFWriteUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
1258
                                IRBuilder<> &B, const DataLayout &DL,
1259
4
                                const TargetLibraryInfo *TLI) {
1260
4
  if (!TLI->has(LibFunc_fwrite_unlocked))
1261
1
    return nullptr;
1262
3
1263
3
  Module *M = B.GetInsertBlock()->getModule();
1264
3
  LLVMContext &Context = B.GetInsertBlock()->getContext();
1265
3
  StringRef FWriteUnlockedName = TLI->getName(LibFunc_fwrite_unlocked);
1266
3
  FunctionCallee F = M->getOrInsertFunction(
1267
3
      FWriteUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
1268
3
      DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
1269
3
1270
3
  if (File->getType()->isPointerTy())
1271
3
    inferLibFuncAttributes(M, FWriteUnlockedName, *TLI);
1272
3
  CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
1273
3
1274
3
  if (const Function *Fn =
1275
3
          dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1276
3
    CI->setCallingConv(Fn->getCallingConv());
1277
3
  return CI;
1278
3
}
1279
1280
Value *llvm::emitFGetCUnlocked(Value *File, IRBuilder<> &B,
1281
1
                               const TargetLibraryInfo *TLI) {
1282
1
  if (!TLI->has(LibFunc_fgetc_unlocked))
1283
0
    return nullptr;
1284
1
1285
1
  Module *M = B.GetInsertBlock()->getModule();
1286
1
  StringRef FGetCUnlockedName = TLI->getName(LibFunc_fgetc_unlocked);
1287
1
  FunctionCallee F = M->getOrInsertFunction(FGetCUnlockedName, B.getInt32Ty(),
1288
1
                                            File->getType());
1289
1
  if (File->getType()->isPointerTy())
1290
1
    inferLibFuncAttributes(M, FGetCUnlockedName, *TLI);
1291
1
  CallInst *CI = B.CreateCall(F, File, FGetCUnlockedName);
1292
1
1293
1
  if (const Function *Fn =
1294
1
          dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1295
1
    CI->setCallingConv(Fn->getCallingConv());
1296
1
  return CI;
1297
1
}
1298
1299
Value *llvm::emitFGetSUnlocked(Value *Str, Value *Size, Value *File,
1300
1
                               IRBuilder<> &B, const TargetLibraryInfo *TLI) {
1301
1
  if (!TLI->has(LibFunc_fgets_unlocked))
1302
0
    return nullptr;
1303
1
1304
1
  Module *M = B.GetInsertBlock()->getModule();
1305
1
  StringRef FGetSUnlockedName = TLI->getName(LibFunc_fgets_unlocked);
1306
1
  FunctionCallee F =
1307
1
      M->getOrInsertFunction(FGetSUnlockedName, B.getInt8PtrTy(),
1308
1
                             B.getInt8PtrTy(), B.getInt32Ty(), File->getType());
1309
1
  inferLibFuncAttributes(M, FGetSUnlockedName, *TLI);
1310
1
  CallInst *CI =
1311
1
      B.CreateCall(F, {castToCStr(Str, B), Size, File}, FGetSUnlockedName);
1312
1
1313
1
  if (const Function *Fn =
1314
1
          dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1315
1
    CI->setCallingConv(Fn->getCallingConv());
1316
1
  return CI;
1317
1
}
1318
1319
Value *llvm::emitFReadUnlocked(Value *Ptr, Value *Size, Value *N, Value *File,
1320
                               IRBuilder<> &B, const DataLayout &DL,
1321
23
                               const TargetLibraryInfo *TLI) {
1322
23
  if (!TLI->has(LibFunc_fread_unlocked))
1323
22
    return nullptr;
1324
1
1325
1
  Module *M = B.GetInsertBlock()->getModule();
1326
1
  LLVMContext &Context = B.GetInsertBlock()->getContext();
1327
1
  StringRef FReadUnlockedName = TLI->getName(LibFunc_fread_unlocked);
1328
1
  FunctionCallee F = M->getOrInsertFunction(
1329
1
      FReadUnlockedName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
1330
1
      DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
1331
1
1332
1
  if (File->getType()->isPointerTy())
1333
1
    inferLibFuncAttributes(M, FReadUnlockedName, *TLI);
1334
1
  CallInst *CI = B.CreateCall(F, {castToCStr(Ptr, B), Size, N, File});
1335
1
1336
1
  if (const Function *Fn =
1337
1
          dyn_cast<Function>(F.getCallee()->stripPointerCasts()))
1338
1
    CI->setCallingConv(Fn->getCallingConv());
1339
1
  return CI;
1340
1
}