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