Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/LibCallsShrinkWrap.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- LibCallsShrinkWrap.cpp ----------------------------------*- C++ -*-===//
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 pass shrink-wraps a call to function if the result is not used.
10
// The call can set errno but is otherwise side effect free. For example:
11
//    sqrt(val);
12
//  is transformed to
13
//    if (val < 0)
14
//      sqrt(val);
15
//  Even if the result of library call is not being used, the compiler cannot
16
//  safely delete the call because the function can set errno on error
17
//  conditions.
18
//  Note in many functions, the error condition solely depends on the incoming
19
//  parameter. In this optimization, we can generate the condition can lead to
20
//  the errno to shrink-wrap the call. Since the chances of hitting the error
21
//  condition is low, the runtime call is effectively eliminated.
22
//
23
//  These partially dead calls are usually results of C++ abstraction penalty
24
//  exposed by inlining.
25
//
26
//===----------------------------------------------------------------------===//
27
28
#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
29
#include "llvm/ADT/SmallVector.h"
30
#include "llvm/ADT/Statistic.h"
31
#include "llvm/Analysis/GlobalsModRef.h"
32
#include "llvm/Analysis/TargetLibraryInfo.h"
33
#include "llvm/IR/CFG.h"
34
#include "llvm/IR/Constants.h"
35
#include "llvm/IR/Dominators.h"
36
#include "llvm/IR/Function.h"
37
#include "llvm/IR/IRBuilder.h"
38
#include "llvm/IR/InstVisitor.h"
39
#include "llvm/IR/Instructions.h"
40
#include "llvm/IR/LLVMContext.h"
41
#include "llvm/IR/MDBuilder.h"
42
#include "llvm/Pass.h"
43
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
44
using namespace llvm;
45
46
#define DEBUG_TYPE "libcalls-shrinkwrap"
47
48
STATISTIC(NumWrappedOneCond, "Number of One-Condition Wrappers Inserted");
49
STATISTIC(NumWrappedTwoCond, "Number of Two-Condition Wrappers Inserted");
50
51
namespace {
52
class LibCallsShrinkWrapLegacyPass : public FunctionPass {
53
public:
54
  static char ID; // Pass identification, replacement for typeid
55
11.6k
  explicit LibCallsShrinkWrapLegacyPass() : FunctionPass(ID) {
56
11.6k
    initializeLibCallsShrinkWrapLegacyPassPass(
57
11.6k
        *PassRegistry::getPassRegistry());
58
11.6k
  }
59
  void getAnalysisUsage(AnalysisUsage &AU) const override;
60
  bool runOnFunction(Function &F) override;
61
};
62
}
63
64
char LibCallsShrinkWrapLegacyPass::ID = 0;
65
22.3k
INITIALIZE_PASS_BEGIN(LibCallsShrinkWrapLegacyPass, "libcalls-shrinkwrap",
66
22.3k
                      "Conditionally eliminate dead library calls", false,
67
22.3k
                      false)
68
22.3k
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
69
22.3k
INITIALIZE_PASS_END(LibCallsShrinkWrapLegacyPass, "libcalls-shrinkwrap",
70
                    "Conditionally eliminate dead library calls", false, false)
