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