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