[AMDGPU] Test codegen'ing True16 additions.
[llvm-project.git] / llvm / lib / Transforms / Utils / BasicBlockUtils.cpp
blob389aab39dec34094a126b8480067e9d583cc604f
1 //===- BasicBlockUtils.cpp - BasicBlock Utilities --------------------------==//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This family of functions perform manipulations on basic blocks, and
10 // instructions contained within basic blocks.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Analysis/CFG.h"
20 #include "llvm/Analysis/DomTreeUpdater.h"
21 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
23 #include "llvm/Analysis/MemorySSAUpdater.h"
24 #include "llvm/IR/BasicBlock.h"
25 #include "llvm/IR/CFG.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DebugInfo.h"
28 #include "llvm/IR/DebugInfoMetadata.h"
29 #include "llvm/IR/Dominators.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/InstrTypes.h"
32 #include "llvm/IR/Instruction.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/IntrinsicInst.h"
35 #include "llvm/IR/IRBuilder.h"
36 #include "llvm/IR/LLVMContext.h"
37 #include "llvm/IR/Type.h"
38 #include "llvm/IR/User.h"
39 #include "llvm/IR/Value.h"
40 #include "llvm/IR/ValueHandle.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include "llvm/Transforms/Utils/Local.h"
46 #include <cassert>
47 #include <cstdint>
48 #include <string>
49 #include <utility>
50 #include <vector>
52 using namespace llvm;
54 #define DEBUG_TYPE "basicblock-utils"
56 static cl::opt<unsigned> MaxDeoptOrUnreachableSuccessorCheckDepth(
57 "max-deopt-or-unreachable-succ-check-depth", cl::init(8), cl::Hidden,
58 cl::desc("Set the maximum path length when checking whether a basic block "
59 "is followed by a block that either has a terminating "
60 "deoptimizing call or is terminated with an unreachable"));
62 void llvm::detachDeadBlocks(
63 ArrayRef<BasicBlock *> BBs,
64 SmallVectorImpl<DominatorTree::UpdateType> *Updates,
65 bool KeepOneInputPHIs) {
66 for (auto *BB : BBs) {
67 // Loop through all of our successors and make sure they know that one
68 // of their predecessors is going away.
69 SmallPtrSet<BasicBlock *, 4> UniqueSuccessors;
70 for (BasicBlock *Succ : successors(BB)) {
71 Succ->removePredecessor(BB, KeepOneInputPHIs);
72 if (Updates && UniqueSuccessors.insert(Succ).second)
73 Updates->push_back({DominatorTree::Delete, BB, Succ});
76 // Zap all the instructions in the block.
77 while (!BB->empty()) {
78 Instruction &I = BB->back();
79 // If this instruction is used, replace uses with an arbitrary value.
80 // Because control flow can't get here, we don't care what we replace the
81 // value with. Note that since this block is unreachable, and all values
82 // contained within it must dominate their uses, that all uses will
83 // eventually be removed (they are themselves dead).
84 if (!I.use_empty())
85 I.replaceAllUsesWith(PoisonValue::get(I.getType()));
86 BB->back().eraseFromParent();
88 new UnreachableInst(BB->getContext(), BB);
89 assert(BB->size() == 1 &&
90 isa<UnreachableInst>(BB->getTerminator()) &&
91 "The successor list of BB isn't empty before "
92 "applying corresponding DTU updates.");
96 void llvm::DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU,
97 bool KeepOneInputPHIs) {
98 DeleteDeadBlocks({BB}, DTU, KeepOneInputPHIs);
101 void llvm::DeleteDeadBlocks(ArrayRef <BasicBlock *> BBs, DomTreeUpdater *DTU,
102 bool KeepOneInputPHIs) {
103 #ifndef NDEBUG
104 // Make sure that all predecessors of each dead block is also dead.
105 SmallPtrSet<BasicBlock *, 4> Dead(BBs.begin(), BBs.end());
106 assert(Dead.size() == BBs.size() && "Duplicating blocks?");
107 for (auto *BB : Dead)
108 for (BasicBlock *Pred : predecessors(BB))
109 assert(Dead.count(Pred) && "All predecessors must be dead!");
110 #endif
112 SmallVector<DominatorTree::UpdateType, 4> Updates;
113 detachDeadBlocks(BBs, DTU ? &Updates : nullptr, KeepOneInputPHIs);
115 if (DTU)
116 DTU->applyUpdates(Updates);
118 for (BasicBlock *BB : BBs)
119 if (DTU)
120 DTU->deleteBB(BB);
121 else
122 BB->eraseFromParent();
125 bool llvm::EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU,
126 bool KeepOneInputPHIs) {
127 df_iterator_default_set<BasicBlock*> Reachable;
129 // Mark all reachable blocks.
130 for (BasicBlock *BB : depth_first_ext(&F, Reachable))
131 (void)BB/* Mark all reachable blocks */;
133 // Collect all dead blocks.
134 std::vector<BasicBlock*> DeadBlocks;
135 for (BasicBlock &BB : F)
136 if (!Reachable.count(&BB))
137 DeadBlocks.push_back(&BB);
139 // Delete the dead blocks.
140 DeleteDeadBlocks(DeadBlocks, DTU, KeepOneInputPHIs);
142 return !DeadBlocks.empty();
145 bool llvm::FoldSingleEntryPHINodes(BasicBlock *BB,
146 MemoryDependenceResults *MemDep) {
147 if (!isa<PHINode>(BB->begin()))
148 return false;
150 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
151 if (PN->getIncomingValue(0) != PN)
152 PN->replaceAllUsesWith(PN->getIncomingValue(0));
153 else
154 PN->replaceAllUsesWith(PoisonValue::get(PN->getType()));
156 if (MemDep)
157 MemDep->removeInstruction(PN); // Memdep updates AA itself.
159 PN->eraseFromParent();
161 return true;
164 bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI,
165 MemorySSAUpdater *MSSAU) {
166 // Recursively deleting a PHI may cause multiple PHIs to be deleted
167 // or RAUW'd undef, so use an array of WeakTrackingVH for the PHIs to delete.
168 SmallVector<WeakTrackingVH, 8> PHIs;
169 for (PHINode &PN : BB->phis())
170 PHIs.push_back(&PN);
172 bool Changed = false;
173 for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
174 if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*()))
175 Changed |= RecursivelyDeleteDeadPHINode(PN, TLI, MSSAU);
177 return Changed;
180 bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU,
181 LoopInfo *LI, MemorySSAUpdater *MSSAU,
182 MemoryDependenceResults *MemDep,
183 bool PredecessorWithTwoSuccessors,
184 DominatorTree *DT) {
185 if (BB->hasAddressTaken())
186 return false;
188 // Can't merge if there are multiple predecessors, or no predecessors.
189 BasicBlock *PredBB = BB->getUniquePredecessor();
190 if (!PredBB) return false;
192 // Don't break self-loops.
193 if (PredBB == BB) return false;
195 // Don't break unwinding instructions or terminators with other side-effects.
196 Instruction *PTI = PredBB->getTerminator();
197 if (PTI->isSpecialTerminator() || PTI->mayHaveSideEffects())
198 return false;
200 // Can't merge if there are multiple distinct successors.
201 if (!PredecessorWithTwoSuccessors && PredBB->getUniqueSuccessor() != BB)
202 return false;
204 // Currently only allow PredBB to have two predecessors, one being BB.
205 // Update BI to branch to BB's only successor instead of BB.
206 BranchInst *PredBB_BI;
207 BasicBlock *NewSucc = nullptr;
208 unsigned FallThruPath;
209 if (PredecessorWithTwoSuccessors) {
210 if (!(PredBB_BI = dyn_cast<BranchInst>(PTI)))
211 return false;
212 BranchInst *BB_JmpI = dyn_cast<BranchInst>(BB->getTerminator());
213 if (!BB_JmpI || !BB_JmpI->isUnconditional())
214 return false;
215 NewSucc = BB_JmpI->getSuccessor(0);
216 FallThruPath = PredBB_BI->getSuccessor(0) == BB ? 0 : 1;
219 // Can't merge if there is PHI loop.
220 for (PHINode &PN : BB->phis())
221 if (llvm::is_contained(PN.incoming_values(), &PN))
222 return false;
224 LLVM_DEBUG(dbgs() << "Merging: " << BB->getName() << " into "
225 << PredBB->getName() << "\n");
227 // Begin by getting rid of unneeded PHIs.
228 SmallVector<AssertingVH<Value>, 4> IncomingValues;
229 if (isa<PHINode>(BB->front())) {
230 for (PHINode &PN : BB->phis())
231 if (!isa<PHINode>(PN.getIncomingValue(0)) ||
232 cast<PHINode>(PN.getIncomingValue(0))->getParent() != BB)
233 IncomingValues.push_back(PN.getIncomingValue(0));
234 FoldSingleEntryPHINodes(BB, MemDep);
237 if (DT) {
238 assert(!DTU && "cannot use both DT and DTU for updates");
239 DomTreeNode *PredNode = DT->getNode(PredBB);
240 DomTreeNode *BBNode = DT->getNode(BB);
241 if (PredNode) {
242 assert(BBNode && "PredNode unreachable but BBNode reachable?");
243 for (DomTreeNode *C : to_vector(BBNode->children()))
244 C->setIDom(PredNode);
247 // DTU update: Collect all the edges that exit BB.
248 // These dominator edges will be redirected from Pred.
249 std::vector<DominatorTree::UpdateType> Updates;
250 if (DTU) {
251 assert(!DT && "cannot use both DT and DTU for updates");
252 // To avoid processing the same predecessor more than once.
253 SmallPtrSet<BasicBlock *, 8> SeenSuccs;
254 SmallPtrSet<BasicBlock *, 2> SuccsOfPredBB(succ_begin(PredBB),
255 succ_end(PredBB));
256 Updates.reserve(Updates.size() + 2 * succ_size(BB) + 1);
257 // Add insert edges first. Experimentally, for the particular case of two
258 // blocks that can be merged, with a single successor and single predecessor
259 // respectively, it is beneficial to have all insert updates first. Deleting
260 // edges first may lead to unreachable blocks, followed by inserting edges
261 // making the blocks reachable again. Such DT updates lead to high compile
262 // times. We add inserts before deletes here to reduce compile time.
263 for (BasicBlock *SuccOfBB : successors(BB))
264 // This successor of BB may already be a PredBB's successor.
265 if (!SuccsOfPredBB.contains(SuccOfBB))
266 if (SeenSuccs.insert(SuccOfBB).second)
267 Updates.push_back({DominatorTree::Insert, PredBB, SuccOfBB});
268 SeenSuccs.clear();
269 for (BasicBlock *SuccOfBB : successors(BB))
270 if (SeenSuccs.insert(SuccOfBB).second)
271 Updates.push_back({DominatorTree::Delete, BB, SuccOfBB});
272 Updates.push_back({DominatorTree::Delete, PredBB, BB});
275 Instruction *STI = BB->getTerminator();
276 Instruction *Start = &*BB->begin();
277 // If there's nothing to move, mark the starting instruction as the last
278 // instruction in the block. Terminator instruction is handled separately.
279 if (Start == STI)
280 Start = PTI;
282 // Move all definitions in the successor to the predecessor...
283 PredBB->splice(PTI->getIterator(), BB, BB->begin(), STI->getIterator());
285 if (MSSAU)
286 MSSAU->moveAllAfterMergeBlocks(BB, PredBB, Start);
288 // Make all PHI nodes that referred to BB now refer to Pred as their
289 // source...
290 BB->replaceAllUsesWith(PredBB);
292 if (PredecessorWithTwoSuccessors) {
293 // Delete the unconditional branch from BB.
294 BB->back().eraseFromParent();
296 // Update branch in the predecessor.
297 PredBB_BI->setSuccessor(FallThruPath, NewSucc);
298 } else {
299 // Delete the unconditional branch from the predecessor.
300 PredBB->back().eraseFromParent();
302 // Move terminator instruction.
303 BB->back().moveBeforePreserving(*PredBB, PredBB->end());
305 // Terminator may be a memory accessing instruction too.
306 if (MSSAU)
307 if (MemoryUseOrDef *MUD = cast_or_null<MemoryUseOrDef>(
308 MSSAU->getMemorySSA()->getMemoryAccess(PredBB->getTerminator())))
309 MSSAU->moveToPlace(MUD, PredBB, MemorySSA::End);
311 // Add unreachable to now empty BB.
312 new UnreachableInst(BB->getContext(), BB);
314 // Inherit predecessors name if it exists.
315 if (!PredBB->hasName())
316 PredBB->takeName(BB);
318 if (LI)
319 LI->removeBlock(BB);
321 if (MemDep)
322 MemDep->invalidateCachedPredecessors();
324 if (DTU)
325 DTU->applyUpdates(Updates);
327 if (DT) {
328 assert(succ_empty(BB) &&
329 "successors should have been transferred to PredBB");
330 DT->eraseNode(BB);
333 // Finally, erase the old block and update dominator info.
334 DeleteDeadBlock(BB, DTU);
336 return true;
339 bool llvm::MergeBlockSuccessorsIntoGivenBlocks(
340 SmallPtrSetImpl<BasicBlock *> &MergeBlocks, Loop *L, DomTreeUpdater *DTU,
341 LoopInfo *LI) {
342 assert(!MergeBlocks.empty() && "MergeBlocks should not be empty");
344 bool BlocksHaveBeenMerged = false;
345 while (!MergeBlocks.empty()) {
346 BasicBlock *BB = *MergeBlocks.begin();
347 BasicBlock *Dest = BB->getSingleSuccessor();
348 if (Dest && (!L || L->contains(Dest))) {
349 BasicBlock *Fold = Dest->getUniquePredecessor();
350 (void)Fold;
351 if (MergeBlockIntoPredecessor(Dest, DTU, LI)) {
352 assert(Fold == BB &&
353 "Expecting BB to be unique predecessor of the Dest block");
354 MergeBlocks.erase(Dest);
355 BlocksHaveBeenMerged = true;
356 } else
357 MergeBlocks.erase(BB);
358 } else
359 MergeBlocks.erase(BB);
361 return BlocksHaveBeenMerged;
364 /// Remove redundant instructions within sequences of consecutive dbg.value
365 /// instructions. This is done using a backward scan to keep the last dbg.value
366 /// describing a specific variable/fragment.
368 /// BackwardScan strategy:
369 /// ----------------------
370 /// Given a sequence of consecutive DbgValueInst like this
372 /// dbg.value ..., "x", FragmentX1 (*)
373 /// dbg.value ..., "y", FragmentY1
374 /// dbg.value ..., "x", FragmentX2
375 /// dbg.value ..., "x", FragmentX1 (**)
377 /// then the instruction marked with (*) can be removed (it is guaranteed to be
378 /// obsoleted by the instruction marked with (**) as the latter instruction is
379 /// describing the same variable using the same fragment info).
381 /// Possible improvements:
382 /// - Check fully overlapping fragments and not only identical fragments.
383 /// - Support dbg.declare. dbg.label, and possibly other meta instructions being
384 /// part of the sequence of consecutive instructions.
385 static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
386 SmallVector<DbgValueInst *, 8> ToBeRemoved;
387 SmallDenseSet<DebugVariable> VariableSet;
388 for (auto &I : reverse(*BB)) {
389 if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {
390 DebugVariable Key(DVI->getVariable(),
391 DVI->getExpression(),
392 DVI->getDebugLoc()->getInlinedAt());
393 auto R = VariableSet.insert(Key);
394 // If the variable fragment hasn't been seen before then we don't want
395 // to remove this dbg intrinsic.
396 if (R.second)
397 continue;
399 if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI)) {
400 // Don't delete dbg.assign intrinsics that are linked to instructions.
401 if (!at::getAssignmentInsts(DAI).empty())
402 continue;
403 // Unlinked dbg.assign intrinsics can be treated like dbg.values.
406 // If the same variable fragment is described more than once it is enough
407 // to keep the last one (i.e. the first found since we for reverse
408 // iteration).
409 ToBeRemoved.push_back(DVI);
410 continue;
412 // Sequence with consecutive dbg.value instrs ended. Clear the map to
413 // restart identifying redundant instructions if case we find another
414 // dbg.value sequence.
415 VariableSet.clear();
418 for (auto &Instr : ToBeRemoved)
419 Instr->eraseFromParent();
421 return !ToBeRemoved.empty();
424 /// Remove redundant dbg.value instructions using a forward scan. This can
425 /// remove a dbg.value instruction that is redundant due to indicating that a
426 /// variable has the same value as already being indicated by an earlier
427 /// dbg.value.
429 /// ForwardScan strategy:
430 /// ---------------------
431 /// Given two identical dbg.value instructions, separated by a block of
432 /// instructions that isn't describing the same variable, like this
434 /// dbg.value X1, "x", FragmentX1 (**)
435 /// <block of instructions, none being "dbg.value ..., "x", ...">
436 /// dbg.value X1, "x", FragmentX1 (*)
438 /// then the instruction marked with (*) can be removed. Variable "x" is already
439 /// described as being mapped to the SSA value X1.
441 /// Possible improvements:
442 /// - Keep track of non-overlapping fragments.
443 static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
444 SmallVector<DbgValueInst *, 8> ToBeRemoved;
445 DenseMap<DebugVariable, std::pair<SmallVector<Value *, 4>, DIExpression *>>
446 VariableMap;
447 for (auto &I : *BB) {
448 if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {
449 DebugVariable Key(DVI->getVariable(), std::nullopt,
450 DVI->getDebugLoc()->getInlinedAt());
451 auto VMI = VariableMap.find(Key);
452 auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI);
453 // A dbg.assign with no linked instructions can be treated like a
454 // dbg.value (i.e. can be deleted).
455 bool IsDbgValueKind = (!DAI || at::getAssignmentInsts(DAI).empty());
457 // Update the map if we found a new value/expression describing the
458 // variable, or if the variable wasn't mapped already.
459 SmallVector<Value *, 4> Values(DVI->getValues());
460 if (VMI == VariableMap.end() || VMI->second.first != Values ||
461 VMI->second.second != DVI->getExpression()) {
462 // Use a sentinal value (nullptr) for the DIExpression when we see a
463 // linked dbg.assign so that the next debug intrinsic will never match
464 // it (i.e. always treat linked dbg.assigns as if they're unique).
465 if (IsDbgValueKind)
466 VariableMap[Key] = {Values, DVI->getExpression()};
467 else
468 VariableMap[Key] = {Values, nullptr};
469 continue;
472 // Don't delete dbg.assign intrinsics that are linked to instructions.
473 if (!IsDbgValueKind)
474 continue;
475 ToBeRemoved.push_back(DVI);
479 for (auto &Instr : ToBeRemoved)
480 Instr->eraseFromParent();
482 return !ToBeRemoved.empty();
485 /// Remove redundant undef dbg.assign intrinsic from an entry block using a
486 /// forward scan.
487 /// Strategy:
488 /// ---------------------
489 /// Scanning forward, delete dbg.assign intrinsics iff they are undef, not
490 /// linked to an intrinsic, and don't share an aggregate variable with a debug
491 /// intrinsic that didn't meet the criteria. In other words, undef dbg.assigns
492 /// that come before non-undef debug intrinsics for the variable are
493 /// deleted. Given:
495 /// dbg.assign undef, "x", FragmentX1 (*)
496 /// <block of instructions, none being "dbg.value ..., "x", ...">
497 /// dbg.value %V, "x", FragmentX2
498 /// <block of instructions, none being "dbg.value ..., "x", ...">
499 /// dbg.assign undef, "x", FragmentX1
501 /// then (only) the instruction marked with (*) can be removed.
502 /// Possible improvements:
503 /// - Keep track of non-overlapping fragments.
504 static bool remomveUndefDbgAssignsFromEntryBlock(BasicBlock *BB) {
505 assert(BB->isEntryBlock() && "expected entry block");
506 SmallVector<DbgAssignIntrinsic *, 8> ToBeRemoved;
507 DenseSet<DebugVariable> SeenDefForAggregate;
508 // Returns the DebugVariable for DVI with no fragment info.
509 auto GetAggregateVariable = [](DbgValueInst *DVI) {
510 return DebugVariable(DVI->getVariable(), std::nullopt,
511 DVI->getDebugLoc()->getInlinedAt());
514 // Remove undef dbg.assign intrinsics that are encountered before
515 // any non-undef intrinsics from the entry block.
516 for (auto &I : *BB) {
517 DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I);
518 if (!DVI)
519 continue;
520 auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI);
521 bool IsDbgValueKind = (!DAI || at::getAssignmentInsts(DAI).empty());
522 DebugVariable Aggregate = GetAggregateVariable(DVI);
523 if (!SeenDefForAggregate.contains(Aggregate)) {
524 bool IsKill = DVI->isKillLocation() && IsDbgValueKind;
525 if (!IsKill) {
526 SeenDefForAggregate.insert(Aggregate);
527 } else if (DAI) {
528 ToBeRemoved.push_back(DAI);
533 for (DbgAssignIntrinsic *DAI : ToBeRemoved)
534 DAI->eraseFromParent();
536 return !ToBeRemoved.empty();
539 bool llvm::RemoveRedundantDbgInstrs(BasicBlock *BB) {
540 bool MadeChanges = false;
541 // By using the "backward scan" strategy before the "forward scan" strategy we
542 // can remove both dbg.value (2) and (3) in a situation like this:
544 // (1) dbg.value V1, "x", DIExpression()
545 // ...
546 // (2) dbg.value V2, "x", DIExpression()
547 // (3) dbg.value V1, "x", DIExpression()
549 // The backward scan will remove (2), it is made obsolete by (3). After
550 // getting (2) out of the way, the foward scan will remove (3) since "x"
551 // already is described as having the value V1 at (1).
552 MadeChanges |= removeRedundantDbgInstrsUsingBackwardScan(BB);
553 if (BB->isEntryBlock() &&
554 isAssignmentTrackingEnabled(*BB->getParent()->getParent()))
555 MadeChanges |= remomveUndefDbgAssignsFromEntryBlock(BB);
556 MadeChanges |= removeRedundantDbgInstrsUsingForwardScan(BB);
558 if (MadeChanges)
559 LLVM_DEBUG(dbgs() << "Removed redundant dbg instrs from: "
560 << BB->getName() << "\n");
561 return MadeChanges;
564 void llvm::ReplaceInstWithValue(BasicBlock::iterator &BI, Value *V) {
565 Instruction &I = *BI;
566 // Replaces all of the uses of the instruction with uses of the value
567 I.replaceAllUsesWith(V);
569 // Make sure to propagate a name if there is one already.
570 if (I.hasName() && !V->hasName())
571 V->takeName(&I);
573 // Delete the unnecessary instruction now...
574 BI = BI->eraseFromParent();
577 void llvm::ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI,
578 Instruction *I) {
579 assert(I->getParent() == nullptr &&
580 "ReplaceInstWithInst: Instruction already inserted into basic block!");
582 // Copy debug location to newly added instruction, if it wasn't already set
583 // by the caller.
584 if (!I->getDebugLoc())
585 I->setDebugLoc(BI->getDebugLoc());
587 // Insert the new instruction into the basic block...
588 BasicBlock::iterator New = I->insertInto(BB, BI);
590 // Replace all uses of the old instruction, and delete it.
591 ReplaceInstWithValue(BI, I);
593 // Move BI back to point to the newly inserted instruction
594 BI = New;
597 bool llvm::IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB) {
598 // Remember visited blocks to avoid infinite loop
599 SmallPtrSet<const BasicBlock *, 8> VisitedBlocks;
600 unsigned Depth = 0;
601 while (BB && Depth++ < MaxDeoptOrUnreachableSuccessorCheckDepth &&
602 VisitedBlocks.insert(BB).second) {
603 if (isa<UnreachableInst>(BB->getTerminator()) ||
604 BB->getTerminatingDeoptimizeCall())
605 return true;
606 BB = BB->getUniqueSuccessor();
608 return false;
611 void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) {
612 BasicBlock::iterator BI(From);
613 ReplaceInstWithInst(From->getParent(), BI, To);
616 BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT,
617 LoopInfo *LI, MemorySSAUpdater *MSSAU,
618 const Twine &BBName) {
619 unsigned SuccNum = GetSuccessorNumber(BB, Succ);
621 Instruction *LatchTerm = BB->getTerminator();
623 CriticalEdgeSplittingOptions Options =
624 CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA();
626 if ((isCriticalEdge(LatchTerm, SuccNum, Options.MergeIdenticalEdges))) {
627 // If it is a critical edge, and the succesor is an exception block, handle
628 // the split edge logic in this specific function
629 if (Succ->isEHPad())
630 return ehAwareSplitEdge(BB, Succ, nullptr, nullptr, Options, BBName);
632 // If this is a critical edge, let SplitKnownCriticalEdge do it.
633 return SplitKnownCriticalEdge(LatchTerm, SuccNum, Options, BBName);
636 // If the edge isn't critical, then BB has a single successor or Succ has a
637 // single pred. Split the block.
638 if (BasicBlock *SP = Succ->getSinglePredecessor()) {
639 // If the successor only has a single pred, split the top of the successor
640 // block.
641 assert(SP == BB && "CFG broken");
642 SP = nullptr;
643 return SplitBlock(Succ, &Succ->front(), DT, LI, MSSAU, BBName,
644 /*Before=*/true);
647 // Otherwise, if BB has a single successor, split it at the bottom of the
648 // block.
649 assert(BB->getTerminator()->getNumSuccessors() == 1 &&
650 "Should have a single succ!");
651 return SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU, BBName);
654 void llvm::setUnwindEdgeTo(Instruction *TI, BasicBlock *Succ) {
655 if (auto *II = dyn_cast<InvokeInst>(TI))
656 II->setUnwindDest(Succ);
657 else if (auto *CS = dyn_cast<CatchSwitchInst>(TI))
658 CS->setUnwindDest(Succ);
659 else if (auto *CR = dyn_cast<CleanupReturnInst>(TI))
660 CR->setUnwindDest(Succ);
661 else
662 llvm_unreachable("unexpected terminator instruction");
665 void llvm::updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred,
666 BasicBlock *NewPred, PHINode *Until) {
667 int BBIdx = 0;
668 for (PHINode &PN : DestBB->phis()) {
669 // We manually update the LandingPadReplacement PHINode and it is the last
670 // PHI Node. So, if we find it, we are done.
671 if (Until == &PN)
672 break;
674 // Reuse the previous value of BBIdx if it lines up. In cases where we
675 // have multiple phi nodes with *lots* of predecessors, this is a speed
676 // win because we don't have to scan the PHI looking for TIBB. This
677 // happens because the BB list of PHI nodes are usually in the same
678 // order.
679 if (PN.getIncomingBlock(BBIdx) != OldPred)
680 BBIdx = PN.getBasicBlockIndex(OldPred);
682 assert(BBIdx != -1 && "Invalid PHI Index!");
683 PN.setIncomingBlock(BBIdx, NewPred);
687 BasicBlock *llvm::ehAwareSplitEdge(BasicBlock *BB, BasicBlock *Succ,
688 LandingPadInst *OriginalPad,
689 PHINode *LandingPadReplacement,
690 const CriticalEdgeSplittingOptions &Options,
691 const Twine &BBName) {
693 auto *PadInst = Succ->getFirstNonPHI();
694 if (!LandingPadReplacement && !PadInst->isEHPad())
695 return SplitEdge(BB, Succ, Options.DT, Options.LI, Options.MSSAU, BBName);
697 auto *LI = Options.LI;
698 SmallVector<BasicBlock *, 4> LoopPreds;
699 // Check if extra modifications will be required to preserve loop-simplify
700 // form after splitting. If it would require splitting blocks with IndirectBr
701 // terminators, bail out if preserving loop-simplify form is requested.
702 if (Options.PreserveLoopSimplify && LI) {
703 if (Loop *BBLoop = LI->getLoopFor(BB)) {
705 // The only way that we can break LoopSimplify form by splitting a
706 // critical edge is when there exists some edge from BBLoop to Succ *and*
707 // the only edge into Succ from outside of BBLoop is that of NewBB after
708 // the split. If the first isn't true, then LoopSimplify still holds,
709 // NewBB is the new exit block and it has no non-loop predecessors. If the
710 // second isn't true, then Succ was not in LoopSimplify form prior to
711 // the split as it had a non-loop predecessor. In both of these cases,
712 // the predecessor must be directly in BBLoop, not in a subloop, or again
713 // LoopSimplify doesn't hold.
714 for (BasicBlock *P : predecessors(Succ)) {
715 if (P == BB)
716 continue; // The new block is known.
717 if (LI->getLoopFor(P) != BBLoop) {
718 // Loop is not in LoopSimplify form, no need to re simplify after
719 // splitting edge.
720 LoopPreds.clear();
721 break;
723 LoopPreds.push_back(P);
725 // Loop-simplify form can be preserved, if we can split all in-loop
726 // predecessors.
727 if (any_of(LoopPreds, [](BasicBlock *Pred) {
728 return isa<IndirectBrInst>(Pred->getTerminator());
729 })) {
730 return nullptr;
735 auto *NewBB =
736 BasicBlock::Create(BB->getContext(), BBName, BB->getParent(), Succ);
737 setUnwindEdgeTo(BB->getTerminator(), NewBB);
738 updatePhiNodes(Succ, BB, NewBB, LandingPadReplacement);
740 if (LandingPadReplacement) {
741 auto *NewLP = OriginalPad->clone();
742 auto *Terminator = BranchInst::Create(Succ, NewBB);
743 NewLP->insertBefore(Terminator);
744 LandingPadReplacement->addIncoming(NewLP, NewBB);
745 } else {
746 Value *ParentPad = nullptr;
747 if (auto *FuncletPad = dyn_cast<FuncletPadInst>(PadInst))
748 ParentPad = FuncletPad->getParentPad();
749 else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(PadInst))
750 ParentPad = CatchSwitch->getParentPad();
751 else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(PadInst))
752 ParentPad = CleanupPad->getParentPad();
753 else if (auto *LandingPad = dyn_cast<LandingPadInst>(PadInst))
754 ParentPad = LandingPad->getParent();
755 else
756 llvm_unreachable("handling for other EHPads not implemented yet");
758 auto *NewCleanupPad = CleanupPadInst::Create(ParentPad, {}, BBName, NewBB);
759 CleanupReturnInst::Create(NewCleanupPad, Succ, NewBB);
762 auto *DT = Options.DT;
763 auto *MSSAU = Options.MSSAU;
764 if (!DT && !LI)
765 return NewBB;
767 if (DT) {
768 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
769 SmallVector<DominatorTree::UpdateType, 3> Updates;
771 Updates.push_back({DominatorTree::Insert, BB, NewBB});
772 Updates.push_back({DominatorTree::Insert, NewBB, Succ});
773 Updates.push_back({DominatorTree::Delete, BB, Succ});
775 DTU.applyUpdates(Updates);
776 DTU.flush();
778 if (MSSAU) {
779 MSSAU->applyUpdates(Updates, *DT);
780 if (VerifyMemorySSA)
781 MSSAU->getMemorySSA()->verifyMemorySSA();
785 if (LI) {
786 if (Loop *BBLoop = LI->getLoopFor(BB)) {
787 // If one or the other blocks were not in a loop, the new block is not
788 // either, and thus LI doesn't need to be updated.
789 if (Loop *SuccLoop = LI->getLoopFor(Succ)) {
790 if (BBLoop == SuccLoop) {
791 // Both in the same loop, the NewBB joins loop.
792 SuccLoop->addBasicBlockToLoop(NewBB, *LI);
793 } else if (BBLoop->contains(SuccLoop)) {
794 // Edge from an outer loop to an inner loop. Add to the outer loop.
795 BBLoop->addBasicBlockToLoop(NewBB, *LI);
796 } else if (SuccLoop->contains(BBLoop)) {
797 // Edge from an inner loop to an outer loop. Add to the outer loop.
798 SuccLoop->addBasicBlockToLoop(NewBB, *LI);
799 } else {
800 // Edge from two loops with no containment relation. Because these
801 // are natural loops, we know that the destination block must be the
802 // header of its loop (adding a branch into a loop elsewhere would
803 // create an irreducible loop).
804 assert(SuccLoop->getHeader() == Succ &&
805 "Should not create irreducible loops!");
806 if (Loop *P = SuccLoop->getParentLoop())
807 P->addBasicBlockToLoop(NewBB, *LI);
811 // If BB is in a loop and Succ is outside of that loop, we may need to
812 // update LoopSimplify form and LCSSA form.
813 if (!BBLoop->contains(Succ)) {
814 assert(!BBLoop->contains(NewBB) &&
815 "Split point for loop exit is contained in loop!");
817 // Update LCSSA form in the newly created exit block.
818 if (Options.PreserveLCSSA) {
819 createPHIsForSplitLoopExit(BB, NewBB, Succ);
822 if (!LoopPreds.empty()) {
823 BasicBlock *NewExitBB = SplitBlockPredecessors(
824 Succ, LoopPreds, "split", DT, LI, MSSAU, Options.PreserveLCSSA);
825 if (Options.PreserveLCSSA)
826 createPHIsForSplitLoopExit(LoopPreds, NewExitBB, Succ);
832 return NewBB;
835 void llvm::createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds,
836 BasicBlock *SplitBB, BasicBlock *DestBB) {
837 // SplitBB shouldn't have anything non-trivial in it yet.
838 assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() ||
839 SplitBB->isLandingPad()) &&
840 "SplitBB has non-PHI nodes!");
842 // For each PHI in the destination block.
843 for (PHINode &PN : DestBB->phis()) {
844 int Idx = PN.getBasicBlockIndex(SplitBB);
845 assert(Idx >= 0 && "Invalid Block Index");
846 Value *V = PN.getIncomingValue(Idx);
848 // If the input is a PHI which already satisfies LCSSA, don't create
849 // a new one.
850 if (const PHINode *VP = dyn_cast<PHINode>(V))
851 if (VP->getParent() == SplitBB)
852 continue;
854 // Otherwise a new PHI is needed. Create one and populate it.
855 PHINode *NewPN = PHINode::Create(PN.getType(), Preds.size(), "split");
856 BasicBlock::iterator InsertPos =
857 SplitBB->isLandingPad() ? SplitBB->begin()
858 : SplitBB->getTerminator()->getIterator();
859 NewPN->insertBefore(InsertPos);
860 for (BasicBlock *BB : Preds)
861 NewPN->addIncoming(V, BB);
863 // Update the original PHI.
864 PN.setIncomingValue(Idx, NewPN);
868 unsigned
869 llvm::SplitAllCriticalEdges(Function &F,
870 const CriticalEdgeSplittingOptions &Options) {
871 unsigned NumBroken = 0;
872 for (BasicBlock &BB : F) {
873 Instruction *TI = BB.getTerminator();
874 if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
875 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
876 if (SplitCriticalEdge(TI, i, Options))
877 ++NumBroken;
879 return NumBroken;
882 static BasicBlock *SplitBlockImpl(BasicBlock *Old, BasicBlock::iterator SplitPt,
883 DomTreeUpdater *DTU, DominatorTree *DT,
884 LoopInfo *LI, MemorySSAUpdater *MSSAU,
885 const Twine &BBName, bool Before) {
886 if (Before) {
887 DomTreeUpdater LocalDTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
888 return splitBlockBefore(Old, SplitPt,
889 DTU ? DTU : (DT ? &LocalDTU : nullptr), LI, MSSAU,
890 BBName);
892 BasicBlock::iterator SplitIt = SplitPt;
893 while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) {
894 ++SplitIt;
895 assert(SplitIt != SplitPt->getParent()->end());
897 std::string Name = BBName.str();
898 BasicBlock *New = Old->splitBasicBlock(
899 SplitIt, Name.empty() ? Old->getName() + ".split" : Name);
901 // The new block lives in whichever loop the old one did. This preserves
902 // LCSSA as well, because we force the split point to be after any PHI nodes.
903 if (LI)
904 if (Loop *L = LI->getLoopFor(Old))
905 L->addBasicBlockToLoop(New, *LI);
907 if (DTU) {
908 SmallVector<DominatorTree::UpdateType, 8> Updates;
909 // Old dominates New. New node dominates all other nodes dominated by Old.
910 SmallPtrSet<BasicBlock *, 8> UniqueSuccessorsOfOld;
911 Updates.push_back({DominatorTree::Insert, Old, New});
912 Updates.reserve(Updates.size() + 2 * succ_size(New));
913 for (BasicBlock *SuccessorOfOld : successors(New))
914 if (UniqueSuccessorsOfOld.insert(SuccessorOfOld).second) {
915 Updates.push_back({DominatorTree::Insert, New, SuccessorOfOld});
916 Updates.push_back({DominatorTree::Delete, Old, SuccessorOfOld});
919 DTU->applyUpdates(Updates);
920 } else if (DT)
921 // Old dominates New. New node dominates all other nodes dominated by Old.
922 if (DomTreeNode *OldNode = DT->getNode(Old)) {
923 std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
925 DomTreeNode *NewNode = DT->addNewBlock(New, Old);
926 for (DomTreeNode *I : Children)
927 DT->changeImmediateDominator(I, NewNode);
930 // Move MemoryAccesses still tracked in Old, but part of New now.
931 // Update accesses in successor blocks accordingly.
932 if (MSSAU)
933 MSSAU->moveAllAfterSpliceBlocks(Old, New, &*(New->begin()));
935 return New;
938 BasicBlock *llvm::SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt,
939 DominatorTree *DT, LoopInfo *LI,
940 MemorySSAUpdater *MSSAU, const Twine &BBName,
941 bool Before) {
942 return SplitBlockImpl(Old, SplitPt, /*DTU=*/nullptr, DT, LI, MSSAU, BBName,
943 Before);
945 BasicBlock *llvm::SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt,
946 DomTreeUpdater *DTU, LoopInfo *LI,
947 MemorySSAUpdater *MSSAU, const Twine &BBName,
948 bool Before) {
949 return SplitBlockImpl(Old, SplitPt, DTU, /*DT=*/nullptr, LI, MSSAU, BBName,
950 Before);
953 BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, BasicBlock::iterator SplitPt,
954 DomTreeUpdater *DTU, LoopInfo *LI,
955 MemorySSAUpdater *MSSAU,
956 const Twine &BBName) {
958 BasicBlock::iterator SplitIt = SplitPt;
959 while (isa<PHINode>(SplitIt) || SplitIt->isEHPad())
960 ++SplitIt;
961 std::string Name = BBName.str();
962 BasicBlock *New = Old->splitBasicBlock(
963 SplitIt, Name.empty() ? Old->getName() + ".split" : Name,
964 /* Before=*/true);
966 // The new block lives in whichever loop the old one did. This preserves
967 // LCSSA as well, because we force the split point to be after any PHI nodes.
968 if (LI)
969 if (Loop *L = LI->getLoopFor(Old))
970 L->addBasicBlockToLoop(New, *LI);
972 if (DTU) {
973 SmallVector<DominatorTree::UpdateType, 8> DTUpdates;
974 // New dominates Old. The predecessor nodes of the Old node dominate
975 // New node.
976 SmallPtrSet<BasicBlock *, 8> UniquePredecessorsOfOld;
977 DTUpdates.push_back({DominatorTree::Insert, New, Old});
978 DTUpdates.reserve(DTUpdates.size() + 2 * pred_size(New));
979 for (BasicBlock *PredecessorOfOld : predecessors(New))
980 if (UniquePredecessorsOfOld.insert(PredecessorOfOld).second) {
981 DTUpdates.push_back({DominatorTree::Insert, PredecessorOfOld, New});
982 DTUpdates.push_back({DominatorTree::Delete, PredecessorOfOld, Old});
985 DTU->applyUpdates(DTUpdates);
987 // Move MemoryAccesses still tracked in Old, but part of New now.
988 // Update accesses in successor blocks accordingly.
989 if (MSSAU) {
990 MSSAU->applyUpdates(DTUpdates, DTU->getDomTree());
991 if (VerifyMemorySSA)
992 MSSAU->getMemorySSA()->verifyMemorySSA();
995 return New;
998 /// Update DominatorTree, LoopInfo, and LCCSA analysis information.
999 static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,
1000 ArrayRef<BasicBlock *> Preds,
1001 DomTreeUpdater *DTU, DominatorTree *DT,
1002 LoopInfo *LI, MemorySSAUpdater *MSSAU,
1003 bool PreserveLCSSA, bool &HasLoopExit) {
1004 // Update dominator tree if available.
1005 if (DTU) {
1006 // Recalculation of DomTree is needed when updating a forward DomTree and
1007 // the Entry BB is replaced.
1008 if (NewBB->isEntryBlock() && DTU->hasDomTree()) {
1009 // The entry block was removed and there is no external interface for
1010 // the dominator tree to be notified of this change. In this corner-case
1011 // we recalculate the entire tree.
1012 DTU->recalculate(*NewBB->getParent());
1013 } else {
1014 // Split block expects NewBB to have a non-empty set of predecessors.
1015 SmallVector<DominatorTree::UpdateType, 8> Updates;
1016 SmallPtrSet<BasicBlock *, 8> UniquePreds;
1017 Updates.push_back({DominatorTree::Insert, NewBB, OldBB});
1018 Updates.reserve(Updates.size() + 2 * Preds.size());
1019 for (auto *Pred : Preds)
1020 if (UniquePreds.insert(Pred).second) {
1021 Updates.push_back({DominatorTree::Insert, Pred, NewBB});
1022 Updates.push_back({DominatorTree::Delete, Pred, OldBB});
1024 DTU->applyUpdates(Updates);
1026 } else if (DT) {
1027 if (OldBB == DT->getRootNode()->getBlock()) {
1028 assert(NewBB->isEntryBlock());
1029 DT->setNewRoot(NewBB);
1030 } else {
1031 // Split block expects NewBB to have a non-empty set of predecessors.
1032 DT->splitBlock(NewBB);
1036 // Update MemoryPhis after split if MemorySSA is available
1037 if (MSSAU)
1038 MSSAU->wireOldPredecessorsToNewImmediatePredecessor(OldBB, NewBB, Preds);
1040 // The rest of the logic is only relevant for updating the loop structures.
1041 if (!LI)
1042 return;
1044 if (DTU && DTU->hasDomTree())
1045 DT = &DTU->getDomTree();
1046 assert(DT && "DT should be available to update LoopInfo!");
1047 Loop *L = LI->getLoopFor(OldBB);
1049 // If we need to preserve loop analyses, collect some information about how
1050 // this split will affect loops.
1051 bool IsLoopEntry = !!L;
1052 bool SplitMakesNewLoopHeader = false;
1053 for (BasicBlock *Pred : Preds) {
1054 // Preds that are not reachable from entry should not be used to identify if
1055 // OldBB is a loop entry or if SplitMakesNewLoopHeader. Unreachable blocks
1056 // are not within any loops, so we incorrectly mark SplitMakesNewLoopHeader
1057 // as true and make the NewBB the header of some loop. This breaks LI.
1058 if (!DT->isReachableFromEntry(Pred))
1059 continue;
1060 // If we need to preserve LCSSA, determine if any of the preds is a loop
1061 // exit.
1062 if (PreserveLCSSA)
1063 if (Loop *PL = LI->getLoopFor(Pred))
1064 if (!PL->contains(OldBB))
1065 HasLoopExit = true;
1067 // If we need to preserve LoopInfo, note whether any of the preds crosses
1068 // an interesting loop boundary.
1069 if (!L)
1070 continue;
1071 if (L->contains(Pred))
1072 IsLoopEntry = false;
1073 else
1074 SplitMakesNewLoopHeader = true;
1077 // Unless we have a loop for OldBB, nothing else to do here.
1078 if (!L)
1079 return;
1081 if (IsLoopEntry) {
1082 // Add the new block to the nearest enclosing loop (and not an adjacent
1083 // loop). To find this, examine each of the predecessors and determine which
1084 // loops enclose them, and select the most-nested loop which contains the
1085 // loop containing the block being split.
1086 Loop *InnermostPredLoop = nullptr;
1087 for (BasicBlock *Pred : Preds) {
1088 if (Loop *PredLoop = LI->getLoopFor(Pred)) {
1089 // Seek a loop which actually contains the block being split (to avoid
1090 // adjacent loops).
1091 while (PredLoop && !PredLoop->contains(OldBB))
1092 PredLoop = PredLoop->getParentLoop();
1094 // Select the most-nested of these loops which contains the block.
1095 if (PredLoop && PredLoop->contains(OldBB) &&
1096 (!InnermostPredLoop ||
1097 InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))
1098 InnermostPredLoop = PredLoop;
1102 if (InnermostPredLoop)
1103 InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI);
1104 } else {
1105 L->addBasicBlockToLoop(NewBB, *LI);
1106 if (SplitMakesNewLoopHeader)
1107 L->moveToHeader(NewBB);
1111 /// Update the PHI nodes in OrigBB to include the values coming from NewBB.
1112 /// This also updates AliasAnalysis, if available.
1113 static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,
1114 ArrayRef<BasicBlock *> Preds, BranchInst *BI,
1115 bool HasLoopExit) {
1116 // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB.
1117 SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end());
1118 for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) {
1119 PHINode *PN = cast<PHINode>(I++);
1121 // Check to see if all of the values coming in are the same. If so, we
1122 // don't need to create a new PHI node, unless it's needed for LCSSA.
1123 Value *InVal = nullptr;
1124 if (!HasLoopExit) {
1125 InVal = PN->getIncomingValueForBlock(Preds[0]);
1126 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1127 if (!PredSet.count(PN->getIncomingBlock(i)))
1128 continue;
1129 if (!InVal)
1130 InVal = PN->getIncomingValue(i);
1131 else if (InVal != PN->getIncomingValue(i)) {
1132 InVal = nullptr;
1133 break;
1138 if (InVal) {
1139 // If all incoming values for the new PHI would be the same, just don't
1140 // make a new PHI. Instead, just remove the incoming values from the old
1141 // PHI.
1142 PN->removeIncomingValueIf(
1143 [&](unsigned Idx) {
1144 return PredSet.contains(PN->getIncomingBlock(Idx));
1146 /* DeletePHIIfEmpty */ false);
1148 // Add an incoming value to the PHI node in the loop for the preheader
1149 // edge.
1150 PN->addIncoming(InVal, NewBB);
1151 continue;
1154 // If the values coming into the block are not the same, we need a new
1155 // PHI.
1156 // Create the new PHI node, insert it into NewBB at the end of the block
1157 PHINode *NewPHI =
1158 PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI);
1160 // NOTE! This loop walks backwards for a reason! First off, this minimizes
1161 // the cost of removal if we end up removing a large number of values, and
1162 // second off, this ensures that the indices for the incoming values aren't
1163 // invalidated when we remove one.
1164 for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) {
1165 BasicBlock *IncomingBB = PN->getIncomingBlock(i);
1166 if (PredSet.count(IncomingBB)) {
1167 Value *V = PN->removeIncomingValue(i, false);
1168 NewPHI->addIncoming(V, IncomingBB);
1172 PN->addIncoming(NewPHI, NewBB);
1176 static void SplitLandingPadPredecessorsImpl(
1177 BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1,
1178 const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
1179 DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI,
1180 MemorySSAUpdater *MSSAU, bool PreserveLCSSA);
1182 static BasicBlock *
1183 SplitBlockPredecessorsImpl(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
1184 const char *Suffix, DomTreeUpdater *DTU,
1185 DominatorTree *DT, LoopInfo *LI,
1186 MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {
1187 // Do not attempt to split that which cannot be split.
1188 if (!BB->canSplitPredecessors())
1189 return nullptr;
1191 // For the landingpads we need to act a bit differently.
1192 // Delegate this work to the SplitLandingPadPredecessors.
1193 if (BB->isLandingPad()) {
1194 SmallVector<BasicBlock*, 2> NewBBs;
1195 std::string NewName = std::string(Suffix) + ".split-lp";
1197 SplitLandingPadPredecessorsImpl(BB, Preds, Suffix, NewName.c_str(), NewBBs,
1198 DTU, DT, LI, MSSAU, PreserveLCSSA);
1199 return NewBBs[0];
1202 // Create new basic block, insert right before the original block.
1203 BasicBlock *NewBB = BasicBlock::Create(
1204 BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB);
1206 // The new block unconditionally branches to the old block.
1207 BranchInst *BI = BranchInst::Create(BB, NewBB);
1209 Loop *L = nullptr;
1210 BasicBlock *OldLatch = nullptr;
1211 // Splitting the predecessors of a loop header creates a preheader block.
1212 if (LI && LI->isLoopHeader(BB)) {
1213 L = LI->getLoopFor(BB);
1214 // Using the loop start line number prevents debuggers stepping into the
1215 // loop body for this instruction.
1216 BI->setDebugLoc(L->getStartLoc());
1218 // If BB is the header of the Loop, it is possible that the loop is
1219 // modified, such that the current latch does not remain the latch of the
1220 // loop. If that is the case, the loop metadata from the current latch needs
1221 // to be applied to the new latch.
1222 OldLatch = L->getLoopLatch();
1223 } else
1224 BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc());
1226 // Move the edges from Preds to point to NewBB instead of BB.
1227 for (BasicBlock *Pred : Preds) {
1228 // This is slightly more strict than necessary; the minimum requirement
1229 // is that there be no more than one indirectbr branching to BB. And
1230 // all BlockAddress uses would need to be updated.
1231 assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
1232 "Cannot split an edge from an IndirectBrInst");
1233 Pred->getTerminator()->replaceSuccessorWith(BB, NewBB);
1236 // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
1237 // node becomes an incoming value for BB's phi node. However, if the Preds
1238 // list is empty, we need to insert dummy entries into the PHI nodes in BB to
1239 // account for the newly created predecessor.
1240 if (Preds.empty()) {
1241 // Insert dummy values as the incoming value.
1242 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
1243 cast<PHINode>(I)->addIncoming(PoisonValue::get(I->getType()), NewBB);
1246 // Update DominatorTree, LoopInfo, and LCCSA analysis information.
1247 bool HasLoopExit = false;
1248 UpdateAnalysisInformation(BB, NewBB, Preds, DTU, DT, LI, MSSAU, PreserveLCSSA,
1249 HasLoopExit);
1251 if (!Preds.empty()) {
1252 // Update the PHI nodes in BB with the values coming from NewBB.
1253 UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit);
1256 if (OldLatch) {
1257 BasicBlock *NewLatch = L->getLoopLatch();
1258 if (NewLatch != OldLatch) {
1259 MDNode *MD = OldLatch->getTerminator()->getMetadata("llvm.loop");
1260 NewLatch->getTerminator()->setMetadata("llvm.loop", MD);
1261 // It's still possible that OldLatch is the latch of another inner loop,
1262 // in which case we do not remove the metadata.
1263 Loop *IL = LI->getLoopFor(OldLatch);
1264 if (IL && IL->getLoopLatch() != OldLatch)
1265 OldLatch->getTerminator()->setMetadata("llvm.loop", nullptr);
1269 return NewBB;
1272 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
1273 ArrayRef<BasicBlock *> Preds,
1274 const char *Suffix, DominatorTree *DT,
1275 LoopInfo *LI, MemorySSAUpdater *MSSAU,
1276 bool PreserveLCSSA) {
1277 return SplitBlockPredecessorsImpl(BB, Preds, Suffix, /*DTU=*/nullptr, DT, LI,
1278 MSSAU, PreserveLCSSA);
1280 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
1281 ArrayRef<BasicBlock *> Preds,
1282 const char *Suffix,
1283 DomTreeUpdater *DTU, LoopInfo *LI,
1284 MemorySSAUpdater *MSSAU,
1285 bool PreserveLCSSA) {
1286 return SplitBlockPredecessorsImpl(BB, Preds, Suffix, DTU,
1287 /*DT=*/nullptr, LI, MSSAU, PreserveLCSSA);
1290 static void SplitLandingPadPredecessorsImpl(
1291 BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1,
1292 const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
1293 DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI,
1294 MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {
1295 assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!");
1297 // Create a new basic block for OrigBB's predecessors listed in Preds. Insert
1298 // it right before the original block.
1299 BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(),
1300 OrigBB->getName() + Suffix1,
1301 OrigBB->getParent(), OrigBB);
1302 NewBBs.push_back(NewBB1);
1304 // The new block unconditionally branches to the old block.
1305 BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1);
1306 BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
1308 // Move the edges from Preds to point to NewBB1 instead of OrigBB.
1309 for (BasicBlock *Pred : Preds) {
1310 // This is slightly more strict than necessary; the minimum requirement
1311 // is that there be no more than one indirectbr branching to BB. And
1312 // all BlockAddress uses would need to be updated.
1313 assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
1314 "Cannot split an edge from an IndirectBrInst");
1315 Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
1318 bool HasLoopExit = false;
1319 UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DTU, DT, LI, MSSAU,
1320 PreserveLCSSA, HasLoopExit);
1322 // Update the PHI nodes in OrigBB with the values coming from NewBB1.
1323 UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit);
1325 // Move the remaining edges from OrigBB to point to NewBB2.
1326 SmallVector<BasicBlock*, 8> NewBB2Preds;
1327 for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
1328 i != e; ) {
1329 BasicBlock *Pred = *i++;
1330 if (Pred == NewBB1) continue;
1331 assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
1332 "Cannot split an edge from an IndirectBrInst");
1333 NewBB2Preds.push_back(Pred);
1334 e = pred_end(OrigBB);
1337 BasicBlock *NewBB2 = nullptr;
1338 if (!NewBB2Preds.empty()) {
1339 // Create another basic block for the rest of OrigBB's predecessors.
1340 NewBB2 = BasicBlock::Create(OrigBB->getContext(),
1341 OrigBB->getName() + Suffix2,
1342 OrigBB->getParent(), OrigBB);
1343 NewBBs.push_back(NewBB2);
1345 // The new block unconditionally branches to the old block.
1346 BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2);
1347 BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
1349 // Move the remaining edges from OrigBB to point to NewBB2.
1350 for (BasicBlock *NewBB2Pred : NewBB2Preds)
1351 NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
1353 // Update DominatorTree, LoopInfo, and LCCSA analysis information.
1354 HasLoopExit = false;
1355 UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DTU, DT, LI, MSSAU,
1356 PreserveLCSSA, HasLoopExit);
1358 // Update the PHI nodes in OrigBB with the values coming from NewBB2.
1359 UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit);
1362 LandingPadInst *LPad = OrigBB->getLandingPadInst();
1363 Instruction *Clone1 = LPad->clone();
1364 Clone1->setName(Twine("lpad") + Suffix1);
1365 Clone1->insertInto(NewBB1, NewBB1->getFirstInsertionPt());
1367 if (NewBB2) {
1368 Instruction *Clone2 = LPad->clone();
1369 Clone2->setName(Twine("lpad") + Suffix2);
1370 Clone2->insertInto(NewBB2, NewBB2->getFirstInsertionPt());
1372 // Create a PHI node for the two cloned landingpad instructions only
1373 // if the original landingpad instruction has some uses.
1374 if (!LPad->use_empty()) {
1375 assert(!LPad->getType()->isTokenTy() &&
1376 "Split cannot be applied if LPad is token type. Otherwise an "
1377 "invalid PHINode of token type would be created.");
1378 PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad);
1379 PN->addIncoming(Clone1, NewBB1);
1380 PN->addIncoming(Clone2, NewBB2);
1381 LPad->replaceAllUsesWith(PN);
1383 LPad->eraseFromParent();
1384 } else {
1385 // There is no second clone. Just replace the landing pad with the first
1386 // clone.
1387 LPad->replaceAllUsesWith(Clone1);
1388 LPad->eraseFromParent();
1392 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,
1393 ArrayRef<BasicBlock *> Preds,
1394 const char *Suffix1, const char *Suffix2,
1395 SmallVectorImpl<BasicBlock *> &NewBBs,
1396 DominatorTree *DT, LoopInfo *LI,
1397 MemorySSAUpdater *MSSAU,
1398 bool PreserveLCSSA) {
1399 return SplitLandingPadPredecessorsImpl(
1400 OrigBB, Preds, Suffix1, Suffix2, NewBBs,
1401 /*DTU=*/nullptr, DT, LI, MSSAU, PreserveLCSSA);
1403 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,
1404 ArrayRef<BasicBlock *> Preds,
1405 const char *Suffix1, const char *Suffix2,
1406 SmallVectorImpl<BasicBlock *> &NewBBs,
1407 DomTreeUpdater *DTU, LoopInfo *LI,
1408 MemorySSAUpdater *MSSAU,
1409 bool PreserveLCSSA) {
1410 return SplitLandingPadPredecessorsImpl(OrigBB, Preds, Suffix1, Suffix2,
1411 NewBBs, DTU, /*DT=*/nullptr, LI, MSSAU,
1412 PreserveLCSSA);
1415 ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
1416 BasicBlock *Pred,
1417 DomTreeUpdater *DTU) {
1418 Instruction *UncondBranch = Pred->getTerminator();
1419 // Clone the return and add it to the end of the predecessor.
1420 Instruction *NewRet = RI->clone();
1421 NewRet->insertInto(Pred, Pred->end());
1423 // If the return instruction returns a value, and if the value was a
1424 // PHI node in "BB", propagate the right value into the return.
1425 for (Use &Op : NewRet->operands()) {
1426 Value *V = Op;
1427 Instruction *NewBC = nullptr;
1428 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {
1429 // Return value might be bitcasted. Clone and insert it before the
1430 // return instruction.
1431 V = BCI->getOperand(0);
1432 NewBC = BCI->clone();
1433 NewBC->insertInto(Pred, NewRet->getIterator());
1434 Op = NewBC;
1437 Instruction *NewEV = nullptr;
1438 if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
1439 V = EVI->getOperand(0);
1440 NewEV = EVI->clone();
1441 if (NewBC) {
1442 NewBC->setOperand(0, NewEV);
1443 NewEV->insertInto(Pred, NewBC->getIterator());
1444 } else {
1445 NewEV->insertInto(Pred, NewRet->getIterator());
1446 Op = NewEV;
1450 if (PHINode *PN = dyn_cast<PHINode>(V)) {
1451 if (PN->getParent() == BB) {
1452 if (NewEV) {
1453 NewEV->setOperand(0, PN->getIncomingValueForBlock(Pred));
1454 } else if (NewBC)
1455 NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));
1456 else
1457 Op = PN->getIncomingValueForBlock(Pred);
1462 // Update any PHI nodes in the returning block to realize that we no
1463 // longer branch to them.
1464 BB->removePredecessor(Pred);
1465 UncondBranch->eraseFromParent();
1467 if (DTU)
1468 DTU->applyUpdates({{DominatorTree::Delete, Pred, BB}});
1470 return cast<ReturnInst>(NewRet);
1473 Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,
1474 BasicBlock::iterator SplitBefore,
1475 bool Unreachable,
1476 MDNode *BranchWeights,
1477 DomTreeUpdater *DTU, LoopInfo *LI,
1478 BasicBlock *ThenBlock) {
1479 SplitBlockAndInsertIfThenElse(
1480 Cond, SplitBefore, &ThenBlock, /* ElseBlock */ nullptr,
1481 /* UnreachableThen */ Unreachable,
1482 /* UnreachableElse */ false, BranchWeights, DTU, LI);
1483 return ThenBlock->getTerminator();
1486 Instruction *llvm::SplitBlockAndInsertIfElse(Value *Cond,
1487 BasicBlock::iterator SplitBefore,
1488 bool Unreachable,
1489 MDNode *BranchWeights,
1490 DomTreeUpdater *DTU, LoopInfo *LI,
1491 BasicBlock *ElseBlock) {
1492 SplitBlockAndInsertIfThenElse(
1493 Cond, SplitBefore, /* ThenBlock */ nullptr, &ElseBlock,
1494 /* UnreachableThen */ false,
1495 /* UnreachableElse */ Unreachable, BranchWeights, DTU, LI);
1496 return ElseBlock->getTerminator();
1499 void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, BasicBlock::iterator SplitBefore,
1500 Instruction **ThenTerm,
1501 Instruction **ElseTerm,
1502 MDNode *BranchWeights,
1503 DomTreeUpdater *DTU, LoopInfo *LI) {
1504 BasicBlock *ThenBlock = nullptr;
1505 BasicBlock *ElseBlock = nullptr;
1506 SplitBlockAndInsertIfThenElse(
1507 Cond, SplitBefore, &ThenBlock, &ElseBlock, /* UnreachableThen */ false,
1508 /* UnreachableElse */ false, BranchWeights, DTU, LI);
1510 *ThenTerm = ThenBlock->getTerminator();
1511 *ElseTerm = ElseBlock->getTerminator();
1514 void llvm::SplitBlockAndInsertIfThenElse(
1515 Value *Cond, BasicBlock::iterator SplitBefore, BasicBlock **ThenBlock,
1516 BasicBlock **ElseBlock, bool UnreachableThen, bool UnreachableElse,
1517 MDNode *BranchWeights, DomTreeUpdater *DTU, LoopInfo *LI) {
1518 assert((ThenBlock || ElseBlock) &&
1519 "At least one branch block must be created");
1520 assert((!UnreachableThen || !UnreachableElse) &&
1521 "Split block tail must be reachable");
1523 SmallVector<DominatorTree::UpdateType, 8> Updates;
1524 SmallPtrSet<BasicBlock *, 8> UniqueOrigSuccessors;
1525 BasicBlock *Head = SplitBefore->getParent();
1526 if (DTU) {
1527 UniqueOrigSuccessors.insert(succ_begin(Head), succ_end(Head));
1528 Updates.reserve(4 + 2 * UniqueOrigSuccessors.size());
1531 LLVMContext &C = Head->getContext();
1532 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
1533 BasicBlock *TrueBlock = Tail;
1534 BasicBlock *FalseBlock = Tail;
1535 bool ThenToTailEdge = false;
1536 bool ElseToTailEdge = false;
1538 // Encapsulate the logic around creation/insertion/etc of a new block.
1539 auto handleBlock = [&](BasicBlock **PBB, bool Unreachable, BasicBlock *&BB,
1540 bool &ToTailEdge) {
1541 if (PBB == nullptr)
1542 return; // Do not create/insert a block.
1544 if (*PBB)
1545 BB = *PBB; // Caller supplied block, use it.
1546 else {
1547 // Create a new block.
1548 BB = BasicBlock::Create(C, "", Head->getParent(), Tail);
1549 if (Unreachable)
1550 (void)new UnreachableInst(C, BB);
1551 else {
1552 (void)BranchInst::Create(Tail, BB);
1553 ToTailEdge = true;
1555 BB->getTerminator()->setDebugLoc(SplitBefore->getDebugLoc());
1556 // Pass the new block back to the caller.
1557 *PBB = BB;
1561 handleBlock(ThenBlock, UnreachableThen, TrueBlock, ThenToTailEdge);
1562 handleBlock(ElseBlock, UnreachableElse, FalseBlock, ElseToTailEdge);
1564 Instruction *HeadOldTerm = Head->getTerminator();
1565 BranchInst *HeadNewTerm =
1566 BranchInst::Create(/*ifTrue*/ TrueBlock, /*ifFalse*/ FalseBlock, Cond);
1567 HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
1568 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
1570 if (DTU) {
1571 Updates.emplace_back(DominatorTree::Insert, Head, TrueBlock);
1572 Updates.emplace_back(DominatorTree::Insert, Head, FalseBlock);
1573 if (ThenToTailEdge)
1574 Updates.emplace_back(DominatorTree::Insert, TrueBlock, Tail);
1575 if (ElseToTailEdge)
1576 Updates.emplace_back(DominatorTree::Insert, FalseBlock, Tail);
1577 for (BasicBlock *UniqueOrigSuccessor : UniqueOrigSuccessors)
1578 Updates.emplace_back(DominatorTree::Insert, Tail, UniqueOrigSuccessor);
1579 for (BasicBlock *UniqueOrigSuccessor : UniqueOrigSuccessors)
1580 Updates.emplace_back(DominatorTree::Delete, Head, UniqueOrigSuccessor);
1581 DTU->applyUpdates(Updates);
1584 if (LI) {
1585 if (Loop *L = LI->getLoopFor(Head); L) {
1586 if (ThenToTailEdge)
1587 L->addBasicBlockToLoop(TrueBlock, *LI);
1588 if (ElseToTailEdge)
1589 L->addBasicBlockToLoop(FalseBlock, *LI);
1590 L->addBasicBlockToLoop(Tail, *LI);
1595 std::pair<Instruction*, Value*>
1596 llvm::SplitBlockAndInsertSimpleForLoop(Value *End, Instruction *SplitBefore) {
1597 BasicBlock *LoopPred = SplitBefore->getParent();
1598 BasicBlock *LoopBody = SplitBlock(SplitBefore->getParent(), SplitBefore);
1599 BasicBlock *LoopExit = SplitBlock(SplitBefore->getParent(), SplitBefore);
1601 auto *Ty = End->getType();
1602 auto &DL = SplitBefore->getModule()->getDataLayout();
1603 const unsigned Bitwidth = DL.getTypeSizeInBits(Ty);
1605 IRBuilder<> Builder(LoopBody->getTerminator());
1606 auto *IV = Builder.CreatePHI(Ty, 2, "iv");
1607 auto *IVNext =
1608 Builder.CreateAdd(IV, ConstantInt::get(Ty, 1), IV->getName() + ".next",
1609 /*HasNUW=*/true, /*HasNSW=*/Bitwidth != 2);
1610 auto *IVCheck = Builder.CreateICmpEQ(IVNext, End,
1611 IV->getName() + ".check");
1612 Builder.CreateCondBr(IVCheck, LoopExit, LoopBody);
1613 LoopBody->getTerminator()->eraseFromParent();
1615 // Populate the IV PHI.
1616 IV->addIncoming(ConstantInt::get(Ty, 0), LoopPred);
1617 IV->addIncoming(IVNext, LoopBody);
1619 return std::make_pair(LoopBody->getFirstNonPHI(), IV);
1622 void llvm::SplitBlockAndInsertForEachLane(ElementCount EC,
1623 Type *IndexTy, Instruction *InsertBefore,
1624 std::function<void(IRBuilderBase&, Value*)> Func) {
1626 IRBuilder<> IRB(InsertBefore);
1628 if (EC.isScalable()) {
1629 Value *NumElements = IRB.CreateElementCount(IndexTy, EC);
1631 auto [BodyIP, Index] =
1632 SplitBlockAndInsertSimpleForLoop(NumElements, InsertBefore);
1634 IRB.SetInsertPoint(BodyIP);
1635 Func(IRB, Index);
1636 return;
1639 unsigned Num = EC.getFixedValue();
1640 for (unsigned Idx = 0; Idx < Num; ++Idx) {
1641 IRB.SetInsertPoint(InsertBefore);
1642 Func(IRB, ConstantInt::get(IndexTy, Idx));
1646 void llvm::SplitBlockAndInsertForEachLane(
1647 Value *EVL, Instruction *InsertBefore,
1648 std::function<void(IRBuilderBase &, Value *)> Func) {
1650 IRBuilder<> IRB(InsertBefore);
1651 Type *Ty = EVL->getType();
1653 if (!isa<ConstantInt>(EVL)) {
1654 auto [BodyIP, Index] = SplitBlockAndInsertSimpleForLoop(EVL, InsertBefore);
1655 IRB.SetInsertPoint(BodyIP);
1656 Func(IRB, Index);
1657 return;
1660 unsigned Num = cast<ConstantInt>(EVL)->getZExtValue();
1661 for (unsigned Idx = 0; Idx < Num; ++Idx) {
1662 IRB.SetInsertPoint(InsertBefore);
1663 Func(IRB, ConstantInt::get(Ty, Idx));
1667 BranchInst *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
1668 BasicBlock *&IfFalse) {
1669 PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());
1670 BasicBlock *Pred1 = nullptr;
1671 BasicBlock *Pred2 = nullptr;
1673 if (SomePHI) {
1674 if (SomePHI->getNumIncomingValues() != 2)
1675 return nullptr;
1676 Pred1 = SomePHI->getIncomingBlock(0);
1677 Pred2 = SomePHI->getIncomingBlock(1);
1678 } else {
1679 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1680 if (PI == PE) // No predecessor
1681 return nullptr;
1682 Pred1 = *PI++;
1683 if (PI == PE) // Only one predecessor
1684 return nullptr;
1685 Pred2 = *PI++;
1686 if (PI != PE) // More than two predecessors
1687 return nullptr;
1690 // We can only handle branches. Other control flow will be lowered to
1691 // branches if possible anyway.
1692 BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());
1693 BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());
1694 if (!Pred1Br || !Pred2Br)
1695 return nullptr;
1697 // Eliminate code duplication by ensuring that Pred1Br is conditional if
1698 // either are.
1699 if (Pred2Br->isConditional()) {
1700 // If both branches are conditional, we don't have an "if statement". In
1701 // reality, we could transform this case, but since the condition will be
1702 // required anyway, we stand no chance of eliminating it, so the xform is
1703 // probably not profitable.
1704 if (Pred1Br->isConditional())
1705 return nullptr;
1707 std::swap(Pred1, Pred2);
1708 std::swap(Pred1Br, Pred2Br);
1711 if (Pred1Br->isConditional()) {
1712 // The only thing we have to watch out for here is to make sure that Pred2
1713 // doesn't have incoming edges from other blocks. If it does, the condition
1714 // doesn't dominate BB.
1715 if (!Pred2->getSinglePredecessor())
1716 return nullptr;
1718 // If we found a conditional branch predecessor, make sure that it branches
1719 // to BB and Pred2Br. If it doesn't, this isn't an "if statement".
1720 if (Pred1Br->getSuccessor(0) == BB &&
1721 Pred1Br->getSuccessor(1) == Pred2) {
1722 IfTrue = Pred1;
1723 IfFalse = Pred2;
1724 } else if (Pred1Br->getSuccessor(0) == Pred2 &&
1725 Pred1Br->getSuccessor(1) == BB) {
1726 IfTrue = Pred2;
1727 IfFalse = Pred1;
1728 } else {
1729 // We know that one arm of the conditional goes to BB, so the other must
1730 // go somewhere unrelated, and this must not be an "if statement".
1731 return nullptr;
1734 return Pred1Br;
1737 // Ok, if we got here, both predecessors end with an unconditional branch to
1738 // BB. Don't panic! If both blocks only have a single (identical)
1739 // predecessor, and THAT is a conditional branch, then we're all ok!
1740 BasicBlock *CommonPred = Pred1->getSinglePredecessor();
1741 if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor())
1742 return nullptr;
1744 // Otherwise, if this is a conditional branch, then we can use it!
1745 BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());
1746 if (!BI) return nullptr;
1748 assert(BI->isConditional() && "Two successors but not conditional?");
1749 if (BI->getSuccessor(0) == Pred1) {
1750 IfTrue = Pred1;
1751 IfFalse = Pred2;
1752 } else {
1753 IfTrue = Pred2;
1754 IfFalse = Pred1;
1756 return BI;
1759 // After creating a control flow hub, the operands of PHINodes in an outgoing
1760 // block Out no longer match the predecessors of that block. Predecessors of Out
1761 // that are incoming blocks to the hub are now replaced by just one edge from
1762 // the hub. To match this new control flow, the corresponding values from each
1763 // PHINode must now be moved a new PHINode in the first guard block of the hub.
1765 // This operation cannot be performed with SSAUpdater, because it involves one
1766 // new use: If the block Out is in the list of Incoming blocks, then the newly
1767 // created PHI in the Hub will use itself along that edge from Out to Hub.
1768 static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock,
1769 const SetVector<BasicBlock *> &Incoming,
1770 BasicBlock *FirstGuardBlock) {
1771 auto I = Out->begin();
1772 while (I != Out->end() && isa<PHINode>(I)) {
1773 auto Phi = cast<PHINode>(I);
1774 auto NewPhi =
1775 PHINode::Create(Phi->getType(), Incoming.size(),
1776 Phi->getName() + ".moved", &FirstGuardBlock->front());
1777 for (auto *In : Incoming) {
1778 Value *V = UndefValue::get(Phi->getType());
1779 if (In == Out) {
1780 V = NewPhi;
1781 } else if (Phi->getBasicBlockIndex(In) != -1) {
1782 V = Phi->removeIncomingValue(In, false);
1784 NewPhi->addIncoming(V, In);
1786 assert(NewPhi->getNumIncomingValues() == Incoming.size());
1787 if (Phi->getNumOperands() == 0) {
1788 Phi->replaceAllUsesWith(NewPhi);
1789 I = Phi->eraseFromParent();
1790 continue;
1792 Phi->addIncoming(NewPhi, GuardBlock);
1793 ++I;
1797 using BBPredicates = DenseMap<BasicBlock *, Instruction *>;
1798 using BBSetVector = SetVector<BasicBlock *>;
1800 // Redirects the terminator of the incoming block to the first guard
1801 // block in the hub. The condition of the original terminator (if it
1802 // was conditional) and its original successors are returned as a
1803 // tuple <condition, succ0, succ1>. The function additionally filters
1804 // out successors that are not in the set of outgoing blocks.
1806 // - condition is non-null iff the branch is conditional.
1807 // - Succ1 is non-null iff the sole/taken target is an outgoing block.
1808 // - Succ2 is non-null iff condition is non-null and the fallthrough
1809 // target is an outgoing block.
1810 static std::tuple<Value *, BasicBlock *, BasicBlock *>
1811 redirectToHub(BasicBlock *BB, BasicBlock *FirstGuardBlock,
1812 const BBSetVector &Outgoing) {
1813 assert(isa<BranchInst>(BB->getTerminator()) &&
1814 "Only support branch terminator.");
1815 auto Branch = cast<BranchInst>(BB->getTerminator());
1816 auto Condition = Branch->isConditional() ? Branch->getCondition() : nullptr;
1818 BasicBlock *Succ0 = Branch->getSuccessor(0);
1819 BasicBlock *Succ1 = nullptr;
1820 Succ0 = Outgoing.count(Succ0) ? Succ0 : nullptr;
1822 if (Branch->isUnconditional()) {
1823 Branch->setSuccessor(0, FirstGuardBlock);
1824 assert(Succ0);
1825 } else {
1826 Succ1 = Branch->getSuccessor(1);
1827 Succ1 = Outgoing.count(Succ1) ? Succ1 : nullptr;
1828 assert(Succ0 || Succ1);
1829 if (Succ0 && !Succ1) {
1830 Branch->setSuccessor(0, FirstGuardBlock);
1831 } else if (Succ1 && !Succ0) {
1832 Branch->setSuccessor(1, FirstGuardBlock);
1833 } else {
1834 Branch->eraseFromParent();
1835 BranchInst::Create(FirstGuardBlock, BB);
1839 assert(Succ0 || Succ1);
1840 return std::make_tuple(Condition, Succ0, Succ1);
1842 // Setup the branch instructions for guard blocks.
1844 // Each guard block terminates in a conditional branch that transfers
1845 // control to the corresponding outgoing block or the next guard
1846 // block. The last guard block has two outgoing blocks as successors
1847 // since the condition for the final outgoing block is trivially
1848 // true. So we create one less block (including the first guard block)
1849 // than the number of outgoing blocks.
1850 static void setupBranchForGuard(SmallVectorImpl<BasicBlock *> &GuardBlocks,
1851 const BBSetVector &Outgoing,
1852 BBPredicates &GuardPredicates) {
1853 // To help keep the loop simple, temporarily append the last
1854 // outgoing block to the list of guard blocks.
1855 GuardBlocks.push_back(Outgoing.back());
1857 for (int i = 0, e = GuardBlocks.size() - 1; i != e; ++i) {
1858 auto Out = Outgoing[i];
1859 assert(GuardPredicates.count(Out));
1860 BranchInst::Create(Out, GuardBlocks[i + 1], GuardPredicates[Out],
1861 GuardBlocks[i]);
1864 // Remove the last block from the guard list.
1865 GuardBlocks.pop_back();
1868 /// We are using one integer to represent the block we are branching to. Then at
1869 /// each guard block, the predicate was calcuated using a simple `icmp eq`.
1870 static void calcPredicateUsingInteger(
1871 const BBSetVector &Incoming, const BBSetVector &Outgoing,
1872 SmallVectorImpl<BasicBlock *> &GuardBlocks, BBPredicates &GuardPredicates) {
1873 auto &Context = Incoming.front()->getContext();
1874 auto FirstGuardBlock = GuardBlocks.front();
1876 auto Phi = PHINode::Create(Type::getInt32Ty(Context), Incoming.size(),
1877 "merged.bb.idx", FirstGuardBlock);
1879 for (auto In : Incoming) {
1880 Value *Condition;
1881 BasicBlock *Succ0;
1882 BasicBlock *Succ1;
1883 std::tie(Condition, Succ0, Succ1) =
1884 redirectToHub(In, FirstGuardBlock, Outgoing);
1885 Value *IncomingId = nullptr;
1886 if (Succ0 && Succ1) {
1887 // target_bb_index = Condition ? index_of_succ0 : index_of_succ1.
1888 auto Succ0Iter = find(Outgoing, Succ0);
1889 auto Succ1Iter = find(Outgoing, Succ1);
1890 Value *Id0 = ConstantInt::get(Type::getInt32Ty(Context),
1891 std::distance(Outgoing.begin(), Succ0Iter));
1892 Value *Id1 = ConstantInt::get(Type::getInt32Ty(Context),
1893 std::distance(Outgoing.begin(), Succ1Iter));
1894 IncomingId = SelectInst::Create(Condition, Id0, Id1, "target.bb.idx",
1895 In->getTerminator());
1896 } else {
1897 // Get the index of the non-null successor.
1898 auto SuccIter = Succ0 ? find(Outgoing, Succ0) : find(Outgoing, Succ1);
1899 IncomingId = ConstantInt::get(Type::getInt32Ty(Context),
1900 std::distance(Outgoing.begin(), SuccIter));
1902 Phi->addIncoming(IncomingId, In);
1905 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
1906 auto Out = Outgoing[i];
1907 auto Cmp = ICmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, Phi,
1908 ConstantInt::get(Type::getInt32Ty(Context), i),
1909 Out->getName() + ".predicate", GuardBlocks[i]);
1910 GuardPredicates[Out] = Cmp;
1914 /// We record the predicate of each outgoing block using a phi of boolean.
1915 static void calcPredicateUsingBooleans(
1916 const BBSetVector &Incoming, const BBSetVector &Outgoing,
1917 SmallVectorImpl<BasicBlock *> &GuardBlocks, BBPredicates &GuardPredicates,
1918 SmallVectorImpl<WeakVH> &DeletionCandidates) {
1919 auto &Context = Incoming.front()->getContext();
1920 auto BoolTrue = ConstantInt::getTrue(Context);
1921 auto BoolFalse = ConstantInt::getFalse(Context);
1922 auto FirstGuardBlock = GuardBlocks.front();
1924 // The predicate for the last outgoing is trivially true, and so we
1925 // process only the first N-1 successors.
1926 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
1927 auto Out = Outgoing[i];
1928 LLVM_DEBUG(dbgs() << "Creating guard for " << Out->getName() << "\n");
1930 auto Phi =
1931 PHINode::Create(Type::getInt1Ty(Context), Incoming.size(),
1932 StringRef("Guard.") + Out->getName(), FirstGuardBlock);
1933 GuardPredicates[Out] = Phi;
1936 for (auto *In : Incoming) {
1937 Value *Condition;
1938 BasicBlock *Succ0;
1939 BasicBlock *Succ1;
1940 std::tie(Condition, Succ0, Succ1) =
1941 redirectToHub(In, FirstGuardBlock, Outgoing);
1943 // Optimization: Consider an incoming block A with both successors
1944 // Succ0 and Succ1 in the set of outgoing blocks. The predicates
1945 // for Succ0 and Succ1 complement each other. If Succ0 is visited
1946 // first in the loop below, control will branch to Succ0 using the
1947 // corresponding predicate. But if that branch is not taken, then
1948 // control must reach Succ1, which means that the incoming value of
1949 // the predicate from `In` is true for Succ1.
1950 bool OneSuccessorDone = false;
1951 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
1952 auto Out = Outgoing[i];
1953 PHINode *Phi = cast<PHINode>(GuardPredicates[Out]);
1954 if (Out != Succ0 && Out != Succ1) {
1955 Phi->addIncoming(BoolFalse, In);
1956 } else if (!Succ0 || !Succ1 || OneSuccessorDone) {
1957 // Optimization: When only one successor is an outgoing block,
1958 // the incoming predicate from `In` is always true.
1959 Phi->addIncoming(BoolTrue, In);
1960 } else {
1961 assert(Succ0 && Succ1);
1962 if (Out == Succ0) {
1963 Phi->addIncoming(Condition, In);
1964 } else {
1965 auto Inverted = invertCondition(Condition);
1966 DeletionCandidates.push_back(Condition);
1967 Phi->addIncoming(Inverted, In);
1969 OneSuccessorDone = true;
1975 // Capture the existing control flow as guard predicates, and redirect
1976 // control flow from \p Incoming block through the \p GuardBlocks to the
1977 // \p Outgoing blocks.
1979 // There is one guard predicate for each outgoing block OutBB. The
1980 // predicate represents whether the hub should transfer control flow
1981 // to OutBB. These predicates are NOT ORTHOGONAL. The Hub evaluates
1982 // them in the same order as the Outgoing set-vector, and control
1983 // branches to the first outgoing block whose predicate evaluates to true.
1984 static void
1985 convertToGuardPredicates(SmallVectorImpl<BasicBlock *> &GuardBlocks,
1986 SmallVectorImpl<WeakVH> &DeletionCandidates,
1987 const BBSetVector &Incoming,
1988 const BBSetVector &Outgoing, const StringRef Prefix,
1989 std::optional<unsigned> MaxControlFlowBooleans) {
1990 BBPredicates GuardPredicates;
1991 auto F = Incoming.front()->getParent();
1993 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i)
1994 GuardBlocks.push_back(
1995 BasicBlock::Create(F->getContext(), Prefix + ".guard", F));
1997 // When we are using an integer to record which target block to jump to, we
1998 // are creating less live values, actually we are using one single integer to
1999 // store the index of the target block. When we are using booleans to store
2000 // the branching information, we need (N-1) boolean values, where N is the
2001 // number of outgoing block.
2002 if (!MaxControlFlowBooleans || Outgoing.size() <= *MaxControlFlowBooleans)
2003 calcPredicateUsingBooleans(Incoming, Outgoing, GuardBlocks, GuardPredicates,
2004 DeletionCandidates);
2005 else
2006 calcPredicateUsingInteger(Incoming, Outgoing, GuardBlocks, GuardPredicates);
2008 setupBranchForGuard(GuardBlocks, Outgoing, GuardPredicates);
2011 BasicBlock *llvm::CreateControlFlowHub(
2012 DomTreeUpdater *DTU, SmallVectorImpl<BasicBlock *> &GuardBlocks,
2013 const BBSetVector &Incoming, const BBSetVector &Outgoing,
2014 const StringRef Prefix, std::optional<unsigned> MaxControlFlowBooleans) {
2015 if (Outgoing.size() < 2)
2016 return Outgoing.front();
2018 SmallVector<DominatorTree::UpdateType, 16> Updates;
2019 if (DTU) {
2020 for (auto *In : Incoming) {
2021 for (auto Succ : successors(In))
2022 if (Outgoing.count(Succ))
2023 Updates.push_back({DominatorTree::Delete, In, Succ});
2027 SmallVector<WeakVH, 8> DeletionCandidates;
2028 convertToGuardPredicates(GuardBlocks, DeletionCandidates, Incoming, Outgoing,
2029 Prefix, MaxControlFlowBooleans);
2030 auto FirstGuardBlock = GuardBlocks.front();
2032 // Update the PHINodes in each outgoing block to match the new control flow.
2033 for (int i = 0, e = GuardBlocks.size(); i != e; ++i)
2034 reconnectPhis(Outgoing[i], GuardBlocks[i], Incoming, FirstGuardBlock);
2036 reconnectPhis(Outgoing.back(), GuardBlocks.back(), Incoming, FirstGuardBlock);
2038 if (DTU) {
2039 int NumGuards = GuardBlocks.size();
2040 assert((int)Outgoing.size() == NumGuards + 1);
2042 for (auto In : Incoming)
2043 Updates.push_back({DominatorTree::Insert, In, FirstGuardBlock});
2045 for (int i = 0; i != NumGuards - 1; ++i) {
2046 Updates.push_back({DominatorTree::Insert, GuardBlocks[i], Outgoing[i]});
2047 Updates.push_back(
2048 {DominatorTree::Insert, GuardBlocks[i], GuardBlocks[i + 1]});
2050 Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1],
2051 Outgoing[NumGuards - 1]});
2052 Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1],
2053 Outgoing[NumGuards]});
2054 DTU->applyUpdates(Updates);
2057 for (auto I : DeletionCandidates) {
2058 if (I->use_empty())
2059 if (auto Inst = dyn_cast_or_null<Instruction>(I))
2060 Inst->eraseFromParent();
2063 return FirstGuardBlock;
2066 void llvm::InvertBranch(BranchInst *PBI, IRBuilderBase &Builder) {
2067 Value *NewCond = PBI->getCondition();
2068 // If this is a "cmp" instruction, only used for branching (and nowhere
2069 // else), then we can simply invert the predicate.
2070 if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {
2071 CmpInst *CI = cast<CmpInst>(NewCond);
2072 CI->setPredicate(CI->getInversePredicate());
2073 } else
2074 NewCond = Builder.CreateNot(NewCond, NewCond->getName() + ".not");
2076 PBI->setCondition(NewCond);
2077 PBI->swapSuccessors();