LLVM API Documentation
00001 //===- SimplifyHalfPowrLibCalls.cpp - Optimize specific half_powr calls ---===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements a simple pass that applies an experimental 00011 // transformation on calls to specific functions. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "simplify-libcalls-halfpowr" 00016 #include "llvm/Transforms/Scalar.h" 00017 #include "llvm/Instructions.h" 00018 #include "llvm/Intrinsics.h" 00019 #include "llvm/Module.h" 00020 #include "llvm/Pass.h" 00021 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 00022 #include "llvm/Transforms/Utils/Cloning.h" 00023 #include "llvm/Target/TargetData.h" 00024 #include "llvm/ADT/STLExtras.h" 00025 #include "llvm/Support/Compiler.h" 00026 #include "llvm/Support/Debug.h" 00027 #include "llvm/Config/config.h" 00028 using namespace llvm; 00029 00030 namespace { 00031 /// This pass optimizes well half_powr function calls. 00032 /// 00033 class VISIBILITY_HIDDEN SimplifyHalfPowrLibCalls : public FunctionPass { 00034 const TargetData *TD; 00035 public: 00036 static char ID; // Pass identification 00037 SimplifyHalfPowrLibCalls() : FunctionPass(&ID) {} 00038 00039 bool runOnFunction(Function &F); 00040 00041 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00042 } 00043 00044 Instruction * 00045 InlineHalfPowrs(const std::vector<Instruction *> &HalfPowrs, 00046 Instruction *InsertPt); 00047 }; 00048 char SimplifyHalfPowrLibCalls::ID = 0; 00049 } // end anonymous namespace. 00050 00051 static RegisterPass<SimplifyHalfPowrLibCalls> 00052 X("simplify-libcalls-halfpowr", "Simplify half_powr library calls"); 00053 00054 // Public interface to the Simplify HalfPowr LibCalls pass. 00055 FunctionPass *llvm::createSimplifyHalfPowrLibCallsPass() { 00056 return new SimplifyHalfPowrLibCalls(); 00057 } 00058 00059 /// InlineHalfPowrs - Inline a sequence of adjacent half_powr calls, rearranging 00060 /// their control flow to better facilitate subsequent optimization. 00061 Instruction * 00062 SimplifyHalfPowrLibCalls::InlineHalfPowrs(const std::vector<Instruction *> &HalfPowrs, 00063 Instruction *InsertPt) { 00064 std::vector<BasicBlock *> Bodies; 00065 BasicBlock *NewBlock = 0; 00066 00067 for (unsigned i = 0, e = HalfPowrs.size(); i != e; ++i) { 00068 CallInst *Call = cast<CallInst>(HalfPowrs[i]); 00069 Function *Callee = Call->getCalledFunction(); 00070 00071 // Minimally sanity-check the CFG of half_powr to ensure that it contains 00072 // the the kind of code we expect. If we're running this pass, we have 00073 // reason to believe it will be what we expect. 00074 Function::iterator I = Callee->begin(); 00075 BasicBlock *Prologue = I++; 00076 if (I == Callee->end()) break; 00077 BasicBlock *SubnormalHandling = I++; 00078 if (I == Callee->end()) break; 00079 BasicBlock *Body = I++; 00080 if (I != Callee->end()) break; 00081 if (SubnormalHandling->getSinglePredecessor() != Prologue) 00082 break; 00083 BranchInst *PBI = dyn_cast<BranchInst>(Prologue->getTerminator()); 00084 if (!PBI || !PBI->isConditional()) 00085 break; 00086 BranchInst *SNBI = dyn_cast<BranchInst>(SubnormalHandling->getTerminator()); 00087 if (!SNBI || SNBI->isConditional()) 00088 break; 00089 if (!isa<ReturnInst>(Body->getTerminator())) 00090 break; 00091 00092 Instruction *NextInst = next(BasicBlock::iterator(Call)); 00093 00094 // Inline the call, taking care of what code ends up where. 00095 NewBlock = SplitBlock(NextInst->getParent(), NextInst, this); 00096 00097 bool B = InlineFunction(Call, 0, TD); 00098 assert(B && "half_powr didn't inline?"); B=B; 00099 00100 BasicBlock *NewBody = NewBlock->getSinglePredecessor(); 00101 assert(NewBody); 00102 Bodies.push_back(NewBody); 00103 } 00104 00105 if (!NewBlock) 00106 return InsertPt; 00107 00108 // Put the code for all the bodies into one block, to facilitate 00109 // subsequent optimization. 00110 (void)SplitEdge(NewBlock->getSinglePredecessor(), NewBlock, this); 00111 for (unsigned i = 0, e = Bodies.size(); i != e; ++i) { 00112 BasicBlock *Body = Bodies[i]; 00113 Instruction *FNP = Body->getFirstNonPHI(); 00114 // Splice the insts from body into NewBlock. 00115 NewBlock->getInstList().splice(NewBlock->begin(), Body->getInstList(), 00116 FNP, Body->getTerminator()); 00117 } 00118 00119 return NewBlock->begin(); 00120 } 00121 00122 /// runOnFunction - Top level algorithm. 00123 /// 00124 bool SimplifyHalfPowrLibCalls::runOnFunction(Function &F) { 00125 TD = getAnalysisIfAvailable<TargetData>(); 00126 00127 bool Changed = false; 00128 std::vector<Instruction *> HalfPowrs; 00129 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { 00130 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 00131 // Look for calls. 00132 bool IsHalfPowr = false; 00133 if (CallInst *CI = dyn_cast<CallInst>(I)) { 00134 // Look for direct calls and calls to non-external functions. 00135 Function *Callee = CI->getCalledFunction(); 00136 if (Callee && Callee->hasExternalLinkage()) { 00137 // Look for calls with well-known names. 00138 if (Callee->getName() == "__half_powrf4") 00139 IsHalfPowr = true; 00140 } 00141 } 00142 if (IsHalfPowr) 00143 HalfPowrs.push_back(I); 00144 // We're looking for sequences of up to three such calls, which we'll 00145 // simplify as a group. 00146 if ((!IsHalfPowr && !HalfPowrs.empty()) || HalfPowrs.size() == 3) { 00147 I = InlineHalfPowrs(HalfPowrs, I); 00148 E = I->getParent()->end(); 00149 HalfPowrs.clear(); 00150 Changed = true; 00151 } 00152 } 00153 assert(HalfPowrs.empty() && "Block had no terminator!"); 00154 } 00155 00156 return Changed; 00157 }