1 //===--- PartiallyInlineLibCalls.cpp - Partially inline libcalls ----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This pass tries to partially inline the fast path of well-known library 10 // functions, such as using square-root instructions for cases where sqrt() 11 // does not need to set errno. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h" 16 #include "llvm/Analysis/DomTreeUpdater.h" 17 #include "llvm/Analysis/TargetLibraryInfo.h" 18 #include "llvm/Analysis/TargetTransformInfo.h" 19 #include "llvm/IR/Dominators.h" 20 #include "llvm/IR/IRBuilder.h" 21 #include "llvm/InitializePasses.h" 22 #include "llvm/Support/DebugCounter.h" 23 #include "llvm/Transforms/Scalar.h" 24 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 25 26 using namespace llvm; 27 28 #define DEBUG_TYPE "partially-inline-libcalls" 29 30 DEBUG_COUNTER(PILCounter, "partially-inline-libcalls-transform", 31 "Controls transformations in partially-inline-libcalls"); 32 33 static bool optimizeSQRT(CallInst *Call, Function *CalledFunc, 34 BasicBlock &CurrBB, Function::iterator &BB, 35 const TargetTransformInfo *TTI, DomTreeUpdater *DTU) { 36 // There is no need to change the IR, since backend will emit sqrt 37 // instruction if the call has already been marked read-only. 38 if (Call->onlyReadsMemory()) 39 return false; 40 41 if (!DebugCounter::shouldExecute(PILCounter)) 42 return false; 43 44 // Do the following transformation: 45 // 46 // (before) 47 // dst = sqrt(src) 48 // 49 // (after) 50 // v0 = sqrt_noreadmem(src) # native sqrt instruction. 51 // [if (v0 is a NaN) || if (src < 0)] 52 // v1 = sqrt(src) # library call. 53 // dst = phi(v0, v1) 54 // 55 56 Type *Ty = Call->getType(); 57 IRBuilder<> Builder(Call->getNextNode()); 58 59 // Split CurrBB right after the call, create a 'then' block (that branches 60 // back to split-off tail of CurrBB) into which we'll insert a libcall. 61 Instruction *LibCallTerm = SplitBlockAndInsertIfThen( 62 Builder.getTrue(), Call->getNextNode(), /*Unreachable=*/false, 63 /*BranchWeights*/ nullptr, DTU); 64 65 auto *CurrBBTerm = cast<BranchInst>(CurrBB.getTerminator()); 66 // We want an 'else' block though, not a 'then' block. 67 cast<BranchInst>(CurrBBTerm)->swapSuccessors(); 68 69 // Create phi that will merge results of either sqrt and replace all uses. 70 BasicBlock *JoinBB = LibCallTerm->getSuccessor(0); 71 JoinBB->setName(CurrBB.getName() + ".split"); 72 Builder.SetInsertPoint(JoinBB, JoinBB->begin()); 73 PHINode *Phi = Builder.CreatePHI(Ty, 2); 74 Call->replaceAllUsesWith(Phi); 75 76 // Finally, insert the libcall into 'else' block. 77 BasicBlock *LibCallBB = LibCallTerm->getParent(); 78 LibCallBB->setName("call.sqrt"); 79 Builder.SetInsertPoint(LibCallTerm); 80 Instruction *LibCall = Call->clone(); 81 Builder.Insert(LibCall); 82 83 // Add attribute "readnone" so that backend can use a native sqrt instruction 84 // for this call. 85 Call->removeFnAttr(Attribute::WriteOnly); 86 Call->addFnAttr(Attribute::ReadNone); 87 88 // Insert a FP compare instruction and use it as the CurrBB branch condition. 89 Builder.SetInsertPoint(CurrBBTerm); 90 Value *FCmp = TTI->isFCmpOrdCheaperThanFCmpZero(Ty) 91 ? Builder.CreateFCmpORD(Call, Call) 92 : Builder.CreateFCmpOGE(Call->getOperand(0), 93 ConstantFP::get(Ty, 0.0)); 94 CurrBBTerm->setCondition(FCmp); 95 96 // Add phi operands. 97 Phi->addIncoming(Call, &CurrBB); 98 Phi->addIncoming(LibCall, LibCallBB); 99 100 BB = JoinBB->getIterator(); 101 return true; 102 } 103 104 static bool runPartiallyInlineLibCalls(Function &F, TargetLibraryInfo *TLI, 105 const TargetTransformInfo *TTI, 106 DominatorTree *DT) { 107 Optional<DomTreeUpdater> DTU; 108 if (DT) 109 DTU.emplace(DT, DomTreeUpdater::UpdateStrategy::Lazy); 110 111 bool Changed = false; 112 113 Function::iterator CurrBB; 114 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE;) { 115 CurrBB = BB++; 116 117 for (BasicBlock::iterator II = CurrBB->begin(), IE = CurrBB->end(); 118 II != IE; ++II) { 119 CallInst *Call = dyn_cast<CallInst>(&*II); 120 Function *CalledFunc; 121 122 if (!Call || !(CalledFunc = Call->getCalledFunction())) 123 continue; 124 125 if (Call->isNoBuiltin() || Call->isStrictFP()) 126 continue; 127 128 // Skip if function either has local linkage or is not a known library 129 // function. 130 LibFunc LF; 131 if (CalledFunc->hasLocalLinkage() || 132 !TLI->getLibFunc(*CalledFunc, LF) || !TLI->has(LF)) 133 continue; 134 135 switch (LF) { 136 case LibFunc_sqrtf: 137 case LibFunc_sqrt: 138 if (TTI->haveFastSqrt(Call->getType()) && 139 optimizeSQRT(Call, CalledFunc, *CurrBB, BB, TTI, 140 DTU.hasValue() ? DTU.getPointer() : nullptr)) 141 break; 142 continue; 143 default: 144 continue; 145 } 146 147 Changed = true; 148 break; 149 } 150 } 151 152 return Changed; 153 } 154 155 PreservedAnalyses 156 PartiallyInlineLibCallsPass::run(Function &F, FunctionAnalysisManager &AM) { 157 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); 158 auto &TTI = AM.getResult<TargetIRAnalysis>(F); 159 auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F); 160 if (!runPartiallyInlineLibCalls(F, &TLI, &TTI, DT)) 161 return PreservedAnalyses::all(); 162 PreservedAnalyses PA; 163 PA.preserve<DominatorTreeAnalysis>(); 164 return PA; 165 } 166 167 namespace { 168 class PartiallyInlineLibCallsLegacyPass : public FunctionPass { 169 public: 170 static char ID; 171 172 PartiallyInlineLibCallsLegacyPass() : FunctionPass(ID) { 173 initializePartiallyInlineLibCallsLegacyPassPass( 174 *PassRegistry::getPassRegistry()); 175 } 176 177 void getAnalysisUsage(AnalysisUsage &AU) const override { 178 AU.addRequired<TargetLibraryInfoWrapperPass>(); 179 AU.addRequired<TargetTransformInfoWrapperPass>(); 180 AU.addPreserved<DominatorTreeWrapperPass>(); 181 FunctionPass::getAnalysisUsage(AU); 182 } 183 184 bool runOnFunction(Function &F) override { 185 if (skipFunction(F)) 186 return false; 187 188 TargetLibraryInfo *TLI = 189 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); 190 const TargetTransformInfo *TTI = 191 &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 192 DominatorTree *DT = nullptr; 193 if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>()) 194 DT = &DTWP->getDomTree(); 195 return runPartiallyInlineLibCalls(F, TLI, TTI, DT); 196 } 197 }; 198 } 199 200 char PartiallyInlineLibCallsLegacyPass::ID = 0; 201 INITIALIZE_PASS_BEGIN(PartiallyInlineLibCallsLegacyPass, 202 "partially-inline-libcalls", 203 "Partially inline calls to library functions", false, 204 false) 205 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 206 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 207 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 208 INITIALIZE_PASS_END(PartiallyInlineLibCallsLegacyPass, 209 "partially-inline-libcalls", 210 "Partially inline calls to library functions", false, false) 211 212 FunctionPass *llvm::createPartiallyInlineLibCallsPass() { 213 return new PartiallyInlineLibCallsLegacyPass(); 214 } 215