1311116Sdim//===-- LibCallsShrinkWrap.cpp ----------------------------------*- C++ -*-===//
2311116Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6311116Sdim//
7311116Sdim//===----------------------------------------------------------------------===//
8311116Sdim//
9311116Sdim// This pass shrink-wraps a call to function if the result is not used.
10311116Sdim// The call can set errno but is otherwise side effect free. For example:
11311116Sdim//    sqrt(val);
12311116Sdim//  is transformed to
13311116Sdim//    if (val < 0)
14311116Sdim//      sqrt(val);
15311116Sdim//  Even if the result of library call is not being used, the compiler cannot
16311116Sdim//  safely delete the call because the function can set errno on error
17311116Sdim//  conditions.
18311116Sdim//  Note in many functions, the error condition solely depends on the incoming
19311116Sdim//  parameter. In this optimization, we can generate the condition can lead to
20311116Sdim//  the errno to shrink-wrap the call. Since the chances of hitting the error
21311116Sdim//  condition is low, the runtime call is effectively eliminated.
22311116Sdim//
23311116Sdim//  These partially dead calls are usually results of C++ abstraction penalty
24311116Sdim//  exposed by inlining.
25311116Sdim//
26311116Sdim//===----------------------------------------------------------------------===//
27311116Sdim
28311116Sdim#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
29311116Sdim#include "llvm/ADT/SmallVector.h"
30311116Sdim#include "llvm/ADT/Statistic.h"
31311116Sdim#include "llvm/Analysis/GlobalsModRef.h"
32311116Sdim#include "llvm/Analysis/TargetLibraryInfo.h"
33311116Sdim#include "llvm/IR/CFG.h"
34311116Sdim#include "llvm/IR/Constants.h"
35321369Sdim#include "llvm/IR/Dominators.h"
36311116Sdim#include "llvm/IR/Function.h"
37311116Sdim#include "llvm/IR/IRBuilder.h"
38311116Sdim#include "llvm/IR/InstVisitor.h"
39311116Sdim#include "llvm/IR/Instructions.h"
40311116Sdim#include "llvm/IR/LLVMContext.h"
41311116Sdim#include "llvm/IR/MDBuilder.h"
42360784Sdim#include "llvm/InitializePasses.h"
43311116Sdim#include "llvm/Pass.h"
44311116Sdim#include "llvm/Transforms/Utils/BasicBlockUtils.h"
45311116Sdimusing namespace llvm;
46311116Sdim
47311116Sdim#define DEBUG_TYPE "libcalls-shrinkwrap"
48311116Sdim
49311116SdimSTATISTIC(NumWrappedOneCond, "Number of One-Condition Wrappers Inserted");
50311116SdimSTATISTIC(NumWrappedTwoCond, "Number of Two-Condition Wrappers Inserted");
51311116Sdim
52311116Sdimnamespace {
53311116Sdimclass LibCallsShrinkWrapLegacyPass : public FunctionPass {
54311116Sdimpublic:
55311116Sdim  static char ID; // Pass identification, replacement for typeid
56311116Sdim  explicit LibCallsShrinkWrapLegacyPass() : FunctionPass(ID) {
57311116Sdim    initializeLibCallsShrinkWrapLegacyPassPass(
58311116Sdim        *PassRegistry::getPassRegistry());
59311116Sdim  }
60311116Sdim  void getAnalysisUsage(AnalysisUsage &AU) const override;
61311116Sdim  bool runOnFunction(Function &F) override;
62311116Sdim};
63311116Sdim}
64311116Sdim
65311116Sdimchar LibCallsShrinkWrapLegacyPass::ID = 0;
66311116SdimINITIALIZE_PASS_BEGIN(LibCallsShrinkWrapLegacyPass, "libcalls-shrinkwrap",
67311116Sdim                      "Conditionally eliminate dead library calls", false,
68311116Sdim                      false)
69311116SdimINITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
70311116SdimINITIALIZE_PASS_END(LibCallsShrinkWrapLegacyPass, "libcalls-shrinkwrap",
71311116Sdim                    "Conditionally eliminate dead library calls", false, false)
72311116Sdim
73311116Sdimnamespace {
74311116Sdimclass LibCallsShrinkWrap : public InstVisitor<LibCallsShrinkWrap> {
75311116Sdimpublic:
76321369Sdim  LibCallsShrinkWrap(const TargetLibraryInfo &TLI, DominatorTree *DT)
77321369Sdim      : TLI(TLI), DT(DT){};
78311116Sdim  void visitCallInst(CallInst &CI) { checkCandidate(CI); }
79321369Sdim  bool perform() {
80321369Sdim    bool Changed = false;
81311116Sdim    for (auto &CI : WorkList) {
82341825Sdim      LLVM_DEBUG(dbgs() << "CDCE calls: " << CI->getCalledFunction()->getName()
83341825Sdim                        << "\n");
84311116Sdim      if (perform(CI)) {
85311116Sdim        Changed = true;
86341825Sdim        LLVM_DEBUG(dbgs() << "Transformed\n");
87311116Sdim      }
88311116Sdim    }
89321369Sdim    return Changed;
90311116Sdim  }
91311116Sdim
92311116Sdimprivate:
93311116Sdim  bool perform(CallInst *CI);
94311116Sdim  void checkCandidate(CallInst &CI);
95311116Sdim  void shrinkWrapCI(CallInst *CI, Value *Cond);
96321369Sdim  bool performCallDomainErrorOnly(CallInst *CI, const LibFunc &Func);
97321369Sdim  bool performCallErrors(CallInst *CI, const LibFunc &Func);
98321369Sdim  bool performCallRangeErrorOnly(CallInst *CI, const LibFunc &Func);
99321369Sdim  Value *generateOneRangeCond(CallInst *CI, const LibFunc &Func);
100321369Sdim  Value *generateTwoRangeCond(CallInst *CI, const LibFunc &Func);
101321369Sdim  Value *generateCondForPow(CallInst *CI, const LibFunc &Func);
102311116Sdim
103311116Sdim  // Create an OR of two conditions.
104311116Sdim  Value *createOrCond(CallInst *CI, CmpInst::Predicate Cmp, float Val,
105311116Sdim                      CmpInst::Predicate Cmp2, float Val2) {
106311116Sdim    IRBuilder<> BBBuilder(CI);
107311116Sdim    Value *Arg = CI->getArgOperand(0);
108311116Sdim    auto Cond2 = createCond(BBBuilder, Arg, Cmp2, Val2);
109311116Sdim    auto Cond1 = createCond(BBBuilder, Arg, Cmp, Val);
110311116Sdim    return BBBuilder.CreateOr(Cond1, Cond2);
111311116Sdim  }
112311116Sdim
113311116Sdim  // Create a single condition using IRBuilder.
114311116Sdim  Value *createCond(IRBuilder<> &BBBuilder, Value *Arg, CmpInst::Predicate Cmp,
115311116Sdim                    float Val) {
116311116Sdim    Constant *V = ConstantFP::get(BBBuilder.getContext(), APFloat(Val));
117311116Sdim    if (!Arg->getType()->isFloatTy())
118311116Sdim      V = ConstantExpr::getFPExtend(V, Arg->getType());
119311116Sdim    return BBBuilder.CreateFCmp(Cmp, Arg, V);
120311116Sdim  }
121311116Sdim
122311116Sdim  // Create a single condition.
123311116Sdim  Value *createCond(CallInst *CI, CmpInst::Predicate Cmp, float Val) {
124311116Sdim    IRBuilder<> BBBuilder(CI);
125311116Sdim    Value *Arg = CI->getArgOperand(0);
126311116Sdim    return createCond(BBBuilder, Arg, Cmp, Val);
127311116Sdim  }
128311116Sdim
129311116Sdim  const TargetLibraryInfo &TLI;
130321369Sdim  DominatorTree *DT;
131311116Sdim  SmallVector<CallInst *, 16> WorkList;
132311116Sdim};
133311116Sdim} // end anonymous namespace
134311116Sdim
135311116Sdim// Perform the transformation to calls with errno set by domain error.
136311116Sdimbool LibCallsShrinkWrap::performCallDomainErrorOnly(CallInst *CI,
137321369Sdim                                                    const LibFunc &Func) {
138311116Sdim  Value *Cond = nullptr;
139311116Sdim
140311116Sdim  switch (Func) {
141321369Sdim  case LibFunc_acos:  // DomainError: (x < -1 || x > 1)
142321369Sdim  case LibFunc_acosf: // Same as acos
143321369Sdim  case LibFunc_acosl: // Same as acos
144321369Sdim  case LibFunc_asin:  // DomainError: (x < -1 || x > 1)
145321369Sdim  case LibFunc_asinf: // Same as asin
146321369Sdim  case LibFunc_asinl: // Same as asin
147311116Sdim  {
148311116Sdim    ++NumWrappedTwoCond;
149311116Sdim    Cond = createOrCond(CI, CmpInst::FCMP_OLT, -1.0f, CmpInst::FCMP_OGT, 1.0f);
150311116Sdim    break;
151311116Sdim  }
152321369Sdim  case LibFunc_cos:  // DomainError: (x == +inf || x == -inf)
153321369Sdim  case LibFunc_cosf: // Same as cos
154321369Sdim  case LibFunc_cosl: // Same as cos
155321369Sdim  case LibFunc_sin:  // DomainError: (x == +inf || x == -inf)
156321369Sdim  case LibFunc_sinf: // Same as sin
157321369Sdim  case LibFunc_sinl: // Same as sin
158311116Sdim  {
159311116Sdim    ++NumWrappedTwoCond;
160311116Sdim    Cond = createOrCond(CI, CmpInst::FCMP_OEQ, INFINITY, CmpInst::FCMP_OEQ,
161311116Sdim                        -INFINITY);
162311116Sdim    break;
163311116Sdim  }
164321369Sdim  case LibFunc_acosh:  // DomainError: (x < 1)
165321369Sdim  case LibFunc_acoshf: // Same as acosh
166321369Sdim  case LibFunc_acoshl: // Same as acosh
167311116Sdim  {
168311116Sdim    ++NumWrappedOneCond;
169311116Sdim    Cond = createCond(CI, CmpInst::FCMP_OLT, 1.0f);
170311116Sdim    break;
171311116Sdim  }
172321369Sdim  case LibFunc_sqrt:  // DomainError: (x < 0)
173321369Sdim  case LibFunc_sqrtf: // Same as sqrt
174321369Sdim  case LibFunc_sqrtl: // Same as sqrt
175311116Sdim  {
176311116Sdim    ++NumWrappedOneCond;
177311116Sdim    Cond = createCond(CI, CmpInst::FCMP_OLT, 0.0f);
178311116Sdim    break;
179311116Sdim  }
180311116Sdim  default:
181311116Sdim    return false;
182311116Sdim  }
183311116Sdim  shrinkWrapCI(CI, Cond);
184311116Sdim  return true;
185311116Sdim}
186311116Sdim
187311116Sdim// Perform the transformation to calls with errno set by range error.
188311116Sdimbool LibCallsShrinkWrap::performCallRangeErrorOnly(CallInst *CI,
189321369Sdim                                                   const LibFunc &Func) {
190311116Sdim  Value *Cond = nullptr;
191311116Sdim
192311116Sdim  switch (Func) {
193321369Sdim  case LibFunc_cosh:
194321369Sdim  case LibFunc_coshf:
195321369Sdim  case LibFunc_coshl:
196321369Sdim  case LibFunc_exp:
197321369Sdim  case LibFunc_expf:
198321369Sdim  case LibFunc_expl:
199321369Sdim  case LibFunc_exp10:
200321369Sdim  case LibFunc_exp10f:
201321369Sdim  case LibFunc_exp10l:
202321369Sdim  case LibFunc_exp2:
203321369Sdim  case LibFunc_exp2f:
204321369Sdim  case LibFunc_exp2l:
205321369Sdim  case LibFunc_sinh:
206321369Sdim  case LibFunc_sinhf:
207321369Sdim  case LibFunc_sinhl: {
208311116Sdim    Cond = generateTwoRangeCond(CI, Func);
209311116Sdim    break;
210311116Sdim  }
211321369Sdim  case LibFunc_expm1:  // RangeError: (709, inf)
212321369Sdim  case LibFunc_expm1f: // RangeError: (88, inf)
213321369Sdim  case LibFunc_expm1l: // RangeError: (11356, inf)
214311116Sdim  {
215311116Sdim    Cond = generateOneRangeCond(CI, Func);
216311116Sdim    break;
217311116Sdim  }
218311116Sdim  default:
219311116Sdim    return false;
220311116Sdim  }
221311116Sdim  shrinkWrapCI(CI, Cond);
222311116Sdim  return true;
223311116Sdim}
224311116Sdim
225311116Sdim// Perform the transformation to calls with errno set by combination of errors.
226311116Sdimbool LibCallsShrinkWrap::performCallErrors(CallInst *CI,
227321369Sdim                                           const LibFunc &Func) {
228311116Sdim  Value *Cond = nullptr;
229311116Sdim
230311116Sdim  switch (Func) {
231321369Sdim  case LibFunc_atanh:  // DomainError: (x < -1 || x > 1)
232311116Sdim                        // PoleError:   (x == -1 || x == 1)
233311116Sdim                        // Overall Cond: (x <= -1 || x >= 1)
234321369Sdim  case LibFunc_atanhf: // Same as atanh
235321369Sdim  case LibFunc_atanhl: // Same as atanh
236311116Sdim  {
237311116Sdim    ++NumWrappedTwoCond;
238311116Sdim    Cond = createOrCond(CI, CmpInst::FCMP_OLE, -1.0f, CmpInst::FCMP_OGE, 1.0f);
239311116Sdim    break;
240311116Sdim  }
241321369Sdim  case LibFunc_log:    // DomainError: (x < 0)
242311116Sdim                        // PoleError:   (x == 0)
243311116Sdim                        // Overall Cond: (x <= 0)
244321369Sdim  case LibFunc_logf:   // Same as log
245321369Sdim  case LibFunc_logl:   // Same as log
246321369Sdim  case LibFunc_log10:  // Same as log
247321369Sdim  case LibFunc_log10f: // Same as log
248321369Sdim  case LibFunc_log10l: // Same as log
249321369Sdim  case LibFunc_log2:   // Same as log
250321369Sdim  case LibFunc_log2f:  // Same as log
251321369Sdim  case LibFunc_log2l:  // Same as log
252321369Sdim  case LibFunc_logb:   // Same as log
253321369Sdim  case LibFunc_logbf:  // Same as log
254321369Sdim  case LibFunc_logbl:  // Same as log
255311116Sdim  {
256311116Sdim    ++NumWrappedOneCond;
257311116Sdim    Cond = createCond(CI, CmpInst::FCMP_OLE, 0.0f);
258311116Sdim    break;
259311116Sdim  }
260321369Sdim  case LibFunc_log1p:  // DomainError: (x < -1)
261311116Sdim                        // PoleError:   (x == -1)
262311116Sdim                        // Overall Cond: (x <= -1)
263321369Sdim  case LibFunc_log1pf: // Same as log1p
264321369Sdim  case LibFunc_log1pl: // Same as log1p
265311116Sdim  {
266311116Sdim    ++NumWrappedOneCond;
267311116Sdim    Cond = createCond(CI, CmpInst::FCMP_OLE, -1.0f);
268311116Sdim    break;
269311116Sdim  }
270321369Sdim  case LibFunc_pow: // DomainError: x < 0 and y is noninteger
271311116Sdim                     // PoleError:   x == 0 and y < 0
272311116Sdim                     // RangeError:  overflow or underflow
273321369Sdim  case LibFunc_powf:
274321369Sdim  case LibFunc_powl: {
275311116Sdim    Cond = generateCondForPow(CI, Func);
276311116Sdim    if (Cond == nullptr)
277311116Sdim      return false;
278311116Sdim    break;
279311116Sdim  }
280311116Sdim  default:
281311116Sdim    return false;
282311116Sdim  }
283311116Sdim  assert(Cond && "performCallErrors should not see an empty condition");
284311116Sdim  shrinkWrapCI(CI, Cond);
285311116Sdim  return true;
286311116Sdim}
287311116Sdim
288311116Sdim// Checks if CI is a candidate for shrinkwrapping and put it into work list if
289311116Sdim// true.
290311116Sdimvoid LibCallsShrinkWrap::checkCandidate(CallInst &CI) {
291311116Sdim  if (CI.isNoBuiltin())
292311116Sdim    return;
293311116Sdim  // A possible improvement is to handle the calls with the return value being
294311116Sdim  // used. If there is API for fast libcall implementation without setting
295311116Sdim  // errno, we can use the same framework to direct/wrap the call to the fast
296311116Sdim  // API in the error free path, and leave the original call in the slow path.
297311116Sdim  if (!CI.use_empty())
298311116Sdim    return;
299311116Sdim
300321369Sdim  LibFunc Func;
301311116Sdim  Function *Callee = CI.getCalledFunction();
302311116Sdim  if (!Callee)
303311116Sdim    return;
304311116Sdim  if (!TLI.getLibFunc(*Callee, Func) || !TLI.has(Func))
305311116Sdim    return;
306311116Sdim
307311116Sdim  if (CI.getNumArgOperands() == 0)
308311116Sdim    return;
309311116Sdim  // TODO: Handle long double in other formats.
310311116Sdim  Type *ArgType = CI.getArgOperand(0)->getType();
311311116Sdim  if (!(ArgType->isFloatTy() || ArgType->isDoubleTy() ||
312311116Sdim        ArgType->isX86_FP80Ty()))
313311116Sdim    return;
314311116Sdim
315311116Sdim  WorkList.push_back(&CI);
316311116Sdim}
317311116Sdim
318311116Sdim// Generate the upper bound condition for RangeError.
319311116SdimValue *LibCallsShrinkWrap::generateOneRangeCond(CallInst *CI,
320321369Sdim                                                const LibFunc &Func) {
321311116Sdim  float UpperBound;
322311116Sdim  switch (Func) {
323321369Sdim  case LibFunc_expm1: // RangeError: (709, inf)
324311116Sdim    UpperBound = 709.0f;
325311116Sdim    break;
326321369Sdim  case LibFunc_expm1f: // RangeError: (88, inf)
327311116Sdim    UpperBound = 88.0f;
328311116Sdim    break;
329321369Sdim  case LibFunc_expm1l: // RangeError: (11356, inf)
330311116Sdim    UpperBound = 11356.0f;
331311116Sdim    break;
332311116Sdim  default:
333321369Sdim    llvm_unreachable("Unhandled library call!");
334311116Sdim  }
335311116Sdim
336311116Sdim  ++NumWrappedOneCond;
337311116Sdim  return createCond(CI, CmpInst::FCMP_OGT, UpperBound);
338311116Sdim}
339311116Sdim
340311116Sdim// Generate the lower and upper bound condition for RangeError.
341311116SdimValue *LibCallsShrinkWrap::generateTwoRangeCond(CallInst *CI,
342321369Sdim                                                const LibFunc &Func) {
343311116Sdim  float UpperBound, LowerBound;
344311116Sdim  switch (Func) {
345321369Sdim  case LibFunc_cosh: // RangeError: (x < -710 || x > 710)
346321369Sdim  case LibFunc_sinh: // Same as cosh
347311116Sdim    LowerBound = -710.0f;
348311116Sdim    UpperBound = 710.0f;
349311116Sdim    break;
350321369Sdim  case LibFunc_coshf: // RangeError: (x < -89 || x > 89)
351321369Sdim  case LibFunc_sinhf: // Same as coshf
352311116Sdim    LowerBound = -89.0f;
353311116Sdim    UpperBound = 89.0f;
354311116Sdim    break;
355321369Sdim  case LibFunc_coshl: // RangeError: (x < -11357 || x > 11357)
356321369Sdim  case LibFunc_sinhl: // Same as coshl
357311116Sdim    LowerBound = -11357.0f;
358311116Sdim    UpperBound = 11357.0f;
359311116Sdim    break;
360321369Sdim  case LibFunc_exp: // RangeError: (x < -745 || x > 709)
361311116Sdim    LowerBound = -745.0f;
362311116Sdim    UpperBound = 709.0f;
363311116Sdim    break;
364321369Sdim  case LibFunc_expf: // RangeError: (x < -103 || x > 88)
365311116Sdim    LowerBound = -103.0f;
366311116Sdim    UpperBound = 88.0f;
367311116Sdim    break;
368321369Sdim  case LibFunc_expl: // RangeError: (x < -11399 || x > 11356)
369311116Sdim    LowerBound = -11399.0f;
370311116Sdim    UpperBound = 11356.0f;
371311116Sdim    break;
372321369Sdim  case LibFunc_exp10: // RangeError: (x < -323 || x > 308)
373311116Sdim    LowerBound = -323.0f;
374311116Sdim    UpperBound = 308.0f;
375311116Sdim    break;
376321369Sdim  case LibFunc_exp10f: // RangeError: (x < -45 || x > 38)
377311116Sdim    LowerBound = -45.0f;
378311116Sdim    UpperBound = 38.0f;
379311116Sdim    break;
380321369Sdim  case LibFunc_exp10l: // RangeError: (x < -4950 || x > 4932)
381311116Sdim    LowerBound = -4950.0f;
382311116Sdim    UpperBound = 4932.0f;
383311116Sdim    break;
384321369Sdim  case LibFunc_exp2: // RangeError: (x < -1074 || x > 1023)
385311116Sdim    LowerBound = -1074.0f;
386311116Sdim    UpperBound = 1023.0f;
387311116Sdim    break;
388321369Sdim  case LibFunc_exp2f: // RangeError: (x < -149 || x > 127)
389311116Sdim    LowerBound = -149.0f;
390311116Sdim    UpperBound = 127.0f;
391311116Sdim    break;
392321369Sdim  case LibFunc_exp2l: // RangeError: (x < -16445 || x > 11383)
393311116Sdim    LowerBound = -16445.0f;
394311116Sdim    UpperBound = 11383.0f;
395311116Sdim    break;
396311116Sdim  default:
397321369Sdim    llvm_unreachable("Unhandled library call!");
398311116Sdim  }
399311116Sdim
400311116Sdim  ++NumWrappedTwoCond;
401311116Sdim  return createOrCond(CI, CmpInst::FCMP_OGT, UpperBound, CmpInst::FCMP_OLT,
402311116Sdim                      LowerBound);
403311116Sdim}
404311116Sdim
405311116Sdim// For pow(x,y), We only handle the following cases:
406311116Sdim// (1) x is a constant && (x >= 1) && (x < MaxUInt8)
407311116Sdim//     Cond is: (y > 127)
408311116Sdim// (2) x is a value coming from an integer type.
409311116Sdim//   (2.1) if x's bit_size == 8
410311116Sdim//         Cond: (x <= 0 || y > 128)
411311116Sdim//   (2.2) if x's bit_size is 16
412311116Sdim//         Cond: (x <= 0 || y > 64)
413311116Sdim//   (2.3) if x's bit_size is 32
414311116Sdim//         Cond: (x <= 0 || y > 32)
415311116Sdim// Support for powl(x,y) and powf(x,y) are TBD.
416311116Sdim//
417311116Sdim// Note that condition can be more conservative than the actual condition
418311116Sdim// (i.e. we might invoke the calls that will not set the errno.).
419311116Sdim//
420311116SdimValue *LibCallsShrinkWrap::generateCondForPow(CallInst *CI,
421321369Sdim                                              const LibFunc &Func) {
422321369Sdim  // FIXME: LibFunc_powf and powl TBD.
423321369Sdim  if (Func != LibFunc_pow) {
424341825Sdim    LLVM_DEBUG(dbgs() << "Not handled powf() and powl()\n");
425311116Sdim    return nullptr;
426311116Sdim  }
427311116Sdim
428311116Sdim  Value *Base = CI->getArgOperand(0);
429311116Sdim  Value *Exp = CI->getArgOperand(1);
430311116Sdim  IRBuilder<> BBBuilder(CI);
431311116Sdim
432311116Sdim  // Constant Base case.
433311116Sdim  if (ConstantFP *CF = dyn_cast<ConstantFP>(Base)) {
434311116Sdim    double D = CF->getValueAPF().convertToDouble();
435311116Sdim    if (D < 1.0f || D > APInt::getMaxValue(8).getZExtValue()) {
436341825Sdim      LLVM_DEBUG(dbgs() << "Not handled pow(): constant base out of range\n");
437311116Sdim      return nullptr;
438311116Sdim    }
439311116Sdim
440311116Sdim    ++NumWrappedOneCond;
441311116Sdim    Constant *V = ConstantFP::get(CI->getContext(), APFloat(127.0f));
442311116Sdim    if (!Exp->getType()->isFloatTy())
443311116Sdim      V = ConstantExpr::getFPExtend(V, Exp->getType());
444311116Sdim    return BBBuilder.CreateFCmp(CmpInst::FCMP_OGT, Exp, V);
445311116Sdim  }
446311116Sdim
447311116Sdim  // If the Base value coming from an integer type.
448311116Sdim  Instruction *I = dyn_cast<Instruction>(Base);
449311116Sdim  if (!I) {
450341825Sdim    LLVM_DEBUG(dbgs() << "Not handled pow(): FP type base\n");
451311116Sdim    return nullptr;
452311116Sdim  }
453311116Sdim  unsigned Opcode = I->getOpcode();
454311116Sdim  if (Opcode == Instruction::UIToFP || Opcode == Instruction::SIToFP) {
455311116Sdim    unsigned BW = I->getOperand(0)->getType()->getPrimitiveSizeInBits();
456311116Sdim    float UpperV = 0.0f;
457311116Sdim    if (BW == 8)
458311116Sdim      UpperV = 128.0f;
459311116Sdim    else if (BW == 16)
460311116Sdim      UpperV = 64.0f;
461311116Sdim    else if (BW == 32)
462311116Sdim      UpperV = 32.0f;
463311116Sdim    else {
464341825Sdim      LLVM_DEBUG(dbgs() << "Not handled pow(): type too wide\n");
465311116Sdim      return nullptr;
466311116Sdim    }
467311116Sdim
468311116Sdim    ++NumWrappedTwoCond;
469311116Sdim    Constant *V = ConstantFP::get(CI->getContext(), APFloat(UpperV));
470311116Sdim    Constant *V0 = ConstantFP::get(CI->getContext(), APFloat(0.0f));
471311116Sdim    if (!Exp->getType()->isFloatTy())
472311116Sdim      V = ConstantExpr::getFPExtend(V, Exp->getType());
473311116Sdim    if (!Base->getType()->isFloatTy())
474311116Sdim      V0 = ConstantExpr::getFPExtend(V0, Exp->getType());
475311116Sdim
476311116Sdim    Value *Cond = BBBuilder.CreateFCmp(CmpInst::FCMP_OGT, Exp, V);
477311116Sdim    Value *Cond0 = BBBuilder.CreateFCmp(CmpInst::FCMP_OLE, Base, V0);
478311116Sdim    return BBBuilder.CreateOr(Cond0, Cond);
479311116Sdim  }
480341825Sdim  LLVM_DEBUG(dbgs() << "Not handled pow(): base not from integer convert\n");
481311116Sdim  return nullptr;
482311116Sdim}
483311116Sdim
484311116Sdim// Wrap conditions that can potentially generate errno to the library call.
485311116Sdimvoid LibCallsShrinkWrap::shrinkWrapCI(CallInst *CI, Value *Cond) {
486321369Sdim  assert(Cond != nullptr && "ShrinkWrapCI is not expecting an empty call inst");
487311116Sdim  MDNode *BranchWeights =
488311116Sdim      MDBuilder(CI->getContext()).createBranchWeights(1, 2000);
489321369Sdim
490344779Sdim  Instruction *NewInst =
491321369Sdim      SplitBlockAndInsertIfThen(Cond, CI, false, BranchWeights, DT);
492311116Sdim  BasicBlock *CallBB = NewInst->getParent();
493311116Sdim  CallBB->setName("cdce.call");
494321369Sdim  BasicBlock *SuccBB = CallBB->getSingleSuccessor();
495321369Sdim  assert(SuccBB && "The split block should have a single successor");
496321369Sdim  SuccBB->setName("cdce.end");
497311116Sdim  CI->removeFromParent();
498311116Sdim  CallBB->getInstList().insert(CallBB->getFirstInsertionPt(), CI);
499341825Sdim  LLVM_DEBUG(dbgs() << "== Basic Block After ==");
500341825Sdim  LLVM_DEBUG(dbgs() << *CallBB->getSinglePredecessor() << *CallBB
501341825Sdim                    << *CallBB->getSingleSuccessor() << "\n");
502311116Sdim}
503311116Sdim
504311116Sdim// Perform the transformation to a single candidate.
505311116Sdimbool LibCallsShrinkWrap::perform(CallInst *CI) {
506321369Sdim  LibFunc Func;
507311116Sdim  Function *Callee = CI->getCalledFunction();
508311116Sdim  assert(Callee && "perform() should apply to a non-empty callee");
509311116Sdim  TLI.getLibFunc(*Callee, Func);
510311116Sdim  assert(Func && "perform() is not expecting an empty function");
511311116Sdim
512321369Sdim  if (performCallDomainErrorOnly(CI, Func) || performCallRangeErrorOnly(CI, Func))
513311116Sdim    return true;
514311116Sdim  return performCallErrors(CI, Func);
515311116Sdim}
516311116Sdim
517311116Sdimvoid LibCallsShrinkWrapLegacyPass::getAnalysisUsage(AnalysisUsage &AU) const {
518321369Sdim  AU.addPreserved<DominatorTreeWrapperPass>();
519311116Sdim  AU.addPreserved<GlobalsAAWrapperPass>();
520311116Sdim  AU.addRequired<TargetLibraryInfoWrapperPass>();
521311116Sdim}
522311116Sdim
523321369Sdimstatic bool runImpl(Function &F, const TargetLibraryInfo &TLI,
524321369Sdim                    DominatorTree *DT) {
525311116Sdim  if (F.hasFnAttribute(Attribute::OptimizeForSize))
526311116Sdim    return false;
527321369Sdim  LibCallsShrinkWrap CCDCE(TLI, DT);
528311116Sdim  CCDCE.visit(F);
529321369Sdim  bool Changed = CCDCE.perform();
530321369Sdim
531321369Sdim// Verify the dominator after we've updated it locally.
532341825Sdim  assert(!DT || DT->verify(DominatorTree::VerificationLevel::Fast));
533321369Sdim  return Changed;
534311116Sdim}
535311116Sdim
536311116Sdimbool LibCallsShrinkWrapLegacyPass::runOnFunction(Function &F) {
537360784Sdim  auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
538321369Sdim  auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
539321369Sdim  auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
540321369Sdim  return runImpl(F, TLI, DT);
541311116Sdim}
542311116Sdim
543311116Sdimnamespace llvm {
544311116Sdimchar &LibCallsShrinkWrapPassID = LibCallsShrinkWrapLegacyPass::ID;
545311116Sdim
546311116Sdim// Public interface to LibCallsShrinkWrap pass.
547311116SdimFunctionPass *createLibCallsShrinkWrapPass() {
548311116Sdim  return new LibCallsShrinkWrapLegacyPass();
549311116Sdim}
550311116Sdim
551311116SdimPreservedAnalyses LibCallsShrinkWrapPass::run(Function &F,
552311116Sdim                                              FunctionAnalysisManager &FAM) {
553311116Sdim  auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
554321369Sdim  auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
555321369Sdim  if (!runImpl(F, TLI, DT))
556311116Sdim    return PreservedAnalyses::all();
557311116Sdim  auto PA = PreservedAnalyses();
558311116Sdim  PA.preserve<GlobalsAA>();
559321369Sdim  PA.preserve<DominatorTreeAnalysis>();
560311116Sdim  return PA;
561311116Sdim}
562311116Sdim}
563