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" |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/AliasAnalysis.h" |
Nick Lewycky | 81e4804 | 2013-07-27 01:24:00 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/CFG.h" |
Cameron Zwarich | 3012787 | 2011-01-18 04:11:31 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | b5b7997 | 2011-01-11 08:13:40 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/LoopInfo.h" |
| 20 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constant.h" |
| 22 | #include "llvm/IR/DataLayout.h" |
| 23 | #include "llvm/IR/Function.h" |
| 24 | #include "llvm/IR/Instructions.h" |
| 25 | #include "llvm/IR/IntrinsicInst.h" |
| 26 | #include "llvm/IR/Type.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ValueHandle.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Scalar.h" |
| 30 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 31 | #include <algorithm> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 71af9b0 | 2008-12-03 06:40:52 +0000 | [diff] [blame] | 34 | /// DeleteDeadBlock - Delete the specified block, which must have no |
| 35 | /// predecessors. |
| 36 | void llvm::DeleteDeadBlock(BasicBlock *BB) { |
Chris Lattner | 2973a25 | 2008-12-03 07:45:15 +0000 | [diff] [blame] | 37 | assert((pred_begin(BB) == pred_end(BB) || |
| 38 | // Can delete self loop. |
| 39 | BB->getSinglePredecessor() == BB) && "Block is not dead!"); |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 40 | TerminatorInst *BBTerm = BB->getTerminator(); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 42 | // Loop through all of our successors and make sure they know that one |
| 43 | // of their predecessors is going away. |
| 44 | for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) |
| 45 | BBTerm->getSuccessor(i)->removePredecessor(BB); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 47 | // Zap all the instructions in the block. |
| 48 | while (!BB->empty()) { |
| 49 | Instruction &I = BB->back(); |
| 50 | // If this instruction is used, replace uses with an arbitrary value. |
| 51 | // Because control flow can't get here, we don't care what we replace the |
| 52 | // value with. Note that since this block is unreachable, and all values |
| 53 | // contained within it must dominate their uses, that all uses will |
| 54 | // eventually be removed (they are themselves dead). |
| 55 | if (!I.use_empty()) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 56 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 57 | BB->getInstList().pop_back(); |
| 58 | } |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 60 | // Zap the block! |
| 61 | BB->eraseFromParent(); |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | 29874e0 | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 64 | /// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are |
| 65 | /// any single-entry PHI nodes in it, fold them away. This handles the case |
| 66 | /// when all entries to the PHI nodes in a block are guaranteed equal, such as |
| 67 | /// when the block has exactly one predecessor. |
Chris Lattner | b5b7997 | 2011-01-11 08:13:40 +0000 | [diff] [blame] | 68 | void llvm::FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P) { |
| 69 | if (!isa<PHINode>(BB->begin())) return; |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 70 | |
Chris Lattner | b5b7997 | 2011-01-11 08:13:40 +0000 | [diff] [blame] | 71 | AliasAnalysis *AA = 0; |
| 72 | MemoryDependenceAnalysis *MemDep = 0; |
| 73 | if (P) { |
| 74 | AA = P->getAnalysisIfAvailable<AliasAnalysis>(); |
| 75 | MemDep = P->getAnalysisIfAvailable<MemoryDependenceAnalysis>(); |
| 76 | } |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 29874e0 | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 78 | while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { |
| 79 | if (PN->getIncomingValue(0) != PN) |
| 80 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 81 | else |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 82 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 83 | |
Chris Lattner | b5b7997 | 2011-01-11 08:13:40 +0000 | [diff] [blame] | 84 | if (MemDep) |
| 85 | MemDep->removeInstruction(PN); // Memdep updates AA itself. |
| 86 | else if (AA && isa<PointerType>(PN->getType())) |
| 87 | AA->deleteValue(PN); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 29874e0 | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 89 | PN->eraseFromParent(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 94 | /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it |
| 95 | /// is dead. Also recursively delete any operands that become dead as |
| 96 | /// 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] | 97 | /// it is ultimately unused or if it reaches an unused cycle. |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 98 | bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI) { |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 99 | // Recursively deleting a PHI may cause multiple PHIs to be deleted |
| 100 | // or RAUW'd undef, so use an array of WeakVH for the PHIs to delete. |
| 101 | SmallVector<WeakVH, 8> PHIs; |
| 102 | for (BasicBlock::iterator I = BB->begin(); |
| 103 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 104 | PHIs.push_back(PN); |
| 105 | |
Dan Gohman | 90fe0bd | 2010-01-05 15:45:31 +0000 | [diff] [blame] | 106 | bool Changed = false; |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 107 | for (unsigned i = 0, e = PHIs.size(); i != e; ++i) |
| 108 | if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*())) |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 109 | Changed |= RecursivelyDeleteDeadPHINode(PN, TLI); |
Dan Gohman | 90fe0bd | 2010-01-05 15:45:31 +0000 | [diff] [blame] | 110 | |
| 111 | return Changed; |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 114 | /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, |
| 115 | /// if possible. The return value indicates success or failure. |
Chris Lattner | 8820292 | 2009-11-01 04:57:33 +0000 | [diff] [blame] | 116 | bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P) { |
Dan Gohman | 1c034dc | 2010-08-17 17:07:02 +0000 | [diff] [blame] | 117 | // Don't merge away blocks who have their address taken. |
| 118 | if (BB->hasAddressTaken()) return false; |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 119 | |
Dan Gohman | 1c034dc | 2010-08-17 17:07:02 +0000 | [diff] [blame] | 120 | // Can't merge if there are multiple predecessors, or no predecessors. |
| 121 | BasicBlock *PredBB = BB->getUniquePredecessor(); |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 122 | if (!PredBB) return false; |
Dan Gohman | 1c034dc | 2010-08-17 17:07:02 +0000 | [diff] [blame] | 123 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 124 | // Don't break self-loops. |
| 125 | if (PredBB == BB) return false; |
| 126 | // Don't break invokes. |
| 127 | if (isa<InvokeInst>(PredBB->getTerminator())) return false; |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 128 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 129 | succ_iterator SI(succ_begin(PredBB)), SE(succ_end(PredBB)); |
Chris Lattner | dc85f8a | 2011-01-08 19:08:40 +0000 | [diff] [blame] | 130 | BasicBlock *OnlySucc = BB; |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 131 | for (; SI != SE; ++SI) |
| 132 | if (*SI != OnlySucc) { |
| 133 | OnlySucc = 0; // There are multiple distinct successors! |
| 134 | break; |
| 135 | } |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 136 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 137 | // Can't merge if there are multiple successors. |
| 138 | if (!OnlySucc) return false; |
Devang Patel | e435a5d | 2008-09-09 01:06:56 +0000 | [diff] [blame] | 139 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 140 | // Can't merge if there is PHI loop. |
| 141 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) { |
| 142 | if (PHINode *PN = dyn_cast<PHINode>(BI)) { |
| 143 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 144 | if (PN->getIncomingValue(i) == PN) |
| 145 | return false; |
| 146 | } else |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | // Begin by getting rid of unneeded PHIs. |
Chris Lattner | dc85f8a | 2011-01-08 19:08:40 +0000 | [diff] [blame] | 151 | if (isa<PHINode>(BB->front())) |
Chris Lattner | b5b7997 | 2011-01-11 08:13:40 +0000 | [diff] [blame] | 152 | FoldSingleEntryPHINodes(BB, P); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 153 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 154 | // Delete the unconditional branch from the predecessor... |
| 155 | PredBB->getInstList().pop_back(); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 156 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 157 | // Make all PHI nodes that referred to BB now refer to Pred as their |
| 158 | // source... |
| 159 | BB->replaceAllUsesWith(PredBB); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 160 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 161 | // Move all definitions in the successor to the predecessor... |
| 162 | PredBB->getInstList().splice(PredBB->end(), BB->getInstList()); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 163 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 164 | // Inherit predecessors name if it exists. |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 165 | if (!PredBB->hasName()) |
| 166 | PredBB->takeName(BB); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 167 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 168 | // Finally, erase the old block and update dominator info. |
| 169 | if (P) { |
Chris Lattner | dc85f8a | 2011-01-08 19:08:40 +0000 | [diff] [blame] | 170 | if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) { |
| 171 | if (DomTreeNode *DTN = DT->getNode(BB)) { |
| 172 | DomTreeNode *PredDTN = DT->getNode(PredBB); |
Jakob Stoklund Olesen | fbbd4ab | 2011-01-11 22:54:38 +0000 | [diff] [blame] | 173 | SmallVector<DomTreeNode*, 8> Children(DTN->begin(), DTN->end()); |
Craig Topper | 6227d5c | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 174 | for (SmallVectorImpl<DomTreeNode *>::iterator DI = Children.begin(), |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 175 | DE = Children.end(); DI != DE; ++DI) |
| 176 | DT->changeImmediateDominator(*DI, PredDTN); |
| 177 | |
| 178 | DT->eraseNode(BB); |
| 179 | } |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 180 | |
Chris Lattner | dc85f8a | 2011-01-08 19:08:40 +0000 | [diff] [blame] | 181 | if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>()) |
| 182 | LI->removeBlock(BB); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 183 | |
Chris Lattner | b681099 | 2011-01-11 08:16:49 +0000 | [diff] [blame] | 184 | if (MemoryDependenceAnalysis *MD = |
| 185 | P->getAnalysisIfAvailable<MemoryDependenceAnalysis>()) |
| 186 | MD->invalidateCachedPredecessors(); |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 189 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 190 | BB->eraseFromParent(); |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 191 | return true; |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 194 | /// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 195 | /// with a value, then remove and delete the original instruction. |
| 196 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 197 | void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 198 | BasicBlock::iterator &BI, Value *V) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 199 | Instruction &I = *BI; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 200 | // 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] | 201 | I.replaceAllUsesWith(V); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 203 | // Make sure to propagate a name if there is one already. |
| 204 | if (I.hasName() && !V->hasName()) |
| 205 | V->takeName(&I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 206 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 207 | // Delete the unnecessary instruction now... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 208 | BI = BIL.erase(BI); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 212 | /// ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 213 | /// instruction specified by I. The original instruction is deleted and BI is |
| 214 | /// updated to point to the new instruction. |
| 215 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 216 | void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 217 | BasicBlock::iterator &BI, Instruction *I) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 218 | assert(I->getParent() == 0 && |
| 219 | "ReplaceInstWithInst: Instruction already inserted into basic block!"); |
| 220 | |
| 221 | // Insert the new instruction into the basic block... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 222 | BasicBlock::iterator New = BIL.insert(BI, I); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 223 | |
| 224 | // Replace all uses of the old instruction, and delete it. |
| 225 | ReplaceInstWithValue(BIL, BI, I); |
| 226 | |
| 227 | // Move BI back to point to the newly inserted instruction |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 228 | BI = New; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 231 | /// ReplaceInstWithInst - Replace the instruction specified by From with the |
| 232 | /// instruction specified by To. |
| 233 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 234 | void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 235 | BasicBlock::iterator BI(From); |
| 236 | ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 237 | } |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 238 | |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 239 | /// SplitEdge - Split the edge connecting specified block. Pass P must |
| 240 | /// not be NULL. |
Bob Wilson | adb6f22 | 2010-02-16 19:49:17 +0000 | [diff] [blame] | 241 | BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, Pass *P) { |
Bob Wilson | ae23daf | 2010-02-16 21:06:42 +0000 | [diff] [blame] | 242 | unsigned SuccNum = GetSuccessorNumber(BB, Succ); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 243 | |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 244 | // If this is a critical edge, let SplitCriticalEdge do it. |
Bob Wilson | adb6f22 | 2010-02-16 19:49:17 +0000 | [diff] [blame] | 245 | TerminatorInst *LatchTerm = BB->getTerminator(); |
| 246 | if (SplitCriticalEdge(LatchTerm, SuccNum, P)) |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 247 | return LatchTerm->getSuccessor(SuccNum); |
| 248 | |
| 249 | // If the edge isn't critical, then BB has a single successor or Succ has a |
| 250 | // single pred. Split the block. |
| 251 | BasicBlock::iterator SplitPoint; |
| 252 | if (BasicBlock *SP = Succ->getSinglePredecessor()) { |
| 253 | // If the successor only has a single pred, split the top of the successor |
| 254 | // block. |
| 255 | assert(SP == BB && "CFG broken"); |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame] | 256 | SP = NULL; |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 257 | return SplitBlock(Succ, Succ->begin(), P); |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 258 | } |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 259 | |
Chris Lattner | b0433d4 | 2011-01-08 18:47:43 +0000 | [diff] [blame] | 260 | // Otherwise, if BB has a single successor, split it at the bottom of the |
| 261 | // block. |
| 262 | assert(BB->getTerminator()->getNumSuccessors() == 1 && |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 263 | "Should have a single succ!"); |
Chris Lattner | b0433d4 | 2011-01-08 18:47:43 +0000 | [diff] [blame] | 264 | return SplitBlock(BB, BB->getTerminator(), P); |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /// SplitBlock - Split the specified block at the specified instruction - every |
| 268 | /// thing before SplitPt stays in Old and everything starting with SplitPt moves |
| 269 | /// to a new block. The two blocks are joined by an unconditional branch and |
| 270 | /// the loop info is updated. |
| 271 | /// |
| 272 | BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P) { |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 273 | BasicBlock::iterator SplitIt = SplitPt; |
Bill Wendling | ab82fd9 | 2011-08-17 21:21:31 +0000 | [diff] [blame] | 274 | while (isa<PHINode>(SplitIt) || isa<LandingPadInst>(SplitIt)) |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 275 | ++SplitIt; |
| 276 | BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split"); |
| 277 | |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 278 | // The new block lives in whichever loop the old one did. This preserves |
| 279 | // 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] | 280 | if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>()) |
Owen Anderson | a90793b | 2008-10-03 06:55:35 +0000 | [diff] [blame] | 281 | if (Loop *L = LI->getLoopFor(Old)) |
| 282 | L->addBasicBlockToLoop(New, LI->getBase()); |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 283 | |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 284 | if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) { |
Gabor Greif | e2d5004 | 2010-09-10 22:25:58 +0000 | [diff] [blame] | 285 | // Old dominates New. New node dominates all other nodes dominated by Old. |
Rafael Espindola | 605e2b5 | 2011-08-24 18:07:01 +0000 | [diff] [blame] | 286 | if (DomTreeNode *OldNode = DT->getNode(Old)) { |
| 287 | std::vector<DomTreeNode *> Children; |
| 288 | for (DomTreeNode::iterator I = OldNode->begin(), E = OldNode->end(); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 289 | I != E; ++I) |
Rafael Espindola | 605e2b5 | 2011-08-24 18:07:01 +0000 | [diff] [blame] | 290 | Children.push_back(*I); |
Devang Patel | a8a8a36 | 2007-07-19 02:29:24 +0000 | [diff] [blame] | 291 | |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 292 | DomTreeNode *NewNode = DT->addNewBlock(New,Old); |
Devang Patel | a8a8a36 | 2007-07-19 02:29:24 +0000 | [diff] [blame] | 293 | for (std::vector<DomTreeNode *>::iterator I = Children.begin(), |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 294 | E = Children.end(); I != E; ++I) |
Devang Patel | a8a8a36 | 2007-07-19 02:29:24 +0000 | [diff] [blame] | 295 | DT->changeImmediateDominator(*I, NewNode); |
Rafael Espindola | 605e2b5 | 2011-08-24 18:07:01 +0000 | [diff] [blame] | 296 | } |
Evan Cheng | 0f1666b | 2010-04-05 21:16:25 +0000 | [diff] [blame] | 297 | } |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 298 | |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 299 | return New; |
| 300 | } |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 301 | |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 302 | /// UpdateAnalysisInformation - Update DominatorTree, LoopInfo, and LCCSA |
| 303 | /// analysis information. |
Bill Wendling | d477014 | 2011-08-18 17:57:57 +0000 | [diff] [blame] | 304 | static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame] | 305 | ArrayRef<BasicBlock *> Preds, |
| 306 | Pass *P, bool &HasLoopExit) { |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 307 | if (!P) return; |
| 308 | |
| 309 | LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>(); |
| 310 | Loop *L = LI ? LI->getLoopFor(OldBB) : 0; |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 311 | |
| 312 | // If we need to preserve loop analyses, collect some information about how |
| 313 | // this split will affect loops. |
| 314 | bool IsLoopEntry = !!L; |
| 315 | bool SplitMakesNewLoopHeader = false; |
| 316 | if (LI) { |
Bill Wendling | 7e8840c | 2011-08-19 00:05:40 +0000 | [diff] [blame] | 317 | bool PreserveLCSSA = P->mustPreserveAnalysisID(LCSSAID); |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame] | 318 | for (ArrayRef<BasicBlock*>::iterator |
| 319 | i = Preds.begin(), e = Preds.end(); i != e; ++i) { |
| 320 | BasicBlock *Pred = *i; |
Bill Wendling | 7e8840c | 2011-08-19 00:05:40 +0000 | [diff] [blame] | 321 | |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 322 | // If we need to preserve LCSSA, determine if any of the preds is a loop |
| 323 | // exit. |
| 324 | if (PreserveLCSSA) |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame] | 325 | if (Loop *PL = LI->getLoopFor(Pred)) |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 326 | if (!PL->contains(OldBB)) |
| 327 | HasLoopExit = true; |
| 328 | |
| 329 | // If we need to preserve LoopInfo, note whether any of the preds crosses |
| 330 | // an interesting loop boundary. |
| 331 | if (!L) continue; |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame] | 332 | if (L->contains(Pred)) |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 333 | IsLoopEntry = false; |
| 334 | else |
| 335 | SplitMakesNewLoopHeader = true; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // Update dominator tree if available. |
| 340 | DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>(); |
| 341 | if (DT) |
| 342 | DT->splitBlock(NewBB); |
| 343 | |
| 344 | if (!L) return; |
| 345 | |
| 346 | if (IsLoopEntry) { |
| 347 | // Add the new block to the nearest enclosing loop (and not an adjacent |
| 348 | // loop). To find this, examine each of the predecessors and determine which |
| 349 | // loops enclose them, and select the most-nested loop which contains the |
| 350 | // loop containing the block being split. |
| 351 | Loop *InnermostPredLoop = 0; |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame] | 352 | for (ArrayRef<BasicBlock*>::iterator |
| 353 | i = Preds.begin(), e = Preds.end(); i != e; ++i) { |
| 354 | BasicBlock *Pred = *i; |
| 355 | if (Loop *PredLoop = LI->getLoopFor(Pred)) { |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 356 | // Seek a loop which actually contains the block being split (to avoid |
| 357 | // adjacent loops). |
| 358 | while (PredLoop && !PredLoop->contains(OldBB)) |
| 359 | PredLoop = PredLoop->getParentLoop(); |
| 360 | |
| 361 | // Select the most-nested of these loops which contains the block. |
| 362 | if (PredLoop && PredLoop->contains(OldBB) && |
| 363 | (!InnermostPredLoop || |
| 364 | InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth())) |
| 365 | InnermostPredLoop = PredLoop; |
| 366 | } |
Bill Wendling | 9210e84 | 2011-08-18 20:39:32 +0000 | [diff] [blame] | 367 | } |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 368 | |
| 369 | if (InnermostPredLoop) |
| 370 | InnermostPredLoop->addBasicBlockToLoop(NewBB, LI->getBase()); |
| 371 | } else { |
| 372 | L->addBasicBlockToLoop(NewBB, LI->getBase()); |
| 373 | if (SplitMakesNewLoopHeader) |
| 374 | L->moveToHeader(NewBB); |
| 375 | } |
| 376 | } |
| 377 | |
Bill Wendling | 1c44d86 | 2011-08-18 20:51:04 +0000 | [diff] [blame] | 378 | /// UpdatePHINodes - Update the PHI nodes in OrigBB to include the values coming |
| 379 | /// from NewBB. This also updates AliasAnalysis, if available. |
| 380 | static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB, |
| 381 | ArrayRef<BasicBlock*> Preds, BranchInst *BI, |
| 382 | Pass *P, bool HasLoopExit) { |
| 383 | // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB. |
| 384 | AliasAnalysis *AA = P ? P->getAnalysisIfAvailable<AliasAnalysis>() : 0; |
| 385 | for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) { |
| 386 | PHINode *PN = cast<PHINode>(I++); |
| 387 | |
| 388 | // Check to see if all of the values coming in are the same. If so, we |
| 389 | // don't need to create a new PHI node, unless it's needed for LCSSA. |
| 390 | Value *InVal = 0; |
| 391 | if (!HasLoopExit) { |
| 392 | InVal = PN->getIncomingValueForBlock(Preds[0]); |
| 393 | for (unsigned i = 1, e = Preds.size(); i != e; ++i) |
| 394 | if (InVal != PN->getIncomingValueForBlock(Preds[i])) { |
| 395 | InVal = 0; |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (InVal) { |
| 401 | // If all incoming values for the new PHI would be the same, just don't |
| 402 | // make a new PHI. Instead, just remove the incoming values from the old |
| 403 | // PHI. |
| 404 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) |
| 405 | PN->removeIncomingValue(Preds[i], false); |
| 406 | } else { |
| 407 | // If the values coming into the block are not the same, we need a PHI. |
| 408 | // Create the new PHI node, insert it into NewBB at the end of the block |
| 409 | PHINode *NewPHI = |
| 410 | PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI); |
| 411 | if (AA) AA->copyValue(PN, NewPHI); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 412 | |
Bill Wendling | 1c44d86 | 2011-08-18 20:51:04 +0000 | [diff] [blame] | 413 | // Move all of the PHI values for 'Preds' to the new PHI. |
| 414 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) { |
| 415 | Value *V = PN->removeIncomingValue(Preds[i], false); |
| 416 | NewPHI->addIncoming(V, Preds[i]); |
| 417 | } |
| 418 | |
| 419 | InVal = NewPHI; |
| 420 | } |
| 421 | |
| 422 | // Add an incoming value to the PHI node in the loop for the preheader |
| 423 | // edge. |
| 424 | PN->addIncoming(InVal, NewBB); |
| 425 | } |
| 426 | } |
| 427 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 428 | /// SplitBlockPredecessors - This method transforms BB by introducing a new |
| 429 | /// basic block into the function, and moving some of the predecessors of BB to |
| 430 | /// be predecessors of the new block. The new predecessors are indicated by the |
| 431 | /// Preds array, which has NumPreds elements in it. The new block is given a |
| 432 | /// suffix of 'Suffix'. |
| 433 | /// |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 434 | /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, |
Cameron Zwarich | 3012787 | 2011-01-18 04:11:31 +0000 | [diff] [blame] | 435 | /// LoopInfo, and LCCSA but no other analyses. In particular, it does not |
| 436 | /// preserve LoopSimplify (because it's complicated to handle the case where one |
| 437 | /// 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] | 438 | /// |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 439 | BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, |
Jakub Staszak | 2fac1d5 | 2011-12-09 21:19:53 +0000 | [diff] [blame] | 440 | ArrayRef<BasicBlock*> Preds, |
| 441 | const char *Suffix, Pass *P) { |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 442 | // Create new basic block, insert right before the original block. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 443 | BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), BB->getName()+Suffix, |
| 444 | BB->getParent(), BB); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 445 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 446 | // The new block unconditionally branches to the old block. |
| 447 | BranchInst *BI = BranchInst::Create(BB, NewBB); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 448 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 449 | // Move the edges from Preds to point to NewBB instead of BB. |
Jakub Staszak | 2fac1d5 | 2011-12-09 21:19:53 +0000 | [diff] [blame] | 450 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) { |
Dan Gohman | b8eb17c | 2009-11-05 18:25:44 +0000 | [diff] [blame] | 451 | // This is slightly more strict than necessary; the minimum requirement |
| 452 | // is that there be no more than one indirectbr branching to BB. And |
| 453 | // all BlockAddress uses would need to be updated. |
| 454 | assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) && |
| 455 | "Cannot split an edge from an IndirectBrInst"); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 456 | Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 459 | // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI |
| 460 | // node becomes an incoming value for BB's phi node. However, if the Preds |
| 461 | // list is empty, we need to insert dummy entries into the PHI nodes in BB to |
| 462 | // account for the newly created predecessor. |
Jakub Staszak | 2fac1d5 | 2011-12-09 21:19:53 +0000 | [diff] [blame] | 463 | if (Preds.size() == 0) { |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 464 | // Insert dummy values as the incoming value. |
| 465 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 466 | cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 467 | return NewBB; |
| 468 | } |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 469 | |
Bill Wendling | a644b33 | 2011-08-18 05:25:23 +0000 | [diff] [blame] | 470 | // Update DominatorTree, LoopInfo, and LCCSA analysis information. |
| 471 | bool HasLoopExit = false; |
Jakub Staszak | 2fac1d5 | 2011-12-09 21:19:53 +0000 | [diff] [blame] | 472 | UpdateAnalysisInformation(BB, NewBB, Preds, P, HasLoopExit); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 473 | |
Bill Wendling | 1c44d86 | 2011-08-18 20:51:04 +0000 | [diff] [blame] | 474 | // Update the PHI nodes in BB with the values coming from NewBB. |
Jakub Staszak | 2fac1d5 | 2011-12-09 21:19:53 +0000 | [diff] [blame] | 475 | UpdatePHINodes(BB, NewBB, Preds, BI, P, HasLoopExit); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 476 | return NewBB; |
| 477 | } |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 478 | |
Bill Wendling | 7e8840c | 2011-08-19 00:05:40 +0000 | [diff] [blame] | 479 | /// SplitLandingPadPredecessors - This method transforms the landing pad, |
| 480 | /// OrigBB, by introducing two new basic blocks into the function. One of those |
| 481 | /// new basic blocks gets the predecessors listed in Preds. The other basic |
| 482 | /// block gets the remaining predecessors of OrigBB. The landingpad instruction |
| 483 | /// OrigBB is clone into both of the new basic blocks. The new blocks are given |
| 484 | /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector. |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 485 | /// |
Bill Wendling | 7e8840c | 2011-08-19 00:05:40 +0000 | [diff] [blame] | 486 | /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, |
| 487 | /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular, |
| 488 | /// it does not preserve LoopSimplify (because it's complicated to handle the |
| 489 | /// case where one of the edges being split is an exit of a loop with other |
| 490 | /// exits). |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 491 | /// |
Bill Wendling | 7e8840c | 2011-08-19 00:05:40 +0000 | [diff] [blame] | 492 | void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB, |
| 493 | ArrayRef<BasicBlock*> Preds, |
| 494 | const char *Suffix1, const char *Suffix2, |
| 495 | Pass *P, |
| 496 | SmallVectorImpl<BasicBlock*> &NewBBs) { |
| 497 | assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!"); |
| 498 | |
| 499 | // Create a new basic block for OrigBB's predecessors listed in Preds. Insert |
| 500 | // it right before the original block. |
| 501 | BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(), |
| 502 | OrigBB->getName() + Suffix1, |
| 503 | OrigBB->getParent(), OrigBB); |
| 504 | NewBBs.push_back(NewBB1); |
| 505 | |
| 506 | // The new block unconditionally branches to the old block. |
| 507 | BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1); |
| 508 | |
| 509 | // Move the edges from Preds to point to NewBB1 instead of OrigBB. |
| 510 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) { |
| 511 | // This is slightly more strict than necessary; the minimum requirement |
| 512 | // is that there be no more than one indirectbr branching to BB. And |
| 513 | // all BlockAddress uses would need to be updated. |
| 514 | assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) && |
| 515 | "Cannot split an edge from an IndirectBrInst"); |
| 516 | Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1); |
| 517 | } |
| 518 | |
| 519 | // Update DominatorTree, LoopInfo, and LCCSA analysis information. |
| 520 | bool HasLoopExit = false; |
| 521 | UpdateAnalysisInformation(OrigBB, NewBB1, Preds, P, HasLoopExit); |
| 522 | |
| 523 | // Update the PHI nodes in OrigBB with the values coming from NewBB1. |
| 524 | UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, P, HasLoopExit); |
| 525 | |
Bill Wendling | 7e8840c | 2011-08-19 00:05:40 +0000 | [diff] [blame] | 526 | // Move the remaining edges from OrigBB to point to NewBB2. |
| 527 | SmallVector<BasicBlock*, 8> NewBB2Preds; |
| 528 | for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB); |
| 529 | i != e; ) { |
| 530 | BasicBlock *Pred = *i++; |
Bill Wendling | 94657b9 | 2011-08-19 23:46:30 +0000 | [diff] [blame] | 531 | if (Pred == NewBB1) continue; |
Bill Wendling | 7e8840c | 2011-08-19 00:05:40 +0000 | [diff] [blame] | 532 | assert(!isa<IndirectBrInst>(Pred->getTerminator()) && |
| 533 | "Cannot split an edge from an IndirectBrInst"); |
Bill Wendling | 7e8840c | 2011-08-19 00:05:40 +0000 | [diff] [blame] | 534 | NewBB2Preds.push_back(Pred); |
| 535 | e = pred_end(OrigBB); |
| 536 | } |
| 537 | |
Bill Wendling | 94657b9 | 2011-08-19 23:46:30 +0000 | [diff] [blame] | 538 | BasicBlock *NewBB2 = 0; |
| 539 | if (!NewBB2Preds.empty()) { |
| 540 | // Create another basic block for the rest of OrigBB's predecessors. |
| 541 | NewBB2 = BasicBlock::Create(OrigBB->getContext(), |
| 542 | OrigBB->getName() + Suffix2, |
| 543 | OrigBB->getParent(), OrigBB); |
| 544 | NewBBs.push_back(NewBB2); |
Bill Wendling | 7e8840c | 2011-08-19 00:05:40 +0000 | [diff] [blame] | 545 | |
Bill Wendling | 94657b9 | 2011-08-19 23:46:30 +0000 | [diff] [blame] | 546 | // The new block unconditionally branches to the old block. |
| 547 | BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2); |
| 548 | |
| 549 | // Move the remaining edges from OrigBB to point to NewBB2. |
| 550 | for (SmallVectorImpl<BasicBlock*>::iterator |
| 551 | i = NewBB2Preds.begin(), e = NewBB2Preds.end(); i != e; ++i) |
| 552 | (*i)->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2); |
| 553 | |
| 554 | // Update DominatorTree, LoopInfo, and LCCSA analysis information. |
| 555 | HasLoopExit = false; |
| 556 | UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, P, HasLoopExit); |
| 557 | |
| 558 | // Update the PHI nodes in OrigBB with the values coming from NewBB2. |
| 559 | UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, P, HasLoopExit); |
| 560 | } |
Bill Wendling | 7e8840c | 2011-08-19 00:05:40 +0000 | [diff] [blame] | 561 | |
| 562 | LandingPadInst *LPad = OrigBB->getLandingPadInst(); |
| 563 | Instruction *Clone1 = LPad->clone(); |
| 564 | Clone1->setName(Twine("lpad") + Suffix1); |
| 565 | NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1); |
| 566 | |
Bill Wendling | 94657b9 | 2011-08-19 23:46:30 +0000 | [diff] [blame] | 567 | if (NewBB2) { |
| 568 | Instruction *Clone2 = LPad->clone(); |
| 569 | Clone2->setName(Twine("lpad") + Suffix2); |
| 570 | NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2); |
Bill Wendling | 7e8840c | 2011-08-19 00:05:40 +0000 | [diff] [blame] | 571 | |
Bill Wendling | 94657b9 | 2011-08-19 23:46:30 +0000 | [diff] [blame] | 572 | // Create a PHI node for the two cloned landingpad instructions. |
| 573 | PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad); |
| 574 | PN->addIncoming(Clone1, NewBB1); |
| 575 | PN->addIncoming(Clone2, NewBB2); |
| 576 | LPad->replaceAllUsesWith(PN); |
| 577 | LPad->eraseFromParent(); |
| 578 | } else { |
| 579 | // There is no second clone. Just replace the landing pad with the first |
| 580 | // clone. |
| 581 | LPad->replaceAllUsesWith(Clone1); |
| 582 | LPad->eraseFromParent(); |
| 583 | } |
Bill Wendling | 7e8840c | 2011-08-19 00:05:40 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Evan Cheng | c3f507f | 2011-01-29 04:46:23 +0000 | [diff] [blame] | 586 | /// FoldReturnIntoUncondBranch - This method duplicates the specified return |
| 587 | /// instruction into a predecessor which ends in an unconditional branch. If |
| 588 | /// the return instruction returns a value defined by a PHI, propagate the |
| 589 | /// right value into the return. It returns the new return instruction in the |
| 590 | /// predecessor. |
| 591 | ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, |
| 592 | BasicBlock *Pred) { |
| 593 | Instruction *UncondBranch = Pred->getTerminator(); |
| 594 | // Clone the return and add it to the end of the predecessor. |
| 595 | Instruction *NewRet = RI->clone(); |
| 596 | Pred->getInstList().push_back(NewRet); |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 597 | |
Evan Cheng | c3f507f | 2011-01-29 04:46:23 +0000 | [diff] [blame] | 598 | // If the return instruction returns a value, and if the value was a |
| 599 | // PHI node in "BB", propagate the right value into the return. |
| 600 | for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end(); |
Evan Cheng | 9c777a4 | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 601 | i != e; ++i) { |
| 602 | Value *V = *i; |
| 603 | Instruction *NewBC = 0; |
| 604 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) { |
| 605 | // Return value might be bitcasted. Clone and insert it before the |
| 606 | // return instruction. |
| 607 | V = BCI->getOperand(0); |
| 608 | NewBC = BCI->clone(); |
| 609 | Pred->getInstList().insert(NewRet, NewBC); |
| 610 | *i = NewBC; |
| 611 | } |
| 612 | if (PHINode *PN = dyn_cast<PHINode>(V)) { |
| 613 | if (PN->getParent() == BB) { |
| 614 | if (NewBC) |
| 615 | NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred)); |
| 616 | else |
| 617 | *i = PN->getIncomingValueForBlock(Pred); |
| 618 | } |
| 619 | } |
| 620 | } |
Jakub Staszak | e673b54 | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 621 | |
Evan Cheng | c3f507f | 2011-01-29 04:46:23 +0000 | [diff] [blame] | 622 | // Update any PHI nodes in the returning block to realize that we no |
| 623 | // longer branch to them. |
| 624 | BB->removePredecessor(Pred); |
| 625 | UncondBranch->eraseFromParent(); |
| 626 | return cast<ReturnInst>(NewRet); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 627 | } |
Devang Patel | 40348e8 | 2011-04-29 22:28:59 +0000 | [diff] [blame] | 628 | |
Evgeniy Stepanov | 4a2dec0 | 2012-10-19 10:48:31 +0000 | [diff] [blame] | 629 | /// SplitBlockAndInsertIfThen - Split the containing block at the |
| 630 | /// specified instruction - everything before and including Cmp stays |
| 631 | /// in the old basic block, and everything after Cmp is moved to a |
| 632 | /// new block. The two blocks are connected by a conditional branch |
| 633 | /// (with value of Cmp being the condition). |
| 634 | /// Before: |
| 635 | /// Head |
| 636 | /// Cmp |
| 637 | /// Tail |
| 638 | /// After: |
| 639 | /// Head |
| 640 | /// Cmp |
| 641 | /// if (Cmp) |
| 642 | /// ThenBlock |
| 643 | /// Tail |
| 644 | /// |
| 645 | /// If Unreachable is true, then ThenBlock ends with |
| 646 | /// UnreachableInst, otherwise it branches to Tail. |
| 647 | /// Returns the NewBasicBlock's terminator. |
| 648 | |
| 649 | TerminatorInst *llvm::SplitBlockAndInsertIfThen(Instruction *Cmp, |
| 650 | bool Unreachable, MDNode *BranchWeights) { |
| 651 | Instruction *SplitBefore = Cmp->getNextNode(); |
| 652 | BasicBlock *Head = SplitBefore->getParent(); |
| 653 | BasicBlock *Tail = Head->splitBasicBlock(SplitBefore); |
| 654 | TerminatorInst *HeadOldTerm = Head->getTerminator(); |
| 655 | LLVMContext &C = Head->getContext(); |
| 656 | BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail); |
| 657 | TerminatorInst *CheckTerm; |
| 658 | if (Unreachable) |
| 659 | CheckTerm = new UnreachableInst(C, ThenBlock); |
| 660 | else |
| 661 | CheckTerm = BranchInst::Create(Tail, ThenBlock); |
| 662 | BranchInst *HeadNewTerm = |
| 663 | BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/Tail, Cmp); |
| 664 | HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights); |
| 665 | ReplaceInstWithInst(HeadOldTerm, HeadNewTerm); |
| 666 | return CheckTerm; |
| 667 | } |
Tom Stellard | 01d7203 | 2013-08-06 02:43:45 +0000 | [diff] [blame^] | 668 | |
| 669 | /// GetIfCondition - Given a basic block (BB) with two predecessors, |
| 670 | /// check to see if the merge at this block is due |
| 671 | /// to an "if condition". If so, return the boolean condition that determines |
| 672 | /// which entry into BB will be taken. Also, return by references the block |
| 673 | /// that will be entered from if the condition is true, and the block that will |
| 674 | /// be entered if the condition is false. |
| 675 | /// |
| 676 | /// This does no checking to see if the true/false blocks have large or unsavory |
| 677 | /// instructions in them. |
| 678 | Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, |
| 679 | BasicBlock *&IfFalse) { |
| 680 | PHINode *SomePHI = dyn_cast<PHINode>(BB->begin()); |
| 681 | BasicBlock *Pred1 = NULL; |
| 682 | BasicBlock *Pred2 = NULL; |
| 683 | |
| 684 | if (SomePHI) { |
| 685 | if (SomePHI->getNumIncomingValues() != 2) |
| 686 | return NULL; |
| 687 | Pred1 = SomePHI->getIncomingBlock(0); |
| 688 | Pred2 = SomePHI->getIncomingBlock(1); |
| 689 | } else { |
| 690 | pred_iterator PI = pred_begin(BB), PE = pred_end(BB); |
| 691 | if (PI == PE) // No predecessor |
| 692 | return NULL; |
| 693 | Pred1 = *PI++; |
| 694 | if (PI == PE) // Only one predecessor |
| 695 | return NULL; |
| 696 | Pred2 = *PI++; |
| 697 | if (PI != PE) // More than two predecessors |
| 698 | return NULL; |
| 699 | } |
| 700 | |
| 701 | // We can only handle branches. Other control flow will be lowered to |
| 702 | // branches if possible anyway. |
| 703 | BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator()); |
| 704 | BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator()); |
| 705 | if (Pred1Br == 0 || Pred2Br == 0) |
| 706 | return 0; |
| 707 | |
| 708 | // Eliminate code duplication by ensuring that Pred1Br is conditional if |
| 709 | // either are. |
| 710 | if (Pred2Br->isConditional()) { |
| 711 | // If both branches are conditional, we don't have an "if statement". In |
| 712 | // reality, we could transform this case, but since the condition will be |
| 713 | // required anyway, we stand no chance of eliminating it, so the xform is |
| 714 | // probably not profitable. |
| 715 | if (Pred1Br->isConditional()) |
| 716 | return 0; |
| 717 | |
| 718 | std::swap(Pred1, Pred2); |
| 719 | std::swap(Pred1Br, Pred2Br); |
| 720 | } |
| 721 | |
| 722 | if (Pred1Br->isConditional()) { |
| 723 | // The only thing we have to watch out for here is to make sure that Pred2 |
| 724 | // doesn't have incoming edges from other blocks. If it does, the condition |
| 725 | // doesn't dominate BB. |
| 726 | if (Pred2->getSinglePredecessor() == 0) |
| 727 | return 0; |
| 728 | |
| 729 | // If we found a conditional branch predecessor, make sure that it branches |
| 730 | // to BB and Pred2Br. If it doesn't, this isn't an "if statement". |
| 731 | if (Pred1Br->getSuccessor(0) == BB && |
| 732 | Pred1Br->getSuccessor(1) == Pred2) { |
| 733 | IfTrue = Pred1; |
| 734 | IfFalse = Pred2; |
| 735 | } else if (Pred1Br->getSuccessor(0) == Pred2 && |
| 736 | Pred1Br->getSuccessor(1) == BB) { |
| 737 | IfTrue = Pred2; |
| 738 | IfFalse = Pred1; |
| 739 | } else { |
| 740 | // We know that one arm of the conditional goes to BB, so the other must |
| 741 | // go somewhere unrelated, and this must not be an "if statement". |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | return Pred1Br->getCondition(); |
| 746 | } |
| 747 | |
| 748 | // Ok, if we got here, both predecessors end with an unconditional branch to |
| 749 | // BB. Don't panic! If both blocks only have a single (identical) |
| 750 | // predecessor, and THAT is a conditional branch, then we're all ok! |
| 751 | BasicBlock *CommonPred = Pred1->getSinglePredecessor(); |
| 752 | if (CommonPred == 0 || CommonPred != Pred2->getSinglePredecessor()) |
| 753 | return 0; |
| 754 | |
| 755 | // Otherwise, if this is a conditional branch, then we can use it! |
| 756 | BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator()); |
| 757 | if (BI == 0) return 0; |
| 758 | |
| 759 | assert(BI->isConditional() && "Two successors but not conditional?"); |
| 760 | if (BI->getSuccessor(0) == Pred1) { |
| 761 | IfTrue = Pred1; |
| 762 | IfFalse = Pred2; |
| 763 | } else { |
| 764 | IfTrue = Pred2; |
| 765 | IfFalse = Pred1; |
| 766 | } |
| 767 | return BI->getCondition(); |
| 768 | } |