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 #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
17 #include "llvm/Analysis/TargetLibraryInfo.h"
18 #include "llvm/Analysis/TargetTransformInfo.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/Transforms/Scalar.h"
21 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
25 #define DEBUG_TYPE "partially-inline-libcalls"
28 static bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
29 BasicBlock &CurrBB, Function::iterator &BB,
30 const TargetTransformInfo *TTI) {
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())
36 // Do the following transformation:
42 // v0 = sqrt_noreadmem(src) # native sqrt instruction.
43 // [if (v0 is a NaN) || if (src < 0)]
44 // v1 = sqrt(src) # library call.
48 // Move all instructions following Call to newly created block JoinBB.
49 // Create phi and replace all uses.
50 BasicBlock *JoinBB = llvm::SplitBlock(&CurrBB, Call->getNextNode());
51 IRBuilder<> Builder(JoinBB, JoinBB->begin());
52 Type *Ty = Call->getType();
53 PHINode *Phi = Builder.CreatePHI(Ty, 2);
54 Call->replaceAllUsesWith(Phi);
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);
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.
67 Call->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
68 CurrBB.getTerminator()->eraseFromParent();
69 Builder.SetInsertPoint(&CurrBB);
70 Value *FCmp = TTI->isFCmpOrdCheaperThanFCmpZero(Ty)
71 ? Builder.CreateFCmpORD(Call, Call)
72 : Builder.CreateFCmpOGE(Call->getOperand(0),
73 ConstantFP::get(Ty, 0.0));
74 Builder.CreateCondBr(FCmp, JoinBB, LibCallBB);
77 Phi->addIncoming(Call, &CurrBB);
78 Phi->addIncoming(LibCall, LibCallBB);
80 BB = JoinBB->getIterator();
84 static bool runPartiallyInlineLibCalls(Function &F, TargetLibraryInfo *TLI,
85 const TargetTransformInfo *TTI) {
88 Function::iterator CurrBB;
89 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE;) {
92 for (BasicBlock::iterator II = CurrBB->begin(), IE = CurrBB->end();
94 CallInst *Call = dyn_cast<CallInst>(&*II);
97 if (!Call || !(CalledFunc = Call->getCalledFunction()))
100 if (Call->isNoBuiltin())
103 // Skip if function either has local linkage or is not a known library
106 if (CalledFunc->hasLocalLinkage() ||
107 !TLI->getLibFunc(*CalledFunc, LF) || !TLI->has(LF))
113 if (TTI->haveFastSqrt(Call->getType()) &&
114 optimizeSQRT(Call, CalledFunc, *CurrBB, BB, TTI))
130 PartiallyInlineLibCallsPass::run(Function &F, FunctionAnalysisManager &AM) {
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();
139 class PartiallyInlineLibCallsLegacyPass : public FunctionPass {
143 PartiallyInlineLibCallsLegacyPass() : FunctionPass(ID) {
144 initializePartiallyInlineLibCallsLegacyPassPass(
145 *PassRegistry::getPassRegistry());
148 void getAnalysisUsage(AnalysisUsage &AU) const override {
149 AU.addRequired<TargetLibraryInfoWrapperPass>();
150 AU.addRequired<TargetTransformInfoWrapperPass>();
151 FunctionPass::getAnalysisUsage(AU);
154 bool runOnFunction(Function &F) override {
158 TargetLibraryInfo *TLI =
159 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
160 const TargetTransformInfo *TTI =
161 &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
162 return runPartiallyInlineLibCalls(F, TLI, TTI);
167 char PartiallyInlineLibCallsLegacyPass::ID = 0;
168 INITIALIZE_PASS_BEGIN(PartiallyInlineLibCallsLegacyPass,
169 "partially-inline-libcalls",
170 "Partially inline calls to library functions", 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)
178 FunctionPass *llvm::createPartiallyInlineLibCallsPass() {
179 return new PartiallyInlineLibCallsLegacyPass();