LLVM 17.0.0git
BasicBlockUtils.cpp
Go to the documentation of this file.
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.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/Analysis/CFG.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"
29#include "llvm/IR/Dominators.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/InstrTypes.h"
32#include "llvm/IR/Instruction.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"
43#include "llvm/Support/Debug.h"
46#include <cassert>
47#include <cstdint>
48#include <string>
49#include <utility>
50#include <vector>
51
52using namespace llvm;
53
54#define DEBUG_TYPE "basicblock-utils"
55
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"));
61
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});
74 }
75
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();
87 }
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.");
93 }
94}
95
97 bool KeepOneInputPHIs) {
98 DeleteDeadBlocks({BB}, DTU, KeepOneInputPHIs);
99}
100
101void 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
111
113 detachDeadBlocks(BBs, DTU ? &Updates : nullptr, KeepOneInputPHIs);
114
115 if (DTU)
116 DTU->applyUpdates(Updates);
117
118 for (BasicBlock *BB : BBs)
119 if (DTU)
120 DTU->deleteBB(BB);
121 else
122 BB->eraseFromParent();
123}
124
126 bool KeepOneInputPHIs) {
128
129 // Mark all reachable blocks.
130 for (BasicBlock *BB : depth_first_ext(&F, Reachable))
131 (void)BB/* Mark all reachable blocks */;
132
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);
138
139 // Delete the dead blocks.
140 DeleteDeadBlocks(DeadBlocks, DTU, KeepOneInputPHIs);
141
142 return !DeadBlocks.empty();
143}
144
146 MemoryDependenceResults *MemDep) {
147 if (!isa<PHINode>(BB->begin()))
148 return false;
149
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()));
155
156 if (MemDep)
157 MemDep->removeInstruction(PN); // Memdep updates AA itself.
158
159 PN->eraseFromParent();
160 }
161 return true;
162}
163
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.
169 for (PHINode &PN : BB->phis())
170 PHIs.push_back(&PN);
171
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);
176
177 return Changed;
178}
179
181 LoopInfo *LI, MemorySSAUpdater *MSSAU,
183 bool PredecessorWithTwoSuccessors,
184 DominatorTree *DT) {
185 if (BB->hasAddressTaken())
186 return false;
187
188 // Can't merge if there are multiple predecessors, or no predecessors.
189 BasicBlock *PredBB = BB->getUniquePredecessor();
190 if (!PredBB) return false;
191
192 // Don't break self-loops.
193 if (PredBB == BB) return false;
194
195 // Don't break unwinding instructions or terminators with other side-effects.
196 Instruction *PTI = PredBB->getTerminator();
197 if (PTI->isExceptionalTerminator() || PTI->mayHaveSideEffects())
198 return false;
199
200 // Can't merge if there are multiple distinct successors.
201 if (!PredecessorWithTwoSuccessors && PredBB->getUniqueSuccessor() != BB)
202 return false;
203
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;
217 }
218
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;
223
224 LLVM_DEBUG(dbgs() << "Merging: " << BB->getName() << " into "
225 << PredBB->getName() << "\n");
226
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);
235 }
236
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);
245 }
246 }
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.
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});
273 }
274
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;
281
282 // Move all definitions in the successor to the predecessor...
283 PredBB->splice(PTI->getIterator(), BB, BB->begin(), STI->getIterator());
284
285 if (MSSAU)
286 MSSAU->moveAllAfterMergeBlocks(BB, PredBB, Start);
287
288 // Make all PHI nodes that referred to BB now refer to Pred as their
289 // source...
290 BB->replaceAllUsesWith(PredBB);
291
292 if (PredecessorWithTwoSuccessors) {
293 // Delete the unconditional branch from BB.
294 BB->back().eraseFromParent();
295
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();
301
302 // Move terminator instruction.
303 PredBB->splice(PredBB->end(), BB);
304
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);
310 }
311 // Add unreachable to now empty BB.
312 new UnreachableInst(BB->getContext(), BB);
313
314 // Inherit predecessors name if it exists.
315 if (!PredBB->hasName())
316 PredBB->takeName(BB);
317
318 if (LI)
319 LI->removeBlock(BB);
320
321 if (MemDep)
323
324 if (DTU)
325 DTU->applyUpdates(Updates);
326
327 if (DT) {
328 assert(succ_empty(BB) &&
329 "successors should have been transferred to PredBB");
330 DT->eraseNode(BB);
331 }
332
333 // Finally, erase the old block and update dominator info.
334 DeleteDeadBlock(BB, DTU);
335
336 return true;
337}
338
341 LoopInfo *LI) {
342 assert(!MergeBlocks.empty() && "MergeBlocks should not be empty");
343
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);
360 }
361 return BlocksHaveBeenMerged;
362}
363
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.
367///
368/// BackwardScan strategy:
369/// ----------------------
370/// Given a sequence of consecutive DbgValueInst like this
371///
372/// dbg.value ..., "x", FragmentX1 (*)
373/// dbg.value ..., "y", FragmentY1
374/// dbg.value ..., "x", FragmentX2
375/// dbg.value ..., "x", FragmentX1 (**)
376///
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).
380///
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.
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;
398
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.
404 }
405
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;
411 }
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();
416 }
417
418 for (auto &Instr : ToBeRemoved)
419 Instr->eraseFromParent();
420
421 return !ToBeRemoved.empty();
422}
423
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.
428///
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
433///
434/// dbg.value X1, "x", FragmentX1 (**)
435/// <block of instructions, none being "dbg.value ..., "x", ...">
436/// dbg.value X1, "x", FragmentX1 (*)
437///
438/// then the instruction marked with (*) can be removed. Variable "x" is already
439/// described as being mapped to the SSA value X1.
440///
441/// Possible improvements:
442/// - Keep track of non-overlapping fragments.
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());
456
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;
470 }
471
472 // Don't delete dbg.assign intrinsics that are linked to instructions.
473 if (!IsDbgValueKind)
474 continue;
475 ToBeRemoved.push_back(DVI);
476 }
477 }
478
479 for (auto &Instr : ToBeRemoved)
480 Instr->eraseFromParent();
481
482 return !ToBeRemoved.empty();
483}
484
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:
494///
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
500///
501/// then (only) the instruction marked with (*) can be removed.
502/// Possible improvements:
503/// - Keep track of non-overlapping fragments.
505 assert(BB->isEntryBlock() && "expected entry block");
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());
512 };
513
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);
529 }
530 }
531 }
532
533 for (DbgAssignIntrinsic *DAI : ToBeRemoved)
534 DAI->eraseFromParent();
535
536 return !ToBeRemoved.empty();
537}
538
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:
543 //
544 // (1) dbg.value V1, "x", DIExpression()
545 // ...
546 // (2) dbg.value V2, "x", DIExpression()
547 // (3) dbg.value V1, "x", DIExpression()
548 //
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).
553 if (BB->isEntryBlock() &&
555 MadeChanges |= remomveUndefDbgAssignsFromEntryBlock(BB);
557
558 if (MadeChanges)
559 LLVM_DEBUG(dbgs() << "Removed redundant dbg instrs from: "
560 << BB->getName() << "\n");
561 return MadeChanges;
562}
563
565 Instruction &I = *BI;
566 // Replaces all of the uses of the instruction with uses of the value
567 I.replaceAllUsesWith(V);
568
569 // Make sure to propagate a name if there is one already.
570 if (I.hasName() && !V->hasName())
571 V->takeName(&I);
572
573 // Delete the unnecessary instruction now...
574 BI = BI->eraseFromParent();
575}
576
578 Instruction *I) {
579 assert(I->getParent() == nullptr &&
580 "ReplaceInstWithInst: Instruction already inserted into basic block!");
581
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());
586
587 // Insert the new instruction into the basic block...
588 BasicBlock::iterator New = I->insertInto(BB, BI);
589
590 // Replace all uses of the old instruction, and delete it.
592
593 // Move BI back to point to the newly inserted instruction
594 BI = New;
595}
596
598 // Remember visited blocks to avoid infinite loop
600 unsigned Depth = 0;
602 VisitedBlocks.insert(BB).second) {
603 if (isa<UnreachableInst>(BB->getTerminator()) ||
605 return true;
606 BB = BB->getUniqueSuccessor();
607 }
608 return false;
609}
610
613 ReplaceInstWithInst(From->getParent(), BI, To);
614}
615
617 LoopInfo *LI, MemorySSAUpdater *MSSAU,
618 const Twine &BBName) {
619 unsigned SuccNum = GetSuccessorNumber(BB, Succ);
620
621 Instruction *LatchTerm = BB->getTerminator();
622
625
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);
631
632 // If this is a critical edge, let SplitKnownCriticalEdge do it.
633 return SplitKnownCriticalEdge(LatchTerm, SuccNum, Options, BBName);
634 }
635
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);
645 }
646
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);
652}
653
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");
663}
664
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;
673
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);
681
682 assert(BBIdx != -1 && "Invalid PHI Index!");
683 PN.setIncomingBlock(BBIdx, NewPred);
684 }
685}
686
688 LandingPadInst *OriginalPad,
689 PHINode *LandingPadReplacement,
691 const Twine &BBName) {
692
693 auto *PadInst = Succ->getFirstNonPHI();
694 if (!LandingPadReplacement && !PadInst->isEHPad())
695 return SplitEdge(BB, Succ, Options.DT, Options.LI, Options.MSSAU, BBName);
696
697 auto *LI = Options.LI;
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)) {
704
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;
722 }
723 LoopPreds.push_back(P);
724 }
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;
731 }
732 }
733 }
734
735 auto *NewBB =
736 BasicBlock::Create(BB->getContext(), BBName, BB->getParent(), Succ);
737 setUnwindEdgeTo(BB->getTerminator(), NewBB);
738 updatePhiNodes(Succ, BB, NewBB, LandingPadReplacement);
739
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");
757
758 auto *NewCleanupPad = CleanupPadInst::Create(ParentPad, {}, BBName, NewBB);
759 CleanupReturnInst::Create(NewCleanupPad, Succ, NewBB);
760 }
761
762 auto *DT = Options.DT;
763 auto *MSSAU = Options.MSSAU;
764 if (!DT && !LI)
765 return NewBB;
766
767 if (DT) {
768 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
770
771 Updates.push_back({DominatorTree::Insert, BB, NewBB});
772 Updates.push_back({DominatorTree::Insert, NewBB, Succ});
773 Updates.push_back({DominatorTree::Delete, BB, Succ});
774
775 DTU.applyUpdates(Updates);
776 DTU.flush();
777
778 if (MSSAU) {
779 MSSAU->applyUpdates(Updates, *DT);
780 if (VerifyMemorySSA)
781 MSSAU->getMemorySSA()->verifyMemorySSA();
782 }
783 }
784
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);
808 }
809 }
810
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!");
816
817 // Update LCSSA form in the newly created exit block.
818 if (Options.PreserveLCSSA) {
819 createPHIsForSplitLoopExit(BB, NewBB, Succ);
820 }
821
822 if (!LoopPreds.empty()) {
824 Succ, LoopPreds, "split", DT, LI, MSSAU, Options.PreserveLCSSA);
825 if (Options.PreserveLCSSA)
826 createPHIsForSplitLoopExit(LoopPreds, NewExitBB, Succ);
827 }
828 }
829 }
830 }
831
832 return NewBB;
833}
834
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!");
841
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);
847
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;
853
854 // Otherwise a new PHI is needed. Create one and populate it.
855 PHINode *NewPN = PHINode::Create(
856 PN.getType(), Preds.size(), "split",
857 SplitBB->isLandingPad() ? &SplitBB->front() : SplitBB->getTerminator());
858 for (BasicBlock *BB : Preds)
859 NewPN->addIncoming(V, BB);
860
861 // Update the original PHI.
862 PN.setIncomingValue(Idx, NewPN);
863 }
864}
865
866unsigned
869 unsigned NumBroken = 0;
870 for (BasicBlock &BB : F) {
871 Instruction *TI = BB.getTerminator();
872 if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
873 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
874 if (SplitCriticalEdge(TI, i, Options))
875 ++NumBroken;
876 }
877 return NumBroken;
878}
879
882 LoopInfo *LI, MemorySSAUpdater *MSSAU,
883 const Twine &BBName, bool Before) {
884 if (Before) {
885 DomTreeUpdater LocalDTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
886 return splitBlockBefore(Old, SplitPt,
887 DTU ? DTU : (DT ? &LocalDTU : nullptr), LI, MSSAU,
888 BBName);
889 }
890 BasicBlock::iterator SplitIt = SplitPt->getIterator();
891 while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) {
892 ++SplitIt;
893 assert(SplitIt != SplitPt->getParent()->end());
894 }
895 std::string Name = BBName.str();
896 BasicBlock *New = Old->splitBasicBlock(
897 SplitIt, Name.empty() ? Old->getName() + ".split" : Name);
898
899 // The new block lives in whichever loop the old one did. This preserves
900 // LCSSA as well, because we force the split point to be after any PHI nodes.
901 if (LI)
902 if (Loop *L = LI->getLoopFor(Old))
903 L->addBasicBlockToLoop(New, *LI);
904
905 if (DTU) {
907 // Old dominates New. New node dominates all other nodes dominated by Old.
908 SmallPtrSet<BasicBlock *, 8> UniqueSuccessorsOfOld;
909 Updates.push_back({DominatorTree::Insert, Old, New});
910 Updates.reserve(Updates.size() + 2 * succ_size(New));
911 for (BasicBlock *SuccessorOfOld : successors(New))
912 if (UniqueSuccessorsOfOld.insert(SuccessorOfOld).second) {
913 Updates.push_back({DominatorTree::Insert, New, SuccessorOfOld});
914 Updates.push_back({DominatorTree::Delete, Old, SuccessorOfOld});
915 }
916
917 DTU->applyUpdates(Updates);
918 } else if (DT)
919 // Old dominates New. New node dominates all other nodes dominated by Old.
920 if (DomTreeNode *OldNode = DT->getNode(Old)) {
921 std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
922
923 DomTreeNode *NewNode = DT->addNewBlock(New, Old);
924 for (DomTreeNode *I : Children)
925 DT->changeImmediateDominator(I, NewNode);
926 }
927
928 // Move MemoryAccesses still tracked in Old, but part of New now.
929 // Update accesses in successor blocks accordingly.
930 if (MSSAU)
931 MSSAU->moveAllAfterSpliceBlocks(Old, New, &*(New->begin()));
932
933 return New;
934}
935
937 DominatorTree *DT, LoopInfo *LI,
938 MemorySSAUpdater *MSSAU, const Twine &BBName,
939 bool Before) {
940 return SplitBlockImpl(Old, SplitPt, /*DTU=*/nullptr, DT, LI, MSSAU, BBName,
941 Before);
942}
944 DomTreeUpdater *DTU, LoopInfo *LI,
945 MemorySSAUpdater *MSSAU, const Twine &BBName,
946 bool Before) {
947 return SplitBlockImpl(Old, SplitPt, DTU, /*DT=*/nullptr, LI, MSSAU, BBName,
948 Before);
949}
950
952 DomTreeUpdater *DTU, LoopInfo *LI,
953 MemorySSAUpdater *MSSAU,
954 const Twine &BBName) {
955
956 BasicBlock::iterator SplitIt = SplitPt->getIterator();
957 while (isa<PHINode>(SplitIt) || SplitIt->isEHPad())
958 ++SplitIt;
959 std::string Name = BBName.str();
960 BasicBlock *New = Old->splitBasicBlock(
961 SplitIt, Name.empty() ? Old->getName() + ".split" : Name,
962 /* Before=*/true);
963
964 // The new block lives in whichever loop the old one did. This preserves
965 // LCSSA as well, because we force the split point to be after any PHI nodes.
966 if (LI)
967 if (Loop *L = LI->getLoopFor(Old))
968 L->addBasicBlockToLoop(New, *LI);
969
970 if (DTU) {
972 // New dominates Old. The predecessor nodes of the Old node dominate
973 // New node.
974 SmallPtrSet<BasicBlock *, 8> UniquePredecessorsOfOld;
975 DTUpdates.push_back({DominatorTree::Insert, New, Old});
976 DTUpdates.reserve(DTUpdates.size() + 2 * pred_size(New));
977 for (BasicBlock *PredecessorOfOld : predecessors(New))
978 if (UniquePredecessorsOfOld.insert(PredecessorOfOld).second) {
979 DTUpdates.push_back({DominatorTree::Insert, PredecessorOfOld, New});
980 DTUpdates.push_back({DominatorTree::Delete, PredecessorOfOld, Old});
981 }
982
983 DTU->applyUpdates(DTUpdates);
984
985 // Move MemoryAccesses still tracked in Old, but part of New now.
986 // Update accesses in successor blocks accordingly.
987 if (MSSAU) {
988 MSSAU->applyUpdates(DTUpdates, DTU->getDomTree());
989 if (VerifyMemorySSA)
990 MSSAU->getMemorySSA()->verifyMemorySSA();
991 }
992 }
993 return New;
994}
995
996/// Update DominatorTree, LoopInfo, and LCCSA analysis information.
1000 LoopInfo *LI, MemorySSAUpdater *MSSAU,
1001 bool PreserveLCSSA, bool &HasLoopExit) {
1002 // Update dominator tree if available.
1003 if (DTU) {
1004 // Recalculation of DomTree is needed when updating a forward DomTree and
1005 // the Entry BB is replaced.
1006 if (NewBB->isEntryBlock() && DTU->hasDomTree()) {
1007 // The entry block was removed and there is no external interface for
1008 // the dominator tree to be notified of this change. In this corner-case
1009 // we recalculate the entire tree.
1010 DTU->recalculate(*NewBB->getParent());
1011 } else {
1012 // Split block expects NewBB to have a non-empty set of predecessors.
1014 SmallPtrSet<BasicBlock *, 8> UniquePreds;
1015 Updates.push_back({DominatorTree::Insert, NewBB, OldBB});
1016 Updates.reserve(Updates.size() + 2 * Preds.size());
1017 for (auto *Pred : Preds)
1018 if (UniquePreds.insert(Pred).second) {
1019 Updates.push_back({DominatorTree::Insert, Pred, NewBB});
1020 Updates.push_back({DominatorTree::Delete, Pred, OldBB});
1021 }
1022 DTU->applyUpdates(Updates);
1023 }
1024 } else if (DT) {
1025 if (OldBB == DT->getRootNode()->getBlock()) {
1026 assert(NewBB->isEntryBlock());
1027 DT->setNewRoot(NewBB);
1028 } else {
1029 // Split block expects NewBB to have a non-empty set of predecessors.
1030 DT->splitBlock(NewBB);
1031 }
1032 }
1033
1034 // Update MemoryPhis after split if MemorySSA is available
1035 if (MSSAU)
1036 MSSAU->wireOldPredecessorsToNewImmediatePredecessor(OldBB, NewBB, Preds);
1037
1038 // The rest of the logic is only relevant for updating the loop structures.
1039 if (!LI)
1040 return;
1041
1042 if (DTU && DTU->hasDomTree())
1043 DT = &DTU->getDomTree();
1044 assert(DT && "DT should be available to update LoopInfo!");
1045 Loop *L = LI->getLoopFor(OldBB);
1046
1047 // If we need to preserve loop analyses, collect some information about how
1048 // this split will affect loops.
1049 bool IsLoopEntry = !!L;
1050 bool SplitMakesNewLoopHeader = false;
1051 for (BasicBlock *Pred : Preds) {
1052 // Preds that are not reachable from entry should not be used to identify if
1053 // OldBB is a loop entry or if SplitMakesNewLoopHeader. Unreachable blocks
1054 // are not within any loops, so we incorrectly mark SplitMakesNewLoopHeader
1055 // as true and make the NewBB the header of some loop. This breaks LI.
1056 if (!DT->isReachableFromEntry(Pred))
1057 continue;
1058 // If we need to preserve LCSSA, determine if any of the preds is a loop
1059 // exit.
1060 if (PreserveLCSSA)
1061 if (Loop *PL = LI->getLoopFor(Pred))
1062 if (!PL->contains(OldBB))
1063 HasLoopExit = true;
1064
1065 // If we need to preserve LoopInfo, note whether any of the preds crosses
1066 // an interesting loop boundary.
1067 if (!L)
1068 continue;
1069 if (L->contains(Pred))
1070 IsLoopEntry = false;
1071 else
1072 SplitMakesNewLoopHeader = true;
1073 }
1074
1075 // Unless we have a loop for OldBB, nothing else to do here.
1076 if (!L)
1077 return;
1078
1079 if (IsLoopEntry) {
1080 // Add the new block to the nearest enclosing loop (and not an adjacent
1081 // loop). To find this, examine each of the predecessors and determine which
1082 // loops enclose them, and select the most-nested loop which contains the
1083 // loop containing the block being split.
1084 Loop *InnermostPredLoop = nullptr;
1085 for (BasicBlock *Pred : Preds) {
1086 if (Loop *PredLoop = LI->getLoopFor(Pred)) {
1087 // Seek a loop which actually contains the block being split (to avoid
1088 // adjacent loops).
1089 while (PredLoop && !PredLoop->contains(OldBB))
1090 PredLoop = PredLoop->getParentLoop();
1091
1092 // Select the most-nested of these loops which contains the block.
1093 if (PredLoop && PredLoop->contains(OldBB) &&
1094 (!InnermostPredLoop ||
1095 InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))
1096 InnermostPredLoop = PredLoop;
1097 }
1098 }
1099
1100 if (InnermostPredLoop)
1101 InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI);
1102 } else {
1103 L->addBasicBlockToLoop(NewBB, *LI);
1104 if (SplitMakesNewLoopHeader)
1105 L->moveToHeader(NewBB);
1106 }
1107}
1108
1109/// Update the PHI nodes in OrigBB to include the values coming from NewBB.
1110/// This also updates AliasAnalysis, if available.
1111static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,
1113 bool HasLoopExit) {
1114 // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB.
1115 SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end());
1116 for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) {
1117 PHINode *PN = cast<PHINode>(I++);
1118
1119 // Check to see if all of the values coming in are the same. If so, we
1120 // don't need to create a new PHI node, unless it's needed for LCSSA.
1121 Value *InVal = nullptr;
1122 if (!HasLoopExit) {
1123 InVal = PN->getIncomingValueForBlock(Preds[0]);
1124 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1125 if (!PredSet.count(PN->getIncomingBlock(i)))
1126 continue;
1127 if (!InVal)
1128 InVal = PN->getIncomingValue(i);
1129 else if (InVal != PN->getIncomingValue(i)) {
1130 InVal = nullptr;
1131 break;
1132 }
1133 }
1134 }
1135
1136 if (InVal) {
1137 // If all incoming values for the new PHI would be the same, just don't
1138 // make a new PHI. Instead, just remove the incoming values from the old
1139 // PHI.
1140
1141 // NOTE! This loop walks backwards for a reason! First off, this minimizes
1142 // the cost of removal if we end up removing a large number of values, and
1143 // second off, this ensures that the indices for the incoming values
1144 // aren't invalidated when we remove one.
1145 for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i)
1146 if (PredSet.count(PN->getIncomingBlock(i)))
1147 PN->removeIncomingValue(i, false);
1148
1149 // Add an incoming value to the PHI node in the loop for the preheader
1150 // edge.
1151 PN->addIncoming(InVal, NewBB);
1152 continue;
1153 }
1154
1155 // If the values coming into the block are not the same, we need a new
1156 // PHI.
1157 // Create the new PHI node, insert it into NewBB at the end of the block
1158 PHINode *NewPHI =
1159 PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI);
1160
1161 // NOTE! This loop walks backwards for a reason! First off, this minimizes
1162 // the cost of removal if we end up removing a large number of values, and
1163 // second off, this ensures that the indices for the incoming values aren't
1164 // invalidated when we remove one.
1165 for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) {
1166 BasicBlock *IncomingBB = PN->getIncomingBlock(i);
1167 if (PredSet.count(IncomingBB)) {
1168 Value *V = PN->removeIncomingValue(i, false);
1169 NewPHI->addIncoming(V, IncomingBB);
1170 }
1171 }
1172
1173 PN->addIncoming(NewPHI, NewBB);
1174 }
1175}
1176
1178 BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1,
1179 const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
1180 DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI,
1181 MemorySSAUpdater *MSSAU, bool PreserveLCSSA);
1182
1183static BasicBlock *
1185 const char *Suffix, DomTreeUpdater *DTU,
1186 DominatorTree *DT, LoopInfo *LI,
1187 MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {
1188 // Do not attempt to split that which cannot be split.
1189 if (!BB->canSplitPredecessors())
1190 return nullptr;
1191
1192 // For the landingpads we need to act a bit differently.
1193 // Delegate this work to the SplitLandingPadPredecessors.
1194 if (BB->isLandingPad()) {
1196 std::string NewName = std::string(Suffix) + ".split-lp";
1197
1198 SplitLandingPadPredecessorsImpl(BB, Preds, Suffix, NewName.c_str(), NewBBs,
1199 DTU, DT, LI, MSSAU, PreserveLCSSA);
1200 return NewBBs[0];
1201 }
1202
1203 // Create new basic block, insert right before the original block.
1205 BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB);
1206
1207 // The new block unconditionally branches to the old block.
1208 BranchInst *BI = BranchInst::Create(BB, NewBB);
1209
1210 Loop *L = nullptr;
1211 BasicBlock *OldLatch = nullptr;
1212 // Splitting the predecessors of a loop header creates a preheader block.
1213 if (LI && LI->isLoopHeader(BB)) {
1214 L = LI->getLoopFor(BB);
1215 // Using the loop start line number prevents debuggers stepping into the
1216 // loop body for this instruction.
1217 BI->setDebugLoc(L->getStartLoc());
1218
1219 // If BB is the header of the Loop, it is possible that the loop is
1220 // modified, such that the current latch does not remain the latch of the
1221 // loop. If that is the case, the loop metadata from the current latch needs
1222 // to be applied to the new latch.
1223 OldLatch = L->getLoopLatch();
1224 } else
1226
1227 // Move the edges from Preds to point to NewBB instead of BB.
1228 for (BasicBlock *Pred : Preds) {
1229 // This is slightly more strict than necessary; the minimum requirement
1230 // is that there be no more than one indirectbr branching to BB. And
1231 // all BlockAddress uses would need to be updated.
1232 assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
1233 "Cannot split an edge from an IndirectBrInst");
1234 Pred->getTerminator()->replaceSuccessorWith(BB, NewBB);
1235 }
1236
1237 // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
1238 // node becomes an incoming value for BB's phi node. However, if the Preds
1239 // list is empty, we need to insert dummy entries into the PHI nodes in BB to
1240 // account for the newly created predecessor.
1241 if (Preds.empty()) {
1242 // Insert dummy values as the incoming value.
1243 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
1244 cast<PHINode>(I)->addIncoming(PoisonValue::get(I->getType()), NewBB);
1245 }
1246
1247 // Update DominatorTree, LoopInfo, and LCCSA analysis information.
1248 bool HasLoopExit = false;
1249 UpdateAnalysisInformation(BB, NewBB, Preds, DTU, DT, LI, MSSAU, PreserveLCSSA,
1250 HasLoopExit);
1251
1252 if (!Preds.empty()) {
1253 // Update the PHI nodes in BB with the values coming from NewBB.
1254 UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit);
1255 }
1256
1257 if (OldLatch) {
1258 BasicBlock *NewLatch = L->getLoopLatch();
1259 if (NewLatch != OldLatch) {
1260 MDNode *MD = OldLatch->getTerminator()->getMetadata("llvm.loop");
1261 NewLatch->getTerminator()->setMetadata("llvm.loop", MD);
1262 // It's still possible that OldLatch is the latch of another inner loop,
1263 // in which case we do not remove the metadata.
1264 Loop *IL = LI->getLoopFor(OldLatch);
1265 if (IL && IL->getLoopLatch() != OldLatch)
1266 OldLatch->getTerminator()->setMetadata("llvm.loop", nullptr);
1267 }
1268 }
1269
1270 return NewBB;
1271}
1272
1275 const char *Suffix, DominatorTree *DT,
1276 LoopInfo *LI, MemorySSAUpdater *MSSAU,
1277 bool PreserveLCSSA) {
1278 return SplitBlockPredecessorsImpl(BB, Preds, Suffix, /*DTU=*/nullptr, DT, LI,
1279 MSSAU, PreserveLCSSA);
1280}
1283 const char *Suffix,
1284 DomTreeUpdater *DTU, LoopInfo *LI,
1285 MemorySSAUpdater *MSSAU,
1286 bool PreserveLCSSA) {
1287 return SplitBlockPredecessorsImpl(BB, Preds, Suffix, DTU,
1288 /*DT=*/nullptr, LI, MSSAU, PreserveLCSSA);
1289}
1290
1292 BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1,
1293 const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
1294 DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI,
1295 MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {
1296 assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!");
1297
1298 // Create a new basic block for OrigBB's predecessors listed in Preds. Insert
1299 // it right before the original block.
1300 BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(),
1301 OrigBB->getName() + Suffix1,
1302 OrigBB->getParent(), OrigBB);
1303 NewBBs.push_back(NewBB1);
1304
1305 // The new block unconditionally branches to the old block.
1306 BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1);
1307 BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
1308
1309 // Move the edges from Preds to point to NewBB1 instead of OrigBB.
1310 for (BasicBlock *Pred : Preds) {
1311 // This is slightly more strict than necessary; the minimum requirement
1312 // is that there be no more than one indirectbr branching to BB. And
1313 // all BlockAddress uses would need to be updated.
1314 assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
1315 "Cannot split an edge from an IndirectBrInst");
1316 Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
1317 }
1318
1319 bool HasLoopExit = false;
1320 UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DTU, DT, LI, MSSAU,
1321 PreserveLCSSA, HasLoopExit);
1322
1323 // Update the PHI nodes in OrigBB with the values coming from NewBB1.
1324 UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit);
1325
1326 // Move the remaining edges from OrigBB to point to NewBB2.
1327 SmallVector<BasicBlock*, 8> NewBB2Preds;
1328 for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
1329 i != e; ) {
1330 BasicBlock *Pred = *i++;
1331 if (Pred == NewBB1) continue;
1332 assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
1333 "Cannot split an edge from an IndirectBrInst");
1334 NewBB2Preds.push_back(Pred);
1335 e = pred_end(OrigBB);
1336 }
1337
1338 BasicBlock *NewBB2 = nullptr;
1339 if (!NewBB2Preds.empty()) {
1340 // Create another basic block for the rest of OrigBB's predecessors.
1341 NewBB2 = BasicBlock::Create(OrigBB->getContext(),
1342 OrigBB->getName() + Suffix2,
1343 OrigBB->getParent(), OrigBB);
1344 NewBBs.push_back(NewBB2);
1345
1346 // The new block unconditionally branches to the old block.
1347 BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2);
1348 BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
1349
1350 // Move the remaining edges from OrigBB to point to NewBB2.
1351 for (BasicBlock *NewBB2Pred : NewBB2Preds)
1352 NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
1353
1354 // Update DominatorTree, LoopInfo, and LCCSA analysis information.
1355 HasLoopExit = false;
1356 UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DTU, DT, LI, MSSAU,
1357 PreserveLCSSA, HasLoopExit);
1358
1359 // Update the PHI nodes in OrigBB with the values coming from NewBB2.
1360 UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit);
1361 }
1362
1363 LandingPadInst *LPad = OrigBB->getLandingPadInst();
1364 Instruction *Clone1 = LPad->clone();
1365 Clone1->setName(Twine("lpad") + Suffix1);
1366 Clone1->insertInto(NewBB1, NewBB1->getFirstInsertionPt());
1367
1368 if (NewBB2) {
1369 Instruction *Clone2 = LPad->clone();
1370 Clone2->setName(Twine("lpad") + Suffix2);
1371 Clone2->insertInto(NewBB2, NewBB2->getFirstInsertionPt());
1372
1373 // Create a PHI node for the two cloned landingpad instructions only
1374 // if the original landingpad instruction has some uses.
1375 if (!LPad->use_empty()) {
1376 assert(!LPad->getType()->isTokenTy() &&
1377 "Split cannot be applied if LPad is token type. Otherwise an "
1378 "invalid PHINode of token type would be created.");
1379 PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad);
1380 PN->addIncoming(Clone1, NewBB1);
1381 PN->addIncoming(Clone2, NewBB2);
1382 LPad->replaceAllUsesWith(PN);
1383 }
1384 LPad->eraseFromParent();
1385 } else {
1386 // There is no second clone. Just replace the landing pad with the first
1387 // clone.
1388 LPad->replaceAllUsesWith(Clone1);
1389 LPad->eraseFromParent();
1390 }
1391}
1392
1395 const char *Suffix1, const char *Suffix2,
1397 DominatorTree *DT, LoopInfo *LI,
1398 MemorySSAUpdater *MSSAU,
1399 bool PreserveLCSSA) {
1401 OrigBB, Preds, Suffix1, Suffix2, NewBBs,
1402 /*DTU=*/nullptr, DT, LI, MSSAU, PreserveLCSSA);
1403}
1406 const char *Suffix1, const char *Suffix2,
1408 DomTreeUpdater *DTU, LoopInfo *LI,
1409 MemorySSAUpdater *MSSAU,
1410 bool PreserveLCSSA) {
1411 return SplitLandingPadPredecessorsImpl(OrigBB, Preds, Suffix1, Suffix2,
1412 NewBBs, DTU, /*DT=*/nullptr, LI, MSSAU,
1413 PreserveLCSSA);
1414}
1415
1417 BasicBlock *Pred,
1418 DomTreeUpdater *DTU) {
1419 Instruction *UncondBranch = Pred->getTerminator();
1420 // Clone the return and add it to the end of the predecessor.
1421 Instruction *NewRet = RI->clone();
1422 NewRet->insertInto(Pred, Pred->end());
1423
1424 // If the return instruction returns a value, and if the value was a
1425 // PHI node in "BB", propagate the right value into the return.
1426 for (Use &Op : NewRet->operands()) {
1427 Value *V = Op;
1428 Instruction *NewBC = nullptr;
1429 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {
1430 // Return value might be bitcasted. Clone and insert it before the
1431 // return instruction.
1432 V = BCI->getOperand(0);
1433 NewBC = BCI->clone();
1434 NewBC->insertInto(Pred, NewRet->getIterator());
1435 Op = NewBC;
1436 }
1437
1438 Instruction *NewEV = nullptr;
1439 if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
1440 V = EVI->getOperand(0);
1441 NewEV = EVI->clone();
1442 if (NewBC) {
1443 NewBC->setOperand(0, NewEV);
1444 NewEV->insertInto(Pred, NewBC->getIterator());
1445 } else {
1446 NewEV->insertInto(Pred, NewRet->getIterator());
1447 Op = NewEV;
1448 }
1449 }
1450
1451 if (PHINode *PN = dyn_cast<PHINode>(V)) {
1452 if (PN->getParent() == BB) {
1453 if (NewEV) {
1454 NewEV->setOperand(0, PN->getIncomingValueForBlock(Pred));
1455 } else if (NewBC)
1456 NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));
1457 else
1458 Op = PN->getIncomingValueForBlock(Pred);
1459 }
1460 }
1461 }
1462
1463 // Update any PHI nodes in the returning block to realize that we no
1464 // longer branch to them.
1465 BB->removePredecessor(Pred);
1466 UncondBranch->eraseFromParent();
1467
1468 if (DTU)
1469 DTU->applyUpdates({{DominatorTree::Delete, Pred, BB}});
1470
1471 return cast<ReturnInst>(NewRet);
1472}
1473
1475 Instruction *SplitBefore,
1476 bool Unreachable,
1477 MDNode *BranchWeights,
1478 DomTreeUpdater *DTU, LoopInfo *LI,
1479 BasicBlock *ThenBlock) {
1481 BasicBlock *Head = SplitBefore->getParent();
1482 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
1483 if (DTU) {
1484 SmallPtrSet<BasicBlock *, 8> UniqueSuccessorsOfHead;
1485 Updates.push_back({DominatorTree::Insert, Head, Tail});
1486 Updates.reserve(Updates.size() + 2 * succ_size(Tail));
1487 for (BasicBlock *SuccessorOfHead : successors(Tail))
1488 if (UniqueSuccessorsOfHead.insert(SuccessorOfHead).second) {
1489 Updates.push_back({DominatorTree::Insert, Tail, SuccessorOfHead});
1490 Updates.push_back({DominatorTree::Delete, Head, SuccessorOfHead});
1491 }
1492 }
1493 Instruction *HeadOldTerm = Head->getTerminator();
1494 LLVMContext &C = Head->getContext();
1495 Instruction *CheckTerm;
1496 bool CreateThenBlock = (ThenBlock == nullptr);
1497 if (CreateThenBlock) {
1498 ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
1499 if (Unreachable)
1500 CheckTerm = new UnreachableInst(C, ThenBlock);
1501 else {
1502 CheckTerm = BranchInst::Create(Tail, ThenBlock);
1503 if (DTU)
1504 Updates.push_back({DominatorTree::Insert, ThenBlock, Tail});
1505 }
1506 CheckTerm->setDebugLoc(SplitBefore->getDebugLoc());
1507 } else
1508 CheckTerm = ThenBlock->getTerminator();
1509 BranchInst *HeadNewTerm =
1510 BranchInst::Create(/*ifTrue*/ ThenBlock, /*ifFalse*/ Tail, Cond);
1511 if (DTU)
1512 Updates.push_back({DominatorTree::Insert, Head, ThenBlock});
1513 HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
1514 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
1515
1516 if (DTU)
1517 DTU->applyUpdates(Updates);
1518
1519 if (LI) {
1520 if (Loop *L = LI->getLoopFor(Head)) {
1521 L->addBasicBlockToLoop(ThenBlock, *LI);
1522 L->addBasicBlockToLoop(Tail, *LI);
1523 }
1524 }
1525
1526 return CheckTerm;
1527}
1528
1530 Instruction **ThenTerm,
1531 Instruction **ElseTerm,
1532 MDNode *BranchWeights,
1533 DomTreeUpdater *DTU) {
1534 BasicBlock *Head = SplitBefore->getParent();
1535
1536 SmallPtrSet<BasicBlock *, 8> UniqueOrigSuccessors;
1537 if (DTU)
1538 UniqueOrigSuccessors.insert(succ_begin(Head), succ_end(Head));
1539
1540 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
1541 Instruction *HeadOldTerm = Head->getTerminator();
1542 LLVMContext &C = Head->getContext();
1543 BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
1544 BasicBlock *ElseBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
1545 *ThenTerm = BranchInst::Create(Tail, ThenBlock);
1546 (*ThenTerm)->setDebugLoc(SplitBefore->getDebugLoc());
1547 *ElseTerm = BranchInst::Create(Tail, ElseBlock);
1548 (*ElseTerm)->setDebugLoc(SplitBefore->getDebugLoc());
1549 BranchInst *HeadNewTerm =
1550 BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/ElseBlock, Cond);
1551 HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
1552 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
1553 if (DTU) {
1555 Updates.reserve(4 + 2 * UniqueOrigSuccessors.size());
1556 for (BasicBlock *Succ : successors(Head)) {
1557 Updates.push_back({DominatorTree::Insert, Head, Succ});
1558 Updates.push_back({DominatorTree::Insert, Succ, Tail});
1559 }
1560 for (BasicBlock *UniqueOrigSuccessor : UniqueOrigSuccessors)
1561 Updates.push_back({DominatorTree::Insert, Tail, UniqueOrigSuccessor});
1562 for (BasicBlock *UniqueOrigSuccessor : UniqueOrigSuccessors)
1563 Updates.push_back({DominatorTree::Delete, Head, UniqueOrigSuccessor});
1564 DTU->applyUpdates(Updates);
1565 }
1566}
1567
1568std::pair<Instruction*, Value*>
1570 BasicBlock *LoopPred = SplitBefore->getParent();
1571 BasicBlock *LoopBody = SplitBlock(SplitBefore->getParent(), SplitBefore);
1572 BasicBlock *LoopExit = SplitBlock(SplitBefore->getParent(), SplitBefore);
1573
1574 auto *Ty = End->getType();
1575 auto &DL = SplitBefore->getModule()->getDataLayout();
1576 const unsigned Bitwidth = DL.getTypeSizeInBits(Ty);
1577
1578 IRBuilder<> Builder(LoopBody->getTerminator());
1579 auto *IV = Builder.CreatePHI(Ty, 2, "iv");
1580 auto *IVNext =
1581 Builder.CreateAdd(IV, ConstantInt::get(Ty, 1), IV->getName() + ".next",
1582 /*HasNUW=*/true, /*HasNSW=*/Bitwidth != 2);
1583 auto *IVCheck = Builder.CreateICmpEQ(IVNext, End,
1584 IV->getName() + ".check");
1585 Builder.CreateCondBr(IVCheck, LoopExit, LoopBody);
1586 LoopBody->getTerminator()->eraseFromParent();
1587
1588 // Populate the IV PHI.
1589 IV->addIncoming(ConstantInt::get(Ty, 0), LoopPred);
1590 IV->addIncoming(IVNext, LoopBody);
1591
1592 return std::make_pair(LoopBody->getFirstNonPHI(), IV);
1593}
1594
1596 Type *IndexTy, Instruction *InsertBefore,
1597 std::function<void(IRBuilderBase&, Value*)> Func) {
1598
1599 IRBuilder<> IRB(InsertBefore);
1600
1601 if (EC.isScalable()) {
1602 Value *NumElements = IRB.CreateElementCount(IndexTy, EC);
1603
1604 auto [BodyIP, Index] =
1605 SplitBlockAndInsertSimpleForLoop(NumElements, InsertBefore);
1606
1607 IRB.SetInsertPoint(BodyIP);
1608 Func(IRB, Index);
1609 return;
1610 }
1611
1612 unsigned Num = EC.getFixedValue();
1613 for (unsigned Idx = 0; Idx < Num; ++Idx) {
1614 IRB.SetInsertPoint(InsertBefore);
1615 Func(IRB, ConstantInt::get(IndexTy, Idx));
1616 }
1617}
1618
1620 Value *EVL, Instruction *InsertBefore,
1621 std::function<void(IRBuilderBase &, Value *)> Func) {
1622
1623 IRBuilder<> IRB(InsertBefore);
1624 Type *Ty = EVL->getType();
1625
1626 if (!isa<ConstantInt>(EVL)) {
1627 auto [BodyIP, Index] = SplitBlockAndInsertSimpleForLoop(EVL, InsertBefore);
1628 IRB.SetInsertPoint(BodyIP);
1629 Func(IRB, Index);
1630 return;
1631 }
1632
1633 unsigned Num = cast<ConstantInt>(EVL)->getZExtValue();
1634 for (unsigned Idx = 0; Idx < Num; ++Idx) {
1635 IRB.SetInsertPoint(InsertBefore);
1636 Func(IRB, ConstantInt::get(Ty, Idx));
1637 }
1638}
1639
1641 BasicBlock *&IfFalse) {
1642 PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());
1643 BasicBlock *Pred1 = nullptr;
1644 BasicBlock *Pred2 = nullptr;
1645
1646 if (SomePHI) {
1647 if (SomePHI->getNumIncomingValues() != 2)
1648 return nullptr;
1649 Pred1 = SomePHI->getIncomingBlock(0);
1650 Pred2 = SomePHI->getIncomingBlock(1);
1651 } else {
1652 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1653 if (PI == PE) // No predecessor
1654 return nullptr;
1655 Pred1 = *PI++;
1656 if (PI == PE) // Only one predecessor
1657 return nullptr;
1658 Pred2 = *PI++;
1659 if (PI != PE) // More than two predecessors
1660 return nullptr;
1661 }
1662
1663 // We can only handle branches. Other control flow will be lowered to
1664 // branches if possible anyway.
1665 BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());
1666 BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());
1667 if (!Pred1Br || !Pred2Br)
1668 return nullptr;
1669
1670 // Eliminate code duplication by ensuring that Pred1Br is conditional if
1671 // either are.
1672 if (Pred2Br->isConditional()) {
1673 // If both branches are conditional, we don't have an "if statement". In
1674 // reality, we could transform this case, but since the condition will be
1675 // required anyway, we stand no chance of eliminating it, so the xform is
1676 // probably not profitable.
1677 if (Pred1Br->isConditional())
1678 return nullptr;
1679
1680 std::swap(Pred1, Pred2);
1681 std::swap(Pred1Br, Pred2Br);
1682 }
1683
1684 if (Pred1Br->isConditional()) {
1685 // The only thing we have to watch out for here is to make sure that Pred2
1686 // doesn't have incoming edges from other blocks. If it does, the condition
1687 // doesn't dominate BB.
1688 if (!Pred2->getSinglePredecessor())
1689 return nullptr;
1690
1691 // If we found a conditional branch predecessor, make sure that it branches
1692 // to BB and Pred2Br. If it doesn't, this isn't an "if statement".
1693 if (Pred1Br->getSuccessor(0) == BB &&
1694 Pred1Br->getSuccessor(1) == Pred2) {
1695 IfTrue = Pred1;
1696 IfFalse = Pred2;
1697 } else if (Pred1Br->getSuccessor(0) == Pred2 &&
1698 Pred1Br->getSuccessor(1) == BB) {
1699 IfTrue = Pred2;
1700 IfFalse = Pred1;
1701 } else {
1702 // We know that one arm of the conditional goes to BB, so the other must
1703 // go somewhere unrelated, and this must not be an "if statement".
1704 return nullptr;
1705 }
1706
1707 return Pred1Br;
1708 }
1709
1710 // Ok, if we got here, both predecessors end with an unconditional branch to
1711 // BB. Don't panic! If both blocks only have a single (identical)
1712 // predecessor, and THAT is a conditional branch, then we're all ok!
1713 BasicBlock *CommonPred = Pred1->getSinglePredecessor();
1714 if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor())
1715 return nullptr;
1716
1717 // Otherwise, if this is a conditional branch, then we can use it!
1718 BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());
1719 if (!BI) return nullptr;
1720
1721 assert(BI->isConditional() && "Two successors but not conditional?");
1722 if (BI->getSuccessor(0) == Pred1) {
1723 IfTrue = Pred1;
1724 IfFalse = Pred2;
1725 } else {
1726 IfTrue = Pred2;
1727 IfFalse = Pred1;
1728 }
1729 return BI;
1730}
1731
1732// After creating a control flow hub, the operands of PHINodes in an outgoing
1733// block Out no longer match the predecessors of that block. Predecessors of Out
1734// that are incoming blocks to the hub are now replaced by just one edge from
1735// the hub. To match this new control flow, the corresponding values from each
1736// PHINode must now be moved a new PHINode in the first guard block of the hub.
1737//
1738// This operation cannot be performed with SSAUpdater, because it involves one
1739// new use: If the block Out is in the list of Incoming blocks, then the newly
1740// created PHI in the Hub will use itself along that edge from Out to Hub.
1741static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock,
1742 const SetVector<BasicBlock *> &Incoming,
1743 BasicBlock *FirstGuardBlock) {
1744 auto I = Out->begin();
1745 while (I != Out->end() && isa<PHINode>(I)) {
1746 auto Phi = cast<PHINode>(I);
1747 auto NewPhi =
1748 PHINode::Create(Phi->getType(), Incoming.size(),
1749 Phi->getName() + ".moved", &FirstGuardBlock->front());
1750 for (auto *In : Incoming) {
1751 Value *V = UndefValue::get(Phi->getType());
1752 if (In == Out) {
1753 V = NewPhi;
1754 } else if (Phi->getBasicBlockIndex(In) != -1) {
1755 V = Phi->removeIncomingValue(In, false);
1756 }
1757 NewPhi->addIncoming(V, In);
1758 }
1759 assert(NewPhi->getNumIncomingValues() == Incoming.size());
1760 if (Phi->getNumOperands() == 0) {
1761 Phi->replaceAllUsesWith(NewPhi);
1762 I = Phi->eraseFromParent();
1763 continue;
1764 }
1765 Phi->addIncoming(NewPhi, GuardBlock);
1766 ++I;
1767 }
1768}
1769
1772
1773// Redirects the terminator of the incoming block to the first guard
1774// block in the hub. The condition of the original terminator (if it
1775// was conditional) and its original successors are returned as a
1776// tuple <condition, succ0, succ1>. The function additionally filters
1777// out successors that are not in the set of outgoing blocks.
1778//
1779// - condition is non-null iff the branch is conditional.
1780// - Succ1 is non-null iff the sole/taken target is an outgoing block.
1781// - Succ2 is non-null iff condition is non-null and the fallthrough
1782// target is an outgoing block.
1783static std::tuple<Value *, BasicBlock *, BasicBlock *>
1785 const BBSetVector &Outgoing) {
1786 assert(isa<BranchInst>(BB->getTerminator()) &&
1787 "Only support branch terminator.");
1788 auto Branch = cast<BranchInst>(BB->getTerminator());
1789 auto Condition = Branch->isConditional() ? Branch->getCondition() : nullptr;
1790
1791 BasicBlock *Succ0 = Branch->getSuccessor(0);
1792 BasicBlock *Succ1 = nullptr;
1793 Succ0 = Outgoing.count(Succ0) ? Succ0 : nullptr;
1794
1795 if (Branch->isUnconditional()) {
1796 Branch->setSuccessor(0, FirstGuardBlock);
1797 assert(Succ0);
1798 } else {
1799 Succ1 = Branch->getSuccessor(1);
1800 Succ1 = Outgoing.count(Succ1) ? Succ1 : nullptr;
1801 assert(Succ0 || Succ1);
1802 if (Succ0 && !Succ1) {
1803 Branch->setSuccessor(0, FirstGuardBlock);
1804 } else if (Succ1 && !Succ0) {
1805 Branch->setSuccessor(1, FirstGuardBlock);
1806 } else {
1807 Branch->eraseFromParent();
1808 BranchInst::Create(FirstGuardBlock, BB);
1809 }
1810 }
1811
1812 assert(Succ0 || Succ1);
1813 return std::make_tuple(Condition, Succ0, Succ1);
1814}
1815// Setup the branch instructions for guard blocks.
1816//
1817// Each guard block terminates in a conditional branch that transfers
1818// control to the corresponding outgoing block or the next guard
1819// block. The last guard block has two outgoing blocks as successors
1820// since the condition for the final outgoing block is trivially
1821// true. So we create one less block (including the first guard block)
1822// than the number of outgoing blocks.
1824 const BBSetVector &Outgoing,
1825 BBPredicates &GuardPredicates) {
1826 // To help keep the loop simple, temporarily append the last
1827 // outgoing block to the list of guard blocks.
1828 GuardBlocks.push_back(Outgoing.back());
1829
1830 for (int i = 0, e = GuardBlocks.size() - 1; i != e; ++i) {
1831 auto Out = Outgoing[i];
1832 assert(GuardPredicates.count(Out));
1833 BranchInst::Create(Out, GuardBlocks[i + 1], GuardPredicates[Out],
1834 GuardBlocks[i]);
1835 }
1836
1837 // Remove the last block from the guard list.
1838 GuardBlocks.pop_back();
1839}
1840
1841/// We are using one integer to represent the block we are branching to. Then at
1842/// each guard block, the predicate was calcuated using a simple `icmp eq`.
1844 const BBSetVector &Incoming, const BBSetVector &Outgoing,
1845 SmallVectorImpl<BasicBlock *> &GuardBlocks, BBPredicates &GuardPredicates) {
1846 auto &Context = Incoming.front()->getContext();
1847 auto FirstGuardBlock = GuardBlocks.front();
1848
1849 auto Phi = PHINode::Create(Type::getInt32Ty(Context), Incoming.size(),
1850 "merged.bb.idx", FirstGuardBlock);
1851
1852 for (auto In : Incoming) {
1853 Value *Condition;
1854 BasicBlock *Succ0;
1855 BasicBlock *Succ1;
1856 std::tie(Condition, Succ0, Succ1) =
1857 redirectToHub(In, FirstGuardBlock, Outgoing);
1858 Value *IncomingId = nullptr;
1859 if (Succ0 && Succ1) {
1860 // target_bb_index = Condition ? index_of_succ0 : index_of_succ1.
1861 auto Succ0Iter = find(Outgoing, Succ0);
1862 auto Succ1Iter = find(Outgoing, Succ1);
1864 std::distance(Outgoing.begin(), Succ0Iter));
1866 std::distance(Outgoing.begin(), Succ1Iter));
1867 IncomingId = SelectInst::Create(Condition, Id0, Id1, "target.bb.idx",
1868 In->getTerminator());
1869 } else {
1870 // Get the index of the non-null successor.
1871 auto SuccIter = Succ0 ? find(Outgoing, Succ0) : find(Outgoing, Succ1);
1873 std::distance(Outgoing.begin(), SuccIter));
1874 }
1875 Phi->addIncoming(IncomingId, In);
1876 }
1877
1878 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
1879 auto Out = Outgoing[i];
1880 auto Cmp = ICmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, Phi,
1882 Out->getName() + ".predicate", GuardBlocks[i]);
1883 GuardPredicates[Out] = Cmp;
1884 }
1885}
1886
1887/// We record the predicate of each outgoing block using a phi of boolean.
1889 const BBSetVector &Incoming, const BBSetVector &Outgoing,
1890 SmallVectorImpl<BasicBlock *> &GuardBlocks, BBPredicates &GuardPredicates,
1891 SmallVectorImpl<WeakVH> &DeletionCandidates) {
1892 auto &Context = Incoming.front()->getContext();
1893 auto BoolTrue = ConstantInt::getTrue(Context);
1894 auto BoolFalse = ConstantInt::getFalse(Context);
1895 auto FirstGuardBlock = GuardBlocks.front();
1896
1897 // The predicate for the last outgoing is trivially true, and so we
1898 // process only the first N-1 successors.
1899 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
1900 auto Out = Outgoing[i];
1901 LLVM_DEBUG(dbgs() << "Creating guard for " << Out->getName() << "\n");
1902
1903 auto Phi =
1905 StringRef("Guard.") + Out->getName(), FirstGuardBlock);
1906 GuardPredicates[Out] = Phi;
1907 }
1908
1909 for (auto *In : Incoming) {
1910 Value *Condition;
1911 BasicBlock *Succ0;
1912 BasicBlock *Succ1;
1913 std::tie(Condition, Succ0, Succ1) =
1914 redirectToHub(In, FirstGuardBlock, Outgoing);
1915
1916 // Optimization: Consider an incoming block A with both successors
1917 // Succ0 and Succ1 in the set of outgoing blocks. The predicates
1918 // for Succ0 and Succ1 complement each other. If Succ0 is visited
1919 // first in the loop below, control will branch to Succ0 using the
1920 // corresponding predicate. But if that branch is not taken, then
1921 // control must reach Succ1, which means that the incoming value of
1922 // the predicate from `In` is true for Succ1.
1923 bool OneSuccessorDone = false;
1924 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
1925 auto Out = Outgoing[i];
1926 PHINode *Phi = cast<PHINode>(GuardPredicates[Out]);
1927 if (Out != Succ0 && Out != Succ1) {
1928 Phi->addIncoming(BoolFalse, In);
1929 } else if (!Succ0 || !Succ1 || OneSuccessorDone) {
1930 // Optimization: When only one successor is an outgoing block,
1931 // the incoming predicate from `In` is always true.
1932 Phi->addIncoming(BoolTrue, In);
1933 } else {
1934 assert(Succ0 && Succ1);
1935 if (Out == Succ0) {
1936 Phi->addIncoming(Condition, In);
1937 } else {
1938 auto Inverted = invertCondition(Condition);
1939 DeletionCandidates.push_back(Condition);
1940 Phi->addIncoming(Inverted, In);
1941 }
1942 OneSuccessorDone = true;
1943 }
1944 }
1945 }
1946}
1947
1948// Capture the existing control flow as guard predicates, and redirect
1949// control flow from \p Incoming block through the \p GuardBlocks to the
1950// \p Outgoing blocks.
1951//
1952// There is one guard predicate for each outgoing block OutBB. The
1953// predicate represents whether the hub should transfer control flow
1954// to OutBB. These predicates are NOT ORTHOGONAL. The Hub evaluates
1955// them in the same order as the Outgoing set-vector, and control
1956// branches to the first outgoing block whose predicate evaluates to true.
1957static void
1959 SmallVectorImpl<WeakVH> &DeletionCandidates,
1960 const BBSetVector &Incoming,
1961 const BBSetVector &Outgoing, const StringRef Prefix,
1962 std::optional<unsigned> MaxControlFlowBooleans) {
1963 BBPredicates GuardPredicates;
1964 auto F = Incoming.front()->getParent();
1965
1966 for (int i = 0, e = Outgoing.size() - 1; i != e; ++i)
1967 GuardBlocks.push_back(
1968 BasicBlock::Create(F->getContext(), Prefix + ".guard", F));
1969
1970 // When we are using an integer to record which target block to jump to, we
1971 // are creating less live values, actually we are using one single integer to
1972 // store the index of the target block. When we are using booleans to store
1973 // the branching information, we need (N-1) boolean values, where N is the
1974 // number of outgoing block.
1975 if (!MaxControlFlowBooleans || Outgoing.size() <= *MaxControlFlowBooleans)
1976 calcPredicateUsingBooleans(Incoming, Outgoing, GuardBlocks, GuardPredicates,
1977 DeletionCandidates);
1978 else
1979 calcPredicateUsingInteger(Incoming, Outgoing, GuardBlocks, GuardPredicates);
1980
1981 setupBranchForGuard(GuardBlocks, Outgoing, GuardPredicates);
1982}
1983
1986 const BBSetVector &Incoming, const BBSetVector &Outgoing,
1987 const StringRef Prefix, std::optional<unsigned> MaxControlFlowBooleans) {
1988 if (Outgoing.size() < 2)
1989 return Outgoing.front();
1990
1992 if (DTU) {
1993 for (auto *In : Incoming) {
1994 for (auto Succ : successors(In))
1995 if (Outgoing.count(Succ))
1996 Updates.push_back({DominatorTree::Delete, In, Succ});
1997 }
1998 }
1999
2000 SmallVector<WeakVH, 8> DeletionCandidates;
2001 convertToGuardPredicates(GuardBlocks, DeletionCandidates, Incoming, Outgoing,
2002 Prefix, MaxControlFlowBooleans);
2003 auto FirstGuardBlock = GuardBlocks.front();
2004
2005 // Update the PHINodes in each outgoing block to match the new control flow.
2006 for (int i = 0, e = GuardBlocks.size(); i != e; ++i)
2007 reconnectPhis(Outgoing[i], GuardBlocks[i], Incoming, FirstGuardBlock);
2008
2009 reconnectPhis(Outgoing.back(), GuardBlocks.back(), Incoming, FirstGuardBlock);
2010
2011 if (DTU) {
2012 int NumGuards = GuardBlocks.size();
2013 assert((int)Outgoing.size() == NumGuards + 1);
2014
2015 for (auto In : Incoming)
2016 Updates.push_back({DominatorTree::Insert, In, FirstGuardBlock});
2017
2018 for (int i = 0; i != NumGuards - 1; ++i) {
2019 Updates.push_back({DominatorTree::Insert, GuardBlocks[i], Outgoing[i]});
2020 Updates.push_back(
2021 {DominatorTree::Insert, GuardBlocks[i], GuardBlocks[i + 1]});
2022 }
2023 Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1],
2024 Outgoing[NumGuards - 1]});
2025 Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1],
2026 Outgoing[NumGuards]});
2027 DTU->applyUpdates(Updates);
2028 }
2029
2030 for (auto I : DeletionCandidates) {
2031 if (I->use_empty())
2032 if (auto Inst = dyn_cast_or_null<Instruction>(I))
2033 Inst->eraseFromParent();
2034 }
2035
2036 return FirstGuardBlock;
2037}
2038
2040 Value *NewCond = PBI->getCondition();
2041 // If this is a "cmp" instruction, only used for branching (and nowhere
2042 // else), then we can simply invert the predicate.
2043 if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {
2044 CmpInst *CI = cast<CmpInst>(NewCond);
2046 } else
2047 NewCond = Builder.CreateNot(NewCond, NewCond->getName() + ".not");
2048
2049 PBI->setCondition(NewCond);
2050 PBI->swapSuccessors();
2051}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
assume Assume Builder
SmallVector< MachineOperand, 4 > Cond
static BasicBlock * SplitBlockPredecessorsImpl(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
static void convertToGuardPredicates(SmallVectorImpl< BasicBlock * > &GuardBlocks, SmallVectorImpl< WeakVH > &DeletionCandidates, const BBSetVector &Incoming, const BBSetVector &Outgoing, const StringRef Prefix, std::optional< unsigned > MaxControlFlowBooleans)
static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB)
Remove redundant instructions within sequences of consecutive dbg.value instructions.
static void calcPredicateUsingBooleans(const BBSetVector &Incoming, const BBSetVector &Outgoing, SmallVectorImpl< BasicBlock * > &GuardBlocks, BBPredicates &GuardPredicates, SmallVectorImpl< WeakVH > &DeletionCandidates)
We record the predicate of each outgoing block using a phi of boolean.
static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB, ArrayRef< BasicBlock * > Preds, BranchInst *BI, bool HasLoopExit)
Update the PHI nodes in OrigBB to include the values coming from NewBB.
static BasicBlock * SplitBlockImpl(BasicBlock *Old, Instruction *SplitPt, DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, const Twine &BBName, bool Before)
static std::tuple< Value *, BasicBlock *, BasicBlock * > redirectToHub(BasicBlock *BB, BasicBlock *FirstGuardBlock, const BBSetVector &Outgoing)
static bool remomveUndefDbgAssignsFromEntryBlock(BasicBlock *BB)
Remove redundant undef dbg.assign intrinsic from an entry block using a forward scan.
static void setupBranchForGuard(SmallVectorImpl< BasicBlock * > &GuardBlocks, const BBSetVector &Outgoing, BBPredicates &GuardPredicates)
static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB, ArrayRef< BasicBlock * > Preds, DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA, bool &HasLoopExit)
Update DominatorTree, LoopInfo, and LCCSA analysis information.
static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock, const SetVector< BasicBlock * > &Incoming, BasicBlock *FirstGuardBlock)
static void calcPredicateUsingInteger(const BBSetVector &Incoming, const BBSetVector &Outgoing, SmallVectorImpl< BasicBlock * > &GuardBlocks, BBPredicates &GuardPredicates)
We are using one integer to represent the block we are branching to.
static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB)
Remove redundant dbg.value instructions using a forward scan.
static void SplitLandingPadPredecessorsImpl(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix1, const char *Suffix2, SmallVectorImpl< BasicBlock * > &NewBBs, DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
static cl::opt< unsigned > MaxDeoptOrUnreachableSuccessorCheckDepth("max-deopt-or-unreachable-succ-check-depth", cl::init(8), cl::Hidden, cl::desc("Set the maximum path length when checking whether a basic block " "is followed by a block that either has a terminating " "deoptimizing call or is terminated with an unreachable"))
BlockVerifier::State From
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
bool End
Definition: ELF_riscv.cpp:464
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
static LVOptions Options
Definition: LVOptions.cpp:25
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
LLVMContext & Context
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static const uint32_t IV[8]
Definition: blake3_impl.h:77
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:152
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:163
iterator begin() const
Definition: ArrayRef.h:151
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:158
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
iterator end()
Definition: BasicBlock.h:328
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:326
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition: BasicBlock.h:384
const LandingPadInst * getLandingPadInst() const
Return the landingpad instruction associated with the landing pad.
Definition: BasicBlock.cpp:525
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:253
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition: BasicBlock.h:507
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:216
const Instruction & front() const
Definition: BasicBlock.h:338
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:105
bool isEntryBlock() const
Return true if this is the entry block of the containing function.
Definition: BasicBlock.cpp:403
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="", bool Before=false)
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:409
const BasicBlock * getUniqueSuccessor() const
Return the successor of this block if it has a unique successor.
Definition: BasicBlock.cpp:330
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:292
const CallInst * getTerminatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize prior to the terminating return in...
Definition: BasicBlock.cpp:180
const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
Definition: BasicBlock.cpp:300
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:322
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:112
const Instruction * getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
Definition: BasicBlock.cpp:223
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:87
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:35
bool isLandingPad() const
Return true if this basic block is a landing pad.
Definition: BasicBlock.cpp:521
bool isEHPad() const
Return true if this basic block is an exception handling block.
Definition: BasicBlock.h:524
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:127
bool canSplitPredecessors() const
Definition: BasicBlock.cpp:378
void splice(BasicBlock::iterator ToIt, BasicBlock *FromBB)
Transfer all instructions from FromBB to this basic block at ToIt.
Definition: BasicBlock.h:480
const Instruction & back() const
Definition: BasicBlock.h:340
void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
Definition: BasicBlock.cpp:349
This class represents a no-op cast from one type to another.
Conditional or Unconditional Branch instruction.
void setCondition(Value *V)
void swapSuccessors()
Swap the successors of this branch instruction.
bool isConditional() const
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
BasicBlock * getSuccessor(unsigned i) const
bool isUnconditional() const
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
Value * getCondition() const
static CleanupPadInst * Create(Value *ParentPad, ArrayRef< Value * > Args=std::nullopt, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static CleanupReturnInst * Create(Value *CleanupPad, BasicBlock *UnwindBB=nullptr, Instruction *InsertBefore=nullptr)
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:701
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
Definition: InstrTypes.h:804
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:825
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:833
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:888
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:840
DWARF expression.
This represents the llvm.dbg.assign instruction.
This represents the llvm.dbg.value instruction.
Identifies a unique instance of a variable.
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:151
iterator end()
Definition: DenseMap.h:84
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
iterator_range< iterator > children()
NodeT * getBlock() const
void flush()
Apply all pending updates to available trees and flush all BasicBlocks awaiting deletion.
void recalculate(Function &F)
Notify DTU that the entry block was replaced.
bool hasDomTree() const
Returns true if it holds a DominatorTree.
void applyUpdates(ArrayRef< DominatorTree::UpdateType > Updates)
Submit updates to all available trees.
void deleteBB(BasicBlock *DelBB)
Delete DelBB.
DominatorTree & getDomTree()
Flush DomTree updates and return DomTree.
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
void splitBlock(NodeT *NewBB)
splitBlock - BB is split and now it has one successor.
DomTreeNodeBase< NodeT > * setNewRoot(NodeT *BB)
Add a new node to the forward dominator tree and make it a new root.
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:321
This instruction extracts a struct member or array element value from an aggregate value.
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:652
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:94
Value * CreateElementCount(Type *DstType, ElementCount EC)
Create an expression which evaluates to the number of elements in EC at runtime.
Definition: IRBuilder.cpp:108
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:180
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2564
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:365
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:70
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:700
const BasicBlock * getParent() const
Definition: Instruction.h:90
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:275
bool mayHaveSideEffects() const LLVM_READONLY
Return true if the instruction may have side effects.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1521
bool isExceptionalTerminator() const
Definition: Instruction.h:178
SymbolTableList< Instruction >::iterator insertInto(BasicBlock *ParentBB, SymbolTableList< Instruction >::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
Definition: Instruction.cpp:98
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:82
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:362
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
The landingpad instruction holds all of the information necessary to generate correct exception handl...
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
unsigned getLoopDepth() const
Return the nesting level of this loop.
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
bool isLoopHeader(const BlockT *BB) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:47
Metadata node.
Definition: Metadata.h:950
Provides a lazy, caching interface for making common memory aliasing information queries,...
void invalidateCachedPredecessors()
Clears the PredIteratorCache info.
void removeInstruction(Instruction *InstToRemove)
Removes an instruction from the dependence analysis, updating the dependence of instructions that pre...
MemorySSA * getMemorySSA() const
Get handle on MemorySSA.
void moveAllAfterSpliceBlocks(BasicBlock *From, BasicBlock *To, Instruction *Start)
From block was spliced into From and To.
void applyUpdates(ArrayRef< CFGUpdate > Updates, DominatorTree &DT, bool UpdateDTFirst=false)
Apply CFG updates, analogous with the DT edge updates.
void moveAllAfterMergeBlocks(BasicBlock *From, BasicBlock *To, Instruction *Start)
From block was merged into To.
void moveToPlace(MemoryUseOrDef *What, BasicBlock *BB, MemorySSA::InsertionPlace Where)
void wireOldPredecessorsToNewImmediatePredecessor(BasicBlock *Old, BasicBlock *New, ArrayRef< BasicBlock * > Preds, bool IdenticalEdgesWereMerged=true)
A new empty BasicBlock (New) now branches directly to Old.
void verifyMemorySSA(VerificationLevel=VerificationLevel::Fast) const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
Definition: MemorySSA.cpp:1862
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
Definition: MemorySSA.h:717
Class that has the common methods + fields of memory uses/defs.
Definition: MemorySSA.h:252
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:398
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1743
Return a value (possibly void), from a function.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
A vector that has set insertion semantics.
Definition: SetVector.h:51
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:88
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition: SetVector.h:219
const value_type & front() const
Return the first element of the SetVector.
Definition: SetVector.h:133
const value_type & back() const
Return the last element of the SetVector.
Definition: SetVector.h:139
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition: SetVector.h:93
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition: DenseSet.h:290
size_type size() const
Definition: SmallPtrSet.h:93
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:344
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false.
Definition: SmallPtrSet.h:379
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:383
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:365
iterator begin() const
Definition: SmallPtrSet.h:403
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:389
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
void reserve(size_type N)
Definition: SmallVector.h:667
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Provides information about what library functions are available for the current target.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:17
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static IntegerType * getInt1Ty(LLVMContext &C)
static IntegerType * getInt32Ty(LLVMContext &C)
bool isTokenTy() const
Return true if this is 'token'.
Definition: Type.h:226
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1724
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
op_range operands()
Definition: User.h:242
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:378
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:535
bool use_empty() const
Definition: Value.h:344
bool hasName() const
Definition: Value.h:261
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:384
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition: DenseSet.h:185
self_iterator getIterator()
Definition: ilist_node.h:82
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Tail
Attemps to make calls as fast as possible while guaranteeing that tail call optimization can always b...
Definition: CallingConv.h:76
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
AssignmentInstRange getAssignmentInsts(DIAssignID *ID)
Return a range of instructions (typically just one) that have ID as an attachment.
Definition: DebugInfo.cpp:1716
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI, Instruction *I)
Replace the instruction specified by BI with the instruction specified by I.
iterator_range< df_ext_iterator< T, SetTy > > depth_first_ext(const T &G, SetTy &S)
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:102
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1839
bool RemoveRedundantDbgInstrs(BasicBlock *BB)
Try to remove redundant dbg.value instructions from given basic block.
bool succ_empty(const Instruction *I)
Definition: CFG.h:255
bool IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB)
Check if we can prove that all paths starting from this block converge to a block that either has a @...
BranchInst * GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, BasicBlock *&IfFalse)
Check whether BB is the merge point of a if-region.
unsigned GetSuccessorNumber(const BasicBlock *BB, const BasicBlock *Succ)
Search for the specified successor of basic block BB and return its position in the terminator instru...
Definition: CFG.cpp:79
void detachDeadBlocks(ArrayRef< BasicBlock * > BBs, SmallVectorImpl< DominatorTree::UpdateType > *Updates, bool KeepOneInputPHIs=false)
Replace contents of every block in BBs with single unreachable instruction.
auto successors(const MachineBasicBlock *BB)
ReturnInst * FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, BasicBlock *Pred, DomTreeUpdater *DTU=nullptr)
This method duplicates the specified return instruction into a predecessor which ends in an unconditi...
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:99
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock * > &NewBBs, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method transforms the landing pad, OrigBB, by introducing two new basic blocks into the function...
BasicBlock * splitBlockBefore(BasicBlock *Old, Instruction *SplitPt, DomTreeUpdater *DTU, LoopInfo *LI, MemorySSAUpdater *MSSAU, const Twine &BBName="")
Split the specified block at the specified instruction SplitPt.
void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified block, which must have no predecessors.
void ReplaceInstWithValue(BasicBlock::iterator &BI, Value *V)
Replace all uses of an instruction (specified by BI) with a value, then remove and delete the origina...
void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, Instruction **ThenTerm, Instruction **ElseTerm, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr)
SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, but also creates the ElseBlock...
BasicBlock * SplitKnownCriticalEdge(Instruction *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions(), const Twine &BBName="")
If it is known that an edge is critical, SplitKnownCriticalEdge can be called directly,...
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:112
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1826
bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Examine each PHI in the given block and delete it if it is dead.
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:511
void InvertBranch(BranchInst *PBI, IRBuilderBase &Builder)
bool EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete all basic blocks from F that are not reachable from its entry node.
bool MergeBlockSuccessorsIntoGivenBlocks(SmallPtrSetImpl< BasicBlock * > &MergeBlocks, Loop *L=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr)
Merge block(s) sucessors, if possible.
void SplitBlockAndInsertForEachLane(ElementCount EC, Type *IndexTy, Instruction *InsertBefore, std::function< void(IRBuilderBase &, Value *)> Func)
Utility function for performing a given action on each lane of a vector with EC elements.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:109
BasicBlock * ehAwareSplitEdge(BasicBlock *BB, BasicBlock *Succ, LandingPadInst *OriginalPad=nullptr, PHINode *LandingPadReplacement=nullptr, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions(), const Twine &BBName="")
Split the edge connect the specficed blocks in the case that Succ is an Exception Handling Block.
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
Definition: SmallVector.h:1303
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition: MemorySSA.cpp:89
void createPHIsForSplitLoopExit(ArrayRef< BasicBlock * > Preds, BasicBlock *SplitBB, BasicBlock *DestBB)
When a loop exit edge is split, LCSSA form may require new PHIs in the new exit block.
bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr, bool PredecessorWithTwoSuccessors=false, DominatorTree *DT=nullptr)
Attempts to merge a block into its predecessor, if possible.
bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
Definition: DebugInfo.cpp:2195
BasicBlock * CreateControlFlowHub(DomTreeUpdater *DTU, SmallVectorImpl< BasicBlock * > &GuardBlocks, const SetVector< BasicBlock * > &Predecessors, const SetVector< BasicBlock * > &Successors, const StringRef Prefix, std::optional< unsigned > MaxControlFlowBooleans=std::nullopt)
Given a set of incoming and outgoing blocks, create a "hub" such that every edge from an incoming blo...
std::pair< Instruction *, Value * > SplitBlockAndInsertSimpleForLoop(Value *End, Instruction *SplitBefore)
Insert a for (int i = 0; i < End; i++) loop structure (with the exception that End is assumed > 0,...
BasicBlock * SplitCriticalEdge(Instruction *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions(), const Twine &BBName="")
If this edge is a critical edge, insert a new node to split the critical edge.
bool FoldSingleEntryPHINodes(BasicBlock *BB, MemoryDependenceResults *MemDep=nullptr)
We know that BB has one predecessor.
bool isCriticalEdge(const Instruction *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.
Definition: CFG.cpp:95
unsigned SplitAllCriticalEdges(Function &F, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
Loop over all of the edges in the CFG, breaking critical edges as they are found.
void updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred, BasicBlock *NewPred, PHINode *Until=nullptr)
Replaces all uses of OldPred with the NewPred block in all PHINodes in a block.
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1976
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is an effectively dead PHI node, due to being a def-use chain of single-use no...
Definition: Local.cpp:637
unsigned succ_size(const MachineBasicBlock *BB)
Value * invertCondition(Value *Condition)
Invert the given true/false value, possibly reusing an existing copy.
Definition: Local.cpp:3535
void DeleteDeadBlocks(ArrayRef< BasicBlock * > BBs, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified blocks from BB.
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the edge connecting the specified blocks, and return the newly created basic block between From...
void setUnwindEdgeTo(Instruction *TI, BasicBlock *Succ)
Sets the unwind edge of an instruction to a particular successor.
unsigned pred_size(const MachineBasicBlock *BB)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
Option class for critical edge splitting.
CriticalEdgeSplittingOptions & setPreserveLCSSA()