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