blob: 87c9d63d58f477c00eae43de0a6ef420d0e55e53 [file] [log] [blame]
Dan Gohman8f027c72008-11-04 23:41:45 +00001//===- SimplifyHalfPowrLibCalls.cpp - Optimize specific half_powr calls ---===//
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 file implements a simple pass that applies an experimental
11// transformation on calls to specific functions.
12//
13//===----------------------------------------------------------------------===//
14
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"
Dan Gohman8f027c72008-11-04 23:41:45 +000025#include "llvm/Support/Debug.h"
Dan Gohman8f027c72008-11-04 23:41:45 +000026using namespace llvm;
27
28namespace {
29 /// This pass optimizes well half_powr function calls.
30 ///
Chris Lattner3e8b6632009-09-02 06:11:42 +000031 class SimplifyHalfPowrLibCalls : public FunctionPass {
Dan Gohman8f027c72008-11-04 23:41:45 +000032 const TargetData *TD;
33 public:
34 static char ID; // Pass identification
Owen Anderson081c34b2010-10-19 17:21:58 +000035 SimplifyHalfPowrLibCalls() : FunctionPass(ID) {
36 initializeSimplifyHalfPowrLibCallsPass(*PassRegistry::getPassRegistry());
37 }
Dan Gohman8f027c72008-11-04 23:41:45 +000038
39 bool runOnFunction(Function &F);
40
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman8f027c72008-11-04 23:41:45 +000042 }
43
44 Instruction *
45 InlineHalfPowrs(const std::vector<Instruction *> &HalfPowrs,
46 Instruction *InsertPt);
47 };
48 char SimplifyHalfPowrLibCalls::ID = 0;
49} // end anonymous namespace.
50
Owen Andersond13db2c2010-07-21 22:09:45 +000051INITIALIZE_PASS(SimplifyHalfPowrLibCalls, "simplify-libcalls-halfpowr",
Owen Andersonce665bd2010-10-07 22:25:06 +000052 "Simplify half_powr library calls", false, false)
Dan Gohman8f027c72008-11-04 23:41:45 +000053
54// Public interface to the Simplify HalfPowr LibCalls pass.
55FunctionPass *llvm::createSimplifyHalfPowrLibCallsPass() {
56 return new SimplifyHalfPowrLibCalls();
57}
58
59/// InlineHalfPowrs - Inline a sequence of adjacent half_powr calls, rearranging
60/// their control flow to better facilitate subsequent optimization.
61Instruction *
Chris Lattner3e8b6632009-09-02 06:11:42 +000062SimplifyHalfPowrLibCalls::
63InlineHalfPowrs(const std::vector<Instruction *> &HalfPowrs,
64 Instruction *InsertPt) {
Dan Gohman8f027c72008-11-04 23:41:45 +000065 std::vector<BasicBlock *> Bodies;
66 BasicBlock *NewBlock = 0;
67
68 for (unsigned i = 0, e = HalfPowrs.size(); i != e; ++i) {
69 CallInst *Call = cast<CallInst>(HalfPowrs[i]);
70 Function *Callee = Call->getCalledFunction();
71
72 // Minimally sanity-check the CFG of half_powr to ensure that it contains
Dan Gohmanf451cb82010-02-10 16:03:48 +000073 // the kind of code we expect. If we're running this pass, we have
Dan Gohman8f027c72008-11-04 23:41:45 +000074 // reason to believe it will be what we expect.
75 Function::iterator I = Callee->begin();
76 BasicBlock *Prologue = I++;
77 if (I == Callee->end()) break;
78 BasicBlock *SubnormalHandling = I++;
79 if (I == Callee->end()) break;
80 BasicBlock *Body = I++;
81 if (I != Callee->end()) break;
82 if (SubnormalHandling->getSinglePredecessor() != Prologue)
83 break;
84 BranchInst *PBI = dyn_cast<BranchInst>(Prologue->getTerminator());
85 if (!PBI || !PBI->isConditional())
86 break;
87 BranchInst *SNBI = dyn_cast<BranchInst>(SubnormalHandling->getTerminator());
88 if (!SNBI || SNBI->isConditional())
89 break;
90 if (!isa<ReturnInst>(Body->getTerminator()))
91 break;
92
Chris Lattner7896c9f2009-12-03 00:50:42 +000093 Instruction *NextInst = llvm::next(BasicBlock::iterator(Call));
Dan Gohman8f027c72008-11-04 23:41:45 +000094
95 // Inline the call, taking care of what code ends up where.
96 NewBlock = SplitBlock(NextInst->getParent(), NextInst, this);
97
Chris Lattner60915142010-04-22 23:07:58 +000098 InlineFunctionInfo IFI(0, TD);
99 bool B = InlineFunction(Call, IFI);
Chris Lattner528f16b2008-12-14 21:36:23 +0000100 assert(B && "half_powr didn't inline?"); B=B;
Dan Gohman8f027c72008-11-04 23:41:45 +0000101
102 BasicBlock *NewBody = NewBlock->getSinglePredecessor();
103 assert(NewBody);
104 Bodies.push_back(NewBody);
105 }
106
107 if (!NewBlock)
108 return InsertPt;
109
110 // Put the code for all the bodies into one block, to facilitate
111 // subsequent optimization.
112 (void)SplitEdge(NewBlock->getSinglePredecessor(), NewBlock, this);
113 for (unsigned i = 0, e = Bodies.size(); i != e; ++i) {
114 BasicBlock *Body = Bodies[i];
115 Instruction *FNP = Body->getFirstNonPHI();
116 // Splice the insts from body into NewBlock.
117 NewBlock->getInstList().splice(NewBlock->begin(), Body->getInstList(),
118 FNP, Body->getTerminator());
119 }
120
121 return NewBlock->begin();
122}
123
124/// runOnFunction - Top level algorithm.
125///
126bool SimplifyHalfPowrLibCalls::runOnFunction(Function &F) {
Dan Gohman02a436c2009-07-24 18:13:53 +0000127 TD = getAnalysisIfAvailable<TargetData>();
Dan Gohman8f027c72008-11-04 23:41:45 +0000128
129 bool Changed = false;
130 std::vector<Instruction *> HalfPowrs;
131 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
132 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
133 // Look for calls.
134 bool IsHalfPowr = false;
135 if (CallInst *CI = dyn_cast<CallInst>(I)) {
136 // Look for direct calls and calls to non-external functions.
137 Function *Callee = CI->getCalledFunction();
138 if (Callee && Callee->hasExternalLinkage()) {
139 // Look for calls with well-known names.
Daniel Dunbar460f6562009-07-26 09:48:23 +0000140 if (Callee->getName() == "__half_powrf4")
Dan Gohman8f027c72008-11-04 23:41:45 +0000141 IsHalfPowr = true;
142 }
143 }
144 if (IsHalfPowr)
145 HalfPowrs.push_back(I);
146 // We're looking for sequences of up to three such calls, which we'll
147 // simplify as a group.
148 if ((!IsHalfPowr && !HalfPowrs.empty()) || HalfPowrs.size() == 3) {
149 I = InlineHalfPowrs(HalfPowrs, I);
150 E = I->getParent()->end();
151 HalfPowrs.clear();
152 Changed = true;
153 }
154 }
155 assert(HalfPowrs.empty() && "Block had no terminator!");
156 }
157
158 return Changed;
159}