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 | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 71af9b0 | 2008-12-03 06:40:52 +0000 | [diff] [blame] | 27 | /// DeleteDeadBlock - Delete the specified block, which must have no |
| 28 | /// predecessors. |
| 29 | void llvm::DeleteDeadBlock(BasicBlock *BB) { |
Chris Lattner | 2973a25 | 2008-12-03 07:45:15 +0000 | [diff] [blame] | 30 | assert((pred_begin(BB) == pred_end(BB) || |
| 31 | // Can delete self loop. |
| 32 | BB->getSinglePredecessor() == BB) && "Block is not dead!"); |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 33 | TerminatorInst *BBTerm = BB->getTerminator(); |
Devang Patel | 5622f07 | 2009-02-24 00:05:16 +0000 | [diff] [blame^] | 34 | |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 35 | // Loop through all of our successors and make sure they know that one |
| 36 | // of their predecessors is going away. |
| 37 | for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) |
| 38 | BBTerm->getSuccessor(i)->removePredecessor(BB); |
| 39 | |
| 40 | // Zap all the instructions in the block. |
| 41 | while (!BB->empty()) { |
| 42 | Instruction &I = BB->back(); |
| 43 | // If this instruction is used, replace uses with an arbitrary value. |
| 44 | // Because control flow can't get here, we don't care what we replace the |
| 45 | // value with. Note that since this block is unreachable, and all values |
| 46 | // contained within it must dominate their uses, that all uses will |
| 47 | // eventually be removed (they are themselves dead). |
| 48 | if (!I.use_empty()) |
| 49 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
| 50 | BB->getInstList().pop_back(); |
| 51 | } |
Devang Patel | 5622f07 | 2009-02-24 00:05:16 +0000 | [diff] [blame^] | 52 | |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 53 | // Zap the block! |
| 54 | BB->eraseFromParent(); |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | 29874e0 | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 57 | /// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are |
| 58 | /// any single-entry PHI nodes in it, fold them away. This handles the case |
| 59 | /// when all entries to the PHI nodes in a block are guaranteed equal, such as |
| 60 | /// when the block has exactly one predecessor. |
| 61 | void llvm::FoldSingleEntryPHINodes(BasicBlock *BB) { |
| 62 | if (!isa<PHINode>(BB->begin())) |
| 63 | return; |
| 64 | |
| 65 | while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { |
| 66 | if (PN->getIncomingValue(0) != PN) |
| 67 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 68 | else |
| 69 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
| 70 | PN->eraseFromParent(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 75 | /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, |
| 76 | /// if possible. The return value indicates success or failure. |
| 77 | bool llvm::MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P) { |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 78 | pred_iterator PI(pred_begin(BB)), PE(pred_end(BB)); |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 79 | // Can't merge the entry block. |
| 80 | if (pred_begin(BB) == pred_end(BB)) return false; |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 81 | |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 82 | BasicBlock *PredBB = *PI++; |
| 83 | for (; PI != PE; ++PI) // Search all predecessors, see if they are all same |
| 84 | if (*PI != PredBB) { |
| 85 | PredBB = 0; // There are multiple different predecessors... |
| 86 | break; |
| 87 | } |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 88 | |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 89 | // Can't merge if there are multiple predecessors. |
| 90 | if (!PredBB) return false; |
Owen Anderson | 3ecaf1b | 2008-07-18 17:46:41 +0000 | [diff] [blame] | 91 | // Don't break self-loops. |
| 92 | if (PredBB == BB) return false; |
| 93 | // Don't break invokes. |
| 94 | if (isa<InvokeInst>(PredBB->getTerminator())) return false; |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 95 | |
| 96 | succ_iterator SI(succ_begin(PredBB)), SE(succ_end(PredBB)); |
| 97 | BasicBlock* OnlySucc = BB; |
| 98 | for (; SI != SE; ++SI) |
| 99 | if (*SI != OnlySucc) { |
| 100 | OnlySucc = 0; // There are multiple distinct successors! |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | // Can't merge if there are multiple successors. |
| 105 | if (!OnlySucc) return false; |
Devang Patel | e435a5d | 2008-09-09 01:06:56 +0000 | [diff] [blame] | 106 | |
| 107 | // Can't merge if there is PHI loop. |
| 108 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) { |
| 109 | if (PHINode *PN = dyn_cast<PHINode>(BI)) { |
| 110 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 111 | if (PN->getIncomingValue(i) == PN) |
| 112 | return false; |
| 113 | } else |
| 114 | break; |
| 115 | } |
| 116 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 117 | // Begin by getting rid of unneeded PHIs. |
| 118 | while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { |
| 119 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 120 | BB->getInstList().pop_front(); // Delete the phi node... |
| 121 | } |
| 122 | |
| 123 | // Delete the unconditional branch from the predecessor... |
| 124 | PredBB->getInstList().pop_back(); |
| 125 | |
| 126 | // Move all definitions in the successor to the predecessor... |
| 127 | PredBB->getInstList().splice(PredBB->end(), BB->getInstList()); |
| 128 | |
| 129 | // Make all PHI nodes that referred to BB now refer to Pred as their |
| 130 | // source... |
| 131 | BB->replaceAllUsesWith(PredBB); |
| 132 | |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 133 | // Inherit predecessors name if it exists. |
| 134 | if (!PredBB->hasName()) |
| 135 | PredBB->takeName(BB); |
| 136 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 137 | // Finally, erase the old block and update dominator info. |
| 138 | if (P) { |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 139 | if (DominatorTree* DT = P->getAnalysisIfAvailable<DominatorTree>()) { |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 140 | DomTreeNode* DTN = DT->getNode(BB); |
| 141 | DomTreeNode* PredDTN = DT->getNode(PredBB); |
| 142 | |
| 143 | if (DTN) { |
| 144 | SmallPtrSet<DomTreeNode*, 8> Children(DTN->begin(), DTN->end()); |
| 145 | for (SmallPtrSet<DomTreeNode*, 8>::iterator DI = Children.begin(), |
| 146 | DE = Children.end(); DI != DE; ++DI) |
| 147 | DT->changeImmediateDominator(*DI, PredDTN); |
| 148 | |
| 149 | DT->eraseNode(BB); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | BB->eraseFromParent(); |
| 155 | |
| 156 | |
| 157 | return true; |
| 158 | } |
| 159 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 160 | /// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 161 | /// with a value, then remove and delete the original instruction. |
| 162 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 163 | void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 164 | BasicBlock::iterator &BI, Value *V) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 165 | Instruction &I = *BI; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 166 | // 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] | 167 | I.replaceAllUsesWith(V); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 169 | // Make sure to propagate a name if there is one already. |
| 170 | if (I.hasName() && !V->hasName()) |
| 171 | V->takeName(&I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 172 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 173 | // Delete the unnecessary instruction now... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 174 | BI = BIL.erase(BI); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 178 | /// ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 179 | /// instruction specified by I. The original instruction is deleted and BI is |
| 180 | /// updated to point to the new instruction. |
| 181 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 182 | void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 183 | BasicBlock::iterator &BI, Instruction *I) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 184 | assert(I->getParent() == 0 && |
| 185 | "ReplaceInstWithInst: Instruction already inserted into basic block!"); |
| 186 | |
| 187 | // Insert the new instruction into the basic block... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 188 | BasicBlock::iterator New = BIL.insert(BI, I); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 189 | |
| 190 | // Replace all uses of the old instruction, and delete it. |
| 191 | ReplaceInstWithValue(BIL, BI, I); |
| 192 | |
| 193 | // Move BI back to point to the newly inserted instruction |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 194 | BI = New; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 197 | /// ReplaceInstWithInst - Replace the instruction specified by From with the |
| 198 | /// instruction specified by To. |
| 199 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 200 | void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 201 | BasicBlock::iterator BI(From); |
| 202 | ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 203 | } |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 205 | /// RemoveSuccessor - Change the specified terminator instruction such that its |
Reid Spencer | bc2eba1 | 2006-05-19 19:09:46 +0000 | [diff] [blame] | 206 | /// successor SuccNum no longer exists. Because this reduces the outgoing |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 207 | /// degree of the current basic block, the actual terminator instruction itself |
Reid Spencer | bc2eba1 | 2006-05-19 19:09:46 +0000 | [diff] [blame] | 208 | /// may have to be changed. In the case where the last successor of the block |
| 209 | /// 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] | 210 | /// surprising change in program behavior if it is not expected. |
| 211 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 212 | void llvm::RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) { |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 213 | assert(SuccNum < TI->getNumSuccessors() && |
| 214 | "Trying to remove a nonexistant successor!"); |
| 215 | |
| 216 | // If our old successor block contains any PHI nodes, remove the entry in the |
| 217 | // PHI nodes that comes from this branch... |
| 218 | // |
| 219 | BasicBlock *BB = TI->getParent(); |
| 220 | TI->getSuccessor(SuccNum)->removePredecessor(BB); |
| 221 | |
| 222 | TerminatorInst *NewTI = 0; |
| 223 | switch (TI->getOpcode()) { |
| 224 | case Instruction::Br: |
| 225 | // If this is a conditional branch... convert to unconditional branch. |
| 226 | if (TI->getNumSuccessors() == 2) { |
| 227 | cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum)); |
| 228 | } else { // Otherwise convert to a return instruction... |
| 229 | Value *RetVal = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 230 | |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 231 | // Create a value to return... if the function doesn't return null... |
| 232 | if (BB->getParent()->getReturnType() != Type::VoidTy) |
| 233 | RetVal = Constant::getNullValue(BB->getParent()->getReturnType()); |
| 234 | |
| 235 | // Create the return... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 236 | NewTI = ReturnInst::Create(RetVal); |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 237 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 238 | break; |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 239 | |
| 240 | case Instruction::Invoke: // Should convert to call |
| 241 | case Instruction::Switch: // Should remove entry |
| 242 | default: |
| 243 | case Instruction::Ret: // Cannot happen, has no successors! |
| 244 | assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!"); |
| 245 | abort(); |
| 246 | } |
| 247 | |
| 248 | if (NewTI) // If it's a different instruction, replace. |
| 249 | ReplaceInstWithInst(TI, NewTI); |
| 250 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 251 | |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 252 | /// SplitEdge - Split the edge connecting specified block. Pass P must |
| 253 | /// not be NULL. |
| 254 | BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, Pass *P) { |
| 255 | TerminatorInst *LatchTerm = BB->getTerminator(); |
| 256 | unsigned SuccNum = 0; |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame] | 257 | #ifndef NDEBUG |
| 258 | unsigned e = LatchTerm->getNumSuccessors(); |
| 259 | #endif |
| 260 | for (unsigned i = 0; ; ++i) { |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 261 | assert(i != e && "Didn't find edge?"); |
| 262 | if (LatchTerm->getSuccessor(i) == Succ) { |
| 263 | SuccNum = i; |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // If this is a critical edge, let SplitCriticalEdge do it. |
| 269 | if (SplitCriticalEdge(BB->getTerminator(), SuccNum, P)) |
| 270 | return LatchTerm->getSuccessor(SuccNum); |
| 271 | |
| 272 | // If the edge isn't critical, then BB has a single successor or Succ has a |
| 273 | // single pred. Split the block. |
| 274 | BasicBlock::iterator SplitPoint; |
| 275 | if (BasicBlock *SP = Succ->getSinglePredecessor()) { |
| 276 | // If the successor only has a single pred, split the top of the successor |
| 277 | // block. |
| 278 | assert(SP == BB && "CFG broken"); |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame] | 279 | SP = NULL; |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 280 | return SplitBlock(Succ, Succ->begin(), P); |
| 281 | } else { |
| 282 | // Otherwise, if BB has a single successor, split it at the bottom of the |
| 283 | // block. |
| 284 | assert(BB->getTerminator()->getNumSuccessors() == 1 && |
| 285 | "Should have a single succ!"); |
| 286 | return SplitBlock(BB, BB->getTerminator(), P); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /// SplitBlock - Split the specified block at the specified instruction - every |
| 291 | /// thing before SplitPt stays in Old and everything starting with SplitPt moves |
| 292 | /// to a new block. The two blocks are joined by an unconditional branch and |
| 293 | /// the loop info is updated. |
| 294 | /// |
| 295 | BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P) { |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 296 | BasicBlock::iterator SplitIt = SplitPt; |
| 297 | while (isa<PHINode>(SplitIt)) |
| 298 | ++SplitIt; |
| 299 | BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split"); |
| 300 | |
| 301 | // The new block lives in whichever loop the old one did. |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 302 | if (LoopInfo* LI = P->getAnalysisIfAvailable<LoopInfo>()) |
Owen Anderson | a90793b | 2008-10-03 06:55:35 +0000 | [diff] [blame] | 303 | if (Loop *L = LI->getLoopFor(Old)) |
| 304 | L->addBasicBlockToLoop(New, LI->getBase()); |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 305 | |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 306 | if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) |
Devang Patel | a8a8a36 | 2007-07-19 02:29:24 +0000 | [diff] [blame] | 307 | { |
| 308 | // Old dominates New. New node domiantes all other nodes dominated by Old. |
| 309 | DomTreeNode *OldNode = DT->getNode(Old); |
| 310 | std::vector<DomTreeNode *> Children; |
| 311 | for (DomTreeNode::iterator I = OldNode->begin(), E = OldNode->end(); |
| 312 | I != E; ++I) |
| 313 | Children.push_back(*I); |
| 314 | |
| 315 | DomTreeNode *NewNode = DT->addNewBlock(New,Old); |
| 316 | |
| 317 | for (std::vector<DomTreeNode *>::iterator I = Children.begin(), |
| 318 | E = Children.end(); I != E; ++I) |
| 319 | DT->changeImmediateDominator(*I, NewNode); |
| 320 | } |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 321 | |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 322 | if (DominanceFrontier *DF = P->getAnalysisIfAvailable<DominanceFrontier>()) |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 323 | DF->splitBlock(Old); |
| 324 | |
| 325 | return New; |
| 326 | } |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 327 | |
| 328 | |
| 329 | /// SplitBlockPredecessors - This method transforms BB by introducing a new |
| 330 | /// basic block into the function, and moving some of the predecessors of BB to |
| 331 | /// be predecessors of the new block. The new predecessors are indicated by the |
| 332 | /// Preds array, which has NumPreds elements in it. The new block is given a |
| 333 | /// suffix of 'Suffix'. |
| 334 | /// |
| 335 | /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree and |
| 336 | /// DominanceFrontier, but no other analyses. |
| 337 | BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, |
| 338 | BasicBlock *const *Preds, |
| 339 | unsigned NumPreds, const char *Suffix, |
| 340 | Pass *P) { |
| 341 | // Create new basic block, insert right before the original block. |
| 342 | BasicBlock *NewBB = |
| 343 | BasicBlock::Create(BB->getName()+Suffix, BB->getParent(), BB); |
| 344 | |
| 345 | // The new block unconditionally branches to the old block. |
| 346 | BranchInst *BI = BranchInst::Create(BB, NewBB); |
| 347 | |
| 348 | // Move the edges from Preds to point to NewBB instead of BB. |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 349 | for (unsigned i = 0; i != NumPreds; ++i) |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 350 | Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 351 | |
| 352 | // Update dominator tree and dominator frontier if available. |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 353 | DominatorTree *DT = P ? P->getAnalysisIfAvailable<DominatorTree>() : 0; |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 354 | if (DT) |
| 355 | DT->splitBlock(NewBB); |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 356 | if (DominanceFrontier *DF = P ? P->getAnalysisIfAvailable<DominanceFrontier>():0) |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 357 | DF->splitBlock(NewBB); |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 358 | AliasAnalysis *AA = P ? P->getAnalysisIfAvailable<AliasAnalysis>() : 0; |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 359 | |
| 360 | |
| 361 | // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI |
| 362 | // node becomes an incoming value for BB's phi node. However, if the Preds |
| 363 | // list is empty, we need to insert dummy entries into the PHI nodes in BB to |
| 364 | // account for the newly created predecessor. |
| 365 | if (NumPreds == 0) { |
| 366 | // Insert dummy values as the incoming value. |
| 367 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) |
| 368 | cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB); |
| 369 | return NewBB; |
| 370 | } |
| 371 | |
| 372 | // Otherwise, create a new PHI node in NewBB for each PHI node in BB. |
| 373 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ) { |
| 374 | PHINode *PN = cast<PHINode>(I++); |
| 375 | |
| 376 | // Check to see if all of the values coming in are the same. If so, we |
| 377 | // don't need to create a new PHI node. |
| 378 | Value *InVal = PN->getIncomingValueForBlock(Preds[0]); |
| 379 | for (unsigned i = 1; i != NumPreds; ++i) |
| 380 | if (InVal != PN->getIncomingValueForBlock(Preds[i])) { |
| 381 | InVal = 0; |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | if (InVal) { |
| 386 | // If all incoming values for the new PHI would be the same, just don't |
| 387 | // make a new PHI. Instead, just remove the incoming values from the old |
| 388 | // PHI. |
| 389 | for (unsigned i = 0; i != NumPreds; ++i) |
| 390 | PN->removeIncomingValue(Preds[i], false); |
| 391 | } else { |
| 392 | // If the values coming into the block are not the same, we need a PHI. |
| 393 | // Create the new PHI node, insert it into NewBB at the end of the block |
| 394 | PHINode *NewPHI = |
| 395 | PHINode::Create(PN->getType(), PN->getName()+".ph", BI); |
| 396 | if (AA) AA->copyValue(PN, NewPHI); |
| 397 | |
| 398 | // Move all of the PHI values for 'Preds' to the new PHI. |
| 399 | for (unsigned i = 0; i != NumPreds; ++i) { |
| 400 | Value *V = PN->removeIncomingValue(Preds[i], false); |
| 401 | NewPHI->addIncoming(V, Preds[i]); |
| 402 | } |
| 403 | InVal = NewPHI; |
| 404 | } |
| 405 | |
| 406 | // Add an incoming value to the PHI node in the loop for the preheader |
| 407 | // edge. |
| 408 | PN->addIncoming(InVal, NewBB); |
| 409 | |
| 410 | // Check to see if we can eliminate this phi node. |
| 411 | if (Value *V = PN->hasConstantValue(DT != 0)) { |
| 412 | Instruction *I = dyn_cast<Instruction>(V); |
| 413 | if (!I || DT == 0 || DT->dominates(I, PN)) { |
| 414 | PN->replaceAllUsesWith(V); |
| 415 | if (AA) AA->deleteValue(PN); |
| 416 | PN->eraseFromParent(); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return NewBB; |
| 422 | } |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 424 | /// AreEquivalentAddressValues - Test if A and B will obviously have the same |
| 425 | /// value. This includes recognizing that %t0 and %t1 will have the same |
| 426 | /// value in code like this: |
| 427 | /// %t0 = getelementptr @a, 0, 3 |
| 428 | /// store i32 0, i32* %t0 |
| 429 | /// %t1 = getelementptr @a, 0, 3 |
| 430 | /// %t2 = load i32* %t1 |
| 431 | /// |
| 432 | static bool AreEquivalentAddressValues(const Value *A, const Value *B) { |
| 433 | // Test if the values are trivially equivalent. |
| 434 | if (A == B) return true; |
| 435 | |
| 436 | // Test if the values come form identical arithmetic instructions. |
| 437 | if (isa<BinaryOperator>(A) || isa<CastInst>(A) || |
| 438 | isa<PHINode>(A) || isa<GetElementPtrInst>(A)) |
| 439 | if (const Instruction *BI = dyn_cast<Instruction>(B)) |
| 440 | if (cast<Instruction>(A)->isIdenticalTo(BI)) |
| 441 | return true; |
| 442 | |
| 443 | // Otherwise they may not be equivalent. |
| 444 | return false; |
| 445 | } |
| 446 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 447 | /// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the |
| 448 | /// instruction before ScanFrom) checking to see if we have the value at the |
| 449 | /// memory address *Ptr locally available within a small number of instructions. |
| 450 | /// If the value is available, return it. |
| 451 | /// |
| 452 | /// If not, return the iterator for the last validated instruction that the |
| 453 | /// value would be live through. If we scanned the entire block and didn't find |
| 454 | /// something that invalidates *Ptr or provides it, ScanFrom would be left at |
| 455 | /// begin() and this returns null. ScanFrom could also be left |
| 456 | /// |
| 457 | /// MaxInstsToScan specifies the maximum instructions to scan in the block. If |
| 458 | /// it is set to 0, it will scan the whole block. You can also optionally |
| 459 | /// specify an alias analysis implementation, which makes this more precise. |
| 460 | Value *llvm::FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB, |
| 461 | BasicBlock::iterator &ScanFrom, |
| 462 | unsigned MaxInstsToScan, |
| 463 | AliasAnalysis *AA) { |
| 464 | if (MaxInstsToScan == 0) MaxInstsToScan = ~0U; |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 465 | |
| 466 | // If we're using alias analysis to disambiguate get the size of *Ptr. |
| 467 | unsigned AccessSize = 0; |
| 468 | if (AA) { |
| 469 | const Type *AccessTy = cast<PointerType>(Ptr->getType())->getElementType(); |
| 470 | AccessSize = AA->getTargetData().getTypeStoreSizeInBits(AccessTy); |
| 471 | } |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 472 | |
| 473 | while (ScanFrom != ScanBB->begin()) { |
| 474 | // Don't scan huge blocks. |
| 475 | if (MaxInstsToScan-- == 0) return 0; |
| 476 | |
| 477 | Instruction *Inst = --ScanFrom; |
| 478 | |
| 479 | // If this is a load of Ptr, the loaded value is available. |
| 480 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 481 | if (AreEquivalentAddressValues(LI->getOperand(0), Ptr)) |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 482 | return LI; |
| 483 | |
| 484 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 485 | // If this is a store through Ptr, the value is available! |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 486 | if (AreEquivalentAddressValues(SI->getOperand(1), Ptr)) |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 487 | return SI->getOperand(0); |
| 488 | |
| 489 | // If Ptr is an alloca and this is a store to a different alloca, ignore |
| 490 | // the store. This is a trivial form of alias analysis that is important |
| 491 | // for reg2mem'd code. |
| 492 | if ((isa<AllocaInst>(Ptr) || isa<GlobalVariable>(Ptr)) && |
| 493 | (isa<AllocaInst>(SI->getOperand(1)) || |
| 494 | isa<GlobalVariable>(SI->getOperand(1)))) |
| 495 | continue; |
| 496 | |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 497 | // If we have alias analysis and it says the store won't modify the loaded |
| 498 | // value, ignore the store. |
| 499 | if (AA && |
| 500 | (AA->getModRefInfo(SI, Ptr, AccessSize) & AliasAnalysis::Mod) == 0) |
| 501 | continue; |
| 502 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 503 | // Otherwise the store that may or may not alias the pointer, bail out. |
| 504 | ++ScanFrom; |
| 505 | return 0; |
| 506 | } |
| 507 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 508 | // If this is some other instruction that may clobber Ptr, bail out. |
| 509 | if (Inst->mayWriteToMemory()) { |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 510 | // If alias analysis claims that it really won't modify the load, |
| 511 | // ignore it. |
| 512 | if (AA && |
| 513 | (AA->getModRefInfo(Inst, Ptr, AccessSize) & AliasAnalysis::Mod) == 0) |
| 514 | continue; |
| 515 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 516 | // May modify the pointer, bail out. |
| 517 | ++ScanFrom; |
| 518 | return 0; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | // Got to the start of the block, we didn't find it, but are done for this |
| 523 | // block. |
| 524 | return 0; |
| 525 | } |