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