Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- CondPropagate.cpp - Propagate Conditional Expressions -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass propagates information about conditional expressions through the |
| 11 | // program, allowing it to eliminate conditional branches in some cases. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "condprop" |
| 16 | #include "llvm/Transforms/Scalar.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Devang Patel | 115fca8 | 2009-02-05 23:32:52 +0000 | [diff] [blame] | 18 | #include "llvm/IntrinsicInst.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Type.h" |
Chris Lattner | e142fdd | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 22 | #include "llvm/Transforms/Utils/Local.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Devang Patel | 1f20511 | 2009-02-05 19:59:42 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallVector.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | STATISTIC(NumBrThread, "Number of CFG edges threaded through branches"); |
| 29 | STATISTIC(NumSwThread, "Number of CFG edges threaded through switches"); |
| 30 | |
| 31 | namespace { |
| 32 | struct VISIBILITY_HIDDEN CondProp : public FunctionPass { |
| 33 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 34 | CondProp() : FunctionPass(&ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 35 | |
| 36 | virtual bool runOnFunction(Function &F); |
| 37 | |
| 38 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 39 | AU.addRequiredID(BreakCriticalEdgesID); |
| 40 | //AU.addRequired<DominanceFrontier>(); |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | bool MadeChange; |
Devang Patel | 1f20511 | 2009-02-05 19:59:42 +0000 | [diff] [blame] | 45 | SmallVector<BasicBlock *, 4> DeadBlocks; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 46 | void SimplifyBlock(BasicBlock *BB); |
| 47 | void SimplifyPredecessors(BranchInst *BI); |
| 48 | void SimplifyPredecessors(SwitchInst *SI); |
| 49 | void RevectorBlockTo(BasicBlock *FromBB, BasicBlock *ToBB); |
Evan Cheng | 62366a7 | 2009-04-14 23:40:03 +0000 | [diff] [blame] | 50 | bool RevectorBlockTo(BasicBlock *FromBB, Value *Cond, BranchInst *BI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 51 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 52 | } |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 53 | |
| 54 | char CondProp::ID = 0; |
| 55 | static RegisterPass<CondProp> X("condprop", "Conditional Propagation"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 56 | |
| 57 | FunctionPass *llvm::createCondPropagationPass() { |
| 58 | return new CondProp(); |
| 59 | } |
| 60 | |
| 61 | bool CondProp::runOnFunction(Function &F) { |
| 62 | bool EverMadeChange = false; |
Devang Patel | 1f20511 | 2009-02-05 19:59:42 +0000 | [diff] [blame] | 63 | DeadBlocks.clear(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 64 | |
| 65 | // While we are simplifying blocks, keep iterating. |
| 66 | do { |
| 67 | MadeChange = false; |
Devang Patel | 1f20511 | 2009-02-05 19:59:42 +0000 | [diff] [blame] | 68 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E;) |
| 69 | SimplifyBlock(BB++); |
Devang Patel | fb18f93 | 2007-07-26 20:21:42 +0000 | [diff] [blame] | 70 | EverMadeChange = EverMadeChange || MadeChange; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | } while (MadeChange); |
Devang Patel | 1f20511 | 2009-02-05 19:59:42 +0000 | [diff] [blame] | 72 | |
| 73 | if (EverMadeChange) { |
| 74 | while (!DeadBlocks.empty()) { |
| 75 | BasicBlock *BB = DeadBlocks.back(); DeadBlocks.pop_back(); |
| 76 | DeleteDeadBlock(BB); |
| 77 | } |
| 78 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 79 | return EverMadeChange; |
| 80 | } |
| 81 | |
| 82 | void CondProp::SimplifyBlock(BasicBlock *BB) { |
| 83 | if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) { |
| 84 | // If this is a conditional branch based on a phi node that is defined in |
| 85 | // this block, see if we can simplify predecessors of this block. |
| 86 | if (BI->isConditional() && isa<PHINode>(BI->getCondition()) && |
| 87 | cast<PHINode>(BI->getCondition())->getParent() == BB) |
| 88 | SimplifyPredecessors(BI); |
| 89 | |
| 90 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) { |
| 91 | if (isa<PHINode>(SI->getCondition()) && |
| 92 | cast<PHINode>(SI->getCondition())->getParent() == BB) |
| 93 | SimplifyPredecessors(SI); |
| 94 | } |
| 95 | |
| 96 | // If possible, simplify the terminator of this block. |
| 97 | if (ConstantFoldTerminator(BB)) |
| 98 | MadeChange = true; |
| 99 | |
| 100 | // If this block ends with an unconditional branch and the only successor has |
| 101 | // only this block as a predecessor, merge the two blocks together. |
| 102 | if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) |
| 103 | if (BI->isUnconditional() && BI->getSuccessor(0)->getSinglePredecessor() && |
| 104 | BB != BI->getSuccessor(0)) { |
| 105 | BasicBlock *Succ = BI->getSuccessor(0); |
| 106 | |
Chris Lattner | e142fdd | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 107 | // If Succ has any PHI nodes, they are all single-entry PHI's. Eliminate |
| 108 | // them. |
| 109 | FoldSingleEntryPHINodes(Succ); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 110 | |
| 111 | // Remove BI. |
| 112 | BI->eraseFromParent(); |
| 113 | |
| 114 | // Move over all of the instructions. |
| 115 | BB->getInstList().splice(BB->end(), Succ->getInstList()); |
| 116 | |
| 117 | // Any phi nodes that had entries for Succ now have entries from BB. |
| 118 | Succ->replaceAllUsesWith(BB); |
| 119 | |
| 120 | // Succ is now dead, but we cannot delete it without potentially |
| 121 | // invalidating iterators elsewhere. Just insert an unreachable |
Devang Patel | 1f20511 | 2009-02-05 19:59:42 +0000 | [diff] [blame] | 122 | // instruction in it and delete this block later on. |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 123 | new UnreachableInst(BB->getContext(), Succ); |
Devang Patel | 1f20511 | 2009-02-05 19:59:42 +0000 | [diff] [blame] | 124 | DeadBlocks.push_back(Succ); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 125 | MadeChange = true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // SimplifyPredecessors(branches) - We know that BI is a conditional branch |
| 130 | // based on a PHI node defined in this block. If the phi node contains constant |
| 131 | // operands, then the blocks corresponding to those operands can be modified to |
| 132 | // jump directly to the destination instead of going through this block. |
| 133 | void CondProp::SimplifyPredecessors(BranchInst *BI) { |
| 134 | // TODO: We currently only handle the most trival case, where the PHI node has |
Devang Patel | 115fca8 | 2009-02-05 23:32:52 +0000 | [diff] [blame] | 135 | // one use (the branch), and is the only instruction besides the branch and dbg |
| 136 | // intrinsics in the block. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 137 | PHINode *PN = cast<PHINode>(BI->getCondition()); |
lattner | f76eb8f | 2009-01-26 02:18:20 +0000 | [diff] [blame] | 138 | |
| 139 | if (PN->getNumIncomingValues() == 1) { |
| 140 | // Eliminate single-entry PHI nodes. |
| 141 | FoldSingleEntryPHINodes(PN->getParent()); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 146 | if (!PN->hasOneUse()) return; |
| 147 | |
| 148 | BasicBlock *BB = BI->getParent(); |
Devang Patel | 115fca8 | 2009-02-05 23:32:52 +0000 | [diff] [blame] | 149 | if (&*BB->begin() != PN) |
| 150 | return; |
| 151 | BasicBlock::iterator BBI = BB->begin(); |
| 152 | BasicBlock::iterator BBE = BB->end(); |
Bill Wendling | 1252bbf | 2009-03-05 01:08:35 +0000 | [diff] [blame] | 153 | while (BBI != BBE && isa<DbgInfoIntrinsic>(++BBI)) /* empty */; |
Devang Patel | 115fca8 | 2009-02-05 23:32:52 +0000 | [diff] [blame] | 154 | if (&*BBI != BI) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 155 | return; |
| 156 | |
| 157 | // Ok, we have this really simple case, walk the PHI operands, looking for |
| 158 | // constants. Walk from the end to remove operands from the end when |
| 159 | // possible, and to avoid invalidating "i". |
Evan Cheng | 62366a7 | 2009-04-14 23:40:03 +0000 | [diff] [blame] | 160 | for (unsigned i = PN->getNumIncomingValues(); i != 0; --i) { |
| 161 | Value *InVal = PN->getIncomingValue(i-1); |
| 162 | if (!RevectorBlockTo(PN->getIncomingBlock(i-1), InVal, BI)) |
| 163 | continue; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 164 | |
Evan Cheng | 62366a7 | 2009-04-14 23:40:03 +0000 | [diff] [blame] | 165 | ++NumBrThread; |
| 166 | |
| 167 | // If there were two predecessors before this simplification, or if the |
| 168 | // PHI node contained all the same value except for the one we just |
| 169 | // substituted, the PHI node may be deleted. Don't iterate through it the |
| 170 | // last time. |
| 171 | if (BI->getCondition() != PN) return; |
| 172 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // SimplifyPredecessors(switch) - We know that SI is switch based on a PHI node |
| 176 | // defined in this block. If the phi node contains constant operands, then the |
| 177 | // blocks corresponding to those operands can be modified to jump directly to |
| 178 | // the destination instead of going through this block. |
| 179 | void CondProp::SimplifyPredecessors(SwitchInst *SI) { |
| 180 | // TODO: We currently only handle the most trival case, where the PHI node has |
Devang Patel | 115fca8 | 2009-02-05 23:32:52 +0000 | [diff] [blame] | 181 | // one use (the branch), and is the only instruction besides the branch and |
| 182 | // dbg intrinsics in the block. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 183 | PHINode *PN = cast<PHINode>(SI->getCondition()); |
| 184 | if (!PN->hasOneUse()) return; |
| 185 | |
| 186 | BasicBlock *BB = SI->getParent(); |
Devang Patel | 115fca8 | 2009-02-05 23:32:52 +0000 | [diff] [blame] | 187 | if (&*BB->begin() != PN) |
| 188 | return; |
| 189 | BasicBlock::iterator BBI = BB->begin(); |
| 190 | BasicBlock::iterator BBE = BB->end(); |
Bill Wendling | 1252bbf | 2009-03-05 01:08:35 +0000 | [diff] [blame] | 191 | while (BBI != BBE && isa<DbgInfoIntrinsic>(++BBI)) /* empty */; |
Devang Patel | 115fca8 | 2009-02-05 23:32:52 +0000 | [diff] [blame] | 192 | if (&*BBI != SI) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 193 | return; |
| 194 | |
| 195 | bool RemovedPreds = false; |
| 196 | |
| 197 | // Ok, we have this really simple case, walk the PHI operands, looking for |
| 198 | // constants. Walk from the end to remove operands from the end when |
| 199 | // possible, and to avoid invalidating "i". |
| 200 | for (unsigned i = PN->getNumIncomingValues(); i != 0; --i) |
| 201 | if (ConstantInt *CI = dyn_cast<ConstantInt>(PN->getIncomingValue(i-1))) { |
| 202 | // If we have a constant, forward the edge from its current to its |
| 203 | // ultimate destination. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 204 | unsigned DestCase = SI->findCaseValue(CI); |
| 205 | RevectorBlockTo(PN->getIncomingBlock(i-1), |
| 206 | SI->getSuccessor(DestCase)); |
| 207 | ++NumSwThread; |
| 208 | RemovedPreds = true; |
| 209 | |
Chris Lattner | 5f641f7 | 2007-08-02 04:47:05 +0000 | [diff] [blame] | 210 | // If there were two predecessors before this simplification, or if the |
| 211 | // PHI node contained all the same value except for the one we just |
| 212 | // substituted, the PHI node may be deleted. Don't iterate through it the |
| 213 | // last time. |
| 214 | if (SI->getCondition() != PN) return; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | |
| 219 | // RevectorBlockTo - Revector the unconditional branch at the end of FromBB to |
| 220 | // the ToBB block, which is one of the successors of its current successor. |
| 221 | void CondProp::RevectorBlockTo(BasicBlock *FromBB, BasicBlock *ToBB) { |
| 222 | BranchInst *FromBr = cast<BranchInst>(FromBB->getTerminator()); |
| 223 | assert(FromBr->isUnconditional() && "FromBB should end with uncond br!"); |
| 224 | |
| 225 | // Get the old block we are threading through. |
| 226 | BasicBlock *OldSucc = FromBr->getSuccessor(0); |
| 227 | |
| 228 | // OldSucc had multiple successors. If ToBB has multiple predecessors, then |
| 229 | // the edge between them would be critical, which we already took care of. |
| 230 | // If ToBB has single operand PHI node then take care of it here. |
Chris Lattner | e142fdd | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 231 | FoldSingleEntryPHINodes(ToBB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 232 | |
| 233 | // Update PHI nodes in OldSucc to know that FromBB no longer branches to it. |
| 234 | OldSucc->removePredecessor(FromBB); |
| 235 | |
| 236 | // Change FromBr to branch to the new destination. |
| 237 | FromBr->setSuccessor(0, ToBB); |
| 238 | |
| 239 | MadeChange = true; |
| 240 | } |
Evan Cheng | 62366a7 | 2009-04-14 23:40:03 +0000 | [diff] [blame] | 241 | |
| 242 | bool CondProp::RevectorBlockTo(BasicBlock *FromBB, Value *Cond, BranchInst *BI){ |
| 243 | BranchInst *FromBr = cast<BranchInst>(FromBB->getTerminator()); |
| 244 | if (!FromBr->isUnconditional()) |
| 245 | return false; |
| 246 | |
| 247 | // Get the old block we are threading through. |
| 248 | BasicBlock *OldSucc = FromBr->getSuccessor(0); |
| 249 | |
| 250 | // If the condition is a constant, simply revector the unconditional branch at |
| 251 | // the end of FromBB to one of the successors of its current successor. |
| 252 | if (ConstantInt *CB = dyn_cast<ConstantInt>(Cond)) { |
| 253 | BasicBlock *ToBB = BI->getSuccessor(CB->isZero()); |
| 254 | |
| 255 | // OldSucc had multiple successors. If ToBB has multiple predecessors, then |
| 256 | // the edge between them would be critical, which we already took care of. |
| 257 | // If ToBB has single operand PHI node then take care of it here. |
| 258 | FoldSingleEntryPHINodes(ToBB); |
| 259 | |
| 260 | // Update PHI nodes in OldSucc to know that FromBB no longer branches to it. |
| 261 | OldSucc->removePredecessor(FromBB); |
| 262 | |
| 263 | // Change FromBr to branch to the new destination. |
| 264 | FromBr->setSuccessor(0, ToBB); |
| 265 | } else { |
Evan Cheng | 45557a6 | 2009-04-15 00:43:54 +0000 | [diff] [blame] | 266 | BasicBlock *Succ0 = BI->getSuccessor(0); |
| 267 | // Do not perform transform if the new destination has PHI nodes. The |
| 268 | // transform will add new preds to the PHI's. |
| 269 | if (isa<PHINode>(Succ0->begin())) |
| 270 | return false; |
Evan Cheng | 62366a7 | 2009-04-14 23:40:03 +0000 | [diff] [blame] | 271 | |
Evan Cheng | 45557a6 | 2009-04-15 00:43:54 +0000 | [diff] [blame] | 272 | BasicBlock *Succ1 = BI->getSuccessor(1); |
| 273 | if (isa<PHINode>(Succ1->begin())) |
| 274 | return false; |
| 275 | |
| 276 | // Insert the new conditional branch. |
| 277 | BranchInst::Create(Succ0, Succ1, Cond, FromBr); |
| 278 | |
| 279 | FoldSingleEntryPHINodes(Succ0); |
| 280 | FoldSingleEntryPHINodes(Succ1); |
Evan Cheng | 62366a7 | 2009-04-14 23:40:03 +0000 | [diff] [blame] | 281 | |
| 282 | // Update PHI nodes in OldSucc to know that FromBB no longer branches to it. |
| 283 | OldSucc->removePredecessor(FromBB); |
| 284 | |
| 285 | // Delete the old branch. |
| 286 | FromBr->eraseFromParent(); |
| 287 | } |
| 288 | |
| 289 | MadeChange = true; |
| 290 | return true; |
| 291 | } |