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