LLVM  7.0.0svn
PartiallyInlineLibCalls.cpp
Go to the documentation of this file.
1 //===--- PartiallyInlineLibCalls.cpp - Partially inline libcalls ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass tries to partially inline the fast path of well-known library
11 // functions, such as using square-root instructions for cases where sqrt()
12 // does not need to set errno.
13 //
14 //===----------------------------------------------------------------------===//
15 
19 #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 
28 static bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
29  BasicBlock &CurrBB, Function::iterator &BB,
30  const TargetTransformInfo *TTI) {
31  // There is no need to change the IR, since backend will emit sqrt
32  // instruction if the call has already been marked read-only.
33  if (Call->onlyReadsMemory())
34  return false;
35 
36  // Do the following transformation:
37  //
38  // (before)
39  // dst = sqrt(src)
40  //
41  // (after)
42  // v0 = sqrt_noreadmem(src) # native sqrt instruction.
43  // [if (v0 is a NaN) || if (src < 0)]
44  // v1 = sqrt(src) # library call.
45  // dst = phi(v0, v1)
46  //
47 
48  // Move all instructions following Call to newly created block JoinBB.
49  // Create phi and replace all uses.
50  BasicBlock *JoinBB = llvm::SplitBlock(&CurrBB, Call->getNextNode());
51  IRBuilder<> Builder(JoinBB, JoinBB->begin());
52  Type *Ty = Call->getType();
53  PHINode *Phi = Builder.CreatePHI(Ty, 2);
54  Call->replaceAllUsesWith(Phi);
55 
56  // Create basic block LibCallBB and insert a call to library function sqrt.
57  BasicBlock *LibCallBB = BasicBlock::Create(CurrBB.getContext(), "call.sqrt",
58  CurrBB.getParent(), JoinBB);
59  Builder.SetInsertPoint(LibCallBB);
60  Instruction *LibCall = Call->clone();
61  Builder.Insert(LibCall);
62  Builder.CreateBr(JoinBB);
63 
64  // Add attribute "readnone" so that backend can use a native sqrt instruction
65  // for this call. Insert a FP compare instruction and a conditional branch
66  // at the end of CurrBB.
67  Call->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
68  CurrBB.getTerminator()->eraseFromParent();
69  Builder.SetInsertPoint(&CurrBB);
70  Value *FCmp = TTI->isFCmpOrdCheaperThanFCmpZero(Ty)
71  ? Builder.CreateFCmpORD(Call, Call)
72  : Builder.CreateFCmpOGE(Call->getOperand(0),
73  ConstantFP::get(Ty, 0.0));
74  Builder.CreateCondBr(FCmp, JoinBB, LibCallBB);
75 
76  // Add phi operands.
77  Phi->addIncoming(Call, &CurrBB);
78  Phi->addIncoming(LibCall, LibCallBB);
79 
80  BB = JoinBB->getIterator();
81  return true;
82 }
83 
85  const TargetTransformInfo *TTI) {
86  bool Changed = false;
87 
88  Function::iterator CurrBB;
89  for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE;) {
90  CurrBB = BB++;
91 
92  for (BasicBlock::iterator II = CurrBB->begin(), IE = CurrBB->end();
93  II != IE; ++II) {
94  CallInst *Call = dyn_cast<CallInst>(&*II);
95  Function *CalledFunc;
96 
97  if (!Call || !(CalledFunc = Call->getCalledFunction()))
98  continue;
99 
100  if (Call->isNoBuiltin())
101  continue;
102 
103  // Skip if function either has local linkage or is not a known library
104  // function.
105  LibFunc LF;
106  if (CalledFunc->hasLocalLinkage() ||
107  !TLI->getLibFunc(*CalledFunc, LF) || !TLI->has(LF))
108  continue;
109 
110  switch (LF) {
111  case LibFunc_sqrtf:
112  case LibFunc_sqrt:
113  if (TTI->haveFastSqrt(Call->getType()) &&
114  optimizeSQRT(Call, CalledFunc, *CurrBB, BB, TTI))
115  break;
116  continue;
117  default:
118  continue;
119  }
120 
121  Changed = true;
122  break;
123  }
124  }
125 
126  return Changed;
127 }
128 
131  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
132  auto &TTI = AM.getResult<TargetIRAnalysis>(F);
133  if (!runPartiallyInlineLibCalls(F, &TLI, &TTI))
134  return PreservedAnalyses::all();
135  return PreservedAnalyses::none();
136 }
137 
138 namespace {
139 class PartiallyInlineLibCallsLegacyPass : public FunctionPass {
140 public:
141  static char ID;
142 
143  PartiallyInlineLibCallsLegacyPass() : FunctionPass(ID) {
146  }
147 
148  void getAnalysisUsage(AnalysisUsage &AU) const override {
152  }
153 
154  bool runOnFunction(Function &F) override {
155  if (skipFunction(F))
156  return false;
157 
158  TargetLibraryInfo *TLI =
159  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
160  const TargetTransformInfo *TTI =
161  &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
162  return runPartiallyInlineLibCalls(F, TLI, TTI);
163  }
164 };
165 }
166 
168 INITIALIZE_PASS_BEGIN(PartiallyInlineLibCallsLegacyPass,
169  "partially-inline-libcalls",
170  "Partially inline calls to library functions", false,
171  false)
174 INITIALIZE_PASS_END(PartiallyInlineLibCallsLegacyPass,
175  "partially-inline-libcalls",
176  "Partially inline calls to library functions", false, false)
177 
179  return new PartiallyInlineLibCallsLegacyPass();
180 }
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
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:289
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:687
void addAttribute(unsigned i, Attribute::AttrKind Kind)
adds the attribute to the list of attributes.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the specified block at the specified instruction - everything before SplitPt stays in Old and e...
iterator end()
Definition: Function.h:644
This class represents a function call, abstracting a target machine&#39;s calling convention.
Analysis pass providing the TargetTransformInfo.
F(f)
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:33
bool onlyReadsMemory() const
Determine if the call does not access or only reads memory.
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:264
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:731
INITIALIZE_PASS_BEGIN(PartiallyInlineLibCallsLegacyPass, "partially-inline-libcalls", "Partially inline calls to library functions", false, false) INITIALIZE_PASS_END(PartiallyInlineLibCallsLegacyPass
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:92
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
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:439
iterator begin()
Definition: Function.h:642
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
Value * getOperand(unsigned i_nocapture) const
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:59
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
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:285
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:101
self_iterator getIterator()
Definition: ilist_node.h:82
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:674
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
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:323
Analysis pass providing the TargetLibraryInfo.
LLVM Value Representation.
Definition: Value.h:73
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 &)
const TerminatorInst * 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:138