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