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