1 //===- SimplifyHalfPowrLibCalls.cpp - Optimize specific half_powr 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 file implements a simple pass that applies an experimental
11 // transformation on calls to specific functions.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "simplify-libcalls-halfpowr"
16 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
22 #include "llvm/Transforms/Utils/Cloning.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Config/config.h"
31 /// This pass optimizes well half_powr function calls.
33 class VISIBILITY_HIDDEN SimplifyHalfPowrLibCalls : public FunctionPass {
36 static char ID; // Pass identification
37 SimplifyHalfPowrLibCalls() : FunctionPass(&ID) {}
39 bool runOnFunction(Function &F);
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 InlineHalfPowrs(const std::vector<Instruction *> &HalfPowrs,
46 Instruction *InsertPt);
48 char SimplifyHalfPowrLibCalls::ID = 0;
49 } // end anonymous namespace.
51 static RegisterPass<SimplifyHalfPowrLibCalls>
52 X("simplify-libcalls-halfpowr", "Simplify half_powr library calls");
54 // Public interface to the Simplify HalfPowr LibCalls pass.
55 FunctionPass *llvm::createSimplifyHalfPowrLibCallsPass() {
56 return new SimplifyHalfPowrLibCalls();
59 /// InlineHalfPowrs - Inline a sequence of adjacent half_powr calls, rearranging
60 /// their control flow to better facilitate subsequent optimization.
62 SimplifyHalfPowrLibCalls::InlineHalfPowrs(const std::vector<Instruction *> &HalfPowrs,
63 Instruction *InsertPt) {
64 std::vector<BasicBlock *> Bodies;
65 BasicBlock *NewBlock = 0;
67 for (unsigned i = 0, e = HalfPowrs.size(); i != e; ++i) {
68 CallInst *Call = cast<CallInst>(HalfPowrs[i]);
69 Function *Callee = Call->getCalledFunction();
71 // Minimally sanity-check the CFG of half_powr to ensure that it contains
72 // the the kind of code we expect. If we're running this pass, we have
73 // reason to believe it will be what we expect.
74 Function::iterator I = Callee->begin();
75 BasicBlock *Prologue = I++;
76 if (I == Callee->end()) break;
77 BasicBlock *SubnormalHandling = I++;
78 if (I == Callee->end()) break;
79 BasicBlock *Body = I++;
80 if (I != Callee->end()) break;
81 if (SubnormalHandling->getSinglePredecessor() != Prologue)
83 BranchInst *PBI = dyn_cast<BranchInst>(Prologue->getTerminator());
84 if (!PBI || !PBI->isConditional())
86 BranchInst *SNBI = dyn_cast<BranchInst>(SubnormalHandling->getTerminator());
87 if (!SNBI || SNBI->isConditional())
89 if (!isa<ReturnInst>(Body->getTerminator()))
92 Instruction *NextInst = next(BasicBlock::iterator(Call));
94 // Inline the call, taking care of what code ends up where.
95 NewBlock = SplitBlock(NextInst->getParent(), NextInst, this);
97 bool B = InlineFunction(Call, 0, TD);
98 assert(B && "half_powr didn't inline?"); B=B;
100 BasicBlock *NewBody = NewBlock->getSinglePredecessor();
102 Bodies.push_back(NewBody);
108 // Put the code for all the bodies into one block, to facilitate
109 // subsequent optimization.
110 (void)SplitEdge(NewBlock->getSinglePredecessor(), NewBlock, this);
111 for (unsigned i = 0, e = Bodies.size(); i != e; ++i) {
112 BasicBlock *Body = Bodies[i];
113 Instruction *FNP = Body->getFirstNonPHI();
114 // Splice the insts from body into NewBlock.
115 NewBlock->getInstList().splice(NewBlock->begin(), Body->getInstList(),
116 FNP, Body->getTerminator());
119 return NewBlock->begin();
122 /// runOnFunction - Top level algorithm.
124 bool SimplifyHalfPowrLibCalls::runOnFunction(Function &F) {
125 TD = getAnalysisIfAvailable<TargetData>();
127 bool Changed = false;
128 std::vector<Instruction *> HalfPowrs;
129 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
130 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
132 bool IsHalfPowr = false;
133 if (CallInst *CI = dyn_cast<CallInst>(I)) {
134 // Look for direct calls and calls to non-external functions.
135 Function *Callee = CI->getCalledFunction();
136 if (Callee && Callee->hasExternalLinkage()) {
137 // Look for calls with well-known names.
138 const char *CalleeName = Callee->getNameStart();
139 if (strcmp(CalleeName, "__half_powrf4") == 0)
144 HalfPowrs.push_back(I);
145 // We're looking for sequences of up to three such calls, which we'll
146 // simplify as a group.
147 if ((!IsHalfPowr && !HalfPowrs.empty()) || HalfPowrs.size() == 3) {
148 I = InlineHalfPowrs(HalfPowrs, I);
149 E = I->getParent()->end();
154 assert(HalfPowrs.empty() && "Block had no terminator!");