1 //===--- PartiallyInlineLibCalls.cpp - Partially inline libcalls ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "partially-inline-libcalls"
17 #include "llvm/Analysis/TargetTransformInfo.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Intrinsics.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Target/TargetLibraryInfo.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
29 class PartiallyInlineLibCalls : public FunctionPass {
33 PartiallyInlineLibCalls() :
35 initializePartiallyInlineLibCallsPass(*PassRegistry::getPassRegistry());
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
39 virtual bool runOnFunction(Function &F);
42 /// Optimize calls to sqrt.
43 bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
44 BasicBlock &CurrBB, Function::iterator &BB);
47 char PartiallyInlineLibCalls::ID = 0;
50 INITIALIZE_PASS(PartiallyInlineLibCalls, "partially-inline-libcalls",
51 "Partially inline calls to library functions", false, false)
53 void PartiallyInlineLibCalls::getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.addRequired<TargetLibraryInfo>();
55 AU.addRequired<TargetTransformInfo>();
56 FunctionPass::getAnalysisUsage(AU);
59 bool PartiallyInlineLibCalls::runOnFunction(Function &F) {
61 Function::iterator CurrBB;
62 TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
63 const TargetTransformInfo *TTI = &getAnalysis<TargetTransformInfo>();
64 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE;) {
67 for (BasicBlock::iterator II = CurrBB->begin(), IE = CurrBB->end();
69 CallInst *Call = dyn_cast<CallInst>(&*II);
72 if (!Call || !(CalledFunc = Call->getCalledFunction()))
75 // Skip if function either has local linkage or is not a known library
77 LibFunc::Func LibFunc;
78 if (CalledFunc->hasLocalLinkage() || !CalledFunc->hasName() ||
79 !TLI->getLibFunc(CalledFunc->getName(), LibFunc))
85 if (TTI->haveFastSqrt(Call->getType()) &&
86 optimizeSQRT(Call, CalledFunc, *CurrBB, BB))
101 bool PartiallyInlineLibCalls::optimizeSQRT(CallInst *Call,
102 Function *CalledFunc,
104 Function::iterator &BB) {
105 // There is no need to change the IR, since backend will emit sqrt
106 // instruction if the call has already been marked read-only.
107 if (Call->onlyReadsMemory())
110 // Do the following transformation:
116 // v0 = sqrt_noreadmem(src) # native sqrt instruction.
118 // v1 = sqrt(src) # library call.
122 // Move all instructions following Call to newly created block JoinBB.
123 // Create phi and replace all uses.
124 BasicBlock *JoinBB = llvm::SplitBlock(&CurrBB, Call->getNextNode(), this);
125 IRBuilder<> Builder(JoinBB, JoinBB->begin());
126 PHINode *Phi = Builder.CreatePHI(Call->getType(), 2);
127 Call->replaceAllUsesWith(Phi);
129 // Create basic block LibCallBB and insert a call to library function sqrt.
130 BasicBlock *LibCallBB = BasicBlock::Create(CurrBB.getContext(), "call.sqrt",
131 CurrBB.getParent(), JoinBB);
132 Builder.SetInsertPoint(LibCallBB);
133 Instruction *LibCall = Call->clone();
134 Builder.Insert(LibCall);
135 Builder.CreateBr(JoinBB);
137 // Add attribute "readnone" so that backend can use a native sqrt instruction
138 // for this call. Insert a FP compare instruction and a conditional branch
139 // at the end of CurrBB.
140 Call->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
141 CurrBB.getTerminator()->eraseFromParent();
142 Builder.SetInsertPoint(&CurrBB);
143 Value *FCmp = Builder.CreateFCmpOEQ(Call, Call);
144 Builder.CreateCondBr(FCmp, JoinBB, LibCallBB);
147 Phi->addIncoming(Call, &CurrBB);
148 Phi->addIncoming(LibCall, LibCallBB);
154 FunctionPass *llvm::createPartiallyInlineLibCallsPass() {
155 return new PartiallyInlineLibCalls();