xref: /aosp_15_r20/external/llvm/lib/Transforms/Scalar/PartiallyInlineLibCalls.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===--- PartiallyInlineLibCalls.cpp - Partially inline libcalls ----------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This pass tries to partially inline the fast path of well-known library
11*9880d681SAndroid Build Coastguard Worker // functions, such as using square-root instructions for cases where sqrt()
12*9880d681SAndroid Build Coastguard Worker // does not need to set errno.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker 
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetLibraryInfo.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetTransformInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IRBuilder.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/BasicBlockUtils.h"
22*9880d681SAndroid Build Coastguard Worker 
23*9880d681SAndroid Build Coastguard Worker using namespace llvm;
24*9880d681SAndroid Build Coastguard Worker 
25*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "partially-inline-libcalls"
26*9880d681SAndroid Build Coastguard Worker 
27*9880d681SAndroid Build Coastguard Worker 
optimizeSQRT(CallInst * Call,Function * CalledFunc,BasicBlock & CurrBB,Function::iterator & BB)28*9880d681SAndroid Build Coastguard Worker static bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
29*9880d681SAndroid Build Coastguard Worker                          BasicBlock &CurrBB, Function::iterator &BB) {
30*9880d681SAndroid Build Coastguard Worker   // There is no need to change the IR, since backend will emit sqrt
31*9880d681SAndroid Build Coastguard Worker   // instruction if the call has already been marked read-only.
32*9880d681SAndroid Build Coastguard Worker   if (Call->onlyReadsMemory())
33*9880d681SAndroid Build Coastguard Worker     return false;
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker   // The call must have the expected result type.
36*9880d681SAndroid Build Coastguard Worker   if (!Call->getType()->isFloatingPointTy())
37*9880d681SAndroid Build Coastguard Worker     return false;
38*9880d681SAndroid Build Coastguard Worker 
39*9880d681SAndroid Build Coastguard Worker   // Do the following transformation:
40*9880d681SAndroid Build Coastguard Worker   //
41*9880d681SAndroid Build Coastguard Worker   // (before)
42*9880d681SAndroid Build Coastguard Worker   // dst = sqrt(src)
43*9880d681SAndroid Build Coastguard Worker   //
44*9880d681SAndroid Build Coastguard Worker   // (after)
45*9880d681SAndroid Build Coastguard Worker   // v0 = sqrt_noreadmem(src) # native sqrt instruction.
46*9880d681SAndroid Build Coastguard Worker   // if (v0 is a NaN)
47*9880d681SAndroid Build Coastguard Worker   //   v1 = sqrt(src)         # library call.
48*9880d681SAndroid Build Coastguard Worker   // dst = phi(v0, v1)
49*9880d681SAndroid Build Coastguard Worker   //
50*9880d681SAndroid Build Coastguard Worker 
51*9880d681SAndroid Build Coastguard Worker   // Move all instructions following Call to newly created block JoinBB.
52*9880d681SAndroid Build Coastguard Worker   // Create phi and replace all uses.
53*9880d681SAndroid Build Coastguard Worker   BasicBlock *JoinBB = llvm::SplitBlock(&CurrBB, Call->getNextNode());
54*9880d681SAndroid Build Coastguard Worker   IRBuilder<> Builder(JoinBB, JoinBB->begin());
55*9880d681SAndroid Build Coastguard Worker   PHINode *Phi = Builder.CreatePHI(Call->getType(), 2);
56*9880d681SAndroid Build Coastguard Worker   Call->replaceAllUsesWith(Phi);
57*9880d681SAndroid Build Coastguard Worker 
58*9880d681SAndroid Build Coastguard Worker   // Create basic block LibCallBB and insert a call to library function sqrt.
59*9880d681SAndroid Build Coastguard Worker   BasicBlock *LibCallBB = BasicBlock::Create(CurrBB.getContext(), "call.sqrt",
60*9880d681SAndroid Build Coastguard Worker                                              CurrBB.getParent(), JoinBB);
61*9880d681SAndroid Build Coastguard Worker   Builder.SetInsertPoint(LibCallBB);
62*9880d681SAndroid Build Coastguard Worker   Instruction *LibCall = Call->clone();
63*9880d681SAndroid Build Coastguard Worker   Builder.Insert(LibCall);
64*9880d681SAndroid Build Coastguard Worker   Builder.CreateBr(JoinBB);
65*9880d681SAndroid Build Coastguard Worker 
66*9880d681SAndroid Build Coastguard Worker   // Add attribute "readnone" so that backend can use a native sqrt instruction
67*9880d681SAndroid Build Coastguard Worker   // for this call. Insert a FP compare instruction and a conditional branch
68*9880d681SAndroid Build Coastguard Worker   // at the end of CurrBB.
69*9880d681SAndroid Build Coastguard Worker   Call->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
70*9880d681SAndroid Build Coastguard Worker   CurrBB.getTerminator()->eraseFromParent();
71*9880d681SAndroid Build Coastguard Worker   Builder.SetInsertPoint(&CurrBB);
72*9880d681SAndroid Build Coastguard Worker   Value *FCmp = Builder.CreateFCmpOEQ(Call, Call);
73*9880d681SAndroid Build Coastguard Worker   Builder.CreateCondBr(FCmp, JoinBB, LibCallBB);
74*9880d681SAndroid Build Coastguard Worker 
75*9880d681SAndroid Build Coastguard Worker   // Add phi operands.
76*9880d681SAndroid Build Coastguard Worker   Phi->addIncoming(Call, &CurrBB);
77*9880d681SAndroid Build Coastguard Worker   Phi->addIncoming(LibCall, LibCallBB);
78*9880d681SAndroid Build Coastguard Worker 
79*9880d681SAndroid Build Coastguard Worker   BB = JoinBB->getIterator();
80*9880d681SAndroid Build Coastguard Worker   return true;
81*9880d681SAndroid Build Coastguard Worker }
82*9880d681SAndroid Build Coastguard Worker 
runPartiallyInlineLibCalls(Function & F,TargetLibraryInfo * TLI,const TargetTransformInfo * TTI)83*9880d681SAndroid Build Coastguard Worker static bool runPartiallyInlineLibCalls(Function &F, TargetLibraryInfo *TLI,
84*9880d681SAndroid Build Coastguard Worker                                        const TargetTransformInfo *TTI) {
85*9880d681SAndroid Build Coastguard Worker   bool Changed = false;
86*9880d681SAndroid Build Coastguard Worker 
87*9880d681SAndroid Build Coastguard Worker   Function::iterator CurrBB;
88*9880d681SAndroid Build Coastguard Worker   for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE;) {
89*9880d681SAndroid Build Coastguard Worker     CurrBB = BB++;
90*9880d681SAndroid Build Coastguard Worker 
91*9880d681SAndroid Build Coastguard Worker     for (BasicBlock::iterator II = CurrBB->begin(), IE = CurrBB->end();
92*9880d681SAndroid Build Coastguard Worker          II != IE; ++II) {
93*9880d681SAndroid Build Coastguard Worker       CallInst *Call = dyn_cast<CallInst>(&*II);
94*9880d681SAndroid Build Coastguard Worker       Function *CalledFunc;
95*9880d681SAndroid Build Coastguard Worker 
96*9880d681SAndroid Build Coastguard Worker       if (!Call || !(CalledFunc = Call->getCalledFunction()))
97*9880d681SAndroid Build Coastguard Worker         continue;
98*9880d681SAndroid Build Coastguard Worker 
99*9880d681SAndroid Build Coastguard Worker       // Skip if function either has local linkage or is not a known library
100*9880d681SAndroid Build Coastguard Worker       // function.
101*9880d681SAndroid Build Coastguard Worker       LibFunc::Func LibFunc;
102*9880d681SAndroid Build Coastguard Worker       if (CalledFunc->hasLocalLinkage() || !CalledFunc->hasName() ||
103*9880d681SAndroid Build Coastguard Worker           !TLI->getLibFunc(CalledFunc->getName(), LibFunc))
104*9880d681SAndroid Build Coastguard Worker         continue;
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker       switch (LibFunc) {
107*9880d681SAndroid Build Coastguard Worker       case LibFunc::sqrtf:
108*9880d681SAndroid Build Coastguard Worker       case LibFunc::sqrt:
109*9880d681SAndroid Build Coastguard Worker         if (TTI->haveFastSqrt(Call->getType()) &&
110*9880d681SAndroid Build Coastguard Worker             optimizeSQRT(Call, CalledFunc, *CurrBB, BB))
111*9880d681SAndroid Build Coastguard Worker           break;
112*9880d681SAndroid Build Coastguard Worker         continue;
113*9880d681SAndroid Build Coastguard Worker       default:
114*9880d681SAndroid Build Coastguard Worker         continue;
115*9880d681SAndroid Build Coastguard Worker       }
116*9880d681SAndroid Build Coastguard Worker 
117*9880d681SAndroid Build Coastguard Worker       Changed = true;
118*9880d681SAndroid Build Coastguard Worker       break;
119*9880d681SAndroid Build Coastguard Worker     }
120*9880d681SAndroid Build Coastguard Worker   }
121*9880d681SAndroid Build Coastguard Worker 
122*9880d681SAndroid Build Coastguard Worker   return Changed;
123*9880d681SAndroid Build Coastguard Worker }
124*9880d681SAndroid Build Coastguard Worker 
125*9880d681SAndroid Build Coastguard Worker PreservedAnalyses
run(Function & F,AnalysisManager<Function> & AM)126*9880d681SAndroid Build Coastguard Worker PartiallyInlineLibCallsPass::run(Function &F, AnalysisManager<Function> &AM) {
127*9880d681SAndroid Build Coastguard Worker   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
128*9880d681SAndroid Build Coastguard Worker   auto &TTI = AM.getResult<TargetIRAnalysis>(F);
129*9880d681SAndroid Build Coastguard Worker   if (!runPartiallyInlineLibCalls(F, &TLI, &TTI))
130*9880d681SAndroid Build Coastguard Worker     return PreservedAnalyses::all();
131*9880d681SAndroid Build Coastguard Worker   return PreservedAnalyses::none();
132*9880d681SAndroid Build Coastguard Worker }
133*9880d681SAndroid Build Coastguard Worker 
134*9880d681SAndroid Build Coastguard Worker namespace {
135*9880d681SAndroid Build Coastguard Worker class PartiallyInlineLibCallsLegacyPass : public FunctionPass {
136*9880d681SAndroid Build Coastguard Worker public:
137*9880d681SAndroid Build Coastguard Worker   static char ID;
138*9880d681SAndroid Build Coastguard Worker 
PartiallyInlineLibCallsLegacyPass()139*9880d681SAndroid Build Coastguard Worker   PartiallyInlineLibCallsLegacyPass() : FunctionPass(ID) {
140*9880d681SAndroid Build Coastguard Worker     initializePartiallyInlineLibCallsLegacyPassPass(
141*9880d681SAndroid Build Coastguard Worker         *PassRegistry::getPassRegistry());
142*9880d681SAndroid Build Coastguard Worker   }
143*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage(AnalysisUsage & AU) const144*9880d681SAndroid Build Coastguard Worker   void getAnalysisUsage(AnalysisUsage &AU) const override {
145*9880d681SAndroid Build Coastguard Worker     AU.addRequired<TargetLibraryInfoWrapperPass>();
146*9880d681SAndroid Build Coastguard Worker     AU.addRequired<TargetTransformInfoWrapperPass>();
147*9880d681SAndroid Build Coastguard Worker     FunctionPass::getAnalysisUsage(AU);
148*9880d681SAndroid Build Coastguard Worker   }
149*9880d681SAndroid Build Coastguard Worker 
runOnFunction(Function & F)150*9880d681SAndroid Build Coastguard Worker   bool runOnFunction(Function &F) override {
151*9880d681SAndroid Build Coastguard Worker     if (skipFunction(F))
152*9880d681SAndroid Build Coastguard Worker       return false;
153*9880d681SAndroid Build Coastguard Worker 
154*9880d681SAndroid Build Coastguard Worker     TargetLibraryInfo *TLI =
155*9880d681SAndroid Build Coastguard Worker         &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
156*9880d681SAndroid Build Coastguard Worker     const TargetTransformInfo *TTI =
157*9880d681SAndroid Build Coastguard Worker         &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
158*9880d681SAndroid Build Coastguard Worker     return runPartiallyInlineLibCalls(F, TLI, TTI);
159*9880d681SAndroid Build Coastguard Worker   }
160*9880d681SAndroid Build Coastguard Worker };
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker 
163*9880d681SAndroid Build Coastguard Worker char PartiallyInlineLibCallsLegacyPass::ID = 0;
164*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(PartiallyInlineLibCallsLegacyPass,
165*9880d681SAndroid Build Coastguard Worker                       "partially-inline-libcalls",
166*9880d681SAndroid Build Coastguard Worker                       "Partially inline calls to library functions", false,
167*9880d681SAndroid Build Coastguard Worker                       false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)168*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
169*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
170*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(PartiallyInlineLibCallsLegacyPass,
171*9880d681SAndroid Build Coastguard Worker                     "partially-inline-libcalls",
172*9880d681SAndroid Build Coastguard Worker                     "Partially inline calls to library functions", false, false)
173*9880d681SAndroid Build Coastguard Worker 
174*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createPartiallyInlineLibCallsPass() {
175*9880d681SAndroid Build Coastguard Worker   return new PartiallyInlineLibCallsLegacyPass();
176*9880d681SAndroid Build Coastguard Worker }
177