71
72
namespace {
73
class LibCallsShrinkWrap : public InstVisitor<LibCallsShrinkWrap> {
74
public:
75
  LibCallsShrinkWrap(const TargetLibraryInfo &TLI, DominatorTree *DT)
76
463k
      : TLI(TLI), DT(DT){};
77
1.82M
  void visitCallInst(CallInst &CI) { checkCandidate(CI); }
78
463k
  bool perform() {
79
463k
    bool Changed = false;
80
463k
    for (auto &CI : WorkList) {
81
115
      LLVM_DEBUG(dbgs() << "CDCE calls: " << CI->getCalledFunction()->getName()
82
115
                        << "\n");
83
115
      if (perform(CI)) {
84
111
        Changed = true;
85
111
        LLVM_DEBUG(dbgs() << "Transformed\n");
86
111
      }
87
115
    }
88
463k
    return Changed;
89
463k
  }
90
91
private:
92
  bool perform(CallInst *CI);
93
  void checkCandidate(CallInst &CI);
94
  void shrinkWrapCI(CallInst *CI, Value *Cond);
95
  bool performCallDomainErrorOnly(CallInst *CI, const LibFunc &Func);
96
  bool performCallErrors(CallInst *CI, const LibFunc &Func);
97
  bool performCallRangeErrorOnly(CallInst *CI, const LibFunc &Func);
98
  Value *generateOneRangeCond(CallInst *CI, const LibFunc &Func);
99
  Value *generateTwoRangeCond(CallInst *CI, const LibFunc &Func);
100
  Value *generateCondForPow(CallInst *CI, const LibFunc &Func);
101
102
  // Create an OR of two conditions.
103
  Value *createOrCond(CallInst *CI, CmpInst::Predicate Cmp, float Val,
104
55
                      CmpInst::Predicate Cmp2, float Val2) {
105
55
    IRBuilder<> BBBuilder(CI);
106
55
    Value *Arg = CI->getArgOperand(0);
107
55
    auto Cond2 = createCond(BBBuilder, Arg, Cmp2, Val2);
108
55
    auto Cond1 = createCond(BBBuilder, Arg, Cmp, Val);
109
55
    return BBBuilder.CreateOr(Cond1, Cond2);
110
55
  }
111
112
  // Create a single condition using IRBuilder.
113
  Value *createCond(IRBuilder<> &BBBuilder, Value *Arg, CmpInst::Predicate Cmp,
114
158
                    float Val) {
115
158
    Constant *V = ConstantFP::get(BBBuilder.getContext(), APFloat(Val));
116
158
    if (!Arg->getType()->isFloatTy())
117
104
      V = ConstantExpr::getFPExtend(V, Arg->getType());
118
158
    return BBBuilder.CreateFCmp(Cmp, Arg, V);
119
158
  }
120
121
  // Create a single condition.
122
48
  Value *createCond(CallInst *CI, CmpInst::Predicate Cmp, float Val) {
123
48
    IRBuilder<> BBBuilder(CI);
124
48
    Value *Arg = CI->getArgOperand(0);
125
48
    return createCond(BBBuilder, Arg, Cmp, Val);
126
48
  }
127
128
  const TargetLibraryInfo &TLI;
129
  DominatorTree *DT;
130
  SmallVector<CallInst *, 16> WorkList;
131
};
132
} // end anonymous namespace
133
134
// Perform the transformation to calls with errno set by domain error.
135
bool LibCallsShrinkWrap::performCallDomainErrorOnly(CallInst *CI,
136
115
                                                    const LibFunc &Func) {
137
115
  Value *Cond = nullptr;
138
115
139
115
  switch (Func) {
140
115
  case LibFunc_acos:  // DomainError: (x < -1 || x > 1)
141
13
  case LibFunc_acosf: // Same as acos
142
13
  case LibFunc_acosl: // Same as acos
143
13
  case LibFunc_asin:  // DomainError: (x < -1 || x > 1)
144
13
  case LibFunc_asinf: // Same as asin
145
13
  case LibFunc_asinl: // Same as asin
146
13
  {
147
13
    ++NumWrappedTwoCond;
148
13
    Cond = createOrCond(CI, CmpInst::FCMP_OLT, -1.0f, CmpInst::FCMP_OGT, 1.0f);
149
13
    break;
150
13
  }
151
13
  case LibFunc_cos:  // DomainError: (x == +inf || x == -inf)
152
12
  case LibFunc_cosf: // Same as cos
153
12
  case LibFunc_cosl: // Same as cos
154
12
  case LibFunc_sin:  // DomainError: (x == +inf || x == -inf)
155
12
  case LibFunc_sinf: // Same as sin
156
12
  case LibFunc_sinl: // Same as sin
157
12
  {
158
12
    ++NumWrappedTwoCond;
159
12
    Cond = createOrCond(CI, CmpInst::FCMP_OEQ, INFINITY, CmpInst::FCMP_OEQ,
160
12
                        -INFINITY);
161
12
    break;
162
12
  }
163
12
  case LibFunc_acosh:  // DomainError: (x < 1)
164
6
  case LibFunc_acoshf: // Same as acosh
165
6
  case LibFunc_acoshl: // Same as acosh
166
6
  {
167
6
    ++NumWrappedOneCond;
168
6
    Cond = createCond(CI, CmpInst::FCMP_OLT, 1.0f);
169
6
    break;
170
6
  }
171
6
  case LibFunc_sqrt:  // DomainError: (x < 0)
172
6
  case LibFunc_sqrtf: // Same as sqrt
173
6
  case LibFunc_sqrtl: // Same as sqrt
174
6
  {
175
6
    ++NumWrappedOneCond;
176
6
    Cond = createCond(CI, CmpInst::FCMP_OLT, 0.0f);
177
6
    break;
178
6
  }
179
78
  default:
180
78
    return false;
181
37
  }
182
37
  shrinkWrapCI(CI, Cond);
183
37
  return true;
184
37
}
185
186
// Perform the transformation to calls with errno set by range error.
187
bool LibCallsShrinkWrap::performCallRangeErrorOnly(CallInst *CI,
188
78
                                                   const LibFunc &Func) {
189
78
  Value *Cond = nullptr;
190
78
191
78
  switch (Func) {
192
78
  case LibFunc_cosh:
193
24
  case LibFunc_coshf:
194
24
  case LibFunc_coshl:
195
24
  case LibFunc_exp:
196
24
  case LibFunc_expf:
197
24
  case LibFunc_expl:
198
24
  case LibFunc_exp10:
199
24
  case LibFunc_exp10f:
200
24
  case LibFunc_exp10l:
201
24
  case LibFunc_exp2:
202
24
  case LibFunc_exp2f:
203
24
  case LibFunc_exp2l:
204
24
  case LibFunc_sinh:
205
24
  case LibFunc_sinhf:
206
24
  case LibFunc_sinhl: {
207
24
    Cond = generateTwoRangeCond(CI, Func);
208
24
    break;
209
24
  }
210
24
  case LibFunc_expm1:  // RangeError: (709, inf)
211
6
  case LibFunc_expm1f: // RangeError: (88, inf)
212
6
  case LibFunc_expm1l: // RangeError: (11356, inf)
213
6
  {
214
6
    Cond = generateOneRangeCond(CI, Func);
215
6
    break;
216
6
  }
217
48
  default:
218
48
    return false;
219
30
  }
220
30
  shrinkWrapCI(CI, Cond);
221
30
  return true;
222
30
}
223
224
// Perform the transformation to calls with errno set by combination of errors.
225
bool LibCallsShrinkWrap::performCallErrors(CallInst *CI,
226
48
                                           const LibFunc &Func) {
227
48
  Value *Cond = nullptr;
228
48
229
48
  switch (Func) {
230
48
  case LibFunc_atanh:  // DomainError: (x < -1 || x > 1)
231
6
                        // PoleError:   (x == -1 || x == 1)
232
6
                        // Overall Cond: (x <= -1 || x >= 1)
233
6
  case LibFunc_atanhf: // Same as atanh
234
6
  case LibFunc_atanhl: // Same as atanh
235
6
  {
236
6
    ++NumWrappedTwoCond;
237
6
    Cond = createOrCond(CI, CmpInst::FCMP_OLE, -1.0f, CmpInst::FCMP_OGE, 1.0f);
238
6
    break;
239
6
  }
240
24
  case LibFunc_log:    // DomainError: (x < 0)
241
24
                        // PoleError:   (x == 0)
242
24
                        // Overall Cond: (x <= 0)
243
24
  case LibFunc_logf:   // Same as log
244
24
  case LibFunc_logl:   // Same as log
245
24
  case LibFunc_log10:  // Same as log
246
24
  case LibFunc_log10f: // Same as log
247
24
  case LibFunc_log10l: // Same as log
248
24
  case LibFunc_log2:   // Same as log
249
24
  case LibFunc_log2f:  // Same as log
250
24
  case LibFunc_log2l:  // Same as log
251
24
  case LibFunc_logb:   // Same as log
252
24
  case LibFunc_logbf:  // Same as log
253
24
  case LibFunc_logbl:  // Same as log
254
24
  {
255
24
    ++NumWrappedOneCond;
256
24
    Cond = createCond(CI, CmpInst::FCMP_OLE, 0.0f);
257
24
    break;
258
24
  }
259
24
  case LibFunc_log1p:  // DomainError: (x < -1)
260
6
                        // PoleError:   (x == -1)
261
6
                        // Overall Cond: (x <= -1)
262
6
  case LibFunc_log1pf: // Same as log1p
263
6
  case LibFunc_log1pl: // Same as log1p
264
6
  {
265
6
    ++NumWrappedOneCond;
266
6
    Cond = createCond(CI, CmpInst::FCMP_OLE, -1.0f);
267
6
    break;
268
6
  }
269
8
  case LibFunc_pow: // DomainError: x < 0 and y is noninteger
270
8
                     // PoleError:   x == 0 and y < 0
271
8
                     // RangeError:  overflow or underflow
272
8
  case LibFunc_powf:
273
8
  case LibFunc_powl: {
274
8
    Cond = generateCondForPow(CI, Func);
275
8
    if (Cond == nullptr)
276
0
      return false;
277
8
    break;
278
8
  }
279
8
  default:
280
4
    return false;
281
44
  }
282
44
  assert(Cond && "performCallErrors should not see an empty condition");
283
44
  shrinkWrapCI(CI, Cond);
284
44
  return true;
285
44
}
286
287
// Checks if CI is a candidate for shrinkwrapping and put it into work list if
288
// true.
289
1.82M
void LibCallsShrinkWrap::checkCandidate(CallInst &CI) {
290
1.82M
  if (CI.isNoBuiltin())
291
1.13M
    return;
292
689k
  // A possible improvement is to handle the calls with the return value being
293
689k
  // used. If there is API for fast libcall implementation without setting
294
689k
  // errno, we can use the same framework to direct/wrap the call to the fast
295
689k
  // API in the error free path, and leave the original call in the slow path.
296
689k
  if (!CI.use_empty())
297
240k
    return;
298
449k
299
449k
  LibFunc Func;
300
449k
  Function *Callee = CI.getCalledFunction();
301
449k
  if (!Callee)
302
30.2k
    return;
303
419k
  if (!TLI.getLibFunc(*Callee, Func) || 
!TLI.has(Func)96.6k
)
304
330k
    return;
305
88.5k
306
88.5k
  if (CI.getNumArgOperands() == 0)
307
4
    return;
308
88.5k
  // TODO: Handle long double in other formats.
309
88.5k
  Type *ArgType = CI.getArgOperand(0)->getType();
310
88.5k
  if (!(ArgType->isFloatTy() || 
ArgType->isDoubleTy()88.5k
||
311
88.5k
        
ArgType->isX86_FP80Ty()88.4k
))
312
88.4k
    return;
313
114
314
114
  WorkList.push_back(&CI);
315
114
}
316
317
// Generate the upper bound condition for RangeError.
318
Value *LibCallsShrinkWrap::generateOneRangeCond(CallInst *CI,
319
6
                                                const LibFunc &Func) {
320
6
  float UpperBound;
321
6
  switch (Func) {
322
6
  case LibFunc_expm1: // RangeError: (709, inf)
323
2
    UpperBound = 709.0f;
324
2
    break;
325
6
  case LibFunc_expm1f: // RangeError: (88, inf)
326
2
    UpperBound = 88.0f;
327
2
    break;
328
6
  case LibFunc_expm1l: // RangeError: (11356, inf)
329
2
    UpperBound = 11356.0f;
330
2
    break;
331
6
  default:
332
0
    llvm_unreachable("Unhandled library call!");
333
6
  }
334
6
335
6
  ++NumWrappedOneCond;
336
6
  return createCond(CI, CmpInst::FCMP_OGT, UpperBound);
337
6
}
338
339
// Generate the lower and upper bound condition for RangeError.
340
Value *LibCallsShrinkWrap::generateTwoRangeCond(CallInst *CI,
341
24
                                                const LibFunc &Func) {
342
24
  float UpperBound, LowerBound;
343
24
  switch (Func) {
344
24
  case LibFunc_cosh: // RangeError: (x < -710 || x > 710)
345
4
  case LibFunc_sinh: // Same as cosh
346
4
    LowerBound = -710.0f;
347
4
    UpperBound = 710.0f;
348
4
    break;
349
4
  case LibFunc_coshf: // RangeError: (x < -89 || x > 89)
350
4
  case LibFunc_sinhf: // Same as coshf
351
4
    LowerBound = -89.0f;
352
4
    UpperBound = 89.0f;
353
4
    break;
354
4
  case LibFunc_coshl: // RangeError: (x < -11357 || x > 11357)
355
4
  case LibFunc_sinhl: // Same as coshl
356
4
    LowerBound = -11357.0f;
357
4
    UpperBound = 11357.0f;
358
4
    break;
359
4
  case LibFunc_exp: // RangeError: (x < -745 || x > 709)
360
2
    LowerBound = -745.0f;
361
2
    UpperBound = 709.0f;
362
2
    break;
363
4
  case LibFunc_expf: // RangeError: (x < -103 || x > 88)
364
2
    LowerBound = -103.0f;
365
2
    UpperBound = 88.0f;
366
2
    break;
367
4
  case LibFunc_expl: // RangeError: (x < -11399 || x > 11356)
368
2
    LowerBound = -11399.0f;
369
2
    UpperBound = 11356.0f;
370
2
    break;
371
4
  case LibFunc_exp10: // RangeError: (x < -323 || x > 308)
372
0
    LowerBound = -323.0f;
373
0
    UpperBound = 308.0f;
374
0
    break;
375
4
  case LibFunc_exp10f: // RangeError: (x < -45 || x > 38)
376
0
    LowerBound = -45.0f;
377
0
    UpperBound = 38.0f;
378
0
    break;
379
4
  case LibFunc_exp10l: // RangeError: (x < -4950 || x > 4932)
380
0
    LowerBound = -4950.0f;
381
0
    UpperBound = 4932.0f;
382
0
    break;
383
4
  case LibFunc_exp2: // RangeError: (x < -1074 || x > 1023)
384
2
    LowerBound = -1074.0f;
385
2
    UpperBound = 1023.0f;
386
2
    break;
387
4
  case LibFunc_exp2f: // RangeError: (x < -149 || x > 127)
388
2
    LowerBound = -149.0f;
389
2
    UpperBound = 127.0f;
390
2
    break;
391
4
  case LibFunc_exp2l: // RangeError: (x < -16445 || x > 11383)
392
2
    LowerBound = -16445.0f;
393
2
    UpperBound = 11383.0f;
394
2
    break;
395
4
  default:
396
0
    llvm_unreachable("Unhandled library call!");
397
24
  }
398
24
399
24
  ++NumWrappedTwoCond;
400
24
  return createOrCond(CI, CmpInst::FCMP_OGT, UpperBound, CmpInst::FCMP_OLT,
401
24
                      LowerBound);
402
24
}
403
404
// For pow(x,y), We only handle the following cases:
405
// (1) x is a constant && (x >= 1) && (x < MaxUInt8)
406
//     Cond is: (y > 127)
407
// (2) x is a value coming from an integer type.
408
//   (2.1) if x's bit_size == 8
409
//         Cond: (x <= 0 || y > 128)
410
//   (2.2) if x's bit_size is 16
411
//         Cond: (x <= 0 || y > 64)
412
//   (2.3) if x's bit_size is 32
413
//         Cond: (x <= 0 || y > 32)
414
// Support for powl(x,y) and powf(x,y) are TBD.
415
//
416
// Note that condition can be more conservative than the actual condition
417
// (i.e. we might invoke the calls that will not set the errno.).
418
//
419
Value *LibCallsShrinkWrap::generateCondForPow(CallInst *CI,
420
8
                                              const LibFunc &Func) {
421
8
  // FIXME: LibFunc_powf and powl TBD.
422
8
  if (Func != LibFunc_pow) {
423
0
    LLVM_DEBUG(dbgs() << "Not handled powf() and powl()\n");
424
0
    return nullptr;
425
0
  }
426
8
427
8
  Value *Base = CI->getArgOperand(0);
428
8
  Value *Exp = CI->getArgOperand(1);
429
8
  IRBuilder<> BBBuilder(CI);
430
8
431
8
  // Constant Base case.
432
8
  if (ConstantFP *CF = dyn_cast<ConstantFP>(Base)) {
433
2
    double D = CF->getValueAPF().convertToDouble();
434
2
    if (D < 1.0f || D > APInt::getMaxValue(8).getZExtValue()) {
435
0
      LLVM_DEBUG(dbgs() << "Not handled pow(): constant base out of range\n");
436
0
      return nullptr;
437
0
    }
438
2
439
2
    ++NumWrappedOneCond;
440
2
    Constant *V = ConstantFP::get(CI->getContext(), APFloat(127.0f));
441
2
    if (!Exp->getType()->isFloatTy())
442
2
      V = ConstantExpr::getFPExtend(V, Exp->getType());
443
2
    return BBBuilder.CreateFCmp(CmpInst::FCMP_OGT, Exp, V);
444
2
  }
445
6
446
6
  // If the Base value coming from an integer type.
447
6
  Instruction *I = dyn_cast<Instruction>(Base);
448
6
  if (!I) {
449
0
    LLVM_DEBUG(dbgs() << "Not handled pow(): FP type base\n");
450
0
    return nullptr;
451
0
  }
452
6
  unsigned Opcode = I->getOpcode();
453
6
  if (Opcode == Instruction::UIToFP || 
Opcode == Instruction::SIToFP2
) {
454
6
    unsigned BW = I->getOperand(0)->getType()->getPrimitiveSizeInBits();
455
6
    float UpperV = 0.0f;
456
6
    if (BW == 8)
457
2
      UpperV = 128.0f;
458
4
    else if (BW == 16)
459
2
      UpperV = 64.0f;
460
2
    else if (BW == 32)
461
2
      UpperV = 32.0f;
462
0
    else {
463
0
      LLVM_DEBUG(dbgs() << "Not handled pow(): type too wide\n");
464
0
      return nullptr;
465
0
    }
466
6
467
6
    ++NumWrappedTwoCond;
468
6
    Constant *V = ConstantFP::get(CI->getContext(), APFloat(UpperV));
469
6
    Constant *V0 = ConstantFP::get(CI->getContext(), APFloat(0.0f));
470
6
    if (!Exp->getType()->isFloatTy())
471
6
      V = ConstantExpr::getFPExtend(V, Exp->getType());
472
6
    if (!Base->getType()->isFloatTy())
473
6
      V0 = ConstantExpr::getFPExtend(V0, Exp->getType());
474
6
475
6
    Value *Cond = BBBuilder.CreateFCmp(CmpInst::FCMP_OGT, Exp, V);
476
6
    Value *Cond0 = BBBuilder.CreateFCmp(CmpInst::FCMP_OLE, Base, V0);
477
6
    return BBBuilder.CreateOr(Cond0, Cond);
478
6
  }
479
0
  LLVM_DEBUG(dbgs() << "Not handled pow(): base not from integer convert\n");
480
0
  return nullptr;
481
0
}
482
483
// Wrap conditions that can potentially generate errno to the library call.
484
111
void LibCallsShrinkWrap::shrinkWrapCI(CallInst *CI, Value *Cond) {
485
111
  assert(Cond != nullptr && "ShrinkWrapCI is not expecting an empty call inst");
486
111
  MDNode *BranchWeights =
487
111
      MDBuilder(CI->getContext()).createBranchWeights(1, 2000);
488
111
489
111
  Instruction *NewInst =
490
111
      SplitBlockAndInsertIfThen(Cond, CI, false, BranchWeights, DT);
491
111
  BasicBlock *CallBB = NewInst->getParent();
492
111
  CallBB->setName("cdce.call");
493
111
  BasicBlock *SuccBB = CallBB->getSingleSuccessor();
494
111
  assert(SuccBB && "The split block should have a single successor");
495
111
  SuccBB->setName("cdce.end");
496
111
  CI->removeFromParent();
497
111
  CallBB->getInstList().insert(CallBB->getFirstInsertionPt(), CI);
498
111
  LLVM_DEBUG(dbgs() << "== Basic Block After ==");
499
111
  LLVM_DEBUG(dbgs() << *CallBB->getSinglePredecessor() << *CallBB
500
111
                    << *CallBB->getSingleSuccessor() << "\n");
501
111
}
502
503
// Perform the transformation to a single candidate.
504
115
bool LibCallsShrinkWrap::perform(CallInst *CI) {
505
115
  LibFunc Func;
506
115
  Function *Callee = CI->getCalledFunction();
507
115
  assert(Callee && "perform() should apply to a non-empty callee");
508
115
  TLI.getLibFunc(*Callee, Func);
509
115
  assert(Func && "perform() is not expecting an empty function");
510
115
511
115
  if (performCallDomainErrorOnly(CI, Func) || 
performCallRangeErrorOnly(CI, Func)78
)
512
67
    return true;
513
48
  return performCallErrors(CI, Func);
514
48
}
515
516
11.6k
void LibCallsShrinkWrapLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
517
11.6k
  AU.addPreserved<DominatorTreeWrapperPass>();
