| Richard Sandiford | 37cd6cf | 2013-08-23 10:27:02 +0000 | [diff] [blame] | 1 | //===--- PartiallyInlineLibCalls.cpp - Partially inline libcalls ----------===// |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| Richard Sandiford | 37cd6cf | 2013-08-23 10:27:02 +0000 | [diff] [blame] | 9 | // This pass tries to partially inline the fast path of well-known library |
| 10 | // functions, such as using square-root instructions for cases where sqrt() |
| 11 | // does not need to set errno. |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| Davide Italiano | 1021c68 | 2016-05-25 23:38:53 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h" |
| Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| Richard Sandiford | 37cd6cf | 2013-08-23 10:27:02 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/TargetTransformInfo.h" |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 18 | #include "llvm/IR/IRBuilder.h" |
| George Burgess IV | d565b0f | 2018-09-13 20:33:04 +0000 | [diff] [blame] | 19 | #include "llvm/Support/DebugCounter.h" |
| Richard Sandiford | 37cd6cf | 2013-08-23 10:27:02 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Scalar.h" |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 25 | #define DEBUG_TYPE "partially-inline-libcalls" |
| 26 | |
| George Burgess IV | d565b0f | 2018-09-13 20:33:04 +0000 | [diff] [blame] | 27 | DEBUG_COUNTER(PILCounter, "partially-inline-libcalls-transform", |
| 28 | "Controls transformations in partially-inline-libcalls"); |
| Andrew Kaylor | 50271f7 | 2016-05-03 22:32:30 +0000 | [diff] [blame] | 29 | |
| Davide Italiano | 08713bd | 2016-05-20 15:43:39 +0000 | [diff] [blame] | 30 | static bool optimizeSQRT(CallInst *Call, Function *CalledFunc, |
| Sanjay Patel | 0de1a4b | 2017-11-27 21:15:43 +0000 | [diff] [blame] | 31 | BasicBlock &CurrBB, Function::iterator &BB, |
| 32 | const TargetTransformInfo *TTI) { |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 33 | // There is no need to change the IR, since backend will emit sqrt |
| 34 | // instruction if the call has already been marked read-only. |
| 35 | if (Call->onlyReadsMemory()) |
| 36 | return false; |
| 37 | |
| George Burgess IV | d565b0f | 2018-09-13 20:33:04 +0000 | [diff] [blame] | 38 | if (!DebugCounter::shouldExecute(PILCounter)) |
| 39 | return false; |
| 40 | |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 41 | // Do the following transformation: |
| 42 | // |
| 43 | // (before) |
| 44 | // dst = sqrt(src) |
| 45 | // |
| 46 | // (after) |
| 47 | // v0 = sqrt_noreadmem(src) # native sqrt instruction. |
| Sanjay Patel | 0de1a4b | 2017-11-27 21:15:43 +0000 | [diff] [blame] | 48 | // [if (v0 is a NaN) || if (src < 0)] |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 49 | // v1 = sqrt(src) # library call. |
| 50 | // dst = phi(v0, v1) |
| 51 | // |
| 52 | |
| 53 | // Move all instructions following Call to newly created block JoinBB. |
| 54 | // Create phi and replace all uses. |
| Chandler Carruth | 32c52c7 | 2015-01-18 02:39:37 +0000 | [diff] [blame] | 55 | BasicBlock *JoinBB = llvm::SplitBlock(&CurrBB, Call->getNextNode()); |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 56 | IRBuilder<> Builder(JoinBB, JoinBB->begin()); |
| Sanjay Patel | 0de1a4b | 2017-11-27 21:15:43 +0000 | [diff] [blame] | 57 | Type *Ty = Call->getType(); |
| 58 | PHINode *Phi = Builder.CreatePHI(Ty, 2); |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 59 | Call->replaceAllUsesWith(Phi); |
| 60 | |
| 61 | // Create basic block LibCallBB and insert a call to library function sqrt. |
| 62 | BasicBlock *LibCallBB = BasicBlock::Create(CurrBB.getContext(), "call.sqrt", |
| 63 | CurrBB.getParent(), JoinBB); |
| 64 | Builder.SetInsertPoint(LibCallBB); |
| 65 | Instruction *LibCall = Call->clone(); |
| 66 | Builder.Insert(LibCall); |
| 67 | Builder.CreateBr(JoinBB); |
| 68 | |
| 69 | // Add attribute "readnone" so that backend can use a native sqrt instruction |
| 70 | // for this call. Insert a FP compare instruction and a conditional branch |
| 71 | // at the end of CurrBB. |
| Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 72 | Call->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone); |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 73 | CurrBB.getTerminator()->eraseFromParent(); |
| 74 | Builder.SetInsertPoint(&CurrBB); |
| Sanjay Patel | 0de1a4b | 2017-11-27 21:15:43 +0000 | [diff] [blame] | 75 | Value *FCmp = TTI->isFCmpOrdCheaperThanFCmpZero(Ty) |
| 76 | ? Builder.CreateFCmpORD(Call, Call) |
| 77 | : Builder.CreateFCmpOGE(Call->getOperand(0), |
| 78 | ConstantFP::get(Ty, 0.0)); |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 79 | Builder.CreateCondBr(FCmp, JoinBB, LibCallBB); |
| 80 | |
| 81 | // Add phi operands. |
| 82 | Phi->addIncoming(Call, &CurrBB); |
| 83 | Phi->addIncoming(LibCall, LibCallBB); |
| 84 | |
| Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 85 | BB = JoinBB->getIterator(); |
| Akira Hatanaka | 5c50a16 | 2013-06-11 22:21:44 +0000 | [diff] [blame] | 86 | return true; |
| 87 | } |
| Richard Sandiford | 37cd6cf | 2013-08-23 10:27:02 +0000 | [diff] [blame] | 88 | |
| Davide Italiano | 1021c68 | 2016-05-25 23:38:53 +0000 | [diff] [blame] | 89 | static bool runPartiallyInlineLibCalls(Function &F, TargetLibraryInfo *TLI, |
| 90 | const TargetTransformInfo *TTI) { |
| Davide Italiano | 08713bd | 2016-05-20 15:43:39 +0000 | [diff] [blame] | 91 | bool Changed = false; |
| Davide Italiano | 1021c68 | 2016-05-25 23:38:53 +0000 | [diff] [blame] | 92 | |
| Davide Italiano | 08713bd | 2016-05-20 15:43:39 +0000 | [diff] [blame] | 93 | Function::iterator CurrBB; |
| Davide Italiano | 08713bd | 2016-05-20 15:43:39 +0000 | [diff] [blame] | 94 | for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE;) { |
| 95 | CurrBB = BB++; |
| 96 | |
| 97 | for (BasicBlock::iterator II = CurrBB->begin(), IE = CurrBB->end(); |
| 98 | II != IE; ++II) { |
| 99 | CallInst *Call = dyn_cast<CallInst>(&*II); |
| 100 | Function *CalledFunc; |
| 101 | |
| 102 | if (!Call || !(CalledFunc = Call->getCalledFunction())) |
| 103 | continue; |
| 104 | |
| Craig Topper | 4968710 | 2017-10-28 00:36:58 +0000 | [diff] [blame] | 105 | if (Call->isNoBuiltin()) |
| 106 | continue; |
| 107 | |
| Davide Italiano | 08713bd | 2016-05-20 15:43:39 +0000 | [diff] [blame] | 108 | // Skip if function either has local linkage or is not a known library |
| 109 | // function. |
| David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 110 | LibFunc LF; |
| Craig Topper | 4968710 | 2017-10-28 00:36:58 +0000 | [diff] [blame] | 111 | if (CalledFunc->hasLocalLinkage() || |
| 112 | !TLI->getLibFunc(*CalledFunc, LF) || !TLI->has(LF)) |
| Davide Italiano | 08713bd | 2016-05-20 15:43:39 +0000 | [diff] [blame] | 113 | continue; |
| 114 | |
| David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 115 | switch (LF) { |
| 116 | case LibFunc_sqrtf: |
| 117 | case LibFunc_sqrt: |
| Davide Italiano | 08713bd | 2016-05-20 15:43:39 +0000 | [diff] [blame] | 118 | if (TTI->haveFastSqrt(Call->getType()) && |
| Sanjay Patel | 0de1a4b | 2017-11-27 21:15:43 +0000 | [diff] [blame] | 119 | optimizeSQRT(Call, CalledFunc, *CurrBB, BB, TTI)) |
| Davide Italiano | 08713bd | 2016-05-20 15:43:39 +0000 | [diff] [blame] | 120 | break; |
| 121 | continue; |
| 122 | default: |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | Changed = true; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return Changed; |
| 132 | } |
| 133 | |
| Davide Italiano | 1021c68 | 2016-05-25 23:38:53 +0000 | [diff] [blame] | 134 | PreservedAnalyses |
| Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 135 | PartiallyInlineLibCallsPass::run(Function &F, FunctionAnalysisManager &AM) { |
| Davide Italiano | 1021c68 | 2016-05-25 23:38:53 +0000 | [diff] [blame] | 136 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); |
| 137 | auto &TTI = AM.getResult<TargetIRAnalysis>(F); |
| 138 | if (!runPartiallyInlineLibCalls(F, &TLI, &TTI)) |
| 139 | return PreservedAnalyses::all(); |
| 140 | return PreservedAnalyses::none(); |
| 141 | } |
| 142 | |
| 143 | namespace { |
| 144 | class PartiallyInlineLibCallsLegacyPass : public FunctionPass { |
| 145 | public: |
| 146 | static char ID; |
| 147 | |
| 148 | PartiallyInlineLibCallsLegacyPass() : FunctionPass(ID) { |
| 149 | initializePartiallyInlineLibCallsLegacyPassPass( |
| 150 | *PassRegistry::getPassRegistry()); |
| 151 | } |
| 152 | |
| 153 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 154 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| 155 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
| 156 | FunctionPass::getAnalysisUsage(AU); |
| 157 | } |
| 158 | |
| 159 | bool runOnFunction(Function &F) override { |
| 160 | if (skipFunction(F)) |
| 161 | return false; |
| 162 | |
| 163 | TargetLibraryInfo *TLI = |
| Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 164 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); |
| Davide Italiano | 1021c68 | 2016-05-25 23:38:53 +0000 | [diff] [blame] | 165 | const TargetTransformInfo *TTI = |
| 166 | &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
| 167 | return runPartiallyInlineLibCalls(F, TLI, TTI); |
| 168 | } |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | char PartiallyInlineLibCallsLegacyPass::ID = 0; |
| 173 | INITIALIZE_PASS_BEGIN(PartiallyInlineLibCallsLegacyPass, |
| 174 | "partially-inline-libcalls", |
| 175 | "Partially inline calls to library functions", false, |
| 176 | false) |
| 177 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 178 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
| 179 | INITIALIZE_PASS_END(PartiallyInlineLibCallsLegacyPass, |
| 180 | "partially-inline-libcalls", |
| 181 | "Partially inline calls to library functions", false, false) |
| 182 | |
| Richard Sandiford | 37cd6cf | 2013-08-23 10:27:02 +0000 | [diff] [blame] | 183 | FunctionPass *llvm::createPartiallyInlineLibCallsPass() { |
| Davide Italiano | 1021c68 | 2016-05-25 23:38:53 +0000 | [diff] [blame] | 184 | return new PartiallyInlineLibCallsLegacyPass(); |
| Richard Sandiford | 37cd6cf | 2013-08-23 10:27:02 +0000 | [diff] [blame] | 185 | } |