LLVM  3.7.0
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 
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/Intrinsics.h"
19 #include "llvm/Pass.h"
22 #include "llvm/Transforms/Scalar.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "partially-inline-libcalls"
28 
29 namespace {
30  class PartiallyInlineLibCalls : public FunctionPass {
31  public:
32  static char ID;
33 
34  PartiallyInlineLibCalls() :
35  FunctionPass(ID) {
37  }
38 
39  void getAnalysisUsage(AnalysisUsage &AU) const override;
40  bool runOnFunction(Function &F) override;
41 
42  private:
43  /// Optimize calls to sqrt.
44  bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
45  BasicBlock &CurrBB, Function::iterator &BB);
46  };
47 
49 }
50 
51 INITIALIZE_PASS(PartiallyInlineLibCalls, "partially-inline-libcalls",
52  "Partially inline calls to library functions", false, false)
53 
54 void PartiallyInlineLibCalls::getAnalysisUsage(AnalysisUsage &AU) const {
55  AU.addRequired<TargetLibraryInfoWrapperPass>();
56  AU.addRequired<TargetTransformInfoWrapperPass>();
58 }
59 
60 bool PartiallyInlineLibCalls::runOnFunction(Function &F) {
61  bool Changed = false;
62  Function::iterator CurrBB;
63  TargetLibraryInfo *TLI =
64  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
65  const TargetTransformInfo *TTI =
66  &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
67  for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE;) {
68  CurrBB = BB++;
69 
70  for (BasicBlock::iterator II = CurrBB->begin(), IE = CurrBB->end();
71  II != IE; ++II) {
72  CallInst *Call = dyn_cast<CallInst>(&*II);
73  Function *CalledFunc;
74 
75  if (!Call || !(CalledFunc = Call->getCalledFunction()))
76  continue;
77 
78  // Skip if function either has local linkage or is not a known library
79  // function.
80  LibFunc::Func LibFunc;
81  if (CalledFunc->hasLocalLinkage() || !CalledFunc->hasName() ||
82  !TLI->getLibFunc(CalledFunc->getName(), LibFunc))
83  continue;
84 
85  switch (LibFunc) {
86  case LibFunc::sqrtf:
87  case LibFunc::sqrt:
88  if (TTI->haveFastSqrt(Call->getType()) &&
89  optimizeSQRT(Call, CalledFunc, *CurrBB, BB))
90  break;
91  continue;
92  default:
93  continue;
94  }
95 
96  Changed = true;
97  break;
98  }
99  }
100 
101  return Changed;
102 }
103 
104 bool PartiallyInlineLibCalls::optimizeSQRT(CallInst *Call,
105  Function *CalledFunc,
106  BasicBlock &CurrBB,
107  Function::iterator &BB) {
108  // There is no need to change the IR, since backend will emit sqrt
109  // instruction if the call has already been marked read-only.
110  if (Call->onlyReadsMemory())
111  return false;
112 
113  // The call must have the expected result type.
114  if (!Call->getType()->isFloatingPointTy())
115  return false;
116 
117  // Do the following transformation:
118  //
119  // (before)
120  // dst = sqrt(src)
121  //
122  // (after)
123  // v0 = sqrt_noreadmem(src) # native sqrt instruction.
124  // if (v0 is a NaN)
125  // v1 = sqrt(src) # library call.
126  // dst = phi(v0, v1)
127  //
128 
129  // Move all instructions following Call to newly created block JoinBB.
130  // Create phi and replace all uses.
131  BasicBlock *JoinBB = llvm::SplitBlock(&CurrBB, Call->getNextNode());
132  IRBuilder<> Builder(JoinBB, JoinBB->begin());
133  PHINode *Phi = Builder.CreatePHI(Call->getType(), 2);
134  Call->replaceAllUsesWith(Phi);
135 
136  // Create basic block LibCallBB and insert a call to library function sqrt.
137  BasicBlock *LibCallBB = BasicBlock::Create(CurrBB.getContext(), "call.sqrt",
138  CurrBB.getParent(), JoinBB);
139  Builder.SetInsertPoint(LibCallBB);
140  Instruction *LibCall = Call->clone();
141  Builder.Insert(LibCall);
142  Builder.CreateBr(JoinBB);
143 
144  // Add attribute "readnone" so that backend can use a native sqrt instruction
145  // for this call. Insert a FP compare instruction and a conditional branch
146  // at the end of CurrBB.
148  CurrBB.getTerminator()->eraseFromParent();
149  Builder.SetInsertPoint(&CurrBB);
150  Value *FCmp = Builder.CreateFCmpOEQ(Call, Call);
151  Builder.CreateCondBr(FCmp, JoinBB, LibCallBB);
152 
153  // Add phi operands.
154  Phi->addIncoming(Call, &CurrBB);
155  Phi->addIncoming(LibCall, LibCallBB);
156 
157  BB = JoinBB;
158  return true;
159 }
160 
162  return new PartiallyInlineLibCalls();
163 }
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
SplitBlock - Split the specified block at the specified instruction - every thing before SplitPt stay...
iterator end()
Definition: Function.h:459
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:78
CallInst - This class represents a function call, abstracting a target machine's calling convention...
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
F(f)
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
bool onlyReadsMemory() const
Determine if the call does not access or only reads memory.
NodeTy * getNextNode()
Get the next node, or 0 for the list tail.
Definition: ilist_node.h:80
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:517
Instruction * clone() const
clone() - Create a copy of 'this' instruction that is identical in all ways except the following: ...
Function does not access memory.
Definition: Attributes.h:99
bool isFloatingPointTy() const
isFloatingPointTy - Return true if this is one of the six floating point types
Definition: Type.h:159
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
iterator begin()
Definition: Function.h:457
void addAttribute(unsigned i, Attribute::AttrKind attr)
addAttribute - adds the attribute to the list of attributes.
bool getLibFunc(StringRef funcName, LibFunc::Func &F) const
Searches for a particular function name.
Wrapper pass for TargetTransformInfo.
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:103
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
Provides information about what library functions are available for the current target.
Function * getCalledFunction() const
getCalledFunction - Return the function called, or null if this is an indirect function invocation...
bool haveFastSqrt(Type *Ty) const
Return true if the hardware has a fast square-root instruction.
LLVM_ATTRIBUTE_UNUSED_RESULT 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:285
FunctionPass * createPartiallyInlineLibCallsPass()
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:32
LLVM Value Representation.
Definition: Value.h:69
void initializePartiallyInlineLibCallsPass(PassRegistry &)
This pass exposes codegen information to IR-level passes.