LLVM  10.0.0svn
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 
18 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/Transforms/Scalar.h"
22 
23 using namespace llvm;
24 
25 #define DEBUG_TYPE "partially-inline-libcalls"
26 
27 DEBUG_COUNTER(PILCounter, "partially-inline-libcalls-transform",
28  "Controls transformations in partially-inline-libcalls");
29 
30 static bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
31  BasicBlock &CurrBB, Function::iterator &BB,
32  const TargetTransformInfo *TTI) {
33  // There is no need to change the IR, since backend will emit sqrt
34  // instruction if the call has already been marked read-only.
35  if (Call->onlyReadsMemory())
36  return false;
37 
38  if (!DebugCounter::shouldExecute(PILCounter))
39  return false;
40 
41  // Do the following transformation:
42  //
43  // (before)
44  // dst = sqrt(src)
45  //
46  // (after)
47  // v0 = sqrt_noreadmem(src) # native sqrt instruction.
48  // [if (v0 is a NaN) || if (src < 0)]
49  // v1 = sqrt(src) # library call.
50  // dst = phi(v0, v1)
51  //
52 
53  // Move all instructions following Call to newly created block JoinBB.
54  // Create phi and replace all uses.
55  BasicBlock *JoinBB = llvm::SplitBlock(&CurrBB, Call->getNextNode());
56  IRBuilder<> Builder(JoinBB, JoinBB->begin());
57  Type *Ty = Call->getType();
58  PHINode *Phi = Builder.CreatePHI(Ty, 2);
59  Call->replaceAllUsesWith(Phi);
60 
61  // Create basic block LibCallBB and insert a call to library function sqrt.
62  BasicBlock *LibCallBB = BasicBlock::Create(CurrBB.getContext(), "call.sqrt",
63  CurrBB.getParent(), JoinBB);
64  Builder.SetInsertPoint(LibCallBB);
65  Instruction *LibCall = Call->clone();
66  Builder.Insert(LibCall);
67  Builder.CreateBr(JoinBB);
68 
69  // Add attribute "readnone" so that backend can use a native sqrt instruction
70  // for this call. Insert a FP compare instruction and a conditional branch
71  // at the end of CurrBB.
72  Call->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
73  CurrBB.getTerminator()->eraseFromParent();
74  Builder.SetInsertPoint(&CurrBB);
75  Value *FCmp = TTI->isFCmpOrdCheaperThanFCmpZero(Ty)
76  ? Builder.CreateFCmpORD(Call, Call)
77  : Builder.CreateFCmpOGE(Call->getOperand(0),
78  ConstantFP::get(Ty, 0.0));
79  Builder.CreateCondBr(FCmp, JoinBB, LibCallBB);
80 
81  // Add phi operands.
82  Phi->addIncoming(Call, &CurrBB);
83  Phi->addIncoming(LibCall, LibCallBB);
84 
85  BB = JoinBB->getIterator();
86  return true;
87 }
88 
90  const TargetTransformInfo *TTI) {
91  bool Changed = false;
92 
93  Function::iterator CurrBB;
94  for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE;) {
95  CurrBB = BB++;
96 
97  for (BasicBlock::iterator II = CurrBB->begin(), IE = CurrBB->end();
98  II != IE; ++II) {
99  CallInst *Call = dyn_cast<CallInst>(&*II);
100  Function *CalledFunc;
101 
102  if (!Call || !(CalledFunc = Call->getCalledFunction()))
103  continue;
104 
105  if (Call->isNoBuiltin())
106  continue;
107 
108  // Skip if function either has local linkage or is not a known library
109  // function.
110  LibFunc LF;
111  if (CalledFunc->hasLocalLinkage() ||
112  !TLI->getLibFunc(*CalledFunc, LF) || !TLI->has(LF))
113  continue;
114 
115  switch (LF) {
116  case LibFunc_sqrtf:
117  case LibFunc_sqrt:
118  if (TTI->haveFastSqrt(Call->getType()) &&
119  optimizeSQRT(Call, CalledFunc, *CurrBB, BB, TTI))
120  break;
121  continue;
122  default:
123  continue;
124  }
125 
126  Changed = true;
127  break;
128  }
129  }
130 
131  return Changed;
132 }
133 
136  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
137  auto &TTI = AM.getResult<TargetIRAnalysis>(F);
138  if (!runPartiallyInlineLibCalls(F, &TLI, &TTI))
139  return PreservedAnalyses::all();
140  return PreservedAnalyses::none();
141 }
142 
143 namespace {
144 class PartiallyInlineLibCallsLegacyPass : public FunctionPass {
145 public:
146  static char ID;
147 
148  PartiallyInlineLibCallsLegacyPass() : FunctionPass(ID) {
151  }
152 
153  void getAnalysisUsage(AnalysisUsage &AU) const override {
157  }
158 
159  bool runOnFunction(Function &F) override {
160  if (skipFunction(F))
161  return false;
162 
163  TargetLibraryInfo *TLI =
164  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
165  const TargetTransformInfo *TTI =
166  &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
167  return runPartiallyInlineLibCalls(F, TLI, TTI);
168  }
169 };
170 }
171 
173 INITIALIZE_PASS_BEGIN(PartiallyInlineLibCallsLegacyPass,
174  "partially-inline-libcalls",
175  "Partially inline calls to library functions", false,
176  false)
179 INITIALIZE_PASS_END(PartiallyInlineLibCallsLegacyPass,
180  "partially-inline-libcalls",
181  "Partially inline calls to library functions", false, false)
182 
184  return new PartiallyInlineLibCallsLegacyPass();
185 }
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:288
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:776
This class represents lattice values for constants.
Definition: AllocatorList.h:23
iterator end()
Definition: Function.h:682
This class represents a function call, abstracting a target machine&#39;s calling convention.
Analysis pass providing the TargetTransformInfo.
F(f)
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.cpp:144
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the specified block at the specified instruction - everything before SplitPt stays in Old and e...
void addAttribute(unsigned i, Attribute::AttrKind Kind)
adds the attribute to the list of attributes.
Definition: InstrTypes.h:1383
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:32
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:273
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:779
INITIALIZE_PASS_BEGIN(PartiallyInlineLibCallsLegacyPass, "partially-inline-libcalls", "Partially inline calls to library functions", false, false) INITIALIZE_PASS_END(PartiallyInlineLibCallsLegacyPass
This file provides an implementation of debug counters.
DEBUG_COUNTER(PILCounter, "partially-inline-libcalls-transform", "Controls transformations in partially-inline-libcalls")
FunctionPass * createPartiallyInlineLibCallsPass()
Instruction * clone() const
Create a copy of &#39;this&#39; instruction that is identical in all ways except the following: ...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:96
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
bool has(LibFunc F) const
Tests whether a library function is available.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
iterator begin()
Definition: Function.h:680
Value * getOperand(unsigned i) const
Definition: User.h:169
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
static bool runOnFunction(Function &F, bool PostInlining)
Wrapper pass for TargetTransformInfo.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
Definition: InstrTypes.h:1611
static bool runPartiallyInlineLibCalls(Function &F, TargetLibraryInfo *TLI, const TargetTransformInfo *TTI)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:99
self_iterator getIterator()
Definition: ilist_node.h:81
static bool shouldExecute(unsigned CounterName)
Definition: DebugCounter.h:73
bool haveFastSqrt(Type *Ty) const
Return true if the hardware has a fast square-root instruction.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
Iterator for intrusive lists based on ilist_node.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Provides information about what library functions are available for the current target.
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
Definition: Constants.cpp:716
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
Definition: InstrTypes.h:1287
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
bool onlyReadsMemory(unsigned OpNo) const
Definition: InstrTypes.h:1557
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
Analysis pass providing the TargetLibraryInfo.
LLVM Value Representation.
Definition: Value.h:74
static bool optimizeSQRT(CallInst *Call, Function *CalledFunc, BasicBlock &CurrBB, Function::iterator &BB, const TargetTransformInfo *TTI)
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...
A container for analyses that lazily runs them and caches their results.
This pass exposes codegen information to IR-level passes.
void initializePartiallyInlineLibCallsLegacyPassPass(PassRegistry &)