10b57cec5SDimitry Andric //===--- PartiallyInlineLibCalls.cpp - Partially inline libcalls ----------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass tries to partially inline the fast path of well-known library
100b57cec5SDimitry Andric // functions, such as using square-root instructions for cases where sqrt()
110b57cec5SDimitry Andric // does not need to set errno.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric
150b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
16fe6060f1SDimitry Andric #include "llvm/Analysis/DomTreeUpdater.h"
170b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
180b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
19fe6060f1SDimitry Andric #include "llvm/IR/Dominators.h"
200b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
21480093f4SDimitry Andric #include "llvm/InitializePasses.h"
220b57cec5SDimitry Andric #include "llvm/Support/DebugCounter.h"
230b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h"
240b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
25*bdd1243dSDimitry Andric #include <optional>
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric using namespace llvm;
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric #define DEBUG_TYPE "partially-inline-libcalls"
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric DEBUG_COUNTER(PILCounter, "partially-inline-libcalls-transform",
320b57cec5SDimitry Andric "Controls transformations in partially-inline-libcalls");
330b57cec5SDimitry Andric
optimizeSQRT(CallInst * Call,Function * CalledFunc,BasicBlock & CurrBB,Function::iterator & BB,const TargetTransformInfo * TTI,DomTreeUpdater * DTU)340b57cec5SDimitry Andric static bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
350b57cec5SDimitry Andric BasicBlock &CurrBB, Function::iterator &BB,
36fe6060f1SDimitry Andric const TargetTransformInfo *TTI, DomTreeUpdater *DTU) {
370b57cec5SDimitry Andric // There is no need to change the IR, since backend will emit sqrt
380b57cec5SDimitry Andric // instruction if the call has already been marked read-only.
390b57cec5SDimitry Andric if (Call->onlyReadsMemory())
400b57cec5SDimitry Andric return false;
410b57cec5SDimitry Andric
420b57cec5SDimitry Andric if (!DebugCounter::shouldExecute(PILCounter))
430b57cec5SDimitry Andric return false;
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric // Do the following transformation:
460b57cec5SDimitry Andric //
470b57cec5SDimitry Andric // (before)
480b57cec5SDimitry Andric // dst = sqrt(src)
490b57cec5SDimitry Andric //
500b57cec5SDimitry Andric // (after)
510b57cec5SDimitry Andric // v0 = sqrt_noreadmem(src) # native sqrt instruction.
520b57cec5SDimitry Andric // [if (v0 is a NaN) || if (src < 0)]
530b57cec5SDimitry Andric // v1 = sqrt(src) # library call.
540b57cec5SDimitry Andric // dst = phi(v0, v1)
550b57cec5SDimitry Andric //
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric Type *Ty = Call->getType();
58fe6060f1SDimitry Andric IRBuilder<> Builder(Call->getNextNode());
59fe6060f1SDimitry Andric
60fe6060f1SDimitry Andric // Split CurrBB right after the call, create a 'then' block (that branches
61fe6060f1SDimitry Andric // back to split-off tail of CurrBB) into which we'll insert a libcall.
62fe6060f1SDimitry Andric Instruction *LibCallTerm = SplitBlockAndInsertIfThen(
63fe6060f1SDimitry Andric Builder.getTrue(), Call->getNextNode(), /*Unreachable=*/false,
64fe6060f1SDimitry Andric /*BranchWeights*/ nullptr, DTU);
65fe6060f1SDimitry Andric
66fe6060f1SDimitry Andric auto *CurrBBTerm = cast<BranchInst>(CurrBB.getTerminator());
67fe6060f1SDimitry Andric // We want an 'else' block though, not a 'then' block.
68fe6060f1SDimitry Andric cast<BranchInst>(CurrBBTerm)->swapSuccessors();
69fe6060f1SDimitry Andric
70fe6060f1SDimitry Andric // Create phi that will merge results of either sqrt and replace all uses.
71fe6060f1SDimitry Andric BasicBlock *JoinBB = LibCallTerm->getSuccessor(0);
72fe6060f1SDimitry Andric JoinBB->setName(CurrBB.getName() + ".split");
73fe6060f1SDimitry Andric Builder.SetInsertPoint(JoinBB, JoinBB->begin());
740b57cec5SDimitry Andric PHINode *Phi = Builder.CreatePHI(Ty, 2);
750b57cec5SDimitry Andric Call->replaceAllUsesWith(Phi);
760b57cec5SDimitry Andric
77fe6060f1SDimitry Andric // Finally, insert the libcall into 'else' block.
78fe6060f1SDimitry Andric BasicBlock *LibCallBB = LibCallTerm->getParent();
79fe6060f1SDimitry Andric LibCallBB->setName("call.sqrt");
80fe6060f1SDimitry Andric Builder.SetInsertPoint(LibCallTerm);
810b57cec5SDimitry Andric Instruction *LibCall = Call->clone();
820b57cec5SDimitry Andric Builder.Insert(LibCall);
830b57cec5SDimitry Andric
84*bdd1243dSDimitry Andric // Add memory(none) attribute, so that the backend can use a native sqrt
85*bdd1243dSDimitry Andric // instruction for this call.
86*bdd1243dSDimitry Andric Call->setDoesNotAccessMemory();
87fe6060f1SDimitry Andric
88fe6060f1SDimitry Andric // Insert a FP compare instruction and use it as the CurrBB branch condition.
89fe6060f1SDimitry Andric Builder.SetInsertPoint(CurrBBTerm);
900b57cec5SDimitry Andric Value *FCmp = TTI->isFCmpOrdCheaperThanFCmpZero(Ty)
910b57cec5SDimitry Andric ? Builder.CreateFCmpORD(Call, Call)
920b57cec5SDimitry Andric : Builder.CreateFCmpOGE(Call->getOperand(0),
930b57cec5SDimitry Andric ConstantFP::get(Ty, 0.0));
94fe6060f1SDimitry Andric CurrBBTerm->setCondition(FCmp);
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric // Add phi operands.
970b57cec5SDimitry Andric Phi->addIncoming(Call, &CurrBB);
980b57cec5SDimitry Andric Phi->addIncoming(LibCall, LibCallBB);
990b57cec5SDimitry Andric
1000b57cec5SDimitry Andric BB = JoinBB->getIterator();
1010b57cec5SDimitry Andric return true;
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric
runPartiallyInlineLibCalls(Function & F,TargetLibraryInfo * TLI,const TargetTransformInfo * TTI,DominatorTree * DT)1040b57cec5SDimitry Andric static bool runPartiallyInlineLibCalls(Function &F, TargetLibraryInfo *TLI,
105fe6060f1SDimitry Andric const TargetTransformInfo *TTI,
106fe6060f1SDimitry Andric DominatorTree *DT) {
107*bdd1243dSDimitry Andric std::optional<DomTreeUpdater> DTU;
108fe6060f1SDimitry Andric if (DT)
109fe6060f1SDimitry Andric DTU.emplace(DT, DomTreeUpdater::UpdateStrategy::Lazy);
110fe6060f1SDimitry Andric
1110b57cec5SDimitry Andric bool Changed = false;
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric Function::iterator CurrBB;
1140b57cec5SDimitry Andric for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE;) {
1150b57cec5SDimitry Andric CurrBB = BB++;
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric for (BasicBlock::iterator II = CurrBB->begin(), IE = CurrBB->end();
1180b57cec5SDimitry Andric II != IE; ++II) {
1190b57cec5SDimitry Andric CallInst *Call = dyn_cast<CallInst>(&*II);
1200b57cec5SDimitry Andric Function *CalledFunc;
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric if (!Call || !(CalledFunc = Call->getCalledFunction()))
1230b57cec5SDimitry Andric continue;
1240b57cec5SDimitry Andric
125fe6060f1SDimitry Andric if (Call->isNoBuiltin() || Call->isStrictFP())
1260b57cec5SDimitry Andric continue;
1270b57cec5SDimitry Andric
12881ad6265SDimitry Andric if (Call->isMustTailCall())
12981ad6265SDimitry Andric continue;
13081ad6265SDimitry Andric
1310b57cec5SDimitry Andric // Skip if function either has local linkage or is not a known library
1320b57cec5SDimitry Andric // function.
1330b57cec5SDimitry Andric LibFunc LF;
1340b57cec5SDimitry Andric if (CalledFunc->hasLocalLinkage() ||
1350b57cec5SDimitry Andric !TLI->getLibFunc(*CalledFunc, LF) || !TLI->has(LF))
1360b57cec5SDimitry Andric continue;
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric switch (LF) {
1390b57cec5SDimitry Andric case LibFunc_sqrtf:
1400b57cec5SDimitry Andric case LibFunc_sqrt:
1410b57cec5SDimitry Andric if (TTI->haveFastSqrt(Call->getType()) &&
142fe6060f1SDimitry Andric optimizeSQRT(Call, CalledFunc, *CurrBB, BB, TTI,
143*bdd1243dSDimitry Andric DTU ? &*DTU : nullptr))
1440b57cec5SDimitry Andric break;
1450b57cec5SDimitry Andric continue;
1460b57cec5SDimitry Andric default:
1470b57cec5SDimitry Andric continue;
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric
1500b57cec5SDimitry Andric Changed = true;
1510b57cec5SDimitry Andric break;
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric return Changed;
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric
1580b57cec5SDimitry Andric PreservedAnalyses
run(Function & F,FunctionAnalysisManager & AM)1590b57cec5SDimitry Andric PartiallyInlineLibCallsPass::run(Function &F, FunctionAnalysisManager &AM) {
1600b57cec5SDimitry Andric auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
1610b57cec5SDimitry Andric auto &TTI = AM.getResult<TargetIRAnalysis>(F);
162fe6060f1SDimitry Andric auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
163fe6060f1SDimitry Andric if (!runPartiallyInlineLibCalls(F, &TLI, &TTI, DT))
1640b57cec5SDimitry Andric return PreservedAnalyses::all();
165fe6060f1SDimitry Andric PreservedAnalyses PA;
166fe6060f1SDimitry Andric PA.preserve<DominatorTreeAnalysis>();
167fe6060f1SDimitry Andric return PA;
1680b57cec5SDimitry Andric }
1690b57cec5SDimitry Andric
1700b57cec5SDimitry Andric namespace {
1710b57cec5SDimitry Andric class PartiallyInlineLibCallsLegacyPass : public FunctionPass {
1720b57cec5SDimitry Andric public:
1730b57cec5SDimitry Andric static char ID;
1740b57cec5SDimitry Andric
PartiallyInlineLibCallsLegacyPass()1750b57cec5SDimitry Andric PartiallyInlineLibCallsLegacyPass() : FunctionPass(ID) {
1760b57cec5SDimitry Andric initializePartiallyInlineLibCallsLegacyPassPass(
1770b57cec5SDimitry Andric *PassRegistry::getPassRegistry());
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const1800b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
1810b57cec5SDimitry Andric AU.addRequired<TargetLibraryInfoWrapperPass>();
1820b57cec5SDimitry Andric AU.addRequired<TargetTransformInfoWrapperPass>();
183fe6060f1SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>();
1840b57cec5SDimitry Andric FunctionPass::getAnalysisUsage(AU);
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric
runOnFunction(Function & F)1870b57cec5SDimitry Andric bool runOnFunction(Function &F) override {
1880b57cec5SDimitry Andric if (skipFunction(F))
1890b57cec5SDimitry Andric return false;
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric TargetLibraryInfo *TLI =
1928bcb0991SDimitry Andric &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
1930b57cec5SDimitry Andric const TargetTransformInfo *TTI =
1940b57cec5SDimitry Andric &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
195fe6060f1SDimitry Andric DominatorTree *DT = nullptr;
196fe6060f1SDimitry Andric if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>())
197fe6060f1SDimitry Andric DT = &DTWP->getDomTree();
198fe6060f1SDimitry Andric return runPartiallyInlineLibCalls(F, TLI, TTI, DT);
1990b57cec5SDimitry Andric }
2000b57cec5SDimitry Andric };
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric
2030b57cec5SDimitry Andric char PartiallyInlineLibCallsLegacyPass::ID = 0;
2040b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(PartiallyInlineLibCallsLegacyPass,
2050b57cec5SDimitry Andric "partially-inline-libcalls",
2060b57cec5SDimitry Andric "Partially inline calls to library functions", false,
2070b57cec5SDimitry Andric false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)2080b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
209fe6060f1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
2100b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
2110b57cec5SDimitry Andric INITIALIZE_PASS_END(PartiallyInlineLibCallsLegacyPass,
2120b57cec5SDimitry Andric "partially-inline-libcalls",
2130b57cec5SDimitry Andric "Partially inline calls to library functions", false, false)
2140b57cec5SDimitry Andric
2150b57cec5SDimitry Andric FunctionPass *llvm::createPartiallyInlineLibCallsPass() {
2160b57cec5SDimitry Andric return new PartiallyInlineLibCallsLegacyPass();
2170b57cec5SDimitry Andric }
218