Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1 | //===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | a704ac8 | 2002-10-08 21:36:33 +0000 | [diff] [blame] | 10 | // Peephole optimize the CFG. |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 18d1f19 | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/Instructions.h" |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 17 | #include "llvm/Type.h" |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CFG.h" |
| 19 | #include <algorithm> |
| 20 | #include <functional> |
Chris Lattner | a2ab489 | 2004-02-24 07:23:58 +0000 | [diff] [blame] | 21 | #include <set> |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 24 | // PropagatePredecessorsForPHIs - This gets "Succ" ready to have the |
| 25 | // predecessors from "BB". This is a little tricky because "Succ" has PHI |
| 26 | // nodes, which need to have extra slots added to them to hold the merge edges |
| 27 | // from BB's predecessors, and BB itself might have had PHI nodes in it. This |
| 28 | // function returns true (failure) if the Succ BB already has a predecessor that |
| 29 | // is a predecessor of BB and incoming PHI arguments would not be discernible. |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 30 | // |
| 31 | // Assumption: Succ is the single successor for BB. |
| 32 | // |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 33 | static bool PropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 34 | assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!"); |
Chris Lattner | 5325c5f | 2002-09-24 00:09:26 +0000 | [diff] [blame] | 35 | |
| 36 | if (!isa<PHINode>(Succ->front())) |
| 37 | return false; // We can make the transformation, no problem. |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 38 | |
| 39 | // If there is more than one predecessor, and there are PHI nodes in |
| 40 | // the successor, then we need to add incoming edges for the PHI nodes |
| 41 | // |
| 42 | const std::vector<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB)); |
| 43 | |
| 44 | // Check to see if one of the predecessors of BB is already a predecessor of |
Chris Lattner | 31116ba | 2003-03-05 21:01:52 +0000 | [diff] [blame] | 45 | // Succ. If so, we cannot do the transformation if there are any PHI nodes |
| 46 | // with incompatible values coming in from the two edges! |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 47 | // |
Chris Lattner | 31116ba | 2003-03-05 21:01:52 +0000 | [diff] [blame] | 48 | for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ); PI != PE; ++PI) |
| 49 | if (find(BBPreds.begin(), BBPreds.end(), *PI) != BBPreds.end()) { |
| 50 | // Loop over all of the PHI nodes checking to see if there are |
| 51 | // incompatible values coming in. |
Chris Lattner | e54d214 | 2003-03-05 21:36:33 +0000 | [diff] [blame] | 52 | for (BasicBlock::iterator I = Succ->begin(); |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 53 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Chris Lattner | 31116ba | 2003-03-05 21:01:52 +0000 | [diff] [blame] | 54 | // Loop up the entries in the PHI node for BB and for *PI if the values |
| 55 | // coming in are non-equal, we cannot merge these two blocks (instead we |
| 56 | // should insert a conditional move or something, then merge the |
| 57 | // blocks). |
| 58 | int Idx1 = PN->getBasicBlockIndex(BB); |
| 59 | int Idx2 = PN->getBasicBlockIndex(*PI); |
| 60 | assert(Idx1 != -1 && Idx2 != -1 && |
| 61 | "Didn't have entries for my predecessors??"); |
| 62 | if (PN->getIncomingValue(Idx1) != PN->getIncomingValue(Idx2)) |
| 63 | return true; // Values are not equal... |
| 64 | } |
| 65 | } |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 66 | |
| 67 | // Loop over all of the PHI nodes in the successor BB |
| 68 | for (BasicBlock::iterator I = Succ->begin(); |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 69 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Chris Lattner | a704ac8 | 2002-10-08 21:36:33 +0000 | [diff] [blame] | 70 | Value *OldVal = PN->removeIncomingValue(BB, false); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 71 | assert(OldVal && "No entry in PHI for Pred BB!"); |
| 72 | |
Chris Lattner | e54d214 | 2003-03-05 21:36:33 +0000 | [diff] [blame] | 73 | // If this incoming value is one of the PHI nodes in BB... |
| 74 | if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) { |
| 75 | PHINode *OldValPN = cast<PHINode>(OldVal); |
| 76 | for (std::vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(), |
| 77 | End = BBPreds.end(); PredI != End; ++PredI) { |
| 78 | PN->addIncoming(OldValPN->getIncomingValueForBlock(*PredI), *PredI); |
| 79 | } |
| 80 | } else { |
| 81 | for (std::vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(), |
| 82 | End = BBPreds.end(); PredI != End; ++PredI) { |
| 83 | // Add an incoming value for each of the new incoming values... |
| 84 | PN->addIncoming(OldVal, *PredI); |
| 85 | } |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
Chris Lattner | 18d1f19 | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 91 | /// GetIfCondition - Given a basic block (BB) with two predecessors (and |
| 92 | /// presumably PHI nodes in it), check to see if the merge at this block is due |
| 93 | /// to an "if condition". If so, return the boolean condition that determines |
| 94 | /// which entry into BB will be taken. Also, return by references the block |
| 95 | /// that will be entered from if the condition is true, and the block that will |
| 96 | /// be entered if the condition is false. |
| 97 | /// |
| 98 | /// |
| 99 | static Value *GetIfCondition(BasicBlock *BB, |
| 100 | BasicBlock *&IfTrue, BasicBlock *&IfFalse) { |
| 101 | assert(std::distance(pred_begin(BB), pred_end(BB)) == 2 && |
| 102 | "Function can only handle blocks with 2 predecessors!"); |
| 103 | BasicBlock *Pred1 = *pred_begin(BB); |
| 104 | BasicBlock *Pred2 = *++pred_begin(BB); |
| 105 | |
| 106 | // We can only handle branches. Other control flow will be lowered to |
| 107 | // branches if possible anyway. |
| 108 | if (!isa<BranchInst>(Pred1->getTerminator()) || |
| 109 | !isa<BranchInst>(Pred2->getTerminator())) |
| 110 | return 0; |
| 111 | BranchInst *Pred1Br = cast<BranchInst>(Pred1->getTerminator()); |
| 112 | BranchInst *Pred2Br = cast<BranchInst>(Pred2->getTerminator()); |
| 113 | |
| 114 | // Eliminate code duplication by ensuring that Pred1Br is conditional if |
| 115 | // either are. |
| 116 | if (Pred2Br->isConditional()) { |
| 117 | // If both branches are conditional, we don't have an "if statement". In |
| 118 | // reality, we could transform this case, but since the condition will be |
| 119 | // required anyway, we stand no chance of eliminating it, so the xform is |
| 120 | // probably not profitable. |
| 121 | if (Pred1Br->isConditional()) |
| 122 | return 0; |
| 123 | |
| 124 | std::swap(Pred1, Pred2); |
| 125 | std::swap(Pred1Br, Pred2Br); |
| 126 | } |
| 127 | |
| 128 | if (Pred1Br->isConditional()) { |
| 129 | // If we found a conditional branch predecessor, make sure that it branches |
| 130 | // to BB and Pred2Br. If it doesn't, this isn't an "if statement". |
| 131 | if (Pred1Br->getSuccessor(0) == BB && |
| 132 | Pred1Br->getSuccessor(1) == Pred2) { |
| 133 | IfTrue = Pred1; |
| 134 | IfFalse = Pred2; |
| 135 | } else if (Pred1Br->getSuccessor(0) == Pred2 && |
| 136 | Pred1Br->getSuccessor(1) == BB) { |
| 137 | IfTrue = Pred2; |
| 138 | IfFalse = Pred1; |
| 139 | } else { |
| 140 | // We know that one arm of the conditional goes to BB, so the other must |
| 141 | // go somewhere unrelated, and this must not be an "if statement". |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | // The only thing we have to watch out for here is to make sure that Pred2 |
| 146 | // doesn't have incoming edges from other blocks. If it does, the condition |
| 147 | // doesn't dominate BB. |
| 148 | if (++pred_begin(Pred2) != pred_end(Pred2)) |
| 149 | return 0; |
| 150 | |
| 151 | return Pred1Br->getCondition(); |
| 152 | } |
| 153 | |
| 154 | // Ok, if we got here, both predecessors end with an unconditional branch to |
| 155 | // BB. Don't panic! If both blocks only have a single (identical) |
| 156 | // predecessor, and THAT is a conditional branch, then we're all ok! |
| 157 | if (pred_begin(Pred1) == pred_end(Pred1) || |
| 158 | ++pred_begin(Pred1) != pred_end(Pred1) || |
| 159 | pred_begin(Pred2) == pred_end(Pred2) || |
| 160 | ++pred_begin(Pred2) != pred_end(Pred2) || |
| 161 | *pred_begin(Pred1) != *pred_begin(Pred2)) |
| 162 | return 0; |
| 163 | |
| 164 | // Otherwise, if this is a conditional branch, then we can use it! |
| 165 | BasicBlock *CommonPred = *pred_begin(Pred1); |
| 166 | if (BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator())) { |
| 167 | assert(BI->isConditional() && "Two successors but not conditional?"); |
| 168 | if (BI->getSuccessor(0) == Pred1) { |
| 169 | IfTrue = Pred1; |
| 170 | IfFalse = Pred2; |
| 171 | } else { |
| 172 | IfTrue = Pred2; |
| 173 | IfFalse = Pred1; |
| 174 | } |
| 175 | return BI->getCondition(); |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | // If we have a merge point of an "if condition" as accepted above, return true |
| 182 | // if the specified value dominates the block. We don't handle the true |
| 183 | // generality of domination here, just a special case which works well enough |
| 184 | // for us. |
Chris Lattner | 0aa5656 | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 185 | static bool DominatesMergePoint(Value *V, BasicBlock *BB, bool AllowAggressive){ |
| 186 | Instruction *I = dyn_cast<Instruction>(V); |
| 187 | if (!I) return true; // Non-instructions all dominate instructions. |
| 188 | BasicBlock *PBB = I->getParent(); |
Chris Lattner | 18d1f19 | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 0aa5656 | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 190 | // We don't want to allow wierd loops that might have the "if condition" in |
| 191 | // the bottom of this block. |
| 192 | if (PBB == BB) return false; |
Chris Lattner | 18d1f19 | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 0aa5656 | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 194 | // If this instruction is defined in a block that contains an unconditional |
| 195 | // branch to BB, then it must be in the 'conditional' part of the "if |
| 196 | // statement". |
| 197 | if (BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator())) |
| 198 | if (BI->isUnconditional() && BI->getSuccessor(0) == BB) { |
| 199 | if (!AllowAggressive) return false; |
| 200 | // Okay, it looks like the instruction IS in the "condition". Check to |
| 201 | // see if its a cheap instruction to unconditionally compute, and if it |
| 202 | // only uses stuff defined outside of the condition. If so, hoist it out. |
| 203 | switch (I->getOpcode()) { |
| 204 | default: return false; // Cannot hoist this out safely. |
| 205 | case Instruction::Load: |
| 206 | // We can hoist loads that are non-volatile and obviously cannot trap. |
| 207 | if (cast<LoadInst>(I)->isVolatile()) |
| 208 | return false; |
| 209 | if (!isa<AllocaInst>(I->getOperand(0)) && |
| 210 | !isa<Constant>(I->getOperand(0)) && |
| 211 | !isa<GlobalValue>(I->getOperand(0))) |
| 212 | return false; |
| 213 | |
| 214 | // Finally, we have to check to make sure there are no instructions |
| 215 | // before the load in its basic block, as we are going to hoist the loop |
| 216 | // out to its predecessor. |
| 217 | if (PBB->begin() != BasicBlock::iterator(I)) |
| 218 | return false; |
| 219 | break; |
| 220 | case Instruction::Add: |
| 221 | case Instruction::Sub: |
| 222 | case Instruction::And: |
| 223 | case Instruction::Or: |
| 224 | case Instruction::Xor: |
| 225 | case Instruction::Shl: |
| 226 | case Instruction::Shr: |
| 227 | break; // These are all cheap and non-trapping instructions. |
| 228 | } |
| 229 | |
| 230 | // Okay, we can only really hoist these out if their operands are not |
| 231 | // defined in the conditional region. |
| 232 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 233 | if (!DominatesMergePoint(I->getOperand(i), BB, false)) |
| 234 | return false; |
| 235 | // Okay, it's safe to do this! |
| 236 | } |
| 237 | |
Chris Lattner | 18d1f19 | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 238 | return true; |
| 239 | } |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 241 | // GatherConstantSetEQs - Given a potentially 'or'd together collection of seteq |
| 242 | // instructions that compare a value against a constant, return the value being |
| 243 | // compared, and stick the constant into the Values vector. |
Chris Lattner | b2b151d | 2004-06-19 07:02:14 +0000 | [diff] [blame^] | 244 | static Value *GatherConstantSetEQs(Value *V, std::vector<ConstantInt*> &Values){ |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 245 | if (Instruction *Inst = dyn_cast<Instruction>(V)) |
| 246 | if (Inst->getOpcode() == Instruction::SetEQ) { |
Chris Lattner | b2b151d | 2004-06-19 07:02:14 +0000 | [diff] [blame^] | 247 | if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(1))) { |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 248 | Values.push_back(C); |
| 249 | return Inst->getOperand(0); |
Chris Lattner | b2b151d | 2004-06-19 07:02:14 +0000 | [diff] [blame^] | 250 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(0))) { |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 251 | Values.push_back(C); |
| 252 | return Inst->getOperand(1); |
| 253 | } |
| 254 | } else if (Inst->getOpcode() == Instruction::Or) { |
| 255 | if (Value *LHS = GatherConstantSetEQs(Inst->getOperand(0), Values)) |
| 256 | if (Value *RHS = GatherConstantSetEQs(Inst->getOperand(1), Values)) |
| 257 | if (LHS == RHS) |
| 258 | return LHS; |
| 259 | } |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | // GatherConstantSetNEs - Given a potentially 'and'd together collection of |
| 264 | // setne instructions that compare a value against a constant, return the value |
| 265 | // being compared, and stick the constant into the Values vector. |
Chris Lattner | b2b151d | 2004-06-19 07:02:14 +0000 | [diff] [blame^] | 266 | static Value *GatherConstantSetNEs(Value *V, std::vector<ConstantInt*> &Values){ |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 267 | if (Instruction *Inst = dyn_cast<Instruction>(V)) |
| 268 | if (Inst->getOpcode() == Instruction::SetNE) { |
Chris Lattner | b2b151d | 2004-06-19 07:02:14 +0000 | [diff] [blame^] | 269 | if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(1))) { |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 270 | Values.push_back(C); |
| 271 | return Inst->getOperand(0); |
Chris Lattner | b2b151d | 2004-06-19 07:02:14 +0000 | [diff] [blame^] | 272 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(Inst->getOperand(0))) { |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 273 | Values.push_back(C); |
| 274 | return Inst->getOperand(1); |
| 275 | } |
| 276 | } else if (Inst->getOpcode() == Instruction::Cast) { |
| 277 | // Cast of X to bool is really a comparison against zero. |
| 278 | assert(Inst->getType() == Type::BoolTy && "Can only handle bool values!"); |
Chris Lattner | b2b151d | 2004-06-19 07:02:14 +0000 | [diff] [blame^] | 279 | Values.push_back(ConstantInt::get(Inst->getOperand(0)->getType(), 0)); |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 280 | return Inst->getOperand(0); |
| 281 | } else if (Inst->getOpcode() == Instruction::And) { |
| 282 | if (Value *LHS = GatherConstantSetNEs(Inst->getOperand(0), Values)) |
| 283 | if (Value *RHS = GatherConstantSetNEs(Inst->getOperand(1), Values)) |
| 284 | if (LHS == RHS) |
| 285 | return LHS; |
| 286 | } |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | |
| 292 | /// GatherValueComparisons - If the specified Cond is an 'and' or 'or' of a |
| 293 | /// bunch of comparisons of one value against constants, return the value and |
| 294 | /// the constants being compared. |
| 295 | static bool GatherValueComparisons(Instruction *Cond, Value *&CompVal, |
Chris Lattner | b2b151d | 2004-06-19 07:02:14 +0000 | [diff] [blame^] | 296 | std::vector<ConstantInt*> &Values) { |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 297 | if (Cond->getOpcode() == Instruction::Or) { |
| 298 | CompVal = GatherConstantSetEQs(Cond, Values); |
| 299 | |
| 300 | // Return true to indicate that the condition is true if the CompVal is |
| 301 | // equal to one of the constants. |
| 302 | return true; |
| 303 | } else if (Cond->getOpcode() == Instruction::And) { |
| 304 | CompVal = GatherConstantSetNEs(Cond, Values); |
| 305 | |
| 306 | // Return false to indicate that the condition is false if the CompVal is |
| 307 | // equal to one of the constants. |
| 308 | return false; |
| 309 | } |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | /// ErasePossiblyDeadInstructionTree - If the specified instruction is dead and |
| 314 | /// has no side effects, nuke it. If it uses any instructions that become dead |
| 315 | /// because the instruction is now gone, nuke them too. |
| 316 | static void ErasePossiblyDeadInstructionTree(Instruction *I) { |
| 317 | if (isInstructionTriviallyDead(I)) { |
| 318 | std::vector<Value*> Operands(I->op_begin(), I->op_end()); |
| 319 | I->getParent()->getInstList().erase(I); |
| 320 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 321 | if (Instruction *OpI = dyn_cast<Instruction>(Operands[i])) |
| 322 | ErasePossiblyDeadInstructionTree(OpI); |
| 323 | } |
| 324 | } |
| 325 | |
Chris Lattner | a2ab489 | 2004-02-24 07:23:58 +0000 | [diff] [blame] | 326 | /// SafeToMergeTerminators - Return true if it is safe to merge these two |
| 327 | /// terminator instructions together. |
| 328 | /// |
| 329 | static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) { |
| 330 | if (SI1 == SI2) return false; // Can't merge with self! |
| 331 | |
| 332 | // It is not safe to merge these two switch instructions if they have a common |
| 333 | // successor, and if that successor has a PHI node, and if that PHI node has |
| 334 | // conflicting incoming values from the two switch blocks. |
| 335 | BasicBlock *SI1BB = SI1->getParent(); |
| 336 | BasicBlock *SI2BB = SI2->getParent(); |
| 337 | std::set<BasicBlock*> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB)); |
| 338 | |
| 339 | for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I) |
| 340 | if (SI1Succs.count(*I)) |
| 341 | for (BasicBlock::iterator BBI = (*I)->begin(); |
| 342 | PHINode *PN = dyn_cast<PHINode>(BBI); ++BBI) |
| 343 | if (PN->getIncomingValueForBlock(SI1BB) != |
| 344 | PN->getIncomingValueForBlock(SI2BB)) |
| 345 | return false; |
| 346 | |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | /// AddPredecessorToBlock - Update PHI nodes in Succ to indicate that there will |
| 351 | /// now be entries in it from the 'NewPred' block. The values that will be |
| 352 | /// flowing into the PHI nodes will be the same as those coming in from |
| 353 | /// ExistPred, and existing predecessor of Succ. |
| 354 | static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred, |
| 355 | BasicBlock *ExistPred) { |
| 356 | assert(std::find(succ_begin(ExistPred), succ_end(ExistPred), Succ) != |
| 357 | succ_end(ExistPred) && "ExistPred is not a predecessor of Succ!"); |
| 358 | if (!isa<PHINode>(Succ->begin())) return; // Quick exit if nothing to do |
| 359 | |
| 360 | for (BasicBlock::iterator I = Succ->begin(); |
| 361 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 362 | Value *V = PN->getIncomingValueForBlock(ExistPred); |
| 363 | PN->addIncoming(V, NewPred); |
| 364 | } |
| 365 | } |
| 366 | |
Chris Lattner | d3e6ae2 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 367 | // isValueEqualityComparison - Return true if the specified terminator checks to |
| 368 | // see if a value is equal to constant integer value. |
| 369 | static Value *isValueEqualityComparison(TerminatorInst *TI) { |
Chris Lattner | a64923a | 2004-03-16 19:45:22 +0000 | [diff] [blame] | 370 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 371 | // Do not permit merging of large switch instructions into their |
| 372 | // predecessors unless there is only one predecessor. |
| 373 | if (SI->getNumSuccessors() * std::distance(pred_begin(SI->getParent()), |
| 374 | pred_end(SI->getParent())) > 128) |
| 375 | return 0; |
| 376 | |
Chris Lattner | d3e6ae2 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 377 | return SI->getCondition(); |
Chris Lattner | a64923a | 2004-03-16 19:45:22 +0000 | [diff] [blame] | 378 | } |
Chris Lattner | d3e6ae2 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 379 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) |
| 380 | if (BI->isConditional() && BI->getCondition()->hasOneUse()) |
| 381 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(BI->getCondition())) |
| 382 | if ((SCI->getOpcode() == Instruction::SetEQ || |
| 383 | SCI->getOpcode() == Instruction::SetNE) && |
| 384 | isa<ConstantInt>(SCI->getOperand(1))) |
| 385 | return SCI->getOperand(0); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | // Given a value comparison instruction, decode all of the 'cases' that it |
| 390 | // represents and return the 'default' block. |
| 391 | static BasicBlock * |
| 392 | GetValueEqualityComparisonCases(TerminatorInst *TI, |
| 393 | std::vector<std::pair<ConstantInt*, |
| 394 | BasicBlock*> > &Cases) { |
| 395 | if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 396 | Cases.reserve(SI->getNumCases()); |
| 397 | for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i) |
| 398 | Cases.push_back(std::make_pair(cast<ConstantInt>(SI->getCaseValue(i)), |
| 399 | SI->getSuccessor(i))); |
| 400 | return SI->getDefaultDest(); |
| 401 | } |
| 402 | |
| 403 | BranchInst *BI = cast<BranchInst>(TI); |
| 404 | SetCondInst *SCI = cast<SetCondInst>(BI->getCondition()); |
| 405 | Cases.push_back(std::make_pair(cast<ConstantInt>(SCI->getOperand(1)), |
| 406 | BI->getSuccessor(SCI->getOpcode() == |
| 407 | Instruction::SetNE))); |
| 408 | return BI->getSuccessor(SCI->getOpcode() == Instruction::SetEQ); |
| 409 | } |
| 410 | |
| 411 | |
| 412 | // FoldValueComparisonIntoPredecessors - The specified terminator is a value |
| 413 | // equality comparison instruction (either a switch or a branch on "X == c"). |
| 414 | // See if any of the predecessors of the terminator block are value comparisons |
| 415 | // on the same value. If so, and if safe to do so, fold them together. |
| 416 | static bool FoldValueComparisonIntoPredecessors(TerminatorInst *TI) { |
| 417 | BasicBlock *BB = TI->getParent(); |
| 418 | Value *CV = isValueEqualityComparison(TI); // CondVal |
| 419 | assert(CV && "Not a comparison?"); |
| 420 | bool Changed = false; |
| 421 | |
| 422 | std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB)); |
| 423 | while (!Preds.empty()) { |
| 424 | BasicBlock *Pred = Preds.back(); |
| 425 | Preds.pop_back(); |
| 426 | |
| 427 | // See if the predecessor is a comparison with the same value. |
| 428 | TerminatorInst *PTI = Pred->getTerminator(); |
| 429 | Value *PCV = isValueEqualityComparison(PTI); // PredCondVal |
| 430 | |
| 431 | if (PCV == CV && SafeToMergeTerminators(TI, PTI)) { |
| 432 | // Figure out which 'cases' to copy from SI to PSI. |
| 433 | std::vector<std::pair<ConstantInt*, BasicBlock*> > BBCases; |
| 434 | BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases); |
| 435 | |
| 436 | std::vector<std::pair<ConstantInt*, BasicBlock*> > PredCases; |
| 437 | BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases); |
| 438 | |
| 439 | // Based on whether the default edge from PTI goes to BB or not, fill in |
| 440 | // PredCases and PredDefault with the new switch cases we would like to |
| 441 | // build. |
| 442 | std::vector<BasicBlock*> NewSuccessors; |
| 443 | |
| 444 | if (PredDefault == BB) { |
| 445 | // If this is the default destination from PTI, only the edges in TI |
| 446 | // that don't occur in PTI, or that branch to BB will be activated. |
| 447 | std::set<ConstantInt*> PTIHandled; |
| 448 | for (unsigned i = 0, e = PredCases.size(); i != e; ++i) |
| 449 | if (PredCases[i].second != BB) |
| 450 | PTIHandled.insert(PredCases[i].first); |
| 451 | else { |
| 452 | // The default destination is BB, we don't need explicit targets. |
| 453 | std::swap(PredCases[i], PredCases.back()); |
| 454 | PredCases.pop_back(); |
| 455 | --i; --e; |
| 456 | } |
| 457 | |
| 458 | // Reconstruct the new switch statement we will be building. |
| 459 | if (PredDefault != BBDefault) { |
| 460 | PredDefault->removePredecessor(Pred); |
| 461 | PredDefault = BBDefault; |
| 462 | NewSuccessors.push_back(BBDefault); |
| 463 | } |
| 464 | for (unsigned i = 0, e = BBCases.size(); i != e; ++i) |
| 465 | if (!PTIHandled.count(BBCases[i].first) && |
| 466 | BBCases[i].second != BBDefault) { |
| 467 | PredCases.push_back(BBCases[i]); |
| 468 | NewSuccessors.push_back(BBCases[i].second); |
| 469 | } |
| 470 | |
| 471 | } else { |
| 472 | // If this is not the default destination from PSI, only the edges |
| 473 | // in SI that occur in PSI with a destination of BB will be |
| 474 | // activated. |
| 475 | std::set<ConstantInt*> PTIHandled; |
| 476 | for (unsigned i = 0, e = PredCases.size(); i != e; ++i) |
| 477 | if (PredCases[i].second == BB) { |
| 478 | PTIHandled.insert(PredCases[i].first); |
| 479 | std::swap(PredCases[i], PredCases.back()); |
| 480 | PredCases.pop_back(); |
| 481 | --i; --e; |
| 482 | } |
| 483 | |
| 484 | // Okay, now we know which constants were sent to BB from the |
| 485 | // predecessor. Figure out where they will all go now. |
| 486 | for (unsigned i = 0, e = BBCases.size(); i != e; ++i) |
| 487 | if (PTIHandled.count(BBCases[i].first)) { |
| 488 | // If this is one we are capable of getting... |
| 489 | PredCases.push_back(BBCases[i]); |
| 490 | NewSuccessors.push_back(BBCases[i].second); |
| 491 | PTIHandled.erase(BBCases[i].first);// This constant is taken care of |
| 492 | } |
| 493 | |
| 494 | // If there are any constants vectored to BB that TI doesn't handle, |
| 495 | // they must go to the default destination of TI. |
| 496 | for (std::set<ConstantInt*>::iterator I = PTIHandled.begin(), |
| 497 | E = PTIHandled.end(); I != E; ++I) { |
| 498 | PredCases.push_back(std::make_pair(*I, BBDefault)); |
| 499 | NewSuccessors.push_back(BBDefault); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // Okay, at this point, we know which new successor Pred will get. Make |
| 504 | // sure we update the number of entries in the PHI nodes for these |
| 505 | // successors. |
| 506 | for (unsigned i = 0, e = NewSuccessors.size(); i != e; ++i) |
| 507 | AddPredecessorToBlock(NewSuccessors[i], Pred, BB); |
| 508 | |
| 509 | // Now that the successors are updated, create the new Switch instruction. |
| 510 | SwitchInst *NewSI = new SwitchInst(CV, PredDefault, PTI); |
| 511 | for (unsigned i = 0, e = PredCases.size(); i != e; ++i) |
| 512 | NewSI->addCase(PredCases[i].first, PredCases[i].second); |
| 513 | Pred->getInstList().erase(PTI); |
| 514 | |
| 515 | // Okay, last check. If BB is still a successor of PSI, then we must |
| 516 | // have an infinite loop case. If so, add an infinitely looping block |
| 517 | // to handle the case to preserve the behavior of the code. |
| 518 | BasicBlock *InfLoopBlock = 0; |
| 519 | for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i) |
| 520 | if (NewSI->getSuccessor(i) == BB) { |
| 521 | if (InfLoopBlock == 0) { |
| 522 | // Insert it at the end of the loop, because it's either code, |
| 523 | // or it won't matter if it's hot. :) |
| 524 | InfLoopBlock = new BasicBlock("infloop", BB->getParent()); |
| 525 | new BranchInst(InfLoopBlock, InfLoopBlock); |
| 526 | } |
| 527 | NewSI->setSuccessor(i, InfLoopBlock); |
| 528 | } |
| 529 | |
| 530 | Changed = true; |
| 531 | } |
| 532 | } |
| 533 | return Changed; |
| 534 | } |
| 535 | |
Chris Lattner | b2b151d | 2004-06-19 07:02:14 +0000 | [diff] [blame^] | 536 | namespace { |
| 537 | /// ConstantIntOrdering - This class implements a stable ordering of constant |
| 538 | /// integers that does not depend on their address. This is important for |
| 539 | /// applications that sort ConstantInt's to ensure uniqueness. |
| 540 | struct ConstantIntOrdering { |
| 541 | bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const { |
| 542 | return LHS->getRawValue() < RHS->getRawValue(); |
| 543 | } |
| 544 | }; |
| 545 | } |
| 546 | |
Chris Lattner | d3e6ae2 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 547 | |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 548 | // SimplifyCFG - This function is used to do simplification of a CFG. For |
| 549 | // example, it adjusts branches to branches to eliminate the extra hop, it |
| 550 | // eliminates unreachable basic blocks, and does other "peephole" optimization |
Chris Lattner | 31116ba | 2003-03-05 21:01:52 +0000 | [diff] [blame] | 551 | // of the CFG. It returns true if a modification was made. |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 552 | // |
| 553 | // WARNING: The entry node of a function may not be simplified. |
| 554 | // |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 555 | bool llvm::SimplifyCFG(BasicBlock *BB) { |
Chris Lattner | 3f5823f | 2003-08-24 18:36:16 +0000 | [diff] [blame] | 556 | bool Changed = false; |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 557 | Function *M = BB->getParent(); |
| 558 | |
| 559 | assert(BB && BB->getParent() && "Block not embedded in function!"); |
| 560 | assert(BB->getTerminator() && "Degenerate basic block encountered!"); |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 561 | assert(&BB->getParent()->front() != BB && "Can't Simplify entry block!"); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 563 | // Remove basic blocks that have no predecessors... which are unreachable. |
Chris Lattner | a2ab489 | 2004-02-24 07:23:58 +0000 | [diff] [blame] | 564 | if (pred_begin(BB) == pred_end(BB) || |
| 565 | *pred_begin(BB) == BB && ++pred_begin(BB) == pred_end(BB)) { |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 566 | //cerr << "Removing BB: \n" << BB; |
| 567 | |
| 568 | // Loop through all of our successors and make sure they know that one |
| 569 | // of their predecessors is going away. |
| 570 | for_each(succ_begin(BB), succ_end(BB), |
| 571 | std::bind2nd(std::mem_fun(&BasicBlock::removePredecessor), BB)); |
| 572 | |
| 573 | while (!BB->empty()) { |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 574 | Instruction &I = BB->back(); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 575 | // If this instruction is used, replace uses with an arbitrary |
| 576 | // constant value. Because control flow can't get here, we don't care |
| 577 | // what we replace the value with. Note that since this block is |
| 578 | // unreachable, and all values contained within it must dominate their |
| 579 | // uses, that all uses will eventually be removed. |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 580 | if (!I.use_empty()) |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 581 | // Make all users of this instruction reference the constant instead |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 582 | I.replaceAllUsesWith(Constant::getNullValue(I.getType())); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 583 | |
| 584 | // Remove the instruction from the basic block |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 585 | BB->getInstList().pop_back(); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 586 | } |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 587 | M->getBasicBlockList().erase(BB); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 588 | return true; |
| 589 | } |
| 590 | |
Chris Lattner | 031340a | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 591 | // Check to see if we can constant propagate this terminator instruction |
| 592 | // away... |
Chris Lattner | 3f5823f | 2003-08-24 18:36:16 +0000 | [diff] [blame] | 593 | Changed |= ConstantFoldTerminator(BB); |
Chris Lattner | 031340a | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 594 | |
Chris Lattner | e54d214 | 2003-03-05 21:36:33 +0000 | [diff] [blame] | 595 | // Check to see if this block has no non-phi instructions and only a single |
| 596 | // successor. If so, replace references to this basic block with references |
| 597 | // to the successor. |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 598 | succ_iterator SI(succ_begin(BB)); |
| 599 | if (SI != succ_end(BB) && ++SI == succ_end(BB)) { // One succ? |
Chris Lattner | e54d214 | 2003-03-05 21:36:33 +0000 | [diff] [blame] | 600 | |
| 601 | BasicBlock::iterator BBI = BB->begin(); // Skip over phi nodes... |
| 602 | while (isa<PHINode>(*BBI)) ++BBI; |
| 603 | |
| 604 | if (BBI->isTerminator()) { // Terminator is the only non-phi instruction! |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 605 | BasicBlock *Succ = *succ_begin(BB); // There is exactly one successor |
| 606 | |
| 607 | if (Succ != BB) { // Arg, don't hurt infinite loops! |
| 608 | // If our successor has PHI nodes, then we need to update them to |
| 609 | // include entries for BB's predecessors, not for BB itself. |
| 610 | // Be careful though, if this transformation fails (returns true) then |
| 611 | // we cannot do this transformation! |
| 612 | // |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 613 | if (!PropagatePredecessorsForPHIs(BB, Succ)) { |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 614 | //cerr << "Killing Trivial BB: \n" << BB; |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 615 | std::string OldName = BB->getName(); |
| 616 | |
Chris Lattner | 569a57f | 2003-03-07 18:13:41 +0000 | [diff] [blame] | 617 | std::vector<BasicBlock*> |
| 618 | OldSuccPreds(pred_begin(Succ), pred_end(Succ)); |
| 619 | |
Chris Lattner | e54d214 | 2003-03-05 21:36:33 +0000 | [diff] [blame] | 620 | // Move all PHI nodes in BB to Succ if they are alive, otherwise |
| 621 | // delete them. |
| 622 | while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) |
| 623 | if (PN->use_empty()) |
| 624 | BB->getInstList().erase(BB->begin()); // Nuke instruction... |
| 625 | else { |
| 626 | // The instruction is alive, so this means that Succ must have |
| 627 | // *ONLY* had BB as a predecessor, and the PHI node is still valid |
Chris Lattner | 569a57f | 2003-03-07 18:13:41 +0000 | [diff] [blame] | 628 | // now. Simply move it into Succ, because we know that BB |
| 629 | // strictly dominated Succ. |
Chris Lattner | e54d214 | 2003-03-05 21:36:33 +0000 | [diff] [blame] | 630 | BB->getInstList().remove(BB->begin()); |
| 631 | Succ->getInstList().push_front(PN); |
Chris Lattner | 569a57f | 2003-03-07 18:13:41 +0000 | [diff] [blame] | 632 | |
| 633 | // We need to add new entries for the PHI node to account for |
| 634 | // predecessors of Succ that the PHI node does not take into |
| 635 | // account. At this point, since we know that BB dominated succ, |
| 636 | // this means that we should any newly added incoming edges should |
| 637 | // use the PHI node as the value for these edges, because they are |
| 638 | // loop back edges. |
| 639 | |
| 640 | for (unsigned i = 0, e = OldSuccPreds.size(); i != e; ++i) |
| 641 | if (OldSuccPreds[i] != BB) |
| 642 | PN->addIncoming(PN, OldSuccPreds[i]); |
Chris Lattner | e54d214 | 2003-03-05 21:36:33 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Chris Lattner | 569a57f | 2003-03-07 18:13:41 +0000 | [diff] [blame] | 645 | // Everything that jumped to BB now goes to Succ... |
| 646 | BB->replaceAllUsesWith(Succ); |
| 647 | |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 648 | // Delete the old basic block... |
| 649 | M->getBasicBlockList().erase(BB); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 650 | |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 651 | if (!OldName.empty() && !Succ->hasName()) // Transfer name if we can |
| 652 | Succ->setName(OldName); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 653 | |
| 654 | //cerr << "Function after removal: \n" << M; |
| 655 | return true; |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
Chris Lattner | e42732e | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 661 | // If this is a returning block with only PHI nodes in it, fold the return |
| 662 | // instruction into any unconditional branch predecessors. |
Chris Lattner | 9f0db32 | 2004-04-02 18:13:43 +0000 | [diff] [blame] | 663 | // |
| 664 | // If any predecessor is a conditional branch that just selects among |
| 665 | // different return values, fold the replace the branch/return with a select |
| 666 | // and return. |
Chris Lattner | e42732e | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 667 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
| 668 | BasicBlock::iterator BBI = BB->getTerminator(); |
| 669 | if (BBI == BB->begin() || isa<PHINode>(--BBI)) { |
Chris Lattner | 9f0db32 | 2004-04-02 18:13:43 +0000 | [diff] [blame] | 670 | // Find predecessors that end with branches. |
Chris Lattner | e42732e | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 671 | std::vector<BasicBlock*> UncondBranchPreds; |
Chris Lattner | 9f0db32 | 2004-04-02 18:13:43 +0000 | [diff] [blame] | 672 | std::vector<BranchInst*> CondBranchPreds; |
Chris Lattner | e42732e | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 673 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { |
| 674 | TerminatorInst *PTI = (*PI)->getTerminator(); |
| 675 | if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) |
| 676 | if (BI->isUnconditional()) |
| 677 | UncondBranchPreds.push_back(*PI); |
Chris Lattner | 9f0db32 | 2004-04-02 18:13:43 +0000 | [diff] [blame] | 678 | else |
| 679 | CondBranchPreds.push_back(BI); |
Chris Lattner | e42732e | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | // If we found some, do the transformation! |
| 683 | if (!UncondBranchPreds.empty()) { |
| 684 | while (!UncondBranchPreds.empty()) { |
| 685 | BasicBlock *Pred = UncondBranchPreds.back(); |
| 686 | UncondBranchPreds.pop_back(); |
| 687 | Instruction *UncondBranch = Pred->getTerminator(); |
| 688 | // Clone the return and add it to the end of the predecessor. |
| 689 | Instruction *NewRet = RI->clone(); |
| 690 | Pred->getInstList().push_back(NewRet); |
| 691 | |
| 692 | // If the return instruction returns a value, and if the value was a |
| 693 | // PHI node in "BB", propagate the right value into the return. |
| 694 | if (NewRet->getNumOperands() == 1) |
| 695 | if (PHINode *PN = dyn_cast<PHINode>(NewRet->getOperand(0))) |
| 696 | if (PN->getParent() == BB) |
| 697 | NewRet->setOperand(0, PN->getIncomingValueForBlock(Pred)); |
| 698 | // Update any PHI nodes in the returning block to realize that we no |
| 699 | // longer branch to them. |
| 700 | BB->removePredecessor(Pred); |
| 701 | Pred->getInstList().erase(UncondBranch); |
| 702 | } |
| 703 | |
| 704 | // If we eliminated all predecessors of the block, delete the block now. |
| 705 | if (pred_begin(BB) == pred_end(BB)) |
| 706 | // We know there are no successors, so just nuke the block. |
| 707 | M->getBasicBlockList().erase(BB); |
| 708 | |
Chris Lattner | e42732e | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 709 | return true; |
| 710 | } |
Chris Lattner | 9f0db32 | 2004-04-02 18:13:43 +0000 | [diff] [blame] | 711 | |
| 712 | // Check out all of the conditional branches going to this return |
| 713 | // instruction. If any of them just select between returns, change the |
| 714 | // branch itself into a select/return pair. |
| 715 | while (!CondBranchPreds.empty()) { |
| 716 | BranchInst *BI = CondBranchPreds.back(); |
| 717 | CondBranchPreds.pop_back(); |
| 718 | BasicBlock *TrueSucc = BI->getSuccessor(0); |
| 719 | BasicBlock *FalseSucc = BI->getSuccessor(1); |
| 720 | BasicBlock *OtherSucc = TrueSucc == BB ? FalseSucc : TrueSucc; |
| 721 | |
| 722 | // Check to see if the non-BB successor is also a return block. |
| 723 | if (isa<ReturnInst>(OtherSucc->getTerminator())) { |
| 724 | // Check to see if there are only PHI instructions in this block. |
| 725 | BasicBlock::iterator OSI = OtherSucc->getTerminator(); |
| 726 | if (OSI == OtherSucc->begin() || isa<PHINode>(--OSI)) { |
| 727 | // Okay, we found a branch that is going to two return nodes. If |
| 728 | // there is no return value for this function, just change the |
| 729 | // branch into a return. |
| 730 | if (RI->getNumOperands() == 0) { |
| 731 | TrueSucc->removePredecessor(BI->getParent()); |
| 732 | FalseSucc->removePredecessor(BI->getParent()); |
| 733 | new ReturnInst(0, BI); |
| 734 | BI->getParent()->getInstList().erase(BI); |
| 735 | return true; |
| 736 | } |
| 737 | |
| 738 | // Otherwise, figure out what the true and false return values are |
| 739 | // so we can insert a new select instruction. |
| 740 | Value *TrueValue = TrueSucc->getTerminator()->getOperand(0); |
| 741 | Value *FalseValue = FalseSucc->getTerminator()->getOperand(0); |
| 742 | |
| 743 | // Unwrap any PHI nodes in the return blocks. |
| 744 | if (PHINode *TVPN = dyn_cast<PHINode>(TrueValue)) |
| 745 | if (TVPN->getParent() == TrueSucc) |
| 746 | TrueValue = TVPN->getIncomingValueForBlock(BI->getParent()); |
| 747 | if (PHINode *FVPN = dyn_cast<PHINode>(FalseValue)) |
| 748 | if (FVPN->getParent() == FalseSucc) |
| 749 | FalseValue = FVPN->getIncomingValueForBlock(BI->getParent()); |
| 750 | |
Chris Lattner | eed034b | 2004-04-02 18:15:10 +0000 | [diff] [blame] | 751 | TrueSucc->removePredecessor(BI->getParent()); |
| 752 | FalseSucc->removePredecessor(BI->getParent()); |
| 753 | |
Chris Lattner | 9f0db32 | 2004-04-02 18:13:43 +0000 | [diff] [blame] | 754 | // Insert a new select instruction. |
| 755 | Value *NewRetVal = new SelectInst(BI->getCondition(), TrueValue, |
| 756 | FalseValue, "retval", BI); |
| 757 | new ReturnInst(NewRetVal, BI); |
| 758 | BI->getParent()->getInstList().erase(BI); |
| 759 | return true; |
| 760 | } |
| 761 | } |
| 762 | } |
Chris Lattner | e42732e | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 763 | } |
Chris Lattner | 3cd98f0 | 2004-02-24 05:54:22 +0000 | [diff] [blame] | 764 | } else if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->begin())) { |
| 765 | // Check to see if the first instruction in this block is just an unwind. |
| 766 | // If so, replace any invoke instructions which use this as an exception |
| 767 | // destination with call instructions. |
| 768 | // |
| 769 | std::vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB)); |
| 770 | while (!Preds.empty()) { |
| 771 | BasicBlock *Pred = Preds.back(); |
| 772 | if (InvokeInst *II = dyn_cast<InvokeInst>(Pred->getTerminator())) |
| 773 | if (II->getUnwindDest() == BB) { |
| 774 | // Insert a new branch instruction before the invoke, because this |
| 775 | // is now a fall through... |
| 776 | BranchInst *BI = new BranchInst(II->getNormalDest(), II); |
| 777 | Pred->getInstList().remove(II); // Take out of symbol table |
| 778 | |
| 779 | // Insert the call now... |
| 780 | std::vector<Value*> Args(II->op_begin()+3, II->op_end()); |
| 781 | CallInst *CI = new CallInst(II->getCalledValue(), Args, |
| 782 | II->getName(), BI); |
| 783 | // If the invoke produced a value, the Call now does instead |
| 784 | II->replaceAllUsesWith(CI); |
| 785 | delete II; |
| 786 | Changed = true; |
| 787 | } |
| 788 | |
| 789 | Preds.pop_back(); |
| 790 | } |
Chris Lattner | 90ea78e | 2004-02-24 16:09:21 +0000 | [diff] [blame] | 791 | |
| 792 | // If this block is now dead, remove it. |
| 793 | if (pred_begin(BB) == pred_end(BB)) { |
| 794 | // We know there are no successors, so just nuke the block. |
| 795 | M->getBasicBlockList().erase(BB); |
| 796 | return true; |
| 797 | } |
| 798 | |
Chris Lattner | a2ab489 | 2004-02-24 07:23:58 +0000 | [diff] [blame] | 799 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->begin())) { |
Chris Lattner | a078f47 | 2004-03-17 02:02:47 +0000 | [diff] [blame] | 800 | if (isValueEqualityComparison(SI)) |
| 801 | if (FoldValueComparisonIntoPredecessors(SI)) |
| 802 | return SimplifyCFG(BB) || 1; |
Chris Lattner | d3e6ae2 | 2004-02-28 21:28:10 +0000 | [diff] [blame] | 803 | } else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) { |
Chris Lattner | 88da6f7 | 2004-05-01 22:36:37 +0000 | [diff] [blame] | 804 | if (BI->isConditional()) { |
Chris Lattner | 2e93c42 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 805 | if (Value *CompVal = isValueEqualityComparison(BI)) { |
| 806 | // This block must be empty, except for the setcond inst, if it exists. |
| 807 | BasicBlock::iterator I = BB->begin(); |
| 808 | if (&*I == BI || |
| 809 | (&*I == cast<Instruction>(BI->getCondition()) && |
| 810 | &*++I == BI)) |
| 811 | if (FoldValueComparisonIntoPredecessors(BI)) |
| 812 | return SimplifyCFG(BB) | true; |
| 813 | } |
| 814 | |
| 815 | // If this basic block is ONLY a setcc and a branch, and if a predecessor |
| 816 | // branches to us and one of our successors, fold the setcc into the |
| 817 | // predecessor and use logical operations to pick the right destination. |
Chris Lattner | 51a6dbc | 2004-05-02 05:02:03 +0000 | [diff] [blame] | 818 | BasicBlock *TrueDest = BI->getSuccessor(0); |
| 819 | BasicBlock *FalseDest = BI->getSuccessor(1); |
Chris Lattner | be6f068 | 2004-05-02 05:19:36 +0000 | [diff] [blame] | 820 | if (BinaryOperator *Cond = dyn_cast<BinaryOperator>(BI->getCondition())) |
Chris Lattner | 2e93c42 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 821 | if (Cond->getParent() == BB && &BB->front() == Cond && |
Chris Lattner | 51a6dbc | 2004-05-02 05:02:03 +0000 | [diff] [blame] | 822 | Cond->getNext() == BI && Cond->hasOneUse() && |
| 823 | TrueDest != BB && FalseDest != BB) |
Chris Lattner | 2e93c42 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 824 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI!=E; ++PI) |
| 825 | if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator())) |
Chris Lattner | 1e94ed6 | 2004-05-02 01:00:44 +0000 | [diff] [blame] | 826 | if (PBI->isConditional() && SafeToMergeTerminators(BI, PBI)) { |
Chris Lattner | 2e93c42 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 827 | if (PBI->getSuccessor(0) == FalseDest || |
| 828 | PBI->getSuccessor(1) == TrueDest) { |
| 829 | // Invert the predecessors condition test (xor it with true), |
| 830 | // which allows us to write this code once. |
| 831 | Value *NewCond = |
| 832 | BinaryOperator::createNot(PBI->getCondition(), |
| 833 | PBI->getCondition()->getName()+".not", PBI); |
| 834 | PBI->setCondition(NewCond); |
| 835 | BasicBlock *OldTrue = PBI->getSuccessor(0); |
| 836 | BasicBlock *OldFalse = PBI->getSuccessor(1); |
| 837 | PBI->setSuccessor(0, OldFalse); |
| 838 | PBI->setSuccessor(1, OldTrue); |
| 839 | } |
| 840 | |
| 841 | if (PBI->getSuccessor(0) == TrueDest || |
| 842 | PBI->getSuccessor(1) == FalseDest) { |
| 843 | // Clone Cond into the predecessor basic block, and and the |
| 844 | // two conditions together. |
| 845 | Instruction *New = Cond->clone(); |
| 846 | New->setName(Cond->getName()); |
| 847 | Cond->setName(Cond->getName()+".old"); |
| 848 | (*PI)->getInstList().insert(PBI, New); |
| 849 | Instruction::BinaryOps Opcode = |
| 850 | PBI->getSuccessor(0) == TrueDest ? |
| 851 | Instruction::Or : Instruction::And; |
| 852 | Value *NewCond = |
| 853 | BinaryOperator::create(Opcode, PBI->getCondition(), |
| 854 | New, "bothcond", PBI); |
| 855 | PBI->setCondition(NewCond); |
| 856 | if (PBI->getSuccessor(0) == BB) { |
| 857 | AddPredecessorToBlock(TrueDest, *PI, BB); |
| 858 | PBI->setSuccessor(0, TrueDest); |
| 859 | } |
| 860 | if (PBI->getSuccessor(1) == BB) { |
| 861 | AddPredecessorToBlock(FalseDest, *PI, BB); |
| 862 | PBI->setSuccessor(1, FalseDest); |
| 863 | } |
| 864 | return SimplifyCFG(BB) | 1; |
| 865 | } |
| 866 | } |
Chris Lattner | 2e93c42 | 2004-05-01 23:35:43 +0000 | [diff] [blame] | 867 | |
Chris Lattner | 88da6f7 | 2004-05-01 22:36:37 +0000 | [diff] [blame] | 868 | // If this block ends with a branch instruction, and if there is one |
| 869 | // predecessor, see if the previous block ended with a branch on the same |
| 870 | // condition, which makes this conditional branch redundant. |
| 871 | pred_iterator PI(pred_begin(BB)), PE(pred_end(BB)); |
| 872 | BasicBlock *OnlyPred = *PI++; |
| 873 | for (; PI != PE; ++PI)// Search all predecessors, see if they are all same |
| 874 | if (*PI != OnlyPred) { |
| 875 | OnlyPred = 0; // There are multiple different predecessors... |
| 876 | break; |
| 877 | } |
| 878 | |
| 879 | if (OnlyPred) |
| 880 | if (BranchInst *PBI = dyn_cast<BranchInst>(OnlyPred->getTerminator())) |
| 881 | if (PBI->isConditional() && |
| 882 | PBI->getCondition() == BI->getCondition() && |
Chris Lattner | 4cbd160 | 2004-05-01 22:41:51 +0000 | [diff] [blame] | 883 | (PBI->getSuccessor(0) != BB || PBI->getSuccessor(1) != BB)) { |
Chris Lattner | 88da6f7 | 2004-05-01 22:36:37 +0000 | [diff] [blame] | 884 | // Okay, the outcome of this conditional branch is statically |
| 885 | // knowable. Delete the outgoing CFG edge that is impossible to |
| 886 | // execute. |
| 887 | bool CondIsTrue = PBI->getSuccessor(0) == BB; |
| 888 | BI->getSuccessor(CondIsTrue)->removePredecessor(BB); |
| 889 | new BranchInst(BI->getSuccessor(!CondIsTrue), BB); |
| 890 | BB->getInstList().erase(BI); |
| 891 | return SimplifyCFG(BB) | true; |
| 892 | } |
Chris Lattner | a2ab489 | 2004-02-24 07:23:58 +0000 | [diff] [blame] | 893 | } |
Chris Lattner | e42732e | 2004-02-16 06:35:48 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 896 | // Merge basic blocks into their predecessor if there is only one distinct |
| 897 | // pred, and if there is only one distinct successor of the predecessor, and |
| 898 | // if there are no PHI nodes. |
| 899 | // |
Chris Lattner | 838b845 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 900 | pred_iterator PI(pred_begin(BB)), PE(pred_end(BB)); |
| 901 | BasicBlock *OnlyPred = *PI++; |
| 902 | for (; PI != PE; ++PI) // Search all predecessors, see if they are all same |
| 903 | if (*PI != OnlyPred) { |
| 904 | OnlyPred = 0; // There are multiple different predecessors... |
| 905 | break; |
| 906 | } |
Chris Lattner | 88da6f7 | 2004-05-01 22:36:37 +0000 | [diff] [blame] | 907 | |
Chris Lattner | 838b845 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 908 | BasicBlock *OnlySucc = 0; |
| 909 | if (OnlyPred && OnlyPred != BB && // Don't break self loops |
| 910 | OnlyPred->getTerminator()->getOpcode() != Instruction::Invoke) { |
| 911 | // Check to see if there is only one distinct successor... |
| 912 | succ_iterator SI(succ_begin(OnlyPred)), SE(succ_end(OnlyPred)); |
| 913 | OnlySucc = BB; |
| 914 | for (; SI != SE; ++SI) |
| 915 | if (*SI != OnlySucc) { |
| 916 | OnlySucc = 0; // There are multiple distinct successors! |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 917 | break; |
| 918 | } |
Chris Lattner | 838b845 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | if (OnlySucc) { |
| 922 | //cerr << "Merging: " << BB << "into: " << OnlyPred; |
| 923 | TerminatorInst *Term = OnlyPred->getTerminator(); |
| 924 | |
| 925 | // Resolve any PHI nodes at the start of the block. They are all |
| 926 | // guaranteed to have exactly one entry if they exist, unless there are |
| 927 | // multiple duplicate (but guaranteed to be equal) entries for the |
| 928 | // incoming edges. This occurs when there are multiple edges from |
| 929 | // OnlyPred to OnlySucc. |
| 930 | // |
| 931 | while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { |
| 932 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 933 | BB->getInstList().pop_front(); // Delete the phi node... |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Chris Lattner | 838b845 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 936 | // Delete the unconditional branch from the predecessor... |
| 937 | OnlyPred->getInstList().pop_back(); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 938 | |
Chris Lattner | 838b845 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 939 | // Move all definitions in the successor to the predecessor... |
| 940 | OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList()); |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 941 | |
Chris Lattner | 838b845 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 942 | // Make all PHI nodes that referred to BB now refer to Pred as their |
| 943 | // source... |
| 944 | BB->replaceAllUsesWith(OnlyPred); |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 945 | |
Chris Lattner | 838b845 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 946 | std::string OldName = BB->getName(); |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 947 | |
Chris Lattner | 838b845 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 948 | // Erase basic block from the function... |
| 949 | M->getBasicBlockList().erase(BB); |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 950 | |
Chris Lattner | 838b845 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 951 | // Inherit predecessors name if it exists... |
| 952 | if (!OldName.empty() && !OnlyPred->hasName()) |
| 953 | OnlyPred->setName(OldName); |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 954 | |
Chris Lattner | 838b845 | 2004-02-11 01:17:07 +0000 | [diff] [blame] | 955 | return true; |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 956 | } |
Chris Lattner | 18d1f19 | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 957 | |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 958 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 959 | if (BranchInst *BI = dyn_cast<BranchInst>((*PI)->getTerminator())) |
| 960 | // Change br (X == 0 | X == 1), T, F into a switch instruction. |
| 961 | if (BI->isConditional() && isa<Instruction>(BI->getCondition())) { |
| 962 | Instruction *Cond = cast<Instruction>(BI->getCondition()); |
| 963 | // If this is a bunch of seteq's or'd together, or if it's a bunch of |
| 964 | // 'setne's and'ed together, collect them. |
| 965 | Value *CompVal = 0; |
Chris Lattner | b2b151d | 2004-06-19 07:02:14 +0000 | [diff] [blame^] | 966 | std::vector<ConstantInt*> Values; |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 967 | bool TrueWhenEqual = GatherValueComparisons(Cond, CompVal, Values); |
| 968 | if (CompVal && CompVal->getType()->isInteger()) { |
| 969 | // There might be duplicate constants in the list, which the switch |
| 970 | // instruction can't handle, remove them now. |
Chris Lattner | b2b151d | 2004-06-19 07:02:14 +0000 | [diff] [blame^] | 971 | std::sort(Values.begin(), Values.end(), ConstantIntOrdering()); |
Chris Lattner | 6f4b45a | 2004-02-24 05:38:11 +0000 | [diff] [blame] | 972 | Values.erase(std::unique(Values.begin(), Values.end()), Values.end()); |
| 973 | |
| 974 | // Figure out which block is which destination. |
| 975 | BasicBlock *DefaultBB = BI->getSuccessor(1); |
| 976 | BasicBlock *EdgeBB = BI->getSuccessor(0); |
| 977 | if (!TrueWhenEqual) std::swap(DefaultBB, EdgeBB); |
| 978 | |
| 979 | // Create the new switch instruction now. |
| 980 | SwitchInst *New = new SwitchInst(CompVal, DefaultBB, BI); |
| 981 | |
| 982 | // Add all of the 'cases' to the switch instruction. |
| 983 | for (unsigned i = 0, e = Values.size(); i != e; ++i) |
| 984 | New->addCase(Values[i], EdgeBB); |
| 985 | |
| 986 | // We added edges from PI to the EdgeBB. As such, if there were any |
| 987 | // PHI nodes in EdgeBB, they need entries to be added corresponding to |
| 988 | // the number of edges added. |
| 989 | for (BasicBlock::iterator BBI = EdgeBB->begin(); |
| 990 | PHINode *PN = dyn_cast<PHINode>(BBI); ++BBI) { |
| 991 | Value *InVal = PN->getIncomingValueForBlock(*PI); |
| 992 | for (unsigned i = 0, e = Values.size()-1; i != e; ++i) |
| 993 | PN->addIncoming(InVal, *PI); |
| 994 | } |
| 995 | |
| 996 | // Erase the old branch instruction. |
| 997 | (*PI)->getInstList().erase(BI); |
| 998 | |
| 999 | // Erase the potentially condition tree that was used to computed the |
| 1000 | // branch condition. |
| 1001 | ErasePossiblyDeadInstructionTree(Cond); |
| 1002 | return true; |
| 1003 | } |
| 1004 | } |
| 1005 | |
Chris Lattner | 18d1f19 | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 1006 | // If there is a trivial two-entry PHI node in this basic block, and we can |
| 1007 | // eliminate it, do so now. |
| 1008 | if (PHINode *PN = dyn_cast<PHINode>(BB->begin())) |
| 1009 | if (PN->getNumIncomingValues() == 2) { |
| 1010 | // Ok, this is a two entry PHI node. Check to see if this is a simple "if |
| 1011 | // statement", which has a very simple dominance structure. Basically, we |
| 1012 | // are trying to find the condition that is being branched on, which |
| 1013 | // subsequently causes this merge to happen. We really want control |
| 1014 | // dependence information for this check, but simplifycfg can't keep it up |
| 1015 | // to date, and this catches most of the cases we care about anyway. |
| 1016 | // |
| 1017 | BasicBlock *IfTrue, *IfFalse; |
| 1018 | if (Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse)) { |
| 1019 | //std::cerr << "FOUND IF CONDITION! " << *IfCond << " T: " |
| 1020 | // << IfTrue->getName() << " F: " << IfFalse->getName() << "\n"; |
| 1021 | |
| 1022 | // Figure out where to insert instructions as necessary. |
| 1023 | BasicBlock::iterator AfterPHIIt = BB->begin(); |
| 1024 | while (isa<PHINode>(AfterPHIIt)) ++AfterPHIIt; |
| 1025 | |
| 1026 | BasicBlock::iterator I = BB->begin(); |
| 1027 | while (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 1028 | ++I; |
| 1029 | |
| 1030 | // If we can eliminate this PHI by directly computing it based on the |
| 1031 | // condition, do so now. We can't eliminate PHI nodes where the |
| 1032 | // incoming values are defined in the conditional parts of the branch, |
| 1033 | // so check for this. |
| 1034 | // |
Chris Lattner | 0aa5656 | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 1035 | if (DominatesMergePoint(PN->getIncomingValue(0), BB, true) && |
| 1036 | DominatesMergePoint(PN->getIncomingValue(1), BB, true)) { |
Chris Lattner | 18d1f19 | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 1037 | Value *TrueVal = |
| 1038 | PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse); |
| 1039 | Value *FalseVal = |
| 1040 | PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue); |
| 1041 | |
Chris Lattner | 0aa5656 | 2004-04-09 22:50:22 +0000 | [diff] [blame] | 1042 | // If one of the incoming values is defined in the conditional |
| 1043 | // region, move it into it's predecessor block, which we know is |
| 1044 | // safe. |
| 1045 | if (!DominatesMergePoint(TrueVal, BB, false)) { |
| 1046 | Instruction *TrueI = cast<Instruction>(TrueVal); |
| 1047 | BasicBlock *OldBB = TrueI->getParent(); |
| 1048 | OldBB->getInstList().remove(TrueI); |
| 1049 | BasicBlock *NewBB = *pred_begin(OldBB); |
| 1050 | NewBB->getInstList().insert(NewBB->getTerminator(), TrueI); |
| 1051 | } |
| 1052 | if (!DominatesMergePoint(FalseVal, BB, false)) { |
| 1053 | Instruction *FalseI = cast<Instruction>(FalseVal); |
| 1054 | BasicBlock *OldBB = FalseI->getParent(); |
| 1055 | OldBB->getInstList().remove(FalseI); |
| 1056 | BasicBlock *NewBB = *pred_begin(OldBB); |
| 1057 | NewBB->getInstList().insert(NewBB->getTerminator(), FalseI); |
| 1058 | } |
| 1059 | |
Chris Lattner | 81bdcb9 | 2004-03-30 19:44:05 +0000 | [diff] [blame] | 1060 | // Change the PHI node into a select instruction. |
| 1061 | BasicBlock::iterator InsertPos = PN; |
| 1062 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 1063 | |
| 1064 | std::string Name = PN->getName(); PN->setName(""); |
| 1065 | PN->replaceAllUsesWith(new SelectInst(IfCond, TrueVal, FalseVal, |
| 1066 | Name, InsertPos)); |
| 1067 | BB->getInstList().erase(PN); |
| 1068 | Changed = true; |
Chris Lattner | 18d1f19 | 2004-02-11 03:36:04 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | } |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1073 | |
Chris Lattner | 031340a | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 1074 | return Changed; |
Chris Lattner | 466a049 | 2002-05-21 20:50:24 +0000 | [diff] [blame] | 1075 | } |