1 //===---- MipsOptimizeMathLibCalls.cpp - Optimize math lib calls. ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass does an IR transformation which enables the backend to emit native
13 //===----------------------------------------------------------------------===//
15 #include "MipsTargetMachine.h"
16 #include "llvm/IR/IRBuilder.h"
17 #include "llvm/IR/Intrinsics.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Target/TargetLibraryInfo.h"
21 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
25 static cl::opt<bool> DisableOpt("disable-mips-math-optimization",
27 cl::desc("MIPS: Disable math lib call "
28 "optimization."), cl::Hidden);
31 class MipsOptimizeMathLibCalls : public FunctionPass {
35 MipsOptimizeMathLibCalls(MipsTargetMachine &TM_) :
36 FunctionPass(ID), TM(TM_) {}
38 virtual const char *getPassName() const {
39 return "MIPS: Optimize calls to math library functions.";
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
44 virtual bool runOnFunction(Function &F);
47 /// Optimize calls to sqrt.
48 bool optimizeSQRT(CallInst *Call, Function *CalledFunc,
50 Function::iterator &BB);
52 const TargetMachine &TM;
55 char MipsOptimizeMathLibCalls::ID = 0;
58 FunctionPass *llvm::createMipsOptimizeMathLibCalls(MipsTargetMachine &TM) {
59 return new MipsOptimizeMathLibCalls(TM);
62 void MipsOptimizeMathLibCalls::getAnalysisUsage(AnalysisUsage &AU) const {
63 AU.addRequired<TargetLibraryInfo>();
64 FunctionPass::getAnalysisUsage(AU);
67 bool MipsOptimizeMathLibCalls::runOnFunction(Function &F) {
71 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
73 if (Subtarget.inMips16Mode())
77 Function::iterator CurrBB;
78 const TargetLibraryInfo *LibInfo = &getAnalysis<TargetLibraryInfo>();
80 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE;) {
83 for (BasicBlock::iterator II = CurrBB->begin(), IE = CurrBB->end();
85 CallInst *Call = dyn_cast<CallInst>(&*II);
88 if (!Call || !(CalledFunc = Call->getCalledFunction()))
91 LibFunc::Func LibFunc;
92 Attribute A = CalledFunc->getAttributes()
93 .getAttribute(AttributeSet::FunctionIndex, "use-soft-float");
95 // Skip if function has "use-soft-float" attribute.
96 if ((A.isStringAttribute() && (A.getValueAsString() == "true")) ||
97 TM.Options.UseSoftFloat)
100 // Skip if function either has local linkage or is not a known library
102 if (CalledFunc->hasLocalLinkage() || !CalledFunc->hasName() ||
103 !LibInfo->getLibFunc(CalledFunc->getName(), LibFunc))
109 if (optimizeSQRT(Call, CalledFunc, *CurrBB, BB))
124 bool MipsOptimizeMathLibCalls::optimizeSQRT(CallInst *Call,
125 Function *CalledFunc,
127 Function::iterator &BB) {
128 // There is no need to change the IR, since backend will emit sqrt
129 // instruction if the call has already been marked read-only.
130 if (Call->onlyReadsMemory())
133 // Do the following transformation:
139 // v0 = sqrt_noreadmem(src) # native sqrt instruction.
141 // v1 = sqrt(src) # library call.
145 // Move all instructions following Call to newly created block JoinBB.
146 // Create phi and replace all uses.
147 BasicBlock *JoinBB = llvm::SplitBlock(&CurrBB, Call->getNextNode(), this);
148 IRBuilder<> Builder(JoinBB, JoinBB->begin());
149 PHINode *Phi = Builder.CreatePHI(Call->getType(), 2);
150 Call->replaceAllUsesWith(Phi);
152 // Create basic block LibCallBB and insert a call to library function sqrt.
153 BasicBlock *LibCallBB = BasicBlock::Create(CurrBB.getContext(), "call.sqrt",
154 CurrBB.getParent(), JoinBB);
155 Builder.SetInsertPoint(LibCallBB);
156 Instruction *LibCall = Call->clone();
157 Builder.Insert(LibCall);
158 Builder.CreateBr(JoinBB);
160 // Add attribute "readnone" so that backend can use a native sqrt instruction
161 // for this call. Insert a FP compare instruction and a conditional branch
162 // at the end of CurrBB.
163 Call->addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
164 CurrBB.getTerminator()->eraseFromParent();
165 Builder.SetInsertPoint(&CurrBB);
166 Value *FCmp = Builder.CreateFCmpOEQ(Call, Call);
167 Builder.CreateCondBr(FCmp, JoinBB, LibCallBB);
170 Phi->addIncoming(Call, &CurrBB);
171 Phi->addIncoming(LibCall, LibCallBB);