518
11.6k
  AU.addPreserved<GlobalsAAWrapperPass>();
519
11.6k
  AU.addRequired<TargetLibraryInfoWrapperPass>();
520
11.6k
}
521
522
static bool runImpl(Function &F, const TargetLibraryInfo &TLI,
523
463k
                    DominatorTree *DT) {
524
463k
  if (F.hasFnAttribute(Attribute::OptimizeForSize))
525
1
    return false;
526
463k
  LibCallsShrinkWrap CCDCE(TLI, DT);
527
463k
  CCDCE.visit(F);
528
463k
  bool Changed = CCDCE.perform();
529
463k
530
463k
// Verify the dominator after we've updated it locally.
531
463k
  assert(!DT || DT->verify(DominatorTree::VerificationLevel::Fast));
532
463k
  return Changed;
533
463k
}
534
535
462k
bool LibCallsShrinkWrapLegacyPass::runOnFunction(Function &F) {
536
462k
  auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
537
462k
  auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
538
18.4E
  auto *DT = DTWP ? 
&DTWP->getDomTree()462k
: nullptr;
539
462k
  return runImpl(F, TLI, DT);
540
462k
}
541
542
namespace llvm {
543
char &LibCallsShrinkWrapPassID = LibCallsShrinkWrapLegacyPass::ID;
544
545
// Public interface to LibCallsShrinkWrap pass.
546
11.6k
FunctionPass *createLibCallsShrinkWrapPass() {
547
11.6k
  return new LibCallsShrinkWrapLegacyPass();
548
11.6k
}
549
550
PreservedAnalyses LibCallsShrinkWrapPass::run(Function &F,
551
1.12k
                                              FunctionAnalysisManager &FAM) {
552
1.12k
  auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
553
1.12k
  auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
554
1.12k
  if (!runImpl(F, TLI, DT))
555
1.11k
    return PreservedAnalyses::all();
556
7
  auto PA = PreservedAnalyses();
557
7
  PA.preserve<GlobalsAA>();
558
7
  PA.preserve<DominatorTreeAnalysis>();
559
7
  return PA;
560
7
}
561
}