Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 1 | //===-- BasicBlockUtils.cpp - BasicBlock Utilities -------------------------==// |
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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
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 | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 9 | // |
| 10 | // This family of functions perform manipulations on basic blocks, and |
| 11 | // instructions contained within basic blocks. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 16 | #include "llvm/Function.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 18 | #include "llvm/Constant.h" |
| 19 | #include "llvm/Type.h" |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysis.h" |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopInfo.h" |
| 22 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 26 | /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, |
| 27 | /// if possible. The return value indicates success or failure. |
| 28 | bool llvm::MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P) { |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 29 | pred_iterator PI(pred_begin(BB)), PE(pred_end(BB)); |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 30 | // Can't merge the entry block. |
| 31 | if (pred_begin(BB) == pred_end(BB)) return false; |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 32 | |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 33 | BasicBlock *PredBB = *PI++; |
| 34 | for (; PI != PE; ++PI) // Search all predecessors, see if they are all same |
| 35 | if (*PI != PredBB) { |
| 36 | PredBB = 0; // There are multiple different predecessors... |
| 37 | break; |
| 38 | } |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 39 | |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 40 | // Can't merge if there are multiple predecessors. |
| 41 | if (!PredBB) return false; |
Owen Anderson | 3ecaf1b | 2008-07-18 17:46:41 +0000 | [diff] [blame] | 42 | // Don't break self-loops. |
| 43 | if (PredBB == BB) return false; |
| 44 | // Don't break invokes. |
| 45 | if (isa<InvokeInst>(PredBB->getTerminator())) return false; |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 46 | |
| 47 | succ_iterator SI(succ_begin(PredBB)), SE(succ_end(PredBB)); |
| 48 | BasicBlock* OnlySucc = BB; |
| 49 | for (; SI != SE; ++SI) |
| 50 | if (*SI != OnlySucc) { |
| 51 | OnlySucc = 0; // There are multiple distinct successors! |
| 52 | break; |
| 53 | } |
| 54 | |
| 55 | // Can't merge if there are multiple successors. |
| 56 | if (!OnlySucc) return false; |
Devang Patel | e435a5d | 2008-09-09 01:06:56 +0000 | [diff] [blame] | 57 | |
| 58 | // Can't merge if there is PHI loop. |
| 59 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) { |
| 60 | if (PHINode *PN = dyn_cast<PHINode>(BI)) { |
| 61 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 62 | if (PN->getIncomingValue(i) == PN) |
| 63 | return false; |
| 64 | } else |
| 65 | break; |
| 66 | } |
| 67 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 68 | // Begin by getting rid of unneeded PHIs. |
| 69 | while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { |
| 70 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 71 | BB->getInstList().pop_front(); // Delete the phi node... |
| 72 | } |
| 73 | |
| 74 | // Delete the unconditional branch from the predecessor... |
| 75 | PredBB->getInstList().pop_back(); |
| 76 | |
| 77 | // Move all definitions in the successor to the predecessor... |
| 78 | PredBB->getInstList().splice(PredBB->end(), BB->getInstList()); |
| 79 | |
| 80 | // Make all PHI nodes that referred to BB now refer to Pred as their |
| 81 | // source... |
| 82 | BB->replaceAllUsesWith(PredBB); |
| 83 | |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 84 | // Inherit predecessors name if it exists. |
| 85 | if (!PredBB->hasName()) |
| 86 | PredBB->takeName(BB); |
| 87 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 88 | // Finally, erase the old block and update dominator info. |
| 89 | if (P) { |
| 90 | if (DominatorTree* DT = P->getAnalysisToUpdate<DominatorTree>()) { |
| 91 | DomTreeNode* DTN = DT->getNode(BB); |
| 92 | DomTreeNode* PredDTN = DT->getNode(PredBB); |
| 93 | |
| 94 | if (DTN) { |
| 95 | SmallPtrSet<DomTreeNode*, 8> Children(DTN->begin(), DTN->end()); |
| 96 | for (SmallPtrSet<DomTreeNode*, 8>::iterator DI = Children.begin(), |
| 97 | DE = Children.end(); DI != DE; ++DI) |
| 98 | DT->changeImmediateDominator(*DI, PredDTN); |
| 99 | |
| 100 | DT->eraseNode(BB); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | BB->eraseFromParent(); |
| 106 | |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 111 | /// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 112 | /// with a value, then remove and delete the original instruction. |
| 113 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 114 | void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 115 | BasicBlock::iterator &BI, Value *V) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 116 | Instruction &I = *BI; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 117 | // Replaces all of the uses of the instruction with uses of the value |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 118 | I.replaceAllUsesWith(V); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 120 | // Make sure to propagate a name if there is one already. |
| 121 | if (I.hasName() && !V->hasName()) |
| 122 | V->takeName(&I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 123 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 124 | // Delete the unnecessary instruction now... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 125 | BI = BIL.erase(BI); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 129 | /// ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 130 | /// instruction specified by I. The original instruction is deleted and BI is |
| 131 | /// updated to point to the new instruction. |
| 132 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 133 | void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 134 | BasicBlock::iterator &BI, Instruction *I) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 135 | assert(I->getParent() == 0 && |
| 136 | "ReplaceInstWithInst: Instruction already inserted into basic block!"); |
| 137 | |
| 138 | // Insert the new instruction into the basic block... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 139 | BasicBlock::iterator New = BIL.insert(BI, I); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 140 | |
| 141 | // Replace all uses of the old instruction, and delete it. |
| 142 | ReplaceInstWithValue(BIL, BI, I); |
| 143 | |
| 144 | // Move BI back to point to the newly inserted instruction |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 145 | BI = New; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 148 | /// ReplaceInstWithInst - Replace the instruction specified by From with the |
| 149 | /// instruction specified by To. |
| 150 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 151 | void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 152 | BasicBlock::iterator BI(From); |
| 153 | ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 156 | /// RemoveSuccessor - Change the specified terminator instruction such that its |
Reid Spencer | bc2eba1 | 2006-05-19 19:09:46 +0000 | [diff] [blame] | 157 | /// successor SuccNum no longer exists. Because this reduces the outgoing |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 158 | /// degree of the current basic block, the actual terminator instruction itself |
Reid Spencer | bc2eba1 | 2006-05-19 19:09:46 +0000 | [diff] [blame] | 159 | /// may have to be changed. In the case where the last successor of the block |
| 160 | /// is deleted, a return instruction is inserted in its place which can cause a |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 161 | /// surprising change in program behavior if it is not expected. |
| 162 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 163 | void llvm::RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) { |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 164 | assert(SuccNum < TI->getNumSuccessors() && |
| 165 | "Trying to remove a nonexistant successor!"); |
| 166 | |
| 167 | // If our old successor block contains any PHI nodes, remove the entry in the |
| 168 | // PHI nodes that comes from this branch... |
| 169 | // |
| 170 | BasicBlock *BB = TI->getParent(); |
| 171 | TI->getSuccessor(SuccNum)->removePredecessor(BB); |
| 172 | |
| 173 | TerminatorInst *NewTI = 0; |
| 174 | switch (TI->getOpcode()) { |
| 175 | case Instruction::Br: |
| 176 | // If this is a conditional branch... convert to unconditional branch. |
| 177 | if (TI->getNumSuccessors() == 2) { |
| 178 | cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum)); |
| 179 | } else { // Otherwise convert to a return instruction... |
| 180 | Value *RetVal = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 181 | |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 182 | // Create a value to return... if the function doesn't return null... |
| 183 | if (BB->getParent()->getReturnType() != Type::VoidTy) |
| 184 | RetVal = Constant::getNullValue(BB->getParent()->getReturnType()); |
| 185 | |
| 186 | // Create the return... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 187 | NewTI = ReturnInst::Create(RetVal); |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 188 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 189 | break; |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 190 | |
| 191 | case Instruction::Invoke: // Should convert to call |
| 192 | case Instruction::Switch: // Should remove entry |
| 193 | default: |
| 194 | case Instruction::Ret: // Cannot happen, has no successors! |
| 195 | assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!"); |
| 196 | abort(); |
| 197 | } |
| 198 | |
| 199 | if (NewTI) // If it's a different instruction, replace. |
| 200 | ReplaceInstWithInst(TI, NewTI); |
| 201 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 202 | |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 203 | /// SplitEdge - Split the edge connecting specified block. Pass P must |
| 204 | /// not be NULL. |
| 205 | BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, Pass *P) { |
| 206 | TerminatorInst *LatchTerm = BB->getTerminator(); |
| 207 | unsigned SuccNum = 0; |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame^] | 208 | #ifndef NDEBUG |
| 209 | unsigned e = LatchTerm->getNumSuccessors(); |
| 210 | #endif |
| 211 | for (unsigned i = 0; ; ++i) { |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 212 | assert(i != e && "Didn't find edge?"); |
| 213 | if (LatchTerm->getSuccessor(i) == Succ) { |
| 214 | SuccNum = i; |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // If this is a critical edge, let SplitCriticalEdge do it. |
| 220 | if (SplitCriticalEdge(BB->getTerminator(), SuccNum, P)) |
| 221 | return LatchTerm->getSuccessor(SuccNum); |
| 222 | |
| 223 | // If the edge isn't critical, then BB has a single successor or Succ has a |
| 224 | // single pred. Split the block. |
| 225 | BasicBlock::iterator SplitPoint; |
| 226 | if (BasicBlock *SP = Succ->getSinglePredecessor()) { |
| 227 | // If the successor only has a single pred, split the top of the successor |
| 228 | // block. |
| 229 | assert(SP == BB && "CFG broken"); |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame^] | 230 | SP = NULL; |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 231 | return SplitBlock(Succ, Succ->begin(), P); |
| 232 | } else { |
| 233 | // Otherwise, if BB has a single successor, split it at the bottom of the |
| 234 | // block. |
| 235 | assert(BB->getTerminator()->getNumSuccessors() == 1 && |
| 236 | "Should have a single succ!"); |
| 237 | return SplitBlock(BB, BB->getTerminator(), P); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /// SplitBlock - Split the specified block at the specified instruction - every |
| 242 | /// thing before SplitPt stays in Old and everything starting with SplitPt moves |
| 243 | /// to a new block. The two blocks are joined by an unconditional branch and |
| 244 | /// the loop info is updated. |
| 245 | /// |
| 246 | BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P) { |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 247 | BasicBlock::iterator SplitIt = SplitPt; |
| 248 | while (isa<PHINode>(SplitIt)) |
| 249 | ++SplitIt; |
| 250 | BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split"); |
| 251 | |
| 252 | // The new block lives in whichever loop the old one did. |
Owen Anderson | a90793b | 2008-10-03 06:55:35 +0000 | [diff] [blame] | 253 | if (LoopInfo* LI = P->getAnalysisToUpdate<LoopInfo>()) |
| 254 | if (Loop *L = LI->getLoopFor(Old)) |
| 255 | L->addBasicBlockToLoop(New, LI->getBase()); |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 256 | |
Devang Patel | a8a8a36 | 2007-07-19 02:29:24 +0000 | [diff] [blame] | 257 | if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) |
| 258 | { |
| 259 | // Old dominates New. New node domiantes all other nodes dominated by Old. |
| 260 | DomTreeNode *OldNode = DT->getNode(Old); |
| 261 | std::vector<DomTreeNode *> Children; |
| 262 | for (DomTreeNode::iterator I = OldNode->begin(), E = OldNode->end(); |
| 263 | I != E; ++I) |
| 264 | Children.push_back(*I); |
| 265 | |
| 266 | DomTreeNode *NewNode = DT->addNewBlock(New,Old); |
| 267 | |
| 268 | for (std::vector<DomTreeNode *>::iterator I = Children.begin(), |
| 269 | E = Children.end(); I != E; ++I) |
| 270 | DT->changeImmediateDominator(*I, NewNode); |
| 271 | } |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 272 | |
| 273 | if (DominanceFrontier *DF = P->getAnalysisToUpdate<DominanceFrontier>()) |
| 274 | DF->splitBlock(Old); |
| 275 | |
| 276 | return New; |
| 277 | } |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 278 | |
| 279 | |
| 280 | /// SplitBlockPredecessors - This method transforms BB by introducing a new |
| 281 | /// basic block into the function, and moving some of the predecessors of BB to |
| 282 | /// be predecessors of the new block. The new predecessors are indicated by the |
| 283 | /// Preds array, which has NumPreds elements in it. The new block is given a |
| 284 | /// suffix of 'Suffix'. |
| 285 | /// |
| 286 | /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree and |
| 287 | /// DominanceFrontier, but no other analyses. |
| 288 | BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, |
| 289 | BasicBlock *const *Preds, |
| 290 | unsigned NumPreds, const char *Suffix, |
| 291 | Pass *P) { |
| 292 | // Create new basic block, insert right before the original block. |
| 293 | BasicBlock *NewBB = |
| 294 | BasicBlock::Create(BB->getName()+Suffix, BB->getParent(), BB); |
| 295 | |
| 296 | // The new block unconditionally branches to the old block. |
| 297 | BranchInst *BI = BranchInst::Create(BB, NewBB); |
| 298 | |
| 299 | // Move the edges from Preds to point to NewBB instead of BB. |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 300 | for (unsigned i = 0; i != NumPreds; ++i) |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 301 | Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 302 | |
| 303 | // Update dominator tree and dominator frontier if available. |
| 304 | DominatorTree *DT = P ? P->getAnalysisToUpdate<DominatorTree>() : 0; |
| 305 | if (DT) |
| 306 | DT->splitBlock(NewBB); |
| 307 | if (DominanceFrontier *DF = P ? P->getAnalysisToUpdate<DominanceFrontier>():0) |
| 308 | DF->splitBlock(NewBB); |
| 309 | AliasAnalysis *AA = P ? P->getAnalysisToUpdate<AliasAnalysis>() : 0; |
| 310 | |
| 311 | |
| 312 | // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI |
| 313 | // node becomes an incoming value for BB's phi node. However, if the Preds |
| 314 | // list is empty, we need to insert dummy entries into the PHI nodes in BB to |
| 315 | // account for the newly created predecessor. |
| 316 | if (NumPreds == 0) { |
| 317 | // Insert dummy values as the incoming value. |
| 318 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) |
| 319 | cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB); |
| 320 | return NewBB; |
| 321 | } |
| 322 | |
| 323 | // Otherwise, create a new PHI node in NewBB for each PHI node in BB. |
| 324 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ) { |
| 325 | PHINode *PN = cast<PHINode>(I++); |
| 326 | |
| 327 | // Check to see if all of the values coming in are the same. If so, we |
| 328 | // don't need to create a new PHI node. |
| 329 | Value *InVal = PN->getIncomingValueForBlock(Preds[0]); |
| 330 | for (unsigned i = 1; i != NumPreds; ++i) |
| 331 | if (InVal != PN->getIncomingValueForBlock(Preds[i])) { |
| 332 | InVal = 0; |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | if (InVal) { |
| 337 | // If all incoming values for the new PHI would be the same, just don't |
| 338 | // make a new PHI. Instead, just remove the incoming values from the old |
| 339 | // PHI. |
| 340 | for (unsigned i = 0; i != NumPreds; ++i) |
| 341 | PN->removeIncomingValue(Preds[i], false); |
| 342 | } else { |
| 343 | // If the values coming into the block are not the same, we need a PHI. |
| 344 | // Create the new PHI node, insert it into NewBB at the end of the block |
| 345 | PHINode *NewPHI = |
| 346 | PHINode::Create(PN->getType(), PN->getName()+".ph", BI); |
| 347 | if (AA) AA->copyValue(PN, NewPHI); |
| 348 | |
| 349 | // Move all of the PHI values for 'Preds' to the new PHI. |
| 350 | for (unsigned i = 0; i != NumPreds; ++i) { |
| 351 | Value *V = PN->removeIncomingValue(Preds[i], false); |
| 352 | NewPHI->addIncoming(V, Preds[i]); |
| 353 | } |
| 354 | InVal = NewPHI; |
| 355 | } |
| 356 | |
| 357 | // Add an incoming value to the PHI node in the loop for the preheader |
| 358 | // edge. |
| 359 | PN->addIncoming(InVal, NewBB); |
| 360 | |
| 361 | // Check to see if we can eliminate this phi node. |
| 362 | if (Value *V = PN->hasConstantValue(DT != 0)) { |
| 363 | Instruction *I = dyn_cast<Instruction>(V); |
| 364 | if (!I || DT == 0 || DT->dominates(I, PN)) { |
| 365 | PN->replaceAllUsesWith(V); |
| 366 | if (AA) AA->deleteValue(PN); |
| 367 | PN->eraseFromParent(); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return NewBB; |
| 373 | } |