LLVM API Documentation
00001 //===-- CondPropagate.cpp - Propagate Conditional Expressions -------------===// 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 pass propagates information about conditional expressions through the 00011 // program, allowing it to eliminate conditional branches in some cases. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "condprop" 00016 #include "llvm/Transforms/Scalar.h" 00017 #include "llvm/Constants.h" 00018 #include "llvm/Function.h" 00019 #include "llvm/Instructions.h" 00020 #include "llvm/Pass.h" 00021 #include "llvm/Type.h" 00022 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 00023 #include "llvm/Transforms/Utils/Local.h" 00024 #include "llvm/ADT/STLExtras.h" 00025 #include "llvm/ADT/Statistic.h" 00026 #include "llvm/Support/Compiler.h" 00027 #include "llvm/Support/Streams.h" 00028 using namespace llvm; 00029 00030 STATISTIC(NumBrThread, "Number of CFG edges threaded through branches"); 00031 STATISTIC(NumSwThread, "Number of CFG edges threaded through switches"); 00032 00033 namespace { 00034 struct VISIBILITY_HIDDEN CondProp : public FunctionPass { 00035 static char ID; // Pass identification, replacement for typeid 00036 CondProp() : FunctionPass(&ID) {} 00037 00038 virtual bool runOnFunction(Function &F); 00039 00040 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00041 AU.addRequiredID(BreakCriticalEdgesID); 00042 //AU.addRequired<DominanceFrontier>(); 00043 } 00044 00045 private: 00046 bool MadeChange; 00047 void SimplifyBlock(BasicBlock *BB); 00048 void SimplifyPredecessors(BranchInst *BI); 00049 void SimplifyPredecessors(SwitchInst *SI); 00050 void RevectorBlockTo(BasicBlock *FromBB, BasicBlock *ToBB); 00051 }; 00052 } 00053 00054 char CondProp::ID = 0; 00055 static RegisterPass<CondProp> X("condprop", "Conditional Propagation"); 00056 00057 FunctionPass *llvm::createCondPropagationPass() { 00058 return new CondProp(); 00059 } 00060 00061 bool CondProp::runOnFunction(Function &F) { 00062 bool EverMadeChange = false; 00063 00064 // While we are simplifying blocks, keep iterating. 00065 do { 00066 MadeChange = false; 00067 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 00068 SimplifyBlock(BB); 00069 EverMadeChange = EverMadeChange || MadeChange; 00070 } while (MadeChange); 00071 return EverMadeChange; 00072 } 00073 00074 void CondProp::SimplifyBlock(BasicBlock *BB) { 00075 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) { 00076 // If this is a conditional branch based on a phi node that is defined in 00077 // this block, see if we can simplify predecessors of this block. 00078 if (BI->isConditional() && isa<PHINode>(BI->getCondition()) && 00079 cast<PHINode>(BI->getCondition())->getParent() == BB) 00080 SimplifyPredecessors(BI); 00081 00082 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) { 00083 if (isa<PHINode>(SI->getCondition()) && 00084 cast<PHINode>(SI->getCondition())->getParent() == BB) 00085 SimplifyPredecessors(SI); 00086 } 00087 00088 // If possible, simplify the terminator of this block. 00089 if (ConstantFoldTerminator(BB)) 00090 MadeChange = true; 00091 00092 // If this block ends with an unconditional branch and the only successor has 00093 // only this block as a predecessor, merge the two blocks together. 00094 if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) 00095 if (BI->isUnconditional() && BI->getSuccessor(0)->getSinglePredecessor() && 00096 BB != BI->getSuccessor(0)) { 00097 BasicBlock *Succ = BI->getSuccessor(0); 00098 00099 // If Succ has any PHI nodes, they are all single-entry PHI's. Eliminate 00100 // them. 00101 FoldSingleEntryPHINodes(Succ); 00102 00103 // Remove BI. 00104 BI->eraseFromParent(); 00105 00106 // Move over all of the instructions. 00107 BB->getInstList().splice(BB->end(), Succ->getInstList()); 00108 00109 // Any phi nodes that had entries for Succ now have entries from BB. 00110 Succ->replaceAllUsesWith(BB); 00111 00112 // Succ is now dead, but we cannot delete it without potentially 00113 // invalidating iterators elsewhere. Just insert an unreachable 00114 // instruction in it. 00115 new UnreachableInst(Succ); 00116 MadeChange = true; 00117 } 00118 } 00119 00120 // SimplifyPredecessors(branches) - We know that BI is a conditional branch 00121 // based on a PHI node defined in this block. If the phi node contains constant 00122 // operands, then the blocks corresponding to those operands can be modified to 00123 // jump directly to the destination instead of going through this block. 00124 void CondProp::SimplifyPredecessors(BranchInst *BI) { 00125 // TODO: We currently only handle the most trival case, where the PHI node has 00126 // one use (the branch), and is the only instruction besides the branch in the 00127 // block. 00128 PHINode *PN = cast<PHINode>(BI->getCondition()); 00129 00130 if (PN->getNumIncomingValues() == 1) { 00131 // Eliminate single-entry PHI nodes. 00132 FoldSingleEntryPHINodes(PN->getParent()); 00133 return; 00134 } 00135 00136 00137 if (!PN->hasOneUse()) return; 00138 00139 BasicBlock *BB = BI->getParent(); 00140 if (&*BB->begin() != PN || &*next(BB->begin()) != BI) 00141 return; 00142 00143 // Ok, we have this really simple case, walk the PHI operands, looking for 00144 // constants. Walk from the end to remove operands from the end when 00145 // possible, and to avoid invalidating "i". 00146 for (unsigned i = PN->getNumIncomingValues(); i != 0; --i) 00147 if (ConstantInt *CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i-1))) { 00148 // If we have a constant, forward the edge from its current to its 00149 // ultimate destination. 00150 RevectorBlockTo(PN->getIncomingBlock(i-1), 00151 BI->getSuccessor(CB->isZero())); 00152 ++NumBrThread; 00153 00154 // If there were two predecessors before this simplification, or if the 00155 // PHI node contained all the same value except for the one we just 00156 // substituted, the PHI node may be deleted. Don't iterate through it the 00157 // last time. 00158 if (BI->getCondition() != PN) return; 00159 } 00160 } 00161 00162 // SimplifyPredecessors(switch) - We know that SI is switch based on a PHI node 00163 // defined in this block. If the phi node contains constant operands, then the 00164 // blocks corresponding to those operands can be modified to jump directly to 00165 // the destination instead of going through this block. 00166 void CondProp::SimplifyPredecessors(SwitchInst *SI) { 00167 // TODO: We currently only handle the most trival case, where the PHI node has 00168 // one use (the branch), and is the only instruction besides the branch in the 00169 // block. 00170 PHINode *PN = cast<PHINode>(SI->getCondition()); 00171 if (!PN->hasOneUse()) return; 00172 00173 BasicBlock *BB = SI->getParent(); 00174 if (&*BB->begin() != PN || &*next(BB->begin()) != SI) 00175 return; 00176 00177 bool RemovedPreds = false; 00178 00179 // Ok, we have this really simple case, walk the PHI operands, looking for 00180 // constants. Walk from the end to remove operands from the end when 00181 // possible, and to avoid invalidating "i". 00182 for (unsigned i = PN->getNumIncomingValues(); i != 0; --i) 00183 if (ConstantInt *CI = dyn_cast<ConstantInt>(PN->getIncomingValue(i-1))) { 00184 // If we have a constant, forward the edge from its current to its 00185 // ultimate destination. 00186 unsigned DestCase = SI->findCaseValue(CI); 00187 RevectorBlockTo(PN->getIncomingBlock(i-1), 00188 SI->getSuccessor(DestCase)); 00189 ++NumSwThread; 00190 RemovedPreds = true; 00191 00192 // If there were two predecessors before this simplification, or if the 00193 // PHI node contained all the same value except for the one we just 00194 // substituted, the PHI node may be deleted. Don't iterate through it the 00195 // last time. 00196 if (SI->getCondition() != PN) return; 00197 } 00198 } 00199 00200 00201 // RevectorBlockTo - Revector the unconditional branch at the end of FromBB to 00202 // the ToBB block, which is one of the successors of its current successor. 00203 void CondProp::RevectorBlockTo(BasicBlock *FromBB, BasicBlock *ToBB) { 00204 BranchInst *FromBr = cast<BranchInst>(FromBB->getTerminator()); 00205 assert(FromBr->isUnconditional() && "FromBB should end with uncond br!"); 00206 00207 // Get the old block we are threading through. 00208 BasicBlock *OldSucc = FromBr->getSuccessor(0); 00209 00210 // OldSucc had multiple successors. If ToBB has multiple predecessors, then 00211 // the edge between them would be critical, which we already took care of. 00212 // If ToBB has single operand PHI node then take care of it here. 00213 FoldSingleEntryPHINodes(ToBB); 00214 00215 // Update PHI nodes in OldSucc to know that FromBB no longer branches to it. 00216 OldSucc->removePredecessor(FromBB); 00217 00218 // Change FromBr to branch to the new destination. 00219 FromBr->setSuccessor(0, ToBB); 00220 00221 MadeChange = true; 00222 }