LibCallsShrinkWrap.cpp 19 KB

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