Dan Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 1 | //===- 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 Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Dan Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | namespace { |
| 29 | /// This pass optimizes well half_powr function calls. |
| 30 | /// |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 31 | class SimplifyHalfPowrLibCalls : public FunctionPass { |
Dan Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 32 | const TargetData *TD; |
| 33 | public: |
| 34 | static char ID; // Pass identification |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 35 | SimplifyHalfPowrLibCalls() : FunctionPass(ID) {} |
Dan Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 36 | |
| 37 | bool runOnFunction(Function &F); |
| 38 | |
| 39 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | Instruction * |
| 43 | InlineHalfPowrs(const std::vector<Instruction *> &HalfPowrs, |
| 44 | Instruction *InsertPt); |
| 45 | }; |
| 46 | char SimplifyHalfPowrLibCalls::ID = 0; |
| 47 | } // end anonymous namespace. |
| 48 | |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 49 | INITIALIZE_PASS(SimplifyHalfPowrLibCalls, "simplify-libcalls-halfpowr", |
| 50 | "Simplify half_powr library calls", false, false); |
Dan Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 51 | |
| 52 | // Public interface to the Simplify HalfPowr LibCalls pass. |
| 53 | FunctionPass *llvm::createSimplifyHalfPowrLibCallsPass() { |
| 54 | return new SimplifyHalfPowrLibCalls(); |
| 55 | } |
| 56 | |
| 57 | /// InlineHalfPowrs - Inline a sequence of adjacent half_powr calls, rearranging |
| 58 | /// their control flow to better facilitate subsequent optimization. |
| 59 | Instruction * |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 60 | SimplifyHalfPowrLibCalls:: |
| 61 | InlineHalfPowrs(const std::vector<Instruction *> &HalfPowrs, |
| 62 | Instruction *InsertPt) { |
Dan Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 63 | std::vector<BasicBlock *> Bodies; |
| 64 | BasicBlock *NewBlock = 0; |
| 65 | |
| 66 | for (unsigned i = 0, e = HalfPowrs.size(); i != e; ++i) { |
| 67 | CallInst *Call = cast<CallInst>(HalfPowrs[i]); |
| 68 | Function *Callee = Call->getCalledFunction(); |
| 69 | |
| 70 | // Minimally sanity-check the CFG of half_powr to ensure that it contains |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 71 | // the kind of code we expect. If we're running this pass, we have |
Dan Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 72 | // reason to believe it will be what we expect. |
| 73 | Function::iterator I = Callee->begin(); |
| 74 | BasicBlock *Prologue = I++; |
| 75 | if (I == Callee->end()) break; |
| 76 | BasicBlock *SubnormalHandling = I++; |
| 77 | if (I == Callee->end()) break; |
| 78 | BasicBlock *Body = I++; |
| 79 | if (I != Callee->end()) break; |
| 80 | if (SubnormalHandling->getSinglePredecessor() != Prologue) |
| 81 | break; |
| 82 | BranchInst *PBI = dyn_cast<BranchInst>(Prologue->getTerminator()); |
| 83 | if (!PBI || !PBI->isConditional()) |
| 84 | break; |
| 85 | BranchInst *SNBI = dyn_cast<BranchInst>(SubnormalHandling->getTerminator()); |
| 86 | if (!SNBI || SNBI->isConditional()) |
| 87 | break; |
| 88 | if (!isa<ReturnInst>(Body->getTerminator())) |
| 89 | break; |
| 90 | |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 91 | Instruction *NextInst = llvm::next(BasicBlock::iterator(Call)); |
Dan Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 92 | |
| 93 | // Inline the call, taking care of what code ends up where. |
| 94 | NewBlock = SplitBlock(NextInst->getParent(), NextInst, this); |
| 95 | |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 96 | InlineFunctionInfo IFI(0, TD); |
| 97 | bool B = InlineFunction(Call, IFI); |
Chris Lattner | 528f16b | 2008-12-14 21:36:23 +0000 | [diff] [blame] | 98 | assert(B && "half_powr didn't inline?"); B=B; |
Dan Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 99 | |
| 100 | BasicBlock *NewBody = NewBlock->getSinglePredecessor(); |
| 101 | assert(NewBody); |
| 102 | Bodies.push_back(NewBody); |
| 103 | } |
| 104 | |
| 105 | if (!NewBlock) |
| 106 | return InsertPt; |
| 107 | |
| 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()); |
| 117 | } |
| 118 | |
| 119 | return NewBlock->begin(); |
| 120 | } |
| 121 | |
| 122 | /// runOnFunction - Top level algorithm. |
| 123 | /// |
| 124 | bool SimplifyHalfPowrLibCalls::runOnFunction(Function &F) { |
Dan Gohman | 02a436c | 2009-07-24 18:13:53 +0000 | [diff] [blame] | 125 | TD = getAnalysisIfAvailable<TargetData>(); |
Dan Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 126 | |
| 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) { |
| 131 | // Look for calls. |
| 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. |
Daniel Dunbar | 460f656 | 2009-07-26 09:48:23 +0000 | [diff] [blame] | 138 | if (Callee->getName() == "__half_powrf4") |
Dan Gohman | 8f027c7 | 2008-11-04 23:41:45 +0000 | [diff] [blame] | 139 | IsHalfPowr = true; |
| 140 | } |
| 141 | } |
| 142 | if (IsHalfPowr) |
| 143 | HalfPowrs.push_back(I); |
| 144 | // We're looking for sequences of up to three such calls, which we'll |
| 145 | // simplify as a group. |
| 146 | if ((!IsHalfPowr && !HalfPowrs.empty()) || HalfPowrs.size() == 3) { |
| 147 | I = InlineHalfPowrs(HalfPowrs, I); |
| 148 | E = I->getParent()->end(); |
| 149 | HalfPowrs.clear(); |
| 150 | Changed = true; |
| 151 | } |
| 152 | } |
| 153 | assert(HalfPowrs.empty() && "Block had no terminator!"); |
| 154 | } |
| 155 | |
| 156 | return Changed; |
| 157 | } |