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