Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1 | //===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | bb190ac | 2002-10-08 21:36:33 +0000 | [diff] [blame] | 10 | // Peephole optimize the CFG. |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 218a822 | 2004-06-20 01:13:18 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "simplifycfg" |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 723c66d | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/Instructions.h" |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 18 | #include "llvm/Type.h" |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 19 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CFG.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | 93e985f | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | c995123 | 2007-04-02 01:44:59 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | #include <functional> |
Chris Lattner | d52c261 | 2004-02-24 07:23:58 +0000 | [diff] [blame] | 28 | #include <set> |
Chris Lattner | 698f96f | 2004-10-18 04:07:22 +0000 | [diff] [blame] | 29 | #include <map> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 2bdcb56 | 2005-08-03 00:19:45 +0000 | [diff] [blame] | 32 | /// SafeToMergeTerminators - Return true if it is safe to merge these two |
| 33 | /// terminator instructions together. |
| 34 | /// |
| 35 | static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) { |
| 36 | if (SI1 == SI2) return false; // Can't merge with self! |
| 37 | |
| 38 | // It is not safe to merge these two switch instructions if they have a common |
| 39 | // successor, and if that successor has a PHI node, and if *that* PHI node has |
| 40 | // conflicting incoming values from the two switch blocks. |
| 41 | BasicBlock *SI1BB = SI1->getParent(); |
| 42 | BasicBlock *SI2BB = SI2->getParent(); |
Chris Lattner | c995123 | 2007-04-02 01:44:59 +0000 | [diff] [blame] | 43 | SmallPtrSet<BasicBlock*, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB)); |
Chris Lattner | 2bdcb56 | 2005-08-03 00:19:45 +0000 | [diff] [blame] | 44 | |
| 45 | for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I) |
| 46 | if (SI1Succs.count(*I)) |
| 47 | for (BasicBlock::iterator BBI = (*I)->begin(); |
| 48 | isa<PHINode>(BBI); ++BBI) { |
| 49 | PHINode *PN = cast<PHINode>(BBI); |
| 50 | if (PN->getIncomingValueForBlock(SI1BB) != |
| 51 | PN->getIncomingValueForBlock(SI2BB)) |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | /// AddPredecessorToBlock - Update PHI nodes in Succ to indicate that there will |
| 59 | /// now be entries in it from the 'NewPred' block. The values that will be |
| 60 | /// flowing into the PHI nodes will be the same as those coming in from |
| 61 | /// ExistPred, an existing predecessor of Succ. |
| 62 | static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred, |
| 63 | BasicBlock *ExistPred) { |
| 64 | assert(std::find(succ_begin(ExistPred), succ_end(ExistPred), Succ) != |
| 65 | succ_end(ExistPred) && "ExistPred is not a predecessor of Succ!"); |
| 66 | if (!isa<PHINode>(Succ->begin())) return; // Quick exit if nothing to do |
| 67 | |
| 68 | for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { |
| 69 | PHINode *PN = cast<PHINode>(I); |
| 70 | Value *V = PN->getIncomingValueForBlock(ExistPred); |
| 71 | PN->addIncoming(V, NewPred); |
| 72 | } |
| 73 | } |
| 74 | |
Chris Lattner | 3b3efc7 | 2005-08-03 00:29:26 +0000 | [diff] [blame] | 75 | // CanPropagatePredecessorsForPHIs - Return true if we can fold BB, an |
| 76 | // almost-empty BB ending in an unconditional branch to Succ, into succ. |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 77 | // |
| 78 | // Assumption: Succ is the single successor for BB. |
| 79 | // |
Chris Lattner | 3b3efc7 | 2005-08-03 00:29:26 +0000 | [diff] [blame] | 80 | static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 81 | assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!"); |
Chris Lattner | 3abb95d | 2002-09-24 00:09:26 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 83 | // Check to see if one of the predecessors of BB is already a predecessor of |
Chris Lattner | e2ca540 | 2003-03-05 21:01:52 +0000 | [diff] [blame] | 84 | // Succ. If so, we cannot do the transformation if there are any PHI nodes |
| 85 | // with incompatible values coming in from the two edges! |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 86 | // |
Chris Lattner | dc88dbe | 2005-08-03 00:38:27 +0000 | [diff] [blame] | 87 | if (isa<PHINode>(Succ->front())) { |
Chris Lattner | c995123 | 2007-04-02 01:44:59 +0000 | [diff] [blame] | 88 | SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB)); |
Chris Lattner | 8e75ee2 | 2005-12-03 18:25:58 +0000 | [diff] [blame] | 89 | for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ); |
Chris Lattner | dc88dbe | 2005-08-03 00:38:27 +0000 | [diff] [blame] | 90 | PI != PE; ++PI) |
Chris Lattner | c995123 | 2007-04-02 01:44:59 +0000 | [diff] [blame] | 91 | if (BBPreds.count(*PI)) { |
Chris Lattner | dc88dbe | 2005-08-03 00:38:27 +0000 | [diff] [blame] | 92 | // Loop over all of the PHI nodes checking to see if there are |
| 93 | // incompatible values coming in. |
| 94 | for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { |
| 95 | PHINode *PN = cast<PHINode>(I); |
| 96 | // Loop up the entries in the PHI node for BB and for *PI if the |
| 97 | // values coming in are non-equal, we cannot merge these two blocks |
| 98 | // (instead we should insert a conditional move or something, then |
| 99 | // merge the blocks). |
| 100 | if (PN->getIncomingValueForBlock(BB) != |
| 101 | PN->getIncomingValueForBlock(*PI)) |
| 102 | return false; // Values are not equal... |
| 103 | } |
| 104 | } |
| 105 | } |
Chris Lattner | 1aad921 | 2005-08-03 00:59:12 +0000 | [diff] [blame] | 106 | |
| 107 | // Finally, if BB has PHI nodes that are used by things other than the PHIs in |
| 108 | // Succ and Succ has predecessors that are not Succ and not Pred, we cannot |
| 109 | // fold these blocks, as we don't know whether BB dominates Succ or not to |
| 110 | // update the PHI nodes correctly. |
| 111 | if (!isa<PHINode>(BB->begin()) || Succ->getSinglePredecessor()) return true; |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 112 | |
Chris Lattner | c995123 | 2007-04-02 01:44:59 +0000 | [diff] [blame] | 113 | // If the predecessors of Succ are only BB and Succ itself, handle it. |
Chris Lattner | 1aad921 | 2005-08-03 00:59:12 +0000 | [diff] [blame] | 114 | bool IsSafe = true; |
| 115 | for (pred_iterator PI = pred_begin(Succ), E = pred_end(Succ); PI != E; ++PI) |
| 116 | if (*PI != Succ && *PI != BB) { |
| 117 | IsSafe = false; |
| 118 | break; |
| 119 | } |
| 120 | if (IsSafe) return true; |
| 121 | |
Chris Lattner | 8e75ee2 | 2005-12-03 18:25:58 +0000 | [diff] [blame] | 122 | // If the PHI nodes in BB are only used by instructions in Succ, we are ok if |
| 123 | // BB and Succ have no common predecessors. |
Chris Lattner | a0fcc3e | 2006-05-14 18:45:44 +0000 | [diff] [blame] | 124 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) { |
Chris Lattner | 1aad921 | 2005-08-03 00:59:12 +0000 | [diff] [blame] | 125 | PHINode *PN = cast<PHINode>(I); |
| 126 | for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); UI != E; |
| 127 | ++UI) |
Chris Lattner | 8e75ee2 | 2005-12-03 18:25:58 +0000 | [diff] [blame] | 128 | if (cast<Instruction>(*UI)->getParent() != Succ) |
| 129 | return false; |
Chris Lattner | 1aad921 | 2005-08-03 00:59:12 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Chris Lattner | 8e75ee2 | 2005-12-03 18:25:58 +0000 | [diff] [blame] | 132 | // Scan the predecessor sets of BB and Succ, making sure there are no common |
| 133 | // predecessors. Common predecessors would cause us to build a phi node with |
| 134 | // differing incoming values, which is not legal. |
Chris Lattner | c995123 | 2007-04-02 01:44:59 +0000 | [diff] [blame] | 135 | SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB)); |
Chris Lattner | 8e75ee2 | 2005-12-03 18:25:58 +0000 | [diff] [blame] | 136 | for (pred_iterator PI = pred_begin(Succ), E = pred_end(Succ); PI != E; ++PI) |
| 137 | if (BBPreds.count(*PI)) |
| 138 | return false; |
| 139 | |
| 140 | return true; |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Chris Lattner | 7e66348 | 2005-08-03 00:11:16 +0000 | [diff] [blame] | 143 | /// TryToSimplifyUncondBranchFromEmptyBlock - BB contains an unconditional |
| 144 | /// branch to Succ, and contains no instructions other than PHI nodes and the |
| 145 | /// branch. If possible, eliminate BB. |
| 146 | static bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB, |
| 147 | BasicBlock *Succ) { |
| 148 | // If our successor has PHI nodes, then we need to update them to include |
| 149 | // entries for BB's predecessors, not for BB itself. Be careful though, |
| 150 | // if this transformation fails (returns true) then we cannot do this |
| 151 | // transformation! |
| 152 | // |
Chris Lattner | 3b3efc7 | 2005-08-03 00:29:26 +0000 | [diff] [blame] | 153 | if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false; |
Chris Lattner | 7e66348 | 2005-08-03 00:11:16 +0000 | [diff] [blame] | 154 | |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 155 | DOUT << "Killing Trivial BB: \n" << *BB; |
Chris Lattner | 7e66348 | 2005-08-03 00:11:16 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 3b3efc7 | 2005-08-03 00:29:26 +0000 | [diff] [blame] | 157 | if (isa<PHINode>(Succ->begin())) { |
| 158 | // If there is more than one pred of succ, and there are PHI nodes in |
| 159 | // the successor, then we need to add incoming edges for the PHI nodes |
| 160 | // |
| 161 | const std::vector<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB)); |
| 162 | |
| 163 | // Loop over all of the PHI nodes in the successor of BB. |
| 164 | for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) { |
| 165 | PHINode *PN = cast<PHINode>(I); |
| 166 | Value *OldVal = PN->removeIncomingValue(BB, false); |
| 167 | assert(OldVal && "No entry in PHI for Pred BB!"); |
| 168 | |
Chris Lattner | dc88dbe | 2005-08-03 00:38:27 +0000 | [diff] [blame] | 169 | // If this incoming value is one of the PHI nodes in BB, the new entries |
| 170 | // in the PHI node are the entries from the old PHI. |
Chris Lattner | 3b3efc7 | 2005-08-03 00:29:26 +0000 | [diff] [blame] | 171 | if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) { |
| 172 | PHINode *OldValPN = cast<PHINode>(OldVal); |
| 173 | for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i) |
| 174 | PN->addIncoming(OldValPN->getIncomingValue(i), |
| 175 | OldValPN->getIncomingBlock(i)); |
| 176 | } else { |
| 177 | for (std::vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(), |
| 178 | End = BBPreds.end(); PredI != End; ++PredI) { |
| 179 | // Add an incoming value for each of the new incoming values... |
| 180 | PN->addIncoming(OldVal, *PredI); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Chris Lattner | 7e66348 | 2005-08-03 00:11:16 +0000 | [diff] [blame] | 186 | if (isa<PHINode>(&BB->front())) { |
| 187 | std::vector<BasicBlock*> |
| 188 | OldSuccPreds(pred_begin(Succ), pred_end(Succ)); |
| 189 | |
| 190 | // Move all PHI nodes in BB to Succ if they are alive, otherwise |
| 191 | // delete them. |
| 192 | while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) |
Chris Lattner | dc88dbe | 2005-08-03 00:38:27 +0000 | [diff] [blame] | 193 | if (PN->use_empty()) { |
| 194 | // Just remove the dead phi. This happens if Succ's PHIs were the only |
| 195 | // users of the PHI nodes. |
| 196 | PN->eraseFromParent(); |
Chris Lattner | 7e66348 | 2005-08-03 00:11:16 +0000 | [diff] [blame] | 197 | } else { |
| 198 | // The instruction is alive, so this means that Succ must have |
| 199 | // *ONLY* had BB as a predecessor, and the PHI node is still valid |
| 200 | // now. Simply move it into Succ, because we know that BB |
| 201 | // strictly dominated Succ. |
Chris Lattner | d423b8b | 2005-08-03 00:23:42 +0000 | [diff] [blame] | 202 | Succ->getInstList().splice(Succ->begin(), |
| 203 | BB->getInstList(), BB->begin()); |
Chris Lattner | 7e66348 | 2005-08-03 00:11:16 +0000 | [diff] [blame] | 204 | |
| 205 | // We need to add new entries for the PHI node to account for |
| 206 | // predecessors of Succ that the PHI node does not take into |
| 207 | // account. At this point, since we know that BB dominated succ, |
| 208 | // this means that we should any newly added incoming edges should |
| 209 | // use the PHI node as the value for these edges, because they are |
| 210 | // loop back edges. |
| 211 | for (unsigned i = 0, e = OldSuccPreds.size(); i != e; ++i) |
| 212 | if (OldSuccPreds[i] != BB) |
| 213 | PN->addIncoming(PN, OldSuccPreds[i]); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Everything that jumped to BB now goes to Succ. |
Chris Lattner | 7e66348 | 2005-08-03 00:11:16 +0000 | [diff] [blame] | 218 | BB->replaceAllUsesWith(Succ); |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 219 | if (!Succ->hasName()) Succ->takeName(BB); |
Chris Lattner | 7e66348 | 2005-08-03 00:11:16 +0000 | [diff] [blame] | 220 | BB->eraseFromParent(); // Delete the old basic block. |
Chris Lattner | 7e66348 | 2005-08-03 00:11:16 +0000 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
Chris Lattner | 723c66d | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 224 | /// GetIfCondition - Given a basic block (BB) with two predecessors (and |
| 225 | /// presumably PHI nodes in it), check to see if the merge at this block is due |
| 226 | /// to an "if condition". If so, return the boolean condition that determines |
| 227 | /// which entry into BB will be taken. Also, return by references the block |
| 228 | /// that will be entered from if the condition is true, and the block that will |
| 229 | /// be entered if the condition is false. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 230 | /// |
Chris Lattner | 723c66d | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 231 | /// |
| 232 | static Value *GetIfCondition(BasicBlock *BB, |
| 233 | BasicBlock *&IfTrue, BasicBlock *&IfFalse) { |
| 234 | assert(std::distance(pred_begin(BB), pred_end(BB)) == 2 && |
| 235 | "Function can only handle blocks with 2 predecessors!"); |
| 236 | BasicBlock *Pred1 = *pred_begin(BB); |
| 237 | BasicBlock *Pred2 = *++pred_begin(BB); |
| 238 | |
| 239 | // We can only handle branches. Other control flow will be lowered to |
| 240 | // branches if possible anyway. |
| 241 | if (!isa<BranchInst>(Pred1->getTerminator()) || |
| 242 | !isa<BranchInst>(Pred2->getTerminator())) |
| 243 | return 0; |
| 244 | BranchInst *Pred1Br = cast<BranchInst>(Pred1->getTerminator()); |
| 245 | BranchInst *Pred2Br = cast<BranchInst>(Pred2->getTerminator()); |
| 246 | |
| 247 | // Eliminate code duplication by ensuring that Pred1Br is conditional if |
| 248 | // either are. |
| 249 | if (Pred2Br->isConditional()) { |
| 250 | // If both branches are conditional, we don't have an "if statement". In |
| 251 | // reality, we could transform this case, but since the condition will be |
| 252 | // required anyway, we stand no chance of eliminating it, so the xform is |
| 253 | // probably not profitable. |
| 254 | if (Pred1Br->isConditional()) |
| 255 | return 0; |
| 256 | |
| 257 | std::swap(Pred1, Pred2); |
| 258 | std::swap(Pred1Br, Pred2Br); |
| 259 | } |
| 260 | |
| 261 | if (Pred1Br->isConditional()) { |
| 262 | // If we found a conditional branch predecessor, make sure that it branches |
| 263 | // to BB and Pred2Br. If it doesn't, this isn't an "if statement". |
| 264 | if (Pred1Br->getSuccessor(0) == BB && |
| 265 | Pred1Br->getSuccessor(1) == Pred2) { |
| 266 | IfTrue = Pred1; |
| 267 | IfFalse = Pred2; |
| 268 | } else if (Pred1Br->getSuccessor(0) == Pred2 && |
| 269 | Pred1Br->getSuccessor(1) == BB) { |
| 270 | IfTrue = Pred2; |
| 271 | IfFalse = Pred1; |
| 272 | } else { |
| 273 | // We know that one arm of the conditional goes to BB, so the other must |
| 274 | // go somewhere unrelated, and this must not be an "if statement". |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | // The only thing we have to watch out for here is to make sure that Pred2 |
| 279 | // doesn't have incoming edges from other blocks. If it does, the condition |
| 280 | // doesn't dominate BB. |
| 281 | if (++pred_begin(Pred2) != pred_end(Pred2)) |
| 282 | return 0; |
| 283 | |
| 284 | return Pred1Br->getCondition(); |
| 285 | } |
| 286 | |
| 287 | // Ok, if we got here, both predecessors end with an unconditional branch to |
| 288 | // BB. Don't panic! If both blocks only have a single (identical) |
| 289 | // predecessor, and THAT is a conditional branch, then we're all ok! |
| 290 | if (pred_begin(Pred1) == pred_end(Pred1) || |
| 291 | ++pred_begin(Pred1) != pred_end(Pred1) || |
| 292 | pred_begin(Pred2) == pred_end(Pred2) || |
| 293 | ++pred_begin(Pred2) != pred_end(Pred2) || |
| 294 | *pred_begin(Pred1) != *pred_begin(Pred2)) |
| 295 | return 0; |
| 296 | |
| 297 | // Otherwise, if this is a conditional branch, then we can use it! |
| 298 | BasicBlock *CommonPred = *pred_begin(Pred1); |
| 299 | if (BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator())) { |
| 300 | assert(BI->isConditional() && "Two successors but not conditional?"); |
| 301 | if (BI->getSuccessor(0) == Pred1) { |
| 302 | IfTrue = Pred1; |
| 303 | IfFalse = Pred2; |
| 304 | } else { |
| 305 | IfTrue = Pred2; |
| 306 | IfFalse = Pred1; |
| 307 | } |
| 308 | return BI->getCondition(); |
| 309 | } |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | |
| 314 | // If we have a merge point of an "if condition" as accepted above, return true |
| 315 | // if the specified value dominates the block. We don't handle the true |
| 316 | // generality of domination here, just a special case which works well enough |
| 317 | // for us. |
Chris Lattner | 9c07866 | 2004-10-14 05:13:36 +0000 | [diff] [blame] | 318 | // |
| 319 | // If AggressiveInsts is non-null, and if V does not dominate BB, we check to |
| 320 | // see if V (which must be an instruction) is cheap to compute and is |
| 321 | // non-trapping. If both are true, the instruction is inserted into the set and |
| 322 | // true is returned. |
| 323 | static bool DominatesMergePoint(Value *V, BasicBlock *BB, |
| 324 | std::set<Instruction*> *AggressiveInsts) { |
Chris Lattner | 570751c | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 325 | Instruction *I = dyn_cast<Instruction>(V); |
Chris Lattner | b74b181 | 2006-10-20 00:42:07 +0000 | [diff] [blame] | 326 | if (!I) { |
| 327 | // Non-instructions all dominate instructions, but not all constantexprs |
| 328 | // can be executed unconditionally. |
| 329 | if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) |
| 330 | if (C->canTrap()) |
| 331 | return false; |
| 332 | return true; |
| 333 | } |
Chris Lattner | 570751c | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 334 | BasicBlock *PBB = I->getParent(); |
Chris Lattner | 723c66d | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 335 | |
Chris Lattner | da895d6 | 2005-02-27 06:18:25 +0000 | [diff] [blame] | 336 | // We don't want to allow weird loops that might have the "if condition" in |
Chris Lattner | 570751c | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 337 | // the bottom of this block. |
| 338 | if (PBB == BB) return false; |
Chris Lattner | 723c66d | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 570751c | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 340 | // If this instruction is defined in a block that contains an unconditional |
| 341 | // branch to BB, then it must be in the 'conditional' part of the "if |
| 342 | // statement". |
| 343 | if (BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator())) |
| 344 | if (BI->isUnconditional() && BI->getSuccessor(0) == BB) { |
Chris Lattner | 9c07866 | 2004-10-14 05:13:36 +0000 | [diff] [blame] | 345 | if (!AggressiveInsts) return false; |
Chris Lattner | 570751c | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 346 | // Okay, it looks like the instruction IS in the "condition". Check to |
| 347 | // see if its a cheap instruction to unconditionally compute, and if it |
| 348 | // only uses stuff defined outside of the condition. If so, hoist it out. |
| 349 | switch (I->getOpcode()) { |
| 350 | default: return false; // Cannot hoist this out safely. |
| 351 | case Instruction::Load: |
| 352 | // We can hoist loads that are non-volatile and obviously cannot trap. |
| 353 | if (cast<LoadInst>(I)->isVolatile()) |
| 354 | return false; |
| 355 | if (!isa<AllocaInst>(I->getOperand(0)) && |
Reid Spencer | 460f16c | 2004-07-18 00:32:14 +0000 | [diff] [blame] | 356 | !isa<Constant>(I->getOperand(0))) |
Chris Lattner | 570751c | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 357 | return false; |
| 358 | |
| 359 | // Finally, we have to check to make sure there are no instructions |
| 360 | // before the load in its basic block, as we are going to hoist the loop |
| 361 | // out to its predecessor. |
| 362 | if (PBB->begin() != BasicBlock::iterator(I)) |
| 363 | return false; |
| 364 | break; |
| 365 | case Instruction::Add: |
| 366 | case Instruction::Sub: |
| 367 | case Instruction::And: |
| 368 | case Instruction::Or: |
| 369 | case Instruction::Xor: |
| 370 | case Instruction::Shl: |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 371 | case Instruction::LShr: |
| 372 | case Instruction::AShr: |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 373 | case Instruction::ICmp: |
| 374 | case Instruction::FCmp: |
Chris Lattner | 570751c | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 375 | break; // These are all cheap and non-trapping instructions. |
| 376 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 570751c | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 378 | // Okay, we can only really hoist these out if their operands are not |
| 379 | // defined in the conditional region. |
| 380 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
Chris Lattner | 9c07866 | 2004-10-14 05:13:36 +0000 | [diff] [blame] | 381 | if (!DominatesMergePoint(I->getOperand(i), BB, 0)) |
Chris Lattner | 570751c | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 382 | return false; |
Chris Lattner | 9c07866 | 2004-10-14 05:13:36 +0000 | [diff] [blame] | 383 | // Okay, it's safe to do this! Remember this instruction. |
| 384 | AggressiveInsts->insert(I); |
Chris Lattner | 570751c | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Chris Lattner | 723c66d | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 387 | return true; |
| 388 | } |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 389 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 390 | // GatherConstantSetEQs - Given a potentially 'or'd together collection of |
| 391 | // icmp_eq instructions that compare a value against a constant, return the |
| 392 | // value being compared, and stick the constant into the Values vector. |
Chris Lattner | 1654cff | 2004-06-19 07:02:14 +0000 | [diff] [blame] | 393 | static Value *GatherConstantSetEQs(Value *V, std::vector<ConstantInt*> &Values){ |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 394 | if (Instruction *Inst = dyn_cast<Instruction>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 395 | if (Inst->getOpcode() == Instruction::ICmp && |
| 396 | cast<ICmpInst>(Inst)->getPredicate() == ICmpInst::ICMP_EQ) { |
Chris Lattner | 1654cff | 2004-06-19 07:02:14 +0000 | [diff] [blame] | 397 | if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(1))) { |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 398 | Values.push_back(C); |
| 399 | return Inst->getOperand(0); |
Chris Lattner | 1654cff | 2004-06-19 07:02:14 +0000 | [diff] [blame] | 400 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(0))) { |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 401 | Values.push_back(C); |
| 402 | return Inst->getOperand(1); |
| 403 | } |
| 404 | } else if (Inst->getOpcode() == Instruction::Or) { |
| 405 | if (Value *LHS = GatherConstantSetEQs(Inst->getOperand(0), Values)) |
| 406 | if (Value *RHS = GatherConstantSetEQs(Inst->getOperand(1), Values)) |
| 407 | if (LHS == RHS) |
| 408 | return LHS; |
| 409 | } |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | // GatherConstantSetNEs - Given a potentially 'and'd together collection of |
| 414 | // setne instructions that compare a value against a constant, return the value |
| 415 | // being compared, and stick the constant into the Values vector. |
Chris Lattner | 1654cff | 2004-06-19 07:02:14 +0000 | [diff] [blame] | 416 | static Value *GatherConstantSetNEs(Value *V, std::vector<ConstantInt*> &Values){ |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 417 | if (Instruction *Inst = dyn_cast<Instruction>(V)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 418 | if (Inst->getOpcode() == Instruction::ICmp && |
| 419 | cast<ICmpInst>(Inst)->getPredicate() == ICmpInst::ICMP_NE) { |
Chris Lattner | 1654cff | 2004-06-19 07:02:14 +0000 | [diff] [blame] | 420 | if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(1))) { |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 421 | Values.push_back(C); |
| 422 | return Inst->getOperand(0); |
Chris Lattner | 1654cff | 2004-06-19 07:02:14 +0000 | [diff] [blame] | 423 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(0))) { |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 424 | Values.push_back(C); |
| 425 | return Inst->getOperand(1); |
| 426 | } |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 427 | } else if (Inst->getOpcode() == Instruction::And) { |
| 428 | if (Value *LHS = GatherConstantSetNEs(Inst->getOperand(0), Values)) |
| 429 | if (Value *RHS = GatherConstantSetNEs(Inst->getOperand(1), Values)) |
| 430 | if (LHS == RHS) |
| 431 | return LHS; |
| 432 | } |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | |
| 438 | /// GatherValueComparisons - If the specified Cond is an 'and' or 'or' of a |
| 439 | /// bunch of comparisons of one value against constants, return the value and |
| 440 | /// the constants being compared. |
| 441 | static bool GatherValueComparisons(Instruction *Cond, Value *&CompVal, |
Chris Lattner | 1654cff | 2004-06-19 07:02:14 +0000 | [diff] [blame] | 442 | std::vector<ConstantInt*> &Values) { |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 443 | if (Cond->getOpcode() == Instruction::Or) { |
| 444 | CompVal = GatherConstantSetEQs(Cond, Values); |
| 445 | |
| 446 | // Return true to indicate that the condition is true if the CompVal is |
| 447 | // equal to one of the constants. |
| 448 | return true; |
| 449 | } else if (Cond->getOpcode() == Instruction::And) { |
| 450 | CompVal = GatherConstantSetNEs(Cond, Values); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 451 | |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 452 | // Return false to indicate that the condition is false if the CompVal is |
| 453 | // equal to one of the constants. |
| 454 | return false; |
| 455 | } |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | /// ErasePossiblyDeadInstructionTree - If the specified instruction is dead and |
| 460 | /// has no side effects, nuke it. If it uses any instructions that become dead |
| 461 | /// because the instruction is now gone, nuke them too. |
| 462 | static void ErasePossiblyDeadInstructionTree(Instruction *I) { |
Chris Lattner | 8cfe633 | 2006-08-03 21:40:24 +0000 | [diff] [blame] | 463 | if (!isInstructionTriviallyDead(I)) return; |
| 464 | |
| 465 | std::vector<Instruction*> InstrsToInspect; |
| 466 | InstrsToInspect.push_back(I); |
| 467 | |
| 468 | while (!InstrsToInspect.empty()) { |
| 469 | I = InstrsToInspect.back(); |
| 470 | InstrsToInspect.pop_back(); |
| 471 | |
| 472 | if (!isInstructionTriviallyDead(I)) continue; |
| 473 | |
| 474 | // If I is in the work list multiple times, remove previous instances. |
| 475 | for (unsigned i = 0, e = InstrsToInspect.size(); i != e; ++i) |
| 476 | if (InstrsToInspect[i] == I) { |
| 477 | InstrsToInspect.erase(InstrsToInspect.begin()+i); |
| 478 | --i, --e; |
| 479 | } |
| 480 | |
| 481 | // Add operands of dead instruction to worklist. |
| 482 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 483 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(i))) |
| 484 | InstrsToInspect.push_back(OpI); |
| 485 | |
| 486 | // Remove dead instruction. |
| 487 | I->eraseFromParent(); |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 491 | // isValueEqualityComparison - Return true if the specified terminator checks to |
| 492 | // see if a value is equal to constant integer value. |
| 493 | static Value *isValueEqualityComparison(TerminatorInst *TI) { |
Chris Lattner | 4bebf08 | 2004-03-16 19:45:22 +0000 | [diff] [blame] | 494 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 495 | // Do not permit merging of large switch instructions into their |
| 496 | // predecessors unless there is only one predecessor. |
| 497 | if (SI->getNumSuccessors() * std::distance(pred_begin(SI->getParent()), |
| 498 | pred_end(SI->getParent())) > 128) |
| 499 | return 0; |
| 500 | |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 501 | return SI->getCondition(); |
Chris Lattner | 4bebf08 | 2004-03-16 19:45:22 +0000 | [diff] [blame] | 502 | } |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 503 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) |
| 504 | if (BI->isConditional() && BI->getCondition()->hasOneUse()) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 505 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) |
| 506 | if ((ICI->getPredicate() == ICmpInst::ICMP_EQ || |
| 507 | ICI->getPredicate() == ICmpInst::ICMP_NE) && |
| 508 | isa<ConstantInt>(ICI->getOperand(1))) |
| 509 | return ICI->getOperand(0); |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | // Given a value comparison instruction, decode all of the 'cases' that it |
| 514 | // represents and return the 'default' block. |
| 515 | static BasicBlock * |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 516 | GetValueEqualityComparisonCases(TerminatorInst *TI, |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 517 | std::vector<std::pair<ConstantInt*, |
| 518 | BasicBlock*> > &Cases) { |
| 519 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 520 | Cases.reserve(SI->getNumCases()); |
| 521 | for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i) |
Chris Lattner | be54dcc | 2005-02-26 18:33:28 +0000 | [diff] [blame] | 522 | Cases.push_back(std::make_pair(SI->getCaseValue(i), SI->getSuccessor(i))); |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 523 | return SI->getDefaultDest(); |
| 524 | } |
| 525 | |
| 526 | BranchInst *BI = cast<BranchInst>(TI); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 527 | ICmpInst *ICI = cast<ICmpInst>(BI->getCondition()); |
| 528 | Cases.push_back(std::make_pair(cast<ConstantInt>(ICI->getOperand(1)), |
| 529 | BI->getSuccessor(ICI->getPredicate() == |
| 530 | ICmpInst::ICMP_NE))); |
| 531 | return BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_EQ); |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | |
Dan Gohman | fa73ea2 | 2007-05-24 14:36:04 +0000 | [diff] [blame^] | 535 | // EliminateBlockCases - Given a vector of bb/value pairs, remove any entries |
Chris Lattner | 623369a | 2005-02-24 06:17:52 +0000 | [diff] [blame] | 536 | // in the list that match the specified block. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 537 | static void EliminateBlockCases(BasicBlock *BB, |
Chris Lattner | 623369a | 2005-02-24 06:17:52 +0000 | [diff] [blame] | 538 | std::vector<std::pair<ConstantInt*, BasicBlock*> > &Cases) { |
| 539 | for (unsigned i = 0, e = Cases.size(); i != e; ++i) |
| 540 | if (Cases[i].second == BB) { |
| 541 | Cases.erase(Cases.begin()+i); |
| 542 | --i; --e; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | // ValuesOverlap - Return true if there are any keys in C1 that exist in C2 as |
| 547 | // well. |
| 548 | static bool |
| 549 | ValuesOverlap(std::vector<std::pair<ConstantInt*, BasicBlock*> > &C1, |
| 550 | std::vector<std::pair<ConstantInt*, BasicBlock*> > &C2) { |
| 551 | std::vector<std::pair<ConstantInt*, BasicBlock*> > *V1 = &C1, *V2 = &C2; |
| 552 | |
| 553 | // Make V1 be smaller than V2. |
| 554 | if (V1->size() > V2->size()) |
| 555 | std::swap(V1, V2); |
| 556 | |
| 557 | if (V1->size() == 0) return false; |
| 558 | if (V1->size() == 1) { |
| 559 | // Just scan V2. |
| 560 | ConstantInt *TheVal = (*V1)[0].first; |
| 561 | for (unsigned i = 0, e = V2->size(); i != e; ++i) |
| 562 | if (TheVal == (*V2)[i].first) |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | // Otherwise, just sort both lists and compare element by element. |
| 567 | std::sort(V1->begin(), V1->end()); |
| 568 | std::sort(V2->begin(), V2->end()); |
| 569 | unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size(); |
| 570 | while (i1 != e1 && i2 != e2) { |
| 571 | if ((*V1)[i1].first == (*V2)[i2].first) |
| 572 | return true; |
| 573 | if ((*V1)[i1].first < (*V2)[i2].first) |
| 574 | ++i1; |
| 575 | else |
| 576 | ++i2; |
| 577 | } |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | // SimplifyEqualityComparisonWithOnlyPredecessor - If TI is known to be a |
| 582 | // terminator instruction and its block is known to only have a single |
| 583 | // predecessor block, check to see if that predecessor is also a value |
| 584 | // comparison with the same value, and if that comparison determines the outcome |
| 585 | // of this comparison. If so, simplify TI. This does a very limited form of |
| 586 | // jump threading. |
| 587 | static bool SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI, |
| 588 | BasicBlock *Pred) { |
| 589 | Value *PredVal = isValueEqualityComparison(Pred->getTerminator()); |
| 590 | if (!PredVal) return false; // Not a value comparison in predecessor. |
| 591 | |
| 592 | Value *ThisVal = isValueEqualityComparison(TI); |
| 593 | assert(ThisVal && "This isn't a value comparison!!"); |
| 594 | if (ThisVal != PredVal) return false; // Different predicates. |
| 595 | |
| 596 | // Find out information about when control will move from Pred to TI's block. |
| 597 | std::vector<std::pair<ConstantInt*, BasicBlock*> > PredCases; |
| 598 | BasicBlock *PredDef = GetValueEqualityComparisonCases(Pred->getTerminator(), |
| 599 | PredCases); |
| 600 | EliminateBlockCases(PredDef, PredCases); // Remove default from cases. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 601 | |
Chris Lattner | 623369a | 2005-02-24 06:17:52 +0000 | [diff] [blame] | 602 | // Find information about how control leaves this block. |
| 603 | std::vector<std::pair<ConstantInt*, BasicBlock*> > ThisCases; |
| 604 | BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases); |
| 605 | EliminateBlockCases(ThisDef, ThisCases); // Remove default from cases. |
| 606 | |
| 607 | // If TI's block is the default block from Pred's comparison, potentially |
| 608 | // simplify TI based on this knowledge. |
| 609 | if (PredDef == TI->getParent()) { |
| 610 | // If we are here, we know that the value is none of those cases listed in |
| 611 | // PredCases. If there are any cases in ThisCases that are in PredCases, we |
| 612 | // can simplify TI. |
| 613 | if (ValuesOverlap(PredCases, ThisCases)) { |
| 614 | if (BranchInst *BTI = dyn_cast<BranchInst>(TI)) { |
| 615 | // Okay, one of the successors of this condbr is dead. Convert it to a |
| 616 | // uncond br. |
| 617 | assert(ThisCases.size() == 1 && "Branch can only have one case!"); |
| 618 | Value *Cond = BTI->getCondition(); |
| 619 | // Insert the new branch. |
| 620 | Instruction *NI = new BranchInst(ThisDef, TI); |
| 621 | |
| 622 | // Remove PHI node entries for the dead edge. |
| 623 | ThisCases[0].second->removePredecessor(TI->getParent()); |
| 624 | |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 625 | DOUT << "Threading pred instr: " << *Pred->getTerminator() |
| 626 | << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n"; |
Chris Lattner | 623369a | 2005-02-24 06:17:52 +0000 | [diff] [blame] | 627 | |
| 628 | TI->eraseFromParent(); // Nuke the old one. |
| 629 | // If condition is now dead, nuke it. |
| 630 | if (Instruction *CondI = dyn_cast<Instruction>(Cond)) |
| 631 | ErasePossiblyDeadInstructionTree(CondI); |
| 632 | return true; |
| 633 | |
| 634 | } else { |
| 635 | SwitchInst *SI = cast<SwitchInst>(TI); |
| 636 | // Okay, TI has cases that are statically dead, prune them away. |
Chris Lattner | c995123 | 2007-04-02 01:44:59 +0000 | [diff] [blame] | 637 | SmallPtrSet<Constant*, 16> DeadCases; |
Chris Lattner | 623369a | 2005-02-24 06:17:52 +0000 | [diff] [blame] | 638 | for (unsigned i = 0, e = PredCases.size(); i != e; ++i) |
| 639 | DeadCases.insert(PredCases[i].first); |
| 640 | |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 641 | DOUT << "Threading pred instr: " << *Pred->getTerminator() |
| 642 | << "Through successor TI: " << *TI; |
Chris Lattner | 623369a | 2005-02-24 06:17:52 +0000 | [diff] [blame] | 643 | |
| 644 | for (unsigned i = SI->getNumCases()-1; i != 0; --i) |
| 645 | if (DeadCases.count(SI->getCaseValue(i))) { |
| 646 | SI->getSuccessor(i)->removePredecessor(TI->getParent()); |
| 647 | SI->removeCase(i); |
| 648 | } |
| 649 | |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 650 | DOUT << "Leaving: " << *TI << "\n"; |
Chris Lattner | 623369a | 2005-02-24 06:17:52 +0000 | [diff] [blame] | 651 | return true; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | } else { |
| 656 | // Otherwise, TI's block must correspond to some matched value. Find out |
| 657 | // which value (or set of values) this is. |
| 658 | ConstantInt *TIV = 0; |
| 659 | BasicBlock *TIBB = TI->getParent(); |
| 660 | for (unsigned i = 0, e = PredCases.size(); i != e; ++i) |
| 661 | if (PredCases[i].second == TIBB) |
| 662 | if (TIV == 0) |
| 663 | TIV = PredCases[i].first; |
| 664 | else |
| 665 | return false; // Cannot handle multiple values coming to this block. |
| 666 | assert(TIV && "No edge from pred to succ?"); |
| 667 | |
| 668 | // Okay, we found the one constant that our value can be if we get into TI's |
| 669 | // BB. Find out which successor will unconditionally be branched to. |
| 670 | BasicBlock *TheRealDest = 0; |
| 671 | for (unsigned i = 0, e = ThisCases.size(); i != e; ++i) |
| 672 | if (ThisCases[i].first == TIV) { |
| 673 | TheRealDest = ThisCases[i].second; |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | // If not handled by any explicit cases, it is handled by the default case. |
| 678 | if (TheRealDest == 0) TheRealDest = ThisDef; |
| 679 | |
| 680 | // Remove PHI node entries for dead edges. |
| 681 | BasicBlock *CheckEdge = TheRealDest; |
| 682 | for (succ_iterator SI = succ_begin(TIBB), e = succ_end(TIBB); SI != e; ++SI) |
| 683 | if (*SI != CheckEdge) |
| 684 | (*SI)->removePredecessor(TIBB); |
| 685 | else |
| 686 | CheckEdge = 0; |
| 687 | |
| 688 | // Insert the new branch. |
| 689 | Instruction *NI = new BranchInst(TheRealDest, TI); |
| 690 | |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 691 | DOUT << "Threading pred instr: " << *Pred->getTerminator() |
| 692 | << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n"; |
Chris Lattner | 623369a | 2005-02-24 06:17:52 +0000 | [diff] [blame] | 693 | Instruction *Cond = 0; |
| 694 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) |
| 695 | Cond = dyn_cast<Instruction>(BI->getCondition()); |
| 696 | TI->eraseFromParent(); // Nuke the old one. |
| 697 | |
| 698 | if (Cond) ErasePossiblyDeadInstructionTree(Cond); |
| 699 | return true; |
| 700 | } |
| 701 | return false; |
| 702 | } |
| 703 | |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 704 | // FoldValueComparisonIntoPredecessors - The specified terminator is a value |
| 705 | // equality comparison instruction (either a switch or a branch on "X == c"). |
| 706 | // See if any of the predecessors of the terminator block are value comparisons |
| 707 | // on the same value. If so, and if safe to do so, fold them together. |
| 708 | static bool FoldValueComparisonIntoPredecessors(TerminatorInst *TI) { |
| 709 | BasicBlock *BB = TI->getParent(); |
| 710 | Value *CV = isValueEqualityComparison(TI); // CondVal |
| 711 | assert(CV && "Not a comparison?"); |
| 712 | bool Changed = false; |
| 713 | |
| 714 | std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB)); |
| 715 | while (!Preds.empty()) { |
| 716 | BasicBlock *Pred = Preds.back(); |
| 717 | Preds.pop_back(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 718 | |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 719 | // See if the predecessor is a comparison with the same value. |
| 720 | TerminatorInst *PTI = Pred->getTerminator(); |
| 721 | Value *PCV = isValueEqualityComparison(PTI); // PredCondVal |
| 722 | |
| 723 | if (PCV == CV && SafeToMergeTerminators(TI, PTI)) { |
| 724 | // Figure out which 'cases' to copy from SI to PSI. |
| 725 | std::vector<std::pair<ConstantInt*, BasicBlock*> > BBCases; |
| 726 | BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases); |
| 727 | |
| 728 | std::vector<std::pair<ConstantInt*, BasicBlock*> > PredCases; |
| 729 | BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases); |
| 730 | |
| 731 | // Based on whether the default edge from PTI goes to BB or not, fill in |
| 732 | // PredCases and PredDefault with the new switch cases we would like to |
| 733 | // build. |
| 734 | std::vector<BasicBlock*> NewSuccessors; |
| 735 | |
| 736 | if (PredDefault == BB) { |
| 737 | // If this is the default destination from PTI, only the edges in TI |
| 738 | // that don't occur in PTI, or that branch to BB will be activated. |
| 739 | std::set<ConstantInt*> PTIHandled; |
| 740 | for (unsigned i = 0, e = PredCases.size(); i != e; ++i) |
| 741 | if (PredCases[i].second != BB) |
| 742 | PTIHandled.insert(PredCases[i].first); |
| 743 | else { |
| 744 | // The default destination is BB, we don't need explicit targets. |
| 745 | std::swap(PredCases[i], PredCases.back()); |
| 746 | PredCases.pop_back(); |
| 747 | --i; --e; |
| 748 | } |
| 749 | |
| 750 | // Reconstruct the new switch statement we will be building. |
| 751 | if (PredDefault != BBDefault) { |
| 752 | PredDefault->removePredecessor(Pred); |
| 753 | PredDefault = BBDefault; |
| 754 | NewSuccessors.push_back(BBDefault); |
| 755 | } |
| 756 | for (unsigned i = 0, e = BBCases.size(); i != e; ++i) |
| 757 | if (!PTIHandled.count(BBCases[i].first) && |
| 758 | BBCases[i].second != BBDefault) { |
| 759 | PredCases.push_back(BBCases[i]); |
| 760 | NewSuccessors.push_back(BBCases[i].second); |
| 761 | } |
| 762 | |
| 763 | } else { |
| 764 | // If this is not the default destination from PSI, only the edges |
| 765 | // in SI that occur in PSI with a destination of BB will be |
| 766 | // activated. |
| 767 | std::set<ConstantInt*> PTIHandled; |
| 768 | for (unsigned i = 0, e = PredCases.size(); i != e; ++i) |
| 769 | if (PredCases[i].second == BB) { |
| 770 | PTIHandled.insert(PredCases[i].first); |
| 771 | std::swap(PredCases[i], PredCases.back()); |
| 772 | PredCases.pop_back(); |
| 773 | --i; --e; |
| 774 | } |
| 775 | |
| 776 | // Okay, now we know which constants were sent to BB from the |
| 777 | // predecessor. Figure out where they will all go now. |
| 778 | for (unsigned i = 0, e = BBCases.size(); i != e; ++i) |
| 779 | if (PTIHandled.count(BBCases[i].first)) { |
| 780 | // If this is one we are capable of getting... |
| 781 | PredCases.push_back(BBCases[i]); |
| 782 | NewSuccessors.push_back(BBCases[i].second); |
| 783 | PTIHandled.erase(BBCases[i].first);// This constant is taken care of |
| 784 | } |
| 785 | |
| 786 | // If there are any constants vectored to BB that TI doesn't handle, |
| 787 | // they must go to the default destination of TI. |
| 788 | for (std::set<ConstantInt*>::iterator I = PTIHandled.begin(), |
| 789 | E = PTIHandled.end(); I != E; ++I) { |
| 790 | PredCases.push_back(std::make_pair(*I, BBDefault)); |
| 791 | NewSuccessors.push_back(BBDefault); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | // Okay, at this point, we know which new successor Pred will get. Make |
| 796 | // sure we update the number of entries in the PHI nodes for these |
| 797 | // successors. |
| 798 | for (unsigned i = 0, e = NewSuccessors.size(); i != e; ++i) |
| 799 | AddPredecessorToBlock(NewSuccessors[i], Pred, BB); |
| 800 | |
| 801 | // Now that the successors are updated, create the new Switch instruction. |
Chris Lattner | 3788059 | 2005-01-29 00:38:26 +0000 | [diff] [blame] | 802 | SwitchInst *NewSI = new SwitchInst(CV, PredDefault, PredCases.size(),PTI); |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 803 | for (unsigned i = 0, e = PredCases.size(); i != e; ++i) |
| 804 | NewSI->addCase(PredCases[i].first, PredCases[i].second); |
Chris Lattner | 13b2f76 | 2005-01-01 16:02:12 +0000 | [diff] [blame] | 805 | |
| 806 | Instruction *DeadCond = 0; |
| 807 | if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) |
| 808 | // If PTI is a branch, remember the condition. |
| 809 | DeadCond = dyn_cast<Instruction>(BI->getCondition()); |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 810 | Pred->getInstList().erase(PTI); |
| 811 | |
Chris Lattner | 13b2f76 | 2005-01-01 16:02:12 +0000 | [diff] [blame] | 812 | // If the condition is dead now, remove the instruction tree. |
| 813 | if (DeadCond) ErasePossiblyDeadInstructionTree(DeadCond); |
| 814 | |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 815 | // Okay, last check. If BB is still a successor of PSI, then we must |
| 816 | // have an infinite loop case. If so, add an infinitely looping block |
| 817 | // to handle the case to preserve the behavior of the code. |
| 818 | BasicBlock *InfLoopBlock = 0; |
| 819 | for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i) |
| 820 | if (NewSI->getSuccessor(i) == BB) { |
| 821 | if (InfLoopBlock == 0) { |
| 822 | // Insert it at the end of the loop, because it's either code, |
| 823 | // or it won't matter if it's hot. :) |
| 824 | InfLoopBlock = new BasicBlock("infloop", BB->getParent()); |
| 825 | new BranchInst(InfLoopBlock, InfLoopBlock); |
| 826 | } |
| 827 | NewSI->setSuccessor(i, InfLoopBlock); |
| 828 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 829 | |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 830 | Changed = true; |
| 831 | } |
| 832 | } |
| 833 | return Changed; |
| 834 | } |
| 835 | |
Chris Lattner | 6306d07 | 2005-08-03 17:59:45 +0000 | [diff] [blame] | 836 | /// HoistThenElseCodeToIf - Given a conditional branch that goes to BB1 and |
Chris Lattner | 37dc938 | 2004-11-30 00:29:14 +0000 | [diff] [blame] | 837 | /// BB2, hoist any common code in the two blocks up into the branch block. The |
| 838 | /// caller of this function guarantees that BI's block dominates BB1 and BB2. |
| 839 | static bool HoistThenElseCodeToIf(BranchInst *BI) { |
| 840 | // This does very trivial matching, with limited scanning, to find identical |
| 841 | // instructions in the two blocks. In particular, we don't want to get into |
| 842 | // O(M*N) situations here where M and N are the sizes of BB1 and BB2. As |
| 843 | // such, we currently just scan for obviously identical instructions in an |
| 844 | // identical order. |
| 845 | BasicBlock *BB1 = BI->getSuccessor(0); // The true destination. |
| 846 | BasicBlock *BB2 = BI->getSuccessor(1); // The false destination |
| 847 | |
| 848 | Instruction *I1 = BB1->begin(), *I2 = BB2->begin(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 849 | if (I1->getOpcode() != I2->getOpcode() || isa<PHINode>(I1) || |
| 850 | isa<InvokeInst>(I1) || !I1->isIdenticalTo(I2)) |
Chris Lattner | 37dc938 | 2004-11-30 00:29:14 +0000 | [diff] [blame] | 851 | return false; |
| 852 | |
| 853 | // If we get here, we can hoist at least one instruction. |
| 854 | BasicBlock *BIParent = BI->getParent(); |
Chris Lattner | 37dc938 | 2004-11-30 00:29:14 +0000 | [diff] [blame] | 855 | |
| 856 | do { |
| 857 | // If we are hoisting the terminator instruction, don't move one (making a |
| 858 | // broken BB), instead clone it, and remove BI. |
| 859 | if (isa<TerminatorInst>(I1)) |
| 860 | goto HoistTerminator; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 861 | |
Chris Lattner | 37dc938 | 2004-11-30 00:29:14 +0000 | [diff] [blame] | 862 | // For a normal instruction, we just move one to right before the branch, |
| 863 | // then replace all uses of the other with the first. Finally, we remove |
| 864 | // the now redundant second instruction. |
| 865 | BIParent->getInstList().splice(BI, BB1->getInstList(), I1); |
| 866 | if (!I2->use_empty()) |
| 867 | I2->replaceAllUsesWith(I1); |
| 868 | BB2->getInstList().erase(I2); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 869 | |
Chris Lattner | 37dc938 | 2004-11-30 00:29:14 +0000 | [diff] [blame] | 870 | I1 = BB1->begin(); |
| 871 | I2 = BB2->begin(); |
Chris Lattner | 37dc938 | 2004-11-30 00:29:14 +0000 | [diff] [blame] | 872 | } while (I1->getOpcode() == I2->getOpcode() && I1->isIdenticalTo(I2)); |
| 873 | |
| 874 | return true; |
| 875 | |
| 876 | HoistTerminator: |
| 877 | // Okay, it is safe to hoist the terminator. |
| 878 | Instruction *NT = I1->clone(); |
| 879 | BIParent->getInstList().insert(BI, NT); |
| 880 | if (NT->getType() != Type::VoidTy) { |
| 881 | I1->replaceAllUsesWith(NT); |
| 882 | I2->replaceAllUsesWith(NT); |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 883 | NT->takeName(I1); |
Chris Lattner | 37dc938 | 2004-11-30 00:29:14 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | // Hoisting one of the terminators from our successor is a great thing. |
| 887 | // Unfortunately, the successors of the if/else blocks may have PHI nodes in |
| 888 | // them. If they do, all PHI entries for BB1/BB2 must agree for all PHI |
| 889 | // nodes, so we insert select instruction to compute the final result. |
| 890 | std::map<std::pair<Value*,Value*>, SelectInst*> InsertedSelects; |
| 891 | for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) { |
| 892 | PHINode *PN; |
| 893 | for (BasicBlock::iterator BBI = SI->begin(); |
Chris Lattner | 0f535c6 | 2004-11-30 07:47:34 +0000 | [diff] [blame] | 894 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) { |
Chris Lattner | 37dc938 | 2004-11-30 00:29:14 +0000 | [diff] [blame] | 895 | Value *BB1V = PN->getIncomingValueForBlock(BB1); |
| 896 | Value *BB2V = PN->getIncomingValueForBlock(BB2); |
| 897 | if (BB1V != BB2V) { |
| 898 | // These values do not agree. Insert a select instruction before NT |
| 899 | // that determines the right value. |
| 900 | SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)]; |
| 901 | if (SI == 0) |
| 902 | SI = new SelectInst(BI->getCondition(), BB1V, BB2V, |
| 903 | BB1V->getName()+"."+BB2V->getName(), NT); |
| 904 | // Make the PHI node use the select for all incoming values for BB1/BB2 |
| 905 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 906 | if (PN->getIncomingBlock(i) == BB1 || PN->getIncomingBlock(i) == BB2) |
| 907 | PN->setIncomingValue(i, SI); |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | // Update any PHI nodes in our new successors. |
| 913 | for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) |
| 914 | AddPredecessorToBlock(*SI, BIParent, BB1); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 915 | |
Chris Lattner | 37dc938 | 2004-11-30 00:29:14 +0000 | [diff] [blame] | 916 | BI->eraseFromParent(); |
| 917 | return true; |
| 918 | } |
| 919 | |
Chris Lattner | 2e42e36 | 2005-09-20 00:43:16 +0000 | [diff] [blame] | 920 | /// BlockIsSimpleEnoughToThreadThrough - Return true if we can thread a branch |
| 921 | /// across this block. |
| 922 | static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) { |
| 923 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
Chris Lattner | e9487f0 | 2005-09-20 01:48:40 +0000 | [diff] [blame] | 924 | unsigned Size = 0; |
| 925 | |
Chris Lattner | 2e42e36 | 2005-09-20 00:43:16 +0000 | [diff] [blame] | 926 | // If this basic block contains anything other than a PHI (which controls the |
| 927 | // branch) and branch itself, bail out. FIXME: improve this in the future. |
Chris Lattner | e9487f0 | 2005-09-20 01:48:40 +0000 | [diff] [blame] | 928 | for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI, ++Size) { |
| 929 | if (Size > 10) return false; // Don't clone large BB's. |
Chris Lattner | 2e42e36 | 2005-09-20 00:43:16 +0000 | [diff] [blame] | 930 | |
Chris Lattner | e9487f0 | 2005-09-20 01:48:40 +0000 | [diff] [blame] | 931 | // We can only support instructions that are do not define values that are |
| 932 | // live outside of the current basic block. |
| 933 | for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end(); |
| 934 | UI != E; ++UI) { |
| 935 | Instruction *U = cast<Instruction>(*UI); |
| 936 | if (U->getParent() != BB || isa<PHINode>(U)) return false; |
| 937 | } |
Chris Lattner | 2e42e36 | 2005-09-20 00:43:16 +0000 | [diff] [blame] | 938 | |
| 939 | // Looks ok, continue checking. |
| 940 | } |
Chris Lattner | e9487f0 | 2005-09-20 01:48:40 +0000 | [diff] [blame] | 941 | |
Chris Lattner | 2e42e36 | 2005-09-20 00:43:16 +0000 | [diff] [blame] | 942 | return true; |
| 943 | } |
| 944 | |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 945 | /// FoldCondBranchOnPHI - If we have a conditional branch on a PHI node value |
| 946 | /// that is defined in the same block as the branch and if any PHI entries are |
| 947 | /// constants, thread edges corresponding to that entry to be branches to their |
| 948 | /// ultimate destination. |
| 949 | static bool FoldCondBranchOnPHI(BranchInst *BI) { |
| 950 | BasicBlock *BB = BI->getParent(); |
| 951 | PHINode *PN = dyn_cast<PHINode>(BI->getCondition()); |
Chris Lattner | 9c88d98 | 2005-09-19 23:57:04 +0000 | [diff] [blame] | 952 | // NOTE: we currently cannot transform this case if the PHI node is used |
| 953 | // outside of the block. |
Chris Lattner | 2e42e36 | 2005-09-20 00:43:16 +0000 | [diff] [blame] | 954 | if (!PN || PN->getParent() != BB || !PN->hasOneUse()) |
| 955 | return false; |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 956 | |
| 957 | // Degenerate case of a single entry PHI. |
| 958 | if (PN->getNumIncomingValues() == 1) { |
| 959 | if (PN->getIncomingValue(0) != PN) |
| 960 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 961 | else |
| 962 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
| 963 | PN->eraseFromParent(); |
| 964 | return true; |
| 965 | } |
| 966 | |
| 967 | // Now we know that this block has multiple preds and two succs. |
Chris Lattner | 2e42e36 | 2005-09-20 00:43:16 +0000 | [diff] [blame] | 968 | if (!BlockIsSimpleEnoughToThreadThrough(BB)) return false; |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 969 | |
| 970 | // Okay, this is a simple enough basic block. See if any phi values are |
| 971 | // constants. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 972 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 973 | ConstantInt *CB; |
| 974 | if ((CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i))) && |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 975 | CB->getType() == Type::Int1Ty) { |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 976 | // Okay, we now know that all edges from PredBB should be revectored to |
| 977 | // branch to RealDest. |
| 978 | BasicBlock *PredBB = PN->getIncomingBlock(i); |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 979 | BasicBlock *RealDest = BI->getSuccessor(!CB->getZExtValue()); |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 980 | |
Chris Lattner | e9487f0 | 2005-09-20 01:48:40 +0000 | [diff] [blame] | 981 | if (RealDest == BB) continue; // Skip self loops. |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 982 | |
Chris Lattner | e9487f0 | 2005-09-20 01:48:40 +0000 | [diff] [blame] | 983 | // The dest block might have PHI nodes, other predecessors and other |
| 984 | // difficult cases. Instead of being smart about this, just insert a new |
| 985 | // block that jumps to the destination block, effectively splitting |
| 986 | // the edge we are about to create. |
| 987 | BasicBlock *EdgeBB = new BasicBlock(RealDest->getName()+".critedge", |
| 988 | RealDest->getParent(), RealDest); |
| 989 | new BranchInst(RealDest, EdgeBB); |
| 990 | PHINode *PN; |
| 991 | for (BasicBlock::iterator BBI = RealDest->begin(); |
| 992 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) { |
| 993 | Value *V = PN->getIncomingValueForBlock(BB); |
| 994 | PN->addIncoming(V, EdgeBB); |
| 995 | } |
| 996 | |
| 997 | // BB may have instructions that are being threaded over. Clone these |
| 998 | // instructions into EdgeBB. We know that there will be no uses of the |
| 999 | // cloned instructions outside of EdgeBB. |
| 1000 | BasicBlock::iterator InsertPt = EdgeBB->begin(); |
| 1001 | std::map<Value*, Value*> TranslateMap; // Track translated values. |
| 1002 | for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) { |
| 1003 | if (PHINode *PN = dyn_cast<PHINode>(BBI)) { |
| 1004 | TranslateMap[PN] = PN->getIncomingValueForBlock(PredBB); |
| 1005 | } else { |
| 1006 | // Clone the instruction. |
| 1007 | Instruction *N = BBI->clone(); |
| 1008 | if (BBI->hasName()) N->setName(BBI->getName()+".c"); |
| 1009 | |
| 1010 | // Update operands due to translation. |
| 1011 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 1012 | std::map<Value*, Value*>::iterator PI = |
| 1013 | TranslateMap.find(N->getOperand(i)); |
| 1014 | if (PI != TranslateMap.end()) |
| 1015 | N->setOperand(i, PI->second); |
| 1016 | } |
| 1017 | |
| 1018 | // Check for trivial simplification. |
| 1019 | if (Constant *C = ConstantFoldInstruction(N)) { |
Chris Lattner | e9487f0 | 2005-09-20 01:48:40 +0000 | [diff] [blame] | 1020 | TranslateMap[BBI] = C; |
| 1021 | delete N; // Constant folded away, don't need actual inst |
| 1022 | } else { |
| 1023 | // Insert the new instruction into its new home. |
| 1024 | EdgeBB->getInstList().insert(InsertPt, N); |
| 1025 | if (!BBI->use_empty()) |
| 1026 | TranslateMap[BBI] = N; |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 1031 | // Loop over all of the edges from PredBB to BB, changing them to branch |
Chris Lattner | e9487f0 | 2005-09-20 01:48:40 +0000 | [diff] [blame] | 1032 | // to EdgeBB instead. |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 1033 | TerminatorInst *PredBBTI = PredBB->getTerminator(); |
| 1034 | for (unsigned i = 0, e = PredBBTI->getNumSuccessors(); i != e; ++i) |
| 1035 | if (PredBBTI->getSuccessor(i) == BB) { |
| 1036 | BB->removePredecessor(PredBB); |
Chris Lattner | e9487f0 | 2005-09-20 01:48:40 +0000 | [diff] [blame] | 1037 | PredBBTI->setSuccessor(i, EdgeBB); |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 1040 | // Recurse, simplifying any other constants. |
| 1041 | return FoldCondBranchOnPHI(BI) | true; |
| 1042 | } |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 1043 | } |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 1044 | |
| 1045 | return false; |
| 1046 | } |
| 1047 | |
Chris Lattner | f58c1a5 | 2005-09-23 06:39:30 +0000 | [diff] [blame] | 1048 | /// FoldTwoEntryPHINode - Given a BB that starts with the specified two-entry |
| 1049 | /// PHI node, see if we can eliminate it. |
| 1050 | static bool FoldTwoEntryPHINode(PHINode *PN) { |
| 1051 | // Ok, this is a two entry PHI node. Check to see if this is a simple "if |
| 1052 | // statement", which has a very simple dominance structure. Basically, we |
| 1053 | // are trying to find the condition that is being branched on, which |
| 1054 | // subsequently causes this merge to happen. We really want control |
| 1055 | // dependence information for this check, but simplifycfg can't keep it up |
| 1056 | // to date, and this catches most of the cases we care about anyway. |
| 1057 | // |
| 1058 | BasicBlock *BB = PN->getParent(); |
| 1059 | BasicBlock *IfTrue, *IfFalse; |
| 1060 | Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse); |
| 1061 | if (!IfCond) return false; |
| 1062 | |
Chris Lattner | 822a879 | 2006-11-18 19:19:36 +0000 | [diff] [blame] | 1063 | // Okay, we found that we can merge this two-entry phi node into a select. |
| 1064 | // Doing so would require us to fold *all* two entry phi nodes in this block. |
| 1065 | // At some point this becomes non-profitable (particularly if the target |
| 1066 | // doesn't support cmov's). Only do this transformation if there are two or |
| 1067 | // fewer PHI nodes in this block. |
| 1068 | unsigned NumPhis = 0; |
| 1069 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I) |
| 1070 | if (NumPhis > 2) |
| 1071 | return false; |
| 1072 | |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 1073 | DOUT << "FOUND IF CONDITION! " << *IfCond << " T: " |
| 1074 | << IfTrue->getName() << " F: " << IfFalse->getName() << "\n"; |
Chris Lattner | f58c1a5 | 2005-09-23 06:39:30 +0000 | [diff] [blame] | 1075 | |
| 1076 | // Loop over the PHI's seeing if we can promote them all to select |
| 1077 | // instructions. While we are at it, keep track of the instructions |
| 1078 | // that need to be moved to the dominating block. |
| 1079 | std::set<Instruction*> AggressiveInsts; |
| 1080 | |
Chris Lattner | f58c1a5 | 2005-09-23 06:39:30 +0000 | [diff] [blame] | 1081 | BasicBlock::iterator AfterPHIIt = BB->begin(); |
| 1082 | while (isa<PHINode>(AfterPHIIt)) { |
| 1083 | PHINode *PN = cast<PHINode>(AfterPHIIt++); |
| 1084 | if (PN->getIncomingValue(0) == PN->getIncomingValue(1)) { |
| 1085 | if (PN->getIncomingValue(0) != PN) |
| 1086 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 1087 | else |
| 1088 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
| 1089 | } else if (!DominatesMergePoint(PN->getIncomingValue(0), BB, |
| 1090 | &AggressiveInsts) || |
| 1091 | !DominatesMergePoint(PN->getIncomingValue(1), BB, |
| 1092 | &AggressiveInsts)) { |
Chris Lattner | 055dc10 | 2005-09-23 07:23:18 +0000 | [diff] [blame] | 1093 | return false; |
Chris Lattner | f58c1a5 | 2005-09-23 06:39:30 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
Chris Lattner | f58c1a5 | 2005-09-23 06:39:30 +0000 | [diff] [blame] | 1097 | // If we all PHI nodes are promotable, check to make sure that all |
| 1098 | // instructions in the predecessor blocks can be promoted as well. If |
| 1099 | // not, we won't be able to get rid of the control flow, so it's not |
| 1100 | // worth promoting to select instructions. |
| 1101 | BasicBlock *DomBlock = 0, *IfBlock1 = 0, *IfBlock2 = 0; |
| 1102 | PN = cast<PHINode>(BB->begin()); |
| 1103 | BasicBlock *Pred = PN->getIncomingBlock(0); |
| 1104 | if (cast<BranchInst>(Pred->getTerminator())->isUnconditional()) { |
| 1105 | IfBlock1 = Pred; |
| 1106 | DomBlock = *pred_begin(Pred); |
| 1107 | for (BasicBlock::iterator I = Pred->begin(); |
| 1108 | !isa<TerminatorInst>(I); ++I) |
| 1109 | if (!AggressiveInsts.count(I)) { |
| 1110 | // This is not an aggressive instruction that we can promote. |
| 1111 | // Because of this, we won't be able to get rid of the control |
| 1112 | // flow, so the xform is not worth it. |
| 1113 | return false; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | Pred = PN->getIncomingBlock(1); |
| 1118 | if (cast<BranchInst>(Pred->getTerminator())->isUnconditional()) { |
| 1119 | IfBlock2 = Pred; |
| 1120 | DomBlock = *pred_begin(Pred); |
| 1121 | for (BasicBlock::iterator I = Pred->begin(); |
| 1122 | !isa<TerminatorInst>(I); ++I) |
| 1123 | if (!AggressiveInsts.count(I)) { |
| 1124 | // This is not an aggressive instruction that we can promote. |
| 1125 | // Because of this, we won't be able to get rid of the control |
| 1126 | // flow, so the xform is not worth it. |
| 1127 | return false; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | // If we can still promote the PHI nodes after this gauntlet of tests, |
| 1132 | // do all of the PHI's now. |
| 1133 | |
| 1134 | // Move all 'aggressive' instructions, which are defined in the |
| 1135 | // conditional parts of the if's up to the dominating block. |
| 1136 | if (IfBlock1) { |
| 1137 | DomBlock->getInstList().splice(DomBlock->getTerminator(), |
| 1138 | IfBlock1->getInstList(), |
| 1139 | IfBlock1->begin(), |
| 1140 | IfBlock1->getTerminator()); |
| 1141 | } |
| 1142 | if (IfBlock2) { |
| 1143 | DomBlock->getInstList().splice(DomBlock->getTerminator(), |
| 1144 | IfBlock2->getInstList(), |
| 1145 | IfBlock2->begin(), |
| 1146 | IfBlock2->getTerminator()); |
| 1147 | } |
| 1148 | |
| 1149 | while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { |
| 1150 | // Change the PHI node into a select instruction. |
| 1151 | Value *TrueVal = |
| 1152 | PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse); |
| 1153 | Value *FalseVal = |
| 1154 | PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue); |
| 1155 | |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 1156 | Value *NV = new SelectInst(IfCond, TrueVal, FalseVal, "", AfterPHIIt); |
| 1157 | PN->replaceAllUsesWith(NV); |
| 1158 | NV->takeName(PN); |
| 1159 | |
Chris Lattner | f58c1a5 | 2005-09-23 06:39:30 +0000 | [diff] [blame] | 1160 | BB->getInstList().erase(PN); |
| 1161 | } |
| 1162 | return true; |
| 1163 | } |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | 1654cff | 2004-06-19 07:02:14 +0000 | [diff] [blame] | 1165 | namespace { |
| 1166 | /// ConstantIntOrdering - This class implements a stable ordering of constant |
| 1167 | /// integers that does not depend on their address. This is important for |
| 1168 | /// applications that sort ConstantInt's to ensure uniqueness. |
| 1169 | struct ConstantIntOrdering { |
| 1170 | bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const { |
Reid Spencer | e1c99d4 | 2007-03-02 23:01:14 +0000 | [diff] [blame] | 1171 | return LHS->getValue().ult(RHS->getValue()); |
Chris Lattner | 1654cff | 2004-06-19 07:02:14 +0000 | [diff] [blame] | 1172 | } |
| 1173 | }; |
| 1174 | } |
| 1175 | |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1176 | // SimplifyCFG - This function is used to do simplification of a CFG. For |
| 1177 | // example, it adjusts branches to branches to eliminate the extra hop, it |
| 1178 | // eliminates unreachable basic blocks, and does other "peephole" optimization |
Chris Lattner | e2ca540 | 2003-03-05 21:01:52 +0000 | [diff] [blame] | 1179 | // of the CFG. It returns true if a modification was made. |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1180 | // |
| 1181 | // WARNING: The entry node of a function may not be simplified. |
| 1182 | // |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 1183 | bool llvm::SimplifyCFG(BasicBlock *BB) { |
Chris Lattner | dc3602b | 2003-08-24 18:36:16 +0000 | [diff] [blame] | 1184 | bool Changed = false; |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1185 | Function *M = BB->getParent(); |
| 1186 | |
| 1187 | assert(BB && BB->getParent() && "Block not embedded in function!"); |
| 1188 | assert(BB->getTerminator() && "Degenerate basic block encountered!"); |
Dan Gohman | ecb7a77 | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 1189 | assert(&BB->getParent()->getEntryBlock() != BB && |
| 1190 | "Can't Simplify entry block!"); |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1191 | |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1192 | // Remove basic blocks that have no predecessors... which are unreachable. |
Chris Lattner | d52c261 | 2004-02-24 07:23:58 +0000 | [diff] [blame] | 1193 | if (pred_begin(BB) == pred_end(BB) || |
| 1194 | *pred_begin(BB) == BB && ++pred_begin(BB) == pred_end(BB)) { |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 1195 | DOUT << "Removing BB: \n" << *BB; |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1196 | |
| 1197 | // Loop through all of our successors and make sure they know that one |
| 1198 | // of their predecessors is going away. |
Chris Lattner | 151c80b | 2005-04-12 18:51:33 +0000 | [diff] [blame] | 1199 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 1200 | SI->removePredecessor(BB); |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1201 | |
| 1202 | while (!BB->empty()) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 1203 | Instruction &I = BB->back(); |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1204 | // If this instruction is used, replace uses with an arbitrary |
Chris Lattner | f5e982d | 2005-08-02 23:29:23 +0000 | [diff] [blame] | 1205 | // value. Because control flow can't get here, we don't care |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1206 | // what we replace the value with. Note that since this block is |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1207 | // unreachable, and all values contained within it must dominate their |
| 1208 | // uses, that all uses will eventually be removed. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1209 | if (!I.use_empty()) |
Chris Lattner | f5e982d | 2005-08-02 23:29:23 +0000 | [diff] [blame] | 1210 | // Make all users of this instruction use undef instead |
| 1211 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1212 | |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1213 | // Remove the instruction from the basic block |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 1214 | BB->getInstList().pop_back(); |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1215 | } |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 1216 | M->getBasicBlockList().erase(BB); |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1217 | return true; |
| 1218 | } |
| 1219 | |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 1220 | // Check to see if we can constant propagate this terminator instruction |
| 1221 | // away... |
Chris Lattner | dc3602b | 2003-08-24 18:36:16 +0000 | [diff] [blame] | 1222 | Changed |= ConstantFoldTerminator(BB); |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 1223 | |
Chris Lattner | 19831ec | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 1224 | // If this is a returning block with only PHI nodes in it, fold the return |
| 1225 | // instruction into any unconditional branch predecessors. |
Chris Lattner | 147af6b | 2004-04-02 18:13:43 +0000 | [diff] [blame] | 1226 | // |
| 1227 | // If any predecessor is a conditional branch that just selects among |
| 1228 | // different return values, fold the replace the branch/return with a select |
| 1229 | // and return. |
Chris Lattner | 19831ec | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 1230 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
| 1231 | BasicBlock::iterator BBI = BB->getTerminator(); |
| 1232 | if (BBI == BB->begin() || isa<PHINode>(--BBI)) { |
Chris Lattner | 147af6b | 2004-04-02 18:13:43 +0000 | [diff] [blame] | 1233 | // Find predecessors that end with branches. |
Chris Lattner | 19831ec | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 1234 | std::vector<BasicBlock*> UncondBranchPreds; |
Chris Lattner | 147af6b | 2004-04-02 18:13:43 +0000 | [diff] [blame] | 1235 | std::vector<BranchInst*> CondBranchPreds; |
Chris Lattner | 19831ec | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 1236 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { |
| 1237 | TerminatorInst *PTI = (*PI)->getTerminator(); |
| 1238 | if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) |
| 1239 | if (BI->isUnconditional()) |
| 1240 | UncondBranchPreds.push_back(*PI); |
Chris Lattner | 147af6b | 2004-04-02 18:13:43 +0000 | [diff] [blame] | 1241 | else |
| 1242 | CondBranchPreds.push_back(BI); |
Chris Lattner | 19831ec | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 1243 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1244 | |
Chris Lattner | 19831ec | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 1245 | // If we found some, do the transformation! |
| 1246 | if (!UncondBranchPreds.empty()) { |
| 1247 | while (!UncondBranchPreds.empty()) { |
| 1248 | BasicBlock *Pred = UncondBranchPreds.back(); |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 1249 | DOUT << "FOLDING: " << *BB |
| 1250 | << "INTO UNCOND BRANCH PRED: " << *Pred; |
Chris Lattner | 19831ec | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 1251 | UncondBranchPreds.pop_back(); |
| 1252 | Instruction *UncondBranch = Pred->getTerminator(); |
| 1253 | // Clone the return and add it to the end of the predecessor. |
| 1254 | Instruction *NewRet = RI->clone(); |
| 1255 | Pred->getInstList().push_back(NewRet); |
| 1256 | |
| 1257 | // If the return instruction returns a value, and if the value was a |
| 1258 | // PHI node in "BB", propagate the right value into the return. |
| 1259 | if (NewRet->getNumOperands() == 1) |
| 1260 | if (PHINode *PN = dyn_cast<PHINode>(NewRet->getOperand(0))) |
| 1261 | if (PN->getParent() == BB) |
| 1262 | NewRet->setOperand(0, PN->getIncomingValueForBlock(Pred)); |
| 1263 | // Update any PHI nodes in the returning block to realize that we no |
| 1264 | // longer branch to them. |
| 1265 | BB->removePredecessor(Pred); |
| 1266 | Pred->getInstList().erase(UncondBranch); |
| 1267 | } |
| 1268 | |
| 1269 | // If we eliminated all predecessors of the block, delete the block now. |
| 1270 | if (pred_begin(BB) == pred_end(BB)) |
| 1271 | // We know there are no successors, so just nuke the block. |
| 1272 | M->getBasicBlockList().erase(BB); |
| 1273 | |
Chris Lattner | 19831ec | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 1274 | return true; |
| 1275 | } |
Chris Lattner | 147af6b | 2004-04-02 18:13:43 +0000 | [diff] [blame] | 1276 | |
| 1277 | // Check out all of the conditional branches going to this return |
| 1278 | // instruction. If any of them just select between returns, change the |
| 1279 | // branch itself into a select/return pair. |
| 1280 | while (!CondBranchPreds.empty()) { |
| 1281 | BranchInst *BI = CondBranchPreds.back(); |
| 1282 | CondBranchPreds.pop_back(); |
| 1283 | BasicBlock *TrueSucc = BI->getSuccessor(0); |
| 1284 | BasicBlock *FalseSucc = BI->getSuccessor(1); |
| 1285 | BasicBlock *OtherSucc = TrueSucc == BB ? FalseSucc : TrueSucc; |
| 1286 | |
| 1287 | // Check to see if the non-BB successor is also a return block. |
| 1288 | if (isa<ReturnInst>(OtherSucc->getTerminator())) { |
| 1289 | // Check to see if there are only PHI instructions in this block. |
| 1290 | BasicBlock::iterator OSI = OtherSucc->getTerminator(); |
| 1291 | if (OSI == OtherSucc->begin() || isa<PHINode>(--OSI)) { |
| 1292 | // Okay, we found a branch that is going to two return nodes. If |
| 1293 | // there is no return value for this function, just change the |
| 1294 | // branch into a return. |
| 1295 | if (RI->getNumOperands() == 0) { |
| 1296 | TrueSucc->removePredecessor(BI->getParent()); |
| 1297 | FalseSucc->removePredecessor(BI->getParent()); |
| 1298 | new ReturnInst(0, BI); |
| 1299 | BI->getParent()->getInstList().erase(BI); |
| 1300 | return true; |
| 1301 | } |
| 1302 | |
| 1303 | // Otherwise, figure out what the true and false return values are |
| 1304 | // so we can insert a new select instruction. |
| 1305 | Value *TrueValue = TrueSucc->getTerminator()->getOperand(0); |
| 1306 | Value *FalseValue = FalseSucc->getTerminator()->getOperand(0); |
| 1307 | |
| 1308 | // Unwrap any PHI nodes in the return blocks. |
| 1309 | if (PHINode *TVPN = dyn_cast<PHINode>(TrueValue)) |
| 1310 | if (TVPN->getParent() == TrueSucc) |
| 1311 | TrueValue = TVPN->getIncomingValueForBlock(BI->getParent()); |
| 1312 | if (PHINode *FVPN = dyn_cast<PHINode>(FalseValue)) |
| 1313 | if (FVPN->getParent() == FalseSucc) |
| 1314 | FalseValue = FVPN->getIncomingValueForBlock(BI->getParent()); |
| 1315 | |
Chris Lattner | b74b181 | 2006-10-20 00:42:07 +0000 | [diff] [blame] | 1316 | // In order for this transformation to be safe, we must be able to |
| 1317 | // unconditionally execute both operands to the return. This is |
| 1318 | // normally the case, but we could have a potentially-trapping |
| 1319 | // constant expression that prevents this transformation from being |
| 1320 | // safe. |
| 1321 | if ((!isa<ConstantExpr>(TrueValue) || |
| 1322 | !cast<ConstantExpr>(TrueValue)->canTrap()) && |
| 1323 | (!isa<ConstantExpr>(TrueValue) || |
| 1324 | !cast<ConstantExpr>(TrueValue)->canTrap())) { |
| 1325 | TrueSucc->removePredecessor(BI->getParent()); |
| 1326 | FalseSucc->removePredecessor(BI->getParent()); |
Chris Lattner | 7aa773b | 2004-04-02 18:15:10 +0000 | [diff] [blame] | 1327 | |
Chris Lattner | b74b181 | 2006-10-20 00:42:07 +0000 | [diff] [blame] | 1328 | // Insert a new select instruction. |
| 1329 | Value *NewRetVal; |
| 1330 | Value *BrCond = BI->getCondition(); |
| 1331 | if (TrueValue != FalseValue) |
| 1332 | NewRetVal = new SelectInst(BrCond, TrueValue, |
| 1333 | FalseValue, "retval", BI); |
| 1334 | else |
| 1335 | NewRetVal = TrueValue; |
| 1336 | |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 1337 | DOUT << "\nCHANGING BRANCH TO TWO RETURNS INTO SELECT:" |
| 1338 | << "\n " << *BI << "Select = " << *NewRetVal |
| 1339 | << "TRUEBLOCK: " << *TrueSucc << "FALSEBLOCK: "<< *FalseSucc; |
Chris Lattner | 0ed7f42 | 2004-09-29 05:43:32 +0000 | [diff] [blame] | 1340 | |
Chris Lattner | b74b181 | 2006-10-20 00:42:07 +0000 | [diff] [blame] | 1341 | new ReturnInst(NewRetVal, BI); |
| 1342 | BI->eraseFromParent(); |
| 1343 | if (Instruction *BrCondI = dyn_cast<Instruction>(BrCond)) |
| 1344 | if (isInstructionTriviallyDead(BrCondI)) |
| 1345 | BrCondI->eraseFromParent(); |
| 1346 | return true; |
| 1347 | } |
Chris Lattner | 147af6b | 2004-04-02 18:13:43 +0000 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | } |
Chris Lattner | 19831ec | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 1351 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 1352 | } else if (isa<UnwindInst>(BB->begin())) { |
Chris Lattner | e14ea08 | 2004-02-24 05:54:22 +0000 | [diff] [blame] | 1353 | // Check to see if the first instruction in this block is just an unwind. |
| 1354 | // If so, replace any invoke instructions which use this as an exception |
Chris Lattner | af17b1d | 2004-07-20 01:17:38 +0000 | [diff] [blame] | 1355 | // destination with call instructions, and any unconditional branch |
| 1356 | // predecessor with an unwind. |
Chris Lattner | e14ea08 | 2004-02-24 05:54:22 +0000 | [diff] [blame] | 1357 | // |
| 1358 | std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB)); |
| 1359 | while (!Preds.empty()) { |
| 1360 | BasicBlock *Pred = Preds.back(); |
Chris Lattner | af17b1d | 2004-07-20 01:17:38 +0000 | [diff] [blame] | 1361 | if (BranchInst *BI = dyn_cast<BranchInst>(Pred->getTerminator())) { |
| 1362 | if (BI->isUnconditional()) { |
| 1363 | Pred->getInstList().pop_back(); // nuke uncond branch |
| 1364 | new UnwindInst(Pred); // Use unwind. |
| 1365 | Changed = true; |
| 1366 | } |
| 1367 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(Pred->getTerminator())) |
Chris Lattner | e14ea08 | 2004-02-24 05:54:22 +0000 | [diff] [blame] | 1368 | if (II->getUnwindDest() == BB) { |
| 1369 | // Insert a new branch instruction before the invoke, because this |
| 1370 | // is now a fall through... |
| 1371 | BranchInst *BI = new BranchInst(II->getNormalDest(), II); |
| 1372 | Pred->getInstList().remove(II); // Take out of symbol table |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1373 | |
Chris Lattner | e14ea08 | 2004-02-24 05:54:22 +0000 | [diff] [blame] | 1374 | // Insert the call now... |
Chris Lattner | 93e985f | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 1375 | SmallVector<Value*,8> Args(II->op_begin()+3, II->op_end()); |
| 1376 | CallInst *CI = new CallInst(II->getCalledValue(), |
| 1377 | &Args[0], Args.size(), II->getName(), BI); |
Chris Lattner | 16d0db2 | 2005-05-14 12:21:56 +0000 | [diff] [blame] | 1378 | CI->setCallingConv(II->getCallingConv()); |
Chris Lattner | e14ea08 | 2004-02-24 05:54:22 +0000 | [diff] [blame] | 1379 | // If the invoke produced a value, the Call now does instead |
| 1380 | II->replaceAllUsesWith(CI); |
| 1381 | delete II; |
| 1382 | Changed = true; |
| 1383 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | e14ea08 | 2004-02-24 05:54:22 +0000 | [diff] [blame] | 1385 | Preds.pop_back(); |
| 1386 | } |
Chris Lattner | 8e509dd | 2004-02-24 16:09:21 +0000 | [diff] [blame] | 1387 | |
| 1388 | // If this block is now dead, remove it. |
| 1389 | if (pred_begin(BB) == pred_end(BB)) { |
| 1390 | // We know there are no successors, so just nuke the block. |
| 1391 | M->getBasicBlockList().erase(BB); |
| 1392 | return true; |
| 1393 | } |
| 1394 | |
Chris Lattner | 623369a | 2005-02-24 06:17:52 +0000 | [diff] [blame] | 1395 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) { |
| 1396 | if (isValueEqualityComparison(SI)) { |
| 1397 | // If we only have one predecessor, and if it is a branch on this value, |
| 1398 | // see if that predecessor totally determines the outcome of this switch. |
| 1399 | if (BasicBlock *OnlyPred = BB->getSinglePredecessor()) |
| 1400 | if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred)) |
| 1401 | return SimplifyCFG(BB) || 1; |
| 1402 | |
| 1403 | // If the block only contains the switch, see if we can fold the block |
| 1404 | // away into any preds. |
| 1405 | if (SI == &BB->front()) |
| 1406 | if (FoldValueComparisonIntoPredecessors(SI)) |
| 1407 | return SimplifyCFG(BB) || 1; |
| 1408 | } |
Chris Lattner | 542f149 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 1409 | } else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) { |
Chris Lattner | 7e66348 | 2005-08-03 00:11:16 +0000 | [diff] [blame] | 1410 | if (BI->isUnconditional()) { |
| 1411 | BasicBlock::iterator BBI = BB->begin(); // Skip over phi nodes... |
| 1412 | while (isa<PHINode>(*BBI)) ++BBI; |
| 1413 | |
| 1414 | BasicBlock *Succ = BI->getSuccessor(0); |
| 1415 | if (BBI->isTerminator() && // Terminator is the only non-phi instruction! |
| 1416 | Succ != BB) // Don't hurt infinite loops! |
| 1417 | if (TryToSimplifyUncondBranchFromEmptyBlock(BB, Succ)) |
| 1418 | return 1; |
| 1419 | |
| 1420 | } else { // Conditional branch |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 1421 | if (isValueEqualityComparison(BI)) { |
Chris Lattner | 623369a | 2005-02-24 06:17:52 +0000 | [diff] [blame] | 1422 | // If we only have one predecessor, and if it is a branch on this value, |
| 1423 | // see if that predecessor totally determines the outcome of this |
| 1424 | // switch. |
| 1425 | if (BasicBlock *OnlyPred = BB->getSinglePredecessor()) |
| 1426 | if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred)) |
| 1427 | return SimplifyCFG(BB) || 1; |
| 1428 | |
Chris Lattner | e67fa05 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 1429 | // This block must be empty, except for the setcond inst, if it exists. |
| 1430 | BasicBlock::iterator I = BB->begin(); |
| 1431 | if (&*I == BI || |
| 1432 | (&*I == cast<Instruction>(BI->getCondition()) && |
| 1433 | &*++I == BI)) |
| 1434 | if (FoldValueComparisonIntoPredecessors(BI)) |
| 1435 | return SimplifyCFG(BB) | true; |
| 1436 | } |
Chris Lattner | eaba3a1 | 2005-09-19 23:49:37 +0000 | [diff] [blame] | 1437 | |
| 1438 | // If this is a branch on a phi node in the current block, thread control |
| 1439 | // through this block if any PHI node entries are constants. |
| 1440 | if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition())) |
| 1441 | if (PN->getParent() == BI->getParent()) |
| 1442 | if (FoldCondBranchOnPHI(BI)) |
| 1443 | return SimplifyCFG(BB) | true; |
Chris Lattner | e67fa05 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 1444 | |
| 1445 | // If this basic block is ONLY a setcc and a branch, and if a predecessor |
| 1446 | // branches to us and one of our successors, fold the setcc into the |
| 1447 | // predecessor and use logical operations to pick the right destination. |
Chris Lattner | 12fe2b1 | 2004-05-02 05:02:03 +0000 | [diff] [blame] | 1448 | BasicBlock *TrueDest = BI->getSuccessor(0); |
| 1449 | BasicBlock *FalseDest = BI->getSuccessor(1); |
Chris Lattner | decb0ca | 2007-04-17 17:47:54 +0000 | [diff] [blame] | 1450 | if (Instruction *Cond = dyn_cast<Instruction>(BI->getCondition())) { |
| 1451 | BasicBlock::iterator CondIt = Cond; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1452 | if ((isa<CmpInst>(Cond) || isa<BinaryOperator>(Cond)) && |
| 1453 | Cond->getParent() == BB && &BB->front() == Cond && |
Chris Lattner | decb0ca | 2007-04-17 17:47:54 +0000 | [diff] [blame] | 1454 | &*++CondIt == BI && Cond->hasOneUse() && |
Chris Lattner | 12fe2b1 | 2004-05-02 05:02:03 +0000 | [diff] [blame] | 1455 | TrueDest != BB && FalseDest != BB) |
Chris Lattner | e67fa05 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 1456 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI!=E; ++PI) |
| 1457 | if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator())) |
Chris Lattner | a1f79fb | 2004-05-02 01:00:44 +0000 | [diff] [blame] | 1458 | if (PBI->isConditional() && SafeToMergeTerminators(BI, PBI)) { |
Chris Lattner | 2636c1b | 2004-06-21 07:19:01 +0000 | [diff] [blame] | 1459 | BasicBlock *PredBlock = *PI; |
Chris Lattner | e67fa05 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 1460 | if (PBI->getSuccessor(0) == FalseDest || |
| 1461 | PBI->getSuccessor(1) == TrueDest) { |
| 1462 | // Invert the predecessors condition test (xor it with true), |
| 1463 | // which allows us to write this code once. |
| 1464 | Value *NewCond = |
| 1465 | BinaryOperator::createNot(PBI->getCondition(), |
| 1466 | PBI->getCondition()->getName()+".not", PBI); |
| 1467 | PBI->setCondition(NewCond); |
| 1468 | BasicBlock *OldTrue = PBI->getSuccessor(0); |
| 1469 | BasicBlock *OldFalse = PBI->getSuccessor(1); |
| 1470 | PBI->setSuccessor(0, OldFalse); |
| 1471 | PBI->setSuccessor(1, OldTrue); |
| 1472 | } |
| 1473 | |
Chris Lattner | 299520d | 2006-02-18 00:33:17 +0000 | [diff] [blame] | 1474 | if ((PBI->getSuccessor(0) == TrueDest && FalseDest != BB) || |
| 1475 | (PBI->getSuccessor(1) == FalseDest && TrueDest != BB)) { |
Chris Lattner | 2636c1b | 2004-06-21 07:19:01 +0000 | [diff] [blame] | 1476 | // Clone Cond into the predecessor basic block, and or/and the |
Chris Lattner | e67fa05 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 1477 | // two conditions together. |
| 1478 | Instruction *New = Cond->clone(); |
Chris Lattner | 2636c1b | 2004-06-21 07:19:01 +0000 | [diff] [blame] | 1479 | PredBlock->getInstList().insert(PBI, New); |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 1480 | New->takeName(Cond); |
| 1481 | Cond->setName(New->getName()+".old"); |
Chris Lattner | e67fa05 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 1482 | Instruction::BinaryOps Opcode = |
| 1483 | PBI->getSuccessor(0) == TrueDest ? |
| 1484 | Instruction::Or : Instruction::And; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1485 | Value *NewCond = |
Chris Lattner | e67fa05 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 1486 | BinaryOperator::create(Opcode, PBI->getCondition(), |
| 1487 | New, "bothcond", PBI); |
| 1488 | PBI->setCondition(NewCond); |
| 1489 | if (PBI->getSuccessor(0) == BB) { |
Chris Lattner | 2636c1b | 2004-06-21 07:19:01 +0000 | [diff] [blame] | 1490 | AddPredecessorToBlock(TrueDest, PredBlock, BB); |
Chris Lattner | e67fa05 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 1491 | PBI->setSuccessor(0, TrueDest); |
| 1492 | } |
| 1493 | if (PBI->getSuccessor(1) == BB) { |
Chris Lattner | 2636c1b | 2004-06-21 07:19:01 +0000 | [diff] [blame] | 1494 | AddPredecessorToBlock(FalseDest, PredBlock, BB); |
Chris Lattner | e67fa05 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 1495 | PBI->setSuccessor(1, FalseDest); |
| 1496 | } |
| 1497 | return SimplifyCFG(BB) | 1; |
| 1498 | } |
| 1499 | } |
Chris Lattner | decb0ca | 2007-04-17 17:47:54 +0000 | [diff] [blame] | 1500 | } |
Chris Lattner | e67fa05 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 1501 | |
Chris Lattner | decb0ca | 2007-04-17 17:47:54 +0000 | [diff] [blame] | 1502 | // Scan predessor blocks for conditional branches. |
Chris Lattner | 2e42e36 | 2005-09-20 00:43:16 +0000 | [diff] [blame] | 1503 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 1504 | if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator())) |
Chris Lattner | 263d1e4 | 2005-09-23 18:47:20 +0000 | [diff] [blame] | 1505 | if (PBI != BI && PBI->isConditional()) { |
| 1506 | |
| 1507 | // If this block ends with a branch instruction, and if there is a |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1508 | // predecessor that ends on a branch of the same condition, make |
| 1509 | // this conditional branch redundant. |
Chris Lattner | 263d1e4 | 2005-09-23 18:47:20 +0000 | [diff] [blame] | 1510 | if (PBI->getCondition() == BI->getCondition() && |
| 1511 | PBI->getSuccessor(0) != PBI->getSuccessor(1)) { |
| 1512 | // Okay, the outcome of this conditional branch is statically |
| 1513 | // knowable. If this block had a single pred, handle specially. |
| 1514 | if (BB->getSinglePredecessor()) { |
| 1515 | // Turn this into a branch on constant. |
| 1516 | bool CondIsTrue = PBI->getSuccessor(0) == BB; |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1517 | BI->setCondition(ConstantInt::get(Type::Int1Ty, CondIsTrue)); |
Chris Lattner | 263d1e4 | 2005-09-23 18:47:20 +0000 | [diff] [blame] | 1518 | return SimplifyCFG(BB); // Nuke the branch on constant. |
| 1519 | } |
| 1520 | |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1521 | // Otherwise, if there are multiple predecessors, insert a PHI |
| 1522 | // that merges in the constant and simplify the block result. |
Chris Lattner | 263d1e4 | 2005-09-23 18:47:20 +0000 | [diff] [blame] | 1523 | if (BlockIsSimpleEnoughToThreadThrough(BB)) { |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1524 | PHINode *NewPN = new PHINode(Type::Int1Ty, |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1525 | BI->getCondition()->getName()+".pr", |
| 1526 | BB->begin()); |
Chris Lattner | 263d1e4 | 2005-09-23 18:47:20 +0000 | [diff] [blame] | 1527 | for (PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 1528 | if ((PBI = dyn_cast<BranchInst>((*PI)->getTerminator())) && |
| 1529 | PBI != BI && PBI->isConditional() && |
| 1530 | PBI->getCondition() == BI->getCondition() && |
| 1531 | PBI->getSuccessor(0) != PBI->getSuccessor(1)) { |
| 1532 | bool CondIsTrue = PBI->getSuccessor(0) == BB; |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 1533 | NewPN->addIncoming(ConstantInt::get(Type::Int1Ty, |
| 1534 | CondIsTrue), *PI); |
Chris Lattner | 263d1e4 | 2005-09-23 18:47:20 +0000 | [diff] [blame] | 1535 | } else { |
| 1536 | NewPN->addIncoming(BI->getCondition(), *PI); |
| 1537 | } |
| 1538 | |
| 1539 | BI->setCondition(NewPN); |
| 1540 | // This will thread the branch. |
| 1541 | return SimplifyCFG(BB) | true; |
| 1542 | } |
Chris Lattner | 2e42e36 | 2005-09-20 00:43:16 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Chris Lattner | 263d1e4 | 2005-09-23 18:47:20 +0000 | [diff] [blame] | 1545 | // If this is a conditional branch in an empty block, and if any |
| 1546 | // predecessors is a conditional branch to one of our destinations, |
| 1547 | // fold the conditions into logical ops and one cond br. |
| 1548 | if (&BB->front() == BI) { |
| 1549 | int PBIOp, BIOp; |
| 1550 | if (PBI->getSuccessor(0) == BI->getSuccessor(0)) { |
| 1551 | PBIOp = BIOp = 0; |
| 1552 | } else if (PBI->getSuccessor(0) == BI->getSuccessor(1)) { |
| 1553 | PBIOp = 0; BIOp = 1; |
| 1554 | } else if (PBI->getSuccessor(1) == BI->getSuccessor(0)) { |
| 1555 | PBIOp = 1; BIOp = 0; |
| 1556 | } else if (PBI->getSuccessor(1) == BI->getSuccessor(1)) { |
| 1557 | PBIOp = BIOp = 1; |
| 1558 | } else { |
| 1559 | PBIOp = BIOp = -1; |
| 1560 | } |
Chris Lattner | 2e42e36 | 2005-09-20 00:43:16 +0000 | [diff] [blame] | 1561 | |
Chris Lattner | 299520d | 2006-02-18 00:33:17 +0000 | [diff] [blame] | 1562 | // Check to make sure that the other destination of this branch |
| 1563 | // isn't BB itself. If so, this is an infinite loop that will |
| 1564 | // keep getting unwound. |
| 1565 | if (PBIOp != -1 && PBI->getSuccessor(PBIOp) == BB) |
| 1566 | PBIOp = BIOp = -1; |
Chris Lattner | 822a879 | 2006-11-18 19:19:36 +0000 | [diff] [blame] | 1567 | |
| 1568 | // Do not perform this transformation if it would require |
| 1569 | // insertion of a large number of select instructions. For targets |
| 1570 | // without predication/cmovs, this is a big pessimization. |
| 1571 | if (PBIOp != -1) { |
| 1572 | BasicBlock *CommonDest = PBI->getSuccessor(PBIOp); |
| 1573 | |
| 1574 | unsigned NumPhis = 0; |
| 1575 | for (BasicBlock::iterator II = CommonDest->begin(); |
| 1576 | isa<PHINode>(II); ++II, ++NumPhis) { |
| 1577 | if (NumPhis > 2) { |
| 1578 | // Disable this xform. |
| 1579 | PBIOp = -1; |
| 1580 | break; |
| 1581 | } |
| 1582 | } |
| 1583 | } |
Chris Lattner | 7f2e1dd | 2006-06-12 20:18:01 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | 263d1e4 | 2005-09-23 18:47:20 +0000 | [diff] [blame] | 1585 | // Finally, if everything is ok, fold the branches to logical ops. |
| 1586 | if (PBIOp != -1) { |
| 1587 | BasicBlock *CommonDest = PBI->getSuccessor(PBIOp); |
| 1588 | BasicBlock *OtherDest = BI->getSuccessor(BIOp ^ 1); |
| 1589 | |
Chris Lattner | 7f2e1dd | 2006-06-12 20:18:01 +0000 | [diff] [blame] | 1590 | // If OtherDest *is* BB, then this is a basic block with just |
| 1591 | // a conditional branch in it, where one edge (OtherDesg) goes |
| 1592 | // back to the block. We know that the program doesn't get |
| 1593 | // stuck in the infinite loop, so the condition must be such |
| 1594 | // that OtherDest isn't branched through. Forward to CommonDest, |
| 1595 | // and avoid an infinite loop at optimizer time. |
| 1596 | if (OtherDest == BB) |
| 1597 | OtherDest = CommonDest; |
| 1598 | |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 1599 | DOUT << "FOLDING BRs:" << *PBI->getParent() |
| 1600 | << "AND: " << *BI->getParent(); |
Chris Lattner | 263d1e4 | 2005-09-23 18:47:20 +0000 | [diff] [blame] | 1601 | |
| 1602 | // BI may have other predecessors. Because of this, we leave |
| 1603 | // it alone, but modify PBI. |
| 1604 | |
| 1605 | // Make sure we get to CommonDest on True&True directions. |
| 1606 | Value *PBICond = PBI->getCondition(); |
| 1607 | if (PBIOp) |
| 1608 | PBICond = BinaryOperator::createNot(PBICond, |
| 1609 | PBICond->getName()+".not", |
| 1610 | PBI); |
| 1611 | Value *BICond = BI->getCondition(); |
| 1612 | if (BIOp) |
| 1613 | BICond = BinaryOperator::createNot(BICond, |
| 1614 | BICond->getName()+".not", |
| 1615 | PBI); |
| 1616 | // Merge the conditions. |
| 1617 | Value *Cond = |
| 1618 | BinaryOperator::createOr(PBICond, BICond, "brmerge", PBI); |
| 1619 | |
| 1620 | // Modify PBI to branch on the new condition to the new dests. |
| 1621 | PBI->setCondition(Cond); |
| 1622 | PBI->setSuccessor(0, CommonDest); |
| 1623 | PBI->setSuccessor(1, OtherDest); |
| 1624 | |
| 1625 | // OtherDest may have phi nodes. If so, add an entry from PBI's |
| 1626 | // block that are identical to the entries for BI's block. |
| 1627 | PHINode *PN; |
| 1628 | for (BasicBlock::iterator II = OtherDest->begin(); |
| 1629 | (PN = dyn_cast<PHINode>(II)); ++II) { |
| 1630 | Value *V = PN->getIncomingValueForBlock(BB); |
| 1631 | PN->addIncoming(V, PBI->getParent()); |
| 1632 | } |
| 1633 | |
| 1634 | // We know that the CommonDest already had an edge from PBI to |
| 1635 | // it. If it has PHIs though, the PHIs may have different |
| 1636 | // entries for BB and PBI's BB. If so, insert a select to make |
| 1637 | // them agree. |
| 1638 | for (BasicBlock::iterator II = CommonDest->begin(); |
| 1639 | (PN = dyn_cast<PHINode>(II)); ++II) { |
| 1640 | Value * BIV = PN->getIncomingValueForBlock(BB); |
| 1641 | unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent()); |
| 1642 | Value *PBIV = PN->getIncomingValue(PBBIdx); |
| 1643 | if (BIV != PBIV) { |
| 1644 | // Insert a select in PBI to pick the right value. |
| 1645 | Value *NV = new SelectInst(PBICond, PBIV, BIV, |
| 1646 | PBIV->getName()+".mux", PBI); |
| 1647 | PN->setIncomingValue(PBBIdx, NV); |
| 1648 | } |
| 1649 | } |
| 1650 | |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 1651 | DOUT << "INTO: " << *PBI->getParent(); |
Chris Lattner | 263d1e4 | 2005-09-23 18:47:20 +0000 | [diff] [blame] | 1652 | |
| 1653 | // This basic block is probably dead. We know it has at least |
| 1654 | // one fewer predecessor. |
| 1655 | return SimplifyCFG(BB) | true; |
| 1656 | } |
Chris Lattner | 2e42e36 | 2005-09-20 00:43:16 +0000 | [diff] [blame] | 1657 | } |
Chris Lattner | 92da2c2 | 2004-05-01 22:36:37 +0000 | [diff] [blame] | 1658 | } |
Chris Lattner | d52c261 | 2004-02-24 07:23:58 +0000 | [diff] [blame] | 1659 | } |
Chris Lattner | 698f96f | 2004-10-18 04:07:22 +0000 | [diff] [blame] | 1660 | } else if (isa<UnreachableInst>(BB->getTerminator())) { |
| 1661 | // If there are any instructions immediately before the unreachable that can |
| 1662 | // be removed, do so. |
| 1663 | Instruction *Unreachable = BB->getTerminator(); |
| 1664 | while (Unreachable != BB->begin()) { |
| 1665 | BasicBlock::iterator BBI = Unreachable; |
| 1666 | --BBI; |
| 1667 | if (isa<CallInst>(BBI)) break; |
| 1668 | // Delete this instruction |
| 1669 | BB->getInstList().erase(BBI); |
| 1670 | Changed = true; |
| 1671 | } |
| 1672 | |
| 1673 | // If the unreachable instruction is the first in the block, take a gander |
| 1674 | // at all of the predecessors of this instruction, and simplify them. |
| 1675 | if (&BB->front() == Unreachable) { |
| 1676 | std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB)); |
| 1677 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) { |
| 1678 | TerminatorInst *TI = Preds[i]->getTerminator(); |
| 1679 | |
| 1680 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 1681 | if (BI->isUnconditional()) { |
| 1682 | if (BI->getSuccessor(0) == BB) { |
| 1683 | new UnreachableInst(TI); |
| 1684 | TI->eraseFromParent(); |
| 1685 | Changed = true; |
| 1686 | } |
| 1687 | } else { |
| 1688 | if (BI->getSuccessor(0) == BB) { |
| 1689 | new BranchInst(BI->getSuccessor(1), BI); |
| 1690 | BI->eraseFromParent(); |
| 1691 | } else if (BI->getSuccessor(1) == BB) { |
| 1692 | new BranchInst(BI->getSuccessor(0), BI); |
| 1693 | BI->eraseFromParent(); |
| 1694 | Changed = true; |
| 1695 | } |
| 1696 | } |
| 1697 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 1698 | for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i) |
| 1699 | if (SI->getSuccessor(i) == BB) { |
Chris Lattner | 42eb752 | 2005-05-20 22:19:54 +0000 | [diff] [blame] | 1700 | BB->removePredecessor(SI->getParent()); |
Chris Lattner | 698f96f | 2004-10-18 04:07:22 +0000 | [diff] [blame] | 1701 | SI->removeCase(i); |
| 1702 | --i; --e; |
| 1703 | Changed = true; |
| 1704 | } |
| 1705 | // If the default value is unreachable, figure out the most popular |
| 1706 | // destination and make it the default. |
| 1707 | if (SI->getSuccessor(0) == BB) { |
| 1708 | std::map<BasicBlock*, unsigned> Popularity; |
| 1709 | for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i) |
| 1710 | Popularity[SI->getSuccessor(i)]++; |
| 1711 | |
| 1712 | // Find the most popular block. |
| 1713 | unsigned MaxPop = 0; |
| 1714 | BasicBlock *MaxBlock = 0; |
| 1715 | for (std::map<BasicBlock*, unsigned>::iterator |
| 1716 | I = Popularity.begin(), E = Popularity.end(); I != E; ++I) { |
| 1717 | if (I->second > MaxPop) { |
| 1718 | MaxPop = I->second; |
| 1719 | MaxBlock = I->first; |
| 1720 | } |
| 1721 | } |
| 1722 | if (MaxBlock) { |
| 1723 | // Make this the new default, allowing us to delete any explicit |
| 1724 | // edges to it. |
| 1725 | SI->setSuccessor(0, MaxBlock); |
| 1726 | Changed = true; |
| 1727 | |
Chris Lattner | 42eb752 | 2005-05-20 22:19:54 +0000 | [diff] [blame] | 1728 | // If MaxBlock has phinodes in it, remove MaxPop-1 entries from |
| 1729 | // it. |
| 1730 | if (isa<PHINode>(MaxBlock->begin())) |
| 1731 | for (unsigned i = 0; i != MaxPop-1; ++i) |
| 1732 | MaxBlock->removePredecessor(SI->getParent()); |
| 1733 | |
Chris Lattner | 698f96f | 2004-10-18 04:07:22 +0000 | [diff] [blame] | 1734 | for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i) |
| 1735 | if (SI->getSuccessor(i) == MaxBlock) { |
| 1736 | SI->removeCase(i); |
| 1737 | --i; --e; |
| 1738 | } |
| 1739 | } |
| 1740 | } |
| 1741 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) { |
| 1742 | if (II->getUnwindDest() == BB) { |
| 1743 | // Convert the invoke to a call instruction. This would be a good |
| 1744 | // place to note that the call does not throw though. |
| 1745 | BranchInst *BI = new BranchInst(II->getNormalDest(), II); |
| 1746 | II->removeFromParent(); // Take out of symbol table |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1747 | |
Chris Lattner | 698f96f | 2004-10-18 04:07:22 +0000 | [diff] [blame] | 1748 | // Insert the call now... |
Chris Lattner | 93e985f | 2007-02-13 02:10:56 +0000 | [diff] [blame] | 1749 | SmallVector<Value*, 8> Args(II->op_begin()+3, II->op_end()); |
| 1750 | CallInst *CI = new CallInst(II->getCalledValue(), |
| 1751 | &Args[0], Args.size(), |
Chris Lattner | 698f96f | 2004-10-18 04:07:22 +0000 | [diff] [blame] | 1752 | II->getName(), BI); |
Chris Lattner | 16d0db2 | 2005-05-14 12:21:56 +0000 | [diff] [blame] | 1753 | CI->setCallingConv(II->getCallingConv()); |
Chris Lattner | 698f96f | 2004-10-18 04:07:22 +0000 | [diff] [blame] | 1754 | // If the invoke produced a value, the Call does now instead. |
| 1755 | II->replaceAllUsesWith(CI); |
| 1756 | delete II; |
| 1757 | Changed = true; |
| 1758 | } |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | // If this block is now dead, remove it. |
| 1763 | if (pred_begin(BB) == pred_end(BB)) { |
| 1764 | // We know there are no successors, so just nuke the block. |
| 1765 | M->getBasicBlockList().erase(BB); |
| 1766 | return true; |
| 1767 | } |
| 1768 | } |
Chris Lattner | 19831ec | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1771 | // Merge basic blocks into their predecessor if there is only one distinct |
| 1772 | // pred, and if there is only one distinct successor of the predecessor, and |
| 1773 | // if there are no PHI nodes. |
| 1774 | // |
Chris Lattner | 2355f94 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 1775 | pred_iterator PI(pred_begin(BB)), PE(pred_end(BB)); |
| 1776 | BasicBlock *OnlyPred = *PI++; |
| 1777 | for (; PI != PE; ++PI) // Search all predecessors, see if they are all same |
| 1778 | if (*PI != OnlyPred) { |
| 1779 | OnlyPred = 0; // There are multiple different predecessors... |
| 1780 | break; |
| 1781 | } |
Chris Lattner | 92da2c2 | 2004-05-01 22:36:37 +0000 | [diff] [blame] | 1782 | |
Chris Lattner | 2355f94 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 1783 | BasicBlock *OnlySucc = 0; |
| 1784 | if (OnlyPred && OnlyPred != BB && // Don't break self loops |
| 1785 | OnlyPred->getTerminator()->getOpcode() != Instruction::Invoke) { |
| 1786 | // Check to see if there is only one distinct successor... |
| 1787 | succ_iterator SI(succ_begin(OnlyPred)), SE(succ_end(OnlyPred)); |
| 1788 | OnlySucc = BB; |
| 1789 | for (; SI != SE; ++SI) |
| 1790 | if (*SI != OnlySucc) { |
| 1791 | OnlySucc = 0; // There are multiple distinct successors! |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1792 | break; |
| 1793 | } |
Chris Lattner | 2355f94 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | if (OnlySucc) { |
Bill Wendling | 0d45a09 | 2006-11-26 10:17:54 +0000 | [diff] [blame] | 1797 | DOUT << "Merging: " << *BB << "into: " << *OnlyPred; |
Chris Lattner | 2355f94 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 1798 | |
| 1799 | // Resolve any PHI nodes at the start of the block. They are all |
| 1800 | // guaranteed to have exactly one entry if they exist, unless there are |
| 1801 | // multiple duplicate (but guaranteed to be equal) entries for the |
| 1802 | // incoming edges. This occurs when there are multiple edges from |
| 1803 | // OnlyPred to OnlySucc. |
| 1804 | // |
| 1805 | while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { |
| 1806 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 1807 | BB->getInstList().pop_front(); // Delete the phi node. |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 1810 | // Delete the unconditional branch from the predecessor. |
Chris Lattner | 2355f94 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 1811 | OnlyPred->getInstList().pop_back(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1812 | |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 1813 | // Move all definitions in the successor to the predecessor. |
Chris Lattner | 2355f94 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 1814 | OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1815 | |
Chris Lattner | 2355f94 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 1816 | // Make all PHI nodes that referred to BB now refer to Pred as their |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 1817 | // source. |
Chris Lattner | 2355f94 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 1818 | BB->replaceAllUsesWith(OnlyPred); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 1819 | |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 1820 | // Inherit predecessors name if it exists. |
| 1821 | if (!OnlyPred->hasName()) |
| 1822 | OnlyPred->takeName(BB); |
| 1823 | |
| 1824 | // Erase basic block from the function. |
Chris Lattner | 2355f94 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 1825 | M->getBasicBlockList().erase(BB); |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 1826 | |
Chris Lattner | 2355f94 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 1827 | return true; |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1828 | } |
Chris Lattner | 723c66d | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 1829 | |
Chris Lattner | 37dc938 | 2004-11-30 00:29:14 +0000 | [diff] [blame] | 1830 | // Otherwise, if this block only has a single predecessor, and if that block |
| 1831 | // is a conditional branch, see if we can hoist any code from this block up |
| 1832 | // into our predecessor. |
| 1833 | if (OnlyPred) |
Chris Lattner | 7613437 | 2004-12-10 17:42:31 +0000 | [diff] [blame] | 1834 | if (BranchInst *BI = dyn_cast<BranchInst>(OnlyPred->getTerminator())) |
| 1835 | if (BI->isConditional()) { |
| 1836 | // Get the other block. |
| 1837 | BasicBlock *OtherBB = BI->getSuccessor(BI->getSuccessor(0) == BB); |
| 1838 | PI = pred_begin(OtherBB); |
| 1839 | ++PI; |
| 1840 | if (PI == pred_end(OtherBB)) { |
| 1841 | // We have a conditional branch to two blocks that are only reachable |
| 1842 | // from the condbr. We know that the condbr dominates the two blocks, |
| 1843 | // so see if there is any identical code in the "then" and "else" |
| 1844 | // blocks. If so, we can hoist it up to the branching block. |
| 1845 | Changed |= HoistThenElseCodeToIf(BI); |
| 1846 | } |
Chris Lattner | 37dc938 | 2004-11-30 00:29:14 +0000 | [diff] [blame] | 1847 | } |
Chris Lattner | 37dc938 | 2004-11-30 00:29:14 +0000 | [diff] [blame] | 1848 | |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 1849 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 1850 | if (BranchInst *BI = dyn_cast<BranchInst>((*PI)->getTerminator())) |
| 1851 | // Change br (X == 0 | X == 1), T, F into a switch instruction. |
| 1852 | if (BI->isConditional() && isa<Instruction>(BI->getCondition())) { |
| 1853 | Instruction *Cond = cast<Instruction>(BI->getCondition()); |
| 1854 | // If this is a bunch of seteq's or'd together, or if it's a bunch of |
| 1855 | // 'setne's and'ed together, collect them. |
| 1856 | Value *CompVal = 0; |
Chris Lattner | 1654cff | 2004-06-19 07:02:14 +0000 | [diff] [blame] | 1857 | std::vector<ConstantInt*> Values; |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 1858 | bool TrueWhenEqual = GatherValueComparisons(Cond, CompVal, Values); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1859 | if (CompVal && CompVal->getType()->isInteger()) { |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 1860 | // There might be duplicate constants in the list, which the switch |
| 1861 | // instruction can't handle, remove them now. |
Chris Lattner | 1654cff | 2004-06-19 07:02:14 +0000 | [diff] [blame] | 1862 | std::sort(Values.begin(), Values.end(), ConstantIntOrdering()); |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 1863 | Values.erase(std::unique(Values.begin(), Values.end()), Values.end()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1864 | |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 1865 | // Figure out which block is which destination. |
| 1866 | BasicBlock *DefaultBB = BI->getSuccessor(1); |
| 1867 | BasicBlock *EdgeBB = BI->getSuccessor(0); |
| 1868 | if (!TrueWhenEqual) std::swap(DefaultBB, EdgeBB); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1869 | |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 1870 | // Create the new switch instruction now. |
Chris Lattner | 3788059 | 2005-01-29 00:38:26 +0000 | [diff] [blame] | 1871 | SwitchInst *New = new SwitchInst(CompVal, DefaultBB,Values.size(),BI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1872 | |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 1873 | // Add all of the 'cases' to the switch instruction. |
| 1874 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 1875 | New->addCase(Values[i], EdgeBB); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1876 | |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 1877 | // We added edges from PI to the EdgeBB. As such, if there were any |
| 1878 | // PHI nodes in EdgeBB, they need entries to be added corresponding to |
| 1879 | // the number of edges added. |
| 1880 | for (BasicBlock::iterator BBI = EdgeBB->begin(); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 1881 | isa<PHINode>(BBI); ++BBI) { |
| 1882 | PHINode *PN = cast<PHINode>(BBI); |
Chris Lattner | 0d56008 | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 1883 | Value *InVal = PN->getIncomingValueForBlock(*PI); |
| 1884 | for (unsigned i = 0, e = Values.size()-1; i != e; ++i) |
| 1885 | PN->addIncoming(InVal, *PI); |
| 1886 | } |
| 1887 | |
| 1888 | // Erase the old branch instruction. |
| 1889 | (*PI)->getInstList().erase(BI); |
| 1890 | |
| 1891 | // Erase the potentially condition tree that was used to computed the |
| 1892 | // branch condition. |
| 1893 | ErasePossiblyDeadInstructionTree(Cond); |
| 1894 | return true; |
| 1895 | } |
| 1896 | } |
| 1897 | |
Chris Lattner | 723c66d | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 1898 | // If there is a trivial two-entry PHI node in this basic block, and we can |
| 1899 | // eliminate it, do so now. |
| 1900 | if (PHINode *PN = dyn_cast<PHINode>(BB->begin())) |
Chris Lattner | f58c1a5 | 2005-09-23 06:39:30 +0000 | [diff] [blame] | 1901 | if (PN->getNumIncomingValues() == 2) |
| 1902 | Changed |= FoldTwoEntryPHINode(PN); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 1904 | return Changed; |
Chris Lattner | 01d1ee3 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1905 | } |