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