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