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