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" |
Dale Johannesen | bd8e650 | 2009-03-03 01:09:07 +0000 | [diff] [blame] | 18 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 19 | #include "llvm/Constant.h" |
| 20 | #include "llvm/Type.h" |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AliasAnalysis.h" |
Cameron Zwarich | 3012787 | 2011-01-18 04:11:31 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | b5b7997 | 2011-01-11 08:13:40 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/LoopInfo.h" |
| 24 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/Local.h" |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Scalar.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ValueHandle.h" |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 71af9b0 | 2008-12-03 06:40:52 +0000 | [diff] [blame] | 33 | /// DeleteDeadBlock - Delete the specified block, which must have no |
| 34 | /// predecessors. |
| 35 | void llvm::DeleteDeadBlock(BasicBlock *BB) { |
Chris Lattner | 2973a25 | 2008-12-03 07:45:15 +0000 | [diff] [blame] | 36 | assert((pred_begin(BB) == pred_end(BB) || |
| 37 | // Can delete self loop. |
| 38 | BB->getSinglePredecessor() == BB) && "Block is not dead!"); |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 39 | TerminatorInst *BBTerm = BB->getTerminator(); |
Devang Patel | 5622f07 | 2009-02-24 00:05:16 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 41 | // Loop through all of our successors and make sure they know that one |
| 42 | // of their predecessors is going away. |
| 43 | for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) |
| 44 | BBTerm->getSuccessor(i)->removePredecessor(BB); |
| 45 | |
| 46 | // Zap all the instructions in the block. |
| 47 | while (!BB->empty()) { |
| 48 | Instruction &I = BB->back(); |
| 49 | // If this instruction is used, replace uses with an arbitrary value. |
| 50 | // Because control flow can't get here, we don't care what we replace the |
| 51 | // value with. Note that since this block is unreachable, and all values |
| 52 | // contained within it must dominate their uses, that all uses will |
| 53 | // eventually be removed (they are themselves dead). |
| 54 | if (!I.use_empty()) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 55 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 56 | BB->getInstList().pop_back(); |
| 57 | } |
Devang Patel | 5622f07 | 2009-02-24 00:05:16 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 59 | // Zap the block! |
| 60 | BB->eraseFromParent(); |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Chris Lattner | 29874e0 | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 63 | /// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are |
| 64 | /// any single-entry PHI nodes in it, fold them away. This handles the case |
| 65 | /// when all entries to the PHI nodes in a block are guaranteed equal, such as |
| 66 | /// when the block has exactly one predecessor. |
Chris Lattner | b5b7997 | 2011-01-11 08:13:40 +0000 | [diff] [blame] | 67 | void llvm::FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P) { |
| 68 | if (!isa<PHINode>(BB->begin())) return; |
| 69 | |
| 70 | AliasAnalysis *AA = 0; |
| 71 | MemoryDependenceAnalysis *MemDep = 0; |
| 72 | if (P) { |
| 73 | AA = P->getAnalysisIfAvailable<AliasAnalysis>(); |
| 74 | MemDep = P->getAnalysisIfAvailable<MemoryDependenceAnalysis>(); |
| 75 | } |
| 76 | |
Chris Lattner | 29874e0 | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 77 | while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { |
| 78 | if (PN->getIncomingValue(0) != PN) |
| 79 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 80 | else |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 81 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
Chris Lattner | b5b7997 | 2011-01-11 08:13:40 +0000 | [diff] [blame] | 82 | |
| 83 | if (MemDep) |
| 84 | MemDep->removeInstruction(PN); // Memdep updates AA itself. |
| 85 | else if (AA && isa<PointerType>(PN->getType())) |
| 86 | AA->deleteValue(PN); |
| 87 | |
Chris Lattner | 29874e0 | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 88 | PN->eraseFromParent(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 93 | /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it |
| 94 | /// is dead. Also recursively delete any operands that become dead as |
| 95 | /// a result. This includes tracing the def-use list from the PHI to see if |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 96 | /// it is ultimately unused or if it reaches an unused cycle. |
Dan Gohman | 90fe0bd | 2010-01-05 15:45:31 +0000 | [diff] [blame] | 97 | bool llvm::DeleteDeadPHIs(BasicBlock *BB) { |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 98 | // Recursively deleting a PHI may cause multiple PHIs to be deleted |
| 99 | // or RAUW'd undef, so use an array of WeakVH for the PHIs to delete. |
| 100 | SmallVector<WeakVH, 8> PHIs; |
| 101 | for (BasicBlock::iterator I = BB->begin(); |
| 102 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 103 | PHIs.push_back(PN); |
| 104 | |
Dan Gohman | 90fe0bd | 2010-01-05 15:45:31 +0000 | [diff] [blame] | 105 | bool Changed = false; |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 106 | for (unsigned i = 0, e = PHIs.size(); i != e; ++i) |
| 107 | if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*())) |
Dan Gohman | 90fe0bd | 2010-01-05 15:45:31 +0000 | [diff] [blame] | 108 | Changed |= RecursivelyDeleteDeadPHINode(PN); |
| 109 | |
| 110 | return Changed; |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 113 | /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, |
| 114 | /// if possible. The return value indicates success or failure. |
Chris Lattner | 8820292 | 2009-11-01 04:57:33 +0000 | [diff] [blame] | 115 | bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P) { |
Dan Gohman | 1c034dc | 2010-08-17 17:07:02 +0000 | [diff] [blame] | 116 | // Don't merge away blocks who have their address taken. |
| 117 | if (BB->hasAddressTaken()) return false; |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 118 | |
Dan Gohman | 1c034dc | 2010-08-17 17:07:02 +0000 | [diff] [blame] | 119 | // Can't merge if there are multiple predecessors, or no predecessors. |
| 120 | BasicBlock *PredBB = BB->getUniquePredecessor(); |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 121 | if (!PredBB) return false; |
Dan Gohman | 1c034dc | 2010-08-17 17:07:02 +0000 | [diff] [blame] | 122 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 123 | // Don't break self-loops. |
| 124 | if (PredBB == BB) return false; |
| 125 | // Don't break invokes. |
| 126 | if (isa<InvokeInst>(PredBB->getTerminator())) return false; |
| 127 | |
| 128 | succ_iterator SI(succ_begin(PredBB)), SE(succ_end(PredBB)); |
Chris Lattner | dc85f8a | 2011-01-08 19:08:40 +0000 | [diff] [blame] | 129 | BasicBlock *OnlySucc = BB; |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 130 | for (; SI != SE; ++SI) |
| 131 | if (*SI != OnlySucc) { |
| 132 | OnlySucc = 0; // There are multiple distinct successors! |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | // Can't merge if there are multiple successors. |
| 137 | if (!OnlySucc) return false; |
Devang Patel | e435a5d | 2008-09-09 01:06:56 +0000 | [diff] [blame] | 138 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 139 | // Can't merge if there is PHI loop. |
| 140 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) { |
| 141 | if (PHINode *PN = dyn_cast<PHINode>(BI)) { |
| 142 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 143 | if (PN->getIncomingValue(i) == PN) |
| 144 | return false; |
| 145 | } else |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | // Begin by getting rid of unneeded PHIs. |
Chris Lattner | dc85f8a | 2011-01-08 19:08:40 +0000 | [diff] [blame] | 150 | if (isa<PHINode>(BB->front())) |
Chris Lattner | b5b7997 | 2011-01-11 08:13:40 +0000 | [diff] [blame] | 151 | FoldSingleEntryPHINodes(BB, P); |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 152 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 153 | // Delete the unconditional branch from the predecessor... |
| 154 | PredBB->getInstList().pop_back(); |
| 155 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 156 | // Make all PHI nodes that referred to BB now refer to Pred as their |
| 157 | // source... |
| 158 | BB->replaceAllUsesWith(PredBB); |
| 159 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 160 | // Move all definitions in the successor to the predecessor... |
| 161 | PredBB->getInstList().splice(PredBB->end(), BB->getInstList()); |
| 162 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 163 | // Inherit predecessors name if it exists. |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 164 | if (!PredBB->hasName()) |
| 165 | PredBB->takeName(BB); |
| 166 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 167 | // Finally, erase the old block and update dominator info. |
| 168 | if (P) { |
Chris Lattner | dc85f8a | 2011-01-08 19:08:40 +0000 | [diff] [blame] | 169 | if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) { |
| 170 | if (DomTreeNode *DTN = DT->getNode(BB)) { |
| 171 | DomTreeNode *PredDTN = DT->getNode(PredBB); |
Jakob Stoklund Olesen | fbbd4ab | 2011-01-11 22:54:38 +0000 | [diff] [blame] | 172 | SmallVector<DomTreeNode*, 8> Children(DTN->begin(), DTN->end()); |
| 173 | for (SmallVector<DomTreeNode*, 8>::iterator DI = Children.begin(), |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 174 | DE = Children.end(); DI != DE; ++DI) |
| 175 | DT->changeImmediateDominator(*DI, PredDTN); |
| 176 | |
| 177 | DT->eraseNode(BB); |
| 178 | } |
Chris Lattner | dc85f8a | 2011-01-08 19:08:40 +0000 | [diff] [blame] | 179 | |
| 180 | if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>()) |
| 181 | LI->removeBlock(BB); |
Chris Lattner | b681099 | 2011-01-11 08:16:49 +0000 | [diff] [blame] | 182 | |
| 183 | if (MemoryDependenceAnalysis *MD = |
| 184 | P->getAnalysisIfAvailable<MemoryDependenceAnalysis>()) |
| 185 | MD->invalidateCachedPredecessors(); |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | BB->eraseFromParent(); |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 190 | return true; |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 193 | /// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 194 | /// with a value, then remove and delete the original instruction. |
| 195 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 196 | void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 197 | BasicBlock::iterator &BI, Value *V) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 198 | Instruction &I = *BI; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 199 | // 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] | 200 | I.replaceAllUsesWith(V); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 201 | |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 202 | // Make sure to propagate a name if there is one already. |
| 203 | if (I.hasName() && !V->hasName()) |
| 204 | V->takeName(&I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 205 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 206 | // Delete the unnecessary instruction now... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 207 | BI = BIL.erase(BI); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 211 | /// ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 212 | /// instruction specified by I. The original instruction is deleted and BI is |
| 213 | /// updated to point to the new instruction. |
| 214 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 215 | void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 216 | BasicBlock::iterator &BI, Instruction *I) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 217 | assert(I->getParent() == 0 && |
| 218 | "ReplaceInstWithInst: Instruction already inserted into basic block!"); |
| 219 | |
| 220 | // Insert the new instruction into the basic block... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 221 | BasicBlock::iterator New = BIL.insert(BI, I); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 222 | |
| 223 | // Replace all uses of the old instruction, and delete it. |
| 224 | ReplaceInstWithValue(BIL, BI, I); |
| 225 | |
| 226 | // Move BI back to point to the newly inserted instruction |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 227 | BI = New; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 230 | /// ReplaceInstWithInst - Replace the instruction specified by From with the |
| 231 | /// instruction specified by To. |
| 232 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 233 | void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 234 | BasicBlock::iterator BI(From); |
| 235 | ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 236 | } |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 237 | |
Bob Wilson | ae23daf | 2010-02-16 21:06:42 +0000 | [diff] [blame] | 238 | /// GetSuccessorNumber - Search for the specified successor of basic block BB |
| 239 | /// and return its position in the terminator instruction's list of |
| 240 | /// successors. It is an error to call this with a block that is not a |
| 241 | /// successor. |
| 242 | unsigned llvm::GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ) { |
Bob Wilson | adb6f22 | 2010-02-16 19:49:17 +0000 | [diff] [blame] | 243 | TerminatorInst *Term = BB->getTerminator(); |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame] | 244 | #ifndef NDEBUG |
Bob Wilson | adb6f22 | 2010-02-16 19:49:17 +0000 | [diff] [blame] | 245 | unsigned e = Term->getNumSuccessors(); |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame] | 246 | #endif |
| 247 | for (unsigned i = 0; ; ++i) { |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 248 | assert(i != e && "Didn't find edge?"); |
Bob Wilson | adb6f22 | 2010-02-16 19:49:17 +0000 | [diff] [blame] | 249 | if (Term->getSuccessor(i) == Succ) |
| 250 | return i; |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 251 | } |
Bob Wilson | adb6f22 | 2010-02-16 19:49:17 +0000 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /// SplitEdge - Split the edge connecting specified block. Pass P must |
| 256 | /// not be NULL. |
| 257 | BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, Pass *P) { |
Bob Wilson | ae23daf | 2010-02-16 21:06:42 +0000 | [diff] [blame] | 258 | unsigned SuccNum = GetSuccessorNumber(BB, Succ); |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 259 | |
| 260 | // If this is a critical edge, let SplitCriticalEdge do it. |
Bob Wilson | adb6f22 | 2010-02-16 19:49:17 +0000 | [diff] [blame] | 261 | TerminatorInst *LatchTerm = BB->getTerminator(); |
| 262 | if (SplitCriticalEdge(LatchTerm, SuccNum, P)) |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 263 | return LatchTerm->getSuccessor(SuccNum); |
| 264 | |
| 265 | // If the edge isn't critical, then BB has a single successor or Succ has a |
| 266 | // single pred. Split the block. |
| 267 | BasicBlock::iterator SplitPoint; |
| 268 | if (BasicBlock *SP = Succ->getSinglePredecessor()) { |
| 269 | // If the successor only has a single pred, split the top of the successor |
| 270 | // block. |
| 271 | assert(SP == BB && "CFG broken"); |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame] | 272 | SP = NULL; |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 273 | return SplitBlock(Succ, Succ->begin(), P); |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 274 | } |
Chris Lattner | b0433d4 | 2011-01-08 18:47:43 +0000 | [diff] [blame] | 275 | |
| 276 | // Otherwise, if BB has a single successor, split it at the bottom of the |
| 277 | // block. |
| 278 | assert(BB->getTerminator()->getNumSuccessors() == 1 && |
| 279 | "Should have a single succ!"); |
| 280 | return SplitBlock(BB, BB->getTerminator(), P); |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /// SplitBlock - Split the specified block at the specified instruction - every |
| 284 | /// thing before SplitPt stays in Old and everything starting with SplitPt moves |
| 285 | /// to a new block. The two blocks are joined by an unconditional branch and |
| 286 | /// the loop info is updated. |
| 287 | /// |
| 288 | BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P) { |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 289 | BasicBlock::iterator SplitIt = SplitPt; |
Bill Wendling | ab82fd9 | 2011-08-17 21:21:31 +0000 | [diff] [blame] | 290 | while (isa<PHINode>(SplitIt) || isa<LandingPadInst>(SplitIt)) |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 291 | ++SplitIt; |
| 292 | BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split"); |
| 293 | |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 294 | // The new block lives in whichever loop the old one did. This preserves |
| 295 | // LCSSA as well, because we force the split point to be after any PHI nodes. |
Chris Lattner | dc85f8a | 2011-01-08 19:08:40 +0000 | [diff] [blame] | 296 | if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>()) |
Owen Anderson | a90793b | 2008-10-03 06:55:35 +0000 | [diff] [blame] | 297 | if (Loop *L = LI->getLoopFor(Old)) |
| 298 | L->addBasicBlockToLoop(New, LI->getBase()); |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 299 | |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 300 | if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) { |
Gabor Greif | e2d5004 | 2010-09-10 22:25:58 +0000 | [diff] [blame] | 301 | // Old dominates New. New node dominates all other nodes dominated by Old. |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 302 | DomTreeNode *OldNode = DT->getNode(Old); |
| 303 | std::vector<DomTreeNode *> Children; |
| 304 | for (DomTreeNode::iterator I = OldNode->begin(), E = OldNode->end(); |
| 305 | I != E; ++I) |
| 306 | Children.push_back(*I); |
Devang Patel | a8a8a36 | 2007-07-19 02:29:24 +0000 | [diff] [blame] | 307 | |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 308 | DomTreeNode *NewNode = DT->addNewBlock(New,Old); |
Devang Patel | a8a8a36 | 2007-07-19 02:29:24 +0000 | [diff] [blame] | 309 | for (std::vector<DomTreeNode *>::iterator I = Children.begin(), |
| 310 | E = Children.end(); I != E; ++I) |
| 311 | DT->changeImmediateDominator(*I, NewNode); |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 312 | } |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 313 | |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 314 | return New; |
| 315 | } |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 316 | |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 317 | /// UpdateAnalysisInformation - Update DominatorTree, LoopInfo, and LCCSA |
| 318 | /// analysis information. |
Bill Wendling | d477014 | 2011-08-18 17:57:57 +0000 | [diff] [blame] | 319 | static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame^] | 320 | ArrayRef<BasicBlock *> Preds, |
| 321 | Pass *P, bool &HasLoopExit) { |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 322 | if (!P) return; |
| 323 | |
| 324 | LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>(); |
| 325 | Loop *L = LI ? LI->getLoopFor(OldBB) : 0; |
| 326 | bool PreserveLCSSA = P->mustPreserveAnalysisID(LCSSAID); |
| 327 | |
| 328 | // If we need to preserve loop analyses, collect some information about how |
| 329 | // this split will affect loops. |
| 330 | bool IsLoopEntry = !!L; |
| 331 | bool SplitMakesNewLoopHeader = false; |
| 332 | if (LI) { |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame^] | 333 | for (ArrayRef<BasicBlock*>::iterator |
| 334 | i = Preds.begin(), e = Preds.end(); i != e; ++i) { |
| 335 | BasicBlock *Pred = *i; |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 336 | // If we need to preserve LCSSA, determine if any of the preds is a loop |
| 337 | // exit. |
| 338 | if (PreserveLCSSA) |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame^] | 339 | if (Loop *PL = LI->getLoopFor(Pred)) |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 340 | if (!PL->contains(OldBB)) |
| 341 | HasLoopExit = true; |
| 342 | |
| 343 | // If we need to preserve LoopInfo, note whether any of the preds crosses |
| 344 | // an interesting loop boundary. |
| 345 | if (!L) continue; |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame^] | 346 | if (L->contains(Pred)) |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 347 | IsLoopEntry = false; |
| 348 | else |
| 349 | SplitMakesNewLoopHeader = true; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // Update dominator tree if available. |
| 354 | DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>(); |
| 355 | if (DT) |
| 356 | DT->splitBlock(NewBB); |
| 357 | |
| 358 | if (!L) return; |
| 359 | |
| 360 | if (IsLoopEntry) { |
| 361 | // Add the new block to the nearest enclosing loop (and not an adjacent |
| 362 | // loop). To find this, examine each of the predecessors and determine which |
| 363 | // loops enclose them, and select the most-nested loop which contains the |
| 364 | // loop containing the block being split. |
| 365 | Loop *InnermostPredLoop = 0; |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame^] | 366 | for (ArrayRef<BasicBlock*>::iterator |
| 367 | i = Preds.begin(), e = Preds.end(); i != e; ++i) { |
| 368 | BasicBlock *Pred = *i; |
| 369 | if (Loop *PredLoop = LI->getLoopFor(Pred)) { |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 370 | // Seek a loop which actually contains the block being split (to avoid |
| 371 | // adjacent loops). |
| 372 | while (PredLoop && !PredLoop->contains(OldBB)) |
| 373 | PredLoop = PredLoop->getParentLoop(); |
| 374 | |
| 375 | // Select the most-nested of these loops which contains the block. |
| 376 | if (PredLoop && PredLoop->contains(OldBB) && |
| 377 | (!InnermostPredLoop || |
| 378 | InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth())) |
| 379 | InnermostPredLoop = PredLoop; |
| 380 | } |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame^] | 381 | } |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 382 | |
| 383 | if (InnermostPredLoop) |
| 384 | InnermostPredLoop->addBasicBlockToLoop(NewBB, LI->getBase()); |
| 385 | } else { |
| 386 | L->addBasicBlockToLoop(NewBB, LI->getBase()); |
| 387 | if (SplitMakesNewLoopHeader) |
| 388 | L->moveToHeader(NewBB); |
| 389 | } |
| 390 | } |
| 391 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 392 | /// SplitBlockPredecessors - This method transforms BB by introducing a new |
| 393 | /// basic block into the function, and moving some of the predecessors of BB to |
| 394 | /// be predecessors of the new block. The new predecessors are indicated by the |
| 395 | /// Preds array, which has NumPreds elements in it. The new block is given a |
| 396 | /// suffix of 'Suffix'. |
| 397 | /// |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 398 | /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, |
Cameron Zwarich | 3012787 | 2011-01-18 04:11:31 +0000 | [diff] [blame] | 399 | /// LoopInfo, and LCCSA but no other analyses. In particular, it does not |
| 400 | /// preserve LoopSimplify (because it's complicated to handle the case where one |
| 401 | /// of the edges being split is an exit of a loop with other exits). |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 402 | /// |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 403 | BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, |
| 404 | BasicBlock *const *Preds, |
| 405 | unsigned NumPreds, const char *Suffix, |
| 406 | Pass *P) { |
| 407 | // Create new basic block, insert right before the original block. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 408 | BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), BB->getName()+Suffix, |
| 409 | BB->getParent(), BB); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 410 | |
| 411 | // The new block unconditionally branches to the old block. |
| 412 | BranchInst *BI = BranchInst::Create(BB, NewBB); |
| 413 | |
| 414 | // Move the edges from Preds to point to NewBB instead of BB. |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 415 | for (unsigned i = 0; i != NumPreds; ++i) { |
Dan Gohman | b8eb17c | 2009-11-05 18:25:44 +0000 | [diff] [blame] | 416 | // This is slightly more strict than necessary; the minimum requirement |
| 417 | // is that there be no more than one indirectbr branching to BB. And |
| 418 | // all BlockAddress uses would need to be updated. |
| 419 | assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) && |
| 420 | "Cannot split an edge from an IndirectBrInst"); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 421 | Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 424 | // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI |
| 425 | // node becomes an incoming value for BB's phi node. However, if the Preds |
| 426 | // list is empty, we need to insert dummy entries into the PHI nodes in BB to |
| 427 | // account for the newly created predecessor. |
| 428 | if (NumPreds == 0) { |
| 429 | // Insert dummy values as the incoming value. |
| 430 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 431 | cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 432 | return NewBB; |
| 433 | } |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 434 | |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 435 | // Update DominatorTree, LoopInfo, and LCCSA analysis information. |
| 436 | bool HasLoopExit = false; |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame^] | 437 | UpdateAnalysisInformation(BB, NewBB, ArrayRef<BasicBlock*>(Preds, NumPreds), |
| 438 | P, HasLoopExit); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 440 | // Otherwise, create a new PHI node in NewBB for each PHI node in BB. |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 441 | AliasAnalysis *AA = P ? P->getAnalysisIfAvailable<AliasAnalysis>() : 0; |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 442 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ) { |
| 443 | PHINode *PN = cast<PHINode>(I++); |
| 444 | |
| 445 | // Check to see if all of the values coming in are the same. If so, we |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 446 | // don't need to create a new PHI node, unless it's needed for LCSSA. |
| 447 | Value *InVal = 0; |
| 448 | if (!HasLoopExit) { |
| 449 | InVal = PN->getIncomingValueForBlock(Preds[0]); |
| 450 | for (unsigned i = 1; i != NumPreds; ++i) |
| 451 | if (InVal != PN->getIncomingValueForBlock(Preds[i])) { |
| 452 | InVal = 0; |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 457 | if (InVal) { |
| 458 | // If all incoming values for the new PHI would be the same, just don't |
| 459 | // make a new PHI. Instead, just remove the incoming values from the old |
| 460 | // PHI. |
| 461 | for (unsigned i = 0; i != NumPreds; ++i) |
| 462 | PN->removeIncomingValue(Preds[i], false); |
| 463 | } else { |
| 464 | // If the values coming into the block are not the same, we need a PHI. |
| 465 | // Create the new PHI node, insert it into NewBB at the end of the block |
| 466 | PHINode *NewPHI = |
Jay Foad | 3ecfc86 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 467 | PHINode::Create(PN->getType(), NumPreds, PN->getName()+".ph", BI); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 468 | if (AA) AA->copyValue(PN, NewPHI); |
| 469 | |
| 470 | // Move all of the PHI values for 'Preds' to the new PHI. |
| 471 | for (unsigned i = 0; i != NumPreds; ++i) { |
| 472 | Value *V = PN->removeIncomingValue(Preds[i], false); |
| 473 | NewPHI->addIncoming(V, Preds[i]); |
| 474 | } |
| 475 | InVal = NewPHI; |
| 476 | } |
| 477 | |
| 478 | // Add an incoming value to the PHI node in the loop for the preheader |
| 479 | // edge. |
| 480 | PN->addIncoming(InVal, NewBB); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 481 | } |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 482 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 483 | return NewBB; |
| 484 | } |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 485 | |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 486 | /// FindFunctionBackedges - Analyze the specified function to find all of the |
| 487 | /// loop backedges in the function and return them. This is a relatively cheap |
| 488 | /// (compared to computing dominators and loop info) analysis. |
| 489 | /// |
| 490 | /// The output is added to Result, as pairs of <from,to> edge info. |
| 491 | void llvm::FindFunctionBackedges(const Function &F, |
| 492 | SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result) { |
| 493 | const BasicBlock *BB = &F.getEntryBlock(); |
| 494 | if (succ_begin(BB) == succ_end(BB)) |
| 495 | return; |
| 496 | |
| 497 | SmallPtrSet<const BasicBlock*, 8> Visited; |
| 498 | SmallVector<std::pair<const BasicBlock*, succ_const_iterator>, 8> VisitStack; |
| 499 | SmallPtrSet<const BasicBlock*, 8> InStack; |
| 500 | |
| 501 | Visited.insert(BB); |
| 502 | VisitStack.push_back(std::make_pair(BB, succ_begin(BB))); |
| 503 | InStack.insert(BB); |
| 504 | do { |
| 505 | std::pair<const BasicBlock*, succ_const_iterator> &Top = VisitStack.back(); |
| 506 | const BasicBlock *ParentBB = Top.first; |
| 507 | succ_const_iterator &I = Top.second; |
| 508 | |
| 509 | bool FoundNew = false; |
| 510 | while (I != succ_end(ParentBB)) { |
| 511 | BB = *I++; |
| 512 | if (Visited.insert(BB)) { |
| 513 | FoundNew = true; |
| 514 | break; |
| 515 | } |
| 516 | // Successor is in VisitStack, it's a back edge. |
| 517 | if (InStack.count(BB)) |
| 518 | Result.push_back(std::make_pair(ParentBB, BB)); |
| 519 | } |
| 520 | |
| 521 | if (FoundNew) { |
| 522 | // Go down one level if there is a unvisited successor. |
| 523 | InStack.insert(BB); |
| 524 | VisitStack.push_back(std::make_pair(BB, succ_begin(BB))); |
| 525 | } else { |
| 526 | // Go up one level. |
| 527 | InStack.erase(VisitStack.pop_back_val().first); |
| 528 | } |
Evan Cheng | c3f507f | 2011-01-29 04:46:23 +0000 | [diff] [blame] | 529 | } while (!VisitStack.empty()); |
| 530 | } |
| 531 | |
| 532 | /// FoldReturnIntoUncondBranch - This method duplicates the specified return |
| 533 | /// instruction into a predecessor which ends in an unconditional branch. If |
| 534 | /// the return instruction returns a value defined by a PHI, propagate the |
| 535 | /// right value into the return. It returns the new return instruction in the |
| 536 | /// predecessor. |
| 537 | ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, |
| 538 | BasicBlock *Pred) { |
| 539 | Instruction *UncondBranch = Pred->getTerminator(); |
| 540 | // Clone the return and add it to the end of the predecessor. |
| 541 | Instruction *NewRet = RI->clone(); |
| 542 | Pred->getInstList().push_back(NewRet); |
| 543 | |
| 544 | // If the return instruction returns a value, and if the value was a |
| 545 | // PHI node in "BB", propagate the right value into the return. |
| 546 | for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end(); |
| 547 | i != e; ++i) |
| 548 | if (PHINode *PN = dyn_cast<PHINode>(*i)) |
| 549 | if (PN->getParent() == BB) |
| 550 | *i = PN->getIncomingValueForBlock(Pred); |
| 551 | |
| 552 | // Update any PHI nodes in the returning block to realize that we no |
| 553 | // longer branch to them. |
| 554 | BB->removePredecessor(Pred); |
| 555 | UncondBranch->eraseFromParent(); |
| 556 | return cast<ReturnInst>(NewRet); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 557 | } |
Devang Patel | 40348e8 | 2011-04-29 22:28:59 +0000 | [diff] [blame] | 558 | |
| 559 | /// GetFirstDebugLocInBasicBlock - Return first valid DebugLoc entry in a |
| 560 | /// given basic block. |
| 561 | DebugLoc llvm::GetFirstDebugLocInBasicBlock(const BasicBlock *BB) { |
Devang Patel | 67352b3 | 2011-05-02 21:57:00 +0000 | [diff] [blame] | 562 | if (const Instruction *I = BB->getFirstNonPHI()) |
| 563 | return I->getDebugLoc(); |
| 564 | // Scanning entire block may be too expensive, if the first instruction |
| 565 | // does not have valid location info. |
Devang Patel | 40348e8 | 2011-04-29 22:28:59 +0000 | [diff] [blame] | 566 | return DebugLoc(); |
| 567 | } |