LLVM 20.0.0git
PartiallyInlineLibCalls.cpp
Go to the documentation of this file.
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
19#include "llvm/IR/Dominators.h"
20#include "llvm/IR/IRBuilder.h"
25#include <optional>
26
27using namespace llvm;
28
29#define DEBUG_TYPE "partially-inline-libcalls"
30
31DEBUG_COUNTER(PILCounter, "partially-inline-libcalls-transform",
32 "Controls transformations in partially-inline-libcalls");
33
34static bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
35 BasicBlock &CurrBB, Function::iterator &BB,
37 // There is no need to change the IR, since backend will emit sqrt
38 // instruction if the call has already been marked read-only.
39 if (Call->onlyReadsMemory())
40 return false;
41
42 if (!DebugCounter::shouldExecute(PILCounter))
43 return false;
44
45 // Do the following transformation:
46 //
47 // (before)
48 // dst = sqrt(src)
49 //
50 // (after)
51 // v0 = sqrt_noreadmem(src) # native sqrt instruction.
52 // [if (v0 is a NaN) || if (src < 0)]
53 // v1 = sqrt(src) # library call.
54 // dst = phi(v0, v1)
55 //
56
57 Type *Ty = Call->getType();
58 IRBuilder<> Builder(Call->getNextNode());
59
60 // Split CurrBB right after the call, create a 'then' block (that branches
61 // back to split-off tail of CurrBB) into which we'll insert a libcall.
63 Builder.getTrue(), Call->getNextNode(), /*Unreachable=*/false,
64 /*BranchWeights*/ nullptr, DTU);
65
66 auto *CurrBBTerm = cast<BranchInst>(CurrBB.getTerminator());
67 // We want an 'else' block though, not a 'then' block.
68 cast<BranchInst>(CurrBBTerm)->swapSuccessors();
69
70 // Create phi that will merge results of either sqrt and replace all uses.
71 BasicBlock *JoinBB = LibCallTerm->getSuccessor(0);
72 JoinBB->setName(CurrBB.getName() + ".split");
73 Builder.SetInsertPoint(JoinBB, JoinBB->begin());
74 PHINode *Phi = Builder.CreatePHI(Ty, 2);
75 Call->replaceAllUsesWith(Phi);
76
77 // Finally, insert the libcall into 'else' block.
78 BasicBlock *LibCallBB = LibCallTerm->getParent();
79 LibCallBB->setName("call.sqrt");
80 Builder.SetInsertPoint(LibCallTerm);
81 Instruction *LibCall = Call->clone();
82 Builder.Insert(LibCall);
83
84 // Add memory(none) attribute, so that the backend can use a native sqrt
85 // instruction for this call.
86 Call->setDoesNotAccessMemory();
87
88 // Insert a FP compare instruction and use it as the CurrBB branch condition.
89 Builder.SetInsertPoint(CurrBBTerm);
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
106 DominatorTree *DT) {
107 std::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 if (Call->isMustTailCall())
129 continue;
130
131 // Skip if function either has local linkage or is not a known library
132 // function.
133 LibFunc LF;
134 if (CalledFunc->hasLocalLinkage() ||
135 !TLI->getLibFunc(*CalledFunc, LF) || !TLI->has(LF))
136 continue;
137
138 switch (LF) {
139 case LibFunc_sqrtf:
140 case LibFunc_sqrt:
141 if (TTI->haveFastSqrt(Call->getType()) &&
142 optimizeSQRT(Call, CalledFunc, *CurrBB, BB, TTI,
143 DTU ? &*DTU : nullptr))
144 break;
145 continue;
146 default:
147 continue;
148 }
149
150 Changed = true;
151 break;
152 }
153 }
154
155 return Changed;
156}
157
160 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
161 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
163 if (!runPartiallyInlineLibCalls(F, &TLI, &TTI, DT))
164 return PreservedAnalyses::all();
167 return PA;
168}
169
170namespace {
171class PartiallyInlineLibCallsLegacyPass : public FunctionPass {
172public:
173 static char ID;
174
175 PartiallyInlineLibCallsLegacyPass() : FunctionPass(ID) {
178 }
179
180 void getAnalysisUsage(AnalysisUsage &AU) const override {
184 FunctionPass::getAnalysisUsage(AU);
185 }
186
187 bool runOnFunction(Function &F) override {
188 if (skipFunction(F))
189 return false;
190
191 TargetLibraryInfo *TLI =
192 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
193 const TargetTransformInfo *TTI =
194 &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
195 DominatorTree *DT = nullptr;
196 if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>())
197 DT = &DTWP->getDomTree();
198 return runPartiallyInlineLibCalls(F, TLI, TTI, DT);
199 }
200};
201}
202
203char PartiallyInlineLibCallsLegacyPass::ID = 0;
204INITIALIZE_PASS_BEGIN(PartiallyInlineLibCallsLegacyPass,
205 "partially-inline-libcalls",
206 "Partially inline calls to library functions", false,
207 false)
211INITIALIZE_PASS_END(PartiallyInlineLibCallsLegacyPass,
212 "partially-inline-libcalls",
213 "Partially inline calls to library functions", false, false)
214
216 return new PartiallyInlineLibCallsLegacyPass();
217}
Lower uses of LDS variables from non kernel functions
always inline
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
Definition: DebugCounter.h:190
static bool runOnFunction(Function &F, bool PostInlining)
#define F(x, y, z)
Definition: MD5.cpp:55
uint64_t IntrinsicInst * II
static bool runPartiallyInlineLibCalls(Function &F, TargetLibraryInfo *TLI, const TargetTransformInfo *TTI, DominatorTree *DT)
partially inline libcalls
static bool optimizeSQRT(CallInst *Call, Function *CalledFunc, BasicBlock &CurrBB, Function::iterator &BB, const TargetTransformInfo *TTI, DomTreeUpdater *DTU)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
Replace intrinsics with calls to vector library
This pass exposes codegen information to IR-level passes.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
Definition: PassManager.h:429
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:448
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:239
This class represents a function call, abstracting a target machine's calling convention.
static bool shouldExecute(unsigned CounterName)
Definition: DebugCounter.h:87
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:317
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
BasicBlockListType::iterator iterator
Definition: Function.h:68
bool hasLocalLinkage() const
Definition: GlobalValue.h:528
Value * CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2343
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:463
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2429
InstTy * Insert(InstTy *I, const Twine &Name="") const
Insert and return the specified instruction.
Definition: IRBuilder.h:142
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:177
Value * CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2323
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2697
BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void preserve()
Mark an analysis as preserved.
Definition: Analysis.h:131
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
bool has(LibFunc F) const
Tests whether a library function is available.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
bool isFCmpOrdCheaperThanFCmpZero(Type *Ty) const
Return true if it is faster to check if a floating-point value is NaN (or not-NaN) versus a compariso...
bool haveFastSqrt(Type *Ty) const
Return true if the hardware has a fast square-root instruction.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:377
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
const ParentTy * getParent() const
Definition: ilist_node.h:32
self_iterator getIterator()
Definition: ilist_node.h:132
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializePartiallyInlineLibCallsLegacyPassPass(PassRegistry &)
FunctionPass * createPartiallyInlineLibCallsPass()
Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...